feat(agent): session plane - session tools run where the loop runs, with a user-set thread chain count (S32)

Merged
CO
19d ago
+973-0

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 declared runs: "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 (parallel runs both sides at once, sequential runs 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 with chain_count_limit only 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, no origin_chain refusal.
  • Setting. threads.chainCountLimit (settings-manager.ts, getThreadChainCountLimit()), documented in docs/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 (every runs: "brain" call runs loop-side, every other call reaches the hand); a sequential mixed batch in source order; a hand-run tool's todo call served on the brain's own session over the plane, read refused; 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/start on 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's webfetch result 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's runs: "brain"; emitting result events unbuffered; dropping the abort break; limit off by one; a hidden default cap of 8; runs: "brain" dropped from todo; the split host sending todo to the hand.

  • Gated push at 0a9a4dac6e: bun run check, knip, changelog gate, packages/agent test/execution 343 passed, the coding-agent files above 29 passed.

  • Loop-side built-ins. web_search, webfetch and the ask-user tools (request_user_input, ask_user_question) declare runs: "brain". Web search and fetch are loop-side under the 2026-11-03 split, and #5641 names ask_user a session tool. The write-set extension is recorded on #5641.

Review fold (first review, at 94931c73b3)

  • In a mixed parallel batch, the tool-result message events are now emitted in source order once both sides finish, as the fused pipeline does. Each side's tool_execution_* events still pass through as they happen.
  • A mixed sequential batch starts no later segment once the run is aborted.
  • docs/settings.md says threads.chainCountLimit has no effect until a thread tool carries a hop count. The wiring is #5899, stacked on this PR; that PR closes #5641.