@@ -10,7 +10,6 @@ import {
1010 type DaemonTerminationResult ,
1111} from './daemon-process.ts' ;
1212import {
13- isProcessAlive ,
1413 readCurrentOwnerIdentity ,
1514 readProcessStartTime ,
1615 type OwnerIdentity ,
@@ -45,148 +44,10 @@ import {
4544 resolveDaemonShutdownReportPath ,
4645 type DaemonShutdownOutcome ,
4746} from './daemon-shutdown-report.ts' ;
47+ import { sweepDeadDaemonStartupPhasesFiles } from './daemon-startup-phases.ts' ;
4848
4949export const DAEMON_STARTUP_EXIT_CODES = Object . freeze ( { busy : 75 , unproven : 78 } ) ;
5050
51- /**
52- * Startup phase markers: a per-launch record, named for the writing pid, of how far that startup
53- * got, so a waiting client tells "slow but progressing" from "stuck" without a timestamp and with
54- * no heartbeat. The writer removes it on completion, the launching client after a joined exit, and
55- * each new registration owner sweeps the files of provably dead pids.
56- */
57- const DAEMON_STARTUP_PHASES = Object . freeze ( [
58- 'modules-loaded' ,
59- 'composition-loaded' ,
60- 'lock-acquired' ,
61- 'recovery-finished' ,
62- 'listening' ,
63- 'registered' ,
64- ] as const ) ;
65-
66- export type DaemonStartupPhase = ( typeof DAEMON_STARTUP_PHASES ) [ number ] ;
67-
68- const DAEMON_STARTUP_PHASE_SET : ReadonlySet < string > = new Set < string > ( DAEMON_STARTUP_PHASES ) ;
69-
70- const DAEMON_STARTUP_PHASES_ENTRY = / ^ d a e m o n - s t a r t u p - p h a s e s - ( \d * ) \. j s o n l $ / ;
71-
72- /** The marker file the daemon process `pid` writes in `stateDir`; an absent file is no evidence. */
73- export function resolveDaemonStartupPhasesPath ( stateDir : string , pid : number ) : string {
74- return path . join ( stateDir , `daemon-startup-phases-${ pid } .jsonl` ) ;
75- }
76-
77- /** Appends one phase marker; a refused append goes to `reportFailure`, never fatal to a startup. */
78- export function recordDaemonStartupMarker (
79- startupPhasesPath : string ,
80- phase : DaemonStartupPhase ,
81- reportFailure ?: ( error : NormalizedError ) => void ,
82- ) : void {
83- try {
84- const timestampMs = Date . now ( ) ;
85- fs . mkdirSync ( path . dirname ( startupPhasesPath ) , { recursive : true } ) ;
86- // A crash mid-append can leave no trailing newline; glue two appends and both are lost.
87- const separator = endsWithoutNewline ( startupPhasesPath ) ? '\n' : '' ;
88- fs . appendFileSync (
89- startupPhasesPath ,
90- `${ separator } ${ JSON . stringify ( { data : { startupPhase : phase , timestampMs } } ) } \n` ,
91- ) ;
92- } catch ( error ) {
93- reportFailure ?.( normalizeError ( error ) ) ;
94- }
95- }
96-
97- function endsWithoutNewline ( file : string ) : boolean {
98- let fd : number | undefined ;
99- try {
100- const stats = fs . statSync ( file ) ;
101- if ( stats . size === 0 ) return false ;
102- const buffer = Buffer . alloc ( 1 ) ;
103- fd = fs . openSync ( file , 'r' ) ;
104- if ( fs . readSync ( fd , buffer , 0 , 1 , stats . size - 1 ) !== 1 ) return false ;
105- return buffer [ 0 ] !== 0x0a ;
106- } catch {
107- return false ;
108- } finally {
109- if ( fd !== undefined ) fs . closeSync ( fd ) ;
110- }
111- }
112-
113- /**
114- * Empties this launch's own record before its first marker: a per-pid name identifies a writer,
115- * not a lifetime. No caller resets a file named for another pid; a refused reset never stops the
116- * startup because the reset ran first when it could run.
117- */
118- export function claimDaemonStartupPhasesFile (
119- startupPhasesPath : string ,
120- reportFailure ?: ( error : NormalizedError ) => void ,
121- ) : void {
122- try {
123- fs . rmSync ( startupPhasesPath , { force : true } ) ;
124- } catch ( error ) {
125- reportFailure ?.( normalizeError ( error ) ) ;
126- }
127- }
128-
129- /** Removes a marker file whose writer is provably gone: a finished startup or a joined exit. */
130- export function removeDaemonStartupPhasesFile ( startupPhasesPath : string ) : void {
131- try {
132- fs . rmSync ( startupPhasesPath , { force : true } ) ;
133- } catch { }
134- }
135-
136- /** Sweeps marker files of provably dead (or unnameable) pids; a live pid's file is kept. */
137- function sweepDeadDaemonStartupPhasesFiles ( stateDir : string ) : void {
138- let entries : string [ ] ;
139- try {
140- entries = fs . readdirSync ( stateDir ) ;
141- } catch {
142- return ;
143- }
144- for ( const entry of entries ) {
145- const match = DAEMON_STARTUP_PHASES_ENTRY . exec ( entry ) ;
146- if ( match === null ) continue ;
147- const deadPid = Number ( match [ 1 ] ) ;
148- if ( deadPid === process . pid ) continue ;
149- if ( ! Number . isSafeInteger ( deadPid ) || deadPid <= 0 || ! isProcessAlive ( deadPid ) ) {
150- removeDaemonStartupPhasesFile ( path . join ( stateDir , entry ) ) ;
151- }
152- }
153- }
154-
155- /**
156- * The phases observed in a marker tail, keeping each phase's earliest occurrence. Unknown and
157- * unparsable lines are ignored: a client never fails a startup over a line it does not know.
158- */
159- export function readDaemonStartupPhaseMarkers (
160- markerTail : string ,
161- ) : Partial < Record < DaemonStartupPhase , number > > {
162- const phases : Partial < Record < DaemonStartupPhase , number > > = { } ;
163- for ( const line of markerTail . split ( '\n' ) ) {
164- if ( ! line . includes ( '"startupPhase"' ) ) continue ;
165- let record : DaemonStartupMarkerRecord ;
166- try {
167- record = JSON . parse ( line ) as DaemonStartupMarkerRecord ;
168- } catch {
169- continue ;
170- }
171- const phase = record . data ?. startupPhase ;
172- const timestampMs = record . data ?. timestampMs ;
173- if (
174- typeof phase !== 'string' ||
175- ! DAEMON_STARTUP_PHASE_SET . has ( phase ) ||
176- typeof timestampMs !== 'number' ||
177- ! Number . isFinite ( timestampMs )
178- ) {
179- continue ;
180- }
181- const seen = phases [ phase as DaemonStartupPhase ] ;
182- if ( seen === undefined || timestampMs < seen ) phases [ phase as DaemonStartupPhase ] = timestampMs ;
183- }
184- return phases ;
185- }
186-
187- type DaemonStartupMarkerRecord = {
188- data ?: { startupPhase ?: unknown ; timestampMs ?: unknown } ;
189- } ;
19051export type DaemonRegistrationFields = Readonly < {
19152 socketPort ?: number ;
19253 httpPort ?: number ;
0 commit comments