@sema-agent/core
Version:
Stateless, task-oriented AI agent core
97 lines (96 loc) • 4.53 kB
JavaScript
import { foreignLine } from "../protocol-http-auth.js";
import { thrownText } from "../thrown-value.js";
import { inlineUntrusted } from "../untrusted-text.js";
function deleteEventScope(ev) {
if (typeof ev["scope"] === "string" && ev["scope"].length > 0)
return ev["scope"];
const from = ev["from"];
if (from !== null && typeof from === "object") {
const fromScope = from.scope;
if (typeof fromScope === "string" && fromScope.length > 0)
return fromScope;
}
return undefined;
}
export function deletionRecordFrom(evidence) {
if (evidence.deletedRev !== undefined)
return evidence.deletedRev === null ? { kind: "deleted" } : { kind: "deleted", rev: evidence.deletedRev };
if (!evidence.sound)
return { kind: "undecidable", why: evidence.unsoundWhy ?? "the store's deletion record is damaged" };
return { kind: "never" };
}
export function deletionRecordOf(id, custody, scope) {
if (typeof scope !== "string" || scope.length === 0)
throw new TypeError("deletionRecordOf: scope must be a non-empty string");
if (custody === undefined)
return { kind: "undecidable", why: "the store keeps no deletion record (it has no custodyOf face)" };
const seenEvs = new Set();
let deletedRev;
let scopelessDelete = false;
for (const ev of custody.events) {
if (typeof ev.ev === "string" && ev.ev.length > 0) {
if (seenEvs.has(ev.ev)) {
return { kind: "undecidable", why: `the store's deletion record carries event id "${foreignLine(ev.ev, 80)}" twice with conflicting payloads` };
}
seenEvs.add(ev.ev);
}
if (ev.channel !== "delete" || ev["id"] !== id)
continue;
const evScope = deleteEventScope(ev);
if (evScope === undefined) {
scopelessDelete = true;
continue;
}
if (evScope !== scope)
continue;
deletedRev = typeof ev["rev"] === "string" && ev["rev"].length > 0 ? ev["rev"] : null;
}
if (scopelessDelete)
return { kind: "undecidable", why: "a deletion event for this id carries no scope — whether it deleted this scope's entry cannot be read" };
return deletionRecordFrom({ deletedRev, sound: custody.state === "complete", unsoundWhy: `the store's deletion record for this id is damaged${custody.reason !== undefined ? ` (${foreignLine(custody.reason, 200)})` : ""}` });
}
export async function unheldIdRecord(backend, id, scope, placement) {
if (placement.state === "held")
return { kind: "held" };
if (placement.state === "unknown")
return { kind: "undecidable", why: "where the store holds this id could not be read" };
return (await memoryDeletionRecords(backend, [id], scope)).get(id);
}
export function unheldIdRefusal(record) {
return record.kind === "deleted"
? "the memory store deleted the entry this file's id names — a deleted entry's bytes are never taken back; to keep the text, write it as a new note without the id line"
: `memory write deferred: this file carries an id the memory store does not hold, and whether the store deleted that entry cannot be read (${inlineUntrusted(record.why, 200)}) — not committed, so a deleted entry is never taken back; remove the id line to commit it as a new note`;
}
export async function memoryDeletionRecords(backend, ids, scope) {
if (typeof scope !== "string" || scope.length === 0)
throw new TypeError("memoryDeletionRecords: scope must be a non-empty string");
let face;
let faceErr;
try {
const raw = backend.custodyOf;
face = typeof raw === "function" ? raw : undefined;
}
catch (err) {
faceErr = foreignLine(thrownText(err), 200);
}
const out = new Map();
for (const id of ids) {
if (out.has(id))
continue;
if (faceErr !== undefined) {
out.set(id, { kind: "undecidable", why: `the store's deletion record could not be read (${faceErr})` });
continue;
}
if (face === undefined) {
out.set(id, deletionRecordOf(id, undefined, scope));
continue;
}
try {
out.set(id, deletionRecordOf(id, await face.call(backend, id), scope));
}
catch (err) {
out.set(id, { kind: "undecidable", why: `the store's deletion record could not be read (${foreignLine(thrownText(err), 200)})` });
}
}
return out;
}