Repository navigation
Expand file tree
/
Copy pathdaemon-registration-owner.ts
More file actions
551 lines (530 loc) · 17.7 KB
/
Copy pathdaemon-registration-owner.ts
File metadata and controls
551 lines (530 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { runCmdDetachedMonitored, type ExecDetachedExit } from '@agent-device/host-kit/command';
import { AppError, normalizeError, type NormalizedError } from '@agent-device/kernel/errors';
import {
stopDaemonProcess,
waitForDaemonExit,
type DaemonTerminationResult,
} from './daemon-process.ts';
import {
readCurrentOwnerIdentity,
readProcessStartTime,
type OwnerIdentity,
} from '@agent-device/host-kit/process';
import {
publishFileSync,
tryAcquireProcessLock,
acquireProcessLockAcquisition,
type ProcessLockAttempt,
type ProcessLockAcquisition,
} from '@agent-device/host-kit/file';
import { emitDiagnostic, withDiagnosticsScope } from '@agent-device/host-kit/diagnostics';
import type { DaemonCodeOrigin } from '@agent-device/host-kit/code-signature';
import {
resolveDaemonPaths,
type DaemonPaths,
type DaemonServerMode,
} from './daemon-resolution.ts';
import { findUnrecoveredRepairCommitFailure } from './session-repair-tombstone.ts';
import {
readRegisteredDaemonOwnership,
type RegisteredDaemonOwnership,
} from './daemon-registration.ts';
import {
buildDaemonShutdownReport,
resolveDaemonShutdownReportPath,
type DaemonShutdownOutcome,
} from './daemon-shutdown-report.ts';
export const DAEMON_STARTUP_EXIT_CODES = Object.freeze({ busy: 75, unproven: 78 });
export type DaemonRegistrationFields = Readonly<{
socketPort?: number;
httpPort?: number;
token: string;
version: string;
codeOrigin: DaemonCodeOrigin;
codeSignature: string;
policyDigest?: string;
}>;
type DaemonRegistrationRemoval =
| Readonly<{ state: 'removed' }>
| Exclude<RegisteredDaemonOwnership, { state: 'match' }>;
export type DaemonRegistrationOwner = Readonly<{
publish(fields: DaemonRegistrationFields): void;
finish(outcome?: DaemonShutdownOutcome): Promise<DaemonRegistrationRemoval>;
}>;
export async function tryAcquireDaemonRegistration(
paths: DaemonPaths,
): Promise<
| Readonly<{ status: 'acquired'; owner: DaemonRegistrationOwner }>
| Exclude<ProcessLockAttempt, { status: 'acquired' }>
> {
const boundPaths = { ...paths };
const identity = readCurrentOwnerIdentity();
const attempt = tryAcquireProcessLock({
lockDirPath: boundPaths.lockPath,
owner: { ...identity, acquiredAtMs: Date.now() },
description: 'daemon registration',
});
if (attempt.status !== 'acquired') return attempt;
const { acquisition } = attempt;
try {
acquisition.assertHeld();
fs.rmSync(resolveDaemonShutdownReportPath(boundPaths.baseDir), { force: true });
} catch (error) {
await releaseRegistrationAfterFailure(acquisition, error, boundPaths.logPath);
}
return {
status: 'acquired',
owner: Object.freeze({
publish({ socketPort, httpPort, ...fields }: DaemonRegistrationFields) {
acquisition.assertHeld();
truncateDaemonLog(boundPaths.logPath);
const transport = socketPort && httpPort ? 'dual' : httpPort ? 'http' : 'socket';
acquisition.assertHeld();
publishFileSync({
destination: boundPaths.infoPath,
contents: JSON.stringify(
{
...fields,
port: socketPort,
httpPort,
transport,
pid: identity.pid,
processStartTime: identity.startTime ?? undefined,
stateDir: boundPaths.baseDir,
},
null,
2,
),
mode: 0o600,
});
},
async finish(outcome?: DaemonShutdownOutcome) {
let removal: DaemonRegistrationRemoval;
try {
if (outcome) writeShutdownReport(boundPaths.baseDir, outcome, acquisition);
removal = removeRegistrationUnderLock(boundPaths.infoPath, identity, acquisition);
} catch (error) {
return await releaseRegistrationAfterFailure(acquisition, error, boundPaths.logPath);
}
await acquisition.release();
return removal;
},
}),
};
}
function removeRegistrationUnderLock(
infoPath: string,
identity: OwnerIdentity | null,
acquisition: ProcessLockAcquisition,
): DaemonRegistrationRemoval {
acquisition.assertHeld();
const ownership = readRegisteredDaemonOwnership(infoPath, identity);
if (ownership.state !== 'match') return ownership;
acquisition.assertHeld();
try {
fs.unlinkSync(infoPath);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
}
return { state: 'removed' };
}
async function releaseRegistrationAfterFailure(
acquisition: ProcessLockAcquisition,
error: unknown,
logPath: string,
): Promise<never> {
try {
await acquisition.release();
} catch (releaseError) {
await recordRegistrationWarning(logPath, 'daemon_registration_release_failed', releaseError);
}
throw error;
}
function writeShutdownReport(
stateDir: string,
outcome: DaemonShutdownOutcome,
acquisition: ProcessLockAcquisition,
): void {
const filePath = resolveDaemonShutdownReportPath(stateDir);
const contents = `${JSON.stringify(buildDaemonShutdownReport(outcome))}\n`;
acquisition.assertHeld();
try {
publishFileSync({ destination: filePath, contents, mode: 0o600 });
} catch {
return;
}
}
/** The daemon's stdout and stderr append to this inode, so it is emptied in place, never replaced. */
function truncateDaemonLog(logPath: string): void {
const descriptor = fs.openSync(logPath, 'a', 0o600);
try {
fs.ftruncateSync(descriptor, 0);
} finally {
fs.closeSync(descriptor);
}
}
declare const privateReplayState: unique symbol;
export type OwnedReplayStateDir = Readonly<{
paths: Readonly<DaemonPaths>;
[privateReplayState]: true;
}>;
export type DaemonStartupLaunch = Readonly<{
pid: number;
startTime?: string;
exited: Promise<ExecDetachedExit>;
}>;
type OwnedStartup = { launch: DaemonStartupLaunch; joined: boolean };
type PrivateReplayState = {
paths: Readonly<DaemonPaths>;
startups: OwnedStartup[];
sealed: boolean;
retirement?: Promise<DaemonRetirementResult>;
};
const privateReplayStates = new WeakMap<OwnedReplayStateDir, PrivateReplayState>();
/** Creates deletion authority only for a fresh private replay directory. */
export function createOwnedReplayStateDir(): OwnedReplayStateDir {
const paths = Object.freeze(
resolveDaemonPaths(fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-replay-daemon-'))),
);
const owned = Object.freeze({ paths }) as OwnedReplayStateDir;
privateReplayStates.set(owned, { paths, startups: [], sealed: false });
return owned;
}
/** Launches and monitors the actual child before recording its private-directory authority. */
export function launchDaemonProcess(
input: Readonly<{
paths: DaemonPaths;
args: string[];
serverMode: DaemonServerMode;
ownedStateDir?: OwnedReplayStateDir;
}>,
): DaemonStartupLaunch {
const owned = input.ownedStateDir && requirePrivateReplayState(input.ownedStateDir, input.paths);
if (owned?.sealed)
throw new AppError('COMMAND_FAILED', 'Replay daemon startup admission is closed.', {
reason: 'daemon_startup_admission_closed',
});
fs.mkdirSync(input.paths.baseDir, { recursive: true });
const logFd = fs.openSync(input.paths.logPath, 'a');
try {
const monitored = runCmdDetachedMonitored(process.execPath, input.args, {
env: {
...process.env,
AGENT_DEVICE_STATE_DIR: input.paths.baseDir,
AGENT_DEVICE_DAEMON_SERVER_MODE: input.serverMode,
},
stdio: ['ignore', logFd, logFd],
});
const startup: OwnedStartup = { launch: Object.freeze({ ...monitored }), joined: false };
if (owned) {
owned.startups.push(startup);
void monitored.exited.then(() => {
startup.joined = true;
});
}
startup.launch = Object.freeze({
...startup.launch,
startTime: readProcessStartTime(monitored.pid) ?? undefined,
});
return startup.launch;
} finally {
fs.closeSync(logFd);
}
}
function requirePrivateReplayState(
capability: OwnedReplayStateDir,
paths: DaemonPaths,
): PrivateReplayState {
const owned = privateReplayStates.get(capability);
if (
!owned ||
Object.entries(owned.paths).some(([key, value]) => paths[key as keyof DaemonPaths] !== value)
) {
throw new AppError(
'COMMAND_FAILED',
'Private replay directory ownership could not be verified.',
{ reason: 'daemon_private_state_unowned' },
);
}
return owned;
}
export type DaemonRetirementInput = Readonly<{
paths: DaemonPaths;
observed: OwnerIdentity | null;
termTimeoutMs?: number;
killTimeoutMs?: number;
lockTimeoutMs?: number;
}>;
type RepairCommitFailure = NonNullable<ReturnType<typeof findUnrecoveredRepairCommitFailure>>;
type ConfirmedDaemonTermination = Extract<DaemonTerminationResult, { status: 'exited' }>;
export type DaemonRetirementResult =
| Readonly<{
status: 'retired';
termination: ConfirmedDaemonTermination;
removedInfo: boolean;
removedStateDir?: boolean;
repairCommitFailure?: RepairCommitFailure;
}>
| Readonly<{ status: 'absent'; removedInfo: false; removedStateDir?: boolean }>
| Readonly<{
status: 'retained';
termination?: DaemonTerminationResult;
removedInfo: boolean;
removedStateDir?: boolean;
reason:
| 'ownership-unproven'
| 'startup-unconfirmed'
| 'exit-unconfirmed'
| 'stop-failed'
| 'lock-busy'
| 'registration-replaced'
| 'metadata-unreadable'
| 'retirement-unconfirmed';
error?: NormalizedError;
repairCommitFailure?: RepairCommitFailure;
}>;
/** Stops only the captured daemon lifetime, then retires its registration under the startup lock. */
export async function stopAndRetireDaemon(
input: DaemonRetirementInput &
Readonly<{
mode: 'graceful' | 'force';
ownedStateDir?: OwnedReplayStateDir;
startupJoinTimeoutMs?: number;
}>,
): Promise<DaemonRetirementResult> {
let owned: PrivateReplayState | undefined;
try {
if (input.ownedStateDir) {
owned = requirePrivateReplayState(input.ownedStateDir, input.paths);
owned.sealed = true;
const launch = owned.startups.find(
({ launch }) =>
launch.pid === input.observed?.pid && launch.startTime === input.observed?.startTime,
)?.launch;
if (!launch?.startTime && (input.observed || owned.startups.length > 0))
throw new AppError(
'COMMAND_FAILED',
'The observed daemon is not an owned startup lifetime.',
{ reason: 'daemon_private_startup_unowned' },
);
if (owned.retirement) return await owned.retirement;
}
} catch (error) {
await joinOwnedStartups(owned, input.startupJoinTimeoutMs ?? 1_000);
return {
status: 'retained',
reason: 'ownership-unproven',
removedInfo: false,
error: normalizeError(error),
};
}
const retirement = retireObservedDaemon(
input,
(identity) =>
stopDaemonProcess(identity, {
mode: input.mode,
termTimeoutMs: input.termTimeoutMs ?? 3_000,
killTimeoutMs: input.killTimeoutMs ?? 1_000,
}),
owned,
input.startupJoinTimeoutMs ?? 1_000,
);
if (owned) owned.retirement = retirement;
const result = await retirement;
if (owned && (result.status === 'absent' || !result.removedStateDir))
owned.retirement = undefined;
return result;
}
/** Recovers a confirmed abandoned registration without signaling a live process. */
export async function recoverAbandonedDaemonRegistration(
input: DaemonRetirementInput,
): Promise<DaemonRetirementResult> {
return await retireObservedDaemon(input, async (identity) => {
if (!identity.startTime?.trim()) return { status: 'retained', reason: 'missing-start-time' };
const confirmed = { pid: identity.pid, startTime: identity.startTime };
return (await waitForDaemonExit(confirmed, { timeoutMs: 0 })).exited
? { status: 'exited', identity: confirmed, mode: 'already-exited' }
: { status: 'retained', reason: 'identity-unverified' };
});
}
async function retireObservedDaemon(
input: DaemonRetirementInput,
terminate: (identity: OwnerIdentity) => Promise<DaemonTerminationResult>,
owned?: PrivateReplayState,
startupJoinTimeoutMs = 1_000,
): Promise<DaemonRetirementResult> {
const paths = { ...input.paths };
const observed = input.observed && { ...input.observed };
let termination: DaemonTerminationResult | undefined;
let stopFailure: NormalizedError | undefined;
if (observed) {
try {
termination = await terminate(observed);
} catch (error) {
stopFailure = normalizeError(error);
}
}
const joined = await joinOwnedStartups(owned, startupJoinTimeoutMs);
if (stopFailure)
return { status: 'retained', reason: 'stop-failed', removedInfo: false, error: stopFailure };
if (termination && termination.status !== 'exited')
return { status: 'retained', reason: 'exit-unconfirmed', termination, removedInfo: false };
if (!joined) {
return { status: 'retained', reason: 'startup-unconfirmed', termination, removedInfo: false };
}
return await retireDaemonRegistration({ ...input, paths }, termination, owned);
}
async function retireDaemonRegistration(
input: DaemonRetirementInput,
termination: ConfirmedDaemonTermination | undefined,
owned?: PrivateReplayState,
): Promise<DaemonRetirementResult> {
const paths = input.paths;
let acquisition: ProcessLockAcquisition;
try {
acquisition = await acquireProcessLockAcquisition({
lockDirPath: paths.lockPath,
owner: { ...readCurrentOwnerIdentity(), acquiredAtMs: Date.now() },
description: 'daemon registration retirement',
timeoutMs: input.lockTimeoutMs ?? 1_000,
});
} catch (error) {
const failure = normalizeError(error);
return {
status: 'retained',
reason:
failure.details?.reason === 'process_lock_timeout' ? 'lock-busy' : 'retirement-unconfirmed',
termination,
removedInfo: false,
error: failure,
};
}
let result: DaemonRetirementResult = {
status: 'retained',
reason: 'retirement-unconfirmed',
removedInfo: false,
};
try {
const removal = removeRegistrationUnderLock(
paths.infoPath,
termination?.identity ?? null,
acquisition,
);
result = retirementAfterRemoval(removal, termination);
result = retirePrivateStateIfEligible(owned, acquisition, result);
} catch (error) {
result = {
status: 'retained',
reason: 'retirement-unconfirmed',
termination,
removedInfo: result.removedInfo,
error: normalizeError(error),
};
}
return await releaseAfterRetirement(acquisition, result, paths.logPath);
}
async function releaseAfterRetirement(
acquisition: ProcessLockAcquisition,
result: DaemonRetirementResult,
logPath: string,
): Promise<DaemonRetirementResult> {
try {
await acquisition.release();
} catch (error) {
const primary = result.status === 'retained' ? result.error : undefined;
if (primary)
await recordRegistrationWarning(logPath, 'daemon_retirement_release_failed', error);
result = {
...result,
status: 'retained',
reason: 'retirement-unconfirmed',
error: primary ?? normalizeError(error),
};
}
return result;
}
function retirementAfterRemoval(
removal: DaemonRegistrationRemoval,
termination: ConfirmedDaemonTermination | undefined,
): DaemonRetirementResult {
if (termination && (removal.state === 'removed' || removal.state === 'absent')) {
return { status: 'retired', termination, removedInfo: removal.state === 'removed' };
}
if (removal.state === 'absent') return { status: 'absent', removedInfo: false };
return {
status: 'retained',
termination,
removedInfo: false,
reason:
removal.state === 'replaced'
? 'registration-replaced'
: removal.state === 'unreadable'
? 'metadata-unreadable'
: 'ownership-unproven',
};
}
async function recordRegistrationWarning(
logPath: string,
phase: string,
error: unknown,
): Promise<void> {
await withDiagnosticsScope({ command: 'daemon', session: 'daemon', logPath, debug: true }, () => {
emitDiagnostic({ level: 'warn', phase, data: { error: normalizeError(error) } });
});
}
async function joinOwnedStartups(
owned: PrivateReplayState | undefined,
timeoutMs: number,
): Promise<boolean> {
if (!owned || owned.startups.every((startup) => startup.joined)) return true;
let timer: ReturnType<typeof setTimeout> | undefined;
try {
return await Promise.race([
Promise.all(owned.startups.map(({ launch }) => launch.exited)).then(() => true),
new Promise<boolean>((resolve) => {
timer = setTimeout(() => resolve(false), timeoutMs);
}),
]);
} finally {
if (timer) clearTimeout(timer);
}
}
function retirePrivateStateIfEligible(
owned: PrivateReplayState | undefined,
acquisition: ProcessLockAcquisition,
result: DaemonRetirementResult,
): DaemonRetirementResult {
if (
!owned ||
(result.status !== 'retired' && !(result.status === 'absent' && owned.startups.length === 0))
)
return result;
try {
acquisition.assertHeld();
const failure = findUnrecoveredRepairCommitFailure(owned.paths.sessionsDir);
if (failure)
return result.status === 'retired'
? { ...result, removedStateDir: false, repairCommitFailure: failure }
: {
status: 'retained',
reason: 'retirement-unconfirmed',
removedInfo: false,
removedStateDir: false,
repairCommitFailure: failure,
};
acquisition.assertHeld();
fs.rmSync(owned.paths.baseDir, { recursive: true, force: true });
return { ...result, removedStateDir: true };
} catch (error) {
return {
...result,
status: 'retained',
reason: 'retirement-unconfirmed',
removedStateDir: false,
error: normalizeError(error),
};
}
}