Skip to content

fix(backup): resolve the effective interval identically everywhere (audit C1, H3) - #425

Merged
passcod merged 1 commit into
mainfrom
claude/pr-370-fix-c1-backup-interval
Aug 1, 2026
Merged

fix(backup): resolve the effective interval identically everywhere (audit C1, H3)#425
passcod merged 1 commit into
mainfrom
claude/pr-370-fix-c1-backup-interval

Conversation

@passcod

@passcod passcod commented Aug 1, 2026

Copy link
Copy Markdown
Member

Fixes C1 (critical) and H3 (high) from the audit in #370. They're two halves of the same disagreement about what a backup schedule means, so they're one change.

C1 — the staleness scan skipped every group on the inherited default

backup::staleness::scan_rows inner-joined server_group_backup_schedule and filtered expected_interval IS NOT NULL, with no fallback to the type default. A (group, type) with no override row — the out-of-the-box case, since tamanu-postgres ships a canopy-wide 6h default and rows are only written by an explicit set_schedule — was never in the scan set.

The schedulers back those pairs up happily via backups_due_now_for_server, but nothing then checked that the backups kept arriving. A device that stopped backing up raised no backup-staleness / backup-never issue, and because reconcile::sweep consumes the same rows, report-gap, size-mismatch and reconcile-missing never ran for it either.

H3 — a manual-only schedule silently reverted to the default cadence

The three resolvers (effective_interval_for_type, effective_interval_secs, group_schedules) used .and_then(|s| s.expected_interval).or_else(default), collapsing "no override row" (inherit) into "override row whose interval is NULL". The model and the set_schedule contract both document the latter as manual-only, deliberately unlike the same row's retention: None which does mean inherit.

So an operator setting expected_interval: null got a group that was still commanded to back up on the 6h default, still showed an interval and a next_run_at in group_schedules — and, because the scan honoured NULL as manual-only, could never raise a staleness alert either.

The fix

One resolver, database::backups::effective_interval: an override row answers on its own, NULL included; only its absence inherits the type's default_interval. All three call sites now go through it.

scan_rows left-joins both server_group_backup_schedule and backup_type_defaults and resolves the same way in Rust — a SQL COALESCE can't express it, because it would flatten manual-only back into the default. The set that is commanded to back up and the set that is monitored are now one set.

Tests

  • database: effective_interval_precedence covers all four states (no row, row with interval, row with NULL, row deleted).
  • database: scan_includes_pair_inheriting_the_type_default_interval — the C1 regression, asserting the inherited 6h shows up on the scan row.
  • database: scan_excludes_pair_whose_override_makes_it_manual_only — the default must not resurrect a manual-only pair.
  • private-server: group_schedules_reports_manual_only_override_as_no_schedule — the API surface of H3.

Ran green against a local Postgres 16 alongside the existing backup suites.


Generated by Claude Code

Two halves of the same disagreement about what a backup schedule means.

The staleness/reconcile scan set (`scan_rows`) inner-joined
`server_group_backup_schedule`, so a `(group, type)` with no override row —
the out-of-the-box case, since `tamanu-postgres` ships a canopy-wide 6h
default and rows are only written by explicit `set_schedule` — was never
scanned. The schedulers back those pairs up happily; nothing then checked
that the backups kept arriving, so a device that stopped backing up raised
no `backup-staleness`/`backup-never` issue, and the reconcile checks
(report-gap, size-mismatch, reconcile-missing) never ran for it either.

Conversely the resolvers collapsed "no override row" (inherit the default)
into "override row with a NULL interval", which the model and the
`set_schedule` contract both document as manual-only. Setting a group to
manual-only silently reverted it to the type default: still commanded to
back up, and shown in `group_schedules` with an interval and a next run.

Both now go through `database::backups::effective_interval`: an override row
answers on its own, NULL included; only its absence inherits the type
default. The scan left-joins both sources and resolves the same way, so the
set that is commanded to back up and the set that is monitored are one set.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SGfH1cdFKPnKpM7ytRThft
@passcod
passcod marked this pull request as ready for review August 1, 2026 19:35
@passcod
passcod added this pull request to the merge queue Aug 1, 2026
Merged via the queue into main with commit 9e8c2d4 Aug 1, 2026
7 checks passed
@passcod
passcod deleted the claude/pr-370-fix-c1-backup-interval branch August 1, 2026 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants