UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

190 lines (189 loc) • 12.9 kB
/** * The current state of a write's target, on the approval card that asks for the write. * * THE RULE (one sentence): a write tool's approval card carries what its target holds now exactly when the model's own `Read` * of that path, issued now, would be served; otherwise the card says why it has none. No read floor of its own, no read path * of its own: the read is the Read tool's — the run's served-read judge (the synthetic `Read` probe with every ask held, so a * read anyone would first be asked about is not served, carrying what the live gate stamps on the model's own `Read` — the * face the run's roster gives `Read` and the leg's facts — so a policy judges the probe as it judges the model's own `Read`; * a roster with no `Read` serves no read at all), then the Read tool's own text read on the execution lane, * its final resolution held to the file the judgment named. Nothing about the read is recorded: the card's read never * stands for the model having read the file (a later `Write` of a file the model never read is still refused). * * WHICH CARDS: a tool whose roster row writes a file on the execution environment ({@link existingTargetCovers}) — the * write family, a path target that is not read-only, the environment leg. Every card in that scope carries the member; * a card outside it never does. * * THE STATES: * text — the target's text, as the Read tool's text lane decodes it (byte-order mark removed, CRLF folded to LF) — * the coordinate the write tool's own result reports the old contents in; * absent — nothing is at the path (said only after the judgment and the resolution allowed the read: a refused path * never tells whether it exists); * withheld — `oversize` (past {@link EXISTING_TARGET_READ_CAP}, by the stat or by the bytes read; never cut short), * `not_text` (a directory, a binary file, a body that is not UTF-8 or a truncated UTF-16 body), * `read_refused` (the resolution, or a judgment layer, refuses the read or would first ask someone about it, * or the run's roster has no `Read`), * `unavailable` (no reader on this lane, a layer that could not answer, the judgment landing on another file, * the stat or the read failing, the wait ending first). * * WHEN: ONE read per ask. The live ask reads only when a person's seat will answer the card ({@link readsCardTarget}: a seat * that answers, not a policy approver's — a policy seat renders no card — and not answering the ask from its declared class * table); the durable park reads only when no lane of * the same ask read before it, before the run lets go of its environment, and keeps the answer on the parked row (a resumed * run never reads again). A park after the live card answered `unavailable` carries the live card's member as read * ({@link cardTargetFor}; "the same arguments" is the approval binding's reading — {@link exactBoundInputHashOf} — so a * park whose arguments went through the stored JSON form is still the same ask's arguments). A card is the target as of * the ask: the file may change before the approved write runs. */ import { type ToolRoster, type ToolRosterEntry } from "./tool-roster.js"; import type { SyntheticServedReadJudge } from "./synthetic-read-verdict.js"; import { type OnAsk } from "./tool-policy.js"; import type { AskClass } from "./ask-origin.js"; import type { ModelFileReader } from "../tools/fs/fs-read.js"; /** The card member's three shapes. */ export declare const EXISTING_TARGET_KINDS: readonly ["text", "absent", "withheld"]; export type ExistingTargetKind = (typeof EXISTING_TARGET_KINDS)[number]; /** Why a card in scope carries no text. */ export declare const EXISTING_TARGET_WITHHELD_REASONS: readonly ["oversize", "not_text", "read_refused", "unavailable"]; export type ExistingTargetWithheldReason = (typeof EXISTING_TARGET_WITHHELD_REASONS)[number]; export type ExistingTarget = { readonly kind: "text"; readonly text: string; } | { readonly kind: "absent"; } | { readonly kind: "withheld"; readonly why: ExistingTargetWithheldReason; }; /** The most bytes a card's read takes from its target; a larger target is `withheld` / `oversize`. */ export declare const EXISTING_TARGET_READ_CAP = 262144; /** The served-read judge's probe face (probe id `approval-card-visibility-probe`). */ export declare const EXISTING_TARGET_PROBE_FACE = "approval-card"; /** Whether a roster row's card carries the member: it writes a file on the execution environment. Read off the row's own * declarations, never a list of names. */ export declare function existingTargetCovers(row: Pick<ToolRosterEntry, "family" | "pathTarget" | "boundaryLegs"> | undefined): boolean; /** What a lane reads a card's target with: the run's served-read judge (it stamps the leg's facts on its probe) and the * lane's file reader. */ export interface ExistingTargetSource { readonly judge: SyntheticServedReadJudge; readonly reader: Pick<ModelFileReader, "readText">; /** The same served-read judgment over the rule layers only — it does not raise the leg's own `classic.PreToolUse`. Read by the * write tools' read-first waiver alone ({@link readFirstWaiverOf}): the waiver is asked at a write's input check, before the * live gate has run the hooks on that write, so a hook's verdict on the path as first written would refuse a write the same * hook redirects to another target (`updatedInput`). `judge` (the card's target, a report's files) hears the hook. */ readonly rulesOnlyJudge: SyntheticServedReadJudge; } /** What one ask's lane read: the arguments it read the target of (and their approval binding), and the member it got. */ interface TargetRead { readonly args: unknown; readonly binding: string | undefined; readonly member: { readonly existingTarget?: ExistingTarget; }; } /** The run's source, filled once the run's judge exists (empty on a lane with no reader), and what each ask's lanes read. */ export interface ExistingTargetSeat { current: ExistingTargetSource | undefined; /** The member an ask's lane read, by the ask's request object: a later lane of the same ask over the same arguments * carries it instead of reading again. */ readonly read: WeakMap<object, TargetRead>; } /** A run's seat, its source not yet filled. */ export declare function createExistingTargetSeat(): ExistingTargetSeat; /** Whether a live ask's seat reads its card's target: a seat that answers the card, is not a policy approver's * ({@link import("./tool-policy.js").asPolicyApprover} — a policy seat renders no card), and does not answer this ask from * its declared class table ({@link import("./tool-policy.js").declareClassAnswers} — the resolver answers a covered ask * without calling the seat, so no card is rendered). `classes` is the class set the request to the seat carries. */ export declare function readsCardTarget(onAsk: OnAsk | undefined, classes: readonly AskClass[] | undefined): boolean; /** * The member one lane puts on one ask's card: what an earlier lane of the same ask (`ask`, its request object) read over the * same arguments (the same object, or the same approval binding); else, when `reads`, one read (recorded for the ask's later * lanes); else the member of a card nobody read. */ export declare function cardTargetFor(seat: ExistingTargetSeat, roster: ToolRoster | undefined, ask: { readonly toolName: string; }, args: unknown, reads: boolean, signal?: AbortSignal): Promise<{ existingTarget?: ExistingTarget; }>; /** * The member the card of `toolName`'s call carries, as a spread: `{}` for a card out of scope (its row on `roster`, by * {@link existingTargetCovers}); `withheld` / `unavailable` with no source (a lane that has no reader, or a caller that does * not read); `withheld` / `read_refused` when the roster has no `Read`. One wait bounds the judgment and the read together * — the judgment's own budget; the call's signal ends it sooner. Never throws. */ export declare function existingTargetOf(roster: ToolRoster | undefined, toolName: string, args: unknown, source: Pick<ExistingTargetSource, "judge" | "reader"> | undefined, signal?: AbortSignal): Promise<{ existingTarget?: ExistingTarget; }>; /** The served-read judge's probe face for the files a report shows (probe id `report-attachment-visibility-probe`). */ export declare const SHOWN_FILE_PROBE_FACE = "report-attachment"; /** A file a report shows a person: the key it was judged and held at, and the held object's size. */ export interface ShownFile { readonly key: string; readonly size: number; } /** * The files a report shows a person — the third reader of {@link servedReadVerdictOf}: each exactly when the run's rules let the * model's own `Read` of it through with nobody asked (the permission; nothing is rendered, so Read's content refusals — an archive, * a text over its whole-file limit — do not apply), resolved as the Read tool resolves it and held to the key the judgment * named, then held open by the environment (`openBound`, the one producer of "it can be read": an environment that cannot hold * it refuses, as it refuses a person's own attachment). Kind and size are the held object's; nothing is read. All or none: the * first file not shown is the answer. One wait bounds the list. Never throws. */ export declare function filesShownOf(seats: { readonly judge: SyntheticServedReadJudge | undefined; readonly reader: Pick<ModelFileReader, "resolveTarget" | "openBound"> | undefined; readonly roster: ToolRoster | undefined; }, paths: readonly string[], signal?: AbortSignal): Promise<{ readonly ok: true; readonly files: readonly ShownFile[]; } | { readonly ok: false; readonly path: string; readonly why: string; }>; /** * The files a leg knows the model has seen though no read entry may stand for them — one registry, read by the read-first * waiver alone ({@link readFirstWaiverOf}): the files the engine put in the leg's context at startup (`seedContextFiles`, * seeded or not), the files the session's transcript shows read or changed with no record a rebuild can stand on, and the * files a rewind's restore dropped the entry of. `has` answers for every key once the leg cannot place a read it rebuilds * (a relative spelling, an unreadable transcript): no file on such a leg can be said never to have been read. */ export interface SeenFiles { has(key: string): boolean; } /** A leg's {@link SeenFiles}: filled by the places that know of such a file, carried to the session's next leg on the same * Runner with the read state's seat (`inherit`), as the live read state is — an earlier leg's knowledge does not lapse * with the leg. */ export declare class SeenFileRegistry implements SeenFiles { private readonly keys; private every; has(key: string): boolean; add(key: string): void; markAll(): void; inherit(from: SeenFileRegistry): void; } /** The read-first waiver's probe face (probe id `read-first-visibility-probe`). */ export declare const READ_FIRST_PROBE_FACE = "read-first"; /** * The write tools' read-first waiver over the run's served-read judgment (`ReadFirstWaiverSeat` in `tools/fs/fs-write.ts`): * a file the model has not read may be changed exactly when the model's own `Read` of it, issued now, would be served with * nobody asked — the roster gives `Read` a face, the served-read judge finds no objection with every ask held, and the Read * tool's own resolution (the read face's roots and deny table) takes the spelling to the write's own target, the file the * judgment named. Anything else — no `Read`, an objection, a layer that could not answer, a judgment that named another * file, a wait that ended, a resolution the Read tool refuses (a path outside the read roots included) or one that lands * elsewhere — is no. Asked first, at each call: whether the model the run requests with now is held to read-first * (`heldToReadFirst`) — then no, whatever the judgment; and whether the leg knows the model has seen the file though no * read entry stands for it ({@link SeenFiles}, keyed as the write's `key`) — such a file is not one the model never read, * so the waiver does not reach it and a change to it waits for a `Read`, as a read file changed on disk or a cut page does. */ export declare function readFirstWaiverOf(input: { readonly judge: SyntheticServedReadJudge; readonly reader: Pick<ModelFileReader, "resolveTarget">; readonly roster: () => ToolRoster | undefined; readonly heldToReadFirst: () => boolean; readonly seenFiles: SeenFiles; }): (target: { readonly path: string; readonly key: string; }, signal: AbortSignal | undefined) => Promise<boolean>; export {};