@@ -66,9 +66,11 @@ export type EnsuredDaemon = {
6666type DaemonStartupWaitResult =
6767 | { kind : 'ready' ; daemon : EnsuredDaemon }
6868 | { kind : 'early_exit' ; exit : ExecDetachedExit }
69- | { kind : 'retry' | 'unproven' | 'timeout' } ;
69+ | { kind : 'unproven' ; error : AppError }
70+ | { kind : 'retry' | 'timeout' } ;
7071
7172const DAEMON_STARTUP_TIMEOUT_MS = 15_000 ;
73+ const MINIMUM_DAEMON_TAKEOVER_BUDGET_MS = 5_000 ;
7274const LIVE_DAEMON_PROBE_RETRIES = 3 ;
7375const LIVE_DAEMON_PROBE_RETRY_DELAY_MS = 200 ;
7476const DAEMON_STARTUP_ATTEMPTS = 2 ;
@@ -191,16 +193,10 @@ async function readReusableLocalDaemon(
191193 deadline ?: number ,
192194) : Promise < DaemonInfo | null > {
193195 const inspection = inspectProcessLock ( settings . paths . lockPath ) ;
194- if ( inspection . state === 'unproven' ) {
195- throw new AppError ( 'COMMAND_FAILED' , 'Daemon registration ownership could not be verified.' , {
196- reason : 'daemon_registration_unproven' ,
197- inspection,
198- stateDir : settings . paths . baseDir ,
199- hint : resolveDaemonStartupHint ( getDaemonMetadataState ( settings . paths ) , settings . paths ) ,
200- } ) ;
201- }
196+ if ( inspection . state === 'unproven' ) throw daemonRegistrationUnprovenError ( settings , inspection ) ;
202197 const existing = readDaemonInfo ( settings . paths . infoPath ) ;
203198 if ( ! existing ) return null ;
199+ if ( ! registrationAllowsDaemonObservation ( inspection , existing ) ) return null ;
204200
205201 const decision = await resolveDaemonTakeover ( existing , {
206202 onClientTransport : ( ) =>
@@ -212,12 +208,36 @@ async function readReusableLocalDaemon(
212208 throw newerDaemonRefusedError ( existing , decision , settings . paths . baseDir ) ;
213209 }
214210
215- if ( deadline !== undefined && Date . now ( ) >= deadline ) return null ;
211+ if ( remainingStartupBudget ( deadline ) < MINIMUM_DAEMON_TAKEOVER_BUDGET_MS ) return null ;
216212 emitDaemonTakeoverNotice ( existing , decision . reason , settings . paths . baseDir ) ;
217213 await retireDaemonForTakeover ( existing , settings . paths ) ;
218214 return null ;
219215}
220216
217+ function registrationAllowsDaemonObservation (
218+ inspection : ProcessLockInspection ,
219+ info : DaemonInfo ,
220+ ) : boolean {
221+ return (
222+ inspection . state === 'absent' ||
223+ ( inspection . state === 'held' &&
224+ inspection . owner . pid === info . pid &&
225+ inspection . owner . startTime === ( info . processStartTime ?? null ) )
226+ ) ;
227+ }
228+
229+ function daemonRegistrationUnprovenError (
230+ settings : DaemonClientSettings ,
231+ inspection ?: ProcessLockInspection ,
232+ ) : AppError {
233+ return new AppError ( 'COMMAND_FAILED' , 'Daemon registration ownership could not be verified.' , {
234+ reason : 'daemon_registration_unproven' ,
235+ inspection,
236+ stateDir : settings . paths . baseDir ,
237+ hint : resolveDaemonStartupHint ( getDaemonMetadataState ( settings . paths ) , settings . paths ) ,
238+ } ) ;
239+ }
240+
221241async function retireDaemonForTakeover ( existing : DaemonInfo , paths : DaemonPaths ) : Promise < void > {
222242 const retirement = await stopAndRetireDaemon ( {
223243 paths : paths ,
@@ -328,6 +348,7 @@ function emitDaemonTakeoverNotice(info: DaemonInfo, reason: string, stateDir: st
328348type FailedDaemonStartup = {
329349 cleanup ?: DaemonRetirementResult ;
330350 startError ?: string ;
351+ startupError ?: NormalizedError ;
331352 daemonProcess ?: ExecDetachedExit | { pid : number } ;
332353 retry : boolean ;
333354} ;
@@ -346,6 +367,7 @@ async function startLocalDaemon(settings: DaemonClientSettings): Promise<Ensured
346367 if ( ! result . retry ) break ;
347368 await sleep ( Math . min ( 150 , Math . max ( 0 , deadline - Date . now ( ) ) ) ) ;
348369 }
370+ const { startError, startupError, daemonProcess } : Partial < FailedDaemonStartup > = failure ?? { } ;
349371 const state = getDaemonMetadataState ( settings . paths ) ;
350372 const daemonLogTail = readRecentLogTail ( settings . paths . logPath ) ;
351373 throw new AppError ( 'COMMAND_FAILED' , 'Failed to start daemon' , {
@@ -357,8 +379,9 @@ async function startLocalDaemon(settings: DaemonClientSettings): Promise<Ensured
357379 startupTimeoutMs : DAEMON_STARTUP_TIMEOUT_MS ,
358380 startupAttempts : attempts ,
359381 cleanupResults,
360- startError : failure ?. startError ,
361- daemonProcess : failure ?. daemonProcess ,
382+ startError,
383+ startupError,
384+ daemonProcess,
362385 ...( daemonLogTail ? { daemonLogTail } : { } ) ,
363386 metadataState : state ,
364387 hint : resolveDaemonStartupHint ( state , settings . paths ) ,
@@ -388,9 +411,11 @@ async function attemptLocalDaemonStartup(
388411 if ( startup . kind === 'ready' ) return { daemon : startup . daemon } ;
389412 if ( startup . kind === 'retry' ) return { retry : true } ;
390413 if ( startup . kind === 'unproven' ) {
414+ const startupError = normalizeError ( startup . error ) ;
391415 return {
392416 retry : false ,
393- startError : 'Daemon registration ownership could not be verified.' ,
417+ startError : startupError . message ,
418+ startupError,
394419 daemonProcess : { pid : launch . pid } ,
395420 } ;
396421 }
@@ -712,7 +737,8 @@ async function waitForDaemonStartup(
712737 while ( Date . now ( ) < deadline ) {
713738 if ( earlyExit ) {
714739 const kind = classifyDaemonStartupExit ( earlyExit ) ;
715- if ( kind === 'unproven' ) return { kind : 'unproven' } ;
740+ if ( kind === 'unproven' )
741+ return { kind : 'unproven' , error : daemonRegistrationUnprovenError ( settings ) } ;
716742 if ( kind === 'failed' ) return { kind : 'early_exit' , exit : earlyExit } ;
717743 const contender = await observeContendingDaemon ( settings , deadline ) ;
718744 if ( contender ) return contender ;
@@ -741,11 +767,19 @@ async function observeContendingDaemon(
741767 settings : DaemonClientSettings ,
742768 deadline : number ,
743769) : Promise < DaemonStartupWaitResult | null > {
744- const winner = await readReusableLocalDaemon ( settings , deadline ) ;
770+ let winner : DaemonInfo | null ;
771+ try {
772+ winner = await readReusableLocalDaemon ( settings , deadline ) ;
773+ } catch ( error ) {
774+ if ( error instanceof AppError && error . details ?. reason === 'daemon_registration_unproven' )
775+ return { kind : 'unproven' , error } ;
776+ throw error ;
777+ }
745778 if ( Date . now ( ) >= deadline ) return null ;
746779 if ( winner ) return { kind : 'ready' , daemon : { info : winner , startedByClient : false } } ;
747780 const inspection = inspectProcessLock ( settings . paths . lockPath ) ;
748- if ( inspection . state === 'unproven' ) return { kind : 'unproven' } ;
781+ if ( inspection . state === 'unproven' )
782+ return { kind : 'unproven' , error : daemonRegistrationUnprovenError ( settings , inspection ) } ;
749783 return isRegistrationAvailable ( inspection ) ? { kind : 'retry' } : null ;
750784}
751785
0 commit comments