agentmux_srv/server/prep/
carry.rs1use crate::backend::blockcontroller::BLOCK_CONTROLLER_PERSISTENT;
11use crate::backend::continuity_handoff::{self, NOTE_KEY};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub(super) enum Reader {
16 Spawn,
18 Note,
20}
21
22fn reader_of(block: &str) -> Reader {
23 match crate::backend::blockcontroller::get_controller(block) {
24 Some(ctrl) if ctrl.controller_type() != BLOCK_CONTROLLER_PERSISTENT => Reader::Note,
25 _ => Reader::Spawn,
26 }
27}
28
29pub fn hold_for_other_panes(def_id: &str, restarted: &[&str]) -> bool {
33 hold_where(def_id, restarted, reader_of)
34}
35
36fn hold_where(def_id: &str, restarted: &[&str], reader: impl Fn(&str) -> Reader) -> bool {
37 let readers: Vec<Reader> = restarted.iter().map(|b| reader(b)).collect();
38 if readers.is_empty() || readers.contains(&Reader::Spawn) {
39 return false;
40 }
41 let Some(handoff) = continuity_handoff::take(def_id) else { return false };
42 tracing::info!(def_id, panes = restarted.len(), "continuity: holding the handoff the agent left as a note, its controller has no spawn step that reads it");
43 let note = handoff.note();
44 for block in restarted {
45 crate::backend::reactive::held_notes::hold(block, NOTE_KEY, ¬e);
46 }
47 true
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53 use crate::backend::continuity_handoff::Handoff;
54 use crate::backend::reactive::held_notes;
55 use std::sync::Arc;
56
57 fn handoff(text: &str) -> Handoff {
58 Handoff { prep_id: "p-1".into(), at_ms: agentmux_common::time::now_ms(), actor: "operator".into(), action: "account_switch".into(), text: text.into() }
59 }
60
61 fn store_installed() {
62 crate::backend::agent_session::set_global_transcript_store(Arc::new(crate::backend::storage::filestore::FileStore::open_in_memory().unwrap()));
63 }
64
65 #[test]
66 fn a_pane_with_no_spawn_step_gets_the_handoff_as_a_held_note_and_it_is_taken() {
67 store_installed();
68 continuity_handoff::store("carry-def-a", &handoff("In flight: PR 7")).unwrap();
69 assert!(hold_where("carry-def-a", &["carry-blk-a1", "carry-blk-a2"], |_| Reader::Note));
70 for block in ["carry-blk-a1", "carry-blk-a2"] {
71 let (message, _held) = held_notes::attach(block, "hello");
72 assert!(message.contains("In flight: PR 7"), "{block}: {message}");
73 }
74 assert!(continuity_handoff::take("carry-def-a").is_none(), "read once");
75 }
76
77 #[test]
78 fn a_pane_whose_spawn_reads_it_is_left_the_handoff() {
79 store_installed();
80 continuity_handoff::store("carry-def-b", &handoff("Waiting on CI")).unwrap();
81 let reader = |b: &str| if b == "carry-blk-b1" { Reader::Spawn } else { Reader::Note };
82 assert!(!hold_where("carry-def-b", &["carry-blk-b1", "carry-blk-b2"], reader));
83 assert_eq!(continuity_handoff::take("carry-def-b").unwrap().text, "Waiting on CI");
84 assert!(!held_notes::attach("carry-blk-b2", "hello").0.contains("Waiting on CI"));
85 }
86
87 #[test]
88 fn nothing_is_held_when_the_agent_left_no_handoff_or_no_pane_restarted() {
89 store_installed();
90 assert!(!hold_where("carry-def-c", &["carry-blk-c"], |_| Reader::Note));
91 continuity_handoff::store("carry-def-d", &handoff("x")).unwrap();
92 assert!(!hold_where("carry-def-d", &[], |_| Reader::Note));
93 assert!(continuity_handoff::take("carry-def-d").is_some(), "left for whoever restarts next");
94 }
95}