From 23662fb23a91b230a1ae6d06dbec51cb69d419ee Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 5 Nov 2026 22:08:19 +0900 Subject: [PATCH 1/8] =?UTF-8?q?fix(storage):=20=EA=B0=9D=EC=B2=B4=EB=A5=BC?= =?UTF-8?q?=20=EB=81=9D=EA=B9=8C=EC=A7=80=20=EB=8F=88=20=EA=B3=84=EC=A0=95?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C=20purge=EB=8A=94=20=EB=8A=98=20partition?= =?UTF-8?q?=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는 객체 목록을 읽고, 하나씩 지우고, 마지막에 journal 행을 한 번 센 뒤 0이면 chunk sweep으로 끝나 `chunkSweep: "COMPLETED"`를 돌려준다. 목록과 세기는 각각 한 순간만 본다. - 다른 탭이 새 객체를 put하는 중이면 purge의 remove가 그 put이 쥔 origin lease를 기다린다. partition에 객체가 3개 이상이면 그 put은 목록 뒤에 commit하고 세기 전에 complete까지 끝난다. 목록에도 행 수에도 없어 sweep이 그 chunk를 건너뛰고, 계정 삭제 뒤에도 파일이 읽힌다 (리뷰 실험 Y1b 메모리 20/20, Chromium 6/6). 저장소 테스트는 객체 0~1개로만 돌아서 이 틈을 놓쳤다. - FILES_READY에서 멈춘 put의 행이 `logicalKey`를 잃으면 세지 못한다. staging receipt가 chunk를 sweep에서 지켜 purge마다 COMPLETED인데 receipt·chunk·manifest가 남는다(Y2). 고른 방법: 마지막 객체까지 돈 purge는 sweep 대신 늘 partition 삭제로 끝낸다(리뷰 2.1절 (d')). exclusive partition lock이 그때 진행 중인 put·delete를 모두 기다린 뒤 디렉터리 전체(staging 포함)와 그 partition 행을 지우므로 두 경우를 함께 막는다. 객체 순회는 `removedObjects`· `releasedBytes` 보고와 예산 단위 진행을 위해 남긴다. 예산이나 다른 탭의 변경(CONFLICT) 때문에 끝까지 돌지 못한 호출은 지금처럼 `moreObjectsAvailable: true`를 돌려주고, 그때만 journal 행을 세서 끝나지 않은 행이 있으면 바로 partition을 지운다(2026-11-03 05-01 그대로). purge 성공 report는 이제 `partitionDeleted`와 `moreObjectsAvailable` 중 정확히 하나를 가진다(walk 뒤 abort도 more로 보고). 버린 대안: - (a) sweep 뒤에 다시 세고 committed 객체를 한 번 더 본다: ①만 막고 ②가 남는다. 다시 볼 때 정책상 남겨야 할 객체를 가려내려면 목록을 끝까지 다시 읽어야 한다. - (b)+(a): 둘 다 막지만 판단 조건이 일곱 개가 된다. - 세기를 아예 없애기: 예산에 걸린 호출의 crash 행 처리가 다음 호출로 밀린다. 지금 동작을 바꿀 이유가 없어 남겼다. 위험·한계: - 모든 승인 purge가 다른 탭의 진행 중 put을 기다린다(지금까지는 끝나지 않은 행이 있을 때만). 기다림 상한은 `mutationLockWaitTimeoutMs` (1103/05-02)이고 abort로 멈출 수 있다. - 같은 partition에 LOGOUT 때 남겨야 하는 객체가 섞이면 이 방법은 맞지 않는다. 지금 정책 검사는 OPAQUE_PARTITION이면 LOGOUT·ACCOUNT_DELETION 모두 purge만 허용해 그런 객체가 생기지 않는다. - port의 `chunkSweep`은 아무도 채우지 않는다. 읽는 코드가 깨지지 않게 타입은 남기고 `@deprecated`로 적었다. - recipe fake의 purge도 늘 `partitionDeleted`다. - `logicalKey`를 잃은 행 자체는 여전히 남는다(05-03 설계). 그 bytes는 이제 남지 않는다. - Firefox·WebKit에서 Y1은 돌리지 않았다. browser spec은 객체 0·3개 두 경우로 늘렸다. 검증: desktop batch에서 확인 - node scripts/run-vitest.ts run tests/unit/opfs-worker-runtime.test.ts tests/unit/opfs-byte-store.test.ts tests/unit/indexeddb-opfs-journal.test.ts - node scripts/run-vitest.ts run tests/recipes/browser-file-storage-contracts.test.ts - corepack pnpm test:browser-capabilities tests/browser-capabilities/opfs-runtime.spec.ts --project=chromium Refs: 1103/05-01 --- .../browser-file-storage-recovery.md | 71 ++--- .../browser-file-storage-fakes.ts | 14 +- .../storage/opfs/opfs-byte-store-adapter.ts | 73 ++---- .../ports/browser-file-storage/opfs-ports.ts | 31 ++- .../browser-capabilities/opfs-runtime.spec.ts | 204 +++++++++------ .../browser-file-storage-contracts.test.ts | 10 +- tests/unit/opfs-worker-runtime.test.ts | 245 ++++++++++++++++-- 7 files changed, 442 insertions(+), 206 deletions(-) diff --git a/docs/operations/browser-file-storage-recovery.md b/docs/operations/browser-file-storage-recovery.md index dc245541..dd758100 100644 --- a/docs/operations/browser-file-storage-recovery.md +++ b/docs/operations/browser-file-storage-recovery.md @@ -40,23 +40,24 @@ cleanup 결과를 확인하지 못하면 성공 값이 아니라 실패로 답 계속 돌고, 관측은 `OBJECT_RECONCILE` `DEGRADED` / `CORRUPT_DATA`로 남는다 ([0929] 05-01). reconcile은 그 row를 지우지 않는다. 그 partition이 승인된 purge로 통째로 지워질 때 함께 지워지는 것은 partition 키(`logicalKey`)를 - 읽을 수 있는 row뿐이다(§7). 승인된 LOGOUT/ACCOUNT_DELETION purge는 그런 - row를 보면 그 partition을 지운다(2026-11-03 05-01). + 읽을 수 있는 row뿐이다(§7). 승인된 LOGOUT/ACCOUNT_DELETION purge는 객체를 + 끝까지 돌면 늘 그 partition을 지운다(1103/05-01 잔여). - `unattributedCorruptTransactions`: 위 수 가운데 `logicalKey`까지 읽을 수 없는 row 수다. 어느 partition에도 속하지 않아 partition 삭제도 지우지 못한다. 그래서 따로 세고 `DEGRADED` 관측에서 뺀다(2026-11-03 05-03). 예전에는 이런 row 하나가 reconcile마다 `DEGRADED`를 남겨 경보가 늘 켜졌다. 같은 수로 남아 있으면 할 일이 없다. 수가 늘면 키 없는 row를 쓴 writer(다른 release, 외부 도구)를 찾는다. 이 row도 수동으로 지우지 않는다(3번). 그 row의 - staging은 partition 디렉터리 안에 있어 그 partition이 지워지면 함께 - 지워지지만, 이 row는 purge의 partition 삭제 조건에 들지 않는다. + staging은 partition 디렉터리 안에 있어 승인된 purge가 그 partition을 + 지울 때 함께 지워진다. 예전에는 purge가 이 row를 세지 못해 chunk sweep으로 + 끝났고, staging receipt가 지킨 chunk·manifest가 `COMPLETED` 뒤에도 + 남았다(1103/05-01 잔여). 3. journal row를 수동으로 삭제하지 않는다. row가 사라지면 stale staging을 추적할 근거가 사라지고 quota만 누수된다. 4. OPFS root나 journal database를 통째로 삭제하거나 schema를 downgrade하지 않는다. rollback은 새 v2 write admission을 닫고 v1+v2 reader를 유지하는 것으로 - 수행한다. runtime이 스스로 지우는 것은 승인된 purge가 손상이나 끝나지 않은 - transaction 때문에 끝나지 못할 때의 그 partition 디렉터리 하나와 그 partition의 - journal row뿐이다(§7). + 수행한다. runtime이 스스로 통째로 지우는 것은 승인된 LOGOUT/ACCOUNT_DELETION + purge의 그 partition 디렉터리 하나와 그 partition의 journal row뿐이다(§7). ## 1. 공통 원칙 @@ -450,27 +451,33 @@ 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의 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`). - 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) -- 손상이나 끝나지 않은 transaction 때문에 끝나지 못하는 `LOGOUT`/ - `ACCOUNT_DELETION` purge는 partition을 통째로 지운다([0929] 05-01, - 2026-11-03 05-01, `opfs-byte-store-adapter.ts` `enforcePolicies`). +- `LOGOUT`/`ACCOUNT_DELETION` purge는 객체를 하나씩 지운 뒤, 마지막 객체까지 + 돌았으면 늘 아래 partition 삭제로 끝낸다(1103/05-01 잔여, + `opfs-byte-store-adapter.ts` `enforcePolicies`). chunk sweep으로 끝내지 않는다. + - 예전 동작: 객체를 다 돈 뒤 journal row를 한 번 세고, 0이면 chunk를 grace 없이 + sweep해 `chunkSweep: "COMPLETED"`를 돌려줬다(05-04, 2026-11-03 05-01). 목록과 + 세기는 각각 한 순간만 본다. 그래서 두 경우에 bytes가 남은 채 `COMPLETED`였다. + ① 다른 tab이 새 객체를 put하는 중이고 partition에 객체가 3개 이상이면, 그 put이 + 목록 뒤에 commit하고 세기 전에 끝났다. 목록에도 row 수에도 없어 sweep이 그 + chunk를 건너뛰었고, 그 파일이 계정 삭제 뒤에도 읽혔다(메모리 20/20, Chromium + 6/6). ② 멈춘 put의 journal row가 `logicalKey`를 잃으면 세지 못했고, 그 + staging receipt·chunk·manifest가 purge마다 남았다. + - 이제 결과: 그 partition의 bytes가 지워진 것은 report에 `partitionDeleted`가 + 있을 때다. `chunkSweep`은 더 이상 채우지 않는다(port 타입에는 호환을 위해 + 남겼다). + - 예산(`budgetMs`·`maxObjects`)이나 다른 tab의 변경 때문에 객체를 끝까지 돌지 + 못하면 report는 `moreObjectsAvailable: true`이고 `partitionDeleted`가 없다. + 같은 purge를 다시 호출한다([0929] 05-02). 다만 이때 그 partition의 journal + row(다른 tab에서 진행 중인 put·delete, crash가 남긴 `PREPARING`·`FILES_READY`, + 키는 읽히는 손상 row)가 하나라도 있으면 그 호출이 바로 partition을 지운다 + (2026-11-03 05-01). +- 승인된 `LOGOUT`/`ACCOUNT_DELETION` purge의 partition 삭제([0929] 05-01, + 2026-11-03 05-01, 1103/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`, 그 + 다음 중 하나일 때: 마지막 객체까지 돌았다, 목록에서 건너뛴 손상 object row, + 손상 row 때문에 `CORRUPT_DATA`로 끝난 remove, 끝까지 돌지 못한 호출에서 그 partition 키를 가진 journal row(`countPartitionTransactions`가 1 이상). TTL·SESSION_END·UNTIL_SYNCED·PRESSURE는 손상 row를 건너뛰고 세기만 한다. - 범위: worker가 origin mutation lease를 쥔 채 @@ -482,8 +489,9 @@ VD-15 목표 runtime을 먼저 구현한다. row는 남긴다. 다른 partition은 건드리지 않는다. - 결과: `partitionDeleted: true`, `moreObjectsAvailable: false`, 건너뛴 row 수 `corruptObjects`. 이 report면 그 partition의 bytes는 모두 지워진 것이다. - `chunkSweep`은 없다. 디렉터리는 지웠는데 journal 정리가 실패하면 실패를 - 반환한다. 같은 purge를 다시 호출하면 이어서 끝난다. + `removedObjects`·`releasedBytes`는 삭제 전에 하나씩 지운 객체만 센다. + 디렉터리는 지웠는데 journal 정리가 실패하면 실패를 반환한다. 같은 purge를 + 다시 호출하면 이어서 끝난다. - 다른 tab의 put·delete와의 순서: 모든 put·delete는 journal `begin`부터 끝날 때까지 partition lock(Web Lock `:partition:::`)을 @@ -504,9 +512,10 @@ VD-15 목표 runtime을 먼저 구현한다. 멈추고 `ABORTED`를 반환한다. 삭제 뒤에 줄 선 put·delete도 자기 `signal`로 기다림을 멈춘다. 어느 쪽이든 partition lock 기다림은 `mutationLockWaitTimeoutMs`(기본 10분)에서 `BLOCKED`(retryable)로 끝난다 - (2026-11-03 05-02). purge가 journal row를 센 뒤에 시작한 put, 삭제가 - lock을 요청한 뒤 온 put의 데이터는 남는다(point-in-time). purge 전에 그 - partition의 쓰기를 멈추는 것이 composition의 몫이다. + (2026-11-03 05-02). 삭제가 lock을 요청한 뒤 온 put의 데이터는 남는다 + (point-in-time). 그 전에 시작한 put은 삭제가 기다렸다가 함께 지운다. + 모든 승인 purge가 이제 이 lock을 기다리므로, 다른 tab에서 오래 걸리는 put이 + 있으면 purge도 그만큼 늦게 끝난다. - 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/recipes/frontend-capabilities/browser-file-storage-fakes.ts b/recipes/frontend-capabilities/browser-file-storage-fakes.ts index d23f572b..cc2ca491 100644 --- a/recipes/frontend-capabilities/browser-file-storage-fakes.ts +++ b/recipes/frontend-capabilities/browser-file-storage-fakes.ts @@ -1652,8 +1652,9 @@ type MemoryDurableObjectStoreOptions = Readonly<{ * a journal row, and a LOGOUT or ACCOUNT_DELETION the policy allows leaves the * partition empty. `reconcile` rolls a row that stopped while PREPARING back * and commits one that stopped after FILES_READY, like the runtime's - * roll-forward; such a purge also drops every stopped row and reports - * `partitionDeleted` (2026-11-03 05-01, 05-04). + * roll-forward; such a purge also drops every stopped row. Every purge + * reports `partitionDeleted`, never a chunk sweep (2026-11-03 05-01, 05-04, + * 1103/05-01 rest). * * It does not model the worker, Web Locks, the composition authority, chunk * dedup, orphan GC or the other maintenance reasons (`UNSUPPORTED`). Its @@ -1926,8 +1927,9 @@ export class MemoryDurableObjectStore const removedObjects = this.#objects.size; this.#objects.clear(); // 2026-11-03 05-01. A stopped put goes with the partition, as the - // runtime's partition delete takes its row and its files. - const partitionDeleted = this.#pending.size > 0; + // runtime's partition delete takes its row and its files. 1103/05-01 + // rest: the runtime ends every purge that walked all objects in that + // delete, so this one always reports it. this.#pending.clear(); return dataSuccess( Object.freeze({ @@ -1936,9 +1938,7 @@ export class MemoryDurableObjectStore releasedBytes, moreObjectsAvailable: false, deadlineReached: false, - ...(partitionDeleted - ? { partitionDeleted: true as const } - : { chunkSweep: "COMPLETED" as const }), + partitionDeleted: true as const, }), ); } diff --git a/src/adapters/storage/opfs/opfs-byte-store-adapter.ts b/src/adapters/storage/opfs/opfs-byte-store-adapter.ts index 9c918f51..e150a461 100644 --- a/src/adapters/storage/opfs/opfs-byte-store-adapter.ts +++ b/src/adapters/storage/opfs/opfs-byte-store-adapter.ts @@ -805,8 +805,7 @@ export function createOpfsByteStoreAdapter( const sweeping = request.reason === "LOGOUT" || request.reason === "ACCOUNT_DELETION"; // [0929] 05-01. Rows the journal could not read and skipped, and damage - // met past the listing: a remove or the sweep failing on an unreadable - // row, or staging the sweep cannot read. + // met past the listing: a remove failing on an unreadable row. let corruptObjects = 0; let damaged = false; @@ -888,14 +887,25 @@ 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. + // 1103/05-01 (rest). A walk that reached the last object no longer ends + // in a chunk sweep. The walk and a journal count each see one moment: + // another tab's put of a new object that committed after the listing + // and settled before the count was in neither, and a stopped put whose + // row lost its partition key is never counted. The sweep then skipped + // their chunks and staging and still reported COMPLETED, so the file + // stayed readable after the account deletion. Such a walk now always + // ends in the partition delete below, which waits for every put and + // delete of the partition and takes the whole directory, staging + // included, so nothing that began before it is left. + const walkFinished = !moreObjectsAvailable && !deadlineReached; + // 2026-11-03 05-01. A walk the budget or another tab's change stopped + // leaves the rest for the next call, unless a put or delete of this + // partition has not finished: 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 this call to the + // partition delete too. let unfinished = 0; - if (sweeping && !request.signal?.aborted) { + if (sweeping && !walkFinished && !request.signal?.aborted) { const counted = await dependencies.journal.countPartitionTransactions( dependencies.scope, ); @@ -904,40 +914,6 @@ export function createOpfsByteStoreAdapter( } 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 - // without that grace. The sweep still skips any chunk the journal or a - // staged put references. - let chunkSweep: OpfsPolicyMaintenanceReport["chunkSweep"]; - if ( - sweeping && - !unfinished && - !corruptObjects && - !damaged && - !moreObjectsAvailable && - !deadlineReached && - !request.signal?.aborted - ) { - const swept = await reconcileOrphanChunks( - deadline, - request.signal, - now(), - ); - if (!swept.ok) { - if (swept.error.code !== "CORRUPT_DATA") { - return rebaseFailure(swept.error, "OBJECT_RECONCILE"); - } - damaged = true; - } else { - chunkSweep = swept.value.status; - damaged = chunkSweep === "STAGING_STATE_UNREADABLE"; - if (chunkSweep === "DEADLINE_REACHED") { - deadlineReached = true; - moreObjectsAvailable = true; - } - } - } // [0929] 05-01. A row nothing can read never lets this purge finish, so // the user's bytes would stay on the device for good. The authority // approved this exact LOGOUT or ACCOUNT_DELETION (the same approval the @@ -949,7 +925,7 @@ export function createOpfsByteStoreAdapter( // wait for every put and delete of the partition in any tab. const partitionDeleted = sweeping && - (corruptObjects > 0 || damaged || unfinished > 0) && + (walkFinished || corruptObjects > 0 || damaged || unfinished > 0) && !request.signal?.aborted; if (partitionDeleted) { const removed = await withPartitionLock( @@ -970,8 +946,12 @@ export function createOpfsByteStoreAdapter( if (!removed.ok) { return rebaseFailure(removed.error, "OBJECT_RECONCILE"); } - chunkSweep = undefined; moreObjectsAvailable = false; + } else if (sweeping) { + // Only an abort between the walk and the delete gets here with the + // walk finished. Either way the partition still holds bytes, so a + // purge reports exactly one of `partitionDeleted` and more work. + moreObjectsAvailable = true; } const report: OpfsPolicyMaintenanceReport = Object.freeze({ inspectedObjects, @@ -979,7 +959,6 @@ export function createOpfsByteStoreAdapter( releasedBytes, moreObjectsAvailable, deadlineReached, - ...(chunkSweep ? { chunkSweep } : {}), ...(corruptObjects ? { corruptObjects } : {}), ...(partitionDeleted ? { partitionDeleted } : {}), }); @@ -1365,7 +1344,6 @@ export function createOpfsByteStoreAdapter( async function reconcileOrphanChunks( deadline: number, signal: AbortSignal | undefined, - olderThanEpochMs = Math.max(0, now() - policy.orphanGracePeriodMs), ): Promise< BrowserDataResult< Readonly<{ @@ -1378,6 +1356,7 @@ export function createOpfsByteStoreAdapter( }> > > { + const olderThanEpochMs = Math.max(0, now() - policy.orphanGracePeriodMs); let inspected = 0; let deleted = 0; // R-5.1. Candidates include committed chunks, so a page can be all diff --git a/src/application/ports/browser-file-storage/opfs-ports.ts b/src/application/ports/browser-file-storage/opfs-ports.ts index 7c638698..48dac066 100644 --- a/src/application/ports/browser-file-storage/opfs-ports.ts +++ b/src/application/ports/browser-file-storage/opfs-ports.ts @@ -152,12 +152,15 @@ export type OpfsPolicyMaintenanceReport = Readonly<{ moreObjectsAvailable: boolean; deadlineReached: boolean; /** - * 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). + * 05-04. How a LOGOUT or ACCOUNT_DELETION chunk sweep ended. + * + * @deprecated The reference runtime and the recipe fake no longer set it + * (1103/05-01 rest): a sweep after the object walk missed a new object + * another tab's put settled during the walk, and the staging of a stopped + * put whose journal row lost its partition key, and still said + * `COMPLETED`. Such a purge now ends in the partition delete, so + * `partitionDeleted` is the one completion signal. Kept so code that reads + * it still compiles; it is always absent. */ chunkSweep?: "COMPLETED" | "DEADLINE_REACHED" | "STAGING_STATE_UNREADABLE"; /** @@ -166,12 +169,16 @@ export type OpfsPolicyMaintenanceReport = Readonly<{ */ corruptObjects?: number; /** - * [0929] 05-01. An approved LOGOUT or ACCOUNT_DELETION that damage kept - * 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. + * [0929] 05-01. An approved LOGOUT or ACCOUNT_DELETION removed the whole + * partition directory and the partition's journal rows, after every put + * and delete of the partition then running in any tab had returned. Every + * byte of the partition is gone; a put that begins after that delete is + * kept. A purge that walked every object always ends this way (1103/05-01 + * rest). One the budget or another tab's change stopped part-way ends this + * way only when damage or an unfinished put or delete of the partition + * (2026-11-03 05-01) was met; otherwise it reports `moreObjectsAvailable` + * and the caller runs it again. For these two reasons the partition's bytes + * are gone exactly when this is set. */ partitionDeleted?: true; }>; diff --git a/tests/browser-capabilities/opfs-runtime.spec.ts b/tests/browser-capabilities/opfs-runtime.spec.ts index 7157a7fc..59d42d64 100644 --- a/tests/browser-capabilities/opfs-runtime.spec.ts +++ b/tests/browser-capabilities/opfs-runtime.spec.ts @@ -329,97 +329,129 @@ async function removeOpfsAuthority( // 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); +// 1103/05-01 rest. That fix still reported a completed sweep, with the new +// object readable, once the purged partition held three or more objects: +// each remove waits for the origin lease the put holds, so the put settled +// before the purge counted the journal. A purge that walked every object now +// always ends in the partition delete. +for (const committedObjects of [0, 3]) { + test(`05-01 an approved purge in another tab waits for a live put and takes it with the partition (${committedObjects} committed objects)`, 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]) }; + const seeded = await secondPage.evaluate(async (count) => { + const runtime = (globalThis as unknown as { __opfsTab: Tab }).__opfsTab; + const codes: string[] = []; + for (let index = 0; index < count; index += 1) { + const put = await runtime.objects.put({ + objectId: `object_secret_${String(index).padStart(4, "0")}`, + expectedGeneration: null, + mediaType: "application/octet-stream", + source: { + byteLength: 1, + async *stream() { + yield { ok: true as const, value: new Uint8Array([index + 1]) }; + }, + }, + }); + codes.push(put.ok ? "OK" : put.error.code); + } + return codes; + }, committedObjects); + expect(seeded).toEqual(Array.from({ length: committedObjects }, () => "OK")); + + 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: { + removedObjects: committedObjects, + moreObjectsAvailable: false, + partitionDeleted: true, }, }); - 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(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(); } - 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, diff --git a/tests/recipes/browser-file-storage-contracts.test.ts b/tests/recipes/browser-file-storage-contracts.test.ts index f4e0202b..0ff59eff 100644 --- a/tests/recipes/browser-file-storage-contracts.test.ts +++ b/tests/recipes/browser-file-storage-contracts.test.ts @@ -672,12 +672,14 @@ describe("production browser file and storage recipe contracts", () => { expectedGeneration: asObjectGeneration(2), }), ).toMatchObject({ ok: false, error: { code: "CONFLICT" } }); - expect( - await store.enforcePolicies({ reason: "ACCOUNT_DELETION" }), - ).toMatchObject({ + // 1103/05-01 rest. Like the runtime, a purge that walked every object + // ends in the partition delete, never in a chunk sweep. + const purged = await store.enforcePolicies({ reason: "ACCOUNT_DELETION" }); + expect(purged).toMatchObject({ ok: true, - value: { removedObjects: 1, chunkSweep: "COMPLETED" }, + value: { removedObjects: 1, partitionDeleted: true }, }); + expect(purged.ok && purged.value.chunkSweep).toBeUndefined(); expect(await store.open({ objectId })).toMatchObject({ ok: false, error: { code: "NOT_FOUND" }, diff --git a/tests/unit/opfs-worker-runtime.test.ts b/tests/unit/opfs-worker-runtime.test.ts index e319754c..063f7c19 100644 --- a/tests/unit/opfs-worker-runtime.test.ts +++ b/tests/unit/opfs-worker-runtime.test.ts @@ -2254,15 +2254,17 @@ describe("05-04 a LOGOUT or ACCOUNT_DELETION purge leaves no partition bytes beh expect(await putBytes(purged, "object_secret_2", [4, 5])).toMatchObject({ ok: true }); expect(await putBytes(kept, "object_kept_0001", [7, 7])).toMatchObject({ ok: true }); - expect(await purged.maintenance.enforcePolicies({ reason })).toMatchObject({ + const report = await purged.maintenance.enforcePolicies({ reason }); + expect(report).toMatchObject({ ok: true, value: { removedObjects: 2, moreObjectsAvailable: false, deadlineReached: false, - chunkSweep: "COMPLETED", + partitionDeleted: true, }, }); + expect(report.ok && report.value.chunkSweep).toBeUndefined(); expect(filesUnder(root, scopeA)).toEqual([]); expect(await openedBytes(kept, "object_kept_0001")).toEqual({ generation: 1, @@ -2273,7 +2275,7 @@ describe("05-04 a LOGOUT or ACCOUNT_DELETION purge leaves no partition bytes beh // 1101/13.6. A journal may hand back a page shorter than the limit (the port // does not promise full pages). A purge that walked two such pages and - // removed everything is finished: it must say so and sweep the chunks. + // removed everything is finished: it must say so and take the partition. it("finishes a purge that walked more than one journal page", async () => { const { root, idb, tab } = sharedRootTabs(); const journal = createIndexedDbOpfsJournal({ @@ -2300,7 +2302,7 @@ describe("05-04 a LOGOUT or ACCOUNT_DELETION purge leaves no partition bytes beh inspectedObjects: 2, removedObjects: 2, moreObjectsAvailable: false, - chunkSweep: "COMPLETED", + partitionDeleted: true, }, }); expect(filesUnder(root, scopeA)).toEqual([]); @@ -2310,25 +2312,89 @@ describe("05-04 a LOGOUT or ACCOUNT_DELETION purge leaves no partition bytes beh // 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); + // 1103/05-01 rest (Y1b). That fix counted the journal once, after the walk. + // Each remove waits for the origin lease the other tab's put holds, so with + // three or more objects the put committed and settled before the count: + // neither the listing nor the count saw it, the sweep skipped its chunks + // and said COMPLETED, and the file stayed readable. One object hid this. + it.each([1, 3, 8])( + "waits for a put that began before the purge and takes it with the partition (%i committed objects)", + async (count) => { + const { root, tab } = sharedRootTabs(); + const purger = tab("p", authority); + const writer = tab("w", authority); + for (let index = 1; index <= count; index += 1) { + expect( + await putBytes(purger, `object_secret_${index}`, [index]), + ).toMatchObject({ ok: true }); + } + const stalled = stalledSource(); + const put = writer.objects.put({ + objectId: "object_late_0001", + expectedGeneration: null, + mediaType: "application/octet-stream", + source: stalled.source, + }); + await stalled.streaming; + const purge = purger.maintenance.enforcePolicies({ reason: "LOGOUT" }); + await new Promise((resolve) => setTimeout(resolve, 20)); + stalled.release(); + + expect(await put).toMatchObject({ ok: true }); + const report = await purge; + expect(report).toMatchObject({ + ok: true, + value: { + removedObjects: count, + 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"); + }, + ); + + // 1103/05-01 rest (Y1a). The same gap without timing: another tab's put of + // a new object commits and settles right after the purge lists the + // partition, so the listing misses it and no journal row is left to count. + it("takes a new object another tab's put settles between the purge's listing and its end", async () => { + const { root, idb, tab } = sharedRootTabs(); + const journal = createIndexedDbOpfsJournal({ + authorityToken: scopeA.authorityToken, + factory: idb.factory, + keyRange: idb.keyRange, + }); + const stalled = stalledSource(); + let late: ReturnType | undefined; + const purger = tab("p", { + ...authority, + journal: { + ...journal, + async listCommittedObjects(request) { + const page = await journal.listCommittedObjects(request); + stalled.release(); + await late; + return page; + }, + }, + }); const writer = tab("w", authority); expect(await putBytes(purger, "object_secret_1", [1, 2, 3])).toMatchObject({ ok: true }); - const stalled = stalledSource(); - const put = writer.objects.put({ + late = writer.objects.put({ objectId: "object_late_0001", expectedGeneration: null, mediaType: "application/octet-stream", source: stalled.source, }); await stalled.streaming; - const purge = purger.maintenance.enforcePolicies({ reason: "LOGOUT" }); - await new Promise((resolve) => setTimeout(resolve, 20)); - stalled.release(); - expect(await put).toMatchObject({ ok: true }); - const report = await purge; + const report = await purger.maintenance.enforcePolicies({ + reason: "ACCOUNT_DELETION", + }); + + expect(await late).toMatchObject({ ok: true, value: { generation: 1 } }); expect(report).toMatchObject({ ok: true, value: { @@ -2339,7 +2405,72 @@ describe("05-04 a LOGOUT or ACCOUNT_DELETION purge leaves no partition bytes beh }); expect(report.ok && report.value.chunkSweep).toBeUndefined(); expect(filesUnder(root, scopeA)).toEqual([]); - expect(await openedBytes(writer, "object_late_0001")).toBe("NOT_FOUND"); + expect(await openedBytes(purger, "object_late_0001")).toBe("NOT_FOUND"); + }); + + // 2026-11-03 05-01. A walk the budget stops part-way is no finished purge. + // It leaves the rest for the next call, unless a put or delete of the + // partition is unfinished: then that call takes the partition at once. + it("continues a purge the object budget stopped, and takes the partition when a put is unfinished", async () => { + const { root, idb, tab } = sharedRootTabs(); + const purger = tab("p", authority); + for (const objectId of ["object_secret_1", "object_secret_2", "object_secret_3"]) { + expect(await putBytes(purger, objectId, [1])).toMatchObject({ ok: true }); + } + + const first = await purger.maintenance.enforcePolicies({ + reason: "ACCOUNT_DELETION", + maxObjects: 1, + }); + expect(first).toEqual({ + ok: true, + value: { + inspectedObjects: 1, + removedObjects: 1, + releasedBytes: 1, + moreObjectsAvailable: true, + deadlineReached: false, + }, + }); + expect(filesUnder(root, scopeA)).not.toEqual([]); + + // A put that died before it wrote anything leaves a PREPARING row. + const journal = createIndexedDbOpfsJournal({ + authorityToken: scopeA.authorityToken, + factory: idb.factory, + keyRange: idb.keyRange, + }); + expect( + await journal.begin({ + transactionId: "transaction_left_0001", + mutation: "PUT", + scope: scopeA, + objectId: "object_left_0001", + expectedGeneration: null, + targetGeneration: 1, + targetByteLength: 1, + targetStoragePolicy: storagePolicy, + startedAtEpochMs: 2, + }), + ).toMatchObject({ ok: true }); + + expect( + await purger.maintenance.enforcePolicies({ + reason: "ACCOUNT_DELETION", + maxObjects: 1, + }), + ).toMatchObject({ + ok: true, + value: { + inspectedObjects: 1, + removedObjects: 1, + moreObjectsAvailable: false, + partitionDeleted: true, + }, + }); + expect(filesUnder(root, scopeA)).toEqual([]); + expect(idb.readRaw("opfs-journal", "transaction_left_0001")).toBeUndefined(); + expect(await openedBytes(purger, "object_secret_3")).toBe("NOT_FOUND"); }); // 2026-11-03 05-01 X2, X2b, X3. A put that stopped before it finished leaves @@ -2420,11 +2551,87 @@ describe("05-04 a LOGOUT or ACCOUNT_DELETION purge leaves no partition bytes beh }, ); + // 1103/05-01 rest (Y2). The journal counts rows by their partition key, so a + // stopped put whose row lost that key was never counted. Its staging + // receipt kept its chunk out of the sweep, and every purge said COMPLETED + // with the receipt, the chunk and the manifest still on disk. The partition + // delete takes them; the keyless row itself stays, counted apart (05-03). + it("leaves no bytes of a stopped put whose journal row lost its partition key", async () => { + 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 before commit: neither the commit nor its compensation + // lands, so the row stays FILES_READY with its staging on disk. + const writer = tab("w", { + journal: { ...journal, commitPut: 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, + }); + const { logicalKey: _dropped, ...keyless } = idb.readRaw( + "opfs-journal", + "transaction_w_1", + ) as Record; + idb.seed("opfs-journal", keyless); + expect(filesUnder(root, scopeA)).not.toEqual([]); + + for (let run = 0; run < 2; run += 1) { + const report = await purger.maintenance.enforcePolicies({ + reason: "ACCOUNT_DELETION", + }); + expect(report).toMatchObject({ + ok: true, + value: { moreObjectsAvailable: false, partitionDeleted: true }, + }); + expect(report.ok && report.value.chunkSweep).toBeUndefined(); + expect(filesUnder(root, scopeA)).toEqual([]); + expect(await purger.maintenance.reconcile()).toMatchObject({ + ok: true, + value: { + committedTransactions: 0, + corruptTransactions: 1, + unattributedCorruptTransactions: 1, + }, + }); + } + expect(await openedBytes(purger, "object_dead_0001")).toBe("NOT_FOUND"); + expect(idb.readRaw("opfs-journal", "transaction_w_1")).toBeDefined(); + }); + // [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. 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). + // not claim to be. 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). The next call walks + // every object and takes the partition (1103/05-01 rest). it("reports a purge that skipped a changing object as unfinished", async () => { const { root, idb, tab } = sharedRootTabs(); const journal = createIndexedDbOpfsJournal({ @@ -2472,7 +2679,7 @@ describe("05-04 a LOGOUT or ACCOUNT_DELETION purge leaves no partition bytes beh value: { removedObjects: 1, moreObjectsAvailable: false, - chunkSweep: "COMPLETED", + partitionDeleted: true, }, }); expect(filesUnder(root, scopeA)).toEqual([]); From f738348d9dece1e8194a00b4b3bfbcaf85c0afe8 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 5 Nov 2026 22:14:54 +0900 Subject: [PATCH 2/8] =?UTF-8?q?fix(bootstrap):=20=EC=8B=A4=ED=8C=A8?= =?UTF-8?q?=ED=95=9C=20scope=20closer=EB=A5=BC=20label=EA=B3=BC=20?= =?UTF-8?q?=ED=95=A8=EA=BB=98=20=EC=A7=84=EB=8B=A8=EC=97=90=20=EB=82=A8?= =?UTF-8?q?=EA=B8=B4=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: 1103/02-01이 제품 closer를 scope reset에 넣었다. template participant 하나가 제품 closer를 차례로 부르고, 하나라도 던지면 이유 없는 Error를 던진다. scope runtime은 그 실패를 `failed = true`로만 받아 scope를 `FAILED`에 둔다. 그동안 모든 서버 요청이 `SCOPE_FENCED`로 막히는데 진단 기록은 0건이다(리뷰 실험 E-3). 계약의 `ProductScopeParticipant.label`을 읽는 코드도 없다. 개발자는 "로그아웃 뒤 앱이 요청을 안 보낸다"만 보고 원인 closer를 찾으려면 closer를 하나씩 지워 봐야 한다. 고른 방법(리뷰 대안 (다)): - scope runtime이 reset의 실패 단계마다 `cache.operation.failed`를 하나 남긴다. `operation: "SCOPE_RESET"`, `reason`은 `PARTICIPANT_FAILED`· `CACHE_RESET_FAILED`·`GENERATION_ACTIVATION_FAILED`(+ 목록 함수가 던질 때 `PARTICIPANTS_UNREADABLE`), `error_kind`, participant 실패면 `scope_participant: