Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(agent): a background task ran in LU's folder, not the project's

David2c8f97142b9b
diff --git a/src-tauri/src/commands/bg_tasks.rs b/src-tauri/src/commands/bg_tasks.rs
index 8914e78a..000b281c 100644
--- a/src-tauri/src/commands/bg_tasks.rs
+++ b/src-tauri/src/commands/bg_tasks.rs
@@ -128,6 +128,12 @@ struct StartArgs {
cwd: Option<String>,
#[serde(default)]
shell: Option<String>,
+ // Same chat context the flat `shell_execute` path carries, so a background
+ // task defaults to the SAME folder its foreground twin would use.
+ #[serde(default)]
+ chat_id: Option<String>,
+ #[serde(default)]
+ working_directory: Option<String>,
}
// ── Internal impls (verbatim from uselu, sans &AppState) ──────────────
@@ -141,7 +147,22 @@ async fn shell_task_start_impl(args: &Value) -> CmdResult {
if a.command.trim().is_empty() {
return Err(bad_request("command is empty"));
}
- if let Some(cwd) = &a.cwd {
+ // Without an explicit cwd the child used to inherit LU's OWN process
+ // directory — the install folder on Windows. The tool exists for
+ // `pnpm install` / `cargo build`, so a task the model started "in the
+ // project" ran somewhere else entirely. Fall back to the workspace the
+ // foreground shell tool resolves.
+ let cwd = match a.cwd.clone() {
+ Some(c) => Some(c),
+ None => crate::commands::filesystem::workspace_root(
+ a.chat_id.as_deref(),
+ a.working_directory.as_deref(),
+ )
+ .to_str()
+ .map(|s| s.to_string())
+ .filter(|s| std::path::Path::new(s).is_dir()),
+ };
+ if let Some(cwd) = &cwd {
let p = std::path::Path::new(cwd);
if !p.is_dir() {
return Err(bad_request(format!("cwd does not exist: {}", cwd)));
@@ -167,7 +188,7 @@ async fn shell_task_start_impl(args: &Value) -> CmdResult {
let mut cmd = TokioCommand::new(&program);
cmd.args(&args_vec);
- if let Some(cwd) = &a.cwd {
+ if let Some(cwd) = &cwd {
cmd.current_dir(cwd);
}
cmd.stdout(std::process::Stdio::piped())
@@ -200,7 +221,7 @@ async fn shell_task_start_impl(args: &Value) -> CmdResult {
status: BgTaskStatus {
id: id.clone(),
command: a.command.clone(),
- cwd: a.cwd.clone(),
+ cwd: cwd.clone(),
started_at: now_secs(),
finished_at: None,
exit_code: None,
@@ -518,3 +539,81 @@ mod cancel_tests {
);
}
}
+
+#[cfg(test)]
+mod cwd_default_tests {
+ use super::*;
+
+ /// The tool exists for `pnpm install` / `cargo build`, so the folder it
+ /// runs in IS the point. Without an explicit cwd the child used to inherit
+ /// LU's own process directory — the install folder on Windows — so a task
+ /// the model started "in the project" ran somewhere else.
+ #[tokio::test]
+ #[cfg_attr(target_os = "windows", ignore = "uses sh/pwd")]
+ async fn a_task_without_an_explicit_cwd_lands_in_the_workspace() {
+ let ws = std::env::temp_dir().join(format!("lu-bg-ws-{}", std::process::id()));
+ std::fs::create_dir_all(&ws).unwrap();
+ // macOS hands out /var/... which is a symlink to /private/var; pwd
+ // reports the resolved path, so compare against that.
+ let expected = std::fs::canonicalize(&ws).unwrap();
+
+ let start = shell_task_start_impl(&json!({
+ "command": "pwd",
+ "shell": "sh",
+ "working_directory": ws.to_string_lossy(),
+ }))
+ .await
+ .expect("start");
+ let id = start["id"].as_str().unwrap().to_string();
+
+ let mut seen = String::new();
+ for _ in 0..40 {
+ tokio::time::sleep(std::time::Duration::from_millis(50)).await;
+ let st = shell_task_status_impl(&json!({ "id": id })).await.unwrap();
+ seen = st["output_tail"].as_str().unwrap_or("").to_string();
+ if !seen.trim().is_empty() {
+ break;
+ }
+ }
+ assert_eq!(
+ seen.trim(),
+ expected.to_string_lossy(),
+ "background task did not start in the workspace",
+ );
+ let _ = std::fs::remove_dir_all(&ws);
+ }
+
+ /// An explicit cwd from the caller still wins over the derived default.
+ #[tokio::test]
+ #[cfg_attr(target_os = "windows", ignore = "uses sh/pwd")]
+ async fn an_explicit_cwd_still_wins() {
+ let a = std::env::temp_dir().join(format!("lu-bg-a-{}", std::process::id()));
+ let b = std::env::temp_dir().join(format!("lu-bg-b-{}", std::process::id()));
+ std::fs::create_dir_all(&a).unwrap();
+ std::fs::create_dir_all(&b).unwrap();
+ let expected = std::fs::canonicalize(&b).unwrap();
+
+ let start = shell_task_start_impl(&json!({
+ "command": "pwd",
+ "shell": "sh",
+ "cwd": b.to_string_lossy(),
+ "working_directory": a.to_string_lossy(),
+ }))
+ .await
+ .expect("start");
+ let id = start["id"].as_str().unwrap().to_string();
+
+ let mut seen = String::new();
+ for _ in 0..40 {
+ tokio::time::sleep(std::time::Duration::from_millis(50)).await;
+ let st = shell_task_status_impl(&json!({ "id": id })).await.unwrap();
+ seen = st["output_tail"].as_str().unwrap_or("").to_string();
+ if !seen.trim().is_empty() {
+ break;
+ }
+ }
+ assert_eq!(seen.trim(), expected.to_string_lossy());
+ let _ = std::fs::remove_dir_all(&a);
+ let _ = std::fs::remove_dir_all(&b);
+ }
+}
diff --git a/src/api/agents/bg-tasks.ts b/src/api/agents/bg-tasks.ts
index 181a8bb6..d80d3f0f 100644
--- a/src/api/agents/bg-tasks.ts
+++ b/src/api/agents/bg-tasks.ts
@@ -21,6 +21,10 @@ export async function bgStart(input: {
command: string
cwd?: string
shell?: string
+ /** Chat context, so the task defaults to the same workspace the foreground
+ * shell tool uses instead of LU own process directory. */
+ chatId?: string
+ workingDirectory?: string
}): Promise<{ id: string }> {
if (!input.command?.trim()) throw new Error('command is required')
// The Rust bg_tasks commands take a SINGLE `args: serde_json::Value` param
@@ -35,6 +39,8 @@ export async function bgStart(input: {
command: input.command,
cwd: input.cwd,
shell: input.shell,
+ chat_id: input.chatId,
+ working_directory: input.workingDirectory,
},
})
}
diff --git a/src/api/mcp/builtin-tools.ts b/src/api/mcp/builtin-tools.ts
index 2e19bba6..4c3df390 100644
--- a/src/api/mcp/builtin-tools.ts
+++ b/src/api/mcp/builtin-tools.ts
@@ -874,7 +874,9 @@ async function runShell(command: string, cwd: string | undefined, timeout = 6000
async function executeShellExecuteBg(args: Record<string, any>): Promise<string> {
const { bgStart } = await import('../agents/bg-tasks')
- const { id } = await bgStart({ command: args.command, cwd: args.cwd })
+ // Thread the chat context through, or the task starts in LU's own directory
+ // instead of the workspace the foreground shell tool uses.
+ const { id } = await bgStart({ command: args.command, cwd: args.cwd, ...chatCtx() })
return `Task started: ${id}. Use shell_task_status to poll, shell_task_kill to cancel.`
}