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.6 KB
Raw Blame
/**
 * A preference page's frontmatter rebuilt from the store's preference
 * fold (task 2.10). A leaf of the preference writer and parser
 * (`preference.ts`), so the readers that walk the fold's pages
 * (`memory-pages.ts`) reach it without importing the writer:
 *
 *  - {@link DERIVED_FIELDS}, the fields a page spells with a `_` prefix;
 *  - {@link storeEntryMeta}, the frontmatter the page of one fold entry
 *    would carry, typed as the legacy parser types it.
 */

import { countersAtReach, type StorePreference } from "../store/read/store-entities.ts";
import type { FrontmatterMap } from "../types.ts";
import { formatFrontmatter, parseFrontmatterText } from "../vault.ts";
import { isRetiredEntry } from "./preference-store.ts";

/**
 * Group C — derived fields that get a `_` prefix in the on-disk
 * shape (dream-rewritten state, see §24). Parser accepts both
 * `name` and `_name`; writer always emits `_name`. Listed once so
 * both `parsePreference`, `parseRetired`, and the migration helper
 * share the same list — a follow-up adding a derived field updates
 * exactly this constant.
 */
export const DERIVED_FIELDS: ReadonlyArray<string> = Object.freeze([
  "status",
  "confirmed_at",
  "last_evidence_at",
  "applied_count",
  "violated_count",
  "confidence",
  "confidence_value",
  "evidenced_by",
  "contradicted_by",
  "lifecycle",
  "revision",
  "content_hash",
]);

const DERIVED_FIELD_SET: ReadonlySet<string> = new Set(DERIVED_FIELDS);

/**
 * `storePreferenceMeta` (`preference.ts`) for an entry the caller already holds, so a
 * reader walking every preference of the fold reads the store once.
 */
export function storeEntryMeta(
  entry: StorePreference,
  slug: string,
): { readonly meta: FrontmatterMap; readonly retired: boolean } {
  const retired = isRetiredEntry(entry);
  const meta: Record<string, unknown> = {};
  for (const [name, value] of Object.entries(entry.state.fields)) {
    // `text` is the page body, not frontmatter (see `textOfFields`).
    if (name === "kind" || name === "id" || name === "text") continue;
    if (DERIVED_FIELD_SET.has(name)) meta[`_${name}`] = value === null ? "null" : value;
    else if (value !== null) meta[name] = value;
  }
  // Below local reach without the private partition's evidence (task 3.18).
  const counters = countersAtReach(entry);
  meta["_applied_count"] = counters.applied_count;
  meta["_violated_count"] = counters.violated_count;
  meta["kind"] = retired ? "brain-retired" : "brain-preference";
  meta["id"] = `${retired ? "ret" : "pref"}-${slug}`;
  const [typed] = parseFrontmatterText(formatFrontmatter(meta as FrontmatterMap, ""));
  return { meta: typed, retired };
}