Skip to content

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

9.8 KB
Raw Blame
import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
import { loadConfig } from "./config.mjs";
import { isBypassed } from "./shared/session-model.mjs";
import { usageEnabled, usageOutput, usageView } from "./usage/settings.mjs";

// The layers, the knobs and the peer are the shared loader's, and
// memory-plugin-shared/plugin-config.test.mjs holds this harness to them. What
// is left here is the compression modes this harness preserves, and
// the bypass patterns it is the only harness to resolve from every layer.
const OVERRIDES = [
  "OPENVIKING_CONFIG_FILE",
  "OPENVIKING_CLI_CONFIG_FILE",
  "OPENVIKING_HOME",
  "OPENVIKING_STATE_DIR",
  "OPENVIKING_BYPASS_SESSION",
  "OPENVIKING_BYPASS_SESSION_PATTERNS",
  "OPENVIKING_RECALL_COMPRESS",
  "OPENVIKING_RECALL_REWRITE",
  "OPENVIKING_RECALL_COMPRESS_MODEL",
  "OPENVIKING_RECALL_COMPRESS_THINKING",
  "OPENVIKING_URL",
  "OPENVIKING_BASE_URL",
  "OPENVIKING_MCP_URL",
  "OPENVIKING_API_KEY",
  "OPENVIKING_BEARER_TOKEN",
  "OPENVIKING_ACCOUNT",
  "OPENVIKING_USER",
  "OPENVIKING_PEER_ID",
  "OPENVIKING_AUTH_MODE",
  "OPENVIKING_CREDENTIAL_SOURCE",
  "OPENVIKING_CREDENTIALS_SOURCE",
  "OPENVIKING_RECALL_QUERY_FILTERS",
  "OPENVIKING_CAPTURE_FILTERS",
  "OPENVIKING_USAGE_VIEW",
  "OPENVIKING_USAGE_OUTPUT",
];

/**
 * Run loadConfig() against a throwaway ~/.openviking pair plus a workspace
 * directory holding `.openviking/config.json`. The bare `.git` is what makes
 * that directory a workspace root; `other/` is a second directory with neither.
 */
function withConfigs({ ov, cli, workspace, env = {} }, fn) {
  const dir = mkdtempSync(join(tmpdir(), "cx-config-"));
  const ovPath = join(dir, "ov.conf");
  const cliPath = join(dir, "ovcli.conf");
  const workspaceDir = join(dir, "workspace");
  const otherDir = join(dir, "other");
  const saved = Object.fromEntries(OVERRIDES.map((key) => [key, process.env[key]]));
  try {
    for (const key of OVERRIDES) delete process.env[key];
    if (ov) writeFileSync(ovPath, JSON.stringify(ov));
    if (cli) writeFileSync(cliPath, JSON.stringify(cli));
    mkdirSync(join(workspaceDir, ".openviking"), { recursive: true });
    mkdirSync(join(workspaceDir, ".git"), { recursive: true });
    mkdirSync(otherDir, { recursive: true });
    if (workspace) {
      writeFileSync(join(workspaceDir, ".openviking", "config.json"), JSON.stringify(workspace));
    }
    process.env.OPENVIKING_CONFIG_FILE = ovPath;
    process.env.OPENVIKING_CLI_CONFIG_FILE = cliPath;
    // Keeps the identity cache and the workspace registry out of the real home.
    process.env.OPENVIKING_HOME = join(dir, "home");
    Object.assign(process.env, env);
    return fn({ ovPath, cliPath, workspaceDir, otherDir });
  } finally {
    for (const [key, value] of Object.entries(saved)) {
      if (value === undefined) delete process.env[key];
      else process.env[key] = value;
    }
    rmSync(dir, { recursive: true, force: true });
  }
}

test("a repository can carry the bypass patterns its contributors share", () => {
  withConfigs({
    cli: { url: "http://127.0.0.1:1933", api_key: "sk-cli" },
    workspace: { version: 1, bypass: { session_patterns: ["**/workspace"] } },
  }, ({ workspaceDir, otherDir }) => {
    const inside = loadConfig(workspaceDir);
    assert.deepEqual(inside.bypassSessionPatterns, ["**/workspace"]);
    assert.equal(isBypassed(inside, { cwd: workspaceDir }), true);

    const outside = loadConfig(otherDir);
    assert.deepEqual(outside.bypassSessionPatterns, []);
    assert.equal(isBypassed(outside, { cwd: otherDir }), false);
  });
});

test("the bypass env vars override the file, matching every other harness", () => {
  withConfigs({
    ov: { codex: { bypassSessionPatterns: ["/from/ov-conf"] } },
    cli: { url: "http://127.0.0.1:1933", api_key: "sk-cli" },
    env: {
      OPENVIKING_BYPASS_SESSION_PATTERNS: "/from/env, /also/env ,",
      OPENVIKING_BYPASS_SESSION: "1",
    },
  }, ({ otherDir }) => {
    const cfg = loadConfig(otherDir);
    assert.deepEqual(cfg.bypassSessionPatterns, ["/from/env", "/also/env"]);
    assert.equal(cfg.bypassSession, true);
    assert.equal(isBypassed(cfg, { cwd: "/anywhere" }), true, "the switch wins regardless of cwd");
  });
});

test("ov.conf's codex section still supplies the patterns when no env var does", () => {
  withConfigs({
    ov: { codex: { bypassSessionPatterns: ["/scratch/*", 7, "  "] } },
    cli: { url: "http://127.0.0.1:1933", api_key: "sk-cli" },
  }, ({ otherDir }) => {
    const cfg = loadConfig(otherDir);
    assert.deepEqual(cfg.bypassSessionPatterns, ["/scratch/*"]);
    assert.equal(cfg.bypassSession, false);
  });
});

// Naming the mode must not count as configuring a local model.
test("compression modes survive config resolution, and a model configures the local compressor", () => {
  withConfigs({
    cli: { url: "http://127.0.0.1:1933", api_key: "sk-cli", plugin: { recallCompress: "auto" } },
  }, ({ otherDir }) => {
    const cfg = loadConfig(otherDir);
    assert.equal(cfg.recallCompress, "auto");
    assert.equal(cfg.recallRewrite, "auto");
    assert.equal(cfg.recallCompressConfigured, false, "naming the switch is not naming a compressor");
  });

  withConfigs({
    cli: { url: "http://127.0.0.1:1933", api_key: "sk-cli", plugin: { recallCompress: "off" } },
  }, ({ otherDir }) => {
    assert.equal(loadConfig(otherDir).recallCompress, "off");
  });

  withConfigs({
    cli: {
      url: "http://127.0.0.1:1933",
      api_key: "sk-cli",
      plugin: { codex: { recallCompressModel: "gpt-5.5" } },
    },
  }, ({ otherDir }) => {
    assert.equal(loadConfig(otherDir).recallCompressConfigured, true);
  });
});

test("input filter rules default to empty and read from the legacy ov.conf block", () => {
  withConfigs({ ov: { server: { host: "127.0.0.1" } } }, () => {
    assert.deepEqual(loadConfig().recallQueryFilters, []);
    assert.deepEqual(loadConfig().captureFilters, []);
  });
  withConfigs({
    ov: { server: { host: "127.0.0.1" }, codex: { captureFilters: ["d/^scratch:/"] } },
  }, () => {
    assert.deepEqual(loadConfig().captureFilters, ["d/^scratch:/"]);
  });
});

test("plugin.codex input filters beat the shared plugin section", () => {
  withConfigs({
    ov: { server: { host: "127.0.0.1" } },
    cli: {
      url: "http://127.0.0.1:1933",
      plugin: {
        recallQueryFilters: ["s/shared//"],
        captureFilters: ["d/^shared/"],
        codex: { recallQueryFilters: ["s/harness//"] },
      },
    },
  }, () => {
    const cfg = loadConfig();
    assert.deepEqual(cfg.recallQueryFilters, ["s/harness//"]);
    assert.deepEqual(cfg.captureFilters, ["d/^shared/"]);
  });
});

test("an env CSV replaces the configured filter list, trimming every entry", () => {
  withConfigs({
    ov: { server: { host: "127.0.0.1" }, codex: { recallQueryFilters: ["s/^hi //"] } },
    env: { OPENVIKING_RECALL_QUERY_FILTERS: " s/^a// , d/^b/ " },
  }, () => {
    assert.deepEqual(loadConfig().recallQueryFilters, ["s/^a//", "d/^b/"]);
  });
});

test("configured filter entries are trimmed and non-strings dropped", () => {
  withConfigs({
    ov: {
      server: { host: "127.0.0.1" },
      codex: { captureFilters: ["  s/^a//  ", "", 7, null, "d/^b/"] },
    },
  }, () => {
    assert.deepEqual(loadConfig().captureFilters, ["s/^a//", "d/^b/"]);
  });
});

test("a comma survives in a configured rule but splits an env one", () => {
  withConfigs({
    ov: { server: { host: "127.0.0.1" }, codex: { captureFilters: ["s/a{2,}/X/"] } },
  }, () => {
    assert.deepEqual(loadConfig().captureFilters, ["s/a{2,}/X/"]);
  });
  withConfigs({
    ov: { server: { host: "127.0.0.1" } },
    env: { OPENVIKING_CAPTURE_FILTERS: "s/a{2,}/X/" },
  }, () => {
    assert.deepEqual(loadConfig().captureFilters, ["s/a{2", "}/X/"]);
  });
});

for (const mode of ["off", "client", "server", "auto", "0", "1"]) {
  test(`compression env preserves ${mode}`, () => {
    withConfigs({env: {OPENVIKING_RECALL_COMPRESS: mode}}, ({otherDir}) => {
      const expected = mode === "0" ? "off" : mode === "1" ? "auto" : mode;
      assert.equal(loadConfig(otherDir).recallRewrite, expected);
    });
  });
}

test("slash-form directory patterns match a Windows working directory", () => {
  const cfg = { bypassSessionPatterns: ["**/scratch"] };
  assert.equal(isBypassed(cfg, { cwd: String.raw`C:\work\scratch` }), true);
  assert.equal(isBypassed(cfg, { cwd: String.raw`C:\work\keep` }), false);
  assert.equal(isBypassed(cfg, { sessionId: String.raw`a\scratch` }), false, "session ids stay literal");
  assert.equal(
    isBypassed({ bypassSessionPatterns: [String.raw`C:\work\scratch`] }, { cwd: String.raw`C:\work\scratch` }),
    true,
    "a backslash pattern still matches the raw path",
  );
});

test("OV-Usage settings come from the config files, and env overrides them", () => {
  withConfigs({
    cli: { url: "http://127.0.0.1:1933", api_key: "sk-cli", plugin: { codex: { usageView: "OFF", usageOutput: "terminal" } } },
  }, ({ otherDir }) => {
    const cfg = loadConfig(otherDir);
    assert.equal(usageEnabled(cfg), false);
    assert.equal(usageOutput(cfg), "terminal");
  });

  withConfigs({
    cli: { url: "http://127.0.0.1:1933", api_key: "sk-cli", plugin: { usageView: false } },
    workspace: { version: 1, usage: { output: "desktop" } },
  }, ({ workspaceDir }) => {
    const cfg = loadConfig(workspaceDir);
    assert.equal(usageEnabled(cfg), false);
    assert.equal(usageOutput(cfg), "desktop");
  });

  withConfigs({
    cli: { url: "http://127.0.0.1:1933", api_key: "sk-cli", plugin: { usageView: "off", usageOutput: "terminal" } },
    env: { OPENVIKING_USAGE_VIEW: "expanded", OPENVIKING_USAGE_OUTPUT: "desktop" },
  }, ({ otherDir }) => {
    const cfg = loadConfig(otherDir);
    assert.equal(usageEnabled(cfg), true);
    assert.equal(usageView(cfg), "expanded");
    assert.equal(usageOutput(cfg), "desktop");
  });
});