UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

165 lines (164 loc) • 11.5 kB
/** * Process identity — the ONE judgment of "is the process this record names still the one that wrote it". * * Every durable record that names a process owes this judgment: a file store's directory lock, a run's * claim, a memory writer's session account. A pid alone is not an identity (the OS hands the number out * again once its holder exits), and a pid is only meaningful inside the process table it was read from * (another host's, another container's pid names nothing here). So the judgment has two halves, asked in * this order: * * 1. COMPARABILITY — was the record written against THIS process table? Same host name, and the same pid * namespace where this host can name one. A record that is not comparable is never judged: its pid is * a number from somewhere else. * 2. IDENTITY — in this table, does the pid still exist (only ESRCH says it does not), and has the start * time the record carries been falsified by the process that holds the pid now? * * The asymmetry is the whole safety argument, and every degrade lands on it: a false "gone" hands a working * process's directory, claim or uncommitted edits to someone else, while a false "live" costs a refusal * that names its cause and its remedy. So anything this module cannot establish reads as live. */ /** * A process identity that survives PID REUSE: a pid, WHEN that pid's process started, and the process * table the observation was made in (the host, and its pid namespace where one can be named). * * The start time is carried TWICE, against two different clocks, and that is the whole robustness of the * thing. Both numbers come from ONE reading of the process's elapsed time, but one is subtracted from the * WALL clock and the other from the host's UPTIME — and a platform that leaves one of them unmoved when * the wall clock is corrected moves the other. (Where `ps` measures elapsed time against the boot clock, a * corrected wall clock shifts the derived wall-clock start and leaves the uptime-relative one alone; where * it measures against a stored absolute start, the reverse holds.) A comparison that insists on * BOTH before calling a record stale therefore cannot be talked into a reclaim by a clock correction. * See {@link processCensus}. * * Both are `null` when this host cannot produce a start time (no process table reader: a `/proc` that is not * this process's own or cannot be read on Linux, no `ps` or a cut-down one that rejects the query elsewhere — see * `process-table.ts`; a probe too slow for its own reading to be worth anything, a reading no process could have given — * a start before the boot or the epoch, or two starts that name no boot a process could have run in). That is * a plain "unknown" — never a zero and never a guess — and every reader treats an unobtainable fingerprint as * UNFALSIFIABLE. A number is never negative, and the two never contradict each other: a record that carries * such starts anyway (written by an engine before this check, or by hand) is read as carrying none * ({@link startsOf}). */ export interface ProcessFingerprint { /** The process id the rest of the record is about. */ readonly pid: number; /** Start of that pid's process against the WALL clock, to the second; `null` when unobtainable here. */ readonly startedAtMs: number | null; /** The same start against this host's UPTIME (ms after boot), to the second; `null` likewise. */ readonly startedSinceBootMs: number | null; /** The host both were read on — a fingerprint from ANOTHER host is comparable to nothing here. */ readonly host: string; /** * The pid namespace the pid was read in (Linux: the `/proc/self/ns/pid` link, e.g. `pid:[4026531836]`). * ABSENT where this host has no namespace it can name (macOS; a Linux whose `/proc` cannot be read) — * on macOS the host name alone then decides comparability; on Linux a record without one is never * comparable (see {@link processCensus}). Two containers can share a host name (a restarted * container keeps its name) while their pids live in different tables, and the namespace is what tells * them apart; a record that names no namespace, read on a host that has one, is therefore not * comparable (it may come from either table) and is never judged gone. */ readonly pidNs?: string; } /** What a record carries about a process — a {@link ProcessFingerprint}, any part of which an older or * foreign spelling may lack (`null` and absent mean the same: not recorded). */ export interface ProcessRecord { readonly pid: number; readonly startedAtMs?: number | null; readonly startedSinceBootMs?: number | null; readonly host?: string | null; readonly pidNs?: string | null; } /** Why a record could not be judged from here — each one a reason it may still be running. */ export type ProcessUncheckable = /** Written on another host: its pid names a process in another process table. */ "another_host" /** Written in another pid namespace (another container), or in one this process cannot name its own against. */ | "another_pid_namespace" /** Written without naming its pid namespace (an older engine, or a writer that could not read it), and read * on a platform that has them (Linux, whether or not this process can read its own): it may come from any * process table that shares this host name. */ | "pid_namespace_unrecorded" /** The pid exists, but no start time could be compared: a process now holding that number cannot be told * apart from the one that wrote the record. */ | "start_time_unknown" /** The process table answered the signal probe with something other than "no such process" or "exists". */ | "probe_refused"; /** The judgment of one record: proven gone, proven still running, or not judgeable from here (and why). */ export type ProcessVerdict = { readonly state: "gone"; } | { readonly state: "running"; } | { readonly state: "uncheckable"; readonly why: ProcessUncheckable; }; /** What a census is told besides the records it judges. */ export interface ProcessCensusOptions { /** * The host has declared that every process which could have written the records this census judges runs in THIS process * table — no other container and no other host opens what those records guard. A record that names NO pid namespace is * then read as naming this one, and judged like any record written here. It fills in only what a record leaves unsaid: a * record that names another namespace, or another host, is exactly as uncomparable as without it. */ readonly singleProcessTable?: true; } /** One question session over the process table. */ export interface ProcessCensus { /** The verdict on one record. Within one census each pid is signalled at most once and its start time is * probed at most once, however many records name it. */ verdict(record: ProcessRecord): ProcessVerdict; } /** The sentence fragment naming why a record could not be judged from here. */ export declare function uncheckableProcessText(why: ProcessUncheckable): string; /** * The fingerprint of a process: this one when called with no pid, any other pid when named. The process * table it names is this one — the pid is read in this host's (and this namespace's) table. * * ONE derivation for both halves of the mechanism — the record a holder WRITES and the probe a later reader * verifies it with — on purpose. Deriving our own start time from `process.uptime()` instead would be * cheaper, but it measures from Node's own initialisation rather than from the kernel's `exec`, so a busy * machine can put the two derivations seconds apart, and a reader comparing across them would read a * healthy holder as gone. One derivation makes the tolerance a rounding allowance instead of a guess. * * The self case is memoized: our start time cannot change, the probe is a subprocess, and a process pays * for it at most once, whatever records it writes. */ export declare function processFingerprint(pid?: number): ProcessFingerprint; /** * A question session over the process table: judge any number of records, signalling each pid at most once * and probing each pid's start time at most once (the probe is a subprocess, bounded at 2 s × 3 attempts, so * a census that re-asked per record would multiply that by the record count). * * The verdict, in order: * 1. COMPARABLE? A record written on another host, or in a pid namespace that is not provably this one * (it names another; it names one and this process cannot name its own; it names none and this host has * one, or this platform has namespaces at all — Linux, even where this process cannot read its own), is * `uncheckable` — and the pid is never signalled: a number from another table names nothing here, and * signalling it would judge whatever unrelated process happens to hold it. With no namespace on either * side of a platform that has none (macOS), the host name decides alone. A record naming no host at all (a pre-fingerprint * spelling) is compared on the namespace alone. A record naming no namespace is read as naming this one * when the census was told so ({@link ProcessCensusOptions.singleProcessTable}) — the one input the host * can supply, and only to this step. * 2. EXISTS? `kill(pid, 0)`: only ESRCH is `gone`. EPERM is a process that exists; any other error is the * table refusing the question — `uncheckable`, never `gone`. * 3. SAME PROCESS? The record's start time against the one observed now. One-sided: whatever holds a * recycled pid necessarily started AFTER the record's writer did (the number was not free until it * exited), so only a LATER observation can falsify, beyond {@link START_TIME_TOLERANCE_MS}. And BOTH * clocks have to condemn the record: the wall-derived start and the uptime-derived one each move under a * clock correction on exactly the platform where the other does not, so one alone never authorises a * "gone". The uptime measure has a second way to condemn, a proof rather than a comparison: a start beyond * this host's own uptime names a boot that is over. A record and an observation sharing no measure at all * are `uncheckable` (`start_time_unknown`); otherwise the process is `running`. A start no process could have * had (negative: a reading that wrapped when it was taken) is no measure, and neither is either start of a * pair that names no boot a process could have run in ({@link startsOf}: a boot before the epoch, or a boot * that is over for a process whose wall start lies inside this one) — a record stuck with such starts is * `uncheckable` like a record that carries none, never "started long before you". */ export declare function processCensus(options?: ProcessCensusOptions): ProcessCensus; /** * Is the process a record names possibly still the one that wrote it? `false` ONLY when it is proven gone * (see {@link processCensus}): a record from another process table, a pid the table will not answer about, * and a fingerprint nothing can falsify all read as live — a false "gone" hands a working process's * directory or claim to someone else, while a false "live" costs a refusal an operator can act on. */ export declare function isProcessLive(record: ProcessRecord): boolean;