Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(local-file-shell): fall back to a usable directory when the pinned cwd is gone (#20735)

Arvin Xu5315f5234801
diff --git a/packages/local-file-shell/src/shell/__tests__/runner.test.ts b/packages/local-file-shell/src/shell/__tests__/runner.test.ts
index 186286020d0..207348bb7b6 100644
--- a/packages/local-file-shell/src/shell/__tests__/runner.test.ts
+++ b/packages/local-file-shell/src/shell/__tests__/runner.test.ts
@@ -23,6 +23,18 @@ describe('runCommand', () => {
fs.rmSync(tmpDir, { force: true, recursive: true });
});
+ const waitFor = async (predicate: () => boolean, timeoutMs = 5_000): Promise<boolean> => {
+ const deadline = Date.now() + timeoutMs;
+ while (Date.now() < deadline) {
+ if (predicate()) return true;
+ await new Promise((resolve) => setTimeout(resolve, 25));
+ }
+ return predicate();
+ };
+
+ const fallbackDirsIn = (root: string): string[] =>
+ fs.readdirSync(root).filter((name) => name.startsWith('lobehub-cwd-fallback-'));
+
describe('foreground observation mode', () => {
it('should execute a simple command and finish immediately', async () => {
const result = await runCommand({ command: 'echo hello' }, { processManager });
@@ -130,32 +142,153 @@ describe('runCommand', () => {
expect(result.stdout).toContain('/tmp');
});
- it('[R4] reports a missing cwd as a missing working directory, not as a missing shell', async () => {
+ it('[R4] falls back and runs when the requested cwd is missing, naming the fallback', async () => {
const missingCwd = path.join(tmpDir, 'missing-worktree');
const result = await runCommand(
- { command: 'echo unreachable', cwd: missingCwd },
+ { command: 'echo reachable && pwd', cwd: missingCwd },
{ processManager },
);
- expect(result.success).toBe(false);
- expect(result.exit_code).toBeUndefined();
+ expect(result.success).toBe(true);
+ expect(result.exit_code).toBe(0);
// Node blames the executable (`spawn /bin/sh ENOENT`) when cwd is missing,
// which sends the model off debugging a healthy shell.
+ expect(result.error ?? '').not.toMatch(/spawn \S+ ENOENT/);
+ // The fallback is reported, never silent: the model must be able to tell
+ // the command ran somewhere else than the cwd it asked for.
+ expect(result.stdout).toContain('Working directory does not exist');
+ expect(result.stdout).toContain(missingCwd);
+ expect(result.stdout).toContain('Falling back to');
+ // The fallback is an isolated directory under the temp root β€” never the
+ // user's home β€” so project-relative commands cannot damage real files.
+ expect(result.stdout).toContain('lobehub-cwd-fallback-');
+ // The command actually ran from the fallback directory.
+ expect(result.stdout).toContain('reachable');
+ });
+
+ it('falls back to a fresh empty directory under the temp root when the cwd is missing', async () => {
+ const missingCwd = path.join(tmpDir, 'missing-worktree');
+ const result = await runCommand(
+ { command: 'echo temp-fallback && pwd', cwd: missingCwd },
+ {
+ processManager,
+ tmpdirOverride: tmpDir,
+ },
+ );
+
+ expect(result.success).toBe(true);
+ expect(result.stdout).toContain('Falling back to');
+ expect(result.stdout).toContain('temp-fallback');
+ // Exactly one isolated fallback directory was created under the temp
+ // root, it is empty, and the run reported it by path.
+ const created = fs
+ .readdirSync(tmpDir)
+ .filter((name) => name.startsWith('lobehub-cwd-fallback-'));
+ expect(created).toHaveLength(1);
+ const fallbackDir = path.join(tmpDir, created[0]);
+ expect(fs.readdirSync(fallbackDir)).toEqual([]);
+ expect(result.stdout).toContain(`Falling back to ${fallbackDir}`);
+ });
+
+ it('keeps the structured error when the fallback directory cannot be created', async () => {
+ const missingCwd = path.join(tmpDir, 'missing-worktree');
+ const result = await runCommand(
+ { command: 'echo unreachable', cwd: missingCwd },
+ {
+ processManager,
+ tmpdirOverride: path.join(tmpDir, 'missing-tmp-root'),
+ },
+ );
+
+ expect(result.success).toBe(false);
+ expect(result.exit_code).toBeUndefined();
expect(result.error).not.toMatch(/spawn \S+ ENOENT/);
expect(result.error).toContain(`Working directory does not exist on ${os.hostname()}`);
expect(result.error).toContain(missingCwd);
+ expect(result.error).toContain('The shell is fine');
});
- it('reports a cwd that is a file as not a directory', async () => {
+ it('does not fall back for a sandboxed run: the fence is built on the requested cwd', async () => {
+ const missingCwd = path.join(tmpDir, 'missing-worktree');
+ const result = await runCommand(
+ { command: 'echo unreachable', cwd: missingCwd },
+ {
+ processManager,
+ sandboxPolicy: {
+ allowNetwork: false,
+ onUnavailable: 'deny',
+ writableRoots: [tmpDir],
+ },
+ },
+ );
+
+ expect(result.success).toBe(false);
+ expect(result.exit_code).toBeUndefined();
+ expect(result.error).toContain(`Working directory does not exist on ${os.hostname()}`);
+ expect(result.error).toContain(missingCwd);
+ expect(result.error).toContain('sandboxed run must start in its requested directory');
+ });
+
+ it('falls back when the requested cwd is a file, naming the real problem', async () => {
const fileCwd = path.join(tmpDir, 'a-file');
fs.writeFileSync(fileCwd, '');
const result = await runCommand(
- { command: 'echo unreachable', cwd: fileCwd },
+ { command: 'echo via-file-cwd', cwd: fileCwd },
{ processManager },
);
- expect(result.success).toBe(false);
- expect(result.error).toContain(`Working directory is not a directory on ${os.hostname()}`);
+ expect(result.success).toBe(true);
+ expect(result.stdout).toContain('Working directory is not a directory');
+ expect(result.stdout).toContain('Falling back to');
+ expect(result.stdout).toContain('via-file-cwd');
+ });
+
+ it('removes the fallback directory once the foreground command closes', async () => {
+ const missingCwd = path.join(tmpDir, 'missing-worktree');
+ const result = await runCommand(
+ { command: 'echo cleanup-foreground && pwd', cwd: missingCwd },
+ { processManager, tmpdirOverride: tmpDir },
+ );
+
+ expect(result.success).toBe(true);
+ expect(result.stdout).toContain('cleanup-foreground');
+ expect(fallbackDirsIn(tmpDir)).toHaveLength(1);
+ const removed = await waitFor(() => fallbackDirsIn(tmpDir).length === 0);
+ expect(removed).toBe(true);
+ });
+
+ it('removes the fallback directory once a background command closes', async () => {
+ const missingCwd = path.join(tmpDir, 'missing-worktree');
+ const bg = await runCommand(
+ { command: 'echo cleanup-background && pwd', cwd: missingCwd, run_in_background: true },
+ { processManager, tmpdirOverride: tmpDir },
+ );
+
+ expect(bg.success).toBe(true);
+ expect(bg.shell_id).toBeDefined();
+ // While the command runs, the fallback directory exists.
+ expect(fallbackDirsIn(tmpDir)).toHaveLength(1);
+ const observed = await processManager.getOutput({ shell_id: bg.shell_id! });
+ expect(observed.stdout).toContain('cleanup-background');
+ const removed = await waitFor(() => fallbackDirsIn(tmpDir).length === 0);
+ expect(removed).toBe(true);
+ });
+
+ it('never creates a fallback directory when the requested cwd is usable', async () => {
+ const usableCwd = path.join(tmpDir, 'usable-cwd');
+ fs.mkdirSync(usableCwd);
+ const result = await runCommand(
+ { command: 'echo usable && pwd', cwd: usableCwd },
+ {
+ processManager,
+ tmpdirOverride: tmpDir,
+ },
+ );
+
+ expect(result.success).toBe(true);
+ expect(result.stdout).not.toContain('Falling back to');
+ const removed = await waitFor(() => fallbackDirsIn(tmpDir).length === 0);
+ expect(removed).toBe(true);
});
it('should merge env into child process environment', async () => {
diff --git a/packages/local-file-shell/src/shell/runner.ts b/packages/local-file-shell/src/shell/runner.ts
index 031f8b8e174..94617866930 100644
--- a/packages/local-file-shell/src/shell/runner.ts
+++ b/packages/local-file-shell/src/shell/runner.ts
@@ -1,6 +1,7 @@
import type { spawn } from 'node:child_process';
-import { stat } from 'node:fs/promises';
+import { mkdtemp, rm, stat } from 'node:fs/promises';
import os from 'node:os';
+import path from 'node:path';
import type { SandboxPolicy } from '@lobechat/device-sandbox';
@@ -39,21 +40,106 @@ export interface RunCommandOptions {
* registers the process with a tracker). Ignored when {@link backend} is set.
*/
spawnProcess?: typeof spawn;
+ /**
+ * Root directory for the isolated fallback directory (see below). Defaults
+ * to `os.tmpdir()`. Test/DI hook β€” the fallback and its reporting are the
+ * behaviors under test, not the specific directory.
+ */
+ tmpdirOverride?: string;
}
/**
- * Node reports a missing spawn cwd as `spawn <shell> ENOENT` β€” blaming the
- * shell binary β€” so the model goes off debugging a healthy shell. Check the
- * directory first and name the real problem, and the machine it happened on
- * (a cwd pinned on another device is the usual cause).
+ * `true` = resolves to a directory; `false` = resolves to a non-directory
+ * (a regular file); `undefined` = cannot be resolved at all (missing, an
+ * unreadable parent, a symlink loop). Keeping the three cases apart lets the
+ * report name what actually happened instead of guessing.
*/
-const checkWorkingDirectory = async (cwd: string): Promise<string | undefined> => {
+const statIsDirectory = async (dir: string): Promise<boolean | undefined> => {
try {
- if ((await stat(cwd)).isDirectory()) return;
- return `Working directory is not a directory on ${os.hostname()}: ${cwd}`;
- } catch (error) {
- if ((error as NodeJS.ErrnoException).code !== 'ENOENT') return;
- return `Working directory does not exist on ${os.hostname()}: ${cwd}. The shell is fine β€” run the command from a directory that exists on this device.`;
+ return (await stat(dir)).isDirectory();
+ } catch {
+ return undefined;
+ }
+};
+
+interface WorkingDirectoryResolution {
+ /** The directory to spawn in, when the request was honored or a fallback was found. */
+ cwd?: string;
+ /** Structured failure: no usable directory anywhere, or a sandboxed run that must not move. */
+ error?: string;
+ /**
+ * Set when the requested cwd was unusable and the run fell back. Prepended to
+ * the command's output so the model sees the caveat next to whatever the
+ * command printed from the fallback directory.
+ */
+ notice?: string;
+ /**
+ * Removes the fallback directory once nothing is running in it. Only set when
+ * the cwd is a fresh fallback; without this, every command against a stale
+ * cwd would leave another directory (and anything the command wrote into it)
+ * under the temp root forever.
+ */
+ remove?: () => Promise<void>;
+}
+
+/**
+ * Resolve a requested cwd to a directory the shell can actually start in.
+ *
+ * A cwd pinned on another machine (a stale repo path, a moved worktree) would
+ * otherwise kill every command in the run: the run cannot change its own pinned
+ * cwd, callers strip a model-supplied cwd before dispatch, and re-running
+ * `cd` inside the command does not help because the check happens before the
+ * shell exists β€” so the same failure repeats forever. Un-sandboxed runs fall
+ * back to a fresh isolated directory created under the OS temp root, and the
+ * fallback is reported so nothing downstream mistakes it for the requested
+ * directory.
+ *
+ * Sandboxed runs do NOT fall back: the sandbox fence (`createLocalSandboxPolicy`)
+ * is built on the requested cwd, so silently spawning in another directory
+ * would grant writes somewhere the user never approved. Those fail loudly.
+ */
+const resolveWorkingDirectory = async (
+ requestedCwd: string,
+ sandboxRequested: boolean,
+ fallbackRoot: string | undefined,
+): Promise<WorkingDirectoryResolution> => {
+ const usable = await statIsDirectory(requestedCwd);
+ if (usable) return { cwd: requestedCwd };
+
+ const reason =
+ usable === false
+ ? `Working directory is not a directory on ${os.hostname()}: ${requestedCwd}`
+ : `Working directory does not exist on ${os.hostname()}: ${requestedCwd}`;
+
+ if (sandboxRequested) {
+ return {
+ error: `${reason}. A sandboxed run must start in its requested directory β€” the sandbox fence is built on it β€” so the command was not run.`,
+ };
+ }
+
+ // The fallback is a fresh, empty directory rather than an existing one: a
+ // populated fallback (the user's home) would run project-relative commands
+ // like `rm -rf ./*` against someone else's files before the notice even
+ // reaches the model. An empty directory cannot be damaged that way, and the
+ // notice plus the visible `pwd` make the move impossible to miss.
+ try {
+ const fallback = await mkdtemp(path.join(fallbackRoot ?? os.tmpdir(), 'lobehub-cwd-fallback-'));
+ return {
+ cwd: fallback,
+ notice: `${reason}. Falling back to ${fallback}.`,
+ // Best-effort cleanup: the fallback is empty and lives under the OS temp
+ // root, so a missed removal is only a small leak β€” never lose the run
+ // over it.
+ remove: async () => {
+ await rm(fallback, { force: true, recursive: true });
+ },
+ };
+ } catch {
+ // No usable fallback: keep the structured error instead of letting the
+ // spawn surface a misleading `spawn <shell> ENOENT`.
+ return {
+ error: `${reason}. The shell is fine β€” run the command from a directory that exists on this device.`,
+ };
}
};
@@ -73,6 +159,7 @@ export async function runCommand(
onSandboxUnavailable,
sandboxPolicy,
spawnProcess,
+ tmpdirOverride,
}: RunCommandOptions,
): Promise<RunCommandResult> {
if (!command) {
@@ -87,9 +174,17 @@ export async function runCommand(
const logPrefix = `[runCommand: ${description || command.slice(0, 50)}]`;
logger?.debug(`${logPrefix} Starting`, { background: run_in_background, cwd, timeout });
+ let effectiveCwd = cwd;
+ let cwdNotice: string | undefined;
+ // Set only when the cwd fell back to a fresh directory; invoked once nothing
+ // is running in it anymore (see the `close` wiring below).
+ let removeCwdFallback: (() => Promise<void>) | undefined;
if (cwd) {
- const cwdError = await checkWorkingDirectory(cwd);
- if (cwdError) return { error: cwdError, success: false };
+ const resolution = await resolveWorkingDirectory(cwd, !!sandboxPolicy, tmpdirOverride);
+ if (resolution.error) return { error: resolution.error, success: false };
+ effectiveCwd = resolution.cwd;
+ cwdNotice = resolution.notice;
+ removeCwdFallback = resolution.remove;
}
const requestedEnv = extraEnv ? { ...process.env, ...extraEnv } : process.env;
@@ -105,6 +200,10 @@ export async function runCommand(
: command;
const shellConfig = await getShellConfig(effectiveCommand);
let outputFiles: ShellOutputFiles | undefined;
+ // Whether the spawned process made it into the process manager. Past this
+ // point a thrown error must NOT remove the fallback: the process is still
+ // running in it, and its eventual `close` event owns the cleanup.
+ let registered = false;
let releaseSandbox: (() => void) | undefined;
// What actually happened, reported back so nothing downstream has to infer a
// security property from the request that asked for it.
@@ -126,7 +225,7 @@ export async function runCommand(
try {
launchPlan = await createSandboxLaunchPlan({
command: shellConfig,
- cwd,
+ cwd: effectiveCwd,
env: requestedEnv,
policy: sandboxPolicy,
});
@@ -144,7 +243,7 @@ export async function runCommand(
outputFiles = shellOutputFiles;
const handle = backend.spawn(
{ args: launchCommand.args, cmd: launchCommand.cmd },
- { cwd, env: launchEnv, outputFiles: shellOutputFiles, shellId },
+ { cwd: effectiveCwd, env: launchEnv, outputFiles: shellOutputFiles, shellId },
);
const shellProcess: ShellProcess = {
@@ -170,16 +269,31 @@ export async function runCommand(
);
shellProcess.exitCode = 1;
});
- handle.once('close', () => releaseSandbox?.());
+ handle.once('close', () => {
+ releaseSandbox?.();
+ // Best-effort removal of the cwd fallback directory once nothing can be
+ // using it. `close` fires after exit *and* after all stdio has flushed,
+ // so nothing reads or writes the fallback anymore. Background runs,
+ // kills (SIGKILL has no exit callback) and `cleanupAll()` all end in a
+ // `close` event, so this is the one choke point that covers every exit
+ // path. Removal races are tolerated by design: a command that wrote into
+ // the fallback loses only its own temp files.
+ if (removeCwdFallback) {
+ removeCwdFallback().catch((error) => {
+ logger?.debug(`${logPrefix} Fallback directory cleanup failed`, { error });
+ });
+ }
+ });
processManager.register(shellId, shellProcess);
+ registered = true;
// Close our fd copy only after error/close listeners are registered; spawn errors are asynchronous.
processManager.closeOutputFiles(shellOutputFiles);
logger?.info?.(`${logPrefix} Started session`, { background: run_in_background, shellId });
if (run_in_background) {
return {
- output: '',
+ output: cwdNotice ?? '',
output_files: processManager.getOutputFilesInfo(shellOutputFiles),
sandboxed,
shell_id: shellId,
@@ -192,14 +306,26 @@ export async function runCommand(
timeout,
});
+ const result: RunCommandResult = { ...observation, sandboxed, shell_id: shellId };
+ if (!cwdNotice) return result;
+ // Put the caveat before whatever the command printed, so the model reads
+ // the fallback next to the output it produced from the fallback directory.
+ const prefix = `${cwdNotice}\n\n`;
return {
- ...observation,
- sandboxed,
- shell_id: shellId,
+ ...result,
+ output: `${prefix}${result.output ?? ''}`,
+ stdout: `${prefix}${result.stdout ?? ''}`,
};
} catch (error) {
releaseSandbox?.();
if (outputFiles) processManager.closeOutputFiles(outputFiles);
+ // The spawn never happened (shell config, sandbox plan build, or a spawn
+ // throw before registration): no `close` event will fire, so remove the
+ // fallback here instead of leaking it. After registration the process is
+ // running in the fallback β€” its `close` event owns the cleanup.
+ if (removeCwdFallback && !registered) {
+ await removeCwdFallback().catch(() => {});
+ }
return { error: (error as Error).message, success: false };
}
}