@sema-agent/core
Version:
Stateless, task-oriented AI agent core
322 lines (321 loc) • 27.8 kB
JavaScript
import { strict as assert } from "node:assert";
import { keepToolResultBytes, rekeyToolResultOwner, ToolResultBytesOverLimitError } from "../tool-result-store.js";
import { identityReadVerdict } from "../owner-identity.js";
import { beginContract } from "./contract-harness.js";
export async function toolResultStoreContract(make, runAssertion, onOptionalMember) {
const { run, settle } = beginContract(runAssertion);
run("put/get full + sliced; write-once no-op; unknown → undefined", async () => {
const store = make();
const ref = "tr_s_c";
await store.put(ref, "0123456789");
assert.deepEqual(await store.get(ref), { content: "0123456789", offset: 0, totalChars: 10 });
assert.deepEqual(await store.get(ref, { offset: 2, limit: 3 }), { content: "234", offset: 2, totalChars: 10 });
await store.put(ref, "IGNORED");
assert.equal((await store.get(ref)).content, "0123456789");
assert.equal(await store.get("tr_unknown"), undefined);
});
run("unsafe refs are rejected at the WRITE by every backend; the read face degrades instead", async () => {
const store = make();
const NUL = String.fromCharCode(0);
const unsafe = ["", ".", "..", "a/b", "../escape", "dir\\ref", `tr_x${NUL}y`];
for (const ref of unsafe) {
await assert.rejects((async () => store.put(ref, "payload"))(), `put(${JSON.stringify(ref)}) must reject — a durable backend cannot key it`);
assert.equal(await store.get(ref), undefined, "get of an unsafe ref degrades to undefined");
}
await store.put("tr_sess-1_call.7", "ok");
assert.equal((await store.get("tr_sess-1_call.7")).content, "ok");
});
run("a ref the contract ACCEPTS round-trips on every backend (no backend is stricter than the rule)", async () => {
const store = make();
for (const ref of ["tr_sess_call:1", "tr_sess_call%2F1", "tr_sess_call 1", "tr_sess_日本語"]) {
await store.put(ref, `payload-${ref}`);
assert.equal((await store.get(ref))?.content, `payload-${ref}`, `put(${JSON.stringify(ref)}) must round-trip here`);
}
assert.equal((await store.get("tr_sess_call:1")).content, "payload-tr_sess_call:1");
});
run("provenance: content+owner are one write; ownerOf answers; a different owner is a TYPED refusal; unowned rows stay unowned", async () => {
const store = make();
assert.equal(typeof store.ownerOf, "function", "a backend must implement ownerOf — a store that cannot say who owns a ref cannot back a host read face");
const ownerOf = (ref) => Promise.resolve(store.ownerOf(ref));
const ref = "tr_s1~c1";
await store.put(ref, "0123456789", { sessionId: "sess-A", taskId: "task-1" });
assert.deepEqual(await ownerOf(ref), { sessionId: "sess-A", taskId: "task-1" }, "the winning write's owner round-trips");
await store.put(ref, "IGNORED", { sessionId: "sess-A", taskId: "task-1" });
assert.equal((await store.get(ref)).content, "0123456789");
assert.deepEqual(await ownerOf(ref), { sessionId: "sess-A", taskId: "task-1" });
await store.put(ref, "IGNORED");
assert.deepEqual(await ownerOf(ref), { sessionId: "sess-A", taskId: "task-1" }, "an ownerless put must not clear the owner");
for (const other of [{ sessionId: "sess-B" }, { sessionId: "sess-A", taskId: "task-2" }]) {
await assert.rejects((async () => store.put(ref, "other tenant's bytes", other))(), (err) => err.code === "tool_result.ref_conflict", `put(${JSON.stringify(other)}) on an occupied ref must reject with code tool_result.ref_conflict`);
}
assert.equal((await store.get(ref)).content, "0123456789", "a refused put must not have overwritten anything");
const unowned = "tr_no_owner~c";
await store.put(unowned, "bytes from a write site that stated no owner");
assert.equal(await ownerOf(unowned), undefined, "a row stored without provenance is UNOWNED");
await store.put(unowned, "bytes from a write site that stated no owner", { sessionId: "sess-A" });
assert.equal(await ownerOf(unowned), undefined, "put must not back-fill an owner onto an unowned row");
assert.equal((await store.get(unowned)).content, "bytes from a write site that stated no owner");
assert.equal(await ownerOf("tr_never~written"), undefined);
});
run("identity: principal (null included) and isolationScope round-trip; anonymous ≠ none recorded; a different identity is a typed refusal; the engine's read judge reads the answer", async () => {
const store = make();
const ownerOf = (ref) => Promise.resolve(store.ownerOf(ref));
const named = { sessionId: "sess-A", principal: "kit-alice" };
const scoped = { sessionId: "sess-A", principal: "kit-alice", isolationScope: "kit-alice-team-2" };
const anonymous = { sessionId: "sess-A", principal: null };
const recordedNone = { sessionId: "sess-A" };
const cases = [
["tr_id~named", named],
["tr_id~scoped", scoped],
["tr_id~anon", anonymous],
["tr_id~none", recordedNone],
];
for (const [ref, provenance] of cases)
await store.put(ref, `bytes of ${ref}`, provenance);
for (const [ref, provenance] of cases)
assert.deepEqual(await ownerOf(ref), provenance, `${ref}: the owner answered is exactly the one written`);
assert.equal((await ownerOf("tr_id~anon")).principal, null, "the anonymous identity reads back as null, never as an absent member");
assert.equal("principal" in await ownerOf("tr_id~none"), false, "an owner written with no identity reads back with no principal member, never as null");
const alice = { principal: "kit-alice" };
const aliceTeam = { principal: "kit-alice", isolationScope: "kit-alice-team-2" };
const anon = { principal: null };
const bob = { principal: "kit-bob" };
assert.equal(identityReadVerdict(await ownerOf("tr_id~named"), alice), "readable");
assert.equal(identityReadVerdict(await ownerOf("tr_id~named"), bob), "other_identity");
assert.equal(identityReadVerdict(await ownerOf("tr_id~named"), aliceTeam), "other_identity", "the same person in another scope of their work");
assert.equal(identityReadVerdict(await ownerOf("tr_id~named"), anon), "other_identity");
assert.equal(identityReadVerdict(await ownerOf("tr_id~scoped"), aliceTeam), "readable");
assert.equal(identityReadVerdict(await ownerOf("tr_id~scoped"), alice), "other_identity");
assert.equal(identityReadVerdict(await ownerOf("tr_id~anon"), anon), "readable");
assert.equal(identityReadVerdict(await ownerOf("tr_id~anon"), alice), "other_identity");
for (const reader of [alice, anon, bob])
assert.equal(identityReadVerdict(await ownerOf("tr_id~none"), reader), "no_identity", "an entry that recorded no identity is nobody's to read");
for (const [ref, other] of [["tr_id~named", { sessionId: "sess-A", principal: "kit-bob" }], ["tr_id~named", { sessionId: "sess-A", principal: null }], ["tr_id~anon", { sessionId: "sess-A", principal: "kit-alice" }], ["tr_id~none", named], ["tr_id~named", scoped]]) {
await assert.rejects((async () => store.put(ref, "another identity's bytes", other))(), (err) => err.code === "tool_result.ref_conflict", `put(${JSON.stringify(other)}) on ${ref} must reject with tool_result.ref_conflict`);
}
assert.equal((await store.get("tr_id~named")).content, "bytes of tr_id~named", "a refused put overwrote nothing");
if (typeof store.putBytes === "function") {
const piece = Uint8Array.from([1, 2, 3]);
for (const [ref, provenance] of [["tr_idb~named", scoped], ["tr_idb~anon", anonymous], ["tr_idb~none", recordedNone]]) {
await store.putBytes(ref, piece, provenance, "application/octet-stream");
assert.deepEqual(await ownerOf(ref), provenance, `${ref}: the byte entry's owner is exactly the one written`);
}
assert.equal(identityReadVerdict(await ownerOf("tr_idb~named"), aliceTeam), "readable");
assert.equal(identityReadVerdict(await ownerOf("tr_idb~anon"), anon), "readable");
assert.equal(identityReadVerdict(await ownerOf("tr_idb~anon"), alice), "other_identity");
assert.equal(identityReadVerdict(await ownerOf("tr_idb~none"), anon), "no_identity", "a byte entry that recorded no identity is nobody's either");
await assert.rejects((async () => store.putBytes("tr_idb~anon", piece, { sessionId: "sess-A", principal: "kit-alice" }, "application/octet-stream"))(), (err) => err.code === "tool_result.ref_conflict", "another identity on an occupied byte ref is the typed refusal");
}
});
run("identity re-key (required): plan counts, leave re-files each owner record in place — the moved identity reads, the old one does not; the rest of the entry stays; a second leave finds nothing", async () => {
const store = make();
assert.equal(typeof store.rekeyIdentity, "function", "rekeyIdentity is a required member of the ToolResultStore contract");
const move = { fromPrincipal: "kit-alice", toPrincipal: "kit-bob", scopes: [{ fromScope: "kit-alice", toScope: "kit-bob" }] };
assert.deepEqual(await store.rekeyIdentity({ ...move, mode: "plan" }), { kind: "plan", rows: 0, conflicting: 0, unpairedScopeRows: 0 }, "an empty store plans nothing");
const unchecked = store;
await assert.rejects((async () => unchecked.rekeyIdentity({ ...move, mode: "arrive" }))(), TypeError, "a step a one-step seat does not offer is a TypeError");
const ownerOf = (ref) => Promise.resolve(store.ownerOf(ref));
await store.put("tr_rk~named", "NAMED", { sessionId: "sess-R", taskId: "t1", principal: "kit-alice" });
await store.put("tr_rk~team", "TEAM", { sessionId: "sess-R", principal: "kit-alice", isolationScope: "kit-team" });
await store.put("tr_rk~other", "OTHER", { sessionId: "sess-S", principal: "kit-carol" });
await store.put("tr_rk~anon", "ANON", { sessionId: "sess-S", principal: null });
await store.put("tr_rk~none", "NONE", { sessionId: "sess-S" });
await store.put("tr_rk~unowned", "UNOWNED");
const bytes = typeof store.putBytes === "function";
if (bytes)
await store.putBytes("tr_rkb~named", Uint8Array.from([7, 8, 9]), { sessionId: "sess-R", principal: "kit-alice" }, "application/octet-stream");
const moving = bytes ? 3 : 2;
assert.deepEqual(await store.rekeyIdentity({ ...move, mode: "plan" }), { kind: "plan", rows: moving, conflicting: 0, unpairedScopeRows: 1 });
assert.deepEqual(await store.rekeyIdentity({ ...move, mode: "leave" }), { kind: "done", mode: "leave", rows: moving });
assert.deepEqual(await ownerOf("tr_rk~named"), { sessionId: "sess-R", taskId: "t1", principal: "kit-bob" }, "the session and task stay, the identity is the new one");
assert.deepEqual(await ownerOf("tr_rk~team"), { sessionId: "sess-R", principal: "kit-bob", isolationScope: "kit-team" });
assert.deepEqual(await ownerOf("tr_rk~other"), { sessionId: "sess-S", principal: "kit-carol" }, "another person's entry stays");
assert.deepEqual(await ownerOf("tr_rk~anon"), { sessionId: "sess-S", principal: null }, "the anonymous identity stays");
assert.deepEqual(await ownerOf("tr_rk~none"), { sessionId: "sess-S" }, "an entry that recorded no identity is not given one");
assert.equal(await ownerOf("tr_rk~unowned"), undefined, "an unowned entry stays unowned");
assert.equal((await store.get("tr_rk~named"))?.content, "NAMED", "the content stays");
assert.equal(identityReadVerdict(await ownerOf("tr_rk~named"), { principal: "kit-bob" }), "readable", "the moved identity reads");
assert.equal(identityReadVerdict(await ownerOf("tr_rk~named"), { principal: "kit-alice" }), "other_identity", "the old identity does not");
if (bytes) {
assert.deepEqual(await ownerOf("tr_rkb~named"), { sessionId: "sess-R", principal: "kit-bob" }, "a byte entry's owner is re-filed the same way");
assert.deepEqual([...(await Promise.resolve((await Promise.resolve(store.bytesOf("tr_rkb~named"))).bytes()))], [7, 8, 9], "its bytes stay");
}
assert.deepEqual(await store.rekeyIdentity({ ...move, mode: "leave" }), { kind: "done", mode: "leave", rows: 0 }, "a second leave finds nothing");
assert.deepEqual(await store.rekeyIdentity({ ...move, mode: "plan" }), { kind: "plan", rows: 0, conflicting: 0, unpairedScopeRows: 0 });
await store.put("tr_rk~named", "IGNORED", { sessionId: "sess-R", taskId: "t1", principal: "kit-bob" });
await assert.rejects((async () => store.put("tr_rk~named", "NAMED", { sessionId: "sess-R", taskId: "t1", principal: "kit-alice" }))(), (err) => err.code === "tool_result.ref_conflict", "the old identity no longer owns the ref");
assert.equal((await store.get("tr_rk~named"))?.content, "NAMED");
});
run("identity re-key (required): a move of the person alone — leave writes each owner record exactly as the one law answers it", async () => {
const store = make();
const move = { fromPrincipal: "kit-alice", toPrincipal: "kit-bob", scopes: [] };
const owners = {
"tr_rk1~named": { sessionId: "sess-R", taskId: "t1", principal: "kit-alice" },
"tr_rk1~team": { sessionId: "sess-R", principal: "kit-alice", isolationScope: "kit-team" },
"tr_rk1~anonInAlice": { sessionId: "sess-S", principal: null, isolationScope: "kit-alice" },
"tr_rk1~carolInAlice": { sessionId: "sess-S", principal: "kit-carol", isolationScope: "kit-alice" },
};
for (const [ref, owner] of Object.entries(owners))
await store.put(ref, ref, owner);
assert.deepEqual(await store.rekeyIdentity({ ...move, mode: "plan" }), { kind: "plan", rows: 2, conflicting: 0, unpairedScopeRows: 2 });
assert.deepEqual(await store.rekeyIdentity({ ...move, mode: "leave" }), { kind: "done", mode: "leave", rows: 2 });
for (const [ref, owner] of Object.entries(owners)) {
const law = rekeyToolResultOwner(owner, move);
assert.deepEqual(await Promise.resolve(store.ownerOf(ref)), law.kind === "rekeyed" ? law.owner : owner, `${ref}: the record leave wrote is the one law's`);
}
assert.equal(identityReadVerdict(await Promise.resolve(store.ownerOf("tr_rk1~named")), { principal: "kit-bob", isolationScope: "kit-alice" }), "readable", "the person's own partition stays with the entry");
});
run("deleteBySession (optional): four-state semantics when present; absence is REPORTED, not silently green", async () => {
const probe = make();
if (probe.deleteBySession === undefined) {
onOptionalMember?.({ member: "deleteBySession", status: "absent" });
return;
}
assert.equal(typeof probe.deleteBySession, "function", `deleteBySession is present but not callable (got ${typeof probe.deleteBySession}) — a garbage member is a DEFECT, not an optional-member absence`);
const store = make();
const del = (sessionId) => Promise.resolve(store.deleteBySession(sessionId));
await store.put("tr_sweep~a", "A-bytes", { sessionId: "sess-X", taskId: "t1" });
await store.put("tr_sweep~b", "B-bytes", { sessionId: "sess-X" });
await store.put("tr_sweep~other", "OTHER-bytes", { sessionId: "sess-Y" });
await store.put("tr_sweep~unowned", "UNOWNED-bytes");
const first = await del("sess-X");
assert.equal(first.deleted, 2, "both rows recorded for the session are deleted (taskId presence does not split the key)");
assert.equal(await store.get("tr_sweep~a"), undefined, "a swept row is gone");
assert.equal(await store.get("tr_sweep~b"), undefined);
assert.equal((await store.get("tr_sweep~other")).content, "OTHER-bytes", "another session's row survives the sweep");
assert.equal(first.unattributable >= 1, true, "an unowned row is counted unattributable");
assert.equal((await store.get("tr_sweep~unowned")).content, "UNOWNED-bytes", "an unowned row is never deleted by a session sweep");
const second = await del("sess-X");
assert.equal(second.deleted, 0, "a repeated sweep deletes nothing");
assert.equal((await store.get("tr_sweep~other")).content, "OTHER-bytes");
onOptionalMember?.({ member: "deleteBySession", status: "verified" });
});
run("listBySession (optional): the sweep's selection, read-only; absence is REPORTED, not silently green", async () => {
const probe = make();
if (probe.listBySession === undefined) {
onOptionalMember?.({ member: "listBySession", status: "absent" });
return;
}
assert.equal(typeof probe.listBySession, "function", `listBySession is present but not callable (got ${typeof probe.listBySession}) — a garbage member is a DEFECT, not an optional-member absence`);
const store = make();
const list = async (sessionId) => Promise.resolve(store.listBySession(sessionId));
const refsOf = (l) => l.entries.map((e) => e.ref).sort();
await store.put("tr_list~a", "A-bytes", { sessionId: "sess-L", taskId: "t1" });
await store.put("tr_list~b", "B-bytes", { sessionId: "sess-L" });
await store.put("tr_list~other", "OTHER-bytes", { sessionId: "sess-M" });
await store.put("tr_list~unowned", "UNOWNED-bytes");
const first = await list("sess-L");
assert.deepEqual(refsOf(first), ["tr_list~a", "tr_list~b"], "the session's rows, and only those, are listed");
const byRef = new Map(first.entries.map((e) => [e.ref, e.provenance]));
assert.deepEqual(byRef.get("tr_list~a"), { sessionId: "sess-L", taskId: "t1" }, "the recorded owner rides the entry, taskId included");
assert.deepEqual(byRef.get("tr_list~b"), { sessionId: "sess-L" }, "a session-only owner carries no taskId");
assert.equal(first.unaddressable, 0, "every row here was written by this store: none is unaddressable (the count is for rows written before the ref was recorded, never a second kind)");
assert.equal((await store.get("tr_list~a")).content, "A-bytes");
assert.deepEqual(await store.ownerOf("tr_list~a"), byRef.get("tr_list~a"), "the listed owner is the one ownerOf answers");
assert.equal(refsOf(first).includes("tr_list~other"), false, "another session's row is not listed");
assert.equal(refsOf(first).includes("tr_list~unowned"), false, "an unowned row is never listed");
assert.equal(first.unattributable >= 1, true, "an unowned row is counted unattributable");
const none = await list("sess-nobody");
assert.deepEqual(none.entries, [], "a session with no entries lists none");
assert.deepEqual(refsOf(await list("sess-L")), refsOf(first), "listing deletes and changes nothing");
assert.equal((await store.get("tr_list~b")).content, "B-bytes", "a listing leaves the bytes where they were");
if (store.deleteBySession !== undefined) {
const swept = await Promise.resolve(store.deleteBySession("sess-L"));
assert.equal(swept.deleted, first.entries.length + first.unaddressable, "the sweep deletes exactly the listed rows and the ones the listing counted but could not name");
assert.deepEqual((await list("sess-L")).entries, [], "a swept session lists nothing");
assert.equal((await store.get("tr_list~other")).content, "OTHER-bytes", "another session's row survives the sweep");
}
onOptionalMember?.({ member: "listBySession", status: "verified" });
});
run("reap (optional): no bound removes nothing, a bad bound is refused, age alone decides; absence is REPORTED, not silently green", async () => {
const probe = make();
if (probe.reap === undefined) {
onOptionalMember?.({ member: "reap", status: "absent" });
return;
}
assert.equal(typeof probe.reap, "function", `reap is present but not callable (got ${typeof probe.reap}) — a garbage member is a DEFECT, not an optional-member absence`);
const store = make();
const reap = async (now, opts) => Promise.resolve(Reflect.apply(store.reap, store, [now, opts]));
await store.put("tr_age~owned", "OWNED-bytes", { sessionId: "sess-A" });
await store.put("tr_age~unowned", "UNOWNED-bytes");
const later = Date.now() + 3_600_000;
assert.equal(await reap(later), 0, "no opts ⇒ nothing is aged out");
assert.equal(await reap(later, {}), 0, "no maxAgeMs ⇒ nothing is aged out");
for (const bad of [Number.NaN, -1, Number.POSITIVE_INFINITY, "60000"]) {
await assert.rejects(reap(later, { maxAgeMs: bad }), (e) => e.code === "config.retention_policy_invalid", `maxAgeMs ${String(bad)} is refused, coded`);
}
await assert.rejects(reap(Number.NaN, { maxAgeMs: 1 }), (e) => e.code === "config.retention_policy_invalid", "a now that is not a number is refused, coded");
assert.equal((await store.get("tr_age~owned")).content, "OWNED-bytes", "a refused sweep removed nothing");
assert.equal(await reap(later, { maxAgeMs: 2 * 3_600_000 }), 0, "an entry younger than the bound is kept");
assert.equal((await store.get("tr_age~unowned")).content, "UNOWNED-bytes");
assert.equal(await reap(later, { maxAgeMs: 60_000 }), 2, "every entry older than the bound is removed, owned or not");
assert.equal(await store.get("tr_age~owned"), undefined);
assert.equal(await store.get("tr_age~unowned"), undefined);
assert.equal(await store.ownerOf("tr_age~owned"), undefined, "the owner record goes with the content");
assert.equal(await reap(later, { maxAgeMs: 60_000 }), 0, "idempotent");
await store.put("tr_age~owned", "NEW-bytes", { sessionId: "sess-B" });
assert.deepEqual(await store.ownerOf("tr_age~owned"), { sessionId: "sess-B" }, "a reaped ref is free for a new owner");
onOptionalMember?.({ member: "reap", status: "verified" });
});
run("putBytes / bytesOf (optional): one election across kinds, one face per ref, and the session sweep required; absence is REPORTED", async () => {
const probe = make();
if (probe.putBytes === undefined && probe.bytesOf === undefined) {
onOptionalMember?.({ member: "putBytes", status: "absent" });
return;
}
assert.equal(typeof probe.putBytes, "function", `putBytes is ${typeof probe.putBytes} beside a bytesOf — the byte arm is a pair`);
assert.equal(typeof probe.bytesOf, "function", `bytesOf is ${typeof probe.bytesOf} beside a putBytes — the byte arm is a pair`);
assert.equal(typeof probe.deleteBySession, "function", "a store with a byte arm must implement deleteBySession — bytes a session produced leave with the session");
const store = make();
const bytesOf = async (ref) => Promise.resolve(store.bytesOf(ref));
const PNG = Uint8Array.from([0x89, 0x50, 0x4e, 0x47, 0, 1, 2, 3, 255]);
const ref = "tr_bytes~c1~b0";
await store.putBytes(ref, PNG, { sessionId: "sess-A" }, "image/png");
const kept = await bytesOf(ref);
assert.equal(kept?.byteLength, PNG.byteLength, "the entry's length is the bytes' length");
assert.equal(kept?.mediaType, "image/png", "the stated media type is stored with the entry");
assert.deepEqual([...(await Promise.resolve(kept.bytes()))], [...PNG], "the bytes read back exactly");
assert.equal(await store.get(ref), undefined, "the text face does not answer a byte entry (one face per ref)");
assert.equal(await bytesOf("tr_bytes~never"), undefined, "an unknown ref holds no bytes");
assert.deepEqual(await store.ownerOf(ref), { sessionId: "sess-A" }, "the byte entry's owner is recorded by the same election");
await store.putBytes(ref, Uint8Array.from([9]), { sessionId: "sess-A" }, "image/png");
assert.equal((await bytesOf(ref))?.byteLength, PNG.byteLength, "a repeat write keeps what is stored");
await assert.rejects((async () => store.putBytes(ref, PNG, { sessionId: "sess-B" }))(), (e) => e.code === "tool_result.ref_conflict", "a different owner is refused, coded");
await assert.rejects((async () => store.put(ref, "text over bytes", { sessionId: "sess-A" }))(), (e) => e.code === "tool_result.ref_conflict", "text on a ref holding bytes is refused, coded");
await store.put("tr_bytes~c2", "text first", { sessionId: "sess-A" });
await assert.rejects((async () => store.putBytes("tr_bytes~c2", PNG, { sessionId: "sess-A" }))(), (e) => e.code === "tool_result.ref_conflict", "bytes on a ref holding text are refused, coded");
assert.equal(await bytesOf("tr_bytes~c2"), undefined, "the refused write left no bytes");
assert.equal((await store.get("tr_bytes~c2"))?.content, "text first", "the refused write left the text as it was");
assert.deepEqual([...(await Promise.resolve((await bytesOf(ref)).bytes()))], [...PNG], "the refused write left the bytes as they were");
await store.putBytes("tr_bytes~other", PNG, { sessionId: "sess-B" });
if (store.listBySession !== undefined) {
const listed = (await Promise.resolve(store.listBySession("sess-A"))).entries.map((e) => e.ref).sort();
assert.deepEqual(listed, [ref, "tr_bytes~c2"].sort(), "a session's byte entries are listed with its text entries");
}
const swept = await Promise.resolve(store.deleteBySession("sess-A"));
assert.equal(swept.deleted, 2, "the session sweep removes its byte entry with its text entry");
assert.equal(await bytesOf(ref), undefined, "a swept byte entry is gone");
assert.equal((await bytesOf("tr_bytes~other"))?.byteLength, PNG.byteLength, "another session's byte entry survives the sweep");
onOptionalMember?.({ member: "putBytes", status: "verified" });
});
run("putBytes refusals (optional): the coded bound refusal reads over_limit with the bound; any other throw reads store_failed", async () => {
const probe = make();
if (probe.putBytes === undefined)
return;
const refusing = (thrown) => {
const s = make();
return Object.assign(Object.create(Object.getPrototypeOf(s)), s, {
putBytes: () => {
throw thrown;
},
});
};
const piece = Uint8Array.from([1, 2, 3, 4, 5, 6, 7, 8]);
const refusal = new ToolResultBytesOverLimitError("r", piece.byteLength, 6);
const bound = await keepToolResultBytes({ store: refusing(refusal), sessionId: "sess-A" }, "c1", piece, "application/zip");
assert.deepEqual(bound, { bytes: 8, mimeType: "application/zip", notKept: "over_limit", limit: 6, saveError: refusal.message }, "the coded refusal is over_limit with the store's bound and its words");
const other = await keepToolResultBytes({ store: refusing(new Error("disk full")), sessionId: "sess-A" }, "c1", piece, "application/zip");
assert.deepEqual(other, { bytes: 8, mimeType: "application/zip", notKept: "store_failed", saveError: "disk full" }, "any other throw is store_failed with the store's text");
});
await settle();
}