Skip to content

fix(deploy): automatic production gate deploys the current main tip - #2777

Merged
simple-agent-manager[bot] merged 3 commits into
mainfrom
sam/close-gap-automatic-production-qaqp1h
Nov 16, 2026
Merged

simple-agent-manager[bot] merged 3 commits into
mainfrom
sam/close-gap-automatic-production-qaqp1h

Conversation

@simple-agent-manager

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

Copy link
Copy Markdown
Contributor

Summary

Closes the automatic production deploy queue gap left by #2763 (idea 01M7FYGMEESYH3AJZJ0CT3NKCZ, "Known limit").

Problem. Since #2763 main CI runs overlap and can finish out of order. deploy.yml puts every trusted candidate in the single deploy-production concurrency group (cancel-in-progress: false), and GitHub keeps one pending run per group. If deploy W is running, the tip Y's CI finishes (Y's run goes pending), then an older commit X's CI finishes (X's run replaces Y's), X ran after W, failed closed at the tip check, and nothing deployed Y until the next merge or a manual dispatch.

Fix (design A). On the automatic path the triggering SHA is only a wake-up signal. After the queue, validateAutomaticProductionDispatch (scripts/deploy/validate-production-dispatch.ts):

  1. reads the current main tip;
  2. lists ci.yml runs for that exact SHA (event=push, branch=main) and requires one accepted by the existing selectSuccessfulCiRun (name CI, push, main, this repository, success);
    • none yet (pending, failed, cancelled) → ends green without deploying. The tip's own CI completion queues a later run, and the last-queued run in the group is never displaced, so the green tip is always reached;
  3. reads the production deployment markers that mark-production-deployment writes (same rule as release.yml: newest deploy.yml, non-dry-run marker whose latest status is success);
    • marker already equals the tip → ends green without deploying (no double deploy, covering "the deploy that just finished");
  4. otherwise outputs deploy_sha=<tip>.

Any GitHub API error or malformed response throws, so the run fails closed before production mutation. deploy.yml now also skips the deploy job when deploy_sha is empty, because deploy-reusable.yml would otherwise fall back to github.sha.

Why not re-dispatch (design B). Re-dispatching deploy.yml for the tip when a stale candidate is detected needs actions: write on the token, starts a second run that goes through the same queue (where it can itself be displaced), and spreads the decision over two runs. Design A keeps one run, one decision, and adds only read-only deployments: read on the single validate-automatic-dispatch job, used to read the markers. The marker read is required for the "already deployed" check; nobody without write access can create a deployment, and a forged marker could only cause a skip, never a deploy.

Invariants kept. Deploys stay serialized (concurrency unchanged). The deployed SHA is always the trusted main tip read after the queue, with a successful CI push run on main in this repository on that exact SHA. Fail closed on API error or ambiguity. The manual workflow_dispatch path and its audited emergency override are untouched.

Behaviour change worth knowing: if the tip's deploy failed (no marker), a later out-of-order CI completion retries the tip's deploy. Deploys are idempotent, and previously a failed tip stayed undeployed until the next merge.

Also: a run triggered by the tip's own successful CI that cannot find that run (API/filter inconsistency) fails red rather than skipping, since no later event would retry it.

Docs: the ci.yml concurrency comment, /do Phase 7b, and architecture/overview.md / architecture/security.md now describe the tip-selecting gate. The Codex do skill had no copy of the recovery wording; CLAUDE.md did not describe the old behaviour.

Validation

  • pnpm lint (eslint on changed files: new test clean; validator/old test errors are pre-existing on main)
  • pnpm typecheck (npx tsc --project scripts/deploy/tsconfig.json --noEmit, the CI command for these scripts)
  • pnpm test — pnpm quality:scripts:test: 70 files, 1151 tests passed
  • Additional validation: mutation checks (below)
  • N/A: no sweep/cron/alarm candidate selection change

New scripts/quality/automatic-production-deploy-queue.test.ts models the deploy-production group (one running, one pending, newer queued run replaces pending) and drives the real validator against a stubbed GitHub API, asserting at every start that anything deployed is the tip with its own green CI:

  • the reported sequence (W running, Y pending replaced by older X) → W then Y deployed;
  • tip CI pending → older run deploys nothing; tip's own completion deploys it;
  • tip already deployed / tip's run pending behind its own deploy → no double deploy;
  • tip CI failed with a green older commit (control) → nothing deployed;
  • green runs for other SHAs, PR/fork/feature/other-workflow runs for the tip → not accepted;
  • dry-run, foreign and non-success markers ignored;
  • tip itself triggered the run but no green run visible → reject (red), not a silent skip;
  • marker found on page 2 of deployments; untrusted next-page URL rejected;
  • 500 from the ref, CI-runs, deployments and statuses APIs, malformed responses, malformed triggering SHA → reject, no deploy_sha written.

Mutation checks (each reverted afterwards): reinstating the old "trigger must equal tip" check reddened 12/16, including the reported sequence; removing the already-deployed check reddened the 2 double-deploy tests; removing the tip-CI gate reddened 5 (pending, failed control, non-tip green, untrusted runs, decision). deploy-safety.test.ts now asserts the deploy_sha != '' guard on the deploy job and the job's exact permissions.

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

  • Staging deployment green — N/A: no staging deploy exercises deploy.yml's workflow_run production path
  • Live app verified via Playwright — N/A: no app code changed
  • Existing workflows confirmed working — N/A: no app code changed
  • New feature/fix verified on staging — N/A: verified by tests above plus watching real production deploys after merge
  • Infrastructure verification completed — N/A: no infra changes (no cloud-init, VM agent, DNS, TLS or provisioning; scripts/deploy change is the production gate only)
  • Mobile and desktop verification notes added for UI changes — N/A: no UI

Staging Verification Evidence

N/A: this changes only the production workflow_run deploy gate, which deploy-staging.yml does not run. The dispatcher stated that the proof is the tests plus watching real production deploys after merge; post-merge evidence will be added to idea 01M7FYGMEESYH3AJZJ0CT3NKCZ.

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

CI push on main completes → deploy.yml on.workflow_run → concurrency group deploy-production (queue) → job validate-automatic-dispatch → validate-production-dispatch.ts:validateAutomaticProductionDispatch() → getCurrentMainTip() → listCiRuns() + selectSuccessfulCiRun() → getLatestProductionDeployedSha() → $GITHUB_OUTPUT deploy_sha → job deploy (if … outputs.deploy_sha != '') → deploy-reusable.yml with target_commit_sha → mark-production-deployment writes the marker read by the next run.

Untested Gaps

GitHub's real concurrency queue and API are modelled, not exercised. After merge I will watch the production Deploy Production runs (including, if two merges land close together, that the tip is deployed) and record the evidence on the idea.

Post-Mortem (Required for bug fix PRs)

What broke

A merged change could sit undeployed after main CI runs finished out of order: the tip's pending deploy run was replaced by an older commit's run, which failed closed.

Root cause

#2763 made main CI runs overlap (one concurrency group per commit) while deploy.yml still validated the triggering run's SHA, which only matched the tip when completion order matched commit order.

Class of bug

A queued job that validates its trigger instead of the current state, under a queue that drops intermediate entries.

Why it wasn't caught

Known and documented by the #2763 author as a limit, with /do papering over it by manual recovery.

Process fix included in this PR

.claude/commands/do.md Phase 7b no longer asks agents to recover by hand; ci.yml comment updated; queue-simulation tests guard the sequence.

Post-mortem file

Idea 01M7FYGMEESYH3AJZJ0CT3NKCZ (note added with design and evidence).

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
security-auditor ADDRESSED 0 CRITICAL/HIGH/MEDIUM, 4 LOW. LOW 1 (tip-triggered run with no visible green CI skipped silently) fixed in a4e3591: now fails red. LOW 2 (marker forgeable by write-access holders, effect is skip only) partly addressed: skip records the marker id; creator filter not added since a workflow token could still forge. LOW 3 (deploy-reusable.yml github.sha fallback) guarded in deploy.yml; fail-fast in the reusable workflow requested from its owner (PR #2770) on feature.ship-faster, since this task must not edit it. LOW 4 hardening accepted as-is (manual-path selectSuccessfulCiRun kept unchanged by constraint).
task-completion-validator ADDRESSED WARN: HIGH stale architecture/overview.md sentence fixed in a4e3591 (and security.md aligned); task file checkboxes ticked; deployments-pagination test and retry-on-failed-deploy note added.

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 CodeRabbit feedback is unresolved, or it did not review and the observed outcome is recorded below

CodeRabbit Notes

Requested at 03:00Z by applying coderabbit-review when the PR was marked ready; the trusted workflow posted the review command and CodeRabbit replied "Review triggered" at 03:00:39Z. Its check on head a4e3591 concluded skipped with "Review skipped: automatic reviews are disabled", and no review or review comment arrived by 04:28Z (~87 min). Outcome: no CodeRabbit review; merging on the remaining gates per rule 25.

Exceptions (If any)

  • Scope: task file committed on the feature branch rather than pushed directly to main.
  • Rationale: the SAM output branch was pre-created; keeps all changes in one reviewed PR.
  • Expiration: this PR.

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

GitHub REST API (official docs): GET /repos/{owner}/{repo}/git/ref/heads/main, GET /repos/{owner}/{repo}/actions/workflows/{id}/runs (head_sha, branch, event filters), GET /repos/{owner}/{repo}/deployments?environment= (newest first, needs deployments: read), GET …/deployments/{id}/statuses. GitHub Actions concurrency docs: one running and one pending run per group; a newly queued run replaces the pending one. Existing marker-reading logic in .github/workflows/release.yml reused as the rule.

Codebase Impact Analysis

.github/workflows/deploy.yml (automatic validate job, deploy job if), scripts/deploy/validate-production-dispatch.ts (automatic path; manual path unchanged apart from shared request helpers), scripts/quality/* tests, .github/workflows/ci.yml comment, .claude/commands/do.md Phase 7b. deploy-reusable.yml untouched (owned by PR #2770).

Documentation & Specs

.claude/commands/do.md, ci.yml comment, apps/www/src/content/docs/docs/architecture/overview.md, apps/www/src/content/docs/docs/architecture/security.md.

Constitution & Risk Check

Principle XI: no new tunables, URLs derived from GITHUB_REPOSITORY. Principle X (simplicity): one run, one decision, read-only permission instead of actions: write. Risk: a skipped deploy job if the marker lookup misreads; covered by tests and fails toward deploying (no marker ⇒ deploy), never toward deploying a non-tip SHA.

🤖 Generated with Claude Code

Overlapping main CI runs finish out of order and GitHub keeps one pending run
in the deploy-production group, so an older commit's deploy run could replace
the tip's pending run and then fail closed, leaving the tip undeployed until
the next merge. The automatic gate now treats the triggering SHA as a wake-up
signal only: after the queue it reads the current main tip and deploys it only
when the tip has its own successful CI push run on main, skipping when the
tip's CI is not green yet (its own completion triggers a later run) or when the
latest production marker already records the tip. API errors fail closed.

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

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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

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 03:00
@simple-agent-manager simple-agent-manager Bot added the coderabbit-review Trigger CodeRabbit review for opt-in PRs label Nov 16, 2026
@raphaeltm

Copy link
Copy Markdown
Owner

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Action not completed

Head commit changed.

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.

Address review: a run triggered by the tip's own successful CI that cannot find
that run now fails closed instead of skipping silently; the already-deployed
skip records the marker deployment id; deployment pagination is tested; the
architecture overview and security docs describe the tip-selecting gate.

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

Copy link
Copy Markdown

@simple-agent-manager
simple-agent-manager Bot merged commit cc79450 into main Nov 16, 2026
45 of 46 checks passed
@simple-agent-manager
simple-agent-manager Bot deleted the sam/close-gap-automatic-production-qaqp1h branch November 16, 2026 04:29
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