Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182/** * daemon-discover — CLI discovery of a running `shardd` (saga 8usqif, §13). * * Reads `~/.shard/daemon.json` (the §13 boot descriptor, whose shape + path constants live * in the L1 `@/contracts/daemon-meta` contract so this L4 module imports no `@/daemon/*` — * the §16 layer rule forbids that) + probes the recorded pid for liveness, so a stale * descriptor from a crashed daemon is reported as "not running" (the caller spawns a fresh * one rather than connecting to a dead socket). * * Returns the live socket path, or `undefined` if no daemon is running. * * Boundary: L4 CLI module. Imports `@/contracts/daemon-meta` (L1) + `node:os`/ * `node:fs`/`node:path`. No `@/core`, `@/sdk`, or `@/daemon`. */import { join } from 'node:path';import { existsSync, readFileSync } from 'node:fs';import { DEFAULT_SHARD_HOME, DAEMON_FILE, type DaemonMeta,} from '@/contracts/daemon-meta';
/** Path to the daemon boot descriptor (`<home>/daemon.json`, §13). */export function daemonMetaPath(home: string = DEFAULT_SHARD_HOME): string { return join(home, DAEMON_FILE);}
/** Parse + validate the daemon boot descriptor; `undefined` if absent/corrupt. */export function readDaemonMeta(home: string = DEFAULT_SHARD_HOME): DaemonMeta | undefined { const path = daemonMetaPath(home); if (!existsSync(path)) return undefined; try { const raw = readFileSync(path, 'utf-8'); const parsed = JSON.parse(raw) as unknown; return isDaemonMeta(parsed) ? parsed : undefined; } catch { return undefined; }}
/** * Discover a running `shardd`: read the boot descriptor + probe the recorded pid for * liveness (a stale `daemon.json` from a crashed daemon → `undefined`, so the caller * spawns a fresh one rather than connecting to a dead socket). Returns the socket path * iff a live daemon is recorded; `undefined` otherwise. */export function discoverDaemon(home: string = DEFAULT_SHARD_HOME): string | undefined { const meta = readDaemonMeta(home); if (meta === undefined) return undefined; if (!isPidAlive(meta.pid)) return undefined; return meta.socketPath;}
/** Is a process with the given pid alive? (Best-effort `kill(pid, 0)` probe — no signal.) */function isPidAlive(pid: number): boolean { if (typeof pid !== 'number' || !Number.isFinite(pid) || pid <= 0) return false; try { process.kill(pid, 0); return true; } catch { // ESRCH (no such process) → dead; EPERM (exists but not ours) → treated as alive is // unsafe here — a stale pid we can't probe is better reported as "not running" so the // caller spawns a fresh daemon rather than connecting to a dead socket. return false; }}
/** Runtime shape check for a parsed daemon boot descriptor. */function isDaemonMeta(v: unknown): v is DaemonMeta { if (v === null || typeof v !== 'object') return false; const m = v as Record<string, unknown>; return ( typeof m.pid === 'number' && typeof m.socketPath === 'string' && typeof m.protocolVersion === 'string' && typeof m.startedAt === 'number' && (m.webHost === undefined || typeof m.webHost === 'string') && (m.webPort === undefined || (typeof m.webPort === 'number' && Number.isInteger(m.webPort))) && (m.webAuth === undefined || m.webAuth === 'enrollment') );}