diff --git a/.agents/skills/github-prs/SKILL.md b/.agents/skills/github-prs/SKILL.md index a814933..29ea71d 100644 --- a/.agents/skills/github-prs/SKILL.md +++ b/.agents/skills/github-prs/SKILL.md @@ -60,8 +60,8 @@ make pr-merge PR=" [ ...]" # merges every green PR now, reports the make pr-merge PR= ARGS=--dry-run # says what it would do ``` -- **Green** means `local-ci` and the section attest the exact head, GitHub can merge it without conflicts, and the checks passed. It squash-merges with `--match-head-commit `, so only the attested head lands; behind `main` is fine. Where Actions cannot run (no CI job ran, or GitHub never started them), it merges on `local-ci` alone and says so. -- Anything else (draft, unattested, pending, failing, conflicting, stacked) is reported with its next step; the script never waits. Only a conflict needs rebase, push, attest. +- **Green** means `local-ci` and the section attest the exact head, GitHub can merge it without conflicts, and the checks passed. It squash-merges with `--match-head-commit `, so only the attested head lands. A head behind `main` is refused ("rebase onto origin/main and re-attest"): the merged tree must be the one you verified. Where Actions cannot run (no CI job ran, or GitHub never started them), it merges on `local-ci` alone and says so. +- Anything else (draft, unattested, pending, failing, conflicting, stacked) is reported with its next step; the script never waits. A head behind `main` or a conflict needs rebase, push, run what it needs, attest; after each merge in a burst, rebase the next PR. - **After each merge, check its deploy** (a follow-up, never a gate; merge the next PR now): it prints `make pr-merge ARGS="--check-deploy "`. Run it once "Deploy staging" has run for that commit. Failed or refused: top priority, fix forward. Cancelled by a newer main deploy: `make pr-merge ARGS=--check-staging` checks main's head. A dry run (`RAILWAY_DEPLOY` not `true`) deployed nothing. After a burst, `ARGS=--then-check-staging` follows main's deploy (bounded). - **Stacked children** are retargeted to `main` before their parent merges. Then `git rebase --onto origin/main `, `git push --force-with-lease`, attest. - Integration branches (`feat/`): `make pr-merge PR= ARGS=--any-base`. @@ -77,7 +77,7 @@ make pr-merge PR= ARGS=--dry-run # says what it would do ## 7. Staging deploys itself -`main` runs no CI. "Deploy staging" deploys a push to Railway staging only when `make ci-local` attested it (`cilocal attest`): the squash of an attested PR head, or a commit with its own `local-ci`. Two verified PRs merged together are not themselves verified; a clash surfaces on staging and is fixed forward. The hourly "Staging catch-up" deploys `main` whenever staging is behind and `main` is attested. Never wait for a deploy or for another PR to settle: merge each PR when its own head is attested and conflict-free, and stack the next at once; check its deploy afterwards (section 5); "Post-merge report" comments on the PR when a push workflow on its commit fails. A failed deploy is fixed forward in parallel and blocks no unrelated merge. +`main` runs no CI. "Deploy staging" deploys a push to Railway staging only when `make ci-local` attested it (`cilocal attest`): the squash of an attested PR head, or a commit with its own `local-ci`. `make pr-merge` refuses a head behind `main`, so every squash is a tree someone verified. The hourly "Staging catch-up" deploys `main` whenever staging is behind and `main` is attested. Never wait for a deploy or for another PR to settle: merge each PR when its own head is attested and conflict-free, and stack the next at once; check its deploy afterwards (section 5); "Post-merge report" comments on the PR when a push workflow on its commit fails. A failed deploy is fixed forward in parallel and blocks no unrelated merge. An unattested `main` is the top priority: both workflows fail and say why. Fix it forward: `git worktree add --detach `, run what the commits since the last attested one need (`make ci` when there is none, as in a new project's first attestation), then `make ci-local COMMIT= RAN="…"` there. It refuses a dirty tree, a commit off `origin/main` or one that already failed, and posts `local-ci` on the commit; then dispatch "Deploy staging". diff --git a/.claude/skills/github-prs/SKILL.md b/.claude/skills/github-prs/SKILL.md index a814933..29ea71d 100644 --- a/.claude/skills/github-prs/SKILL.md +++ b/.claude/skills/github-prs/SKILL.md @@ -60,8 +60,8 @@ make pr-merge PR=" [ ...]" # merges every green PR now, reports the make pr-merge PR= ARGS=--dry-run # says what it would do ``` -- **Green** means `local-ci` and the section attest the exact head, GitHub can merge it without conflicts, and the checks passed. It squash-merges with `--match-head-commit `, so only the attested head lands; behind `main` is fine. Where Actions cannot run (no CI job ran, or GitHub never started them), it merges on `local-ci` alone and says so. -- Anything else (draft, unattested, pending, failing, conflicting, stacked) is reported with its next step; the script never waits. Only a conflict needs rebase, push, attest. +- **Green** means `local-ci` and the section attest the exact head, GitHub can merge it without conflicts, and the checks passed. It squash-merges with `--match-head-commit `, so only the attested head lands. A head behind `main` is refused ("rebase onto origin/main and re-attest"): the merged tree must be the one you verified. Where Actions cannot run (no CI job ran, or GitHub never started them), it merges on `local-ci` alone and says so. +- Anything else (draft, unattested, pending, failing, conflicting, stacked) is reported with its next step; the script never waits. A head behind `main` or a conflict needs rebase, push, run what it needs, attest; after each merge in a burst, rebase the next PR. - **After each merge, check its deploy** (a follow-up, never a gate; merge the next PR now): it prints `make pr-merge ARGS="--check-deploy "`. Run it once "Deploy staging" has run for that commit. Failed or refused: top priority, fix forward. Cancelled by a newer main deploy: `make pr-merge ARGS=--check-staging` checks main's head. A dry run (`RAILWAY_DEPLOY` not `true`) deployed nothing. After a burst, `ARGS=--then-check-staging` follows main's deploy (bounded). - **Stacked children** are retargeted to `main` before their parent merges. Then `git rebase --onto origin/main `, `git push --force-with-lease`, attest. - Integration branches (`feat/`): `make pr-merge PR= ARGS=--any-base`. @@ -77,7 +77,7 @@ make pr-merge PR= ARGS=--dry-run # says what it would do ## 7. Staging deploys itself -`main` runs no CI. "Deploy staging" deploys a push to Railway staging only when `make ci-local` attested it (`cilocal attest`): the squash of an attested PR head, or a commit with its own `local-ci`. Two verified PRs merged together are not themselves verified; a clash surfaces on staging and is fixed forward. The hourly "Staging catch-up" deploys `main` whenever staging is behind and `main` is attested. Never wait for a deploy or for another PR to settle: merge each PR when its own head is attested and conflict-free, and stack the next at once; check its deploy afterwards (section 5); "Post-merge report" comments on the PR when a push workflow on its commit fails. A failed deploy is fixed forward in parallel and blocks no unrelated merge. +`main` runs no CI. "Deploy staging" deploys a push to Railway staging only when `make ci-local` attested it (`cilocal attest`): the squash of an attested PR head, or a commit with its own `local-ci`. `make pr-merge` refuses a head behind `main`, so every squash is a tree someone verified. The hourly "Staging catch-up" deploys `main` whenever staging is behind and `main` is attested. Never wait for a deploy or for another PR to settle: merge each PR when its own head is attested and conflict-free, and stack the next at once; check its deploy afterwards (section 5); "Post-merge report" comments on the PR when a push workflow on its commit fails. A failed deploy is fixed forward in parallel and blocks no unrelated merge. An unattested `main` is the top priority: both workflows fail and say why. Fix it forward: `git worktree add --detach `, run what the commits since the last attested one need (`make ci` when there is none, as in a new project's first attestation), then `make ci-local COMMIT= RAN="…"` there. It refuses a dirty tree, a commit off `origin/main` or one that already failed, and posts `local-ci` on the commit; then dispatch "Deploy staging". diff --git a/docs/CI.md b/docs/CI.md index fa198bb..4f785e6 100644 --- a/docs/CI.md +++ b/docs/CI.md @@ -78,20 +78,20 @@ It runs from cron on the dev box (the machine with Docker, `gh` and the `CI_RUNN ## Required checks and staging -The main ruleset should require `Secret scan` and `Local verification` from GitHub Actions on every pull request, and nothing else from CI. A pull request can add a workflow of its own whose job takes a required check's name; requiring the `local-ci` status as well closes that, because only someone with write access can post it, and `make pr-merge`, which runs on the merger's machine, makes the same checks itself. It need not require a head to be up to date with its base, and neither does `make pr-merge`: only a conflict stops it. A repository whose Actions cannot run (billing, or turned off) requires the `local-ci` status itself instead ([below](#when-actions-cannot-run)). +The main ruleset should require `Secret scan` and `Local verification` from GitHub Actions on every pull request, and nothing else from CI. A pull request can add a workflow of its own whose job takes a required check's name; requiring the `local-ci` status as well closes that, because only someone with write access can post it, and `make pr-merge`, which runs on the merger's machine, makes the same checks itself. It need not require a head to be up to date with its base: `make pr-merge` refuses one that is behind (GitHub's compare), so the merged tree is the verified one. A repository whose Actions cannot run (billing, or turned off) requires the `local-ci` status itself instead ([below](#when-actions-cannot-run)). **Staging deploys only what `make ci-local` attested.** Railway's GitHub auto-deploy is off (`repo: ""` in `deploy/platform.yaml`): `Deploy staging` runs on every push to `main`, and its plan job (`cilocal attest `) lets the rollout start only when either: - the pushed commit itself has a successful `local-ci` status (`make ci-local COMMIT= RAN="…"` in a clean worktree of it posts it), or - the pull request whose squash made it (by GitHub's commit-to-PR index, or its `(#NNN)` squash title) passed local verification on the merged head. -A direct push, or the squash of an unattested head, deploys nothing, and the hourly `Staging catch-up` keeps refusing it until it is attested. Production takes only a release staging already ran ([RELEASES.md](RELEASES.md#what-is-gated)). **The merged tree of two verified PRs is not itself verified**: a head behind its base squashes into a tree nobody ran; a clash surfaces on staging and is fixed forward. An unattested `main` is the top priority: `git worktree add --detach `, run what the commits since the last attested one need (`make ci` when there is none, as in a [new project](#local-verification)), then `make ci-local COMMIT= RAN="…"` there; then dispatch `Deploy staging` (or wait for the catch-up). +A direct push, or the squash of an unattested head, deploys nothing, and the hourly `Staging catch-up` keeps refusing it until it is attested. Production takes only a release staging already ran ([RELEASES.md](RELEASES.md#what-is-gated)). **Only a verified tree merges**: `make pr-merge` refuses a head behind its base, because GitHub would squash it into a tree nobody built (the base's newer commits plus the PR). Two green PRs broke a downstream `main` that way: one removed a function the other still called. Rebase onto the base, run what the change needs and attest again; a burst of PRs therefore lands one after another, each rebased onto the last. An unattested `main` is the top priority: `git worktree add --detach `, run what the commits since the last attested one need (`make ci` when there is none, as in a [new project](#local-verification)), then `make ci-local COMMIT= RAN="…"` there; then dispatch `Deploy staging` (or wait for the catch-up). ## Merging on local verification -Pull requests open ready and stack. Each merges the moment it is attested, conflict-free and green, with `make pr-merge PR="..."` (`cilocal merge`; the skill [github-prs](../.claude/skills/github-prs/SKILL.md)), which squashes exactly the attested head with `--match-head-commit` and never waits. Per PR it decides (`verify.Decide`): merged is done; closed, draft (`gh pr ready `), stacked on another branch (unless `ARGS=--any-base`), conflicting, not attested, a failed check, a hollow green (`Local verification` passed but `Secret scan` skipped) or a pending one (including `Local verification` not reported yet) is skipped with its next step, and the others still merge. Only the newest run of each workflow, and the newest attempt of each job, counts. Before merging it retargets the open PRs stacked on the branch to `main` and prints the `git rebase --onto` for each. `ARGS=--dry-run` says what it would do. +Pull requests open ready and stack. Each merges the moment it is attested, conflict-free and green, with `make pr-merge PR="..."` (`cilocal merge`; the skill [github-prs](../.claude/skills/github-prs/SKILL.md)), which squashes exactly the attested head with `--match-head-commit` and never waits. Per PR it decides (`verify.Decide`): merged is done; closed, draft (`gh pr ready `), stacked on another branch (unless `ARGS=--any-base`), conflicting, behind its base, not attested, a failed check, a hollow green (`Local verification` passed but `Secret scan` skipped) or a pending one (including `Local verification` not reported yet) is skipped with its next step, and the others still merge. Only the newest run of each workflow, and the newest attempt of each job, counts. Before merging it retargets the open PRs stacked on the branch to `main` and prints the `git rebase --onto` for each. `ARGS=--dry-run` says what it would do. -- **Never wait** on another PR, its verification or a staging deploy. Behind `main` is fine; a conflict means rebase, push and attest again. +- **Never wait** on another PR, its verification or a staging deploy. A head behind `main`, or one that conflicts, is refused with "rebase onto origin/main and re-attest": rebase, push, run what it needs, attest. `make pr-merge` reads the base once more right before the merge, so only GitHub's own instant is left for a race. - **After the merge, check its deploy.** `make pr-merge` prints the squash commit and `make pr-merge ARGS="--check-deploy "`. Once `Deploy staging` has run for it, run that once: one read of that commit's `Deploy staging` runs and of `/healthz` (200 with `data.status` `ok`; `STAGING_WEB_URL` from the env, else the repository variable). A failed or refused deploy is the top priority, fixed forward; one cancelled by a newer `main` commit is checked on main's head (`ARGS=--check-staging`); a dry run (`RAILWAY_DEPLOY` is not `true`) says nothing was deployed. It is a follow-up, not a gate: keep merging. `ARGS=--then-check-staging` follows main's deploy (bounded) after a burst. `Post-merge report` comments on the PR when its deploy fails. - **Ruleset first.** `make pr-merge` calls `gh pr merge`, so the ruleset (required reviews, required checks) still applies on top of it. Never `--delete-branch`: it closes the PRs stacked on the branch. diff --git a/scripts/cilocal/github.go b/scripts/cilocal/github.go index 017efc3..1f6f9bd 100644 --- a/scripts/cilocal/github.go +++ b/scripts/cilocal/github.go @@ -66,6 +66,21 @@ func (hub github) api(ctx context.Context, path string, into any) error { return nil } +// behind is how many commits of the branch `base` the commit `head` lacks +// (GitHub's compare): 0 when the head already contains the base. +func (hub github) behind(ctx context.Context, base, head string) (int, error) { + var compare struct { + BehindBy *int `json:"behind_by"` + } + if err := hub.api(ctx, "compare/"+base+"..."+head, &compare); err != nil { + return 0, err + } + if compare.BehindBy == nil { + return 0, fmt.Errorf("gh api compare/%s...%s: no behind_by", base, head) + } + return *compare.BehindBy, nil +} + // statuses is every status ever posted on `sha`, newest first. func (hub github) statuses(ctx context.Context, sha string) ([]verify.Status, error) { var statuses []verify.Status diff --git a/scripts/cilocal/merge.go b/scripts/cilocal/merge.go index 06061b3..59af7de 100644 --- a/scripts/cilocal/merge.go +++ b/scripts/cilocal/merge.go @@ -21,7 +21,8 @@ import ( // (.claude/skills/github-prs). It never waits: a PR that is a draft, not // attested on its head, failing, pending or conflicting is reported with the // next step and left alone (verify.Decide), so one stuck PR never holds up -// the others; a head behind main merges unless it conflicts. Before merging a +// the others. A head behind its base is refused (rebase and re-attest), so +// what merges is exactly the tree that was verified. Before merging a // PR it retargets to main every open PR stacked on its branch, so the stack // survives. // @@ -137,9 +138,15 @@ func mergeOne(ctx context.Context, hub github, number int, opts mergeFlags) (ver if err != nil { return verify.Verdict{}, err } + behind := 0 + if pull.State == "OPEN" { + if behind, err = hub.behind(ctx, pull.BaseRefName, pull.HeadRefOid); err != nil { + return verify.Verdict{}, err + } + } verdict := verify.Decide(verify.Pull{ Number: number, State: pull.State, Draft: pull.IsDraft, Base: pull.BaseRefName, Mergeable: pull.Mergeable, - Checks: pull.Checks, Verification: verification, + Behind: behind, Checks: pull.Checks, Verification: verification, }, opts.options) if verdict.Action != verify.ActionMerge { return verdict, nil @@ -151,6 +158,16 @@ func mergeOne(ctx context.Context, hub github, number int, opts mergeFlags) (ver if opts.dryRun { return verify.Verdict{Action: verify.ActionMerge, Reason: "would merge " + verify.Short(pull.HeadRefOid) + " (dry run)", Next: "", Note: ""}, nil } + // The base may have moved since it was read: read it once more right + // before the merge, which leaves only GitHub's own instant for a race. + if behind, err := hub.behind(ctx, pull.BaseRefName, pull.HeadRefOid); err != nil { + return verify.Verdict{}, err + } else if behind > 0 { + return verify.Decide(verify.Pull{ + Number: number, State: pull.State, Draft: pull.IsDraft, Base: pull.BaseRefName, Mergeable: pull.Mergeable, + Behind: behind, Checks: pull.Checks, Verification: verification, + }, opts.options), nil + } // Squash exactly the head that was attested; never --delete-branch (it // closes the PRs stacked on the branch). if _, err := output(ctx, "", "gh", "pr", "merge", strconv.Itoa(number), "-R", hub.repo, "--squash", "--match-head-commit", pull.HeadRefOid); err != nil { diff --git a/scripts/cilocal/verify/merge.go b/scripts/cilocal/verify/merge.go index b4c8400..58fba1d 100644 --- a/scripts/cilocal/verify/merge.go +++ b/scripts/cilocal/verify/merge.go @@ -10,8 +10,9 @@ import ( // The merge decision (make pr-merge, docs/CI.md "Merging on local // verification"): each listed pull request merges now or is reported with // its next step. Nothing waits: a draft, unattested, failing, pending or -// conflicting PR is left alone so it never holds up the others, and a head -// behind main merges unless it conflicts. +// conflicting PR is left alone so it never holds up the others. A head +// behind its base is refused: GitHub would squash it into a tree (the base +// plus the PR) that nobody built, which is how two green PRs broke main. // Main is the branch pull requests land on (scripts/branches.sh). const Main = "main" @@ -197,7 +198,11 @@ type Pull struct { Draft bool Base string Mergeable string // MERGEABLE, CONFLICTING, UNKNOWN - Checks []Check + // Behind is how many commits of the base branch the head lacks + // (GitHub's compare): 0 when the head contains the base, so the tree + // that was verified is exactly the tree the merge makes. + Behind int + Checks []Check Verification } @@ -244,9 +249,13 @@ func Decide(pull Pull, options MergeOptions) Verdict { return skip("is stacked on "+pull.Base, fmt.Sprintf("merge its parent first (make pr-merge retargets it to %s), or gh pr edit %d --base %s", Main, pull.Number, Main)) case pull.Mergeable == "CONFLICTING": - // A head behind main still merges when GitHub can apply it cleanly: - // only a conflict stops it. return skip("conflicts with "+pull.Base, fmt.Sprintf("git rebase origin/%s, push, %s", pull.Base, NextStep)) + case pull.Behind > 0: + // Merging it would squash a tree nobody built: the base's newer + // commits plus this PR. Two green PRs broke main this way (one + // removed what the other still called). + return skip(fmt.Sprintf("is %d commit(s) behind %s: the merged tree (%s plus this PR) was never built or tested", pull.Behind, pull.Base, pull.Base), + fmt.Sprintf("rebase onto origin/%s and re-attest: git rebase origin/%s, push, %s", pull.Base, pull.Base, NextStep)) } if problems := LocalVerificationProblems(pull.Verification); len(problems) > 0 { return skip("is not verified locally: "+strings.Join(problems, "; "), NextStep) diff --git a/scripts/cilocal/verify/merge_test.go b/scripts/cilocal/verify/merge_test.go index fd926f4..a1d806a 100644 --- a/scripts/cilocal/verify/merge_test.go +++ b/scripts/cilocal/verify/merge_test.go @@ -95,6 +95,7 @@ func TestDecide(test *testing.T) { {"stacked", func(pull verify.Pull) verify.Pull { pull.Base = "feat/a"; return pull }, verify.MergeOptions{}, verify.ActionSkip, "is stacked on feat/a", "gh pr edit 12 --base main"}, {"stacked, any base", func(pull verify.Pull) verify.Pull { pull.Base = "feat/a"; return pull }, verify.MergeOptions{AnyBase: true}, verify.ActionMerge, "green", ""}, {"conflicting", func(pull verify.Pull) verify.Pull { pull.Mergeable = "CONFLICTING"; return pull }, verify.MergeOptions{}, verify.ActionSkip, "conflicts with main", "git rebase origin/main"}, + {"behind its base: the merged tree was never built", func(pull verify.Pull) verify.Pull { pull.Behind = 1; return pull }, verify.MergeOptions{}, verify.ActionSkip, "1 commit(s) behind main", "rebase onto origin/main and re-attest"}, {"unattested", func(pull verify.Pull) verify.Pull { pull.Verification = unattested; return pull }, verify.MergeOptions{}, verify.ActionSkip, "is not verified locally: no local-ci status", "make ci-local RAN="}, {"failed check", func(pull verify.Pull) verify.Pull { pull.Checks = append(green(), ciCheck(verify.AlwaysRuns, "206", "COMPLETED", "FAILURE"))