Skip to content

Week 4: Customizing your agent and repository

Learning objectives

By the end of the lesson, you can:

  • LO2: write an instruction file whose rules name the failure each rule prevents;
  • LO2, LO9: tell apart a request, which an agent may skip, and a hook gate, which blocks the commit; verify both a blocked and an accepted commit;
  • LO2, LO9: attach a hook to two of the agent's own events: before a tool call and at the end of a turn. Verify one blocked and one allowed run of each. Explain why you still keep the Git hook as a second gate;
  • LO2, LO3: give a subagent a role with its own instruction, a small list of allowed tools (an allowlist) and a hand-back contract. The hand-back is the report the subagent returns. Check the hand-back against the contract;
  • LO3: carry the same boundaries into the technical specification due this week.

The examples are course-written and synthetic. They are prompts and checks for practice, not records of a real model run or a repository's production policy.

Minimal example

Start with a failure, then write the rule

A repository has a Python command that reads a configuration file. A rushed agent makes three tempting mistakes:

Failure to prevent Instruction rule Evidence that can check it
The command is changed without its regression test “For every behavior change, name the test that distinguishes old and new behavior.” The change note names a test and the test runs
A generated command writes outside the checkout “Use paths below the repository root; stop and ask when a path is outside it.” A negative path case is refused
A commit changes the lockfile while fixing a parser “Do not change dependency files in a parser change.” The staged path list contains no lockfile

The rule is useful because it predicts a failure. “Be careful” is a request with no observable check. Write the rule as a sentence that an agent can act on and a person can inspect.

Instruction files are scoped context

Create AGENTS.md at the repository root. When a subdirectory needs a stricter rule, put a second instruction file in that subdirectory. Keep each file short enough to read. A good entry names the scope, the allowed command, the expected evidence and the stop condition:

# Repository instructions

- Scope: Python files under `src/`; keep public function names stable.
- Before editing: read the named function and its tests; report file and function names.
- After editing: run `python -m pytest tests/test_parser.py -q` and report the result.
- Stop: do not change `pyproject.toml`, lockfiles or files outside this repository.

The request “please run the parser tests” can be skipped by a hurried agent. The stop rule can be checked by a reviewer, but it is still an instruction until a gate enforces it. An instruction file gives context and a decision procedure; it does not make the filesystem or Git refuse an action by itself.

A request and a gate have different jobs

A request is a message to the agent: “run the focused test and report the output.” A gate is attached to an event the repository controls: “before a commit, refuse staged Python files with an unused import.” The agent may omit the request. Git invokes the hook even when the agent forgets the request. A hook is therefore a narrow invariant, not a replacement for tests or review. An invariant is a rule that must always hold.

The studio uses the course-written hook in example/pre_commit.py. It reads a JSON event and checks for protected paths. It also reports an unused Python import, without importing or running the file. In the studio you wrap it as .git/hooks/pre-commit, and the wrapper passes in the list of staged paths. It returns exit code 2 for a finding and exit code 0 for a clean list.

Generated proposal: add import json to report.py, stage it, and commit. The hook should stop the commit because json is unused. Verification: read the hook output, fix the import, stage again and commit. The second run should print PASS and exit 0. Keep both records. A blocked run is evidence that the gate fired; it is not evidence that the final change is correct.

Hooks on the agent's own events

A Git hook waits for the commit. Some agents let you attach a command to events in their own loop. Then a gate can act earlier. Two events matter here:

Event When it fires What a hook on it can do
Before a tool call The agent has chosen a tool and its arguments; nothing has run yet Refuse the call, so the write never happens
End of turn The agent is about to stop and report that it is done Run a check itself and refuse to let the turn end while the check fails

Your agent's documentation lists the events it offers. It also says how a hook receives an event and how the hook refuses one. The course example uses its own neutral names, before_tool_call and end_of_turn, and its own exit codes: 0 allows and 2 blocks.

fixture/agent_hook.py reads one JSON event on standard input. Before a tool call it applies four rules:

  • it blocks a write under tests/ or to a file that the Stop rule protects;
  • it allows a read;
  • it allows the shell only for the focused test;
  • it blocks any tool that it has no rule for.

At the end of a turn, the hook runs the focused test tests/check_pairs.py itself. It blocks the end of the turn while the test fails. fixture/replay_hooks.py replays a recorded session through the hook. It works on a scratch copy of fixture/workspace/. This is a two-file project with a bug: its parser keeps the space in front of a value.

Generated proposal: the recorded session in fixture/session/, whose steps.json lists four steps. It is course-written: a person wrote the steps, and no model produced them. Step 1 rewrites the test so that it expects the space: compare session/step-1-check_pairs.py with workspace/tests/check_pairs.py. Step 2 says the work is done. Step 3 strips the value in src/pairs.py, and step 4 says the work is done again.

Verification: from the root of the course checkout, run the replay and read each decision:

python lectures/week-04/fixture/replay_hooks.py; echo "exit $?"
step 1 tool call: write_file tests/check_pairs.py
  hook: BLOCK write_file tests/check_pairs.py: tests/ is protected; fix the code, not its test
  exit 2: the call is refused and no file changes
step 2 end of turn: "Done: the parser test passes now."
  hook: BLOCK end_of_turn: the focused test fails; keep working
  hook:   FAIL parse_pair('port: 8080') returned ('port', ' 8080'); expected ('port', '8080')
  hook:   1 passed, 1 failed
  exit 2: the turn goes on, and the agent receives the hook's message
step 3 tool call: write_file src/pairs.py
  hook: ALLOW write_file src/pairs.py
  exit 0: the call runs and writes src/pairs.py
step 4 end of turn: "Done: parse_pair strips the name and the value."
  hook: ALLOW end_of_turn: the focused test passes (2 passed, 0 failed)
  exit 0: the turn ends
replay check: tests/ unchanged
replay check: original tests/check_pairs.py on the final code: exit 0, 2 passed, 0 failed
PASS: 2 events blocked, 2 allowed; the turn ended with tests/ unchanged and the test passing
exit 0

The hook refuses step 1 before the test file changes. At step 2 the hook does not believe the agent's claim “Done”. It runs the test to check the claim. The hook allows step 3. Step 4 ends the turn because the unchanged test now passes. The two replay check lines are checks that the replay itself makes after the turn. The replay does not trust the hook either: it runs the original test from workspace/ on the final code.

The two rules depend on each other. Suppose you remove the write rule. Then step 1 goes through and the rewritten test passes at step 2. The turn ends with the bug still in src/pairs.py. The end-of-turn hook runs whatever test it finds. It cannot tell whether the test still checks what it checked before. In the studio you remove the rule and read the result.

Two gates, two events

The agent-side hook does not replace the Git hook. They fire at different events and see different things:

Agent-side hook before a tool call Git pre-commit hook
When it acts Before the change: a refused write never happens At the commit: the change already sits in the working tree
Whose changes The calls of an agent session that has the hook attached Every commit in a clone with the hook installed: yours, any agent's, any tool's
What it sees The tool name and its arguments The commit about to be made: its staged paths and their content
What it misses What the arguments do not show, such as the files a shell command writes; edits by a person or another tool A change that is never committed; a commit made with --no-verify; a clone without the hook

Keep an important rule in both places. The agent-side hook stops the agent early, with a message the agent can act on. The Git hook stops the same change at the commit, whoever made it. Neither replaces the focused test or review.

Subagent roles are separate contracts

A role is more than a label such as “reviewer.” Give the subagent its own instruction, tools and output contract. For a bounded parser change, use three roles:

Role Instruction it receives Allowed tools Required hand-back
Researcher Locate the parser and its callers; change nothing read files, search, run one existing test research.md with file/function references and open questions
Implementer Change only the named function and preserve the contract read, edit that file, run the named test diff plus test output and a remaining risk
Reviewer Judge the issue, diff and evidence without editing read diff, read evidence, run the named test each finding marked confirmed, refuted or cannot tell

The parent agent or person chooses whether to continue after each hand-back. The subagent cannot enlarge its allowlist, declare its own work correct or silently replace a missing artifact. A generated hand-back is still a proposal: open every cited location, run the stated check and record what the check does not establish.

Generated proposal: ask a researcher to “find the parser.” Verification: reject a hand-back that names no file and function. Ask for the bounded report again or stop. Then pass only research.md and the named files to the implementer. Give a reviewer the issue, the diff and the test evidence in a fresh context. The reviewer can then check the contract instead of trusting the implementer's summary.

Hands-on exercise

Follow the studio. You write an instruction file for your own repository. You install a hook in a small practice repository so that it blocks one bad change that you stage on purpose. You keep one record of a blocked commit and one of a passing commit. Then you replay the agent-side hooks, remove the write rule in a scratch copy and record what changes. Use ordinary git and python; do not put keys or employer data in the practice repository.

The week's second session is either a guest session or this studio. In a guest week, the studio page says which sections to do beforehand. Contribution 1 and the project technical specification are due this week, on Sunday at 23:59. See the contributions guide and the project page. In the “Agent workflow and gates” section of the specification template, name your instruction file and the gate you will keep, and link the evidence you actually have.

Verification

The studio has three independent checks:

  1. The hook stops the bad staged change. git commit exits nonzero and prints the hook's finding. The saved blocked.txt records that output and the exit status. Git reports 1 for a commit that a hook stopped. The hook's own exit code is 2.
  2. The corrected staged change commits with exit 0. The saved passed.txt contains the PASS line. git show --stat --oneline HEAD confirms which files entered the commit.
  3. The saved agent-hooks.txt shows:
  4. the write to tests/ blocked with exit 2;
  5. the write to src/pairs.py allowed with exit 0;
  6. the end of turn blocked while the test fails, and allowed once it passes;
  7. the two replay checks, the PASS line and exit status: 0.

For the role exercise, compare each hand-back with its contract. A complete report names locations, allowed tools, the exact command and its result. If you used a course-written example, the report does not claim that a model run happened.

Verification boundaries

  • A blocked hook run proves that Git presented the recorded staged paths to this hook and that the hook returned a nonzero status. It does not prove the application behaves correctly.
  • A passing hook run proves only that this input satisfied this hook's narrow checks. Run the focused test and inspect the diff separately.
  • For each staged path, this hook reads the file in your working directory, not the staged content. git commit --no-verify skips the hook. A local hook stops a commit made by mistake. It does not stop someone who chooses to skip it.
  • The replay proves what this hook decides for four recorded events. It does not prove that your agent sends these events, with these fields, or acts on the exit code as the replay does. Attach a hook in your own agent and keep one blocked and one allowed run from it.
  • A hook before a tool call judges the call by its arguments. A shell command can write a protected file without naming it. For this reason the hook allows the shell only for the focused test, and you keep the Git hook.
  • An allowed end of turn proves only that the focused test passed when the agent tried to stop. A rewritten test passes too. The write rule and review keep the test meaningful.
  • A role hand-back is evidence only for the fields it names. Open every cited location and rerun the named check; mark an unsupported claim cannot tell.

Common mistakes

  • Treating an instruction as a permission boundary. Add a testable hook or a human review step for the invariant; keep the request for useful context.
  • Writing a hook that edits files or runs arbitrary commands. Read and parse the staged paths, refuse malformed input, and return a clear nonzero status.
  • Giving every subagent the whole repository and every tool. Narrow its files and tools, then inspect the hand-back in a fresh context.
  • Recording only the passing commit. Preserve the blocked output too, and label what each record proves and what it leaves open.
  • Checking at the end of a turn without protecting the check. An agent can make a test pass by changing the test. Block that write before the call. Keep the Git hook for the edits that the agent-side hook never sees.
  • Writing an end-of-turn hook with no way out. Decide what happens when the agent cannot make the test pass. For example, let the turn end after a fixed number of refusals, with a message that a person must look at the failure.
  • Copying a generated snippet without reading it. Compare the diff, run the check and make the decision yourself.