memorix icon indicating copy to clipboard operation
memorix copied to clipboard

feat(sync): optional provider-agnostic multi-device store sync (keep local-first)

Open RaviTharuma opened this issue 2 months ago • 2 comments

Summary

There is no supported way to keep one user's Memorix memory current across several machines (e.g. a workstation, a home machine, and a portable laptop). Each machine keeps an independent store at ~/.memorix/data, so the memory a user sees depends on which machine they open.

Use case

A single user works on multiple devices and wants the same, up-to-date memory everywhere — open any device, see the same observations, sessions, and knowledge. This is one user syncing their own devices, not multiple users collaborating in real time.

Why the two obvious options fall short

  1. One central serve-http and point every machine at it. docs/DOCKER.md supports this, but it degrades project-scoped features: git-root detection, project binding, project memorix.toml/.env, and Git Memory need the service to see the repo at the path the client reports — a central host usually cannot see a laptop's working copy. It also adds hot-path latency, breaks offline use, and serializes writes onto one SQLite file.
  2. Replace SQLite with a central DB (e.g. Postgres). This contradicts the local-first boundary in docs/KNOWN_ISSUES_AND_ROADMAP.md, removes offline use, and would require rewriting the synchronous better-sqlite3 store layer.

Proposal (for discussion)

Keep the store local on every machine and add an optional, provider-agnostic replication layer that keeps those local stores consistent through an interchangeable remote (local dir/rsync, S3-compatible object store, or a small edge datastore — chosen by config, not hard-coded to any vendor).

The current schema already carries what a row-level last-writer-wins merge needs: monotonic observations.id + meta.next_id, writeGeneration, updatedAt/createdAt, and lifecycle fields (admissionState, status, revisionCount). So a first version can replicate observation rows without a schema migration.

Design constraints: opt-in (default off), asynchronous/off the hot path, offline-friendly, preview-first/auditable, and no new required dependency (a local-filesystem adapter needs nothing).

I wrote up a concrete, reviewable design — SyncRemote interface, merge strategy, phased delivery, and open questions — and can open it as a docs PR for direction before any implementation. Prior art already noted in docs/CLOUD_SYNC_AND_MULTI_AGENT_RESEARCH.md ("local SQLite + background cloud sync") but never specified or built.

Questions for maintainer direction

  1. Is a local-first replication layer (vs. central service or Postgres) the direction you'd accept for multi-device use?
  2. memorix sync already means cross-agent rules sync — prefer a separated namespace (sync store ...) or a new top-level verb?
  3. Row-level LWW with lifecycle-aware overrides as the v1 default — acceptable?
  4. Which adapters belong in-tree vs. optional/companion packages?

Happy to iterate before writing implementation.

RaviTharuma avatar Sep 06 '26 12:09 RaviTharuma

Implementation is up in #277 (branch feat/multi-device-sync-spec) — the PR moved from design-doc to working code.

Shape: one merge engine behind a fixed SyncRemote interface; the backend is configuration, not code (MEMORIX_SYNC_PROVIDER=fs|s3|postgres). Local SQLite stays canonical on every machine — sync is opt-in and off the hot path.

Correctness (convergent by construction):

  • Rows merge by a stable cross-device syncKey, never the replica-local integer id, so independent inserts on different machines can't collide.
  • Per-row logical clock (revision, writer) is a strict total order → replaying batches in any order converges (LWW).
  • Durable tombstones in sync_row_state → a stale upsert can't resurrect a deleted row.
  • All new state is additive (sync_row_state/sync_meta); the observations schema is untouched, so no migration.

CLI: memorix sync store <push|pull|status> with --dry/--json/--push-only/--pull-only.

Verified: pure merge-policy tests (incl. a permutation/convergence case), in-memory engine e2e, per-adapter round-trips, plus a two-datadir smoke where two real SQLite stores reconcile through a shared fs remote (id-collision safety, bidirectional replication, topic-key convergence, delete propagation, idempotent steady state all pass). tsc --noEmit clean across the new surface.

Happy to adjust scope/naming before review.

RaviTharuma avatar Sep 06 '26 12:09 RaviTharuma

已完成并发布在 v1.9.0。PR #277 的多设备同步设计和初始实现保留在合并历史中,PR #279 在此基础上补齐了当前存储契约、分页、哈希校验、原子应用、命名空间隔离和真实双数据目录 smoke。现在默认仍是本地优先,跨设备同步必须显式开启,因此关闭这个需求单。

AVIDS2 avatar Sep 07 '26 15:09 AVIDS2