Skip to main content

agentmux_srv_core/backend/
open_here_mark.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A pane bound with "Open here" and the refusal it is expected to get.
5//!
6//! The agent picker's Open here binds a fresh pane to an agent that is open in another pane. The
7//! single-live-instance guard refuses that second pane (`open_in_other_pane`), and the pane answers
8//! the refusal at once with Move here. The refusal is a step of the move, not news: written into the
9//! pane's history it would stay there as a red line above the conversation after the agent has moved.
10//! The window marks the pane (`agent:open_here`) before it binds; the refusal still reaches the pane's
11//! recovery row, only the line in the history is left out.
12
13use crate::backend::obj::{meta_get_bool, MetaMapType};
14use crate::backend::storage::store::Store;
15
16/// Block meta the window sets on a pane bound with Open here. srv clears it once the agent has moved.
17pub const META_OPEN_HERE: &str = "agent:open_here";
18
19/// Whether `message`, a refusal about to be written into the pane's history, is the one an Open here
20/// pane expects: the agent is open in another pane, and this pane is marked.
21pub fn is_expected_open_here_refusal(meta: &MetaMapType, message: &str) -> bool {
22    meta_get_bool(meta, META_OPEN_HERE, false) && crate::backend::blockcontroller::persistent::is_held_elsewhere_error(message)
23}
24
25/// [`is_expected_open_here_refusal`] for the block `block_id` as stored.
26pub fn expected_for_block(store: &Store, block_id: &str, message: &str) -> bool {
27    store
28        .must_get::<crate::backend::obj::Block>(block_id)
29        .is_ok_and(|block| is_expected_open_here_refusal(&block.meta, message))
30}
31
32/// The agent has moved into this pane. Drop the mark, so every refusal after this one is news again,
33/// and the "open in another pane" failure that asked for the move: it is no longer true, and left in
34/// the pane's meta it brings the recovery row back when the pane reloads.
35pub fn settle_after_move(block_id: &str, store: &std::sync::Arc<Store>, event_bus: &Option<std::sync::Arc<crate::backend::eventbus::EventBus>>) {
36    let written = store.with_tx(|tx| {
37        let mut block = tx.must_get::<crate::backend::obj::Block>(block_id)?;
38        let mut update = MetaMapType::new();
39        if block.meta.contains_key(META_OPEN_HERE) {
40            update.insert(META_OPEN_HERE.to_string(), serde_json::Value::Null);
41        }
42        let refused = block
43            .meta
44            .get(crate::backend::blockcontroller::core::META_LAST_FAILURE)
45            .and_then(|f| f.get("code"))
46            .and_then(|c| c.as_str())
47            == Some("open_in_other_pane");
48        if refused {
49            update.insert(crate::backend::blockcontroller::core::META_LAST_FAILURE.to_string(), serde_json::Value::Null);
50        }
51        if update.is_empty() {
52            return Ok(false);
53        }
54        block.meta = crate::backend::obj::merge_meta(&block.meta, &update, true);
55        tx.update(&mut block)?;
56        Ok(true)
57    });
58    match written {
59        Ok(true) => {
60            if let Some(bus) = event_bus {
61                crate::backend::blockcontroller::core::broadcast_block_update(store, bus, block_id);
62            }
63        }
64        Ok(false) => {}
65        Err(e) => tracing::warn!(block_id = %block_id, error = %e, "could not settle the pane after the move"),
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72    use crate::backend::blockcontroller::persistent::held_elsewhere_error;
73    use serde_json::json;
74
75    fn meta(pairs: &[(&str, serde_json::Value)]) -> MetaMapType {
76        pairs.iter().map(|(k, v)| (k.to_string(), v.clone())).collect()
77    }
78
79    #[test]
80    fn a_marked_pane_expects_the_refusal_for_an_agent_open_elsewhere() {
81        let m = meta(&[(META_OPEN_HERE, json!(true))]);
82        assert!(is_expected_open_here_refusal(&m, &held_elsewhere_error("blk-1", false)));
83    }
84
85    #[test]
86    fn an_unmarked_pane_keeps_the_refusal_in_its_history() {
87        assert!(!is_expected_open_here_refusal(&meta(&[]), &held_elsewhere_error("blk-1", false)));
88        assert!(!is_expected_open_here_refusal(&meta(&[(META_OPEN_HERE, json!(false))]), &held_elsewhere_error("blk-1", false)));
89    }
90
91    #[test]
92    fn a_marked_pane_keeps_every_other_refusal() {
93        let m = meta(&[(META_OPEN_HERE, json!(true))]);
94        assert!(!is_expected_open_here_refusal(&m, "Attachments aren't supported for container agents yet."));
95        assert!(!is_expected_open_here_refusal(&m, "Agent was taken over by another AgentMux instance on this host."));
96    }
97}