From 0aa0139ba9b06dd51989b54b908e84c871d9691d Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Tue, 17 Nov 2026 22:11:46 +0530 Subject: [PATCH 1/2] fix(browser): fail the IndexedDB handle after a failed flush and settle every flush (#894) An IndexedDB flush now lands whole or not at all and always settles. After a failed flush the handle rejects further writes, commits, checkpoints and imports with STG-046 until reopened; the failure itself is STG-048. Co-Authored-By: Claude Sonnet 5.5 --- CHANGELOG.md | 3 + CLAUDE.md | 4 +- Cargo.toml | 1 + docs/ERROR_REFERENCE.md | 22 +- docs/TEST_COVERAGE.md | 12 +- .../2026-11-17-idb-flush-recovery-design.md | 144 ++++ docs/testing/894-idb-flush-recovery.md | 112 +++ src/browser/flush_tests.rs | 657 ++++++++++++++++++ src/browser/indexeddb.rs | 157 ++++- src/browser/mod.rs | 264 ++++--- src/browser/tx_tests.rs | 10 +- src/error.rs | 2 +- tests/crash_kill_test.rs | 2 +- 13 files changed, 1255 insertions(+), 135 deletions(-) create mode 100644 docs/superpowers/specs/2026-11-17-idb-flush-recovery-design.md create mode 100644 docs/testing/894-idb-flush-recovery.md create mode 100644 src/browser/flush_tests.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 9889cae51f..559e2d0dae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -172,6 +172,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- **IndexedDB flush recovery** (#894, v3.0.0). Page puts and deletes now land atomically, including when queueing throws; abort-only failures and repeated request errors settle once with STG-048 naming the IndexedDB write and DOM exception. Flushes run in application order, and checkpoint waits for earlier writes even with no pages of its own. After failure, new writes, commits, checkpoints and imports reject with STG-046 without changing memory; already applied work stays readable and exportable. Close and reopen recovers the successful prefix before failure. Unstarted flushes reject with API-018 after close. No new dependencies, file-format, schema or public-method changes. The next v2.x patch backport is separate; #894 stays open until release. + + - **Window partition scratch obeys `query_memory_limit`** (#765). Each window now charges its partition row indices while they coexist with input rows and earlier result columns, returns `QRY-012` if reserving them fails, and releases the charge after the window or on error. An in-place total sort removes the stable sort's uncharged merge buffer; partition and row order are unchanged. - **An expression's `=` and `!=` equate a keyword with a ref to the entity it names** (#744). Patterns, joins, `not` bodies and `or` branches treat the keyword `:bob` as equal to a ref to the entity `:bob` names, but `[(= ?g :bob)]` compared the two values as they are, so with `?g` bound to that ref it kept no row and `[(!= ?g :bob)]` kept the row: the same question answered differently by how the query was written, with no error. `=` and `!=` now compare as joins do, in queries, rule bodies, `not` clauses and binding expressions, against a keyword constant, a keyword value or a bound `Val(Ref)`. A keyword still equals only the same keyword, and `<`, `>`, `<=` and `>=` on a keyword or a ref are still a type mismatch (no row). - **`checkpoint`, `verify` and `rebuild_indexes` inside a `WriteTransaction` return `INT-001` instead of deadlocking** (#734). Called on a thread that holds a `WriteTransaction`, they waited on the write lock that the transaction already held, so the thread hung. They now return `INT-001`, as `execute`, `query`, `fact_log` and `begin_write` already did, and the transaction stays usable. `prepare` and `PreparedQuery::execute`/`query` make the same check; they could not deadlock, but they ran inside the transaction where `execute`/`query` refused. `checkpoint` deadlocked in every release since `WriteTransaction` was added; the fix for it and for the prepared calls ships in v2.x too. diff --git a/CLAUDE.md b/CLAUDE.md index 91dd562198..0e681cc6e2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -197,7 +197,7 @@ cargo run < demos/demo_negation.txt 8. **`src/error.rs`** — Structured error codes: `MinigrafError`, `ErrorCategory`, `ErrorCode` registry (PRS/QRY/STG/WAL/API/INT codes); must match `docs/ERROR_REFERENCE.md`. `MinigrafError::invalid_argument` (API-017) and `MinigrafError::closed` (API-018) exist for language bindings; browser OPFS adds API-026 (unavailable context), API-027 (busy) and STG-048 (failed browser storage operation) -9. **`src/browser/`** — Browser WASM backend (`browser` feature): `backend.rs` (buffer/OPFS dispatch), `opfs.rs` (`OpfsBackend` over an exclusive dedicated-worker sync access handle, one `.graph` file and a 256-page cache; no WAL; sole-valid-meta recovery; crash-atomic import stages `.graph-import`), `buffer.rs` (`BrowserBufferBackend`, in-memory pages with dirty tracking; free pages a pinned generation may read are withheld from release, `pins::split_released`), `indexeddb.rs` (IndexedDB persistence on the main thread or in workers); `openOpfs(name)` selects OPFS explicitly (Tier 2 in v3.0.0); all modes have synchronous `close()`, invalidating dependents and releasing the OPFS lock, with no save; `BrowserDb.query` returns a `BrowserCursor` (JSON row batches); `BrowserDb.prepare` returns a `BrowserPreparedQuery` (`execute`/`query` with a bindings object; `withMaxDerivedFacts`/`withQueryMemoryLimit`/`withMaxIterations` return a new one, #872; read by `binds.rs`: plain JS values or `{type, value}` forms, #181; a set as `{type: 'set', value: [..]}` or a JS `Set`, #316); `BrowserDb.beginWrite` returns a `BrowserWriteTransaction` (#824: sync `execute` staging through `db::Staged`, `commit()` a `Promise` resolved after the IndexedDB flush, `rollback()`; `BrowserDbInner::tx_open` makes every other read/write call `INT-001`; finished = `API-018`; tests in `tx_tests.rs`); `udfs.rs`: `registerPredicate`/`registerAggregate` (#180; values as `{type, value}` objects, a throw is `QRY-014`; JS values held in a thread-local table, the registry keeps ids). Tests here must be `#[wasm_bindgen_test]` (the module only builds for wasm32); `.github/workflows/wasm.yml` runs them in headless browsers, runs the WASI tests under Wasmtime, and lints both wasm targets. New file-backed integration test files need `#![cfg(not(target_arch = "wasm32"))]` +9. **`src/browser/`** — Browser WASM backend (`browser` feature): `backend.rs` (buffer/OPFS dispatch), `opfs.rs` (`OpfsBackend` over an exclusive dedicated-worker sync access handle, one `.graph` file and a 256-page cache; no WAL; sole-valid-meta recovery; crash-atomic import stages `.graph-import`), `buffer.rs` (`BrowserBufferBackend`, in-memory pages with dirty tracking; free pages a pinned generation may read are withheld from release, `pins::split_released`), `indexeddb.rs` (IndexedDB persistence on the main thread or in workers; atomic puts/deletes, abort/error settlement, STG-048 on failure, ordered flushes and persistent per-handle STG-046 refusal after failure, #894; checkpoint waits for earlier applied writes, close discards unstarted flushes, reads/export retain already-applied memory); `openOpfs(name)` selects OPFS explicitly (Tier 2 in v3.0.0); all modes have synchronous `close()`, invalidating dependents and releasing the OPFS lock, with no save; `BrowserDb.query` returns a `BrowserCursor` (JSON row batches); `BrowserDb.prepare` returns a `BrowserPreparedQuery` (`execute`/`query` with a bindings object; `withMaxDerivedFacts`/`withQueryMemoryLimit`/`withMaxIterations` return a new one, #872; read by `binds.rs`: plain JS values or `{type, value}` forms, #181; a set as `{type: 'set', value: [..]}` or a JS `Set`, #316); `BrowserDb.beginWrite` returns a `BrowserWriteTransaction` (#824: sync `execute` staging through `db::Staged`, `commit()` a `Promise` resolved after the IndexedDB flush, `rollback()`; `BrowserDbInner::tx_open` makes every other read/write call `INT-001`; finished = `API-018`; tests in `tx_tests.rs`); `udfs.rs`: `registerPredicate`/`registerAggregate` (#180; values as `{type, value}` objects, a throw is `QRY-014`; JS values held in a thread-local table, the registry keeps ids). Tests here must be `#[wasm_bindgen_test]` (the module only builds for wasm32); `.github/workflows/wasm.yml` runs them in headless browsers, runs the WASI tests under Wasmtime, and lints both wasm targets. New file-backed integration test files need `#![cfg(not(target_arch = "wasm32"))]` ### Data Model @@ -250,7 +250,7 @@ Auto-migrates v7 → v8 on open (spec §9, with a backup meta page). v1–v6 are ## Test Coverage -**2172 tests** (2149 active, 23 ignored; unit + integration + doc). Engine answers once checked by the executor are frozen in `tests/answers/` (#551). +**2172 tests** (2149 active, 23 ignored; unit + integration + doc). Browser-only inventory is separate: #894 adds 13 wasm tests (94 main-thread / 127 dedicated-worker tests in total); see `docs/testing/894-idb-flush-recovery.md` for execution results. Engine answers once checked by the executor are frozen in `tests/answers/` (#551). See `docs/TEST_COVERAGE.md` for the full per-file breakdown. The #760 deterministic storage suite adds three native unit tests in a separate `--cfg minigraf_shuttle` build, outside the default count. Run only `graph::storage::deterministic::` with that cfg; ordinary unit tests do not enter its scheduler. `deterministic-schedules.yml` runs relevant v3 PRs on Linux/macOS/Windows plus Linux MSRV, and larger nightly/manual searches with replay artifacts. The identical main workflow activates the v3 schedule after both PRs merge. See `docs/testing/deterministic-schedules.md` for scope, controls and findings. diff --git a/Cargo.toml b/Cargo.toml index 6863f3347f..ea2f8b3612 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,7 @@ web-sys = { version = "0.3", optional = true, features = [ "IdbOpenDbRequest", "IdbRequest", "IdbTransaction", + "DomException", "IdbTransactionMode", "StorageManager", "Window", diff --git a/docs/ERROR_REFERENCE.md b/docs/ERROR_REFERENCE.md index 413d556664..35096f0272 100644 --- a/docs/ERROR_REFERENCE.md +++ b/docs/ERROR_REFERENCE.md @@ -3153,7 +3153,7 @@ let mut out = LogWriter::create("memory.v2.graph", OpenOptions::new())?; ### STG-046 Earlier checkpoint failed on this handle -**Error text**: `an earlier checkpoint failed on this handle and is not retried; reopen the database` +**Error text**: `an earlier checkpoint or IndexedDB flush failed on this handle and is not retried; reopen the database` **Cause**: A checkpoint (automatic, `checkpoint()`, `rebuild_indexes()`, or the one on close) failed with an I/O error after it may have written pages: a failed write, a full disk, or a failed `fsync`. After a failed `fsync` the operating system may have dropped the written pages while marking them clean, so a second `fsync` can report success for data that never reached the disk (the PostgreSQL "fsyncgate" problem). The checkpoint's meta page may also have reached the disk although the call failed; a retry would then overwrite pages that page references. So the handle never retries: every later write, checkpoint and index rebuild through it fails with STG-046. @@ -3165,6 +3165,19 @@ working. There is no WAL. Close and reopen to select the complete previous or new generation; the rejected transaction may be wholly present. Closing does not retry the failed commit. +In IndexedDB mode, a failed flush rejects with STG-048 (naming the IndexedDB +write and DOM exception); failure to prepare its pages keeps its engine code. +The handle then refuses newly admitted transact/retract, commit (even empty), +checkpoint and import with STG-046, changing nothing. Reads, cursors, prepared +queries, rules, transaction staging, `exportGraph()` and `close()` still work. +Memory includes the failed operation and overlapping work applied before the +failure became known. Export reflects that memory, not an acknowledgement of +durability. Later flushes wait for earlier ones and send nothing if an earlier +flush failed. Close and reopen recovers the state before the failed flush; +a rejected IndexedDB write is never stored. A failed import stays visible in +memory but its puts and deletes leave IndexedDB unchanged. Closing does not +retry anything. + **Resolution**: - Fix the cause (free disk space, check the device and the kernel log), then drop every clone of the handle and open the database again. Opening picks the newest complete checkpoint and replays the WAL. - Do not copy the `.graph` file without its `.wal` while the handle is in this state. @@ -3191,12 +3204,13 @@ not retry the failed commit. **Error text**: `browser storage operation failed: {} ({})` -**Cause**: An OPFS operation failed: opening an entry, acquiring its sync access handle, reading, writing, flushing, checking size, truncating, or staging an import. The message names the operation and the browser exception (for example `QuotaExceededError`), or a short read/write. Short transfers are never accepted as complete pages. Lock contention uses STG-026; an unavailable OPFS context uses API-026. +**Cause**: An IndexedDB write (transaction creation, request queueing, error or abort), or an OPFS operation failed: opening an entry, acquiring its sync access handle, reading, writing, flushing, checking size, truncating, or staging an import. The message names the operation and the browser exception (for example `QuotaExceededError`), or a short read/write. Short transfers are never accepted as complete pages. Lock contention uses STG-026; an unavailable OPFS context uses API-026. **Resolution**: - Check the operation and exception name, available origin quota and browser permissions. -- After a commit may have written, later writes fail with STG-046. Call `close()` and reopen. Reads may still include the unconfirmed transaction; a rejected commit does not prove it is absent on disk. -- An import failure at publication closes the database; reopen to recover the complete old or new image. +- In OPFS mode, after a commit may have written, later writes fail with STG-046. Call `close()` and reopen. Reads may still include the unconfirmed transaction; a rejected commit does not prove it is absent on disk. +- An OPFS import failure at publication closes the database; reopen to recover the complete old or new image. +- After an IndexedDB flush fails, later durable operations fail with STG-046. Close and reopen to recover the successful prefix before the failed flush. The rejected write, including all its page deletes, is absent from IndexedDB. **Scenario**: Origin quota is exhausted while a worker writes a data page; `execute()` rejects with STG-048 naming the write and `QuotaExceededError`. diff --git a/docs/TEST_COVERAGE.md b/docs/TEST_COVERAGE.md index dd0fb3fb8a..5ae3c79ed4 100644 --- a/docs/TEST_COVERAGE.md +++ b/docs/TEST_COVERAGE.md @@ -2,6 +2,14 @@ ## Coverage Summary +**#894 verification (2026-11-17)**: native default tests pass (2,149 active, +23 ignored); strict native all-target and browser-target Clippy, build and +formatting pass. Firefox passes **94 main-thread** and **127 dedicated-worker** +tests. The distribution binary is **1,270,880 bytes** (+16 bytes), below the +1,280,000-byte gate. The pre-fix multi-page durable-map and abort-only deadline +failures, one-line existing crash-harness Clippy correction, and unverified lanes +are documented in the [PR 1 evidence](testing/894-idb-flush-recovery.md). + | | Default features | With `--features test-hooks` | |---|---|---| | **Active** | 2,149 | 2,187 | @@ -728,6 +736,8 @@ These are not `#[test]`s; run them with `cargo kani` or `.github/workflows/kani. None of these are counted in the summary. The wasm32 targets run in CI (`.github/workflows/wasm.yml`). +- **IndexedDB flush recovery** (#894; `src/browser/flush_tests.rs`, 13 new browser tests, plus the updated closed-connection case in `tx_tests.rs`): atomic puts/deletes after queue-time throws; abort-only settlement with a deadline; multiple request errors without callback exceptions; failed-handle refusal without counter/fact changes; execute/commit/checkpoint/smaller-import failures; missing dirty-page preparation errors; ordered overlaps and checkpoint boundaries; close/reopen with queued work; migration failure and intact-page-map recovery. Reopened exports pass the shared native image integrity walk, allowing absent free pages. Healthy operation retains no settled flush history. See [PR 1 evidence and scope](testing/894-idb-flush-recovery.md). Native test inventory is unchanged. + - **OPFS worker mode** (#441; `src/browser/opfs_tests.rs`, 34 worker tests plus one main-thread test): - AC1/AC4: `acknowledged_writes_survive_close_and_reopen`, `the_database_is_one_file_and_no_wal`. - AC2/AC8: `invalid_names_are_api_017_before_anything_is_created`, `a_second_open_is_stg_026_until_close`, `open_opfs_on_the_main_thread_is_api_026_and_creates_nothing`. @@ -738,7 +748,7 @@ None of these are counted in the summary. The wasm32 targets run in CI (`.github - AC13/AC14: `opfs_open_read_bound`, `opfs_cache_residency_bound`, `indexeddb_mode_works_in_a_worker`. - AC3 parity: the worker build runs the existing transaction, prepared-query and UDF suites against OPFS as well as IndexedDB. The main-thread build preserves the existing IndexedDB tests. Worker CI sets `RUSTFLAGS=--cfg minigraf_browser_worker` for Firefox and Chrome. -- **In a browser** (`wasm-pack test --headless`, Firefox and Chrome): 81 `wasm_bindgen_test` tests (inherited Firefox main-thread result, 2026-11-14, after #441). The final #641 snapshot passed in headless Firefox on the bench; Chrome remains a CI run. +- **In a browser** (`wasm-pack test --headless`, Firefox and Chrome): 94 `wasm_bindgen_test` tests after #894 (81 inherited plus 13 flush-recovery tests); see the [#894 verification record](testing/894-idb-flush-recovery.md) for actual runs. The final #641 snapshot passed in headless Firefox on the bench; Chrome remains a CI run. - Three committed-memory-backend input-order fixtures: scalar Sort omission, streaming counts with Boolean/float representatives and mixed numeric group order, and partition windows preserving float edges and numeric group classes (#641). - Twenty-six for `BrowserDb`, including IndexedDB persistence, the default `allow_overrides` list (#869), the cursor, and the flush returning every dirty page and failing on one it cannot read (#470). - Eleven for `BrowserBufferBackend` (which used `#[test]` and never ran before). diff --git a/docs/superpowers/specs/2026-11-17-idb-flush-recovery-design.md b/docs/superpowers/specs/2026-11-17-idb-flush-recovery-design.md new file mode 100644 index 0000000000..a431bfc56b --- /dev/null +++ b/docs/superpowers/specs/2026-11-17-idb-flush-recovery-design.md @@ -0,0 +1,144 @@ +# Spec — 006-idb-flush-recovery (#894) + +**Mission Type:** code (plus error-reference and docs-site text) + +**Base branches:** PR 1 on `v3` (format v8). After PR 1 merges, PR 2 backports to `main` (v2.x, format v7). A `minigraf-docs` PR covers the docs site. Merge methods: `v3` uses a merge commit, `main` squash. + +## 1. User Intent + +> #894 + +Issue #894, "BrowserDb (IndexedDB): a failed flush can leave the store with a meta that references missing pages" (bug, data-integrity, known-issue, v3.0.0). The #896 spike reproduced it on `main`, v2.0.11 and `@minigraf/browser@2.0.11`: a realistic `QuotaExceededError` abort leaves the write's promise pending forever, the next write is acknowledged, and reopen fails with `INT-052`. + +Planning decisions by the user (2026-11-17): + +> After an IndexedDB flush fails, later operations on that handle follow the **failed-handle rule**: writes, commit, checkpoint and import reject with STG-046, as native and OPFS do; close + reopen recovers. (Chosen over cumulative recovery and over a checkpoint-retry hybrid.) + +> The v2.x backport is part of this mission (PR 2 to `main`, after the v3 PR merges). + +> On v2.x, register STG-046 and STG-048 with the v3 meanings. + +> IndexedDB `importGraph()` keeps today's order: the live store is swapped before the flush, so a failed import leaves the imported graph visible in memory and the handle failed; a reopen shows the old store. + +## 2. Goal + +Make an IndexedDB-backed `BrowserDb` never acknowledge a write over pages that did not reach IndexedDB. Every IndexedDB flush must land whole or not at all, and must settle. Once a flush fails, the handle refuses further durable work with STG-046 until it is closed and reopened. That is the rule native (#390) and OPFS (#441) already follow. A reopen then loads a complete, consistent store: the state before the failed flush. Ship the fix in v3.0.0 and in the next v2.x patch. + +## 3. Context & Investigation Findings + +Read at `v3` `df7e8170d` and `main` `82e1ea5e6`. Round-1 reports: `Claude-01.md`, `Codex-01.md`. + +### Mechanism (v3, `src/browser/mod.rs`) +- Every persistent write first commits in memory (`commit_stamped` :851: `load_facts`, `pfs.save()`, `take_commit_flush`), then drops the `RefCell` borrow and awaits `IndexedDbBackend::write_pages`. Paths: `apply_write` :791 (`execute` transact/retract), `BrowserWriteTransaction::commit` :914, `checkpoint` :469, IndexedDB `import_graph` :534 (swaps `inner.pfs` before the await), and the migration/free-page flush in `open` :276. +- `take_dirty_pages` :1216 drains the dirty set before reading bytes. `take_flush` :1259 and `BrowserBufferBackend::release` (`buffer.rs:68`) report each freed id once. A failed flush therefore loses its puts and deletes. The next save writes only its own delta and a meta that references the lost pages. +- `pfs.save()` has already succeeded when the IndexedDB flush fails, so `PersistentFactStorage::failed` (`persistent_facts.rs:46–53`, the STG-046 flag) is never set in IndexedDB mode. OPFS mode writes inside `save()`, so its failures already set it. +- Overlap: the borrow is released before every await, and `commit()` clears `tx_open` before its flush. A later operation can commit in memory and queue its IndexedDB transaction before an earlier flush has failed. IndexedDB runs overlapping readwrite transactions on one store in creation order, but it still commits the later one after the earlier one aborts. + +### IndexedDB adapter (`src/browser/indexeddb.rs`) +- `transaction_to_promise` :38 listens for `complete` and `error`, not `abort`. An abort with no request error, which the realistic quota case produces, never settles. Its `onerror` is `Closure::once`, so each further bubbling request `AbortError` throws "FnOnce called more than once" (spike). +- `write_pages` :155 queues deletes then puts with `?`. A synchronous throw returns without aborting the transaction, so the requests already queued commit (the spike's sync-throw partial commit). +- Flush failures are bare strings (`"IdbTransaction failed"`). OPFS failures are STG-048 (`opfs.rs:47`). + +### Precedent +- STG-046 (ERROR_REFERENCE, v3): after a failed commit that may have written, every later write, checkpoint and index rebuild through the handle fails. Queries keep working, and reopening reads afresh. Its OPFS paragraph (#441) applies the same rule to BrowserDb OPFS mode. IndexedDB mode is the one persistent mode without it. +- STG-048 "Browser storage operation failed" (v3, #441) names the operation and the browser exception. +- `docs/superpowers/specs/2026-10-07-browser-page-reuse-design.md` (#411/#440) deferred failed-flush recovery. The #441 spec (`TandemKit/005-browser-opfs/Spec.md`, D12) delegated it to #894. + +### v2.x (`main`) +- Same shape, format v7: `take_dirty_pages` drains, and `write_pages(dirty_pages)` sends puts only. The header (page 0) is rewritten, fact pages are appended, and index pages are rewritten. `BrowserDb` has `open`, `openInMemory`, `execute`, `checkpoint`, `exportGraph`, `importGraph`, with no transactions or `close()`. `error.rs` ends at STG-045, and native v2 has no failed-handle rule. + +### Tests and CI +- `src/browser/tx_tests.rs:275` `failed_flush_rejects_like_execute` (v3) closes the IDB connection, an unrecoverable failure. Both the failed `execute` and the failed `commit()` reject, their facts stay visible in memory, and a reopen shows the previous state. Under AC4 the commit is admitted after the handle failed, so it must change nothing (AC15). +- `src/browser/opfs_tests.rs:681–741` `submitted_indexeddb_flush_settles_after_close_and_reopen` pins close/reopen behaviour with a submitted IndexedDB flush, and uses a `JsPatch` interception of `IDBDatabase.transaction`. Another test in the same file pins API-018 for checkpoint, import and execute pending at `close()`, in all three modes. +- `.github/workflows/wasm.yml`: `wasm-pack test --headless` in Firefox and Chrome (window and worker lanes) and Safari (`macos-15`). There is no wasm size gate. The browser code is wasm-only. The native size gate (`binary-size.yml`, v3 `SIZE_LIMIT_BYTES` 1,280,000 = 1,250 KiB) is affected only by the error registrations PR 2 adds on `main`. +- The spike harness (`.worktrees/spike-896/spikes/896-idb-flush/`, Playwright, Chromium, CDP quota override) is the only realistic `QuotaExceededError` trigger. Its oracle checks that every page below the v7 header's `page_count` is present. On v3, free pages are deliberately absent (#440), so any v3 use must check pages reachable from the meta instead. + +## 4. Acceptance Criteria + +Terms: +- A **flush** is the one IndexedDB readwrite transaction that stores a commit's pages and page deletes. +- A flush **fails** when its transaction aborts or errors, when queueing a request on it throws, or when building it after the in-memory commit fails (e.g. an unreadable dirty page, `INT-052`). +- A **failed handle** is a `BrowserDb` (and its transactions, cursors and prepared queries) on which a flush has failed. + +**Flush atomicity and settlement** +- AC1. A flush lands whole or not at all. A synchronous throw while its requests are queued leaves IndexedDB exactly as it was, with no request committed. +- AC2. Every flush settles: `complete` resolves, and `error` or `abort` rejects, exactly once. An abort with no request error rejects; it does not hang. Several request errors on one transaction raise no page error or "FnOnce called more than once" exception. +- AC3. A flush failure in IndexedDB rejects its operation with **STG-048**. The message names the operation (IndexedDB write) and the DOM exception name when one exists (e.g. `QuotaExceededError`, `AbortError`). A failure while building the flush after the in-memory commit (e.g. an unreadable dirty page) keeps its own engine code (e.g. INT-052) and fails the handle in the same way. + +**Failed-handle rule** +- AC4. Once a handle is known to have failed, every **newly admitted** `execute` transact/retract, `importGraph()`, `checkpoint()` and `BrowserWriteTransaction.commit()` (empty or not) rejects with **STG-046**. It starts no IndexedDB transaction and changes no database state: no facts, no transaction counter, no live store swap. A commit refused with STG-046 finishes its transaction the way a refused commit does today: the staged facts are discarded, the INT-001 exclusion is cleared, and later calls on that transaction give API-018. `beginWrite()` and a transaction's `execute` (staging, reading) still work. Queries, cursors, prepared queries, rule registration, `exportGraph()` and `close()` keep working. The existing admission rules come first and are unchanged: API-018 after `close()` or on a finished transaction, INT-001 while a transaction is open, API-027 where it applies. +- AC5. Work already applied in memory before the failure became known stays readable on the failed handle. That covers the failed write itself and any overlapping successor applied behind it (AC6), as `failed_flush_rejects_like_execute` asserts today for the failed write. +- AC6. **Persistence order.** Each persistent operation's outcome is ordered after every operation of the handle applied in memory before it. Its flush, if it has one, reaches IndexedDB only after every earlier flush has settled successfully. If an earlier flush fails, a later operation already applied in memory sends nothing and rejects with STG-046. When the earlier flushes succeed, the later one is sent and resolves as today, so overlapping successful writes keep working. +- AC7. **Checkpoint boundary.** `checkpoint()` resolves only once every write applied before it was called is stored. With no earlier durability work outstanding and no pages of its own to flush, it resolves without waiting, as today. Behind an in-flight flush it waits: it resolves if that flush succeeds and rejects with STG-046 if it fails, even when the checkpoint has no pages of its own. Writes applied after the checkpoint was called are not part of its boundary. +- AC8. `close()` is unchanged and does no durable work. A flush not yet started when `close()` runs, including one queued behind a delayed predecessor, is never started, and its operation rejects with API-018 (the existing `commit()` check extended to every path). A flush already submitted settles by its own outcome, and its late completion never overwrites writes made through a fresh handle (`opfs_tests.rs` `submitted_indexeddb_flush_settles_after_close_and_reopen` keeps passing). +- AC9. **Store oracle.** Tests are quiescent: every promise has settled, the handle is closed, and a fresh `BrowserDb.open` of the name follows. That reopen opens without error. Its logical content equals the result of the successful operation prefix up to the first failure (successful transacts, retractions and imports, an import replacing earlier data), and holds nothing from a rejected operation. Every page the selected meta reaches is present. An `exportGraph()` of the reopened database opens natively and passes the native integrity check (v3 `verify()`, run from native test code or an equivalent route; no new `BrowserDb` method). After a failed import, the store's key-to-bytes map equals its map before the import: no imported bytes and no deletes took effect. +- AC10. *(v3 only)* An `open` whose migration/free-page flush fails rejects with STG-048 and returns no handle. A later `open` of the same name succeeds on the unchanged store, migrating again if needed. +- AC11. In-memory and OPFS modes behave as before, and all their existing tests pass. + +**Tests** (wasm-bindgen, `--features browser`, every lane `wasm.yml` runs; names are suggestions) +- AC12. Deterministic tests run against real browser IndexedDB transactions and cover these faults: **a recoverable abort** (the connection stays usable), **an abort-only failure** (no request error), **a throw while queueing after earlier requests were queued**, **several request errors on one transaction**, and the existing **closed-connection** case. Production builds carry no fault-injection control. +- AC13. For each of `execute`, `commit()`, `checkpoint()` (its own flush, and an empty checkpoint behind an in-flight write, both success and failure per AC7) and a **smaller-file `importGraph()`**: the operation whose own flush fails rejects with STG-048 (an empty checkpoint waiting behind it rejects with STG-046 per AC7), the next write, commit, checkpoint and import each reject with STG-046, and the reopen satisfies AC9. +- AC14. Also covered: + - repeated refusal after one injected fault, with only one transaction reaching IndexedDB; + - a **multi-page write** (≥ 400 facts, enough to append pages IndexedDB never held: the spike's INT-052 shape); + - an abort-only fault settling within the test deadline; + - a **flush-preparation failure** (AC3): its error code is asserted, and no later operation submits; + - **overlap** cases, each asserting that no successor transaction is sent, the AC5 memory contract and the unchanged store: execute then execute, commit then execute, a failed write with an import applied behind it, a failed import with a write applied behind it, and checkpoint behind a write; + - an all-success mixed-operation control, in which every overlapping operation resolves and the reopened database equals the logical result of the operations in order (AC9). +- AC15. `failed_flush_rejects_like_execute` is updated. The first failure asserts STG-048. Erin's commit, begun after the handle failed, rejects STG-046, finishes the transaction, and leaves Erin out of memory, so memory holds four names, not five. +- AC16. The adapter has its own tests: abort-only rejection, multiple request errors settling once, and a queue-time throw leaving the store unchanged. + +**v2.x backport (PR 2, `main`)** +- AC17. Same behaviour on v7 for `execute`, `checkpoint` and `importGraph`: AC1–AC7, with AC4 restricted to the v2 API (no transactions, no `close()`); AC9 without `verify()`, with native open of the export as the integrity route; and AC11 for in-memory mode. `main`'s `open` performs no IndexedDB write (`main:src/browser/mod.rs:76–94`). Instead of AC10, test that a fresh open after a rejected write loads the unchanged store and that writes through the fresh handle work. Don't add v3's open-time writeback to `main`. STG-046 and STG-048 are registered on `main` with the v3 code, title and error text. Their ERROR_REFERENCE entries describe only what v2.x has: the IndexedDB browser path, since v2 native has no failed-handle rule. No other v3 code or API is backported. +- AC18. The v2 tests mirror AC12–AC16, minus transactions and `close()`. + +**Evidence and gates** +- AC19. Before the fix, at least the multi-page and abort-only cases fail (or hang, bounded by the test deadline) on unmodified `v3`, and pass after it. The same holds on `main` for PR 2. +- AC20. Evaluator-run evidence: the spike harness's realistic quota case (CDP quota override, Chromium), adapted to the AC9 oracle (v3 free pages are absent by design, so the check covers pages the meta reaches, not a dense range), passes against a PR 1 build and a PR 2 build. The harness itself is not merged. +- AC21. Native `cargo test` and `cargo clippy` (native and `wasm32-unknown-unknown --features browser`) are clean on both branches. Each branch's enforced size gate passes (`binary-size.yml`: v3 `SIZE_LIMIT_BYTES` 1,280,000 = 1,250 KiB; `main`'s own limit), and the PR reports the actual native size delta. `main` gains native-compiled error registrations. No file format, IndexedDB schema/version or public method changes. The fix adds no whole-store rewrite to the healthy path. v3 keeps the page-reuse design's O(change) commits, and `main` keeps its existing write volume. No history or page batches grow with the number of settled flushes; constant-size status such as a sequence counter is fine. + +**Documentation** +- AC22. Core, in the same PR: ERROR_REFERENCE STG-046 gains an IndexedDB paragraph (rule, what stays readable, reopen recovers to the state before the failed flush, a rejected write is never stored) and STG-048 names IndexedDB writes. `src/error.rs` messages match. The `BrowserDb` rustdoc (`execute`, `checkpoint`, `importGraph`, `commit`) states the rule. CHANGELOG, `docs/TEST_COVERAGE.md` and the CLAUDE.md test count are updated, and #421's #894 entry gets a Fix line naming v3.0.0 and the v2.x patch. +- AC23. Docs site (`../minigraf-docs`): the browser/IndexedDB durability text states the rule, tagged `@since` the v2.x patch version (and v3.0.0), without rewriting shared text. +- AC24. No commit or PR text uses a closing keyword for #894. It stays open until a published release has the fix. +- AC25. The approved spec is copied to `docs/superpowers/specs/2026-11-17-idb-flush-recovery-design.md` in PR 1. + +## 5. Edge Cases & Boundaries + +- Validation failures (parse errors, API-019 and the like) are not flush failures. They reject as today and do not fail the handle. +- A `checkpoint()` on a healthy handle with no outstanding earlier flush and no pages of its own resolves without waiting, as today. Behind an in-flight flush it follows AC7. On a failed handle it rejects STG-046, just as native `save` refuses before it checks for dirty state. +- Rule registration (`execute` of a `rule`) is in-memory only and allowed on a failed handle. +- `exportGraph()` on a failed handle exports the in-memory state, including rejected writes. It is not a durability acknowledgement, and the docs say so (the escape hatch the spike observed). +- A failed import: memory shows the imported graph, the handle is failed, and a reopen shows the old store, and no imported bytes or deletes have taken effect (user decision). +- Cursor pins (#543): withheld pages and pinned generations behave as today on a healthy handle. A failed handle frees nothing more. +- Independent handles or tabs writing the same IndexedDB name at the same time are a cross-handle problem and out of scope. Close followed by reopen while an old flush is submitted or queued is in scope (AC8). +- Persistent quota exhaustion: every attempt rejects. There is no background retry. +- A tab kill during a flush leaves IndexedDB at the previous or the new state (IndexedDB transactions are atomic). This needs no new test. + +## 6. Key Decisions + +- **D1 Failed handle, not cumulative recovery (user).** It's one rule across native, OPFS and IndexedDB. It needs no per-page intent bookkeeping under page reuse, import or pins, and it backports to v7 unchanged. A rejected write never becomes durable. Codex preferred cumulative recovery (the handle self-heals and rejected memory changes persist later). The user chose the failed handle. +- **D2 Ordered flushes, not busy rejection.** A later operation's flush waits for its predecessors and is dropped (STG-046) if one fails. Overlapping successful writes keep working as today, and no new API-027 path is added (Codex's recommendation, agreed). +- **D3 STG-048 for the failure itself, STG-046 afterwards.** This matches OPFS. Today's bare strings are replaced. Codex suggested keeping today's error conventions; coded errors are the project rule (#277), and STG-048 already means "browser storage operation failed". +- **D4 Applied work stays visible in memory** (unchanged, #894 "unless decided otherwise"). That covers the failed write and successors applied before the failure was known. Operations admitted after the handle is known to have failed change nothing (AC4). +- **D8 Preparation failures keep their code.** An engine error such as INT-052 while building a flush keeps its code and fails the handle. STG-048 is only for browser storage calls. +- **D5 Import order unchanged (user).** +- **D6 v2 codes (user).** STG-046 and STG-048 are registered on `main` with the v3 meanings. Codes are never recycled, and v3 already owns them. +- **D7 Backport in this mission (user).** PR 2 starts after PR 1 merges. + +## 7. Out of Scope + +- Cumulative retry or self-healing handles. Rolling back the in-memory state of a failed write. +- Repairing stores already damaged by #894 (the old handle's `exportGraph()` is only an observation). +- Cross-tab or cross-handle writer isolation for IndexedDB. +- OPFS, native WAL, file format, IndexedDB schema. +- Merging the #896 spike branch. Closing #894 (it closes after release). +- Binding repos: `minigraf-wasm` re-exports `BrowserDb`, and its v3/v2.x pins pick the fix up at the release cascade. Its README text about failures is updated only if it describes the old behaviour (check, don't assume). + +## 8. Possible Directions & Ideas (non-binding) + +- The failure state could live on `Db` (next to `closed`) or reuse `PersistentFactStorage::failed`. Note that an IndexedDB import replaces `inner.pfs` (`mod.rs:577`), so a flag held only on the old storage would be lost. +- Fault injection can be done from test code by intercepting `IDBDatabase.transaction` / `IDBObjectStore.put`, as `opfs_tests.rs:681–741` (`JsPatch`) and the spike's `harness.js` do, or with a `#[cfg(test)]` hook in `write_pages`. Either way the result must not reach production builds (AC12). +- A per-handle flush chain (a promise or a sequence number of the last settled flush) is one way to meet AC6. +- An abort listener plus idempotent settle (`Closure` that ignores repeats), and `tx.abort()` on any queue-time error, cover AC1–AC2. +- `superpowers:test-driven-development` fits AC19 (failing first on unmodified branches). diff --git a/docs/testing/894-idb-flush-recovery.md b/docs/testing/894-idb-flush-recovery.md new file mode 100644 index 0000000000..0b2095f7f5 --- /dev/null +++ b/docs/testing/894-idb-flush-recovery.md @@ -0,0 +1,112 @@ +# IndexedDB flush recovery — PR 1 evidence (#894) + +Scope: `fix/894-idb-flush-recovery`, based on `df7e8170d4cf3bedcdbf91e85d81f7b7ea1ca959` +(`v3`). The [approved design](../superpowers/specs/2026-11-17-idb-flush-recovery-design.md) +is copied verbatim. No v2 backport or docs-site changes are included. + +## Regression evidence (AC19) + +Before any production change, the new wasm tests ran against the base production +code in headless Firefox. Each isolated run exited 1: + +- `multipage_failed_write_refuses_successors`: the 400-fact write failed the + STG-048 assertion, returning the raw `QuotaExceededError: injected queue failure`. + Run result: 0 passed, 1 failed, 90 filtered out, 0.07 seconds. +- `adapter_abort_only_settles`: the real transaction aborted after its last + request succeeded, with no request error. The promise remained pending and the + test's 1,500 ms deadline rejected it (`deadline: flush did not settle`). + Run result: 0 passed, 1 failed, 90 filtered out, 1.53 seconds. +- An initial combined run also failed the empty-checkpoint boundary assertion; + it subsequently timed out in a failure-refusal test on the old implementation. + The two required cases above were therefore run independently. + +A second control run used an archived base with production files unchanged and +Rust 1.99.1. The strengthened multi-page test failed **the unchanged-durable-bytes +oracle**, and the adapter queue-throw test failed its unchanged-map assertion; +the abort-only case again reached its deadline. Result: **0 passed, 3 failed, +81 filtered out**, 1.63 seconds (`target/verification/red-integrity.log`). +This independently demonstrates partial persistence, not just the old error text. + +The original red runs used Rust 1.94.0. Subsequent verification uses installed Rust 1.99.1, +`CARGO_BUILD_JOBS=4`, one build at a time, and debug information disabled for dev +and test profiles. Caches, build output, browser temporary profiles and logs live +under this worktree's ignored `target/verification/`. The browser test server +requires execution outside the network-restricted sandbox. + +## Test map + +All 13 new tests are `#[wasm_bindgen_test]` in `src/browser/flush_tests.rs` and +participate in the existing window and dedicated-worker browser lanes. Faults +wrap real IndexedDB connections/transactions from test code only. No fault +switch is compiled into production. + +| Acceptance criteria | Tests / assertions | +|---|---| +| AC1–AC3, AC12, AC16 | Adapter abort-only deadline and usable connection; repeated real request errors with a page-error observer; second-put queue throw after a queued delete and put, with the entire key/bytes map unchanged. STG-048 names IndexedDB write and the DOM exception. | +| AC4–AC5, AC13–AC15 | Multi-page execute; transaction commit; checkpoint with its own dirty page; smaller (empty) import. Shared refusal checks transact, retract, repeated checkpoint/import, empty and nonempty commit, unchanged counters/facts/export, finished transactions, INT-001 precedence, reads, cursor, prepared query and rules. Closed connection test now keeps four names, excluding Erin. | +| AC6–AC7, AC14 | Delayed abort across execute→execute, commit→execute, execute→import, import→execute and execute→checkpoint: one transaction submitted, expected memory contents, successor STG-046, unchanged durable map. Successful mixed write/checkpoint/import/commit/retract sequence and a checkpoint excluding a later delayed write. | +| AC3, AC14 | Missing dirty page yields INT-052 for execute, commit and checkpoint and fails the handle. A later preparation failure waits for the earlier success and preserves its durable prefix. | +| AC8 | Close behind a delayed submitted flush prevents the queued execute, commit, checkpoint or import from submitting (API-018); a fresh handle's write survives late completion. Existing submitted-flush and OPFS lifecycle tests remain in their suite. | +| AC9–AC10 | Quiescent close/reopen checks exact logical content and the unchanged durable page map after failures. Reopened exports pass `image_of` and `PersistentFactStorage::verify`, the same image decoder and integrity walker used by native open/verify, compiled for the browser; this covers reachable pages while allowing absent free-page bytes. A failed v7 migration leaves original bytes unchanged and a fresh open migrates successfully. | +| AC21 | Healthy commits retain no settled barrier history; no schema/version, file format or public-method change. Only the existing web-sys dependency's `DomException` bindings are enabled. No whole-store rewrite added to commits. | + +## Verification + +- `cargo build`: passed. +- `cargo test`: **2,149 passed, 23 ignored, 0 failed**, across 87 suite summaries. +- `cargo clippy --target wasm32-unknown-unknown --features browser -- -D warnings`: passed. +- Native strict Clippy initially found `clippy::for_unbounded_range` in the + pre-existing `tests/crash_kill_test.rs:543`; the archived base revision fails + with exactly the same diagnostic. The counter now uses `1..=u64::MAX`, the + suggested explicit bound, with no production-code change. The exact requested + `cargo clippy --all-targets -- -D warnings` then passed. The affected crash-kill + suite was rerun: **2 passed, 0 failed**. This one-line harness change is the + only implementation change outside the browser/error/documentation scope. +- `cargo fmt --check`: passed; the spec copy also matches byte-for-byte. +- Distribution gate, `cargo build --profile dist`: **1,270,880 bytes**, versus + **1,270,864 bytes** before the native error-message change (**+16 bytes**), + below the unchanged **1,280,000-byte** limit by **9,120 bytes**. Both builds + used Rust 1.99.1 and the same dependency lockfile. +- `wasm-pack test --headless --firefox --features browser` (explicit local + `--geckodriver` path): **94 passed, 0 failed**, 1.83 seconds. Firefox 159.0.1, + GeckoDriver 0.37.1. +- Dedicated-worker Firefox, `RUSTFLAGS='--cfg minigraf_browser_worker'` with + the same command plus `--lib`: **127 passed, 0 failed**, 12.21 seconds, + including the OPFS suite and submitted-flush close/reopen regression. The + current worker inventory is 114 inherited tests plus 13 new tests (the older + #441 record's 113 count describes its earlier snapshot). + +No Chrome or Safari run, CDP quota run, or separate native process opening a +browser-produced export was performed here. The AC9 equivalent integrity route +is the shared native decoder/walker compiled into the browser test, as described +above. The deterministic browser tests pass in both local Firefox contexts. + +## Files changed + +| File | Change | +|---|---| +| `src/browser/indexeddb.rs` | Atomic queue rollback, owned listeners, idempotent abort/error/complete settlement and coded write errors. | +| `src/browser/mod.rs` | Ordered per-handle flushes, failure admission, checkpoint boundaries, import-swap-safe failure state and rustdoc. | +| `src/browser/flush_tests.rs` | Thirteen new browser regressions and test-only JavaScript fault wrappers. | +| `src/browser/tx_tests.rs` | Closed-connection first error STG-048, Erin's refused commit STG-046 and four readable names. | +| `Cargo.toml` | Enable `DomException` on the existing optional web-sys dependency. | +| `src/error.rs` | STG-046 text includes IndexedDB flush failures; STG-048's existing format already names operation and exception. | +| `tests/crash_kill_test.rs` | Explicit maximum for the existing counter range to satisfy current strict Clippy. | +| `docs/ERROR_REFERENCE.md` | IndexedDB failed-handle and browser-write error semantics. | +| `docs/TEST_COVERAGE.md` | Browser inventory and regression coverage/evidence link. | +| `CLAUDE.md` | Browser architecture and separate browser test counts. | +| `CHANGELOG.md` | Unreleased v3.0.0 recovery entry. | +| `docs/superpowers/specs/2026-11-17-idb-flush-recovery-design.md` | Immutable approved spec copied verbatim. | +| `docs/testing/894-idb-flush-recovery.md` | This report. | + +## Remaining external work + +- AC20 is evaluator-run realistic Chromium quota evidence, outside this round's + requested implementation scope. The deterministic tests here inject real + transaction aborts and synchronous DOM exceptions, not CDP quota exhaustion. +- PR 2 (the next v2.x patch) and the docs-site PR remain separate. +- The user required writes only inside this worktree. No remote issue was edited. + The corresponding prepared update for #421's #894 entry is: + + **Fix:** v3.0.0; backport planned for the next v2.x patch (PR 2 after PR 1 merges). + Keep #894 open until the fix is published. diff --git a/src/browser/flush_tests.rs b/src/browser/flush_tests.rs new file mode 100644 index 0000000000..c74e33372d --- /dev/null +++ b/src/browser/flush_tests.rs @@ -0,0 +1,657 @@ +//! IndexedDB flush failures (#894). Faults wrap real browser transactions; +//! no injection controls are compiled into the library. +use super::*; +use js_sys::{Function, Promise, Reflect, Uint8Array}; +use wasm_bindgen_futures::{JsFuture, future_to_promise}; +use wasm_bindgen_test::*; + +const QUERY: &str = "(query [:find ?n :where [?e :n ?n] :order-by [?n :asc]])"; + +fn name() -> String { + format!( + "flush-894-{}-{}", + js_sys::Date::now(), + js_sys::Math::random() + ) +} + +fn write(first: usize, end: usize) -> String { + let facts: String = (first..end).map(|i| format!("[:e{i} :n {i}]")).collect(); + format!("(transact [{facts}])") +} + +fn code(result: Result, expected: &str) { + let Err(e) = result else { + panic!("expected rejection {expected}") + }; + let message = js_text(&e); + assert!(message.starts_with(&format!("[{expected}]")), "{message}"); +} + +/// A bounded wait also catches the old abort-only hang. Clear its timer on +/// either outcome so a passing test leaves no work behind. +async fn deadline(p: Promise) -> Result { + let bounded: Promise = Function::new_with_args( + "p", + r#" + return new Promise((resolve,reject) => { + const timer = setTimeout(() => reject('deadline: flush did not settle'), 1500); + p.then(v => { clearTimeout(timer); resolve(v); }, + e => { clearTimeout(timer); reject(e); }); + }); + "#, + ) + .call1(&JsValue::NULL, &p) + .unwrap() + .unchecked_into(); + JsFuture::from(bounded).await +} + +fn execute(db: &BrowserDb, command: String) -> Promise { + let inner = db.inner.clone(); + future_to_promise(async move { + BrowserDb { inner } + .execute(command) + .await + .map(JsValue::from) + }) +} + +fn checkpoint(db: &BrowserDb) -> Promise { + let inner = db.inner.clone(); + future_to_promise(async move { + BrowserDb { inner } + .checkpoint() + .await + .map(|()| JsValue::UNDEFINED) + }) +} + +fn import(db: &BrowserDb, bytes: Uint8Array) -> Promise { + let inner = db.inner.clone(); + future_to_promise(async move { + BrowserDb { inner } + .import_graph(bytes) + .await + .map(|()| JsValue::UNDEFINED) + }) +} + +async fn tick() { + JsFuture::from(Promise::resolve(&JsValue::NULL)) + .await + .unwrap(); +} + +async fn rows(db: &BrowserDb) -> Vec { + let json = db.execute(QUERY.into()).await.unwrap(); + let value: serde_json::Value = serde_json::from_str(&json).unwrap(); + value["results"] + .as_array() + .unwrap() + .iter() + .map(|r| r[0].as_u64().unwrap() as usize) + .collect() +} + +fn adapter(db: &BrowserDb) -> IndexedDbBackend { + db.inner.borrow().idb.as_ref().unwrap().clone_handle() +} + +/// Patch only one connection (or, for open(), the prototype with a name +/// filter). Restoration also removes the page-error observer. +struct Fault { + state: JsValue, + restore: Function, +} +impl Fault { + fn new(target: &JsValue, store: &str, mode: &str) -> Self { + let state = js_sys::Object::new(); + let restore = Function::new_with_args("target,name,mode,state", r#" + const descriptor = Object.getOwnPropertyDescriptor(target,'transaction'); + const original = target.transaction; + state.sent = 0; state.errors = 0; state.pageErrors = []; + const pageError = e => { state.pageErrors.push(e.message); e.preventDefault(); }; + globalThis.addEventListener('error',pageError); + Object.defineProperty(target,'transaction',{configurable:true,value:function(...args) { + const tx = original.apply(this,args); + if (args[0] !== name || args[1] !== 'readwrite') return tx; + state.sent++; + if (state.sent !== 1) return tx; + tx.addEventListener('error',() => state.errors++); + if (mode === 'hold') { + const setter = Object.getOwnPropertyDescriptor(IDBTransaction.prototype,'oncomplete').set; + Object.defineProperty(tx,'oncomplete',{set(callback) { + setter.call(tx,event => { state.resume = () => callback(event); }); + }}); + } + const objectStore = tx.objectStore; + tx.objectStore = function(...args) { + const s = objectStore.apply(this,args), put = s.put; + let queued = 0, last; + s.put = function(...args) { + if (mode === 'throw' && queued++ === 1) + throw new DOMException('injected queue failure','QuotaExceededError'); + // add() of existing keys yields real request errors. + const req = mode === 'errors' ? s.add(...args) : put.apply(this,args); + last = req; + if (mode === 'abort' || mode === 'delayed-abort') { + req.addEventListener('success',() => { + if (req !== last) return; + // No outstanding requests: abort event only. + tx.abort(); + }); + } + return req; + }; + return s; + }; + if (mode === 'delayed-abort') { + const setter = Object.getOwnPropertyDescriptor(IDBTransaction.prototype,'onabort').set; + Object.defineProperty(tx,'onabort',{set(callback) { + setter.call(tx,event => { state.resume = () => callback(event); }); + }}); + } + return tx; + }}); + return () => { + if (descriptor) Object.defineProperty(target,'transaction',descriptor); + else delete target.transaction; + globalThis.removeEventListener('error',pageError); + }; + "#).call4(&JsValue::NULL, target, &store.into(), &mode.into(), &state).unwrap().unchecked_into(); + Self { + state: state.into(), + restore, + } + } + fn for_db(db: &BrowserDb, mode: &str) -> Self { + let idb = adapter(db); + Self::new(&idb.db, &idb.store_name, mode) + } + fn number(&self, key: &str) -> f64 { + Reflect::get(&self.state, &key.into()) + .unwrap() + .as_f64() + .unwrap() + } + async fn resume(&self) { + let p: Promise = Function::new_with_args( + "s", + r#" + return new Promise((resolve,reject) => { + const end = Date.now()+1500; + function poll() { + if (s.resume) { s.resume(); resolve(); } + else if (Date.now()>end) reject('no held outcome'); + else setTimeout(poll,0); + } + poll(); + }); + "#, + ) + .call1(&JsValue::NULL, &self.state) + .unwrap() + .unchecked_into(); + JsFuture::from(p).await.unwrap(); + } + fn no_page_errors(&self) { + let errors: js_sys::Array = Reflect::get(&self.state, &"pageErrors".into()) + .unwrap() + .unchecked_into(); + assert_eq!(errors.length(), 0, "callbacks must not throw"); + } +} +impl Drop for Fault { + fn drop(&mut self) { + self.restore.call0(&JsValue::NULL).unwrap(); + } +} + +async fn reopen(n: &str, expected: Vec) { + let db = BrowserDb::open(n).await.expect("fresh reopen"); + assert_eq!(rows(&db).await, expected); + // The exact integrity walker used by native Minigraf::verify(), over a + // native-format export; catches absent reachable pages, not free holes. + assert!(db.inner.borrow().pfs.verify().unwrap().problems.is_empty()); + image_of(db.export_graph().unwrap().to_vec()) + .expect("export passes native image integrity walk"); + db.close().unwrap(); +} + +async fn refused(db: &BrowserDb) { + let before = rows(db).await; + let count = db.inner.borrow().fact_storage.current_tx_count(); + let bytes = db.export_graph().unwrap().to_vec(); + for _ in 0..2 { + code(db.execute(write(900, 901)).await, "STG-046"); + code(db.execute("(retract [[:e0 :n 0]])".into()).await, "STG-046"); + code(db.checkpoint().await, "STG-046"); + code( + db.import_graph(Uint8Array::new_with_length(0)).await, + "STG-046", + ); + for nonempty in [false, true] { + let mut tx = db.begin_write().unwrap(); + if nonempty { + tx.execute(&write(901, 902)).unwrap(); + } + tx.execute(QUERY).unwrap(); + code(db.checkpoint().await, "INT-001"); + code(deadline(tx.commit()).await, "STG-046"); + code(tx.execute(QUERY), "API-018"); + code(deadline(tx.commit()).await, "API-018"); + } + } + assert_eq!(db.inner.borrow().fact_storage.current_tx_count(), count); + assert_eq!(rows(db).await, before); + assert_eq!(db.export_graph().unwrap().to_vec(), bytes); + let prepared = db.prepare(QUERY).unwrap(); + prepared.execute(JsValue::UNDEFINED).unwrap(); + let mut cursor = db.query(QUERY).unwrap(); + cursor.next_batch(2).unwrap(); + db.execute("(rule [(number ?e ?n) [?e :n ?n]])".into()) + .await + .unwrap(); +} + +#[wasm_bindgen_test] +async fn adapter_abort_only_settles() { + let n = name(); + let idb = IndexedDbBackend::open(&n).await.unwrap(); + idb.write_pages(vec![(0, vec![1])], vec![]).await.unwrap(); + let before = idb.load_all_pages().await.unwrap(); + let fault = Fault::new(&idb.db, &n, "abort"); + let copy = idb.clone_handle(); + let p = future_to_promise(async move { + copy.write_pages(vec![(0, vec![2]), (1, vec![3])], vec![]) + .await + .map(|()| JsValue::UNDEFINED) + }); + let error = deadline(p) + .await + .expect_err("abort rejects before deadline"); + assert!(js_text(&error).contains("STG-048"), "{}", js_text(&error)); + assert!(js_text(&error).contains("IndexedDB write")); + assert!(js_text(&error).contains("AbortError")); + assert_eq!(fault.number("errors"), 0.0, "abort-only, no request errors"); + assert_eq!(idb.load_all_pages().await.unwrap(), before); + fault.no_page_errors(); + drop(fault); + idb.write_pages(vec![(1, vec![4])], vec![]) + .await + .expect("connection is recoverable"); + idb.db.close(); +} + +#[wasm_bindgen_test] +async fn adapter_queue_throw_is_atomic() { + let n = name(); + let idb = IndexedDbBackend::open(&n).await.unwrap(); + idb.write_pages(vec![(0, vec![1]), (9, vec![9])], vec![]) + .await + .unwrap(); + let before = idb.load_all_pages().await.unwrap(); + let fault = Fault::new(&idb.db, &n, "throw"); + let error = idb + .write_pages(vec![(0, vec![2]), (1, vec![3])], vec![9]) + .await + .unwrap_err(); + assert_eq!( + idb.load_all_pages().await.unwrap(), + before, + "queued delete and put must both roll back" + ); + assert!(js_text(&error).contains("STG-048")); + assert!(js_text(&error).contains("QuotaExceededError")); + fault.no_page_errors(); + idb.db.close(); +} + +#[wasm_bindgen_test] +async fn adapter_multiple_request_errors_settle_once() { + let n = name(); + let idb = IndexedDbBackend::open(&n).await.unwrap(); + let pages = vec![(0, vec![1]), (1, vec![2]), (2, vec![3])]; + idb.write_pages(pages.clone(), vec![]).await.unwrap(); + let before = idb.load_all_pages().await.unwrap(); + let fault = Fault::new(&idb.db, &n, "errors"); + code(idb.write_pages(pages, vec![]).await, "STG-048"); + assert_eq!(idb.load_all_pages().await.unwrap(), before); + assert!(fault.number("errors") >= 2.0); + fault.no_page_errors(); + idb.db.close(); +} + +#[wasm_bindgen_test] +async fn multipage_failed_write_refuses_successors() { + let n = name(); + let db = BrowserDb::open(&n).await.unwrap(); + db.execute(write(0, 2)).await.unwrap(); + let before = adapter(&db).load_all_pages().await.unwrap(); + let fault = Fault::for_db(&db, "throw"); + let failed = db.execute(write(2, 402)).await; + assert!( + adapter(&db).load_all_pages().await.unwrap() == before, + "multi-page failure must leave every durable byte unchanged" + ); + code(failed, "STG-048"); + assert_eq!(rows(&db).await.len(), 402); + assert!( + db.inner + .borrow() + .pfs + .with_backend(|b| buffer_page_ids(b).any(|id| !before.contains_key(&id))), + "write appended pages" + ); + refused(&db).await; + assert_eq!(fault.number("sent"), 1.0); + assert_eq!(adapter(&db).load_all_pages().await.unwrap(), before); + drop(fault); + db.close().unwrap(); + reopen(&n, vec![0, 1]).await; +} + +#[wasm_bindgen_test] +async fn commit_checkpoint_and_smaller_import_fail_atomically() { + for operation in ["commit", "checkpoint", "import"] { + let n = name(); + let db = BrowserDb::open(&n).await.unwrap(); + db.execute(write(0, 400)).await.unwrap(); + let before = adapter(&db).load_all_pages().await.unwrap(); + let fault = Fault::for_db(&db, "abort"); + let p = match operation { + "commit" => { + let mut tx = db.begin_write().unwrap(); + tx.execute(&write(400, 401)).unwrap(); + tx.commit() + } + "checkpoint" => { + // Real dirty bytes of its own, without another flush. + db.inner.borrow_mut().pfs.with_backend_mut(|b| { + let page = b.read_page(0).unwrap(); + b.write_page(0, &page).unwrap(); + }); + checkpoint(&db) + } + _ => import(&db, Uint8Array::new_with_length(0)), + }; + code(deadline(p).await, "STG-048"); + assert_eq!( + rows(&db).await.len(), + match operation { + "commit" => 401, + "import" => 0, + _ => 400, + } + ); + refused(&db).await; + assert_eq!(fault.number("sent"), 1.0); + assert_eq!(adapter(&db).load_all_pages().await.unwrap(), before); + drop(fault); + db.close().unwrap(); + reopen(&n, (0..400).collect()).await; + } +} + +#[wasm_bindgen_test] +async fn preparation_error_keeps_code_and_fails_handle() { + for operation in ["execute", "commit", "checkpoint"] { + let n = name(); + let db = BrowserDb::open(&n).await.unwrap(); + db.execute(write(0, 2)).await.unwrap(); + let fault = Fault::for_db(&db, "hold"); + db.inner.borrow_mut().pfs.with_backend_mut(|b| { + if let BrowserBackend::Buffer(buffer) = b { + buffer.mark_dirty_for_test(999_999); + } + }); + let p = match operation { + "commit" => { + let mut tx = db.begin_write().unwrap(); + tx.execute(&write(2, 3)).unwrap(); + tx.commit() + } + "checkpoint" => checkpoint(&db), + _ => execute(&db, write(2, 3)), + }; + code(deadline(p).await, "INT-052"); + refused(&db).await; + assert_eq!(fault.number("sent"), 0.0); + db.close().unwrap(); + reopen(&n, vec![0, 1]).await; + } +} + +#[wasm_bindgen_test] +async fn overlapping_failure_never_submits_successor() { + for pair in [ + "execute-execute", + "commit-execute", + "execute-import", + "import-execute", + "execute-checkpoint", + ] { + let n = name(); + let db = BrowserDb::open(&n).await.unwrap(); + db.execute(write(0, 2)).await.unwrap(); + let before = adapter(&db).load_all_pages().await.unwrap(); + let fault = Fault::for_db(&db, "delayed-abort"); + let first = if pair.starts_with("commit") { + let mut tx = db.begin_write().unwrap(); + tx.execute(&write(2, 402)).unwrap(); + tx.commit() + } else if pair.starts_with("import") { + import(&db, Uint8Array::new_with_length(0)) + } else { + execute(&db, write(2, 402)) + }; + tick().await; + let second = if pair.ends_with("import") { + import(&db, Uint8Array::new_with_length(0)) + } else if pair.ends_with("checkpoint") { + checkpoint(&db) + } else { + execute(&db, write(500, 501)) + }; + tick().await; + assert_eq!(fault.number("sent"), 1.0, "successor waits"); + assert_eq!( + rows(&db).await.len(), + match pair { + "execute-import" => 0, + "import-execute" => 1, + "execute-checkpoint" => 402, + _ => 403, + } + ); + fault.resume().await; + code(deadline(first).await, "STG-048"); + code(deadline(second).await, "STG-046"); + refused(&db).await; + assert_eq!(fault.number("sent"), 1.0); + assert_eq!(adapter(&db).load_all_pages().await.unwrap(), before); + drop(fault); + db.close().unwrap(); + reopen(&n, vec![0, 1]).await; + } +} + +#[wasm_bindgen_test] +async fn successful_checkpoint_boundary_and_mixed_overlap() { + let n = name(); + let db = BrowserDb::open(&n).await.unwrap(); + db.execute(write(0, 2)).await.unwrap(); + let fault = Fault::for_db(&db, "hold"); + let first = execute(&db, write(2, 402)); + tick().await; + let boundary = checkpoint(&db); + let observed = js_sys::Object::new(); + Function::new_with_args("p,s", "p.then(() => s.done=true)") + .call2(&JsValue::NULL, &boundary, &observed) + .unwrap(); + tick().await; + assert!( + !Reflect::get(&observed, &"done".into()).unwrap().is_truthy(), + "empty checkpoint waits" + ); + let replace = import(&db, Uint8Array::new_with_length(0)); + tick().await; + let mut tx = db.begin_write().unwrap(); + tx.execute(&write(600, 602)).unwrap(); + let commit = tx.commit(); + let retract = execute(&db, "(retract [[:e600 :n 600]])".into()); + tick().await; + assert_eq!(fault.number("sent"), 1.0); + fault.resume().await; + for p in [first, boundary, replace, commit, retract] { + deadline(p).await.unwrap(); + } + assert_eq!(rows(&db).await, vec![601]); + drop(fault); + db.close().unwrap(); + reopen(&n, vec![601]).await; +} + +#[wasm_bindgen_test] +async fn close_discards_every_unstarted_flush() { + for operation in ["execute", "commit", "checkpoint", "import"] { + let n = name(); + let db = BrowserDb::open(&n).await.unwrap(); + db.execute(write(0, 2)).await.unwrap(); + let fault = Fault::for_db(&db, "hold"); + let first = execute(&db, write(2, 3)); + tick().await; + let second = match operation { + "commit" => { + let mut tx = db.begin_write().unwrap(); + tx.execute(&write(3, 4)).unwrap(); + tx.commit() + } + "checkpoint" => checkpoint(&db), + "import" => import(&db, Uint8Array::new_with_length(0)), + _ => execute(&db, write(3, 4)), + }; + tick().await; + db.close().unwrap(); + let fresh = BrowserDb::open(&n).await.unwrap(); + fresh.execute(write(9, 10)).await.unwrap(); + fault.resume().await; + deadline(first).await.unwrap(); + code(deadline(second).await, "API-018"); + assert_eq!(fault.number("sent"), 1.0); + drop(fault); + fresh.close().unwrap(); + reopen(&n, vec![0, 1, 2, 9]).await; + } +} + +#[wasm_bindgen_test] +async fn migration_flush_failure_leaves_original_pages() { + let n = name(); + let idb = IndexedDbBackend::open(&n).await.unwrap(); + let bytes = include_bytes!("../../tests/golden/v7_basic.graph"); + let pages: Vec<_> = bytes + .chunks(crate::storage::PAGE_SIZE) + .enumerate() + .map(|(i, p)| (i as u64, p.to_vec())) + .collect(); + idb.write_pages(pages, vec![]).await.unwrap(); + let before = idb.load_all_pages().await.unwrap(); + let prototype = Reflect::get(&js_sys::global(), &"IDBDatabase".into()).unwrap(); + let prototype = Reflect::get(&prototype, &"prototype".into()).unwrap(); + let fault = Fault::new(&prototype, &n, "throw"); + code(BrowserDb::open(&n).await, "STG-048"); + assert_eq!(idb.load_all_pages().await.unwrap(), before); + drop(fault); + let db = BrowserDb::open(&n).await.unwrap(); + image_of(db.export_graph().unwrap().to_vec()).unwrap(); + db.close().unwrap(); + idb.db.close(); +} + +#[wasm_bindgen_test] +async fn checkpoint_does_not_wait_for_later_writes() { + let n = name(); + let db = BrowserDb::open(&n).await.unwrap(); + let first_fault = Fault::for_db(&db, "hold"); + let first = execute(&db, write(0, 2)); + tick().await; + let boundary = checkpoint(&db); + tick().await; + // Restore admission of transactions, retaining the first held callback. + first_fault.restore.call0(&JsValue::NULL).unwrap(); + let later_fault = Fault::for_db(&db, "hold"); + let later = execute(&db, write(2, 3)); + tick().await; + first_fault.resume().await; + deadline(first).await.unwrap(); + deadline(boundary) + .await + .expect("boundary excludes later pending flush"); + later_fault.resume().await; + deadline(later).await.unwrap(); + drop(later_fault); + drop(first_fault); + db.close().unwrap(); + reopen(&n, vec![0, 1, 2]).await; +} + +#[wasm_bindgen_test] +async fn preparation_failure_preserves_earlier_successful_prefix() { + let n = name(); + let db = BrowserDb::open(&n).await.unwrap(); + let fault = Fault::for_db(&db, "hold"); + let first = execute(&db, write(0, 2)); + tick().await; + db.inner.borrow_mut().pfs.with_backend_mut(|b| { + if let BrowserBackend::Buffer(buffer) = b { + buffer.mark_dirty_for_test(999_999); + } + }); + let broken = execute(&db, write(2, 3)); + let observed = js_sys::Object::new(); + Function::new_with_args("p,s", "p.catch(() => s.done=true)") + .call2(&JsValue::NULL, &broken, &observed) + .unwrap(); + tick().await; + assert!( + !Reflect::get(&observed, &"done".into()).unwrap().is_truthy(), + "even preparation failure waits for earlier work" + ); + code(db.checkpoint().await, "STG-046"); + fault.resume().await; + deadline(first).await.unwrap(); + code(deadline(broken).await, "INT-052"); + assert_eq!(fault.number("sent"), 1.0); + assert!( + db.inner.flush_tail.borrow().is_none(), + "settled barriers are released" + ); + drop(fault); + db.close().unwrap(); + reopen(&n, vec![0, 1]).await; +} + +#[wasm_bindgen_test] +async fn validation_errors_do_not_fail_handle_or_retain_flushes() { + let n = name(); + let db = BrowserDb::open(&n).await.unwrap(); + assert!(db.execute("(transact [[:bad :n]])".into()).await.is_err()); + assert!( + db.import_graph(Uint8Array::new_with_length(1)) + .await + .is_err() + ); + for i in 0..20 { + db.execute(write(i, i + 1)).await.unwrap(); + db.checkpoint().await.unwrap(); + assert!(db.inner.failed_flush.get().is_none()); + assert!( + db.inner.flush_tail.borrow().is_none(), + "no settled history retained" + ); + } + db.close().unwrap(); + reopen(&n, (0..20).collect()).await; +} diff --git a/src/browser/indexeddb.rs b/src/browser/indexeddb.rs index 1f1ccbaf75..64453ce37b 100644 --- a/src/browser/indexeddb.rs +++ b/src/browser/indexeddb.rs @@ -4,7 +4,9 @@ //! Called directly by `BrowserDb` after synchronous `PersistentFactStorage::save()`. use js_sys::{Array, Promise, Uint8Array}; +use std::cell::Cell; use std::collections::HashMap; +use std::rc::Rc; use wasm_bindgen::JsCast; use wasm_bindgen::closure::Closure; use wasm_bindgen::prelude::*; @@ -34,25 +36,104 @@ fn request_to_promise(request: &IdbRequest) -> Promise { }) } -/// Converts an `IdbTransaction` completion into a JS `Promise`. -fn transaction_to_promise(tx: &IdbTransaction) -> Promise { - let tx = tx.clone(); - Promise::new(&mut |resolve, reject| { - let on_complete: Closure = - Closure::once(move |_: web_sys::Event| { - resolve.call0(&JsValue::NULL).ok(); - }); - let on_error: Closure = - Closure::once(move |_: web_sys::Event| { - reject - .call1(&JsValue::NULL, &JsValue::from_str("IdbTransaction failed")) - .ok(); - }); - tx.set_oncomplete(Some(on_complete.as_ref().unchecked_ref())); - tx.set_onerror(Some(on_error.as_ref().unchecked_ref())); - on_complete.forget(); - on_error.forget(); - }) +/// Own the transaction's listeners until settlement. Request errors bubble to +/// the transaction, potentially more than once; all three events share one +/// idempotent settlement flag. Dropping this guard detaches and frees them. +struct TransactionWait { + tx: IdbTransaction, + promise: Promise, + _complete: Closure, + _error: Closure, + _abort: Closure, +} + +impl TransactionWait { + fn new(tx: &IdbTransaction) -> Self { + let settled = Rc::new(Cell::new(false)); + let mut listeners = None; + let promise = Promise::new(&mut |resolve, reject| { + let done = settled.clone(); + let complete: Closure = + Closure::new(move |_: web_sys::Event| { + if !done.replace(true) { + let _ = resolve.call0(&JsValue::NULL); + } + }); + let done = settled.clone(); + let failed_tx = tx.clone(); + let reject_error = reject.clone(); + let error: Closure = + Closure::new(move |event: web_sys::Event| { + if !done.replace(true) { + // On a bubbling request error tx.error may not yet be + // set. Preserve the request's original exception name. + let exception = event + .target() + .and_then(|t| t.dyn_into::().ok()) + .and_then(|r| r.error().ok().flatten()) + .or_else(|| failed_tx.error()); + let reason = transaction_error(exception); + // Never allow a prevented request error to commit a + // prefix of this batch. + let _ = failed_tx.abort(); + let _ = reject_error.call1(&JsValue::NULL, &reason); + } + }); + let done = settled.clone(); + let aborted_tx = tx.clone(); + let abort: Closure = + Closure::new(move |_: web_sys::Event| { + if !done.replace(true) { + let reason = transaction_error(aborted_tx.error()); + let _ = reject.call1(&JsValue::NULL, &reason); + } + }); + tx.set_oncomplete(Some(complete.as_ref().unchecked_ref())); + tx.set_onerror(Some(error.as_ref().unchecked_ref())); + tx.set_onabort(Some(abort.as_ref().unchecked_ref())); + listeners = Some((complete, error, abort)); + }); + // Promise::new invokes its executor synchronously. + let (complete, error, abort) = match listeners { + Some(listeners) => listeners, + None => unreachable!("Promise executor runs synchronously"), + }; + Self { + tx: tx.clone(), + promise, + _complete: complete, + _error: error, + _abort: abort, + } + } +} + +impl Drop for TransactionWait { + fn drop(&mut self) { + self.tx.set_oncomplete(None); + self.tx.set_onerror(None); + self.tx.set_onabort(None); + } +} + +fn write_error(error: JsValue) -> JsValue { + let name = js_sys::Reflect::get(&error, &"name".into()) + .ok() + .and_then(|n| n.as_string()) + .unwrap_or_else(|| super::js_text(&error)); + super::to_js_error(crate::error::err_coded!( + crate::error::ErrorCode::Stg048, + "IndexedDB write", + name + )) +} + +fn transaction_error(exception: Option) -> JsValue { + write_error(exception.map(JsValue::from).unwrap_or_else(|| { + // Explicit abort() has no transaction.error, but its DOM outcome is + // still AbortError. + JsValue::from_str("AbortError") + })) } /// Async wrapper around a browser IndexedDB database. @@ -165,23 +246,29 @@ impl IndexedDbBackend { } let tx = self .db - .transaction_with_str_and_mode(&self.store_name, IdbTransactionMode::Readwrite)?; - let store = tx.object_store(&self.store_name)?; - - for page_id in &deletes { - store.delete(&JsValue::from_f64(*page_id as f64))?; - } - for (page_id, data) in &pages { - let key = JsValue::from_f64(*page_id as f64); - let arr = Uint8Array::from(data.as_slice()); - store.put_with_key(&arr, &key)?; + .transaction_with_str_and_mode(&self.store_name, IdbTransactionMode::Readwrite) + .map_err(write_error)?; + let wait = TransactionWait::new(&tx); + let queued = (|| { + let store = tx.object_store(&self.store_name)?; + for page_id in &deletes { + store.delete(&JsValue::from_f64(*page_id as f64))?; + } + for (page_id, data) in &pages { + let key = JsValue::from_f64(*page_id as f64); + let arr = Uint8Array::from(data.as_slice()); + store.put_with_key(&arr, &key)?; + } + Ok::<(), JsValue>(()) + })(); + if let Err(error) = queued { + // A synchronous throw does not abort IndexedDB automatically. + // Roll back every earlier put/delete, then observe settlement. + let _ = tx.abort(); + let _ = JsFuture::from(wait.promise.clone()).await; + return Err(write_error(error)); } - - // Wait for the transaction to commit. The IDB transaction commits - // automatically once all requests have been processed and no new - // requests are made. We wait here to ensure durability before - // returning to the caller. - JsFuture::from(transaction_to_promise(&tx)).await?; + JsFuture::from(wait.promise.clone()).await?; Ok(()) } } diff --git a/src/browser/mod.rs b/src/browser/mod.rs index c7b320dbc9..7e4399fff0 100644 --- a/src/browser/mod.rs +++ b/src/browser/mod.rs @@ -11,6 +11,9 @@ mod backend; mod binds; /// Synchronous in-memory page buffer with dirty-page tracking. pub mod buffer; +/// IndexedDB atomicity, settlement and ordered flush regressions (#894). +#[cfg(test)] +mod flush_tests; /// Async IndexedDB backend for browser WASM persistence. pub mod indexeddb; /// OPFS file backend for `BrowserDb.openOpfs` (#441). @@ -64,6 +67,12 @@ struct Db { epoch: Cell, /// OPFS mode's file and where it lives; `None` in the other modes. opfs: Option, + /// First failed IndexedDB flush, independent of the replaceable pfs. + failed_flush: Cell>, + /// Only the last outstanding barrier is retained; settled batches and + /// predecessor promises are released, not a history of all writes. + flush_tail: RefCell>, + flush_sequence: Cell, } struct OpfsMode { @@ -89,6 +98,9 @@ impl Db { running: Cell::new(0), epoch: Cell::new(0), opfs, + failed_flush: Cell::new(None), + flush_tail: RefCell::new(None), + flush_sequence: Cell::new(0), }) } @@ -122,6 +134,87 @@ impl Db { Ok(()) } + fn refuse_failed_flush(&self) -> Result<(), JsValue> { + if self.failed_flush.get().is_some() { + return Err(to_js_error(err_coded!(ErrorCode::Stg046))); + } + Ok(()) + } + + fn fail_flush(&self, sequence: u64) { + self.failed_flush.set(Some( + self.failed_flush + .get() + .map_or(sequence, |old| old.min(sequence)), + )); + } + + /// Reserve the durability boundary synchronously, before a commit's JS + /// promise body is deferred. Barriers always resolve: each operation + /// reports its own error, with no unhandled predecessor rejection. + fn flush(self: &Rc, work: FlushWork) -> js_sys::Promise { + let idb = self + .borrow() + .idb + .as_ref() + .map(IndexedDbBackend::clone_handle); + let Some(idb) = idb else { + return match work.0 { + Ok(_) => js_sys::Promise::resolve(&JsValue::UNDEFINED), + Err(error) => js_sys::Promise::reject(&error), + }; + }; + let sequence = self.flush_sequence.get() + 1; + self.flush_sequence.set(sequence); + if work.0.is_err() { + // Preparation failed after memory changed. Record it now, but + // allow earlier flushes to finish their own successful prefix. + self.fail_flush(sequence); + } + let previous = self.flush_tail.borrow_mut().take(); + if previous.is_none() && work.0.as_ref().is_ok_and(Flush::is_empty) { + return js_sys::Promise::resolve(&JsValue::UNDEFINED); + } + let mut release = None; + let barrier = js_sys::Promise::new(&mut |resolve, _| release = Some(resolve)); + *self.flush_tail.borrow_mut() = Some((sequence, barrier)); + let db = self.clone(); + wasm_bindgen_futures::future_to_promise(async move { + if let Some((_, previous)) = previous { + let _ = wasm_bindgen_futures::JsFuture::from(previous).await; + } + let result = async { + if db.closed.get() { + return Err(closed_error("database")); + } + if db + .failed_flush + .get() + .is_some_and(|failed| failed < sequence) + { + return Err(to_js_error(err_coded!(ErrorCode::Stg046))); + } + let flush = work.0?; + idb.write_pages(flush.puts, flush.deletes) + .await + .inspect_err(|_| db.fail_flush(sequence)) + } + .await; + if db + .flush_tail + .borrow() + .as_ref() + .is_some_and(|(last, _)| *last == sequence) + { + db.flush_tail.borrow_mut().take(); + } + if let Some(release) = release { + let _ = release.call0(&JsValue::NULL); + } + result.map(|()| JsValue::UNDEFINED) + }) + } + /// Count a running query until the guard drops. fn run(&self) -> Running<'_> { self.running.set(self.running.get() + 1); @@ -229,6 +322,13 @@ fn refuse_in_tx(db: &Db) -> Result<(), JsValue> { /// `execute`, `checkpoint`, `importGraph` and a transaction's `commit` /// return `Promise`s; the other methods are synchronous. /// +/// IndexedDB flushes are atomic and ordered. A failed flush rejects with +/// `[STG-048]`; a failure preparing pages keeps its engine code. The handle +/// then refuses new writes, commits, checkpoints and imports with `[STG-046]` +/// until close and reopen. Reads and export still include work already applied +/// in memory, including rejected work; export is not a durability acknowledgement. +/// Reopen recovers the successful prefix before the failed flush. +/// /// **Not compatible with Node.js.** Use `@minigraf/node` for server-side use. #[wasm_bindgen] pub struct BrowserDb { @@ -368,6 +468,10 @@ impl BrowserDb { /// Execute a Datalog command string and return a JSON-encoded result. /// + /// In IndexedDB mode, a failed flush rejects with `[STG-048]` and later + /// transact/retract calls reject with `[STG-046]` without changing memory. + /// Queries and rules remain available; close and reopen to recover. + /// /// Returns a `Promise` in JavaScript. The JSON shape is: /// - Query: `{"variables": [...], "results": [[...], ...]}` /// - Transact: `{"transacted": }` @@ -402,6 +506,7 @@ impl BrowserDb { return Ok(query_result_to_json(result)); } + self.inner.refuse_failed_flush()?; match cmd { DatalogCommand::Transact(tx) => { let facts = @@ -462,25 +567,26 @@ impl BrowserDb { /// Flush all dirty pages to IndexedDB. /// - /// Write-through means individual `execute()` calls already flush dirty pages, - /// so `checkpoint()` is only needed after `import_graph()` or explicit bulk ops. - /// No-op for in-memory databases, and in OPFS mode, where every commit is - /// already stored in the file. + /// Waits for every IndexedDB write applied before this call, even if it + /// has no pages of its own. Later writes are outside this boundary. Its + /// own flush failure is `[STG-048]`; an earlier failed flush is `[STG-046]`. + /// Close and reopen to recover. With no earlier work or dirty pages it + /// resolves immediately. In memory and OPFS every commit is already stored. pub async fn checkpoint(&self) -> Result<(), JsValue> { self.inner.admit()?; refuse_in_tx(&self.inner)?; - let (flush, idb) = { + self.inner.refuse_failed_flush()?; + let work = { let mut inner = self.inner.borrow_mut(); - inner.pfs.save().map_err(to_js_error)?; - ( - take_commit_flush(&mut inner)?, - inner.idb.as_ref().map(IndexedDbBackend::clone_handle), + FlushWork( + inner + .pfs + .save() + .map_err(to_js_error) + .and_then(|()| take_commit_flush(&mut inner)), ) }; - - if let Some(idb) = idb { - idb.write_pages(flush.puts, flush.deletes).await?; - } + wasm_bindgen_futures::JsFuture::from(self.inner.flush(work)).await?; Ok(()) } @@ -488,7 +594,8 @@ impl BrowserDb { /// /// The blob is byte-for-bit compatible with native `.graph` files opened by /// `Minigraf::open()`. Pages are always in ascending `page_id` order. Free - /// pages are written as zeros. + /// pages are written as zeros. On a failed IndexedDB handle this includes + /// rejected work already applied in memory; it does not acknowledge durability. /// /// Call `checkpoint()` on native before importing a file here to ensure /// no WAL entries are missing from the main file. @@ -530,10 +637,14 @@ impl BrowserDb { /// The blob must be a checkpointed native `.graph` file (no pending WAL sidecar). /// All existing data is overwritten. After import, the new data is immediately /// queryable and all dirty pages are flushed to IndexedDB. + /// An IndexedDB flush failure is `[STG-048]`: memory retains the imported + /// graph, while IndexedDB retains the previous graph. Later durable work + /// is `[STG-046]` until close and reopen, including imports (no live swap). #[wasm_bindgen(js_name = importGraph)] pub async fn import_graph(&self, data: js_sys::Uint8Array) -> Result<(), JsValue> { self.inner.admit()?; refuse_in_tx(&self.inner)?; + self.inner.refuse_failed_flush()?; if self.inner.opfs.is_some() { return self.import_opfs(data).await; } @@ -550,19 +661,10 @@ impl BrowserDb { } // ── Sync section ────────────────────────────────────────────────────────── - let (flush, idb) = { + let work = { let mut inner = self.inner.borrow_mut(); let buffer = BrowserBackend::Buffer(BrowserBufferBackend::load_pages_all_dirty(pages)); - // Page cache capacity 0 — see comment in `open_in_memory` above. - let mut new_pfs = PersistentFactStorage::new(buffer, 0).map_err(to_js_error)?; - let new_fact_storage = new_pfs.storage().clone(); - - // Drain dirty set and collect owned page bytes before swapping - // inner, leaving out the free pages (#440). - let free = new_pfs.free_page_ids().map_err(to_js_error)?; - let mut flush = take_flush(&mut new_pfs, free)?; - // Every page of the old database the new one does not hold must go - // too, or a reopen would load it next to the imported pages. + let new_pfs = PersistentFactStorage::new(buffer, 0).map_err(to_js_error)?; let new_ids: std::collections::HashSet = new_pfs.with_backend(|b| buffer_page_ids(b).collect()); let stale: Vec = inner.pfs.with_backend(|b| { @@ -570,25 +672,21 @@ impl BrowserDb { .filter(|id| !new_ids.contains(id)) .collect() }); - flush.deletes.extend(stale); - flush.deletes.sort_unstable(); - flush.deletes.dedup(); - + // Preserve the existing memory contract: an import becomes + // visible before its flush. Failure belongs to Db, not this pfs. + inner.fact_storage = new_pfs.storage().clone(); inner.pfs = new_pfs; - inner.fact_storage = new_fact_storage; - // Ids of the old database. Its open cursors keep its buffer alive. inner.withheld.clear(); - - ( - flush, - inner.idb.as_ref().map(IndexedDbBackend::clone_handle), - ) + FlushWork((|| { + let free = inner.pfs.free_page_ids().map_err(to_js_error)?; + let mut flush = take_flush(&mut inner.pfs, free)?; + flush.deletes.extend(stale); + flush.deletes.sort_unstable(); + flush.deletes.dedup(); + Ok(flush) + })()) }; - // ── Borrow dropped ──────────────────────────────────────────────────────── - - if let Some(idb) = idb { - idb.write_pages(flush.puts, flush.deletes).await?; - } + wasm_bindgen_futures::JsFuture::from(self.inner.flush(work)).await?; Ok(()) } @@ -819,7 +917,7 @@ impl BrowserDb { }) .collect(); - let flush = commit_stamped(&mut inner, stamped)?; + let flush = commit_stamped(&mut inner, stamped); let json = if is_retract { format!(r#"{{"retracted":{}}}"#, tx_id) @@ -832,15 +930,7 @@ impl BrowserDb { // ── Borrow dropped here ─────────────────────────────────────────────── // ── Async section: flush to IDB (no RefCell borrow held) ───────────── - let idb = self - .inner - .borrow() - .idb - .as_ref() - .map(IndexedDbBackend::clone_handle); - if let Some(idb) = idb { - idb.write_pages(flush.puts, flush.deletes).await?; - } + wasm_bindgen_futures::JsFuture::from(self.inner.flush(flush)).await?; Ok(result_json) } @@ -851,19 +941,21 @@ impl BrowserDb { fn commit_stamped( inner: &mut BrowserDbInner, stamped: Vec, -) -> Result { - // One batch, so no query sees part of the transaction (#554). - inner - .fact_storage - .load_facts(stamped) - .map_err(to_js_error)?; +) -> FlushWork { + FlushWork((|| { + // One batch, so no query sees part of the transaction (#554). + inner + .fact_storage + .load_facts(stamped) + .map_err(to_js_error)?; - inner.pfs.mark_dirty(); - inner.pfs.save().map_err(to_js_error)?; + inner.pfs.mark_dirty(); + inner.pfs.save().map_err(to_js_error)?; - // Collect dirty pages as owned Vec<(u64, Vec)> — no borrows - // escape — and drop the pages this commit freed (#440). - take_commit_flush(inner) + // Collect dirty pages as owned Vec<(u64, Vec)> — no borrows + // escape — and drop the pages this commit freed (#440). + take_commit_flush(inner) + })()) } // ── BrowserWriteTransaction ───────────────────────────────────────────────── @@ -909,6 +1001,11 @@ impl BrowserWriteTransaction { /// commit that is refused (for example `[API-019]`, a valid time that /// ended before the commit time) discards the staged facts. /// + /// After an IndexedDB flush fails, even an empty commit rejects with + /// `[STG-046]`, discards staging and finishes without changing the database. + /// The failed flush itself is `[STG-048]` (preparation errors keep their + /// engine code); already applied facts stay readable until close/reopen. + /// /// If `close()` runs before an IndexedDB flush starts, the flush is not /// started and the promise rejects with `[API-018]`. pub fn commit(&mut self) -> js_sys::Promise { @@ -921,26 +1018,13 @@ impl BrowserWriteTransaction { let committed = { let mut inner = self.db.borrow_mut(); inner.tx_open = false; - tx_commit(&mut inner, staged).map(|flush| { - ( - flush, - inner.idb.as_ref().map(IndexedDbBackend::clone_handle), - ) - }) + self.db + .refuse_failed_flush() + .and_then(|()| tx_commit(&mut inner, staged)) }; - let db = self.db.clone(); match committed { Err(e) => js_sys::Promise::reject(&e), - Ok((flush, idb)) => wasm_bindgen_futures::future_to_promise(async move { - if let (Some(flush), Some(idb)) = (flush, idb) { - // Never into a database closed (and maybe reopened) since. - if db.closed.get() { - return Err(closed_error("database")); - } - idb.write_pages(flush.puts, flush.deletes).await?; - } - Ok(JsValue::UNDEFINED) - }), + Ok(work) => self.db.flush(work), } } @@ -1006,19 +1090,16 @@ fn tx_execute( } } -/// A transaction's commit: `None` when nothing was staged. -fn tx_commit( - inner: &mut BrowserDbInner, - staged: crate::db::Staged, -) -> Result, JsValue> { +/// Validate before applying. Empty staging still has a durability boundary. +fn tx_commit(inner: &mut BrowserDbInner, staged: crate::db::Staged) -> Result { let facts = staged.pending_facts; if facts.is_empty() { - return Ok(None); + return Ok(FlushWork(Ok(Flush::default()))); } let (facts, tx_id) = crate::db::Staged::checked_for_commit(facts).map_err(to_js_error)?; let tx_count = inner.fact_storage.allocate_tx_count(); let stamped = crate::db::Staged::stamp(facts, tx_id, tx_count); - commit_stamped(inner, stamped).map(Some) + Ok(commit_stamped(inner, stamped)) } // ── BrowserCursor ─────────────────────────────────────────────────────────── @@ -1227,11 +1308,22 @@ fn take_dirty_pages( /// What one commit sends to IndexedDB, in one transaction: the pages it wrote /// (its meta page among them) and the keys of the free pages it dropped. +#[derive(Default)] struct Flush { puts: Vec<(u64, Vec)>, deletes: Vec, } +impl Flush { + fn is_empty(&self) -> bool { + self.puts.is_empty() && self.deletes.is_empty() + } +} + +/// Memory was applied; any error here is a failed flush preparation, unlike +/// parse/validation errors before application. Preserve its engine code. +struct FlushWork(Result); + /// The flush of a commit: drop the bytes of the pages it freed and of those /// withheld earlier, except the ids still in force, which a pinned generation /// (an open cursor's) may read (#543); those stay withheld. diff --git a/src/browser/tx_tests.rs b/src/browser/tx_tests.rs index ced739ba9f..67136d2cf0 100644 --- a/src/browser/tx_tests.rs +++ b/src/browser/tx_tests.rs @@ -271,8 +271,8 @@ async fn rollback_and_drop_discard_and_survive_reopen() { assert_eq!(names(&db).await.len(), 3, "neither after reopening"); } -/// A failed IndexedDB flush rejects the commit's promise and leaves what a -/// failed flush of `execute` leaves: the facts in memory, not in the store. +/// The first failed flush leaves its facts in memory, not in IndexedDB. +/// A commit admitted afterwards is refused before applying its staged facts. #[wasm_bindgen_test] async fn failed_flush_rejects_like_execute() { let name = "minigraf-test-tx-flush"; @@ -283,12 +283,12 @@ async fn failed_flush_rejects_like_execute() { let failed_execute = db .execute(r#"(transact [[:dave :person/name "Dave"]])"#.to_string()) .await; - assert!(failed_execute.is_err(), "execute's flush fails"); + assert_eq!(code_of(&failed_execute.unwrap_err()), "STG-048"); let mut tx = db.begin_write().expect("begin"); tx.execute(r#"(transact [[:erin :person/name "Erin"]])"#) .unwrap(); - assert!(commit(&mut tx).await.is_err(), "the commit's flush fails"); - assert_eq!(names(&db).await.len(), 5, "both in memory, as before"); + assert_eq!(code_of(&commit(&mut tx).await.unwrap_err()), "STG-046"); + assert_eq!(names(&db).await.len(), 4, "Erin was never applied"); assert!( tx.execute(NAMES).is_err(), "the transaction is finished after a failed flush" diff --git a/src/error.rs b/src/error.rs index b59ba9dfb9..9fa59c9b2d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1470,7 +1470,7 @@ pub(crate) const REGISTRY: &[(ErrorCode, &str, &str, ErrorCategory)] = &[ ( ErrorCode::Stg046, "STG-046", - "an earlier checkpoint failed on this handle and is not retried; reopen the database", + "an earlier checkpoint or IndexedDB flush failed on this handle and is not retried; reopen the database", ErrorCategory::Storage, ), ( diff --git a/tests/crash_kill_test.rs b/tests/crash_kill_test.rs index 5f250b8a70..ac943fc4f2 100644 --- a/tests/crash_kill_test.rs +++ b/tests/crash_kill_test.rs @@ -540,7 +540,7 @@ fn crash_kill_child_entrypoint() { let mut sync_rng = Rng::new(seed.rotate_left(29)); let per_tx = matches!(variant, Variant::PerTxSync); let mut live = BTreeSet::new(); - for k in 1.. { + for k in 1..=u64::MAX { let (stmt, next) = next_tx(&mut rng, &live); if per_tx { let opts = TxOptions::new().synchronous(tx_sync_mode(&mut sync_rng, k)); From 2fd597b2122a6be89888f80f7c2ce531797cd764 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Tue, 17 Nov 2026 23:16:17 +0530 Subject: [PATCH 2/2] test(browser): cover a non-empty smaller import failing its flush (#894) Co-Authored-By: Claude Sonnet 5.5 --- CHANGELOG.md | 1 - CLAUDE.md | 2 +- docs/ERROR_REFERENCE.md | 2 +- docs/TEST_COVERAGE.md | 7 +++++++ src/browser/flush_tests.rs | 24 ++++++++++++++++++++++++ 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 559e2d0dae..5b778bd5b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -174,7 +174,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **IndexedDB flush recovery** (#894, v3.0.0). Page puts and deletes now land atomically, including when queueing throws; abort-only failures and repeated request errors settle once with STG-048 naming the IndexedDB write and DOM exception. Flushes run in application order, and checkpoint waits for earlier writes even with no pages of its own. After failure, new writes, commits, checkpoints and imports reject with STG-046 without changing memory; already applied work stays readable and exportable. Close and reopen recovers the successful prefix before failure. Unstarted flushes reject with API-018 after close. No new dependencies, file-format, schema or public-method changes. The next v2.x patch backport is separate; #894 stays open until release. - - **Window partition scratch obeys `query_memory_limit`** (#765). Each window now charges its partition row indices while they coexist with input rows and earlier result columns, returns `QRY-012` if reserving them fails, and releases the charge after the window or on error. An in-place total sort removes the stable sort's uncharged merge buffer; partition and row order are unchanged. - **An expression's `=` and `!=` equate a keyword with a ref to the entity it names** (#744). Patterns, joins, `not` bodies and `or` branches treat the keyword `:bob` as equal to a ref to the entity `:bob` names, but `[(= ?g :bob)]` compared the two values as they are, so with `?g` bound to that ref it kept no row and `[(!= ?g :bob)]` kept the row: the same question answered differently by how the query was written, with no error. `=` and `!=` now compare as joins do, in queries, rule bodies, `not` clauses and binding expressions, against a keyword constant, a keyword value or a bound `Val(Ref)`. A keyword still equals only the same keyword, and `<`, `>`, `<=` and `>=` on a keyword or a ref are still a type mismatch (no row). - **`checkpoint`, `verify` and `rebuild_indexes` inside a `WriteTransaction` return `INT-001` instead of deadlocking** (#734). Called on a thread that holds a `WriteTransaction`, they waited on the write lock that the transaction already held, so the thread hung. They now return `INT-001`, as `execute`, `query`, `fact_log` and `begin_write` already did, and the transaction stays usable. `prepare` and `PreparedQuery::execute`/`query` make the same check; they could not deadlock, but they ran inside the transaction where `execute`/`query` refused. `checkpoint` deadlocked in every release since `WriteTransaction` was added; the fix for it and for the prepared calls ships in v2.x too. diff --git a/CLAUDE.md b/CLAUDE.md index 0e681cc6e2..ff484769bc 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -250,7 +250,7 @@ Auto-migrates v7 → v8 on open (spec §9, with a backup meta page). v1–v6 are ## Test Coverage -**2172 tests** (2149 active, 23 ignored; unit + integration + doc). Browser-only inventory is separate: #894 adds 13 wasm tests (94 main-thread / 127 dedicated-worker tests in total); see `docs/testing/894-idb-flush-recovery.md` for execution results. Engine answers once checked by the executor are frozen in `tests/answers/` (#551). +**2172 tests** (2149 active, 23 ignored; unit + integration + doc). Browser-only inventory is separate: #894 adds 14 wasm tests (95 main-thread / 128 dedicated-worker tests in total); see `docs/testing/894-idb-flush-recovery.md` for execution results. Engine answers once checked by the executor are frozen in `tests/answers/` (#551). See `docs/TEST_COVERAGE.md` for the full per-file breakdown. The #760 deterministic storage suite adds three native unit tests in a separate `--cfg minigraf_shuttle` build, outside the default count. Run only `graph::storage::deterministic::` with that cfg; ordinary unit tests do not enter its scheduler. `deterministic-schedules.yml` runs relevant v3 PRs on Linux/macOS/Windows plus Linux MSRV, and larger nightly/manual searches with replay artifacts. The identical main workflow activates the v3 schedule after both PRs merge. See `docs/testing/deterministic-schedules.md` for scope, controls and findings. diff --git a/docs/ERROR_REFERENCE.md b/docs/ERROR_REFERENCE.md index 35096f0272..f3b6740ecd 100644 --- a/docs/ERROR_REFERENCE.md +++ b/docs/ERROR_REFERENCE.md @@ -3151,7 +3151,7 @@ let mut out = LogWriter::create("memory.v2.graph", OpenOptions::new())?; --- -### STG-046 Earlier checkpoint failed on this handle +### STG-046 Earlier checkpoint or IndexedDB flush failed on this handle **Error text**: `an earlier checkpoint or IndexedDB flush failed on this handle and is not retried; reopen the database` diff --git a/docs/TEST_COVERAGE.md b/docs/TEST_COVERAGE.md index 5ae3c79ed4..9c7fc550cf 100644 --- a/docs/TEST_COVERAGE.md +++ b/docs/TEST_COVERAGE.md @@ -2,6 +2,13 @@ ## Coverage Summary +**#894 evaluator follow-up**: the nonempty smaller-import abort regression raises +the flush suite to 14 browser-only tests (95 main-thread / 128 dedicated-worker +tests in the inventory). Native counts are unchanged. The test checks failed-handle +refusal, the unchanged durable page map, and the 400-row reopen integrity oracle. +Firefox main-thread verification passes all 95 tests; the worker count is inventory +only for this follow-up (the earlier worker run is recorded below). + **#894 verification (2026-11-17)**: native default tests pass (2,149 active, 23 ignored); strict native all-target and browser-target Clippy, build and formatting pass. Firefox passes **94 main-thread** and **127 dedicated-worker** diff --git a/src/browser/flush_tests.rs b/src/browser/flush_tests.rs index c74e33372d..54e823972b 100644 --- a/src/browser/flush_tests.rs +++ b/src/browser/flush_tests.rs @@ -394,6 +394,30 @@ async fn commit_checkpoint_and_smaller_import_fail_atomically() { } } +#[wasm_bindgen_test] +async fn nonempty_smaller_import_fails_atomically() { + let source = BrowserDb::open_in_memory().unwrap(); + source.execute(write(500, 502)).await.unwrap(); + let image = source.export_graph().unwrap(); + source.close().unwrap(); + + let n = name(); + let db = BrowserDb::open(&n).await.unwrap(); + db.execute(write(0, 400)).await.unwrap(); + assert!(image.length() < db.export_graph().unwrap().length()); + let before = adapter(&db).load_all_pages().await.unwrap(); + let fault = Fault::for_db(&db, "abort"); + code(deadline(import(&db, image)).await, "STG-048"); + assert_eq!(rows(&db).await, vec![500, 501]); + refused(&db).await; + assert_eq!(fault.number("sent"), 1.0); + assert_eq!(adapter(&db).load_all_pages().await.unwrap(), before); + fault.no_page_errors(); + drop(fault); + db.close().unwrap(); + reopen(&n, (0..400).collect()).await; +} + #[wasm_bindgen_test] async fn preparation_error_keeps_code_and_fails_handle() { for operation in ["execute", "commit", "checkpoint"] {