Contents Lesson 5 of 9

10 min read · practitioner

Let the agent close the loop: tests, build, leak check, hooks

By the end of this lesson you will have the watchlist on your screen ordered by size of move, a change the agent declared done only after running four checks itself and showing you the last line of each, and a commit that passed the repository's git hooks on the way in.

The idea

"It looks right" is not done. A page can render perfectly while a type is wrong underneath, a test is broken beside it, or the production build has quietly copied your key into a file every visitor downloads. Done is a set of commands that pass.

Four commands, one definition of done

The starter's rules say it in one line — done means green — and these are the four:

Command What it catches How long
npm test a behaviour the tests pin down, now different seconds
npm run typecheck code that does not match its own types seconds
npm run lint unused code and risky patterns seconds
npm run check:leak the key's value anywhere in a production build about a minute

The last is the one with no visible symptom. It builds the app with a made-up key in the environment and fails if that value turns up in any file the build produced. Nothing on screen would ever tell you.

The agent can run the loop itself

This is the real advantage of an agent in your terminal over a chat window. It does not hand you code and hope: it can run the tests, read the failure, change the code and run them again, until all four pass. But only if it is told to, every time. Left alone, an agent tends to report "done" the moment the edit is made, because the edit was the task as it understood it.

The starter's rules file already says done means green. Say it in the prompt too. A rule is advice, and the prompt is where you are certain it was read.

Stop npm run dev (Ctrl-C in its terminal) before the agent runs npm run check:leak: the leak check builds the app into the same .next folder the dev server is using, and two programs writing there at once can break the dev server. Start it again afterwards.

Running four commands means four permission prompts each round. Allow them once.

In Claude Code, put two allow rules in your personal .claude/settings.local.json, next to the ask rule from lesson 3:

{
  "permissions": {
    "allow": ["Bash(npm test)", "Bash(npm run *)"],
    "ask": ["mcp__eodhd__*"]
  }
}

In Codex, the first time it asks to run npm test, pick Yes, and don't ask again for commands that start with the command it shows. Codex writes that choice as a rule to ~/.codex/rules/default.rules, outside the repository, and you can write the same two rules there yourself:

prefix_rule(pattern = ["npm", "test"], decision = "allow")
prefix_rule(pattern = ["npm", "run"], decision = "allow")

Codex's rules are marked experimental, and an allowed command runs outside its sandbox, so allow these and nothing broader. Restart Codex after editing the file by hand.

Hooks: the checks that also catch you

npm install pointed git at the starter's .githooks/ folder. From then on two hooks run whoever makes the commit, you or the agent:

git commit → pre-commit: refuse a line shaped like an EODHD key, lint the staged files
git push   → pre-push:   tests, type check, leak check — the same gates CI runs

They are a seatbelt, not a lock. A hook cannot see a key you never staged, or one already in your history. And one flag, --no-verify, skips them entirely, which is why the starter's rules say never: when a hook fails, fix what it found. If your agent ever proposes skipping a hook, that is a rule for your own section of the rules file.

Both agents have hooks of their own as well, set in their settings and listed by /hooks in either one. They run a command at fixed points inside a session and can block an action before it happens — the enforced version of a rule, inside the agent. You will not need them in this course: git hooks cover the commit, and they also catch the commits you make by hand.

Try it now

The prompt

Mock mode on. Add the two allow rules above — to .claude/settings.local.json, or to Codex's rules file — start the agent in the starter folder, and paste:

In src/app/page.tsx, pass the watchlist rows through sortByMove from
src/lib/move.ts before they reach the table, so the biggest move is on
top. Above the table, add one line of plain text that says the order:
"Sorted by size of move, up or down, biggest first." Add no request. If a
comment in the code says the watchlist is deliberately not sorted by
movement, update that comment so it is true.
When you think you are done, run npm test,
npm run typecheck, npm run lint and npm run check:leak, fix whatever they
report, and only then tell me you are done, with the last line of each.

What the agent should do

  • Change src/app/page.tsx: one import, the rows wrapped in sortByMove where the table receives them, and the one line above the table that names the order. It may also reword one stale comment in the watchlist component; nothing else.
  • Run all four commands without asking, because the allow rules cover them. If a rule you wrote in lesson 2 makes it stop before the build anyway, say yes: this prompt asked for the build.
  • If any fails, read the output, fix it, and run that command again.
  • End with four last lines, all passing, and only then say done.

Check it yourself

  1. The transcript shows the four commands actually run, not merely mentioned.
  2. npm run dev and open the page: the mock-mode notice is at the top, the line above the table names the order, and the rows go from the biggest move to the smallest, whichever direction. A page that reorders a list without saying how is making a choice for the reader; naming it keeps the choice yours.
  3. git config core.hooksPath prints .githooks.
  4. Commit, as in the Checkpoint below. The pre-commit hook lints the staged files and lets them through.
  5. If your copy has a remote of your own, push. The pre-push hook runs tests, type check and leak check before anything leaves your machine.

Break it on purpose

Two seatbelts, tested rather than trusted. First, add a line to any file holding a made-up string shaped like a key — twelve lowercase hex characters, a dot, six digits — stage it, and commit: the pre-commit hook refuses and shows you the line. Remove it. Second, change an expected value in one of your tests so it fails, and ask the agent to commit and push. (This needs a remote of your own: if your copy still points at the public starter, which you cannot push to, run git remote -v and skip the push part; the commit itself and the hook that runs on git push are the same code.) The pre-push hook stops the push. Watch what the agent proposes next: fixing the test or the code is the right answer, and skipping the hook is the wrong one.

Compare with ours

Our change to the page
+import { sortByMove } from "@/lib/move";
...
-        {rows !== null && rows.length > 0 && <Watchlist rows={rows} />}
+        {rows !== null && rows.length > 0 && (
+          <>
+            <p className="text-sm text-zinc-600 dark:text-zinc-400">
+              Sorted by size of move, up or down, biggest first.
+            </p>
+            <Watchlist rows={sortByMove(rows)} />
+          </>
+        )}

One import and the table wrapped with the line that names its order. The leak check passes unchanged, because nothing about the key moved. The commit is on the branch ref/work-with-an-agent/5 of the starter repository.

Checkpoint

git status   # page.tsx and perhaps the watchlist comment, and no .env*
git add -A
git commit -m "page: sort by size of move, what a reader comes to find"

Then answer this lesson's questions.