Skip to main content

agentmux_srv/server/
pane_pin.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! An agent's `pin` and `unpin` of a pane (docs/specs/SPEC_PIN_TRANSFORM_FACILITY_2026_12_02.md ยง9): the pane stays on
5//! screen in every tab of its window, at the place it has in its own tab, or stops doing so. Both run what the
6//! person's thumbtack runs (`service::pin`), so a refusal (the window has its one pin, the pane is alone in its tab)
7//! reads the same for both. Only the agent's own pane or a pane it opened; a floating pane is its own window and is
8//! not pinnable. Nothing here runs or reads on the host, so a container agent may call it.
9
10use agentmux_common::api_types::PanePlaceRequest;
11
12use super::*;
13
14fn refuse(status: StatusCode, text: impl Into<String>) -> Response {
15    (status, Json(json!({ "error": text.into() }))).into_response()
16}
17
18/// Whether `agent` may pin or unpin `target`: its own pane, or one it opened.
19fn may_touch(state: &AppState, agent: &str, target: &str) -> bool {
20    let own = super::app_api::agent_block::find_agent_block_by_uid(&state.mstore, agent).ok().flatten().map(|o| o.block.oid);
21    own.as_deref() == Some(target) || super::pane_openers::opened_by(target, agent)
22}
23
24/// The pane's workspace and its tab.
25async fn home(state: &AppState, target: &str) -> Option<(String, String)> {
26    let s = state.srv_state.lock().await;
27    let tab = s.blocks.get(target)?.tab_id.clone();
28    let ws = s.tabs.get(&tab)?.workspace_id.clone();
29    Some((ws, tab))
30}
31
32/// Whether `target` is pinned in `ws`, as the answer says it: with the tab it belongs to and the place it has there
33/// (fractions of the tab's area), which is where every tab shows it.
34async fn record_of(state: &AppState, ws: &str, target: &str) -> serde_json::Value {
35    let s = state.srv_state.lock().await;
36    if super::service::pin::pin_holding(&s, ws, target).is_none() {
37        return json!({ "pane": target, "pinned": false });
38    }
39    let tab = s.blocks.get(target).map(|b| b.tab_id.clone()).unwrap_or_default();
40    let rect = super::service::pin::pane_rect(&s, target).map(|r| json!({ "left": r.left, "top": r.top, "width": r.width, "height": r.height }));
41    json!({ "pane": target, "pinned": true, "tab_id": tab, "rect": rect })
42}
43
44/// Why `agent` may not touch `target`, or nothing.
45fn refusal(state: &AppState, agent: &str, target: &str) -> Option<Response> {
46    if !may_touch(state, agent, target) {
47        return Some(refuse(StatusCode::FORBIDDEN, "NOT_YOURS: you can pin or unpin only your own pane and panes you opened; ask the person to pin others"));
48    }
49    if super::pane_openers::is_floating(target) {
50        return Some(refuse(StatusCode::CONFLICT, format!("FLOATING: {target} is a floating window, which has its own place on screen: Pane dock it into a tab first")));
51    }
52    if super::pane_openers::is_background(target) {
53        return Some(refuse(StatusCode::CONFLICT, format!("BACKGROUND_SESSION: {target} is a background browser session no screen shows: Browser show makes it a pane first")));
54    }
55    None
56}
57
58/// `pin`: the pane stays where it is, and shows there in every tab of its window.
59pub(super) async fn pin(state: &AppState, agent: &str, req: &PanePlaceRequest) -> Response {
60    let target = req.target_id.as_str();
61    if let Some(r) = refusal(state, agent, target) {
62        return r;
63    }
64    let Some((ws, _tab)) = home(state, target).await else {
65        return refuse(StatusCode::NOT_FOUND, format!("that pane is gone: {target} is no longer in a tab"));
66    };
67    let pinned = super::service::pin::pin_holding(&*state.srv_state.lock().await, &ws, target).is_some();
68    if !pinned {
69        if let Err(e) = super::service::pin::pin_block(state, &ws, target).await {
70            return refuse(StatusCode::CONFLICT, format!("NOT_PINNED: {e}"));
71        }
72    }
73    (StatusCode::OK, Json(record_of(state, &ws, target).await)).into_response()
74}
75
76/// `unpin`: the pane becomes an ordinary pane of the tab that is showing (it moves there when that is not its own tab).
77pub(super) async fn unpin(state: &AppState, agent: &str, req: &PanePlaceRequest) -> Response {
78    let target = req.target_id.as_str();
79    if let Some(r) = refusal(state, agent, target) {
80        return r;
81    }
82    let Some((ws, tab)) = home(state, target).await else {
83        return refuse(StatusCode::NOT_FOUND, format!("that pane is gone: {target} is no longer in a tab"));
84    };
85    let (pinned, showing) = {
86        let s = state.srv_state.lock().await;
87        (super::service::pin::pin_holding(&s, &ws, target).is_some(), s.workspaces.get(&ws).and_then(|w| w.active_tab_id.clone()))
88    };
89    if !pinned {
90        return refuse(StatusCode::CONFLICT, format!("NOT_PINNED: {target} is not pinned"));
91    }
92    let dst = showing.filter(|t| !t.is_empty()).unwrap_or(tab);
93    if let Err(e) = super::service::pin::unpin_block(state, &ws, target, &dst).await {
94        return refuse(StatusCode::CONFLICT, format!("NOT_UNPINNED: {e}"));
95    }
96    (StatusCode::OK, Json(json!({ "pane": target, "pinned": false, "tab_id": dst }))).into_response()
97}
98
99#[cfg(test)]
100#[path = "pane_pin_tests.rs"]
101mod tests;