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
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
appendAuditRecord,
AUDIT_WEEK_SHARD_GRAMMAR,
isoWeekLabel,
} from "../../src/core/reliability/audit.ts";
import {
JSONL_LEDGER_EXT,
parseShardedName,
shardedFileName,
} from "../../src/core/brain/ledger-shards.ts";
import { withFileLock } from "../../src/core/reliability/lock.ts";
import { buildProbeReport } from "../../src/core/reliability/probe.ts";
import { fakeCredential } from "../helpers/fake-credentials.ts";
import { withDeviceId, useUnshardedLegacyLog } from "../helpers/device-id.ts";
// The legacy day log and ledgers are read under their un-sharded names.
useUnshardedLegacyLog();
const RESEARCH_TYPE = "research";
const LOGGED_KEY = fakeCredential("secret-", "value");
let tmp: string;
beforeEach(() => {
tmp = join(tmpdir(), `o2b-reliability-${crypto.randomUUID()}`);
mkdirSync(tmp, { recursive: true });
});
afterEach(() => {
rmSync(tmp, { recursive: true, force: true });
});
describe("withFileLock", () => {
test("serializes access to a lock target and releases after callback", async () => {
const target = join(tmp, "Brain", "_brain.yaml");
mkdirSync(join(tmp, "Brain"), { recursive: true });
writeFileSync(target, "schema_version: 1\n", "utf8");
const events: string[] = [];
let releaseFirst!: () => void;
let markFirstStarted!: () => void;
const firstCanRelease = new Promise<void>((resolve) => {
releaseFirst = resolve;
});
const firstStarted = new Promise<void>((resolve) => {
markFirstStarted = resolve;
});
const first = withFileLock(target, { staleMs: 1_000, retries: 0 }, async () => {
events.push("first");
markFirstStarted();
await firstCanRelease;
});
await firstStarted;
const second = withFileLock(
target,
{ staleMs: 1_000, retries: 20, retryDelayMs: 10 },
async () => {
events.push("second");
},
);
await Promise.resolve();
expect(events).toEqual(["first"]);
releaseFirst();
await Promise.all([first, second]);
expect(events).toEqual(["first", "second"]);
});
test("does not create the target file while acquiring a lock", async () => {
const target = join(tmp, "Brain", "_brain.yaml");
await withFileLock(target, { staleMs: 1_000, retries: 0 }, () => {
expect(existsSync(target)).toBe(false);
});
expect(existsSync(target)).toBe(false);
});
});
describe("appendAuditRecord", () => {
test("appends redacted ISO-week JSONL audit records", () => {
const auditRoot = join(tmp, "Brain", "log", "schema-mutations");
const path = appendAuditRecord(auditRoot, {
timestamp: "2026-05-30T12:00:00.000Z",
actor: "tester",
action: "schema_apply_mutations",
target: "Brain/_brain.yaml",
ok: true,
details: { token: RESEARCH_TYPE, api_key: LOGGED_KEY },
});
expect(path.endsWith("2026-W22.jsonl")).toBe(true);
const lines = readFileSync(path, "utf8").trim().split("\n");
expect(lines).toHaveLength(1);
expect(lines[0]).toContain("schema_apply_mutations");
expect(lines[0]).toContain("***REDACTED***");
expect(lines[0]).not.toContain(LOGGED_KEY);
});
test("the device id shards the week file name (t_774dea61)", () => {
const auditRoot = join(tmp, "Brain", "log", "device-sharded");
const record = {
timestamp: "2026-05-30T12:00:00.000Z",
actor: "tester",
action: "probe",
target: "Brain/x",
ok: true,
};
const onA = withDeviceId("a", () =>
appendAuditRecord(auditRoot, { ...record, action: "from-a" }),
);
const onB = withDeviceId("b", () =>
appendAuditRecord(auditRoot, { ...record, action: "from-b" }),
);
expect(onA.endsWith("2026-W22.a.jsonl")).toBe(true);
expect(onB.endsWith("2026-W22.b.jsonl")).toBe(true);
expect(existsSync(join(auditRoot, "2026-W22.a.jsonl"))).toBe(true);
expect(existsSync(join(auditRoot, "2026-W22.b.jsonl"))).toBe(true);
expect(existsSync(join(auditRoot, "2026-W22.jsonl"))).toBe(false);
// The empty device id is the legacy un-sharded file: names and bytes
// unchanged, no migration.
const legacy = withDeviceId("", () =>
appendAuditRecord(auditRoot, { ...record, action: "legacy" }),
);
expect(legacy.endsWith("2026-W22.jsonl")).toBe(true);
expect(existsSync(join(auditRoot, "2026-W22.jsonl"))).toBe(true);
});
test("rejects invalid timestamps before week bucketing", () => {
expect(() =>
appendAuditRecord(join(tmp, "Brain", "log", "schema-mutations"), {
timestamp: "not-a-date",
actor: "tester",
action: "schema_apply_mutations",
target: "Brain/_brain.yaml",
ok: true,
}),
).toThrow("invalid audit timestamp");
});
});
describe("buildProbeReport", () => {
test("summarizes probe checks with stable severity counts", () => {
const report = buildProbeReport([
{ name: "brain_root", status: "ok", message: "present" },
{
name: "search_index",
status: "warning",
message: "missing",
remediation: "run o2b search index",
},
{
name: "snapshot_restore",
status: "critical",
message: "drift detected",
},
]);
expect(report.ok).toBe(false);
expect(report.counts).toEqual({ ok: 1, warning: 1, critical: 1 });
expect(report.checks.map((check) => check.name)).toEqual([
"brain_root",
"search_index",
"snapshot_restore",
]);
});
});
describe("audit week shard grammar", () => {
test("the reader grammar parses every name the writer composes, and nothing else", () => {
const week = isoWeekLabel(new Date("2026-01-01T12:00:00Z"));
for (const shardId of ["", "laptop-01"]) {
const name = shardedFileName(week, shardId, JSONL_LEDGER_EXT);
expect(parseShardedName(name, AUDIT_WEEK_SHARD_GRAMMAR)).toMatchObject({
base: week,
shardId,
});
}
expect(parseShardedName(`${week}.md`, AUDIT_WEEK_SHARD_GRAMMAR)).toBeNull();
expect(parseShardedName("2026-01.jsonl", AUDIT_WEEK_SHARD_GRAMMAR)).toBeNull();
});
});