Skip to main content

Module subprocess

Module subprocess 

Source
Expand description

SubprocessController: manages agent CLI as stateless per-turn subprocess invocations.

Architecture: Each user message spawns a fresh claude -p process. Multi-turn continuity uses --resume <session-id>. The process reads one JSON message from stdin, runs the agentic loop, streams NDJSON on stdout, then exits.

State machine: INIT ─(spawn)─> RUNNING ─(process exits)─> DONE DONE ─(new message)─> RUNNING (re-spawn with –resume)

I/O model (2 async tasks per turn):

  1. stdout_reader: piped stdout → .jsonl persistence + MPS blockfile events on “output” subject
  2. process_waiter: wait for exit, update status, publish lifecycle event

§Module layout

This module was split from a single ~1770-line subprocess file into a directory so each piece of the (large) turn state machines could live in its own file:

  • mod.rs (this file): spawn config, inner state, controller struct + bookkeeping (new/lock helpers/status snapshot/publish), and impl Controller for SubprocessController.
  • session: session-id capture/hydration state machine.
  • host_spawn: spawn_turn — host-subprocess turn via std::process, in named phases.
  • container_spawn: spawn_container_turn — Docker-exec turn via bollard, plus its publish_line output helper.
  • tests (cfg(test)): unit tests, mostly session-id focused.
  • turn_tests (cfg(test)): what a host turn does, with a stub CLI.

host_spawn and container_spawn are impl SubprocessController blocks in their own files — Rust allows multiple impl <Type> blocks across files/modules for the same type. spawn_container_turn takes a request in its file and runs the accepted turn through the steps in container_spawn_steps.

Modules§

argv
Provider-aware construction of per-turn subprocess arguments.
codex_compact
/compact for a Codex pane.
codex_context
How full a Codex thread’s context is, for the pane’s context meter.
codex_fresh
Agents fresh for a Codex or Gemini pane (docs/specs/SPEC_AGENT_SELF_COMPACTION_2026_10_23.md §3.3).
codex_mcp
The MCP servers a Codex exec turn gets: AgentMux’s own agentmux server and the catalog servers, from the .mcp.json the launch wrote in the agent’s folder, as -c mcp_servers.<name>.<key>=<toml> overrides. SPEC_CONNECTORS_MCP_WHAT_AGENTS_RUN_2026_10_10.md §5.
codex_usage
The calls a Codex thread made, read from its session file for the per-task token ledger (docs/specs/SPEC_TOKEN_ANALYSIS_PER_KANBAN_TASK_2026_11_22.md section 4.2). codex exec --json reports only the thread’s running totals, so the ledger reads the calls from Codex’s own file (the one codex_context.rs finds), once each turn has exited.
container_spawn
Container-exec turn spawn (spawn_container_turn) for SubprocessController.
gemini_mcp
The MCP servers a Gemini turn gets: AgentMux’s own agentmux server and the catalog servers, from the .mcp.json the launch wrote in the agent’s folder, as a settings file Gemini loads on top of its own. SPEC_CONNECTORS_MCP_WHAT_AGENTS_RUN_2026_10_10.md §5.
host_spawn
Host-subprocess turn spawn (spawn_turn) for SubprocessController.
session
Session-id capture and hydration state machine for SubprocessController.

Structs§

SubprocessController
SubprocessController manages per-turn subprocess lifecycle for agent blocks.
SubprocessSpawnConfig
Configuration for spawning a subprocess turn.

Constants§

BLOCK_CONTROLLER_SUBPROCESS
SUBPROCESS_OUTPUT_SUBJECT
MPS file subject name for subprocess output (replaces “term” from PTY).

Functions§

forget_left_for_replacement
Drop what a replaced controller left for block_id: the block is gone, so nothing will take it.

Type Aliases§

StartupStep
A container turn’s first step: ready the container. Err carries the text the pane has already been shown; the turn ends without running.