Skip to content

feat: stream background shell output live and convert client MCP servers to the backend's wire shape - #459

Merged
william0wang merged 10 commits into
mainfrom
docs/acp-v2-face-invariants
Dec 1, 2026
Merged

william0wang merged 10 commits into
mainfrom
docs/acp-v2-face-invariants

Conversation

@william0wang

@william0wang william0wang commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

Three reported problems on the ACP faces, plus the V2 background work this branch already carried and one fault found while verifying them:

  • ACP v2 session/new fails with the v2-spec mcpServers shape (T3 Code 0.1.13): -32602 mcpServers.0: Unrecognized key: "type" #457 — client mcpServers are converted to the backend's wire shape on both faces. The backend's session/create / session/resume take ONE strict shape (stdio {name, command, args, env: [{name, value}]} with no type / _meta; http/sse {name, type, url, headers: [...]}); forwarded verbatim, T3 Code's V2 {type: "stdio", …} failed every session create with Unrecognized key: "type". The SDK 2.12.0 bump had also dropped the SDK 1.x spelling (env / headers as string maps) from the V1 schema, so 0.98.0's V1 session/new answered -32602 where 0.97.0's took it. New src/config/client-mcp-servers.ts: toWireMcpServers accepts both spellings, the V1 session/new|resume|load|fork routes parse mcpServers themselves (v1SessionParams), and every face converts before any state is recorded — the shared fork() included (fix: answer V1 session/fork in the ACP draft's shape and populate fork scope #455 follow-up: a fork's next reload sent what it remembered verbatim, and the fork result's model list is now cached from snapshot.settings, where it actually lives).
  • Headless entitlement fails on ZCode desktop 4.0.0: credential store uses oauth:zai_cn:access_token + oauth:linked_bigmodel, all account:* coding-plan models unentitled #456 — desktop 4.0's regional login. A CN sign-in is the zai_cn provider and stores no oauth:bigmodel:access_token; the BigModel plan it is linked to rides oauth:linked_bigmodel ({userId, customerId, accessToken}), honored only while userId is the active profile's id and preferred over a direct sign-in's own token — as upstream's materialFromSnapshot does. codingPlanLoginToken (config/project-token.ts) is now the one reader of a family's plan login JWT (entitlement, the project-token exchange, official MCP auth, the reset cards).
  • A background shell's output streams while it runs. Upstream watches output progress for foreground runs alone and the end event's tail is the HEAD of the output file, so the V2 face reads the task's output file itself (every 500 ms, 64 KiB catch-up cut to a line start, chunks of at most 256 KiB never split inside a UTF-8 sequence, read to the end before the exit frame). Background agents are filed by taskKind, else by the childSessionId of their first event (SubagentSpawned); every upstream terminal status ends the V2 item.
  • An edit or a retry reaches the row it names on 4.0. A turn's rows share the entityId of the message they belong to — 4.0 leads a turn with a turnHeader row under the user message's id, and a reply's reasoning row carries the reply's id — so the first id match was a row without actions, and every V1 session/edit_user_message / session/retry_turn addressed by message id failed with the backend's guard.actionUnavailable. The target is now the row of the asked kind among the message's own rows (src/handlers/rewind.ts).

Closes #457
Closes #456

Verification

  • vitest run: 155 files / 1737 tests; tsc --noEmit clean; eslint . at 0 errors. Every new group was reverse-checked (the cases fail with the fix removed).
  • Shape matrix measured live against the real 4.0.0 backend with no model call (.zcode/scratch/v2-probes/live/mcp-shapes.live.ts).
  • Release e2e over stdio on the built dist/ (real backend, real HOME), model pinned to GLM-5.3-Flash. V2: 30 steps green (prompt, queue while running + cancel + sendNow, file-changing turn, change detail, file rewind, resume, fork, archive / list / unarchive, background shell + cancel, close); the 31st, _zcode/queue/setAutoDrain, answers the pre-command view by design and its push carries the assertion. V1: 12/12 steps (new, prompt, tail load, load_earlier, session/edit_user_message, metadata attach, idle cancel) — the edit step rewinds the latest user message, the backend runs the replacement turn and the reply renders; on 0.98.0 the same step failed with guard.actionUnavailable.
  • Through the real hub on this build (gpui-live probe, production AcpConnection + SessionCore): handshake, new, pin, one GLM-5.3-Flash turn; the background shell's output arrived as one terminal_output_chunk per second while it ran, exit frame last, session/close retired it.

Notes

…nd agents as subagents

- background.ts: a task that carries a childSessionId but no taskKind is a
  subagent — SubagentSpawned reaches the wire before the task event and was
  filed as "other" for good; output tails are cumulative snapshots, diffed
  before they are streamed as terminal_output_chunk; killed, stopped, lost,
  timed_out and spawn_error settle the task instead of leaving it running
- background-tasks.ts: the output tail was forwarded only on the reused
  launch-card path, which needs _meta.terminal_output (Zed) — every task
  event now reaches the V2 face, settled per session in event order
- tests: 9 new cases in tests/v2-background.test.ts
… output file

- background.ts: a running background shell publishes nothing on the wire
  while it runs (the adapter watches output progress for foreground runs
  alone) and the end event's tail is the HEAD of the output file — the V2
  face now tails the task's output file itself (every 500 ms, a 64 KiB
  catch-up cut to a line start, chunks of at most 256 KiB never split
  inside a UTF-8 sequence, read to the end before the exit frame) and skips
  the end tail once the file delivered anything
- background-tasks.ts: V2 notes settle per session in event order
  (settleV2) so two task events cannot land out of order
- tests: tests/v2-background.test.ts
… and authorization gotchas

- AGENTS.md: the V2 face targets ZCode 4.0 and later only; why a
  running background shell's output is not on the wire, how a background
  agent is told from other tasks by its first event, which client reuses a
  launch card, the archive HTTP path, the child transcript replay rule, who
  is offered MCP authorization's URL form, and the v4 query watermark
  behind the rewind reads
…faces

- #457: the backend's session/create takes ONE strict shape (a stdio
  entry without type or _meta and with env as a required name/value array,
  an http entry with headers as pairs); forwarded verbatim, T3 Code's V2
  entries failed every create, and the SDK 2.12.0 bump also dropped the
  SDK 1.x spelling from the V1 schema, so V1 session/new answered -32602
  where 0.97.0 took it
- client-mcp-servers.ts (new): toWireMcpServers accepts both spellings and
  rejects the rest with the entry's path; v1SessionParams lets the V1
  session/new|resume|load routes parse mcpServers themselves
- session.ts: convert at the top of newSession / resumeSession / loadSession,
  before any state is recorded, so a malformed entry leaves no placeholder
- v2/initialize.ts: advertise session.mcp.stdio and session.mcp.http
- tests: tests/client-mcp-servers.test.ts; AGENTS.md, docs/REMOTE-CLIENTS.md
- #456: a desktop 4.0 CN sign-in is the zai_cn provider and stores no
  oauth:bigmodel:access_token — the BigModel plan it is linked to rides
  oauth:linked_bigmodel ({userId, customerId, accessToken}), honored only
  while userId is the active profile's id and preferred over a direct
  sign-in's own token, as upstream's materialFromSnapshot does
- project-token.ts: codingPlanLoginToken is the one reader of a family's
  plan login JWT; official-mcp-auth.ts and settings/coding-plan.ts read
  through it
- tests: project-token.test.ts, account-provider.test.ts; AGENTS.md
Conflict in src/index.ts (buildV1App): #455 added a hand-copied
mcpServers schema for the V1 session/fork route where this branch parses
mcpServers on the V1 session routes itself (v1SessionParams, #457). The
fork route now uses the same parser. The shared fork() still stored the
entries raw at this commit; the next commit converts them.
…orking

- #455 follow-up: the shared fork() remembered a client's mcpServers
  verbatim, so a fork's next reload sent entries the backend's strict
  zcodeProtocolMcpServerSchema rejects — the #457 failure mode on the fork
  path; the V1 route's own copy of the SDK schema is gone with it
- extensions.ts: fork() converts first (toWireMcpServers), so a malformed
  entry neither forks in the backend nor leaves anything recorded
- index.ts: the V1 session/fork route parses mcpServers exactly as
  session/new does (v1SessionParams)
- session.ts: adoptForkedSessionScope takes the converted list
- tests: fork-response.test.ts, v2-ops.test.ts; AGENTS.md, docs/PROTOCOL.md
- fork() passed the whole fork result to cacheModelAvailability, but a
  fork result carries no top-level settings — they sit under `snapshot`
  (zcodeSessionForkResultSchema) — so the fork's model availability was
  never cached at fork time and an object-form session/setModel on a fresh
  fork resolved its level from the personal rule alone until the next
  resume flight
- tests: fork-response.test.ts
…d match

- a message's id is shared by the other rows of its turn: 4.0 opens a
  turn with a turnHeader row under the user message's id, and a reply's
  reasoning row carries the reply's id, so resolveEditRow and
  resolveRetryRow took the header (or the reasoning row) and every
  session/edit_user_message on 4.0 answered "only the latest user message
  can be edited" — the same for retry_turn by message id
- rewind.ts: rowsOfMessage narrows a handle to its message's rows and the
  target is the userInput / assistantText row among them; an unknown id
  still answers not found, a wrong-kind or non-editable one the guard
  message
- tests: a 4.0-shaped row window in tests/rewind.test.ts (read live from a
  one-turn session)
@william0wang
william0wang merged commit 9658d52 into main Dec 1, 2026
2 checks passed
@william0wang
william0wang deleted the docs/acp-v2-face-invariants branch December 1, 2026 03:20
Dopetaiga added a commit to Dopetaiga/zcode-acp that referenced this pull request Dec 1, 2026
…on, terminal-by-default statuses, gated stderr dump

Review: william0wang#453 (review)

- Rebased onto main (0.98.0 + william0wang#455 + william0wang#459); format-only reflows dropped;
  docs/handoff removed (repo docs are English-only).
- F1: one notification turn may serve SEVERAL finished tasks (upstream
  merges at the drain point) — the queue is snapshotted whole at
  turn.started, sealed onto the FIRST target only, the rest dropped;
  backend-side cancelled/killed/stopped never enter the queue (no result
  turn follows); synthetic <marker> notifications (stall reminders) never
  seal; markOutstanding/markCancelled purge queued + snapshotted targets.
- F2: _zcode/pendingBackgroundWork registered in the V2 private table
  (src/v2/tools.ts privateMethods) and advertised in v2AgentCapabilities
  _meta.zcode — reachable and declared on BOTH faces.
- F3: liveness is v2TaskOutcome (shared rule, no fourth copy): tracked
  rawStatus feeds the outstanding filters; toAcpStatus is terminal-by-
  default (timed_out/spawn_error/lost/stopped/killed settle the card as
  failed instead of spinning).
- F4: the stderr dump fires on SELF-exits only (teardown dumps raced
  process.exit and were shutdown noise); lines split on bare CR and
  truncated per line (512 chars); credential-shaped assignments are
  redacted; the hub skips re-emitting bridge stderr-dump lines (already
  diary'd by the bridge — were doubled).
- F5: the seal is skipped while a V2 connection holds the session
  (results arrive through the ordered V2 settle chain, not a late
  out-of-chain tool_call_update).
- F6: clamping is surrogate-safe (a cut landing inside a UTF-16 pair
  moves off the high surrogate — serde_json rejects lone surrogates).
- F7: bash resultText rides the bg_* card path too (outputTail ??
  stdoutTail), not just the Zed launch-card path.
- Also: the seal emission carries .catch (file style); the meta
  description echo is clamped (1000 chars) — a multi-KB subagent prompt
  no longer rides every status update.
- Tests: merged-turn seal/drift, cancelled-not-queued, failed result
  turn drops targets, stall-marker skip, V2-holder skip, five upstream
  terminal statuses (card + query), no-status-yet outstanding, surrogate
  clamp, meta description clamp, bg_* bash resultText, teardown-no-dump,
  CR split, per-line truncation, redaction, V2 face probe + declaration.
william0wang added a commit that referenced this pull request Dec 1, 2026
…s for it (#461)

On 4.0 the first event for a subagent is its spawn (SubagentSpawned, a
generic session.updated carrying childSessionId and no taskKind); the kind
only rides the task event that follows. The listener froze the kind at
first sight, so every real subagent task stayed kindless for good and the
held-prompt chain read "nothing outstanding": session/prompt returned while
the agent was still running (live: prompt at t+9.3s, the card completed at
t+15s). The V2 face already classified this way (kindOf, #459); the V1
listener now does the same, and a later sighting that names the kind
re-classifies the same tracked task.
Dopetaiga pushed a commit to Dopetaiga/zcode-acp that referenced this pull request Dec 1, 2026
…s for it

On 4.0 the first event for a subagent is its spawn (SubagentSpawned, a
generic session.updated carrying childSessionId and no taskKind); the kind
only rides the task event that follows. The listener froze the kind at
first sight, so every real subagent task stayed kindless for good and the
held-prompt chain read "nothing outstanding": session/prompt returned while
the agent was still running (live: prompt at t+9.3s, the card completed at
t+15s). The V2 face already classified this way (kindOf, william0wang#459); the V1
listener now does the same, and a later sighting that names the kind
re-classifies the same tracked task.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant