feat(agent): session plane - session tools run where the loop runs, with a user-set thread chain count (S32)
Part of #5641
Plan row S32 of the brain/hand split: the session plane and a user-set thread chain count (decision K8).
What changes
- Routing (
packages/agent/src/execution/plane-session.ts, new).createSplitToolHost()runs every call to a tool declaredruns: "brain"on the loop's own prepared pipeline (createLoopToolHost(), which reads the agent's hooks and batch store at each batch) and every other call on the hand. A batch of one side goes to that host whole; a mixed batch keeps the loop's mode (parallelruns both sides at once,sequentialruns same-side runs in source order) and the result messages come back in the model's source order, a repeated call ID included. - Session plane.
serveSessionPlane()/createSessionPlaneClient(): a hand-run tool asks the brain that owns its call for a loop-side tool call over the nested plane from #5640. A tool that is not declared loop-side is refused (not_loop_side). - Thread chain count (
packages/agent/src/execution/chain-count.ts, new). A thread call carries its hop count;admitChainCall()refuses withchain_count_limitonly past the limit the user set, read at every admission. A→B→A is allowed. Unset means unlimited: no default cap, no warning, no cost limit, noorigin_chainrefusal. - Setting.
threads.chainCountLimit(settings-manager.ts,getThreadChainCountLimit()), documented indocs/settings.md. - Inert until a session is split; fused sessions are unchanged.
Tests
-
packages/coding-agent/test/execution/session-plane.test.ts: routing enumerated from the shipped tool registry of a real CLI session (everyruns: "brain"call runs loop-side, every other call reaches the hand); a sequential mixed batch in source order; a hand-run tool'stodocall served on the brain's own session over the plane,readrefused; A→B→A; no limit set (real settings file) → a 12-hop chain is admitted;threads.chainCountLimit: 3→ call 4 refused. -
packages/coding-agent/test/runtime/todo-state.test.ts -t 'split session with an existing list'(carried from #5646):todo done/starton a split session run on the brain against the existing list and never reach the hand; the list, result text and widget renders equal the fused session's. -
packages/coding-agent/test/execution/loop-side-builtins.test.ts: the three tools are declared loop-side; a split session routes them to the loop and never to the hand; a split session'swebfetchresult equals the fused session's and runs in the loop's process. -
Regression tests for the source-ordered result events and the sequential abort stop.
-
Mutations, each killed: dropping
webfetch'sruns: "brain"; emitting result events unbuffered; dropping the abort break; limit off by one; a hidden default cap of 8;runs: "brain"dropped fromtodo; the split host sendingtodoto the hand. -
Gated push at 0a9a4dac6e:
bun run check, knip, changelog gate,packages/agenttest/execution 343 passed, the coding-agent files above 29 passed. -
Loop-side built-ins.
web_search,webfetchand the ask-user tools (request_user_input,ask_user_question) declareruns: "brain". Web search and fetch are loop-side under the 2026-11-03 split, and #5641 namesask_usera session tool. The write-set extension is recorded on #5641.
Review fold (first review, at 94931c73b3)
- In a mixed
parallelbatch, the tool-result message events are now emitted in source order once both sides finish, as the fused pipeline does. Each side'stool_execution_*events still pass through as they happen. - A mixed
sequentialbatch starts no later segment once the run is aborted. docs/settings.mdsaysthreads.chainCountLimithas no effect until a thread tool carries a hop count. The wiring is #5899, stacked on this PR; that PR closes #5641.