From d13a393600a3b91dee127c5b47509af4a03b358d Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Wed, 4 Nov 2026 20:29:34 +0900 Subject: [PATCH] =?UTF-8?q?fix(storage):=20=EA=B3=84=EC=A0=95=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20purge=EA=B0=80=20=EB=81=9D=EB=82=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EC=9D=80=20put=EC=9D=84=20=EB=B3=B4=EB=A9=B4=20partit?= =?UTF-8?q?ion=EC=9D=84=20=EC=A7=80=EC=9A=B4=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: 손상이 없는 LOGOUT·ACCOUNT_DELETION purge는 commit된 object 행과 참조 없는 chunk만 본다. 같은 partition의 끝나지 않은 journal 행 (PREPARING·FILES_READY)은 보지 않고, sweep은 staging receipt가 가리키는 chunk를 건너뛰면서도 COMPLETED를 돌려준다. 그래서 - 다른 탭이 put 중이면 purge가 "COMPLETED"를 보고한 뒤 그 객체가 그대로 읽힌다(X1, 실제 Chromium·Firefox에서도 같음). - commit 직전에 멈춘 put(FILES_READY)은 purge 뒤 다음 reconcile이 commit해 계정 삭제한 사용자 파일이 객체로 다시 생긴다(X2). - 업로드 중 멈춘 put(PREPARING)은 chunk가 grace 기간 동안 남고(X2b), 그 행이 손상됐으면 partition 삭제도 일어나지 않아 영원히 남는다(X3). 손상이 있을 때만 partition 삭제가 진행 중 put까지 기다렸다 지웠다. 고른 방법(리뷰 5.1절 (c)): LOGOUT·ACCOUNT_DELETION이 객체를 다 돈 뒤 그 partition 키를 가진 journal 행 수를 센다. 하나라도 있으면(읽을 수 있든 없든) sweep 대신 이미 있는 partition 삭제로 넘긴다. partition 삭제는 exclusive partition lock으로 진행 중 put·delete가 돌아오기를 기다린 뒤 디렉터리와 그 partition의 행을 모두 지운다(e5b7b3e). OpfsJournalPort에 countPartitionTransactions(scope)를 더했다. removePartition이 지우는 같은 LOGICAL_KEY_INDEX 범위를 세므로, 센 행은 삭제가 지운다. 버린 대안: (a) 문서에 "reconcile 먼저" 안내 — X1·X3을 막지 못한다. (b) report에 수만 추가 — 지울 방법이 없다(X3). (d) 항상 partition 삭제 — 더 단순하지만 끝나지 않은 행이 없을 때의 예산 단위 진행과 removedObjects·releasedBytes 보고를 잃는다. 위험·한계: - 공개 포트에 필수 메서드가 하나 는다. OpfsJournalPort를 직접 구현한 쪽은 추가해야 한다([계약]). - 끝나지 않은 행이 하나라도 있으면 purge가 예산 없는 partition 삭제가 되고, 다른 탭의 진행 중 put이 끝날 때까지 기다린다. 그 put의 결과도 지운다(손상 경로와 같은 의미). purge가 행을 센 뒤 시작한 put은 남는다(point-in-time). - 기존 테스트 "never sweeps the chunk of a put that runs while the purge does"는 X1 동작을 기대값으로 고정하고 있었다. 새 의미로 바꿨다. 0929/05-02 테스트는 덮어쓰기가 purge의 목록과 remove 사이에 끝나도록 바꿔 CONFLICT 경로를 계속 본다. - 소속을 알 수 없는(logicalKey가 깨진) 행은 여기서도 세지 못한다(05-03). - WebKit은 기본 정책에서 OPFS 지원 판정을 통과하지 못해 browser spec이 일찍 끝난다. offline-indexeddb 번들 37,423 → 37,521 / 38,500 gzip B. 검증: - RED: opfs-worker-runtime.test.ts 새 4건(X1, FILES_READY, PREPARING, 손상 행)이 partitionDeleted 없음으로 실패. - GREEN: OPFS 단위 3파일 + optional-runtime-host + tests/recipes + tests/contract 172건 통과. journal에 countPartitionTransactions 테스트 1건(손상 행 포함, 다른 partition 제외, 삭제 뒤 0). - browser: opfs-runtime.spec.ts에 X1 2탭 판을 더했다. heavy lock 안에서 3엔진 9/9 통과(WebKit은 지원 판정에서 끝남). - check:types, 바꾼 파일 eslint, check:optional-recipes:source PASS, verify:documentation 통과. Refs: docs/reviews/2026-11-03-maturity-review/05-adapters-storage-kernel.md 05-01 --- .../browser-file-storage-recovery.md | 36 ++-- .../storage/opfs/indexeddb-opfs-journal.ts | 42 ++++ .../storage/opfs/opfs-byte-store-adapter.ts | 22 ++- .../ports/browser-file-storage/opfs-ports.ts | 24 ++- .../browser-capabilities/opfs-runtime.spec.ts | 180 ++++++++++++++---- tests/unit/indexeddb-opfs-journal.test.ts | 45 +++++ tests/unit/opfs-byte-store.test.ts | 9 + tests/unit/opfs-worker-runtime.test.ts | 140 ++++++++++++-- 8 files changed, 431 insertions(+), 67 deletions(-) diff --git a/docs/operations/browser-file-storage-recovery.md b/docs/operations/browser-file-storage-recovery.md index 66ae77e5..074dd405 100644 --- a/docs/operations/browser-file-storage-recovery.md +++ b/docs/operations/browser-file-storage-recovery.md @@ -44,8 +44,9 @@ cleanup 결과를 확인하지 못하면 성공 값이 아니라 실패로 답 근거가 사라지고 quota만 누수된다. 4. OPFS root나 journal database를 통째로 삭제하거나 schema를 downgrade하지 않는다. rollback은 새 v2 write admission을 닫고 v1+v2 reader를 유지하는 것으로 - 수행한다. runtime이 스스로 지우는 것은 승인된 purge가 손상 때문에 끝나지 못할 - 때의 그 partition 디렉터리 하나와 그 partition의 journal row뿐이다(§7). + 수행한다. runtime이 스스로 지우는 것은 승인된 purge가 손상이나 끝나지 않은 + transaction 때문에 끝나지 못할 때의 그 partition 디렉터리 하나와 그 partition의 + journal row뿐이다(§7). ## 1. 공통 원칙 @@ -58,8 +59,9 @@ incident 중에도 다음 작업은 금지한다. - origin의 모든 `caches.keys()` 삭제 - user-authored/unsynced IndexedDB 또는 OPFS data 자동 purge - OPFS의 예외도 하나다. authority가 승인한 LOGOUT/ACCOUNT_DELETION이 손상된 - journal row나 staging 때문에 끝나지 못할 때, runtime이 그 partition - 디렉터리 하나와 그 partition의 journal row만 지운다(§7 recovery). + row나 staging, 또는 그 partition의 끝나지 않은 put·delete 때문에 끝나지 못할 + 때, runtime이 그 partition 디렉터리 하나와 그 partition의 journal row만 + 지운다(§7 recovery). - schema version downgrade - 무한 reload/update loop - filename, path, record/object key, URL/query, digest, body를 incident log에 복사 @@ -438,20 +440,28 @@ VD-15 목표 runtime을 먼저 구현한다. - `COMMITTED` + file missing/corrupt: reconstructable만 server rehydrate; user-created private는 read-only/export/recovery - stale staging/orphan chunk는 grace period와 bounded mark/sweep 후 제거 -- `LOGOUT`/`ACCOUNT_DELETION` purge는 객체를 모두 지운 뒤 그 partition의 chunk를 +- `LOGOUT`/`ACCOUNT_DELETION` purge는 객체를 모두 지운 뒤 그 partition의 journal + row를 센다. 하나라도 있으면(다른 tab에서 진행 중인 put·delete, crash가 남긴 + `PREPARING`·`FILES_READY`, 키는 읽히는 손상 row) sweep 대신 아래 partition + 삭제로 간다(2026-11-03 05-01). 예전에는 이 row를 보지 않고 `COMPLETED`를 + 돌려줬다. 그래서 진행 중 put의 객체가 purge 뒤에도 읽혔고, `FILES_READY` row는 + 다음 reconcile이 commit해 객체로 되살렸다. row가 없으면 그 partition의 chunk를 grace 없이 sweep한다(05-04, `opfs-byte-store-adapter.ts` `enforcePolicies`). - journal이나 staging receipt가 참조하는 chunk는 이때도 지우지 않는다. report의 - `chunkSweep`이 `COMPLETED`여야 bytes가 지워진 것이다. `DEADLINE_REACHED`면 같은 + staging receipt가 참조하는 chunk는 이때도 지우지 않는다. 이 두 reason에서는 + report의 `chunkSweep`이 `COMPLETED`이거나 `partitionDeleted`가 있을 때 그 + partition의 bytes가 지워진 것이다. `DEADLINE_REACHED`면 같은 purge를 다시 호출하고, `STAGING_STATE_UNREADABLE`이면 읽을 수 없는 staging부터 처리한다. 다른 탭이 바꾸고 있던 객체를 지우지 못하면 report는 `moreObjectsAvailable: true`이고 `chunkSweep`이 없다. 이때도 같은 purge를 다시 호출한다([0929] 05-02) -- 손상 때문에 끝나지 못하는 `LOGOUT`/`ACCOUNT_DELETION` purge는 partition을 - 통째로 지운다([0929] 05-01, `opfs-byte-store-adapter.ts` `enforcePolicies`). +- 손상이나 끝나지 않은 transaction 때문에 끝나지 못하는 `LOGOUT`/ + `ACCOUNT_DELETION` purge는 partition을 통째로 지운다([0929] 05-01, + 2026-11-03 05-01, `opfs-byte-store-adapter.ts` `enforcePolicies`). - 조건: 정책이 그 reason을 허용하고, authority가 이번 purge의 proof를 내주고 consumer가 소비했을 때(IndexedDB의 database 전체 삭제와 같은 승인). 그리고 다음 중 하나를 만났을 때: 목록에서 건너뛴 손상 object row, 손상 row 때문에 - `CORRUPT_DATA`로 끝난 remove나 chunk sweep, `STAGING_STATE_UNREADABLE`. + `CORRUPT_DATA`로 끝난 remove나 chunk sweep, `STAGING_STATE_UNREADABLE`, 그 + partition 키를 가진 journal row(`countPartitionTransactions`가 1 이상). TTL·SESSION_END·UNTIL_SYNCED·PRESSURE는 손상 row를 건너뛰고 세기만 한다. - 범위: worker가 origin mutation lease를 쥔 채 `authorities///` 디렉터리를 @@ -479,9 +489,9 @@ VD-15 목표 runtime을 먼저 구현한다. approved purge meets another tab's put parked before …"(5가지 순서). - 한계: 삭제는 진행 중인 put·delete를 기다린다. source가 멈춘 put은 put idle bound(05-07)까지 삭제를 붙잡는다. purge의 `signal`을 abort하면 기다림을 - 멈추고 `ABORTED`를 반환한다. 삭제가 lock을 요청한 뒤 온 put의 데이터는 다른 - purge처럼 남는다(point-in-time). purge 전에 그 partition의 쓰기를 멈추는 - 것이 composition의 몫이다. + 멈추고 `ABORTED`를 반환한다. purge가 journal row를 센 뒤에 시작한 put, 삭제가 + lock을 요청한 뒤 온 put의 데이터는 남는다(point-in-time). purge 전에 그 + partition의 쓰기를 멈추는 것이 composition의 몫이다. - lock/worker 문제면 OPFS write off → approved capped fallback 또는 online-only - scope binding mismatch면 affected scope를 read-only로 격리하고 registry/ composition을 forward fix; 다른 token path로 bytes를 이동하거나 추측 복구 금지 diff --git a/src/adapters/storage/opfs/indexeddb-opfs-journal.ts b/src/adapters/storage/opfs/indexeddb-opfs-journal.ts index e215e086..b449952f 100644 --- a/src/adapters/storage/opfs/indexeddb-opfs-journal.ts +++ b/src/adapters/storage/opfs/indexeddb-opfs-journal.ts @@ -864,6 +864,48 @@ export function createIndexedDbOpfsJournal( ); }, + async countPartitionTransactions(scope) { + if ( + !keyRange || + !isBoundScope(scope) || + !isValidOpfsStorageScope(scope) + ) { + return browserDataFailure( + keyRange ? "INVALID_INPUT" : "UNSUPPORTED", + "INDEXEDDB_READ", + ); + } + return await withDatabase("INDEXEDDB_READ", (db) => + runTransaction( + db, + [JOURNAL_STORE], + "readonly", + "INDEXEDDB_READ", + (transaction, context) => { + // 2026-11-03 05-01. The key range `removePartition` deletes, so a + // row counted here is a row that delete takes. + const scopeKey = storageScopeKey(scope); + let rows = 0; + walkIndexedDbCursor({ + request: transaction + .objectStore(JOURNAL_STORE) + .index(LOGICAL_KEY_INDEX) + .openCursor( + keyRange.bound(`${scopeKey}|`, `${scopeKey}|\uffff`), + ), + sink: context, + translate: translateFor("INDEXEDDB_READ"), + visit: () => { + rows += 1; + return { kind: "CONTINUE" }; + }, + done: () => context.succeed(rows), + }); + }, + ), + ); + }, + async removePartition(scope) { if ( !keyRange || diff --git a/src/adapters/storage/opfs/opfs-byte-store-adapter.ts b/src/adapters/storage/opfs/opfs-byte-store-adapter.ts index 0f68c3f6..f2b8e2ad 100644 --- a/src/adapters/storage/opfs/opfs-byte-store-adapter.ts +++ b/src/adapters/storage/opfs/opfs-byte-store-adapter.ts @@ -876,6 +876,22 @@ export function createOpfsByteStoreAdapter( } afterObjectId = page.value.nextObjectId; } + // 2026-11-03 05-01. A put or delete of this partition that has not + // finished holds bytes the object walk never sees and the sweep skips: + // one running in another tab, or a row a crash left (PREPARING, or + // FILES_READY, which the next reconcile would commit) or nothing can + // read. Any such row sends the purge to the partition delete below, + // which first waits for every live one. + let unfinished = 0; + if (sweeping && !request.signal?.aborted) { + const counted = await dependencies.journal.countPartitionTransactions( + dependencies.scope, + ); + if (!counted.ok) { + return rebaseFailure(counted.error, "OBJECT_RECONCILE"); + } + unfinished = counted.value; + } // 05-04. Removing an object leaves its chunks, and GC only takes a // chunk 24 h after it was written, so a purged user's bytes stayed on // the device. Once every object is gone the partition's chunks are swept @@ -884,6 +900,7 @@ export function createOpfsByteStoreAdapter( let chunkSweep: OpfsPolicyMaintenanceReport["chunkSweep"]; if ( sweeping && + !unfinished && !corruptObjects && !damaged && !moreObjectsAvailable && @@ -920,7 +937,7 @@ export function createOpfsByteStoreAdapter( // wait for every put and delete of the partition in any tab. const partitionDeleted = sweeping && - (corruptObjects > 0 || damaged) && + (corruptObjects > 0 || damaged || unfinished > 0) && !request.signal?.aborted; if (partitionDeleted) { const removed = await withPartitionLock( @@ -1610,6 +1627,7 @@ function snapshotOpfsJournal( listIncomplete, listCommittedObjects, isChunkReferenced, + countPartitionTransactions, removePartition, } = source; if ( @@ -1624,6 +1642,7 @@ function snapshotOpfsJournal( listIncomplete, listCommittedObjects, isChunkReferenced, + countPartitionTransactions, removePartition, ].some((method) => typeof method !== "function") ) { @@ -1640,6 +1659,7 @@ function snapshotOpfsJournal( listIncomplete: listIncomplete.bind(source), listCommittedObjects: listCommittedObjects.bind(source), isChunkReferenced: isChunkReferenced.bind(source), + countPartitionTransactions: countPartitionTransactions.bind(source), removePartition: removePartition.bind(source), }); } diff --git a/src/application/ports/browser-file-storage/opfs-ports.ts b/src/application/ports/browser-file-storage/opfs-ports.ts index 1d21467e..a43ddffd 100644 --- a/src/application/ports/browser-file-storage/opfs-ports.ts +++ b/src/application/ports/browser-file-storage/opfs-ports.ts @@ -145,9 +145,12 @@ export type OpfsPolicyMaintenanceReport = Readonly<{ moreObjectsAvailable: boolean; deadlineReached: boolean; /** - * 05-04. LOGOUT and ACCOUNT_DELETION only, once every object is removed: - * how the sweep of the partition's now unreferenced chunks ended. Chunk - * bytes are gone only when it is `COMPLETED`, or when `partitionDeleted`. + * 05-04. LOGOUT and ACCOUNT_DELETION only, once every object is removed and + * no put or delete of the partition is unfinished: how the sweep of the + * partition's now unreferenced chunks ended. For those two reasons the + * partition's bytes are gone exactly when this is `COMPLETED` or + * `partitionDeleted` is set; a put that begins after the purge reached its + * journal check is kept (2026-11-03 05-01). */ chunkSweep?: "COMPLETED" | "DEADLINE_REACHED" | "STAGING_STATE_UNREADABLE"; /** @@ -157,8 +160,11 @@ export type OpfsPolicyMaintenanceReport = Readonly<{ corruptObjects?: number; /** * [0929] 05-01. An approved LOGOUT or ACCOUNT_DELETION that damage kept - * from finishing removed the whole partition directory and the partition's - * journal rows instead. Every byte of the partition is gone. + * from finishing, or that found an unfinished put or delete of the + * partition (2026-11-03 05-01), removed the whole partition directory and + * the partition's journal rows instead, after every put and delete of the + * partition then running in any tab had returned. Every byte of the + * partition is gone. */ partitionDeleted?: true; }>; @@ -376,6 +382,14 @@ export interface OpfsJournalPort { scope: OpfsStorageScope, digestHex: string, ): Promise>; + /** + * 2026-11-03 05-01. How many journal rows are keyed to this one partition, + * readable or not: every put or delete of it that has not finished, live in + * some tab or left by a crash. Found by key, like `removePartition`. + */ + countPartitionTransactions( + scope: OpfsStorageScope, + ): Promise>; /** * [0929] 05-01. Deletes every row keyed to this one partition, readable or * not: committed objects, journal rows, chunk references and the budget. diff --git a/tests/browser-capabilities/opfs-runtime.spec.ts b/tests/browser-capabilities/opfs-runtime.spec.ts index cecf5a07..7157a7fc 100644 --- a/tests/browser-capabilities/opfs-runtime.spec.ts +++ b/tests/browser-capabilities/opfs-runtime.spec.ts @@ -229,13 +229,15 @@ type OpfsTabScope = Readonly<{ /** * Installs an OPFS runtime for `scope` in this tab and keeps it on - * `globalThis.__opfsTab` for later evaluate calls. + * `globalThis.__opfsTab` for later evaluate calls. `approvePurges` stands in + * for an authority that approves every LOGOUT and ACCOUNT_DELETION. */ async function installOpfsTab( page: import("@playwright/test").Page, scope: OpfsTabScope, + approvePurges = false, ): Promise { - return await page.evaluate(async (tabScope) => { + return await page.evaluate(async ({ tabScope, approvePurges }) => { const runtimeModulePath = "/src/adapters/storage/opfs/browser-opfs-runtime.ts"; const policyModulePath = "/src/adapters/storage/opfs/opfs-policy.ts"; @@ -272,11 +274,153 @@ async function installOpfsTab( unavailableFallback: "EXPORT_REQUIRED", }, policy, + ...(approvePurges + ? { + requestMaintenanceAuthority: () => ({ + authorized: true as const, + proofToken: `proof_${crypto.randomUUID().replaceAll("-", "")}`, + expiresAtEpochMs: Date.now() + 60_000, + }), + consumeMaintenanceAuthority: () => true, + } + : {}), }); return true; + }, { tabScope: scope, approvePurges }); +} + +/** Deletes everything the OPFS runtime wrote for `scope`'s authority. */ +async function removeOpfsAuthority( + page: import("@playwright/test").Page, + scope: OpfsTabScope, +): Promise { + return await page.evaluate(async (tabScope) => { + const journalModulePath = + "/src/adapters/storage/opfs/indexeddb-opfs-journal.ts"; + const policyModulePath = "/src/adapters/storage/opfs/opfs-policy.ts"; + const { opfsJournalDatabaseName } = (await import( + /* @vite-ignore */ journalModulePath + )) as typeof import("../../src/adapters/storage/opfs/indexeddb-opfs-journal.ts"); + const { DEFAULT_OPFS_RUNTIME_POLICY } = (await import( + /* @vite-ignore */ policyModulePath + )) as typeof import("../../src/adapters/storage/opfs/opfs-policy.ts"); + const deletion = await new Promise((resolve) => { + const request = indexedDB.deleteDatabase( + opfsJournalDatabaseName(tabScope.authorityToken), + ); + request.onsuccess = () => resolve("DELETED"); + request.onerror = () => resolve(request.error?.name ?? "UNKNOWN"); + request.onblocked = () => resolve("BLOCKED"); + }); + try { + const root = await ( + await navigator.storage.getDirectory() + ).getDirectoryHandle(DEFAULT_OPFS_RUNTIME_POLICY.rootDirectoryName); + await ( + await root.getDirectoryHandle("authorities") + ).removeEntry(tabScope.authorityToken, { recursive: true }); + } catch { + // Nothing was written for this authority. + } + return deletion; }, scope); } +// 2026-11-03 05-01 X1. The purge used to report a completed sweep while the +// other tab's put landed, and the object stayed readable after the account +// deletion. It now waits for that put and takes it with the partition. +test("05-01 an approved purge in another tab waits for a live put and takes it with the partition", async ({ + page, +}) => { + const secondPage = await page.context().newPage(); + const suffix = crypto.randomUUID().replaceAll("-", ""); + const scope: OpfsTabScope = { + namespace: "durable-objects", + authorityToken: `authority_${suffix}`, + namespaceToken: `namespace_${suffix}`, + partitionToken: `partition_${suffix}`, + }; + type Tab = import("../../src/adapters/storage/opfs/browser-opfs-runtime.ts").BrowserOpfsRuntime; + try { + await Promise.all([page.goto("/"), secondPage.goto("/")]); + if (!(await installOpfsTab(page, scope))) return; + expect(await installOpfsTab(secondPage, scope, true)).toBe(true); + + await page.evaluate(async () => { + const state = globalThis as unknown as { + __opfsTab: Tab; + __release?: () => void; + __put?: Promise; + }; + let streaming!: () => void; + const started = new Promise((resolve) => { + streaming = resolve; + }); + const gate = new Promise((resolve) => { + state.__release = resolve; + }); + state.__put = state.__opfsTab.objects.put({ + objectId: "object_tab_12345678", + expectedGeneration: null, + mediaType: "application/octet-stream", + source: { + byteLength: 4, + async *stream() { + streaming(); + await gate; + yield { ok: true as const, value: new Uint8Array([2, 7, 1, 8]) }; + }, + }, + }); + await started; + }); + await secondPage.evaluate(() => { + const state = globalThis as unknown as { + __opfsTab: Tab; + __purge?: Promise; + }; + state.__purge = state.__opfsTab.maintenance.enforcePolicies({ + reason: "ACCOUNT_DELETION", + }); + }); + await page.waitForTimeout(250); + const put = await page.evaluate(async () => { + const state = globalThis as unknown as { + __release: () => void; + __put: Promise; + }; + state.__release(); + return await state.__put; + }); + const report = await secondPage.evaluate( + async () => + await (globalThis as unknown as { __purge: Promise }).__purge, + ); + const opened = await secondPage.evaluate(async () => { + const runtime = (globalThis as unknown as { __opfsTab: Tab }).__opfsTab; + const result = await runtime.objects.open({ + objectId: "object_tab_12345678", + }); + return result.ok ? "OPENED" : result.error.code; + }); + + expect(put).toMatchObject({ ok: true, value: { generation: 1 } }); + expect(report).toMatchObject({ + ok: true, + value: { moreObjectsAvailable: false, partitionDeleted: true }, + }); + expect(opened).toBe("NOT_FOUND"); + } finally { + for (const tab of [page, secondPage]) { + await tab.evaluate(() => + (globalThis as unknown as { __opfsTab?: Tab }).__opfsTab?.close(), + ); + } + expect(await removeOpfsAuthority(page, scope)).toBe("DELETED"); + await secondPage.close(); + } +}); + test("05-01 a reconcile in another tab skips a live put and the put stays readable", async ({ page, }) => { @@ -375,37 +519,7 @@ test("05-01 a reconcile in another tab skips a live put and the put stays readab (globalThis as unknown as { __opfsTab?: Tab }).__opfsTab?.close(), ); } - const cleanup = await page.evaluate(async (tabScope) => { - const journalModulePath = - "/src/adapters/storage/opfs/indexeddb-opfs-journal.ts"; - const policyModulePath = "/src/adapters/storage/opfs/opfs-policy.ts"; - const { opfsJournalDatabaseName } = (await import( - /* @vite-ignore */ journalModulePath - )) as typeof import("../../src/adapters/storage/opfs/indexeddb-opfs-journal.ts"); - const { DEFAULT_OPFS_RUNTIME_POLICY } = (await import( - /* @vite-ignore */ policyModulePath - )) as typeof import("../../src/adapters/storage/opfs/opfs-policy.ts"); - const deletion = await new Promise((resolve) => { - const request = indexedDB.deleteDatabase( - opfsJournalDatabaseName(tabScope.authorityToken), - ); - request.onsuccess = () => resolve("DELETED"); - request.onerror = () => resolve(request.error?.name ?? "UNKNOWN"); - request.onblocked = () => resolve("BLOCKED"); - }); - try { - const root = await ( - await navigator.storage.getDirectory() - ).getDirectoryHandle(DEFAULT_OPFS_RUNTIME_POLICY.rootDirectoryName); - await ( - await root.getDirectoryHandle("authorities") - ).removeEntry(tabScope.authorityToken, { recursive: true }); - } catch { - // Nothing was written for this authority. - } - return deletion; - }, scope); - expect(cleanup).toBe("DELETED"); + expect(await removeOpfsAuthority(page, scope)).toBe("DELETED"); await secondPage.close(); } }); diff --git a/tests/unit/indexeddb-opfs-journal.test.ts b/tests/unit/indexeddb-opfs-journal.test.ts index daca31de..ed3dab4d 100644 --- a/tests/unit/indexeddb-opfs-journal.test.ts +++ b/tests/unit/indexeddb-opfs-journal.test.ts @@ -865,6 +865,51 @@ describe("[0929] 05-01 a corrupt row is skipped and counted; a partition's rows ).toMatchObject({ preparedObject: { corrupt: true } }); }); + it("counts the unfinished rows of one partition, a damaged one included (2026-11-03 05-01)", async () => { + const { factory, journal } = createHarness(); + await commitObject(journal, "transaction_obj00001", "object_aaaa0001", 1); + expect(await journal.countPartitionTransactions(scope)).toEqual({ + ok: true, + value: 0, + }); + expect( + await journal.begin( + beginInput("transaction_live0002", "object_bbbb0002", 1), + ), + ).toMatchObject({ ok: true }); + expect( + await journal.begin( + beginInput( + "transaction_kept0003", + "object_kept_0003", + 1, + otherPartition, + ), + ), + ).toMatchObject({ ok: true }); + // A row whose keys still read but whose value does not. + factory.seed("opfs-journal", { + ...(factory.readRaw("opfs-journal", "transaction_live0002") as object), + transactionId: "transaction_bad00004", + logicalKey: `${scopeKeyOf(scope)}|object_dddd0004`, + phase: "GARBLED", + }); + + expect(await journal.countPartitionTransactions(scope)).toEqual({ + ok: true, + value: 2, + }); + expect(await journal.countPartitionTransactions(otherPartition)).toEqual({ + ok: true, + value: 1, + }); + expect(await journal.removePartition(scope)).toMatchObject({ ok: true }); + expect(await journal.countPartitionTransactions(scope)).toEqual({ + ok: true, + value: 0, + }); + }); + it("removes every row keyed to one partition, the corrupt one included, and none of another", async () => { const { factory, journal } = createHarness(); await commitObject(journal, "transaction_obj00001", "object_aaaa0001", 1); diff --git a/tests/unit/opfs-byte-store.test.ts b/tests/unit/opfs-byte-store.test.ts index 2467ad41..fb60e1e3 100644 --- a/tests/unit/opfs-byte-store.test.ts +++ b/tests/unit/opfs-byte-store.test.ts @@ -243,6 +243,15 @@ function createJournal(): OpfsJournalPort & { ), ); }, + async countPartitionTransactions(scope) { + return browserDataSuccess( + [...transactions.values()].filter( + (transaction) => + transaction.scope.namespaceToken === scope.namespaceToken && + transaction.scope.partitionToken === scope.partitionToken, + ).length, + ); + }, async removePartition() { objects.clear(); transactions.clear(); diff --git a/tests/unit/opfs-worker-runtime.test.ts b/tests/unit/opfs-worker-runtime.test.ts index e3514111..5e1aa194 100644 --- a/tests/unit/opfs-worker-runtime.test.ts +++ b/tests/unit/opfs-worker-runtime.test.ts @@ -11,7 +11,10 @@ import type { BrowserStoragePolicy, ByteSource, } from "../../src/application/ports/browser-file-storage/shared.ts"; -import { browserDataSuccess } from "../../src/adapters/browser-file-storage/result.ts"; +import { + browserDataFailure, + browserDataSuccess, +} from "../../src/adapters/browser-file-storage/result.ts"; import { createOpfsByteStoreAdapter } from "../../src/adapters/storage/opfs/opfs-byte-store-adapter.ts"; import { createIndexedDbOpfsJournal } from "../../src/adapters/storage/opfs/indexeddb-opfs-journal.ts"; import { @@ -649,6 +652,7 @@ function referenceJournal(referenced: ReadonlySet): OpfsJournalPort { complete: unused, rollback: unused, listCommittedObjects: unused, + countPartitionTransactions: unused, removePartition: unused, async listIncomplete() { return browserDataSuccess({ @@ -2414,8 +2418,12 @@ describe("05-04 a LOGOUT or ACCOUNT_DELETION purge leaves no partition bytes beh expect(filesUnder(root, scopeA)).toEqual([]); }); - it("never sweeps the chunk of a put that runs while the purge does", async () => { - const { tab } = sharedRootTabs(); + // 2026-11-03 05-01 X1. The purge used to walk past a put that began before + // it, report a completed sweep and leave that object readable. A put or + // delete of the partition that has not finished now sends the purge to the + // partition delete, which waits for it and then takes it. + it("waits for a put that began before the purge and takes it with the partition", async () => { + const { root, tab } = sharedRootTabs(); const purger = tab("p", authority); const writer = tab("w", authority); expect(await putBytes(purger, "object_secret_1", [1, 2, 3])).toMatchObject({ ok: true }); @@ -2432,23 +2440,127 @@ describe("05-04 a LOGOUT or ACCOUNT_DELETION purge leaves no partition bytes beh stalled.release(); expect(await put).toMatchObject({ ok: true }); - expect(await purge).toMatchObject({ ok: true, value: { removedObjects: 1 } }); - expect(await openedBytes(writer, "object_late_0001")).toEqual({ - generation: 1, - bytes: [5], + const report = await purge; + expect(report).toMatchObject({ + ok: true, + value: { + removedObjects: 1, + moreObjectsAvailable: false, + partitionDeleted: true, + }, }); + expect(report.ok && report.value.chunkSweep).toBeUndefined(); + expect(filesUnder(root, scopeA)).toEqual([]); + expect(await openedBytes(writer, "object_late_0001")).toBe("NOT_FOUND"); }); + // 2026-11-03 05-01 X2, X2b, X3. A put that stopped before it finished leaves + // its journal row and its staged files. The purge reported a completed sweep + // over them; the next reconcile then committed a FILES_READY put, so the + // account's file came back as a readable object. + it.each([ + ["stopped before commit (FILES_READY)", "commitPut", false], + ["stopped while uploading (PREPARING)", "markFilesReady", false], + ["whose journal row is damaged", "commitPut", true], + ] as const)( + "takes the partition when an approved purge finds a put %s", + async (_label, failingStep, damageRow) => { + const { root, idb, tab } = sharedRootTabs(); + const journal = createIndexedDbOpfsJournal({ + authorityToken: scopeA.authorityToken, + factory: idb.factory, + keyRange: idb.keyRange, + }); + const writerWorker = loopbackGateway( + createOpfsWorkerRuntime({ + root: root as unknown as FileSystemDirectoryHandle, + crypto: globalThis.crypto, + policy: runtimePolicy, + leaseManager: exclusiveLeases(), + dedicatedWorker: true, + supportsSynchronousAccessHandles: false, + }), + ); + const unavailable = async () => + browserDataFailure("UNAVAILABLE", "INDEXEDDB_WRITE", { + retryable: true, + recovery: "RETRY", + }); + // The tab crashes at this step: the journal write never lands and, + // before commit, its compensation never runs either. + const writer = tab("w", { + journal: { ...journal, [failingStep]: unavailable } as OpfsJournalPort, + worker: { + ...writerWorker, + abortPreparedPut: async () => + browserDataFailure("UNAVAILABLE", "OBJECT_WRITE", { + retryable: true, + recovery: "RETRY", + }), + }, + }); + const purger = tab("p", authority); + expect(await putBytes(purger, "object_secret_1", [1, 2, 3])).toMatchObject({ ok: true }); + expect(await putBytes(writer, "object_dead_0001", [9, 9, 9])).toMatchObject({ + ok: false, + }); + expect(idb.readRaw("opfs-journal", "transaction_w_1")).toBeDefined(); + if (damageRow) { + idb.seed("opfs-journal", { + ...(idb.readRaw("opfs-journal", "transaction_w_1") as object), + phase: "GARBLED", + }); + } + + const report = await purger.maintenance.enforcePolicies({ + reason: "ACCOUNT_DELETION", + }); + + expect(report).toMatchObject({ + ok: true, + value: { moreObjectsAvailable: false, partitionDeleted: true }, + }); + expect(filesUnder(root, scopeA)).toEqual([]); + expect(idb.readRaw("opfs-journal", "transaction_w_1")).toBeUndefined(); + // Nothing is left for a later reconcile to bring back. + expect(await purger.maintenance.reconcile()).toMatchObject({ + ok: true, + value: { committedTransactions: 0, corruptTransactions: 0 }, + }); + expect(await openedBytes(purger, "object_dead_0001")).toBe("NOT_FOUND"); + expect(filesUnder(root, scopeA)).toEqual([]); + }, + ); + // [0929] 05-02. Another tab overwrites the object the purge is removing, so // the purge's remove ends in CONFLICT. That purge is not finished and must - // not claim a completed chunk sweep. + // not claim a completed chunk sweep. The overwrite finishes between the + // purge's listing and its remove: one still running when the purge checks + // the journal sends it to the partition delete instead (2026-11-03 05-01). it("reports a purge that skipped a changing object as unfinished", async () => { - const { root, tab } = sharedRootTabs(); - const purger = tab("p", authority); - const writer = tab("w", authority); - expect(await putBytes(purger, "object_secret_1", [1, 2, 3])).toMatchObject({ ok: true }); + const { root, idb, tab } = sharedRootTabs(); + const journal = createIndexedDbOpfsJournal({ + authorityToken: scopeA.authorityToken, + factory: idb.factory, + keyRange: idb.keyRange, + }); const stalled = stalledSource(); - const overwrite = writer.objects.put({ + let overwrite: ReturnType | undefined; + const purger = tab("p", { + ...authority, + journal: { + ...journal, + async listCommittedObjects(request) { + const page = await journal.listCommittedObjects(request); + stalled.release(); + await overwrite; + return page; + }, + }, + }); + const writer = tab("w", authority); + expect(await putBytes(writer, "object_secret_1", [1, 2, 3])).toMatchObject({ ok: true }); + overwrite = writer.objects.put({ objectId: "object_secret_1", expectedGeneration: 1, mediaType: "application/octet-stream", @@ -2456,8 +2568,6 @@ describe("05-04 a LOGOUT or ACCOUNT_DELETION purge leaves no partition bytes beh }); await stalled.streaming; const purge = purger.maintenance.enforcePolicies({ reason: "ACCOUNT_DELETION" }); - await new Promise((resolve) => setTimeout(resolve, 20)); - stalled.release(); expect(await overwrite).toMatchObject({ ok: true }); const first = await purge;