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
7.1 KB
Raw Blame
/**
 * `active.{most_applied_window_days, most_applied_limit}` block (v0.10.11).
 */

import { describe, expect, test } from "bun:test";
import { parseBrainYaml } from "../../../src/core/brain/yaml-parse.ts";
import {
  BrainConfigError,
  resolveScopedRulesMaxChars,
  validateBrainConfigDetailed,
} from "../../../src/core/brain/policy.ts";

function validate(yaml: string) {
  return validateBrainConfigDetailed(parseBrainYaml(yaml), "<test>");
}

const HEAD = `schema_version: 1\n`;

describe("active.most_applied_* config block", () => {
  test("absent block β†’ cfg.active is undefined; consumers use defaults", () => {
    const { config } = validate(HEAD);
    expect(config.active).toBeUndefined();
  });

  test("present with values β†’ loaded into BrainMostAppliedConfig", () => {
    const { config } = validate(
      HEAD + `active:\n` + `  most_applied_window_days: 7\n` + `  most_applied_limit: 3\n`,
    );
    expect(config.active?.most_applied?.window_days).toBe(7);
    expect(config.active?.most_applied?.limit).toBe(3);
  });

  test("empty active block (no most_applied keys) is permitted", () => {
    const { config } = validate(HEAD + `active:\n  __reserved: 1\n`);
    // unknown sub-key warns, but the block is otherwise valid
    expect(config.active).toEqual({});
  });

  test("window_days: 0 rejected", () => {
    expect(() =>
      validate(HEAD + `active:\n  most_applied_window_days: 0\n  most_applied_limit: 5\n`),
    ).toThrow(BrainConfigError);
  });

  test("window_days: 366 rejected", () => {
    expect(() =>
      validate(HEAD + `active:\n  most_applied_window_days: 366\n  most_applied_limit: 5\n`),
    ).toThrow(BrainConfigError);
  });

  test("limit: 0 rejected", () => {
    expect(() =>
      validate(HEAD + `active:\n  most_applied_window_days: 30\n  most_applied_limit: 0\n`),
    ).toThrow(BrainConfigError);
  });

  test("limit: 51 rejected", () => {
    expect(() =>
      validate(HEAD + `active:\n  most_applied_window_days: 30\n  most_applied_limit: 51\n`),
    ).toThrow(BrainConfigError);
  });

  test("non-integer window_days rejected", () => {
    expect(() =>
      validate(HEAD + `active:\n  most_applied_window_days: 30.5\n  most_applied_limit: 10\n`),
    ).toThrow(BrainConfigError);
  });

  test("string window_days rejected", () => {
    expect(() => validate(HEAD + `active:\n  most_applied_window_days: "thirty"\n`)).toThrow(
      BrainConfigError,
    );
  });

  test("only window_days set β†’ limit defaults to 10", () => {
    const { config } = validate(HEAD + `active:\n  most_applied_window_days: 14\n`);
    expect(config.active?.most_applied?.window_days).toBe(14);
    expect(config.active?.most_applied?.limit).toBe(10);
  });

  test("only limit set β†’ window_days defaults to 30", () => {
    const { config } = validate(HEAD + `active:\n  most_applied_limit: 3\n`);
    expect(config.active?.most_applied?.window_days).toBe(30);
    expect(config.active?.most_applied?.limit).toBe(3);
  });

  test("unknown sub-key under active: emits warning, doesn't throw", () => {
    const { warnings } = validate(
      HEAD + `active:\n  most_applied_window_days: 30\n  future_thing: 1\n`,
    );
    const found = warnings.find((w) => w.message.includes("future_thing"));
    expect(found).toBeDefined();
  });

  test("non-object active value is a hard error", () => {
    expect(() => validate(HEAD + `active: not-a-map\n`)).toThrow(BrainConfigError);
  });
});

describe("active.inject_budget_chars (token-diet, t_40eb1de7)", () => {
  test("absent β†’ undefined; consumers fall back to the default budget", () => {
    const { config } = validate(HEAD + `active:\n  most_applied_limit: 5\n`);
    expect(config.active?.inject_budget_chars).toBeUndefined();
  });

  test("present β†’ loaded", () => {
    const { config } = validate(HEAD + `active:\n  inject_budget_chars: 4000\n`);
    expect(config.active?.inject_budget_chars).toBe(4000);
  });

  test("below minimum rejected", () => {
    expect(() => validate(HEAD + `active:\n  inject_budget_chars: 100\n`)).toThrow(
      BrainConfigError,
    );
  });

  test("non-integer rejected", () => {
    expect(() => validate(HEAD + `active:\n  inject_budget_chars: many\n`)).toThrow(
      BrainConfigError,
    );
  });
});

describe("active.standing_rules_max_chars (silence-is-not-an-answer, U8)", () => {
  test("absent β†’ undefined; the reader falls back to its own default", () => {
    const { config } = validate(HEAD + `active:\n  inject_budget_chars: 4000\n`);
    expect(config.active?.standing_rules_max_chars).toBeUndefined();
  });

  test("present β†’ loaded, and independent of the injection budget", () => {
    const { config } = validate(
      HEAD + `active:\n  inject_budget_chars: 500\n  standing_rules_max_chars: 12000\n`,
    );
    expect(config.active?.standing_rules_max_chars).toBe(12_000);
    expect(config.active?.inject_budget_chars).toBe(500);
  });

  test("below minimum rejected", () => {
    expect(() => validate(HEAD + `active:\n  standing_rules_max_chars: 10\n`)).toThrow(
      BrainConfigError,
    );
  });

  test("above maximum rejected", () => {
    expect(() => validate(HEAD + `active:\n  standing_rules_max_chars: 1000000\n`)).toThrow(
      BrainConfigError,
    );
  });

  test("non-integer rejected", () => {
    expect(() => validate(HEAD + `active:\n  standing_rules_max_chars: lots\n`)).toThrow(
      BrainConfigError,
    );
  });

  test("the key is known: it does not warn as unrecognised", () => {
    const { warnings } = validate(HEAD + `active:\n  standing_rules_max_chars: 4000\n`);
    expect(warnings.find((w) => w.message.includes("standing_rules_max_chars"))).toBeUndefined();
  });
});

describe("active.scoped_rules_max_chars (scoped operator rules)", () => {
  test("absent β†’ undefined, and the resolver answers the default", () => {
    const { config } = validate(HEAD + `active:\n  inject_budget_chars: 4000\n`);
    expect(config.active?.scoped_rules_max_chars).toBeUndefined();
    expect(resolveScopedRulesMaxChars(config)).toBe(2000);
    expect(resolveScopedRulesMaxChars(null)).toBe(2000);
  });

  test("present β†’ loaded and resolved", () => {
    const { config } = validate(HEAD + `active:\n  scoped_rules_max_chars: 500\n`);
    expect(config.active?.scoped_rules_max_chars).toBe(500);
    expect(resolveScopedRulesMaxChars(config)).toBe(500);
  });

  test("below minimum rejected with the range message naming the key", () => {
    expect(() => validate(HEAD + `active:\n  scoped_rules_max_chars: 150\n`)).toThrow(
      /active\.scoped_rules_max_chars/,
    );
    expect(() => validate(HEAD + `active:\n  scoped_rules_max_chars: 150\n`)).toThrow(
      BrainConfigError,
    );
  });

  test("above maximum and non-integer rejected", () => {
    expect(() => validate(HEAD + `active:\n  scoped_rules_max_chars: 1000000\n`)).toThrow(
      BrainConfigError,
    );
    expect(() => validate(HEAD + `active:\n  scoped_rules_max_chars: lots\n`)).toThrow(
      BrainConfigError,
    );
  });

  test("the key is known: it does not warn as unrecognised", () => {
    const { warnings } = validate(HEAD + `active:\n  scoped_rules_max_chars: 2000\n`);
    expect(warnings.find((w) => w.message.includes("scoped_rules_max_chars"))).toBeUndefined();
  });
});