Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

4.5 KB
Raw Blame
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";

const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..", "..");

// The shared request builder only sends `session_id` — and with it `dedup_turns`
// and server-side query expansion — when the caller supplies one. Every harness
// that already owns an OV session id has to forward it at its recall call site,
// which no harness unit test covers end to end: they each stub the transport
// below the point where the id would have been added. These assertions pin the
// wiring so a harness cannot silently fall back to stateless recall again.
const CALL_SITES = [
  {
    name: "the thin-harness hook forwards its derived session id",
    file: join(ROOT, "examples", "agent-hook-plugin", "scripts", "hook.mjs"),
    pattern: /recallForPrompt\([^)]*\{[^}]*\bsessionId\b/s,
  },
  {
    name: "OpenCode recall forwards the mapped OV session id",
    file: join(ROOT, "examples", "opencode-plugin", "lib", "memory-recall.mjs"),
    pattern: /sessionId:\s*sessionID\s*\?\s*sessionManager\.getMappedSessionId\(sessionID\)/,
  },
  {
    name: "pi recall forwards the sync manager's session id",
    file: join(ROOT, "examples", "pi-coding-agent-extension", "recall.ts"),
    pattern: /sessionId:\s*this\.sessionId\(\)/,
  },
  // Under `actor` scope the effective peer is the only one asked, so dropping
  // the pre-git id here makes every memory written before it unreachable.
  {
    name: "pi recall forwards the pre-git peer for the actor-scope dual read",
    file: join(ROOT, "examples", "pi-coding-agent-extension", "recall.ts"),
    pattern: /legacyPeerId:\s*this\.config\.legacyPeerId/,
  },
];

for (const { name, file, pattern } of CALL_SITES) {
  test(name, async () => {
    const source = await readFile(file, "utf-8");
    assert.match(source, pattern);
  });
}

test("pi wires the sync manager into the recall manager", async () => {
  const source = await readFile(join(ROOT, "examples", "pi-coding-agent-extension", "index.ts"), "utf-8");
  // Only the session-id getter is asserted; later arguments (the recall
  // ledger) come and go without changing what this test is about.
  assert.match(source, /new RecallManager\(\s*client,\s*config,\s*\(\)\s*=>\s*sync\.sessionId,/);
  // The getter exists because SyncManager has to be constructed first.
  assert.ok(
    source.indexOf("new SyncManager(") < source.indexOf("new RecallManager("),
    "SyncManager must be constructed before RecallManager reads its session id",
  );
});

test("OpenCode passes the session manager into the recall component", async () => {
  const source = await readFile(join(ROOT, "examples", "opencode-plugin", "lib", "plugin-runtime.mjs"), "utf-8");
  assert.match(source, /createMemoryRecall\(\{\s*config,\s*sessionManager\s*\}\)/);
});

// Forwarding a session id makes the server spend the query-expansion fuse, so
// an adapter that pins its own HTTP timeout aborts a request the server is
// still inside and drops back to the path without dedup or expansion. Each
// adapter's own budget has to stay a default, not a ceiling.
const TIMEOUT_PASSTHROUGH = [
  {
    name: "OpenCode keeps the per-request deadline the helper hands down",
    file: join(ROOT, "examples", "opencode-plugin", "lib", "memory-recall.mjs"),
    pattern: /timeoutMs:\s*options\.timeoutMs\s*\?\?\s*\d+/,
  },
];

for (const { name, file, pattern } of TIMEOUT_PASSTHROUGH) {
  test(name, async () => {
    const source = await readFile(file, "utf-8");
    assert.match(source, pattern);
  });
}

// The env var alone is not enough: buildContextSearchBody only emits
// `query_expansion` when the harness also marks it as configured. Both halves
// are declared once now — the knob in the schema, the flag in the one builder
// every loader calls — and `plugin-config.test.mjs` checks each harness gets it.
test("the query-expansion opt-out is declared once and reported for every harness", async () => {
  const { KNOB_BY_NAME } = await import("./lib/config-schema.mjs");
  const knob = KNOB_BY_NAME.get("recallQueryExpansion");
  assert.equal(knob.env, "OPENVIKING_RECALL_QUERY_EXPANSION");
  assert.deepEqual(knob.values, ["auto", "off"]);
  assert.equal(knob.sendOnlyWhenConfigured, true);

  const builder = join(ROOT, "examples", "memory-plugin-shared", "lib", "plugin-config.mjs");
  assert.match(await readFile(builder, "utf-8"), /`\$\{knob\.name\}Configured`/);
});