@@ -3,11 +3,10 @@ import { v4 as uuidv4 } from 'uuid';
3
3
// These imports will be used by the cleanup method
4
4
import { BrowserManager } from '../tools/browser/BrowserManager.js' ;
5
5
import { agentStates } from '../tools/interaction/agentStart.js' ;
6
- import { processStates } from '../tools/system/shellStart .js' ;
6
+ import { shellTracker } from '../tools/system/ShellTracker .js' ;
7
7
8
8
// Types of background processes we can track
9
9
export enum BackgroundToolType {
10
- SHELL = 'shell' ,
11
10
BROWSER = 'browser' ,
12
11
AGENT = 'agent' ,
13
12
}
@@ -30,17 +29,6 @@ export interface BackgroundTool {
30
29
metadata : Record < string , any > ; // Additional tool-specific information
31
30
}
32
31
33
- // Shell process specific data
34
- export interface ShellBackgroundTool extends BackgroundTool {
35
- type : BackgroundToolType . SHELL ;
36
- metadata : {
37
- command : string ;
38
- exitCode ?: number | null ;
39
- signaled ?: boolean ;
40
- error ?: string ;
41
- } ;
42
- }
43
-
44
32
// Browser process specific data
45
33
export interface BrowserBackgroundTool extends BackgroundTool {
46
34
type : BackgroundToolType . BROWSER ;
@@ -60,10 +48,7 @@ export interface AgentBackgroundTool extends BackgroundTool {
60
48
}
61
49
62
50
// Utility type for all background tool types
63
- export type AnyBackgroundTool =
64
- | ShellBackgroundTool
65
- | BrowserBackgroundTool
66
- | AgentBackgroundTool ;
51
+ export type AnyBackgroundTool = BrowserBackgroundTool | AgentBackgroundTool ;
67
52
68
53
/**
69
54
* Registry to keep track of all background processes
@@ -74,22 +59,6 @@ export class BackgroundTools {
74
59
// Private constructor for singleton pattern
75
60
constructor ( readonly ownerName : string ) { }
76
61
77
- // Register a new shell process
78
- public registerShell ( command : string ) : string {
79
- const id = uuidv4 ( ) ;
80
- const tool : ShellBackgroundTool = {
81
- id,
82
- type : BackgroundToolType . SHELL ,
83
- status : BackgroundToolStatus . RUNNING ,
84
- startTime : new Date ( ) ,
85
- metadata : {
86
- command,
87
- } ,
88
- } ;
89
- this . tools . set ( id , tool ) ;
90
- return id ;
91
- }
92
-
93
62
// Register a new browser process
94
63
public registerBrowser ( url ?: string ) : string {
95
64
const id = uuidv4 ( ) ;
@@ -177,12 +146,6 @@ export class BackgroundTools {
177
146
tool . status === BackgroundToolStatus . RUNNING ,
178
147
) ;
179
148
180
- const shellTools = tools . filter (
181
- ( tool ) : tool is ShellBackgroundTool =>
182
- tool . type === BackgroundToolType . SHELL &&
183
- tool . status === BackgroundToolStatus . RUNNING ,
184
- ) ;
185
-
186
149
const agentTools = tools . filter (
187
150
( tool ) : tool is AgentBackgroundTool =>
188
151
tool . type === BackgroundToolType . AGENT &&
@@ -193,19 +156,15 @@ export class BackgroundTools {
193
156
const browserCleanupPromises = browserTools . map ( ( tool ) =>
194
157
this . cleanupBrowserSession ( tool ) ,
195
158
) ;
196
- const shellCleanupPromises = shellTools . map ( ( tool ) =>
197
- this . cleanupShellProcess ( tool ) ,
198
- ) ;
199
159
const agentCleanupPromises = agentTools . map ( ( tool ) =>
200
160
this . cleanupSubAgent ( tool ) ,
201
161
) ;
202
162
163
+ // Clean up shell processes using ShellTracker
164
+ await shellTracker . cleanupAllShells ( ) ;
165
+
203
166
// Wait for all cleanup operations to complete in parallel
204
- await Promise . all ( [
205
- ...browserCleanupPromises ,
206
- ...shellCleanupPromises ,
207
- ...agentCleanupPromises ,
208
- ] ) ;
167
+ await Promise . all ( [ ...browserCleanupPromises , ...agentCleanupPromises ] ) ;
209
168
}
210
169
211
170
/**
@@ -230,38 +189,6 @@ export class BackgroundTools {
230
189
}
231
190
}
232
191
233
- /**
234
- * Cleans up a shell process
235
- * @param tool The shell tool to clean up
236
- */
237
- private async cleanupShellProcess ( tool : ShellBackgroundTool ) : Promise < void > {
238
- try {
239
- const processState = processStates . get ( tool . id ) ;
240
- if ( processState && ! processState . state . completed ) {
241
- processState . process . kill ( 'SIGTERM' ) ;
242
-
243
- // Force kill after a short timeout if still running
244
- await new Promise < void > ( ( resolve ) => {
245
- setTimeout ( ( ) => {
246
- try {
247
- if ( ! processState . state . completed ) {
248
- processState . process . kill ( 'SIGKILL' ) ;
249
- }
250
- } catch {
251
- // Ignore errors on forced kill
252
- }
253
- resolve ( ) ;
254
- } , 500 ) ;
255
- } ) ;
256
- }
257
- this . updateToolStatus ( tool . id , BackgroundToolStatus . COMPLETED ) ;
258
- } catch ( error ) {
259
- this . updateToolStatus ( tool . id , BackgroundToolStatus . ERROR , {
260
- error : error instanceof Error ? error . message : String ( error ) ,
261
- } ) ;
262
- }
263
- }
264
-
265
192
/**
266
193
* Cleans up a sub-agent
267
194
* @param tool The agent tool to clean up
0 commit comments