Skip to content

fix(repo): send snapshot for shallow-compacted docs - #18

Open
jacob-petterle wants to merge 1 commit into
mainfrom
fix/shallow-snapshot-sync
Open

fix(repo): send snapshot for shallow-compacted docs#18
jacob-petterle wants to merge 1 commit into
mainfrom
fix/shallow-snapshot-sync

Conversation

@jacob-petterle

Copy link
Copy Markdown
Contributor

Summary

  • Shallow-compacted docs (truncated oplog) fail to sync to peers with divergent version vectors
  • The sync protocol tries delta mode, which silently produces empty data
  • Fix: detect shallow docs via shallowSinceVV() and force snapshot mode
  • Non-shallow docs are completely unaffected (identical code path)

Root cause: After daemon compacts conversation docs to shallow snapshots, the browser initializes 20 schema ops with its own peer ID. When the daemon tries export({ mode: 'update', from: browserVersion }), the shallow doc can't compute the delta because the browser's peer ops don't exist in the truncated oplog. The protocol marks sync as "synced" with 0 data.

Fix: 5-line change in #buildSyncResponseMessage — detect shallow docs and send full snapshot instead of delta.

const isShallow = docState.doc.shallowSinceVV().toJSON().size > 0
const useSnapshot = isEmpty || isShallow

Risk

Essentially zero.

  • Non-shallow docs: isShallow = falseuseSnapshot = isEmpty → identical to before
  • Shallow docs: sends snapshot instead of broken delta → strictly better
  • No data mutation — only changes export mode for read-only sync responses
  • shallowSinceVV() is a stable Loro API

Test plan

  • Existing handle-sync-request.test.ts tests pass (9/9)
  • New test: "should send snapshot (not delta) when document is shallow" — verifies sync-response command is produced for shallow docs with divergent peer versions
  • New test: "shallow doc snapshot is importable by fresh browser" — end-to-end proof that a shallow snapshot gives a fresh peer all data

🤖 Generated with Claude Code

When a document has been shallow-compacted (oplog history truncated),
the sync protocol incorrectly tries to compute a delta update for
peers with divergent version vectors. The delta export fails silently
because the peer's ops don't exist in the truncated oplog, causing
the protocol to falsely report "synced" with zero data transferred.

Detect shallow docs via shallowSinceVV() and force snapshot mode,
which always contains the full current state and imports correctly
into any peer regardless of their version vector.

Non-shallow docs are completely unaffected — the isShallow check
evaluates to false, preserving the existing isEmpty-based logic.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant