|
1 | | -import fs from 'node:fs'; |
2 | 1 | import type { ProcessLockInspection } from '@agent-device/host-kit/file'; |
3 | 2 | import { |
| 3 | + isProcessAlive, |
4 | 4 | ownerIdentityDiffers, |
5 | 5 | isProcessPid, |
6 | 6 | ownerIdentityMatches, |
| 7 | + readProcessStartTime, |
7 | 8 | type OwnerIdentity, |
8 | 9 | } from '@agent-device/host-kit/process'; |
9 | | -import { resolveDaemonPaths } from './daemon-resolution.ts'; |
| 10 | +import { |
| 11 | + readDaemonRecord, |
| 12 | + readRecordedDaemonTransport, |
| 13 | + resolveDaemonPaths, |
| 14 | + type DaemonPaths, |
| 15 | + type DaemonServerMode, |
| 16 | +} from './daemon-resolution.ts'; |
10 | 17 |
|
11 | 18 | /** |
12 | 19 | * The daemon identity published in a state dir's `daemon.json`. It is the only |
@@ -34,32 +41,29 @@ export function processLockHoldsDaemonIdentity( |
34 | 41 | /** The raw record's identity. A pid of `null` is a file that names no owner, not owner zero. */ |
35 | 42 | type ParsedRegistration = { pid: number | null; startTime: string | null }; |
36 | 43 |
|
| 44 | +type DaemonRecord = Readonly<Record<string, unknown>>; |
| 45 | + |
| 46 | +/** |
| 47 | + * Whether `daemon.json` registers a daemon: a projection of the one record reader that adds the |
| 48 | + * identity, so a record that names no owner is `ownerless` however it came to. |
| 49 | + */ |
37 | 50 | function readRegistration( |
38 | 51 | infoPath: string, |
39 | 52 | ): |
40 | | - | Readonly<{ status: 'registered'; identity: OwnerIdentity }> |
| 53 | + | Readonly<{ status: 'registered'; identity: OwnerIdentity; record: DaemonRecord }> |
41 | 54 | | Readonly<{ status: 'absent' | 'unreadable' | 'ownerless' }> { |
42 | | - let raw: string; |
43 | | - try { |
44 | | - raw = fs.readFileSync(infoPath, 'utf8'); |
45 | | - } catch (error) { |
46 | | - // Only a path the host reports as gone proves there is no record. A permission error, an I/O |
47 | | - // fault, or a read this process lost leaves a file present whose contents are unknown, which no |
48 | | - // caller may read as "someone removed it". |
49 | | - const code = (error as NodeJS.ErrnoException | null)?.code; |
50 | | - return code === 'ENOENT' || code === 'ENOTDIR' |
51 | | - ? { status: 'absent' } |
52 | | - : { status: 'unreadable' }; |
53 | | - } |
54 | | - let parsed: ParsedRegistration; |
55 | | - try { |
56 | | - parsed = parseRegistration(JSON.parse(raw) as { pid?: unknown; processStartTime?: unknown }); |
57 | | - } catch { |
58 | | - return { status: 'ownerless' }; |
| 55 | + const read = readDaemonRecord(infoPath); |
| 56 | + if (read.status !== 'present') { |
| 57 | + return { status: read.status === 'invalid' ? 'ownerless' : read.status }; |
59 | 58 | } |
| 59 | + const parsed = parseRegistration(read.record); |
60 | 60 | return parsed.pid === null |
61 | 61 | ? { status: 'ownerless' } |
62 | | - : { status: 'registered', identity: { pid: parsed.pid, startTime: parsed.startTime } }; |
| 62 | + : { |
| 63 | + status: 'registered', |
| 64 | + identity: { pid: parsed.pid, startTime: parsed.startTime }, |
| 65 | + record: read.record, |
| 66 | + }; |
63 | 67 | } |
64 | 68 |
|
65 | 69 | function parseRegistration(parsed: { |
@@ -110,6 +114,64 @@ export function readRegisteredDaemonOwnership( |
110 | 114 | return { state: 'unproven' }; |
111 | 115 | } |
112 | 116 |
|
| 117 | +/** What `daemon.json` says about the daemon serving a state dir, checked against the process. */ |
| 118 | +export type DaemonStatus = Readonly<{ |
| 119 | + /** |
| 120 | + * `unreadable`: the record exists but cannot be read; `invalid`: it is not a record. Either way |
| 121 | + * whether a daemon runs is unknown. |
| 122 | + */ |
| 123 | + state: 'running' | 'stopped' | 'unreadable' | 'invalid'; |
| 124 | + stateDir: string; |
| 125 | + pid?: number; |
| 126 | + version?: string; |
| 127 | + transport?: DaemonServerMode; |
| 128 | + /** |
| 129 | + * The provider plugin inventory the running daemon published, as recorded; `readPluginInventory` |
| 130 | + * validates it where it is shown, so this module stays out of the inventory vocabulary's closure. |
| 131 | + */ |
| 132 | + plugins?: unknown; |
| 133 | +}>; |
| 134 | + |
| 135 | +/** |
| 136 | + * Reads the daemon's published record without contacting it. The record counts while its pid names |
| 137 | + * a live process, unless a readable start time differs from the recorded one: a record a crashed |
| 138 | + * daemon left behind, or whose pid was reused, is `stopped`, and a failed start-time read does not |
| 139 | + * end the record. |
| 140 | + */ |
| 141 | +export function readDaemonStatus(paths: DaemonPaths): DaemonStatus { |
| 142 | + const base = { stateDir: paths.baseDir } as const; |
| 143 | + const read = readDaemonRecord(paths.infoPath); |
| 144 | + if (read.status !== 'present') { |
| 145 | + return read.status === 'absent' |
| 146 | + ? { state: 'stopped', ...base } |
| 147 | + : { state: read.status, ...base }; |
| 148 | + } |
| 149 | + const { record } = read; |
| 150 | + const parsed = parseRegistration(record); |
| 151 | + if (parsed.pid === null) return { state: 'stopped', ...base }; |
| 152 | + const identity = { pid: parsed.pid, startTime: parsed.startTime }; |
| 153 | + if (identity.startTime === null || !isProcessAlive(identity.pid)) { |
| 154 | + return { state: 'stopped', ...base }; |
| 155 | + } |
| 156 | + if ( |
| 157 | + ownerIdentityDiffers(identity, { |
| 158 | + pid: identity.pid, |
| 159 | + startTime: readProcessStartTime(identity.pid), |
| 160 | + }) |
| 161 | + ) { |
| 162 | + return { state: 'stopped', ...base }; |
| 163 | + } |
| 164 | + const transport = readRecordedDaemonTransport(record.transport); |
| 165 | + return { |
| 166 | + state: 'running', |
| 167 | + stateDir: paths.baseDir, |
| 168 | + pid: identity.pid, |
| 169 | + ...(typeof record.version === 'string' ? { version: record.version } : {}), |
| 170 | + ...(transport ? { transport } : {}), |
| 171 | + plugins: record.plugins, |
| 172 | + }; |
| 173 | +} |
| 174 | + |
113 | 175 | function readableStartTime(value: unknown): string | null { |
114 | 176 | return typeof value === 'string' && value.trim().length > 0 ? value : null; |
115 | 177 | } |
|
0 commit comments