Expand description
PersistentSubprocessController: manages agent CLI as a long-running process with bidirectional NDJSON streaming via stdin/stdout.
Architecture: A single CLI process is spawned on first message and kept alive for the entire session. User messages are written as NDJSON lines to stdin without closing it. This enables mid-turn input (redirecting the agent while it is still processing).
State machine: INIT ─(first message)─> RUNNING ─(idle between turns)─> RUNNING RUNNING ─(kill/stop)─> DONE RUNNING ─(process crash)─> DONE (auto-restart possible via session_id)
I/O model (3 async tasks per session):
- stdin_writer: mpsc channel → process stdin (NDJSON lines)
- stdout_reader: process stdout → .jsonl persistence + MPS blockfile events
- process_waiter: wait for exit, update status
Module layout (split 2026-09-22; one impl PersistentSubprocessController
block per file, all private methods pub(super)):
mod.rs— types, shared helpers, construction,Controllerimplstatus.rs— runtime status snapshot/publication and heartbeateager_resume.rs— spawn-on-construction when a session id is on filequeue.rs—send_message, spawn claim, queue drain/replayresume_retry.rs— stale---resumerecovery effects and retry batchinput.rs— user messages, question answers, tool decisions, stdinspawn.rs—spawn_process: builds the command, spawns, wires the tasksstderr_reader.rs,stdout_reader.rs,process_waiter.rs— the bodies of those per-session I/O taskslifecycle.rs— stop/restart,session_id,needs_spawntests/— one file per former inline test module
Re-exports§
pub use resume_retry::pane_history_session_id;
Modules§
- eager_
resume - Eager resume: spawning the CLI on controller construction (before the first message) when a prior session id is on file, so the first turn is not charged the full cold-start.
- input
- User-facing input surface: user messages, AskUserQuestion answers/denials, tool-permission decisions, raw stdin, and inbound control frames.
- lifecycle
- Stop/restart and session-identity queries:
stop_process, the kill request path,session_id,needs_spawn, and the deferred-restart flag. - outcome_
line - The
agentmux_session_outcomeframe a persistent controller writes when a resume’s fate is known. - process_
waiter - The persistent CLI’s process waiter task, started by
spawn_process(spawn.rs): thechild.wait()arm (the process exited on its own) and the kill arm (a stop or restart asked for it). - queue
- Message intake and the spawn-claim/queue-drain protocol:
send_messageand everything that decides whether a message is written to stdin now, queued behind an in-flight spawn, or replayed after one. - queued
- The two kinds of message the persistent controller holds before it writes
them to the process: the human path’s queue around a spawn
([
QueuedMessage]) and the deliveries waiting for the process to take a write ([Deferred]). Each carries where the message came from, for the record written when it is delivered. - resume_
retry - Stale-
--resumerecovery: executing the effectspersistent_resumedecides, locating a recoverable session id, and the retry-batch rewrite. - segments
- This controller’s side of the segment index
(
backend::continuity_segments): each spawn is one segment of its agent’s conversation. Best-effort throughout: a failed write is logged and never affects the process. - spawn
spawn_process: builds the CLI argv/env, spawns the child, and starts the per-session I/O tasks (stdin writer, stdout reader, stderr reader, waiter). The stdin writer runs inline; the other three bodies live instderr_reader.rs,stdout_reader.rsandprocess_waiter.rs.- spawn_
steps - The steps of
spawn_process, one function each, in the order they ran: launch the child, take its pipes, mark the process running, register the spawn, start the stdin writer, the stdout reader and the process waiter. Moved out ofspawn_processwithout a change in behaviour. - status
- Runtime status: the
BlockControllerRuntimeStatussnapshot, its publication to the frontend, the status heartbeat, and turn-activity marking. - stderr_
reader - The persistent CLI’s stderr reader task, started by
spawn_process(spawn.rs): logs every line and reacts to a stale--resume. - stdout_
reader - The persistent CLI’s stdout reader task, started by
spawn_process(spawn.rs): control frames, session-id capture, resume-state events, API retry state, the API’s reachability, failure classification, the task feed and the output append.
Structs§
- Persistent
Spawn Config - Configuration for spawning the persistent process.
PartialEqis derived forpersistent_resume::RetryPayload’s own derive and its exhaustive unit tests — not used elsewhere in this file itself. - Persistent
Subprocess Controller - PersistentSubprocessController keeps a long-running CLI process alive, sending user messages as NDJSON lines on stdin.
Constants§
- ASK_
USER_ QUESTION_ DENY_ MESSAGE - Fixed, server-owned decline message for a cancelled AskUserQuestion (the
Cancel button / Escape in
AgentQuestionPanel.tsx) — not client-suppliable, since Cancel and Escape both mean exactly one thing and there is no user- authored content to carry. Seedeny_questionand docs/specs/SPEC_AGENT_CONTROL_PROTOCOL_2026_06_15.md. - BLOCK_
CONTROLLER_ PERSISTENT - PERSISTENT_
OUTPUT_ SUBJECT - MPS file subject name for persistent subprocess output.
Functions§
- held_
elsewhere_ error - The one spawn refusal that is a decision, not a failure:
--resume <sid>was asked for while another pane still holds (or is still closing) that same conversation —session_held_elsewhere. Built here so thatis_held_elsewhere_erroris checked against the same textspawn_processactually returns, rather than a second copy that could drift;tests/reopen_guard.rspins the wording users see. - is_
held_ elsewhere_ error - Did
spawn_processrefuse because ofsession_held_elsewhere? The answer decides whether a caller may fall back to a FRESH spawn: for any other failure a blank session is a reasonable recovery, but for this one it silently hands the user’s accepted prompt to a new conversation while the real one is open next door — exactly what the guard exists to prevent (codex P1 on PR #3551).