@sema-agent/core
Version:
Stateless, task-oriented AI agent core
85 lines (84 loc) • 3.81 kB
JavaScript
import { strict as assert } from "node:assert";
import { decodeGrantHolder, encodeGrantHolder, isHolderSuperseded } from "../grant-holder.js";
import { readStoreFence } from "../checkpoint-generation.js";
export function deadGrants(grants) {
return [
["replaced", grants.replaced()],
["ended", grants.ended()],
["unknownStatus", grants.unknownStatus()],
];
}
const LIVE_STATUSES = new Set(["running", "parked"]);
export function inMemoryGrantTable() {
const runs = new Map();
let n = 0;
const run = (leg, status) => {
const taskId = `kit-run-${++n}`;
runs.set(taskId, { leg, status });
return taskId;
};
const holderIsCurrent = (holder) => {
const { taskId, legId } = decodeGrantHolder(holder);
const row = runs.get(taskId);
return row !== undefined && row.leg === legId && LIVE_STATUSES.has(row.status);
};
const grants = {
current: () => encodeGrantHolder({ taskId: run("leg-1", "running"), legId: "leg-1" }),
replaced: () => encodeGrantHolder({ taskId: run("leg-2", "running"), legId: "leg-1" }),
ended: () => encodeGrantHolder({ taskId: run("leg-1", "completed"), legId: "leg-1" }),
unknownStatus: () => encodeGrantHolder({ taskId: run("leg-1", "kit-status-outside-the-live-set"), legId: "leg-1" }),
malformed: "kit-not-a-grant",
retire: (holder) => {
const { taskId } = decodeGrantHolder(holder);
const row = runs.get(taskId);
if (row !== undefined)
runs.set(taskId, { leg: row.leg, status: "completed" });
},
};
return { holderIsCurrent, grants };
}
export async function rejectsSuperseded(fn, what) {
let thrown;
let threw = false;
try {
await fn();
}
catch (err) {
thrown = err;
threw = true;
}
assert.ok(threw, `${what}: a write carrying a grant that is not current must be refused, got a result`);
if (!isHolderSuperseded(thrown))
assert.fail(`${what}: the refusal must be holder.superseded (holderSupersededError), got ${describeThrown(thrown)}`);
}
function describeThrown(thrown) {
return thrown instanceof Error ? `an ${thrown.constructor === Error ? "Error" : "error object"}` : typeof thrown;
}
export async function rejectsMalformed(fn, what) {
let thrown;
let threw = false;
try {
await fn();
}
catch (err) {
thrown = err;
threw = true;
}
assert.ok(threw, `${what}: a write carrying a malformed holder must throw (the judge throws, the write throws with it), got a result`);
if (isHolderSuperseded(thrown))
assert.fail(`${what}: a malformed holder was answered holder.superseded — the judge's throw must reach the caller, not be read as a dead grant`);
}
export function holderBinding(kind, bareDeclares, composed, grants) {
if (!bareDeclares && composed === undefined && grants === undefined)
return undefined;
assert.ok(composed !== undefined && grants !== undefined, `a ${kind} store binding that judges holders hands the kit both \`composed\` (the store as the deployment composes it, every wrapper included) and \`grants\` (holders minted against that store's judge) — got ${composed === undefined ? "no composed" : "composed"} and ${grants === undefined ? "no grants" : "grants"}`);
return {
composed: () => {
const store = composed();
const fence = readStoreFence(kind, `the kit's composed ${kind} store`, store);
assert.ok(fence !== undefined, `the composed ${kind} store must declare \`fence = { holder: "writes" }\` — a wrapper that does not forward its inner store's declaration fences nothing`);
return store;
},
grants,
};
}