Open History to see the last commit
1/**
2 * Law 2 of the multi-tab Electric owner: after successful recovery and source
3 * catch-up, durable rows and every open eager Collection equal the source head.
4 * The authority is the persisted multi-tab eventual-consistency contract in
5 * oracle-coverage.md (#2209). Arrival-order regressions before this cut remain
6 * permitted. This judgment knows no cursor, coordinator, or storage algorithm.
7 *
8 * The Node driver supplies its independent server log; the Chromium/real SDK
9 * driver supplies PostgreSQL rows. Both use this same settled-row judgment.
10 * It does not judge intermediate publication, on-demand subsets, or optimism.
11 *
12 * This module owns the independent model and refinement check: the source head
13 * predicts both durable and public rows, ignoring row order but retaining every
14 * row occurrence and requiring each tab to be ready. The legal history grammar
15 * and Node production driver live in multi-tab-electric-oracle.test.ts. The
16 * receiving grammar and Chromium driver live in
17 * ../e2e/electric-source-prefix-oracle.opfs.spec.ts and its page module.
18 * Each driver calls this comparison after recovery and complete source delivery.
19 */
20import { declareOracle } from '../../db/tests/oracle-declaration'
21
22type SourceRow = { id: string | number; label: string }
23
24function canonicalRows(rows: ReadonlyArray<SourceRow>): Array<SourceRow> {
25 return rows
26 .map(({ id, label }) => ({ id, label }))
27 .sort((a, b) => String(a.id).localeCompare(String(b.id)))
28}
29
30/** Compare durable rows and ready tabs with the independently supplied head. */
31export function sourceHeadViolation(
32 sourceHead: ReadonlyArray<SourceRow>,
33 observation: {
34 durableRows: ReadonlyArray<SourceRow>
35 tabs: ReadonlyArray<{ status: string; rows: ReadonlyArray<SourceRow> }>
36 },
37): string | undefined {
38 // Source-head equality is independent of how storage orders or repairs
39 // interleaved commits. Readiness is supplied by each tab's own source.
40 const expected = canonicalRows(sourceHead)
41 if (
42 JSON.stringify(canonicalRows(observation.durableRows)) !==
43 JSON.stringify(expected)
44 ) {
45 return `durable rows differ from source head: ${JSON.stringify({ expected, actual: observation.durableRows })}`
46 }
47 for (const [index, tab] of observation.tabs.entries()) {
48 if (
49 tab.status !== `ready` ||
50 JSON.stringify(canonicalRows(tab.rows)) !== JSON.stringify(expected)
51 ) {
52 return `tab ${index} differs from ready source head: ${JSON.stringify({ expected, actual: tab })}`
53 }
54 }
55 return undefined
56}
57
58declareOracle({ check: [sourceHeadViolation] })