@@ -4,11 +4,13 @@ import path from 'node:path';
44import { afterEach , test , vi } from 'vitest' ;
55import { AppError } from '@agent-device/kernel/errors' ;
66import type { ExecDetachedExit } from '@agent-device/host-kit/command' ;
7+ import { DAEMON_STARTUP_EXIT_CODES } from '@agent-device/daemon-contracts/daemon-registration-owner' ;
78import {
8- DAEMON_STARTUP_EXIT_CODES ,
9+ readDaemonStartupPhaseMarkers ,
910 recordDaemonStartupMarker ,
1011 resolveDaemonStartupPhasesPath ,
11- } from '@agent-device/daemon-contracts/daemon-registration-owner' ;
12+ type DaemonStartupPhase ,
13+ } from '@agent-device/daemon-contracts/daemon-startup-phases' ;
1214import {
1315 resolveDaemonPaths ,
1416 type DaemonPaths ,
@@ -134,9 +136,20 @@ async function awaitFile(file: string) {
134136
135137test ( 'a daemon slower than the retired 15 s budget starts successfully while it keeps progressing' , async ( t ) => {
136138 if ( ! ( await supportsLoopbackBind ( ) ) ) return t . skip ( 'loopback unavailable' ) ;
139+ vi . stubEnv ( 'AGENT_DEVICE_DAEMON_STARTUP_TIMEOUT_MS' , '150000' ) ;
137140 const http = await startHttpDaemonFixture ( { devices : [ ] } ) ;
138141 const paths = resolveDaemonPaths ( mkdtempForTestSync ( 'daemon-start-slow-success-' ) ) ;
139- // Virtual time waits ~20 s past the retired 15 s deadline; real markers hold off the stall.
142+ // Virtual time runs past the 60 s stall window with the ceiling above the span, so only the
143+ // successive phase markers keep this wait alive: each phase is new, so the last observed
144+ // progress never goes 60 s stale, while the same span with no readable markers fails as
145+ // startup-stalled. A repeated rewrite of one phase would not advance progress, because the
146+ // reader keeps each phase's earliest timestamp.
147+ const phaseSchedule : Array < readonly [ DaemonStartupPhase , number ] > = [
148+ [ 'modules-loaded' , 10_000 ] ,
149+ [ 'composition-loaded' , 25_000 ] ,
150+ [ 'lock-acquired' , 40_000 ] ,
151+ [ 'recovery-finished' , 70_000 ] ,
152+ ] ;
140153 const startedAt = Date . now ( ) ;
141154 const now = { value : startedAt } ;
142155 vi . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => now . value ) ;
@@ -149,21 +162,25 @@ test('a daemon slower than the retired 15 s budget starts successfully while it
149162 return child ;
150163 } ) ;
151164 fs . writeFileSync ( path . join ( paths . baseDir , 'defer-publication' ) , 'wait' ) ;
152- let polls = 0 ;
165+ let nextPhase = 0 ;
153166 pause . mockImplementation ( async ( ) => {
154- polls += 1 ;
155167 if ( ! booted ) {
156168 booted = fs . existsSync ( path . join ( paths . baseDir , 'registration-held' ) ) ;
157169 if ( ! booted ) return await realPause ( ) ;
158170 }
159- if ( now . value - spawned . at < 20_000 ) {
171+ if ( spawned . child && now . value - spawned . at < 70_000 ) {
160172 now . value += 2_000 ;
161- if ( polls % 3 === 0 && spawned . child )
173+ while (
174+ nextPhase < phaseSchedule . length &&
175+ now . value - spawned . at >= phaseSchedule [ nextPhase ] ! [ 1 ]
176+ ) {
162177 recordDaemonStartupMarker (
163178 resolveDaemonStartupPhasesPath ( paths . baseDir , spawned . child . pid ) ,
164- 'lock-acquired' ,
179+ phaseSchedule [ nextPhase ] ! [ 0 ] ,
165180 ) ;
166- if ( now . value - spawned . at >= 20_000 )
181+ nextPhase += 1 ;
182+ }
183+ if ( nextPhase === phaseSchedule . length )
167184 fs . rmSync ( path . join ( paths . baseDir , 'defer-publication' ) , { force : true } ) ;
168185 return await realPause ( ) ;
169186 }
@@ -172,13 +189,27 @@ test('a daemon slower than the retired 15 s budget starts successfully while it
172189 try {
173190 const response = await sendToDaemon ( request ( paths ) ) ;
174191 assert . equal ( response . ok , true ) ;
192+ const elapsed = now . value - spawned . at ;
175193 assert . ok (
176- now . value - spawned . at >= 20_000 ,
177- ' the startup this test proves must exceed the retired 15 s budget' ,
194+ elapsed >= 60_000 ,
195+ ` the startup this test proves must outlast the stall window ( ${ elapsed } ms)` ,
178196 ) ;
179197 assert . equal ( spawn . mock . calls . length , 1 ) ;
180198 assert . equal ( http . rpcRequests . length , 1 ) ;
181199 assert . ok ( spawned . child && isProcessAlive ( spawned . child . pid ) , 'the client must not retire it' ) ;
200+ assert . ok ( spawned . child ) ;
201+ // The wait survived the stall window on distinct successive phases, not on one repeated
202+ // marker: every scheduled phase is in this launch's own record.
203+ const markers = readDaemonStartupPhaseMarkers (
204+ fs . readFileSync ( resolveDaemonStartupPhasesPath ( paths . baseDir , spawned . child . pid ) , 'utf8' ) ,
205+ ) ;
206+ for ( const [ phase ] of phaseSchedule ) {
207+ assert . equal (
208+ typeof markers [ phase ] ,
209+ 'number' ,
210+ `the observed progress must include the ${ phase } phase` ,
211+ ) ;
212+ }
182213 } finally {
183214 await closeLoopbackServer ( http . server ) ;
184215 }
0 commit comments