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
2.1 KB
Raw Blame
import { existsSync, mkdirSync, readFileSync } from "node:fs";
import { dirname } from "node:path";

import { atomicWriteFileSync } from "../fs-atomic.ts";
import { BRAIN_ROOT_REL } from "./paths.ts";
import { assertVaultIdentityForWrite } from "./vault-identity.ts";
import { machineStatePath } from "../state/machine-state.ts";

export interface ClaudeMemoryManifestEntry {
  readonly pref_id: string;
  readonly sha256: string;
  readonly imported_at: string;
}

export interface ClaudeMemoryManifest {
  readonly version: 1;
  readonly imports: Readonly<Record<string, ClaudeMemoryManifestEntry>>;
}

/** The manifest: in the vault, or in the device-local state once switched (D14). */
function manifestPath(vault: string): string {
  return machineStatePath(vault, BRAIN_ROOT_REL, ".imports", "claude-memory.json");
}

/**
 * Load the import-history sidecar. Treats a missing file as "empty
 * manifest" — first-time imports rely on this. Type-validates the
 * parsed `imports` field defensively: a malformed file (hand-edited
 * to non-object, truncated, etc.) returns the empty shape rather than
 * propagating an unknown blob into the rest of the import pipeline.
 */
export function loadManifest(vault: string): ClaudeMemoryManifest {
  const p = manifestPath(vault);
  if (!existsSync(p)) return { version: 1, imports: {} };
  let raw: unknown;
  try {
    raw = JSON.parse(readFileSync(p, "utf8"));
  } catch {
    return { version: 1, imports: {} };
  }
  if (
    raw === null ||
    typeof raw !== "object" ||
    typeof (raw as { imports?: unknown }).imports !== "object" ||
    (raw as { imports?: unknown }).imports === null ||
    Array.isArray((raw as { imports?: unknown }).imports)
  ) {
    return { version: 1, imports: {} };
  }
  return {
    version: 1,
    imports: (raw as { imports: Record<string, ClaudeMemoryManifestEntry> }).imports,
  };
}

export function saveManifest(vault: string, m: ClaudeMemoryManifest): void {
  // Vault-identity write guard (context-integrity-gates, Unit J).
  assertVaultIdentityForWrite(vault);
  const p = manifestPath(vault);
  mkdirSync(dirname(p), { recursive: true });
  atomicWriteFileSync(p, JSON.stringify(m, null, 2) + "\n");
}