From 7ee5538ed634fabe5cba6f8c153a69bc209f9d9e Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 29 Oct 2026 11:59:26 -0700 Subject: [PATCH 01/10] test(acpmux): turn cap for sessions that take many agents' messages (red) The turn cap's setting (SessionMeta.turnCapMs, _acpmux/set_turn_cap, acpmux session turn-cap) and its tests, with the cap not yet applied to prompts: the tests past the cap fail. Co-Authored-By: Claude Opus 5.5 Agent-Session: https://cmux.com/deeplink/agent?id=aid-1288bbb2b5d2f5c6b66a --- cmux-tui/crates/acpmux/README.md | 1 + .../crates/acpmux/docs/acpmux-schema.json | 3 +- .../crates/acpmux/spec/acpmux-protocol.json | 12 + cmux-tui/crates/acpmux/src/cli/command.rs | 20 ++ cmux-tui/crates/acpmux/src/cli/mod.rs | 1 + cmux-tui/crates/acpmux/src/cli/run/extra.rs | 11 + .../crates/acpmux/src/cli/session_turn_cap.rs | 82 ++++++ cmux-tui/crates/acpmux/src/hub/fork.rs | 2 + cmux-tui/crates/acpmux/src/hub/mod.rs | 3 + cmux-tui/crates/acpmux/src/hub/resolve.rs | 1 + cmux-tui/crates/acpmux/src/hub/session.rs | 4 + cmux-tui/crates/acpmux/src/hub/turn_cap.rs | 139 +++++++++ .../crates/acpmux/src/hub/turn_cap_tests.rs | 269 ++++++++++++++++++ .../crates/acpmux/src/prompt_provenance.rs | 3 +- cmux-tui/crates/acpmux/src/protocol/mod.rs | 1 + cmux-tui/crates/acpmux/src/rpc.rs | 3 + cmux-tui/crates/acpmux/src/server/requests.rs | 25 ++ cmux-tui/crates/acpmux/src/store.rs | 5 + .../acpmux/protocol/generated/methods.ts | 4 + 19 files changed, 587 insertions(+), 2 deletions(-) create mode 100644 cmux-tui/crates/acpmux/src/cli/session_turn_cap.rs create mode 100644 cmux-tui/crates/acpmux/src/hub/turn_cap.rs create mode 100644 cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs diff --git a/cmux-tui/crates/acpmux/README.md b/cmux-tui/crates/acpmux/README.md index 6a7d39c364d0..1ea87408dcd0 100644 --- a/cmux-tui/crates/acpmux/README.md +++ b/cmux-tui/crates/acpmux/README.md @@ -301,6 +301,7 @@ acpmux pending # every pending pe acpmux session deny NAME # an allow is the cmux app's alone (the chat's permission card) acpmux session rules NAME '{"autoDeny": ["rm -rf"], "ask": ["execute"], "default": "approve"}' acpmux session limits NAME [--max N | --clear] # sub-sessions running at once (more wait), running and queued now +acpmux session turn-cap NAME [--minutes N | --off | --clear] # past it, other agents' messages wait for the next turn acpmux session tag NAME task=review --ttl 3600; acpmux ls --tag task=review acpmux ls --status running | --pending # filters; --json for the full records acpmux ls --limit 20 --fields name,harness,model,status,cwd # newest sessions, selected fields diff --git a/cmux-tui/crates/acpmux/docs/acpmux-schema.json b/cmux-tui/crates/acpmux/docs/acpmux-schema.json index f9a5c474bacf..a99bb9bacd75 100644 --- a/cmux-tui/crates/acpmux/docs/acpmux-schema.json +++ b/cmux-tui/crates/acpmux/docs/acpmux-schema.json @@ -85,6 +85,7 @@ "_acpmux/permission_chat_revoke": {"params":{"sessionId":"string"}, "result":{"active":false}, "note":"Revokes future chat approvals; already accepted operations are not undone."}, "_acpmux/set_policy": {"params": {"sessionId": "string", "policy": "ask|approve-reads|approve-edits|approve-all|deny-all"}, "result": "SessionSummary"}, "_acpmux/set_rules": {"params": {"sessionId": "string", "rules": "{autoApprove?: string[], autoDeny?: string[], ask?: string[], default?: 'approve'|'deny'|'ask'} | null"}, "result": "SessionSummary"}, + "_acpmux/set_turn_cap": {"params": {"sessionId": "string", "turnCapMs": "number? (ms; 0 turns the cap off) | null (clears the session's own value) | absent (reads it)"}, "result": {"sessionId": "string", "turnCapMs": "number? (the session's own value)", "effectiveTurnCapMs": "number? (the cap in force: its own, else the default its settings give; absent: no cap)"}, "errors": "data.reason permission.person_required: a write from anything but the app or the local unix socket with no agent caller", "note": "hub/turn_cap.rs. Past the cap, the running turn takes no other agent's steer: agent messages wait in the queue and the next turn takes them together (a steerOnly one is refused steer.unavailable with data.turnCap true). The person's own steers still go in. The hub steers one notice per turn (provenance kind turn_cap) once an agent message waits, and never cancels the turn."}, "_acpmux/set_tools": {"params": {"sessionId": "string", "id": "string (a ToolRow id)", "enabled": "boolean"}, "result": {"sessionId": "string", "tools": "ToolRow[]", "toolsMutable": "true", "applied": "now|afterTurn|nextStart (now: the idle chat's Claude Code restarted on it and resumed; afterTurn: a turn runs, and its process restarts when it ends; nextStart: no process runs, and the next start takes it)"}, "errors": "data.reason tools.immutable: the harness takes its tools when the chat starts (toolsMutable false: every ACP harness; refused without starting the chat); tools.fixed: canToggle false (a skill, a plugin, or a server the user's own Claude config turned off); tools.remote: a remote WebSocket connection (only the app or the unix socket); code -32002: no such row", "note": "Records the chat's choice for one MCP server: every start of its Claude Code gets --disallowedTools mcp__ for each server that is off, so it survives respawn, resume and daemon restart, and a fork keeps it. Claude Code's mcp_toggle is never sent (it persists into ~/.claude.json for every session in the folder). Watchers get _acpmux/session_changed kind tools. initialize lists the feature toolInventory (cx-thur3)."}, "_acpmux/mcp/reconnect": {"params": {"sessionId": "string", "server": "string (an MCP server's name, its ToolRow id without mcp:)"}, "result": "{}", "errors": "data.reason mcp.unsupported: the chat's harness is not Claude Code; tools.remote: a remote WebSocket connection (only the app or the unix socket); invalid params: no server, or the chat's Claude Code is not running (its servers connect when it starts); code -32002: the chat has no such server; internal: Claude Code refused (its error) or did not answer in 30 s", "note": "Sends Claude Code's mcp_reconnect control request (serverName = server) to the running process; never starts one. Once Claude Code reconnected it, its ToolRow status (and mcpServers) reads connected until the next system/init, and watchers get _acpmux/session_changed kind tools. initialize lists capability mcp-reconnect-v1."}, "_acpmux/archive": {"params": {"sessionId": "string", "archived": "boolean"}, "result": "SessionSummary", "note": "The chat menu's Archive (true: sets the `archived` tag to now in milliseconds; the lists hide the chat) or Unarchive (false: removes it). One tag with one value, so a page relay allows it without reading tag names (cx-jlzsf)."}, @@ -152,7 +153,7 @@ "turn_result": {"status": "completed|cancelled|failed", "stopReason": "string?", "turnId": "string (shared with turn_started)", "turnSeq": "number (seq of turn_started)", "promptId": "string", "error": "string?", "code": "number?", "errorText": "string? (why the turn failed; a terminal error the harness reported in-band, such as Codex _meta.codex.error without willRetry, fails the turn and the prompt is answered with that error)", "errorCode": "number|object|null", "errorSource": "agent|codex|acpmux (acpmux: a stalled turn acpmux ended)", "errorChunkSeqs": "number[]? (the trailing agent_message_chunk records whose text is exactly errorText: hide them, show the error)", "detail": "string? (outcome_unknown when the daemon restarted mid-turn; stalled when its model showed no sign of work for config stalledTurnEndMs, default 21600000, 0: never, four times that inside a tool call that had not ended, and nobody held the turn: hub/stalled_turn.rs)", "stalledMs": "number? (with detail stalled: how long the model had been silent)", "model": "string? (the session's model when an answered turn ended)", "costUsd|inputTokens|outputTokens|cacheReadTokens|cacheWriteTokens": "TurnUsage fields of an answered turn, as its harness reported them (a fork's log copies its parent's records before its forked record: count those once)", "gitBranch": "string? (the branch checked out in the session's cwd when the turn ended, answered or failed; absent on a detached HEAD, outside a repository, when git could not be read within 2 s, and in older logs)", "gitHead": "string? (the full HEAD commit at that moment, as gitBranch)"}, "git": {"gitBranch": "string|null (the branch the session's cwd has checked out now; null on a detached HEAD or outside a repository)", "note": "Written when the branch changes while a client holds the chat (a watch on the folder's HEAD, hub/git_watch.rs; no polling), and on the next attach for a change made while none did. Never for a branch the log's last git record already told, a daemon restart included. The branch the session started on (session.json gitBranch) never changes."}, "EventKinds": "string[]: each entry is a category or an exact record kind; a record passes when any entry matches. Categories: transcript (agent session/update except .replay, usage_update, available_commands_update, current_mode_update, config_option_update; plus mux user_message, queued, dequeued, turn_started, turn_silent, turn_heard, turn_result, permission_request, permission_auto, permission_decision, permission_group, permission_chat_allowance, message_superseded, failover, policy_unenforced, forked, imported, mode, model, stopped, exited), mux (every mux record), wire (in/out requests, responses, harness lines and .replay records), all.", - "user_message": {"text": "string", "client": "string", "promptId": "string", "turnId": "string", "steer": "boolean? (steered into the running turn)", "images": "[{id, mimeType, name?}]? (the prompt's images, kept under their ids; _acpmux/attachment reads one back)", "inlineParts": "[{kind: text, text, pasted?: true} | {kind: image, id, mimeType, name?, persisted?: false} | {kind: pasted|file, name, mimeType?, size?, text?} | {kind: document|audio, name, mimeType?, size?, pageCount?} | {kind: file, name, uri?, mimeType?}]? (ACP prompt blocks in original order; attachment data is never stored; text on file/pasted parts is copied from the corresponding ACP text block)", "pasted": "[{start: number, end: number}]? (each text block the client marked _meta.cmux.pasted, as UTF-16 code-unit offsets into text, so text.slice(start, end) is the paste; absent when nothing was pasted)", "_meta.provenance": "{kind: \"agent_message\", from, fromName?, to, msgId, threadId, replyTo?}? (the prompt's _meta.acpmux.provenance: an agent's message, not the person's words)"}, + "user_message": {"text": "string", "client": "string", "promptId": "string", "turnId": "string", "steer": "boolean? (steered into the running turn)", "images": "[{id, mimeType, name?}]? (the prompt's images, kept under their ids; _acpmux/attachment reads one back)", "inlineParts": "[{kind: text, text, pasted?: true} | {kind: image, id, mimeType, name?, persisted?: false} | {kind: pasted|file, name, mimeType?, size?, text?} | {kind: document|audio, name, mimeType?, size?, pageCount?} | {kind: file, name, uri?, mimeType?}]? (ACP prompt blocks in original order; attachment data is never stored; text on file/pasted parts is copied from the corresponding ACP text block)", "pasted": "[{start: number, end: number}]? (each text block the client marked _meta.cmux.pasted, as UTF-16 code-unit offsets into text, so text.slice(start, end) is the paste; absent when nothing was pasted)", "_meta.provenance": "{kind: \"agent_message\", from, fromName?, to, msgId, threadId, replyTo?}? (the prompt's _meta.acpmux.provenance: an agent's message, not the person's words); hub-set {kind: \"turn_cap\", turnId, capMs} on the one notice steered into a turn past its turn cap (_acpmux/set_turn_cap), never a client's"}, "queued": {"text": "string", "client": "string", "position": "number", "promptId": "string", "turnId": "string", "images": "[{id, mimeType, name?}]? (as user_message: the prompt's images, kept under their ids when it was queued; an _acpmux/queue_edit update records its own on queue_updated)", "inlineParts": "as user_message.inlineParts (fresh ordered metadata from queued blocks)", "pasted": "[{start, end}]? (as user_message; an _acpmux/queue_edit update records its own on queue_updated)", "_meta.provenance": "object? (as user_message; a restart keeps it, an _acpmux/queue_edit update drops it)"}, "dequeued": {"promptId": "string", "turnId": "string", "queued": "number (still waiting)"}, "queue_removed": {"promptId": "string", "turnId": "string", "queued": "number (still waiting)", "reason": "withdrawn? (_acpmux/prompt_withdraw; absent for _acpmux/queue_remove)"}, diff --git a/cmux-tui/crates/acpmux/spec/acpmux-protocol.json b/cmux-tui/crates/acpmux/spec/acpmux-protocol.json index 5eecea2f7442..b01e335801a3 100644 --- a/cmux-tui/crates/acpmux/spec/acpmux-protocol.json +++ b/cmux-tui/crates/acpmux/spec/acpmux-protocol.json @@ -419,6 +419,12 @@ "params": true, "result": true }, + { + "kind": "request", + "name": "_acpmux/mesh_control", + "params": true, + "result": true + }, { "kind": "request", "name": "_acpmux/step_now", @@ -928,6 +934,12 @@ "params": true, "result": true }, + { + "kind": "request", + "name": "_acpmux/set_turn_cap", + "params": true, + "result": true + }, { "kind": "request", "name": "_acpmux/person_enroll", diff --git a/cmux-tui/crates/acpmux/src/cli/command.rs b/cmux-tui/crates/acpmux/src/cli/command.rs index dd45008d560b..46fb3041d29b 100644 --- a/cmux-tui/crates/acpmux/src/cli/command.rs +++ b/cmux-tui/crates/acpmux/src/cli/command.rs @@ -432,6 +432,8 @@ pub enum Command { #[command(hide = true)] LimitsCmd { session: String, max: Option, clear: bool }, #[command(hide = true)] + TurnCapCmd { session: String, minutes: Option, off: bool, clear: bool }, + #[command(hide = true)] Schema, #[command(hide = true)] Models { @@ -880,6 +882,21 @@ pub enum SessionCmd { #[arg(long)] clear: bool, }, + /// The turn cap: past it, the running turn takes no other agent's message (they wait for the + /// next turn, and the agent is told once to end its turn); the person's still go in. Omit + /// the flags to show it; setting it takes the app or this socket, never an agent. + TurnCap { + session: String, + /// The cap in minutes (1 to 1440). + #[arg(long, value_parser = clap::value_parser!(u64).range(1..=1440), conflicts_with_all = ["off", "clear"])] + minutes: Option, + /// No cap for this session, whatever its default. + #[arg(long, conflicts_with = "clear")] + off: bool, + /// Drop the session's own value: back to its default. + #[arg(long)] + clear: bool, + }, /// One line per turn. History { session: String, @@ -1156,6 +1173,9 @@ pub fn flatten(c: Command) -> Command { SessionCmd::Limits { session, max, clear } => { Command::LimitsCmd { session, max, clear } } + SessionCmd::TurnCap { session, minutes, off, clear } => { + Command::TurnCapCmd { session, minutes, off, clear } + } SessionCmd::Rules { session, rules, clear } => { Command::RulesCmd { session, rules, clear } } diff --git a/cmux-tui/crates/acpmux/src/cli/mod.rs b/cmux-tui/crates/acpmux/src/cli/mod.rs index 7a07cccf1c09..8065f1bec1da 100644 --- a/cmux-tui/crates/acpmux/src/cli/mod.rs +++ b/cmux-tui/crates/acpmux/src/cli/mod.rs @@ -48,6 +48,7 @@ pub(crate) mod send_relay; pub mod sender; pub mod session_folder; pub mod session_limits; +pub mod session_turn_cap; pub mod shutdown; pub mod stdio; pub(crate) mod steer_reply; diff --git a/cmux-tui/crates/acpmux/src/cli/run/extra.rs b/cmux-tui/crates/acpmux/src/cli/run/extra.rs index c3a3f0869a2b..e9e73536e929 100644 --- a/cmux-tui/crates/acpmux/src/cli/run/extra.rs +++ b/cmux-tui/crates/acpmux/src/cli/run/extra.rs @@ -77,6 +77,17 @@ pub(super) async fn run(cmd: Command, json_out: bool, _suppress_reads: bool) -> let client = connect(true).await?; crate::cli::session_limits::run(&client, &session, max, clear, json_out).await } + Command::TurnCapCmd { session, minutes, off, clear } => { + use crate::cli::session_turn_cap::Change; + let change = match (minutes, off, clear) { + (Some(m), _, _) => Change::Minutes(m), + (None, true, _) => Change::Off, + (None, false, true) => Change::Clear, + (None, false, false) => Change::Show, + }; + let client = connect(true).await?; + crate::cli::session_turn_cap::run(&client, &session, change, json_out).await + } Command::ResumeQueue { session } => { let client = connect(true).await?; let id = crate::cli::resolve::control_id(&client, &session).await?; diff --git a/cmux-tui/crates/acpmux/src/cli/session_turn_cap.rs b/cmux-tui/crates/acpmux/src/cli/session_turn_cap.rs new file mode 100644 index 000000000000..fe412e9ba20e --- /dev/null +++ b/cmux-tui/crates/acpmux/src/cli/session_turn_cap.rs @@ -0,0 +1,82 @@ +//! `acpmux session turn-cap SESSION [--minutes N | --off | --clear]`: a session's turn cap +//! (`hub/turn_cap.rs`). Past it, the running turn takes no other agent's steer; their messages +//! wait for the next turn. With no flag it shows the cap; a write goes through +//! `_acpmux/set_turn_cap`, which takes the person (the app or this socket), never an agent. + +use crate::cli::output::print_json; +use crate::cli::run::resolve_id; +use crate::client::Client; +use crate::rpc::method; +use anyhow::Result; +use serde_json::{Value, json}; + +/// What the command asks for. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Change { + Show, + Minutes(u64), + Off, + Clear, +} + +/// The request's params for `change`. +pub fn params(id: &str, change: Change) -> Value { + match change { + Change::Show => json!({"sessionId": id}), + Change::Minutes(m) => json!({"sessionId": id, "turnCapMs": m.saturating_mul(60_000)}), + Change::Off => json!({"sessionId": id, "turnCapMs": 0}), + Change::Clear => json!({"sessionId": id, "turnCapMs": Value::Null}), + } +} + +pub async fn run(client: &Client, session: &str, change: Change, json_out: bool) -> Result<()> { + let id = resolve_id(client, session).await?; + let reply = client.request(method::MUX_SET_TURN_CAP, params(&id, change)).await?; + if json_out { + print_json(&reply); + } else { + println!("{}", describe(&reply)); + } + Ok(()) +} + +/// The cap as a line a person reads. +pub fn describe(reply: &Value) -> String { + let own = reply["turnCapMs"].as_u64(); + match reply["effectiveTurnCapMs"].as_u64() { + None if own == Some(0) => "no turn cap (turned off for this session)".to_owned(), + None => "no turn cap".to_owned(), + Some(ms) => { + let from = if own.is_some() { "this session's" } else { "default" }; + format!("turn cap {} min ({from})", ms.div_ceil(60_000)) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn each_change_is_one_request_shape() { + assert_eq!(params("s", Change::Show), json!({"sessionId": "s"})); + assert_eq!(params("s", Change::Minutes(20))["turnCapMs"], 1_200_000); + assert_eq!(params("s", Change::Off)["turnCapMs"], 0); + assert!(params("s", Change::Clear)["turnCapMs"].is_null()); + } + + #[test] + fn describe_names_the_cap_and_where_it_comes_from() { + let r = |own: Value, eff: Value| json!({"turnCapMs": own, "effectiveTurnCapMs": eff}); + assert_eq!(describe(&r(Value::Null, Value::Null)), "no turn cap"); + assert_eq!( + describe(&r(json!(0), Value::Null)), + "no turn cap (turned off for this session)" + ); + assert_eq!( + describe(&r(json!(600_000), json!(600_000))), + "turn cap 10 min (this session's)" + ); + assert_eq!(describe(&r(Value::Null, json!(1_200_000))), "turn cap 20 min (default)"); + } +} diff --git a/cmux-tui/crates/acpmux/src/hub/fork.rs b/cmux-tui/crates/acpmux/src/hub/fork.rs index 8a9cdbf83292..0d0b3840b60d 100644 --- a/cmux-tui/crates/acpmux/src/hub/fork.rs +++ b/cmux-tui/crates/acpmux/src/hub/fork.rs @@ -332,6 +332,8 @@ impl Hub { // A fork is the person's branch of the chat, never a spawn (`lineage.rs`). lineage: None, subagent_retry_open: false, + // The person's setting for the chat it came from, not for the fork. + turn_cap_ms: None, }; let new = self.make_session(meta); match made { diff --git a/cmux-tui/crates/acpmux/src/hub/mod.rs b/cmux-tui/crates/acpmux/src/hub/mod.rs index 18cbe8dc9acd..fe126910205c 100644 --- a/cmux-tui/crates/acpmux/src/hub/mod.rs +++ b/cmux-tui/crates/acpmux/src/hub/mod.rs @@ -121,6 +121,7 @@ mod store_load; mod stream; mod tags; mod tap; +pub(crate) mod turn_cap; mod titles; mod tool_paths; mod tools; @@ -1081,6 +1082,8 @@ impl Hub { stalled_gen: AtomicU64::new(0), last_model_at: AtomicU64::new(0), silent_turn: StdMutex::new(None), + turn_cap_noticed: StdMutex::new(None), + turn_cap_timer: StdMutex::new(None), last_active: AtomicU64::new(self.clock_now()), hibernated_at: AtomicU64::new(hibernated_at), wake_cause: StdMutex::new(None), diff --git a/cmux-tui/crates/acpmux/src/hub/resolve.rs b/cmux-tui/crates/acpmux/src/hub/resolve.rs index b8dc83406f89..bfd9d53055b3 100644 --- a/cmux-tui/crates/acpmux/src/hub/resolve.rs +++ b/cmux-tui/crates/acpmux/src/hub/resolve.rs @@ -365,5 +365,6 @@ pub(super) fn draft_meta(d: Draft<'_>) -> SessionMeta { subsession_limits: Default::default(), lineage: None, subagent_retry_open: false, + turn_cap_ms: None, } } diff --git a/cmux-tui/crates/acpmux/src/hub/session.rs b/cmux-tui/crates/acpmux/src/hub/session.rs index 1a0133964c2d..43f9812eac7a 100644 --- a/cmux-tui/crates/acpmux/src/hub/session.rs +++ b/cmux-tui/crates/acpmux/src/hub/session.rs @@ -202,6 +202,10 @@ pub struct Session { pub(super) last_model_at: AtomicU64, /// The turn that logged `turn_silent` and has not heard its model since (`silent_turn.rs`). pub(super) silent_turn: StdMutex>, + /// The turn the turn cap's notice went into (`turn_cap.rs`): one notice per turn. + pub(super) turn_cap_noticed: StdMutex>, + /// The turn cap's timer for the running turn (`turn_cap.rs`); the next turn aborts it. + pub(super) turn_cap_timer: StdMutex>, /// The hub clock's time (`Hub::clock_now`) of the last record or /// attach change; the idle harness exit counts from it (`idle.rs`). pub(super) last_active: AtomicU64, diff --git a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs new file mode 100644 index 000000000000..e40ac137f067 --- /dev/null +++ b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs @@ -0,0 +1,139 @@ +//! Part of `Hub`; see `hub/mod.rs`. The turn cap: a session that takes messages from many agents +//! (a coordinator) never ends its turn when every message steers into it (Leo, 2026-10-29: "You +//! never end a fucking turn. As soon as it comes in, it fucking, like, occupies you for another +//! fucking turn."). +//! +//! A session with a cap (`SessionMeta::turn_cap_ms`, else [`default_turn_cap_ms`]) whose running +//! turn is older than the cap stops taking other agents' steers: they wait in the queue, and the +//! next turn takes them together (`digest.rs`). The person's own steers still go in. The hub +//! steers one notice into that turn, once ([`NOTICE_KIND`]), when an agent's message is waiting +//! for it: on the first agent message past the cap, or at the cap when one waits already +//! ([`Hub::arm_turn_cap`]'s timer). The hub never cancels the turn itself. +//! +//! The timer: one per session, armed when a turn starts. It wakes once, at the cap, and ends; a +//! turn that starts before then aborts the previous turn's timer (`Session::turn_cap_timer`). It +//! holds the session weakly, so a closed session's timer does nothing. + +use super::*; +use std::sync::Weak; + +/// The cap a coordinator session gets when it set none itself (P2's `coordinator` setting). +pub const COORDINATOR_TURN_CAP_MS: u64 = 20 * 60 * 1000; + +/// The provenance kind of the notice (`prompt_provenance.rs`): hub-set, never a client's. +pub const NOTICE_KIND: &str = "turn_cap"; + +/// The cap a session's other settings give it when it set none itself: none today. The +/// coordinator setting gives [`COORDINATOR_TURN_CAP_MS`] here, the one place that links the two. +pub(crate) fn default_turn_cap_ms(_meta: &SessionMeta) -> Option { + None +} + +/// The session's cap: its own `turnCapMs` (0 turns it off), else [`default_turn_cap_ms`]. +pub(crate) fn turn_cap_ms(meta: &SessionMeta) -> Option { + match meta.turn_cap_ms { + Some(0) => None, + Some(ms) => Some(ms), + None => default_turn_cap_ms(meta), + } +} + +/// The running turn's id and the cap when that turn is at or past the cap at `now` (Unix ms). +fn capped_turn(session: &Session, now: u64) -> Option<(String, u64)> { + let cap = turn_cap_ms(&session.meta.lock().unwrap_or_else(|e| e.into_inner()))?; + let turn = session.turn()?; + (now.saturating_sub(turn.started_at) >= cap).then_some((turn.turn_id, cap)) +} + +/// The notice's words. +pub(super) fn notice_text(cap_ms: u64) -> String { + let minutes = cap_ms.div_ceil(60_000); + format!( + "Turn cap reached ({minutes} min): finish the current step and end your turn; your inbox digest comes next turn." + ) +} + +impl Hub { + /// Whether another agent's message (`agent`, from `digest::agent_sender`) that arrives now + /// waits for the next turn instead of steering in: the running turn is past its cap. Any + /// agent message past the cap asks for the notice, steer or not. The person's never waits. + pub(super) fn turn_cap_holds(self: &Arc, session: &Arc, agent: bool) -> bool { + if !agent { + return false; + } + let Some((turn_id, cap)) = capped_turn(session, now_ms()) else { return false }; + self.turn_cap_notice(session, &turn_id, cap); + true + } + + /// Arms the cap's timer for turn `turn_id`, which starts now (module docs). + pub(super) fn arm_turn_cap(self: &Arc, session: &Arc, turn_id: &str) { + let mut timer = session.turn_cap_timer.lock().unwrap_or_else(|e| e.into_inner()); + if let Some(previous) = timer.take() { + previous.abort(); + } + let Some(cap) = turn_cap_ms(&session.meta.lock().unwrap_or_else(|e| e.into_inner())) else { + return; + }; + let hub = Arc::downgrade(self); + let weak: Weak = Arc::downgrade(session); + let turn_id = turn_id.to_owned(); + let task = tokio::spawn(async move { + tokio::time::sleep(std::time::Duration::from_millis(cap)).await; + let (Some(hub), Some(session)) = (hub.upgrade(), weak.upgrade()) else { return }; + let Some((running, cap)) = capped_turn(&session, now_ms()) else { return }; + // Another agent's message waits (`queue.rs` keeps its sender as `run.agent`). + let waiting = session + .queue + .lock() + .unwrap_or_else(|e| e.into_inner()) + .iter() + .any(|q| q.run.agent.is_some()); + if running == turn_id && waiting { + hub.turn_cap_notice(&session, &running, cap); + } + }); + *timer = Some(task.abort_handle()); + } + + /// Steers the notice into turn `turn_id`, once for that turn. A session whose agent does not + /// steer gets none: no message steers into its turn anyway. + fn turn_cap_notice(self: &Arc, session: &Arc, turn_id: &str, cap: u64) { + if !session.steering.load(Ordering::SeqCst) { + return; + } + { + let mut noticed = session.turn_cap_noticed.lock().unwrap_or_else(|e| e.into_inner()); + if noticed.as_deref() == Some(turn_id) { + return; + } + *noticed = Some(turn_id.to_owned()); + } + let opts = PromptOptions { + prompt_id: Some(format!("turn-cap-{turn_id}")), + // Into this turn or nowhere: a turn that ended meanwhile needs no notice. + steer_only: true, + provenance: Some(json!({"kind": NOTICE_KIND, "turnId": turn_id, "capMs": cap})), + ..PromptOptions::default() + }; + let blocks = vec![json!({"type": "text", "text": notice_text(cap)})]; + let (hub, session) = (self.clone(), session.clone()); + tokio::spawn(async move { + if let Err(e) = hub.prompt_with(&session, blocks, "acpmux", true, opts).await { + tracing::info!(session = %session.id, "turn cap notice not steered: {}", e.message); + } + }); + } + + /// Sets or clears (`None`) the session's own cap, in ms; 0 turns the cap off. Read at each + /// message and turn start, so a running turn takes the new cap at once; its timer starts with + /// the next turn. + pub(crate) fn set_turn_cap(&self, session: &Session, cap_ms: Option) { + session.meta.lock().unwrap_or_else(|e| e.into_inner()).turn_cap_ms = cap_ms; + self.save_meta(session); + } +} + +#[cfg(test)] +#[path = "turn_cap_tests.rs"] +mod tests; diff --git a/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs b/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs new file mode 100644 index 000000000000..a1db7935af44 --- /dev/null +++ b/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs @@ -0,0 +1,269 @@ +//! The turn cap (`turn_cap.rs`) against the fake agent steering as Claude Code does: a prompt +//! while a turn runs is read into it at once ("steer: "), and the turn runs on until the +//! test opens its gate. + +use super::*; +use std::time::Duration; +use tokio::sync::oneshot; + +const WAIT: Duration = Duration::from_secs(20); +const NOTICE: &str = "Turn cap reached"; + +struct Fixture { + hub: Arc, + session: Arc, + events: tokio::sync::broadcast::Receiver, + dir: std::path::PathBuf, +} + +async fn fixture() -> Fixture { + use crate::config::{Config, StoreMode}; + let fake = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fake_agent.py"); + let mut cfg: Config = serde_json::from_value(json!({ + "harnesses": {"fake": {"argv": ["/usr/bin/env", "FAKE_CLAUDE_STEER=1", "python3", fake]}}, + "defaultHarness": "fake", + })) + .unwrap(); + cfg.store.mode = StoreMode::Memory; + let store = crate::store::open(&cfg.store, std::path::Path::new("/nonexistent")).unwrap(); + let hub = Hub::new(cfg, store); + let dir = std::env::temp_dir().join(format!("acpmux-turn-cap-{}", uuid::Uuid::now_v7())); + std::fs::create_dir_all(&dir).unwrap(); + let session = hub + .new_session(NewRequest { cwd: Some(std::env::temp_dir()), ..Default::default() }) + .await + .unwrap(); + let events = hub.subscribe(); + Fixture { hub, session, events, dir } +} + +impl Drop for Fixture { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.dir); + } +} + +impl Fixture { + /// Waits for the agent to stream `text` in this session. + async fn heard(&mut self, text: &str) { + let id = self.session.id.clone(); + let events = &mut self.events; + tokio::time::timeout(WAIT, async { + loop { + let event = events.recv().await.unwrap(); + if event.session_id == id + && event.record.msg["params"]["update"]["content"]["text"] == text + { + return; + } + } + }) + .await + .unwrap_or_else(|_| panic!("the agent never said {text:?}")); + } + + /// Starts a turn of the person's that runs until [`Fixture::open`] opens gate `name`. + async fn gated_turn(&mut self, name: &str) -> tokio::task::JoinHandle> { + let fifo = self.dir.join(name); + let path = std::ffi::CString::new(fifo.to_str().unwrap()).unwrap(); + assert_eq!(unsafe { libc::mkfifo(path.as_ptr(), 0o600) }, 0); + let (hub, session) = (self.hub.clone(), self.session.clone()); + let text = format!("gate: {}", fifo.display()); + let turn = tokio::spawn(async move { + hub.prompt(&session, vec![json!({"type": "text", "text": text})], "test", false).await + }); + self.heard("before-gate").await; + turn + } + + /// Opens gate `name`: its turn ends. + async fn open(&self, name: &str) { + let fifo = self.dir.join(name); + tokio::task::spawn_blocking(move || std::fs::write(fifo, b"go")).await.unwrap().unwrap(); + } + + /// Sends `text`, from agent `from` or (`None`) the person, as a steer when `steer`; answers + /// with its acceptance (`queued`, `steer`) and its task. + async fn send( + &self, + text: &str, + from: Option<&str>, + steer: bool, + steer_only: bool, + ) -> (Result, tokio::task::JoinHandle>) { + let (tx, rx) = oneshot::channel(); + let opts = PromptOptions { + agent: from.map(str::to_owned), + steer_only, + on_accepted: Some(Box::new(move |v| { + let _ = tx.send(v); + })), + ..PromptOptions::default() + }; + let (hub, session) = (self.hub.clone(), self.session.clone()); + let blocks = vec![json!({"type": "text", "text": text})]; + let task = + tokio::spawn( + async move { hub.prompt_with(&session, blocks, "test", steer, opts).await }, + ); + match tokio::time::timeout(WAIT, rx).await.expect("accepted or refused in time") { + Ok(accepted) => (Ok(accepted), task), + // Refused: its options, and the acceptance with them, went unused. + Err(_) => { + let refused = task.await.unwrap().expect_err("refused before its acceptance"); + (Err(refused.clone()), tokio::spawn(async move { Err(refused) })) + } + } + } + + /// Makes the running turn `cap` ms old, plus one. + fn age_turn(&self, cap: u64) { + let mut turn = self.session.turn.lock().unwrap(); + let turn = turn.as_mut().expect("a running turn"); + turn.started_at = turn.started_at.saturating_sub(cap + 1); + } + + /// The notices the agent got, by the turn their provenance names. + fn notices(&self) -> Vec { + let records = self.hub.events(&self.session.id, 0, 100_000).unwrap(); + let sent = records + .iter() + .filter(|r| r.dir == "out" && r.kind == "session/prompt") + .filter(|r| r.msg["params"]["prompt"].to_string().contains(NOTICE)) + .count(); + let logged: Vec = records + .iter() + .filter(|r| r.kind == "user_message") + .filter(|r| r.msg["_meta"]["provenance"]["kind"] == NOTICE_KIND) + .map(|r| r.msg["_meta"]["provenance"]["turnId"].as_str().unwrap_or("").to_owned()) + .collect(); + assert_eq!(sent, logged.len(), "every notice logged is one the agent got"); + logged + } +} + +#[tokio::test] +async fn a_sessions_own_cap_wins_and_zero_turns_it_off() { + let f = fixture().await; + let mut meta = f.session.meta(); + assert_eq!(turn_cap_ms(&meta), None, "no cap by default"); + meta.turn_cap_ms = Some(60_000); + assert_eq!(turn_cap_ms(&meta), Some(60_000)); + meta.turn_cap_ms = Some(0); + assert_eq!(turn_cap_ms(&meta), None); + assert_eq!( + notice_text(COORDINATOR_TURN_CAP_MS), + "Turn cap reached (20 min): finish the current step and end your turn; your inbox digest comes next turn." + ); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn under_the_cap_an_agent_message_steers_in_as_before() { + let mut f = fixture().await; + f.hub.set_turn_cap(&f.session, Some(3_600_000)); + let turn = f.gated_turn("g").await; + let (accepted, _) = f.send("from lane a", Some("lane-a"), true, false).await; + assert_eq!(accepted.unwrap()["steer"], true); + f.heard("steer: from lane a").await; + f.open("g").await; + tokio::time::timeout(WAIT, turn).await.unwrap().unwrap().unwrap(); + assert!(f.notices().is_empty(), "no notice under the cap"); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn past_the_cap_agent_messages_wait_one_notice_goes_in_and_the_persons_steer_still_does() { + let mut f = fixture().await; + let cap = 600_000; + f.hub.set_turn_cap(&f.session, Some(cap)); + let turn = f.gated_turn("g").await; + let turn_id = f.session.turn().unwrap().turn_id; + f.age_turn(cap); + let (first, _) = f.send("from lane a", Some("lane-a"), true, false).await; + let first = first.unwrap(); + assert_eq!(first["queued"], true, "held for the next turn: {first}"); + assert_ne!(first["steer"], true); + f.heard(&format!("steer: {}", notice_text(cap))).await; + let (second, _) = f.send("from lane b", Some("lane-b"), true, false).await; + assert_eq!(second.unwrap()["queued"], true); + // Steer-only cannot wait: refused, and it says why. + let (only, _) = f.send("steer or nothing", Some("lane-c"), true, true).await; + let only = only.expect_err("a steer-only agent message past the cap"); + assert!(only.message.contains("turn cap"), "{}", only.message); + // The person's own steer still goes in. + let (person, _) = f.send("from the person", None, true, false).await; + assert_eq!(person.unwrap()["steer"], true); + f.heard("steer: from the person").await; + tokio::time::sleep(Duration::from_millis(500)).await; + assert_eq!(f.notices(), [turn_id], "exactly one notice, into the capped turn"); + assert!(f.session.turn().is_some(), "the hub never ends the turn itself"); + f.open("g").await; + tokio::time::timeout(WAIT, turn).await.unwrap().unwrap().unwrap(); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn held_messages_reach_the_next_turn_and_the_next_turn_has_its_own_cap() { + let mut f = fixture().await; + let cap = 600_000; + f.hub.set_turn_cap(&f.session, Some(cap)); + let turn = f.gated_turn("g1").await; + f.age_turn(cap); + let (_, a) = f.send("held from lane a", Some("lane-a"), true, false).await; + let (_, b) = f.send("held from lane b", Some("lane-b"), true, false).await; + f.heard(&format!("steer: {}", notice_text(cap))).await; + f.open("g1").await; + tokio::time::timeout(WAIT, turn).await.unwrap().unwrap().unwrap(); + // The next turn takes both, together (`digest.rs`), as one prompt. + let a = tokio::time::timeout(WAIT, a).await.unwrap().unwrap().unwrap(); + let b = tokio::time::timeout(WAIT, b).await.unwrap().unwrap().unwrap(); + assert_eq!(a["_meta"]["acpmux"]["turnId"], b["_meta"]["acpmux"]["turnId"]); + let records = f.hub.events(&f.session.id, 0, 100_000).unwrap(); + let joined: Vec<_> = records + .iter() + .filter(|r| r.dir == "out" && r.kind == "session/prompt") + .map(|r| r.msg["params"]["prompt"].to_string()) + .filter(|p| p.contains("held from lane a")) + .collect(); + assert_eq!(joined.len(), 1, "delivered once: {joined:?}"); + assert!(joined[0].contains("held from lane b")); + assert!(!records.iter().any(|r| { + r.msg["params"]["update"]["content"]["text"] + .as_str() + .is_some_and(|t| t.starts_with("steer: held from")) + })); + // A new turn is under its cap again, and past it gets a notice of its own. + let turn = f.gated_turn("g2").await; + let second_turn = f.session.turn().unwrap().turn_id; + let (under, _) = f.send("fresh from lane a", Some("lane-a"), true, false).await; + assert_eq!(under.unwrap()["steer"], true); + f.age_turn(cap); + let (_, _held) = f.send("late from lane a", Some("lane-a"), true, false).await; + f.heard(&format!("steer: {}", notice_text(cap))).await; + let notices = f.notices(); + assert_eq!(notices.len(), 2, "{notices:?}"); + assert_eq!(notices[1], second_turn); + f.open("g2").await; + tokio::time::timeout(WAIT, turn).await.unwrap().unwrap().unwrap(); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn the_timer_sends_the_notice_at_the_cap_only_when_a_message_waits() { + let mut f = fixture().await; + f.hub.set_turn_cap(&f.session, Some(400)); + // Nothing waits: the cap passes with no notice. + let turn = f.gated_turn("quiet").await; + tokio::time::sleep(Duration::from_millis(1_200)).await; + assert!(f.notices().is_empty()); + f.open("quiet").await; + tokio::time::timeout(WAIT, turn).await.unwrap().unwrap().unwrap(); + // An agent message queued under the cap (no steer): the timer sends the notice at the cap. + f.hub.set_turn_cap(&f.session, Some(3_000)); + let turn = f.gated_turn("busy").await; + let turn_id = f.session.turn().unwrap().turn_id; + let (queued, _) = f.send("report from lane a", Some("lane-a"), false, false).await; + assert_eq!(queued.unwrap()["queued"], true); + assert!(f.notices().is_empty(), "nothing before the cap"); + f.heard(&format!("steer: {}", notice_text(3_000))).await; + assert_eq!(f.notices(), [turn_id]); + f.open("busy").await; + tokio::time::timeout(WAIT, turn).await.unwrap().unwrap().unwrap(); +} diff --git a/cmux-tui/crates/acpmux/src/prompt_provenance.rs b/cmux-tui/crates/acpmux/src/prompt_provenance.rs index a7372436febb..e14f09150b50 100644 --- a/cmux-tui/crates/acpmux/src/prompt_provenance.rs +++ b/cmux-tui/crates/acpmux/src/prompt_provenance.rs @@ -20,7 +20,8 @@ //! connection error cut off (`hub/auto_resume.rs`), `{kind: "session_message", from, //! fromName, to}` on a prompt another session sent with none (`session_message` below), and //! `{kind: "task_result", from, fromName, to, taskId, state}` on the prompt that brings a task's -//! result to the chat that gave it (`hub/tasks.rs`). +//! result to the chat that gave it (`hub/tasks.rs`), and `{kind: "turn_cap", turnId, capMs}` on +//! the one notice steered into a turn past its turn cap (`hub/turn_cap.rs`). use serde_json::{Map, Value, json}; diff --git a/cmux-tui/crates/acpmux/src/protocol/mod.rs b/cmux-tui/crates/acpmux/src/protocol/mod.rs index f9616c549097..5292c2efbe5c 100644 --- a/cmux-tui/crates/acpmux/src/protocol/mod.rs +++ b/cmux-tui/crates/acpmux/src/protocol/mod.rs @@ -279,6 +279,7 @@ pub fn registry() -> Vec { untyped(method::MUX_SET_POLICY, Request), untyped("_acpmux/set_default_policy", Request), untyped(method::MUX_SET_RULES, Request), + untyped(method::MUX_SET_TURN_CAP, Request), untyped(method::MUX_PERSON_ENROLL, Request), untyped(method::MUX_COMPUTER_USE_ENROLL, Request), untyped(method::MUX_PERSON_PROVE, Request), diff --git a/cmux-tui/crates/acpmux/src/rpc.rs b/cmux-tui/crates/acpmux/src/rpc.rs index d76840b62086..45d3e5b0afe7 100644 --- a/cmux-tui/crates/acpmux/src/rpc.rs +++ b/cmux-tui/crates/acpmux/src/rpc.rs @@ -347,6 +347,9 @@ pub mod method { pub const MUX_PERMISSION_CHAT_REVOKE: &str = "_acpmux/permission_chat_revoke"; pub const MUX_SET_POLICY: &str = "_acpmux/set_policy"; pub const MUX_SET_RULES: &str = "_acpmux/set_rules"; + /// A session's turn cap (`hub/turn_cap.rs`): `{sessionId, turnCapMs?}`, a number sets it + /// (0 turns it off), null clears it, absent reads it. + pub const MUX_SET_TURN_CAP: &str = "_acpmux/set_turn_cap"; pub const MUX_TAG: &str = "_acpmux/tag"; /// The chat menu's Archive (`archived: true`) or Unarchive of one session: /// the `archived` tag, valued with when (milliseconds), set or removed. diff --git a/cmux-tui/crates/acpmux/src/server/requests.rs b/cmux-tui/crates/acpmux/src/server/requests.rs index c2c216478afd..8e8d2530c2fc 100644 --- a/cmux-tui/crates/acpmux/src/server/requests.rs +++ b/cmux-tui/crates/acpmux/src/server/requests.rs @@ -1799,6 +1799,31 @@ async fn dispatch_request( hub.set_rules(&s, rules); Ok(hub.session_summary(&s)) } + method::MUX_SET_TURN_CAP => { + let s = hub.resolve(session_key(¶ms)?)?; + if let Some(value) = params.get("turnCapMs") { + // The person's: the app, or the local socket with no agent behind it. A + // session's agent never lifts a cap, its own or another's. + if !conn.is_person() && (conn.origin != Origin::Local || conn.caller().is_some()) { + return Err(crate::hub::person::person_required( + "changing a session's turn cap", + )); + } + let cap = match value { + Value::Null => None, + v => Some(v.as_u64().ok_or_else(|| { + RpcError::invalid_params("turnCapMs must be a whole number of ms or null") + })?), + }; + hub.set_turn_cap(&s, cap); + } + let meta = s.meta(); + Ok(json!({ + "sessionId": s.id, + "turnCapMs": meta.turn_cap_ms, + "effectiveTurnCapMs": crate::hub::turn_cap::turn_cap_ms(&meta), + })) + } method::MUX_ATTACH => { let s = hub.resolve(session_key(¶ms)?)?; // A restart reads the chat logs after its ready line: the queue, turn and status diff --git a/cmux-tui/crates/acpmux/src/store.rs b/cmux-tui/crates/acpmux/src/store.rs index 95f800c71f68..974e7280a370 100644 --- a/cmux-tui/crates/acpmux/src/store.rs +++ b/cmux-tui/crates/acpmux/src/store.rs @@ -357,6 +357,11 @@ pub struct SessionMeta { /// that retry. Never copied to a fork. #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub subagent_retry_open: bool, + /// The session's own turn cap in ms (`hub/turn_cap.rs`): past it, other agents' messages + /// wait for the next turn. 0 turns it off; `None` takes the default its settings give + /// (`turn_cap::default_turn_cap_ms`). Set by the person only; never copied to a fork. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub turn_cap_ms: Option, } /// Where a session sits in the tree of sessions that started each other diff --git a/webviews/src/agent-session/acpmux/protocol/generated/methods.ts b/webviews/src/agent-session/acpmux/protocol/generated/methods.ts index d238e95733fd..219bc496f0f2 100644 --- a/webviews/src/agent-session/acpmux/protocol/generated/methods.ts +++ b/webviews/src/agent-session/acpmux/protocol/generated/methods.ts @@ -62,6 +62,7 @@ export interface AcpmuxRequests { "_acpmux/prompt_withdraw": { params: unknown; result: unknown }; "_acpmux/send_queue": { params: unknown; result: unknown }; "_acpmux/send_status": { params: unknown; result: unknown }; + "_acpmux/mesh_control": { params: unknown; result: unknown }; "_acpmux/step_now": { params: unknown; result: unknown }; "_acpmux/steer_withdraw": { params: unknown; result: unknown }; "_acpmux/agent_pair_resume": { params: unknown; result: unknown }; @@ -138,6 +139,7 @@ export interface AcpmuxRequests { "_acpmux/set_policy": { params: unknown; result: unknown }; "_acpmux/set_default_policy": { params: unknown; result: unknown }; "_acpmux/set_rules": { params: unknown; result: unknown }; + "_acpmux/set_turn_cap": { params: unknown; result: unknown }; "_acpmux/person_enroll": { params: unknown; result: unknown }; "_acpmux/computer_use_enroll": { params: unknown; result: unknown }; "_acpmux/person_prove": { params: unknown; result: unknown }; @@ -331,6 +333,7 @@ export const ACPMUX_METHODS: ReadonlyArray<{ name: string; kind: AcpmuxMethodKin { name: "_acpmux/prompt_withdraw", kind: "request", acp: false, typed: false }, { name: "_acpmux/send_queue", kind: "request", acp: false, typed: false }, { name: "_acpmux/send_status", kind: "request", acp: false, typed: false }, + { name: "_acpmux/mesh_control", kind: "request", acp: false, typed: false }, { name: "_acpmux/step_now", kind: "request", acp: false, typed: false }, { name: "_acpmux/steer_withdraw", kind: "request", acp: false, typed: false }, { name: "_acpmux/agent_pair_resume", kind: "request", acp: false, typed: false }, @@ -408,6 +411,7 @@ export const ACPMUX_METHODS: ReadonlyArray<{ name: string; kind: AcpmuxMethodKin { name: "_acpmux/set_policy", kind: "request", acp: false, typed: false }, { name: "_acpmux/set_default_policy", kind: "request", acp: false, typed: false }, { name: "_acpmux/set_rules", kind: "request", acp: false, typed: false }, + { name: "_acpmux/set_turn_cap", kind: "request", acp: false, typed: false }, { name: "_acpmux/person_enroll", kind: "request", acp: false, typed: false }, { name: "_acpmux/computer_use_enroll", kind: "request", acp: false, typed: false }, { name: "_acpmux/person_prove", kind: "request", acp: false, typed: false }, From 3ffee6ed2297568de5c48622df038e23a1f55ca4 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 29 Oct 2026 12:00:11 -0700 Subject: [PATCH 02/10] feat(acpmux): past a session's turn cap, agent messages wait for the next turn A session with a turn cap whose running turn is older than the cap takes no other agent's steer: their messages wait in the queue and the next turn takes them together. The person's own steers still go in. The hub steers one notice into that turn, once, when an agent message waits, and never cancels the turn itself. Co-Authored-By: Claude Opus 5.5 Agent-Session: https://cmux.com/deeplink/agent?id=aid-1288bbb2b5d2f5c6b66a --- cmux-tui/crates/acpmux/src/hub/turns.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/cmux-tui/crates/acpmux/src/hub/turns.rs b/cmux-tui/crates/acpmux/src/hub/turns.rs index 774d8cb3be4f..dc801203703e 100644 --- a/cmux-tui/crates/acpmux/src/hub/turns.rs +++ b/cmux-tui/crates/acpmux/src/hub/turns.rs @@ -271,12 +271,20 @@ impl Hub { }; let text = prompt_text(&blocks); let running = session.turn(); - let steer_now = steer && session.steering.load(Ordering::SeqCst) && running.is_some(); + // Who sent it, as the queue knows its prompts' senders (`digest.rs`). + let agent = super::digest::agent_sender(opts.provenance.as_ref(), opts.agent.as_deref()); + // Another agent's message to a turn past its cap waits for the next turn (`turn_cap.rs`). + let capped = running.is_some() && self.turn_cap_holds(session, agent.is_some()); + let steer_now = + steer && session.steering.load(Ordering::SeqCst) && running.is_some() && !capped; if steer && opts.steer_only && !steer_now { - return Err(RpcError::invalid_params( - "steer.unavailable: the session has no running turn that its agent can steer", - ) - .with_data(json!({"reason": "steer.unavailable"}))); + let why = if capped { + "steer.unavailable: the running turn is past its turn cap; other agents' messages wait for its next turn" + } else { + "steer.unavailable: the session has no running turn that its agent can steer" + }; + return Err(RpcError::invalid_params(why) + .with_data(json!({"reason": "steer.unavailable", "turnCap": capped}))); } if !live { self.cold_prompt_check(session).await?; @@ -284,8 +292,6 @@ impl Hub { if steer_now && session.moving().is_some() { return Err(super::queue::moving_error()); } - // Who sent it, as the queue knows its prompts' senders (`digest.rs`). - let agent = super::digest::agent_sender(opts.provenance.as_ref(), opts.agent.as_deref()); if steer_now { let turn_id = running.map(|t| t.turn_id).unwrap_or_default(); let opts = PromptOptions { prompt_id: Some(prompt_id), on_accepted: None, ..opts }; @@ -402,6 +408,7 @@ impl Hub { drop(guard); return Err(e); } + self.arm_turn_cap(session, &turn_id); // A lane's report to a parent whose close began since `queue_slot` checked is refused // (`lane_reports.rs`). Checked once the turn is set: `kill` marks the close before it // clears the turn, so a close that began any time before this check is seen. From 9520290ecf8ad8d79a591db68240bb0f492186ac Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 29 Oct 2026 12:16:00 -0700 Subject: [PATCH 03/10] style(acpmux): turn cap module order and the coordinator default's lint Co-Authored-By: Claude Opus 5.5 Agent-Session: https://cmux.com/deeplink/agent?id=aid-1288bbb2b5d2f5c6b66a --- cmux-tui/crates/acpmux/src/hub/mod.rs | 2 +- cmux-tui/crates/acpmux/src/hub/turn_cap.rs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cmux-tui/crates/acpmux/src/hub/mod.rs b/cmux-tui/crates/acpmux/src/hub/mod.rs index fe126910205c..104b27945170 100644 --- a/cmux-tui/crates/acpmux/src/hub/mod.rs +++ b/cmux-tui/crates/acpmux/src/hub/mod.rs @@ -121,10 +121,10 @@ mod store_load; mod stream; mod tags; mod tap; -pub(crate) mod turn_cap; mod titles; mod tool_paths; mod tools; +pub(crate) mod turn_cap; mod watch; pub use new_request::{ NewRequest, declared_model_json, profile_takes_model_at_spawn, terminal_harness_refusal, diff --git a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs index e40ac137f067..67037471f6dd 100644 --- a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs +++ b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs @@ -17,7 +17,9 @@ use super::*; use std::sync::Weak; -/// The cap a coordinator session gets when it set none itself (P2's `coordinator` setting). +/// The cap a coordinator session gets when it set none itself (the `coordinator` setting, which +/// [`default_turn_cap_ms`] reads once it lands). +#[allow(dead_code)] // read by `default_turn_cap_ms` with the coordinator setting pub const COORDINATOR_TURN_CAP_MS: u64 = 20 * 60 * 1000; /// The provenance kind of the notice (`prompt_provenance.rs`): hub-set, never a client's. From fc324dbe86a20f2f17cd072bacbbdca8af8c7239 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 29 Oct 2026 12:26:20 -0700 Subject: [PATCH 04/10] fix(acpmux): keep the turn cap's check out of the prompt future The cap's sender lookup and refusal text lived in run_prompt_joined, whose future grew past the debug test thread's stack in steer_now::send_now_for_a_queued_prompt_steers_it_in_at_once. Co-Authored-By: Claude Opus 5.5 Agent-Session: https://cmux.com/deeplink/agent?id=aid-1288bbb2b5d2f5c6b66a --- cmux-tui/crates/acpmux/src/hub/turn_cap.rs | 21 +++++++++++++++++++-- cmux-tui/crates/acpmux/src/hub/turns.rs | 14 ++++---------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs index 67037471f6dd..83af63438a6a 100644 --- a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs +++ b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs @@ -55,12 +55,29 @@ pub(super) fn notice_text(cap_ms: u64) -> String { ) } +/// The refusal of a steer-only prompt that cannot steer now; `capped`: because the running turn is +/// past its cap. +pub(super) fn steer_unavailable(capped: bool) -> RpcError { + let why = if capped { + "steer.unavailable: the running turn is past its turn cap; other agents' messages wait for its next turn" + } else { + "steer.unavailable: the session has no running turn that its agent can steer" + }; + RpcError::invalid_params(why) + .with_data(json!({"reason": "steer.unavailable", "turnCap": capped})) +} + impl Hub { /// Whether another agent's message (`agent`, from `digest::agent_sender`) that arrives now /// waits for the next turn instead of steering in: the running turn is past its cap. Any /// agent message past the cap asks for the notice, steer or not. The person's never waits. - pub(super) fn turn_cap_holds(self: &Arc, session: &Arc, agent: bool) -> bool { - if !agent { + #[inline(never)] + pub(super) fn turn_cap_holds( + self: &Arc, + session: &Arc, + opts: &PromptOptions, + ) -> bool { + if super::digest::agent_sender(opts.provenance.as_ref(), opts.agent.as_deref()).is_none() { return false; } let Some((turn_id, cap)) = capped_turn(session, now_ms()) else { return false }; diff --git a/cmux-tui/crates/acpmux/src/hub/turns.rs b/cmux-tui/crates/acpmux/src/hub/turns.rs index dc801203703e..f702b0b1be87 100644 --- a/cmux-tui/crates/acpmux/src/hub/turns.rs +++ b/cmux-tui/crates/acpmux/src/hub/turns.rs @@ -271,20 +271,12 @@ impl Hub { }; let text = prompt_text(&blocks); let running = session.turn(); - // Who sent it, as the queue knows its prompts' senders (`digest.rs`). - let agent = super::digest::agent_sender(opts.provenance.as_ref(), opts.agent.as_deref()); // Another agent's message to a turn past its cap waits for the next turn (`turn_cap.rs`). - let capped = running.is_some() && self.turn_cap_holds(session, agent.is_some()); + let capped = running.is_some() && self.turn_cap_holds(session, &opts); let steer_now = steer && session.steering.load(Ordering::SeqCst) && running.is_some() && !capped; if steer && opts.steer_only && !steer_now { - let why = if capped { - "steer.unavailable: the running turn is past its turn cap; other agents' messages wait for its next turn" - } else { - "steer.unavailable: the session has no running turn that its agent can steer" - }; - return Err(RpcError::invalid_params(why) - .with_data(json!({"reason": "steer.unavailable", "turnCap": capped}))); + return Err(super::turn_cap::steer_unavailable(capped)); } if !live { self.cold_prompt_check(session).await?; @@ -292,6 +284,8 @@ impl Hub { if steer_now && session.moving().is_some() { return Err(super::queue::moving_error()); } + // Who sent it, as the queue knows its prompts' senders (`digest.rs`). + let agent = super::digest::agent_sender(opts.provenance.as_ref(), opts.agent.as_deref()); if steer_now { let turn_id = running.map(|t| t.turn_id).unwrap_or_default(); let opts = PromptOptions { prompt_id: Some(prompt_id), on_accepted: None, ..opts }; From b06d1ab937deeaf1aeb81c56ec4f3fda89f0992f Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 29 Oct 2026 12:36:31 -0700 Subject: [PATCH 05/10] fix(acpmux): the turn cap check borrows only the sender fields Co-Authored-By: Claude Opus 5.5 Agent-Session: https://cmux.com/deeplink/agent?id=aid-1288bbb2b5d2f5c6b66a --- cmux-tui/crates/acpmux/src/hub/turn_cap.rs | 12 +++++++----- cmux-tui/crates/acpmux/src/hub/turns.rs | 3 ++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs index 83af63438a6a..5e8b3211e7df 100644 --- a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs +++ b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs @@ -68,16 +68,18 @@ pub(super) fn steer_unavailable(capped: bool) -> RpcError { } impl Hub { - /// Whether another agent's message (`agent`, from `digest::agent_sender`) that arrives now - /// waits for the next turn instead of steering in: the running turn is past its cap. Any - /// agent message past the cap asks for the notice, steer or not. The person's never waits. + /// Whether a prompt that arrives now and is another agent's message (`digest::agent_sender` + /// of its `provenance` and `agent`) waits for the next turn instead of steering in: the + /// running turn is past its cap. Any agent message past the cap asks for the notice, steer or + /// not. The person's never waits. #[inline(never)] pub(super) fn turn_cap_holds( self: &Arc, session: &Arc, - opts: &PromptOptions, + provenance: Option<&Value>, + agent: Option<&str>, ) -> bool { - if super::digest::agent_sender(opts.provenance.as_ref(), opts.agent.as_deref()).is_none() { + if super::digest::agent_sender(provenance, agent).is_none() { return false; } let Some((turn_id, cap)) = capped_turn(session, now_ms()) else { return false }; diff --git a/cmux-tui/crates/acpmux/src/hub/turns.rs b/cmux-tui/crates/acpmux/src/hub/turns.rs index f702b0b1be87..41a0e7ddd96a 100644 --- a/cmux-tui/crates/acpmux/src/hub/turns.rs +++ b/cmux-tui/crates/acpmux/src/hub/turns.rs @@ -272,7 +272,8 @@ impl Hub { let text = prompt_text(&blocks); let running = session.turn(); // Another agent's message to a turn past its cap waits for the next turn (`turn_cap.rs`). - let capped = running.is_some() && self.turn_cap_holds(session, &opts); + let capped = running.is_some() + && self.turn_cap_holds(session, opts.provenance.as_ref(), opts.agent.as_deref()); let steer_now = steer && session.steering.load(Ordering::SeqCst) && running.is_some() && !capped; if steer && opts.steer_only && !steer_now { From 1fab7c8560706bb4da11449baa2adb6e3cb9f535 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 29 Oct 2026 13:03:21 -0700 Subject: [PATCH 06/10] fix(acpmux): turn cap review: Send now, interrupts, the notice's turn, restarts, stale callers - The queue's Send now of a held agent message still steers in past the cap. - An agent's interrupt held past the cap waits without stopping the turn. - The notice goes only into the turn it names, checked before it is logged and again before it is written. - The turn the notice went into is saved in the session's meta, so a turn a restart adopts gets no second notice. - A local connection with a caller token this daemon does not know cannot write the cap. - The timer test checks "nothing before the cap" only while the cap is ahead. Co-Authored-By: Claude Opus 5.5 Agent-Session: https://cmux.com/deeplink/agent?id=aid-1288bbb2b5d2f5c6b66a --- cmux-tui/crates/acpmux/src/hub/fork.rs | 1 + cmux-tui/crates/acpmux/src/hub/handoff/ops.rs | 1 + cmux-tui/crates/acpmux/src/hub/mod.rs | 4 +- cmux-tui/crates/acpmux/src/hub/queue.rs | 1 + cmux-tui/crates/acpmux/src/hub/resolve.rs | 1 + cmux-tui/crates/acpmux/src/hub/session.rs | 2 - cmux-tui/crates/acpmux/src/hub/steer_end.rs | 7 ++ cmux-tui/crates/acpmux/src/hub/turn_cap.rs | 31 +++++-- .../crates/acpmux/src/hub/turn_cap_tests.rs | 90 +++++++++++++++++-- cmux-tui/crates/acpmux/src/hub/turns.rs | 4 +- cmux-tui/crates/acpmux/src/server/requests.rs | 5 +- cmux-tui/crates/acpmux/src/store.rs | 4 + 12 files changed, 134 insertions(+), 17 deletions(-) diff --git a/cmux-tui/crates/acpmux/src/hub/fork.rs b/cmux-tui/crates/acpmux/src/hub/fork.rs index 0d0b3840b60d..557351416608 100644 --- a/cmux-tui/crates/acpmux/src/hub/fork.rs +++ b/cmux-tui/crates/acpmux/src/hub/fork.rs @@ -334,6 +334,7 @@ impl Hub { subagent_retry_open: false, // The person's setting for the chat it came from, not for the fork. turn_cap_ms: None, + turn_cap_noticed: None, }; let new = self.make_session(meta); match made { diff --git a/cmux-tui/crates/acpmux/src/hub/handoff/ops.rs b/cmux-tui/crates/acpmux/src/hub/handoff/ops.rs index 66cf1599aa5f..0b8429a3cbf4 100644 --- a/cmux-tui/crates/acpmux/src/hub/handoff/ops.rs +++ b/cmux-tui/crates/acpmux/src/hub/handoff/ops.rs @@ -608,6 +608,7 @@ impl Hub { display_parts: None, full_access_device: None, no_queue: false, + send_now: false, }; let (hub, session) = (self.clone(), target.clone()); let run = diff --git a/cmux-tui/crates/acpmux/src/hub/mod.rs b/cmux-tui/crates/acpmux/src/hub/mod.rs index 104b27945170..246eb0274cca 100644 --- a/cmux-tui/crates/acpmux/src/hub/mod.rs +++ b/cmux-tui/crates/acpmux/src/hub/mod.rs @@ -355,6 +355,9 @@ pub struct PromptOptions { /// A person's subagent retry (`subagent_retry_manual.rs`): refused with `retry.busy` where it /// would wait (behind a turn, the queue, the turn lock or a run slot), never queued. pub no_queue: bool, + /// A queued prompt sent now (`queue.rs` `steer_queued`): it steers in even past the running + /// turn's cap (`turn_cap.rs`). + pub send_now: bool, } /// The outcome of one client prompt id, shared with a resend of it. @@ -1082,7 +1085,6 @@ impl Hub { stalled_gen: AtomicU64::new(0), last_model_at: AtomicU64::new(0), silent_turn: StdMutex::new(None), - turn_cap_noticed: StdMutex::new(None), turn_cap_timer: StdMutex::new(None), last_active: AtomicU64::new(self.clock_now()), hibernated_at: AtomicU64::new(hibernated_at), diff --git a/cmux-tui/crates/acpmux/src/hub/queue.rs b/cmux-tui/crates/acpmux/src/hub/queue.rs index 32151b91c75c..13d9c11e5257 100644 --- a/cmux-tui/crates/acpmux/src/hub/queue.rs +++ b/cmux-tui/crates/acpmux/src/hub/queue.rs @@ -336,6 +336,7 @@ impl Hub { agent: run.agent.clone(), display_parts: run.display_parts.clone(), full_access_device: run.full_access_device.clone(), + send_now: true, ..Default::default() }; Box::pin(self.run_prompt(session, run.blocks.clone(), client, true, opts)).await diff --git a/cmux-tui/crates/acpmux/src/hub/resolve.rs b/cmux-tui/crates/acpmux/src/hub/resolve.rs index bfd9d53055b3..0c6eb33289e8 100644 --- a/cmux-tui/crates/acpmux/src/hub/resolve.rs +++ b/cmux-tui/crates/acpmux/src/hub/resolve.rs @@ -366,5 +366,6 @@ pub(super) fn draft_meta(d: Draft<'_>) -> SessionMeta { lineage: None, subagent_retry_open: false, turn_cap_ms: None, + turn_cap_noticed: None, } } diff --git a/cmux-tui/crates/acpmux/src/hub/session.rs b/cmux-tui/crates/acpmux/src/hub/session.rs index 43f9812eac7a..17f74b4b1c5f 100644 --- a/cmux-tui/crates/acpmux/src/hub/session.rs +++ b/cmux-tui/crates/acpmux/src/hub/session.rs @@ -202,8 +202,6 @@ pub struct Session { pub(super) last_model_at: AtomicU64, /// The turn that logged `turn_silent` and has not heard its model since (`silent_turn.rs`). pub(super) silent_turn: StdMutex>, - /// The turn the turn cap's notice went into (`turn_cap.rs`): one notice per turn. - pub(super) turn_cap_noticed: StdMutex>, /// The turn cap's timer for the running turn (`turn_cap.rs`); the next turn aborts it. pub(super) turn_cap_timer: StdMutex>, /// The hub clock's time (`Hub::clock_now`) of the last record or diff --git a/cmux-tui/crates/acpmux/src/hub/steer_end.rs b/cmux-tui/crates/acpmux/src/hub/steer_end.rs index c537ca1125de..ad6d5f3575e6 100644 --- a/cmux-tui/crates/acpmux/src/hub/steer_end.rs +++ b/cmux-tui/crates/acpmux/src/hub/steer_end.rs @@ -124,6 +124,9 @@ impl Hub { let cancels = session.cancels.load(std::sync::atomic::Ordering::SeqCst); let prompt_id = opts.prompt_id.unwrap_or_default(); self.check_steer(session, control, opts.full_access_device.as_deref())?; + if !super::turn_cap::notice_reaches(session, opts.provenance.as_ref(), turn_id) { + return Err(RpcError::invalid_params(crate::claude_stdio::STEER_NO_TURN)); + } // A running turn has a live agent. let child = self.child_for(session).await?; // A family message is checked again here, as a dispatched one is, and the gate held @@ -154,6 +157,10 @@ impl Hub { if session.cancels.load(std::sync::atomic::Ordering::SeqCst) != cancels { return Err(super::turns::stopped_before_prompt()); } + // The turn cap's notice goes into the turn it names or nowhere (`turn_cap.rs`). + if !super::turn_cap::notice_reaches(session, opts.provenance.as_ref(), turn_id) { + return Err(RpcError::invalid_params(crate::claude_stdio::STEER_NO_TURN)); + } let mut params = json!({"sessionId": agent_sid, "prompt": wire_blocks}); // Its promptId names it to a withdraw (`queue_edit.rs` `withdraw`). params["_meta"] = json!({"steer": true, "promptId": prompt_id}); diff --git a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs index 5e8b3211e7df..e19dbc4e8019 100644 --- a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs +++ b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs @@ -5,10 +5,13 @@ //! //! A session with a cap (`SessionMeta::turn_cap_ms`, else [`default_turn_cap_ms`]) whose running //! turn is older than the cap stops taking other agents' steers: they wait in the queue, and the -//! next turn takes them together (`digest.rs`). The person's own steers still go in. The hub -//! steers one notice into that turn, once ([`NOTICE_KIND`]), when an agent's message is waiting -//! for it: on the first agent message past the cap, or at the cap when one waits already -//! ([`Hub::arm_turn_cap`]'s timer). The hub never cancels the turn itself. +//! next turn takes them together (`digest.rs`); one sent with `interrupt` stops nothing. The +//! person's own steers still go in, and so does the queue's Send now. The hub steers one notice +//! into that turn, once ([`NOTICE_KIND`]; the turn it went into is saved in the session's meta, so +//! a turn a restart adopts gets no second), when an agent's message is waiting for it: on the +//! first agent message past the cap, or at the cap when one waits already ([`Hub::arm_turn_cap`]'s +//! timer). The notice goes only into the turn it names ([`notice_reaches`]). The hub never cancels +//! the turn itself. //! //! The timer: one per session, armed when a turn starts. It wakes once, at the cap, and ends; a //! turn that starts before then aborts the previous turn's timer (`Session::turn_cap_timer`). It @@ -47,6 +50,18 @@ fn capped_turn(session: &Session, now: u64) -> Option<(String, u64)> { (now.saturating_sub(turn.started_at) >= cap).then_some((turn.turn_id, cap)) } +/// Whether a steer with `provenance` may go into turn `turn_id`: anything but the notice, and the +/// notice only into the turn it names while that turn still runs. A notice for a turn that ended +/// never reaches the next one. +pub(super) fn notice_reaches(session: &Session, provenance: Option<&Value>, turn_id: &str) -> bool { + let Some(p) = provenance.filter(|p| p.get("kind").and_then(Value::as_str) == Some(NOTICE_KIND)) + else { + return true; + }; + p.get("turnId").and_then(Value::as_str) == Some(turn_id) + && session.turn().is_some_and(|t| t.turn_id == turn_id) +} + /// The notice's words. pub(super) fn notice_text(cap_ms: u64) -> String { let minutes = cap_ms.div_ceil(60_000); @@ -124,12 +139,14 @@ impl Hub { return; } { - let mut noticed = session.turn_cap_noticed.lock().unwrap_or_else(|e| e.into_inner()); - if noticed.as_deref() == Some(turn_id) { + let mut meta = session.meta.lock().unwrap_or_else(|e| e.into_inner()); + if meta.turn_cap_noticed.as_deref() == Some(turn_id) { return; } - *noticed = Some(turn_id.to_owned()); + meta.turn_cap_noticed = Some(turn_id.to_owned()); } + // Saved before it goes, so a turn a restart adopts gets no second one. + self.save_meta(session); let opts = PromptOptions { prompt_id: Some(format!("turn-cap-{turn_id}")), // Into this turn or nowhere: a turn that ended meanwhile needs no notice. diff --git a/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs b/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs index a1db7935af44..23548e190afe 100644 --- a/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs +++ b/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs @@ -91,15 +91,25 @@ impl Fixture { steer: bool, steer_only: bool, ) -> (Result, tokio::task::JoinHandle>) { - let (tx, rx) = oneshot::channel(); let opts = PromptOptions { agent: from.map(str::to_owned), steer_only, - on_accepted: Some(Box::new(move |v| { - let _ = tx.send(v); - })), ..PromptOptions::default() }; + self.send_with(text, steer, opts).await + } + + /// [`Fixture::send`] with any options. + async fn send_with( + &self, + text: &str, + steer: bool, + mut opts: PromptOptions, + ) -> (Result, tokio::task::JoinHandle>) { + let (tx, rx) = oneshot::channel(); + opts.on_accepted = Some(Box::new(move |v| { + let _ = tx.send(v); + })); let (hub, session) = (self.hub.clone(), self.session.clone()); let blocks = vec![json!({"type": "text", "text": text})]; let task = @@ -259,11 +269,81 @@ async fn the_timer_sends_the_notice_at_the_cap_only_when_a_message_waits() { f.hub.set_turn_cap(&f.session, Some(3_000)); let turn = f.gated_turn("busy").await; let turn_id = f.session.turn().unwrap().turn_id; + let started = f.session.turn().unwrap().started_at; let (queued, _) = f.send("report from lane a", Some("lane-a"), false, false).await; assert_eq!(queued.unwrap()["queued"], true); - assert!(f.notices().is_empty(), "nothing before the cap"); + // Checked only while the cap is still ahead: a slow machine may already be past it. + let notices = f.notices(); + if now_ms().saturating_sub(started) < 2_500 { + assert!(notices.is_empty(), "nothing before the cap"); + } f.heard(&format!("steer: {}", notice_text(3_000))).await; assert_eq!(f.notices(), [turn_id]); f.open("busy").await; tokio::time::timeout(WAIT, turn).await.unwrap().unwrap().unwrap(); } + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn past_the_cap_send_now_still_steers_a_held_message_in_and_an_agents_interrupt_stops_nothing() + { + let mut f = fixture().await; + let cap = 600_000; + f.hub.set_turn_cap(&f.session, Some(cap)); + let turn = f.gated_turn("g").await; + let turn_id = f.session.turn().unwrap().turn_id; + f.age_turn(cap); + // An agent's interrupt past the cap waits like any of its messages: the turn runs on. + let opts = + PromptOptions { agent: Some("lane-a".into()), interrupt: true, ..PromptOptions::default() }; + let (interrupt, _) = f.send_with("stop for me", true, opts).await; + assert_eq!(interrupt.unwrap()["queued"], true); + let (held, held_task) = f.send("held from lane b", Some("lane-b"), true, false).await; + let prompt_id = held.unwrap()["promptId"].as_str().unwrap().to_owned(); + f.heard(&format!("steer: {}", notice_text(cap))).await; + tokio::time::sleep(Duration::from_millis(300)).await; + assert_eq!(f.session.turn().map(|t| t.turn_id), Some(turn_id.clone()), "nothing stopped it"); + // The queue's Send now is the person's: it steers the held message in, past the cap. + let edit = json!({"op": "send_now", "promptId": prompt_id}); + let sent = f.hub.queue_edit(&f.session, &edit, Control::default(), false, None).await.unwrap(); + assert_eq!(sent["ok"], true, "{sent}"); + f.heard("steer: held from lane b").await; + let answer = tokio::time::timeout(WAIT, held_task).await.unwrap().unwrap().unwrap(); + assert_eq!(answer["_meta"]["acpmux"]["steer"], true, "{answer}"); + assert_eq!(f.session.turn().map(|t| t.turn_id), Some(turn_id), "still the same turn"); + f.open("g").await; + tokio::time::timeout(WAIT, turn).await.unwrap().unwrap().unwrap(); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_notice_reaches_only_the_turn_it_names_and_a_restored_marker_sends_none() { + let mut f = fixture().await; + let cap = 600_000; + f.hub.set_turn_cap(&f.session, Some(cap)); + let turn = f.gated_turn("g").await; + let turn_id = f.session.turn().unwrap().turn_id; + // A notice for a turn that ended never goes into the one that runs now. + let stale = PromptOptions { + steer_only: true, + provenance: Some(json!({"kind": NOTICE_KIND, "turnId": "an-ended-turn", "capMs": cap})), + ..PromptOptions::default() + }; + let blocks = vec![json!({"type": "text", "text": notice_text(cap)})]; + let refused = f.hub.prompt_with(&f.session, blocks, "acpmux", true, stale).await; + let refused = refused.expect_err("a notice for another turn"); + assert!(refused.message.starts_with(crate::claude_stdio::STEER_NO_TURN), "{}", refused.message); + // The turn already had its notice (a restart adopted it with its saved marker): none again. + f.session.meta.lock().unwrap().turn_cap_noticed = Some(turn_id.clone()); + f.age_turn(cap); + let (held, _) = f.send("from lane a", Some("lane-a"), true, false).await; + assert_eq!(held.unwrap()["queued"], true); + tokio::time::sleep(Duration::from_millis(500)).await; + let records = f.hub.events(&f.session.id, 0, 100_000).unwrap(); + let sent = records + .iter() + .filter(|r| r.dir == "out" && r.kind == "session/prompt") + .filter(|r| r.msg["params"]["prompt"].to_string().contains(NOTICE)) + .count(); + assert_eq!(sent, 0, "no notice reached the agent"); + f.open("g").await; + tokio::time::timeout(WAIT, turn).await.unwrap().unwrap().unwrap(); +} diff --git a/cmux-tui/crates/acpmux/src/hub/turns.rs b/cmux-tui/crates/acpmux/src/hub/turns.rs index 41a0e7ddd96a..5b09426b1bd8 100644 --- a/cmux-tui/crates/acpmux/src/hub/turns.rs +++ b/cmux-tui/crates/acpmux/src/hub/turns.rs @@ -273,6 +273,7 @@ impl Hub { let running = session.turn(); // Another agent's message to a turn past its cap waits for the next turn (`turn_cap.rs`). let capped = running.is_some() + && !opts.send_now && self.turn_cap_holds(session, opts.provenance.as_ref(), opts.agent.as_deref()); let steer_now = steer && session.steering.load(Ordering::SeqCst) && running.is_some() && !capped; @@ -305,7 +306,8 @@ impl Hub { prompt_id: &prompt_id, control, trust_gate, - interrupt: opts.interrupt, + // Held past the cap, it waits: it stops no turn (`turn_cap.rs`). + interrupt: opts.interrupt && !capped, interrupt_turn: opts.interrupt_turn.as_deref(), provenance: opts.provenance.as_ref(), agent: agent.as_deref(), diff --git a/cmux-tui/crates/acpmux/src/server/requests.rs b/cmux-tui/crates/acpmux/src/server/requests.rs index 8e8d2530c2fc..e56a8ef7c997 100644 --- a/cmux-tui/crates/acpmux/src/server/requests.rs +++ b/cmux-tui/crates/acpmux/src/server/requests.rs @@ -1283,6 +1283,7 @@ async fn dispatch_request( }, full_access_device: super::remote_guard::full_access_device(hub, conn, ¶ms), no_queue: false, + send_now: false, }; let agent_message = opts.provenance.is_some() && !person_message; super::agent_messages::person_prompt(hub, conn, &s, agent_message).await; @@ -1804,7 +1805,9 @@ async fn dispatch_request( if let Some(value) = params.get("turnCapMs") { // The person's: the app, or the local socket with no agent behind it. A // session's agent never lifts a cap, its own or another's. - if !conn.is_person() && (conn.origin != Origin::Local || conn.caller().is_some()) { + // A caller token this daemon does not know is an agent's all the same. + let agent = conn.caller().is_some() || conn.unproven_caller.get().is_some(); + if !conn.is_person() && (conn.origin != Origin::Local || agent) { return Err(crate::hub::person::person_required( "changing a session's turn cap", )); diff --git a/cmux-tui/crates/acpmux/src/store.rs b/cmux-tui/crates/acpmux/src/store.rs index 974e7280a370..ca5594a97cce 100644 --- a/cmux-tui/crates/acpmux/src/store.rs +++ b/cmux-tui/crates/acpmux/src/store.rs @@ -362,6 +362,10 @@ pub struct SessionMeta { /// (`turn_cap::default_turn_cap_ms`). Set by the person only; never copied to a fork. #[serde(default, skip_serializing_if = "Option::is_none")] pub turn_cap_ms: Option, + /// The turn the turn cap's notice went into (`hub/turn_cap.rs`), kept so a turn a restart + /// adopts gets no second one. Never copied to a fork. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub turn_cap_noticed: Option, } /// Where a session sits in the tree of sessions that started each other From e2b8be599d2ddcc282c1a5964336090004d90334 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 29 Oct 2026 13:13:51 -0700 Subject: [PATCH 07/10] fix(acpmux): box the steer's future inside the prompt's The prompt future sits near a debug test thread's stack limit on base; the turn cap's additions pushed two hub_integration tests past it. Co-Authored-By: Claude Opus 5.5 Agent-Session: https://cmux.com/deeplink/agent?id=aid-1288bbb2b5d2f5c6b66a --- cmux-tui/crates/acpmux/src/hub/turns.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cmux-tui/crates/acpmux/src/hub/turns.rs b/cmux-tui/crates/acpmux/src/hub/turns.rs index 5b09426b1bd8..c8da3955c603 100644 --- a/cmux-tui/crates/acpmux/src/hub/turns.rs +++ b/cmux-tui/crates/acpmux/src/hub/turns.rs @@ -291,7 +291,10 @@ impl Hub { if steer_now { let turn_id = running.map(|t| t.turn_id).unwrap_or_default(); let opts = PromptOptions { prompt_id: Some(prompt_id), on_accepted: None, ..opts }; - return self.steer_turn(session, blocks, client, &turn_id, opts, &mut accept).await; + // Boxed: the steer's future stays out of this one, which every prompt's caller holds + // on its stack (a debug build's test thread has little room). + return Box::pin(self.steer_turn(session, blocks, client, &turn_id, opts, &mut accept)) + .await; } // An automatic continuation runs only while its chat still ends on the cut turn: no await // from here to `queue_slot`'s choice, so it never joins the queue (`auto_resume.rs`). From 5da544ef426215048060661f594be1dd8b354697 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 29 Oct 2026 13:39:08 -0700 Subject: [PATCH 08/10] fix(acpmux): the turn cap's notice is checked against its turn as it is written The notice's turn is checked again under the input handoff, which every prompt's write takes, so no later turn's prompt is written between the check and the notice. A notice logged but never written leaves steer_withdrawn in the log. Co-Authored-By: Claude Opus 5.5 Agent-Session: https://cmux.com/deeplink/agent?id=aid-1288bbb2b5d2f5c6b66a --- cmux-tui/crates/acpmux/src/hub/steer_end.rs | 38 +++++++++++---- cmux-tui/crates/acpmux/src/hub/turn_cap.rs | 18 +++++--- .../crates/acpmux/src/hub/turn_cap_tests.rs | 46 +++++++++++++++++++ cmux-tui/crates/acpmux/src/hub/turns.rs | 2 + 4 files changed, 89 insertions(+), 15 deletions(-) diff --git a/cmux-tui/crates/acpmux/src/hub/steer_end.rs b/cmux-tui/crates/acpmux/src/hub/steer_end.rs index 103002a4250a..2f138a5f8abe 100644 --- a/cmux-tui/crates/acpmux/src/hub/steer_end.rs +++ b/cmux-tui/crates/acpmux/src/hub/steer_end.rs @@ -23,7 +23,9 @@ fn ends_turn(answer: &Value) -> bool { /// ended this turn first. With `confirm_written`, `on_sent` requires the harness writer /// receipt; otherwise it runs when the transport accepts the prompt. Before its answer is awaited, the first-message title path uses this gap so a harness title update can /// win without waiting for the whole turn, and a family message lets go of its gate there -/// (`full_access_bound.rs` `family_gate`). +/// (`full_access_bound.rs` `family_gate`). A `bound` steer goes only into turn `turn_id`: checked +/// while it holds the input handoff, which no other prompt is written past, so no later turn's +/// prompt can come between the check and its write (the turn cap's notice, `turn_cap.rs`). #[allow(clippy::too_many_arguments)] pub(super) async fn agent_prompt_hook( session: &Session, @@ -31,6 +33,7 @@ pub(super) async fn agent_prompt_hook( params: Value, turn_id: &str, steer: bool, + bound: bool, fence: Option<&crate::agent::Fence>, confirm_written: bool, on_sent: F, @@ -69,6 +72,9 @@ pub(super) async fn agent_prompt_hook( { return settled(answer); } + if bound && session.turn().is_none_or(|t| t.turn_id != turn_id) { + return Err(RpcError::invalid_params(crate::claude_stdio::STEER_NO_TURN)); + } delivery_started.store(true, std::sync::atomic::Ordering::SeqCst); let callback = move || { drop(input_handoff); @@ -157,10 +163,6 @@ impl Hub { if session.cancels.load(std::sync::atomic::Ordering::SeqCst) != cancels { return Err(super::turns::stopped_before_prompt()); } - // The turn cap's notice goes into the turn it names or nowhere (`turn_cap.rs`). - if !super::turn_cap::notice_reaches(session, opts.provenance.as_ref(), turn_id) { - return Err(RpcError::invalid_params(crate::claude_stdio::STEER_NO_TURN)); - } let mut params = json!({"sessionId": agent_sid, "prompt": wire_blocks}); // Its promptId names it to a withdraw (`queue_edit.rs` `withdraw`). params["_meta"] = json!({"steer": true, "promptId": prompt_id}); @@ -194,9 +196,20 @@ impl Hub { let fence = family_gate.map(|hold| self.hold_until_written(session, hold)); // A fence no writer took is cut (review 86 of #23322). let _unsent = crate::agent::CutOnDrop(fence.as_ref()); - let answer = - agent_prompt_hook(session, &child, params, turn_id, true, fence.as_ref(), false, || {}) - .await; + // The turn cap's notice goes into the turn it names or nowhere (`turn_cap.rs`). + let bound = super::turn_cap::is_notice(opts.provenance.as_ref()); + let answer = agent_prompt_hook( + session, + &child, + params, + turn_id, + true, + bound, + fence.as_ref(), + false, + || {}, + ) + .await; session .steer_now .lock() @@ -208,7 +221,14 @@ impl Hub { } let mut r = match answer { Err(e) if e.message.starts_with(crate::claude_stdio::STEER_NO_TURN) => { - let Some(blocks) = fallback else { return Err(e) }; + let Some(blocks) = fallback else { + // Logged above, it never went: the log says so (a notice whose turn ended). + if bound { + let msg = json!({"promptId": prompt_id, "turnId": turn_id}); + self.append(session, "mux", "steer_withdrawn", msg); + } + return Err(e); + }; // A send-now (`interrupt`) still goes next, but the turn it was for has ended: // it stops no other. let opts = PromptOptions { diff --git a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs index e19dbc4e8019..afef8d80423d 100644 --- a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs +++ b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs @@ -10,7 +10,8 @@ //! into that turn, once ([`NOTICE_KIND`]; the turn it went into is saved in the session's meta, so //! a turn a restart adopts gets no second), when an agent's message is waiting for it: on the //! first agent message past the cap, or at the cap when one waits already ([`Hub::arm_turn_cap`]'s -//! timer). The notice goes only into the turn it names ([`notice_reaches`]). The hub never cancels +//! timer). The notice goes only into the turn it names ([`notice_reaches`], checked again as it is +//! written; one logged but never written leaves `steer_withdrawn`). The hub never cancels //! the turn itself. //! //! The timer: one per session, armed when a turn starts. It wakes once, at the cap, and ends; a @@ -50,15 +51,20 @@ fn capped_turn(session: &Session, now: u64) -> Option<(String, u64)> { (now.saturating_sub(turn.started_at) >= cap).then_some((turn.turn_id, cap)) } +/// Whether `provenance` is the notice's. +pub(super) fn is_notice(provenance: Option<&Value>) -> bool { + provenance.and_then(|p| p.get("kind")).and_then(Value::as_str) == Some(NOTICE_KIND) +} + /// Whether a steer with `provenance` may go into turn `turn_id`: anything but the notice, and the /// notice only into the turn it names while that turn still runs. A notice for a turn that ended -/// never reaches the next one. +/// never reaches the next one: checked here before it is logged, and again as it is written +/// (`steer_end.rs` `agent_prompt_hook`, `bound`). pub(super) fn notice_reaches(session: &Session, provenance: Option<&Value>, turn_id: &str) -> bool { - let Some(p) = provenance.filter(|p| p.get("kind").and_then(Value::as_str) == Some(NOTICE_KIND)) - else { + if !is_notice(provenance) { return true; - }; - p.get("turnId").and_then(Value::as_str) == Some(turn_id) + } + provenance.and_then(|p| p.get("turnId")).and_then(Value::as_str) == Some(turn_id) && session.turn().is_some_and(|t| t.turn_id == turn_id) } diff --git a/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs b/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs index 23548e190afe..b0badd4619b5 100644 --- a/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs +++ b/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs @@ -347,3 +347,49 @@ async fn a_notice_reaches_only_the_turn_it_names_and_a_restored_marker_sends_non f.open("g").await; tokio::time::timeout(WAIT, turn).await.unwrap().unwrap().unwrap(); } + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_notice_whose_turn_ends_on_its_way_never_reaches_the_next_turn() { + let mut f = fixture().await; + let cap = 600_000; + let first = f.gated_turn("g1").await; + let turn_id = f.session.turn().unwrap().turn_id; + // The notice stops after it is logged, before it is written (its model-sized copies). + let (arrived_tx, arrived) = oneshot::channel(); + let (release, release_rx) = oneshot::channel(); + *f.hub.copy_seam.lock().unwrap() = Some((arrived_tx, release_rx)); + let opts = PromptOptions { + prompt_id: Some(format!("turn-cap-{turn_id}")), + steer_only: true, + provenance: Some(json!({"kind": NOTICE_KIND, "turnId": turn_id, "capMs": cap})), + ..PromptOptions::default() + }; + let (hub, session) = (f.hub.clone(), f.session.clone()); + let notice = tokio::spawn(async move { + let blocks = vec![json!({"type": "text", "text": notice_text(cap)})]; + hub.prompt_with(&session, blocks, "acpmux", true, opts).await + }); + tokio::time::timeout(WAIT, arrived).await.unwrap().unwrap(); + // Its turn ends and the next one starts meanwhile. + f.open("g1").await; + tokio::time::timeout(WAIT, first).await.unwrap().unwrap().unwrap(); + let second = f.gated_turn("g2").await; + assert_ne!(f.session.turn().unwrap().turn_id, turn_id); + release.send(()).unwrap(); + let refused = tokio::time::timeout(WAIT, notice).await.unwrap().unwrap(); + let refused = refused.expect_err("a notice for a turn that ended"); + assert!(refused.message.starts_with(crate::claude_stdio::STEER_NO_TURN), "{}", refused.message); + let records = f.hub.events(&f.session.id, 0, 100_000).unwrap(); + let written = records + .iter() + .filter(|r| r.dir == "out" && r.kind == "session/prompt") + .filter(|r| r.msg["params"]["prompt"].to_string().contains(NOTICE)) + .count(); + assert_eq!(written, 0, "the next turn's agent never got it"); + let withdrawn = records + .iter() + .any(|r| r.kind == "steer_withdrawn" && r.msg["promptId"] == format!("turn-cap-{turn_id}")); + assert!(withdrawn, "the log says the logged notice never went"); + f.open("g2").await; + tokio::time::timeout(WAIT, second).await.unwrap().unwrap().unwrap(); +} diff --git a/cmux-tui/crates/acpmux/src/hub/turns.rs b/cmux-tui/crates/acpmux/src/hub/turns.rs index 1dbf6ddfcbb7..4f37b4a96a22 100644 --- a/cmux-tui/crates/acpmux/src/hub/turns.rs +++ b/cmux-tui/crates/acpmux/src/hub/turns.rs @@ -638,6 +638,7 @@ impl Hub { json!({"sessionId": agent_sid, "prompt": wire}), &turn_id, false, + false, fence.as_ref(), confirm_team_write, || { @@ -783,6 +784,7 @@ impl Hub { json!({"sessionId": sid2, "prompt": wire}), &turn_id, false, + false, fence.as_ref(), confirm_team_write, || { From c3ed34f889ee85b3140891cff5609bd70d53c94e Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 29 Oct 2026 14:08:27 -0700 Subject: [PATCH 09/10] fix(acpmux): turn cap review round 3: the notice marker and a timing-dependent test - The notice marker is set only while the turn it names still runs, read under the meta lock, so an attempt for an ended turn never replaces a later turn's marker. - The timer test reads only logged notices before the cap: a notice may be logged and still on its way on a slow machine. Co-Authored-By: Claude Opus 5.5 Agent-Session: https://cmux.com/deeplink/agent?id=aid-1288bbb2b5d2f5c6b66a --- cmux-tui/crates/acpmux/src/hub/turn_cap.rs | 6 +++- .../crates/acpmux/src/hub/turn_cap_tests.rs | 28 ++++++++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs index afef8d80423d..dcf17bcb1911 100644 --- a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs +++ b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs @@ -145,8 +145,12 @@ impl Hub { return; } { + // The turn is read under the meta lock (nothing takes the meta lock under the turn's), + // so an attempt for a turn that has ended never replaces a later turn's marker. let mut meta = session.meta.lock().unwrap_or_else(|e| e.into_inner()); - if meta.turn_cap_noticed.as_deref() == Some(turn_id) { + if meta.turn_cap_noticed.as_deref() == Some(turn_id) + || session.turn().is_none_or(|t| t.turn_id != turn_id) + { return; } meta.turn_cap_noticed = Some(turn_id.to_owned()); diff --git a/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs b/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs index b0badd4619b5..acb5808efdf3 100644 --- a/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs +++ b/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs @@ -133,20 +133,27 @@ impl Fixture { turn.started_at = turn.started_at.saturating_sub(cap + 1); } - /// The notices the agent got, by the turn their provenance names. + /// The notices logged, by the turn their provenance names. + fn logged_notices(&self) -> Vec { + let records = self.hub.events(&self.session.id, 0, 100_000).unwrap(); + records + .iter() + .filter(|r| r.kind == "user_message") + .filter(|r| r.msg["_meta"]["provenance"]["kind"] == NOTICE_KIND) + .map(|r| r.msg["_meta"]["provenance"]["turnId"].as_str().unwrap_or("").to_owned()) + .collect() + } + + /// The notices the agent got, by the turn their provenance names: called once every notice + /// logged has been heard (none may still be on its way). fn notices(&self) -> Vec { + let logged = self.logged_notices(); let records = self.hub.events(&self.session.id, 0, 100_000).unwrap(); let sent = records .iter() .filter(|r| r.dir == "out" && r.kind == "session/prompt") .filter(|r| r.msg["params"]["prompt"].to_string().contains(NOTICE)) .count(); - let logged: Vec = records - .iter() - .filter(|r| r.kind == "user_message") - .filter(|r| r.msg["_meta"]["provenance"]["kind"] == NOTICE_KIND) - .map(|r| r.msg["_meta"]["provenance"]["turnId"].as_str().unwrap_or("").to_owned()) - .collect(); assert_eq!(sent, logged.len(), "every notice logged is one the agent got"); logged } @@ -272,10 +279,11 @@ async fn the_timer_sends_the_notice_at_the_cap_only_when_a_message_waits() { let started = f.session.turn().unwrap().started_at; let (queued, _) = f.send("report from lane a", Some("lane-a"), false, false).await; assert_eq!(queued.unwrap()["queued"], true); - // Checked only while the cap is still ahead: a slow machine may already be past it. - let notices = f.notices(); + // Checked only while the cap is still ahead: a slow machine may already be past it, its + // notice logged and still on its way to the agent. + let logged = f.logged_notices(); if now_ms().saturating_sub(started) < 2_500 { - assert!(notices.is_empty(), "nothing before the cap"); + assert!(logged.is_empty(), "nothing before the cap"); } f.heard(&format!("steer: {}", notice_text(3_000))).await; assert_eq!(f.notices(), [turn_id]); From a405b91aaea3a6af95776cace63eb6bd005f8d3f Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 29 Oct 2026 14:42:42 -0700 Subject: [PATCH 10/10] fix(acpmux): turn cap review round 4: a message crossing the cap, and the notice's log dedupe - An agent message queued as the running turn crosses its cap re-checks the cap after it is queued, so the timer's earlier look at the queue cannot leave the turn without its notice. - The notice goes with resend: a notice the log already holds (its meta marker's save was lost) is answered from the log, never sent again. Co-Authored-By: Claude Opus 5.5 Agent-Session: https://cmux.com/deeplink/agent?id=aid-1288bbb2b5d2f5c6b66a --- cmux-tui/crates/acpmux/src/hub/turn_cap.rs | 11 +++++++++++ .../crates/acpmux/src/hub/turn_cap_tests.rs | 19 +++++++++++++++++++ cmux-tui/crates/acpmux/src/hub/turns.rs | 9 +++++++++ 3 files changed, 39 insertions(+) diff --git a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs index dcf17bcb1911..2d2d1db03311 100644 --- a/cmux-tui/crates/acpmux/src/hub/turn_cap.rs +++ b/cmux-tui/crates/acpmux/src/hub/turn_cap.rs @@ -108,6 +108,14 @@ impl Hub { true } + /// Sends the notice when the running turn is past its cap: an agent message was just queued + /// behind it ([`Hub::turn_cap_notice`] sends one per turn). + pub(super) fn turn_cap_recheck(self: &Arc, session: &Arc) { + if let Some((turn_id, cap)) = capped_turn(session, now_ms()) { + self.turn_cap_notice(session, &turn_id, cap); + } + } + /// Arms the cap's timer for turn `turn_id`, which starts now (module docs). pub(super) fn arm_turn_cap(self: &Arc, session: &Arc, turn_id: &str) { let mut timer = session.turn_cap_timer.lock().unwrap_or_else(|e| e.into_inner()); @@ -159,6 +167,9 @@ impl Hub { self.save_meta(session); let opts = PromptOptions { prompt_id: Some(format!("turn-cap-{turn_id}")), + // One the log holds already (sent before a restart whose meta save was lost) is + // answered from the log, never sent again (`prompt_ledger.rs` `logged_prompt`). + resend: true, // Into this turn or nowhere: a turn that ended meanwhile needs no notice. steer_only: true, provenance: Some(json!({"kind": NOTICE_KIND, "turnId": turn_id, "capMs": cap})), diff --git a/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs b/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs index acb5808efdf3..7d6cf4ee25aa 100644 --- a/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs +++ b/cmux-tui/crates/acpmux/src/hub/turn_cap_tests.rs @@ -401,3 +401,22 @@ async fn a_notice_whose_turn_ends_on_its_way_never_reaches_the_next_turn() { f.open("g2").await; tokio::time::timeout(WAIT, second).await.unwrap().unwrap().unwrap(); } + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_notice_the_log_holds_is_not_sent_again_when_its_marker_is_lost() { + let mut f = fixture().await; + let cap = 600_000; + f.hub.set_turn_cap(&f.session, Some(cap)); + let turn = f.gated_turn("g").await; + let turn_id = f.session.turn().unwrap().turn_id; + f.age_turn(cap); + let (_, _a) = f.send("from lane a", Some("lane-a"), true, false).await; + f.heard(&format!("steer: {}", notice_text(cap))).await; + // The meta save of its marker was lost (a crash before it reached the disk). + f.session.meta.lock().unwrap().turn_cap_noticed = None; + let (_, _b) = f.send("from lane b", Some("lane-b"), true, false).await; + tokio::time::sleep(Duration::from_millis(500)).await; + assert_eq!(f.notices(), [turn_id], "the log's notice stands; none is sent again"); + f.open("g").await; + tokio::time::timeout(WAIT, turn).await.unwrap().unwrap().unwrap(); +} diff --git a/cmux-tui/crates/acpmux/src/hub/turns.rs b/cmux-tui/crates/acpmux/src/hub/turns.rs index 146a16c02071..e82d92a9f14e 100644 --- a/cmux-tui/crates/acpmux/src/hub/turns.rs +++ b/cmux-tui/crates/acpmux/src/hub/turns.rs @@ -259,12 +259,21 @@ impl Hub { // is committed (queued, started, or steered in), never before a refusal. let supersede = opts.supersede.clone(); let replaced = std::sync::OnceLock::>::new(); + // Another agent's message queued as the running turn crosses its cap still gets the + // turn its notice (`turn_cap.rs`): the timer may have looked at the queue just before. + let from_agent = + super::digest::agent_sender(opts.provenance.as_ref(), opts.agent.as_deref()).is_some() + && !opts.send_now; let mut accept = |mut v: Value| { if let Some(f) = on_accepted.take() { if supersede.is_some() { super::queue::supersede::mark(&mut v, replaced.get()); } + let queued = v.get("queued") == Some(&Value::Bool(true)); f(v); + if queued && from_agent { + self.turn_cap_recheck(session); + } } }; // A prompt that cannot reach any agent (none running, and its folder or harness is gone)