Skip to main content

agentmux_srv/server/app_api/
agent_tab_default.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Where a pane an agent opens goes when the agent does not say: a tab of its own pane group.
5//!
6//! An agent that opens a pane (an Editor, Files or widget pane) and names no placement gets a new tab in the pane group
7//! of the pane it opened last, or in its own pane's group when it has none open there. A Browser is different: the first
8//! one an agent drives gets a pane of its own (a split below the agent's pane), and each later one is a new tab of the
9//! newest browser pane the agent opened that is still open in its own window tab. An agent gets a split only when it
10//! asks for one (`split`), a floating window only when it asks for one (`floating`), and a tab of the person's other
11//! panes, of another agent's browser or of another window tab never: the group is looked for in the agent's own tab
12//! only, among panes the agent opened.
13
14use crate::backend::obj::{Block, LayoutState, Tab};
15use crate::backend::storage::store::Store;
16use crate::backend::rpc_types::CommandPaneOpenData;
17use crate::server::AppState;
18
19/// Whether `cmd` says where the pane goes: a split, a floating window, a pane to stack onto, no placement at all, or a
20/// tab of its own.
21fn names_where(cmd: &CommandPaneOpenData) -> bool {
22    cmd.floating == Some(true)
23        || cmd.skip_placement == Some(true)
24        || cmd.stack_onto_block_id.as_deref().is_some_and(|b| !b.is_empty())
25        || cmd.split_direction.as_deref().is_some_and(|d| !d.is_empty())
26        || cmd.tab_id.as_deref().is_some_and(|t| !t.is_empty())
27        || super::floater_place::names_placement(cmd)
28}
29
30/// The panes the layout of tab `tab_id` holds: a block made with no placement is in the tab but in no pane.
31fn laid_out(mstore: &Store, tab_id: &str) -> Vec<String> {
32    let layout = mstore.get::<Tab>(tab_id).ok().flatten().and_then(|tab| mstore.get::<LayoutState>(&tab.layoutstate).ok().flatten());
33    layout.and_then(|l| l.rootnode).map(|root| super::member_block_ids(&root)).unwrap_or_default()
34}
35
36/// Where an agent's first browser goes: below the agent's own pane, which gives up half of its space (`down:half`, see
37/// `resolve_placement`).
38pub(crate) const BROWSER_FIRST_SPLIT: &str = "down:half";
39
40/// Whether `block_id` is a browser pane.
41fn is_browser(mstore: &Store, block_id: &str) -> bool {
42    mstore.get::<Block>(block_id).ok().flatten().is_some_and(|b| crate::backend::obj::meta_get_string(&b.meta, "view", "") == "browser")
43}
44
45/// The browser pane `agent`'s next browser stacks onto: the newest browser pane it opened that is still in a pane of
46/// the tab of its own pane `own`. `None` for the agent's first browser, and after its browsers were closed or moved
47/// to another tab: the next one is a first again. `opened` is the agent's docked panes, newest first.
48pub(crate) fn browser_target(mstore: &Store, opened: &[String], own: &str) -> Option<String> {
49    let own_tab = super::resolve_tab_id_for_block(mstore, own).ok()?;
50    let held = laid_out(mstore, &own_tab);
51    opened.iter().find(|pane| held.contains(pane) && is_browser(mstore, pane)).cloned()
52}
53
54/// The pane `agent` stacks its next pane onto: the newest pane it opened that is still there, docked, in a pane of the
55/// tab of its own pane `own`; else `own`. `opened` is the agent's docked panes, newest first.
56pub(crate) fn target_for(mstore: &Store, opened: &[String], own: &str) -> Option<String> {
57    let own_tab = super::resolve_tab_id_for_block(mstore, own).ok()?;
58    let held = laid_out(mstore, &own_tab);
59    let earlier = opened.iter().find(|pane| held.contains(pane));
60    Some(earlier.map_or_else(|| own.to_string(), Clone::clone))
61}
62
63/// Make `cmd` open as a tab of the agent's own pane group when it names no place, for an agent's open (`by_agent`; a
64/// person's own shell keeps its splits). `agent` is the opener, known by its
65/// signed identity (none for a container's own route, which has no history to look at); the agent's own pane is the one
66/// the request names as its reference.
67pub(crate) fn apply(state: &AppState, by_agent: bool, agent: Option<&str>, cmd: &mut CommandPaneOpenData) {
68    if !by_agent || names_where(cmd) {
69        return;
70    }
71    let Some(own) = cmd.split_reference_block_id.clone().filter(|b| !b.is_empty()) else { return };
72    let opened = agent.map(crate::server::pane_openers::docked_of).unwrap_or_default();
73    if cmd.view == "browser" {
74        match browser_target(&state.mstore, &opened, &own) {
75            // Shown in its pane, so the person sees what the agent drives: switching the tab of a pane of the agent's
76            // own browsers takes nobody's focus or caret.
77            Some(browser) => (cmd.stack_onto_block_id, cmd.focus) = (Some(browser), Some(true)),
78            None => cmd.split_direction = Some(BROWSER_FIRST_SPLIT.to_string()),
79        }
80        return;
81    }
82    if let Some(target) = target_for(&state.mstore, &opened, &own) {
83        cmd.stack_onto_block_id = Some(target);
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90    use serde_json::json;
91
92    fn cmd(v: serde_json::Value) -> CommandPaneOpenData {
93        serde_json::from_value(v).unwrap()
94    }
95
96    #[test]
97    fn a_request_that_says_where_is_left_as_it_is() {
98        for said in [
99            json!({ "view": "editor", "split_direction": "right" }),
100            json!({ "view": "editor", "floating": true }),
101            json!({ "view": "editor", "skip_placement": true }),
102            json!({ "view": "editor", "stack_onto_block_id": "b9" }),
103            json!({ "view": "editor", "tab_id": "t1" }),
104            json!({ "view": "editor", "floating": true, "width": 800 }),
105        ] {
106            assert!(names_where(&cmd(said.clone())), "{said}");
107        }
108        for open in [json!({ "view": "editor" }), json!({ "view": "browser", "split_reference_block_id": "own" }), json!({ "view": "editor", "stack_onto_block_id": "" })] {
109            assert!(!names_where(&cmd(open.clone())), "{open}");
110        }
111    }
112}