worker: reap expired sandbox workspace backups from R2 - #70
Open
ai-yappa[bot] wants to merge 1 commit into
Open
ai-yappa[bot] wants to merge 1 commit into
ai-yappa[bot] wants to merge 1 commit into
Conversation
Cloudflare Sandbox createBackup ttl only rejects expired archives at restore time; the platform never deletes the R2 objects. The codevil-sandbox-backups bucket had no lifecycle rule and no cleaner, so every 600MB /workspace snapshot accumulated indefinitely (23.5GB and counting, 13.5GB over the R2 free tier). Add a daily scheduled sweep that deletes backups/ objects older than the 30-day WORKSPACE_CACHE_TTL_SECONDS and removes the matching workspace_snapshots rows from D1 so restores never target a missing archive.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Cloudflare Sandboxes stores every
createBackup()asbackups/{backupId}/data.sqsh+meta.jsonin thecodevil-sandbox-backupsR2 bucket. Thettloption only causes restore-time rejection — the platform does not delete the R2 objects (confirmed in the Backup/Restore guide). The bucket has no lifecycle-expiration rule and the worker never deletes stale archives, so every ~600 MB/workspacesnapshot accumulates forever.Current state (checked via Cloudflare API, Sep 20 2026):
codevil-sandbox-backups(40 backups), driven by the warm workspace-cache featureWORKSPACE_CACHE_TTL_SECONDSChange
Add a daily scheduled sweep (
scheduledhandler +[triggers] crons = ["0 4 * * *"]) that:backups/objects in the R2 bucket (paginated, 1000/page)uploadedtime is older than the 30-day TTLworkspace_snapshotsrows from D1 so restores never target a deleted archiveNew module:
packages/worker/src/backup-reaper.ts(pure selection + sweep orchestration), 7 unit tests inpackages/worker/test/backup-reaper.test.mjs, wired intopackages/worker/src/index.ts.Notes
WORKSPACE_CACHE_TTL_SECONDS(30 days) thatcreateBackupalready passes — the sweep enforces the TTL the code believed was being enforced.backups/is the platform-recommended complement (works without a deploy). It requires Cloudflare API write access, which the sandbox token doesn't have. A follow-up with#billing:read-scoped-plus write creds can add it viaPUT /accounts/{account_id}/r2/buckets/codevil-sandbox-backups/lifecycle.Verification
pnpm run buildpassespackages/workertest suite: 498/498 pass (7 new)