UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

192 lines (191 loc) • 15.5 kB
/** * The ONE restore of a session's tracked set and the ONE reading of its answer, reached DOWN for by the two seats that * restore: the file-history phase's restore seat (`prepare-file-history.ts`, a `restoreFiles` / `rewindFilesTo` run) * and the out-of-run rewind (`rewind-files.ts`, `Runner.rewindFiles`). A rewind outside a run is therefore the same * history operation as the one a run performs, refused with the same codes and sentences, and handing the paths it * moved to the same read-state channel. The person's half of "this turn has no boundary" when the capture failed * ({@link boundaryUnrecordedNotice}) is the same fact said at the run instead of at the rewind, so it lives here too. */ import type { ExecutionEnv } from "../../internal/harness.js"; import type { EngineNotice } from "../types.js"; import type { FileHistoryError, FileHistoryLeftAsIsFile, FileHistoryRestoreOptions, FileHistoryRestorePlan, FileHistoryRestoreResult, FileHistoryStore, RewindFilesResult } from "../file-history-store.js"; import type { SessionReadFileStates } from "./session-read-file-states.js"; import type { RewindOutcome } from "./rewind-outcome.js"; import type { TaskRewindSettlement } from "../terminal-cause.js"; /** A restore outcome a seat may accept: the ledger, with `failed` (the operation could not run) already refused — * the out-of-run rewind's answer carries exactly this. */ export type AcceptedFileHistoryRestore = Extract<RewindFilesResult, { dryRun: false; }>["restore"]; /** * The ONE restore of a session's tracked set and the ONE reading of its answer — the run's restore seat and the * out-of-run `Runner.rewindFiles` both call it, so the two cannot disagree about what a restore means: * · every path the attempt may have moved (`filesChanged` ∪ `failed[].path`, whatever the outcome) goes to the * session's read-state channel (`noteRestored`), because a read record of the old bytes must stop vouching * for them on the session's next leg; * · `not_found` is "no boundary for that entry" — `rewind_snapshot.unresolvable` (in a "before" restore, the * before-specific sentence); * · any other `failed` outcome is `rewind.restore_failed` ({@link restoreFailedError}: the ledger in its sentence * and its members, and the store's own judgment, on the coded `detail` channel — a host reads those, never the prose). * A `complete` or `partial` answer is returned for the caller to accept: only a run goes on to work on the tree, * so only the run seat has a partial-restore policy. */ export declare function settleFileHistoryRestore(input: { store: FileHistoryStore; /** The session the restore is for: its read-state channel and every sentence name it. */ sessionId: string; /** The session's own history scope (`fileHistoryScopeOf` of its row): the one the store restores from. */ scope: string; target: string; before: boolean; env: ExecutionEnv; root: string; signal: AbortSignal | undefined; readStates: Pick<SessionReadFileStates, "noteRestored"> | undefined; /** The rewind's restore options (`unplacedHistoryRecordedUnder`), handed to the store as they are — the word `"rewind_root"` read as `root` ({@link restoreOptionsAt}). */ options: FileHistoryRestoreOptions | undefined; /** Refuse before any file moves when the restore would refuse or fail one ({@link planFileHistoryRestore}) — a caller that does * not accept a partial restore; a caller that reads the ledger itself leaves it off. */ wholeOrNothing: boolean; /** Handed to the store's restore as it is: the store's restore input (`forkedSession` there) says what the fact is and what it changes. */ forkedSession?: boolean; /** The rewind's record of whether the tree moved ({@link RewindOutcome.moved}): written here once the restore asked the env what * it asked — whatever the restore then answers, or if it throws; untouched when the restore is refused before it runs. */ outcome?: Pick<RewindOutcome, "moved">; }): Promise<AcceptedFileHistoryRestore>; /** * What `rewind.restore_failed` carries on its coded `detail` (the error's own member, which a failed task's * `terminal.detail` reads as its `coded` account): `code`, the store's code when the store failed the operation; `permanence`, * whether repeating the same rewind can succeed (a `GovernanceRetryClass` word) — the store's judgment of its fault when it * stated one; `terminal` when a run stopped the restore itself ({@link RUN_STOPPED_RESTORE}: its plan would refuse or fail a file, * nothing moved; or a write failed after the plan passed, the tree partly restored) — do not send it again on its own, a person * decides; `transient` when the session's row could not be read (the history has no address yet; nothing planned); absent when * nobody judged it; and the restore's ledger in the success arm's * own row types — `refused` and `failed` as the store answered them, with the counts a host reads when a list does not fit the * terminal's snapshot, which leaves an over-bound list out whole (`filesChangedCount` is the sentence's "applied N"; * `refusedCount` / `failedCount` are the lists' lengths); the files it left as they are (`leftAsIs`, each with its class, and * `leftAsIsCount`) when there were any — never a cause of the failure. The counts are declared before the lists on purpose: see * {@link restoreFailedError}. On `Runner.rewindFiles` only, `rewind` — whether the tree moved (`moved`) and `branch: "untouched"` * ({@link TaskRewindSettlement}); a run states the same fact once, on its failed terminal (`terminal.rewind`), never in this detail. */ export type FileHistoryRestoreFailureDetail = Partial<Pick<FileHistoryError, "code" | "permanence">> & { filesChangedCount: number; refusedCount: number; failedCount: number; leftAsIsCount?: number; } & Pick<FileHistoryRestoreResult, "refused" | "failed" | "files" | "leftAsIs"> & { rewind?: TaskRewindSettlement; }; /** The engine's judgment of a restore a run stopped on — nothing moved because its plan would refuse or fail a file (repeating the * same rewind plans the same), or a write failed after the plan passed and the tree is partly restored (a repeat would write over * any file a person changed since): `terminal` — do not send it again on its own; a person decides. */ export declare const RUN_STOPPED_RESTORE: Pick<FileHistoryError, "permanence">; /** * THE `rewind.restore_failed` error — the one mint for every way a restore fails: the store's operation-level failure * ({@link settleFileHistoryRestore}), a plan that failed as a whole ({@link refuseFailedPlan}: a preview's and a run's alike), the run * seat's default refusal of a partial plan, and a partial restore after its plan passed. The sentence is the caller's (it states * what only that arm knows); the member beside it is read here, once, off the restore's own answer, so the numbers in the sentence * ({@link fileHistoryRestoreLedgerLine}) and the members a host reads cannot be two counts. `judged` is the engine's own * `permanence` for an arm the store did not fail ({@link FileHistoryRestoreFailureDetail}); a store that stated one is read first. */ export declare function restoreFailedError(restored: FileHistoryRestoreResult, message: string, judged?: Pick<FileHistoryError, "permanence">): Error & { code: "rewind.restore_failed"; detail: FileHistoryRestoreFailureDetail; }; /** How a boundary was asked for — the three spellings of the one refusal "the requested anchor has no boundary on this session". */ type BoundaryAsk = "restore" | "before" | "preview"; /** * A plan ({@link planFileHistoryRestore}) that failed as a whole is the restore's own refusal, read once for every seat that plans * — the preview (`Runner.rewindFiles` with `dryRun`) and a run that accepts no partial restore: no boundary for the entry is * `rewind_snapshot.unresolvable` (in the asker's sentence); any other failure is `rewind.restore_failed` carrying the store's * `code` and `permanence` — what the restore itself would answer. Nothing moved either way. A plan that did not fail as a whole * returns. */ export declare function refuseFailedPlan(planned: FileHistoryRestoreResult, target: string, sessionId: string, ask: BoundaryAsk): void; /** * A plan that lists frozen files and asked a write whose real path the shared restore half did not read ({@link * FileHistoryRestorePlan.unheld}): the store converged a file without that half, or through an env it wrapped, so which file the write * lands on cannot be told and it cannot be kept off a frozen file — refused before any file moved, in every way of asking * (`rewind.restore_failed`, `permanence: "terminal"`: the same deployment plans the same; only a store that hands each file to that * half on the env it is handed restores such a history). The sentence names the frozen files ({@link listFrozenFileHistoryEntries}'s * same set) and the way on. A plan with no such write returns. */ export declare function refuseUnheldPlan(plan: Pick<FileHistoryRestorePlan, "restore" | "unheld" | "unplacedForFork">, target: string, sessionId: string, ask: BoundaryAsk): void; /** What {@link refuseTargetOutsideOwnLog} reads of the session: an entry by id. */ export interface OwnLogReader { getEntry(id: string): Promise<unknown>; } /** * A rewind goes only to a boundary the session itself recorded, and a boundary is keyed by an entry of the session's own log. What the * history store holds under a scope can outlive the row it was recorded for (a row whose occupant predates the address epoch shares its * id's scope with every such occupant of that id, and an id erased and created again with the store not told leaves it there): the * target must be an entry THIS session's log holds, read the way a branch target (`resumeAt`) is, before * any history verb is asked about it. An entry the log does not hold gets the one refusal an entry with no boundary gets, whatever the cause — * a different sentence would tell the caller that someone else's boundary is there. * No history verb is asked about the target and no file is moved before this answers (a run's own fork carry, which every run of a fork * makes whatever it rewinds to, can come first). */ export declare function refuseTargetOutsideOwnLog(session: OwnLogReader, sessionId: string, target: string, ask: BoundaryAsk): Promise<void>; /** Whether `target` is an entry of the session's own log, read the way a branch target (`resumeAt`) is — the judgment * {@link refuseTargetOutsideOwnLog} refuses on, and the one an out-of-run read answers `no_checkpoint` on. */ export declare function entryInOwnLog(session: OwnLogReader, target: string): Promise<boolean>; /** * A fork that works in a tree of its own (`forkHasOwnTree`, `../session-checkout.ts`) restores no file: its history scope can hold * rows keyed against the root it worked at before its tree, and nothing on a row says which root that was. The one refusal an entry * with no boundary gets, in both rewinds and every way of asking; the run records nothing for such a session either, and no fork carry * crosses a session tree (`recordedRootOf`). */ export declare function refuseRestoreOnForkWithOwnTree(forkWithOwnTree: boolean, sessionId: string, target: string, ask: BoundaryAsk): void; /** The per-file ledger as one sentence: each count, with the files written or deleted named (by what was done to each, where * the answer carries `files`), and each refused / failed path with its reason. A PLANNED ledger (nothing moved) says what the * restore would have done instead ("would apply", "would write" / "would delete"). */ export declare function fileHistoryRestoreLedgerLine(r: FileHistoryRestoreResult, planned?: boolean): string; /** * The run's note that its rewind left files as they are ({@link FileHistoryRestoreResult.leftAsIs}) — `restore_left_as_is` on * `TaskResult.rewindNotes`, a sentence that stands on its own: which files (by key, each with the directory it lies in — the row's * `place`), why each class of them did not move, and how a host learns of them beforehand; on a `complete` restore, how many other * files it restored and how many were already as recorded, counted from the ledger (none is said as none; a partial one is said by * its own note or failure) — and beside it the same files as rows (`leftAsIs`, the restore's own rows copied: the type a failed * restore's `detail.leftAsIs` and `Runner.rewindFiles`' `restore.leftAsIs` carry), so a host reads where each one lies off `place`, * never off the sentence. `undefined` when the restore left none. */ export declare function restoreLeftAsIsNote(restored: Pick<FileHistoryRestoreResult, "leftAsIs" | "outcome" | "filesChanged" | "identicalSkipped">, target: string): { code: "restore_left_as_is"; message: string; leftAsIs: FileHistoryLeftAsIsFile[]; } | undefined; /** * The run's note that its rewind wrote or deleted files OUTSIDE the root the run works in — `restore_outside_root` on * `TaskResult.rewindNotes`: each such file by its absolute path as addressed (`targetPath`) and what was done to it, read off the * answer's own rows (`files` with `outsideRoot`, which reads where the file really lies — a key spelled through a link inside the * root that leads out of it included: a write or delete that landed, a row the store then failed included). `undefined` when the * restore moved no file outside the root (refused and left rows move nothing). */ export declare function restoreOutsideRootNote(restored: Pick<AcceptedFileHistoryRestore, "files">, target: string): { code: "restore_outside_root"; message: string; } | undefined; /** The person's half of a rewind that left a file as it is (`rewind.file_left_as_is`, audience `user`), once per session per file * and class. The sentence names the file as the run's note does — its key and the directory its history records (`place`, a path on * the machine that recorded it); `detail: { sessionId, path, class }` — the file's key and the row's class, no directory. */ export declare function fileLeftAsIsNotice(input: { sessionId: string; } & Pick<FileHistoryLeftAsIsFile, "path" | "place" | "class">): EngineNotice; /** A restore whose entry has no boundary on the session (the preview's form names the preview); a boundary that is there and * cannot be read is the restore's failure, in a preview too ({@link refuseFailedPlan}). */ export declare function noBoundaryRefusal(target: string, sessionId: string, preview?: boolean): Error & { code?: string; }; /** * The person's half of a boundary capture that failed (`rewind.boundary_unrecorded`, audience `user`): the turn goes on, and a * rewind to its entry will meet {@link noBoundaryRefusal}. Minted by the boundary seat where it discloses the same failure on * `onError` (`{ phase: "rewind" }`), so the two are one fact. `detail: { sessionId, entryId, cause }` — `entryId` is the row a * rewind would name, `cause` the store's code from the closed set (`FILE_HISTORY_ERROR_CODES`; `aborted` for the capture's * time fence); never the store's message, never a path. Said once per failed capture, which is at most once per run. */ export declare function boundaryUnrecordedNotice(input: { sessionId: string; entryId: string; cause: FileHistoryError["code"]; }): EngineNotice; export {};