Skip to content

Give web the worker's schema-drift guard - #333

Open
joei4cm wants to merge 2 commits into
CopilotKit:mainfrom
joei4cm:fix/183-web-schema-drift-guard
Open

joei4cm wants to merge 2 commits into
CopilotKit:mainfrom
joei4cm:fix/183-web-schema-drift-guard

Conversation

@joei4cm

@joei4cm joei4cm commented Dec 3, 2026 •

Copy link
Copy Markdown

Summary

apps/web/start.sh ran a bare prisma migrate deploy and nothing else. The worker, which migrates the same database, refused to boot against a schema it did not match. So a drifted deploy half-succeeded: the worker stayed down while web kept serving traffic against that schema — and if web deployed first, it applied the migrations with no guard around it at all, closing the repair window before the worker ever looked.

This extracts the worker's guard into a shared script and applies it to both services.

Refs #183

What changed

  • The guard moved to packages/outpost/db/schema-drift-guard.sh and is sourced by both apps/worker/start.sh and apps/web/start.sh. It retains the timeout bound, OUTPOST_ALLOW_SCHEMA_DRIFT release valve, distinction between "the schema differs" (exit 2) and "the check never ran" (any other exit), and stdout/stderr split. The timeout now uses SIGKILL for the read-only diff: BusyBox can otherwise report success when a timed-out process catches SIGTERM and exits zero. The sourced guard also preserves its caller's shell error setting. The comments moved with it.
  • web now runs the guard. migrate deploy only compares the migrations directory against the _prisma_migrations bookkeeping table and never inspects the schema, so a migration recorded as applied but never executed is invisible to it. web now refuses to serve against that database, exactly like the worker.
  • The web image gets the guard. One COPY line; the worker's image already copies the whole packages/ tree.
  • web's healthcheck start-period goes from 10s to 120s. web now runs two prisma CLI invocations before it binds its port, and at 10s an orchestrator honouring that status could restart the container mid-check — killing it mid-migration manufactures the P3009 failed-migration state the guard exists to report. The value now matches the worker's.
  • CI exercises both services. The step "Verify the worker startup guard" is now "Verify the startup guard" and runs each service's start.sh against a stub CLI, asserting every branch: clean schema boots, drift blocks the boot, the override boots, an unrecognised override does not, a tool failure blocks, a deploy failure blocks, a bad timeout value does not fail the step closed, diagnostics reach stderr, and an overridden boot does not page.

The migration itself is still run by each service separately — two concurrent migrators against one database is the existing arrangement, and consolidating that is a separate change.

One thing a reviewer might reach for: the comment in 20260812220000_create_missing_systemconfig_table/migration.sql says the repair's assertion cannot be deferred to the worker's drift guard because "apps/web/start.sh runs a bare migrate deploy against this same database with no guard". That is now false. The migration file is still left alone — Prisma records a checksum of each migration, so editing one that has already been applied changes migration history and can cause checksum errors. The comment stands as a record of what was true when it was written.

Tests

  • The CI step above is the regression test. It runs each service's start.sh against a stub CLI and asserts every branch, preserves the caller's errexit setting, and runs the timeout in node:24-alpine against a child that catches SIGTERM and exits zero. It passes under bash, dash and busybox sh locally.

  • End-to-end against a real PostgreSQL 16 and the real prisma CLI, with apps/web/start.sh and apps/worker/start.sh run against it:

    database web worker
    matches schema.prisma boots, exit 0 boots, exit 0
    one extra column on Ticket blocked, exit 1, FATAL naming the difference blocked, exit 1
    same, with OUTPOST_ALLOW_SCHEMA_DRIFT=1 boots, exit 0, WARNING on stderr —
  • turbo run typecheck --filter=@copilotkit/outpost-worker and the worker's 84 tests pass; pnpm lint passes. Full pnpm test passes on rerun (11/11 packages). An earlier run hit the pre-existing flaky packages/outpost/ai/src/github-evidence-auth.test.ts ("1 of 2 exchanges arrived"), which this change does not touch and which fails on some runs and passes on others.

Note for reviewers

The guard's own comments carry the reasoning, including why ANY difference is fatal rather than classified: an earlier revision failed only on missing objects, so a sibling service mid-rollout would not block the worker — and that classifier silently passed real drift (missing enum values and wrong column types both escaped it) while printing "schema matches" against a drifted database. A guard that can be wrong in the reassuring direction is worse than no guard, so the crude check stands and OUTPOST_ALLOW_SCHEMA_DRIFT is the release valve.

One consequence worth naming: since both services now refuse to start on drift, a deploy that leaves the database skewed blocks both of them, not just the worker. That is the intended reading of the issue — a half-deployed pair is what the guard exists to prevent — but it does mean an operator mid-incident needs OUTPOST_ALLOW_SCHEMA_DRIFT=1 on both services, not just the worker. The skew itself is more likely now, too: the two services already migrate concurrently (Prisma's advisory lock serialises the migrations, and docs/deployment.md says so), and a drift check that runs while the sibling is applying a migration sees the intermediate state. That was already true of the worker; web now shares it. OUTPOST_ALLOW_SCHEMA_DRIFT=1 is the valve for that window.

🤖 Generated with Claude Code

joei4cm and others added 2 commits December 4, 2026 06:34
web and worker migrate the same database, but only the worker checked
that the database really matches schema.prisma. `migrate deploy` compares
the migrations directory against the _prisma_migrations bookkeeping table
and never inspects the schema, so a migration recorded as applied but
never executed is invisible to it. web therefore kept serving traffic
against a schema the worker refuses to run against — and if web deployed
first, it applied migrations with no guard around it, closing the repair
window before the worker ever looked.

The guard now lives in packages/outpost/db/schema-drift-guard.sh and
both start.sh files source it. Its behaviour is unchanged: the same
timeout bound, the same OUTPOST_ALLOW_SCHEMA_DRIFT release valve, the
same distinction between "the schema differs" (exit 2) and "the check
never ran" (any other exit), and the same stdout/stderr split that keeps
NOTE and WARNING lines out of an alert keyed on stdout.

The CI step that exercises the guard against a stub CLI now runs both
services, so a change that breaks one of them fails there.

Refs CopilotKit#183

Co-Authored-By: Claude Code <noreply@anthropic.com>
BusyBox timeout returns the supervised command's status, so a child that catches TERM and exits zero could make a timed-out drift check look successful. Kill the read-only diff at the deadline and normalize the resulting status instead.

Capture deploy and diff statuses in conditionals rather than toggling errexit, so sourcing the guard does not change the caller's shell settings. Cover both cases in CI, including the BusyBox timeout used by the images.

Co-Authored-By: Claude Code <noreply@anthropic.com>

This branch has not been deployed

No deployments
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