Skip to content

Hook: only the chief writes the logbook; agents run read commands only (T83) - #36

Merged
erickb336 merged 17 commits into
mainfrom
tool/t83-agent-state-writes
Oct 6, 2026
Merged

erickb336 merged 17 commits into
mainfrom
tool/t83-agent-state-writes

Conversation

@erickb336

@erickb336 erickb336 commented Oct 5, 2026 •

Copy link
Copy Markdown
Owner

Scope of the guard

The hook stops agents' mistakes and the common forgery forms. Deliberate shell forgery (scripts, variables, links made at run time) is the sandbox's job (tasks T94–T99); merges stay paused until the sandbox switch (T99) merges.

What

Only the chief writes the logbook, and the chief writes it only through sage.mjs. This PR changes the hook (agents and the chief), the state tool sage.mjs, the hook matcher, the README and the sage skill.

Who is an agent

An event is an agent's when it has an agent_id field (also an empty one), or when its agent_type is a sage role other than sage:chief-of-staff. Every other event is the main session (the chief). The agent rule holds in every mode, also out of sage mode.

Agents (hook)

  • State tool. The hook decides by the program that runs, not by a mention. The program is the first word after assignments, options and wrappers (env, xargs, sudo, timeout, ...), and the word after -exec. It runs sage.mjs when it is the file; when it is an interpreter (node, python, ...) whose words up to its script, whose heredoc or whose piped input name the file; and when text that sh -c, bash -c or eval runs does. A variable or substitution in those places counts as a run when the text names the file. So cat, grep, git diff and git commit -m "fix sage.mjs" pass, and so does a description that names it.
  • Read commands only. A run of sage.mjs passes only as one plain command: node <path ending in skills/sage/sage.mjs> <read> [--project <p>], with the copy that the hook loads, and no quote, variable, ;, &&, ||, |, backtick, $(, >, <, newline or ... The reads are an allow-list: status, merge-check, logbook (not repair), standing (not add), config with no key=value.
  • PR script. An agent never runs sage-pr.mjs (same program rule).
  • File tools. Write, Edit, MultiEdit and NotebookEdit under the sage root are refused, after links (also dangling ones) and .. are resolved, in any case and Unicode form (NFKC and lower case, as APFS compares names).
  • Shell writes near the logbook. A write form (>, tee, cp, mv, rm, ln, sed -i, PowerShell writes, ...) is refused with the sage root's path, .claude (except a plain worktree path), a variable by the home folder, a path that leads into the sage root through a link or .., or a pattern with a link on its path. A pattern or a logbook file's name in a project passes (rm dist/*, jq ... > config.json); with cd, pushd, .., ~, $, a backtick or an absolute path it is refused.
  • Root variables. SAGE_HOME or CLAUDE_CONFIG_DIR set in the command to a plain path outside the real sage root (a scratch folder for tests) passes, and so does a later $SAGE_HOME of it. A $SAGE_HOME that the command did not set so, an assignment of another value ($HOME/..., the real root), or a bare name in a command with a substitution or pipe is refused with a write.
  • Sandbox. An agent's dangerouslyDisableSandbox is refused.
  • Every command tool. Bash, Monitor, PowerShell and mcp__terminal__*; the PreToolUse matcher in hooks/claude.json now names them.
  • Fail closed. When the sage root cannot be resolved, or the check throws, every command and file change of an agent is refused.

The chief (hook)

  • The chief's file tools never write under the sage root, in sage mode and out of it.
  • The chief's shell never writes with the sage root's path, .claude/sage, or a root variable as above. In sage mode, a logbook file's name (tasks.tsv, ledger.tsv, config.json, ...) counts too; out of sage mode a project's own config.json stays free. node <path>/sage.mjs ... as a command of its own passes. The owner's own ! commands are no hook events.
  • When the hook throws on the chief's command that writes near the logbook (or cannot be checked for it), it refuses it.
  • The merge form needs a PR number with no leading zero (gh pr merge 040 ... is refused).

State tool (sage.mjs)

  • PR history. Each change of a task's pr= (task set pr=, verdict --pr) writes a decision row PR <old> → <new>. The merge check for PR n counts every task that is on n, was on n by a decision row, or has ledger rows with PR n on any commit. A cleared or changed pr= no longer takes a large task's verdicts out of the merge.
  • Leading zero. A PR number has no leading zero (pr=040 and --pr 040 are refused); an older row with 040 counts as PR 40.
  • The merge check's message for a task without verdicts says that it is or was a task of the PR.

Why

The T77 re-check (R354) showed that a sandboxed agent could run verdict, task set branch=main, init and config cycles.*=1 through the state tool, after which the merge check said "may merge" on its own unreviewed commit. S1 and S2 showed that a changed pr= or a hand edit by the chief could do the same.

Evidence

  • Rounds 1–3: tests in scripts/sage-hook.test.mjs and scripts/sage.test.mjs cover each forgery step, the spellings, links, .., patterns, PowerShell, the chief's writes, the PR history and the leading zero.
  • Cycle-1 repair (R445): new tests C2 (reads, diffs and commits that name sage.mjs pass; bash -c, eval, -exec, xargs, a pipe into node, node -e and a variable that run it are refused), C3 (scratch SAGE_HOME and CLAUDE_CONFIG_DIR pass for an agent and the chief; the real root is refused), C4 (a file tool path to a root that does not exist yet, in upper case and in compatibility forms), and the ledger-history test in sage.test.mjs.
  • On 5d6948b, C2 and C3 fail. C4 and the ledger-history test pass on 5d6948b (the behaviour was there; the tests were missing). On mutated copies they fail: without the ledger line of prHistory, and without the fold in canonical.
  • npm run build, npm run check pass; npm test twice on 317da33, with a fake ps and kill first on PATH: 260 of 260 pass both times.

Not verified

  • Not run in a live Claude Code session with a real subagent; the tests feed the hook JSON events as Claude Code does.
  • The exact input schema of Monitor, PowerShell and the terminal MCP tools is not confirmed; the program rule reads every text field of the tool input, and allows a read only from a command string.
  • A program that runs sage.mjs from inside a script file, or a name built at run time, is not seen; that is the sandbox's job.
  • A pattern whose link is deeper than the first pattern part (for example d*/x/* where d1/x is a link) is not caught; the hook does not expand patterns.

Risk flag: auth. The owner merges this pull request, before PR 34.

🤖 Generated with Claude Code

erickb336 and others added 16 commits October 5, 2026 00:23
…y (T83)

An agent event (agent_id) may run the state tool only as one plain read
command (status, merge-check, logbook, standing, config with no key=value),
by an allow-list, from any command tool (Bash, Monitor, PowerShell,
mcp__terminal__*). It never runs sage-pr.mjs, never writes under the sage
root with a file tool, a link or a shell write, and never runs outside the
sandbox. On an error the hook refuses such commands and agent file changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…T83 S1, S2)

S1: each change of a build's pr= writes a decision row (PR <old> → <new>).
The merge check counts every task that was ever on the PR: by its pr, by
those rows, and by its verdicts on a commit with that --pr. A cleared pr=
no longer takes a task out of the merge.

S2: the hook refuses the chief's Write, Edit, MultiEdit and NotebookEdit
under the sage root, and a shell write that names the sage root (or, in
sage mode, a logbook file), unless the command is only node .../sage.mjs.

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

- File tools: compare canonical paths (realpath.native, lower case, NFC),
  so another case or Unicode form of the sage root is refused (S-CASE).
- Agent shell: deny by default near the logbook. A write form with a
  logbook file, .claude, a pattern that can name them, a variable by the
  home folder, or a cd home is refused (F1, S-PWSH). Reads with
  2>/dev/null or 2>&1 pass, and the write words count only as commands
  (F3).
- Every agent check fails closed (F2); an empty agent_id or a sage role's
  agent_type is an agent's event (S-EMPTYID); an agent runs only the
  state tool that the hook loads (S-OWNCOPY).
- The chief's rule reads case and patterns too (S-CHIEFCASE).
- A PR number has no leading zero, in the hook and the state tool; an
  older 040 row counts for PR 40 (S-PRZERO).
- README and SKILL: the sandbox (T80) is the full guard for shell writes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he sage root are refused (T83 round 2)

- COST-GLOB (owner decision): an agent's pattern or logbook-file name is
  refused only with cd, pushd, .., ~, $, a backtick or an absolute path,
  or when a word resolves into the sage root or a folder that holds it.
- S-WTLINK: every word of an agent's shell write is resolved through the
  links on disk; ln/link to the root, or to a target that the hook
  cannot read, is refused.
- S-FOLD: NFKC before lower case (ſ is s, st is st).
- S-DOTDOT: a file tool's path keeps its .. for the system to resolve
  after each link.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…is home again (T83 round 3)

S-GLOBLINK: the hook does not expand a pattern, so it now refuses an agent's
shell write when a part before the first wildcard is a link, or when the first
pattern part names a link (also a dangling one) in its folder.
S-HOMEBRACE: wordsOf gives ${HOME} as $HOME again ("$$$1", not "$$1").
README and SKILL.md: deliberate shell forgery is the sandbox's job (T94-T99).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…oot variable set to a scratch folder passes (T83 C2, C3, C4)

An agent's read, diff or commit message that names sage.mjs passes; running the
state tool's write or the PR script stays refused (behind env, xargs, -exec,
sh -c, eval, a pipe into node, or a variable). SAGE_HOME or CLAUDE_CONFIG_DIR set
in the command to a plain path outside the real sage root no longer makes a
write near the logbook, for agents and the chief. New test: a file tool's path
to a sage root that does not exist yet is compared folded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…the chief's shell rule by mode (T83 C2, C3, C7)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Conflicts resolved: the hook keeps both agent rules (agentWriteProblem for the
logbook, agentProblem for stash and the process list), one SHELL_TOOLS pattern,
main's shell reader with redirection targets kept in redirects, and the state
tool's exported PR rule. The README and the sage skill keep both sides' rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…); a prefix SAGE_HOME is not safe; reviewer reads pass (T83 N1, N2, N4)

- scriptRun reads the programs that run through main's programsRun (T100), so
  for, while, if, {, !, case, sudo -u <user>, npx -p and the other wrappers no
  longer hide a state-tool write. One way to find programs, not two.
- usesRootVar: only an assignment that is a command of its own (or export)
  makes a later $SAGE_HOME safe; a prefix assignment does not.
- node --check, python code that imports only read modules, diff <(git show),
  and reader pipes that redirect to a scratch file pass; runs stay refused.
- nearLogbook and partsOf check each word once, so 1 MB stays under 200 ms.
- R454-N5 checks the stash rule alone: the logbook rule refuses an agent
  command that the hook cannot read (fail closed).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…a write is refused; uniq, less and sort -o write; $? is no pattern; refusals name the word

- Remove pythonReads, READ_MODULES, the node --check skip and writeText
  (T83-S1-PATHLIBOS, T83-S2-UNIQ part): python or node code that names
  sage.mjs is refused again.
- uniq and less always count as writes, sort -o / --output too (T83-S2-UNIQ,
  R667-READERS).
- usesRootVar: every $SAGE_HOME or $CLAUDE_CONFIG_DIR is a use, whatever
  assignments come before it, for agents and the chief (T83-N2-PREFIXVAR).
- wordsOf: $? $# $* $@ $$ $! $- $0 are variables, not patterns (T83-Q1-DOLLARQ).
- nearLogbook, namesRoot, linksNear and shellWrite return the word, and each
  refusal names it (T83-Q2-GLOBLOOP).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ort --o… writes, no program through a pattern, every refusal names its word in its own case

- wordsOf: remove round 5's special-parameter rewrite (T83-R5-DOLLARQUOTE):
  hom$''? passed as hom$. One unquote() drops the $ of an ANSI-C or locale
  quote and the quotes, in wordsOf, namesRoot, usesRootVar and scriptRun.
  echo exit=$? is refused and the refusal says to use || echo FAIL or ; echo done.
- sort: any option that starts with --o writes (T83-R5-SORTABBR).
- globRun: a pattern in a program's path, or in the script that node, python
  or a shell runs, is refused and the part is named (T83-PRE-GLOBRUN).
- hidesTool: a hidden state-tool run names the keyword, wrapper, program,
  node option or non-plain text, and the plain way to read sage.mjs (T83-Q3-N1WORD).
- partsOf keeps each word's own capitals; nearLogbook and namesRoot match on
  the folded form and name the original (T83-Q4-CASE).
- Tests: Q1-DOLLARQ replaced by R5-DOLLARQUOTE; R5-SORTABBR, PRE-GLOBRUN,
  Q3-N1WORD, Q4-CASE added; Q2-WORD asserts the root in its own case.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…writes

# Conflicts:
#	scripts/sage-hook.test.mjs
@erickb336

Copy link
Copy Markdown
Owner Author

Round 6 (head ef9d43a: round-6 commit 6a0b69b, then main merged at f1ffd8a, PR 52). Five items:

  1. T83-R5-DOLLARQUOTE: round 5's $? rewrite is removed; $'' and $"" drop their $ as the shell does, so hom$''? is the pattern hom?. echo exit=$? after a redirect is refused; the refusal names $? and says to use || echo FAIL or ; echo done.
  2. T83-R5-SORTABBR: any sort option that starts with --o writes (--out=, --outp=, --output=).
  3. T83-PRE-GLOBRUN: a pattern in a program's path, or in the script that node, python or a shell runs, is refused and the part is named ("sag?.mjs").
  4. T83-Q3-N1WORD: a hidden state-tool run names its word (for, {, !, sudo, npx, python3, --check, -e, ;) and the plain way to read sage.mjs.
  5. T83-Q4-CASE: the named word keeps the command's capitals ($HOME, ~/.Claude/Sage/y).

The five new tests fail on 2c543fc and pass on the new head. Full npm test: 446 pass. R677 N1–N3, N5, N9–N13 now refuse; N4 (chief, tmp-folder root) stays allowed by the chief's text-or-.claude/sage match (the real root shape is refused and tested). R454-N5 merged as asked: PR 52's title and 2 s bound, PR 36's body.

🤖 Generated with Claude Code

…names sage.mjs runs only plain readers, a bare redirect to the logbook is a write; the pattern rule is gone

A. Drop globRun: [ -f x ], [[ ]], if [[ ]], [ -d d ] || mkdir and $(( )) pass again for an agent (T83-R6-BRACKET, T83-R6-ARITH).
B. Name rule: a command that names sage.mjs or sage-pr.mjs may run only plain readers (cat, grep, rg, head, tail, wc, ls, diff, shasum, jq, awk and sed without -i, git show/log/diff/status/blame/grep/ls-files/add/commit); any other program is refused and named, whatever its options (T83-R6-OPTVALUE). The refusal gives the plain read form and the cat/git show way (T83-R6-HINTFIT). scriptRun, hidesTool, kind and INTERPRETERS are gone.
C. A command of only redirections is a command to the shell reader, so '> ~/.claude/sage/<p>/ledger.tsv' with no command word, also after ; or |, is refused for an agent and the chief (T83-R6-BAREREDIRECT).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@erickb336
erickb336 merged commit 1e64e63 into main Oct 6, 2026
1 check passed
@erickb336
erickb336 deleted the tool/t83-agent-state-writes branch October 6, 2026 10:31
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.

1 participant