@@ -10,11 +10,13 @@ import {
1010} from './daemon-process.ts' ;
1111import {
1212 readCurrentOwnerIdentity ,
13+ ownerIdentityMatches ,
1314 readProcessStartTime ,
1415 type OwnerIdentity ,
1516} from '@agent-device/host-kit/process' ;
1617import {
1718 publishFileSync ,
19+ inspectProcessLock ,
1820 tryAcquireProcessLock ,
1921 acquireProcessLockAcquisition ,
2022 type ProcessLockAttempt ,
@@ -30,6 +32,7 @@ import {
3032import { findUnrecoveredRepairCommitFailure } from './session-repair-tombstone.ts' ;
3133import {
3234 readRegisteredDaemonOwnership ,
35+ readRegisteredDaemonIdentity ,
3336 type RegisteredDaemonOwnership ,
3437} from './daemon-registration.ts' ;
3538import {
@@ -182,8 +185,9 @@ export type DaemonStartupLaunch = Readonly<{
182185 pid : number ;
183186 startTime ?: string ;
184187 exited : Promise < ExecDetachedExit > ;
188+ readIdentity ( ) : OwnerIdentity ;
185189} > ;
186- type OwnedStartup = { launch : DaemonStartupLaunch ; joined : boolean } ;
190+ type OwnedStartup = { launch : DaemonStartupLaunch ; joined : boolean ; startTime ?: string } ;
187191type PrivateReplayState = {
188192 paths : Readonly < DaemonPaths > ;
189193 startups : OwnedStartup [ ] ;
@@ -227,23 +231,40 @@ export function launchDaemonProcess(
227231 } ,
228232 stdio : [ 'ignore' , logFd , logFd ] ,
229233 } ) ;
230- const startup : OwnedStartup = { launch : Object . freeze ( { ...monitored } ) , joined : false } ;
231- if ( owned ) {
232- owned . startups . push ( startup ) ;
233- void monitored . exited . then ( ( ) => {
234- startup . joined = true ;
235- } ) ;
236- }
237- startup . launch = Object . freeze ( {
238- ...startup . launch ,
239- startTime : readProcessStartTime ( monitored . pid ) ?? undefined ,
234+ const boundPaths = { ...input . paths } ;
235+ const startup : OwnedStartup = {
236+ launch : Object . freeze ( {
237+ ...monitored ,
238+ get startTime ( ) {
239+ return startup . startTime ;
240+ } ,
241+ readIdentity ( ) {
242+ recoverStartupBirth ( boundPaths , startup ) ;
243+ return { pid : monitored . pid , startTime : startup . startTime ?? null } ;
244+ } ,
245+ } ) ,
246+ joined : false ,
247+ } ;
248+ if ( owned ) owned . startups . push ( startup ) ;
249+ void monitored . exited . then ( ( ) => {
250+ startup . joined = true ;
240251 } ) ;
252+ startup . startTime = readProcessStartTime ( monitored . pid ) ?? undefined ;
241253 return startup . launch ;
242254 } finally {
243255 fs . closeSync ( logFd ) ;
244256 }
245257}
246258
259+ function recoverStartupBirth ( paths : DaemonPaths , startup : OwnedStartup ) : void {
260+ if ( startup . startTime || startup . joined ) return ;
261+ const registration = readRegisteredDaemonIdentity ( paths . infoPath ) ;
262+ if ( registration ?. pid !== startup . launch . pid || ! registration . startTime ) return ;
263+ const lock = inspectProcessLock ( paths . lockPath ) ;
264+ if ( lock . state !== 'held' || ! ownerIdentityMatches ( lock . owner , registration ) ) return ;
265+ startup . startTime = registration . startTime ;
266+ }
267+
247268function requirePrivateReplayState (
248269 capability : OwnedReplayStateDir ,
249270 paths : DaemonPaths ,
@@ -309,20 +330,12 @@ export async function stopAndRetireDaemon(
309330 } > ,
310331) : Promise < DaemonRetirementResult > {
311332 let owned : PrivateReplayState | undefined ;
333+ let observed = input . observed ;
312334 try {
313335 if ( input . ownedStateDir ) {
314336 owned = requirePrivateReplayState ( input . ownedStateDir , input . paths ) ;
315337 owned . sealed = true ;
316- const launch = owned . startups . find (
317- ( { launch } ) =>
318- launch . pid === input . observed ?. pid && launch . startTime === input . observed ?. startTime ,
319- ) ?. launch ;
320- if ( ! launch ?. startTime && ( input . observed || owned . startups . length > 0 ) )
321- throw new AppError (
322- 'COMMAND_FAILED' ,
323- 'The observed daemon is not an owned startup lifetime.' ,
324- { reason : 'daemon_private_startup_unowned' } ,
325- ) ;
338+ observed = confirmOwnedStartupIdentity ( owned , observed ) ;
326339 if ( owned . retirement ) return await owned . retirement ;
327340 }
328341 } catch ( error ) {
@@ -335,7 +348,7 @@ export async function stopAndRetireDaemon(
335348 } ;
336349 }
337350 const retirement = retireObservedDaemon (
338- input ,
351+ { ... input , observed } ,
339352 ( identity ) =>
340353 stopDaemonProcess ( identity , {
341354 mode : input . mode ,
@@ -352,6 +365,28 @@ export async function stopAndRetireDaemon(
352365 return result ;
353366}
354367
368+ function confirmOwnedStartupIdentity (
369+ owned : PrivateReplayState ,
370+ observed : OwnerIdentity | null ,
371+ ) : OwnerIdentity | null {
372+ if ( ! observed ) {
373+ if ( owned . startups . length === 0 ) return null ;
374+ throw unownedStartupError ( ) ;
375+ }
376+ const startup = owned . startups . find ( ( { launch } ) => launch . pid === observed . pid ) ;
377+ if ( ! startup ) throw unownedStartupError ( ) ;
378+ const { launch } = startup ;
379+ const identity = observed . startTime ? observed : launch . readIdentity ( ) ;
380+ if ( launch . startTime && launch . startTime === identity . startTime ) return identity ;
381+ throw unownedStartupError ( ) ;
382+ }
383+
384+ function unownedStartupError ( ) : AppError {
385+ return new AppError ( 'COMMAND_FAILED' , 'The observed daemon is not an owned startup lifetime.' , {
386+ reason : 'daemon_private_startup_unowned' ,
387+ } ) ;
388+ }
389+
355390/** Recovers a confirmed abandoned registration without signaling a live process. */
356391export async function recoverAbandonedDaemonRegistration (
357392 input : DaemonRetirementInput ,
0 commit comments