Skip to main content

Module persistent

Module persistent 

Source
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):

  1. stdin_writer: mpsc channel → process stdin (NDJSON lines)
  2. stdout_reader: process stdout → .jsonl persistence + MPS blockfile events
  3. 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, Controller impl
  • status.rs — runtime status snapshot/publication and heartbeat
  • eager_resume.rs — spawn-on-construction when a session id is on file
  • queue.rs — send_message, spawn claim, queue drain/replay
  • resume_retry.rs — stale---resume recovery effects and retry batch
  • input.rs — user messages, question answers, tool decisions, stdin
  • spawn.rs — spawn_process: builds the command, spawns, wires the tasks
  • stderr_reader.rs, stdout_reader.rs, process_waiter.rs — the bodies of those per-session I/O tasks
  • lifecycle.rs — stop/restart, session_id, needs_spawn
  • tests/ — 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_outcome frame 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): the child.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_message and 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---resume recovery: executing the effects persistent_resume decides, 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 in stderr_reader.rs, stdout_reader.rs and process_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 of spawn_process without a change in behaviour.
status
Runtime status: the BlockControllerRuntimeStatus snapshot, 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§

PersistentSpawnConfig
Configuration for spawning the persistent process. PartialEq is derived for persistent_resume::RetryPayload’s own derive and its exhaustive unit tests — not used elsewhere in this file itself.
PersistentSubprocessController
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. See deny_question and 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 that is_held_elsewhere_error is checked against the same text spawn_process actually returns, rather than a second copy that could drift; tests/reopen_guard.rs pins the wording users see.
is_held_elsewhere_error
Did spawn_process refuse because of session_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).