Skip to main content

agentmux_srv_core/backend/container/mounts/
writable.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The host folders a container agent can write: what its pane binds writable into its container, each with
5//! the place it has there. Read from the pane's own settings, the values its container is made from, so it
6//! holds before the container exists and while the runtime is down.
7
8use std::path::{Path, PathBuf};
9
10use crate::backend::blockcontroller::META_KEY_CMD_CWD;
11use crate::backend::obj::{meta_get_string, MetaMapType};
12
13/// One host folder bound writable into a container, and where it is there.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct WritableBind {
16    pub host: PathBuf,
17    /// The absolute path of the folder inside the container.
18    pub target: String,
19}
20
21/// The folders the pane `meta` binds writable into its agent's container: its workspace, `memory` (the host
22/// folder of the agent's Personal Memory, when it has one) and every volume of its list that is a host
23/// folder not bound read-only. Nothing for a pane whose agent does not run in a container.
24pub fn writable_binds(meta: &MetaMapType, memory: Option<&Path>) -> Vec<WritableBind> {
25    if !crate::backend::runtime_adapter::RuntimeKind::of_block_meta(meta).is_container() {
26        return Vec::new();
27    }
28    let mut binds = Vec::new();
29    let workspace = meta_get_string(meta, META_KEY_CMD_CWD, "");
30    if !workspace.trim().is_empty() {
31        binds.push(WritableBind {
32            host: crate::backend::base::expand_home_dir_safe(workspace.trim()),
33            target: super::CONTAINER_WORKSPACE_DIR.to_string(),
34        });
35    }
36    if let Some(memory) = memory {
37        binds.push(WritableBind { host: memory.to_path_buf(), target: super::container_memory_dir() });
38    }
39    let volumes: Vec<String> = serde_json::from_str(&meta_get_string(meta, "agent:container_volumes", "[]")).unwrap_or_default();
40    for spec in &volumes {
41        let Some((source, target, read_only)) = super::super::parse_volume_spec(spec) else { continue };
42        if read_only || target.is_empty() || !is_host_path(source) {
43            continue;
44        }
45        binds.push(WritableBind { host: crate::backend::base::expand_home_dir_safe(source), target: target.to_string() });
46    }
47    binds
48}
49
50/// Whether a volume's source names a host folder (a bind) and not a named volume: the rule a new
51/// container's mounts are made by.
52fn is_host_path(source: &str) -> bool {
53    source.starts_with('/') || source.starts_with('~') || (source.len() >= 2 && source.as_bytes()[1] == b':')
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    fn pane(mode: &str, cwd: &str, volumes: &str) -> MetaMapType {
61        let mut meta = MetaMapType::new();
62        meta.insert("agentMode".to_string(), serde_json::json!(mode));
63        meta.insert(META_KEY_CMD_CWD.to_string(), serde_json::json!(cwd));
64        meta.insert("agent:container_volumes".to_string(), serde_json::json!(volumes));
65        meta
66    }
67
68    #[test]
69    fn a_container_agents_pane_binds_its_workspace_its_memory_and_its_writable_host_volumes() {
70        let volumes = r#"["/data/shared:/data","/data/docs:/docs:ro","cache:/cache","C:\\repos\\x:/x","broken"]"#;
71        let binds = writable_binds(&pane("container", "/w/aria", volumes), Some(Path::new("/claude/projects/-w-aria/memory")));
72        let got: Vec<(String, &str)> = binds.iter().map(|b| (b.host.to_string_lossy().replace('\\', "/"), b.target.as_str())).collect();
73        assert_eq!(
74            got,
75            vec![
76                ("/w/aria".to_string(), "/workspace"),
77                ("/claude/projects/-w-aria/memory".to_string(), "/home/agent/.claude/projects/-workspace/memory"),
78                ("/data/shared".to_string(), "/data"),
79                ("C:/repos/x".to_string(), "/x"),
80            ],
81            "a read-only volume, a named volume and a spec with no target are not folders the agent can write"
82        );
83    }
84
85    #[test]
86    fn a_pane_whose_agent_runs_on_the_host_binds_nothing() {
87        assert!(writable_binds(&pane("host", "/w/host", r#"["/data:/data"]"#), Some(Path::new("/m"))).is_empty());
88        assert!(writable_binds(&pane("container", " ", "[]"), None).is_empty(), "no folder is bound");
89    }
90}