diff --git a/crates/ui-egui/src/agent_chat/acp.rs b/crates/ui-egui/src/agent_chat/acp.rs index 96555942a..fc8c0cb0e 100644 --- a/crates/ui-egui/src/agent_chat/acp.rs +++ b/crates/ui-egui/src/agent_chat/acp.rs @@ -68,6 +68,36 @@ impl McpBridge { "env": [], }) } + + /// The same bridge for an agent inside WSL: the CLI as WSL sees it. It still runs on the + /// Windows side (WSL starts Windows programs itself), so the port is the same. + pub fn in_wsl(&self) -> McpBridge { + McpBridge { cli: wsl_path(&self.cli), port: self.port } + } +} + +/// The agent runs inside WSL: its program is `wsl` or `wsl.exe` (any case, with or without a +/// folder). Such an agent can't use Windows paths, so the session hands it [`wsl_path`]s (#5546). +pub fn runs_in_wsl(argv: &[String]) -> bool { + argv.first().is_some_and(|program| { + let name = program.rsplit(['\\', '/']).next().unwrap_or(program); + name.eq_ignore_ascii_case("wsl") || name.eq_ignore_ascii_case("wsl.exe") + }) +} + +/// A Windows drive path as WSL shows it under its default mount root: `C:\Users\u` is +/// `/mnt/c/Users/u`. Anything else (a UNC share, a path already in that form) is kept. +pub fn wsl_path(path: &std::path::Path) -> std::path::PathBuf { + let text = path.to_string_lossy(); + let text = text.strip_prefix(r"\\?\").unwrap_or(&text); + let mut chars = text.chars(); + match (chars.next(), chars.next(), chars.next()) { + (Some(drive), Some(':'), Some('\\' | '/') | None) if drive.is_ascii_alphabetic() => { + let rest: String = chars.map(|c| if c == '\\' { '/' } else { c }).collect(); + format!("/mnt/{}/{rest}", drive.to_ascii_lowercase()).into() + } + _ => path.to_path_buf(), + } } /// What every session is told about where it is. Agents that don't read `_meta` ignore it. @@ -513,6 +543,32 @@ mod tests { assert_eq!(session_new_params(std::path::Path::new("/tmp"), None)["mcpServers"], json!([])); } + /// #5546: an agent started through `wsl.exe` gets the session's paths as WSL shows them. + #[test] + fn an_agent_inside_wsl_gets_wsl_paths() { + let argv = |line: &str| split_command(line); + assert!(runs_in_wsl(&argv("wsl.exe -d Ubuntu -e bash -lc claude-agent-acp"))); + assert!(runs_in_wsl(&argv("WSL claude-agent-acp"))); + assert!(runs_in_wsl(&argv(r#""C:\Windows\System32\wsl.exe" -e claude-agent-acp"#))); + assert!(!runs_in_wsl(&argv("claude-agent-acp"))); + assert!(!runs_in_wsl(&argv("wslconfig /l"))); + assert!(!runs_in_wsl(&[])); + + let wsl = |p: &str| wsl_path(std::path::Path::new(p)).to_string_lossy().into_owned(); + assert_eq!(wsl(r"C:\Users\u\AppData\Local\SolidCraft\agent-chat\work"), "/mnt/c/Users/u/AppData/Local/SolidCraft/agent-chat/work"); + assert_eq!(wsl(r"\\?\D:\Program Files\SolidCraft\solidcraft-cli.exe"), "/mnt/d/Program Files/SolidCraft/solidcraft-cli.exe"); + assert_eq!(wsl("E:/designs"), "/mnt/e/designs"); + assert_eq!(wsl("C:"), "/mnt/c/"); + assert_eq!(wsl("/home/u/designs"), "/home/u/designs"); + assert_eq!(wsl(r"\\server\share\x"), r"\\server\share\x"); + + let b = McpBridge { cli: r"C:\Program Files\SolidCraft\solidcraft-cli.exe".into(), port: 41234 }; + let p = session_new_params(&wsl_path(std::path::Path::new(r"C:\Users\u\work")), Some(&b.in_wsl())); + assert_eq!(p["cwd"], "/mnt/c/Users/u/work"); + assert_eq!(p["mcpServers"][0]["command"], "/mnt/c/Program Files/SolidCraft/solidcraft-cli.exe"); + assert_eq!(p["mcpServers"][0]["args"], json!(["mcp", "--connect", "127.0.0.1:41234"])); + } + #[test] fn auth_methods_of_both_adapters_are_read() { // As the Claude adapter offers them to a client with terminal login. diff --git a/crates/ui-egui/src/agent_chat/acp_client.rs b/crates/ui-egui/src/agent_chat/acp_client.rs index 82918e90f..730c0cf0c 100644 --- a/crates/ui-egui/src/agent_chat/acp_client.rs +++ b/crates/ui-egui/src/agent_chat/acp_client.rs @@ -46,10 +46,13 @@ impl AcpBackend { pub fn start(argv: &[String], ctx: StartContext) -> Result { let wake = ctx.wake; let proc = AgentProcess::spawn(argv, &ctx.cwd, ENV_REMOVE, wake)?; + // An agent inside WSL is told the same folder and bridge as WSL shows them (#5546). + let (cwd, bridge) = + if acp::runs_in_wsl(argv) { (acp::wsl_path(&ctx.cwd), ctx.bridge.as_ref().map(acp::McpBridge::in_wsl)) } else { (ctx.cwd, ctx.bridge) }; let mut b = AcpBackend { proc, - cwd: ctx.cwd, - bridge: ctx.bridge, + cwd, + bridge, next_id: 0, pending: HashMap::new(), session: None, @@ -257,8 +260,9 @@ pub fn test_agent(argv: &[String], cwd: &std::path::Path) -> Result