Skip to main content

agentmux_srv/server/
browser_agent_downloads.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Where a download in an agent's own browser pane may be saved without a dialog
5//! (docs/specs/SPEC_BROWSER_PANE_DOWNLOADS_2026_10_14.md ยง7.5, D3b). The host decides inside a CEF callback
6//! that can't wait for srv, so srv pushes it each pane's workspace (`browser_host_sync`), only for a pane an
7//! agent owns and that the person hasn't taken over or been handed, and that does not browse as the
8//! person (a pane in the Personal profile keeps the dialog: it has the person's sign-ins, and what it
9//! downloads is the person's to place). The host adds `downloads/` itself and checks the folder before
10//! writing. Each download, saved or refused, is a row in the Audit view (`browser_dialog`).
11
12use std::collections::HashMap;
13
14use serde_json::{json, Value};
15
16use crate::server::AppState;
17
18/// The setting that turns it off: the person is asked in the dialog as before. On unless false.
19pub(crate) const SETTING: &str = "browser:downloadagentsave";
20
21pub(crate) fn enabled(extra: &HashMap<String, Value>) -> bool {
22    extra.get(SETTING).and_then(|v| v.as_bool()).unwrap_or(true)
23}
24
25/// The panes that may save into a workspace: owned by an agent that has one, and not handed to the person.
26/// `owners` is (pane, workspace), the workspace empty when the agent has none on record.
27fn workdirs_for(owners: &[(String, String)], handed: &[String], on: bool) -> serde_json::Map<String, Value> {
28    let mut out = serde_json::Map::new();
29    if !on {
30        return out;
31    }
32    for (pane, workdir) in owners {
33        if workdir.is_empty() || handed.contains(pane) {
34            continue;
35        }
36        out.insert(pane.clone(), json!(workdir));
37    }
38    out
39}
40
41/// Whether the pane with `meta` browses as the person (no identity of its own): the Personal profile.
42fn browses_as_the_person(meta: &crate::backend::obj::MetaMapType) -> bool {
43    crate::server::browser_identity::browsing_as(meta) == "personal"
44}
45
46/// What the host is sent: pane -> its owner's workspace on this computer (a container agent's `/workspace` is
47/// bound from the same folder).
48pub(crate) fn workdirs(state: &AppState, owned: &[String], handed: &[String]) -> Value {
49    let on = enabled(&state.config_watcher.get_full_config().settings.extra);
50    // A pane we can't read, or one in the Personal profile, is left out like a handed pane: the dialog stays.
51    let handed: Vec<String> = handed
52        .iter()
53        .cloned()
54        .chain(owned.iter().filter(|pane| {
55            state.mstore.get::<crate::backend::obj::Block>(pane).ok().flatten().map_or(true, |b| browses_as_the_person(&b.meta))
56        }).cloned())
57        .collect();
58    let owners: Vec<(String, String)> = owned
59        .iter()
60        .map(|pane| {
61            let workdir = crate::server::browser_owner::owner_of(pane)
62                .and_then(|agent| crate::backend::agent_resolve::resolve_agent_id(&state.mstore, &agent).ok())
63                .and_then(|id| state.mstore.agent_def_get(&id).ok().flatten())
64                .map(|def| crate::backend::base::expand_home_dir_safe(&def.working_directory).to_string_lossy().to_string())
65                .filter(|dir| !dir.is_empty())
66                .unwrap_or_default();
67            (pane.clone(), workdir)
68        })
69        .collect();
70    Value::Object(workdirs_for(&owners, &handed, on))
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    fn owners() -> Vec<(String, String)> {
78        vec![
79            ("p1".into(), "/w/a".into()),
80            ("p2".into(), "/w/b".into()),
81            ("p3".into(), String::new()),
82        ]
83    }
84
85    #[test]
86    fn only_a_pane_with_a_workspace_that_is_not_handed_over_may_save_without_asking() {
87        let m = workdirs_for(&owners(), &["p2".to_string()], true);
88        assert_eq!(m.len(), 1);
89        assert_eq!(m["p1"], "/w/a");
90    }
91
92    #[test]
93    fn a_pane_in_the_personal_profile_is_the_person_s_and_a_pane_with_its_own_jar_is_not() {
94        let meta = |identity: Option<&str>| {
95            let mut m = crate::backend::obj::MetaMapType::new();
96            if let Some(i) = identity {
97                m.insert(crate::server::browser_identity::IDENTITY_META_KEY.to_string(), json!(i));
98            }
99            m
100        };
101        assert!(browses_as_the_person(&meta(None)));
102        assert!(!browses_as_the_person(&meta(Some("incognito:0123456789abcdef"))));
103        assert!(!browses_as_the_person(&meta(Some("profile:agent-posa"))));
104    }
105
106    #[test]
107    fn the_setting_turns_it_off_and_is_on_by_default() {
108        assert!(workdirs_for(&owners(), &[], false).is_empty());
109        let mut extra = HashMap::new();
110        assert!(enabled(&extra));
111        extra.insert(SETTING.to_string(), json!(false));
112        assert!(!enabled(&extra));
113        extra.insert(SETTING.to_string(), json!(true));
114        assert!(enabled(&extra));
115    }
116}