automerge / automerge-classic

A JSON-like data structure (a CRDT) that can be modified concurrently by different users, and merged again automatically.
http://automerge.org/
MIT License
14.76k stars 467 forks source link

Failed test with initial schema hack #445

Closed okdistribute closed 2 years ago

okdistribute commented 2 years ago

This test fails after I save and then re-load the document. If you remove lines 1445 and 1446, the tests pass. If you port this test to preview.4, the test passes. So this seems to be a bug somewhere between preview.4 and preview.5. I did a git bisect and found that hash 325f9e8f7d4a16ed7a168494161cecf47185aea9 is the first bad commit.

This is a hack for supporting branching and merging from unrelated documents that were created within the same app. It is not directly supported by the Automerge API, which is probably how the bug snuck through. I think it's important to make this hack more automatic in the Automerge API. If I get some time to think about it I can say more or make a suggestion about how the API should work. Without this, there is no way to get reliable behavior for merging two branched documents that don't start with a single author.

√ automerge % npm test test/test.js

  Automerge
    changes API
      1) should apply changes from another document initialized with the same schema

  0 passing (37ms)
  1 failing

  1) Automerge
       changes API
         should apply changes from another document initialized with the same schema:
     RangeError: Expected seq 2, got seq 1 from actor 0000
      at applyChanges (backend/new.js:1536:15)
      at BackendDoc.applyChanges (backend/new.js:1778:35)
      at Object.applyChanges (backend/backend.js:29:23)
      at Object.applyChanges (src/automerge.js:93:37)
      at Context.<anonymous> (test/test.js:1448:36)
      at processImmediate (internal/timers.js:461:21)

EDIT: On further investigation, I found that changeIndexByHash does not contain actor 0000 in the re-hydrated version of the document. https://github.com/automerge/automerge/blob/main/backend/new.js#L1523

ept commented 2 years ago

Yes, we definitely need to improve this. See also issues #4 and #374 for previous discussions of the same problem. If you want to have a go at fixing this, I can help you get started.

ept commented 2 years ago

Thanks for the test case. I think I've fixed the bug on #446 — can you give it a try?