Skip to main content

agentmux_srv/server/prep/
carry.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Where the handoff an agent left goes once its session has restarted (spec ยง3.4). A persistent
5//! Claude pane reads it in its own spawn (`carry_prep_handoff`), as the first section of the packet
6//! or a held note. A pane of any other controller (a subprocess, ACP or app-server agent) has no
7//! such step, so the action that restarted it hands the handoff over here, as a note held for the
8//! pane's next message.
9
10use crate::backend::blockcontroller::BLOCK_CONTROLLER_PERSISTENT;
11use crate::backend::continuity_handoff::{self, NOTE_KEY};
12
13/// What reads a restarted pane's handoff.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub(super) enum Reader {
16    /// Its own spawn does (persistent Claude, or a pane that is not up yet and will take it when it is).
17    Spawn,
18    /// Nothing does: it is held as a note for the pane's next message.
19    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
29/// After the agent `def_id`'s panes `restarted` came up again: hold its handoff as a note for each
30/// pane whose controller has no spawn step that reads it. Left where it is (for the spawn to read)
31/// when any pane of the agent is read by its spawn. `true` when it was held.
32pub 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, &note);
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}