@@ -62,51 +62,31 @@ export function matchAgentBrowserChromeProcess(
6262 processInfo : HostProcessInfo ,
6363 status : AgentBrowserToolStatus ,
6464) : AgentBrowserProcessMatch | undefined {
65- if ( processInfo . pid === hostProcessId ( ) || ! isChromeLikeCommand ( processInfo . command ) )
66- return undefined ;
67- const installs = managedInstalls ( status ) ;
68- if (
69- installs . some ( ( install ) =>
70- processInfo . command . includes ( agentBrowserChromeLaunchMarker ( install ) ) ,
71- )
72- ) {
73- return { process : processInfo , reason : 'launch-marker' } ;
74- }
75- if (
76- installs
77- . flatMap ( managedBrowserHomeMarkers )
78- . some ( ( marker ) => isCommandUnderPath ( processInfo , marker ) )
79- ) {
80- return { process : processInfo , reason : 'managed-browser-home' } ;
81- }
82- return undefined ;
65+ return matchChromeProcess ( processInfo , managedFleetOwnership ( status ) ) ;
8366}
8467
8568export function summarizeAgentBrowserProcesses (
8669 processes : HostProcessInfo [ ] ,
8770 status : AgentBrowserToolStatus ,
8871) : AgentBrowserProcessSummary {
89- return summarizeMatches ( processes , ( processInfo ) =>
90- matchAgentBrowserChromeProcess ( processInfo , status ) ,
91- ) ;
72+ const ownership = managedFleetOwnership ( status ) ;
73+ return summarizeMatches ( processes , ( processInfo ) => matchChromeProcess ( processInfo , ownership ) ) ;
9274}
9375
9476function matchManagedAgentBrowserProcess (
9577 processInfo : HostProcessInfo ,
96- status : AgentBrowserToolStatus ,
78+ ownership : ManagedFleetOwnership ,
9779) : AgentBrowserProcessMatch | undefined {
98- return (
99- matchAgentBrowserChromeProcess ( processInfo , status ) ??
100- matchAgentBrowserDaemonProcess ( processInfo , status )
101- ) ;
80+ return matchChromeProcess ( processInfo , ownership ) ?? matchDaemonProcess ( processInfo , ownership ) ;
10281}
10382
10483export function summarizeManagedAgentBrowserProcesses (
10584 processes : HostProcessInfo [ ] ,
10685 status : AgentBrowserToolStatus ,
10786) : AgentBrowserProcessSummary {
87+ const ownership = managedFleetOwnership ( status ) ;
10888 return summarizeMatches ( processes , ( processInfo ) =>
109- matchManagedAgentBrowserProcess ( processInfo , status ) ,
89+ matchManagedAgentBrowserProcess ( processInfo , ownership ) ,
11090 ) ;
11191}
11292
@@ -121,14 +101,17 @@ export async function recordManagedAgentBrowserProcesses(
121101 store : OwnedProcessRecordStore ,
122102) : Promise < AgentBrowserProcessSummary > {
123103 const processes = await listRealHostProcesses ( ) ;
124- const roots = summarizeManagedAgentBrowserProcesses ( processes , status ) ;
104+ const ownership = managedFleetOwnership ( status ) ;
105+ const roots = summarizeMatches ( processes , ( processInfo ) =>
106+ matchManagedAgentBrowserProcess ( processInfo , ownership ) ,
107+ ) ;
125108 const ownedProcesses = expandProcessTree ( roots . pids , processes ) ;
126109 const summary : AgentBrowserProcessSummary = {
127110 count : ownedProcesses . length ,
128111 pids : ownedProcesses . map ( ( { pid } ) => pid ) ,
129112 processes : ownedProcesses . map ( ( process ) => ( {
130113 process,
131- reason : matchManagedAgentBrowserProcess ( process , status ) ?. reason ?? 'descendant' ,
114+ reason : matchManagedAgentBrowserProcess ( process , ownership ) ?. reason ?? 'descendant' ,
132115 } ) ) ,
133116 } ;
134117 const records : OwnedProcessRecord [ ] = summary . processes . flatMap ( ( { process : processInfo } ) => {
@@ -222,38 +205,65 @@ function isChromeLikeCommand(command: string): boolean {
222205 ) ;
223206}
224207
225- function matchAgentBrowserDaemonProcess (
208+ /**
209+ * What marks a process as part of the managed fleet: the current install plus every sibling
210+ * version directory under the same state dir, so a fleet left running by a previous pin still
211+ * matches after an upgrade moves the current install.
212+ */
213+ type ManagedFleetOwnership = {
214+ launchMarkers : string [ ] ;
215+ browserHomeRoots : string [ ] ;
216+ installRoots : string [ ] ;
217+ } ;
218+
219+ function managedFleetOwnership ( status : AgentBrowserToolStatus ) : ManagedFleetOwnership {
220+ const siblings = listManagedInstallPaths ( status . stateDir ) . filter (
221+ ( install ) => path . resolve ( install . installDir ) !== path . resolve ( status . installDir ) ,
222+ ) ;
223+ const installs : ManagedInstallPaths [ ] = [ status , ...siblings ] ;
224+ return {
225+ launchMarkers : installs . map ( agentBrowserChromeLaunchMarker ) ,
226+ browserHomeRoots : uniqueStrings ( installs . flatMap ( managedBrowserHomeMarkers ) ) ,
227+ installRoots : installs . map (
228+ ( install ) => `${ normalizePathSeparators ( path . resolve ( install . installDir ) ) } /` ,
229+ ) ,
230+ } ;
231+ }
232+
233+ function matchChromeProcess (
226234 processInfo : HostProcessInfo ,
227- status : AgentBrowserToolStatus ,
235+ ownership : ManagedFleetOwnership ,
236+ ) : AgentBrowserProcessMatch | undefined {
237+ if ( processInfo . pid === hostProcessId ( ) || ! isChromeLikeCommand ( processInfo . command ) )
238+ return undefined ;
239+ if ( ownership . launchMarkers . some ( ( marker ) => processInfo . command . includes ( marker ) ) ) {
240+ return { process : processInfo , reason : 'launch-marker' } ;
241+ }
242+ if ( ownership . browserHomeRoots . some ( ( root ) => isCommandUnderPath ( processInfo , root ) ) ) {
243+ return { process : processInfo , reason : 'managed-browser-home' } ;
244+ }
245+ return undefined ;
246+ }
247+
248+ function matchDaemonProcess (
249+ processInfo : HostProcessInfo ,
250+ ownership : ManagedFleetOwnership ,
228251) : AgentBrowserProcessMatch | undefined {
229252 if ( processInfo . pid === hostProcessId ( ) || isChromeLikeCommand ( processInfo . command ) )
230253 return undefined ;
231254 const command = normalizePathSeparators ( processInfo . command ) ;
232- return managedInstalls ( status ) . some ( ( install ) =>
233- command . includes ( `${ normalizePathSeparators ( path . resolve ( install . installDir ) ) } /` ) ,
234- )
255+ return ownership . installRoots . some ( ( root ) => command . includes ( root ) )
235256 ? { process : processInfo , reason : 'agent-browser-daemon' }
236257 : undefined ;
237258}
238259
239- /**
240- * The current install plus every sibling version directory under the same state dir, so a fleet
241- * left running by a previous pin still matches after an upgrade moves the current install.
242- */
243- function managedInstalls ( status : AgentBrowserToolStatus ) : ManagedInstallPaths [ ] {
244- const siblings = listManagedInstallPaths ( status . stateDir ) . filter (
245- ( install ) => path . resolve ( install . installDir ) !== path . resolve ( status . installDir ) ,
246- ) ;
247- return [ status , ...siblings ] ;
248- }
249-
250260function managedBrowserHomeMarkers (
251261 status : Pick < ManagedInstallPaths , 'homeDir' | 'runtimeHomeDir' > ,
252262) : string [ ] {
253- return uniqueStrings ( [
263+ return [
254264 path . join ( status . homeDir , '.agent-browser' , 'browsers' ) ,
255265 path . join ( status . runtimeHomeDir , '.agent-browser' , 'browsers' ) ,
256- ] ) ;
266+ ] ;
257267}
258268
259269function isCommandUnderPath ( processInfo : HostProcessInfo , rootPath : string ) : boolean {
0 commit comments