fix(storage): finish partition purge past corrupt rows (R-5.4)
SESSION_END, LOGOUT and ACCOUNT_DELETION purges validated every row they were about to delete and checked the dataset budget counters on each step. One unreadable record, a record without a valid sidecar, a bad receipt or a budget counter off by one aborted the batch with CORRUPT_DATA, and the next batch restarted and hit the same row, so the partition's personal data never left the device. The partition purge now deletes by key without making row validity a precondition. Rows that fail validation are counted and reported as one CORRUPT_DATA observation per batch through the existing observe channel. Per-row budget arithmetic is removed; the transaction that finds every store empty resets the budget row to zero, which also heals a drifted counter. Between batches the budget can only read high (blocks writes, never admits). The purge still opens only the partition's own database, whose name is derived from the partition token. RETENTION_SWEEP is unchanged and still fails closed on corrupt rows. Verified: new test "R-5.4 completes a LOGOUT/ACCOUNT_DELETION purge past corrupt rows" seeds a corrupt record, an invalid sidecar, an invalid orphan sidecar, an invalid receipt and a drifted budget. Before the fix the first batch failed CORRUPT_DATA; now the purge reaches COMPLETE, all rows are gone, the budget is zero, a CORRUPT_DATA observation is emitted, and a second partition routed through the same factory keeps its row. Gates: check:architecture, check:types:app/test/web-worker, lint, test:component pass; test:unit passes except removal-fixture.test.ts, which needs gitignored release artifacts absent from a fresh worktree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
68f4a93a19
commit
46af07b4fe
@@ -2366,11 +2366,21 @@ export function createIndexedDbRuntime<Value, WireValue, Query>(
|
||||
);
|
||||
}
|
||||
|
||||
function purgePartitionRecords(
|
||||
/**
|
||||
* R-5.4. A partition purge (session end, logout, account deletion) exists to
|
||||
* remove everything, so it deletes by key and never stops on a row it cannot
|
||||
* read: failing closed here left personal data on the device for good. Rows
|
||||
* that fail validation are counted and reported as one CORRUPT_DATA
|
||||
* observation per batch. Per-row budget arithmetic is dropped too; the
|
||||
* budget is reset to zero in the transaction that finds every store empty.
|
||||
* Between batches it can only read high, which blocks writes, never admits.
|
||||
*/
|
||||
async function purgePartitionRecords(
|
||||
input: IndexedDbLifecycleBatchInput,
|
||||
deadline: number,
|
||||
): Promise<BrowserDataResult<IndexedDbLifecycleBatchReceipt>> {
|
||||
return runTransaction(
|
||||
let corruptRows = 0;
|
||||
const result = await runTransaction<IndexedDbLifecycleBatchReceipt>(
|
||||
[
|
||||
dependencies.recordStore,
|
||||
dependencies.governanceStore,
|
||||
@@ -2486,47 +2496,30 @@ export function createIndexedDbRuntime<Value, WireValue, Query>(
|
||||
walkPurge(
|
||||
receipts.openCursor(),
|
||||
({ cursor, resume }) => {
|
||||
if (!isStoredReceipt(cursor.value)) {
|
||||
context.fail(corruptFailure("INDEXEDDB_WRITE"));
|
||||
// `fail` aborts, so the pump has nowhere to go.
|
||||
return { kind: "SUSPEND" };
|
||||
}
|
||||
scannedRows += 1;
|
||||
if (!isStoredReceipt(cursor.value)) corruptRows += 1;
|
||||
const deletion = receipts.delete(cursor.primaryKey);
|
||||
deletion.onerror = () =>
|
||||
context.requestFailed(deletion.error);
|
||||
deletion.onsuccess = () => {
|
||||
const budgetRequest = governance.get(
|
||||
INDEXEDDB_DATASET_BUDGET_KEY,
|
||||
);
|
||||
budgetRequest.onerror = () =>
|
||||
context.requestFailed(budgetRequest.error);
|
||||
budgetRequest.onsuccess = () => {
|
||||
if (
|
||||
!isStoredDatasetBudget(budgetRequest.result) ||
|
||||
budgetRequest.result.receiptCount < 1
|
||||
) {
|
||||
context.fail(corruptFailure("INDEXEDDB_WRITE"));
|
||||
return;
|
||||
}
|
||||
const storedBudgetRequest = governance.put({
|
||||
...budgetRequest.result,
|
||||
receiptCount:
|
||||
budgetRequest.result.receiptCount - 1,
|
||||
} satisfies StoredDatasetBudget);
|
||||
storedBudgetRequest.onerror = () =>
|
||||
context.requestFailed(
|
||||
storedBudgetRequest.error,
|
||||
);
|
||||
storedBudgetRequest.onsuccess = () => {
|
||||
deletedRows += 1;
|
||||
resume({ kind: "CONTINUE" });
|
||||
};
|
||||
};
|
||||
deletedRows += 1;
|
||||
resume({ kind: "CONTINUE" });
|
||||
};
|
||||
return { kind: "SUSPEND" };
|
||||
},
|
||||
() => succeed("COMPLETE", false),
|
||||
() => {
|
||||
// Every store above is empty in this same transaction, so zero
|
||||
// is exact, and a budget row that had drifted is healed.
|
||||
const budgetReset = governance.put({
|
||||
bindingKey: INDEXEDDB_DATASET_BUDGET_KEY,
|
||||
budgetVersion: 1,
|
||||
usedBytes: 0,
|
||||
receiptCount: 0,
|
||||
} satisfies StoredDatasetBudget);
|
||||
budgetReset.onerror = () =>
|
||||
context.requestFailed(budgetReset.error);
|
||||
budgetReset.onsuccess = () => succeed("COMPLETE", false);
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
@@ -2534,46 +2527,21 @@ export function createIndexedDbRuntime<Value, WireValue, Query>(
|
||||
walkPurge(
|
||||
retention.openCursor(),
|
||||
({ cursor, resume }) => {
|
||||
scannedRows += 1;
|
||||
if (
|
||||
!isStoredRetentionRecord(
|
||||
cursor.value,
|
||||
String(cursor.primaryKey),
|
||||
)
|
||||
) {
|
||||
context.fail(corruptFailure("INDEXEDDB_WRITE"));
|
||||
return { kind: "SUSPEND" };
|
||||
corruptRows += 1;
|
||||
}
|
||||
scannedRows += 1;
|
||||
const measuredBytes = cursor.value.measuredBytes;
|
||||
const budgetRequest = governance.get(
|
||||
INDEXEDDB_DATASET_BUDGET_KEY,
|
||||
);
|
||||
budgetRequest.onerror = () =>
|
||||
context.requestFailed(budgetRequest.error);
|
||||
budgetRequest.onsuccess = () => {
|
||||
if (
|
||||
!isStoredDatasetBudget(budgetRequest.result) ||
|
||||
budgetRequest.result.usedBytes < measuredBytes
|
||||
) {
|
||||
context.fail(corruptFailure("INDEXEDDB_WRITE"));
|
||||
return;
|
||||
}
|
||||
const deletion = retention.delete(cursor.primaryKey);
|
||||
deletion.onerror = () =>
|
||||
context.requestFailed(deletion.error);
|
||||
deletion.onsuccess = () => {
|
||||
const budgetWrite = governance.put({
|
||||
...budgetRequest.result,
|
||||
usedBytes:
|
||||
budgetRequest.result.usedBytes - measuredBytes,
|
||||
} satisfies StoredDatasetBudget);
|
||||
budgetWrite.onerror = () =>
|
||||
context.requestFailed(budgetWrite.error);
|
||||
budgetWrite.onsuccess = () => {
|
||||
deletedRows += 1;
|
||||
resume({ kind: "CONTINUE" });
|
||||
};
|
||||
};
|
||||
const deletion = retention.delete(cursor.primaryKey);
|
||||
deletion.onerror = () =>
|
||||
context.requestFailed(deletion.error);
|
||||
deletion.onsuccess = () => {
|
||||
deletedRows += 1;
|
||||
resume({ kind: "CONTINUE" });
|
||||
};
|
||||
return { kind: "SUSPEND" };
|
||||
},
|
||||
@@ -2584,66 +2552,32 @@ export function createIndexedDbRuntime<Value, WireValue, Query>(
|
||||
walkPurge(
|
||||
records.openCursor(),
|
||||
({ cursor, resume }) => {
|
||||
if (!isStoredRecord(cursor.value)) {
|
||||
context.fail(corruptFailure("INDEXEDDB_WRITE"));
|
||||
return { kind: "SUSPEND" };
|
||||
}
|
||||
scannedRows += 1;
|
||||
const key = cursor.value.key;
|
||||
const key = cursor.primaryKey;
|
||||
const recordCorrupt = !isStoredRecord(cursor.value);
|
||||
const sidecarRequest = retention.get(key);
|
||||
sidecarRequest.onerror = () =>
|
||||
context.requestFailed(sidecarRequest.error);
|
||||
sidecarRequest.onsuccess = () => {
|
||||
if (
|
||||
recordCorrupt ||
|
||||
!isStoredRetentionRecord(
|
||||
sidecarRequest.result,
|
||||
key,
|
||||
String(key),
|
||||
)
|
||||
) {
|
||||
context.fail(corruptFailure("INDEXEDDB_WRITE"));
|
||||
return;
|
||||
corruptRows += 1;
|
||||
}
|
||||
const measuredBytes =
|
||||
sidecarRequest.result.measuredBytes;
|
||||
const budgetRequest = governance.get(
|
||||
INDEXEDDB_DATASET_BUDGET_KEY,
|
||||
);
|
||||
budgetRequest.onerror = () =>
|
||||
context.requestFailed(budgetRequest.error);
|
||||
budgetRequest.onsuccess = () => {
|
||||
if (
|
||||
!isStoredDatasetBudget(budgetRequest.result) ||
|
||||
budgetRequest.result.usedBytes < measuredBytes
|
||||
) {
|
||||
context.fail(corruptFailure("INDEXEDDB_WRITE"));
|
||||
return;
|
||||
}
|
||||
const recordDelete = records.delete(key);
|
||||
recordDelete.onerror = () =>
|
||||
context.requestFailed(recordDelete.error);
|
||||
recordDelete.onsuccess = () => {
|
||||
const sidecarDelete = retention.delete(key);
|
||||
sidecarDelete.onerror = () =>
|
||||
context.requestFailed(sidecarDelete.error);
|
||||
sidecarDelete.onsuccess = () => {
|
||||
const storedBudgetRequest = governance.put({
|
||||
bindingKey: INDEXEDDB_DATASET_BUDGET_KEY,
|
||||
budgetVersion: 1,
|
||||
usedBytes:
|
||||
budgetRequest.result.usedBytes -
|
||||
measuredBytes,
|
||||
receiptCount:
|
||||
budgetRequest.result.receiptCount,
|
||||
} satisfies StoredDatasetBudget);
|
||||
storedBudgetRequest.onerror = () =>
|
||||
context.requestFailed(
|
||||
storedBudgetRequest.error,
|
||||
);
|
||||
storedBudgetRequest.onsuccess = () => {
|
||||
deletedRows += 1;
|
||||
resume({ kind: "CONTINUE" });
|
||||
};
|
||||
};
|
||||
const recordDelete = records.delete(key);
|
||||
recordDelete.onerror = () =>
|
||||
context.requestFailed(recordDelete.error);
|
||||
recordDelete.onsuccess = () => {
|
||||
const sidecarDelete = retention.delete(key);
|
||||
sidecarDelete.onerror = () =>
|
||||
context.requestFailed(sidecarDelete.error);
|
||||
sidecarDelete.onsuccess = () => {
|
||||
deletedRows += 1;
|
||||
resume({ kind: "CONTINUE" });
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -2653,6 +2587,16 @@ export function createIndexedDbRuntime<Value, WireValue, Query>(
|
||||
);
|
||||
},
|
||||
);
|
||||
if (corruptRows > 0) {
|
||||
observe({
|
||||
operation: "INDEXEDDB_WRITE",
|
||||
outcome: "FAILED",
|
||||
schemaVersion: dependencies.schemaVersion,
|
||||
countBucket: countBucket(corruptRows),
|
||||
failureCode: "CORRUPT_DATA",
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async function enforceLifecycleBatch(
|
||||
|
||||
@@ -1240,6 +1240,127 @@ describe("IndexedDB runtime", () => {
|
||||
).toMatchObject({ usedBytes: 0, receiptCount: 0 });
|
||||
});
|
||||
|
||||
it.each(["LOGOUT", "ACCOUNT_DELETION"] as const)(
|
||||
"R-5.4 completes a %s purge past corrupt rows, reports them, and leaves other partitions alone",
|
||||
async (action) => {
|
||||
const partitionScope = {
|
||||
...TEST_SCOPE,
|
||||
accountScope: "OPAQUE_PARTITION" as const,
|
||||
};
|
||||
const otherScope = {
|
||||
...partitionScope,
|
||||
partitionToken: "partitiontoken_002",
|
||||
};
|
||||
const partitionPolicy = {
|
||||
...TEST_STORAGE_POLICY,
|
||||
accountScope: "OPAQUE_PARTITION" as const,
|
||||
logoutAction: "PURGE_PARTITION" as const,
|
||||
accountDeletionAction: "PURGE_PARTITION" as const,
|
||||
};
|
||||
// One factory, routed by the physical database name the runtime derives
|
||||
// from its partition, the way the browser keeps databases apart.
|
||||
const purged = new MemoryIndexedDbFactory();
|
||||
const other = new MemoryIndexedDbFactory();
|
||||
const otherName = indexedDbPhysicalDatabaseName(otherScope);
|
||||
const factory = {
|
||||
open: (name: string, version?: number) =>
|
||||
(name === otherName ? other : purged).factory.open(name, version),
|
||||
cmp: purged.factory.cmp,
|
||||
} as IDBFactory;
|
||||
const observations: IndexedDbObservation[] = [];
|
||||
const runtime = createIndexedDbRuntime(
|
||||
dependencies(purged, {
|
||||
scope: partitionScope,
|
||||
storagePolicy: partitionPolicy,
|
||||
factory,
|
||||
nowMonotonicMilliseconds: () => 0,
|
||||
observe: (event) => observations.push(event),
|
||||
}),
|
||||
);
|
||||
const otherRuntime = createIndexedDbRuntime(
|
||||
dependencies(other, {
|
||||
scope: otherScope,
|
||||
storagePolicy: partitionPolicy,
|
||||
factory,
|
||||
nowMonotonicMilliseconds: () => 0,
|
||||
}),
|
||||
);
|
||||
expect(await runtime.open()).toMatchObject({ ok: true });
|
||||
expect(await otherRuntime.open()).toMatchObject({ ok: true });
|
||||
for (const key of ["healthy", "bad-record", "bad-sidecar"]) {
|
||||
expect(
|
||||
await runtime.compareAndSwap({
|
||||
key,
|
||||
value: { label: key, rank: 1 },
|
||||
expectedRevision: null,
|
||||
idempotencyKey: `${key}-create`,
|
||||
}),
|
||||
).toMatchObject({ ok: true });
|
||||
}
|
||||
expect(
|
||||
await otherRuntime.compareAndSwap({
|
||||
key: "other-partition",
|
||||
value: { label: "kept", rank: 1 },
|
||||
expectedRevision: null,
|
||||
idempotencyKey: "other-create",
|
||||
}),
|
||||
).toMatchObject({ ok: true });
|
||||
|
||||
// Four corrupt rows and a budget that no longer matches the data.
|
||||
purged.seed("records", { key: "bad-record", codecVersion: 0 });
|
||||
purged.seed("retention", { recordKey: "bad-sidecar", measuredBytes: -1 });
|
||||
purged.seed("retention", { recordKey: "orphan-sidecar" });
|
||||
purged.seed("receipts", { idempotencyKey: "bad-receipt" });
|
||||
purged.seed("governance", {
|
||||
bindingKey: "dataset-budget",
|
||||
budgetVersion: 1,
|
||||
usedBytes: 1,
|
||||
receiptCount: 0,
|
||||
});
|
||||
|
||||
const receipts = [];
|
||||
for (let batch = 0; batch < 5; batch += 1) {
|
||||
const receipt = await runtime.enforceLifecycleBatch({
|
||||
action,
|
||||
maxRows: 2,
|
||||
maxDurationMs: 1_000,
|
||||
});
|
||||
receipts.push(receipt);
|
||||
if (!receipt.ok || receipt.value.state === "COMPLETE") break;
|
||||
}
|
||||
|
||||
expect(receipts.at(-1)).toMatchObject({
|
||||
ok: true,
|
||||
value: { state: "COMPLETE" },
|
||||
});
|
||||
for (const key of ["healthy", "bad-record", "bad-sidecar"]) {
|
||||
expect(purged.readRaw("records", key)).toBeUndefined();
|
||||
expect(purged.readRaw("retention", key)).toBeUndefined();
|
||||
expect(purged.readRaw("receipts", `${key}-create`)).toBeUndefined();
|
||||
}
|
||||
expect(purged.readRaw("retention", "orphan-sidecar")).toBeUndefined();
|
||||
expect(purged.readRaw("receipts", "bad-receipt")).toBeUndefined();
|
||||
expect(purged.readRaw("governance", "dataset-budget")).toEqual({
|
||||
bindingKey: "dataset-budget",
|
||||
budgetVersion: 1,
|
||||
usedBytes: 0,
|
||||
receiptCount: 0,
|
||||
});
|
||||
// Corruption is reported, never a reason to stop deleting.
|
||||
const corrupt = observations.filter(
|
||||
(event) => event.failureCode === "CORRUPT_DATA",
|
||||
);
|
||||
expect(corrupt.length).toBeGreaterThan(0);
|
||||
expect(observations.at(-1)).toMatchObject({ outcome: "SUCCESS" });
|
||||
|
||||
expect(other.readRaw("records", "other-partition")).toBeDefined();
|
||||
expect(await otherRuntime.read("other-partition")).toMatchObject({
|
||||
ok: true,
|
||||
value: { value: { label: "kept" } },
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("rejects invalid measurements, byte overage, and receipt-count overage atomically", async () => {
|
||||
const invalidMemory = new MemoryIndexedDbFactory();
|
||||
const invalidBase = dependencies(invalidMemory);
|
||||
|
||||
Reference in New Issue
Block a user