diff --git a/src/adapters/storage/opfs/opfs-worker-runtime.ts b/src/adapters/storage/opfs/opfs-worker-runtime.ts index 99c4accf..6a5e9a9f 100644 --- a/src/adapters/storage/opfs/opfs-worker-runtime.ts +++ b/src/adapters/storage/opfs/opfs-worker-runtime.ts @@ -1219,6 +1219,21 @@ export function createOpfsWorkerRuntime( ); await directory.removeEntry(fileName); } else { + // R-5.2. A dedup hit must restart the chunk's grace period. Orphan GC + // reads "not referenced" from the page journal and deletes in the + // worker, so another tab's GC can hold a stale answer for a chunk that + // this put is about to reference once staging is gone. An atomic + // rewrite of the identical bytes refreshes lastModified, and readers of + // the committed chunk see either copy, never a partial file. + const { directory, fileName } = await resolveFileParent( + dependencies.root, + path, + false, + ); + await writeWithAtomicWritable( + await directory.getFileHandle(fileName), + bytes, + ); return; } } catch (error) { diff --git a/tests/unit/opfs-worker-runtime.test.ts b/tests/unit/opfs-worker-runtime.test.ts index d3fb7609..0cc7cedd 100644 --- a/tests/unit/opfs-worker-runtime.test.ts +++ b/tests/unit/opfs-worker-runtime.test.ts @@ -697,6 +697,128 @@ describe("R-5.1 orphan GC pages past referenced chunks", () => { }); }); +function memoryFileAt(root: MemoryDirectory, path: readonly string[]): MemoryFile { + let current: MemoryNode = root; + for (const segment of path) { + if (!(current instanceof MemoryDirectory)) break; + current = current.children.get(segment)!; + } + if (!(current instanceof MemoryFile)) throw new Error("No such file."); + return current; +} + +describe("R-5.2 a dedup-reused chunk is protected by the grace period again", () => { + it("refreshes the chunk mtime on reuse so orphan GC neither lists nor deletes it", async () => { + const root = new MemoryDirectory(); + const runtime = createOpfsWorkerRuntime({ + root: root as unknown as FileSystemDirectoryHandle, + crypto: globalThis.crypto, + policy: runtimePolicy, + leaseManager: immediateLeases({ count: 0 }), + dedicatedWorker: true, + supportsSynchronousAccessHandles: false, + }); + async function putAndFinalize( + transactionId: string, + physicalGenerationId: OpfsPhysicalGenerationId, + ): Promise { + await runtime.handleRequest( + beginRequest( + `request_begin_${transactionId}`, + transactionId, + scopeA, + physicalGenerationId, + ), + ); + await runtime.handleRequest({ + requestId: `request_append_${transactionId}`, + protocolVersion: OPFS_WORKER_PROTOCOL_VERSION, + kind: "APPEND_CHUNK", + scope: scopeA, + transactionId, + sequence: 0, + bytes: new Uint8Array([77]).buffer, + }); + const prepared = preparedValue( + await runtime.handleRequest({ + requestId: `request_finish_${transactionId}`, + protocolVersion: OPFS_WORKER_PROTOCOL_VERSION, + kind: "FINISH_PUT", + scope: scopeA, + transactionId, + }), + ); + expect( + await runtime.handleRequest({ + requestId: `request_finalize_${transactionId}`, + protocolVersion: OPFS_WORKER_PROTOCOL_VERSION, + kind: "FINALIZE_PUT", + transactionId, + preparedObject: prepared, + }), + ).toMatchObject({ ok: true }); + return prepared; + } + + const first = await putAndFinalize( + "transaction_dedup_0001", + PHYSICAL_GENERATION_A, + ); + const digestHex = first.chunks[0]!.digestHex; + // The chunk is now older than any grace period. + memoryFileAt(root, chunkFilePath(scopeA, digestHex)).lastModified = 0; + const cutoff = Date.now() - runtimePolicy.orphanGracePeriodMs; + expect( + await runtime.handleRequest({ + requestId: "request_dedup_list_before", + protocolVersion: OPFS_WORKER_PROTOCOL_VERSION, + kind: "LIST_ORPHAN_CANDIDATES", + scope: scopeA, + olderThanEpochMs: cutoff, + maxEntries: 10, + }), + ).toMatchObject({ ok: true, value: { digests: [digestHex] } }); + + // A second PUT of the same bytes reuses the chunk and then leaves staging, + // so from here on only the chunk mtime protects it from a GC that read + // "not referenced" just before the reuse committed. + const second = await putAndFinalize( + "transaction_dedup_0002", + "b".repeat(32) as OpfsPhysicalGenerationId, + ); + expect(second.chunks[0]!.digestHex).toBe(digestHex); + + expect( + await runtime.handleRequest({ + requestId: "request_dedup_list_after", + protocolVersion: OPFS_WORKER_PROTOCOL_VERSION, + kind: "LIST_ORPHAN_CANDIDATES", + scope: scopeA, + olderThanEpochMs: cutoff, + maxEntries: 10, + }), + ).toMatchObject({ ok: true, value: { digests: [] } }); + expect( + await runtime.handleRequest({ + requestId: "request_dedup_delete", + protocolVersion: OPFS_WORKER_PROTOCOL_VERSION, + kind: "DELETE_ORPHAN_CHUNK", + scope: scopeA, + digestHex, + olderThanEpochMs: cutoff, + }), + ).toMatchObject({ ok: true, value: { deleted: false } }); + expect( + await runtime.handleRequest({ + requestId: "request_dedup_verify", + protocolVersion: OPFS_WORKER_PROTOCOL_VERSION, + kind: "VERIFY_OBJECT", + preparedObject: second, + }), + ).toMatchObject({ ok: true, value: true }); + }); +}); + describe("OPFS worker client lifecycle", () => { it("rejects a colliding concurrent request id without replacing the first RPC", async () => { let listener: ((event: MessageEvent) => void) | undefined;