Skip to main content

agentmux_srv_core/backend/
user_stop.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The user's Stop on an agent's turn, and the grace window after it. The
5//! turn end a Stop causes is the user's own action: the notification router
6//! sends no "finished" for it, and the Swarm Supervisor neither judges nor
7//! continues it (SPEC_SWARM_SUPERVISOR_2026_10_16.md §3.4). Recorded by
8//! `notify.emit`'s `TurnStopped` (`server/notify_handlers.rs`) at once, for the
9//! Supervisor, and again by the notification router when it reaches that event
10//! in its queue, so a turn start queued before the Stop can't erase it.
11
12use std::collections::HashMap;
13use std::sync::{Mutex, MutexGuard, OnceLock};
14
15/// How long after a user Stop the turn end it causes counts as the user's own
16/// action.
17pub const STOP_GRACE_MS: i64 = 15_000;
18
19/// How long a Stop is remembered for [`stopped_since`]: longer than the longest
20/// Supervisor wait before a continuation (`supervisor:cooldown`, 600 s).
21const STOP_KEEP_MS: i64 = 11 * 60 * 1000;
22
23/// block_id → when the user last stopped its turn (epoch ms).
24#[derive(Default)]
25struct Stops(HashMap<String, i64>);
26
27impl Stops {
28    fn note(&mut self, block_id: &str, now_ms: i64) {
29        // Bounded: an entry past the grace window can't matter any more.
30        self.0.retain(|_, at| now_ms - *at < STOP_KEEP_MS);
31        self.0.insert(block_id.to_string(), now_ms);
32    }
33
34    fn recent(&self, block_id: &str, now_ms: i64) -> bool {
35        self.0.get(block_id).is_some_and(|at| now_ms - *at < STOP_GRACE_MS)
36    }
37
38    fn since(&self, block_id: &str, since_ms: i64) -> bool {
39        self.0.get(block_id).is_some_and(|at| *at >= since_ms)
40    }
41
42    fn forget(&mut self, block_id: &str) {
43        self.0.remove(block_id);
44    }
45}
46
47fn stops() -> MutexGuard<'static, Stops> {
48    static STOPS: OnceLock<Mutex<Stops>> = OnceLock::new();
49    STOPS.get_or_init(Default::default).lock().unwrap_or_else(|e| e.into_inner())
50}
51
52/// The user pressed Stop on this block's turn.
53pub fn note_user_stop(block_id: &str) {
54    stops().note(block_id, agentmux_common::time::now_ms());
55}
56
57/// Whether the user stopped this block's turn within the grace window.
58pub fn stopped_recently(block_id: &str) -> bool {
59    stops().recent(block_id, agentmux_common::time::now_ms())
60}
61
62/// Whether the user stopped this block's turn at or after `since_ms` (epoch ms), however long
63/// ago that is within [`STOP_KEEP_MS`]: a continuation that waited asks this, not the grace window.
64pub fn stopped_since(block_id: &str, since_ms: i64) -> bool {
65    stops().since(block_id, since_ms)
66}
67
68/// A new turn started on this block: an earlier Stop no longer applies to it.
69pub fn forget(block_id: &str) {
70    stops().forget(block_id);
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn a_user_stop_holds_the_block_for_the_grace_window_only() {
79        let mut stops = Stops::default();
80        stops.note("b1", 1_000_000);
81        assert!(stops.recent("b1", 1_000_000 + STOP_GRACE_MS - 1));
82        assert!(!stops.recent("b1", 1_000_000 + STOP_GRACE_MS));
83        assert!(!stops.recent("b2", 1_000_000));
84    }
85
86    #[test]
87    fn a_new_turn_forgets_the_stop() {
88        let mut stops = Stops::default();
89        stops.note("b1", 1_000_000);
90        stops.forget("b1");
91        assert!(!stops.recent("b1", 1_000_001));
92    }
93
94    /// The order the router sees: `notify.emit` records the Stop, a turn start
95    /// queued before it is processed (and forgets it), then the queued Stop
96    /// records it again. The turn end that follows is still the user's.
97    #[test]
98    fn a_turn_start_queued_before_the_stop_does_not_erase_it() {
99        let mut stops = Stops::default();
100        stops.note("b1", 1_000_000);
101        stops.forget("b1");
102        stops.note("b1", 1_000_005);
103        assert!(stops.recent("b1", 1_000_010));
104    }
105
106    #[test]
107    fn stops_past_the_window_are_dropped_when_a_new_one_is_noted() {
108        let mut stops = Stops::default();
109        stops.note("old", 1_000_000);
110        stops.note("new", 1_000_000 + STOP_KEEP_MS);
111        assert!(!stops.0.contains_key("old"));
112        assert!(stops.0.contains_key("new"));
113    }
114
115    #[test]
116    fn the_shared_record_is_seen_by_every_reader() {
117        let block = "user-stop-shared-record-test-block";
118        note_user_stop(block);
119        assert!(stopped_recently(block));
120        forget(block);
121        assert!(!stopped_recently(block));
122    }
123
124    #[test]
125    fn a_stop_is_remembered_past_the_grace_window_for_a_waiting_continuation() {
126        let mut stops = Stops::default();
127        stops.note("b1", 1_000_000);
128        let later = 1_000_000 + 5 * 60 * 1000;
129        assert!(!stops.recent("b1", later), "the grace window is over");
130        assert!(stops.since("b1", 1_000_000 - 1), "but a wait that began before the Stop still sees it");
131        assert!(!stops.since("b1", 1_000_001), "a Stop before the wait began is not this wait's");
132        assert!(!stops.since("other", 0));
133        stops.note("b2", later);
134        assert!(stops.since("b1", 0), "another pane's Stop doesn't erase it within the keep window");
135    }
136}