Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(mock-api): add cascade and v1 erasure support to memory routes

Steven Enamakel10b91a334819
diff --git a/scripts/mock-api/routes/__tests__/memory.test.mjs b/scripts/mock-api/routes/__tests__/memory.test.mjs
index d6aabd5158..4f45903ed0 100644
--- a/scripts/mock-api/routes/__tests__/memory.test.mjs
+++ b/scripts/mock-api/routes/__tests__/memory.test.mjs
@@ -116,7 +116,9 @@ test("forget refuses an empty selector without confirm_all", async () => {
const r = await call("POST", "/memory/forget", { scope: "s", selector: {} });
assert.equal(r.status, 422);
await write("s", "k1", "x");
- const del = await call("POST", "/memory/forget", { scope: "s", selector: {}, confirm_all: true });
+ const del = await call("POST", "/memory/forget", {
+ scope: "s", selector: {}, confirm_all: true, cascade: "redact_events",
+ });
assert.equal(del.json.data.deleted.events, 1);
});
@@ -143,7 +145,9 @@ test("forget rejects unsupported selectors and scopes ID deletion", async () =>
assert.equal(unsupported.status, 400);
assert.equal(unsupported.json.errorCode, "UNSUPPORTED_SELECTOR");
const del = await call("POST", "/memory/forget", {
- scope: "scope-a", selector: { memory_ids: [first.json.data.event_id, "evt_2"] },
+ scope: "scope-a",
+ selector: { memory_ids: [first.json.data.event_id, "evt_2"] },
+ cascade: "redact_events",
});
assert.equal(del.json.data.deleted.events, 1);
const otherScope = await call("POST", "/memory/recall", { scope: "scope-b" });
@@ -240,3 +244,47 @@ test("scopes honours the prefix filter", async () => {
const r = await call("GET", "/memory/scopes?prefix=app%3Atinymemory");
assert.deepEqual(r.json.data.items.map((i) => i.path), ["app:tinymemory/app:learnings"]);
});
+
+test("forget without a cascade keeps the events, as CortexDB's derived_only default does", async () => {
+ const first = await write("s", "k1", "keep");
+ const kept = await call("POST", "/memory/forget", {
+ scope: "s", selector: { memory_ids: [first.json.data.event_id] },
+ });
+ assert.equal(kept.json.data.deleted.events, 0);
+ const bad = await call("POST", "/memory/forget", {
+ scope: "s", selector: { memory_ids: ["x"] }, cascade: "everything",
+ });
+ assert.equal(bad.status, 400);
+ assert.equal(bad.json.errorCode, "INVALID_CASCADE");
+ const gone = await call("POST", "/memory/forget", {
+ scope: "s", selector: { memory_ids: [first.json.data.event_id] }, cascade: "redact_events",
+ });
+ assert.equal(gone.json.data.deleted.events, 1);
+});
+
+test("v1 erasures erase a scope and everything below it, unwrapped", async () => {
+ await write("app:brain/source:gmail", "k1", "mail");
+ await write("app:brain/source:gmail/app:documents", "k2", "doc");
+ await write("app:brain/source:notion", "k3", "page");
+ const refused = await call("POST", "/memory/v1/erasures", {
+ scope: "app:brain/source:gmail", confirm_all: true,
+ });
+ assert.equal(refused.status, 400);
+ assert.equal(refused.json.error_code, "UNKNOWN_FIELD");
+ const root = await call("POST", "/memory/v1/erasures", { scope: "" });
+ assert.equal(root.status, 422);
+ const erased = await call("POST", "/memory/v1/erasures", {
+ scope: "app:brain/source:gmail", audit_note: "disconnect",
+ });
+ assert.equal(erased.status, 200);
+ assert.equal(erased.json.erased, true);
+ assert.equal(erased.json.scopes, 2);
+ const job = await call("GET", `/memory/v1/erasures/${erased.json.erasure_ids[0]}`);
+ assert.equal(job.json.status, "completed");
+ const missing = await call("GET", "/memory/v1/erasures/nope");
+ assert.equal(missing.status, 404);
+ const scopes = await call("GET", "/memory/scopes?limit=100");
+ assert.deepEqual(scopes.json.data.items.map((i) => i.path), ["app:brain/source:notion"]);
+ const empty = await call("POST", "/memory/v1/erasures", { scope: "app:brain/source:gmail" });
+ assert.equal(empty.json.scopes, 0, "nothing stored is still a success");
+});
diff --git a/scripts/mock-api/routes/memory.mjs b/scripts/mock-api/routes/memory.mjs
index caf4a4411b..dd84c2506f 100644
--- a/scripts/mock-api/routes/memory.mjs
+++ b/scripts/mock-api/routes/memory.mjs
@@ -27,6 +27,14 @@ import { behavior } from "../state.mjs";
* letters) they hold, honours `view: "descend"`, the metadata `labels`
* filter and `budgets.per_layer_limits.events`, and mints a `pack_<n>` id
* that `/memory/answer` must be given as `use_pack_id`;
+ * - `/memory/forget` honours `cascade`: the default, `derived_only`, keeps
+ * the events (it only drops what was derived from them), `redact_events`
+ * removes them, anything else is a 400 `INVALID_CASCADE`;
+ * - `POST /memory/v1/erasures` is memory-api's scoped erasure, answered
+ * unwrapped: `{scope, audit_note}` only (else 400 `UNKNOWN_FIELD`), the
+ * root is a 422 `ROOT_ERASURE_REFUSED`, and it erases the scope and every
+ * scope below it synchronously: `{erased, scope, scopes, erasure_ids}`;
+ * `GET /memory/v1/erasures/:id` reads one back (404 otherwise);
* - `/memory/answer` answers deterministically from the pack it is given:
* "grounded answer for <question>" followed by the pack's top event text;
* - `DELETE /memory` erases the caller's whole store and answers
@@ -167,6 +175,11 @@ export async function handleMemory(ctx) {
if (!path.startsWith("/memory/")) return false;
let route = path.slice("/memory/".length).replace(/\/+$/, "");
let eventId = null;
+ let erasureId = null;
+ if (route.startsWith("v1/erasures/")) {
+ erasureId = decodeURIComponent(route.slice("v1/erasures/".length));
+ route = "v1/erasures";
+ }
if (route.startsWith("events/")) {
eventId = decodeURIComponent(route.slice("events/".length));
route = "events";
@@ -178,6 +191,7 @@ export async function handleMemory(ctx) {
"forget",
"scopes",
"answer",
+ "v1/erasures",
]);
if (!known.has(route)) return false;
@@ -329,6 +343,16 @@ export async function handleMemory(ctx) {
fail(res, 422, "EMPTY_SELECTOR_WITHOUT_CONFIRMATION");
return true;
}
+ const cascade = body.cascade === undefined ? "derived_only" : body.cascade;
+ if (cascade !== "derived_only" && cascade !== "redact_events") {
+ fail(res, 400, "INVALID_CASCADE");
+ return true;
+ }
+ if (cascade === "derived_only") {
+ // Only what was derived goes; the events stay.
+ ok(res, { deleted: { events: 0 }, requested: ids.length, matched: 0 });
+ return true;
+ }
const before = store.events.length;
const requestedIds = new Set(ids);
store.events = selective
@@ -343,6 +367,46 @@ export async function handleMemory(ctx) {
return true;
}
+ if (route === "v1/erasures" && method === "GET" && erasureId !== null) {
+ const job = store.erasures?.get(erasureId);
+ if (!job) {
+ json(res, 404, { error_code: "NOT_FOUND" });
+ return true;
+ }
+ json(res, 200, job);
+ return true;
+ }
+
+ if (route === "v1/erasures" && method === "POST" && erasureId === null) {
+ const unknown = Object.keys(body).find((k) => k !== "scope" && k !== "audit_note");
+ if (unknown) {
+ json(res, 400, { error_code: "UNKNOWN_FIELD", message: unknown });
+ return true;
+ }
+ const scope = String(body.scope ?? "").replace(/\/+$/, "");
+ if (!scope || scope === "/") {
+ json(res, 422, { error_code: "ROOT_ERASURE_REFUSED" });
+ return true;
+ }
+ const below = `${scope}/`;
+ const held = [
+ ...new Set(
+ store.events
+ .map((e) => e.scope)
+ .filter((s) => s === scope || s.startsWith(below)),
+ ),
+ ];
+ store.events = store.events.filter((e) => !held.includes(e.scope));
+ store.erasures ??= new Map();
+ const ids = held.map((s) => {
+ const id = `erasure_${store.erasures.size + 1}`;
+ store.erasures.set(id, { erasure_id: id, scope: s, status: "completed", phase: "done" });
+ return id;
+ });
+ json(res, 200, { erased: true, scope, scopes: ids.length, erasure_ids: ids });
+ return true;
+ }
+
if (route === "scopes" && method === "GET") {
const limit = Number(queryOf(url).get("limit") || DEFAULT_SCOPE_PAGE) || DEFAULT_SCOPE_PAGE;
const prefix = queryOf(url).get("prefix") || "";