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.
Design - Ingestion & Import Robustness
Problem
Open Second Brain's ingestion/import pipeline has four concrete gaps, all in the same subsystem:
- No within-pass resumability. A large folder ingest is planned by
planBatchesand dispatched batch-by-batch; the content-hash manifest only records a source once its fullingestSourcewrite lands. An interruption mid-plan leaves no durable ledger of which planned items already completed beyond what the manifest happens to have flushed - a resumed plan re-derives its work list from scratch, and the caller has no plan-scoped progress signal. - Only the
claudememory backend is importable. TheMemorySourceBackendregistry exposes exactly one adapter, so a user migrating off mem0 (a popular agent-memory store) or holding a generic memory-store JSON dump has no path into Brain preferences. - No cheap freshness gate on deterministic refresh.
clusters runre-materializesBrain/clusters/from the whole vault on every invocation. An agent that wants "refresh-on-demand at the start of graph work" must pay a full recompute even when nothing changed. - No command turns a source into citeable atomic claims. The block-id wikilink parser, source sha256 provenance, and session-summary lineage all exist, but nothing composes them into a distillation of a source into discrete, block-traceable claims.
Scope
t_ba1fa5f6Incremental per-item checkpointing (resumable batch ingest).t_ac9d2588mem0 / generic-JSON importers on theMemorySourceBackendseam.t_845fe240Staleness fast-path (--if-stale) forclusters run.t_2e2e959fSource distillation into atomic claims with block-level provenance.
Out of scope
- Granola connector (
t_df66855f) and ZeroEntropy provider (t_8d49f059) - distinct subsystems (connector family / embedding-provider family). - Onboarding checklist (
t_84500f39), LoCoMo eval (t_8dabe2b0) - unrelated themes. - Applying the staleness gate to the
dreamlearning pass:dreamis not a clean input-mtime->output-mtime materialization (it reads log events and signals and already carries extensive content-hash no-op idempotency). Forcing an mtime gate onto it would be a misfit, not a completion. The gate is built as a reusable primitive; only the deterministic materialize path (clusters run) is wired in this release. - Any model call in the deterministic core (unchanged invariant: the kernel calls no LLM; agents supply extractions/claims).
Chosen approach
Variant 1 (compose on existing seams). See variants.md.
Card 1 - Incremental per-item checkpointing
New module src/core/brain/ingest/checkpoint.ts, modeled on
content-manifest.ts:
IngestCheckpoint = { schema_version: 1, plan_id, source_dir, completed: string[], updated_at }.computePlanId(sourceDir, discoveredPaths)- short SHA-256 hex over the canonicalsourceDirand the sorted full discovered path set, so the id is stable across resume even as the remaining set shrinks.checkpointPath(vault, planId)-><vault>/.open-second-brain/ingest-checkpoints/<plan_id>.json(a machine artifact, not curated memory - mirrors the manifest location).readCheckpoint,recordCompleted(union-as-you-go, atomic, byte-identity no-op skip),clearCheckpoint. A corrupt file or unknownschema_versionis a hard error, never a silent reset (mirrorsreadManifest).- Opt-out env
OSB_INGEST_NO_CHECKPOINT(truthy) makes record/read inert - the deterministic-test escape hatch mirroring upstream'sGRAPHIFY_NO_INCREMENTAL_CACHE.
Wiring:
planBatchesgainsopts.resume?: boolean. When resuming it computes the stableplanIdfrom the full discovered set, reads the checkpoint, and excludes already-completed paths before content-hash classification - so a resumed plan never re-hashes completed items (the fast-path that keeps a large-vault resume near-free). The content manifest stays authoritative for everything the checkpoint does not cover.BatchPlangainsplanIdandresumedCompleted(count excluded via the checkpoint).ingestSourcegainsopts.planId?: string. On a successful ingest of a vault-file source, when aplanIdis set and checkpointing is enabled, it records the source path into that plan's checkpoint. The content manifest stays the authoritative final state; the checkpoint is plan-scoped progress.- MCP
brain_ingest_batch_plangainsresume(bool) and returnsplan_id+resumed_completed; when a resumed plan comes back empty (fully drained) it clears the checkpoint (authoritative-final cleanup).brain_ingest_sourcegains an optionalplan_id. - CLI
brain batch-plangains--resumeand prints theplan_id.
Card 2 - mem0 / generic-JSON importers
The current seam is one-file-one-entry (readdirSync *.md, one
parseMemoryFile(text) -> one entry). mem0 and generic exports are one JSON
file holding many records, so the seam is generalized (not worked around):
MemorySourceBackendgainsdiscoverMemoryFiles(dir): string[](default: sorted*.md, excludingMEMORY.md) andparseMemoryEntries(text): MemorySourceParse[](0..N). Theclaudebackend implementsdiscoverMemoryFilesas today's*.mdwalk andparseMemoryEntriesas a single-element wrap ofparseClaudeMemoryFile- byte-identical output, regression-pinned.import-claude-memory.tsiteratesdiscoverMemoryFilesthen flattensparseMemoryEntries; the manifest/dedup key becomesbasenamefor single-entry files andbasename#<slug>for multi-entry files, so one JSON file mapping to many preferences is tracked per entry without collisions.- New backends:
agent-backend/mem0.ts(idmem0): parses a mem0 export - a top-level array, or{results|memories: [...]}. Each record mapsmemory|text|data-> body,id|name-> name,metadata.description(or a truncated body) -> description. Structure-only; no natural-language gating.agent-backend/generic.ts(idgeneric): a documented neutral schema - an array of{ name, description, body }objects (the catch-all dump format).
- Registry entries added;
--from <id>/--backend <id>selector added to theimport-claude-memoryCLI verb (overrides thememory_backendconfig key); unknown id fails loudly with the registered list (existinggetMemoryBackend).
Card 3 - Staleness fast-path
New src/core/brain/staleness.ts:
newestMtimeMs(paths)/oldestMtimeMs(paths)andevaluateStaleness({inputs, outputs}): { fresh, newest_input_ms, oldest_output_ms }. Fresh iff outputs is non-empty AND every output's mtime >= every input's mtime.
clusters run gains --if-stale: inputs = listVaultPages(vault), outputs =
Brain/clusters/*.md. When fresh it prints a freshness-skip line, appends a
communities metric tagged skipped: "fresh", and returns 0 without opening the
store or recomputing. Otherwise it runs as today.
Card 4 - Source distillation
New src/core/brain/distill/distill-source.ts:
distillSource(vault, { sourcePath, claims, agent, now })whereclaimsis a non-empty list of{ text, block? }. Provider-agnostic: the agent supplies the atomic claims and the block ids; the core runs no model.- Validates each claim has non-empty text; block ids are validated structurally
via
parseWikilinkRich(no vocabulary gating). - Writes
Brain/distillations/dist-<slug>-<hash>.md: frontmatter (kind: brain-distillation,source_path,source_hash,provenance,created_at/updated_at,tags), body = a## Claimssection (one bullet per claim, each carrying[[<source>#^<block>]]when a block is given) plus the shared## Sourcesprovenance section. Idempotent on the source identity hash; a byte-identical re-run is a no-op (mirrorsingestSource). - New
BRAIN_DISTILLATIONS_REL+distillationPagePath(vault, slug)inpaths.ts. - CLI verb
o2b brain distilland MCP toolbrain_distill_source.
Design decisions
- Content manifest stays authoritative; the checkpoint is plan-scoped progress. Avoids two competing sources of truth for "is this source done."
planIdkeys on the full discovered set, not the remaining set, so it is stable across resume.- The
claudebackend must stay byte-identical through the seam widening - enforced by a regression test, matching the existing contract inagent-backend/claude.ts. - Staleness is not applied to
dream(justified under Out of scope). - Distillation composes existing primitives (
parseWikilinkRich,provenance) rather than re-deriving them. - All new machine artifacts live under
.open-second-brain/, all new curated pages underBrain/, consistent with existing layout.
File changes
New:
src/core/brain/ingest/checkpoint.tssrc/core/brain/agent-backend/mem0.ts,.../generic.tssrc/core/brain/staleness.tssrc/core/brain/distill/distill-source.tssrc/cli/brain/verbs/distill.tssrc/mcp/brain/distill-tools.ts(or foldbrain_distill_sourceinto an existing group)- tests mirroring each new module + CLI + MCP surface.
Modified:
src/core/brain/agent-backend/types.ts(seam widening)src/core/brain/agent-backend/claude.ts(implement new methods, byte-identical)src/core/brain/agent-backend/registry.ts(register mem0, generic)src/core/brain/import-claude-memory.ts(collection-aware iteration + keys)src/cli/brain/verbs/import-claude-memory.ts(--from/--backend)src/core/brain/ingest/batch-plan.ts(resume,planId)src/core/brain/ingest/ingest.ts(planIdcompletion recording)src/mcp/brain/ingest-tools.ts(resume,plan_id,brain_ingest_sourceplan_id)src/cli/brain/verbs/batch-plan.ts(--resume)src/cli/brain/verbs/clusters.ts(--if-stale)src/core/brain/paths.ts(distillations path)- CLI/MCP registration for the new
distillverb/tool. CHANGELOG.md,package.json(version bump), version mirrors viasync-version.ts.
Risks
- Seam widening regressions on the
claudeimport path - mitigated by a byte-identity regression test and by keepingparseMemoryEntriesa thin wrap. - Manifest key change for multi-entry files could collide - mitigated by the
basename#<slug>scheme and the existing duplicate-prefId guard. - mtime coarseness on fast filesystems could mark just-written outputs
"fresh" against a same-second input change - mitigated by
>=semantics that err toward recompute only when an input is strictly newer, and the gate is opt-in (--if-stale), never the default. - Distillation page proliferation - mitigated by idempotency on the source identity hash (one source -> one page, rewritten in place).