UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

94 lines (93 loc) • 7.6 kB
/** * The process anchor — a second witness, beside the process table, to "is the process a directory lock names still there". * * A lock record names its writer by pid, and a pid only means something in the process table it was read in: a lock left by * another container (another pid namespace) cannot be judged by pid at all. The anchor is a named pipe (FIFO) the holder creates * in the lock's directory (`<lock dir>/.anchors/p.<id>`) and keeps OPEN for as long as it holds a lock there; the kernel closes * it the instant the process ends, however it ends. Anyone can then ask the kernel whether some process has it open: opening a * FIFO for writing without blocking fails with `ENXIO` exactly when no process has it open for reading — a question that needs * no pid and no shared process table, only the same kernel and the same file. * * What it proves, and what it does not, is decided here and nowhere else: * - OPEN — some process has the pipe open, so the lock is treated as held (a working holder is never judged gone). A tool * that happens to read the pipe, or a descriptor a holder passed down on purpose, keeps it reading open: that costs a refusal. * - CLOSED — only when the reader can show it is asking the same kernel, in the same boot, about the same pipe on a local disk * (the five conditions in {@link readAnchor}); anything it cannot show leaves the anchor without a vote. * * The holder side is Linux-only (the platform with pid namespaces) and only on a local file system: no FIFO is left on a network * disk or in a user's macOS directory. The descriptor is a plain number held in this module's table — never a `FileHandle`, * which one runtime closes when it is garbage-collected — and it is opened close-on-exec, so a child process never inherits it. */ /** * The name of the directory, inside a lock's directory (`<lock dir>/.anchors`), that holds the anchors of the processes holding locks there * (`p.<32 hex>`, and `t.<32 hex>` while one is being made). On Linux, for a store on a local disk, each entry is a NAMED PIPE (FIFO) that its holder keeps * OPEN for as long as it holds a lock there — so a tool that reads a data root file by file (a backup, an archive, a diagnostic, a test fixture) must SKIP * every directory of this name, by name, before it opens anything in it: while its holder lives the pipe opens (the holder keeps both ends) and a read of it blocks * the calling thread, since nobody writes to it; one a killed holder left behind has no one holding it, and a read-only `open` of it blocks until a writer comes. A directory of this name is no data (it holds no row of any store), and nothing * under it is worth copying: the next holder makes its own. On macOS and on a network disk the engine makes none, so a reader there cannot meet one. * (Exported at the package root as `PROCESS_ANCHOR_DIR` — this very constant, which the engine's own readers and writers use.) */ export declare const ANCHOR_DIR = ".anchors"; /** What a lock record carries about its writer's anchor. `dev` / `ino` are decimal strings (an inode number can exceed 2^53). */ export interface AnchorRecord { /** `p.<32 hex>` — the anchor's file name under the lock directory's {@link ANCHOR_DIR}. */ readonly name: string; readonly dev: string; readonly ino: string; /** The `statfs` type of the anchor's file system, as its holder saw it. */ readonly fsType: number; /** The kernel's boot id when the anchor was made (`/proc/sys/kernel/random/boot_id`), `null` where it could not be read. */ readonly bootId: string | null; } /** Why an anchor could not vouch that its holder is gone — every one leaves the judgment to the process table. */ export type AnchorUnproven = "kernel_unknown" | "kernel_differs" | "host_unknown" | "host_differs" | "filesystem_unknown" | "filesystem_not_local" | "filesystem_differs" | "anchor_missing" | "anchor_unreadable" | "anchor_not_fifo" | "anchor_replaced" | "probe_refused"; /** * What a lock's anchor says: `open` (some process holds it open), `closed` (no process does — proven), `absent` (the record names * no anchor), or why it could not say. */ export type AnchorReading = "open" | "closed" | "absent" | AnchorUnproven; /** A record's `anchor` member, read strictly: the record if every part has its shape, `undefined` for one that does not. */ export declare function readAnchorMember(value: unknown): AnchorRecord | undefined; /** * The name of the file system `dir` is on, for a refusal's words — only where it is NOT one an anchor is built on, and only on * the platform that builds anchors (elsewhere no anchor is ever built, whatever the disk). `undefined` otherwise. */ export declare function nonLocalFilesystemOf(dir: string): string | undefined; /** * Read a lock's anchor. `open` needs only that the very pipe the record names is open (no proof of anything else — reading a * holder as present is the safe direction). `closed` needs ALL of, in this order, each answering the first reason it fails: * K3 the record's pipe is at its name, a FIFO, with the recorded device and inode; * K4 opening it write-only without blocking answers `ENXIO` (and only that code — a unix socket on Linux answers it too, so K3 * is asked again afterwards); * K0 the record's boot id is this kernel's (another boot, or another machine with the same disk, would share nothing); * K1 the record's host is this host name; * K2 the anchor's file system is a local one, of the type its holder saw. */ export declare function readAnchor(lockDir: string, anchor: AnchorRecord, recordHost: string | null): AnchorReading; /** One holding of an anchor: the record member a lock written now carries, and the hand-back. */ export interface AnchorHold { readonly record: AnchorRecord; /** Whether this holding created the anchor (the first lock of this process in the directory). */ readonly fresh: boolean; /** Give this holding back; the last one closes the pipe and removes it. Idempotent. */ release(): void; } /** * Hold this process's anchor for a lock about to be published at `lockPath` — making it if this is the process's first lock in * that directory. `undefined` where no anchor is made (any platform but Linux; a file system that is not a local disk; no * `mkfifo`): the lock is then published without one and judged by the process table alone. */ export declare function holdAnchor(lockPath: string, lockDir: string): AnchorHold | undefined; /** * The lock a write to `target` would land under while this process has lost it — the first lock of a directory whose anchor is no * longer the pipe this process opened, at or above `target` — or `undefined` (no anchor held over it, or every one still holds). * Costs nothing where this process holds no anchor (every platform but Linux), one `fstat` per covering anchor otherwise. */ export declare function lostLockCovering(target: string): string | undefined; /** * Remove the anchors other processes left in `lockDir` that nobody holds any more — each only after it is shown to be a pipe no * process has open — except the ones a lock record still there names (`referenced`: that record's reader must still be able to * read its anchor, read again right before each removal) and this process's own. A half-made anchor (`t.*`) goes once it is an * hour old and no process holds it open. Best effort throughout. */ export declare function sweepLeftoverAnchors(lockDir: string, referenced: () => ReadonlySet<string>): void;