Skip to main content

agentmux_srv/server/
browser_same_site.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `Browser open` on a site the agent already has a pane on: the answer says so (the ids), so the agent can use that pane
5//! (`navigate`) instead of opening a second one. Nothing is refused: a second pane is sometimes what is wanted (another
6//! profile, a side-by-side look), so this only tells.
7
8use agentmux_common::popup_rules::origin_of;
9use crate::backend::obj::Block;
10
11/// The panes in `panes` (id and the address the pane was opened or last moved to) whose site is `url`'s: the same scheme, host
12/// and port. Empty for an address that is not http(s).
13pub(crate) fn same_site(panes: &[(String, String)], url: &str) -> Vec<String> {
14    let wanted = origin_of(url);
15    if wanted.is_empty() {
16        return Vec::new();
17    }
18    panes.iter().filter(|(_, at)| origin_of(at) == wanted).map(|(id, _)| id.clone()).collect()
19}
20
21/// The ids of the browser panes `agent` owns that are on `url`'s site now.
22pub(crate) async fn owned_on_site(state: &crate::server::AppState, agent: &str, url: &str) -> Vec<String> {
23    let owned: Vec<String> = crate::server::browser_owner::all_owners().into_iter().filter(|(_, who)| who == agent).map(|(id, _)| id).collect();
24    if owned.is_empty() {
25        return Vec::new();
26    }
27    let panes: Vec<(String, String)> = owned
28        .iter()
29        .filter_map(|id| {
30            let block = state.mstore.get::<Block>(id).ok().flatten()?;
31            let at = block.meta.get("url").and_then(|v| v.as_str())?.to_string();
32            Some((id.clone(), at))
33        })
34        .collect();
35    same_site(&panes, url)
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    fn panes() -> Vec<(String, String)> {
43        vec![
44            ("b1".into(), "https://app.example.com/inbox".into()),
45            ("b2".into(), "https://app.example.com:8443/inbox".into()),
46            ("b3".into(), "http://app.example.com/".into()),
47            ("b4".into(), "https://other.example.org/".into()),
48        ]
49    }
50
51    #[test]
52    fn a_pane_on_the_same_scheme_host_and_port_is_the_same_site() {
53        assert_eq!(same_site(&panes(), "https://app.example.com/settings?tab=1"), vec!["b1".to_string()]);
54    }
55
56    #[test]
57    fn another_port_scheme_or_host_is_another_site() {
58        assert_eq!(same_site(&panes(), "https://app.example.com:8443/x"), vec!["b2".to_string()]);
59        assert_eq!(same_site(&panes(), "http://app.example.com/x"), vec!["b3".to_string()]);
60        assert!(same_site(&panes(), "https://elsewhere.example.net/").is_empty());
61    }
62
63    #[test]
64    fn an_address_that_is_not_a_web_address_matches_nothing() {
65        assert!(same_site(&panes(), "file:///etc/hosts").is_empty());
66        assert!(same_site(&panes(), "").is_empty());
67        assert!(same_site(&[("b9".into(), "about:blank".into())], "https://app.example.com/").is_empty());
68    }
69}