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
4.5 KB
Raw Blame
/**
 * t_4b2bd8f7 follow-through: `brain_ingest_batch_plan`'s MCP payload surfaces
 * the planner's `ignoreWarnings` (a malformed pattern in the repository's OWN
 * ignore files) as structured fields, following the same "emit only when
 * non-empty" idiom `skipped_non_extractable` already uses. A clean tree must
 * serialize byte-identically to before: no key, not an empty array.
 */

import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";

import { bootstrapBrain } from "../../src/core/brain/init.ts";
import { atomicWriteFileSync } from "../../src/core/fs-atomic.ts";
import { SKIPPED_PAGE_REASON } from "../../src/core/brain/ingest/extractable-gate.ts";
import { INGEST_TOOLS } from "../../src/mcp/brain/ingest-tools.ts";
import type { ServerContext } from "../../src/mcp/tool-contract.ts";

let vault: string;
let configHome: string;
let ctx: ServerContext;

beforeEach(() => {
  vault = mkdtempSync(join(tmpdir(), "o2b-ingest-tool-warn-vault-"));
  configHome = mkdtempSync(join(tmpdir(), "o2b-ingest-tool-warn-cfg-"));
  const configPath = join(configHome, "config.yaml");
  atomicWriteFileSync(configPath, `vault: ${vault}\nagent_name: claude\n`);
  bootstrapBrain(vault, { configPath });
  ctx = { vault, configPath, repoRoot: null };
});

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

function write(rel: string, content = "hello\n"): void {
  const abs = join(vault, rel);
  mkdirSync(join(abs, ".."), { recursive: true });
  writeFileSync(abs, content, "utf8");
}

const batchPlan = INGEST_TOOLS.find((t) => t.name === "brain_ingest_batch_plan")!.handler;

describe("brain_ingest_batch_plan ignore_warnings (t_4b2bd8f7 follow-through)", () => {
  test("a malformed .gitignore pattern surfaces as a structured ignore_warnings entry", async () => {
    write("Docs/keep.md");
    write("Docs/.gitignore", "keep-nothing.md\n[unterminated\n");

    const res = (await batchPlan(ctx, { source_dir: "Docs" })) as Record<string, unknown>;

    expect(res["ignore_warnings"]).toEqual([
      {
        source: "Docs/.gitignore",
        line: 2,
        pattern: "[unterminated",
        reason: expect.stringContaining("unterminated"),
      },
    ]);
    // The plan still succeeds and the file is still discovered (nothing is
    // silently dropped by the malformed pattern).
    expect(res["total_files"]).toBe(1);
    expect(typeof res["plan_id"]).toBe("string");
  });

  test("a warning never changes the plan's planId versus the same tree without it", async () => {
    write("Docs/keep.md");
    const clean = (await batchPlan(ctx, { source_dir: "Docs" })) as Record<string, unknown>;

    write("Docs/.gitignore", "[unterminated\n");
    const withWarning = (await batchPlan(ctx, { source_dir: "Docs" })) as Record<string, unknown>;

    expect(withWarning["plan_id"]).toBe(clean["plan_id"]);
    expect(withWarning["total_files"]).toBe(1);
  });

  test("a clean tree produces a byte-identical payload: the key is absent, not empty", async () => {
    write("Docs/a.md");
    write("Docs/b.md");

    const res = (await batchPlan(ctx, { source_dir: "Docs" })) as Record<string, unknown>;

    expect("ignore_warnings" in res).toBe(false);
    expect(res).not.toHaveProperty("ignore_warnings");
  });

  test("skipped_non_extractable crosses the wire as the typed reason token plus detail (P4)", async () => {
    write("Docs/a.md", "---\nschema_type: paper\n---\nbody\n");
    write("Docs/b.md", "---\nschema_type: memo\n---\nbody\n");
    write(
      "Brain/_brain.yaml",
      "schema_version: 1\nschema:\n  page_types:\n    - paper\n    - memo\n  extractable:\n    - paper\n",
    );
    const res = (await batchPlan(ctx, { source_dir: "Docs" })) as Record<string, unknown>;

    expect(res["skipped_non_extractable"]).toEqual([
      { path: "Docs/b.md", reason: SKIPPED_PAGE_REASON.notExtractable, detail: "memo" },
    ]);
    expect(res["skip_reason_counts"]).toEqual({ [SKIPPED_PAGE_REASON.notExtractable]: 1 });
  });

  test("a plan with no skips carries no skip_reason_counts key (byte-identical when absent)", async () => {
    write("Docs/a.md", "---\nschema_type: memo\n---\nbody\n");

    const res = (await batchPlan(ctx, { source_dir: "Docs" })) as Record<string, unknown>;

    expect("skip_reason_counts" in res).toBe(false);
    expect("skipped_non_extractable" in res).toBe(false);
  });
});