diff --git a/packages/core/src/tools/shell.test.ts b/packages/core/src/tools/shell.test.ts index e5e9fb27da5..ebacbc5e54c 100644 --- a/packages/core/src/tools/shell.test.ts +++ b/packages/core/src/tools/shell.test.ts @@ -20,11 +20,13 @@ const mockHomedir = vi.hoisted(() => vi.fn()); const mockShellExecutionService = vi.hoisted(() => vi.fn()); const mockShellBackground = vi.hoisted(() => vi.fn()); +const mockShellOnExit = vi.hoisted(() => vi.fn()); vi.mock('../services/shellExecutionService.js', () => ({ ShellExecutionService: { execute: mockShellExecutionService, background: mockShellBackground, + onExit: mockShellOnExit, }, })); diff --git a/packages/core/src/tools/shell.ts b/packages/core/src/tools/shell.ts index 9ad657febdf..079832636a0 100644 --- a/packages/core/src/tools/shell.ts +++ b/packages/core/src/tools/shell.ts @@ -695,6 +695,25 @@ export class ShellToolInvocation extends BaseToolInvocation< // If the model requested to run in the background, do so after a short delay. let completed = false; if (this.params.is_background) { + if (tempFilePath || tempDir) { + ShellExecutionService.onExit(pid, () => { + if (tempFilePath) { + try { + fs.unlinkSync(tempFilePath); + } catch { + // Ignore + } + } + if (tempDir) { + try { + fs.rmSync(tempDir, { recursive: true, force: true }); + } catch { + // Ignore + } + } + }); + } + resultPromise .then(() => { completed = true;