{"slug": "better-bots-via-hooks", "title": "Better Bots via Hooks", "summary": "Developers can enforce coding conventions and prevent unwanted commits by using git pre-commit hooks, which run automated checks before allowing a commit. This approach reduces the need to embed strict rules in agent instructions and helps bots follow guidelines, though agents may bypass hooks with --no-verify.", "body_md": "It can be tedious trying to get your bot to do *exactly* what you want to do,\nbut it’s not always necessary. If there are hard conventions that you need to\nenforce (like tidying, not committing certain kinds of changes), try enforcing\nthese at the `git`\n\npre-commit hook level. The advantage of this is that you\ndon’t need to go through contortions to put certain kinds of requirements in\n`AGENT.md`\n\nfiles, skill instructions etc. If you let your agent commit your\ncode, the pre-commit hook can prevent them from committing until they play by\nthe rules.\n\n\"[Captain Hook Topiary at Epcot Flower & Garden Festival 2014](https://www.flickr.com/photos/64441474@N06/13642118603)\" by [Austin Kirk](https://www.flickr.com/photos/aukirk/) is licensed under [CC BY 2.0 ](https://creativecommons.org/licenses/by/2.0/?ref=openverse).\n\n## Caveat Emptor[#](#caveat-emptor)\n\nIt’s always possible that your agent decides to commit with `--no-verify`\n\nor\neven deletes your hooks, so consider this part of a belt and suspenders\napproach to getting the results you want.\n\n## Show Me the Hooks[#](#show-me-the-hooks)\n\nHere’s an example of a `pre-commit`\n\nhook that I’m using for\n[https://mymindisracing.com](https://mymindisracing.com). It\n\n- prevents commits to\n`main`\n\n- prevents updates to a scaffolding file that I keep under version control but don’t want changed\n- enforces tidying and linting:\n\n``` bash\n#!/bin/sh\n# Pre-commit hook to run precious lint on staged files\n\n# Check if we're on main branch\nbranch=$(git symbolic-ref --short HEAD 2>/dev/null)\nif [ \"$branch\" = \"main\" ]; then\n    echo \"ERROR: Direct commits to 'main' branch are not allowed.\"\n    echo \"Please create a feature branch instead:\"\n    echo \"  git checkout -b feature/your-feature-name\"\n    exit 1\nfi\n\n# Block commits that include .serena/project.yml (tool artifact, never commit)\nif git diff --cached --name-only | grep -q '^\\.serena/project\\.yml$'; then\n    echo \"ERROR: .serena/project.yml is staged for commit.\"\n    echo \"This file is a local tool artifact and should not be committed.\"\n    echo \"  git reset HEAD .serena/project.yml\"\n    exit 1\nfi\n\n# Run precious lint on staged files\nprecious lint -q --staged\n\n# Capture the exit code\nRESULT=$?\n\n# If precious lint failed, prevent the commit\nif [ $RESULT -ne 0 ]; then\n    echo \"pre-commit hook failed: precious lint found issues with staged files\"\n    echo \"Please run 'precious tidy -q --staged' and try again\"\n    exit 1\nfi\n\nexit 0\n```\n\nAgents can do a lot of things really well, but they do tend to perform better when given guardrails. Adding this kind of basic safeguard can save a lot of tedium in your day to day work.", "url": "https://wpnews.pro/news/better-bots-via-hooks", "canonical_source": "https://www.olafalders.com/2026/06/15/better-bots-via-hooks/", "published_at": "2026-06-15 00:00:00+00:00", "updated_at": "2026-06-29 18:53:58.955011+00:00", "lang": "en", "topics": ["developer-tools", "ai-agents"], "entities": ["git", "precious"], "alternates": {"html": "https://wpnews.pro/news/better-bots-via-hooks", "markdown": "https://wpnews.pro/news/better-bots-via-hooks.md", "text": "https://wpnews.pro/news/better-bots-via-hooks.txt", "jsonld": "https://wpnews.pro/news/better-bots-via-hooks.jsonld"}}