Skip to content

fix(staging): keep new Durable Object migrations off primary staging - #2809

Merged
simple-agent-manager[bot] merged 10 commits into
mainfrom
sam/give-staging-lease-fair-4qynm8
Nov 16, 2026
Merged

simple-agent-manager[bot] merged 10 commits into
mainfrom
sam/give-staging-lease-fair-4qynm8

Conversation

@simple-agent-manager

@simple-agent-manager simple-agent-manager Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part 2 of SAM task 01M7HK7TY9GA8DTV3BGS4QYNM8 (part 1 was #2783, the lease queue). This change keeps branches that add a Durable Object migration off primary staging.

Problem: Durable Object migrations only move forward. Draft PR #2721 deployed DO migration tag v23 to primary staging while main was at v22. After that, the migration preflight (scripts/deploy/durable-object-migrations.ts) failed closed for every main-based branch there until #2721 merged, which collapsed two stagings back to one lane.

Change:

  • New scripts/deploy/staging-migration-guard.mjs, run in the queue job of deploy-staging.yml. It runs only for primary-staging deploys, and before a place is reserved.
  • It compares the [[migrations]] tags in the branch's apps/api/wrangler.toml against origin/main's copy (a shallow git fetch of main, then git show FETCH_HEAD:…).
  • If the branch adds a tag that main lacks, the deploy is refused with a message telling the agent to use staging2.
  • It fails closed when either file can't be read, or when any [[migrations]] block's tag can't be parsed.
  • reserve, acquire, release and deploys to staging2 are not checked.
  • No production path is touched: nothing in deploy-reusable.yml, deploy.yml or the sync script. A test asserts that the reusable workflow does not reference the guard.

Clean-up duty (documented, not automated): when such a branch is done on staging2 and has not merged, the lease holder tears staging2 down (Teardown → staging2 → DELETE) and redeploys main before releasing. staging2 rebuilds from nothing.

Primary staging today: #2721 merged at 08:11Z, so main is at v23 and primary's earlier v23 state has cleared. Nothing to repair.

Validation

  • pnpm lint (eslint on changed files). SonarCloud S4036 (git resolved via PATH) fixed in b58aa96: absolute /usr/bin/git on runners
  • pnpm typecheck (N/A to .mjs)
  • pnpm test: node --test scripts/deploy/staging-migration-guard.test.mjs 5 pass (against the real apps/api/wrangler.toml); deploy-staging-permissions.test.ts 28 pass (it runs the guard's node tests in CI and pins the step's position and condition)
  • Additional validation, the CLI path run for real against origin/main: unchanged branch → allowed; branch with an appended v99 tag → Staging deploy refused: This branch adds Durable Object migration tag(s) v99 … Use -f environment=staging2 … (exit 1); the same branch with LEASE_ENVIRONMENT=staging2 → allowed
  • N/A: no sweep/cron candidate selection

Staging Verification (REQUIRED for all code changes — merge-blocking)

  • Staging deployment green
  • Live app verified via Playwright: N/A, no app/runtime code changed
  • Existing workflows confirmed working
  • New feature/fix verified on staging
  • Infrastructure verification completed: N/A, no infra changes
  • Mobile and desktop verification notes added for UI changes: N/A, no UI

Staging Verification Evidence

Live refusal, 16:18Z (run 43906017573): I pushed a throwaway branch (sam/migration-guard-proof, since deleted) that adds a v99 migration tag, and dispatched a primary-staging deploy.

  • The queue job's guard step failed with: Staging deploy refused: This branch adds Durable Object migration tag(s) v99 that origin/main does not have. … Use -f environment=staging2; if the branch has not merged when you are done, tear staging2 down and redeploy main before releasing.
  • No reservation was written: the newest primary record stayed another branch's live v1 lease (7829525321).
  • lease, deploy and every downstream job were skipped.

Main-based deploys are unaffected: the guard passes when no tag is new. That is shown by the local CLI run and the new git-backed CLI test. The dry-run deploy on #2783 (43901912468) exercised the same queue job.

Design choice: a dry run of a migration branch on primary is also refused. It would be refused for real anyway, and staging2 is where such branches belong.

UI Compliance Checklist (Required for UI changes)

N/A: no UI changes.

UI Screenshot Evidence

N/A: no UI changes.

End-to-End Verification (Required for multi-component changes)

  • Data flow traced from user input to final outcome with code path citations
  • Capability test exercises the complete happy path across system boundaries
  • All spec/doc assumptions about existing behavior verified against code
  • Manual verification steps documented below

Data Flow Trace

gh workflow run deploy-staging.yml -f environment=staging → job queue → step "Keep new Durable Object migrations off primary staging" → staging-migration-guard.mjs main (git fetch origin main, checkInstallation → newMigrationTags → migrationTags) → exit 1 stops the job before the staging-lease.mjs queue step, so no reservation is written and lease/deploy are skipped.

Untested Gaps

N/A: the GitHub step wiring is pinned by the workflow test and shown live in the staging evidence.

Post-Mortem (Required for bug fix PRs)

What broke

A draft PR's DO migration v23 deployed to primary staging blocked every main-based branch there until it merged.

Root cause

Nothing stopped a branch with new forward-only migrations from deploying to the shared primary installation.

Class of bug

An irreversible state change in a shared environment, made by unmerged work.

Why it wasn't caught

The migration preflight protects against inconsistent history, but nothing chose which installation such branches may use.

Process fix included in this PR

The guard itself, plus .claude/rules/13-staging-verification.md, .claude/commands/do.md, .agents/skills/do/SKILL.md and the full reference (clean-up duty).

Post-mortem file

tasks/archive/2026-11-16-staging-lease-fair-queue.md

Specialist Review Evidence (Required for agent-authored PRs)

  • All local reviewers completed and findings addressed before merge
  • If any reviewer did NOT complete: needs-human-review label added and merge deferred to human
Reviewer Status Outcome
task-completion-validator ADDRESSED PASS, no CRITICAL/HIGH. LOW 1: the CLI git path was untested → a git-backed CLI test was added (fails when the refusal is disabled), 78cec91. LOW 2: the guard if: could drift from the deploy job → the test now asserts it contains the deploy job's condition. LOW 4 → doc sentence (the guard runs from the branch's workflow copy; the preflight is the backstop). LOW 5 → ::error:: annotation. LOW 3 (a dry run of a migration branch on primary is refused) is kept by design. LOW 6 (name-only comparison) is accepted: it does not cause the ordering problem.

CodeRabbit Review Evidence (Required for agent-authored PRs)

  • CodeRabbit requested when the PR was marked ready for review
  • Waited about 15 minutes (or until a skip, rate-limit or quota reply)
  • Either CodeRabbit reviewed and no feedback is unresolved, or it did not review and the outcome is recorded below

CodeRabbit Notes

  • 16:18Z: automatic status "Review skipped: Bot user detected".
  • 16:20:43Z: coderabbit-review label applied when marked ready. The label-triggered coderabbit-bot-review.yml runs ended in startup_failure (43906434315, 43906446710).
  • 16:38:51Z: dispatched coderabbit-bot-review.yml from main once (43909800652). CodeRabbit replied at 16:39Z: "Action not completed. Review rate limited."
  • Per rule 25 the wait ends on a rate-limit reply: no review, merging on the remaining gates.

Exceptions (If any)

  • Scope: none
  • Rationale: none
  • Expiration: none

Agent Preflight (Required)

  • Preflight completed before code changes

Classification

  • external-api-change
  • cross-component-change
  • business-logic-change
  • public-surface-change
  • docs-sync-change
  • security-sensitive-change
  • ui-change
  • infra-change

External References

N/A: no external API; the change uses git and the existing wrangler.toml format ([[migrations]] with tag = "vN"), as read by scripts/deploy/durable-object-migrations.ts.

Codebase Impact Analysis

.github/workflows/deploy-staging.yml (job queue), new scripts/deploy/staging-migration-guard.mjs with its test, scripts/quality/deploy-staging-permissions.test.ts. No production workflow and no app code.

Documentation & Specs

.claude/rules/13-staging-verification.md, .claude/commands/do.md, .agents/skills/do/SKILL.md, .agent-instructions/reference/rules/13-staging-verification-full.md.

Constitution & Risk Check

Principle X: the smallest check, at the point where the installation is chosen. Fail closed on an unreadable comparison. Risk: a branch that only reorders or renames tags would be refused on primary; that is acceptable and easy to diagnose from the message.

🤖 Generated with Claude Code

raphaeltm and others added 8 commits November 16, 2026 07:21
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Reservations are append-only deployment records written by a new queue
job outside any concurrency group, so no later dispatch can cancel a
waiting agent's place. Only a branch that holds the lease or whose turn
it is reaches the serialized lease job. At release or expiry the head of
the queue gets a configurable grace period, then the next entry; a
releasing holder queues behind existing waiters; reservations expire.
Records are version 2, which older workflow copies refuse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- An expired holder's reservation is no longer dropped, so it can take
  the slot again or queue behind waiters.
- A release enters the serialized group only when the branch holds the
  lease or is queued, and the lease job uses its own group
  (staging-lease-<installation>), so stray releases and older workflow
  copies cannot displace an entitled pending run.
- The lease job honours the turn the queue job saw (bounded to three
  grace periods), so a runner or group wait cannot cost the head its turn.
- History is read across up to five pages before failing closed.
- status parses string payloads for the recorded grace period.
- Docs: release only after your own deploy run finished.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A reservation made after the branch's old one expired goes to the back.
- A missed head that reserves again is placed by when it reserved, ahead
  of later reservations.
- When a waiter ahead leaves, every remaining entry keeps the end of the
  turn it was told (promisedEnd), so a sleeping agent cannot miss it.
- Snapshots keep recently missed entries for the admission slack, so a
  delayed lease job can still prove the turn it was let through on.
- Reservations record the grace period, so status is right before the
  first snapshot.
- Docs: release comes after every live check; rule 13 states the
  early-release turn window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A workflow copy from before the queue refuses any version-2 record, so a
reservation written over its live lease locks it out of renewing or
releasing. While the holder's record is version 1, the queue job writes
nothing and tells the caller to wake at the legacy expiry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…imary staging

Durable Object migrations only move forward, so a branch that deploys a
migration tag main lacks blocks every main-based branch on that
installation until it merges. deploy-staging.yml now refuses such deploys
on primary staging before a place is reserved, and tells the agent to use
staging2. The comparison reads apps/api/wrangler.toml on the branch and on
origin/main, and fails closed when it cannot read every tag. Documents the
clean-up duty: tear staging2 down and redeploy main before releasing if
the branch has not merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

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 37 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 64a264d and b58aa96.

Files selected for processing: 9

  • .agent-instructions/reference/rules/13-staging-verification-full.md
  • .agents/skills/do/SKILL.md
  • .claude/commands/do.md
  • .claude/rules/13-staging-verification.md
  • .github/workflows/deploy-staging.yml
  • scripts/deploy/staging-migration-guard.mjs
  • scripts/deploy/staging-migration-guard.test.mjs
  • scripts/quality/deploy-staging-permissions.test.ts
  • tasks/archive/2026-11-16-staging-lease-fair-queue.md
  • 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

… deploy condition

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@simple-agent-manager
simple-agent-manager Bot marked this pull request as ready for review November 16, 2026 16:20
@simple-agent-manager simple-agent-manager Bot added the coderabbit-review Trigger CodeRabbit review for opt-in PRs label Nov 16, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@raphaeltm

Copy link
Copy Markdown
Owner

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@sonarqubecloud

Copy link
Copy Markdown

@simple-agent-manager
simple-agent-manager Bot merged commit 10ec60b into main Nov 16, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

coderabbit-review Trigger CodeRabbit review for opt-in PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant