Skip to main content

agentmux_srv_core/backend/lsp/
workspace.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Workspace-root detection for LSP. Walks up from a file's directory
5// looking for project-marker files (`.git`, `Cargo.toml`, `package.json`,
6// `go.mod`, `pyproject.toml`). Falls back to the file's parent dir if
7// none found.
8
9use std::path::{Path, PathBuf};
10
11use crate::backend::fs_ops::container_folders::Place;
12
13/// Whether the language server for `file`, rooted at `root`, runs on the host: only when no container agent
14/// can write the file or the root. `place` answers for each, as it is written and as it resolves (links
15/// followed). A server on the host is for the person's own folders; where a folder's owner can't be told,
16/// none is started either.
17pub fn runs_on_host(file: &Path, root: &Path, place: &dyn Fn(&str) -> Place) -> bool {
18    [file, root].into_iter().all(|path| matches!(place(&path.to_string_lossy()), Place::Host))
19}
20
21/// Whether a language server for `file`, rooted at `root`, is started on the host: [`runs_on_host`] says so
22/// of the file and the root, and no folder a container agent can write lies beneath the root (a server
23/// works with everything beneath its root, not only the file). Reads the disk and the store.
24pub fn served_on_host(file: &Path, root: &Path) -> bool {
25    use crate::backend::fs_ops::container_folders::{holds_a_container_agents_folder, place};
26    runs_on_host(file, root, &place) && !holds_a_container_agents_folder(&root.to_string_lossy())
27}
28
29/// Project-marker files we treat as a workspace root.
30/// Order doesn't matter — we return the *closest* ancestor that has any
31/// of these (walking up from the file's dir).
32const MARKERS: &[&str] = &[
33    ".git",
34    "Cargo.toml",
35    "package.json",
36    "go.mod",
37    "pyproject.toml",
38    "deno.json",
39    "deno.jsonc",
40    "tsconfig.json",
41];
42
43pub fn detect_workspace_root(file_path: &Path) -> PathBuf {
44    let mut current = file_path.parent();
45    while let Some(dir) = current {
46        for marker in MARKERS {
47            if dir.join(marker).exists() {
48                return dir.to_path_buf();
49            }
50        }
51        current = dir.parent();
52    }
53    // Fall back to the file's parent (or "." if even that's missing).
54    file_path
55        .parent()
56        .unwrap_or_else(|| Path::new("."))
57        .to_path_buf()
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use std::fs;
64
65    #[test]
66    fn falls_back_to_parent_dir_when_no_marker() {
67        let tmp = tempfile::tempdir().unwrap();
68        let file = tmp.path().join("loose.txt");
69        fs::write(&file, "").unwrap();
70        // No markers anywhere — root is the temp dir
71        assert_eq!(detect_workspace_root(&file), tmp.path());
72    }
73
74    #[test]
75    fn finds_git_root_up_the_tree() {
76        let tmp = tempfile::tempdir().unwrap();
77        let root = tmp.path();
78        fs::create_dir(root.join(".git")).unwrap();
79        let sub = root.join("a/b/c");
80        fs::create_dir_all(&sub).unwrap();
81        let file = sub.join("file.rs");
82        fs::write(&file, "").unwrap();
83        assert_eq!(detect_workspace_root(&file), root);
84    }
85
86    #[test]
87    fn finds_cargo_root() {
88        let tmp = tempfile::tempdir().unwrap();
89        let root = tmp.path();
90        fs::write(root.join("Cargo.toml"), "").unwrap();
91        let sub = root.join("src");
92        fs::create_dir(&sub).unwrap();
93        let file = sub.join("main.rs");
94        fs::write(&file, "").unwrap();
95        assert_eq!(detect_workspace_root(&file), root);
96    }
97
98    #[test]
99    fn closest_ancestor_wins_when_nested_markers() {
100        // Outer Cargo.toml, inner package.json — file inside inner should
101        // resolve to inner.
102        let tmp = tempfile::tempdir().unwrap();
103        let outer = tmp.path();
104        fs::write(outer.join("Cargo.toml"), "").unwrap();
105        let inner = outer.join("frontend");
106        fs::create_dir(&inner).unwrap();
107        fs::write(inner.join("package.json"), "").unwrap();
108        let file = inner.join("src/index.ts");
109        fs::create_dir(inner.join("src")).unwrap();
110        fs::write(&file, "").unwrap();
111        assert_eq!(detect_workspace_root(&file), inner);
112    }
113
114    use crate::backend::fs_ops::container_folders::{self, place};
115
116    /// A temp folder holding `ws`, counted as bound writable into a container, and `own`, the person's.
117    /// Each holds a project (`package.json`) with one source file.
118    fn an_agents_folder_and_the_persons() -> (tempfile::TempDir, PathBuf, PathBuf) {
119        let tmp = tempfile::tempdir().unwrap();
120        let (ws, own) = (tmp.path().join("ws"), tmp.path().join("own"));
121        for project in [ws.join("proj"), own.join("proj")] {
122            fs::create_dir_all(project.join("src")).unwrap();
123            fs::write(project.join("package.json"), "{}").unwrap();
124            fs::write(project.join("src").join("a.ts"), "").unwrap();
125        }
126        container_folders::add_for_test(&ws, &format!("lsp-{}", uuid::Uuid::new_v4().simple()), "/workspace");
127        (tmp, ws, own)
128    }
129
130    fn link_dir(to: &Path, at: &Path) -> bool {
131        #[cfg(unix)]
132        return std::os::unix::fs::symlink(to, at).is_ok();
133        #[cfg(windows)]
134        return std::os::windows::fs::symlink_dir(to, at).is_ok();
135    }
136
137    fn link_file(to: &Path, at: &Path) -> bool {
138        #[cfg(unix)]
139        return std::os::unix::fs::symlink(to, at).is_ok();
140        #[cfg(windows)]
141        return std::os::windows::fs::symlink_file(to, at).is_ok();
142    }
143
144    #[test]
145    fn a_language_server_runs_on_the_host_for_a_folder_no_container_agent_can_write() {
146        let (_tmp, _ws, own) = an_agents_folder_and_the_persons();
147        let file = own.join("proj").join("src").join("a.ts");
148        let root = detect_workspace_root(&file);
149        assert_eq!(root, own.join("proj"));
150        assert!(runs_on_host(&file, &root, &place));
151    }
152
153    #[test]
154    fn no_language_server_runs_on_the_host_for_a_file_in_a_container_agents_folder() {
155        let (_tmp, ws, _own) = an_agents_folder_and_the_persons();
156        let file = ws.join("proj").join("src").join("a.ts");
157        let root = detect_workspace_root(&file);
158        assert_eq!(root, ws.join("proj"));
159        assert!(!runs_on_host(&file, &root, &place));
160        // The folder itself, and a file that is not there yet, are the agent's too.
161        assert!(!runs_on_host(&ws, &ws, &place));
162        assert!(!runs_on_host(&ws.join("new.ts"), &ws, &place));
163    }
164
165    #[test]
166    fn no_language_server_runs_on_the_host_while_a_folders_owner_cannot_be_told() {
167        let (_tmp, _ws, own) = an_agents_folder_and_the_persons();
168        let file = own.join("proj").join("src").join("a.ts");
169        assert!(!runs_on_host(&file, &own.join("proj"), &|_| Place::Unknown));
170        // One of the two not known is enough.
171        let file_text = file.to_string_lossy().into_owned();
172        assert!(!runs_on_host(&file, &own.join("proj"), &|asked| if asked == file_text { Place::Host } else { Place::Unknown }));
173    }
174
175    #[test]
176    fn a_project_folder_a_container_agent_can_write_gets_no_server_for_a_file_outside_it() {
177        let (_tmp, ws, own) = an_agents_folder_and_the_persons();
178        let file = own.join("proj").join("src").join("a.ts");
179        assert!(matches!(place(&file.to_string_lossy()), Place::Host));
180        assert!(!runs_on_host(&file, &ws.join("proj"), &place));
181    }
182
183    /// A folder outside the agent's that is a link into it: the file and the root are written outside, and
184    /// both resolve inside.
185    #[test]
186    fn a_linked_folder_that_resolves_into_a_container_agents_folder_gets_no_server() {
187        let (tmp, ws, _own) = an_agents_folder_and_the_persons();
188        let link = tmp.path().join("link");
189        if !link_dir(&ws.join("proj"), &link) {
190            return;
191        }
192        let file = link.join("src").join("a.ts");
193        let root = detect_workspace_root(&file);
194        assert_eq!(root, link, "the root is found as the path is written");
195        assert!(!runs_on_host(&file, &root, &place));
196    }
197
198    /// A file outside the agent's folder that is a link to one inside it: its own project folder is the
199    /// person's, and the file resolves inside.
200    #[test]
201    fn a_linked_file_that_resolves_into_a_container_agents_folder_gets_no_server() {
202        let (_tmp, ws, own) = an_agents_folder_and_the_persons();
203        let file = own.join("proj").join("src").join("linked.ts");
204        if !link_file(&ws.join("proj").join("src").join("a.ts"), &file) {
205            return;
206        }
207        let root = detect_workspace_root(&file);
208        assert_eq!(root, own.join("proj"));
209        assert!(matches!(place(&root.to_string_lossy()), Place::Host));
210        assert!(!runs_on_host(&file, &root, &place));
211    }
212
213    /// The person's project `own/proj` with one package of it, `packages/agent`, bound writable into a container.
214    fn a_project_holding_an_agents_folder(own: &Path) -> PathBuf {
215        let package = own.join("proj").join("packages").join("agent");
216        fs::create_dir_all(&package).unwrap();
217        container_folders::add_for_test(&package, &format!("lsp-{}", uuid::Uuid::new_v4().simple()), "/workspace");
218        package
219    }
220
221    #[test]
222    fn a_project_with_a_container_agents_folder_beneath_it_gets_no_server() {
223        let (_tmp, _ws, own) = an_agents_folder_and_the_persons();
224        let file = own.join("proj").join("src").join("a.ts");
225        let root = detect_workspace_root(&file);
226        assert!(served_on_host(&file, &root), "the person's project, before a folder beneath it is an agent's");
227        a_project_holding_an_agents_folder(&own);
228        // The file and the root are the person's; the folder beneath the root is not.
229        assert!(runs_on_host(&file, &root, &place));
230        assert!(!served_on_host(&file, &root));
231    }
232
233    #[test]
234    fn a_container_agents_folder_beside_a_project_leaves_its_server_on_the_host() {
235        let (tmp, ws, own) = an_agents_folder_and_the_persons();
236        // `ws` is beside `own`; so is a folder whose name starts with the project's.
237        let longer = own.join("proj-agent");
238        fs::create_dir_all(&longer).unwrap();
239        container_folders::add_for_test(&longer, &format!("lsp-{}", uuid::Uuid::new_v4().simple()), "/workspace");
240        let file = own.join("proj").join("src").join("a.ts");
241        assert!(served_on_host(&file, &detect_workspace_root(&file)));
242        assert!(!served_on_host(&ws.join("proj").join("src").join("a.ts"), &ws.join("proj")));
243        assert!(!served_on_host(&tmp.path().join("loose.ts"), tmp.path()), "the folder above both holds the agent's");
244    }
245
246    /// The project is reached through a link: the root is written outside, and resolves to the folder that
247    /// holds the agent's.
248    #[test]
249    fn a_linked_project_with_a_container_agents_folder_beneath_it_gets_no_server() {
250        let (tmp, _ws, own) = an_agents_folder_and_the_persons();
251        a_project_holding_an_agents_folder(&own);
252        let link = tmp.path().join("link");
253        if !link_dir(&own.join("proj"), &link) {
254            return;
255        }
256        let file = link.join("src").join("a.ts");
257        let root = detect_workspace_root(&file);
258        assert_eq!(root, link, "the root is found as the path is written");
259        assert!(runs_on_host(&file, &root, &place));
260        assert!(!served_on_host(&file, &root));
261    }
262
263    /// A link inside the agent's folder that points out of it: the path as written is the agent's.
264    #[test]
265    fn a_link_out_of_a_container_agents_folder_is_still_the_agents_path() {
266        let (_tmp, ws, own) = an_agents_folder_and_the_persons();
267        let link = ws.join("out");
268        if !link_dir(&own.join("proj"), &link) {
269            return;
270        }
271        let file = link.join("src").join("a.ts");
272        assert!(!runs_on_host(&file, &detect_workspace_root(&file), &place));
273    }
274}