Skip to content

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

14.5 KB
Raw Blame
import assert from "node:assert/strict";
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";

import {
  buildCodexExecArgs,
  detectRecallCompressorProfile,
  invalidateRecallCompressorProfileCache,
  loadCachedRecallCompressorProfile,
  loadCodexModelsCache,
  markRecallCompressorRuntimeFailed,
  resolveRecallCompressorProfile,
  userProviderOverrides,
} from "./recall-compressor-profile.mjs";

function baseCfg(overrides = {}) {
  return {
    recallCompress: true,
    recallCompressModel: "",
    recallCompressThinking: "",
    recallCompressConfigured: false,
    recallCompressDetectOnStartup: true,
    recallCompressDetectTtlMs: 604_800_000,
    ...overrides,
  };
}

async function withTempState(action) {
  const stateDir = await mkdtemp(join(tmpdir(), "ov-compressor-state-"));
  const codexHome = await mkdtemp(join(tmpdir(), "ov-codex-home-"));
  const prevState = process.env.OPENVIKING_CODEX_STATE_DIR;
  process.env.OPENVIKING_CODEX_STATE_DIR = stateDir;
  try {
    await action({ stateDir, codexHome });
  } finally {
    if (prevState === undefined) delete process.env.OPENVIKING_CODEX_STATE_DIR;
    else process.env.OPENVIKING_CODEX_STATE_DIR = prevState;
    await rm(stateDir, { recursive: true, force: true });
    await rm(codexHome, { recursive: true, force: true });
  }
}

async function writeModelsCache(codexHome, slugs) {
  await writeFile(
    join(codexHome, "models_cache.json"),
    JSON.stringify({
      fetched_at: "2026-06-15T00:00:00Z",
      models: slugs.map((slug) => ({ slug })),
    }),
  );
}

test("buildCodexExecArgs injects a configured compressor base URL", () => {
  const args = buildCodexExecArgs(
    { model: "gpt-6-luna", thinking: "default" },
    "/tmp/last-message.txt",
    { recallCompressBaseUrl: "https://compressor.example/v1" },
  );

  assert.deepEqual(args.slice(0, 8), [
    "-m", "gpt-6-luna",
    "-c", 'model_provider="openviking_compressor"',
    "-c", 'model_providers.openviking_compressor.name="openviking_compressor"',
    "-c", 'model_providers.openviking_compressor.base_url="https://compressor.example/v1"',
  ]);
  assert.ok(args.indexOf("exec") > args.indexOf('model_providers.openviking_compressor.base_url="https://compressor.example/v1"'));
});

test("buildCodexExecArgs preserves the existing command without a compressor base URL", () => {
  const args = buildCodexExecArgs(
    { model: "gpt-6-luna", thinking: "default" },
    "/tmp/last-message.txt",
    {},
    { CODEX_HOME: "/nonexistent-codex-home" },
  );

  assert.equal(args.some((arg) => arg.includes("openviking_compressor")), false);
  assert.equal(args.some((arg) => arg.startsWith("model_provider")), false);
});

async function withCodexConfig(toml, action) {
  const codexHome = await mkdtemp(join(tmpdir(), "ov-codex-home-"));
  try {
    await writeFile(join(codexHome, "config.toml"), toml);
    await action({ CODEX_HOME: codexHome });
  } finally {
    await rm(codexHome, { recursive: true, force: true });
  }
}

test("buildCodexExecArgs forwards the user's active custom provider", async () => {
  await withCodexConfig([
    'model_provider = "custom"',
    'model = "gpt-6.1-sol"',
    "",
    '[projects."/tmp"]',
    'trust_level = "trusted"',
    "[model_providers.custom]",
    'base_url = "http://127.0.0.1:1936/v1"',
    'wire_api = "responses"',
    "# comment",
    'experimental_bearer_token = "secret"',
    'http_headers = { "X-Team" = "ov" }',
    "requires_openai_auth = false",
    "[model_providers.other]",
    'base_url = "http://other"',
  ].join("\n"), async (env) => {
    const args = buildCodexExecArgs({ model: "gpt-6-luna", thinking: "low" }, "/tmp/out.txt", {}, env);
    const overrides = args.filter((_, i) => args[i - 1] === "-c");
    assert.deepEqual(overrides.filter((v) => v.startsWith("model_provider")), [
      'model_provider="custom"',
      'model_providers.custom.base_url="http://127.0.0.1:1936/v1"',
      'model_providers.custom.wire_api="responses"',
      'model_providers.custom.experimental_bearer_token="secret"',
      'model_providers.custom.http_headers={ "X-Team" = "ov" }',
      "model_providers.custom.requires_openai_auth=false",
    ]);
    assert.ok(args.indexOf("exec") > args.indexOf("model_providers.custom.requires_openai_auth=false"));
  });
});

test("userProviderOverrides skips built-in providers and unsupported layouts", async () => {
  await withCodexConfig('model_provider = "openai"\n', (env) => {
    assert.deepEqual(userProviderOverrides(env), []);
  });
  await withCodexConfig('model_provider = "custom"\n[model_providers.custom]\nbase_url = "x"\n[model_providers.custom.http_headers]\nA = "b"\n', (env) => {
    assert.deepEqual(userProviderOverrides(env), []);
  });
  await withCodexConfig('model_provider = "custom"\n[model_providers.custom]\nbase_url = "x" # trailing\n', (env) => {
    assert.deepEqual(userProviderOverrides(env), []);
  });
  await withCodexConfig('model_provider = "custom"\n', (env) => {
    assert.deepEqual(userProviderOverrides(env), []);
  });
});

test("a configured compressor base URL takes precedence over the user's provider", async () => {
  await withCodexConfig('model_provider = "custom"\n[model_providers.custom]\nbase_url = "x"\n', (env) => {
    const args = buildCodexExecArgs(
      { model: "gpt-6-luna", thinking: "low" },
      "/tmp/out.txt",
      { recallCompressBaseUrl: "https://compressor.example/v1" },
      env,
    );
    assert.equal(args.includes('model_provider="custom"'), false);
    assert.ok(args.includes('model_provider="openviking_compressor"'));
  });
});

test("loadCodexModelsCache returns empty set when cache missing", async () => {
  await withTempState(async ({ codexHome }) => {
    const result = await loadCodexModelsCache({ CODEX_HOME: codexHome });
    assert.equal(result.present, false);
    assert.equal(result.slugs.size, 0);
  });
});

test("loadCodexModelsCache parses available slugs", async () => {
  await withTempState(async ({ codexHome }) => {
    await writeModelsCache(codexHome, ["gpt-6-luna", "gpt-5.6-luna", "codex-auto-review"]);
    const result = await loadCodexModelsCache({ CODEX_HOME: codexHome });
    assert.equal(result.present, true);
    assert.equal(result.slugs.size, 3);
    assert.equal(result.slugs.has("gpt-6-luna"), true);
    assert.equal(result.slugs.has("gpt-5.6-luna"), true);
  });
});

test("resolveRecallCompressorProfile picks first candidate present in cache", async () => {
  await withTempState(async ({ stateDir, codexHome }) => {
    await writeModelsCache(codexHome, ["gpt-5.6-luna", "codex-auto-review"]);
    const profile = await resolveRecallCompressorProfile(
      baseCfg(),
      {},
      { CODEX_HOME: codexHome },
    );
    assert.equal(profile.enabled, true);
    assert.equal(profile.model, "gpt-5.6-luna");
    assert.equal(profile.thinking, "low");
    assert.equal(profile.detected, true);
    const persisted = JSON.parse(
      await readFile(join(stateDir, "recall-compressor-profile.json"), "utf-8"),
    );
    assert.equal(persisted.profile.model, "gpt-5.6-luna");
  });
});

test("resolveRecallCompressorProfile honors configured candidate first when present", async () => {
  await withTempState(async ({ codexHome }) => {
    await writeModelsCache(codexHome, ["gpt-5.5", "gpt-6-luna"]);
    const profile = await resolveRecallCompressorProfile(
      baseCfg({
        recallCompressModel: "gpt-5.5",
        recallCompressThinking: "low",
        recallCompressConfigured: true,
      }),
      {},
      { CODEX_HOME: codexHome },
    );
    assert.equal(profile.enabled, true);
    assert.equal(profile.model, "gpt-5.5");
    assert.equal(profile.thinking, "low");
    assert.equal(profile.source, "configured");
    assert.equal(profile.detected, true);
  });
});

test("resolveRecallCompressorProfile falls back optimistically when cache missing", async () => {
  await withTempState(async ({ codexHome }) => {
    const profile = await resolveRecallCompressorProfile(
      baseCfg(),
      {},
      { CODEX_HOME: codexHome },
    );
    assert.equal(profile.enabled, true);
    assert.equal(profile.model, "gpt-6-luna");
    assert.equal(profile.detected, false);
  });
});

test("resolveRecallCompressorProfile disables when compress is configured off", async () => {
  await withTempState(async ({ codexHome }) => {
    await writeModelsCache(codexHome, ["gpt-5.6-luna"]);
    const profile = await resolveRecallCompressorProfile(
      baseCfg({ recallCompress: false }),
      {},
      { CODEX_HOME: codexHome },
    );
    assert.equal(profile.enabled, false);
    assert.equal(profile.source, "configured_off");
  });
});

test("invalidateRecallCompressorProfileCache removes the persisted profile", async () => {
  await withTempState(async ({ stateDir, codexHome }) => {
    await writeModelsCache(codexHome, ["gpt-6-luna"]);
    await resolveRecallCompressorProfile(baseCfg(), {}, { CODEX_HOME: codexHome });
    const path = join(stateDir, "recall-compressor-profile.json");
    await readFile(path, "utf-8"); // exists
    await invalidateRecallCompressorProfileCache();
    await assert.rejects(() => readFile(path, "utf-8"), /ENOENT/);
  });
});

test("detectRecallCompressorProfile prefers cached profile (no probe even when cache exists)", async () => {
  await withTempState(async ({ stateDir, codexHome }) => {
    // Seed cache with a different model than what the resolver would pick now.
    await writeModelsCache(codexHome, ["gpt-6-luna"]);
    await resolveRecallCompressorProfile(baseCfg(), {}, { CODEX_HOME: codexHome });

    // Change the catalogue so the resolver would pick something else if it
    // ran again; cache-first detect should NOT re-resolve.
    await writeModelsCache(codexHome, ["gpt-5.6-luna"]);

    const profile = await detectRecallCompressorProfile(
      baseCfg(),
      {},
      { CODEX_HOME: codexHome },
    );
    assert.equal(profile.model, "gpt-6-luna");
    // Persisted profile is unchanged.
    const persisted = JSON.parse(
      await readFile(join(stateDir, "recall-compressor-profile.json"), "utf-8"),
    );
    assert.equal(persisted.profile.model, "gpt-6-luna");
  });
});

test("detectRecallCompressorProfile resolves on cache miss", async () => {
  await withTempState(async ({ codexHome }) => {
    await writeModelsCache(codexHome, ["gpt-5.6-luna"]);
    const profile = await detectRecallCompressorProfile(
      baseCfg(),
      {},
      { CODEX_HOME: codexHome },
    );
    assert.equal(profile.enabled, true);
    assert.equal(profile.model, "gpt-5.6-luna");
    assert.equal(profile.thinking, "low");
  });
});

test("detectRecallCompressorProfile after invalidate re-resolves against current catalogue", async () => {
  await withTempState(async ({ codexHome }) => {
    await writeModelsCache(codexHome, ["gpt-6-luna", "gpt-5.6-luna"]);
    const initial = await detectRecallCompressorProfile(
      baseCfg(),
      {},
      { CODEX_HOME: codexHome },
    );
    assert.equal(initial.model, "gpt-6-luna");

    // Simulate runtime compress failure → invalidate cache, then drop the
    // failing slug from the catalogue. Next detect should pick the next one.
    await invalidateRecallCompressorProfileCache();
    await writeModelsCache(codexHome, ["gpt-5.6-luna"]);

    const next = await detectRecallCompressorProfile(
      baseCfg(),
      {},
      { CODEX_HOME: codexHome },
    );
    assert.equal(next.model, "gpt-5.6-luna");
    assert.equal(next.thinking, "low");
    assert.equal(next.detected, true);
  });
});

test("detectRecallCompressorProfile with detect_on_startup=false returns null on cache miss", async () => {
  await withTempState(async ({ codexHome }) => {
    await writeModelsCache(codexHome, ["gpt-5.6-luna"]);
    const profile = await detectRecallCompressorProfile(
      baseCfg({ recallCompressDetectOnStartup: false }),
      {},
      { CODEX_HOME: codexHome },
    );
    assert.equal(profile, null);
  });
});

test("markRecallCompressorRuntimeFailed writes a disabled profile cached for UPS skip", async () => {
  await withTempState(async ({ codexHome }) => {
    await writeModelsCache(codexHome, ["gpt-5.6-luna"]);
    await resolveRecallCompressorProfile(baseCfg(), {}, { CODEX_HOME: codexHome });

    await markRecallCompressorRuntimeFailed(baseCfg(), { failedModel: "gpt-6-luna" });
    const cached = await loadCachedRecallCompressorProfile(baseCfg());
    assert.ok(cached, "expected cached profile to exist");
    assert.equal(cached.enabled, false);
    assert.equal(cached.source, "runtime_failed");
    assert.equal(cached.failedModel, "gpt-6-luna");
  });
});

test("detectRecallCompressorProfile recovers across SessionStart after runtime_failed marker", async () => {
  await withTempState(async ({ codexHome }) => {
    // Initial healthy resolve.
    await writeModelsCache(codexHome, ["gpt-6-luna", "gpt-5.6-luna"]);
    const initial = await detectRecallCompressorProfile(baseCfg(), {}, { CODEX_HOME: codexHome });
    assert.equal(initial.model, "gpt-6-luna");

    // Runtime compress failure marks the cache disabled. UPS within the
    // same session would now read this disabled profile and skip compress.
    await markRecallCompressorRuntimeFailed(baseCfg(), { failedModel: "gpt-6-luna" });
    const within = await loadCachedRecallCompressorProfile(baseCfg());
    assert.equal(within.enabled, false);

    // Next codex SessionStart: catalogue shrinks (the failed slug went
    // away). Detect should treat runtime_failed as cache miss and
    // re-resolve against the current catalogue, picking gpt-5.6-luna.
    await writeModelsCache(codexHome, ["gpt-5.6-luna"]);
    const recovered = await detectRecallCompressorProfile(baseCfg(), {}, { CODEX_HOME: codexHome });
    assert.equal(recovered.enabled, true);
    assert.equal(recovered.model, "gpt-5.6-luna");
    assert.equal(recovered.thinking, "low");
  });
});

test("detectRecallCompressorProfile with detect_on_startup=false honors runtime_failed marker", async () => {
  await withTempState(async ({ codexHome }) => {
    await writeModelsCache(codexHome, ["gpt-5.6-luna"]);
    await resolveRecallCompressorProfile(baseCfg(), {}, { CODEX_HOME: codexHome });
    await markRecallCompressorRuntimeFailed(baseCfg(), { failedModel: "gpt-5.6-luna" });

    const profile = await detectRecallCompressorProfile(
      baseCfg({ recallCompressDetectOnStartup: false }),
      {},
      { CODEX_HOME: codexHome },
    );
    // With detect-on-startup disabled, we respect the marker (don't auto-
    // recover); UPS keeps skipping compress until manual intervention.
    assert.ok(profile);
    assert.equal(profile.enabled, false);
    assert.equal(profile.source, "runtime_failed");
  });
});