fix(storage): sweep a purged OPFS partition's chunks without the GC grace (05-04)

A LOGOUT or ACCOUNT_DELETION purge removed object directories only; chunk
bytes stayed until a GC 24 h later that might never run for that user.
Once every object is gone the purge now sweeps the partition's unreferenced
chunks at once and reports how the sweep ended. Chunks the journal or a
staged put still references are kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-25 22:29:07 +09:00
co-authored by Claude Opus 5.5
parent 3e37fbf0ed
commit 6ab564f376
4 changed files with 148 additions and 7 deletions
@@ -394,6 +394,12 @@ 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를
grace 없이 sweep한다(05-04, `opfs-byte-store-adapter.ts` `enforcePolicies`).
journal이나 staging receipt가 참조하는 chunk는 이때도 지우지 않는다. report의
`chunkSweep`이 `COMPLETED`여야 bytes가 지워진 것이다. `DEADLINE_REACHED`면 같은
purge를 다시 호출하고, `STAGING_STATE_UNREADABLE`이면 읽을 수 없는 staging부터
처리한다
- lock/worker 문제면 OPFS write off → approved capped fallback 또는 online-only
- scope binding mismatch면 affected scope를 read-only로 격리하고 registry/
composition을 forward fix; 다른 token path로 bytes를 이동하거나 추측 복구 금지
@@ -846,12 +846,38 @@ export function createOpfsByteStoreAdapter(
afterObjectId = page.value.nextObjectId;
moreObjectsAvailable = page.value.moreAvailable;
}
// 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 (
(request.reason === "LOGOUT" ||
request.reason === "ACCOUNT_DELETION") &&
!moreObjectsAvailable &&
!deadlineReached &&
!request.signal?.aborted
) {
const swept = await reconcileOrphanChunks(
deadline,
request.signal,
now(),
);
if (!swept.ok) return rebaseFailure(swept.error, "OBJECT_RECONCILE");
chunkSweep = swept.value.status;
if (chunkSweep === "DEADLINE_REACHED") {
deadlineReached = true;
moreObjectsAvailable = true;
}
}
const report: OpfsPolicyMaintenanceReport = Object.freeze({
inspectedObjects,
removedObjects,
releasedBytes,
moreObjectsAvailable,
deadlineReached,
...(chunkSweep ? { chunkSweep } : {}),
});
return browserDataSuccess(report);
},
@@ -1188,6 +1214,7 @@ export function createOpfsByteStoreAdapter(
async function reconcileOrphanChunks(
deadline: number,
signal: AbortSignal | undefined,
olderThanEpochMs = Math.max(0, now() - policy.orphanGracePeriodMs),
): Promise<
BrowserDataResult<
Readonly<{
@@ -1200,10 +1227,6 @@ 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
@@ -119,6 +119,12 @@ export type OpfsPolicyMaintenanceReport = Readonly<{
releasedBytes: number;
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`.
*/
chunkSweep?: "COMPLETED" | "DEADLINE_REACHED" | "STAGING_STATE_UNREADABLE";
}>;
export type OpfsSensitiveMaintenanceReason =
+109 -3
View File
@@ -2094,7 +2094,11 @@ function sharedRootTabs(policy = runtimePolicy) {
const root = new MemoryDirectory();
const leases = exclusiveLeases();
const idb = new MemoryIndexedDbFactory();
const tab = (tag: string) => {
const tab = (
tag: string,
extra: Partial<Parameters<typeof createOpfsByteStoreAdapter>[0]> = {},
) => {
const tabScope = extra.scope ?? scopeA;
const runtime = createOpfsWorkerRuntime({
root: root as unknown as FileSystemDirectoryHandle,
crypto: globalThis.crypto,
@@ -2106,15 +2110,16 @@ function sharedRootTabs(policy = runtimePolicy) {
let sequence = 0;
return createOpfsByteStoreAdapter({
journal: createIndexedDbOpfsJournal({
authorityToken: scopeA.authorityToken,
authorityToken: tabScope.authorityToken,
factory: idb.factory,
keyRange: idb.keyRange,
}),
worker: loopbackGateway(runtime, policy),
scope: scopeA,
scope: tabScope,
storagePolicy,
policy,
createTransactionId: () => `transaction_${tag}_${++sequence}`,
...extra,
});
};
return { root, leases, idb, tab };
@@ -2292,3 +2297,104 @@ describe("05-02 an empty receipt left by a failed first write is no receipt", ()
expect(await put()).toMatchObject({ ok: true, value: { generation: 1 } });
});
});
describe("05-04 a LOGOUT or ACCOUNT_DELETION purge leaves no partition bytes behind", () => {
const scopeP2: OpfsStorageScope = Object.freeze({
...scopeA,
partitionToken: "partition_87654321",
});
const authority = {
requestMaintenanceAuthority: () => ({
authorized: true as const,
proofToken: "proof_token_1234567890",
expiresAtEpochMs: Date.now() + 60_000,
}),
consumeMaintenanceAuthority: () => true,
};
function filesUnder(root: MemoryDirectory, scope: OpfsStorageScope): string[] {
const files: string[] = [];
const walk = (directory: MemoryDirectory, path: string) => {
for (const [name, node] of directory.children) {
if (node instanceof MemoryDirectory) walk(node, `${path}/${name}`);
else files.push(`${path}/${name}`);
}
};
const partition = [
"authorities",
scope.authorityToken,
scope.namespaceToken,
scope.partitionToken,
].reduce<MemoryNode | undefined>(
(node, segment) =>
node instanceof MemoryDirectory ? node.children.get(segment) : node,
root,
);
if (partition instanceof MemoryDirectory) walk(partition, "");
return files;
}
const putBytes = (
adapter: ReturnType<typeof createOpfsByteStoreAdapter>,
objectId: string,
bytes: number[],
) =>
adapter.objects.put({
objectId,
expectedGeneration: null,
mediaType: "application/octet-stream",
source: bytesSource(new Uint8Array(bytes)),
});
it.each(["LOGOUT", "ACCOUNT_DELETION"] as const)(
"removes the chunks of a %s-purged partition and keeps other partitions",
async (reason) => {
const { root, tab } = sharedRootTabs();
const purged = tab("p", authority);
const kept = tab("k", { ...authority, scope: scopeP2 });
expect(await putBytes(purged, "object_secret_1", [1, 2, 3])).toMatchObject({ ok: true });
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({
ok: true,
value: {
removedObjects: 2,
moreObjectsAvailable: false,
deadlineReached: false,
chunkSweep: "COMPLETED",
},
});
expect(filesUnder(root, scopeA)).toEqual([]);
expect(await openedBytes(kept, "object_kept_0001")).toEqual({
generation: 1,
bytes: [7, 7],
});
},
);
it("never sweeps the chunk of a put that runs while the purge does", async () => {
const { 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 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 });
expect(await purge).toMatchObject({ ok: true, value: { removedObjects: 1 } });
expect(await openedBytes(writer, "object_late_0001")).toEqual({
generation: 1,
bytes: [5],
});
});
});