Skip to main content

Module blockcontroller

Module blockcontroller 

Source
Expand description

Block controller: manages lifecycle of each block (terminal, command, web app). Port of Go’s pkg/blockcontroller/blockcontroller.go.

Architecture:

  • Global controller registry maps block_id → Controller
  • Each controller manages the lifecycle of one block
  • ShellController handles “shell” and “cmd” block types
  • Controllers dispatch I/O between the user and the process/service

Modules§

acp
AcpController: manages agent CLIs that speak the Agent Client Protocol (ACP).
acp_mcp
The MCP servers an ACP agent gets in session/new and session/load: the ones in its .mcp.json (AgentMux’s own agentmux server and the user’s), in ACP’s shape. SPEC_NINE_NEW_HARNESSES_2026_10_14.md §5 item 3.
acp_prelaunch
Files two ACP agents need in their own config dirs before they start. SPEC_NINE_NEW_HARNESSES_2026_10_14.md §7.5 (Grok Build), §7.6 (DeepSeek Harness).
agent_lock
In-memory registry of PtyShell agent leases — the authoritative, hot-path answer to “is a human’s keystroke for this block currently locked out?”.
app_server
Provider-neutral App Server process and JSONL/JSON-RPC transport.
app_server_controller
First-class controller shell for a Codex App Server process.
app_server_protocol
Codex App Server’s stable thread/turn adapter.
core
Shared helpers used by all block controllers (persistent, subprocess, acp).
durable_mode
Whether a shell pane runs durable (SPEC_REMOTE_TERMINALS_AND_DURABLE_SESSIONS_2026_10_02.md §7.1): plain, durable because a setting says so, or durable by default.
durable_ssh
Durable SSH terminals (P4 of SPEC_REMOTE_TERMINALS_AND_DURABLE_SESSIONS_2026_10_02.md, §7).
health
Per-block turn-activity tracking (is a turn currently in flight, and the last process exit code). Previously part of a larger “agent health” detector that also did silence-based unresponsive detection; that detection logic was removed (see docs/specs/SPEC_REMOVE_AGENT_UNRESPONSIVE_DETECTION_2026_08_25.md) — this struct keeps only the turn-active bookkeeping, which several other subsystems depend on independently of the removed detector (broker::process::lifecycle_from, the Swarm pane’s running/idle badge, subagent-watcher reconciliation, muxspect describe).
loop_stop
What follows a turn the loop guard (agents::loop_guard) ended, the same for every controller: a note held for the agent’s next turn, and one to its leader (docs/specs/SPEC_ROUTE_AGENT_BRAKES_2026_11_10.md section 5). How the turn is ended is the controller’s own: Claude’s control-protocol interrupt (persistent/loop_stop.rs), ACP’s session/cancel (acp_spawn.rs).
msp
MspController: Muse Code over the Muse Session Protocol (MSP), muse serve’s newline-delimited JSON-RPC 2.0 on stdio. Shaped like the ACP controller (acp.rs): one long-lived process, one session in it, each user message a turn, every line the host writes kept in the pane’s transcript for the frontend’s msp-translator.ts. The protocol state lives in msp_session.rs; this file spawns the process and moves lines.
msp_protocol
The Muse Session Protocol (MSP) messages AgentMux sends to muse serve, and the MCP servers it passes in them. Checked against Muse Code 1.4.14-R7309.1 (muse schema generate-ts; against 1.4.7 the stable surface only gained items, hook names and one capability), SPEC_NINE_NEW_HARNESSES_2026_10_14.md §7.7.
msp_session
The MSP client’s state: the handshake, the session, which turns are still running, and what each line from muse serve asks of the controller. No I/O, so the controller’s whole reaction to a line is testable without a process (MspController in msp.rs does the I/O).
persistent
PersistentSubprocessController: manages agent CLI as a long-running process with bidirectional NDJSON streaming via stdin/stdout.
persistent_resume
One spawn attempt’s stale---resume retry decision, plus any terminal error-result line observed for it, as a single explicit state machine.
pidregistry
Block PID registry: maps blockId → OS process ID for per-pane metrics collection. Used by the sysinfo loop to query per-process CPU/memory stats.
process_tree
Process tree traversal: BFS from a root PID to collect all descendant PIDs. Used by the sysinfo loop to aggregate CPU/memory across entire process trees.
prompt_record
The transcript record for a message srv delivered to an agent, with where it came from (docs/specs/SPEC_MESSAGE_ORIGIN_AND_ONE_CONTAINER_2026_10_25.md §8, P3).
resync_lock
One resync of a block at a time. resync_controller looks the block’s controller up, decides, and then creates and starts one; two of them for the same block at once both saw “no controller, or one still in init” and both started it. Seen on a fresh Muse install: a forced resync and an ordinary one 74 ms apart spawned muse serve twice, 28 ms apart, and the first process was thrown away.
resync_steps
The steps of resync_controller, one function each: pick the effective controller type, decide what to do with a controller the block already has, then build and start a new one of the right type. Moved out of resync_controller without a change in behaviour.
session_agent
The controllers that keep one agent process and a protocol session in it: ACP agents (acp) and Muse Code over MSP (msp). The message paths (agentinput, agent.send, reactive delivery) treat them alike: start the agent again if it exited, then hand it the message.
session_recovery
Session recovery after unclean shutdown (Phase 4.2 — ultra-long-sessions).
session_stats
Session metadata tracking for agent panes (Phase 1.4 — ultra-long-sessions).
shell
ShellController: manages lifecycle of shell and command blocks. Port of Go’s pkg/blockcontroller/shellcontroller.go.
subprocess
SubprocessController: manages agent CLI as stateless per-turn subprocess invocations.
term_size
The size each block’s PTY was last given: at spawn (its runtime options) and on every resize. Kept so a reader that doesn’t drive the terminal (a paired device watching it) can render at the desktop’s width; the PTY itself keeps no record srv can ask.
watchdog
Agent process watchdog: kills agent panes that exceed max-runtime or idle-output limits.

Structs§

BlockControllerRuntimeStatus
Runtime status of a block controller, sent to the UI.
BlockInputUnion
Input sent to a block controller. Can be raw terminal data, a signal, or a resize event.
MessageAcceptedEvent
The agent-message-accepted event’s payload (mps_events::AgentMessageAccepted).
MessageQueuedEvent
The agent-message-queued event’s payload (mps_events::AgentMessageQueued).
PendingFacts
What Controller::pending_for_devices reports.
PendingRequest
A request parked for the user’s answer: its id as the transcript names it (the tool use, or the App Server’s item), and its kind: question (a question for the person) or approval (a tool or command to allow).

Enums§

AgentDelivery
How a controller-aware agent message was delivered.
SendOutcome
What an automated send to a persistent agent did with one message.
StopOutcome
How a Controller::shutdown ended. Logged per closed agent (spec §9.8) — a provider that is often Killed needs a better shutdown or a longer grace period.

Constants§

BLOCK_CONTROLLER_ACP
BLOCK_CONTROLLER_APP_SERVER
BLOCK_CONTROLLER_CMD
BLOCK_CONTROLLER_MSP
Muse Code over MSP (muse serve): msp.rs.
BLOCK_CONTROLLER_PERSISTENT
BLOCK_CONTROLLER_SHELL
BLOCK_CONTROLLER_SUBPROCESS
DEFAULT_GRACEFUL_KILL_WAIT_MS
Grace period before forceful kill in milliseconds.
DEFAULT_TIMEOUT_MS
Default controller operation timeout in milliseconds.
META_KEY_CMD
META_KEY_CMD_ARGS
META_KEY_CMD_CLEAR_ON_START
META_KEY_CMD_CLOSE_ON_EXIT
META_KEY_CMD_CLOSE_ON_EXIT_DELAY
META_KEY_CMD_CLOSE_ON_EXIT_FORCE
META_KEY_CMD_CWD
META_KEY_CMD_ENV
META_KEY_CMD_INIT_SCRIPT
META_KEY_CMD_INIT_SCRIPT_BASH
META_KEY_CMD_INIT_SCRIPT_FISH
META_KEY_CMD_INIT_SCRIPT_PWSH
META_KEY_CMD_INIT_SCRIPT_ZSH
META_KEY_CMD_JWT
META_KEY_CMD_RUN_ONCE
META_KEY_CMD_RUN_ON_START
META_KEY_CMD_SHELL
META_KEY_CONNECTION
META_KEY_CONTROLLER
META_KEY_SHELL_SUBBLOCK_ID
On an agent block: the block id of its shell drawer, the headless terminal the drawer, the agent’s PtyShell and a paired device’s pane list share.
META_KEY_TERM_LOCAL_SHELL_OPTS
META_KEY_TERM_LOCAL_SHELL_PATH
RESYNC_ERR_ALREADY_EXITED
Sentinel error string resync_controller returns when it declines to respawn a STATUS_DONE controller because its caller passed respawn_if_done: false (see that parameter’s own doc comment). Callers that want to treat “already exited” differently from a genuine failure (e.g. try_attach_to_existing_shell falling through to a fresh shell instead of surfacing a 500) match on this exact string rather than parsing every other possible error message resync_controller can return.
SHUTDOWN_GRACE
Grace period for closing agents: one deadline per close, shared by every agent it stops (spec §9.2). Matches the kill task’s long-standing 5s.
STATUS_DONE
STATUS_INIT
STATUS_RUNNING

Traits§

Controller
Trait for block controllers. Each block type has its own implementation. Port of Go’s blockcontroller.Controller interface.

Functions§

closing_blocks_still_running
Blocks mid-close whose process has not exited yet. Their controllers have already left CONTROLLER_REGISTRY, so a registry scan can’t see them.
cmd_env_of
A block’s cmd:env as a string map. The frontend writes an object; the JSON-string form is accepted too, so every spawn path reads the same env.
delete_controller
Unregister (delete) a controller by block ID, stopping it first. Removes from the registry before calling stop() so no new callers can reach it.
deliver_agent_message_from
[deliver_agent_message], saying who the input is from (turn provenance, SPEC_AGENT_SELF_QUIT §6.3). Only the persistent controller records it (see send_user_message_outcome_from); ACP and App Server turns carry no turn provenance, but both store from with the message in the transcript.
get_all_controllers
Get all controllers (snapshot).
get_block_controller_status
Get the runtime status for a block’s controller. Returns None if no controller is registered.
get_controller
Get a controller by block ID.
is_closing
mark_closing
Refuse to (re)spawn a controller for block_id until unmark_closing.
mark_closing_stopped
Signal that a closing block’s process has exited. The block stays marked closing (no respawn) until unmark_closing.
meta_string_map
A meta value that is an object of strings, or a JSON string encoding one, as a string map. Non-string values are skipped; any other shape is empty.
pane_note
A line from AgentMux in a terminal pane’s output: dimmed, in brackets, on a line of its own, so it reads as AgentMux’s and not the shell’s.
publish_controller_status
Publish a controller status event via MPS broker. The sole publish point for controllerstatus, used by every controller type (persistent CLI, subprocess CLI, ACP agents, plain shell/PTY panes — 13 call sites).
register_controller
Register a controller, stopping any previous one for the same block.
release_block_processes
Drop the block’s process tracker (killing anything left in its tree) and its Process Broker entry. The second half of delete_controller; a graceful close calls it only after the process has exited.
resync_controller
Resync a block’s controller — the main entry point for starting/restarting blocks. Port of Go’s ResyncController.
send_input
Send input to a block’s controller.
set_close_on_exit_handler
Install the close-on-exit callback. Called exactly once, from bootstrap::install_close_on_exit_handler right after AppState is built in main.rs — see that function’s doc comment for what the callback actually does. A second call is a no-op (OnceLock::set silently returns Err on an already-initialized cell, which this discards — matching broker::process::set_global’s identical fire-and-forget style; there is exactly one legitimate caller).
stop_all_controllers
Stop all running controllers gracefully.
stop_block_controller
Stop a block’s controller gracefully.
system_note_into_turn_or_hold
srv’s own note text for the agent in block_id, about the work at hand: into its running turn where the controller can join one atomically (a persistent agent: PersistentSubprocessController::note_into_turn_or_hold), else held under key for its next turn. It never starts a turn; the other controllers can only queue a message as a turn of its own, so theirs is always held. Returns whether it joined a turn.
take_controller
Remove a controller from CONTROLLER_REGISTRY and hand it back, without stopping it — for the graceful close, which awaits Controller::shutdown and only then calls release_block_processes. Once removed, no new input reaches it.
unmark_closing
Lift mark_closing. Called once the block’s record is gone (or the close failed and the block stays). Wakes anyone still waiting.
wait_closing_stopped
Wait until a closing block’s process has exited, or timeout. Returns immediately for a block that isn’t closing.

Type Aliases§

ShutdownFuture