Skip to content

feat(runtime): record what backup retention deleted - #387

Closed
prisis wants to merge 2 commits into
alphafrom
feat/log-backup-retention
Closed

prisis wants to merge 2 commits into
alphafrom
feat/log-backup-retention

Conversation

@prisis

@prisis prisis commented Aug 9, 2026 •

Copy link
Copy Markdown
Member

The scheduled backup's retention deletes objects from R2 and, until now, said nothing when it worked. The only log was on the failure path.

That is the gap behind plan 313 §4.4 ("No silent deletion of anything"). It is narrower than it sounds — retention is opt-in, so with no backupRetain configured nothing is ever deleted — but when an operator does configure it, snapshots were disappearing with no record.

Both retention defects found during review were silent successes: the cron deleting an operator's hand-taken snapshots, and later deleting another deployment's. Each was reconstructed afterwards from a missing file. A line naming what was pruned turns the next one into something you notice.

What it does

Names the retention window and the deleted keys, capped at 10 with a (+N more) summary — a first run after enabling retention on an old bucket can delete many, and a log line nobody can read is its own kind of silence. Stays quiet when it deletes nothing, so an ordinary cron fire adds no noise.

It uses the same console channel as the existing retention-failure warning; a Worker has no other operator-visible sink at this point in the run.

What it deliberately is not

A forensic record, not an approval gate. R2 deletes are irreversible and this is emitted after the fact — an operator who wants to review a prune before it destroys anything wants the prune verb with --dry-run, which is plan 313's WS4 and still open. The comment says so, so the next reader does not mistake this for the fix.

Verification

@lunora/runtime 857 tests (one new). The new test asserts both halves — that a prune names what it deleted, and that a no-op prune logs nothing — and I confirmed it is not vacuous by removing the log and watching only that test fail (expected \"info\" to be called), then restoring.

lint:types and lint:eslint --max-warnings=0 clean, Prettier clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_011sk9BLaUZDkPVAZ1sKhuDP

Summary by CodeRabbit

  • Bug Fixes
    • Improved scheduled backup retention handling when no backups are stale.
    • Retention cleanup now processes snapshot and related metadata deletions more efficiently.
    • Deletion logs now show up to 10 removed backup keys and report any additional deletions by count.

Retention is opt-in and its deletes are irreversible, but a successful
prune left no trace - only the failure path logged. Both retention
defects found in review, pruning an operator's snapshots and then
another deployment's, were silent successes that had to be
reconstructed from a missing file.

It now names the count and the keys, capped so a first run on an old
bucket does not print a line nobody can read, and stays quiet when it
deletes nothing. This is a forensic record, not an approval gate - that
is plan 313's WS4, still open.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011sk9BLaUZDkPVAZ1sKhuDP
@netlify

netlify Bot commented Aug 9, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for lunorash ready!

Name Link
🔨 Latest commit 507244d
🔍 Latest deploy log https://app.netlify.com/projects/lunorash/deploys/6a78386a2767500008523001
😎 Deploy Preview https://deploy-preview-387--lunorash.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Thank you for following the naming conventions! 🙏

@coderabbitai

coderabbitai Bot commented Aug 9, 2026 •

Copy link
Copy Markdown

Review Change Stack

Walkthrough

The retention flow now handles empty stale-manifest lists, deletes stale snapshots and sidecars concurrently in snapshot-first order, and limits retention logs to 10 deleted keys with an additional count.

Changes

Retention pruning

Layer / File(s) Summary
Retention pruning flow
packages/runtime/src/scheduled-backup.ts
The flow materializes stale manifests once, exits when none exist, deletes snapshots before sidecars, and logs the retention limit with bounded keys and deletion counts.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: logging successful backup-retention deletions.
Description check ✅ Passed The description explains the change, rationale, behavior, limitations, and verification, but omits the template’s formal checklist and CLA section.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/log-backup-retention

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Thank you for confirming the Contributor License Agreement! 🙏

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/runtime/src/scheduled-backup.ts`:
- Around line 138-149: Update the stale-pruning deletion flow around the
Promise.all call to use per-delete tracking or Promise.allSettled, recording
each successfully deleted backup key even when another deletion rejects.
Preserve the existing snapshot-then-sidecar deletion order and failure warning
behavior, while ensuring successful deletions are logged after partial failures.
- Around line 142-143: Update the retention-order comment near the snapshot and
sidecar deletion logic to replace the ungrammatical phrase with “because the
retention marker lives on the sidecar,” preserving the explanation that snapshot
deletion must precede sidecar deletion.
- Around line 162-167: Update the retention-success logging around shown and
rest to represent both R2 deletion targets for each stale manifest, including
snapshot keys, and report the actual deleted-object count and capped key list.
Preserve the cap behavior, and add tests covering one stale manifest and the cap
boundary.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 8aa5f91c-22ca-4f3c-b0b2-32e8cfadba6e

📥 Commits

Reviewing files that changed from the base of the PR and between af2ca1e and 872464c.

⛔ Files ignored due to path filters (1)
  • packages/runtime/__tests__/scheduled-backup.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
📒 Files selected for processing (1)
  • packages/runtime/src/scheduled-backup.ts

Comment on lines 138 to +149
await Promise.all(
mine
.toSorted((a, b) => b.localeCompare(a))
.slice(retain)
.map(async (manifestKey) => {
// Snapshot first, sidecar second. A sidecar without its snapshot
// is a listable entry that fails to restore; a snapshot without
// its sidecar is invisible to retention forever, because the
// marker retention prunes on lives on the sidecar. If only one
// delete lands, this order is the recoverable one — the next run
// sees the sidecar again and retries.
await store.delete(backupObjectKeyOfManifest(manifestKey));
await store.delete(manifestKey);
}),
stale.map(async (manifestKey) => {
// Snapshot first, sidecar second. A sidecar without its snapshot
// is a listable entry that fails to restore; a snapshot without
// its sidecar is invisible to retention forever, because the
// marker retention prunes on lives on the sidecar. If only one
// delete lands, this order is the recoverable one — the next run
// sees the sidecar again and retries.
await store.delete(backupObjectKeyOfManifest(manifestKey));
await store.delete(manifestKey);
}),
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Record successful deletions when pruning partially fails.

Promise.all rejects when one deletion fails, so execution skips the success log even though other deletion promises can already have completed. R2 does not roll back those deletions. Collect fulfilled delete results with Promise.allSettled or equivalent per-delete tracking, log the successful keys, and preserve the existing failure warning for rejected operations.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/runtime/src/scheduled-backup.ts` around lines 138 - 149, Update the
stale-pruning deletion flow around the Promise.all call to use per-delete
tracking or Promise.allSettled, recording each successfully deleted backup key
even when another deletion rejects. Preserve the existing snapshot-then-sidecar
deletion order and failure warning behavior, while ensuring successful deletions
are logged after partial failures.

Comment on lines +142 to +143
// its sidecar is invisible to retention forever, because the
// marker retention prunes on lives on the sidecar. If only one

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Correct the retention-order comment.

The phrase because the marker retention prunes on lives on the sidecar is not grammatical. Replace it with because the retention marker lives on the sidecar. This comment documents why snapshot deletion must precede sidecar deletion.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/runtime/src/scheduled-backup.ts` around lines 142 - 143, Update the
retention-order comment near the snapshot and sidecar deletion logic to replace
the ungrammatical phrase with “because the retention marker lives on the
sidecar,” preserving the explanation that snapshot deletion must precede sidecar
deletion.

Comment on lines +162 to +167
const shown = stale.slice(0, MAX_LOGGED_PRUNED_KEYS);
const rest = stale.length - shown.length;

// eslint-disable-next-line no-console -- server-side diagnostic, same channel as the retention-failure warning below; a Worker has no other operator-visible sink here.
console.info(
`[lunora] backup retention kept the newest ${String(retain)} and deleted ${String(stale.length)}: ${shown.join(", ")}${rest > 0 ? ` (+${String(rest)} more)` : ""}`,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Log the actual deleted object keys and count.

stale contains manifest keys, but each entry triggers two R2 deletions. A successful prune of one stale manifest therefore reports deleted 1 while deleting two objects, and the snapshot key is absent from the forensic record. Build the capped log list from both delete targets, or rename the message to explicitly describe backup records rather than deleted keys. Add a test for one stale manifest and for the cap boundary.

Proposed fix
-    const shown = stale.slice(0, MAX_LOGGED_PRUNED_KEYS);
-    const rest = stale.length - shown.length;
+    const deletedKeyCount = stale.length * 2;
+    const shown = stale
+        .slice(0, Math.ceil(MAX_LOGGED_PRUNED_KEYS / 2))
+        .flatMap((manifestKey) => [backupObjectKeyOfManifest(manifestKey), manifestKey])
+        .slice(0, MAX_LOGGED_PRUNED_KEYS);
+    const rest = deletedKeyCount - shown.length;

     console.info(
-        `[lunora] backup retention kept the newest ${String(retain)} and deleted ${String(stale.length)}: ${shown.join(", ")}${rest > 0 ? \` (+${String(rest)} more)\` : ""}`,
+        `[lunora] backup retention kept the newest ${String(retain)} and deleted ${String(deletedKeyCount)}: ${shown.join(", ")}${rest > 0 ? \` (+${String(rest)} more)\` : ""}`,
     );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const shown = stale.slice(0, MAX_LOGGED_PRUNED_KEYS);
const rest = stale.length - shown.length;
// eslint-disable-next-line no-console -- server-side diagnostic, same channel as the retention-failure warning below; a Worker has no other operator-visible sink here.
console.info(
`[lunora] backup retention kept the newest ${String(retain)} and deleted ${String(stale.length)}: ${shown.join(", ")}${rest > 0 ? ` (+${String(rest)} more)` : ""}`,
const deletedKeyCount = stale.length * 2;
const shown = stale
.slice(0, Math.ceil(MAX_LOGGED_PRUNED_KEYS / 2))
.flatMap((manifestKey) => [backupObjectKeyOfManifest(manifestKey), manifestKey])
.slice(0, MAX_LOGGED_PRUNED_KEYS);
const rest = deletedKeyCount - shown.length;
// eslint-disable-next-line no-console -- server-side diagnostic, same channel as the retention-failure warning below; a Worker has no other operator-visible sink here.
console.info(
`[lunora] backup retention kept the newest ${String(retain)} and deleted ${String(deletedKeyCount)}: ${shown.join(", ")}${rest > 0 ? ` (+${String(rest)} more)` : ""}`,
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/runtime/src/scheduled-backup.ts` around lines 162 - 167, Update the
retention-success logging around shown and rest to represent both R2 deletion
targets for each stale manifest, including snapshot keys, and report the actual
deleted-object count and capped key list. Preserve the cap behavior, and add
tests covering one stale manifest and the cap boundary.

@codspeed

codspeed Bot commented Aug 9, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 47 untouched benchmarks
⏩ 216 skipped benchmarks1


Comparing feat/log-backup-retention (507244d) with alpha (1762097)

Open in CodSpeed

Footnotes

  1. 216 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@prisis

prisis commented Aug 9, 2026

Copy link
Copy Markdown
Member Author

Superseded by #389, merged as 01dc8bf40.

This branch is an ancestor of feat/backup-prune-verb, so its commits went in with that merge. Because the repo squash-merges, they do not appear in alpha's history under their own shas and this PR will keep reporting as unmerged — the content is in.

Verified on alpha rather than inferred from PR state: the retention preview route, both retention log records, the retention and prune CLI verbs, and pruneBackups being reachable only from runBackupPrune (the scheduled path calls selectStaleBackups and never deletes).

@prisis prisis closed this Aug 9, 2026
@prisis
prisis deleted the feat/log-backup-retention branch August 11, 2026 22:29
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using our GitHub Discussions tab for questions.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 10, 2026

This branch was previously deployed

1 inactive deployment
benchmarks — 507244df Deployed Aug 9, 2026 by prisis via Benchmarks #1603
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant