feat(agent,rpc): remote ToolHost and hand executor that run the hand's tool handlers on every call (S27a)

Merged
CO
21d ago
+1519-38

Summary

Part of #5482. Stacked on #5436 (base feat/rt10-loopless-runtime); retarget to main once it merges. Also closes the deferred obligation recorded on #5413 for the remote executor's side.

A session can now run its tool calls on a hand (a tool-execution-only process) instead of in its own process:

  • packages/agent/src/execution/remote-tool-host.ts: createRemoteToolHost({ transport }) is a ToolHost. It sends each model-issued call (name and raw arguments) to the hand over the durable-core Transport, one lane per execution scope, and keeps the loop's side: tool_execution_start/_end, the batch's ordering (sequential waits per call), a forwarded abort, and result messages in source order. Each call is sent once; a link that fails before a settle becomes an error result, never a retry.
  • packages/agent/src/execution/remote-executor.ts: createRemoteExecutor({ transport, openScope }) is the hand's side. It opens each scope lazily, bound to the scope's root; a later call naming the same scope with another root is refused (scope_binding_mismatch), and so is any execution ID it already accepted (duplicate_execution). What the session says about itself (sessionRef) is never passed to a tool. Tools marked executionMode: "sequential" keep their barrier inside the scope.
  • packages/agent/src/execution/remote-protocol.ts: the wire messages (invoke, abort, settled, refused) and a strict decoder.
  • packages/coding-agent/src/modes/rpc/remote-executor-scope.ts: createLooplessHandScopes(configuration) opens each scope on a loopless runtime (#5436) whose cwd is the scope root.

The hand runs its tool handlers for every call (fail-closed)

  • loopless-execute-grant.ts gains a second closed-allowlist caller, remote-tool-executor. The guard in execute() is unchanged: a runtime without an issued grant still refuses every call (loopless-execute-guard.test.ts unchanged and green).
  • A runtime built with that grant runs its own extensions' tool_call handlers (permission checks among them) after argument validation and before the tool, and tool_result handlers after it, as a session does (loopless-tool-call.ts, loopless-tool-handlers.ts). A block, an abort, or a handler that throws stops the call before the tool runs.
  • Two runtime facts a session-less runner refused, and that the builtin handlers need, are now bound by the loopless runtime itself (ExtensionRunner.bindSessionlessFacts): pi.getAllTools() (the permission system resolves extension tools' permission parsers with it) and ctx.getLoadedHookSources() (where hook configuration is read). An unbound session-less runner still refuses both. pi.getAllTools() uses one projection shared with the session (core/tool-info.ts, moved out of AgentSession.getAllTools).
  • The builtin shell-hooks extension cannot give PreToolUse/PostToolUse hooks their session input on a runtime without a session. There it refuses every call when any tool hook is configured, and does nothing when none is. Forwarding those hooks to the process that owns the session is the two-sided forwarding change.

Inertness

Nothing in the product creates a remote ToolHost or a hand executor yet: no CLI flag, setting, RPC method or worker selects them. Sessions keep their in-process ToolHost. The two edits outside new files only affect runtimes without a session (loopless runtimes), and the getAllTools move is the same projection.

Not in this change (follow-ups on #5482)

  • KILL-HAND, MULTI-HAND/MULTI-KILL and FAULT-HAND conformance cases and the win32 process-semantics simulation.
  • Split-local equivalence cells and the extension-contract-unchanged split-local case.
  • Two-phase preparation of a parallel batch on the hand (today each call runs its handlers then its tool as it arrives), tool-name alias resolution and result-image normalization on the hand.
  • Two-sided forwarding of handlers that need the session (the shell-hooks extension above).

Tests

  • test/hand/remote-executor-select.test.ts, test/hand/permission-order-split-local.test.ts, test/hand/varied-session-info.test.ts, test/hand/scope-binding.test.ts (new).
  • Exact-head gate rows: see the comment below.

KILL

None registered here: the executor adds no durable write. Dispatch-at-most-once is enforced in memory per hand; the KILL-HAND cases come with the follow-up above.

Dead code

None made dead.


Summary by cubic

Adds remote tool execution (part of #5482): a session can now run its tool calls on a hand, a separate tool-execution-only process, instead of in its own process. The session side uses a new remote ToolHost (createRemoteToolHost) and the hand side a matching executor (createRemoteExecutor), linked by the transport protocol in remote-protocol.ts, over the reference transport (createReliableTransport, createInProcessNetwork, localEndpoint) now exported from the package root.

The hand runs its tool handlers for every call (fail-closed)

  • Loopless runtimes built with a new remote-tool-executor grant now run their extensions' tool_call/tool_result handlers around every call, as a session does: a block, an abort, or a handler that throws stops the call before the tool runs. The in-process test harness's grant still runs the tool alone.
  • The executor opens each scope lazily, refuses duplicate execution IDs and scope root mismatches, and sends each call at most once: a failed link becomes an error result, never a retry. The session reference never reaches a tool.
  • The loopless runtime now binds pi.getAllTools(), pi.getActiveTools() (an empty set), a no-op pi.setActiveTools(), and getLoadedHookSources() so the builtin permission system, hook loading, and tool-search catalog work without a session; unbound runtimes still refuse them. toolInfoOf shares the getAllTools projection with AgentSession.
  • The builtin shell-hooks extension refuses every call when tool hooks are configured on a runtime without a session, since those hooks need session input.

Inertness

  • Nothing in the product selects the remote ToolHost or starts a hand executor yet; sessions keep their in-process ToolHost.
  • The only edits outside new files affect runtimes without a session.