Open History to see the last commit
1/**
2 * Real-provider refinement of the shared-driver fairness oracle. The page runs
3 * the same legal neutral and fixed-storm histories through public `preload()`,
4 * the browser/core adapter boundary, and Chromium's OPFSCoopSyncVFS worker. It
5 * freezes logical completions, public rows, raw dequeue reach, and the K=1
6 * violation result before cleanup, then reports provider cleanup separately.
7 * This fixture adds real OPFS/worker evidence; it does not claim multi-tab,
8 * multi-process, non-Chromium, latency, or unbounded-eventuality coverage.
9 */
10import { openBrowserWASQLiteOPFSDatabase } from '../src/index'
11import {
12 findSharedDriverFairnessViolation,
13 observeSharedDriverFairness,
14} from '../tests/shared-driver-fairness-oracle'
15import type {
16 SharedDriverFairnessObservation,
17 SharedDriverFairnessScenario,
18 SharedDriverFairnessViolation,
19} from '../tests/shared-driver-fairness-oracle'
20
21export type OPFSOracleResult =
22 | {
23 status: `complete`
24 provider: `Chromium OPFSCoopSyncVFS worker`
25 observation: SharedDriverFairnessObservation
26 violation: SharedDriverFairnessViolation | undefined
27 opfsCleanupFailures: ReadonlyArray<string>
28 }
29 | {
30 status: `failed-before-checkpoint`
31 provider: `Chromium OPFSCoopSyncVFS worker`
32 primaryFailure: string
33 opfsCleanupFailures: ReadonlyArray<string>
34 }
35
36declare global {
37 interface Window {
38 __tanstackDriverFairnessOracle?: OPFSOracleResult
39 }
40}
41
42async function removeOPFSArtifacts(
43 databaseName: string,
44): Promise<ReadonlyArray<string>> {
45 const failures: Array<string> = []
46 const root = await navigator.storage.getDirectory()
47 for (const suffix of [``, `-journal`, `-wal`]) {
48 try {
49 await root.removeEntry(`${databaseName}${suffix}`)
50 } catch (error) {
51 if (!(error instanceof DOMException && error.name === `NotFoundError`)) {
52 failures.push(
53 `${databaseName}${suffix}: ${error instanceof Error ? error.message : String(error)}`,
54 )
55 }
56 }
57 }
58
59 // OPFSCoopSyncVFS creates one private temporary access-handle directory per
60 // worker. This page owns its isolated origin for the fixture run.
61 const iterableRoot = root as FileSystemDirectoryHandle & {
62 entries: () => AsyncIterableIterator<[string, FileSystemHandle]>
63 }
64 for await (const [name, handle] of iterableRoot.entries()) {
65 if (handle.kind !== `directory` || !name.startsWith(`.ahp-`)) continue
66 try {
67 await root.removeEntry(name, { recursive: true })
68 } catch (error) {
69 failures.push(
70 `${name}: ${error instanceof Error ? error.message : String(error)}`,
71 )
72 }
73 }
74 return failures
75}
76
77function scenarioFromLocation(): SharedDriverFairnessScenario {
78 const mode = new URL(location.href).searchParams.get(`mode`)
79 if (mode === `neutral`) {
80 return {
81 id: `opfs-neutral-reach`,
82 work: [0, 1].map((index) => ({
83 kind: `hydrate` as const,
84 id: `hydrate-${index}`,
85 seededRows: [
86 { id: `row-${index}-0`, value: index * 10 },
87 { id: `row-${index}-1`, value: index * 10 + 1 },
88 ],
89 })),
90 }
91 }
92 return {
93 id: `opfs-fixed-persist-storm`,
94 work: [
95 ...Array.from({ length: 5 }, (_, index) => ({
96 kind: `persist` as const,
97 id: `persist-${index}`,
98 mutationsPerPersist: 2,
99 })),
100 ...Array.from({ length: 4 }, (_, index) => ({
101 kind: `hydrate` as const,
102 id: `hydrate-${index}`,
103 seededRows: [
104 { id: `row-${index}-0`, value: index * 10 },
105 { id: `row-${index}-1`, value: index * 10 + 1 },
106 ],
107 })),
108 ],
109 }
110}
111
112async function run(): Promise<void> {
113 const status = document.querySelector<HTMLOutputElement>(`#oracle-status`)
114 const databaseName = `ws5b-${crypto.randomUUID()}.sqlite`
115 let observation: SharedDriverFairnessObservation | undefined
116 let violation: SharedDriverFairnessViolation | undefined
117 let primaryFailure: string | undefined
118 let opfsCleanupFailures: ReadonlyArray<string> = []
119 try {
120 observation = await observeSharedDriverFairness(
121 () => openBrowserWASQLiteOPFSDatabase({ databaseName }),
122 scenarioFromLocation(),
123 )
124 // Freeze the reached semantic checkpoint before attempting OPFS cleanup.
125 violation = findSharedDriverFairnessViolation(observation)
126 } catch (error) {
127 primaryFailure = error instanceof Error ? error.message : String(error)
128 }
129
130 try {
131 opfsCleanupFailures = await removeOPFSArtifacts(databaseName)
132 } catch (cleanupError) {
133 opfsCleanupFailures = [
134 cleanupError instanceof Error
135 ? cleanupError.message
136 : String(cleanupError),
137 ]
138 }
139
140 if (observation) {
141 window.__tanstackDriverFairnessOracle = {
142 status: `complete`,
143 provider: `Chromium OPFSCoopSyncVFS worker`,
144 observation,
145 violation,
146 opfsCleanupFailures,
147 }
148 if (status) status.value = `complete`
149 } else {
150 window.__tanstackDriverFairnessOracle = {
151 status: `failed-before-checkpoint`,
152 provider: `Chromium OPFSCoopSyncVFS worker`,
153 primaryFailure: primaryFailure ?? `unknown failure before checkpoint`,
154 opfsCleanupFailures,
155 }
156 if (status) status.value = `failed-before-checkpoint`
157 }
158}
159
160void run()