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
6.9 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

import type { DreamRunSummary } from "../../../../src/core/brain/dream.ts";
import { bootstrapBrain } from "../../../../src/core/brain/init.ts";
import {
  buildOperatorSummary,
  renderOperatorSummaryMarkdown,
} from "../../../../src/core/brain/trust/operator-summary.ts";

let vault: string;

beforeEach(() => {
  vault = mkdtempSync(join(tmpdir(), "o2b-op-summary-"));
  bootstrapBrain(vault);
});

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

function emptyDream(over: Partial<DreamRunSummary> = {}): DreamRunSummary {
  return Object.freeze({
    run_id: "dream-2026-05-25-000000",
    changed: false,
    new_unconfirmed: [],
    confirmed: [],
    retired: [],
    contradictions: [],
    moved_to_processed: [],
    suppressed: [],
    warnings: [],
    uncertain: [],
    quarantined: [],
    ...over,
  }) as DreamRunSummary;
}

describe("buildOperatorSummary - structural envelope", () => {
  test("clean vault produces all-zero envelope with trust=clean", () => {
    const r = buildOperatorSummary(vault, { dreamSummary: emptyDream() });
    expect(r.trust_verdict).toBe("clean");
    expect(r.verification_delta.summary.confirmed).toBe(0);
    expect(r.verification_delta.summary.drift).toBe(0);
    expect(r.instruction_file_warnings).toEqual([]);
    expect(r.doctor_summary.warning_count).toBe(0);
    expect(r.doctor_summary.error_count).toBe(0);
    expect(r.dream_summary.warning_count).toBe(0);
    expect(r.dream_summary.uncertain_count).toBe(0);
    expect(r.dream_summary.quarantined_count).toBe(0);
  });

  test("instruction-file warning surfaces in envelope; verdict stays clean (informational)", () => {
    // The fixture's instruction file is operator-authored, so it carries
    // the `cli` stamp - the page-origin vocabulary's one operator-authored
    // answer. Unstamped, the delivery-provenance doctor check would fire
    // on it and the trust verdict would read `watch` for a reason this
    // test is not about: here only the instruction-file ceiling is under
    // test, and that warning is informational by contract.
    writeFileSync(
      join(vault, "CLAUDE.md"),
      "---\norigin_channel: cli\n---\n" + "line\n".repeat(300),
    );
    const r = buildOperatorSummary(vault, {
      dreamSummary: emptyDream(),
      guardrails: {
        promotion_min_signals: 2,
        promotion_min_distinct_agents: 1,
        promotion_min_age_days: 0,
        instruction_file_max_lines: 100,
        untrusted_source_delimiting: false,
        derived_fact_synthesis: false,
        provenance_trust_ordering: false,
        owner_scoped_facts: false,
        marker_writeback: false,
        ambient_writeback: true,
        ambient_ttl_days: 0,
      },
    });
    expect(r.instruction_file_warnings).toHaveLength(1);
    expect(r.instruction_file_warnings[0]?.path).toBe("CLAUDE.md");
    // Doctor errors zero; instruction-file warning is informational only
    // and is NOT counted toward the trust verdict (it lives outside the
    // doctor warnings list). Verdict stays clean.
    expect(r.trust_verdict).toBe("clean");
  });

  test("without dream summary, verification delta is empty and trust is clean", () => {
    const r = buildOperatorSummary(vault, {});
    expect(r.verification_delta.summary.confirmed).toBe(0);
    expect(r.verification_delta.entries).toEqual([]);
    expect(r.trust_verdict).toBe("clean");
  });

  test("returned object is frozen", () => {
    const r = buildOperatorSummary(vault, { dreamSummary: emptyDream() });
    expect(Object.isFrozen(r)).toBe(true);
  });
});

describe("buildOperatorSummary - collect_errors (review M10)", () => {
  test("a failed maintenance scan lands in the envelope as a named row", () => {
    // A FILE at Brain/preferences makes the dedup scan's readdir refuse;
    // every other reader on this surface degrades instead of throwing.
    rmSync(join(vault, "Brain", "preferences"), { recursive: true });
    writeFileSync(join(vault, "Brain", "preferences"), "not a directory", "utf8");

    const r = buildOperatorSummary(vault, {});
    // The break can refuse more than one scan on this surface (the lint
    // resolver shares the directory); the row is about WHAT refused, so
    // the assertion is on the shape and the named dedup entry.
    const errors = r.collect_errors ?? [];
    expect(errors.length).toBeGreaterThan(0);
    expect(errors.some((e) => e.scan === "dedup")).toBe(true);
    for (const e of errors) {
      expect(e.message.replace(/\\/g, "/")).toContain("preferences");
    }
  });

  test("below local reach (a readable filter) each row is the closed form, never the host path", () => {
    rmSync(join(vault, "Brain", "preferences"), { recursive: true });
    writeFileSync(join(vault, "Brain", "preferences"), "not a directory", "utf8");

    const r = buildOperatorSummary(vault, { readable: () => true });
    const errors = r.collect_errors ?? [];
    expect(errors.some((e) => e.scan === "dedup")).toBe(true);
    for (const e of errors) {
      expect(e.message).toMatch(new RegExp(`^${e.scan} scan failed \\(`));
      expect(e.message).not.toContain(tmpdir());
    }
  });

  test("a healthy vault carries no collect_errors row", () => {
    const r = buildOperatorSummary(vault, {});
    expect(r.collect_errors).toBeUndefined();
  });

  test("the markdown renderer names the collected errors when present", () => {
    rmSync(join(vault, "Brain", "preferences"), { recursive: true });
    writeFileSync(join(vault, "Brain", "preferences"), "not a directory", "utf8");

    const md = renderOperatorSummaryMarkdown(buildOperatorSummary(vault, {}));
    expect(md).toContain("## Collection errors");
    expect(md).toContain("dedup");
  });
});

describe("renderOperatorSummaryMarkdown", () => {
  test("markdown contains the three signal sections", () => {
    const r = buildOperatorSummary(vault, { dreamSummary: emptyDream() });
    const md = renderOperatorSummaryMarkdown(r);
    expect(md).toContain("# Operator summary");
    expect(md).toContain("Trust:");
    expect(md).toContain("Doctor:");
    expect(md).toContain("Dream:");
  });

  test("markdown lists instruction-file warnings when present", () => {
    writeFileSync(join(vault, "CLAUDE.md"), "x\n".repeat(300));
    const r = buildOperatorSummary(vault, {
      dreamSummary: emptyDream(),
      guardrails: {
        promotion_min_signals: 2,
        promotion_min_distinct_agents: 1,
        promotion_min_age_days: 0,
        instruction_file_max_lines: 100,
        untrusted_source_delimiting: false,
        derived_fact_synthesis: false,
        provenance_trust_ordering: false,
        owner_scoped_facts: false,
        marker_writeback: false,
        ambient_writeback: true,
        ambient_ttl_days: 0,
      },
    });
    const md = renderOperatorSummaryMarkdown(r);
    expect(md).toContain("CLAUDE.md");
  });
});