Skip to content

feat(apps): guard startup against newer databases and handed-off domains - #562

Merged
RentnerKev merged 4 commits into
mainfrom
rentner/feat/legacy-startup-guard
Nov 24, 2026
Merged

RentnerKev merged 4 commits into
mainfrom
rentner/feat/legacy-startup-guard

Conversation

@RentnerKev

Copy link
Copy Markdown
Member

Summary

This PR adds two startup safety nets that must be in place before the first built-in feature moves into an official app (#457). As agreed, the upgrade preview follows as its own PR, before the first release that removes a built-in feature. That PR needs a separate release asset, because the strict v1 readiness schema rejects new fields, and it needs provider profiles in the catalog.

Older image on a newer database

  • Drizzle applies only migrations newer than the latest recorded created_at. An older image therefore used to start silently on a database migrated by a newer image.
  • web/src/db/migrate.ts now compares the latest recorded migration with the newest migration the image ships, inside the migration lock and before applying anything. If the database is newer, it throws DatabaseNewerThanImageError and the CLI exits with status 78. The log line names both migrations and the remedy.
  • The check uses only the newest timestamp, not the full history. Two historic migration files were removed from the repository, so older installations legitimately carry rows the current journal does not list.
  • docker/production/entrypoint.sh treats status 78 as proxy-only mode:
    • The controller, Caddy, PostgreSQL and Valkey keep serving the retained proxy snapshot.
    • Web, background workers and the app manager stay stopped, and the database is not touched.
    • Every other migration failure still stops the appliance, as before.

Handed-off domains

  • WorkerLifecycleOptions gets an admit check that runs right before the first start, after restore admission. If it returns false, the loaded worker is never started and its state becomes the new protected handed_off. If the check fails, it retries like a failed load.

  • host_monitoring, docker_discovery and crowdsec_reconciliation are admitted only while the handoff journal leaves monitoring, docker-discovery and crowdsec to platform (isLegacyFeaturePlatformOwned). A restart, or a downgrade to any image with this check, therefore never runs a second productive copy beside the app.

  • Domains without their own web worker, and the Rust controller workers, are not gated here:

    • load-balancer rendering inside proxy reconciliation;
    • OIDC request paths;
    • DDNS and Docker observation in the controller, which has no database access.

    Each extraction gates its own paths with withPlatformOwnedLegacyFeature when it registers its source.

Related issue

Refs #457. Builds on #560. No overlap with #561 or with the handoff transport work.

Type of change

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

Testing

  • web/src/tests/db/database-migrate.db.test.ts simulates a future migration row. Both runMigrate and the CLI process refuse it, the CLI exits 78, and nothing is applied.

  • web/src/tests/tooling/docker/production/startup-guard.test.ts runs the entrypoint guard functions against fixture children:

    • status 0 continues to web;
    • status 78 holds in proxy-only mode after checking children;
    • status 1 is fatal.

    It also checks that the shell status matches DATABASE_NEWER_THAN_IMAGE_EXIT_CODE.

  • worker-lifecycle.test.ts covers handed_off without a start, and a failed admission read that retries the same unstarted worker.

  • worker-admission.db.test.ts covers the exported, committed and platform-owned cases.

  • start.coverage.test.ts checks that exactly the three feature workers ask the journal, with the right domains.

  • Local runs against PostgreSQL 18:

    • 55 tests across the migration, handoff, lifecycle and start suites pass;
    • 129 tests that touch worker states pass;
    • typecheck and oxlint (default and type-aware) pass;
    • shellcheck reports no new findings for entrypoint.sh.

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

An appliance started with an image older than its database no longer runs web on that database. It stays in proxy-only mode until the matching or a newer image starts. The support report can show the new worker state handed_off.

Screenshots

None.

🤖 Generated with Claude Code

Two safety nets for moving built-in features into official apps.

Newer database:
- Drizzle applies only migrations newer than the latest recorded one, so
  an older image used to start silently on a database migrated by a
  newer image. `migrate.ts` now compares the latest recorded migration
  with the newest one the image ships, refuses before applying anything
  and exits with status 78.
- The entrypoint treats status 78 as proxy-only mode. The controller,
  Caddy, PostgreSQL and Valkey keep serving the retained proxy snapshot,
  while web, background workers and the app manager stay stopped and
  the database is left unchanged. Other migration failures still stop
  the appliance.

Handed-off domains:
- Worker lifecycles accept an `admit` check that runs right before the
  first start. A false result leaves the loaded worker unstarted in the
  new `handed_off` state; a failing check retries like a failed load.
- Host monitoring, Docker discovery and CrowdSec reconciliation start
  only while the handoff journal leaves their domain to platform, so a
  restart or a downgrade to any image with this check never runs a
  second productive copy.

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 59 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 fe911d7 and 6380b1d.

Files selected for processing: 14

  • docker/production/entrypoint.sh
  • docker/production/tests/backups.sh
  • web/src/config/background-workers.config.ts
  • web/src/db/migrate.ts
  • web/src/server/Foundation/Types/worker-lifecycle.types.ts
  • web/src/server/Foundation/worker-lifecycle.server.ts
  • web/src/server/Foundation/worker-observability.ts
  • web/src/server/LegacyHandoff/worker-admission.server.ts
  • web/src/start.ts
  • web/src/tests/db/database-migrate.db.test.ts
  • web/src/tests/server/Foundation/worker-lifecycle.test.ts
  • web/src/tests/server/LegacyHandoff/worker-admission.db.test.ts
  • web/src/tests/start.coverage.test.ts
  • web/src/tests/tooling/docker/production/startup-guard.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

rentnerproxy-pr-automation Bot and others added 3 commits November 24, 2026 21:13
The backup harness runs the real normal-startup dispatcher with stubbed
roles. The dispatcher now calls `migrate_or_hold_proxy` around the
migration step, so the harness loads that guard, the proxy-only hold and
its status constant from the entrypoint.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@RentnerKev
RentnerKev merged commit 9bdc9cd into main Nov 24, 2026
65 checks passed
@RentnerKev
RentnerKev deleted the rentner/feat/legacy-startup-guard branch November 24, 2026 22:21
RentnerKev added a commit that referenced this pull request Nov 24, 2026
Since #562, an older image refuses a database that a newer image
migrated and serves only the retained proxy snapshot. The image
comparison still started the baseline normally after the candidate's
upgrade, so every candidate with a new migration failed there.

Compare the newest migration both images ship. Without new migrations
the baseline must start normally as before; with them it must hold
proxy-only mode with web stopped, while the cluster, rows and role grant
stay intact.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RentnerKev added a commit that referenced this pull request Nov 24, 2026
* feat(apps): serve the legacy feature handoff through App API v1

Wire the four Marketplace contract operations from the native broker to
the handoff journal: getLegacyHandoff, beginLegacyHandoff,
readLegacyHandoffPart and commitLegacyHandoff.

Each call resolves the active generation with a live lease and requires
the domain as a declared provider profile with an authorized
app.provider.register grant, all in one read committed transaction.
Domains without a registered source refuse begin, and app removal returns
uncommitted handoffs to platform while committed ownership stays.

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

* test(apps): check legacy handoff routes in the native app fixture

The production fixture app now reads its declared monitoring domain as
platform owned, is refused begin while no feature source is registered,
and gets 403 for an undeclared domain and after its provider grant is
revoked.

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

* feat(apps): bind legacy handoff part reads to the export digest

RentnerProxy/marketplace#81 now passes the export digest with every part
read, so a read of a replaced export fails with 409 instead of returning
a part. Pin the Marketplace crates to the updated contract head.

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

* fix(apps): run app management in read committed transactions

The removal check now aborts legacy handoffs, and their domain lock
refuses any other isolation level. State the level explicitly instead of
relying on the database default.

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

* style: apply automatic formatting

* chore(deps): pin the Marketplace crates to the merged handoff contract

RentnerProxy/marketplace#81 is merged. Pin all four crates to its merge
commit on Marketplace main, which stays reachable after the PR branch is
deleted. Its tree matches the final PR head; against the previous pin
only rustfmt changes in Rust test code differ.

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

* fix(apps): bind handoff keys and recheck authority after journal work

Address the review of the legacy handoff transport:

- Begin and commit bind their Idempotency-Key to the domain (and digest)
  before the journal changes. Reuse with other input returns 409
  idempotencyConflict. Keys expire after 30 days and at most 1,024 stay
  per installation; the journal still dedupes retries of forgotten keys.
  Purging an app's data drops its keys.
- Every handoff request rechecks the caller's deadline after the journal
  work, so an expired lease or delegation rolls the work back with 401.
- The native fixture checks that begin's 404 is the service's refusal.
- A DB test covers a begin between the two removal checks.

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

* style: apply automatic formatting

* test(apps): roll back a handoff begin whose broker disconnects

A begin forwarded just before the app manager stops the broker can wait
on the installation row while the second removal check holds it. The new
DB test serves the gateway on a real socket, disconnects the client while
the begin waits, and checks that the export it then runs is rolled back.

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

* ci: expect proxy-only rollback after a migrating candidate

Since #562, an older image refuses a database that a newer image
migrated and serves only the retained proxy snapshot. The image
comparison still started the baseline normally after the candidate's
upgrade, so every candidate with a new migration failed there.

Compare the newest migration both images ship. Without new migrations
the baseline must start normally as before; with them it must hold
proxy-only mode with web stopped, while the cluster, rows and role grant
stay intact.

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

* test(apps): synchronize the handoff deadline and in-flight tests

Address the review of the handoff DB tests:

- The lease-expiry case holds the export until the deadline passed and
  asserts that the export ran, so a slow authorization cannot pass the
  401 check without exercising the post-journal recheck.
- The in-flight case fails fast when the removal check's lock setup
  fails, and waits until the begin's session is blocked by exactly that
  check's backend, failing if it never is.

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

* test(apps): pin the in-flight begin to its own database session

The in-flight test now finds the session that waits to share-lock the
installation row and requires the removal check's backend among exactly
its blockers. The export, which runs in the begin's transaction, records
its backend PID, and the test asserts it is that waiting session.

The handoff DB tests also get an explicit 20 s timeout: each imports the
gateway cold and makes dozens of round trips, which can exceed Bun's 5 s
default on slow runners.

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: rentnerproxy-pr-automation[bot] <353791173+rentnerproxy-pr-automation[bot]@users.noreply.github.com>
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