Skip to content

Local-first 🧠 memory for Hermes Agent that lives in your Obsidian vault and remembers project context. Nightly 😴 dream passes turn repeat corrections into confirmed preferences with measurable confidence. Adapters ship for Claude Code, Codex, and OpenClaw, with an MCP server for anything else.

TypeScript
7.0 KB
Raw Blame
/**
 * brain_health MCP tool (F6 surface): returns the semantic-health
 * report (contradiction / concept-gap / stale-claim domains + verdict)
 * produced by runDoctor's reconciliation pass. Read-only.
 */

import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

import { JSONRPC_VERSION, MCPServer, PROTOCOL_VERSION } from "../../src/mcp/index.ts";
import { atomicWriteFileSync } from "../../src/core/fs-atomic.ts";
import { writeSignal } from "../../src/core/brain/signal.ts";
import { writePreference } from "../../src/core/brain/preference.ts";
import { BRAIN_PREFERENCE_STATUS, BRAIN_SIGNAL_SIGN } from "../../src/core/brain/types.ts";

let tmp: string;
let vault: string;
let configHome: string;
let configPath: string;
const savedEnv: Record<string, string | undefined> = {};

beforeEach(() => {
  tmp = mkdtempSync(join(tmpdir(), "o2b-mcp-health-"));
  vault = join(tmp, "vault");
  for (const d of ["preferences", "retired", "inbox", "processed", "log"]) {
    mkdirSync(join(vault, "Brain", d), { recursive: true });
  }
  writeFileSync(join(vault, "Brain", "_brain.yaml"), "schema_version: 1\n");
  configHome = mkdtempSync(join(tmpdir(), "o2b-mcp-health-cfg-"));
  configPath = join(configHome, "config.yaml");
  for (const k of ["VAULT_AGENT_NAME", "VAULT_TIMEZONE", "VAULT_DIR", "OPEN_SECOND_BRAIN_CONFIG"]) {
    savedEnv[k] = process.env[k];
    delete process.env[k];
  }
  process.env["OPEN_SECOND_BRAIN_CONFIG"] = configPath;
  atomicWriteFileSync(configPath, `vault: ${vault}\nagent_name: claude\n`);
});

afterEach(() => {
  rmSync(tmp, { recursive: true, force: true });
  rmSync(configHome, { recursive: true, force: true });
  for (const [k, v] of Object.entries(savedEnv)) {
    if (v === undefined) delete process.env[k];
    else process.env[k] = v;
  }
});

async function initialize(server: MCPServer): Promise<void> {
  await server.handleRequest({
    jsonrpc: JSONRPC_VERSION,
    id: 1,
    method: "initialize",
    params: {
      protocolVersion: PROTOCOL_VERSION,
      capabilities: {},
      clientInfo: { name: "health-test", version: "0" },
    },
  });
  await server.handleRequest({ jsonrpc: JSONRPC_VERSION, method: "notifications/initialized" });
}

async function callTool(
  server: MCPServer,
  name: string,
  args: Record<string, unknown>,
): Promise<Record<string, unknown>> {
  const r = (await server.handleRequest({
    jsonrpc: JSONRPC_VERSION,
    id: 9,
    method: "tools/call",
    params: { name, arguments: args },
  })) as { result: { content: ReadonlyArray<{ type: string; text: string }> } };
  return JSON.parse(r.result.content[0]!.text);
}

describe("brain_health MCP tool", () => {
  test("clean vault returns a clean verdict and empty domains", async () => {
    const server = new MCPServer({ vault, configPath });
    await initialize(server);
    const out = await callTool(server, "brain_health", {});
    expect(out["verdict"]).toBe("clean");
    expect(out["contradictions"]).toEqual([]);
    expect(out["concept_gaps"]).toEqual([]);
    expect(out["stale_claims"]).toEqual([]);
  });

  test("contradictory preferences surface with an investigate verdict", async () => {
    const pos = writeSignal(vault, {
      topic: "indentation",
      signal: BRAIN_SIGNAL_SIGN.positive,
      agent: "t",
      principle: "use tabs",
      created_at: "2026-05-01T00:00:00Z",
      date: "2026-05-01",
      slug: "tabs-pos",
    }).id;
    const neg = writeSignal(vault, {
      topic: "indentation",
      signal: BRAIN_SIGNAL_SIGN.negative,
      agent: "t",
      principle: "use spaces",
      created_at: "2026-05-01T00:00:00Z",
      date: "2026-05-01",
      slug: "tabs-neg",
    }).id;
    for (const [slug, principle, ev] of [
      ["tabs-rule", "always indent source with tabs not spaces", pos],
      ["spaces-rule", "never indent source with tabs always spaces", neg],
    ] as const) {
      writePreference(
        vault,
        {
          slug,
          topic: slug,
          principle,
          created_at: "2026-05-01T00:00:00Z",
          unconfirmed_until: "2026-05-08T00:00:00Z",
          confirmed_at: "2026-05-08T00:00:00Z",
          status: BRAIN_PREFERENCE_STATUS.confirmed,
          evidenced_by: [`[[${ev}]]`],
        },
        { overwrite: true },
      );
    }
    const server = new MCPServer({ vault, configPath });
    await initialize(server);
    const out = await callTool(server, "brain_health", {});
    expect(out["verdict"]).toBe("investigate");
    expect((out["contradictions"] as unknown[]).length).toBe(1);
  });

  test("silence_before watermark surfaces the suppressed mirror (snake_case)", async () => {
    // A burst of confirmed preferences dated well before the watermark
    // trips batch-inflation detection; setting `health.silence_before`
    // after that date hides the burst and the tool must mirror the hidden
    // count back as `suppressed.batch_inflation`.
    for (let i = 0; i < 6; i++) {
      writePreference(
        vault,
        {
          slug: `batch-${i}`,
          topic: `topic-${i}`,
          principle: `rule number ${i} for the batch`,
          created_at: "2026-01-01T00:00:00Z",
          unconfirmed_until: "2026-01-08T00:00:00Z",
          confirmed_at: "2026-01-01T00:00:00Z",
          status: BRAIN_PREFERENCE_STATUS.confirmed,
          evidenced_by: [],
          content_hash: "0".repeat(64),
        },
        { overwrite: true },
      );
    }
    writeFileSync(
      join(vault, "Brain", "_brain.yaml"),
      'schema_version: 1\n\nhealth:\n  silence_before: "2026-06-01"\n',
    );

    const server = new MCPServer({ vault, configPath });
    await initialize(server);
    const out = await callTool(server, "brain_health", {});

    expect(out["suppressed"]).toEqual({
      concept_gaps: 0,
      batch_inflation: 1,
      baseline: "2026-06-01",
    });
    expect(out["batch_inflation"] as unknown[]).toEqual([]);
  });

  test("no watermark set means no suppressed key at all", async () => {
    // Same burst as above, but _brain.yaml (written in beforeEach) carries
    // no `health.silence_before` - the finding surfaces normally and the
    // tool must not emit a `suppressed` key, not even an empty one.
    for (let i = 0; i < 6; i++) {
      writePreference(
        vault,
        {
          slug: `batch-${i}`,
          topic: `topic-${i}`,
          principle: `rule number ${i} for the batch`,
          created_at: "2026-01-01T00:00:00Z",
          unconfirmed_until: "2026-01-08T00:00:00Z",
          confirmed_at: "2026-01-01T00:00:00Z",
          status: BRAIN_PREFERENCE_STATUS.confirmed,
          evidenced_by: [],
          content_hash: "0".repeat(64),
        },
        { overwrite: true },
      );
    }

    const server = new MCPServer({ vault, configPath });
    await initialize(server);
    const out = await callTool(server, "brain_health", {});

    expect("suppressed" in out).toBe(false);
    expect((out["batch_inflation"] as unknown[]).length).toBe(1);
  });
});