The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(mock-api): add labels filtering, event detail, recall ranking, and answer grounding
Steven Enamakelbc50392b90b0
diff --git a/scripts/mock-api/routes/__tests__/memory.test.mjs b/scripts/mock-api/routes/__tests__/memory.test.mjsindex 6d8bf33218..862eb55506 100644--- a/scripts/mock-api/routes/__tests__/memory.test.mjs+++ b/scripts/mock-api/routes/__tests__/memory.test.mjs@@ -149,3 +149,94 @@ test("forget rejects unsupported selectors and scopes ID deletion", async () =>const otherScope = await call("POST", "/memory/recall", { scope: "scope-b" });assert.equal(otherScope.json.data.layers.events.length, 1);});++const writeLabelled = (scope, key, text, labels, role = "user") =>+ call("POST", "/memory/experience", {+ scope,+ idempotency_key: key,+ modality: "observation",+ content: { kind: "message", role, text },+ context: { labels },+ });++test("events keep context labels and the labels filter keeps any match", async () => {+ await writeLabelled("s", "k1", "alpha", ["item:a"]);+ await writeLabelled("s", "k2", "beta", ["item:b"]);+ await writeLabelled("s", "k3", "gamma", ["item:c", "item:a"]);+ const all = await call("GET", "/memory/events?scope=s&limit=50");+ assert.equal(all.json.data.items.length, 6, "every event is emitted twice");+ const some = await call("GET", "/memory/events?scope=s&labels=item%3Aa%2Citem%3Ab");+ const ids = new Set(some.json.data.items.map((e) => e.id));+ assert.deepEqual([...ids].sort(), ["evt_1", "evt_2", "evt_3"]);+ const onlyB = await call("GET", "/memory/events?scope=s&labels=item%3Ab");+ assert.deepEqual([...new Set(onlyB.json.data.items.map((e) => e.id))], ["evt_2"]);+ assert.deepEqual(onlyB.json.data.items[0].context.labels, ["item:b"]);+});++test("events/:id returns one event and 404s an unknown id", async () => {+ await writeLabelled("s", "k1", "alpha", ["item:a"]);+ const hit = await call("GET", "/memory/events/evt_1");+ assert.equal(hit.status, 200);+ assert.equal(hit.json.data.content.text, "alpha");+ const miss = await call("GET", "/memory/events/evt_99");+ assert.equal(miss.status, 404);+ assert.equal(miss.json.errorCode, "NOT_FOUND");+});++test("recall ranks by query words, honours labels, budgets and descend", async () => {+ await writeLabelled("tm:memory/tm:learnings", "k1", "Alice prefers dark roast coffee", ["l:1"], "user");+ await writeLabelled("tm:memory/tm:learnings", "k2", "Bob likes green tea", ["l:2"], "assistant");+ await writeLabelled("tm:memory/tm:documents", "k3", "coffee machine manual", ["d:1"]);+ const exact = await call("POST", "/memory/recall", {+ scope: "tm:memory/tm:learnings",+ query: "what coffee does Alice prefer?",+ });+ assert.equal(exact.json.data.layers.events.length, 1);+ assert.equal(exact.json.data.layers.events[0].content.text, "[user] Alice prefers dark roast coffee");+ const descend = await call("POST", "/memory/recall", {+ scope: "tm:memory",+ view: "descend",+ query: "coffee",+ });+ assert.equal(descend.json.data.layers.events.length, 2);+ const flat = await call("POST", "/memory/recall", { scope: "tm:memory", query: "coffee" });+ assert.equal(flat.json.data.layers.events.length, 0);+ const filtered = await call("POST", "/memory/recall", {+ scope: "tm:memory",+ view: "descend",+ query: "",+ filters: { metadata: { labels: ["l:2"] } },+ });+ assert.equal(filtered.json.data.layers.events[0].content.text, "[assistant] Bob likes green tea");+ const budget = await call("POST", "/memory/recall", {+ scope: "tm:memory",+ view: "descend",+ query: "",+ budgets: { per_layer_limits: { events: 1 } },+ });+ assert.equal(budget.json.data.layers.events.length, 1);+});++test("answer is grounded in the pack it is given and refuses an unknown pack", async () => {+ await writeLabelled("s", "k1", "The deploy key rotates monthly", ["l:1"]);+ const pack = await call("POST", "/memory/recall", { scope: "s", query: "deploy key" });+ const packId = pack.json.data.pack_id;+ const answered = await call("POST", "/memory/answer", {+ scope: "s",+ question: "when does the deploy key rotate?",+ use_pack_id: packId,+ });+ assert.equal(answered.status, 200);+ assert.match(answered.json.data.answer, /rotates monthly/);+ assert.equal(answered.json.data.citations[0].id, "evt_1");+ const bad = await call("POST", "/memory/answer", { scope: "s", question: "q", use_pack_id: "pack_nope" });+ assert.equal(bad.status, 400);+ assert.equal(bad.json.errorCode, "MISSING_PACK");+});++test("scopes honours the prefix filter", async () => {+ await write("tm:memory/tm:learnings", "k1", "x");+ await write("other:scope", "k2", "y");+ const r = await call("GET", "/memory/scopes?prefix=tm%3Amemory");+ assert.deepEqual(r.json.data.items.map((i) => i.path), ["tm:memory/tm:learnings"]);+});diff --git a/scripts/mock-api/routes/memory.mjs b/scripts/mock-api/routes/memory.mjsindex f12c559652..fb40cf09dd 100644--- a/scripts/mock-api/routes/memory.mjs+++ b/scripts/mock-api/routes/memory.mjs@@ -19,7 +19,18 @@ import { behavior } from "../state.mjs";* - a reused body `idempotency_key` with different text is a 409* `IDEMPOTENCY_CONFLICT`, and forgetting does not release the key;* - `GET /memory/scopes` honours `limit` and caps at 50 without it;- * - `/memory/events` lists newest first with every record emitted twice.+ * - `/memory/events` lists newest first with every record emitted twice,+ * honours `scope`, `limit`, an offset `cursor` and a comma-separated+ * `labels` filter (events carrying ANY one of the labels);+ * - `GET /memory/events/:id` returns one event (404 NOT_FOUND otherwise);+ * - `/memory/recall` ranks a scope's events by how many query words (3++ * 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/answer` answers deterministically from the pack it is given:+ * "grounded answer for <question>" followed by the pack's top event text.+ *+ * Everything is in memory and deterministic; `resetMockMemory()` clears it.*/const DEFAULT_SCOPE_PAGE = 50;@@ -38,7 +49,7 @@ const ANSWER_KEYS = new Set(["use_pack_id",]);-/** token -> { events, idempotency, claims, nextOffset, nextId } */+/** token -> { events, idempotency, claims, packs, nextOffset, nextId, nextPack } */const stores = new Map();export function resetMockMemory() {@@ -52,8 +63,10 @@ function storeFor(token) {events: [],idempotency: new Map(),claims: new Set(),+ packs: new Map(),nextOffset: 0,nextId: 0,+ nextPack: 0,};stores.set(token, store);}@@ -95,11 +108,39 @@ function textOf(event) {return event?.content?.text;}+/** Whether `event` carries any one of `wanted` (an empty list keeps all). */+function labelled(event, wanted) {+ if (!wanted.length) return true;+ const labels = Array.isArray(event?.context?.labels) ? event.context.labels : [];+ return labels.some((label) => wanted.includes(label));+}++/** Lower-cased query words of 3+ alphanumeric characters. */+function wordsOf(query) {+ return String(query || "")+ .split(/\s+/)+ .map((w) => w.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, "").toLowerCase())+ .filter((w) => w.length >= 3);+}++/** Render an event's text for a reader, prefixing the speaker. */+function rendered(event) {+ const text = textOf(event);+ if (typeof text !== "string") return event;+ const role = String(event?.content?.role || "user");+ return { ...event, content: { ...event.content, text: `[${role}] ${text}` } };+}+export async function handleMemory(ctx) {const { method, url, res, req, parsedBody } = ctx;const path = url.split("?")[0];if (!path.startsWith("/memory/")) return false;- const route = path.slice("/memory/".length).replace(/\/+$/, "");+ let route = path.slice("/memory/".length).replace(/\/+$/, "");+ let eventId = null;+ if (route.startsWith("events/")) {+ eventId = decodeURIComponent(route.slice("events/".length));+ route = "events";+ }const known = new Set(["experience","events",@@ -160,26 +201,41 @@ export async function handleMemory(ctx) {store.nextId += 1;const id = `evt_${store.nextId}`;store.idempotency.set(key, { text, scope, modality, content, id });+ const given = body.context && typeof body.context === "object" ? body.context : {};store.events.push({id,scope,modality,wal_offset: store.nextOffset,content,- context: { recorded_at: new Date().toISOString() },+ context: { ...given, recorded_at: new Date().toISOString() },});ok(res, { event_id: id, status: "captured", replayed_from_idempotency: false });return true;}+ if (route === "events" && method === "GET" && eventId !== null) {+ const found = store.events.find((e) => e.id === eventId);+ if (!found) {+ fail(res, 404, "NOT_FOUND", "event not found");+ return true;+ }+ ok(res, found);+ return true;+ }+if (route === "events" && method === "GET") {const params = queryOf(url);const scope = params.get("scope") || "";const cursor = Number(params.get("cursor") || 0) || 0;const limit = Number(params.get("limit") || 50) || 50;+ const wanted = (params.get("labels") || "")+ .split(",")+ .map((l) => l.trim())+ .filter(Boolean);const stream = [];for (const event of [...store.events].reverse()) {- if (event.scope !== scope) continue;+ if (event.scope !== scope || !labelled(event, wanted)) continue;stream.push(event, event);}const items = stream.slice(cursor, cursor + limit);@@ -194,17 +250,31 @@ export async function handleMemory(ctx) {if (route === "recall" && method === "POST") {const scope = String(body.scope || "");- const query = String(body.query || "").toLowerCase();- const hits = store.events- .filter((e) => e.scope === scope)- .filter((e) => !query || String(textOf(e) ?? "").toLowerCase().includes(query))- .map((e) => {- const text = textOf(e);- return typeof text === "string"- ? { ...e, content: { ...e.content, text: `[user] ${text}` } }- : e;- });- ok(res, { pack_id: "pack_test", layers: { events: hits } });+ const descend = body.view === "descend";+ const wanted = Array.isArray(body.filters?.metadata?.labels)+ ? body.filters.metadata.labels.map(String)+ : [];+ const words = wordsOf(body.query);+ const budgetRaw = body.budgets?.per_layer_limits?.events;+ const budget = Number.isFinite(Number(budgetRaw)) && budgetRaw !== undefined+ ? Number(budgetRaw)+ : Infinity;+ const inScope = (e) => e.scope === scope || (descend && e.scope.startsWith(`${scope}/`));+ const scored = [];+ // Newest first, so equal scores rank the most recent event highest.+ for (const e of [...store.events].reverse()) {+ if (!inScope(e) || !labelled(e, wanted)) continue;+ const text = String(textOf(e) ?? "").toLowerCase();+ const score = words.filter((w) => text.includes(w)).length;+ if (words.length && score === 0) continue;+ scored.push({ score, e });+ }+ scored.sort((a, b) => b.score - a.score);+ const events = scored.slice(0, budget).map(({ e }) => rendered(e));+ store.nextPack += 1;+ const packId = `pack_${store.nextPack}`;+ store.packs.set(packId, events);+ ok(res, { pack_id: packId, layers: { events } });return true;}@@ -245,7 +315,10 @@ export async function handleMemory(ctx) {if (route === "scopes" && method === "GET") {const limit = Number(queryOf(url).get("limit") || DEFAULT_SCOPE_PAGE) || DEFAULT_SCOPE_PAGE;- const paths = [...new Set(store.events.map((e) => e.scope))].sort();+ const prefix = queryOf(url).get("prefix") || "";+ const paths = [...new Set(store.events.map((e) => e.scope))]+ .filter((p) => !prefix || p === prefix || p.startsWith(prefix))+ .sort();ok(res, { items: paths.slice(0, limit).map((path) => ({ path })) });return true;}@@ -256,10 +329,28 @@ export async function handleMemory(ctx) {fail(res, 400, "VALIDATION_ERROR", `unknown key ${unknown}`);return true;}+ let events = [];+ if (body.use_pack_id !== undefined) {+ const pack = store.packs.get(String(body.use_pack_id));+ if (!pack) {+ fail(res, 400, "MISSING_PACK", "unknown use_pack_id");+ return true;+ }+ events = pack;+ }+ const top = events.find((e) => typeof textOf(e) === "string");+ const question = String(body.question || "");ok(res, {- answer: "grounded",- citations: [{ id: "evt_1", key: "k", content: "c", score: 0.5 }],- context_block: "c",+ answer: top+ ? `grounded answer for ${question}: ${textOf(top)}`+ : `grounded answer for ${question}`,+ citations: events.slice(0, 5).map((e, i) => ({+ id: e.id,+ key: e.id,+ content: String(textOf(e) ?? ""),+ score: Number((1 / (1 + i)).toFixed(4)),+ })),+ context_block: events.map((e) => String(textOf(e) ?? "")).join("\n"),diagnostics: { answer_model: "mock" },});return true;