UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

95 lines (94 loc) • 6.86 kB
import { CheckpointError, realApprovalOrgFact } from "../checkpoint-store.js"; import { resumeCancelledSentence } from "../park-noun.js"; import { constraintChainDigest, constraintChainDigestReading, constraintChainEntryOfSuppliedLayer } from "../tool-policy.js"; import { chainChanged, chainUnjudgeable, inRecordedSpelling, withMismatchCause } from "./chain-mismatch.js"; import { earlierChainMeaningRefusal } from "../checkpoint-row-verdict.js"; import { chainInstallationGaps, chainInstallationRefusal } from "./child-tree.js"; import { chainEntryFault } from "../mods/stations.js"; import { refuseRecordedAndMountedToolNames } from "../resume-config-judge.js"; import { chainRefusalOfThrown } from "./chain-refusal-throw.js"; export function resumeCheckpointScreen(input) { const { cp, resumeSignal, internals, tools, runner, chainRefusal } = input; const preCasGateBit = cp.gate.kind === "irreversible_ask" ? cp.gate.realApproval : undefined; if (realApprovalOrgFact(preCasGateBit) !== undefined && runner.deps.permissionRuleStore?.partitions.org !== true) { throw new CheckpointError("checkpoint.unsupported_version", `this checkpoint's approval was minted under organization governance (org ${String(realApprovalOrgFact(preCasGateBit))}, routed as ${String(preCasGateBit?.origin)}) and this worker has no org adjudication wiring (its permission-rule store carries no org partition) — a governed approval may only be redeemed where governance can be enforced; the checkpoint stays pending, resume it on an org-wired worker`, { reason: "governed_unwired" }); } if (resumeSignal?.aborted === true && cp.status === "pending") { throw new CheckpointError("checkpoint.resume_aborted", resumeCancelledSentence("before it began (the signal it was handed was already cancelled, so it took nothing)", cp)); } if (cp.state.workspaceHandle !== undefined && runner.deps.executionEnvFactory === undefined) { throw new CheckpointError("checkpoint.unsupported_version", "checkpoint has a remote workspaceHandle but no RunnerDeps.executionEnvFactory is wired to rebuild the env", { reason: "env_factory_missing" }); } if (cp.state.workspaceHandle !== undefined && internals?.requestedCwd !== undefined) { throw new CheckpointError("checkpoint.cwd_conflicts_restore", `internals.requestedCwd ("${internals.requestedCwd}") cannot be combined with a checkpoint workspace restore — ` + "the restored workspace's own mount path is authoritative for the task root. Drop requestedCwd on resume legs " + "(the checkpoint stays pending and is resumable without it)."); } refuseRecordedAndMountedToolNames(cp, internals, tools); const chainRung = withMismatchCause(chainRungRefusal(cp, internals, chainRefusal)); if (chainRung !== undefined) return { resumeInternals: internals, chainRung }; const resumeInternals = internals?.inheritedGate?.parentConstraints !== undefined ? { ...internals, inheritedGate: { ...internals.inheritedGate, parentConstraints: Object.freeze(internals.inheritedGate.parentConstraints.map((e) => ({ ...e }))), }, } : internals; return { resumeInternals }; } function chainRungRefusal(cp, internals, chainRefusal) { if (chainRefusal !== undefined) return chainRefusal; try { const chain = internals?.inheritedGate?.parentConstraints; const verdict = parentConstraintChainVerdict(cp.state, chain); if (verdict !== undefined) return verdict; const resupplied = (chain ?? []).map((pc) => pc.policy); const gaps = resupplied.length > 0 ? chainInstallationGaps(resupplied, { live: internals?.inheritedGate?.pathRuleInstallations, seed: cp.state.inheritedGate?.pathRuleInstallations }, (p) => p) : []; if (gaps.length > 0) return chainInstallationRefusal(gaps, true); const malformed = chainEntryFault(chain ?? []); return malformed !== undefined ? chainUnjudgeable(`${malformed}; rejected pre-CAS (the checkpoint stays pending)`) : undefined; } catch (e) { return chainRefusalOfThrown(e, (what) => chainUnjudgeable(`the inherited constraint chain could not be judged (${what} was thrown); rejected pre-CAS (the checkpoint stays pending)`)); } } export function parentConstraintChainVerdict(state, supplied) { const recorded = state.inheritedGate; if (recorded?.requiresParentConstraint !== true) return undefined; const count = supplied?.length ?? 0; if (count === 0) { return new CheckpointError("resume.parent_constraint_missing", "this checkpoint's task ran under inherited parent-policy constraints (state.inheritedGate.requiresParentConstraint) " + "— the live policy/onAsk closures cannot be persisted, so the resume must re-supply them via " + "resumeStream(token, outcome, taskConfig, internals) with internals.inheritedGate.parentConstraints; " + "rejected pre-CAS (the checkpoint stays pending) rather than silently dropping the ancestors' gate"); } const expected = recorded.parentConstraintCount; if (expected !== undefined && count !== expected) { return chainChanged(`re-supplied ${count} parent constraint(s) but the checkpoint recorded ${expected} — a partial/mismatched ` + "re-supply would run the resumed leg under a different ancestor chain than it suspended with; " + "rejected pre-CAS (the checkpoint stays pending) — re-resume with the full original chain"); } const expectedDigest = recorded.constraintDigest; const persistedChain = recorded.constraintChain; if (expectedDigest !== undefined && persistedChain !== undefined) { const suppliedChain = (supplied ?? []).map((pc, i) => inRecordedSpelling(constraintChainEntryOfSuppliedLayer(pc, persistedChain[i]), persistedChain[i])); const reading = constraintChainDigestReading(suppliedChain, expectedDigest); if (reading === "earlier") return earlierChainMeaningRefusal(); if (reading !== "same") { return chainChanged("the re-supplied parent-constraint chain's projection content does not match what the checkpoint recorded " + `(recorded ${expectedDigest}, re-supplied ${constraintChainDigest(suppliedChain)}) — a same-shape chain with different frozen ` + "deny data would run the resumed leg under a different ancestor gate than it suspended with; " + "rejected pre-CAS (the checkpoint stays pending) — re-resume with the original chain's policies " + "(including, when the row recorded one, the ancestor's auto-mode arming recipe verbatim)"); } } return undefined; }