Skip to main content

agentmux_srv_core/backend/spawn_env/
workspaces.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Which folder each agent on a block was started in, so a browser upload can be
5//! checked against it (`server/browser_uploads.rs`) and the spawn paths can record
6//! it without reaching up into the server.
7
8use std::collections::HashMap;
9use std::path::PathBuf;
10use std::sync::{Mutex, OnceLock};
11
12fn launch_workspaces() -> &'static Mutex<HashMap<String, PathBuf>> {
13    static W: OnceLock<Mutex<HashMap<String, PathBuf>>> = OnceLock::new();
14    W.get_or_init(Default::default)
15}
16
17/// Remember the workspace the agent on `block_id` was started in: the
18/// `AGENTMUX_AGENT_WORKDIR` srv just put in its spawn env (host agents only;
19/// a container agent has none). Called by every spawn path right after
20/// `spawn_env::block_identity::carry_agent_workdir_env`.
21///
22/// Uploads are checked against this record, not the block's `cmd:cwd`: any
23/// client with the instance auth key can rewrite that meta, so an agent
24/// could otherwise point its "workspace" at `/` and upload anything.
25pub fn record_launch_workspace(block_id: &str, env_vars: &HashMap<String, String>) {
26    // A block is one or the other: a host launch ends any container record.
27    container_workspaces().lock().unwrap_or_else(|p| p.into_inner()).remove(block_id);
28    let mut map = launch_workspaces().lock().unwrap_or_else(|p| p.into_inner());
29    match env_vars.get("AGENTMUX_AGENT_WORKDIR").filter(|w| !w.trim().is_empty()) {
30        Some(w) => {
31            map.insert(block_id.to_string(), PathBuf::from(w));
32        }
33        None => {
34            map.remove(block_id);
35        }
36    }
37}
38
39/// The workspace the agent on `block_id` was launched in, if srv started it
40/// as a host agent.
41pub fn agent_workspace(block_id: &str) -> Option<PathBuf> {
42    launch_workspaces().lock().unwrap_or_else(|p| p.into_inner()).get(block_id).cloned()
43}
44
45fn container_workspaces() -> &'static Mutex<HashMap<String, PathBuf>> {
46    static W: OnceLock<Mutex<HashMap<String, PathBuf>>> = OnceLock::new();
47    W.get_or_init(Default::default)
48}
49
50/// Remember the host folder mounted at `/workspace` in the container of the
51/// agent on `block_id`, as srv started it: the mount's source, not the block's
52/// `cmd:cwd`, which any client can rewrite after the launch. Called where the
53/// container's mounts are decided.
54pub fn record_container_workspace(block_id: &str, host_dir: Option<&str>) {
55    launch_workspaces().lock().unwrap_or_else(|p| p.into_inner()).remove(block_id);
56    let mut map = container_workspaces().lock().unwrap_or_else(|p| p.into_inner());
57    match host_dir.map(str::trim).filter(|d| !d.is_empty()) {
58        Some(d) => {
59            map.insert(block_id.to_string(), PathBuf::from(d));
60        }
61        None => {
62            map.remove(block_id);
63        }
64    }
65}
66
67/// The host folder behind `/workspace` for the container agent on `block_id`.
68pub fn container_workspace(block_id: &str) -> Option<PathBuf> {
69    container_workspaces().lock().unwrap_or_else(|p| p.into_inner()).get(block_id).cloned()
70}