Skip to content

Commit 3dca767

Browse files
committed
fix: update CLI cleanup to use ShellTracker instead of processStates
1 parent 65378e3 commit 3dca767

File tree

4 files changed

+21
-33
lines changed

4 files changed

+21
-33
lines changed

packages/agent/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export * from './tools/system/sequenceComplete.js';
1010
export * from './tools/system/shellMessage.js';
1111
export * from './tools/system/shellExecute.js';
1212
export * from './tools/system/listBackgroundTools.js';
13+
export * from './tools/system/listShells.js';
14+
export * from './tools/system/ShellTracker.js';
1315

1416
// Tools - Browser
1517
export * from './tools/browser/BrowserManager.js';

packages/agent/src/tools/system/ShellTracker.test.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,19 @@ describe('ShellTracker', () => {
104104
// Get running shells
105105
const runningShells = shellTracker.getShells(ShellStatus.RUNNING);
106106
expect(runningShells.length).toBe(1);
107-
expect(runningShells[0].id).toBe('shell-1');
107+
expect(runningShells.length).toBe(1);
108+
expect(runningShells[0]!.id).toBe('shell-1');
108109

109110
// Get completed shells
110111
const completedShells = shellTracker.getShells(ShellStatus.COMPLETED);
111112
expect(completedShells.length).toBe(1);
112-
expect(completedShells[0].id).toBe('shell-2');
113+
expect(completedShells.length).toBe(1);
114+
expect(completedShells[0]!.id).toBe('shell-2');
113115

114116
// Get error shells
115117
const errorShells = shellTracker.getShells(ShellStatus.ERROR);
116118
expect(errorShells.length).toBe(1);
117-
expect(errorShells[0].id).toBe('shell-3');
119+
expect(errorShells.length).toBe(1);
120+
expect(errorShells[0]!.id).toBe('shell-3');
118121
});
119122
});

packages/agent/src/tools/system/listShells.test.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ describe('listShellsTool', () => {
8181

8282
expect(result.shells.length).toBe(1);
8383
expect(result.count).toBe(1);
84-
expect(result.shells[0].id).toBe('shell-1');
85-
expect(result.shells[0].status).toBe(ShellStatus.RUNNING);
84+
expect(result.shells.length).toBe(1);
85+
expect(result.shells[0]!.id).toBe('shell-1');
86+
expect(result.shells[0]!.status).toBe(ShellStatus.RUNNING);
8687
});
8788

8889
it('should include metadata when verbose is true', async () => {
@@ -105,9 +106,10 @@ describe('listShellsTool', () => {
105106
);
106107

107108
expect(result.shells.length).toBe(1);
108-
expect(result.shells[0].id).toBe('shell-3');
109-
expect(result.shells[0].status).toBe(ShellStatus.ERROR);
110-
expect(result.shells[0].metadata).toBeDefined();
111-
expect(result.shells[0].metadata?.error).toBe('Command not found');
109+
expect(result.shells.length).toBe(1);
110+
expect(result.shells[0]!.id).toBe('shell-3');
111+
expect(result.shells[0]!.status).toBe(ShellStatus.ERROR);
112+
expect(result.shells[0]!.metadata).toBeDefined();
113+
expect(result.shells[0]!.metadata?.error).toBe('Command not found');
112114
});
113115
});

packages/cli/src/utils/cleanup.ts

+5-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BrowserManager, processStates } from 'mycoder-agent';
1+
import { BrowserManager, shellTracker } from 'mycoder-agent';
22
import { agentStates } from 'mycoder-agent/dist/tools/interaction/agentStart.js';
33

44
/**
@@ -55,29 +55,10 @@ export async function cleanupResources(): Promise<void> {
5555

5656
// 2. Clean up shell processes
5757
try {
58-
if (processStates.size > 0) {
59-
console.log(`Terminating ${processStates.size} shell processes...`);
60-
for (const [id, state] of processStates.entries()) {
61-
if (!state.state.completed) {
62-
console.log(`Terminating process ${id}...`);
63-
try {
64-
state.process.kill('SIGTERM');
65-
// Force kill after a short timeout if still running
66-
setTimeout(() => {
67-
try {
68-
if (!state.state.completed) {
69-
state.process.kill('SIGKILL');
70-
}
71-
// eslint-disable-next-line unused-imports/no-unused-vars
72-
} catch (e) {
73-
// Ignore errors on forced kill
74-
}
75-
}, 500);
76-
} catch (e) {
77-
console.error(`Error terminating process ${id}:`, e);
78-
}
79-
}
80-
}
58+
const runningShells = shellTracker.getShells();
59+
if (runningShells.length > 0) {
60+
console.log(`Terminating ${runningShells.length} shell processes...`);
61+
await shellTracker.cleanupAllShells();
8162
}
8263
} catch (error) {
8364
console.error('Error terminating shell processes:', error);

0 commit comments

Comments
 (0)