Skip to main content

agentmux_srv/server/
browser_host_sync.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The host's copy of srv's browser-pane records: which panes an agent owns
5//! (native-popups spec §3) and the sites each limited pane may go to
6//! (SPEC_BROWSER_PANE_ALLOWED_ORIGINS_2026_10_09.md §6). The host reads both
7//! inside CEF callbacks that can't wait for srv.
8
9use crate::server::AppState;
10
11/// Keep the host's copy of the agent-owned panes current: push the whole set
12/// whenever it changes or the host registers (`browser_owner::changed`), and
13/// every minute regardless, so a push the host missed is made up for.
14pub(crate) fn spawn(state: AppState) {
15    // A deleted pane's browser ownership, waits and limits go with it.
16    super::browser_pane_gone::install();
17    // The owners and site limits the last run kept, before anything can change them.
18    super::browser_pane_records::restore(&state);
19    super::pane_openers::restore(&state);
20    // A session is not restored: the ones the last run left close.
21    super::browser_session_close::close_left_by_the_last_run(&state);
22    tokio::spawn(async move {
23        loop {
24            let _ = tokio::time::timeout(
25                std::time::Duration::from_secs(60),
26                crate::server::browser_owner::changed().notified(),
27            )
28            .await;
29            super::browser_pane_records::save();
30            super::pane_openers::save();
31            super::browser_session_close::close_due(&state).await;
32            super::browser_sessions::sync_strips(&state);
33            push(&state).await;
34        }
35    });
36}
37
38/// Send the host the agent-owned panes, each limited pane's site list, the
39/// panes asking the person about a navigation (allowed-origins spec §6), and
40/// the panes handed to the person, whose page dialogs are theirs to answer
41/// (SPEC_BROWSER_PANE_NATIVE_DIALOGS_2026_10_17.md §4.3).
42/// The sync loop does this on every change; a caller that needs the host to
43/// have it before the next navigation awaits it directly. The host replaces
44/// its copy with each push, so pushes go one at a time, each taking its
45/// snapshot inside the lock: an older one can't arrive after a newer one.
46pub(crate) async fn push(state: &AppState) {
47    static ONE_AT_A_TIME: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
48    let _turn = ONE_AT_A_TIME.lock().await;
49    let Some(host) = state.host_ipc.lock().await.clone() else {
50        return;
51    };
52    let owned = crate::server::browser_owner::owned_panes();
53    let handed = crate::server::browser_attention::asking(crate::server::browser_attention::Kind::Handoff);
54    let mut body = serde_json::json!({
55        "download_workdirs": crate::server::browser_agent_downloads::workdirs(state, &owned, &handed),
56        "panes": owned,
57        "allowed": crate::server::browser_allowlist::snapshot(),
58        "asking": crate::server::browser_attention::asking(crate::server::browser_attention::Kind::Navigation),
59        "unlimited": crate::server::browser_allowlist::confirmed_unlimited(),
60        "handoff": handed,
61    });
62    // Left out when the store can't be read: the host then keeps its jars.
63    if let Some(jars) = crate::server::browser_identity::live_jars(state) {
64        body["jars"] = serde_json::json!(jars);
65    }
66    // The named profiles there are: the host drops the jar and folder of one
67    // deleted. Left out when they can't be read, so it keeps them all.
68    if let Some(ids) = crate::server::browser_identity::profile_ids() {
69        body["profiles"] = serde_json::json!(ids);
70    }
71    if let Err(e) = crate::server::ui_handlers::proxy_to_host_timeout(state, &host, "owned_panes", body, Some(std::time::Duration::from_secs(5))).await {
72        tracing::debug!(error = %e, "[browser-popup] couldn't send the owned panes to the host");
73    }
74    // A zoom of a pane that is no longer the zoomer's: the host drops the unowned ones itself, this the rest.
75    crate::server::browser_zoom::reconcile(state, &host, &handed).await;
76}