UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

159 lines (158 loc) • 9 kB
import { type ProcessCensus, type ProcessUncheckable } from "../../core/process-identity.js"; import { type AnchorReading, type AnchorRecord } from "./process-anchor.js"; /** * The ONE reading of a file store's directory lock — what its record says, who holds it, and what a person can do about it. * * Three places ask: taking a lock ({@link import("./fs-atomic.js").BootLock}), the refusal it throws, and the listing of a data * root's locks ({@link readDataRootLocks}). They all read through {@link inspectLockAt}, so a host never needs a second judgment of * its own (and the listing and the refusal can never disagree about the same file). */ /** The names of the locks that guard a directory for a whole process's life — the one table the listing and every taker read. */ export declare const STORE_LOCK_NAMES: readonly ["LOCK", "ADOPTION-LOCK", "PERMISSION-RULES-WRITER-LOCK"]; /** A name from {@link STORE_LOCK_NAMES}: a directory fence is only ever taken under one of these. */ export type StoreLockName = (typeof STORE_LOCK_NAMES)[number]; /** The on-disk spelling of a lock record. Bumped only when the SHAPE changes, never when a value does. */ export declare const LOCK_RECORD_VERSION = 2; /** What a lock file names: the fingerprint of the process that published it, and its anchor when it made one. */ export interface LockRecord { readonly pid: number; readonly startedAtMs: number | null; readonly startedSinceBootMs: number | null; readonly host: string | null; readonly pidNs: string | null; readonly anchor?: AnchorRecord; /** Drawn at each publish (32 hex): which process wrote these bytes, when the bytes alone cannot say ({@link publishedHere}). */ readonly nonce?: string; } /** Why a lock record could not be read: its bytes are not one, a later engine wrote it, or a member has the wrong type. */ type UnreadWhy = "bytes" | "version" | "member_type"; /** * Who holds a directory lock, as this process can tell. * - `this_process` — a record this process published (its nonce was minted here), whatever the table says; a second open of the * same directory here is a conflict too. A record that only names this process's number is judged like any other holder's. * - `running` — a live process: proven by this process table (`by: "process_table"`), or by its anchor being held open * (`by: "anchor"`; `pid` then names a process in ITS OWN process table, which may be another container's, and `host` — present * when the record names one — is the host name that process ran under: where a person looks for it). * - `gone` — proven gone: the next acquisition takes the lock over. `by` names the proof (the process table, or the process * table under the host's declaration of one process table for the data root; `anchor` is reserved for a closed anchor's proof, * which this release reports and does not act on, so it is never given yet). * - `uncheckable` — neither proven running nor proven gone: `why` is the process table's reason, `anchor` what the anchor says * (`absent` when the record names none), `fsType` the file system the directory is on when that is why no anchor is made. * - `unread` — the record could not be read (`why`); nothing about its holder is judged. */ export type LockHolder = { readonly state: "this_process"; } | { readonly state: "running"; readonly pid: number; readonly by: "process_table" | "anchor"; readonly host?: string; } | { readonly state: "gone"; readonly by: "process_table" | "anchor" | "declaration"; } | { readonly state: "uncheckable"; readonly pid: number; readonly why: ProcessUncheckable; readonly anchor: AnchorReading; readonly fsType?: string; } | { readonly state: "unread"; readonly pid?: number; readonly why: UnreadWhy; }; /** * What a person (or the host acting for one) can do about a lock that refuses: remove the lock file once no process uses it, or * declare that every process opening the data root runs in this one process table — offered only where that declaration would * judge this very lock gone. */ export type LockRemedy = { readonly kind: "remove_lock"; readonly path: string; } | { readonly kind: "declare_single_process_table"; readonly root: string; }; /** One lock, read: where it is, its form, who holds it, and what can be done about it. */ export interface LockReading { readonly path: string; readonly form: "file"; readonly holder: LockHolder; readonly remedies: readonly LockRemedy[]; } export type { AnchorReading }; /** The bytes THIS process publishes into a lock file — one line of JSON, complete before it is linked. */ export declare function serializeLockRecord(anchor?: AnchorRecord): string; /** Whether `record` is one this process published (its nonce was minted here). */ export declare function publishedHere(record: LockRecord): boolean; type Parsed = { readonly record: LockRecord; } | { readonly unread: UnreadWhy; readonly pid?: number; }; /** Hold a declared root (its physical path). The door that judges the declaration and resolves the root is `declareDataRoot`. */ export declare function holdSingleProcessTableRoot(physicalRoot: string): void; /** A lock file's bytes, read once: absent, or the raw bytes and what they parse to. */ export type LockBytes = { readonly state: "absent"; } | { readonly state: "present"; readonly raw: string; readonly parsed: Parsed; }; /** Read a lock file's bytes (no judgment yet). A read that fails for any reason but absence reads as bytes that are no record. */ export declare function readLockBytes(lockPath: string): LockBytes; /** The parsed record of read bytes, when they were a record. */ export declare function lockRecordOf(bytes: LockBytes): LockRecord | undefined; /** A judged lock: the reading, and — for the one acquisition that removes on it — the declared root the verdict rests on. */ export interface JudgedLock { readonly reading: LockReading; readonly declaredRoot?: string; } /** * Judge read bytes: the process table first, then the anchor, then the three sentences — * · the table OR the anchor says running ⇒ `running` (an anchor held open is a holder, whatever the table says); * · otherwise the table says gone ⇒ `gone`; * · otherwise `uncheckable`, with the table's reason and the anchor's reading. * A CLOSED anchor is reported and never acted on: it does not make a holder gone in this release. */ export declare function judgeLockBytes(lockPath: string, bytes: Extract<LockBytes, { state: "present"; }>, census: ProcessCensus): JudgedLock; /** Read and judge one lock file: `undefined` when there is none. */ export declare function readLockAt(lockPath: string, census?: ProcessCensus): LockReading | undefined; /** The anchor names the lock records in `dir` carry — a sweep of leftover anchors there must not remove what a record names. */ export declare function anchorNamesReferencedIn(dir: string): ReadonlySet<string>; /** * THE clause every refusal of a held lock says, whichever store words the rest: who holds `where` (the lock, named by the caller — * a path for an operator, a phrase for a reader who is given no location), and what the remedies are. One clause, so the facts * and the advice are said the same way everywhere; the advice is generated from the remedies, so a host that renders its own * advice from {@link LockRemedy} and one that shows this sentence never disagree. No library call is named. */ export declare function lockRefusalClause(reading: { readonly holder?: LockHolder; readonly remedies: readonly LockRemedy[]; }, where: string): string; /** A data root's directory locks, read: every lock in place, and every directory the walk could not enter. */ export interface DataRootLocks { readonly root: string; readonly locks: readonly LockReading[]; readonly unwalked: readonly { readonly path: string; readonly code: string; }[]; } /** * Read every directory lock under a data root — the same reading an open of that directory makes, so a host can show what is * there, who holds it and what can be done, without opening a store or judging anything itself. * * It walks `root` and the directories below it to {@link MAX_LOCK_LISTING_DEPTH} levels, follows no symbolic link, skips anchor * directories, and recognises locks by the engine's own table of lock names (plus a reclaim gate an earlier engine left beside * one). Locks taken for a single operation (a mailbox's, a claim's removal gate) are not listed: they do not keep a store from * opening. A directory that cannot be read is listed in `unwalked` with its error code — never silently skipped. Nothing is * created, written or removed. * * Refused `config.invalid_argument` for a `root` that is not a non-empty string. */ export declare function readDataRootLocks(root: string): DataRootLocks;