@@ -6,6 +6,7 @@ import http from 'node:http';
66import path from 'node:path' ;
77import { test } from 'vitest' ;
88import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution' ;
9+ import { readRegisteredDaemonIdentity } from '@agent-device/daemon-contracts/daemon-registration' ;
910import { startDaemonRuntime , type DaemonRuntimeController } from '@agent-device/daemon' ;
1011import { createDaemonRuntimeDeps } from './daemon-composition.ts' ;
1112import { runCmdBackground } from '@agent-device/host-kit/command' ;
@@ -292,10 +293,44 @@ test('daemon entrypoint reaps itself when idle: exits cleanly, releases its regi
292293 // Nothing ever talks to it, so the idle reap, not a signal, is what closes it.
293294 const result = await daemon . wait ;
294295
295- assert . equal ( result . exitCode , 0 ) ;
296- assert . equal ( result . stderr , '' ) ;
297- assert . equal ( fs . existsSync ( paths . infoPath ) , false ) ;
298- assert . equal ( fs . existsSync ( paths . lockPath ) , false ) ;
296+ try {
297+ assert . equal ( result . exitCode , 0 ) ;
298+ assert . equal ( result . stderr , '' ) ;
299+ assert . equal ( fs . existsSync ( paths . infoPath ) , false ) ;
300+ assert . equal ( fs . existsSync ( paths . lockPath ) , false ) ;
301+ } catch ( error ) {
302+ let diagnostics : { phase : string ; reason ?: string ; code ?: string } [ ] | 'unavailable' ;
303+ try {
304+ diagnostics = fs
305+ . readFileSync ( paths . logPath , 'utf8' )
306+ . split ( '\n' )
307+ . filter ( Boolean )
308+ . map (
309+ ( line ) =>
310+ JSON . parse ( line ) as {
311+ phase : string ;
312+ data ?: { reason ?: string ; error ?: { code ?: string } } ;
313+ } ,
314+ )
315+ . filter (
316+ ( { phase } ) =>
317+ phase === 'daemon_info_removal_declined' ||
318+ phase === 'daemon_registration_finish_failed' ,
319+ )
320+ . map ( ( { phase, data } ) => ( { phase, reason : data ?. reason , code : data ?. error ?. code } ) ) ;
321+ } catch {
322+ diagnostics = 'unavailable' ;
323+ }
324+ if ( error instanceof Error ) {
325+ error . message += `\nRegistration cleanup evidence: ${ JSON . stringify ( {
326+ identity : readRegisteredDaemonIdentity ( paths . infoPath ) ,
327+ infoExists : fs . existsSync ( paths . infoPath ) ,
328+ lockExists : fs . existsSync ( paths . lockPath ) ,
329+ diagnostics,
330+ } ) } `;
331+ }
332+ throw error ;
333+ }
299334 } finally {
300335 if ( isProcessAlive ( pid ) ) {
301336 daemon . child . kill ( 'SIGKILL' ) ;
0 commit comments