@@ -3,11 +3,10 @@ import { v4 as uuidv4 } from 'uuid';
33// These imports will be used by the cleanup method
44import { BrowserManager } from '../tools/browser/BrowserManager.js' ;
55import { agentStates } from '../tools/interaction/agentStart.js' ;
6- import { processStates } from '../tools/system/shellStart .js' ;
6+ import { shellTracker } from '../tools/system/ShellTracker .js' ;
77
88// Types of background processes we can track
99export enum BackgroundToolType {
10- SHELL = 'shell' ,
1110 BROWSER = 'browser' ,
1211 AGENT = 'agent' ,
1312}
@@ -30,17 +29,6 @@ export interface BackgroundTool {
3029 metadata : Record < string , any > ; // Additional tool-specific information
3130}
3231
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-
4432// Browser process specific data
4533export interface BrowserBackgroundTool extends BackgroundTool {
4634 type : BackgroundToolType . BROWSER ;
@@ -60,10 +48,7 @@ export interface AgentBackgroundTool extends BackgroundTool {
6048}
6149
6250// Utility type for all background tool types
63- export type AnyBackgroundTool =
64- | ShellBackgroundTool
65- | BrowserBackgroundTool
66- | AgentBackgroundTool ;
51+ export type AnyBackgroundTool = BrowserBackgroundTool | AgentBackgroundTool ;
6752
6853/**
6954 * Registry to keep track of all background processes
@@ -74,22 +59,6 @@ export class BackgroundTools {
7459 // Private constructor for singleton pattern
7560 constructor ( readonly ownerName : string ) { }
7661
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-
9362 // Register a new browser process
9463 public registerBrowser ( url ?: string ) : string {
9564 const id = uuidv4 ( ) ;
@@ -177,12 +146,6 @@ export class BackgroundTools {
177146 tool . status === BackgroundToolStatus . RUNNING ,
178147 ) ;
179148
180- const shellTools = tools . filter (
181- ( tool ) : tool is ShellBackgroundTool =>
182- tool . type === BackgroundToolType . SHELL &&
183- tool . status === BackgroundToolStatus . RUNNING ,
184- ) ;
185-
186149 const agentTools = tools . filter (
187150 ( tool ) : tool is AgentBackgroundTool =>
188151 tool . type === BackgroundToolType . AGENT &&
@@ -193,19 +156,15 @@ export class BackgroundTools {
193156 const browserCleanupPromises = browserTools . map ( ( tool ) =>
194157 this . cleanupBrowserSession ( tool ) ,
195158 ) ;
196- const shellCleanupPromises = shellTools . map ( ( tool ) =>
197- this . cleanupShellProcess ( tool ) ,
198- ) ;
199159 const agentCleanupPromises = agentTools . map ( ( tool ) =>
200160 this . cleanupSubAgent ( tool ) ,
201161 ) ;
202162
163+ // Clean up shell processes using ShellTracker
164+ await shellTracker . cleanupAllShells ( ) ;
165+
203166 // 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 ] ) ;
209168 }
210169
211170 /**
@@ -230,38 +189,6 @@ export class BackgroundTools {
230189 }
231190 }
232191
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-
265192 /**
266193 * Cleans up a sub-agent
267194 * @param tool The agent tool to clean up
0 commit comments