Repository navigation
Conversation
A merge can move a stamp and leave the content as it was. The new stamp was kept in memory but not saved, so it was lost at the next restart, and pulled again from a peer each time. Persisters are now told about it. Synchronizers, which ask for changes without hashes, are not. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Preamble
Hi 👋🏼 Big fan of this library. I'm using it to build a small personal app :)
Full caveat: I'm using this in my spare time and I've used Claude to detect, diagnose and propose this fix. I hope that's okay? Completely understand if you prefer to close this or need more clarity/time/etc. before going ahead. It seemed a potential bug worth reporting upstream with a potential fix.
Summary
A
MergeableStorecan merge a newer stamp for a Cell or Value whose content is the same. The store updates the stamp in memory, but autosave doesn't persist it. With this change, autosave persists it.The content stays correct and nothing reports an error, so the effect appears only in synchronization. After each restart, the store's hashes differ from those of its peers, and the store pulls the same Cells again. A
WsServerDurableObjectrepeats this each time it wakes from hibernation. In local workerd, with 1,900 Rows in this state and one client, each wake transferred about 750 KB in each direction. With this change, the client sent one message per wake and about 0 KB.Worked example
A phone and a server synchronize one Cell, and the server saves to a file. In this table,
dog@1means the contentdogwith stamp 1.dog@1dog@1dog@1cat, then back todog.dog@3dog@1dog@1dog@3.dog@3dog@3dog@1(not saved)dog@3dog@1dog@1After step 4, the server pulls
dog@3again and doesn't save it again. Each later restart repeats step 4. With this change, step 3 savesdog@3, so step 4 loadsdog@3and the hashes match.This script runs those steps:
On
main, the script printstrue, thenfalse. With this change, it printstruetwice.Cause
mergeContentOrChangesupdates a stamp when the incoming HLC is newer. Autosave gets its changes fromgetTransactionMergeableChanges, which readstouchedCellsandtouchedValues.cellChangedandvalueChangedfill those sets, and the underlying Store calls them only when content changes.Change
The store keeps two more sets,
stampedCellsandstampedValues. They contain everything that was touched, and anything that a merge gave a different hash. Persisters ask for changes with hashes, so they read the new sets. Synchronizers ask for changes without hashes, so they read the existing sets.Claude first added merged Cells to
touchedCells. The existing tests passed, but that version isn't safe, because Synchronizers readtouchedCells. Content fromsetDefaultContenthas an empty stamp, and the merge treats an empty stamp as newer each time. Three stores in a ring, each with two Synchronizers, exchanged 1,640 messages in one second and 1,664 in the next. With the change in this PR, they exchange none, and one of the new tests covers that case.Effects on existing apps
Method signatures, the wire format, and the stored format don't change. Apps might notice these effects:
startAutoSavewrites the same number each time it starts.getTransactionMergeableChanges(true), and therefore thechangesthat a custom Persister receives, can include a Cell whose content is the same.Claude also restarted a
WsServertwice with stale storage, 1,000 Rows, and 20 clients that reconnected together. This table shows the total traffic, measured once for each combination:mainmainFirst-restart traffic varies with the order in which clients answer.
Storage that earlier versions wrote
The first version of this description said that stale storage is saved once, and that was incomplete. Claude found this after deploying the change to the app.
If an earlier version merged new stamps and then saved a real edit, it wrote the Table hash and the content hash from memory, where the Rows were current, while the stored Rows were stale. A store adjusts those hashes but doesn't recompute them, so they stay wrong after this change saves the Rows. There are two outcomes:
WsServerDurableObjectwith two affected Tables and one client exchanged 11 messages on each wake with this change. In the app, three Tables were affected.In the app, Claude now computes each Table hash from its Row hashes, and the content hash from the Table hashes, before each load. With 1,987 Rows that takes about 3 ms, and the same
WsServerDurableObjectexchanged one message on each wake. This PR doesn't include that step. It could be a separate change if you want TinyBase to do it.The same step matters when a save fails and a later save succeeds. The later save writes hashes that include the lost Rows, so peers that match them don't send those Rows again. This happens with and without this PR.
How did you test this change?
Claude added four tests:
mergeable-store.test.ts: after such a merge,getTransactionMergeableChangesincludes the changes with hashes and omits them without hashes. Merging the same content again adds nothing.persisters/mergeable.test.ts: autosave persists the stamps once, for a Cell and a Value.durable-object-sql-storage.test.ts: in fragmented mode, the stamps are saved when the same merge also changes another Cell. In that case, the content hash is correct after a reload, even when a Row isn't.synchronizers.test.ts: the ring of stores described in the Change section sends no messages.On
main, the first three tests fail. On thetouchedCellsversion, the first and fourth tests fail. With this change, all four pass.Claude also ran these checks:
unit,unit-types,unit-persisters, andunit-synchronizersprojects pass in four runs (7,177 tests). Onmain, 7,173 tests pass. In earlier runs on a busy machine,synchronizers.test.tsfailed intermittently on both versions.convergence.fuzz,hub.fuzz, andefficiencypass withFUZZ_RUNS=500.beta.Claude didn't run the projects that need database servers, the end-to-end tests, or the documentation build. The change adds one sentence to the
getTransactionMergeableChangesdocumentation.🤖 Generated with Claude Code