Mechanism: Hooks & Automation
One-sentence definition
Hooks and automation connect agent work to events — save, commit, PR open, CI fail — so verify and fix loops run reliably instead of only when you remember to paste test output.
The problem
You ask the agent to run tests after every edit. It forgets. You manually loop. Or CI fails at night with no structured handoff back to an agent. Automation gap means human glue carries every repetition.
Hooks turn repeated vibe patterns into pipelines with termination conditions.
Symptoms:
- Same manual verify ritual every session
- Agent claims it ran tests; CI disagrees
- No agent step between local pass and PR
- Fear of auto-run agents without guardrails
How it works
Event (save · commit · PR · schedule)
│
▼
Hook / workflow step
│
├── run verify command
├── optional: invoke agent with fixed prompt
└── stop on pass or cap ([termination](/mechanism-termination-condition/))Safe automation stack:
- Auto verify only (tests, lint) — no agent
- Agent on failed verify with goal + diff + log
- Human checkpoint before auto-commit (rare; high risk)
Cursor hooks: format, test, custom scripts on file or session events.
When to use it
- Repeated verify loops on same project
- CI failures that fit a fix template
- Formatting/lint fixes with clear stop rules
- Team standardization of post-edit checks
When not to use it
- Auto-agent on every save without constraints — runaway diffs
- Long-running agent in hook with no timeout cap
- Secrets in hook logs sent to cloud agents
Failure modes
Unbounded auto-agent — Edits forever on hook. Fix: Max iterations + verify-only default.
Hook without verify — Agent edits, no test. Fix: Verify command exit code drives flow.
Silent hook failure — Tests fail; you do not notice. Fix: Visible notification or CI gate.
Wrong event — Agent on every keystroke. Fix: Debounce; run on save or pre-commit.
Prod hook — Auto-deploy on agent pass. Fix: Playground + human merge.
Minimal example
Context: Health route project with pre-commit tests.
Steps:
- Hook: on
git commit, runpnpm test -- health.test.ts. - Optional second hook script: if fail, write log to
.agent-last-test.logfor chat paste. - Agent session reads log via diff-as-context — not auto-fix without you.
Done when: You never commit a failing health test by accident.
Tool instances (optional deep-dive)
Portable idea above; this section is tool-specific. Date: June 2026.
Cursor
- Hooks (
hooks.json): afterFileEdit, beforeSubmitPrompt, sessionEnd — run verify scripts. - Pair hooks with rules: “Read .agent-last-test.log if present.”
- Do not auto-run full Agent without approval unless sandboxed.
Other tools
Git pre-commit hooks, GitHub Actions, n8n — event → verify → optional agent step.
Related mechanisms
- Verify loop — what hooks often automate
- Loop — hooks as outer loop driver
- Termination condition — caps on automated runs
Try it yourself
Exercise: Add one verify-only hook (save or pre-commit) for a single test file. No agent yet.
Done when: A failing test blocks commit or notifies you automatically.

