S00-005: Select idb and verify transactional storage failure policies

Changes: Propose ADR 0004 comparing native IndexedDB, idb and Dexie; select idb 8.0.4 with account, transaction, quota, upgrade and key ownership policy. Repair rollback and add injected quota/blocked upgrade checks, exact wrapper lock, runtime guard, license texts and reproduction notes. Preserve historical recovery artifacts.
Validation: Fresh-cache npm ci and npm run check passed on Node 24.21.0/npm 11.19.0; manifest, HTTPS/SHA512 and license checks passed. make check passed 72 tooling tests; tasks.py validate and git diff --check passed. Native browser quota, disk durability and process restart were not tested; ADR remains proposed for independent QA.

Task: S00-005
Agent: auto-storage-0ce544ac4b40
Role: storage
Action: claim
Status: review

Task title: Выбрать IndexedDB wrapper и migration policy
This commit is contained in:
wt committed 2026-10-15 18:33:15 +07:00
1 parent 8f918bc040
commit 0e2b3098df
17 files changed
+713 -128

No files matched your search

+2 -1
View File
@@ -1,2 +1,3 @@
registry=https://registry.npmjs.org/
strict-ssl=true
strict-ssl=true
engine-strict=true
+41
View File
@@ -0,0 +1,41 @@
# Storage design experiment (S00-005)
Synthetic fixtures only. No app adapter, shared contract, secrets or real messages.
ADR: `docs/decisions/0004-storage-design.md` (proposed pending independent QA).
`RECOVERY.md` preserves the earlier interruption inventory; its missing-output
notes are historical, superseded by this task's code and evidence.
From this directory, using Node 24.21.0 and bundled npm 11.19.0:
```sh
npm ci --ignore-scripts --no-audit --no-fund --cache /tmp/luma-storage-review-cache --fetch-retries=0 --fetch-timeout=15000
npm run check
```
`check` refuses other Node/npm versions. Lockfile pins idb 8.0.4 (ISC) and
fake-indexeddb 6.2.5 (Apache-2.0), with HTTPS URLs/SHA512 integrity and no transitives.
`.npmrc` retains verified TLS. Runtime dependencies are isolated to this experiment.
Full license texts are in `licenses/`; production distributions must preserve
idb's notice. fake-indexeddb is a test fixture and must not enter production paths.
- `src/rollback.mjs`: actual unique-index ConstraintError, failed upgrade rollback,
complete schema/data assertions, corrected retry and same-process connection reopen.
- `src/quota-blocked.mjs`: explicitly injected quota error after successful requests,
multi-store abort/checkpoint invariance, account B preservation, retry/reopen;
held old connection, versionchange/blocked and release; stale-version refusal.
- `src/_probe.mjs`: preserved exploratory native-API fixture, outside check suite.
Each fixture uses its own synthetic database name and deletes it before/after.
The blocked scenario has a 3-second failure deadline. CI should impose an overall
process timeout as well. Exceptions/assertion failures cause nonzero exit;
PASS text is printed only after the corresponding assertions.
fake-indexeddb is in memory. Quota fault injection checks the policy response and
real fixture transaction abort, not a native request/commit QuotaExceededError.
Reopen is not process restart. No real browser was available (empty inventory).
Browser multi-tab, physical quota/disk failure, crash/restart, eviction and
StorageManager permission/persistence grants remain external integration checks.
Future browser testing should use a disposable isolated origin/profile, repeat
the schema and blocked scenarios on native IDB, then validate request/commit
quota failures and restart persistence without filling a user's normal origin.
No TLS, CORS or encryption workaround is required or authorized.
@@ -0,0 +1,209 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and
distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the
copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other
entities that control, are controlled by, or are under common control with
that entity. For the purposes of this definition, "control" means (i) the
power, direct or indirect, to cause the direction or management of such
entity, whether by contract or otherwise, or (ii) ownership of
fifty percent (50%) or more of the outstanding shares, or (iii) beneficial
ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising
permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation source,
and configuration files.
"Object" form shall mean any form resulting from mechanical transformation
or translation of a Source form, including but not limited to compiled
object code, generated documentation, and conversions to
other media types.
"Work" shall mean the work of authorship, whether in Source or Object
form, made available under the License, as indicated by a copyright notice
that is included in or attached to the work (an example is provided in the
Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form,
that is based on (or derived from) the Work and for which the editorial
revisions, annotations, elaborations, or other modifications represent,
as a whole, an original work of authorship. For the purposes of this
License, Derivative Works shall not include works that remain separable
from, or merely link (or bind by name) to the interfaces of, the Work and
Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original
version of the Work and any modifications or additions to that Work or
Derivative Works thereof, that is intentionally submitted to Licensor for
inclusion in the Work by the copyright owner or by an individual or
Legal Entity authorized to submit on behalf of the copyright owner.
For the purposes of this definition, "submitted" means any form of
electronic, verbal, or written communication sent to the Licensor or its
representatives, including but not limited to communication on electronic
mailing lists, source code control systems, and issue tracking systems
that are managed by, or on behalf of, the Licensor for the purpose of
discussing and improving the Work, but excluding communication that is
conspicuously marked or otherwise designated in writing by the copyright
owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on
behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License.
Subject to the terms and conditions of this License, each Contributor
hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
royalty-free, irrevocable copyright license to reproduce, prepare
Derivative Works of, publicly display, publicly perform, sublicense,
and distribute the Work and such Derivative Works in
Source or Object form.
3. Grant of Patent License.
Subject to the terms and conditions of this License, each Contributor
hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
royalty-free, irrevocable (except as stated in this section) patent
license to make, have made, use, offer to sell, sell, import, and
otherwise transfer the Work, where such license applies only to those
patent claims licensable by such Contributor that are necessarily
infringed by their Contribution(s) alone or by combination of their
Contribution(s) with the Work to which such Contribution(s) was submitted.
If You institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
Contribution incorporated within the Work constitutes direct or
contributory patent infringement, then any patent licenses granted to
You under this License for that Work shall terminate as of the date such
litigation is filed.
4. Redistribution.
You may reproduce and distribute copies of the Work or Derivative Works
thereof in any medium, with or without modifications, and in Source or
Object form, provided that You meet the following conditions:
1. You must give any other recipients of the Work or Derivative Works a
copy of this License; and
2. You must cause any modified files to carry prominent notices stating
that You changed the files; and
3. You must retain, in the Source form of any Derivative Works that You
distribute, all copyright, patent, trademark, and attribution notices from
the Source form of the Work, excluding those notices that do not pertain
to any part of the Derivative Works; and
4. If the Work includes a "NOTICE" text file as part of its distribution,
then any Derivative Works that You distribute must include a readable copy
of the attribution notices contained within such NOTICE file, excluding
those notices that do not pertain to any part of the Derivative Works,
in at least one of the following places: within a NOTICE text file
distributed as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or, within a
display generated by the Derivative Works, if and wherever such
third-party notices normally appear. The contents of the NOTICE file are
for informational purposes only and do not modify the License.
You may add Your own attribution notices within Derivative Works that You
distribute, alongside or as an addendum to the NOTICE text from the Work,
provided that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and may
provide additional or different license terms and conditions for use,
reproduction, or distribution of Your modifications, or for any such
Derivative Works as a whole, provided Your use, reproduction, and
distribution of the Work otherwise complies with the conditions
stated in this License.
5. Submission of Contributions.
Unless You explicitly state otherwise, any Contribution intentionally
submitted for inclusion in the Work by You to the Licensor shall be under
the terms and conditions of this License, without any additional
terms or conditions. Notwithstanding the above, nothing herein shall
supersede or modify the terms of any separate license agreement you may
have executed with Licensor regarding such Contributions.
6. Trademarks.
This License does not grant permission to use the trade names, trademarks,
service marks, or product names of the Licensor, except as required for
reasonable and customary use in describing the origin of the Work and
reproducing the content of the NOTICE file.
7. Disclaimer of Warranty.
Unless required by applicable law or agreed to in writing, Licensor
provides the Work (and each Contributor provides its Contributions)
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied, including, without limitation, any warranties
or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS
FOR A PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any risks
associated with Your exercise of permissions under this License.
8. Limitation of Liability.
In no event and under no legal theory, whether in tort
(including negligence), contract, or otherwise, unless required by
applicable law (such as deliberate and grossly negligent acts) or agreed
to in writing, shall any Contributor be liable to You for damages,
including any direct, indirect, special, incidental, or consequential
damages of any character arising as a result of this License or out of
the use or inability to use the Work (including but not limited to damages
for loss of goodwill, work stoppage, computer failure or malfunction,
or any and all other commercial damages or losses), even if such
Contributor has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability.
While redistributing the Work or Derivative Works thereof, You may choose
to offer, and charge a fee for, acceptance of support, warranty,
indemnity, or other liability obligations and/or rights consistent with
this License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf of any
other Contributor, and only if You agree to indemnify, defend, and hold
each Contributor harmless for any liability incurred by, or claims
asserted against, such Contributor by reason of your accepting any such
warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work
To apply the Apache License to your work, attach the following boilerplate
notice, with the fields enclosed by brackets "[]" replaced with your own
identifying information. (Don't include the brackets!) The text should be
enclosed in the appropriate comment syntax for the file format. We also
recommend that a file or class name and description of purpose be included
on the same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2017 Jeremy Scheff
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing
permissions and limitations under the License.
@@ -0,0 +1,6 @@
ISC License (ISC)
Copyright (c) 2016, Jake Archibald <jaffathecake@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+8 -1
View File
@@ -8,7 +8,8 @@
"name": "0004-storage-design",
"version": "0.0.0",
"dependencies": {
"fake-indexeddb": "6.2.5"
"fake-indexeddb": "6.2.5",
"idb": "8.0.4"
},
"engines": {
"node": "24.21.0",
@@ -23,6 +24,12 @@
"engines": {
"node": ">=18"
}
},
"node_modules/idb": {
"version": "8.0.4",
"resolved": "https://registry.npmjs.org/idb/-/idb-8.0.4.tgz",
"integrity": "sha512-jd0ITCrN+Zah4J7R/Z2jnR3W7A4BXDAjmo62MwWuSjxCiueUAf7X5rbodEdidWzeknwXHmLpdZkoiiCppNi30Q==",
"license": "ISC"
}
}
}
+8 -4
View File
@@ -3,14 +3,18 @@
"version": "0.0.0",
"private": true,
"type": "module",
"engines": { "node": "24.21.0", "npm": "11.19.0" },
"engines": {
"node": "24.21.0",
"npm": "11.19.0"
},
"packageManager": "npm@11.19.0",
"scripts": {
"check:rollback": "node src/rollback.mjs",
"check:quota": "node src/quota-blocked.mjs",
"check": "npm run check:rollback && npm run check:quota"
"check": "node src/check-runtime.mjs && npm run check:rollback && npm run check:quota"
},
"dependencies": {
"fake-indexeddb": "6.2.5"
"fake-indexeddb": "6.2.5",
"idb": "8.0.4"
}
}
}
@@ -0,0 +1,3 @@
import assert from 'node:assert/strict';
assert.equal(process.version, 'v24.21.0');
assert.match(process.env.npm_config_user_agent ?? '', /^npm\/11\.19\.0 /);
@@ -0,0 +1,69 @@
import 'fake-indexeddb/auto';
import assert from 'node:assert/strict';
import { openDB, deleteDB } from 'idb';
const name = 'luma-design-quota-blocked';
await deleteDB(name);
let db = await openDB(name, 1, { upgrade(db) {
for (const store of ['history', 'checkpoints', 'settings', 'crypto']) db.createObjectStore(store);
} });
// Illustrative composite keys, not an application contract.
await db.put('history', { text: 'Synthetic other account' }, ['B', 'm1']);
await db.put('checkpoints', 'before', 'A');
await db.put('settings', 'B-setting', ['B', 'theme']);
await db.put('crypto', 'B-opaque-fixture-not-key-material', ['B', 'state']);
// Explicit quota fault injection: fake-indexeddb has no real disk quota.
async function saveBatch(injectFailure) {
const tx = db.transaction(['history', 'checkpoints'], 'readwrite');
const completion = tx.done;
completion.catch(() => {});
try {
await tx.objectStore('history').put({ text: 'Synthetic pending' }, ['A', 'm1']);
await tx.objectStore('checkpoints').put('after', 'A');
if (injectFailure) throw new DOMException('Synthetic quota injection', 'QuotaExceededError');
await completion;
return { saved: true };
} catch (error) {
try { tx.abort(); } catch { /* Engine may have already aborted. */ }
await completion.catch(() => {});
return { saved: false, reason: error.name };
}
}
assert.deepEqual(await saveBatch(true), { saved: false, reason: 'QuotaExceededError' });
assert.equal(await db.get('history', ['A', 'm1']), undefined);
assert.equal(await db.get('checkpoints', 'A'), 'before');
assert.deepEqual(await db.get('history', ['B', 'm1']), { text: 'Synthetic other account' });
assert.equal(await db.get('settings', ['B', 'theme']), 'B-setting');
assert.equal(await db.get('crypto', ['B', 'state']), 'B-opaque-fixture-not-key-material');
assert.deepEqual(await saveBatch(false), { saved: true });
db.close();
db = await openDB(name, 1);
assert.equal(await db.get('checkpoints', 'A'), 'after');
assert.deepEqual(await db.get('history', ['A', 'm1']), { text: 'Synthetic pending' });
let versionChange = false;
let upgraded = false;
db.addEventListener('versionchange', () => { versionChange = true; });
let signalBlocked;
const blocked = new Promise(resolve => { signalBlocked = resolve; });
const pending = openDB(name, 2, {
blocked() { signalBlocked(); },
upgrade(db) { upgraded = true; db.createObjectStore('v2Marker'); },
});
const timer = setTimeout(() => { throw new Error('blocked event timeout'); }, 3000);
await blocked;
assert.equal(versionChange, true);
assert.equal(upgraded, false);
assert.equal(db.version, 1);
db.close();
const next = await pending;
clearTimeout(timer);
assert.equal(upgraded, true);
assert.equal(next.version, 2);
assert.equal(await next.get('checkpoints', 'A'), 'after');
next.close();
await assert.rejects(openDB(name, 1), { name: 'VersionError' });
await deleteDB(name);
console.log('PASS quota policy (injected): no saved success or checkpoint advance on abort; account isolation; retry/reopen');
console.log('PASS blocked upgrade: versionchange then blocked; close releases upgrade; stale version refused');
+45 -107
View File
@@ -1,108 +1,46 @@
import "fake-indexeddb/auto";
import assert from "node:assert/strict";
import 'fake-indexeddb/auto';
import assert from 'node:assert/strict';
import { openDB, deleteDB } from 'idb';
// Bounded storage experiment (S00-005, focused check "Rollback experiment").
// Uses fake-indexeddb 6.2.5 (in-memory, pure-JS IndexedDB implementation) to exercise
// the transaction/schema-upgrade rollback guarantees that a Web storage layer relies
// on for STORE-001 (transactional saves or verifiable equivalent recovery).
// This is an API-semantics probe, not a browser-durability claim.
const DB = "rollback-probe";
function open(version, upgrade) {
return new Promise((resolve, reject) => {
const req = indexedDB.open(DB, version);
const events = { upgraded: false, blocked: false };
req.onupgradeneeded = () => {
events.upgraded = true;
if (upgrade) upgrade(req.result);
};
req.onsuccess = () => resolve({ db: req.result, events });
req.onerror = () => reject(new Error(`open(v${version}): ${req.error?.name}`));
req.onblocked = () => { events.blocked = true; };
});
}
function getAll(db, store) {
return new Promise((resolve, reject) => {
const tx = db.transaction(store, "readonly");
tx.objectStore(store).getAll().onsuccess = (e) => resolve(e.target.result);
tx.onerror = () => reject(tx.error);
});
}
async function seedV1() {
const { db } = await open(1, (db) => {
db.createObjectStore("accounts", { keyPath: "accountId" });
});
db.close();
// write a durable per-account record (configuration, non-secret fixture)
const w = await open(1);
await new Promise((resolve, reject) => {
const tx = w.db.transaction("accounts", "readwrite");
tx.objectStore("accounts").add({ accountId: "A1", displayName: "Alice" });
tx.oncomplete = resolve;
tx.onerror = () => reject(tx.error);
});
w.db.close();
}
// Scenario 1 (rollback): upgrade v1 -> v2 that fails mid-way because a data migration
// violates a new unique index inside the versionchange transaction. The whole upgrade
// must abort and revert: schema version stays at v1, the new index/store is absent,
// and pre-existing v1 data is untouched.
async function failedUpgradeRollsBack() {
const req = indexedDB.open(DB, 2);
let aborted = true;
req.onupgradeneeded = () => {
const db = req.result;
const accounts = db.createObjectStore("accounts", { keyPath: "accountId" });
accounts.createIndex("displayName", "displayName", { unique: true });
// Both rows share the same unique displayName -> the second write fails and
// aborts the versionchange transaction (organic failure, not a manual abort).
accounts.add({ accountId: "A1", displayName: "Shared" });
accounts.add({ accountId: "A2", displayName: "Shared" });
};
req.onsuccess = () => { aborted = false; req.result.close(); };
req.onerror = () => { /* default abort */ };
await new Promise((resolve) => { req.onsuccess = resolve; req.onerror = resolve; });
assert.equal(aborted, true, "failing data migration must abort the upgrade (no fake success)");
// Schema version must remain at v1 after the abort.
const meta = await indexedDB.databases().then((l) => l.find((d) => d.name === DB));
assert.equal(meta.version, 1, "failed upgrade must keep persisted schema version at v1");
const { db } = await open(1);
assert.ok(db.objectStoreNames.contains("accounts"), "v1 accounts store survives");
const rows = await getAll(db, "accounts");
assert.equal(rows.length, 1, "no partial migration rows persisted");
assert.equal(rows[0].accountId, "A1", "pre-existing v1 data intact after rollback");
db.close();
}
// Scenario 2 (retry after rollback): after a clean rollback a corrected migration can be
// retried and commits fully to v2 without a stale schema or lost data.
async function retryAfterRollbackSucceeds() {
const { db, events } = await open(2, (db) => {
const accounts = db.createObjectStore("accounts", { keyPath: "accountId" });
accounts.createIndex("displayName", "displayName", { unique: true });
accounts.add({ accountId: "A1", displayName: "Alice" });
accounts.add({ accountId: "A2", displayName: "Bob" });
});
assert.equal(events.upgraded, true, "retry upgrade runs");
assert.equal(db.version, 2, "retry commits to v2");
const rows = await getAll(db, "accounts");
assert.equal(rows.length, 2, "both accounts migrated");
db.close();
}
async function main() {
await seedV1();
await failedUpgradeRollsBack();
await retryAfterRollbackSucceeds();
console.log("rollback.mjs PASS: failed v1->v2 upgrade rolled back atomically (schema v1 kept, no partial data); corrected retry committed cleanly to v2.");
}
await main();
// Synthetic API semantics; reopening is not a disk/process restart.
const name = 'luma-design-rollback';
await deleteDB(name);
let db = await openDB(name, 1, { upgrade(db) {
db.createObjectStore('accounts', { keyPath: 'accountId' });
} });
await db.put('accounts', { accountId: 'A1', displayName: 'Fixture One' });
db.close();
let constraint;
await assert.rejects(openDB(name, 2, { upgrade(db, old, next, tx) {
assert.equal(old, 1);
db.createObjectStore('migrationMarker');
const accounts = tx.objectStore('accounts');
accounts.createIndex('displayName', 'displayName', { unique: true });
accounts.put({ accountId: 'A1', displayName: 'Duplicate' }).catch(() => {});
accounts.add({ accountId: 'A2', displayName: 'Duplicate' }).catch(error => { constraint = error.name; });
// Observe promise rejections without suppressing native request error/abort.
tx.done.catch(() => {});
} }), { name: 'AbortError' });
assert.equal(constraint, 'ConstraintError');
db = await openDB(name, 1);
assert.equal(db.version, 1);
assert.deepEqual([...db.objectStoreNames], ['accounts']);
assert.deepEqual([...db.transaction('accounts').store.indexNames], []);
assert.deepEqual(await db.getAll('accounts'), [{ accountId: 'A1', displayName: 'Fixture One' }]);
db.close();
db = await openDB(name, 2, { upgrade(db, old, next, tx) {
db.createObjectStore('migrationMarker');
const accounts = tx.objectStore('accounts');
accounts.createIndex('displayName', 'displayName', { unique: true });
accounts.add({ accountId: 'A2', displayName: 'Fixture Two' }).catch(() => {});
tx.done.catch(() => {});
} });
assert.equal(db.version, 2);
assert.equal((await db.getAll('accounts')).length, 2);
assert.equal((await db.getFromIndex('accounts', 'displayName', 'Fixture Two')).accountId, 'A2');
db.close();
db = await openDB(name, 2);
assert.equal((await db.getAll('accounts')).length, 2);
db.close();
await deleteDB(name);
console.log('PASS rollback: ConstraintError; v1 schema/data restored; corrected v2 retry and reopen');