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
/**
* `budgetActiveBody` (token-diet, t_40eb1de7 part 2): fits the
* rendered active.md body into the SessionStart injection budget,
* dropping sections in fixed priority order (retired first, confirmed
* last) and pointing the agent at `brain_context` when truncated.
*/
import { describe, expect, test } from "bun:test";
import { budgetActiveBody } from "../../../src/core/brain/active-budget.ts";
function body(): string {
return [
"# Active Brain Preferences",
"",
"Auto-generated by `dream`. Do not edit β changes will be overwritten.",
"",
"## Confirmed (2)",
"",
"- `pref-a` (confidence: high (0.95)) β Rule A",
"- `pref-b` (confidence: medium (0.50)) β Rule B",
"",
"## Most-applied (30d) (1)",
"",
"- `pref-a` (applied_in_window: 4)",
"",
"## Quarantine (1)",
"",
"_Probationary rules β still active._",
"",
"- `pref-q` (applied: 0 / violated: 2) β Quarantined rule",
"",
"## Recently retired (last 1)",
"",
"- `pref-r` β low_confidence on 2026-05-01",
].join("\n");
}
describe("budgetActiveBody", () => {
test("body within budget passes through byte-identical", () => {
const input = body();
const out = budgetActiveBody(input, 10_000);
expect(out).toBe(input);
});
test("over budget: retired drops first, then quarantine, then most-applied", () => {
const input = body();
// Budget that can hold everything except the retired tail.
const out = budgetActiveBody(input, input.length - 20);
expect(out).toContain("## Confirmed");
expect(out).not.toContain("## Recently retired");
expect(out).toContain("brain_context");
});
test("tight budget keeps the confirmed head and points at brain_context", () => {
const out = budgetActiveBody(body(), 250);
expect(out).toContain("# Active Brain Preferences");
expect(out).toContain("## Confirmed");
expect(out).not.toContain("## Quarantine");
expect(out).not.toContain("## Recently retired");
expect(out).toContain("brain_context");
});
test("deterministic", () => {
expect(budgetActiveBody(body(), 700)).toBe(budgetActiveBody(body(), 700));
});
test("unsectioned body still respects the budget", () => {
const flat = Array.from({ length: 50 }, (_, i) => `line ${i}`).join("\n");
const out = budgetActiveBody(flat, 120);
expect(out.length).toBeLessThanOrEqual(120 + 300); // content budget + notice overhead
expect(out).toContain("line 0");
});
});
/**
* The truncation notice used to be a fixed sentence that named nothing,
* while the budgeter one line above it returned the exact list of keys
* it had dropped. The agent was told a rule had gone and not which one -
* the same silence this release is about. Section headings are text this
* project authors, never operator text, so naming them is safe.
*/
describe("budgetActiveBody: the truncation notice names what it dropped", () => {
test("an over-budget body names the dropped headings", () => {
const input = body();
const out = budgetActiveBody(input, input.length - 20);
expect(out).toContain("Recently retired");
// Named as dropped, not merely present: the heading itself is gone.
expect(out).not.toContain("## Recently retired");
expect(out).not.toContain("pref-r");
});
test("a tighter budget names every dropped heading in drop order", () => {
const out = budgetActiveBody(body(), 250);
const notice = out.slice(out.lastIndexOf("_Injection truncated"));
const retired = notice.indexOf("Recently retired");
const quarantine = notice.indexOf("Quarantine");
const mostApplied = notice.indexOf("Most-applied");
expect(retired).toBeGreaterThanOrEqual(0);
expect(quarantine).toBeGreaterThan(retired);
expect(mostApplied).toBeGreaterThan(quarantine);
});
test("the notice quantifies the cut in characters", () => {
const input = body();
const out = budgetActiveBody(input, 250);
expect(out).toContain(String(input.length));
});
test("a within-budget body is byte-identical to its input", () => {
// The idempotent-write comparison upstream depends on this, so the
// richer notice must stay strictly on the truncated path.
const input = body();
expect(budgetActiveBody(input, input.length)).toBe(input);
expect(budgetActiveBody(input, 10_000)).toBe(input);
});
test("a body with no dropped sections still points at the full view", () => {
const flat = Array.from({ length: 50 }, (_, i) => `line ${i}`).join("\n");
const out = budgetActiveBody(flat, 120);
expect(out).toContain("brain_context");
});
});
// ----- Headline tier ladder (context-injection-pipeline, C2) -----------------
/**
* A body whose Most-applied and Quarantine sections hold more bullets
* than the tier keeps: the ladder must compact both to their top three
* ranked bullets (the named-constant default) before any whole-section
* drop, ranked by the inline tags (`applied_in_window` descending;
* `applied` descending then `violated` ascending) with render order
* restored among the survivors.
*/
function tierableBody(): string {
return [
"# Active Brain Preferences",
"",
"Auto-generated by `dream`. Do not edit β changes will be overwritten.",
"",
"## Most-applied (30d) (5)",
"",
"- `m1` (applied_in_window: 3)",
"- `m2` (applied_in_window: 9)",
"- `m3` (applied_in_window: 6)",
"- `m4` (applied_in_window: 1)",
"- `m5` (applied_in_window: 8)",
"",
"## Quarantine (5)",
"",
"_Probationary rules β still active._",
"",
"- `q1` (applied: 0 / violated: 4) β one",
"- `q2` (applied: 3 / violated: 0) β two",
"- `q3` (applied: 1 / violated: 1) β three",
"- `q4` (applied: 5 / violated: 2) β four",
"- `q5` (applied: 2 / violated: 0) β five",
].join("\n");
}
/** The exact tiered join the ladder must produce for {@link tierableBody}. */
function expectedTieredJoin(): string {
return [
[
"# Active Brain Preferences",
"",
"Auto-generated by `dream`. Do not edit β changes will be overwritten.",
"",
"## Most-applied (30d) (5)",
"",
"- `m2` (applied_in_window: 9)",
"- `m3` (applied_in_window: 6)",
"- `m5` (applied_in_window: 8)",
].join("\n"),
[
"## Quarantine (5)",
"",
"_Probationary rules β still active._",
"",
"- `q2` (applied: 3 / violated: 0) β two",
"- `q4` (applied: 5 / violated: 2) β four",
"- `q5` (applied: 2 / violated: 0) β five",
].join("\n"),
].join("\n\n");
}
/**
* The external review's own scenario: a keep-guard Confirmed section, a
* high-value Most-applied section holding ten ranked bullets, a Recently
* retired section holding four, and a body exactly five characters over
* its budget - the smallest overflow tiering could be asked to close.
*/
function reviewScenarioBody(): string {
const mostApplied = Array.from(
{ length: 10 },
(_, i) => `- \`m${i}\` (applied_in_window: ${20 - i})`,
).join("\n");
const retired = [
"- `r0` β low_confidence on 2026-05-01",
"- `r1` β low_confidence on 2026-05-02",
"- `r2` β low_confidence on 2026-05-03",
"- `r3` β low_confidence on 2026-05-04",
].join("\n");
return [
"# Active Brain Preferences",
"",
"Auto-generated by `dream`. Do not edit β changes will be overwritten.",
"",
"## Confirmed (1)",
"",
"- `c0` (confidence: high (0.95)) β Rule C",
"",
"## Most-applied (30d) (10)",
"",
mostApplied,
"",
"## Recently retired (last 4)",
"",
retired,
].join("\n");
}
describe("budgetActiveBody: the headline tier ladder", () => {
const TIER_NOTICE =
// Labels are the full headings, exactly as the drop notice names them.
"_Headlines kept to fit the injection budget: Most-applied (30d) (5), Quarantine (5). " +
"Call `brain_context` (or run `o2b brain export --format llms-txt`) for the full preference set._";
test("five chars over budget: the least important section tiers first and the high-value one survives whole", () => {
const input = reviewScenarioBody();
const out = budgetActiveBody(input, input.length - 5);
// Tiering the retired section (its fourth bullet goes) frees far more
// than the five overflowing characters, so the ladder stops there and
// the ten-bullet Most-applied section survives intact - where an
// all-at-once ladder would have compacted it to three bullets while
// the low-value retired section kept its headlines.
expect(out).toContain("## Most-applied (30d) (10)");
expect(out.split("(applied_in_window:").length - 1).toBe(10);
// The retired section is tiered to its top three bullets.
expect(out.split("low_confidence on").length - 1).toBe(3);
expect(out).not.toContain("`r3`");
// The tier notice names the one section actually compacted.
// The cut count does not fit beside the tiered body: the plain notice rides.
expect(out).toContain("Headlines kept to fit the injection budget: Recently retired (last 4).");
expect(out).not.toContain("Injection truncated");
});
test("with room beside the tiered body the tier-alone notice counts the rule lines cut", () => {
const retired = Array.from(
{ length: 20 },
(_, i) => `- \`r${i}\` β low_confidence on 2026-05-${String(i + 1).padStart(2, "0")}`,
).join("\n");
const input = [
"# Active Brain Preferences",
"",
"## Confirmed (1)",
"",
"- `c0` (confidence: high (0.95)) β Rule C",
"",
"## Recently retired (last 20)",
"",
retired,
].join("\n");
const budget = input.length - 200;
const out = budgetActiveBody(input, budget);
expect(out.split("low_confidence on").length - 1).toBe(3);
expect(out).toContain(
"_Headlines kept to fit the injection budget: Recently retired (last 20); 17 rule lines cut.",
);
expect(out.length).toBeLessThanOrEqual(budget);
expect(out).not.toContain("Injection truncated");
});
test("within budget a body with tierable sections passes through byte-identical", () => {
const input = tierableBody();
expect(budgetActiveBody(input, 100_000)).toBe(input);
});
test("over budget a tiered section keeps heading, lead-ins and exactly the top three ranked bullets", () => {
const tiered = expectedTieredJoin();
const out = budgetActiveBody(tierableBody(), tiered.length);
expect(out).toContain(tiered);
expect(out).not.toContain("- `m1` (applied_in_window: 3)");
expect(out).not.toContain("- `m4` (applied_in_window: 1)");
expect(out).not.toContain("- `q1` (applied: 0 / violated: 4) β one");
expect(out).not.toContain("- `q3` (applied: 1 / violated: 1) β three");
// Exactly the top three survive in each tiered section.
expect(out.split("(applied_in_window:").length - 1).toBe(3);
expect(out.split("(applied:").length - 1).toBe(3);
});
test("tiering alone fits the budget: the tier notice rides alone, no drop notice", () => {
const tiered = expectedTieredJoin();
const out = budgetActiveBody(tierableBody(), tiered.length);
expect(out).toBe(`${tiered}\n\n${TIER_NOTICE}`);
expect(out).not.toContain("Injection truncated");
expect(out).toContain("brain_context");
});
test("drops still occur in today's order, and the notice names the headlines kept", () => {
const tiered = expectedTieredJoin();
const out = budgetActiveBody(tierableBody(), tiered.length - 10);
expect(out).toContain("Injection truncated to budget");
expect(out).toContain("Dropped 1 section (Quarantine (5))");
expect(out).toContain("Headlines kept in: Most-applied (30d)");
expect(out).not.toContain("## Quarantine");
expect(out).toContain("- `m2` (applied_in_window: 9)");
expect(out.split("(applied_in_window:").length - 1).toBe(3);
// The kept-headlines clause names SURVIVORS only: Quarantine was
// tiered and then dropped whole, so it is already named in the
// Dropped list and must not be named here too.
const clauseStart = out.indexOf("_Headlines kept in:");
expect(clauseStart).toBeGreaterThanOrEqual(0);
const clause = out.slice(clauseStart, out.indexOf("._", clauseStart) + 2);
// Its count is of the surviving sections only: the two Most-applied lines.
expect(clause).toBe("_Headlines kept in: Most-applied (30d) (5); 2 rule lines cut._");
// The total of both tiered sections rides the drop notice.
expect(out).toContain("Dropped 1 section (Quarantine (5)); 4 rule lines cut.");
expect(clause).not.toContain("Quarantine");
});
test("after tiering, the drop notice quantifies the cut against the ORIGINAL body size", () => {
const input = tierableBody();
const tiered = expectedTieredJoin();
// Below the tiered body: the ladder tiers both sections, the body
// still overflows, and the drop pass removes Quarantine. The notice
// is built from the TIERED sections, so its total must be the
// original join - quoting the tiered size would claim fewer lost
// characters than the vault actually held.
const out = budgetActiveBody(input, tiered.length - 10);
const notice = out.slice(out.lastIndexOf("_Injection truncated"));
const m = /kept (\d+) of (\d+) characters/.exec(notice);
expect(m).not.toBeNull();
expect(m![2]).toBe(String(input.length));
expect(Number(m![1])).toBeLessThan(input.length);
});
test("a section tiered and then dropped whole is named only in the Dropped list", () => {
// Most-applied holds too few bullets to tier; Quarantine holds five
// and a budget just below the tiered body drops it whole. The notice
// must not name Quarantine in BOTH the Dropped list and the
// kept-headlines clause.
const quarantine = [
"## Quarantine (5)",
"",
"_Probationary rules β still active._",
"",
"- `q1` (applied: 0 / violated: 4) β one",
"- `q2` (applied: 3 / violated: 0) β two",
"- `q3` (applied: 1 / violated: 1) β three",
"- `q4` (applied: 5 / violated: 2) β four",
"- `q5` (applied: 2 / violated: 0) β five",
].join("\n");
const tiered = [
"## Quarantine (5)",
"",
"_Probationary rules β still active._",
"",
"- `q2` (applied: 3 / violated: 0) β two",
"- `q4` (applied: 5 / violated: 2) β four",
"- `q5` (applied: 2 / violated: 0) β five",
].join("\n");
const head = [
"# Active Brain Preferences",
"",
"Auto-generated by `dream`. Do not edit β changes will be overwritten.",
"",
"## Confirmed (1)",
"",
"- `pref-a` (confidence: high (0.95)) β Rule A",
"",
"## Most-applied (30d) (1)",
"",
"- `pref-a` (applied_in_window: 4)",
].join("\n");
const input = `${head}\n\n${quarantine}`;
// Below the full body (the tier fires) and below the tiered body
// (Quarantine still drops), above the body without Quarantine
// (everything else survives intact).
const budget = input.length - (quarantine.length - tiered.length) - 1;
const out = budgetActiveBody(input, budget);
expect(out).toContain("Dropped 1 section (Quarantine (5))");
expect(out).toContain("## Most-applied");
expect(out).not.toContain("Headlines kept in: Quarantine");
expect(out).not.toContain("Headlines kept in");
});
test("keep-guard sections are exempt: an oversized Confirmed section stays whole", () => {
// Five confirmed rules share the preamble's keep-guard priority: the
// ladder must not touch them even though they exceed the tier size,
// while the five-bullet Most-applied section right below is compacted.
const confirmedBullets = Array.from(
{ length: 5 },
(_, i) => `- \`c${i}\` (confidence: high (0.9${i})) β Confirmed rule number ${i}`,
).join("\n");
const mostAppliedBullets = Array.from(
{ length: 5 },
(_, i) => `- \`gm${i}\` (applied_in_window: ${10 - i})`,
).join("\n");
const input = [
"# Active Brain Preferences",
"",
"Auto-generated by `dream`. Do not edit β changes will be overwritten.",
"",
"## Confirmed (5)",
"",
confirmedBullets,
"",
"## Most-applied (30d) (5)",
"",
mostAppliedBullets,
].join("\n");
const tieredTotal = [
[
"# Active Brain Preferences",
"",
"Auto-generated by `dream`. Do not edit β changes will be overwritten.",
"",
"## Confirmed (5)",
"",
confirmedBullets,
].join("\n"),
[
"## Most-applied (30d) (5)",
"",
"- `gm0` (applied_in_window: 10)",
"- `gm1` (applied_in_window: 9)",
"- `gm2` (applied_in_window: 8)",
].join("\n"),
].join("\n\n");
const out = budgetActiveBody(input, tieredTotal.length);
// All five confirmed rules survive byte for byte.
expect(out).toContain(confirmedBullets);
// The non-guard section was tiered to exactly its top three.
expect(out).toContain("- `gm0` (applied_in_window: 10)");
expect(out).not.toContain("- `gm3` (applied_in_window: 7)");
expect(out.split("(applied_in_window:").length - 1).toBe(3);
expect(out).toContain("Headlines kept to fit the injection budget");
expect(out).not.toContain("Injection truncated");
});
test("a headlines-only remnant that still overflows drops through the headLines guard", () => {
const bullets = Array.from(
{ length: 10 },
(_, i) =>
`- \`n${i}\` β filler note line number ${i} carrying some prose so the section is long`,
).join("\n");
const input = ["## Notes", "", "_A side notebook of observations._", "", bullets].join("\n");
// A budget the tiered section cannot meet even after the ladder:
// trimming may only leave the heading and its lead-in, which the
// guard must turn into a whole-section drop.
const out = budgetActiveBody(input, 100);
expect(out).not.toContain("## Notes");
expect(out).not.toContain("_A side notebook of observations._");
expect(out).not.toContain("`n0`");
expect(out).toContain("Injection truncated to budget");
expect(out).toContain("Notes");
// The section's only tier is the Dropped list: a clause naming
// headlines "kept" in a section that is gone contradicts the notice.
expect(out).not.toContain("Headlines kept in");
});
test("deterministic: identical inputs produce identical tiered output", () => {
const tiered = expectedTieredJoin();
expect(budgetActiveBody(tierableBody(), tiered.length)).toBe(
budgetActiveBody(tierableBody(), tiered.length),
);
expect(budgetActiveBody(tierableBody(), tiered.length - 10)).toBe(
budgetActiveBody(tierableBody(), tiered.length - 10),
);
});
});