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
8.3 KB
Raw Blame
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 { traceIdeaLineage, IdeaLineageError } from "../../src/core/brain/idea-lineage.ts";
import { appendContinuityRecord } from "../../src/core/brain/continuity/store.ts";
import { appendSessionSummary } from "../../src/core/brain/session-summary.ts";

let vault: string;

beforeEach(() => {
  vault = mkdtempSync(join(tmpdir(), "o2b-idea-lineage-"));
  // A legacy Brain/ tree: an unswitched machine (a vault without one is store-only).
  mkdirSync(join(vault, "Brain"), { recursive: true });
});

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

function writeDream(date: string, body: Record<string, unknown>): void {
  mkdirSync(join(vault, "Brain", "log"), { recursive: true });
  const line = JSON.stringify({ ts: `${date}T08:00:00Z`, kind: "dream", payload: body });
  writeFileSync(join(vault, "Brain", "log", `${date}.jsonl`), `${line}\n`);
}

describe("traceIdeaLineage over the continuity source graph", () => {
  test("a digest traces back to the session turns it was distilled from", () => {
    appendContinuityRecord(vault, {
      kind: "session_turn",
      createdAt: "2026-06-14T09:00:00.000Z",
      payload: { session_id: "s1", turn_id: "t1", role: "user", text: "please add lineage" },
    });
    const digest = appendSessionSummary(vault, {
      sessionId: "s1",
      decisions: ["build the tracer"],
      sourceTurnIds: ["t1"],
      createdAt: "2026-06-14T10:00:00.000Z",
    });

    const result = traceIdeaLineage(vault, { id: digest.id });
    expect(result.root.id).toBe(digest.id);
    expect(result.root.stage).toBe("conclusion");

    const turn = result.nodes.find((n) => n.kind === "session_turn");
    expect(turn).toBeDefined();
    expect(turn!.stage).toBe("observation");
    expect(result.edges.some((e) => e.from === digest.id && e.to === turn!.id)).toBe(true);
  });

  test("a source referenced more than once appears once and traversal terminates", () => {
    const turn = appendContinuityRecord(vault, {
      kind: "session_turn",
      createdAt: "2026-06-14T09:00:00.000Z",
      payload: { session_id: "s2", turn_id: "t1", role: "user", text: "hi" },
    });
    // A node whose sourceRefs reference the same turn twice (by record id
    // and by turn id) must not produce a duplicate node or loop.
    const node = appendContinuityRecord(vault, {
      kind: "session_summary_node",
      createdAt: "2026-06-14T10:00:00.000Z",
      sourceRefs: [
        { id: turn.id, kind: "continuity_record" },
        { id: "t1", kind: "session_turn" },
      ],
      payload: { session_id: "s2", depth: 1, summary: "rollup" },
    });

    const result = traceIdeaLineage(vault, { id: node.id });
    const turnNodes = result.nodes.filter((n) => n.id === turn.id);
    expect(turnNodes.length).toBe(1);
    expect(result.truncated).toBe(false);
  });

  test("a turn ref resolves within the digest's own session when turn ids collide", () => {
    // Two sessions reuse turn_id "t1". The digest in session A must link to
    // A's turn, never B's.
    const turnA = appendContinuityRecord(vault, {
      kind: "session_turn",
      createdAt: "2026-06-14T09:00:00.000Z",
      payload: { session_id: "A", turn_id: "t1", role: "user", text: "in A" },
    });
    appendContinuityRecord(vault, {
      kind: "session_turn",
      createdAt: "2026-06-14T09:30:00.000Z",
      payload: { session_id: "B", turn_id: "t1", role: "user", text: "in B" },
    });
    const digestA = appendSessionSummary(vault, {
      sessionId: "A",
      decisions: ["decided in A"],
      sourceTurnIds: ["t1"],
      createdAt: "2026-06-14T10:00:00.000Z",
    });

    const result = traceIdeaLineage(vault, { id: digestA.id });
    const turnNodes = result.nodes.filter((n) => n.kind === "session_turn");
    expect(turnNodes.length).toBe(1);
    expect(turnNodes[0]!.id).toBe(turnA.id);
  });

  test("an unknown id is a typed error, not a silent empty chain", () => {
    expect(() => traceIdeaLineage(vault, { id: "ctn_does_not_exist" })).toThrow(IdeaLineageError);
  });

  test("a depth bound is reported as truncated", () => {
    // Chain: n2 -> n1 -> turn. maxDepth 1 stops before the turn.
    const turn = appendContinuityRecord(vault, {
      kind: "session_turn",
      createdAt: "2026-06-14T08:00:00.000Z",
      payload: { session_id: "s3", turn_id: "t1", role: "user", text: "root cause" },
    });
    const n1 = appendContinuityRecord(vault, {
      kind: "session_summary_node",
      createdAt: "2026-06-14T09:00:00.000Z",
      sourceRefs: [{ id: turn.id, kind: "continuity_record" }],
      payload: { session_id: "s3", depth: 1, summary: "level 1" },
    });
    const n2 = appendContinuityRecord(vault, {
      kind: "session_summary_node",
      createdAt: "2026-06-14T10:00:00.000Z",
      sourceRefs: [{ id: n1.id, kind: "continuity_record" }],
      payload: { session_id: "s3", depth: 2, summary: "level 2" },
    });

    const result = traceIdeaLineage(vault, { id: n2.id }, { maxDepth: 1 });
    expect(result.truncated).toBe(true);
  });
});

describe("idea lineage no-session semantics (t_22d58fa7)", () => {
  test("two no-session records never merge through a fabricated scope key", () => {
    // Store order decides the global turn index, so the string-session
    // turn is appended first. The second turn carries a NUMERIC session
    // id - the value the turn index used to stringify into a "5" scope,
    // merging two unrelated records into one group.
    const stringTurn = appendContinuityRecord(vault, {
      kind: "session_turn",
      createdAt: "2026-06-14T09:00:00.000Z",
      payload: { session_id: "real", turn_id: "t1", role: "user", text: "string session" },
    });
    appendContinuityRecord(vault, {
      kind: "session_turn",
      createdAt: "2026-06-14T09:30:00.000Z",
      payload: { session_id: 5, turn_id: "t1", role: "user", text: "numeric session" },
    });
    const referrer = appendContinuityRecord(vault, {
      kind: "session_summary_node",
      createdAt: "2026-06-14T10:00:00.000Z",
      sourceRefs: [{ id: "t1", kind: "session_turn" }],
      payload: { session_id: 5, depth: 1, summary: "numeric referrer" },
    });

    // A number names no session, so the referrer resolves through the
    // global turn index (store order), never through a fabricated "5"
    // scope that would bind it to the other numeric record.
    const result = traceIdeaLineage(vault, { id: referrer.id });
    expect(
      result.edges.some((edge) => edge.from === referrer.id && edge.to === stringTurn.id),
    ).toBe(true);
    const numericTurn = result.nodes.find((node) => node.label.includes("numeric session"));
    expect(numericTurn).toBeUndefined();
  });

  test("a record with no session still resolves its sources through the global turn index", () => {
    const orphanTurn = appendContinuityRecord(vault, {
      kind: "session_turn",
      createdAt: "2026-06-14T09:00:00.000Z",
      payload: { turn_id: "t9", role: "user", text: "unattributed observation" },
    });
    const orphanDigest = appendContinuityRecord(vault, {
      kind: "session_summary_digest",
      createdAt: "2026-06-14T10:00:00.000Z",
      sourceRefs: [{ id: "t9", kind: "session_turn" }],
      payload: { decisions: ["unattributed conclusion"], content_hash: "0123456789abcdef" },
    });

    const result = traceIdeaLineage(vault, { id: orphanDigest.id });
    expect(result.edges.some((edge) => edge.to === orphanTurn.id)).toBe(true);
    expect(result.truncated).toBe(false);
  });
});

describe("traceIdeaLineage for a preference (belief evolution)", () => {
  test("traces creation then promotion of a preference", () => {
    writeDream("2026-05-01", { run_id: "r1", new_unconfirmed: ["[[pref-foo|First rule]]"] });
    writeDream("2026-05-10", { run_id: "r2", confirmed: ["[[pref-foo|First rule]]"] });

    const result = traceIdeaLineage(vault, { id: "pref-foo" });
    expect(result.root.id).toBe("pref-foo");
    expect(result.root.kind).toBe("preference");
    const stages = result.nodes.map((n) => n.stage);
    expect(stages).toContain("observation"); // creation
    expect(stages).toContain("synthesis"); // promotion
  });

  test("an unknown preference is a typed error", () => {
    expect(() => traceIdeaLineage(vault, { id: "pref-missing" })).toThrow(IdeaLineageError);
  });
});