dsh-brain/docs/LOCAL_MEMORY_IMPLEMENTATION.md

5.5 KiB

Local Memory Implementation (Phase 1)

This repository now implements the local-first memory phase from docs/plans/LOCAL_MEMORY_PLAN.md. Cloud syncing remains disabled by default and no local memory operation requires an account, token or network.

Layout

  • Global: $DSH_HOME/brain/
    • runtime/memories.json — local JSON record store
    • runtime/USER.md, runtime/MEMORY.md — generated compact projections
    • spaces/global/space.json — stable global space marker
    • backups/, state/
  • Project: <project>/.dsh/brain/
    • documents/ — project summaries and decisions
    • memory-space/ — project memory records and space marker
    • state/
  • The project .dsh/.gitignore ignores brain/ only; it does not ignore the rest of .dsh.
  • No .mnemon directory is created.

Management API

The settings-ui bundle exposes a local-memory management surface under /dsh-brain/local-memory:

  • GET /status — storage roots, space markers and sync switches;
  • GET /spaces and GET /documents — available spaces and project documents;
  • GET /memories?limit=&offset= — paginated local records;
  • GET /search?q=&k= — bounded local search over the project and global spaces;
  • POST /forget — revision-checked tombstone delete;
  • POST /backup — create a copy of the global brain root;
  • GET|POST /migrate — inspect legacy .mnemon locations, copy one into backups, or roll a copied backup into a new destination.

All write routes require a same-origin request. These routes only use LocalMemoryManager's public methods and never touch SQLite, CLI or provider privates.

Data model

Every memory record has a unique id, scope, space id, content, category, tags, source, created/updated timestamps, revision (starting at 1), a local monotonic changeSeq, and a tombstone flag for deletes. Writes are atomic and serialized through a per-space lock. Exact duplicate content returns the existing record with duplicate: true.

Automatic behavior

  • Global and project spaces are created lazily and idempotently on first memory use; restarting or opening multiple windows reuses the same markers.
  • Explicit long-term statements from user turns are captured incrementally into USER.md/MEMORY.md by LocalMemoryManager.summarizeConversation; assistant text and raw transcripts are never stored by that path.
  • The generated USER.md/MEMORY.md files are exposed as a compact system-prompt section (dsh-brain:local-memory) once per turn; they are not re-injected in full and are empty when local memory is disabled.
  • proposeProfile(..., inferred=true) keeps inferred behavior in a pending area; only explicit entries are projected into USER.md. correctProfile increments the revision and regenerates the projection.
  • Search defaults to the project and global spaces only and returns five items.
  • Cloud memory is selected only when cloudMemoryEnabled is true and a memory token is configured; otherwise the memory tools use the local remote.

Migration

legacyMigrationReport() reports existing .mnemon locations without moving anything. migrateLegacy(path) copies a legacy tree into backups/ and records the operation in state/migrations.json; rollbackMigration(backup, destination) copies it back to a new location. Neither operation deletes the original.

Verification

node verify-local-memory.mjs covers directory creation, idempotent spaces, deduplication, revisions/tombstones, restart persistence, summary/profile projections, documents, migration reporting and the local memory remote. node scripts/verify-all.mjs runs the complete repository suite. node verify-local-memory-acceptance.mjs and docs/LOCAL_MEMORY_ACCEPTANCE.md map the plan criteria to executable evidence.

Workspace and reliability

  • Every tool/session operation runs inside LocalMemoryManager.withWorkspace (AsyncLocalStorage). Project stores and document stores are cached per normalized workspace path, not per manager instance, so switching A to B in one process cannot read A's project records.
  • JsonMemoryStore and JsonDocumentStore distinguish a missing data file from a corrupt or incompatible one. Corrupt files are preserved and reported; only a missing file initializes an empty store.
  • Initialization, project mapping, session checkpoints and projection writes use per-target file locks and unique temporary names. A committed memory write with a failed projection leaves state/projection.json dirty; the next ensureGlobal() retries it.
  • src/mnemon/native.ts exposes the local store as a Mnemon Native provider using the vendored dsh-brain-mnemon.memory-space-provider/v1 contract.

Mnemon fork and remaining gaps

vendor/dsh-mnemon is the independent dsh-brain-mnemon source fork of omdsh-dev/dsh-mnemon (upstream revision 8b650ab5ef33522e03887b250b13336593daa26f, 0.5.54, MIT). It retains the complete upstream Monorepo source, including Runtime, Documents, Memory Spaces, Skills Source, Mnemon Native, all external Provider plugins and all strategy plugins. See docs/MNEMON_FORK.md and vendor/dsh-mnemon/FORK_NOTICE.md.

The provider-SDK subset used by dsh-brain is copied to src/mnemon/fork; the package no longer depends on dsh-mnemon-source-memory-spaces. The local JSON engine above remains the phase-1 kernel behind the public memory tools. Loading the full upstream Mnemon runtime as the memory engine, cloud change cursors, remote conflict merge and external Provider adapters remain later cloud-sync work.