From 68e88b081dcc27dd1bc9087cb4d84e1e9c4edaeb Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Sun, 1 Nov 2026 22:20:56 +0900 Subject: [PATCH] =?UTF-8?q?fix(opfs):=20=EA=B0=9D=EC=B2=B4=EB=A5=BC=20?= =?UTF-8?q?=EB=AA=BB=20=EC=A7=80=EC=9A=B4=20purge=EA=B0=80=20=EC=99=84?= =?UTF-8?q?=EB=A3=8C=EC=99=80=20chunk=20sweep=EC=9D=84=20=EB=B3=B4?= =?UTF-8?q?=EA=B3=A0=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8A=94=EB=8B=A4=20(09?= =?UTF-8?q?29/05-02)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: OPFS purge는 remove가 CONFLICT·NOT_FOUND면 그 객체를 건너뛴다. chunk sweep 조건은 "남은 객체가 없다"가 아니라 !moreObjectsAvailable && !deadlineReached && !aborted라서, 다른 탭이 같은 객체를 덮어쓰는 동안 ACCOUNT_DELETION purge를 부르면 {removedObjects: 0, moreObjectsAvailable: false, chunkSweep: "COMPLETED"}를 돌려준다. 객체와 chunk는 남아 있는데 보고는 완료로 읽힌다. port 주석은 sweep이 "once every object is removed"라고 약속하고 runbook도 COMPLETED를 완료 조건으로 적는다. 고른 방법: 지난 리뷰의 첫 방향. 건너뛸 때 moreObjectsAvailable을 true로 두어 report가 미완료를 드러내고 sweep 조건이 sweep을 막게 한다 (chunkSweep 칸이 빠진다). port 계약은 바꾸지 않고 코드를 기존 주석에 맞춘다. 이 값을 뒤에서 false로 되돌리는 대입이 없다는 근거(다음 페이지로 넘어가는 대입은 그 페이지에 객체가 더 있을 때만 실행되고, PRESSURE는 sweep하지 않는다)를 코드 주석에 적었다. port의 moreObjectsAvailable 주석과 runbook에 "다시 호출한다"를 더했다. 버린 대안: - skippedObjects 칸을 report에 더하고 완료 조건을 "skippedObjects === 0 그리고 COMPLETED"로 고친다: port 추가와 문서 조건 변경이 필요하고 번들도 더 커진다. offline-indexeddb 예산 여유가 9 B뿐이다. - 따로 skipped 플래그를 두고 끝에서 합친다: 같은 동작에 바이트가 더 든다. 위험·한계: - offline-indexeddb 레시피 번들 36,891 -> 36,896 / 36,900 gzip B. 예산은 올리지 않았지만 여유가 4 B로 줄었다([0929] 05-04). - 별도로 발견했고 고치지 않은 것: 여러 페이지에 걸친 purge는 마지막 페이지의 OR 대입이 앞 페이지에서 넘어올 때 넣은 true를 유지해, 다 지워도 moreObjectsAvailable true를 보고하고 sweep하지 않는다. 보수적 방향이라 다시 부르면 끝나지만 한 번에 끝나지 않는다. 코드로만 확인했고 실행하지 않았다. 동작 변경이라 범위 밖. - 실제 브라우저(Playwright OPFS spec)에서는 돌리지 않았다. 검증: - RED: opfs-worker-runtime "reports a purge that skipped a changing object as unfinished"가 `- "moreObjectsAvailable": true` `+ "moreObjectsAvailable": false`로 실패(removedObjects 0, 6번째 칸 chunkSweep 존재). - GREEN: 같은 파일 64/64 통과. 두 번째 purge가 removedObjects 1, chunkSweep COMPLETED, partition 파일 0개로 끝난다. - 회귀: opfs-worker-runtime, opfs-byte-store, indexeddb-opfs-journal, indexeddb-local-draft 4파일 104/104 통과. tsc app·test·web-worker· recipes 통과, eslint 통과, verify-documentation-readiness PASS_SCOPED, check-optional-recipes PASS. Refs: docs/reviews/2026-09-29-maturity-review/05-adapters-storage-kernel.md --- .../browser-file-storage-recovery.md | 4 +- .../storage/opfs/opfs-byte-store-adapter.ts | 5 +++ .../ports/browser-file-storage/opfs-ports.ts | 5 +++ tests/unit/opfs-worker-runtime.test.ts | 41 +++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/docs/operations/browser-file-storage-recovery.md b/docs/operations/browser-file-storage-recovery.md index 81765b77..d7ace2c2 100644 --- a/docs/operations/browser-file-storage-recovery.md +++ b/docs/operations/browser-file-storage-recovery.md @@ -433,7 +433,9 @@ VD-15 목표 runtime을 먼저 구현한다. journal이나 staging receipt가 참조하는 chunk는 이때도 지우지 않는다. report의 `chunkSweep`이 `COMPLETED`여야 bytes가 지워진 것이다. `DEADLINE_REACHED`면 같은 purge를 다시 호출하고, `STAGING_STATE_UNREADABLE`이면 읽을 수 없는 staging부터 - 처리한다 + 처리한다. 다른 탭이 바꾸고 있던 객체를 지우지 못하면 report는 + `moreObjectsAvailable: true`이고 `chunkSweep`이 없다. 이때도 같은 purge를 다시 + 호출한다([0929] 05-02) - 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/opfs-byte-store-adapter.ts b/src/adapters/storage/opfs/opfs-byte-store-adapter.ts index 2390325a..1f590f52 100644 --- a/src/adapters/storage/opfs/opfs-byte-store-adapter.ts +++ b/src/adapters/storage/opfs/opfs-byte-store-adapter.ts @@ -819,6 +819,11 @@ export function createOpfsByteStoreAdapter( }); if (!removed.ok) { if (removed.error.code === "CONFLICT" || removed.error.code === "NOT_FOUND") { + // [0929] 05-02. Another context changed this object, so it may + // still be here: report more work and skip the chunk sweep. No + // later assignment clears this — the page-advance one below only + // runs when the page had more objects, and PRESSURE never sweeps. + moreObjectsAvailable = true; continue; } return rebaseFailure(removed.error, "OBJECT_RECONCILE"); diff --git a/src/application/ports/browser-file-storage/opfs-ports.ts b/src/application/ports/browser-file-storage/opfs-ports.ts index b0971694..5893d8a0 100644 --- a/src/application/ports/browser-file-storage/opfs-ports.ts +++ b/src/application/ports/browser-file-storage/opfs-ports.ts @@ -131,6 +131,11 @@ export type OpfsPolicyMaintenanceReport = Readonly<{ inspectedObjects: number; removedObjects: number; releasedBytes: number; + /** + * Objects may remain. It is also `true` when another context changed an + * object this run had to remove, which left that object in place + * ([0929] 05-02). + */ moreObjectsAvailable: boolean; deadlineReached: boolean; /** diff --git a/tests/unit/opfs-worker-runtime.test.ts b/tests/unit/opfs-worker-runtime.test.ts index 3899f8e3..8aba8732 100644 --- a/tests/unit/opfs-worker-runtime.test.ts +++ b/tests/unit/opfs-worker-runtime.test.ts @@ -2397,6 +2397,47 @@ describe("05-04 a LOGOUT or ACCOUNT_DELETION purge leaves no partition bytes beh bytes: [5], }); }); + + // [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. + 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 stalled = stalledSource(); + const overwrite = writer.objects.put({ + objectId: "object_secret_1", + expectedGeneration: 1, + mediaType: "application/octet-stream", + source: stalled.source, + }); + 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; + expect(first).toMatchObject({ + ok: true, + value: { inspectedObjects: 1, removedObjects: 0, moreObjectsAvailable: true }, + }); + expect(first.ok && first.value.chunkSweep).toBeUndefined(); + + expect( + await purger.maintenance.enforcePolicies({ reason: "ACCOUNT_DELETION" }), + ).toMatchObject({ + ok: true, + value: { + removedObjects: 1, + moreObjectsAvailable: false, + chunkSweep: "COMPLETED", + }, + }); + expect(filesUnder(root, scopeA)).toEqual([]); + }); }); describe("05-07 a put whose source stops lets go of the origin lock", () => {