Skip to content

Lead build step 3: the hook's write list for leads (T273) - #124

Merged
erickb336 merged 10 commits into
mainfrom
sage/t273-lead-writes
Oct 12, 2026
Merged

erickb336 merged 10 commits into
mainfrom
sage/t273-lead-writes

Conversation

@erickb336

@erickb336 erickb336 commented Oct 12, 2026 •

Copy link
Copy Markdown
Owner

Closes #118

What this is

Step 3 of the lead build (design: #62, section 8, "Logbook writes"). Until now the sage hook refused every state tool write by an agent. Now an agent of the type sage:lead, in the lead slot that the chief started as lead-T<n>, may run a short list of state tool commands on that task only. Every other agent, and every child of a lead, is still refused. The lead agent file and the chief's instructions are step 4 (#119).

How it works

  • At the chief's spawn of a lead, the slot now records the task from the name lead-T<n> (mark task-T<n>). The slot binds to the lead's agent_id only from the Agent tool's result, as before.
  • slots.leadTask(agent_id) gives the task of the bound, live lead slot of this session, or nothing.
  • In agentCheck, a command that is not a read goes to leadProblem when agent_type is exactly sage:lead. It checks the slot, the tool, --project and SAGE_PROJECT, the command's words, and each id through the logbook view.
  • The state tool's taskFor is now logbookView(project, env, { max }): one bounded read of every table (a byte limit per table) that gives the logbook folder, a task's row, the row of a run id or a finding key, a task's one running lead run, and the tasks that are or were on a PR. taskFor had no other caller, so it is gone (no second path).

Each decision and its test

All tests are in scripts/sage-hook.test.mjs (events sent to the hook, which calls handle()), except where the test names scripts/sage.test.mjs.

# Decision What I built Test
1 Only agent_type exactly sage:lead, with a bound lead slot, and only the lead list leadProblem and LEAD_WRITES. Reads (status, standing, merge-check, and also logbook and config) stay as for every agent. A refusal ends with "Report to the sage what else the logbook needs." "T273: each command on the lead's list passes for the lead's own task"; "T273: each command that is not on the lead's list is refused, with the sentence to report to the sage"; "T273: a lead's slot is bound only by the spawn's result, and names the task of the spawn's name"
2 Own task only: each task word, and the task of each run id or finding key (also verdict --run); nothing or a throw refuses view.row(id)?.task === own. run done also needs a run of the five team roles, so a lead cannot close its own lead run. A dismissal needs a low finding and --reason. Each command has a fixed count of words, so no word goes unread. "T273: the same commands on another task are refused, also through a run id or a finding key of another task"; "T273: a made-up run id or finding key is refused"
3 No --key on finding add opt.key === undefined, in both forms (--key X, --key=X) "T273: a lead may not pass --key to finding add, in either form"
4 Same logbook as the state tool, or refuse --project and SAGE_PROJECT I chose to refuse them (least code; a lead does not need them, because its folder is the project or a worktree of it). A lead's write also runs only from Bash, because the terminal tool has its own folder. The hook resolves the event's folder with realpath, because the state tool uses process.cwd(), which is the physical path: without it, a folder through a link (/var on macOS) gave another logbook. A mutant with resolve fails 2 of the T273 tests. "T273: a lead's write takes no --project and no SAGE_PROJECT (decision 4)". It also runs the real state tool in the folder and shows that it writes the logbook that the hook checked.
5 A byte limit on the tables, fail closed LEAD_READ_MAX = 4 MiB per table: the state tool's own limit for its observations file. The largest real table on 2026-10-11 was 0.35 MB. "T273: an oversized or damaged logbook table refuses a lead's write that reads it (fail closed)"; scripts/sage.test.mjs "T266-TASK-FOR: logbookView gives the row ..." (exactly max reads, max + 1 throws)
6 A child of a lead, and every other agent, still cannot write Unchanged path for every type but sage:lead; a sage:lead type with no bound lead slot (a child that claims it, another session, after the lead's stop) is refused "T273: a child of the lead and every other agent still cannot write the logbook; the lead's own write passes"

| #118 point 4 | In sage mode, the main session's spawn of sage:lead named lead-T<n> passes only when T has a running lead run | logbookView(...).leadRun(task) reads the runs with the same 4 MiB limit per table. The hook reads the logbook of the event's folder through its links, the one that the lead's writes use. No running lead run: the refusal names run add T<n> --role lead --project <folder>. A logbook that cannot be read, or an event with no folder: refused. | "#118 point 4: in sage mode the main session starts lead-T only when T has a running lead run in the logbook"; scripts/sage.test.mjs "#118 point 4: logbookView gives a task's one running lead run, ..." |

Each of the 10 new hook tests (9 for T273, 1 for point 4) fails on main and passes on this branch: a detached worktree of main with this test file gives 245 pass and 10 fail, and the 10 that fail are exactly the new ones. The older lead tests now spawn leads in a fixture folder whose logbook has running lead runs, and they still pass on main.

Repair round 1 (R886)

Each fix has a test that fails on daac31f and passes now: a detached worktree of daac31f with this branch's two test files gives 11 failures, among them each test below.

Finding Fix Test
F-T273-1 (medium) The chief's spawn records the lead's logbook folder (sha256 of the folder that the hook resolves) in the slot. A lead write whose folder gives another logbook is refused. "F-T273-1: a lead's write from a folder of another logbook is refused"
F-T273-2 (medium) task set pr= passes only while the task has no PR, and only to a PR that no other task has or had (the state tool's own PR history: tasks, decision rows, ledger). pr= (clear), a second pr= in one command, and a PR with a leading zero are refused. Every lead verdict needs --pr equal to its task's PR; a task with no PR takes no lead verdict. "F-T273-2: a lead sets pr= only while its task has none, to a PR of no other task; each verdict names its task's PR"
F-T273-7 (medium) A lead's task set on a held task is refused. state= passes only as the cycle's next move from the task's current state: briefed to building, building to reviewing, reviewing to verifying, repairing to reviewing, verifying to verified, verified to reviewing. One key per command. "F-T273-7: a lead makes only the cycle's next state move, and never moves a held task" (every state times every target)
F-T273-3 (low) run add --branch must pass the state tool's ofTask for the lead's task. "F-T273-3: a lead's run add --branch must be a branch of its own task"
F-T273-4 (low) The slot records the running lead run that the spawn found. Each lead write needs that run to be the task's running lead run. "F-T273-4: a lead writes only while the lead run that its slot started for still runs"
F-T273-5 (low) The chief's spawn of lead-T<n> is refused while any slot, in any session, holds a lead of that task in that logbook. "F-T273-5: the chief starts no second lead-T while a slot for that task lives, also from another session"
F-T273-6 (low) Closed by the verdict rule of F-T273-2. as F-T273-2

Every lead write now reads the logbook once (to check the logbook and the lead run), so the oversized-table test now expects round to be refused too.

Repair round 2 (R890)

Each test below fails on 29e9f82 and passes now: a detached worktree of 29e9f82 with this branch's hook test file gives 5 failures, exactly these 5 tests.

Finding Fix Test
F-T273-8 (medium) The check happens where a PR lands (option b). For each merge, the hook reads the PR's head branch with gh pr view <n> --json headRefName,isCrossRepository in the merge's folder (the repository that gh pr merge acts on), read only, at most 5 seconds. It passes the branch to the merge check, which refuses unless each task that is or was on the PR owns that branch: its branch=, or a branch of the task by the state tool's ofTask. A failed or slow read, or a PR from a fork, refuses with a sentence. The rule runs after each task's own judgment, so a refusal still names missing verdicts first. merge-check --head <branch> (with --pr) runs the same check by hand; the sage skill and the README describe it. A state tool that cannot load refuses before the hook asks GitHub. "F-T273-8: a lead's verdicts on a PR whose head branch is another task's do not pass the merge (B1); the task's own branch does"; "F-T273-8: a task in the PR's history with another branch refuses the merge"; "F-T273-8: a failed, slow or fork read of the head branch refuses the merge (fail closed)"; "F-T273-8: merge-check --head runs the same check by hand, and needs --pr"
T275 (low, two lines) A lead's task set branch= with an empty value is refused; an option given twice in a lead command is refused. "T275: a lead never clears its branch, and gives each option once"

In the tests, gh is a fake first on PATH for every hook session: it answers gh pr view with a set head branch, a failure, a fork or no answer. No test reaches GitHub.

Repair round 3 (R894)

origin/main (e8b823d, #75) is merged in first, with no conflict. Each test below fails on f569116 and passes now: a detached worktree of f569116 with this branch's hook test file gives 4 failures, these 3 tests and the older test whose expected sentence changed ("the merge check gets the pull request's number from the merge command").

Finding Fix Test
F-T273-9 (medium) The merge check takes the project being merged. Only a task in that project's logbook counts as a task of the pull request that can approve the merge. A task of another logbook with verdicts on the SHA must still pass, but never approves. When the project's own logbook has no task of the PR with verdicts, the refusal says "no task of PR in this project's logbook". The hook passes the merge's folder (resolved with realpath, as for a lead); the hand-run merge-check uses the project that the state tool resolves (--project, SAGE_PROJECT, the current folder). "F-T273-9: only a task of the merged project's logbook approves a merge; another logbook's task can still block (C3)"
F-T273-10 (low) merge-check --head= (empty) refuses, next to the "--head needs --pr" check. "F-T273-10: merge-check refuses an empty --head"
F-T273-11 (low) The branch-rule refusal now ends: "Rename the branch to a task name (for example sage/t-), or the user lands this pull request." It no longer names branch=. "F-T273-11: the branch-rule refusal says to rename the branch to a task name, or that the user lands it"

The hook test sessions now keep their logbook for the checkout where bash() runs commands (FEATURE), because a merge counts only the tasks of its own project's logbook. Two older tests now expect the new "in this project's logbook" sentence.

Repair round 4 (R901)

origin/main (bb9a7ab, #125) is merged in first, with no conflict. A detached worktree of f16cea0 with this branch's hook test file gives 3 failures, exactly the tests of F-T273-12, -13 and -14.

Finding Fix Test
F-T273-12 (medium) While the lead's task is held, every lead write is refused with the "only the sage moves a held task" sentence (reads still pass). Second layer: the merge check refuses a held task ("T is held: only the sage moves a held task, and it does not merge until then"). "F-T273-12: while its task is held, a lead writes nothing, and a held task never passes the merge check"
F-T273-13 (medium) A main-session TaskStop whose task_id is lead-T<n> ends and frees that task's lead slot in this session, as TaskStop by the agent id does. The one-lead refusal names both forms: "stop it with TaskStop (by its agent id or by its name lead-T)". An agent's TaskStop still frees nothing. "F-T273-13: the main session's TaskStop of lead-T frees that lead's slot, so a fresh lead can start"
F-T273-14 (low) The main session's TaskStop records an end for the slot's agent (by id or by name), so the stopped lead's later spawns refuse with "this lead or spawn already ended". "F-T273-14: a lead that the main session stopped starts no child, by its agent id or by its name"
F-T273-15 (low, order 26) I could not reproduce the stall. The harness had three unbounded waits: send (spawnSync with no timeout), sendAsync (a promise that resolved only on close, with no timeout and no error handler, so a spawn that fails under load never settled), and sage. Each hook call now has a 60-second bound with SIGKILL, sendAsync rejects on error, on its bound and on a non-zero exit, and each test of the file has a 5-minute timeout, so a hang fails fast. "F-T273-15: a hook call that never ends fails within its bound instead of stalling the file" (a preload keeps the hook alive; both calls fail at a 3-second bound). With f16cea0's harness, the same test never ended: node's own --test-timeout=30000 did not stop it, because the hook child kept the runner alive, and I stopped the run by hand.

Repair round 5 (R905)

origin/main had not moved. A detached worktree of fee07bf with this branch's two test files gives 4 failures: the tests of F-T273-16, -17 and -18, and the F-T273-5 test, whose expected sentence changed. The F-T273-20 test fails with fee07bf's own harness ("bounded is not defined"); its fix is in the test file itself.

Finding Fix Test
F-T273-16 (medium) (1) The main session's spawn of lead-T<n> is refused while this session holds a live lead slot of that name in any logbook (and, as before, while any session holds one for this logbook). (2) TaskStop by lead-T<n> frees this session's one slot of that name. The refusal now reads "a lead named lead-T already holds a slot, in this session or for this logbook (…)". "F-T273-16: a session holds one lead of a name lead-T in any logbook, and TaskStop by that name stops only it"
F-T273-20 (medium, order 26) Every spawnSync and execFileSync of scripts/sage-hook.test.mjs goes through one helper, bounded(): a 60-second timeout with SIGKILL, unless the call gives its own timeout. "F-T273-20: every synchronous child call of this file has a bound and is killed at it"
F-T273-17 (low) When a spawn's result arrives for a call whose slot a TaskStop already freed, the hook records the agent as ended, so its spawns refuse with "already ended". "F-T273-17: a TaskStop of lead-T before the spawn's result binds still ends the agent that binds later"
F-T273-18 (low) The state tool cannot know its caller, so I took the second option: the state tool itself refuses run add, finding add, finding triage, finding close and verdict on a held task, for every caller. The chief's own moves stay: task set state= (held to building or briefed), round, run done and finding move. A lead command that the hook checked before the round set held now stops in the state tool. "F-T273-18: a held task takes no run add, finding change or verdict from any caller; the chief's moves stay" (scripts/sage.test.mjs)
F-T273-19 (low) The comment above the TaskStop branch now says what the branch does, and release lost its unused attempted argument. covered by the F-T273-13, -14 and T255 M3 tests

Not verified live

  • A real sage:lead agent in Claude Code: its events' agent_type and agent_id, and that its agent_id is the agentId of the spawn's result. The lead agent file comes in step 4.
  • The hook's gh pr view against real GitHub: the tests use a fake gh. The repository that gh pr view reads is the one that gh pr merge in the same folder acts on; I did not run a real merge.
  • That a subagent's Bash event's cwd is the folder where its command runs, and that the hook's environment has the same SAGE_PROJECT, SAGE_HOME and CLAUDE_CONFIG_DIR as the lead's shell.

Limits to know

  • The hook keeps its plain-command parser (no quotes), so a lead's free-text value is one word: --summary=no-test-for-empty-input, --reason=covered-by-T5. Step 4's agent file must say so.
  • A lead's write fails when SAGE_PROJECT is set in the session's environment, or when a table of the logbook is over 4 MiB.

Checks

npm run build, npm run check and npm test pass on the head commit. Each commit builds and passes the check on its own.

🤖 Generated with Claude Code

erickb336 and others added 2 commits October 11, 2026 21:28
…yte limit (T273)

taskFor becomes rowFor: the hook needs a run's role (run done only for the
five team roles) and a finding's severity (a lead dismisses only a low
finding). With max, a table over max bytes throws, so a caller fails closed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…T273)

A state tool write by an agent passes only for agent type sage:lead in the
bound slot of its lead-T<n> spawn, which now records the task. The command
must be on the lead list of #62 section 8; each task word must be the lead's
task, and each run id or finding key a row of that task (rowFor, 4 MiB per
table). A lead's write runs from Bash with no --project and no SAGE_PROJECT,
and the hook resolves the folder's links as process.cwd() does. Children and
other agents stay refused.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…run (T273, #118 point 4)

In sage mode, the main session's spawn of sage:lead named lead-T<n> passes
only when T<n> has one running lead run in the logbook of the event's folder
(through its links, the logbook that the lead's writes use). The state tool's
leadRunFor reads it with the same 4 MiB limit per table as rowFor, through one
shared reader. A refusal names the run add command; an unreadable logbook or
an event with no folder refuses too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
erickb336 and others added 7 commits October 11, 2026 22:54
…nd branch rules (T273 round 1)

F-T273-1: the slot records the lead's logbook folder; a write from a folder of
another logbook is refused. F-T273-4: it records the running lead run, and a
write needs that run still running. F-T273-5: a second lead-T<n> is refused
while a slot for that task lives, in any session. F-T273-2/6: pr= only while
the task has none, to a PR of no other task; each verdict names the task's PR.
F-T273-7: no move from held, and only the cycle's next state move.
F-T273-3: run add --branch is a branch of the task. One bounded logbook read,
logbookView, replaces rowFor and leadRunFor.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…73 round 2)

F-T273-8: for each merge, the hook reads the PR's head branch with gh pr view
(read only, 5 seconds, no fork) in the merge's folder and passes it to the
merge check. The merge check refuses unless each task that is or was on the
PR owns that branch: its branch=, or a branch of the task (ofTask). A failed
or slow read refuses. merge-check --head runs the same check by hand.
T275: a lead never clears its branch, and gives each option once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…und 3)

F-T273-9: the merge check takes the project being merged; only a task of its
logbook counts as a task of the pull request. A task of another logbook with
verdicts on the SHA must still pass. The hook passes the merge's folder; the
hand-run merge-check uses the project that the state tool resolves.
F-T273-10: an empty --head refuses. F-T273-11: the branch-rule refusal says to
rename the branch to a task name, or that the user lands the pull request.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t waits (T273 round 4)

F-T273-12: while its task is held, a lead writes nothing; the merge check
refuses a held task. F-T273-13: the main session's TaskStop of lead-T<n> frees
that task's lead slot; the one-lead refusal names both forms. F-T273-14: the
main session's TaskStop records an end, so a stopped lead starts no child.
F-T273-15: each hook call in the hook tests has a 60 s bound and each test a
5 minute timeout, so a hang fails instead of stalling the file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…state tool (T273 round 5)

F-T273-16: a session holds one lead of a name lead-T<n> in any logbook, and
TaskStop by that name stops only it. F-T273-17: a spawn result that arrives
after the TaskStop freed its slot ends the agent. F-T273-18: the state tool
refuses run add, finding add/triage/close and verdict on a held task, for every
caller. F-T273-19: TaskStop comment, and release has no unused argument.
F-T273-20: every sync child call of the hook tests goes through bounded().

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@erickb336
erickb336 merged commit d6414fa into main Oct 12, 2026
1 check passed
@erickb336
erickb336 deleted the sage/t273-lead-writes branch October 12, 2026 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lead build step 3: the hook's write list for leads (T181)

1 participant