@sema-agent/core
Version:
Stateless, task-oriented AI agent core
55 lines (54 loc) • 4.31 kB
JavaScript
import { CheckpointError, readPendingSteerQueue } from "../checkpoint-store.js";
import { GLOBAL_USAGE_KEY, resolveUsageWindows, usageRetryAfterMs } from "../usage-window-store.js";
import { placementValueOrAbsent } from "./checkpoint-scope.js";
import { assertNoRetiredRunnerDeps } from "./retired-runner-deps.js";
import { resolveMicroCompactKnob } from "./prepare-config-doors.js";
export function resumeInternalsAndConfig(input) {
const { cp, config, internals, outcomeGate, runner } = input;
let windowEntrance;
assertNoRetiredRunnerDeps(runner.deps);
resolveMicroCompactKnob(runner.deps.microCompact);
if (runner.deps.lockedConfig?.keys?.includes("toolPolicy") === true && config.basePolicyForResumeEdit !== undefined) {
throw new CheckpointError("checkpoint.invalid_outcome", "TaskSpec.basePolicyForResumeEdit is administratively locked by this deployment (locked key \"toolPolicy\") — a task-supplied " +
"resume-edit policy is refused pre-CAS (the checkpoint stays pending); remove the field or change the deployment's lock configuration");
}
let recheckGovernanceWindow;
{
const rowPrincipal = cp.principal || undefined;
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 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 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 wait = usageRetryAfterMs(await usageWindowStore.read(ledgerKey, preCasWindows, Date.now()), 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;
windowEntrance = assertWindowOpen();
}
}
}
return { recheckGovernanceWindow, windowEntrance };
}