Skip to content

Add the ACP client engine: isolated agent launch, handshake, bounded update stream (#4292 stage 1) - #4319

Merged
mstomar125 merged 2 commits into
mainfrom
issue-4292-session-1
Nov 20, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-4292-session-1

Conversation

@mstomar125

@mstomar125 mstomar125 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stage 1 of the session side of the ACP chat (#4292): the client engine that runs one external agent over stdio and produces an in-memory stream of typed events. Nothing is written to the graph, no gRPC, no UI, nothing launches it yet: inert until stage 2.

Agent-lane sign-off: the user (Mayank) explicitly granted agent-lane sign-off for this build on 2026-11-20 ("you have signoff on each"); it stands in for the agent owner's. This PR touches packages/agent (new acp/ module, lib.rs export, one accessor on the shared process helper).

What is in this PR

New packages/agent/src/acp/:

  • launch.rs: AcpLaunch descriptor (program, args, literal env, named daemon env, credential references), validated (no NUL, bounded, valid and non-repeated env names, base variables not overridable).
  • wire.rs: AcpLimits (every bound in one struct), AcpError, and a bounded line reader: a line over the message limit is dropped while it streams past and never held; a byte ceiling is enforced inside the read loop so a stream with no newline cannot grow.
  • events.rs: typed events and update parsing. Tool input/output, diffs and locations are never copied out; the echo of the user's own message is dropped; plan updates render as a markdown checklist; every string is bounded.
  • engine.rs: AcpSession::start (launch + initialize + session/new inside one startup limit, credential lookup included) and an actor that owns the process and connection: prompt, cancel, answer_permission, close.
  • tests.rs: engine tests against a scripted Python fake agent (unix only, like the existing runner tests).

Reuse, not copy: process launch, process-group guard, private 0700 working directory, reap, credential resolution inside a timeout and the raw-byte redactor all come from local_agent/process.rs and redaction.rs (merged with the tool runners). The only edit there is a working_dir() accessor on Isolated.

Protocol decisions (per the approved spec and rulings)

  • initialize offers fs: {readTextFile: false, writeTextFile: false} and terminal: false; no elicitation. session/new sends mcpServers: [] (daemon-supplied context is a later stage). session/set_mode / set_config_option are never called, so a bypass mode is never selected.
  • Agent-initiated requests: everything except session/request_permission is answered -32601 (fs/terminal/extension methods included), counted against a cap.
  • Permission requests are surfaced as AcpEvent::PermissionRequest, never auto-answered. Only allow_once / reject_once options are surfaced and accepted; allow_always / reject_always are hidden and cannot be selected (an answer naming any option not surfaced is refused). Waiting requests are answered cancelled on cancel, on timeout, and past the pending cap; a flood beyond the per-turn cap ends the session.
  • Cancel: session/cancel, waiting permissions answered cancelled, a grace period, then the process group is killed.

Bounds (all in AcpLimits, tested with small values)

Per-message size (oversize dropped, repeats end the session), per-turn and per-session bytes, per-turn update count, noise lines and bytes, unwanted requests and stray responses, pending and per-turn permissions, prompt size, startup, turn, idle (not counted while a permission waits for the user), permission wait, cancel grace, write, and a stalled event reader. JSON depth stays on serde's default recursion limit: a deeply nested line is noise.

Untrusted-agent handling: credentials are removed from the raw bytes of every line in one pass before parsing, and from error text before it is cut; responses are matched by id; stderr is drained and discarded; the child is killed on drop (process group), on close, on any fault; the private working directory is removed.

Review fixes (security review of 270cb5a)

  • Redaction now covers every decoded string that is surfaced (text, thought, title, plan entries, command names, message and tool ids, option names, session title, agent name and version, stop reason, error text), in the order redact then cut, on top of the raw-byte pass (JSON escapes such as \uXXXX bypass a raw-byte match). Regression: a fake agent sends the credential as raw JSON escapes in a text chunk, thought, plan entry, title, tool call, command name and permission option name; no surfaced event holds the credential or any 4-character piece of it. A unit test checks a credential straddling the cut at every offset.
  • The idle clock is not counted while a permission waits, and restarts when that request is answered, cancelled or times out (test: idle shorter than the permission wait; the session survives past idle and ends the wait on the permission timeout).

BINDING requirements for stage 2 and later

  1. The writer MUST call AcpSession::redact on ACCUMULATED text before storing or showing it (a credential split across two streamed chunks is invisible to the per-line and per-string passes) and must never log or store raw updates.
  2. Launch descriptors are built from a pinned ABSOLUTE program path and fixed arguments only. AcpLaunch::validate is a shape check, not a policy: a relative program or PATH search, a leading -, symlinks, and literal env values that select a bypass or permission mode are NOT forbidden by it. Anything built from node fields or peer-authored data goes through a code-owned allow-list.
  3. HOME and USER are inherited from the daemon (base variables; validate forbids overriding them), so the Claude adapter would run with the user's real HOME, config and login and could reach the macOS keychain. Stage 2 must isolate it (a literal CLAUDE_CONFIG_DIR or equivalent, or an explicit private-HOME option on the descriptor) and supply the adapter's token ONLY through secrets (resolved inside the startup timeout, in the redaction set, never logged). A process-group kill does not reach descendants that call setsid.
  4. Never offer allow_always / reject_always, never call session/set_mode, never act on Mode updates (display only).
  5. Late session/update notifications after TurnEnded are possible (capped at 256 by the unwanted-message counter): the writer must tolerate or drop them. Handle TurnFailed { auth_required } without ever sending credentials the user did not configure.
  6. The consumer must keep draining events (the actor waits up to consumer_stall, 60 s, with commands delayed). A stage-4 session end must call close or drop the handle on every exit path.

Known limits, for the next stages

  • Resume (session/resume, session/load) is not in this stage.

Staged plan (each PR independently mergeable, smallest and most security-relevant first)

  1. This PR: process launch + ACP client engine + fake-agent tests (inert).
  2. Launch descriptors for Claude Code with a pinned adapter, availability detection and the context delivered at session/new (system prompt append + nodespace mcp entry), still behind no caller.
  3. Update-to-message writer: mapping onto ai-chat-message parts (batched text, in-place tool updates, version-conflict retry, stop-reason system messages, permission answers as system messages), against the in-memory stream.
  4. Daemon: gRPC RPCs (launch with node_id, send prompt, cancel, answer permission, subscribe, terminate), session id vocabulary, end write (session_status, last_active).
  5. Resume: session/resume, fall back to load + replay suppression, fall back to a new session with a marker; startup recovery of stale active chats.
  6. Viewer wiring and the retype entry point (live data into the existing viewer); second harness (OpenCode) after.

Testing

  • 41 new tests (engine against the fake agent, wire reader, update parsing). Hostile cases: fs/terminal/unknown requests, permission flood and per-turn flood, oversize and repeated oversize messages, never-ending updates (event cap, byte cap, turn timeout, idle timeout), id mismatch and flood, non-JSON noise and flood, 5000-deep nesting, death mid-turn, ignored initialize, bad protocol version, missing session id, auth-required at startup and on a prompt, foreign-session updates, stderr flood, credential echo in a chunk and in an error, hung credential store, drop kills the process group.
  • Mutation checks (one batch, each caught by the named test): raw-byte redaction removed, request refusal removed, allow_always accepted, option validation removed, cancel not answering waiting permissions.
  • bun run quality:fix clean; bun run test:changed was queued behind another gate at PR time (result will be commented); the core gate runs via bun run merge.
  • Not individually run: the cancel-drain mutation (the run was starved by gate-slot queueing); its test asserts the cancelled resolution event and the cancelled answer. The drop-kill mutation is equivalent (the closed command channel also stops the actor).

🤖 Generated with Claude Code

mstomar125 and others added 2 commits November 20, 2026 08:43
…update stream (stage 1)

An in-memory session with one external agent over stdio JSON-RPC: launch
through the shared process helpers, initialize with no fs or terminal
capability, session/new, prompt turns as typed events, permission requests
surfaced (once-only choices), cancellation, and bounds on everything the
agent sends. Tested against a scripted fake agent, hostile cases included.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…e a permission waits

Credentials sent as JSON escapes bypassed the raw-byte pass; every surfaced
string is now cleaned after decoding, redacted before it is cut. The idle
clock does not run while a permission request waits and restarts when it is
resolved. Tests for both, and for a credential straddling the cut.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@mstomar125
mstomar125 merged commit 6ae6799 into main Nov 20, 2026
@mstomar125
mstomar125 deleted the issue-4292-session-1 branch November 20, 2026 07:43
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