Customizing your agent and repository
Week 4 · Instruction files, hook gates and subagent roles
The failure comes first
An agent changes a parser and also edits the lockfile.
What rule would have predicted and prevented that failure?
One rule, one failure, one check
| Failure | Rule | Check |
|---|---|---|
| Lockfile changes with parser | Keep dependency files out of parser fixes | staged path list |
| Test is omitted | Name and run the focused test | command output |
| Path escapes checkout | Stop on an outside path | negative path case |
An instruction file is scoped context
AGENTS.md can state:
Before editing: read the target and its tests.
After editing: run the focused test.
Stop: do not change lockfiles for this task.
The rule is readable. It is not yet a Git permission boundary.
Request versus gate
Request: “Run the focused test and report it.”
Gate: “At before_commit, exit 2 when a staged Python file has an unused import.”
A request can be skipped. Git still runs the gate.
Generate, then verify
Generated proposal: add import json to report.py.
Verification: stage it, commit and read the nonzero hook result. Remove the import,
commit again and read the PASS result.
The first record proves the gate stopped that input. The second proves the clean path passed.
A hook is a narrow invariant
An invariant is a rule that must always hold. A hook should:
- read the staged paths;
- reject malformed input and protected paths;
- return a nonzero status for a finding;
- avoid importing or executing the staged file.
It does not replace tests, review or a specification.
Live gate: bad commit
$ git commit -m "practice: blocked import"
src/report.py:1: unused import 'json'
STOP: fix the findings above, then run the hook again
The commit is not created. Save this output as evidence.
Live gate: good commit
$ git commit -m "practice: pass the gate"
PASS: no protected file and no unused import in 1 file(s)
Now inspect git show --stat. A green hook does not verify application behavior.
Hooks on the agent's own events
Before a tool call: the agent has chosen a tool and its arguments; nothing has run.
End of turn: the agent is about to stop and report that it is done.
A hook on either event can refuse. Your agent's documentation names its events.
Before a tool call: refuse the write
step 1 tool call: write_file tests/check_pairs.py
exit 2: the call is refused and no file changes
step 3 tool call: write_file src/pairs.py
exit 0: the call runs and writes src/pairs.py
The rewritten test never reaches the disk. The fix does.
End of turn: not while the test fails
step 2 end of turn: "Done: the parser test passes now."
hook: 1 passed, 1 failed
exit 2: the turn goes on, and the agent receives the hook's message
“Done” is a claim. The hook runs the test before the turn may end.
The two rules depend on each other
Remove the tests/ write rule and replay:
hook: ALLOW end_of_turn: the focused test passes (2 passed, 0 failed)
FAIL: files under tests/ changed; the original focused test fails
The rewritten test passes. The bug stays.
Two gates, two events
| Agent-side hook | Git pre-commit hook | |
|---|---|---|
| Acts | before the change | at the commit |
| Covers | this agent's calls | every author in this clone |
| Misses | what arguments do not show | uncommitted changes, --no-verify |
Keep a rule that matters in both places.
Role is a contract
A subagent role supplies:
- one instruction;
- a small tool allowlist;
- a named hand-back;
- a stop condition.
A label such as “reviewer” supplies none of these by itself.
Researcher role
Instruction: locate the parser and its callers; change nothing.
Tools: read, search, one named read-only test.
Hand-back: research.md with file/function references, command and open questions.
Stop: parser is generated or the named test is absent.
Implementer role
Instruction: change only the named function and preserve its contract.
Tools: read, edit that file, run the named test.
Hand-back: diff, test output and one remaining risk.
The parent opens the diff and decides whether to continue.
Reviewer role
Instruction: judge issue, diff and evidence without editing.
Tools: read the named files and run the named check.
Hand-back: each finding is confirmed, refuted or cannot tell with evidence.
A summary without a location or input is not a finding.
Fresh context, smaller boundary
Pass the implementer only the role contract, the research record and named files.
Pass the reviewer the issue, diff and evidence. Do not pass the implementer's private plan.
A fresh context makes the contract visible; it does not make the reviewer infallible.
Studio output
You will keep:
AGENTS.mdandinstruction-review.md;blocked.txtandpassed.txt;agent-hooks.txtfrom the replay;hook-record.mdwith the commit hash;role-contract.mdandrole-review.md;studio-record.mdending in one decision line: accept, narrow, recheck or stop.
Check the evidence
For every record ask:
- Which exact input did the check see?
- What did the exit status establish?
- Which behavior remains untested?
Do not turn a course-written example or a generated proposal into a real run record.
Technical specification hand-off
Carry the same boundaries into this week's project technical specification:
- instruction scope and failure it prevents;
- hook event, input and exit condition;
- subagent role, tools and hand-back;
- the check that could prove each claim wrong.
Exit decision
Accept only the rule that your records check.
Narrow when a role or gate is too broad.
Recheck when the command or input is missing.
Stop when the boundary cannot be enforced safely.
Studio
Write the rule. Trigger the gate. Read the hand-back.
Then decide from evidence.