Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

rfc-1657-trailbase.md

135 lines7.9 KB

1# RFC 1657 follow-up: TrailBase stream termination
2​
3Base: origin/main ad043b745. This is the narrow error-handling bug found while
4examining #1521; it does not implement that PR's polling policy.
5​
6- [x] Reproduce with the actual Collection and TrailBase adapter.
7- [x] Add close/error matrix checking reported errors, unhandled rejections,
8 reader lock, cleanup timer, retained rows/status, and absence of extra loads.
9- [x] RED: normal close passes; errored stream leaks an unhandled rejection and
10 keeps its reader locked. Later cleanup can reject again when canceling it.
11 /private/tmp/trailbase-stream-red.log.
12- [x] Fix: observe both settlements of reader.closed, clear interval, release
13 reader, and clear only the matching active-reader reference. The existing
14 listen catch remains the error reporter.
15- [x] GREEN: all12 package runtime tests pass; no type errors. ESLint no errors
16 and one pre-existing require-await warning. Prettier unchanged.
17 /private/tmp/trailbase-stream-green.log.
18- [ ] Release note and PR after integration review. No implementation pushed.
19​
20## Why the tests missed it
21​
22Existing tests close streams normally or cancel them deliberately. None errors
23a live stream after startup. The listen() rejection handler did not observe the
24separate promise returned by reader.closed.finally(). Normal close and rejected
25close therefore need separate laws, including resource cleanup after failure.
26​
27## Boundaries preserved
28​
29Initial subscribe failure still rejects readiness. Post-start disconnection
30keeps the last ready rows. No polling/reconnect behavior, automatic refetch,
31mutation confirmation policy, or core change is added.
32​
33The broader #1521 proposal claims a polling cycle which does not exist, marks
34ready even after required list failure, and skips acknowledgement waits based
35only on initial subscription availability. Do not transplant it. Stale same-ID
36ack evidence and actual degradation/recovery policy remain separate decisions.
37​
38## PR preparation review
39​
40Fetched origin/main: still ad043b745. Simplification review found no worthwhile
41cuts. Correctness review found a regression in the initial fix: reader.closed
42can settle before listen() drains its last queued event. Releasing the lock at
43that point turns graceful buffered closure into a spurious subscription error.
44​
45- [x] Add buffered-close to the close/error matrix. Enqueue two inserts before
46 closing; assert both rows applied, no error report, timer cleared, reader
47 released and later collection cleanup safe. RED: one fail, two controls pass.
48 `/private/tmp/trailbase-buffered-close-red.log`.
49- [x] Retire the reader after listen() settles, with a caught finally chain.
50 reader.closed now only clears the timer and observes both settlements.
51- [x] Full package: 13 runtime tests plus one type test pass, no type errors.
52 Built the missing Electric declaration dependency before the full type gate.
53 ESLint: no errors, one pre-existing require-await warning.
54 `/private/tmp/trailbase-prep-final-{green,lint}.log`.
55- [ ] Commit review follow-up, changeset and PR after preparation approval.
56​
57The original matrix terminated an idle reader after readiness. It did not cross
58buffered delivery with close. This is why it missed the race; no new recovery
59policy or retry loop is needed to correct reader ownership.
60​
61The follow-up review found that retiring the reader after listen() rejects must
62also cancel a still-open source: parsing/writing can fail without a stream read
63failure. Added an actual parse-failure test with an underlying cancel spy. RED:
64zero source cancellations. The listener's handled finally now awaits cancellation
65(preserving the original error on an already-errored stream) before releasing its
66lock. Buffered-close, normal-close and read-error controls remain GREEN.
67​
68Final gate: 14 runtime tests and one type test pass; no type errors, lint errors,
69or new warnings. RED log: /private/tmp/trailbase-processing-failure-red.log.
70Final GREEN/lint logs reuse the prep-final paths above.
71​
72Adjacent pre-existing limitation, not claimed solved: erroring the stream and
73calling collection.cleanup() in the same turn can still expose the unobserved
74promise from cancelEventReader(). Its cancellation path is unchanged. The
75reviewer also verified that an error during pending initial loading now retires
76the reader before a later, settled cleanup. Do not equate that with the same-turn
77cleanup case. Track the latter as a further cancellation test/fix before claiming
78full termination coverage.
79​
80## Same-turn cleanup follow-up
81​
82User approved including the adjacent cancellation failure. Added the four-cell
83loading/ready × close/error matrix with no microtask between stream termination
84and collection cleanup. Both error cells RED, both close controls GREEN.
85`/private/tmp/trailbase-same-turn-red.log`.
86​
87cancelEventReader now observes its cancellation promise, since native errored
88streams reject cancellation with the original stream error. The reader is still
89retired synchronously. The tests observe preload immediately, resolve delayed
90loading after cleanup, and assert no unhandled rejection, no late rows, no new
91fetch/subscription, an unlocked stream and cleaned-up status.
92​
93TrailBase has no dedicated fast-check/model oracle yet. Its local tests are
94example/matrix integration tests. Its service-backed E2E entry runs shared
95predicate, pagination, join, deduplication, collation, mutation, live-update and
96progressive suites; these are not a generated lifecycle model. E2E was inspected,
97not run during this preparation. A bounded future lifecycle oracle should vary
98startup/list completion, event delivery, stream termination, processing failures,
99cleanup and restart, with independent row/status/resource observations. Do not
100add polling semantics to that model without a separate policy decision.
101​
102Full local gate: 18 runtime tests plus one type test pass; no type errors or
103lint errors (one pre-existing require-await warning). Logs:
104`/private/tmp/trailbase-same-turn-{green,lint}.log`. No new commit or push yet.
105​
106## Lifecycle oracle (user-approved)
107​
108- [x] Drive the actual adapter and collection with controlled subscribe/list
109 promises and native streams; model visible rows independently.
110- [x] Cross eager/on-demand, startup failures, delayed old-session settlements,
111 row edits, graceful/buffered close, read/parse failures, cleanup and restart.
112 Keep existing unit and loading-time matrices. Extract only the shared mock.
113- [x] Add 32 corpus cases plus fixed and fresh-seed fast-check campaigns using
114 the existing replay configuration, not a second seed parser.
115- [x] New bug RED in both generated campaigns and both modes' fixed witnesses:
116 old canceled subscribe rejects after restart and cancels the current reader.
117 Fixed seed 714203, path 21:0:2:2; random seed -951227069, path 39:0:3:2:2:2.
118 Log: /private/tmp/trailbase-oracle-first.log.
119- [x] Guard canceled startup before touching shared reader ownership. GREEN.
120- [x] Mutation assay: all five known stream fault variants rejected by this
121 oracle itself. Logs: /private/tmp/trailbase-oracle-assay-\*.log.
122- [x] 10× campaign: 800 generated histories plus 32 fixed cases pass, random
123 seed 127535183. Log: /private/tmp/trailbase-oracle-stress.log.
124- [x] Document laws, exclusions, replay commands and mutation evidence in
125 packages/trailbase-db-collection/tests/ORACLE.md.
126​
127The gap that exposed the new bug was ownership across sessions: testing only
128cleanup followed by old settlement leaves no replacement resource to corrupt.
129The oracle settles old work while the next stream is live, then sends new edits.
130This is a small lifecycle oracle, not complete TrailBase adapter coverage.
131No polling, reconnect, pagination or mutation-confirmation policy was added.
132​
133Final local gate: 52 runtime tests plus one type test pass, no type errors.
134Lint has no errors and only the existing require-await warning. Logs:
135/private/tmp/trailbase-oracle-{green,lint}.log. Changes remain local, uncommitted.