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
3.8 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync } 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 { buildToolTable } from "../../src/mcp/tools.ts";

let vault: string;

beforeEach(() => {
  vault = mkdtempSync(join(tmpdir(), "o2b-mcp-pre-compact-extract-"));
  // An unswitched machine: a vault with no `Brain/` is a store-only fresh
  // install (design D14 item 1), whose writes need a device id and a store.
  mkdirSync(join(vault, "Brain"), { recursive: true });
});

afterEach(() => {
  rmSync(vault, { recursive: true, force: true });
});

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

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

describe("brain_pre_compact_extract tool registration", () => {
  test("registered in the full tool table only", () => {
    expect(
      buildToolTable("full").find((tool) => tool.name === "brain_pre_compact_extract"),
    ).toBeDefined();
    expect(
      buildToolTable("writer").find((tool) => tool.name === "brain_pre_compact_extract"),
    ).toBeUndefined();
  });
});

describe("brain_pre_compact_extract tool", () => {
  test("emits sanitized typed continuity records idempotently", async () => {
    const args = {
      session_id: "session-mcp",
      turn_start: "turn-1",
      turn_end: "turn-2",
      text: "Rule: Stay opt-in.\nCommitment: Remove data:image/png;base64,QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVo=",
    };
    const first = await callExtract(args);
    const second = await callExtract(args);

    expect(first).toMatchObject({ count: 2, errors: [] });
    expect(second).toMatchObject({ count: 2, errors: [] });
    expect((first.records as Array<{ id: string }>).map((record) => record.id)).toEqual(
      (second.records as Array<{ id: string }>).map((record) => record.id),
    );
    expect(JSON.stringify(first)).not.toContain("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVo");
  });

  test("dry_run previews candidate records without persisting", async () => {
    const args = {
      session_id: "session-preview",
      turn_start: "turn-1",
      turn_end: "turn-1",
      text: "Decision: Preview only.",
    };
    const preview = await callExtract({ ...args, dry_run: true });
    expect(preview).toMatchObject({ count: 1, dry_run: true, errors: [] });

    // The preview's logical identity (timestamp-independent dedupe_key)
    // matches the record the real path later appends — the preview predicts
    // real extraction. (record `id` folds in the ms-precise createdAt, which
    // differs between two un-pinned calls, so compare dedupe_key.)
    const real = await callExtract(args);
    expect(real).toMatchObject({ count: 1, dry_run: false });
    const key = (recs: unknown): string =>
      (recs as Array<{ payload: { dedupe_key: string } }>)[0]!.payload.dedupe_key;
    expect(key(preview.records)).toBe(key(real.records));
  });
});