UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

261 lines (260 loc) • 16.6 kB
/** * `Runner.readSessionChanges` — what changed in a session's files, read outside any run: its request, its answer and its bounds. * * THE RULE. Each question has one source and one reading, and this verb mints no second reading of any of them: * · a checkpoint against the working tree now (`since`, and the newest item of the list) — the rewind preview's own computation: * the store's `restore` planned on a view of the env that writes nothing (`Runner.rewindFiles({ dryRun: true })` reads the same * plan), read in the other direction (the checkpoint before, the working tree after); * · two adjacent checkpoints (`checkpoints`, `checkpoint`) — the store's read seat (`FileHistoryStore.readBoundaries`, one batch per * page), whose targets are the ones the store's `restore` converges to (`fileHistoryTargetOf`), and the bytes through its * exchange (`blobExchange.get`, each content address read once per call); * · "where did a run begin" — the person's input row that says so (`legFirstInput`), the row the run's checkpoint is keyed on; * · the session's uncommitted changes against HEAD (`git`) — git, run in the env the session's runs use, at the session's root (its * own tree when it works in one). * (What one turn of the agent changed is another question: the session log's edit cards answer it, not this verb.) * * The engine hands whole texts or a reason it hands none (`content.omitted`), never a cut one; a file it could not compare says so * (`status: "uncompared"`, `lines.uncounted`), never "unchanged"; a count it cannot make is absent, never 0. It reads with no lock, * no gate and no frame — also while a leg of the session runs — and writes nothing (no fork carry either). A refusal is a thrown * `Error` carrying a `code`; an answer that cannot be read is `kind: "unavailable"` with its word. */ import type { SessionRowExpectation } from "./session.js"; /** Each side's text is handed whole up to this many bytes; a larger side is `content.omitted: "too_large"` (its `lines` still * counted where the source counts them) — never cut. */ export declare const CHANGES_CONTENT_MAX_BYTES = 1000000; /** The texts one call hands, both sides of every row together, stay within this many bytes: rows are given their texts in the * answer's order (`since`: the order its plan reads the files) while they fit, and a row whose texts would carry the call past it is * `content.omitted: "budget_spent"` — the second bound beside {@link CHANGES_CONTENT_MAX_BYTES} (a side), so `contentOf` × two sides × * a side's bound never reaches memory whole. */ export declare const CHANGES_CONTENT_BUDGET_BYTES = 64000000; /** At most this many paths in `contentOf` (more ⇒ `changes.invalid_spec`). */ export declare const CHANGES_CONTENT_OF_MAX = 500; /** A page of the checkpoint list holds at most this many items (`source.limit`, 1 to this, default this). */ export declare const CHANGES_CHECKPOINTS_PAGE_MAX = 100; /** At most this many rows in a git `rows` answer: more changed files ⇒ `totals` (the counts, and the rows `contentOf` names). */ export declare const CHANGES_ROWS_MAX = 500; /** * What the verb reads: * · `checkpoints` — the session's checkpoints on its current branch (since its latest compaction), newest first, each paired with * the next one after it — the newest with the working tree: one page of at most `limit` items; `before` names an item's * `before.entryId` of an earlier page and asks for the items older than it; * · `checkpoint` — one item of that list, the one whose `before.entryId` is `entryId`, with the texts `contentOf` names; * · `since` — the checkpoint of `entryId` (an entry of this session's own log) against the working tree now; * · `git` — the session's uncommitted changes against `base`: `"head"` (the commit HEAD names, resolved once per call; the empty tree * on an unborn HEAD), the one base today. */ export type ChangesSource = { readonly kind: "checkpoints"; readonly limit?: number; readonly before?: string; } | { readonly kind: "checkpoint"; readonly entryId: string; } | { readonly kind: "since"; readonly entryId: string; } | { readonly kind: "git"; readonly base?: "head"; }; /** * Why a row's `lines` are not counted (`lines.uncounted`) or its texts are not handed (`content.omitted`) — a closed set, every word * of it produced by a source: * · `binary` — the bytes are not text this engine hands as it is: a NUL byte, or not UTF-8 (lines are not counted for a NUL; on the * git source, where git reads the file as binary); * · `too_large` — a side is over {@link CHANGES_CONTENT_MAX_BYTES}; * · `budget_spent` — both sides could be handed, but the call's texts so far leave too little of * {@link CHANGES_CONTENT_BUDGET_BYTES} for them (the file may be small: ask for it alone); * · `unreadable` — a side could not be read: the store does not hold the bytes it names, or holds other bytes under their address; * a record of the file's history the chain does not hold; the working tree's file could not be read (a link, a directory or * nothing at the path when it was read); git could not hand the commit's side; * · `untracked` — a file git does not track (the git source): listed by name, its lines not counted; * · `submodule` — a submodule's commit changed (the git source): no file text on either side; * · `repository_filter` — a filter driver the repository's own configuration defines governs the file (the git source): the read * runs no such program, so git compared the bytes on disk, not what the driver would make of them — not compared, no texts; * · `base_unavailable` — the checkpoint holds no version of a file tracked after it and never captured before (a rewind refuses it * the same way); * · `frozen_history` / `recorded_outside_root` — a rewind would leave the file as it is (`leftAsIs`, with that class); * · `rewind_refused` — a rewind would refuse the file (`refusal`: the refusal's class and reason, as the preview names them); * · `mode_only` — only the file's mode changed: there is no text to hand. */ export declare const CHANGES_REASONS: readonly ["binary", "too_large", "unreadable", "base_unavailable", "frozen_history", "recorded_outside_root", "rewind_refused", "mode_only", "untracked", "submodule", "repository_filter", "budget_spent"]; /** One word of {@link CHANGES_REASONS}. */ export type ChangesReason = (typeof CHANGES_REASONS)[number]; /** One changed file. */ export interface ChangeRow { /** History sources: the file's history key as the store holds it — relative to the session's root for a file under it, the absolute * real path of a file recorded outside it. The git source: relative to the repository's top. Neither is the spelling a tool call * gave (`TaskResult.editedFiles`): a host matches files by resolving both, never by comparing the strings. */ readonly path: string; /** `uncompared` (history sources): the source could not compare this file — `lines.uncounted` says why. Never read as "unchanged". * `renamed` / `untracked`: the git source. */ readonly status: "added" | "modified" | "deleted" | "renamed" | "untracked" | "uncompared"; /** Where a `renamed` file was, relative to the repository's top. Present iff `status` is `renamed`. */ readonly renamedFrom?: string; /** Only the file's mode changed (`lines` 0 / 0, no texts). */ readonly modeOnly?: true; /** The lines `after` holds that `before` does not (`insertions`) and the reverse (`deletions`), or why they are not counted. */ readonly lines: { readonly insertions: number; readonly deletions: number; } | { readonly uncounted: ChangesReason; }; /** Present iff `lines.uncounted` is `rewind_refused`: the refusal's class and reason, as the rewind preview names them. */ readonly refusal?: { readonly class: string; readonly reason: string; }; /** Present only on a row whose path the request's `contentOf` named: both texts whole (`null` = the file is not there on that * side), or why the engine hands neither. */ readonly content?: { readonly before: string | null; readonly after: string | null; } | { readonly omitted: ChangesReason; }; } /** What the rows are read against: a checkpoint, by the entry it is keyed on (the person's input that opened its run); or, on the git * source (read its two arms by `base`), `base: "head"` with the commit HEAD named when the call read it, or `base: "empty_tree"` * when HEAD named none (an unborn branch). */ export type ChangesBefore = { readonly kind: "checkpoint"; readonly entryId: string; } | { readonly kind: "git"; readonly base: "head"; readonly commit: string; } | { readonly kind: "git"; readonly base: "empty_tree"; }; /** What the rows are read up to: the next checkpoint, or the working tree as the call read it. `skipped` — on an item of the list * (and `checkpoint`) only: how many runs between the two recorded no checkpoint; absent when that is not known (a row in between * does not say whether it opened its run, or the store cannot tell a damaged checkpoint from a missing one) — never 0 for unknown. */ export type ChangesAfter = { readonly kind: "checkpoint"; readonly entryId: string; readonly skipped?: number; } | { readonly kind: "working_tree"; readonly skipped?: number; }; /** * Why the verb could not read the source (or one item of the list) — a closed set, every word produced by a source: * · `store_unconfigured` — the deployment wired no file-history store (or declared it `"disabled"`); * · `store_cannot_read` — the store has no read seat (`readBoundaries`) or no blob exchange (`blobExchange`): a pair of checkpoints * cannot be read (a checkpoint against the working tree still is); * · `history_unreadable` — the store failed while it read this (`fault`: its code and permanence); a damaged checkpoint is its own * item, never folded into a neighbour; * · `files_env_unsupported` — no env on the session's workspace: none is mounted; or the deployment has an `executionEnvFactory` and * does not state that it answers purpose `"read_changes"` (`RunnerDeps.executionEnvFactoryHonorsReadChanges` is not `true` — a * static env does not stand in); or the factory refused the purpose (`env.purpose_unsupported`). Any other factory refusal reaches * the caller as thrown; * · `no_checkpoint` — the entry is not in this session's own log, has no checkpoint, is not an item of the list (`checkpoint`, or a * stale `before`), the session is a fork that works in a tree of its own, or a fork's entry from before its history was carried in * (its first run, or a rewind, carries it); * · `not_a_repository` — the session's root is in no git work tree (the git source); * · `repository_busy` — a merge or a rebase is in progress there (the git source); * · `git_failed` — a git start failed, ran past 5 s or wrote more than 4 MiB (the git source). */ export declare const CHANGES_UNAVAILABLE_REASONS: readonly ["store_unconfigured", "store_cannot_read", "history_unreadable", "files_env_unsupported", "no_checkpoint", "not_a_repository", "repository_busy", "git_failed"]; /** One word of {@link CHANGES_UNAVAILABLE_REASONS}. */ export type ChangesUnavailable = (typeof CHANGES_UNAVAILABLE_REASONS)[number]; /** The store's own failure, beside `history_unreadable` only: its code (`FileHistoryError["code"]`) and, when it judged one, whether * asking again can succeed. */ export interface ChangesStoreFault { readonly code: string; readonly permanence?: "transient" | "terminal"; } /** One item of the checkpoint list: a pair read, or why this pair could not be. */ export type ChangesItem = { readonly kind: "rows"; readonly before: Extract<ChangesBefore, { kind: "checkpoint"; }>; readonly after: ChangesAfter; readonly rows: readonly ChangeRow[]; } | { readonly kind: "unavailable"; readonly entryId: string; readonly reason: "files_env_unsupported" | "store_cannot_read" | "history_unreadable"; readonly fault?: ChangesStoreFault; }; /** `Runner.readSessionChanges`' answer. */ export type SessionChanges = { readonly kind: "rows"; readonly source: ChangesSource; readonly before: ChangesBefore; readonly after: ChangesAfter; readonly rows: readonly ChangeRow[]; } /** One page of the list; `nextBefore` — the last item's `before.entryId` — present only when older items remain. */ | { readonly kind: "checkpoints"; readonly source: ChangesSource; readonly items: readonly ChangesItem[]; readonly nextBefore?: string; } /** The git source with more changed files than {@link CHANGES_ROWS_MAX}: how many (`filesCount`), the counted ones' totals, how many * of the rest each reason left uncounted, and the rows of the paths `contentOf` named (`named`, with their texts). */ | { readonly kind: "totals"; readonly source: ChangesSource; readonly before: ChangesBefore; readonly after: ChangesAfter; readonly filesCount: number; readonly counted: { readonly files: number; readonly insertions: number; readonly deletions: number; }; readonly uncounted: Readonly<Partial<Record<ChangesReason, number>>>; readonly named: readonly ChangeRow[]; } | { readonly kind: "unavailable"; readonly source: ChangesSource; readonly reason: ChangesUnavailable; readonly fault?: ChangesStoreFault; }; /** One side of a row's texts as a source read it: its text (`null` = the file is not there on that side) and the bytes it was read * from (`size`), or why it is not handed. */ export type ChangesSide = { readonly text: string | null; readonly size: number; } | { readonly omitted: ChangesReason; }; /** THE reading of a side's bytes as the text the engine hands (every source reads its sides here): over * {@link CHANGES_CONTENT_MAX_BYTES} ⇒ `too_large`; a NUL byte or bytes that are not UTF-8 ⇒ `binary`; else the text, decoded with no * byte changed (a leading byte-order mark kept). `null` = no file on that side. */ export declare function changesSideOf(bytes: Uint8Array | null): ChangesSide; /** What is left of one call's {@link CHANGES_CONTENT_BUDGET_BYTES} — one per call, spent by {@link changesContentOf} row by row. */ export interface ChangesContentBudget { left: number; } /** A call's budget, whole. */ export declare function changesContentBudget(): ChangesContentBudget; /** A row's `content` from its two sides: both texts, while they fit what is left of the call's budget (spent by them, else * `budget_spent`); or — when a side has none to hand — one reason, the first of a side that could not be read, one over a side's bound, * one that is not text (the same order on every source). Only texts handed spend the budget. */ export declare function changesContentOf(before: ChangesSide, after: ChangesSide, budget: ChangesContentBudget): NonNullable<ChangeRow["content"]>; /** `Runner.readSessionChanges`' request. A bad value is refused `changes.invalid_spec` before anything is read. */ export interface SessionChangesRequest { /** The session. It must exist (`resume.session_not_found`). */ sessionId: string; source: ChangesSource; /** Paths (as rows name them) to answer with both texts, at most {@link CHANGES_CONTENT_OF_MAX}; a path that is no row's has none. * Not taken with `checkpoints` (`changes.invalid_spec`): one item's texts are `checkpoint`'s. */ contentOf?: readonly string[]; /** The directory the session works in, stated by the caller: an absolute host path (`isAbsoluteHostPath`, * `@sema-agent/primitives`), handed to the factory as `ExecutionEnvFactoryContext.requestedCwd` and held to it * (`config.cwd_unsupported` / `config.cwd_outside_root` / `config.cwd_not_honored`) wherever the read asks for an env — the * member `Runner.rewindFiles` reads. */ cwd?: string; /** The occupant the caller read the session as — the member `Runner.rewindFiles` reads, read the same way (another row under the * id ⇒ `resume.session_not_found`). */ expect?: SessionRowExpectation; /** Caller cancel: an aborted call answers nothing (it throws the signal's reason). */ signal?: AbortSignal; }