agentmux_srv_core/backend/
user_stop.rs1use std::collections::HashMap;
13use std::sync::{Mutex, MutexGuard, OnceLock};
14
15pub const STOP_GRACE_MS: i64 = 15_000;
18
19const STOP_KEEP_MS: i64 = 11 * 60 * 1000;
22
23#[derive(Default)]
25struct Stops(HashMap<String, i64>);
26
27impl Stops {
28 fn note(&mut self, block_id: &str, now_ms: i64) {
29 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
52pub fn note_user_stop(block_id: &str) {
54 stops().note(block_id, agentmux_common::time::now_ms());
55}
56
57pub fn stopped_recently(block_id: &str) -> bool {
59 stops().recent(block_id, agentmux_common::time::now_ms())
60}
61
62pub fn stopped_since(block_id: &str, since_ms: i64) -> bool {
65 stops().since(block_id, since_ms)
66}
67
68pub 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 #[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}