UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

91 lines (90 loc) • 7.84 kB
import { CheckpointError, readPendingSteerQueue } from "../checkpoint-store.js"; import { resumeCancelledSentence } from "../park-noun.js"; import { GLOBAL_USAGE_KEY, resolveUsageWindows, usageRetryAfterMs } from "../usage-window-store.js"; import { compositionLegRecord, placementValueOrAbsent, sameCompositionLeg } from "./checkpoint-scope.js"; import { enterConfigDoor } from "../config-refusal-input.js"; import { raceSettlementAgainstSignal } from "./abort-race.js"; import { refuseRetiredInheritedConstraints, refuseScopesOffInheritedIsolationScope } from "../resume-config-judge.js"; import { ownToolListsDigestOf } from "./sub-agent-deny-lists.js"; import { judgedStoredPrincipalOf } from "../principal-name.js"; export function resumeInternalsAndConfig(input) { const { cp, config, internals, outcomeGate, runner, resumeSignal, chainRefused } = input; if (!chainRefused) refuseRetiredInheritedConstraints(internals); let recheckGovernanceWindow; { const rowPrincipal = judgedStoredPrincipalOf(cp.principal); const suppliedPrincipal = config.principal || undefined; if (rowPrincipal !== undefined && suppliedPrincipal !== undefined && suppliedPrincipal !== rowPrincipal) { throw new CheckpointError("resume.principal_mismatch", `the resume config carries principal ${JSON.stringify(suppliedPrincipal)} but this checkpoint was suspended under principal ${JSON.stringify(rowPrincipal)} — ` + "running the resumed leg under a different identity would move its usage-ledger bucket, scope derivation and attribution; " + "refused pre-CAS (the checkpoint stays pending): re-resume with the original principal, or omit the field to inherit the recorded one"); } const rowIsolationScope = judgedStoredPrincipalOf(cp.isolationScope); const suppliedIsolationScope = config.isolationScope || undefined; if (rowIsolationScope !== undefined && suppliedIsolationScope !== undefined && suppliedIsolationScope !== rowIsolationScope) { throw new CheckpointError("resume.isolation_scope_mismatch", `the resume config carries isolationScope ${JSON.stringify(suppliedIsolationScope)} but this checkpoint was suspended under isolation scope ${JSON.stringify(rowIsolationScope)} — ` + "running the resumed leg in a different scope would move its registry domain, offloads and parked rows; " + "refused pre-CAS (the checkpoint stays pending): re-resume with the original isolation scope, or omit the field to inherit the recorded one"); } refuseScopesOffInheritedIsolationScope(rowIsolationScope, suppliedIsolationScope, config); { const recordedPlacementRoot = placementValueOrAbsent(cp.state.placementRootSessionId); const suppliedPlacementRoot = placementValueOrAbsent(internals?.placementRoot); if (recordedPlacementRoot !== undefined && suppliedPlacementRoot !== undefined && suppliedPlacementRoot !== recordedPlacementRoot) { throw new CheckpointError("resume.placement_mismatch", `the resume supplied explicit placement root ${JSON.stringify(suppliedPlacementRoot)} but this checkpoint recorded placement root ${JSON.stringify(recordedPlacementRoot)} at suspend — ` + "resuming under a different placement fixed point would re-place the leg (and its descendants) on another target; " + "refused pre-CAS (the checkpoint stays pending): re-resume with the recorded root, or omit internals.placementRoot to inherit it"); } } { const recordedLeg = enterConfigDoor("inheritedGate", () => compositionLegRecord(cp.state.compositionLeg)); const suppliedLeg = enterConfigDoor("deps", () => compositionLegRecord(internals?.compositionLeg)); if (recordedLeg !== undefined && suppliedLeg !== undefined && !sameCompositionLeg(recordedLeg, suppliedLeg)) { throw new CheckpointError("resume.composition_mismatch", `the resume supplied composition leg ${JSON.stringify(suppliedLeg)} but this checkpoint was parked as ${JSON.stringify(recordedLeg)} — ` + "continuing would run the leg as another composition's leg or file its next card under another host session; " + "refused pre-CAS (the checkpoint stays pending): re-resume with the recorded value, or omit internals.compositionLeg to inherit it", { reason: "live_leg_contradicts_row" }); } } if (internals?.ownToolLists !== undefined) { const liveLists = ownToolListsDigestOf(internals.ownToolLists); if (liveLists !== undefined) { const recordedLists = ownToolListsDigestOf(cp.state.ownToolLists); if (recordedLists !== liveLists) { throw new CheckpointError("resume.agent_definition_mismatch", recordedLists === undefined ? "this checkpoint records no readable digest of the tool lists its agent ran under, so the agent definition its name resolves to now cannot be shown to be the one it was parked under — " + "refused pre-CAS (the checkpoint stays pending): expire it and run the work again" : "the agent definition this checkpoint's agent name resolves to now has other tool lists than the ones it was parked under — " + "another definition carries the name, and continuing would run the agent with that definition's tools; " + "refused pre-CAS (the checkpoint stays pending): expire it, or resume it where the name resolves to the definition it was parked under", { reason: recordedLists === undefined ? "row_records_no_lists" : "live_lists_contradict_row" }); } } } const owesDelivery = outcomeGate !== "resource_limit" || readPendingSteerQueue(cp.state).length > 0 || (cp.state.runningBackgroundTasks?.length ?? 0) > 0; const usageWindowStore = runner.deps.usageWindowStore; if (owesDelivery && usageWindowStore !== undefined) { const preCasWindows = resolveUsageWindows(runner.deps.usageWindows); if (preCasWindows !== undefined && preCasWindows.length > 0) { const ledgerKey = (suppliedPrincipal ?? rowPrincipal) || GLOBAL_USAGE_KEY; const assertWindowOpen = async () => { const read = usageWindowStore.read(ledgerKey, preCasWindows, Date.now()); const raced = resumeSignal === undefined ? undefined : await raceSettlementAgainstSignal(read, resumeSignal); if (raced?.tag === "aborted") { throw new CheckpointError("checkpoint.resume_aborted", resumeCancelledSentence("while this deployment's usage limits were being read", cp)); } if (raced?.tag === "threw") throw raced.error; const wait = usageRetryAfterMs(raced === undefined ? await read : raced.value, preCasWindows); if (wait !== undefined) { throw new CheckpointError("resume.usage_window_exhausted", `a deployment usage window for ledger key ${JSON.stringify(ledgerKey)} is exhausted, and this checkpoint still owes a delivery a re-mint cannot carry — ` + `refused pre-CAS (nothing consumed, nothing unpinned): the same token and the same decision are redeemable once the window frees, in ${String(wait)}ms`, { retryAfterMs: wait }); } }; recheckGovernanceWindow = assertWindowOpen; } } } return { recheckGovernanceWindow }; }