UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

52 lines (51 loc) • 4.11 kB
/** * A store holder's one "closed" latch — once a holder is closed it lays nothing more down under the data root. * * THE RULE. A write verb that BEGINS after its holder was closed is refused and touches nothing on disk: after a file backend's * `dispose`, every store it built; after a store object's own `close` / `dispose`, that object. Creating a row, removing or stamping * a row that is not there, and a migration write a read makes on the way are write verbs like any other. A write that began before * the close and lands after it (it awaited first) is not this rule's: it lands as it did before. A holder that was not closed writes * as before — another backend over the same root, a store the host built itself, the memory engine, the exported write primitives. * * THE SHAPE OF A REFUSAL is the verb's own failure shape: a verb that throws throws a plain error carrying `code: "log_closed"` (the * code a closed session log already refuses with; it joins no store's typed error set); a verb that answers a failure result answers * its own failure arm (the file-history verbs' `*_failed`, whose "nothing was persisted" holds at a close). * * ONE LATCH PER HOLDER. The backend holds one; each store holds its own, and a store the backend built is handed the backend's by name * right after its construction ({@link builtUnder}); a store's own close sets its own. A write verb reads its store's latch on entry, * before its first await; a session object a repo handed out asks that repo's latch before each of its writes. The latch is never keyed * by the root's path: a backend opened later over the same root holds a latch of its own. * * THE STATIC FALLBACK. Each store interface the backend hands out carries a closed table ({@link VerbTable}) naming every member a * `"write"` or a `"read"`; a member added to the interface and not named does not compile, and one contract pin drives every * `"write"` member of every table once its holder is closed. */ /** What one member of a store interface is to this rule: `"write"` — it may lay bytes down under the data root or remove them, so it is * refused once its holder is closed; `"read"` — anything else (a read, a view handed out, a cache-only step, a declared fact, the close). */ export type VerbClass = "write" | "read"; /** A store interface's closed table: every member named, a member surface that carries verbs of its own (a nested capability) named by * its own table. Written `satisfies VerbTable<TheInterface>`, so an interface member left out does not compile. */ export type VerbTable<I> = { readonly [K in keyof I]-?: VerbClass | VerbTable<NonNullable<I[K]>>; }; export declare class HolderLatch { private shut; private readonly holder; /** `holder` = the store this latch belongs to (a backend that built it hands it its own latch, {@link builtUnder}); absent = a holder no * backend can build (the backend itself). */ constructor(holder?: object); /** Close this holder: every write verb that begins from here on is refused. `true` for the call that closed it, `false` for a repeat — * what a store's own idempotent close reads (a backend's dispose having closed the holder that built it does not count here). */ close(): boolean; /** Whether this holder, or the backend that built it, is closed. */ get closed(): boolean; /** For a verb that throws: refuse `verb` once the holder is closed (see the module rule), before anything is touched. */ refuse(verb: string): void; } /** The refusal a throwing write verb gives once its holder is closed: a plain error carrying `code: "log_closed"`. */ export declare function closedHolderRefusal(verb: string): Error & { code: "log_closed"; }; /** A backend hands a store it has just built its own latch, by name: the store is closed when the backend is. Only the stores the * backend constructs itself are handed it — a store the host builds meanwhile (in an option getter the backend reads) is not. */ export declare function builtUnder<T extends object>(store: T, backend: HolderLatch): T;