Skip to content

Apply nested Claude agent spawn rules (T181) - #89

Merged
erickb336 merged 3 commits into
mainfrom
codex/t181-nested-spawn
Oct 9, 2026
Merged

erickb336 merged 3 commits into
mainfrom
codex/t181-nested-spawn

Conversation

@erickb336

@erickb336 erickb336 commented Oct 9, 2026 •

Copy link
Copy Markdown
Owner

Closes #62
Sage task T181

What changes

Sage applies the brief and capacity checks to nested Claude agents. Only the main session starts a lead. A lead can start an implementer, code reviewer, security reviewer, UX reviewer or QA agent. Other agents cannot start agents.

The hook allows at most three leads per project and three children per lead. Leads and children also consume the project cap and the existing total fuse. The lower limit wins. As before, the native event folder selects the project through projectName; the three-child limit follows the caller across folders. Task-bound project authority belongs to later steps. The main session keeps its existing direct specialist spawns and refusal texts.

A reservation records the exact session, caller and tool call. The hook uses the documented structured Agent result to bind the child ID. It does not infer parent ownership from start order. Missing or ambiguous results keep capacity charged. A requested TaskStop does not count as confirmed completion for new lead or child slots.

The existing shared-core role policy already describes the lead/team limits. This PR connects those rules to Claude's native hook events and capacity lifecycle. It does not change the state tool, add the lead agent file or grant leads state-write permissions; those are later T181 steps.

Scope and coordination

Sources change in packages/sage-claude/hooks/, with tests in scripts/sage-hook.test.mjs and one README rules row. npm run build produces the committed plugin files. Claude-owned issues #64–#68 remain separate.

Slot mutations share a short filesystem lock. A failed lock acquisition refuses new admission and retains held capacity. The hook never steals a lock based only on age or probes a process list.

Validation

All commands ran on b26a811d25f976846b35703b651b8327a61878ab with fake process tools first on PATH, env -u NODE_TEST_CONTEXT, and caffeinate -i.

Command Last result
npm run build built 212 files
npm run check all checks pass
Focused hook tests, command below tests 21; pass 21; fail 0; skipped 0
npm test tests 718; pass 718; fail 0; cancelled 0; skipped 0
git diff --check Exit 0
node --test --test-name-pattern='T181:|brief.*sage agent|at most max_agents|cap comes|stopped or dead|slot|report gate|report.*full|SubagentStop' scripts/sage-hook.test.mjs

For regression proof, git worktree add --detach <temporary-directory> origin/main selected 5431a74. Only the exact final test file was copied to that checkout. node --test --test-name-pattern='T181:' scripts/sage-hook.test.mjs ended with tests 12; pass 0; fail 12; skipped 0. Every group failed with ERR_ASSERTION; none failed from a missing fixture or module. The copied file matches the branch exactly, and the pre-existing main tests remain an unchanged byte prefix.

The compatibility controls also reproduced the upgrade defect on the preceding implementation before repair. Tests use the actual five-marker pending layout emitted by main. Complete old reservations receive an opaque internal identity; incomplete records gain no authority. Replay cases cover own stop, parent cleanup, bound and unbound lease expiry, and a new result that tries to claim an old child ID.

Every test run puts fake ps, pgrep and kill first on PATH. Fixtures send JSON to the hook; they do not launch real agents or execute attack strings in a shell. Existing test assertions remain unchanged.

Review

First clean cycle on b26a811:

  • Code/security: full scoped review; 22 focused tests, 12 replay cases and 6 compatibility cases pass.
  • QA: 114 independent assertions and 20 focused test groups pass.

Second clean cycle on the same b26a811 commit:

  • Code/security: fresh scoped review; 7 new boundary tests and 22 focused tests pass.
  • QA: 96 independent assertions in 11 fresh event sequences and 4 focused groups pass.

Both code/security and QA cycles are clean on the exact final commit.

Earlier review findings were repaired on this branch. Earlier failed cycles do not count.

Limits and not verified

  • A confirmed root lead retains its slot until matching completion or the one-hour lease. This step does not assume that the main task registry ID equals the Agent result ID.
  • Native nested Claude event delivery, shared session identity, and parent/child lifetime still need the live check in T181 live check: nested agents in a real Claude Code session, before step 2 #68 before step 2.
  • Parent-end cleanup follows the owner's assumption that existing children stop with their lead. A permitted child call without a confirmed result remains charged; a late asynchronous result does not prove the child stopped.
  • An interrupted process can leave the slot lock behind. The hook fails closed rather than reclaim a lock whose owner may still run. Automatic abandoned-lock recovery is not implemented.
  • Legacy Agent/Task result shapes without the documented fields retain capacity until valid completion evidence or lease expiry. An old pending reservation mixed with newer exact reservations can also remain charged until its lease when no authoritative join exists.
  • This step does not prove automatic Sage operation in a real Codex project session. That missing feature is tracked separately in Missing feature: automatic Sage workflows and gated autopilot in Codex #88.
  • Power-loss durability and simultaneous old/new hook writers during an upgrade remain unverified.

GitHub CI

CI run 37864046630 passed on the exact reviewed commit b26a811d25f976846b35703b651b8327a61878ab.

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.

T181 step 1: Lead build: the hook applies the spawn rules to nested agents

1 participant