Skip to content

fix(backups): check app rows, generations and handoff parts against the journal - #564

Merged
RentnerKev merged 3 commits into
mainfrom
rentner/fix/app-backup-validation
Nov 24, 2026
Merged

RentnerKev merged 3 commits into
mainfrom
rentner/fix/app-backup-validation

Conversation

@RentnerKev

Copy link
Copy Markdown
Member

Summary

Backup validation already checks that app_runtime_installations matches the native installation journal, but dependent app rows, recorded generations and the new handoff parts were only tied together by foreign keys or not checked at all. A restored appliance could therefore start with rows the journal does not govern, and a journal behind the database only surfaced later as a conflict when the app registered its next generation.

This PR closes the four remaining backup gaps of #457. Every new check fails closed before any destructive restore step and reports BACKUP_VALIDATION=application_state.

  • Dependent rows: one bounded evidence query reads the installation rows, every installation_id referenced by the twelve installation-scoped app_runtime_* tables (grants, role permissions, secrets, events and the rest) plus legacy_feature_handoffs, and the highest generation per installation. Each reference must belong to an active installation in the backup inventory, independent of whether the dump still carries its foreign keys.
  • Generations: validation fails when the journal generation of an installation is lower than its highest app_runtime_generations.generation. The app manager registers journal generation + 1 next, which the database would reject as a conflict.
  • Handoff parts: isolated validation trial-decrypts every legacy_feature_handoff_parts row with the backed-up app key and its legacy-feature-handoff:v1:<domain>:<installationId>:<sequence>:<sha256> context, and checks the plaintext against the stored hash. Each exported handoff must hold exactly part_count parts with sequences 0..part_count-1, a committed handoff none, and no part may lack its journal row. Older backups without the tables are recognized through to_regclass.
  • After restore: the managed restore, the offline maintenance restore and scripts/production-restore.ts re-run the rows-versus-journal check once the database and app state are applied, before the restore journal completes. A mismatch keeps the journal pending, so the appliance stays stopped until the operator resumes or investigates. The CLI step is verify restored application state; the backup agent and the CLIs share the new backup-export.js --check-restored-app-inventory entry point.
  • Live checks: the capture of a new backup and a resumed portable restore now use the same journal comparison. The pre-restore check of an in-place target keeps comparing identities only, because its database is about to be replaced and its journal may already be ahead of the backup.
  • Diagnostics: app inventory, schema, generation and handoff failures now map to the new application_state reason instead of runtime_validation; the restore and smoke diagnostics allow it.

Related issue

Refs #457. The issue stays open for the parallel handoff transport, upgrade preview and the follow-ups below.

Follow-ups that are intentionally not part of this PR:

  • An offline CLI backup that writes format v6 when apps exist (scripts/production-backup.ts and the legacy guards in scripts/backup/volume-commands.ts).
  • A supported "retire app state" step, so an offline v6 restore can target the same appliance (core/src/app_manager/portable_backup.rs requires an empty target).
  • Optionally, sealing downloaded backups the way off-host copies are sealed.

Type of change

  • Bug fix
  • New feature
  • Refactoring
  • Documentation
  • CI / tooling
  • Dependency update
  • Other

Testing

  • bun run typecheck
  • oxlint and oxlint --config .oxlintrc.type-aware.json on all changed files
  • In a disposable Linux container (PostgreSQL 18.7, Bun 1.4.5, debug test controller built from core/), with synthetic data only:
    • validate-backup.test.ts, validate-backup.coverage.test.ts: one case per new rejection plus the positive paths for v4, v5 and v6 backups
    • backup-app-validation.db.test.ts (new): runs the evidence query and handoff validation against a fully migrated database, including pagination over 20 parts, a dropped foreign key with an orphaned grant, a journal behind the database, a missing part, a sequence gap, a wrong key and parts of a committed handoff
    • production-restore.test.ts, production-restore-stream.test.ts: command contract of the post-restore check, its position in the portable restore, and a mismatch that leaves the appliance stopped without completing the journal
    • maintenance-commands.test.ts, production-smoke-ci.test.ts: the new reason passes through, unknown reasons stay hidden
    • app-state.test.ts, legacy-archive-migration.db.test.ts and all of web/src/tests/tooling/docker/production/, including the real-PostgreSQL backup orchestration round trips

Checklist

  • I tested the relevant functionality.
  • Existing tests pass.
  • I added or updated tests where appropriate.
  • This PR does not include secrets, credentials, production data, or private certificates/keys.
  • I updated documentation where necessary.
  • I linked the relevant issue for a feature or fix, or explained why no issue is needed.

Breaking changes

None. Backups that previously validated still do, unless their app rows disagree with their own journal, which this PR rejects on purpose. App-related validation failures now report application_state instead of runtime_validation.

Screenshots

None

🤖 Generated with Claude Code

…he journal

Backup validation compared only the installation rows with the native
installation journal. Dependent rows, recorded generations and the new
handoff parts were tied together by foreign keys or not checked.

- Read installation rows, every installation referenced by the
  installation-scoped app tables and the handoff journal, and the highest
  generation per installation in one bounded query. Reject references
  outside the active inventory and a journal behind the database, which
  the next app registration would turn into a conflict.
- Trial-decrypt every legacy feature handoff part with the backed-up app
  key, check its content hash, and require exactly parts 0..n-1 for an
  exported handoff and none for a committed one. Older schemas are
  recognized through to_regclass.
- Re-run the journal comparison after the managed, offline and CLI
  restores applied the database and app state, before the restore
  journal completes, so a mismatch keeps the appliance stopped.
- Use the journal comparison for new captures and resumed portable
  restores; the in-place pre-restore check still compares identities.
- Report app state failures as BACKUP_VALIDATION=application_state.

Refs #457

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 10 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 9bdc9cd and 556b40e.

Files selected for processing: 17

  • docker/production/backups/app-database.ts
  • docker/production/backups/export.ts
  • docker/production/backups/maintenance.ts
  • docker/production/backups/restore.ts
  • docker/web/Types/validate-backup.types.ts
  • docker/web/validate-backup.ts
  • scripts/backup/maintenance-commands.ts
  • scripts/backup/volume-commands.ts
  • scripts/production-restore.ts
  • scripts/smoke/resources.ts
  • web/src/tests/db/backup-app-validation.db.test.ts
  • web/src/tests/tooling/docker/web/validate-backup.coverage.test.ts
  • web/src/tests/tooling/docker/web/validate-backup.test.ts
  • web/src/tests/tooling/github/production-smoke-ci.test.ts
  • web/src/tests/tooling/production-restore-stream.test.ts
  • web/src/tests/tooling/production-restore.test.ts
  • web/src/tests/tooling/scripts/backup/maintenance-commands.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@RentnerKev
RentnerKev merged commit 1ab0624 into main Nov 24, 2026
63 checks passed
@RentnerKev
RentnerKev deleted the rentner/fix/app-backup-validation branch November 24, 2026 23:10
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.

1 participant