|
| 1 | +import { appendFile, readFile, writeFile } from 'node:fs/promises'; |
| 2 | +import { spawnSync } from 'node:child_process'; |
| 3 | +import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { expect, test } from 'vitest'; |
| 6 | +import { tryAcquireDaemonRegistration } from '@agent-device/daemon-contracts/daemon-registration-owner'; |
| 7 | +import { |
| 8 | + claimDaemonStartupPhasesFile, |
| 9 | + readDaemonStartupPhaseMarkers, |
| 10 | + recordDaemonStartupMarker, |
| 11 | + removeDaemonStartupPhasesFile, |
| 12 | + resolveDaemonStartupPhasesPath, |
| 13 | +} from '@agent-device/daemon-contracts/daemon-startup-phases'; |
| 14 | +import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution'; |
| 15 | +import { mkdtempForTest } from '@agent-device/test-kit'; |
| 16 | + |
| 17 | +test('per-launch marker files: a fresh launch never reads an older launch phases', async () => { |
| 18 | + const stateDir = await mkdtempForTest('agent-device-markers-'); |
| 19 | + const first = resolveDaemonStartupPhasesPath(stateDir, 1111); |
| 20 | + const second = resolveDaemonStartupPhasesPath(stateDir, 2222); |
| 21 | + recordDaemonStartupMarker(first, 'lock-acquired'); |
| 22 | + expect(second).not.toBe(first); |
| 23 | + expect(existsSync(second)).toBe(false); |
| 24 | + recordDaemonStartupMarker(second, 'modules-loaded'); |
| 25 | + const firstPhases = readDaemonStartupPhaseMarkers(readFileSync(first, 'utf8')); |
| 26 | + const secondPhases = readDaemonStartupPhaseMarkers(readFileSync(second, 'utf8')); |
| 27 | + expect(Object.keys(firstPhases)).toEqual(['lock-acquired']); |
| 28 | + expect(Object.keys(secondPhases)).toEqual(['modules-loaded']); |
| 29 | +}); |
| 30 | + |
| 31 | +test('marker reading keeps the earliest record when a phase replays after a later one', () => { |
| 32 | + const text = [ |
| 33 | + '{"data":{"startupPhase":"listening","timestampMs":500}}', |
| 34 | + '{"data":{"startupPhase":"lock-acquired","timestampMs":20}}', |
| 35 | + '{"data":{"startupPhase":"lock-acquired","timestampMs":35}}', |
| 36 | + ].join('\n'); |
| 37 | + const phases = readDaemonStartupPhaseMarkers(text); |
| 38 | + expect(phases['lock-acquired']).toBe(20); |
| 39 | + expect(phases.listening).toBe(500); |
| 40 | +}); |
| 41 | + |
| 42 | +test('marker reading ignores unknown phases and unparsable lines', () => { |
| 43 | + const text = [ |
| 44 | + '{"data":{"startupPhase":"heartbeat","timestampMs":10}}', |
| 45 | + '{"data":{"startupPhase":"registered","timestampMs":"7"}}', |
| 46 | + 'not json', |
| 47 | + '{"data":{"startupPhase":"listening","timestampMs":30}}', |
| 48 | + ].join('\n'); |
| 49 | + expect(readDaemonStartupPhaseMarkers(text)).toEqual({ listening: 30 }); |
| 50 | +}); |
| 51 | + |
| 52 | +test('the producer lifetime claim empties a same-pid leftover', async () => { |
| 53 | + // Pid reuse after both the old client and daemon died before either cleanup: the entry claims |
| 54 | + // its record before its first marker; this child repeats that producer sequence itself. |
| 55 | + const stateDir = await mkdtempForTest('agent-device-markers-'); |
| 56 | + writeFileSync(path.join(stateDir, 'package.json'), '{"type":"module"}'); |
| 57 | + const entry = path.join(stateDir, 'claim-producer.js'); |
| 58 | + const registrationUrl = new URL('./daemon-startup-phases.ts', import.meta.url).href; |
| 59 | + writeFileSync( |
| 60 | + entry, |
| 61 | + `import { claimDaemonStartupPhasesFile, recordDaemonStartupMarker, resolveDaemonStartupPhasesPath } from ${JSON.stringify(registrationUrl)}; |
| 62 | +const file = resolveDaemonStartupPhasesPath(${JSON.stringify(stateDir)}, process.pid); |
| 63 | +recordDaemonStartupMarker(file, 'listening'); |
| 64 | +recordDaemonStartupMarker(file, 'registered'); |
| 65 | +claimDaemonStartupPhasesFile(file); |
| 66 | +recordDaemonStartupMarker(file, 'modules-loaded'); |
| 67 | +`, |
| 68 | + ); |
| 69 | + const child = spawnSync(process.execPath, ['--experimental-strip-types', entry], { |
| 70 | + encoding: 'utf8', |
| 71 | + }); |
| 72 | + if (child.status !== 0) throw new Error(child.stderr); |
| 73 | + const phases = readDaemonStartupPhaseMarkers( |
| 74 | + readFileSync(resolveDaemonStartupPhasesPath(stateDir, child.pid!), 'utf8'), |
| 75 | + ); |
| 76 | + expect(phases).toEqual({ 'modules-loaded': expect.any(Number) }); |
| 77 | +}); |
| 78 | + |
| 79 | +test('a refused claim reset reports through the callback instead of throwing', async () => { |
| 80 | + const stateDir = await mkdtempForTest('agent-device-markers-'); |
| 81 | + const markerPath = resolveDaemonStartupPhasesPath(stateDir, 778); |
| 82 | + mkdirSync(markerPath, { recursive: true }); |
| 83 | + const reported: unknown[] = []; |
| 84 | + expect(() => |
| 85 | + claimDaemonStartupPhasesFile(markerPath, (error) => { |
| 86 | + reported.push(error); |
| 87 | + }), |
| 88 | + ).not.toThrow(); |
| 89 | + expect(reported).toHaveLength(1); |
| 90 | + expect(reported[0]).toHaveProperty('message'); |
| 91 | +}); |
| 92 | + |
| 93 | +test('acquiring the registration retires files of dead or unowned pids only', async () => { |
| 94 | + const paths = resolveDaemonPaths(await mkdtempForTest('agent-device-markers-')); |
| 95 | + const dead = resolveDaemonStartupPhasesPath(paths.baseDir, spawnDeadPid()); |
| 96 | + const own = resolveDaemonStartupPhasesPath(paths.baseDir, process.pid); |
| 97 | + const unparseable = path.join(paths.baseDir, 'daemon-startup-phases-.jsonl'); |
| 98 | + const legacy = path.join(paths.baseDir, 'daemon-startup-phases.jsonl'); |
| 99 | + await Promise.all([ |
| 100 | + writeFile(own, 'x\n', 'utf8'), |
| 101 | + writeFile(dead, 'x\n', 'utf8'), |
| 102 | + writeFile(unparseable, 'x\n', 'utf8'), |
| 103 | + writeFile(legacy, 'x\n', 'utf8'), |
| 104 | + ]); |
| 105 | + const attempt = await tryAcquireDaemonRegistration(paths); |
| 106 | + expect(attempt.status).toBe('acquired'); |
| 107 | + if (attempt.status !== 'acquired') throw new Error('registration refused'); |
| 108 | + expect(existsSync(dead)).toBe(false); |
| 109 | + expect(existsSync(unparseable)).toBe(false); |
| 110 | + expect(existsSync(own)).toBe(true); |
| 111 | + expect(existsSync(legacy)).toBe(true); |
| 112 | + await attempt.owner.finish(); |
| 113 | +}); |
| 114 | + |
| 115 | +test('removeDaemonStartupPhasesFile removes a file and tolerates one already gone', async () => { |
| 116 | + const stateDir = await mkdtempForTest('agent-device-markers-'); |
| 117 | + const file = resolveDaemonStartupPhasesPath(stateDir, 4242); |
| 118 | + await writeFile(file, 'x\n', 'utf8'); |
| 119 | + removeDaemonStartupPhasesFile(file); |
| 120 | + expect(existsSync(file)).toBe(false); |
| 121 | + expect(() => removeDaemonStartupPhasesFile(file)).not.toThrow(); |
| 122 | +}); |
| 123 | + |
| 124 | +test('a marker write failure reports through the callback, keeping startup running', async () => { |
| 125 | + const stateDir = await mkdtempForTest('agent-device-markers-'); |
| 126 | + const markerPath = resolveDaemonStartupPhasesPath(stateDir, 777); |
| 127 | + mkdirSync(markerPath, { recursive: true }); |
| 128 | + const reported: unknown[] = []; |
| 129 | + expect(() => |
| 130 | + recordDaemonStartupMarker(markerPath, 'modules-loaded', (error) => { |
| 131 | + reported.push(error); |
| 132 | + }), |
| 133 | + ).not.toThrow(); |
| 134 | + expect(reported).toHaveLength(1); |
| 135 | + expect(reported[0]).toHaveProperty('message'); |
| 136 | +}); |
| 137 | + |
| 138 | +test('a crash-replay append keeps the file valid JSONL', async () => { |
| 139 | + const stateDir = await mkdtempForTest('agent-device-markers-'); |
| 140 | + const markerPath = resolveDaemonStartupPhasesPath(stateDir, 999); |
| 141 | + await appendFile(markerPath, '{"data":{"startupPhase":"modules-lo', 'utf8'); |
| 142 | + recordDaemonStartupMarker(markerPath, 'composition-loaded'); |
| 143 | + const tail = await readFile(markerPath, 'utf8'); |
| 144 | + expect(Object.keys(readDaemonStartupPhaseMarkers(tail))).toEqual(['composition-loaded']); |
| 145 | +}); |
| 146 | + |
| 147 | +function spawnDeadPid(): number { |
| 148 | + const child = spawnSync(process.execPath, ['-e', ''], { encoding: 'utf8' }); |
| 149 | + if (child.error) throw child.error; |
| 150 | + const pid = child.pid; |
| 151 | + if (pid === undefined) throw new Error('spawnSync returned no pid'); |
| 152 | + return pid; |
| 153 | +} |
0 commit comments