fix(db): preserve restored rows across subscription restart #2313
Merged
KyleAMathewsmerged 7 commits into
main fromrefs/pull/2313/head·merged by KyleAMathews Showing 10 changed files with 173 additions and 0 deletions (loading 8).
x
@@ -0,0 +1,5 @@
+---
+'@tanstack/db': patch
+---
+
+Preserve restored rows for direct `subscribeChanges()` consumers after Collection cleanup and restart, even when their values are unchanged. This prevents incorrect delete events when the new sync run restores the same rows.
168 additions and 0 deletions+168-0docs/contributing/oracle-reviews/2026-10-30-pr-2313-review.mdAddedView file
@@ -0,0 +1,168 @@
+# PR #2313 external review evaluation
+
+Reviewed head: `ef6316a910c24e60cbcfe8e3e613b0bddc821958`.
+Source: the maintainer's ten-item external review, in source order below.
+The repeated source numbers receive stable IDs P1–P3, O1–O6, and D1.
+This follow-up changes tests and documentation. Production remains byte-for-byte
+identical to the reviewed head. No product-law change is required.
+
+## Laws, experiments, and inferences
+
+| Law and authority | Existing enforcement | Decisive experiment | Inference and repair |
+| --- | --- | --- | --- |
+| A successful direct on-demand restart publishes restored rows, deletes omitted rows, suppresses equal public updates, and keeps partial or failed replacements private. Authority: `subscription.ts`'s public change-message boundary, the lifecycle grammar, and ARCHITECTURE's cleanup/restart and replay sections. | The publication model independently accumulates source and subscriber rows and compares exact per-command batches. Its old reach labels did not establish restored equality independently of fixture numbering. | On reviewed production, prepend one request/resolve/release of the other demand to the old grammar. The old fixed acquisition campaign passes, but a labeled equal case retains `{id:'a',value:1}` and actually restores `{id:'a',value:2}`. Restoring the original equality-suppression defect makes all 12 expanded restart campaigns fail at the shared publication comparison. | The production fix remains correct. Explicit input row values, observed retained/delivered values, actual settlements, owner lifetimes, and acquisition counts now establish the premises. |
+| An eager restart's next public batch reconciles retained subscriber rows with installed state. An empty ready batch reconciles an empty replacement. Authority: ARCHITECTURE, “Cleanup, restart, and detached waiters.” | The existing restart fixture marks an empty eager run ready immediately. It does not promise whole-run atomicity for a source that streams several commits. | The new ordinary boundary test starts with a/b/c, cleans up, restarts, commits a while loading, then commits b/c and marks ready. It observes delete b/c in the first batch and insert b/c in the second. | P1's observation is real and is accepted design under the existing contract. Waiting until ready would change that contract and delay streamed eager updates. This PR does not make that change. |
+| Cleanup ends a dependent graph. Public cleanup rejects active live queries, dependent cleanup ends them first, and internal cleanup crashes them. Source-only restart does not revive a crashed graph. Authority: public `cleanup()` documentation, glossary, ARCHITECTURE laws 14/16 and its explicit direct-restart limitation. | The cleanup oracle already exercises admission and dependent invalidation. The publication owner explicitly excludes compiled includes. | Six real compiled-query witnesses cross eager/on-demand with public, dependent, and internal cleanup. Public cleanup rejects. Internal cleanup keeps b/d in a crashed query after source-only restart. Cleaning up and preloading the query again publishes only b. | P2 conflates retained terminal rows with a live replacement. A supported query restart removes d. No delegated-publication production repair is justified by this trace. |
+| Ordinary subscription admission is closed during active cleanup. Initial listener registration owns no demand, replay, or public rows. Authority: glossary's admission rule and AGENTS.md's distinction between valid operations and contradictory collaborators. | The cleanup oracle has 18 admission cells. Actual `Collection.on` only registers a callback. The only synchronous cleanup-start callback during construction has no user-visible work to retire. | An executable registration probe calls the real public subscription boundary while adapter cleanup is held. It throws before either listener-registration method runs. Ordinary registration and repeated unsubscribe succeed. The unchanged 18-cell admission owner passes. | P3's arbitrary throwing-registration conditional can leak resources, but the proposed throwing/reentrant collaborator does not occur at this constructor boundary. Do not add defensive recovery for substituted internal methods. |
+