Files
OmniRoute/tests/unit/memory-tools.test.ts
Diego Rodrigues de Sa e Souza bf5b615969 feat(memory): x-omniroute-no-memory opt-out + memory off-by-default + token-cost alert (PRD-2026-06-19) (#4290)
* feat(memory): x-omniroute-no-memory opt-out + memory off-by-default + token-cost UI alert

PRD-2026-06-19-no-memory-header. The gateway injects up to memorySettings.maxTokens
(~2k) of memory (and skills) context into every chat call for memory-enabled keys,
inflating tokens+cost ~137x for clients that manage their own context (e.g. Omniflow).

Three changes:
- A) x-omniroute-no-memory request header (mirrors x-omniroute-no-cache): when truthy
  (true/1/yes), skip memory+skills injection for that request. New pure helper
  isNoMemoryRequested() in chatCore/headers.ts; chatCore passes memoryOwnerId=null on
  opt-out (a null owner disables both injection branches).
- B) Memory OFF by default: DEFAULT_MEMORY_SETTINGS.enabled true->false. Enabling injects
  billed context per request, so it's now an explicit opt-in. Installs that already
  enabled it keep it; unset installs default off (no migration seeds memoryEnabled).
- C) Settings -> Memory shows a token-cost warning callout when memory is enabled
  (new settings.memoryTokenCostWarning i18n key, interpolating the configured maxTokens).

Tests: no-memory-header.test.ts (5, helper truthiness/case/Headers); memory-settings-default
and chatcore-memory-skills-injection aligned to the new off-by-default. 65/65 memory+chatcore
tests green; typecheck/lint/file-size/i18n(@65) clean.

* test(memory): enable memory in memory-tools test (memory now off by default)

The full CI unit suite flagged memory-tools.test.ts 'memory search ...' failing
after DEFAULT_MEMORY_SETTINGS.enabled flipped to false: omniroute_memory_search
routes through retrieveMemories, which returns [] while memory is disabled
(enabled:false → maxTokens 0). The memory MCP tools operate within the memory
subsystem, so the test now enables memory explicitly (updateSettings + cache
invalidation) — the realistic precondition for a client using the tools.
Aligns the test to the intentional off-by-default change; assertions unchanged.
2026-06-19 16:14:18 -03:00

146 lines
4.8 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-memory-tools-"));
const originalDataDir = process.env.DATA_DIR;
process.env.DATA_DIR = tmpDir;
const core = await import("../../src/lib/db/core.ts");
const { memoryTools } = await import("../../open-sse/mcp-server/tools/memoryTools.ts");
const memoryStore = await import("../../src/lib/memory/store.ts");
const settingsDb = await import("../../src/lib/db/settings.ts");
const { invalidateMemorySettingsCache } = await import("../../src/lib/memory/settings.ts");
function resetStorage() {
core.resetDbInstance();
fs.rmSync(tmpDir, { recursive: true, force: true });
fs.mkdirSync(tmpDir, { recursive: true });
core.getDbInstance();
}
test.beforeEach(async () => {
resetStorage();
// PRD-2026-06-19: memory is OFF by default now. The memory MCP tools operate
// within the memory subsystem (omniroute_memory_search → retrieveMemories, which
// returns nothing while memory is disabled), so enable memory explicitly — the
// realistic precondition for a client using the memory tools.
await settingsDb.updateSettings({ memoryEnabled: true });
invalidateMemorySettingsCache();
});
test.after(() => {
core.resetDbInstance();
process.env.DATA_DIR = originalDataDir;
fs.rmSync(tmpDir, { recursive: true, force: true });
});
test("memory add stores entries with default session and metadata", async () => {
const result = await memoryTools.omniroute_memory_add.handler({
apiKeyId: "key-add",
type: "factual",
key: "pref:language",
content: "TypeScript is preferred.",
});
const rowsResult = await memoryStore.listMemories({ apiKeyId: "key-add" });
const rows = Array.isArray(rowsResult) ? rowsResult : rowsResult.data;
assert.equal(result.success, true);
assert.equal(result.data.message, "Memory created successfully");
assert.equal(rows.length, 1);
assert.equal(rows[0].sessionId, "");
assert.deepEqual(rows[0].metadata, {});
assert.equal(rows[0].content, "TypeScript is preferred.");
});
test("memory search filters by type, enforces limit, and reports token totals", async () => {
await memoryTools.omniroute_memory_add.handler({
apiKeyId: "key-search",
sessionId: "search",
type: "factual",
key: "pref:stack",
content: "TypeScript and Node.js are used for backend work.",
metadata: { source: "user" },
});
await memoryTools.omniroute_memory_add.handler({
apiKeyId: "key-search",
sessionId: "search",
type: "semantic",
key: "pref:hobby",
content: "Gardening is a weekend hobby.",
metadata: { source: "user" },
});
await memoryTools.omniroute_memory_add.handler({
apiKeyId: "key-search",
sessionId: "search",
type: "factual",
key: "pref:language",
content: "TypeScript services are written every day.",
metadata: { source: "user" },
});
const result = await memoryTools.omniroute_memory_search.handler({
apiKeyId: "key-search",
query: "typescript backend",
type: "factual",
limit: 1,
});
assert.equal(result.success, true);
assert.equal(result.data.count, 1);
assert.equal(result.data.memories.length, 1);
assert.equal(result.data.memories[0].type, "factual");
assert.match(result.data.memories[0].content, /TypeScript/i);
assert.ok(result.data.totalTokens > 0);
});
test("memory clear deletes only older filtered entries and reports the deleted count", async () => {
const older = await memoryStore.createMemory({
apiKeyId: "key-clear",
sessionId: "clear",
type: "factual",
key: "old",
content: "This memory should be removed.",
metadata: {},
expiresAt: null,
});
const newer = await memoryStore.createMemory({
apiKeyId: "key-clear",
sessionId: "clear",
type: "factual",
key: "new",
content: "This memory should remain.",
metadata: {},
expiresAt: null,
});
const db = core.getDbInstance();
const cutoff = new Date("2025-01-01T00:00:00.000Z");
db.prepare("UPDATE memories SET created_at = ? WHERE id = ?").run(
"2024-01-01T00:00:00.000Z",
older.id
);
db.prepare("UPDATE memories SET created_at = ? WHERE id = ?").run(
"2025-06-01T00:00:00.000Z",
newer.id
);
const result = await memoryTools.omniroute_memory_clear.handler({
apiKeyId: "key-clear",
type: "factual",
olderThan: cutoff.toISOString(),
});
const remainingResult = await memoryStore.listMemories({ apiKeyId: "key-clear" });
const remaining = Array.isArray(remainingResult) ? remainingResult : remainingResult.data;
assert.equal(result.success, true);
assert.equal(result.data.deletedCount, 1);
assert.equal(result.data.message, "Cleared 1 memories");
assert.equal(remaining.length, 1);
assert.equal(remaining[0].id, newer.id);
});