@sema-agent/core
Version:
Stateless, task-oriented AI agent core
363 lines (362 loc) • 24 kB
JavaScript
import { strict as assert } from "node:assert";
import { randomUUID } from "node:crypto";
import { SESSION_RULES_GRAMMAR, SessionPolicyError, readSessionRulesShape } from "../session-policy-store.js";
import { beginContract } from "./contract-harness.js";
const OWNER = "kit-owner";
const G = SESSION_RULES_GRAMMAR;
const A = "KitToolA";
const B = "KitToolB";
const fresh = () => `kit-2151-${randomUUID()}`;
const declared = (rules) => ({ ...rules, grammar: G });
const replayed = (row) => ({ ...row, grammar: row.grammar ?? 0 });
const pair = () => {
const id = randomUUID();
return { from: `kit-rekey-from-${id}`, to: `kit-rekey-to-${id}` };
};
const owner = (principal) => ({ kind: "principal", principal });
const isLoosenRefusal = (e) => e instanceof SessionPolicyError && e.code === "loosen_forbidden";
const isGrammarRequired = (e) => e instanceof SessionPolicyError && e.code === "grammar_required";
async function partOf(store, sessionId) {
const row = await store.getRules(sessionId, OWNER);
assert.ok(row !== null, "the row the case wrote must read back");
const read = readSessionRulesShape(row);
assert.ok(!("fault" in read), `the stored row must read as rules: ${"fault" in read ? read.sentence : ""}`);
return read.rules.operatorRules;
}
export async function sessionPolicyStoreContract(make, runAssertion) {
const { run, settle } = beginContract(runAssertion);
run("the empty-string principal is the session-wide key: a write with it reads back by no principal and by it alike, one lineage", async () => {
const store = make();
const sid = fresh();
const stored = await store.putRules(sid, "", declared({ toolDeny: [A] }));
assert.deepEqual((await store.getRules(sid, undefined))?.toolDeny, [A]);
assert.deepEqual((await store.getRules(sid, ""))?.toolDeny, [A]);
const next = await store.putRules(sid, undefined, declared({ toolDeny: [A, B] }));
assert.equal(next.rev, stored.rev + 1, "the two spellings write one key");
});
run("a non-operator write over no row stamps an EMPTY operator part — the owner takes its own deny back", async () => {
const store = make();
const sid = fresh();
await store.putRules(sid, OWNER, declared({ toolDeny: [A] }));
assert.deepEqual(await partOf(store, sid), { grammar: G });
const back = await store.putRules(sid, OWNER, declared({}));
assert.equal(back.toolDeny, undefined);
assert.deepEqual(await partOf(store, sid), { grammar: G });
});
run("an operator write's whole value is the operator's: no part on the row, and the owner's drop of its entry is refused with nothing written", async () => {
const store = make();
const sid = fresh();
const stored = await store.putRules(sid, OWNER, declared({ toolDeny: [A] }), { operator: true });
assert.equal(await partOf(store, sid), undefined);
await assert.rejects(store.putRules(sid, OWNER, declared({})), isLoosenRefusal);
const row = await store.getRules(sid, OWNER);
assert.equal(row?.rev, stored.rev);
assert.deepEqual(row?.toolDeny, [A]);
});
run("an owner write over an operator row carries the operator's buckets as its part — above them it adds and takes back, below them it is refused", async () => {
const store = make();
const sid = fresh();
await store.putRules(sid, OWNER, declared({ toolDeny: [A] }), { operator: true });
await store.putRules(sid, OWNER, declared({ toolDeny: [A, B] }));
assert.deepEqual(await partOf(store, sid), { toolDeny: [A], grammar: G });
await store.putRules(sid, OWNER, declared({ toolDeny: [A] }));
assert.deepEqual((await store.getRules(sid, OWNER))?.toolDeny, [A]);
await assert.rejects(store.putRules(sid, OWNER, declared({})), isLoosenRefusal);
assert.deepEqual(await partOf(store, sid), { toolDeny: [A], grammar: G });
});
run("the same entry from both writers: once an operator write carries it, the owner's drop is refused", async () => {
const store = make();
const sid = fresh();
await store.putRules(sid, OWNER, declared({ toolDeny: [A] }));
await store.putRules(sid, OWNER, declared({ toolDeny: [A] }), { operator: true });
await assert.rejects(store.putRules(sid, OWNER, declared({})), isLoosenRefusal);
assert.deepEqual((await store.getRules(sid, OWNER))?.toolDeny, [A]);
});
run("a part handed in with the value is no authority: the stored part is the judge's", async () => {
const store = make();
const sid = fresh();
await store.putRules(sid, OWNER, declared({ toolDeny: [A] }), { operator: true });
const forged = { toolDeny: [A, B], grammar: G, operatorRules: { grammar: G } };
await store.putRules(sid, OWNER, forged);
assert.deepEqual(await partOf(store, sid), { toolDeny: [A], grammar: G });
await assert.rejects(store.putRules(sid, OWNER, declared({})), isLoosenRefusal);
});
run("allowDirs: the owner's own confinement is the owner's to remove; an operator's is not", async () => {
const store = make();
const own = fresh();
await store.putRules(own, OWNER, declared({ allowDirs: ["/work/src"] }));
await store.putRules(own, OWNER, declared({}));
assert.equal((await store.getRules(own, OWNER))?.allowDirs, undefined);
const operators = fresh();
await store.putRules(operators, OWNER, declared({ allowDirs: ["/work"] }), { operator: true });
await store.putRules(operators, OWNER, declared({ allowDirs: ["/work/src"] }));
await store.putRules(operators, OWNER, declared({ allowDirs: ["/work"] }));
await assert.rejects(store.putRules(operators, OWNER, declared({})), isLoosenRefusal);
assert.deepEqual((await store.getRules(operators, OWNER))?.allowDirs, ["/work"]);
});
run("a stored row replayed by a non-operator onto a fresh or erased key keeps who may take what back (an import's replay)", async () => {
const store = make();
const exported = async (sid) => {
const records = store.listBySession !== undefined ? await store.listBySession(sid) : [{ principal: OWNER, rules: (await store.getRules(sid, OWNER)) }];
assert.equal(records.length, 1);
return replayed(records[0].rules);
};
const opSid = fresh();
await store.putRules(opSid, OWNER, declared({ toolDeny: [A] }), { operator: true });
const opRow = await exported(opSid);
const replay = fresh();
await store.putRules(replay, OWNER, opRow);
await assert.rejects(store.putRules(replay, OWNER, declared({})), isLoosenRefusal);
const mixedSid = fresh();
await store.putRules(mixedSid, OWNER, declared({ toolDeny: [A] }), { operator: true });
await store.putRules(mixedSid, OWNER, declared({ toolDeny: [A, B] }));
const mixed = await exported(mixedSid);
const target = fresh();
await store.putRules(target, OWNER, mixed);
assert.deepEqual(await partOf(store, target), { toolDeny: [A], grammar: G });
await store.putRules(target, OWNER, declared({ toolDeny: [A] }));
await assert.rejects(store.putRules(target, OWNER, declared({})), isLoosenRefusal);
if (store.deleteBySession !== undefined) {
await store.deleteBySession(replay);
await store.putRules(replay, OWNER, opRow);
await assert.rejects(store.putRules(replay, OWNER, declared({})), isLoosenRefusal);
}
});
run("a value built on the no-row record (`{ rev: 0 }`, what a read of an unwritten key answers) is written now: the owner takes back what it added through it", async () => {
const store = make();
const sid = fresh();
const noRowRecord = { rev: 0, toolDeny: [A], grammar: G };
await store.putRules(sid, OWNER, noRowRecord);
assert.deepEqual(await partOf(store, sid), { grammar: G });
const back = await store.putRules(sid, OWNER, declared({}));
assert.equal(back.toolDeny, undefined);
});
run("the part rides the session's enumeration (an export carries who may take what back)", async () => {
const store = make();
if (store.listBySession === undefined)
return;
const sid = fresh();
await store.putRules(sid, OWNER, declared({ toolDeny: [A] }), { operator: true });
await store.putRules(sid, OWNER, declared({ toolDeny: [A, B] }));
const records = await store.listBySession(sid);
assert.equal(records.length, 1);
const read = readSessionRulesShape(records[0].rules);
assert.ok(!("fault" in read));
assert.deepEqual(read.rules.operatorRules, { toolDeny: [A], grammar: G });
});
run("a write that declares no grammar is refused `grammar_required` with nothing written — on a fresh key, as an operator, and over a row", async () => {
const store = make();
const undeclaredStore = store;
const sid = fresh();
await assert.rejects(undeclaredStore.putRules(sid, OWNER, { toolDeny: [A] }), isGrammarRequired);
await assert.rejects(undeclaredStore.putRules(sid, OWNER, { toolDeny: [A] }, { operator: true }), isGrammarRequired);
assert.equal(await store.getRules(sid, OWNER), null);
const stored = await store.putRules(sid, OWNER, declared({ toolDeny: [A] }), { operator: true });
const { grammar: _stamp, ...unstamped } = stored;
void _stamp;
await assert.rejects(undeclaredStore.putRules(sid, OWNER, unstamped, { operator: true }), isGrammarRequired);
const stampSaysNothing = { ...stored, grammar: undefined };
await assert.rejects(undeclaredStore.putRules(sid, OWNER, stampSaysNothing, { operator: true }), isGrammarRequired);
const row = await store.getRules(sid, OWNER);
assert.equal(row?.rev, stored.rev);
assert.deepEqual(row?.toolDeny, [A]);
});
run("rekeyPrincipal is a member; a plan over an owner with no rows is empty, and so is a move onto the same principal", async () => {
const store = make();
const { from } = pair();
assert.equal(typeof store.rekeyPrincipal, "function");
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: `${from}-elsewhere`, mode: "plan" }), { kind: "plan", rows: 0, conflicting: 0 });
const sid = fresh();
await store.putRules(sid, from, declared({ toolDeny: [A] }));
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: from, mode: "plan" }), { kind: "plan", rows: 0, conflicting: 0 });
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: from, mode: "arrive" }), { kind: "done", mode: "arrive", rows: 0 });
});
run("plan → arrive → leave re-files the row: arrive leaves the source rules in place, the target reads them at the row's own stamp, leave erases the source and a second plan finds nothing left", async () => {
const store = make();
const { from, to } = pair();
const sid = fresh();
await store.putRules(sid, from, declared({ toolDeny: [A] }));
const source = await store.putRules(sid, from, declared({ toolDeny: [A, B] }));
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "plan" }), { kind: "plan", rows: 1, conflicting: 0 });
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "arrive" }), { kind: "done", mode: "arrive", rows: 1 });
assert.deepEqual((await store.getRules(sid, from))?.toolDeny, [A, B], "arrive moves no source rule");
const arrived = await store.getRules(sid, to);
assert.deepEqual(arrived?.toolDeny, [A, B]);
assert.equal(arrived?.rev, source.rev, "a target with no mark takes the row's own stamp");
assert.equal(arrived?.gen ?? 0, source.gen ?? 0);
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "leave" }), { kind: "done", mode: "leave", rows: 1 });
assert.equal(await store.getRules(sid, from), null);
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "plan" }), { kind: "plan", rows: 0, conflicting: 0 });
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "leave" }), { kind: "done", mode: "leave", rows: 0 });
assert.equal((await store.getRules(sid, to))?.rev, source.rev);
});
run("after leave the source key's next write climbs past the row that left and opens a new lineage", async () => {
const store = make();
const { from, to } = pair();
const sid = fresh();
await store.putRules(sid, from, declared({ toolDeny: [A] }));
const source = await store.putRules(sid, from, declared({ toolDeny: [A, B] }));
await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "arrive" });
await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "leave" });
const again = await store.putRules(sid, from, declared({ toolDeny: [A] }));
assert.ok(again.rev > source.rev, `rev ${again.rev} must climb past ${source.rev}`);
assert.ok((again.gen ?? 0) > (source.gen ?? 0), "the erase step opened the next lineage");
});
run("a target row that is not this move's arrival makes arrive a conflict with nothing written — equal rules included", async () => {
const store = make();
const { from, to } = pair();
const sid = fresh();
const source = await store.putRules(sid, from, declared({ toolDeny: [A] }));
const target = await store.putRules(sid, to, declared({ toolDeny: [A] }));
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "plan" }), { kind: "plan", rows: 1, conflicting: 1 });
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "arrive" }), { kind: "conflict", mode: "arrive", rows: 1, conflicting: 1 });
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "leave" }), { kind: "done", mode: "leave", rows: 0 });
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "withdraw" }), { kind: "done", mode: "withdraw", rows: 0 });
const s = await store.getRules(sid, from);
const t = await store.getRules(sid, to);
assert.equal(s?.rev, source.rev);
assert.equal(t?.rev, target.rev);
assert.deepEqual([s?.toolDeny, t?.toolDeny], [[A], [A]]);
});
run("withdraw erases the arrival and clears the record: the source rules stay, leave then erases nothing, and a later arrive lands above the withdrawn lineage", async () => {
const store = make();
const { from, to } = pair();
const sid = fresh();
await store.putRules(sid, from, declared({ toolDeny: [A] }));
const source = await store.putRules(sid, from, declared({ toolDeny: [A, B] }));
await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "arrive" });
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "withdraw" }), { kind: "done", mode: "withdraw", rows: 1 });
assert.equal(await store.getRules(sid, to), null);
const kept = await store.getRules(sid, from);
assert.equal(kept?.rev, source.rev);
assert.deepEqual(kept?.toolDeny, [A, B]);
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "leave" }), { kind: "done", mode: "leave", rows: 0 }, "the record is gone");
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "arrive" }), { kind: "done", mode: "arrive", rows: 1 });
const again = await store.getRules(sid, to);
assert.ok(again !== null && again.rev > source.rev && (again.gen ?? 0) > (source.gen ?? 0), "the second arrival lands above the withdrawn one, in a new lineage");
});
run("an ordinary write of the source after arrive drops the record: leave keeps that row, and a plan reports it", async () => {
const store = make();
const { from, to } = pair();
const sid = fresh();
await store.putRules(sid, from, declared({ toolDeny: [A] }));
await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "arrive" });
await store.putRules(sid, from, declared({ toolDeny: [A, B] }));
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "leave" }), { kind: "done", mode: "leave", rows: 0 });
assert.deepEqual((await store.getRules(sid, from))?.toolDeny, [A, B], "the confirmed write is not lost");
assert.deepEqual(await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "plan" }), { kind: "plan", rows: 1, conflicting: 1 });
});
run("the counters: a target whose lineage was erased raises the arrival to its next write, and a mark-only source raises the target's floor", async () => {
const store = make();
if (store.deleteBySession === undefined)
return;
const { from, to } = pair();
const sid = fresh();
for (const deny of [[A], [A, B], [B]])
await store.putRules(sid, to, declared({ toolDeny: deny }));
await store.deleteBySession(sid);
const source = await store.putRules(sid, from, declared({ toolDeny: [A] }));
await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "arrive" });
const arrived = await store.getRules(sid, to);
assert.equal(arrived?.rev, 4, "the per-position maximum of the row's (1, 0) and the target's next write");
assert.ok((arrived?.gen ?? 0) > (source.gen ?? 0), "the arrival opens the target's next lineage, never one it erased");
const markOnly = fresh();
for (const deny of [[A], [A, B], [B]])
await store.putRules(markOnly, from, declared({ toolDeny: deny }));
await store.deleteBySession(markOnly);
await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "arrive" });
const first = await store.putRules(markOnly, to, declared({ toolDeny: [A] }));
assert.ok(first.rev > 3 && (first.gen ?? 0) >= 1, `the target's first write (${first.rev}, ${first.gen}) climbs past the source's mark`);
});
run("a floor a mark-only source raised over a live target row outlives the target's next write: the erase after it opens a lineage above every one the source wore", async () => {
const store = make();
if (store.deleteBySession === undefined)
return;
const { from, to } = pair();
const sid = fresh();
let worn = 0;
for (const deny of [[A], [B]]) {
worn = Math.max(worn, (await store.putRules(sid, from, declared({ toolDeny: deny }))).gen ?? 0);
await store.deleteBySession(sid);
}
await store.putRules(sid, to, declared({ toolDeny: [A] }));
await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "arrive" });
await store.putRules(sid, to, declared({ toolDeny: [A, B] }));
await store.deleteBySession(sid);
const again = await store.putRules(sid, to, declared({ toolDeny: [A] }));
assert.ok((again.gen ?? 0) > worn, `the target's lineage after its erase (${again.gen}) must be above every lineage the source wore (${worn})`);
});
run("a withdrawn lineage is never handed to a later arrival", async () => {
const store = make();
if (store.deleteBySession === undefined)
return;
const { from, to } = pair();
const other = `${from}-c`;
const sid = fresh();
await store.putRules(sid, to, declared({ toolDeny: [A] }));
for (let i = 0; i < 5; i++) {
await store.putRules(sid, from, declared({ toolDeny: [A] }));
await store.putRules(sid, other, declared({ toolDeny: [A] }));
await store.deleteBySession(sid);
}
for (let i = 0; i < 5; i++)
await store.putRules(sid, from, declared({ toolDeny: i % 2 === 0 ? [A, B] : [A] }));
for (let i = 0; i < 15; i++)
await store.putRules(sid, other, declared({ toolDeny: i % 2 === 0 ? [A, B] : [A] }));
const a = await store.getRules(sid, from);
const c = await store.getRules(sid, other);
assert.equal(a?.gen, c?.gen, "the two sources share one lineage number");
await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "arrive" });
const withdrawn = await store.getRules(sid, to);
await store.rekeyPrincipal({ from: owner(from), toPrincipal: to, mode: "withdraw" });
await store.rekeyPrincipal({ from: owner(other), toPrincipal: to, mode: "arrive" });
const later = await store.getRules(sid, to);
assert.ok(withdrawn !== null && later !== null);
assert.notEqual(later.gen ?? 0, withdrawn.gen ?? 0, "the later arrival must not continue the withdrawn lineage");
});
run("the input is read before anything else: a malformed one is a TypeError and nothing is written", async () => {
const store = make();
const unchecked = store;
const { from, to } = pair();
const sid = fresh();
await store.putRules(sid, from, declared({ toolDeny: [A] }));
const bad = [
undefined,
{ from: owner(from), toPrincipal: to },
{ from: owner(from), toPrincipal: to, mode: "apply" },
{ from: owner(from), toPrincipal: to, mode: "plan", dryRun: true },
{ from: { kind: "principal", principal: " " }, toPrincipal: to, mode: "arrive" },
{ from: owner(from), toPrincipal: "", mode: "arrive" },
];
for (const input of bad)
await assert.rejects(unchecked.rekeyPrincipal(input), TypeError, JSON.stringify(input) ?? "undefined");
assert.equal(await store.getRules(sid, to), null);
assert.deepEqual((await store.getRules(sid, from))?.toolDeny, [A]);
});
run("a write declaring an earlier grammar is stored in that grammar — the stamp is the grammar the judge read the entries in, on a fresh key and over a row", async () => {
const store = make();
const sid = fresh();
const first = await store.putRules(sid, OWNER, { toolDeny: [A], grammar: 4 }, { operator: true });
assert.equal(first.grammar, 4);
const over = await store.putRules(sid, OWNER, { toolDeny: [A, B], grammar: 4 }, { expectedRev: first.rev });
assert.equal(over.grammar, 4);
const row = await store.getRules(sid, OWNER);
assert.equal(row?.grammar, 4);
assert.deepEqual(row?.toolDeny, [A, B]);
});
run("a non-operator write declaring a grammar older than the stored rules it is compared against is refused `grammar_older_than_rules`, nothing written", async () => {
const store = make();
const sid = fresh();
const stored = await store.putRules(sid, OWNER, declared({ toolDeny: [A] }), { operator: true });
await assert.rejects(store.putRules(sid, OWNER, { toolDeny: [A, B], grammar: G - 1 }, { expectedRev: stored.rev }), (e) => {
if (!(e instanceof SessionPolicyError && e.code === "grammar_older_than_rules"))
return false;
assert.deepEqual({ declared: e.detail.declaredGrammar, rules: e.detail.rulesGrammar }, { declared: G - 1, rules: G });
return true;
});
const row = await store.getRules(sid, OWNER);
assert.equal(row?.rev, stored.rev);
assert.deepEqual(row?.toolDeny, [A]);
});
await settle();
}