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
3.1 KB
Raw Blame
/**
 * Opt-in session codec integration (Vault portability suite, Feature 1
 * Task 7). With `rawCodec`, writeSignal stores the raw body compressed
 * and stamps a `_raw_codec` marker; parseSignal expands it on read.
 * Without the flag (default) the body is verbatim and the read path is
 * byte-identical to pre-suite.
 */

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

import { writeSignal, parseSignal } from "../../src/core/brain/signal.ts";

let vault: string;
beforeEach(() => {
  vault = mkdtempSync(join(tmpdir(), "o2b-sig-codec-"));
  mkdirSync(join(vault, "Brain", "inbox"), { recursive: true });
});
afterEach(() => rmSync(vault, { recursive: true, force: true }));

// Whitespace-heavy body so the codec's body savings exceed the fixed
// `_raw_codec` marker-line overhead, making the on-disk file shorter.
const RAW =
  "Observed run:\n\n```bash\ngit status\n```\n\nThen a note." +
  "\n".repeat(60) +
  "Tail after a long blank run.";

function write(rawCodec: boolean): string {
  return writeSignal(vault, {
    topic: "obs",
    signal: "positive",
    agent: "claude",
    principle: "capture the observation",
    created_at: "2026-05-29T10:00:00Z",
    date: "2026-05-29",
    slug: "obs",
    raw: RAW,
    ...(rawCodec ? { rawCodec: true } : {}),
  }).path;
}

describe("signal raw codec", () => {
  test("with rawCodec: stamps the marker and round-trips the raw on read", () => {
    const path = write(true);
    const onDisk = readFileSync(path, "utf8");
    expect(onDisk).toContain("_raw_codec");
    expect(parseSignal(path).raw).toBe(RAW);
  });

  test("parseSignal reads the marker back beside the expanded raw", () => {
    // `raw` alone cannot tell a byte-level rewriter that the section on
    // disk is the compressed form; the parsed flag carries that fact.
    const compressed = parseSignal(write(true));
    expect(compressed.rawCodec).toBe(true);
    expect(compressed.raw).toBe(RAW);
    const verbatim = parseSignal(write(false));
    expect(verbatim.rawCodec).toBeUndefined();
    expect(verbatim.raw).toBe(RAW);
  });

  test("with rawCodec: the stored body is shorter than verbatim", () => {
    const compressed = readFileSync(write(true), "utf8").length;
    const verbatim = readFileSync(write(false), "utf8").length;
    expect(compressed).toBeLessThan(verbatim);
  });

  test("without rawCodec (default): no marker, raw stored verbatim", () => {
    const path = write(false);
    const onDisk = readFileSync(path, "utf8");
    expect(onDisk).not.toContain("_raw_codec");
    expect(onDisk).toContain("Tail after a long blank run.");
    expect(parseSignal(path).raw).toBe(RAW);
  });

  test("parseSignal fails fast on an unknown _raw_codec version", () => {
    const path = write(true);
    // Tamper the marker to an unsupported codec version.
    const tampered = readFileSync(path, "utf8").replace(/_raw_codec: .*/u, "_raw_codec: 999");
    writeFileSync(path, tampered, "utf8");
    expect(() => parseSignal(path)).toThrow(/_raw_codec/);
  });
});