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.7 KB
Raw Blame
/**
 * C5 (t_a82b674e): caller-settable per-memory expiration on signals.
 *
 * A caller can stamp an explicit `expiration_date` on a signal at write
 * time. The default read/list path silently drops signals past their
 * date; an opt-in `showExpired` flag re-includes them for audit. An
 * expired-by-date signal is FILTERED on read, never deleted or moved.
 */

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

import { parseSignal, writeSignal, type WriteSignalInput } from "../../../src/core/brain/signal.ts";
import { queryByRuleTopic } from "../../../src/core/brain/query.ts";
import { filterExpired } from "../../../src/core/brain/expiration.ts";

let vault: string;

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

function baseSignal(slug: string, overrides: Partial<WriteSignalInput> = {}): WriteSignalInput {
  return {
    topic: "deploy",
    signal: "positive",
    agent: "tester",
    principle: `Principle for ${slug}`,
    created_at: "2026-05-01T00:00:00Z",
    date: "2026-05-01",
    slug,
    ...overrides,
  };
}

describe("writeSignal — expiration_date frontmatter", () => {
  test("stamps expiration_date into the frontmatter when supplied", () => {
    const res = writeSignal(vault, baseSignal("with-expiry", { expiration_date: "2026-07-15" }));
    const text = readFileSync(res.path, "utf8");
    expect(text).toContain("expiration_date: 2026-07-15");
  });

  test("does NOT emit expiration_date when the caller omits it (byte-identical)", () => {
    const res = writeSignal(vault, baseSignal("no-expiry"));
    const text = readFileSync(res.path, "utf8");
    expect(text).not.toContain("expiration_date");
  });

  test("parseSignal reads the expiration_date back", () => {
    const res = writeSignal(vault, baseSignal("roundtrip", { expiration_date: "2026-07-15" }));
    const parsed = parseSignal(res.path);
    expect(parsed.expiration_date).toBe("2026-07-15");
  });

  test("rejects an unparseable expiration_date on write", () => {
    expect(() => writeSignal(vault, baseSignal("bad", { expiration_date: "whenever" }))).toThrow();
  });
});

describe("queryByRuleTopic — default drops expired signals, showExpired re-includes", () => {
  const now = new Date("2026-08-01T00:00:00Z");

  function seed(): void {
    writeSignal(vault, baseSignal("live-sig", { expiration_date: "2026-12-31" }));
    writeSignal(vault, baseSignal("expired-sig", { expiration_date: "2026-07-15" }));
    writeSignal(vault, baseSignal("no-expiry-sig"));
  }

  test("default query drops signals past their expiration_date", () => {
    seed();
    const res = queryByRuleTopic(vault, "deploy", { now });
    const principles = res.signals.map((s) => s.principle).sort();
    // The expired one is dropped; the live one and the never-expiring one stay.
    expect(principles).toEqual(["Principle for live-sig", "Principle for no-expiry-sig"]);
  });

  test("showExpired: true re-includes the expired signal (audit path)", () => {
    seed();
    const res = queryByRuleTopic(vault, "deploy", { now, showExpired: true });
    expect(res.signals.length).toBe(3);
  });

  test("an expired signal is FILTERED, not deleted from the inbox", () => {
    const res = writeSignal(vault, baseSignal("expired-sig", { expiration_date: "2026-07-15" }));
    queryByRuleTopic(vault, "deploy", { now });
    expect(existsSync(res.path)).toBe(true);
  });

  test("an ambient-extracted signal stamped with a full-timestamp TTL expires at that instant", () => {
    // The ambient lane (Task 11) stamps `expiration_date = created + N` as a
    // full ISO timestamp through this same writer; the read side must drop it
    // at the exact instant, not at end of day.
    const res = writeSignal(
      vault,
      baseSignal("ambient-ttl", {
        source_type: "extracted",
        expiration_date: "2026-07-15T09:30:00Z",
      }),
    );
    const parsed = parseSignal(res.path);
    expect(parsed.source_type).toBe("extracted");
    expect(filterExpired([parsed], { now: new Date("2026-07-15T09:29:59Z") }).length).toBe(1);
    expect(filterExpired([parsed], { now: new Date("2026-07-15T09:30:01Z") }).length).toBe(0);
    const query = queryByRuleTopic(vault, "deploy", { now: new Date("2026-08-01T00:00:00Z") });
    expect(query.signals.length).toBe(0);
    expect(
      queryByRuleTopic(vault, "deploy", {
        now: new Date("2026-08-01T00:00:00Z"),
        showExpired: true,
      }).signals.length,
    ).toBe(1);
  });
});