@@ -30,11 +30,10 @@ import {
3030 agentBrowserChromeLaunchMarker ,
3131 appendAgentDeviceChromeArgs ,
3232 cleanupManagedAgentBrowserOrphans ,
33- matchAgentBrowserChromeProcess ,
3433 resolveAgentBrowserIdleTimeoutMs ,
35- summarizeManagedAgentBrowserProcesses ,
3634 summarizeAgentBrowserProcesses ,
3735} from './agent-browser-lifecycle.ts' ;
36+ import { managedInstallPaths } from './agent-browser-install-paths.ts' ;
3837import { recordManagedAgentBrowserProcesses } from './agent-browser-process-record.ts' ;
3938import { getManagedAgentBrowserStatus } from './agent-browser-tool.ts' ;
4039
@@ -87,6 +86,13 @@ test('managed agent-browser idle timeout defaults to five minutes and respects o
8786 ) ;
8887} ) ;
8988
89+ function matchChrome (
90+ processInfo : { pid : number ; command : string } ,
91+ status : ReturnType < typeof getManagedAgentBrowserStatus > ,
92+ ) {
93+ return summarizeAgentBrowserProcesses ( [ processInfo ] , status ) . processes [ 0 ] ;
94+ }
95+
9096test ( 'ownership detection accepts only Chrome-like processes with managed markers' , ( ) => {
9197 const stateDir = mkdtempForTestSync ( 'agent-device-web-life-' ) ;
9298 try {
@@ -104,30 +110,24 @@ test('ownership detection accepts only Chrome-like processes with managed marker
104110 'Google Chrome for Testing' ,
105111 ) ;
106112
107- const markerMatch = matchAgentBrowserChromeProcess (
113+ const markerMatch = matchChrome (
108114 { pid : 11 , command : `/Applications/Chromium.app/Contents/MacOS/Chromium ${ marker } ` } ,
109115 status ,
110116 ) ;
111- const homeMatch = matchAgentBrowserChromeProcess (
112- { pid : 12 , command : `${ managedChrome } --type=renderer` } ,
113- status ,
114- ) ;
115- const userChrome = matchAgentBrowserChromeProcess (
117+ const homeMatch = matchChrome ( { pid : 12 , command : `${ managedChrome } --type=renderer` } , status ) ;
118+ const userChrome = matchChrome (
116119 {
117120 pid : 13 ,
118121 command :
119122 '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome --profile-directory=Default' ,
120123 } ,
121124 status ,
122125 ) ;
123- const broadRuntimeHome = matchAgentBrowserChromeProcess (
126+ const broadRuntimeHome = matchChrome (
124127 { pid : 15 , command : `${ status . runtimeHomeDir } /profile-cache/chrome-helper` } ,
125128 status ,
126129 ) ;
127- const markedNonBrowser = matchAgentBrowserChromeProcess (
128- { pid : 14 , command : `/bin/sh -c echo ${ marker } ` } ,
129- status ,
130- ) ;
130+ const markedNonBrowser = matchChrome ( { pid : 14 , command : `/bin/sh -c echo ${ marker } ` } , status ) ;
131131
132132 assert . equal ( markerMatch ?. reason , 'launch-marker' ) ;
133133 assert . equal ( homeMatch ?. reason , 'managed-browser-home' ) ;
@@ -320,18 +320,102 @@ test('records the managed browser daemon and Chrome fleet at the spawn owner', a
320320 }
321321} ) ;
322322
323- test ( 'managed process summary includes the recordable agent-browser daemon' , ( ) => {
323+ function mockProcessList ( lines : string [ ] ) {
324+ mockRunCmd . mockResolvedValue ( { stdout : lines . join ( '\n' ) , stderr : '' , exitCode : 0 } ) ;
325+ }
326+
327+ test ( 'recording the fleet owns the recordable agent-browser daemon' , async ( ) => {
328+ const stateDir = mkdtempForTestSync ( 'agent-device-web-life-' ) ;
329+ try {
330+ installFakeManagedAgentBrowser ( stateDir ) ;
331+ const status = getManagedAgentBrowserStatus ( { stateDir } ) ;
332+ const command = `${ process . execPath } ${ status . entryScript } ` ;
333+ const store = { replace : vi . fn ( ) , clear : vi . fn ( ) , read : vi . fn ( ( ) => [ ] ) } ;
334+ mockProcessList ( [ ` 101 1 ${ command } ` ] ) ;
335+
336+ const summary = await recordManagedAgentBrowserProcesses ( status , store ) ;
337+
338+ expect ( summary . processes [ 0 ] ?. reason ) . toBe ( 'agent-browser-daemon' ) ;
339+ expect ( store . replace ) . toHaveBeenCalledWith ( { kind : 'daemon' } , [
340+ { pid : 101 , startTime : 'start-101' , command : 'command-101' , purpose : 'managed-web-browser' } ,
341+ ] ) ;
342+ } finally {
343+ fs . rmSync ( stateDir , { recursive : true , force : true } ) ;
344+ }
345+ } ) ;
346+
347+ function installPreviousVersionFleet ( stateDir : string ) {
348+ const previous = managedInstallPaths ( stateDir , '0.27.1' ) ;
349+ fs . mkdirSync ( previous . installDir , { recursive : true } ) ;
350+ const entryScript = path . join (
351+ previous . installDir ,
352+ 'package' ,
353+ 'node_modules' ,
354+ 'agent-browser' ,
355+ 'bin' ,
356+ 'agent-browser.js' ,
357+ ) ;
358+ return {
359+ previous,
360+ daemonCommand : `${ process . execPath } ${ entryScript } ` ,
361+ markedChromeCommand : `/Applications/Chromium.app/Contents/MacOS/Chromium ${ agentBrowserChromeLaunchMarker ( previous ) } ` ,
362+ homeChromeCommand : `${ path . join ( previous . runtimeHomeDir , '.agent-browser' , 'browsers' ) } /chrome-150/chrome --type=renderer` ,
363+ } ;
364+ }
365+
366+ test ( 'a fleet left running by a previous version stays owned after the pin moves' , async ( ) => {
367+ const stateDir = mkdtempForTestSync ( 'agent-device-web-life-' ) ;
368+ try {
369+ installFakeManagedAgentBrowser ( stateDir ) ;
370+ const status = getManagedAgentBrowserStatus ( { stateDir } ) ;
371+ const { previous, daemonCommand, markedChromeCommand, homeChromeCommand } =
372+ installPreviousVersionFleet ( stateDir ) ;
373+ assert . notEqual ( previous . installDir , status . installDir ) ;
374+ const store = { replace : vi . fn ( ) , clear : vi . fn ( ) , read : vi . fn ( ( ) => [ ] ) } ;
375+ mockProcessList ( [
376+ ` 101 1 ${ daemonCommand } ` ,
377+ ` 201 1 ${ markedChromeCommand } ` ,
378+ ` 202 1 ${ homeChromeCommand } ` ,
379+ ' 999 1 /Applications/Google Chrome.app/Contents/MacOS/Google Chrome' ,
380+ ] ) ;
381+
382+ const summary = await recordManagedAgentBrowserProcesses ( status , store ) ;
383+
384+ assert . deepEqual ( summary . pids , [ 101 , 201 , 202 ] ) ;
385+ assert . deepEqual (
386+ summary . processes . map ( ( { reason } ) => reason ) ,
387+ [ 'agent-browser-daemon' , 'launch-marker' , 'managed-browser-home' ] ,
388+ ) ;
389+ expect ( store . replace ) . toHaveBeenCalledWith (
390+ { kind : 'daemon' } ,
391+ [ 101 , 201 , 202 ] . map ( ( pid ) => ( {
392+ pid,
393+ startTime : `start-${ pid } ` ,
394+ command : `command-${ pid } ` ,
395+ purpose : 'managed-web-browser' ,
396+ } ) ) ,
397+ ) ;
398+ } finally {
399+ fs . rmSync ( stateDir , { recursive : true , force : true } ) ;
400+ }
401+ } ) ;
402+
403+ test ( 'a previous version fleet from another state dir is not claimed' , async ( ) => {
324404 const stateDir = mkdtempForTestSync ( 'agent-device-web-life-' ) ;
405+ const otherStateDir = mkdtempForTestSync ( 'agent-device-web-life-other-' ) ;
325406 try {
326407 installFakeManagedAgentBrowser ( stateDir ) ;
327408 const status = getManagedAgentBrowserStatus ( { stateDir } ) ;
328- expect (
329- summarizeManagedAgentBrowserProcesses (
330- [ { pid : 101 , command : `${ process . execPath } ${ status . entryScript } ` } ] ,
331- status ,
332- ) . processes [ 0 ] ?. reason ,
333- ) . toBe ( 'agent-browser-daemon' ) ;
409+ const { daemonCommand, markedChromeCommand } = installPreviousVersionFleet ( otherStateDir ) ;
410+ const store = { replace : vi . fn ( ) , clear : vi . fn ( ) , read : vi . fn ( ( ) => [ ] ) } ;
411+ mockProcessList ( [ ` 101 1 ${ daemonCommand } ` , ` 201 1 ${ markedChromeCommand } ` ] ) ;
412+
413+ const summary = await recordManagedAgentBrowserProcesses ( status , store ) ;
414+
415+ assert . equal ( summary . count , 0 ) ;
416+ expect ( store . replace ) . not . toHaveBeenCalled ( ) ;
334417 } finally {
335418 fs . rmSync ( stateDir , { recursive : true , force : true } ) ;
419+ fs . rmSync ( otherStateDir , { recursive : true , force : true } ) ;
336420 }
337421} ) ;
0 commit comments