@sema-agent/core
Version:
Stateless, task-oriented AI agent core
28 lines (27 loc) • 1.42 kB
JavaScript
import { CheckpointError } from "../checkpoint-store.js";
export async function resumeClaim(input) {
const { token, cp, store, outcomeForStore, locallyClaimedTokens, parentConstraintRegistry } = input;
locallyClaimedTokens.add(token);
let won;
let claimLossStatus;
if (store.claimTerminal !== undefined) {
const claim = await store.claimTerminal(token, cp.scope, { kind: "resolve", outcome: outcomeForStore, expect: { rev: cp.rev ?? 0 } });
won = claim.claimed;
if (!claim.claimed)
claimLossStatus = claim.current.status;
}
else {
won = await store.resolve(token, cp.scope, outcomeForStore, { rev: cp.rev ?? 0 });
}
if (!won)
locallyClaimedTokens.delete(token);
if (!won) {
const stillPending = claimLossStatus !== undefined ? claimLossStatus === "pending" : (await store.get(token))?.status === "pending";
if (stillPending) {
throw new CheckpointError("checkpoint.reopened_concurrently", "checkpoint changed concurrently (its revision advanced via a resolve/reopen cycle since this resume validated) — not executed; re-resume against the current state");
}
parentConstraintRegistry.delete(token);
throw new CheckpointError("checkpoint.already_resolved", "checkpoint already resolved or expired — not re-executed (idempotent)");
}
return await input.next({});
}