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
5.5 KB
Raw Blame
/**
 * Count-triggered fact rollup ladder (S3, t_c5263e27): pure ladder
 * planning - thresholds, counter resets, composition, and idempotency.
 */

import { expect, test } from "bun:test";

import {
  DEFAULT_FACT_ROLLUP_THRESHOLD,
  DEFAULT_ROLLUP_IDENTITY_THRESHOLD,
  planRollupLadder,
  resolveRollupThresholds,
  ROLLUP_TIER,
  type RollupThresholds,
} from "../../../src/core/brain/rollup-ladder.ts";
import type { LinkCandidateManifest } from "../../../src/core/brain/notes/link-candidates.ts";
import { DEFAULT_BRAIN_CONFIG } from "../../../src/core/brain/policy.ts";

const THRESHOLDS: RollupThresholds = { fact: 5, identity: 2 };
const RUN_ID = "dream-2026-07-19-100000";

/**
 * The wikilink targets a fired rung offers, as a VALUE. The planner is
 * pure and takes the manifest rather than walking a vault for one, which
 * is exactly what lets this suite stay a pure-function suite.
 */
const LINK_CANDIDATES: LinkCandidateManifest = Object.freeze({
  candidates: Object.freeze(["analytical-engine", "vector-index-storage"]),
  total: 2,
  truncated: false,
  selection: "all",
});

test("below threshold nothing fires and no counters move", () => {
  const plan = planRollupLadder({
    factCount: 4,
    ledger: null,
    thresholds: THRESHOLDS,
    runId: RUN_ID,
    linkCandidates: () => LINK_CANDIDATES,
  });
  expect(plan.fired).toBe(false);
  expect(plan.entries).toHaveLength(0);
  expect(plan.ledger.baselines[ROLLUP_TIER.fact] ?? 0).toBe(0);
});

test("reaching the fact threshold fires one rollup and resets the counter", () => {
  const plan = planRollupLadder({
    factCount: 5,
    ledger: null,
    thresholds: THRESHOLDS,
    runId: RUN_ID,
    linkCandidates: () => LINK_CANDIDATES,
  });
  expect(plan.fired).toBe(true);
  expect(plan.entries).toHaveLength(1);
  const entry = plan.entries[0]!;
  expect(entry.tier).toBe(ROLLUP_TIER.fact);
  expect(entry.produces).toBe(ROLLUP_TIER.rollup);
  expect(entry.fromCount).toBe(0);
  expect(entry.toCount).toBe(5);
  expect(entry.newSinceLast).toBe(5);
  expect(entry.envelope.status).toBe("needs-llm-step");
  expect(entry.envelope.target_path).toContain(RUN_ID);
  // Counter reset recorded in the ledger.
  expect(plan.ledger.baselines[ROLLUP_TIER.fact]).toBe(5);
  expect(plan.ledger.produced[ROLLUP_TIER.fact]).toBe(1);
});

test("a fired plan is idempotent: replaying it moves no counter", () => {
  const first = planRollupLadder({
    factCount: 5,
    ledger: null,
    thresholds: THRESHOLDS,
    runId: RUN_ID,
    linkCandidates: () => LINK_CANDIDATES,
  });
  const second = planRollupLadder({
    factCount: 5,
    ledger: first.ledger,
    thresholds: THRESHOLDS,
    runId: RUN_ID,
    linkCandidates: () => LINK_CANDIDATES,
  });
  expect(second.fired).toBe(false);
  expect(second.entries).toHaveLength(0);
});

test("the ladder composes: enough fact rollups cascade into an identity rollup", () => {
  // identity threshold 1 means the single fact rollup fired this pass
  // immediately satisfies the rollup -> identity rung.
  const plan = planRollupLadder({
    factCount: 5,
    ledger: null,
    thresholds: { fact: 5, identity: 1 },
    runId: RUN_ID,
    linkCandidates: () => LINK_CANDIDATES,
  });
  expect(plan.entries).toHaveLength(2);
  expect(plan.entries.map((e) => e.tier)).toEqual([ROLLUP_TIER.fact, ROLLUP_TIER.rollup]);
  expect(plan.entries[1]!.produces).toBe(ROLLUP_TIER.identity);
  expect(plan.ledger.produced[ROLLUP_TIER.rollup]).toBe(1);
});

test("new facts beyond the last rollup re-arm the fact rung", () => {
  const first = planRollupLadder({
    factCount: 5,
    ledger: null,
    thresholds: THRESHOLDS,
    runId: RUN_ID,
    linkCandidates: () => LINK_CANDIDATES,
  });
  // Five more facts since the reset (10 total) crosses the threshold again.
  const second = planRollupLadder({
    factCount: 10,
    ledger: first.ledger,
    thresholds: THRESHOLDS,
    runId: RUN_ID,
    linkCandidates: () => LINK_CANDIDATES,
  });
  expect(second.fired).toBe(true);
  expect(second.entries[0]!.fromCount).toBe(5);
  expect(second.entries[0]!.toCount).toBe(10);
});

test("thresholds resolve from config, falling back to the named constants", () => {
  expect(resolveRollupThresholds(DEFAULT_BRAIN_CONFIG)).toEqual({
    fact: DEFAULT_FACT_ROLLUP_THRESHOLD,
    identity: DEFAULT_ROLLUP_IDENTITY_THRESHOLD,
  });
  const overridden = resolveRollupThresholds({
    ...DEFAULT_BRAIN_CONFIG,
    rollup: { fact_threshold: 3 },
  });
  expect(overridden.fact).toBe(3);
  expect(overridden.identity).toBe(DEFAULT_ROLLUP_IDENTITY_THRESHOLD);
});

test("the link-candidate manifest is not built when no rung fires", () => {
  // The manifest is a whole-vault walk - and, under owner-scope delivery, a
  // frontmatter read per note. Only a FIRED rung carries one, so a run that
  // fires nothing (the common case, and every dry run) must not pay for it.
  let built = 0;
  const plan = planRollupLadder({
    factCount: 4,
    ledger: null,
    thresholds: THRESHOLDS,
    runId: RUN_ID,
    linkCandidates: () => {
      built += 1;
      return LINK_CANDIDATES;
    },
  });
  expect(plan.fired).toBe(false);
  expect(built).toBe(0);
});

test("two rungs firing in one pass build the manifest once", () => {
  let built = 0;
  const plan = planRollupLadder({
    factCount: 5,
    ledger: null,
    thresholds: { fact: 5, identity: 1 },
    runId: RUN_ID,
    linkCandidates: () => {
      built += 1;
      return LINK_CANDIDATES;
    },
  });
  expect(plan.entries.length).toBeGreaterThan(1);
  expect(built).toBe(1);
  for (const entry of plan.entries) {
    expect(entry.envelope.link_candidates).toEqual(LINK_CANDIDATES);
  }
});