fix(cloud): A4 review R2: a record scrub never strips a workspace that resumed meanwhile; R4 note

scrubStoppedRecord re-reads the row right before the PATCH and only scrubs one that is still
suspended or archived with no session; if the row resumed while the PATCH was in flight, the live
policy is re-applied at once (reason scrub_race). Failed rows are left out of the sweep on purpose:
failAndRelease destroys their provider sandbox.
This commit is contained in:
Neil
2026-11-15 15:02:05 -08:00
parent 97c12b73f7
commit 7da2415f79
2 changed files with 50 additions and 2 deletions
+20 -2
View File
@@ -873,7 +873,21 @@ export async function scrubStoppedRecord(deps: LifecycleDeps, row: SandboxRow):
if (!recordMayHoldValues(deps, row)) {
return
}
const outcome = await scrubRecordPolicy(deps, row)
// Review R2: the row may have resumed since it was selected (or since its stop). Only a still-stopped row
// with no session is scrubbed; a scrub that raced a resume is undone by a fresh apply right after.
const stopped = (fresh: SandboxRow | null): boolean =>
fresh !== null && (fresh.status === 'suspended' || fresh.status === 'archived') && !fresh.session_id
const before = await getSandbox(deps.db, row.id)
if (!before || !stopped(before)) {
return
}
const outcome = await scrubRecordPolicy(deps, before)
const after = await getSandbox(deps.db, row.id)
if (after && !stopped(after) && after.session_id && (after.status === 'running' || after.status === 'resuming')) {
console.log(JSON.stringify({ event: 'policy_scrub_raced_resume', sandboxId: row.id }))
await applySandboxPolicy(deps, after, 'scrub_race').catch(() => undefined)
return
}
const ok = outcome === 'scrubbed'
await deps.db
.prepare('UPDATE sandboxes SET policy_scrub_pending = ?, policy_scrubbed_at = COALESCE(?, policy_scrubbed_at) WHERE id = ?')
@@ -882,7 +896,11 @@ export async function scrubStoppedRecord(deps: LifecycleDeps, row: SandboxRow):
console.log(JSON.stringify({ event: 'policy_record_scrub', sandboxId: row.id, outcome: typeof outcome === 'string' ? outcome : outcome.failed }))
}
/** Stopped rows whose record may still hold values: a failed scrub, or an apply since the last scrub. */
/**
* Stopped rows whose record may still hold values: a failed scrub, or an apply since the last scrub.
* 'failed' rows are left out on purpose (review R4): failAndRelease destroys their provider sandbox, and the
* cron retries a destroy that didn't finish, so no record is left to scrub.
*/
export async function rowsToScrub(db: D1Database, limit = 25): Promise<SandboxRow[]> {
const { results } = await db
.prepare(
@@ -113,3 +113,33 @@ describe('A4: the post-resume write retries, and a lasting failure is visible, n
expect(record).not.toContain(KEY)
})
})
describe('A4 review R2: a scrub never strips a workspace that resumed meanwhile', () => {
it('a row that resumed before the scrub is skipped; one that resumed during it gets a fresh apply', async () => {
const t = await setup()
const life = (await lifecycleDeps(t.h.env, t.h.deps))!
const { scrubStoppedRecord } = await import('../src/lifecycle')
// Selected while stopped, resumed before the scrub runs: nothing is written.
expect((await t.h.call('POST', `/v1/sandboxes/${t.id}/suspend`, { bearer: t.mia })).status).toBe(200)
const stale = await t.row()
expect((await t.h.call('POST', `/v1/sandboxes/${t.id}/resume`, { bearer: t.mia })).status).toBe(200)
t.box().recordPolicy = undefined
await scrubStoppedRecord(life, stale)
expect(t.box().recordPolicy).toBeUndefined()
// Resumes while the PATCH is in flight: the live policy is re-applied with its values.
expect((await t.h.call('POST', `/v1/sandboxes/${t.id}/suspend`, { bearer: t.mia })).status).toBe(200)
const deps: LifecycleDeps = {
...life,
provider: {
...life.provider,
updateRecordNetworkPolicy: async (name, policy) => {
await life.provider.updateRecordNetworkPolicy!(name, policy)
expect((await t.h.call('POST', `/v1/sandboxes/${t.id}/resume`, { bearer: t.mia })).status).toBe(200)
t.box().policy = { allow: {} }
}
}
}
await scrubStoppedRecord(deps, await t.row())
expect(JSON.stringify(t.box().policy)).toContain(KEY)
})
})