@sema-agent/core
Version:
Stateless, task-oriented AI agent core
101 lines (100 loc) • 7.77 kB
TypeScript
import { appendFileSync as nodeAppendFileSync, chmodSync as nodeChmodSync, copyFileSync as nodeCopyFileSync, linkSync as nodeLinkSync, mkdirSync as nodeMkdirSync, openSync as nodeOpenSync, readdirSync as nodeReaddirSync, readFileSync as nodeReadFileSync, renameSync as nodeRenameSync, rmdirSync as nodeRmdirSync, rmSync as nodeRmSync, unlinkSync as nodeUnlinkSync, utimesSync as nodeUtimesSync, writeFileSync as nodeWriteFileSync } from "node:fs";
import { openExclusiveTemp as coreOpenExclusiveTemp } from "../../core/exclusive-temp.js";
import { type EngineNotice } from "../../core/engine-notice.js";
/**
* The file store's descriptors: the append logs' budget, and the one rule every descriptor the store takes goes through
* ({@link takeYielding}). The store's modules take the functions that open a descriptor from here, not from `node:fs`
* (`openSync`, `readFileSync`, `writeFileSync`, `appendFileSync`, `readdirSync`, `rmSync`, `copyFileSync`, and the engine's
* `openExclusiveTemp`) — each the same function, run through the rule when it is called with a path (a call on a descriptor already
* open takes none, and runs as it is).
*/
/** What {@link appendLogDescriptorStats} answers: the budget in force and what the append logs of this loaded module did under it. */
export interface AppendLogDescriptorStats {
/** The most descriptors the append logs of this loaded module hold at once ({@link setAppendLogDescriptorBudget}). */
readonly budget: number;
/** Descriptors held now. */
readonly held: number;
/** The most held at once since this module loaded. */
readonly peak: number;
/** Descriptors released because the budget was reached (the log keeps its file and reopens on its next write). */
readonly parks: number;
/** Released descriptors opened again by a write. */
readonly reopens: number;
/** Appends refused because the path no longer named the file the log was bound to (`log_file_replaced`). */
readonly refusals: number;
}
/** Why the append logs parked: the budget was reached, or an open met the process's descriptor limit and lowered the budget. */
type AppendLogParkCause = "budget_reached" | "descriptors_exhausted";
/**
* The descriptor pool of every append log in this loaded module: the logs that hold a descriptor now, least recently written first
* (Map insertion order), each with the closure that parks it, and the counters {@link appendLogDescriptorStats} reads. It holds only
* the logs that hold a descriptor, so it never keeps more than the budget of them alive. `announced` holds every (cause, budget value)
* whose first park has been said (one `store.append_log_parked` per cause and value, for the life of the module): the process's
* descriptor limit met is said at least once, even at a budget value an earlier reaching of the budget already said.
*/
export declare const descriptorPool: {
budget: number;
held: Map<object, () => void>;
peak: number;
parks: number;
reopens: number;
refusals: number;
announced: Set<string>;
};
/** Park the least recently written logs other than `except` until at most `keep` hold a descriptor. With nothing left to park it
* stops. The first park under a budget value is said once ({@link announceParking}). */
export declare function parkDownTo(keep: number, except: object | undefined, cause: AppendLogParkCause): void;
/**
* The one rule every descriptor the file store takes goes through: `attempt` (an open, a read or write of a whole file, a listing, a
* removal) that meets the process's descriptor limit ({@link metDescriptorLimit}) while append logs hold descriptors lowers the budget
* in force to half of what they hold (at least one), parks down to it — the rest of the process gets descriptors back, not only this
* attempt — and tries again. With no log holding a descriptor the error is thrown as it is. Which open meets the limit first (a log's,
* a session's staging file, a session file read again, a directory a recursive removal walks) makes no difference: each yields the same
* way, so a process that runs out of descriptors once lowers the budget instead of keeping every log's descriptor and refusing every
* open after.
*/
export declare function takeYielding<T>(attempt: () => T): T;
/** Install the throat's question (the lock module, once). */
export declare function setLostLockGuard(guard: (target: string) => void): void;
export declare const openSync: typeof nodeOpenSync;
/** The lock machinery's own opens (an anchor's pipe, the probe of another holder's): never asked about a lost lock — they are how
* one is judged. Only the anchor module takes this (a gate pins that). */
export declare const openSyncUnguarded: typeof nodeOpenSync;
export declare const readFileSync: typeof nodeReadFileSync;
export declare const writeFileSync: typeof nodeWriteFileSync;
export declare const appendFileSync: typeof nodeAppendFileSync;
export declare const readdirSync: typeof nodeReaddirSync;
export declare const rmSync: typeof nodeRmSync;
export declare const copyFileSync: typeof nodeCopyFileSync;
export declare const openExclusiveTemp: typeof coreOpenExclusiveTemp;
export declare const unlinkSync: typeof nodeUnlinkSync;
/** A lock file's removal by the lock machinery that judged it (a hand-back of one's own after its bytes were checked, a reclaim of a
* holder shown gone, an abandoned one taken back): never asked about a lost lock — that is how a lock, lost or not, is handed back,
* and a lock left on disk naming a live process blocks every other one. Every other removal goes through the throat. */
export declare const unlinkLockFile: typeof nodeUnlinkSync;
export declare const renameSync: typeof nodeRenameSync;
export declare const linkSync: typeof nodeLinkSync;
export declare const mkdirSync: typeof nodeMkdirSync;
export declare const rmdirSync: typeof nodeRmdirSync;
export declare const utimesSync: typeof nodeUtimesSync;
export declare const chmodSync: typeof nodeChmodSync;
/**
* Set how many descriptors the append logs of this loaded module may hold at once — the file backend's session, mailbox, journal
* and ledger logs and any log a host builds with `AppendLog`. A log past the budget parks: its descriptor is closed, the file it
* is bound to is kept, and its next write opens it again. A whole number from 1 to 65536; anything else is refused
* (`config.invalid_argument`) and the budget in force stays. A budget below what is held now parks down to it at once. The budget is
* this module instance's, not the process's: a second loaded copy of the package, or a worker thread, has its own. Any open of the
* file store that meets the process's descriptor limit lowers the budget in force on its own (to half of what the logs held then;
* {@link takeYielding}); it stays lowered until this is called again.
*/
export declare function setAppendLogDescriptorBudget(budget: number): void;
/** The budget in force and what the append logs of this loaded module did under it — plain data, read at the call. */
export declare function appendLogDescriptorStats(): AppendLogDescriptorStats;
/**
* Say `notice` on the default seat AFTER the synchronous work that raised it has finished — never inside it. A store's write is a
* synchronous critical section that spans more than the log (the session store checks the leaf, appends, then moves the leaf), and a
* seat is host code that may write to the same store; called from inside, its write would pass the same check. One microtask per
* notice, so notices are said in the order they were raised. A process that exits synchronously before the microtask runs loses them.
*/
export declare function queueStoreNotice(notice: EngineNotice): void;
export {};