agentmux_srv/server/prep/mod.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Prepare, then proceed (SPEC_AGENT_PREP_BEFORE_SESSION_RESTART_2026_11_14.md ยง3): when a user
5//! action is about to restart an agent's session, the agent gets a short window to reach a safe
6//! point and leave a handoff, then the action runs.
7//!
8//! An action calls [`decide`]. `Skip` means nothing is lost or the agent cannot take a turn: the
9//! action runs at once, as it always did. `Ask` means [`open`]: one `[AgentMux]` note starts a turn
10//! on the agent, the agent answers with `Agents ready` (a handoff, or once for more time), and the
11//! action's closure runs when the agent has answered and its turn has ended, when the person presses
12//! "Switch now", or when the window runs out. A window, never a gate: nothing waits for an agent that
13//! cannot answer, and the closure runs exactly once.
14
15mod carry;
16mod decide;
17mod handlers;
18mod ledger;
19mod meta;
20mod note;
21mod window;
22
23pub use carry::hold_for_other_panes;
24pub use decide::{decide, Decision};
25pub use handlers::{handle_ready_self, register};
26pub use ledger::{cancel, is_open, open, open_with};
27pub(crate) use meta::set_block_meta;
28
29use std::time::Duration;
30
31/// How long the agent has, from the person's action.
32#[cfg(not(test))]
33pub const WINDOW: Duration = Duration::from_secs(30);
34/// Short in tests, which wait for real.
35#[cfg(test)]
36pub const WINDOW: Duration = Duration::from_millis(1_000);
37/// The one extension an agent may ask for (a push or a deploy in progress), from the same moment.
38#[cfg(not(test))]
39pub const EXTENDED_WINDOW: Duration = Duration::from_secs(120);
40#[cfg(test)]
41pub const EXTENDED_WINDOW: Duration = Duration::from_millis(3_000);
42/// The pane meta the composer's chip reads: `{id, until_ms, extended, why, action}`.
43pub const META_PREP: &str = "agent:prep";
44/// The audit actions: one when the agent is asked, one when it has answered or the window ended.
45pub const AUDIT_REQUESTED: &str = "prep.requested";
46pub const AUDIT_ANSWERED: &str = "prep.answered";
47
48/// What a user action asks of the agent before it restarts the agent's session.
49#[derive(Debug, Clone)]
50pub struct Prep {
51 pub def_id: String,
52 /// The agent as its panes show it, for the note and the audit rows.
53 pub agent_name: String,
54 /// The agent's open panes.
55 pub blocks: Vec<String>,
56 /// A short name for the note, the audit and the chip (`account_switch`).
57 pub action: &'static str,
58 /// What the action moves to; asking for the same thing again is a no-op, for another replaces it.
59 pub target: String,
60 /// One line for the note: what will happen.
61 pub what: String,
62 /// Who asked: `operator`, or an agent's slug.
63 pub actor: String,
64 /// Whether the action ends a turn still running when the window ends (`when: now`), as the note says.
65 pub cuts_turn: bool,
66}
67
68/// How the window ended.
69#[derive(Debug, Clone, Copy, PartialEq, Eq)]
70pub enum How {
71 /// The agent answered and its turn has ended.
72 Ready,
73 /// The window ran out first (or the agent never answered).
74 TimedOut,
75 /// The person pressed "Switch now".
76 PersonSaidNow,
77 /// The note could not be delivered, so nothing was asked.
78 NotAsked,
79}
80
81impl How {
82 /// The audit reason of the `prep.answered` row.
83 pub fn reason(self) -> &'static str {
84 match self {
85 How::Ready => "ready",
86 How::TimedOut => "timed_out",
87 How::PersonSaidNow => "person_said_now",
88 How::NotAsked => "not_asked:note_not_delivered",
89 }
90 }
91}
92
93/// What the action does once the window ends. Runs exactly once, on a blocking thread.
94pub type Proceed = Box<dyn FnOnce(How) + Send + 'static>;
95
96#[cfg(test)]
97pub(crate) mod tests;
98#[cfg(test)]
99pub(crate) use ledger::{answer, Answer};