Week 4 studio
This 90-minute studio is individual work. Use a disposable copy of your project
repository. Do not place credentials, employer data or private material in it. The
commands below use only ordinary git and python. Section 3 has a written alternative
for a student with no agent at hand.
In a guest week, do sections 1 and 2 before the session. Bring AGENTS.md,
instruction-review.md, blocked.txt, passed.txt, agent-hooks.txt and
hook-record.md. Do sections 3 and 4 after the talk.
Studio sequence
| Minutes | Activity | Evidence to keep |
|---|---|---|
| 0–15 | Name one failure an agent could cause in your repository | failure and the rule that prevents it |
| 15–30 | Write and review AGENTS.md |
instruction file and one peer question |
| 30–50 | Install the hook and make it block a bad commit | blocked.txt with the finding and the nonzero exit status |
| 50–60 | Fix the change and commit it | passed.txt and git show --stat |
| 60–70 | Replay the agent-side hooks, then remove the write rule and replay again | agent-hooks.txt and two sentences in hook-record.md |
| 70–85 | Specify one subagent role and inspect its hand-back | role contract and checked hand-back |
| 85–90 | Decide what to accept or narrow | one decision line with evidence |
You can produce the same outputs with keyboard-only editing and a text record. A partner may read your rule, but each student makes their own decision and keeps their own records.
1. Write the instruction file
From the root of the disposable project copy, create AGENTS.md:
cat > AGENTS.md <<'__INSTRUCTIONS__'
# Repository instructions
- Scope: Python files under `src/`; keep public names stable.
- Before editing: read the target function and its tests; name both in your note.
- After editing: run `python -m pytest tests -q` when the repository has tests.
- Stop: do not change lockfiles or files outside this checkout for this task.
__INSTRUCTIONS__
Replace the paths and the command with ones that exist in your repository. If the project
has no code yet, use the ones your specification plans. Then write a four-row table in
instruction-review.md. Each row gives a rule, the failure it prevents, and the evidence a
reviewer could inspect. Mark any rule that is only a request, because an instruction file
alone cannot enforce it.
2. Install a small hook gate
First, from the root of the course checkout, copy the course-written hook to a temporary path. Keep the same shell open while you work in the disposable project copy:
HOOK_SOURCE="$(mktemp)"
cp lectures/week-04/example/pre_commit.py "$HOOK_SOURCE"
From the root of the disposable project copy, make a small practice repository for the two commits. It is a separate repository inside the copy. This way the hook you install here does not run on your project's own commits:
mkdir -p week04-studio-hook/src
cp "$HOOK_SOURCE" week04-studio-hook/pre_commit.py
cd week04-studio-hook
git init -q
git config user.name "Week 4 practice"
git config user.email "week4@example.edu"
cat > .git/hooks/pre-commit <<'__HOOK__'
#!/usr/bin/env python3
import json
import subprocess
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from pre_commit import run
paths = subprocess.check_output(
["git", "diff", "--cached", "--name-only"], text=True
).splitlines()
event = {"event": "before_commit", "paths": paths}
raise SystemExit(run([json.dumps(event)]))
__HOOK__
chmod +x .git/hooks/pre-commit
The copied hook is course-written teaching code. Read it before using it. It checks staged
paths and returns 2 for a protected path or an unused import. It does not execute the
staged file. Compare its PROTECTED set with the Stop rule you wrote. The set names only
Dockerfile, pyproject.toml and uv.lock at the repository root.
Create the intentionally bad change and capture the failed attempt:
printf 'import json\n\nvalue = 1\n' > src/report.py
git add src/report.py
rc=0
git commit -m "practice: blocked import" > blocked.txt 2>&1 || rc=$?
echo "exit status: $rc" >> blocked.txt
cat blocked.txt
[ "$rc" -ne 0 ] && grep -F "unused import 'json'" blocked.txt
The command must not create the commit. blocked.txt must contain the hook's finding and
exit status: 1. Git reports 1 for a commit that a hook stopped. The hook's own exit code
is 2. If it commits, or the record shows an error in place of the finding, stop and inspect
the wrapper before proceeding; do not rewrite the record to make it look blocked.
Now fix the violation and capture the passing run:
printf 'value = 1\n' > src/report.py
git add src/report.py
git commit -m "practice: pass the gate" > passed.txt 2>&1
cat passed.txt
grep -F "PASS: no protected file" passed.txt
git show --stat --oneline HEAD
Keep blocked.txt, passed.txt, and the commit hash in hook-record.md. Explain that
the hook proves that this staged-path check ran. It does not prove that the Python behavior
is correct. Run the repository's focused test separately when one exists.
These two records come from the practice repository. Its hook sits in .git/hooks/, and
Git does not commit that directory. Your project needs a gate of its own by the alpha in
week 7. Choose one rule in your AGENTS.md that a commit can break. Keep the hook in a
committed directory such as .githooks/. To start from this section's two files:
- Put
pre_commit.pyat the root of the project. - Save the wrapper as
.githooks/pre-commit, executable as before. - In the wrapper, change
parents[2]toparents[1], because the wrapper now sits one directory below the root, not two. If you leave it unchanged, it looks forpre_commit.pyabove the project. Then every commit stops with an import error instead of a finding. - Run
git config core.hooksPath .githooksin the project. Record one blocked and one passing commit there in the same way.
Change pre_commit.py to fit your project before you rely on it. As written, it stops
every commit that does any of these:
- touches
pyproject.toml,uv.lockorDockerfileat the repository root; - deletes a
.pyfile; - stages a
.pyfile under a directory whose name starts with a dot; - stages nothing, such as an amend that only rewords the message.
The dot rule is why pre_commit.py stays at the root: the hook refuses a commit that adds
it under .githooks/.
Replay the agent-side hooks
The notes show a hook on the agent's own events and a replay of four recorded steps. Run the replay yourself on a scratch copy. In a second terminal, from the root of the course checkout:
REPLAY="$(mktemp -d)"
cp -r lectures/week-04/fixture/. "$REPLAY"/
cd "$REPLAY"
rc=0
python replay_hooks.py > agent-hooks.txt 2>&1 || rc=$?
echo "exit status: $rc" >> agent-hooks.txt
cat agent-hooks.txt
agent-hooks.txt must hold the output printed in the notes. It ends with the PASS line
and exit status: 0. Keep it with blocked.txt and passed.txt. Then read the generated
proposal that the replay fed to the hook. Step 1 changes one expected value in the test:
diff workspace/tests/check_pairs.py session/step-1-check_pairs.py; echo "exit $?"
12c12
< ("port: 8080", ("port", "8080")),
---
> ("port: 8080", ("port", " 8080")),
exit 1
Predict the hook's decision for a shell command that edits the test, then run it:
printf '%s\n' '{"event":"before_tool_call","tool":"run_shell","arguments":{"command":"sed -i s/8080/9090/ tests/check_pairs.py"}}' | python agent_hook.py; echo "exit $?"
BLOCK run_shell: only the focused test may run; a command's text does not show what it writes
exit 2
The hook blocks the command because it allows the shell only for the focused test, not
because it found tests/ in the text. A command can reach a file without naming it.
Now remove the write rule. In the scratch copy's agent_hook.py, change the line
PROTECTED_DIRS = ("tests",) to PROTECTED_DIRS = (). Predict which steps run and how the
replay ends, then run it:
python replay_hooks.py; echo "exit $?"
step 1 tool call: write_file tests/check_pairs.py
hook: ALLOW write_file tests/check_pairs.py
exit 0: the call runs and writes tests/check_pairs.py
step 2 end of turn: "Done: the parser test passes now."
hook: ALLOW end_of_turn: the focused test passes (2 passed, 0 failed)
exit 0: the turn ends
turn ended at step 2; steps 3-4 were not replayed
replay check: changed under tests/: tests/check_pairs.py
replay check: original tests/check_pairs.py on the final code: exit 1, 1 passed, 1 failed
FAIL: files under tests/ changed; the original focused test fails
exit 1
In hook-record.md, write two sentences:
- what the end-of-turn hook checked when it let the turn end at step 2;
- one change that the agent-side hook would not see but your Git hook from this section would.
You may change the scratch copy as you like. The course checkout stays as it was.
3. Give a subagent a role
Write role-contract.md with exactly one role. This is an exercise in writing a prompt
and reviewing the result. You may use your coding agent directly. Do not give it
credentials or a command tool without limits.
Role: researcher
Task: locate the function that parses one report and its callers.
Allowed tools: read files, search text, run one named read-only test.
Forbidden: edits, network, dependency installation, commits.
Hand-back: research.md with file/function references, the test command and two open questions.
Stop condition: stop if the parser is generated or the named test is absent.
Run the role where there is code to read. If your project copy already has a function with
a test, replace the task and the named test with yours, as you did in section 1. If it has
no code yet, run the role in week04-studio-hook. Ask for the function in pre_commit.py
that decides whether a path is protected, and for its callers. Name the read-only check
below. It prints a finding, exits 2 and changes no file:
python pre_commit.py '{"event":"before_commit","paths":["pyproject.toml"]}'
Ask for the hand-back using that role contract. Open every cited file and run the named
read-only check yourself. In role-review.md, mark each claim confirmed, refuted
or cannot tell, with the location or output that supports the mark. If you continue to
an implementer role, pass it only role-contract.md, research.md and the named files.
Record its diff and test output separately. A role name alone is not a boundary: it needs
an allowlist and a hand-back contract.
Written alternative. If you have no agent at hand, check the course-written hand-back
below instead of an agent's. It is about pre_commit.py in week04-studio-hook. A person
wrote it, so say so in role-review.md. Do not record it as a model run.
research.md (course-written for teaching; a person wrote it, not an agent)
1. run() in pre_commit.py reports a staged pyproject.toml as protected and exits 2.
Check: python pre_commit.py '{"event":"before_commit","paths":["pyproject.toml"]}'; echo $?
2. unused_imports() in pre_commit.py imports the staged file to find unused names.
3. The gate makes the commit safe.
4. Decision line
End studio-record.md with one line:
Decision: accept | narrow | recheck | stop — evidence: blocked.txt, passed.txt, agent-hooks.txt and role-review.md
Choose the word that matches your evidence. A clean hook run can support accepting the staged-path invariant; it cannot support accepting untested application behavior.