Skip to main content

agentmux_srv/server/
hold_reason.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Why a jekt is held (`held_reason` in the inject answer;
5//! SPEC_JEKT_DELIVERY_STATES_AND_MAILBOX_2026_10_01.md §5.1).
6
7use crate::backend::agent_state::AgentState;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub(crate) enum HoldReason {
11    /// A known agent with no process here.
12    NotRunning,
13    /// The agent's pane is open here, but its controller is not there yet: AgentMux is starting and
14    /// resuming its panes (`backend::reactive::controller_wait`). Delivered when the controller registers; the
15    /// sender is told what a stopped agent's sender is while it is started (`HeldStart::Starting`).
16    Starting,
17    /// The agent is here but its spawn gate refused: not signed in. Also an
18    /// agent whose sign-in was refused (`AgentState::SignedOut`): to the
19    /// sender it is the same condition.
20    NeedsLogin,
21    /// The agent is here but its account is out of quota
22    /// (`AgentState::Limited`). Held from step 2c of
23    /// `docs/specs/SPEC_UNRESPONSIVE_AGENTS_2026_10_23.md` §5.
24    Limited,
25    /// The agent is here and signed in, but its session has been quiet past the
26    /// prompt cache's lifetime and the message is routine
27    /// (`backend::cold_hold`).
28    Idle,
29    /// Quota mode is tight, the message is routine and the agent has no turn in flight
30    /// (`backend::quota_mode`).
31    Quota,
32    /// The agent is between turns and its model API is offline (`backend::agent_status::decide`).
33    ApiOffline,
34    /// The agent runs on a LAN peer that is not reachable now (`server::reactive::peer_hold`): kept on the
35    /// sender's instance and delivered when the peer is seen again.
36    PeerOffline,
37}
38
39impl HoldReason {
40    pub(crate) fn as_str(self) -> &'static str {
41        match self {
42            HoldReason::NotRunning => "not_running",
43            HoldReason::Starting => "starting",
44            HoldReason::NeedsLogin => "needs_login",
45            HoldReason::Limited => "usage_limit",
46            HoldReason::Idle => crate::backend::cold_hold::REASON_IDLE,
47            HoldReason::Quota => crate::backend::cold_hold::REASON_QUOTA,
48            HoldReason::ApiOffline => "api_offline",
49            HoldReason::PeerOffline => "peer_offline",
50        }
51    }
52
53    /// The same reason in `agent_availability`'s terms.
54    pub(crate) fn wait_reason(self) -> crate::backend::agent_availability::WaitReason {
55        use crate::backend::agent_availability::WaitReason;
56        match self {
57            // Its start ends the wait, as for an agent that is not running.
58            HoldReason::NotRunning | HoldReason::Starting => WaitReason::NotRunning,
59            HoldReason::NeedsLogin => WaitReason::NeedsLogin,
60            HoldReason::Limited => WaitReason::UsageLimit,
61            HoldReason::Idle => WaitReason::Cold,
62            HoldReason::Quota => WaitReason::Quota,
63            HoldReason::ApiOffline => WaitReason::ApiOffline,
64            // Its machine has to come back; the sender reads `retry_when` from the answer (`peer_online`).
65            HoldReason::PeerOffline => WaitReason::NotRunning,
66        }
67    }
68
69    /// The reason a message to an agent in `state` is held, or `None` when
70    /// that state doesn't hold messages.
71    pub(crate) fn for_state(state: AgentState) -> Option<Self> {
72        match state {
73            AgentState::Limited => Some(HoldReason::Limited),
74            AgentState::SignedOut => Some(HoldReason::NeedsLogin),
75            _ => None,
76        }
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn quota_is_a_hold_reason_with_its_own_wire_strings() {
86        assert_eq!(HoldReason::Quota.as_str(), "quota");
87        assert_eq!(HoldReason::Quota.wait_reason().retry_when(), "mode_off_or_session_active");
88        assert_eq!(HoldReason::for_state(AgentState::Idle), None, "an idle state alone holds nothing");
89    }
90
91    #[test]
92    fn an_offline_api_is_a_hold_reason_that_matches_its_wait_reason() {
93        assert_eq!(HoldReason::ApiOffline.as_str(), HoldReason::ApiOffline.wait_reason().as_str());
94        assert_eq!(HoldReason::ApiOffline.wait_reason().retry_when(), "api_online");
95    }
96
97    #[test]
98    fn an_unavailable_agent_holds_with_its_reason_and_the_rest_do_not() {
99        assert_eq!(HoldReason::for_state(AgentState::Limited).map(HoldReason::as_str), Some("usage_limit"));
100        assert_eq!(HoldReason::for_state(AgentState::SignedOut).map(HoldReason::as_str), Some("needs_login"));
101        for s in [AgentState::Idle, AgentState::Working, AgentState::Waiting, AgentState::Stopped, AgentState::Error] {
102            assert_eq!(HoldReason::for_state(s), None, "{s:?}");
103        }
104    }
105}