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): make shell ids unique across device processes (#20097)

Arvin Xua8853098c9ec
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 917e24b6de7..186286020d0 100644
--- a/packages/local-file-shell/src/shell/__tests__/runner.test.ts
+++ b/packages/local-file-shell/src/shell/__tests__/runner.test.ts
@@ -39,11 +39,36 @@ describe('runCommand', () => {
const first = await runCommand({ command: 'echo first' }, { processManager: localManager });
const second = await runCommand({ command: 'echo second' }, { processManager: localManager });
- expect(first.shell_id).toBe('sh-1');
- expect(second.shell_id).toBe('sh-2');
+ expect(first.shell_id).toMatch(/^sh-[\da-f]{6}-1$/);
+ expect(second.shell_id).toBe(first.shell_id!.replace(/-1$/, '-2'));
localManager.cleanupAll();
});
+ // Regression: shell ids were a bare per-process counter (`sh-1`, `sh-2`…),
+ // so when getCommandOutput reached a different device process than the one
+ // that ran the command (app restart, two device processes on one machine),
+ // the same id named an unrelated command there and its output came back.
+ it('should not resolve a shell ID issued by another process to its own command', async () => {
+ const processA = new ShellProcessManager(tmpDir);
+ const processB = new ShellProcessManager(tmpDir);
+
+ try {
+ const fromA = await runCommand({ command: 'echo from-A' }, { processManager: processA });
+ const fromB = await runCommand({ command: 'echo from-B' }, { processManager: processB });
+
+ expect(fromA.shell_id).not.toBe(fromB.shell_id);
+
+ const crossed = await processB.getOutput({ shell_id: fromA.shell_id!, timeout: 0 });
+
+ expect(crossed.stdout).not.toContain('from-B');
+ expect(crossed.success).toBe(false);
+ expect(crossed.error).toContain(`Shell ID ${fromA.shell_id} not found`);
+ } finally {
+ processA.cleanupAll();
+ processB.cleanupAll();
+ }
+ });
+
it('should capture stderr output separately', async () => {
const result = await runCommand({ command: 'echo error >&2' }, { processManager });
@@ -243,7 +268,7 @@ describe('runCommand', () => {
expect(result.success).toBe(true);
expect(result.shell_id).toBeDefined();
expect(result.exit_code).toBeUndefined();
- expect(result.output_files?.stdout.path).toMatch(/sh-\d+\/stdout\.log$/);
+ expect(result.output_files?.stdout.path).toMatch(/sh-[\da-f]{6}-\d+\/stdout\.log$/);
expect(result.stdout).toBeUndefined();
});
diff --git a/packages/local-file-shell/src/shell/process-manager.ts b/packages/local-file-shell/src/shell/process-manager.ts
index 090366a7fe5..3a6f0aaa7a2 100644
--- a/packages/local-file-shell/src/shell/process-manager.ts
+++ b/packages/local-file-shell/src/shell/process-manager.ts
@@ -1,4 +1,5 @@
import type { ChildProcess } from 'node:child_process';
+import { randomBytes } from 'node:crypto';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
@@ -92,9 +93,27 @@ export interface ShellProcess {
startedAt?: number;
}
+/**
+ * Not-found text for a shell id this process never issued. Shell ids travel
+ * through the model and the device gateway, so a lookup can land on a device
+ * process other than the one that started the command (the app restarted, or
+ * two device processes are connected for the same machine). Say so, so the
+ * model re-runs the command instead of debugging a command that may be fine.
+ */
+const shellNotFoundError = (shellId: string): string =>
+ `Shell ID ${shellId} not found in this device process. It may have been started by a different or restarted process; its output is not available here.`;
+
export class ShellProcessManager {
private nextShellId = 1;
+ /**
+ * Per-instance random prefix for shell ids. A bare counter restarts at 1 in
+ * every process, so `sh-3` from one device process named an unrelated
+ * command in another and getCommandOutput returned that command's output.
+ * The prefix makes an id from another process miss (not found) instead.
+ */
+ private readonly shellIdToken = randomBytes(3).toString('hex');
+
private readonly outputRunDir: string;
private processes = new Map<string, ShellProcess>();
@@ -111,7 +130,7 @@ export class ShellProcessManager {
}
createShellId(): string {
- return `sh-${this.nextShellId++}`;
+ return `sh-${this.shellIdToken}-${this.nextShellId++}`;
}
createOutputFiles(shellId: string): ShellOutputFiles {
@@ -194,7 +213,7 @@ export class ShellProcessManager {
const shellProcess = this.processes.get(shell_id);
if (!shellProcess) {
return {
- error: `Shell ID ${shell_id} not found`,
+ error: shellNotFoundError(shell_id),
output: '',
running: false,
stderr: '',
@@ -322,7 +341,7 @@ export class ShellProcessManager {
kill(shell_id: string): KillCommandResult {
const shellProcess = this.processes.get(shell_id);
if (!shellProcess) {
- return { error: `Shell ID ${shell_id} not found`, success: false };
+ return { error: shellNotFoundError(shell_id), success: false };
}
try {
diff --git a/packages/tool-runtime/src/LocalSystemExecutionRuntime.ts b/packages/tool-runtime/src/LocalSystemExecutionRuntime.ts
index 8cc7ec107c7..e51feaf102e 100644
--- a/packages/tool-runtime/src/LocalSystemExecutionRuntime.ts
+++ b/packages/tool-runtime/src/LocalSystemExecutionRuntime.ts
@@ -451,6 +451,11 @@ export class LocalSystemExecutionRuntime extends ComputerRuntime {
case 'getCommandOutput': {
return {
+ // Surface raw.error so a failed lookup reaches the model as its real
+ // reason ("Shell ID … not found in this device process …") rather
+ // than "[UNKNOWN_EXEC_ERROR] Tool execution failed" β€” the generic text
+ // left the model unable to tell a lost session from a broken command.
+ error: raw.error ? { message: String(raw.error) } : undefined,
result: {
durationMs: raw.duration_ms,
exitCode: raw.exit_code,
@@ -468,6 +473,7 @@ export class LocalSystemExecutionRuntime extends ComputerRuntime {
case 'killCommand': {
return {
+ error: raw.error ? { message: String(raw.error) } : undefined,
result: { error: raw.error, success: raw.success },
success: raw.success,
};
diff --git a/packages/tool-runtime/src/__tests__/LocalSystemExecutionRuntime.test.ts b/packages/tool-runtime/src/__tests__/LocalSystemExecutionRuntime.test.ts
index 9823011743b..eaded5639fe 100644
--- a/packages/tool-runtime/src/__tests__/LocalSystemExecutionRuntime.test.ts
+++ b/packages/tool-runtime/src/__tests__/LocalSystemExecutionRuntime.test.ts
@@ -737,6 +737,46 @@ describe('LocalSystemExecutionRuntime.executeToolCall β€” dispatch', () => {
});
});
+describe('LocalSystemExecutionRuntime shell session lookups', () => {
+ // Regression: a shell_id that reaches a device process which never issued it
+ // (app restarted, or two device processes for one machine) came back as
+ // "[UNKNOWN_EXEC_ERROR] Tool execution failed" β€” the service's reason was
+ // dropped, so the model could not tell a lost session from a failed command.
+ const notFound =
+ 'Shell ID sh-1a2b3c-4 not found in this device process. It may have been started by a different or restarted process; its output is not available here.';
+
+ it('surfaces the not-found reason for getCommandOutput', async () => {
+ const service = createService({
+ getCommandOutput: vi.fn().mockResolvedValue({
+ error: notFound,
+ output: '',
+ running: false,
+ stderr: '',
+ stdout: '',
+ success: false,
+ }),
+ });
+ const runtime = new LocalSystemExecutionRuntime(service);
+
+ const output = await runtime.executeToolCall('getCommandOutput', { shell_id: 'sh-1a2b3c-4' });
+
+ expect(output?.success).toBe(false);
+ expect(output?.content).toBe(notFound);
+ });
+
+ it('surfaces the not-found reason for killCommand', async () => {
+ const service = createService({
+ killCommand: vi.fn().mockResolvedValue({ error: notFound, success: false }),
+ });
+ const runtime = new LocalSystemExecutionRuntime(service);
+
+ const output = await runtime.executeToolCall('killCommand', { shell_id: 'sh-1a2b3c-4' });
+
+ expect(output?.success).toBe(false);
+ expect(output?.content).toBe(notFound);
+ });
+});
+
describe('LocalSystemExecutionRuntime.runCommand', () => {
it('surfaces a pre-spawn failure reason instead of UNKNOWN_EXEC_ERROR', async () => {
// A command that never starts has no process, so no stderr and no exit