@sema-agent/core
Version:
Stateless, task-oriented AI agent core
136 lines (135 loc) • 11.3 kB
TypeScript
/**
* The ONE receipt of a caller's object: how a value someone else's code built becomes a value the receiver owns.
* Every reader of a caller's object reads it here — a constructor's configuration (`new Runner`'s deps, a brain's
* config), a station's answer record, a mods handler's payload and answer, what a tool's `execute` returns, the two trees a stored
* checkpoint row's index facts read (its gate and its pending action: the top record only, `INDEX_TREE_DATA`) — each with its own mode, never its own
* copy of the walk.
*
* The walk: a primitive is itself. A CONTAINER — an ordinary record (prototype `Object.prototype` or none) or an
* ordinary array — within `depth` levels is copied member by member off its own property descriptors, captured in
* one call before any of the caller's code can run (under `wire`, read live in the order `JSON.stringify` reads them); each
* member lands as an enumerable data property of the copy, which is filled on no prototype (so no setter up a chain runs and
* an own `__proto__` key stays a plain key) and handed its ordinary prototype when full. Anything else — a function, an
* object of another prototype, a container past `depth` — is handed as it is, refused, or (an object of another
* prototype under `copy`) copied off its own members like a record, by `others`, which also says whether cycles and
* shared references keep their shape or are refused / walked again (and `leaves` whether what is handed on is a structured
* clone of itself; `wire`, whether the members are those the JSON wire reads). A refusal refuses the whole receipt, or — under
* `contain` — only the member it was met in. The walk keeps its own stack (one frame per container being copied), so
* neither how deep a value nests nor how deep its caller already stands decides a verdict: only `depth` does.
*
* Zero-dependency on purpose (this folder compiles on its own): a Proxy cannot be recognised from here, so the
* host passes its probe (`isProxy`); with a probe, a Proxy is refused before anything is asked of it.
*/
/** How a receipt reads. */
export interface ReceiptMode {
/**
* `call-once`: every own string-keyed member is read — a data property's value, or an accessor's getter called exactly
* once on the object (a getter that throws refuses the receipt; one that moves the object's prototype refuses it, but
* under `others: "copy"`).
* `refuse`: only own enumerable string-keyed DATA properties are members; an accessor, a non-enumerable property or
* a symbol-keyed one refuses the receipt (no getter ever runs) — under `contain`, each is a member left out and reported
* (a symbol-keyed one at its container's path and its `String(symbol)`), its container received.
*/
readonly accessors: "call-once" | "refuse";
/** How many levels of containers are copied (`Infinity` = every level). */
readonly depth: number;
/** What a value that is not copied is (a function, another prototype, a container past `depth`, a symbol or a
* bigint): handed as it is, or a refusal of the receipt. Shared references keep their shape in the copy (one copy
* each); under `as-is` a cycle does too, under `refuse` a cycle is refused and a shared reference must fit `depth`
* where it sits each time it is met. `copy` is `refuse`, except that an object of another prototype within `depth`
* — a class instance, another realm's object or array, an array of a subclass — is copied as an ordinary record (an
* array as an ordinary array) off its OWN members: its prototype is not kept and no code of the prototype runs, so a
* prototype's getters and a built-in's internal state (a `Date`'s time, a `Map`'s entries) are not members and are
* not copied, while every own data member is (and an own accessor is read as `accessors` says; one that moves the
* object's prototype changes nothing the copy holds). A Proxy and a cycle are still refused. */
readonly others: "as-is" | "refuse" | "copy";
/** `refuse`: a container (an ordinary record or array) past `depth` refuses the receipt (`too-deep`) even where
* `others` hands other values as they are — a value of another kind there is still what `others` says — and a shared
* reference must fit `depth` where it sits each time it is met, by the height its first walk found (a walk that ran
* into a container still being walked — a cycle — counts that container as one level, and so does an object the
* reader holds, met directly or in what holds it): `depth` is a bound, never a copy depth. Absent ⇒ a container past
* `depth` is what `others` says. */
readonly past?: "refuse";
/** A fault met in a member — its getter threw, it is a value the receipt refuses, it holds a cycle, it runs too deep,
* or it could not be copied at all (`uncopyable`) — leaves THAT member out of its container's copy and is reported in the
* receipt's `faults` (where it was met); the walk goes on with the next member. A container refused where it was met
* is refused again wherever else it is met (never read twice). Only a fault of the received value itself refuses the
* receipt. Absent ⇒ the first fault refuses the receipt. */
readonly contain?: true;
/** Every container the copy makes is frozen (a received value cannot be changed after it was received). */
readonly freeze?: boolean;
/** The members are the ones the JSON wire carries, read as it reads them, and none of the caller's serializers runs
* (`accessors: "call-once"` only): a record's members are its own enumerable ones (a non-enumerable one is never read), a
* list's its indices (another own member of a list is never read), named when the container is met and each read live, in
* order, walked to its end before the next (a dense list or a record; a sparse list off its own members); an object that
* would be serialized through a `toJSON` (found off
* property descriptors up its prototype chain, nothing run) is not data — refused as `not-data`, its members with it —
* except the platform's own `Date.prototype.toJSON` (by identity), whose answer the receipt reads off the date's own
* time value (an ISO string, or `null` for an invalid date), never through a method the object could override. */
readonly wire?: true;
/** Under `others: "as-is"`, an object that is not copied is replaced by a structured clone of that object alone (one
* clone per object, so a shared one stays shared); a function, and an object the clone refuses (a handle, a class
* instance holding a function), are handed as they are. */
readonly leaves?: "cloned";
/** The host's Proxy probe. */
readonly isProxy?: (value: object) => boolean;
/** Members of the received value itself (its top level only, never `wire`) that are read PER USE rather than at the receipt:
* one of them that is an own accessor is never called by the receipt — the copy carries it as an accessor whose every read
* calls the caller's getter on the caller's object, once per read (a getter that throws, throws at that read). One that is a
* data property is received like any other member. */
readonly live?: readonly string[];
}
/** Why a receipt was refused. `uncopyable` has two causes, and depth is neither (the walk keeps its own stack): reading
* the value threw where none of its getters runs — a Proxy's trap, on a receipt given no Proxy probe; what it threw is
* never read — or the value holds more objects than one receipt can index. */
export declare const RECEIPT_FAULTS: readonly ["proxy", "accessor", "hidden", "getter-threw", "prototype-moved", "not-data", "cycle", "too-deep", "uncopyable"];
export type ReceiptFaultKind = (typeof RECEIPT_FAULTS)[number];
/** A refusal and where in the value it was met (member names from the top). */
export interface ReceiptFault {
readonly kind: ReceiptFaultKind;
readonly path: readonly string[];
}
/** A receipt: the copy (with, under `contain`, every fault a member was left out for), or the refusal. */
export type Receipt = {
readonly ok: true;
readonly value: unknown;
readonly faults?: readonly ReceiptFault[];
} | {
readonly ok: false;
readonly fault: ReceiptFault;
};
/** Whether `v` is an ordinary record: an object whose prototype is `Object.prototype` or none (an array is not one). */
export declare function isPlainRecord(v: unknown): v is Record<string, unknown>;
/** A data member's descriptor, itself on no prototype: a definition reads `get` / `set` / `value` … off its descriptor, and one
* inherited from `Object.prototype` would be read (and run). */
export declare function dataMember(value: unknown): PropertyDescriptor;
/** `value` as the next member of an engine-owned list — defined, never assigned, so a setter some prototype holds at that index
* never runs (a `push` or an assignment to a new index asks the prototype chain first). */
export declare function appendOwn<T>(list: T[], value: T): void;
/**
* Receive `value` by `mode`. `held` names objects the reader already owns: met in the walk, each is kept as itself — not
* received again, never walked — so a member handed back unchanged keeps its identity. The set is consulted as it is,
* never copied, so one set serves every receipt of the same owner.
*/
export declare function receive(value: unknown, mode: ReceiptMode, held?: {
has(value: object): boolean;
}): Receipt;
/** The mods bus's mode: a payload and an answer received whole — no accessor runs, no depth limit, cycles and shared
* references kept, a function or a class instance handed as it is, every copied container frozen (so a member the
* bus holds cannot be changed under it by a handler that holds it too). The engine's own event is received in it as it
* is; what a handler gives the bus — a payload, an answer, a `$` call's arguments — in it held to the bus's one depth
* (the site rules set `depth` and `past: "refuse"`). */
export declare const MODS_PAYLOAD_MODE: ReceiptMode;
/** One clause naming a fault for a sentence ("an accessor at input.deep.x"). */
export declare function describeFault(fault: ReceiptFault): string;
/**
* The SEALED objects reachable from `roots`: every ordinary record or array whose whole graph is frozen data — frozen, of
* the ordinary prototype, every member an enumerable string-keyed data property holding a primitive (no symbol, no
* bigint) or another sealed object. Nothing in such a graph can change, so an answer may hold one as itself and the
* station's every read of it reads what was checked. Read off property descriptors (no getter ever runs), by an explicit
* stack (no depth decides); a cycle of frozen data is sealed. An object of another kind, an unfrozen container and every
* container that reaches one is not sealed: an answer holding it is received like any other value (copied as data, or
* refused); a sealed object an unfrozen container holds is still found. Taken as a snapshot at the moment it is asked —
* what a handler later freezes is not in it.
*/
export declare function sealedObjects(roots: readonly unknown[]): ReadonlySet<object>;