agentmux_srv_core/backend/agent_runtime_state.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! One agent's runtime state as a viewer sees it (`server/agent_runtime.rs` builds the rows),
5//! kept below the server so `mps_events` can name the `agentruntime:changed:` payload.
6
7use serde::{Deserialize, Serialize};
8
9use super::rpc_types::HandoverSummary;
10use super::runtime_adapter::RuntimeKind;
11use super::storage::store::Store;
12
13/// The state of one agent as a viewer sees it.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ts_rs::TS)]
15#[ts(export, export_to = "../../../frontend/types/rpc/")]
16#[serde(rename_all = "snake_case")]
17pub enum AgentRuntimeState {
18 /// No process: never started, ended, or its pane closed.
19 Stopped,
20 /// A process is coming up and has not reported a state yet.
21 Starting,
22 Idle,
23 Working,
24 /// A question waits for the user in a running turn, or the agent is between turns and a Browser request
25 /// of its is still open (`detail` is `browser_request`).
26 WaitingOnUser,
27 /// Out of quota; `resets_at` when the provider said when it lifts.
28 Limited,
29 SignedOut,
30 /// Its process ended with an error.
31 Error,
32}
33
34/// Whether the agent should be running (spec rule 5).
35#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ts_rs::TS)]
36#[ts(export, export_to = "../../../frontend/types/rpc/")]
37#[serde(rename_all = "snake_case")]
38pub enum AgentDesiredState {
39 Running,
40 Stopped,
41}
42
43/// One agent's runtime state, with what a row needs to show it before it has a pane. The payload of
44/// `agentruntime:changed:<agent id>` and a row of `agentruntime.list`.
45#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ts_rs::TS)]
46#[ts(export, export_to = "../../../frontend/types/rpc/")]
47pub struct AgentRuntime {
48 /// The agent's UID, the id of `agentidentities:changed:<agent id>`.
49 pub agent_id: String,
50 pub state: AgentRuntimeState,
51 /// Why, for a state that has a reason: `usage_limit`, `needs_login`, `starts_on_first_message` or
52 /// `browser_request`; for `error`, the class of its last failure (`rate_limited`, `network`...).
53 #[serde(default, skip_serializing_if = "Option::is_none")]
54 #[ts(optional)]
55 pub detail: Option<String>,
56 /// When a limit lifts (unix ms), when it is known.
57 #[serde(default, skip_serializing_if = "Option::is_none")]
58 #[ts(optional, type = "number")]
59 pub resets_at: Option<u64>,
60 /// The block the agent runs in, shown in a pane or parked; absent for an agent that has never had one.
61 #[serde(default, skip_serializing_if = "Option::is_none")]
62 #[ts(optional)]
63 pub block_id: Option<String>,
64 /// The pane where the person answers, when that is not the agent's own: the Browser pane its open
65 /// request waits in (`detail` is `browser_request`).
66 #[serde(default, skip_serializing_if = "Option::is_none")]
67 #[ts(optional)]
68 pub waiting_in: Option<String>,
69 pub desired: AgentDesiredState,
70 pub name: String,
71 pub provider: String,
72 pub icon: String,
73 /// The colour its pane shows, once it has had one.
74 #[serde(default, skip_serializing_if = "Option::is_none")]
75 #[ts(optional)]
76 pub color: Option<String>,
77 /// Why a start would be refused now, for an agent that is stopped and wanted running (a message waits
78 /// for it, or its block says running): a limit it is over, as `agent_lifecycle::start_blocked` words it.
79 #[serde(default, skip_serializing_if = "Option::is_none")]
80 #[ts(optional)]
81 pub blocked: Option<String>,
82 /// Where it runs: from its pane's `agentMode` when it has a block, else from its definition's
83 /// `agent_type`, so a stopped agent says it too. Anything unknown is `host`.
84 #[serde(default)]
85 pub runtime: RuntimeKind,
86 /// The model it runs, as its harness names it, when anything says (live facts spec §4.1).
87 #[serde(default, skip_serializing_if = "Option::is_none")]
88 #[ts(optional)]
89 pub model_id: Option<String>,
90 /// The short form of `model_id` (`Sonnet 5.5`), or the route's own label.
91 #[serde(default, skip_serializing_if = "Option::is_none")]
92 #[ts(optional)]
93 pub model_label: Option<String>,
94 /// Where the model is known from: `effective` (the CLI reported it), `observed`, `route`,
95 /// `requested` or `unknown`. A `requested` or `unknown` one is shown muted.
96 #[serde(default, skip_serializing_if = "Option::is_none")]
97 #[ts(optional)]
98 pub model_source: Option<String>,
99 /// The effort level it runs at, when it is known.
100 #[serde(default, skip_serializing_if = "Option::is_none")]
101 #[ts(optional)]
102 pub effort: Option<String>,
103 /// The short name of the model it switches to at the end of the turn, when a model change waits.
104 #[serde(default, skip_serializing_if = "Option::is_none")]
105 #[ts(optional)]
106 pub pending_switch: Option<String>,
107 /// The short name of the model its definition asks for, when that is not the one it runs.
108 #[serde(default, skip_serializing_if = "Option::is_none")]
109 #[ts(optional)]
110 pub definition_model_label: Option<String>,
111 /// When it went quiet (unix ms), while it owes work and has done nothing for the quiet time: the Swarm shows
112 /// "quiet 22 min" from it (SPEC_LEADER_TOLD_WHEN_AN_AGENT_GOES_QUIET_2026_11_21.md §6). From the last quiet sweep.
113 #[serde(default, skip_serializing_if = "Option::is_none")]
114 #[ts(optional, type = "number")]
115 pub quiet_since_ms: Option<u64>,
116 /// Where the agent went, when it was handed over or moved to another computer: its pane in the window shows that
117 /// it moved, with the way back. Absent for an agent that is here.
118 #[serde(default, skip_serializing_if = "Option::is_none")]
119 #[ts(optional)]
120 pub handover: Option<HandoverSummary>,
121}
122
123/// Where the agent went, when it carries a hand-over mark. A mark that cannot be read is no mark here: the row still lists, and the
124/// launch gate is what keeps the agent stopped.
125pub fn handover_summary(mstore: &Store, definition_id: &str) -> Option<HandoverSummary> {
126 let mark = mstore.agent_handover_get(definition_id).ok().flatten()?;
127 Some(HandoverSummary { state: mark.state, to: mark.to, at_ms: mark.at_ms })
128}