@sema-agent/core
Version:
Stateless, task-oriented AI agent core
53 lines (52 loc) • 3.83 kB
TypeScript
import type { SessionStorage } from "../session.js";
import type { WriteRefusal } from "./contracts.js";
import type { RewindOutcome } from "./rewind-outcome.js";
/** The entries this prepare appended through the watched storage — recorded once a branch is armed (`ids` set). */
export interface OwnAppends {
ids: Set<string> | undefined;
}
/** The branch a `resumeAt` acquire wrote: the watched storage, the leaf the request found, the leaf it set, the own appends since, and
* the leg's record of where the leaf stands ({@link RewindOutcome.branch}; absent on a branch no leg record follows), which the
* write-back states at each step it takes. */
export interface ResumeAtBranch {
storage: SessionStorage;
leafBefore: string | null;
leafSet: string | null;
own: OwnAppends;
outcome?: Pick<RewindOutcome, "branch">;
}
export type { WriteRefusal };
/** A fresh latch: nothing refused yet, no stop bound. */
export declare function writeRefusalLatch(): WriteRefusal;
/** Latch the word a refused write said, if it is one of the latch's two (anything else leaves it as it is). */
export declare function latchWriteRefusal(ref: WriteRefusal, err: unknown): void;
/**
* Wrap a storage so a refused append/leaf write latches its word on `ref` ({@link WriteRefusal}), and every append
* that lands is recorded on `own` while a branch is armed. The harness flattens a mid-run storage failure into
* a generic error (and, since a lost CAS makes every subsequent append fail, even its failure-reporting append
* fails) — so the store's code is gone by the time the task result is assembled. Catching it here, at the
* storage boundary, keeps it as `TaskResult.errorCode` (`conflict` / `holder.superseded`). Per-task and non-mutating (a
* Proxy over the shared storage), so it never stacks across tasks on a cached session.
*/
export declare function detectConflicts(storage: SessionStorage, ref: WriteRefusal, own: OwnAppends): SessionStorage;
/**
* Put a refused prepare's `resumeAt` branch back: the leaf returns to `leafBefore` when it still sits on the
* branch leaf or on this prepare's own appends above it, and the write is conditional on the leaf just
* verified. A no-op when the leaf is already there. Throws a sentence naming both leaves when the leaf is
* not this request's to take back or the write cannot land. `signal` aborted before the write starts ⇒ no
* write (the caller's bound expired and the view is being dropped); a storage write already in flight cannot
* be called back — the storage contract carries no cancellation.
*
* Where the leaf stands is stated on `branch.outcome` at each step, so a bound that expires mid-way leaves the last thing known: the
* leaf found where the request found it ⇒ `put_back` (`untouched` when the request had branched at that very leaf); another writer's
* entries past the request's ⇒ `left_moved`, as is a bound that expired before the write started; a conditional write the store
* refused as a conflict ⇒ the leaf read again (`put_back` where it is where the request found it, else `left_moved`); a read that
* threw, or a write started and not answered, ⇒ not known (`undefined`); the write landed ⇒ `put_back`.
*/
export declare function writeBranchBack(branch: ResumeAtBranch, sessionId: string, signal?: AbortSignal): Promise<void>;
/**
* The session cleanup's write-back leg: {@link writeBranchBack} bounded like every other cleanup leg (a throw or
* a timeout goes to `report`, never to the caller). A write-back still pending when the bound expires does not
* START its write afterwards — the view it would write through is being dropped.
*/
export declare function settleBranchWriteBack(branch: ResumeAtBranch, sessionId: string, label: string, report: (err: Error) => void): Promise<void>;