UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

99 lines (98 loc) • 7.21 kB
import type { ExecutionEnv } from "../../internal/harness-types.js"; import { FileError, type BoundFileObject, type Result } from "../../internal/harness.js"; /** * What binding a read to a judged key came to — the ONE reading of an environment's `openBound` answer, shared by every * reader that judges an object and then reads that same object (the file reader behind a user attachment, the plan file's * reader, the LSP session's read gate and its target facts): * * - `bound` — the object, opened at the key; the reader reads only from it and closes it. * - `unbindable` — this environment cannot bind a read on this target: it has no `openBound`, or it answers `not_supported`. * The two are one fact, the method's absence on the target, and every reader treats it as absence (its own no-binding * disposition). * - `refused` — the environment answered, and the answer is no (`precondition_failed`, `not_found`, `invalid`, any other * code): the object is not there to read, or is not the one that was judged. No code but `not_supported` ever falls * back to the no-binding disposition. An answer outside the contract is this too, with the code `unknown`: a member that * is not a function (an adapter without types may hold `null`) or that cannot be read, a call that throws or rejects, a * value that is no result (no `ok`, an `ok` without an object, a failure without a code). No reader meets an exception * from the reading, and the object was not shown to be the judged one. */ export type BoundOpening = { readonly kind: "bound"; readonly object: BoundFileObject; } | { readonly kind: "unbindable"; } | { readonly kind: "refused"; readonly error: FileError; }; /** Open the object at `key` through `env.openBound` and read the answer as a {@link BoundOpening}; it never throws. */ export declare function openBoundAt(env: Pick<ExecutionEnv, "openBound">, key: string, signal?: AbortSignal): Promise<BoundOpening>; /** What a reader reads: the bound object, or — where the environment cannot bind — the one by-name simulation of it * ({@link simulatedObjectAt}). Either way the reader takes its kind, size and bytes from it and closes it. `byName` is * present on the simulation only: the key it reads by name and its judgment asked again (the reader's re-judgment and the * look it takes before every answer) — a program that must read the object starts on the key by name only after * `check` answers, right before the start. A held object has no `byName`: a program reads its bytes. */ export type ReadObject = Pick<BoundFileObject, "kind" | "size" | "readBytes" | "close"> & { readonly byName?: { readonly key: string; check(signal?: AbortSignal): Promise<Result<void, FileError>>; }; }; /** The reader's own judgment of its target, asked again after a by-name read: true ⇔ the name still lands on the key. */ export type Rejudgment = (signal?: AbortSignal) => Promise<boolean>; /** A reader's judgment as its {@link Rejudgment}: `judge` resolves the reader's path now; it still lands when it answers * `key` again. A throw is a no. */ export declare function sameKeyAgain(judge: (signal?: AbortSignal) => Promise<{ readonly ok: boolean; readonly key?: string; }>, key: string): Rejudgment; /** A read object's whole content within `max` bytes: `over` when it is larger than `max` — by its size before the read * (`size`), or by the read refusing it as over its cap (`invalid`) with its size after (`read`: grown past `max` while it * was read); `failed` for every other failure, its own code kept (`aborted`, `precondition_failed`, …). The read asks for * `max` bytes and the object takes at most one byte more to tell a larger file; a file that grew after the read ended is * read as it was. */ export declare function readWithin(object: ReadObject, max: number, signal?: AbortSignal): Promise<{ readonly kind: "bytes"; readonly bytes: Uint8Array; } | { readonly kind: "over"; readonly size: number; readonly on: "size" | "read"; } | { readonly kind: "failed"; readonly error: FileError; }>; /** What {@link readObjectAt} answered: the object, or the step that gave none — `open`: the environment's bound open was * refused (the file may well be there; it could not be opened, so the failure is a failed READ and says its reason as every * read failure does); `look`: the by-name simulation's look at the leaf failed (the object's kind and size could not be taken, * so the failure is a failed STAT). A reader says which one it was — a refused opening is never told as a failed stat. */ export type ReadObjectAnswer = { ok: true; value: ReadObject; } | { ok: false; error: FileError; step: "open" | "look"; }; /** * The object a reader reads whose no-binding disposition is to read by name — the model's own reads and the reads made for * it (a person's path attachment is not one: it reads only a bound object, `openBoundAt`). Passing `rejudge` is that * declaration. `bound` ⇒ the bound object; `unbindable` ⇒ {@link simulatedObjectAt}; `refused` ⇒ the refusal. A carrier that opened * before it judged hands its answer to {@link objectOfOpening}, the same reading, with no second open. */ export declare function readObjectAt(env: Pick<ExecutionEnv, "openBound" | "fileInfo" | "readBinaryFile">, key: string, rejudge: Rejudgment, signal?: AbortSignal): Promise<ReadObjectAnswer>; export declare function objectOfOpening(env: Pick<ExecutionEnv, "fileInfo" | "readBinaryFile">, key: string, rejudge: Rejudgment, opening: BoundOpening, signal?: AbortSignal): Promise<ReadObjectAnswer>; /** * The one by-name simulation of a bound object at a judged `key` (judge → read → judge again): every answer it gives comes * from the reader's own judgment asked again (`rejudge`) followed by a look at the leaf without following a link (`fileInfo`) * — a judgment that no longer lands on the key is `precondition_failed`, a link at the leaf is refused as the bound open * refuses it (`precondition_failed`), and anything the look does not call a regular file or a directory keeps the look's * answer. The kind is the first look's; the size a fresh look's. The bytes are ONE bounded read of the key * (`readBinaryFile(key, signal, max + 1)` — never more than `max + 1` bytes taken; the environment's bounded read opens * without blocking and reads only a regular file), and only then the judgment and the look again: a link, or an object of * another kind, found there drops the bytes (another regular file put in its place is not told from it: the looks compare * kinds). A swap that lands and reverts entirely between a look and the read is the window every * unbound read shares (KL-1503-b). A cancelled signal answers `aborted`. Nothing is held; `close` releases nothing. `byName` * offers the same look and judgment to a program that reads the key by name, asked right before it starts. */ export declare function simulatedObjectAt(env: Pick<ExecutionEnv, "fileInfo" | "readBinaryFile">, key: string, rejudge: Rejudgment, signal?: AbortSignal): Promise<Result<ReadObject, FileError>>;