UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

62 lines (61 loc) • 5.23 kB
/** * The single-shot TERMINAL settle of a pending checkpoint row by an actor of THIS Runner, and the duty an expired row carries — one * statement for the actors that settle a row so: the resume preflight's terminal arm (the deployment ruled the binding dead), * `Runner.voidVerifierPause` (the host voids a verifier's own card) and the stream-side reap of a suspended run. * * `expire` races `resolve` on the same pending row and exactly one settler wins; `expired` is terminal (an expired token is never * redeemed again — reopen serves resolved rows only). So the duty is keyed on the ROW, not on who won: once the row is expired, the * token's parked parent-constraint entry is evicted and, when THIS process holds the paused env, its reap record is TAKEN (one * synchronous take — the record is the ownership token, so two observers never destroy twice) and the env destroyed, BOUNDED (a * wedged adapter must not hold the settle's answer hostage: a timeout or throw goes to `onDestroyError`, never masks the settle). * The winner runs it; a settler that LOSES reads what the row is now and runs the same duty when it finds it `expired` — another * Runner, the deployment reaper or an earlier settler expired it, and none of them holds this process's env. A row the loser finds * anything else (a resume resolved it and owns the env) keeps its env and its record. A store throw on the settle closes this * attempt's claim and propagates; a throw reading the row after a loss is said and the record kept for a later settle to act on. */ import type { CheckpointStore, CheckpointToken } from "../checkpoint-store.js"; import type { LocalClaims } from "./local-claims.js"; import type { InheritedGate, SuspendReap } from "./contracts.js"; export interface ExpireRowInput { /** borrowed-readonly — the row's token and its OWN scope (read off the row, never the caller's). */ token: CheckpointToken; scope: string; /** borrowed-readonly — the store the row lives in. */ store: CheckpointStore; /** borrowed-readonly — the grant holder of the run the settle is issued in the name of (absent ⇒ a host's own settle carries none): * one more precondition of the expire CAS, judged by the store; a holder it does not hold current loses like any settler. */ holder?: string | undefined; /** borrowed-mutable — the Runner's same-Runner claims: this attempt opens its own with the CAS and closes it once the duty is decided. */ locallyClaimedTokens: LocalClaims; /** borrowed-mutable — the Runner's parked-constraint registry: the token's entry is evicted once the row is expired. */ parentConstraintRegistry: Map<CheckpointToken, NonNullable<InheritedGate["parentConstraints"]>>; /** borrowed-mutable — the Runner's suspended-env reap handles: whoever runs the expired duty takes and retires this token's. */ suspendedEnvReaps: Map<CheckpointToken, SuspendReap>; /** borrowed-readonly — the bounded destroy's label, and where its timeout or throw (or a failed read after a loss) is said, with the parked session. */ destroyLabel: string; onDestroyError: (err: unknown, sessionId: string) => void; } /** Settle the row `expired`; `true` = this call won the CAS, `false` = another settler already had (the duty still runs if the row is expired). */ export declare function expireRowWithDuty(input: ExpireRowInput): Promise<boolean>; /** The expire CAS a run issues in its own name: with the holder as the store's write option, or — no holder — the two-argument call as * ever (a store written against the older interface sees exactly what it saw). */ export declare function expireCarrying(store: CheckpointStore, token: CheckpointToken, scope: string, holder: string | undefined): Promise<boolean>; /** * THE reading of a lost single-shot CAS that carried the run's holder (the resume's claim, the resume preflight's terminal expire): the * row is still the one the caller validated — pending, the same row (scope), the same generation — so nothing moved it and what failed * is the holder: the caller answers `holder.superseded` ({@link import("../grant-holder.js").holderSupersededError}). Anything else is * the caller's own loss reading (a moved generation, a consumed row). */ export declare function lostToHolder(holder: string | undefined, loss: { status: "pending" | "resolved" | "expired" | "absent"; rev: number | undefined; sameRow: boolean; }, validatedRev: number): boolean; /** * A settle attempt of this Runner LOST its CAS: read the row, and run the expired duty when it is `expired`. Answers what was seen — * the row's status, `"absent"` (no row), or `undefined` (nothing held here to act on, or the read threw: said, record kept) — and * never throws. */ export declare function settleLostRow(input: Omit<ExpireRowInput, "scope" | "locallyClaimedTokens">): Promise<"pending" | "resolved" | "expired" | "absent" | undefined>; /** The expired row's duty in THIS Runner: evict the parked constraint, take the reap record, destroy its env (bounded). */ export declare function runExpiredDuty(input: Omit<ExpireRowInput, "scope" | "store" | "locallyClaimedTokens">): Promise<void>;