fix(storage): let retention sweep and receipt prune step over a corrupt row (05-05)
One corrupt sidecar or receipt stopped every later sweep or prune, so expired rows stayed on the device and receipts grew until all writes failed LIMIT_EXCEEDED. Both now leave the corrupt row, count it and report it as one CORRUPT_DATA observation, and keep a budget they cannot account for as it is. A malformed budget row in prune now fails as CORRUPT_DATA, not MIGRATION_FAILED. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
6ab564f376
commit
b87ab5ee8e
@@ -199,6 +199,12 @@ remote tab 강제 종료, database 삭제, 무한 reload는 금지한다.
|
||||
|
||||
- `MIGRATION_FAILED`, unknown codec, schema postcondition failure
|
||||
- `CORRUPT_DATA`, `NOT_READABLE`, forced close
|
||||
- `RETENTION_SWEEP`(`INDEXEDDB_WRITE`)과 `pruneExpiredReceipts`
|
||||
(`INDEXEDDB_MIGRATE`)의 `CORRUPT_DATA` 관측은 실패가 아니다. 손상 행을 지우지
|
||||
않고 건너뛴 수이고, 나머지 정상 행은 정리된다(05-05,
|
||||
`indexeddb-runtime.ts` `purgeEligibleRecords`,
|
||||
`indexeddb-maintenance.ts` `pruneExpiredReceipts`). budget이 삭제 bytes나
|
||||
receipt 수를 감당하지 못하면 그 값은 내리지 않고 둔다
|
||||
- transaction request success 후 commit failure
|
||||
- `LIMIT_EXCEEDED`: dataset logical byte/receipt budget 초과
|
||||
- lifecycle batch가 proof 없이 실행되거나 TTL/UNTIL_SYNCED record가 잘못 노출·삭제
|
||||
|
||||
@@ -146,6 +146,14 @@ function migrationFailure(): BrowserDataFailure {
|
||||
return failureOf(migrationFailed());
|
||||
}
|
||||
|
||||
function corruptFailure(): BrowserDataFailure {
|
||||
return failureOf(
|
||||
browserDataFailure("CORRUPT_DATA", "INDEXEDDB_MIGRATE", {
|
||||
recovery: "READ_ONLY",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function unsupportedFailure(): BrowserDataFailure {
|
||||
return failureOf(unsupported());
|
||||
}
|
||||
@@ -1124,6 +1132,11 @@ export function createIndexedDbMaintenance<WireValue>(
|
||||
const opened = await openExactVersion(input.signal);
|
||||
if (!opened.ok) return observeResult(opened);
|
||||
const database = opened.value;
|
||||
// 05-05. A corrupt receipt is left in place and counted, not a stop: one
|
||||
// bad row used to end every later prune, so receipts piled up until every
|
||||
// write failed with LIMIT_EXCEEDED. A receipt count already at zero stays
|
||||
// there. Both are reported as one CORRUPT_DATA observation.
|
||||
let corruptRows = 0;
|
||||
|
||||
try {
|
||||
const pruned = await runTransaction<
|
||||
@@ -1188,8 +1201,8 @@ export function createIndexedDbMaintenance<WireValue>(
|
||||
String(cursor.primaryKey) ||
|
||||
cursor.value.expiresAtEpochMs > cutoff.value
|
||||
) {
|
||||
context.fail(migrationFailure());
|
||||
return { kind: "SUSPEND" };
|
||||
corruptRows += 1;
|
||||
return { kind: "CONTINUE" };
|
||||
}
|
||||
let deleteRequest: IDBRequest<undefined>;
|
||||
try {
|
||||
@@ -1207,11 +1220,14 @@ export function createIndexedDbMaintenance<WireValue>(
|
||||
budgetRequest.onerror = () =>
|
||||
context.requestFailed(budgetRequest.error);
|
||||
budgetRequest.onsuccess = () => {
|
||||
if (
|
||||
!isStoredDatasetBudget(budgetRequest.result) ||
|
||||
budgetRequest.result.receiptCount < 1
|
||||
) {
|
||||
context.fail(migrationFailure());
|
||||
if (!isStoredDatasetBudget(budgetRequest.result)) {
|
||||
context.fail(corruptFailure());
|
||||
return;
|
||||
}
|
||||
if (budgetRequest.result.receiptCount < 1) {
|
||||
corruptRows += 1;
|
||||
deletedRows += 1;
|
||||
resume({ kind: "CONTINUE" });
|
||||
return;
|
||||
}
|
||||
const budgetWrite = governance.put({
|
||||
@@ -1248,6 +1264,15 @@ export function createIndexedDbMaintenance<WireValue>(
|
||||
});
|
||||
},
|
||||
);
|
||||
if (corruptRows > 0) {
|
||||
observe({
|
||||
operation: "INDEXEDDB_MIGRATE",
|
||||
outcome: "FAILED",
|
||||
schemaVersion: dependencies.schemaVersion,
|
||||
countBucket: countBucket(corruptRows),
|
||||
failureCode: "CORRUPT_DATA",
|
||||
});
|
||||
}
|
||||
return observeResult(
|
||||
pruned,
|
||||
pruned.ok ? pruned.value.deletedRows : 0,
|
||||
|
||||
@@ -2045,12 +2045,21 @@ export function createIndexedDbRuntime<Value, WireValue, Query>(
|
||||
return browserDataSuccess(undefined);
|
||||
}
|
||||
|
||||
function purgeEligibleRecords(
|
||||
/**
|
||||
* 05-05. Like the partition purge (R-5.4), the sweep never stops on a row it
|
||||
* cannot read: failing closed left every later expired row on the device and
|
||||
* failed the next sweep the same way. A corrupt sidecar is left in place,
|
||||
* counted and reported as one CORRUPT_DATA observation per batch. A budget
|
||||
* that cannot account for a deleted row's bytes is left as it is: it can
|
||||
* only read high, which blocks writes and never admits one.
|
||||
*/
|
||||
async function purgeEligibleRecords(
|
||||
input: IndexedDbLifecycleBatchInput,
|
||||
deadline: number,
|
||||
cutoffEpochMs: number,
|
||||
): Promise<BrowserDataResult<IndexedDbLifecycleBatchReceipt>> {
|
||||
return runTransaction(
|
||||
let corruptRows = 0;
|
||||
const result = await runTransaction<IndexedDbLifecycleBatchReceipt>(
|
||||
[
|
||||
dependencies.recordStore,
|
||||
dependencies.governanceStore,
|
||||
@@ -2141,11 +2150,9 @@ export function createIndexedDbRuntime<Value, WireValue, Query>(
|
||||
"UNTIL_SYNCED" &&
|
||||
cursor.value.synchronization !== "CONFIRMED")
|
||||
) {
|
||||
context.fail(corruptFailure("INDEXEDDB_WRITE"));
|
||||
// `fail` aborts, so the pump has nowhere to go. Suspending
|
||||
// without ever resuming says that without claiming a summary
|
||||
// the sweep never reached.
|
||||
return { kind: "SUSPEND" };
|
||||
scannedRows += 1;
|
||||
corruptRows += 1;
|
||||
return { kind: "CONTINUE" };
|
||||
}
|
||||
scannedRows += 1;
|
||||
const measuredBytes = cursor.value.measuredBytes;
|
||||
@@ -2177,13 +2184,16 @@ export function createIndexedDbRuntime<Value, WireValue, Query>(
|
||||
budgetRequest.onerror = () =>
|
||||
context.requestFailed(budgetRequest.error);
|
||||
budgetRequest.onsuccess = () => {
|
||||
if (
|
||||
!isStoredDatasetBudget(budgetRequest.result) ||
|
||||
budgetRequest.result.usedBytes < measuredBytes
|
||||
) {
|
||||
if (!isStoredDatasetBudget(budgetRequest.result)) {
|
||||
context.fail(corruptFailure("INDEXEDDB_WRITE"));
|
||||
return;
|
||||
}
|
||||
if (budgetRequest.result.usedBytes < measuredBytes) {
|
||||
corruptRows += 1;
|
||||
deletedRows += 1;
|
||||
resume({ kind: "CONTINUE" });
|
||||
return;
|
||||
}
|
||||
const storedBudgetRequest = governance.put({
|
||||
bindingKey: INDEXEDDB_DATASET_BUDGET_KEY,
|
||||
budgetVersion: 1,
|
||||
@@ -2216,6 +2226,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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1123,3 +1123,40 @@ describe("Q-6.5-7 stored-row rules stay the same at every call site", () => {
|
||||
).toMatchObject({ ok: false, error: { code: "POLICY_REJECTED" } });
|
||||
});
|
||||
});
|
||||
|
||||
describe("05-05 a receipt prune steps over a corrupt receipt", () => {
|
||||
it("deletes every expired healthy receipt, leaves the corrupt one and reports CORRUPT_DATA", async () => {
|
||||
const memory = new MemoryIndexedDbFactory();
|
||||
await prepareSchema(memory);
|
||||
seedReceipt(memory, "expired-a", 100);
|
||||
seedReceipt(memory, "expired-b", 200);
|
||||
memory.seed("receipts", { idempotencyKey: "broken", expiresAtEpochMs: 0 });
|
||||
const observations: IndexedDbObservation[] = [];
|
||||
const maintenance = createMaintenance(memory, defaultPolicy(), {
|
||||
nowEpochMilliseconds: () => 300,
|
||||
observe: (event) => observations.push(event),
|
||||
});
|
||||
|
||||
expect(
|
||||
await maintenance.pruneExpiredReceipts({
|
||||
maxRows: 10,
|
||||
maxDurationMs: 10_000,
|
||||
}),
|
||||
).toMatchObject({
|
||||
ok: true,
|
||||
value: { state: "COMPLETE", deletedRows: 2 },
|
||||
});
|
||||
expect(memory.readRaw("receipts", "expired-a")).toBeUndefined();
|
||||
expect(memory.readRaw("receipts", "expired-b")).toBeUndefined();
|
||||
expect(memory.readRaw("receipts", "broken")).toBeDefined();
|
||||
expect(
|
||||
memory.readRaw("governance", "dataset-budget"),
|
||||
).toMatchObject({ receiptCount: 0 });
|
||||
expect(observations).toContainEqual(
|
||||
expect.objectContaining({
|
||||
outcome: "FAILED",
|
||||
failureCode: "CORRUPT_DATA",
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1035,9 +1035,10 @@ describe("IndexedDB runtime", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("aborts a sweep whose budget row cannot account for the deleted bytes", async () => {
|
||||
it("05-05 sweeps a row the budget cannot account for and leaves the budget high", async () => {
|
||||
const memory = new MemoryIndexedDbFactory();
|
||||
let epochMs = 1_000;
|
||||
const observations: IndexedDbObservation[] = [];
|
||||
const runtime = createIndexedDbRuntime(
|
||||
dependencies(memory, {
|
||||
storagePolicy: {
|
||||
@@ -1054,6 +1055,7 @@ describe("IndexedDB runtime", () => {
|
||||
authorized: true,
|
||||
proofToken: "sweep_authority_proof_001",
|
||||
}),
|
||||
observe: (event) => observations.push(event),
|
||||
}),
|
||||
);
|
||||
expect(await runtime.open()).toMatchObject({ ok: true });
|
||||
@@ -1066,9 +1068,8 @@ describe("IndexedDB runtime", () => {
|
||||
}),
|
||||
).toMatchObject({ ok: true });
|
||||
|
||||
// A budget that cannot account for the record's bytes is corrupt, and the
|
||||
// sweep only discovers it after both deletions have already succeeded
|
||||
// inside the transaction.
|
||||
// A budget that cannot account for the record's bytes is corrupt. The
|
||||
// sweep finds it only after both deletions succeeded in the transaction.
|
||||
memory.seed("governance", {
|
||||
bindingKey: "dataset-budget",
|
||||
budgetVersion: 1,
|
||||
@@ -1085,15 +1086,20 @@ describe("IndexedDB runtime", () => {
|
||||
maxDurationMs: 1_000,
|
||||
}),
|
||||
).toMatchObject({
|
||||
ok: false,
|
||||
error: { code: "CORRUPT_DATA" },
|
||||
ok: true,
|
||||
value: { state: "COMPLETE", deletedRows: 1 },
|
||||
});
|
||||
// `fail` aborts. Recording the failure without aborting would report the
|
||||
// same code while letting the two deletions commit against a budget that
|
||||
// never moved.
|
||||
expect(aborts.count).toBe(1);
|
||||
expect(memory.readRaw("records", "sweep-record")).toBeDefined();
|
||||
expect(memory.readRaw("retention", "sweep-record")).toBeDefined();
|
||||
expect(aborts.count).toBe(0);
|
||||
expect(memory.readRaw("records", "sweep-record")).toBeUndefined();
|
||||
expect(memory.readRaw("retention", "sweep-record")).toBeUndefined();
|
||||
// Not lowered below what it cannot account for: a budget that reads high
|
||||
// blocks writes, one that reads low would admit them.
|
||||
expect(memory.readRaw("governance", "dataset-budget")).toMatchObject({
|
||||
usedBytes: 0,
|
||||
});
|
||||
expect(observations).toContainEqual(
|
||||
expect.objectContaining({ failureCode: "CORRUPT_DATA" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("requires lifecycle authority and only deletes sync-confirmed rows", async () => {
|
||||
@@ -1606,3 +1612,67 @@ describe("05-03 a LOGOUT or ACCOUNT_DELETION purge survives corrupt governance",
|
||||
expect(memory.readRaw("records", "personal-1")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("05-05 a retention sweep steps over a corrupt row", () => {
|
||||
it("deletes every expired healthy row, leaves the corrupt sidecar and reports it", async () => {
|
||||
const memory = new MemoryIndexedDbFactory();
|
||||
let epochMs = 1_000;
|
||||
const observations: IndexedDbObservation[] = [];
|
||||
const runtime = createIndexedDbRuntime(
|
||||
dependencies(memory, {
|
||||
storagePolicy: {
|
||||
...TEST_STORAGE_POLICY,
|
||||
retention: { kind: "TTL", maxAgeMs: 10 },
|
||||
},
|
||||
keyRange:
|
||||
memory.keyRange as NonNullable<
|
||||
IndexedDbRuntimeDependencies<Item, Item, Query>["keyRange"]
|
||||
>,
|
||||
nowEpochMilliseconds: () => epochMs,
|
||||
nowMonotonicMilliseconds: () => 0,
|
||||
observe: (event) => observations.push(event),
|
||||
}),
|
||||
);
|
||||
expect(await runtime.open()).toMatchObject({ ok: true });
|
||||
for (const key of ["a", "b", "c"]) {
|
||||
await runtime.compareAndSwap({
|
||||
key,
|
||||
value: { label: key, rank: 1 },
|
||||
expectedRevision: null,
|
||||
idempotencyKey: `create-${key}`,
|
||||
});
|
||||
}
|
||||
// Sorts first on the eligibility index and names a record it is not keyed by.
|
||||
memory.seed("retention", {
|
||||
recordKey: "zzz",
|
||||
writtenAtEpochMs: 0,
|
||||
synchronization: "NONE",
|
||||
measuredBytes: 0,
|
||||
eligibleAtEpochMs: 0,
|
||||
});
|
||||
epochMs = 5_000;
|
||||
|
||||
for (let attempt = 0; attempt < 2; attempt += 1) {
|
||||
expect(
|
||||
await runtime.enforceLifecycleBatch({
|
||||
action: "RETENTION_SWEEP",
|
||||
maxRows: 100,
|
||||
maxDurationMs: 1_000,
|
||||
}),
|
||||
).toMatchObject({ ok: true, value: { state: "COMPLETE" } });
|
||||
}
|
||||
expect(
|
||||
["a", "b", "c"].filter(
|
||||
(key) => memory.readRaw("records", key) !== undefined,
|
||||
),
|
||||
).toEqual([]);
|
||||
expect(memory.readRaw("retention", "zzz")).toBeDefined();
|
||||
expect(observations).toContainEqual(
|
||||
expect.objectContaining({
|
||||
operation: "INDEXEDDB_WRITE",
|
||||
outcome: "FAILED",
|
||||
failureCode: "CORRUPT_DATA",
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user