@sema-agent/core
Version:
Stateless, task-oriented AI agent core
68 lines (67 loc) • 5.06 kB
TypeScript
/**
* THE process table, read — the one reader every question about other processes asks: when did a pid's process start (the
* fingerprint a lock, a claim or a writer's account carries), which processes descend from a pid (the process-tree kill), and
* whether a process group still has a member that can run (a host deciding whether a group is finished).
*
* Where it is read, by platform and never by what happens to be installed:
* · Linux — `/proc` itself (`/proc/<pid>/stat`), not `ps`: a `ps` on Linux reads the same files, and a container image without
* `ps` would otherwise have no reading at all. `/proc` is believed only when it is THIS process's: `/proc/self` names this
* process's own pid. A `/proc` mounted from another pid namespace (a child namespace that kept its parent's mount) lists that
* namespace's processes under that namespace's numbers, so a pid read there is somebody else; such a `/proc`, and one that
* cannot be read, is a table this process cannot read.
* · Other POSIX platforms (macOS, the BSDs) — `ps`, with the POSIX `etime` field (elapsed since start).
* · Windows — none.
*
* The Linux start field (`starttime`, field 22) counts clock ticks since boot at the kernel's USER_HZ, which is 100 on every
* architecture Node runs on; a start derived from it is checked by the fingerprint's own reading of "is this a start" like any
* other, so a kernel with another tick rate yields no reading for this process rather than a wrong one.
*/
/** What a table read answers when it cannot answer: the reason, in words for a notice. */
export interface TableUnreadable {
readonly unreadable: string;
}
/**
* How long ago `pid`'s process started, in ms, measured against the boot clock reading `uptimeNowMs` (sampled by the caller just
* before this call): a number; `null` when the table has no such row (the process is gone — an answer, not a failure); or why the
* table could not be asked.
*/
export declare function processElapsedMs(pid: number, uptimeNowMs: number): number | null | TableUnreadable;
/** One row of the table: the parent, the process group, whether the process has exited and waits to be reaped (a zombie), and a
* mark that tells the same process from a later one holding its number ({@link sameProcessMark}). */
export interface ProcessRow {
readonly pid: number;
readonly ppid: number;
readonly pgid: number;
readonly exited: boolean;
readonly mark: string;
}
/** The rows read, and how many processes the table listed whose row it would not show (`/proc` mounted `hidepid`: another
* user's process is listed and cannot be read) — a row it hid may be anyone's, in any group. */
export type ProcessTable = ReadonlyMap<number, ProcessRow> & {
readonly hidden?: number;
};
/** The whole table, every row read at one moment — or why it could not be read. `psTimeoutMs` bounds the `ps` run where the
* table is read through one (a kill path waits less than a question does). */
export declare function readProcessTable(psTimeoutMs?: number): ProcessTable | TableUnreadable;
/**
* Whether a row read later (`now`) is the same process as the row read earlier (`then`) under one number. `/proc` gives the exact
* start tick (same tick ⇔ same process); `ps` gives the elapsed time, which only grows for one process, while a process that took
* the number after it started later (so it reads younger) — unless the newcomer happens to be older than the first reading, the
* window every elapsed-time comparison leaves. Marks of two different readers, or unreadable ones, are not the same process.
*/
export declare function sameProcessMark(then: string, now: string): boolean;
/** What a process group holds: a member that can still run, only members that have exited and wait to be reaped, nothing, or
* members this process cannot see (the table could not be read, or it hides a group the kernel says exists). */
export type ProcessGroupReading = "running_member" | "exited_only" | "empty" | "unreadable";
/**
* Read process group `pgid`: whether anything in it can still run. A signal probe on the group (`kill(-pgid, 0)`) succeeds for a
* group whose every member has exited and not been reaped, so it cannot tell a finished group from a working one; this reads the
* members' states. `empty` is said only when the kernel itself has no such group; a group the kernel has and the table does not
* show (a `/proc` mounted `hidepid`, a table that could not be read) is `unreadable` — a caller treats that as still running.
*
* @contract process_group.reading — the four answers are a closed set; `running_member` and `unreadable` both mean "do not assume it
* is finished".
*/
export declare function processGroupReading(pgid: number): ProcessGroupReading;
/** {@link processGroupReading} against a table already read (a caller that read it for another question at the same moment). */
export declare function processGroupReadingIn(table: ProcessTable | TableUnreadable, pgid: number): ProcessGroupReading;