From eaf949839c18e474fc14b341269ab45e8aed6ee9 Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Wed, 25 Nov 2026 01:07:24 -0800 Subject: [PATCH 1/3] Keep a slot for the pull requests a red candidate's first round clears With `spare` set, while a red candidate of several pull requests on the trunk has its first round running, the chain takes one slot fewer, so those the round clears are pushed in the tick it reads. The round's read ends the hold whatever it says, as does the red candidate going; a round that never reports reads as lost after the timeout. The same read ends the wait for probes beside a candidate stacked on the red one. Off unless set. Nothing that decides who is a suspect or who leaves changes: only fill's slot count and the stacked candidate's wait. --- README.md | 2 +- doc/design.md | 4 +- eval/check_test.go | 14 ++ internal/queue/plan.go | 18 +- internal/queue/plan_test.go | 436 ++++++++++++++++++++++++++++++++++++ internal/queue/world.go | 4 + internal/run/run.go | 3 +- 7 files changed, 473 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 31158c4..0c18f27 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ A pull request joins the queue when it's labelled `merge-queue` and leaves when } ``` -`admit` names the statuses or check runs a pull request's own head must pass before it joins the queue, and `threads` keeps one out while a review thread is unresolved (the queue reads its first hundred, and one with more waits). `slots` is how many candidates build at once, and `max_batch` how many pull requests each adds. Where builds cost more than a short wait does, `gather` holds a candidate back while another is building until it would add that many. The builds that find a failure's cause run beside them: three for each pull request the red build added, all at once where `started` or `begins` is set. Set `started` to a word your CI's pending status carries once a build is running (Buildkite's says "started"), so the queue can find a failure's cause in one build time, not three; check runs need no setting. Where the context that judges a build appears only at its end, set `begins` to a context that answers once your CI has queued the build's jobs, such as a pipeline's first step. Where your CI reports as a GitHub App's check runs, set `app` to that App's database ID (`"app": 15368`): every context but the queue's own status is then read only from its check runs, and a commit status or another App's check run answers nothing. The queue's own status is always a commit status, and no build, probe, admit or begins context may share its name. Set `cancels` where your CI stops a build whose branch is deleted; otherwise a build the queue no longer wants keeps its slot until it reports. `run.Config` in `internal/run/run.go` lists every setting. +`admit` names the statuses or check runs a pull request's own head must pass before it joins the queue, and `threads` keeps one out while a review thread is unresolved (the queue reads its first hundred, and one with more waits). `slots` is how many candidates build at once, and `max_batch` how many pull requests each adds. Where builds cost more than a short wait does, `gather` holds a candidate back while another is building until it would add that many. The builds that find a failure's cause run beside them: three for each pull request the red build added, all at once where `started` or `begins` is set. Set `started` to a word your CI's pending status carries once a build is running (Buildkite's says "started"), so the queue can find a failure's cause in one build time, not three; check runs need no setting. Where the context that judges a build appears only at its end, set `begins` to a context that answers once your CI has queued the build's jobs, such as a pipeline's first step. Where your CI reports as a GitHub App's check runs, set `app` to that App's database ID (`"app": 15368`): every context but the queue's own status is then read only from its check runs, and a commit status or another App's check run answers nothing. The queue's own status is always a commit status, and no build, probe, admit or begins context may share its name. Set `cancels` where your CI stops a build whose branch is deleted; otherwise a build the queue no longer wants keeps its slot until it reports. Set `spare` to keep one slot free while a failed candidate of several pull requests has its first round of builds, for the pull requests that round clears, and to build those beside a candidate stacked on the failed one rather than after it. `run.Config` in `internal/run/run.go` lists every setting. Set `keep` (for example, `"keep": "3m"`) to leave a landed candidate's branch standing for at least that long after the queue marks it landed. The candidate at the trunk's tip is marked at the landing, or by the next read if that tick died first. A candidate beneath it is marked once its verdict is in. One failed or errored context is a verdict, so another still running, or one that appears later, does not hold the branch longer. Where `cancels` is set, a lower candidate still running is deleted at once, as without `keep`. A retry the trunk holds is a candidate like any other. A probe, a check and a round's build are never marked. A second mark from ticks run beside each other only lengthens the stand; ticks must run one at a time. Without `keep`, the landing push has the remote compare and delete the candidate's branch, so whatever deletes that branch first stops the landing. With `keep`, the queue compares the branch just before pushing the trunk alone, so deleting the branch between that comparison and the push does not stop the landing. diff --git a/doc/design.md b/doc/design.md index 1c0eca8..d5f7d3b 100644 --- a/doc/design.md +++ b/doc/design.md @@ -32,7 +32,7 @@ The queue uses its own mark because the trunk's own builds report on the same co ## Blame -A red candidate at the bottom of the chain is set aside with the pull requests it added, and the chain goes on without them, built on the tip. A candidate stacked on the red one is dropped once it fails too. Until then it may yet land them all. Where rounds are probes, which answer in a fraction of a build, the chain waits for that answer. Where they are whole builds it does not wait: it is built beside the candidates still running. +A red candidate at the bottom of the chain is set aside with the pull requests it added, and the chain goes on without them, built on the tip. A candidate stacked on the red one is dropped once it fails too. Until then it may yet land them all. Where rounds are probes, which answer in a fraction of a build, the chain waits for that answer. Where they are whole builds it does not wait: it is built beside the candidates still running. With `spare`, the wait lasts only until the red candidate's first round has read, and the pull requests that round clears are built beside the stacked candidate. Should the red have been a flake, that is a whole build more, and where the stacked candidate is the slower, the cleared ones land first and it is thrown away. The red candidate is narrowed by rounds: builds of some of its prefixes. A round's build never lands. What the rounds have said so far is a span, from the highest prefix that passed to the lowest above it that failed. The culprit is in it. A round is read once every build in it has reported, and the rounds are read in order. @@ -116,7 +116,7 @@ The queue posts `pending` on everything it pushes, which gives each wait a start ## Dials -`slots` is how many candidates are under test at once. A round's builds are beside them and are not counted. `max_batch` is how many pull requests a candidate adds, and `gather` how few it may add while another is building: fewer builds for each landing, for a wait of at most one build. `eval/main.go` holds the values each shape ships with, and the hillclimb moves them. +`slots` is how many candidates are under test at once. A round's builds are beside them and are not counted. With `spare`, while a red candidate of several pull requests on the trunk has its first round running, the chain takes one slot fewer, so those the round clears find one free as it reads. The round's read ends it, whatever it says, as does the red candidate going; a round that never reports reads as lost after `timeout`. `max_batch` is how many pull requests a candidate adds, and `gather` how few it may add while another is building: fewer builds for each landing, for a wait of at most one build. `eval/main.go` holds the values each shape ships with, and the hillclimb moves them. ## Not in the engine diff --git a/eval/check_test.go b/eval/check_test.go index be2af89..01312f3 100644 --- a/eval/check_test.go +++ b/eval/check_test.go @@ -246,6 +246,20 @@ func TestCheckerCurrentPlanner(t *testing.T) { } } +func TestCheckerCurrentPlannerWithSpare(t *testing.T) { + worlds := defaultWorlds() + for i := range worlds { + worlds[i].Config.Spare = true + } + sum := checkSuite(worlds, mutant{plan: queue.Plan, verify: queue.Verify}) + t.Logf("queue.Plan with spare: %d of %d whole, %d capped, %d states, %d broken", + sum.whole, sum.worlds, sum.capped, sum.states, sum.brokenWorlds) + for _, inv := range sum.brokenInvs { + sample := sum.bestByInv[inv] + t.Errorf("%s is broken in %s:\n%s", inv, sample.world.Name, formatTrace(sample.result)) + } +} + func TestCheckerCounterexample(t *testing.T) { w := checkWorld{ Name: "2 PRs crashes+stale (good+breaks, 2 of 2)", diff --git a/internal/queue/plan.go b/internal/queue/plan.go index 1c6e657..380baa1 100644 --- a/internal/queue/plan.go +++ b/internal/queue/plan.go @@ -81,6 +81,10 @@ type red struct { func (r *red) suspects() []Member { return r.chain[max(r.lo, 0):r.hi] } +// clearing says the red candidate sits on the trunk with several pull requests and its first +// round has yet to read, so some of them may be cleared to go back on the chain at any tick. +func (p *plan) clearing(r *red) bool { return r.base == p.w.Trunk && len(r.chain) > 1 && r.read == 0 } + func (p *plan) act(a Action) { p.acts = append(p.acts, a) } // admit sorts the queue into those that may join the chain and those that wait in place. @@ -296,14 +300,15 @@ func (p *plan) under(b Build) *red { // stackedOn keeps a candidate stacked on a red one while it may still land it, which is until // it fails. Where probes will say within a fraction of a build whether the red is real, the -// chain waits for them before it goes on beside it. +// chain waits for them before it goes on beside it: with spare, only until the first round +// has read. func (p *plan) stackedOn(r *red, b Build) { if b.Verdict == Failure || b.Verdict == Error { p.drop(b) return } r.passed = r.passed || b.Verdict == Success - p.wait = p.wait || (b.Verdict == Pending && !r.whole) + p.wait = p.wait || (b.Verdict == Pending && !r.whole && (!p.c.Spare || r.read == 0)) p.above = append(p.above, b) } @@ -604,7 +609,8 @@ func (p *plan) evidence(r *red, j int, whole bool) Build { // candidate again in their order, whatever commits now carry them. It waits while probes are // deciding whether a red candidate is real. While a candidate is still building, one that // would add fewer pull requests than gather waits for more, or for that build, unless it -// holds one labelled to go first. +// holds one labelled to go first. With spare, it keeps a slot free for the pull requests a +// red candidate's first round is about to clear. func (p *plan) fill() { in := map[int]bool{} for _, m := range p.chain { @@ -617,7 +623,11 @@ func (p *plan) fill() { if p.wait { return } - for p.pending < p.c.Slots { + slots := p.c.Slots + if p.c.Spare && slots > 1 && slices.ContainsFunc(p.reds, p.clearing) { + slots-- + } + for p.pending < slots { parent, added, held, first := p.prefix(len(p.chain)), 0, len(p.chain), false for _, pr := range p.order { if !free(pr) { diff --git a/internal/queue/plan_test.go b/internal/queue/plan_test.go index dfe6def..309f6c2 100644 --- a/internal/queue/plan_test.go +++ b/internal/queue/plan_test.go @@ -724,3 +724,439 @@ func TestKeepZeroAddsNothingToARecording(t *testing.T) { t.Fatalf("zero config JSON = %s, want %s", c, want) } } + +// played is a queue played a minute a tick: each world is the last with the planner's own +// actions applied, and CI begins each build as it is pushed and answers it once it has run +// for as many ticks as takes says, failing it where fails says. +type played struct { + t *testing.T + w World + c Config + seen map[SHA]bool + takes func(Build) int + fails func(Build) bool + acts [][]Action + pushed []Build +} + +func (q *played) Merge(parent SHA, pr PR, note string) (SHA, bool) { + return commits{}.Merge(parent, pr, note) +} +func (q *played) Mark(parent SHA, note string) SHA { return commits{}.Mark(parent, note) } +func (q *played) Seen(sha SHA) bool { return q.seen[sha] } + +func newPlayed(t *testing.T, c Config, takes func(Build) int, fails func(Build) bool, numbers ...int) *played { + now := time.Date(2026, 11, 25, 2, 0, 0, 0, time.UTC) + q := &played{t: t, c: c, seen: map[SHA]bool{}, takes: takes, fails: fails} + q.w = World{Now: now, Trunk: "trunk", Repo: q, PRs: queuedAt(now.Add(-time.Hour), numbers...)} + return q +} + +// arrive queues pull requests now, behind those already queued. +func (q *played) arrive(numbers ...int) { + for _, n := range numbers { + q.w.PRs = append(q.w.PRs, PR{Number: n, Head: fmt.Sprintf("h%d", n), Queued: q.w.Now, Checks: Success}) + } +} + +func (q *played) tick() []Action { + q.w.Now = q.w.Now.Add(time.Minute) + for i, b := range q.w.Builds { + if b.Verdict == Pending && q.w.Now.Sub(b.Started) >= time.Duration(q.takes(b))*time.Minute { + q.w.Builds[i].Verdict = Success + if q.fails(b) { + q.w.Builds[i].Verdict = Failure + } + } + } + acts := Plan(q.w, q.c) + for _, a := range acts { + switch a.Do { + case "push": + b := a.Build + b.Started = q.w.Now + q.seen[b.SHA] = true + q.w.Builds = append(q.w.Builds, b) + q.pushed = append(q.pushed, b) + case "delete": + q.w.Builds = slices.DeleteFunc(q.w.Builds, func(b Build) bool { return b.SHA == a.SHA }) + case "land": + q.w.Trunk = a.SHA + q.w.Builds = slices.DeleteFunc(q.w.Builds, func(b Build) bool { return b.SHA == a.SHA }) + q.w.PRs = slices.DeleteFunc(q.w.PRs, func(pr PR) bool { + return slices.ContainsFunc(a.Members, func(m Member) bool { return m.Number == pr.Number }) + }) + case "eject": + q.w.PRs = slices.DeleteFunc(q.w.PRs, func(pr PR) bool { return pr.Number == a.PR }) + } + } + q.acts = append(q.acts, acts) + return acts +} + +func (q *played) ticks(n int) { + for range n { + q.tick() + } +} + +func numbers(chain []Member) (ns []int) { + for _, m := range chain { + ns = append(ns, m.Number) + } + return ns +} + +// log is every tick's pushed candidates, landings and ejections, one line a tick that took any. +func (q *played) log() string { + var lines []string + for i, acts := range q.acts { + var did []string + for _, a := range acts { + switch { + case a.Do == "push" && a.Build.Round == 0: + did = append(did, fmt.Sprintf("push %v", numbers(a.Build.Chain))) + case a.Do == "push": + did = append(did, fmt.Sprintf("round %d of %d", a.Build.Round, len(a.Build.Chain))) + case a.Do == "land": + did = append(did, fmt.Sprintf("land %v", numbers(a.Members))) + case a.Do == "eject": + did = append(did, fmt.Sprintf("eject %d", a.PR)) + } + } + if len(did) > 0 { + lines = append(lines, fmt.Sprintf("%d: %s", i+1, strings.Join(did, ", "))) + } + } + return strings.Join(lines, "\n") +} + +func holds(b Build, n int) bool { + return slices.ContainsFunc(b.Chain, func(m Member) bool { return m.Number == n }) +} + +// when is the first tick, counted from 1, whose actions hold one that match matches, or 0. +func (q *played) when(match func(Action) bool) int { + for i, acts := range q.acts { + if slices.ContainsFunc(acts, match) { + return i + 1 + } + } + return 0 +} + +// pushedAgain matches a candidate that holds pull request n pushed after the first tick. +func (q *played) pushedAgain(n int) func(Action) bool { + first := q.pushed[0].Pushed + return func(a Action) bool { + return a.Do == "push" && a.Build.Round == 0 && holds(a.Build, n) && a.Build.Pushed.After(first) + } +} + +func ejected(n int) func(Action) bool { + return func(a Action) bool { return a.Do == "eject" && a.PR == n } +} + +// whole counts the whole builds the queue began: candidates, and rounds' builds of a tree. +func (q *played) whole() (n int) { + for _, b := range q.pushed { + if b.Kind != Probe { + n++ + } + } + return n +} + +// #1, #2 and #3 failed together, and #2 breaks the build. #4's candidate stacked on theirs +// builds slowly and holds a slot, and #4 and #5 arrive while the first round runs. With spare, +// the third slot stays free, and the round's read at tick 19 clears #1 and #3, which are +// pushed in that tick. Without it, #4 and #5 take the third slot and #1 and #3 wait for #4 to +// land. Either way #2 leaves at tick 21 on the same builds. +func TestWithSpareASlotWaitsForThePullRequestsARedCandidatesFirstRoundClears(t *testing.T) { + play := func(spare bool) *played { + var slow SHA + takes := func(b Build) int { + switch { + case b.SHA == slow: + return 40 + case b.Round > 0: + return 8 + } + return 10 + } + c := Config{Slots: 3, MaxBatch: 3, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute, Spare: spare} + q := newPlayed(t, c, takes, func(b Build) bool { return holds(b, 2) }, 1, 2, 3, 4) + q.tick() + slow = q.pushed[1].SHA + q.ticks(11) + q.arrive(5) + q.ticks(30) + return q + } + evidence := func(q *played) string { + for _, acts := range q.acts { + for _, a := range acts { + if a.Do == "eject" { + return fmt.Sprintf("#%d on %q", a.PR, a.Evidence) + } + } + } + return "nobody" + } + held, plain := play(true), play(false) + if got := held.when(held.pushedAgain(5)); got != 19 { + t.Errorf("with spare #5 was pushed at tick %d, where the free slot waits for the read at 19:\n%s", got, held.log()) + } + if got := held.when(held.pushedAgain(1)); got != 19 { + t.Errorf("with spare #1 was pushed at tick %d, where the round clears it at 19:\n%s", got, held.log()) + } + if got := plain.when(plain.pushedAgain(5)); got != 13 { + t.Errorf("without spare #5 was pushed at tick %d, where it takes the third slot at 13:\n%s", got, plain.log()) + } + if got := plain.when(plain.pushedAgain(1)); got != 22 { + t.Errorf("without spare #1 was pushed at tick %d, where it waits for #4 to land at 21, and the tick after:\n%s", got, plain.log()) + } + if a, b := evidence(held), evidence(plain); a != b || held.when(ejected(2)) != 21 || plain.when(ejected(2)) != 21 { + t.Errorf("with spare ejected %s at tick %d, and without it %s at tick %d, where #2 leaves at 21 on the same builds", a, held.when(ejected(2)), b, plain.when(ejected(2))) + } +} + +// Spare keeps no slot for a red candidate of one pull request, which nothing can clear, for one +// whose base the trunk has moved past, for a red trunk, for a flake, nor with one slot. Each +// world pushes the same candidates with spare as without. +func TestSpareKeepsNoSlotWhereNoFirstRoundOnTheTrunkIsAboutToClearAnybody(t *testing.T) { + now := time.Date(2026, 11, 25, 3, 0, 0, 0, time.UTC) + two := []Member{member(1, "m1"), member(2, "m2")} + probe := func(sha, base SHA, chain []Member, round int, verdict Verdict) Build { + return Build{SHA: sha, Kind: Probe, Base: base, Chain: chain, Round: round, Of: two[len(two)-1].Merge, Verdict: verdict, Pushed: now, Started: now} + } + red := func(base SHA) Build { + return Build{SHA: "m2", Base: base, Chain: two, Verdict: Failure, Pushed: now, Started: now} + } + fixes := queuedAt(now, 1, 2, 4, 5) + fixes[2].First, fixes[3].First = true, true + cfg := Config{Slots: 3, MaxBatch: 1, Probes: true, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute} + one := cfg + one.Slots = 1 + for _, tc := range []struct { + name string + prs []PR + builds []Build + c Config + want int + }{ + {"a red of one pull request", queuedAt(now, 1, 4, 5, 6), []Build{{SHA: "m1", Base: "trunk", Chain: two[:1], Verdict: Failure, Pushed: now, Started: now}}, cfg, 3}, + {"a red on a base below the trunk", queuedAt(now, 1, 2, 4, 5, 6), []Build{red("old"), probe("p0", "old", nil, 1, Pending), probe("p1", "old", two[:1], 1, Pending)}, cfg, 3}, + {"a red trunk", fixes, []Build{red("trunk"), probe("p0", "trunk", nil, 1, Failure), probe("p1", "trunk", two[:1], 1, Failure)}, cfg, 2}, + {"a flake", queuedAt(now, 1, 2, 4, 5), []Build{red("trunk"), probe("p0", "trunk", nil, 1, Success), probe("p1", "trunk", two[:1], 1, Success), probe("p2", "trunk", two, 2, Success)}, cfg, 3}, + {"one slot", queuedAt(now, 1, 2, 4), []Build{red("trunk"), probe("p0", "trunk", nil, 1, Pending), probe("p1", "trunk", two[:1], 1, Pending)}, one, 1}, + } { + w := World{Now: now.Add(time.Minute), Trunk: "trunk", Repo: commits{}, PRs: tc.prs, Builds: tc.builds} + count := func(c Config) (n int) { + for _, a := range Plan(w, c) { + if a.Do == "push" && a.Build.Round == 0 { + n++ + } + } + return n + } + spare := tc.c + spare.Spare = true + if got, plain := count(spare), count(tc.c); got != tc.want || plain != tc.want { + t.Errorf("%s: pushed %d candidates with spare and %d without, where both push %d", tc.name, got, plain, tc.want) + } + } +} + +// The slot spare keeps for #1, #2 and #3 is given back when the red candidate goes, as #3's +// label comes off mid-round, and when its round outlives the timeout: the round then reads as +// lost, so the hold lasts no longer than the timeout after the round was pushed. +func TestTheSlotSpareKeepsIsGivenBackWhenTheRedGoesOrItsRoundOutlivesTheTimeout(t *testing.T) { + now := time.Date(2026, 11, 25, 3, 0, 0, 0, time.UTC) + three := []Member{member(1, "m1"), member(2, "m2"), member(3, "m3")} + cfg := Config{Slots: 3, MaxBatch: 1, Probes: true, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute, Spare: true} + count := func(after time.Duration, numbers ...int) (n int) { + w := World{Now: now.Add(after), Trunk: "trunk", Repo: commits{}, PRs: queuedAt(now, numbers...), + Builds: []Build{{SHA: "m3", Base: "trunk", Chain: three, Verdict: Failure, Pushed: now, Started: now}}} + for j := range 3 { + w.Builds = append(w.Builds, Build{SHA: fmt.Sprint("p", j), Kind: Probe, Base: "trunk", Chain: three[:j], Round: 1, Of: "m3", Pushed: now}) + } + for _, a := range Plan(w, cfg) { + if a.Do == "push" && a.Build.Round == 0 { + n++ + } + } + return n + } + for _, tc := range []struct { + name string + after time.Duration + numbers []int + want int + }{ + {"the round running", 5 * time.Minute, []int{1, 2, 3, 4, 5, 6}, 2}, + {"#3 gone", 5 * time.Minute, []int{1, 2, 4, 5, 6}, 3}, + {"the round at the timeout", 90 * time.Minute, []int{1, 2, 3, 4, 5, 6}, 2}, + {"the round past the timeout", 91 * time.Minute, []int{1, 2, 3, 4, 5, 6}, 3}, + } { + if got := count(tc.after, tc.numbers...); got != tc.want { + t.Errorf("%s: pushed %d candidates, want %d", tc.name, got, tc.want) + } + } +} + +// #1, #2 and #3 failed together, and #4's candidate stacked on theirs is building. Probes +// will say within a fraction of a build whether it is doomed, so nothing is pushed while the +// first round runs, with spare or without. Once the round has read, clearing #1 and #3, +// spare builds them beside the stacked candidate, where the queue otherwise waits for it. +func TestWithSpareTheChainGoesOnBesideAStackedCandidateOnceTheFirstRoundReads(t *testing.T) { + now := time.Date(2026, 11, 25, 3, 0, 0, 0, time.UTC) + three := []Member{member(1, "m1"), member(2, "m2"), member(3, "m3")} + probe := func(sha SHA, j int, verdict Verdict) Build { + return Build{SHA: sha, Kind: Probe, Base: "trunk", Chain: three[:j], Round: 1, Of: "m3", Verdict: verdict, Pushed: now, Started: now} + } + stacked := Build{SHA: "m4", Base: "trunk", Chain: append(slices.Clone(three), member(4, "m4")), Verdict: Pending, Pushed: now, Started: now} + pushed := func(spare bool, verdicts ...Verdict) (chains [][]int) { + w := World{Now: now.Add(5 * time.Minute), Trunk: "trunk", Repo: commits{}, PRs: queuedAt(now, 1, 2, 3, 4, 5), + Builds: []Build{{SHA: "m3", Base: "trunk", Chain: three, Verdict: Failure, Pushed: now, Started: now}, stacked, + probe("p0", 0, verdicts[0]), probe("p1", 1, verdicts[1]), probe("p2", 2, verdicts[2])}} + for _, a := range Plan(w, Config{Slots: 3, MaxBatch: 3, Probes: true, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute, Spare: spare}) { + if a.Do == "push" && a.Build.Round == 0 { + chains = append(chains, numbers(a.Build.Chain)) + } + } + return chains + } + for _, spare := range []bool{false, true} { + if got := pushed(spare, Pending, Pending, Pending); len(got) > 0 { + t.Errorf("spare %v: pushed %v while the first round runs, where the chain waits for it", spare, got) + } + } + if got := pushed(false, Success, Success, Failure); len(got) > 0 { + t.Errorf("without spare pushed %v once the round read, where the chain waits for the stacked candidate", got) + } + if got := pushed(true, Success, Success, Failure); fmt.Sprint(got) != "[[1 3 4] [1 3 4 5]]" { + t.Errorf("with spare pushed %v once the round read, where #1 and #3 are built beside the stacked candidate", got) + } +} + +// #1, #2 and #3's candidate failed by chance, and #4's stacked on it will land all four. With +// spare, the first round's read clears #1 and #2, and they are built with #4 beside the stacked +// candidate. Where the stacked one reports first, it lands all four at the same tick as without +// spare, and the build of #1, #2 and #4 is one whole build more, thrown away before it reports. +// Where it builds slowly, #1, #2 and #4 land at tick 23 and #3 at 34, where all four would +// have landed at 36: two whole builds more, and the stacked candidate, which would have +// passed, is thrown away. +func TestWithSpareAFlakeBuildsTheClearedPullRequestsBesideTheCandidateThatLandsThem(t *testing.T) { + play := func(spare bool, slow int) *played { + var red, stacked SHA + takes := func(b Build) int { + switch { + case b.Kind == Probe: + return 2 + case b.SHA == stacked: + return slow + } + return 10 + } + c := Config{Slots: 2, MaxBatch: 3, Probes: true, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute, Spare: spare} + q := newPlayed(t, c, takes, func(b Build) bool { return b.SHA == red }, 1, 2, 3) + q.tick() + red = q.pushed[0].SHA + q.ticks(4) + q.arrive(4) + q.tick() + stacked = q.pushed[1].SHA + q.ticks(60) + return q + } + landings := func(q *played) string { + var out []string + for i, acts := range q.acts { + for _, a := range acts { + if a.Do == "land" { + out = append(out, fmt.Sprintf("%v at %d", numbers(a.Members), i+1)) + } + } + } + return strings.Join(out, ", ") + } + for _, tc := range []struct { + spare bool + slow, whole int + landed, thrown string + }{ + {false, 10, 2, "[1 2 3 4] at 16", ""}, + {true, 10, 3, "[1 2 3 4] at 16", "[1 2 4]"}, + {false, 30, 3, "[1 2 3 4] at 36", ""}, + {true, 30, 5, "[1 2 4] at 23, [3] at 34", "[1 2 3 4]"}, + } { + q := play(tc.spare, tc.slow) + var thrown []string + for _, b := range q.pushed { + if b.Kind == Candidate && !slices.ContainsFunc(q.acts, func(acts []Action) bool { + return slices.ContainsFunc(acts, func(a Action) bool { return a.Do == "land" && a.SHA == b.SHA }) + }) && b.SHA != q.pushed[0].SHA { + thrown = append(thrown, fmt.Sprint(numbers(b.Chain))) + } + } + if got := landings(q); got != tc.landed || q.whole() != tc.whole || strings.Join(thrown, " ") != tc.thrown { + t.Errorf("spare %v, stacked candidate %d ticks: landed %s on %d whole builds, throwing away %v, where it lands %s on %d, throwing away %q:\n%s", + tc.spare, tc.slow, got, q.whole(), thrown, tc.landed, tc.whole, tc.thrown, q.log()) + } + } +} + +// #2 breaks the build, and the first round of #1 to #4's red candidate narrows the suspects to +// #2 alone. #1, #3 and #4 go back on the chain at once and land at tick 31, while the third +// build of #2's fair test is still running. A pull request under the red candidate has left, +// so the red candidate goes with its evidence, and #2 is free again: it is built into the next +// candidate at tick 32, fails again, and leaves at 54 on a fair test begun over. +func TestASuspectWalksBackInWhenAPullRequestBelowItLandsBeforeItsFairTestEnds(t *testing.T) { + c := Config{Slots: 3, MaxBatch: 4, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute} + q := newPlayed(t, c, func(Build) int { return 10 }, func(b Build) bool { return holds(b, 2) }, 1, 2, 3, 4) + q.ticks(60) + want := `1: push [1 2 3 4] +11: round 1 of 1, round 1 of 2, round 1 of 0 +21: round 2 of 1, push [1 3 4] +22: round 3 of 2 +31: land [1 3 4] +32: push [2] +42: round 1 of 1 +43: round 2 of 0 +44: round 3 of 1 +54: eject 2` + if got := q.log(); got != want { + t.Errorf("the queue did\n%s\nwhere #2 walks back in at 32 and leaves at 54:\n%s", got, want) + } +} + +// As above, with #5, #6 and #7 arriving while the first round runs. Without spare they fill +// the slots, #1, #3 and #4 are pushed only at tick 24, and #2's fair test ends first: it leaves +// at 32. With spare the third slot waits for them, they are pushed at 21 and land at 31, and +// #2 walks back in at 32 and leaves at 54. +func TestSpareLetsTheClearedLandBeforeTheSuspectsFairTestEnds(t *testing.T) { + play := func(spare bool) *played { + c := Config{Slots: 3, MaxBatch: 4, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute, Spare: spare} + q := newPlayed(t, c, func(Build) int { return 10 }, func(b Build) bool { return holds(b, 2) }, 1, 2, 3, 4) + q.ticks(12) + q.arrive(5) + q.ticks(2) + q.arrive(6) + q.ticks(2) + q.arrive(7) + q.ticks(50) + return q + } + for _, tc := range []struct { + spare bool + cleared, again, left int + }{{false, 24, 0, 32}, {true, 21, 32, 54}} { + q := play(tc.spare) + again := q.when(func(a Action) bool { return a.Do == "push" && fmt.Sprint(numbers(a.Build.Chain)) == "[2]" }) + if q.when(q.pushedAgain(1)) != tc.cleared || again != tc.again || q.when(ejected(2)) != tc.left { + t.Errorf("spare %v: #1 pushed at %d, #2 pushed again at %d and ejected at %d, where %d, %d and %d:\n%s", + tc.spare, q.when(q.pushedAgain(1)), again, q.when(ejected(2)), tc.cleared, tc.again, tc.left, q.log()) + } + } +} diff --git a/internal/queue/world.go b/internal/queue/world.go index 1137ae2..036e86e 100644 --- a/internal/queue/world.go +++ b/internal/queue/world.go @@ -153,6 +153,10 @@ type Config struct { Recheck time.Duration // Keep is how long a candidate's branch stands after the queue marks it landed. Keep time.Duration `json:",omitzero"` + // Spare keeps a slot free while a red candidate of several pull requests on the trunk has + // its first round running, for those the round clears, and lets the chain go on beside a + // candidate stacked on it once that round has read. + Spare bool `json:",omitzero"` } // Action is one thing a tick does. The executor applies them in order. diff --git a/internal/run/run.go b/internal/run/run.go index b592c71..3efdfe5 100644 --- a/internal/run/run.go +++ b/internal/run/run.go @@ -45,6 +45,7 @@ type Config struct { Gather int `json:"gather"` // a candidate built while another is building adds at least this many Timeout string `json:"timeout"` Keep string `json:"keep,omitzero"` + Spare bool `json:"spare,omitzero"` // a slot stays free for the pull requests a red candidate's first round clears Recheck string `json:"recheck"` Author struct { Name string `json:"name"` @@ -123,7 +124,7 @@ func (c Config) dials() queue.Config { timeout, _ := time.ParseDuration(c.Timeout) recheck, _ := time.ParseDuration(c.Recheck) keep, _ := time.ParseDuration(c.Keep) - return queue.Config{Slots: c.Slots, MaxBatch: c.MaxBatch, Gather: c.Gather, Probes: c.Probe != "", Cancels: c.Cancels, Timeout: timeout, Recheck: recheck, Keep: keep} + return queue.Config{Slots: c.Slots, MaxBatch: c.MaxBatch, Gather: c.Gather, Probes: c.Probe != "", Cancels: c.Cancels, Timeout: timeout, Recheck: recheck, Keep: keep, Spare: c.Spare} } // Queue is one repository's queue, ready to tick. From d431614bd70c0d63b13898e2447c98eedad6622f Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Wed, 25 Nov 2026 01:36:51 -0800 Subject: [PATCH 2/3] Spare holds a slot and nothing else The shorter wait beside a candidate stacked on a red one changed who leaves: where #3 repairs what #2 breaks, it built #1 and #3 beside the candidate of all three, and #3 left on that build's own fair test where the stacked candidate would have landed all three. The chain waits for the probes as it did before, and spare is the slot hold alone. The hold is a limit: fill uses one slot fewer while the first round of a red candidate of several pull requests on the trunk runs. Gather may still hold a cleared pull request back, a landing or an ejection in the tick ends the plan before fill, and where the timeout is zero a round that never reports holds the slot while the red stands. Tests: the repairing pull request lands with the stacked candidate, with spare and without; a suspect a later arrival repairs leaves on the old trees without spare and lands on the new trunk with it; a cleared pull request alone waits under gather; the hold under a zero timeout; a flake of whole-build rounds lands the cleared first on as many builds, and one of probes builds the same with spare as without. --- README.md | 2 +- doc/design.md | 6 +- internal/queue/plan.go | 9 +- internal/queue/plan_test.go | 348 +++++++++++++++++++++++------------- internal/queue/world.go | 5 +- internal/run/run.go | 2 +- 6 files changed, 232 insertions(+), 140 deletions(-) diff --git a/README.md b/README.md index 0c18f27..968ca82 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ A pull request joins the queue when it's labelled `merge-queue` and leaves when } ``` -`admit` names the statuses or check runs a pull request's own head must pass before it joins the queue, and `threads` keeps one out while a review thread is unresolved (the queue reads its first hundred, and one with more waits). `slots` is how many candidates build at once, and `max_batch` how many pull requests each adds. Where builds cost more than a short wait does, `gather` holds a candidate back while another is building until it would add that many. The builds that find a failure's cause run beside them: three for each pull request the red build added, all at once where `started` or `begins` is set. Set `started` to a word your CI's pending status carries once a build is running (Buildkite's says "started"), so the queue can find a failure's cause in one build time, not three; check runs need no setting. Where the context that judges a build appears only at its end, set `begins` to a context that answers once your CI has queued the build's jobs, such as a pipeline's first step. Where your CI reports as a GitHub App's check runs, set `app` to that App's database ID (`"app": 15368`): every context but the queue's own status is then read only from its check runs, and a commit status or another App's check run answers nothing. The queue's own status is always a commit status, and no build, probe, admit or begins context may share its name. Set `cancels` where your CI stops a build whose branch is deleted; otherwise a build the queue no longer wants keeps its slot until it reports. Set `spare` to keep one slot free while a failed candidate of several pull requests has its first round of builds, for the pull requests that round clears, and to build those beside a candidate stacked on the failed one rather than after it. `run.Config` in `internal/run/run.go` lists every setting. +`admit` names the statuses or check runs a pull request's own head must pass before it joins the queue, and `threads` keeps one out while a review thread is unresolved (the queue reads its first hundred, and one with more waits). `slots` is how many candidates build at once, and `max_batch` how many pull requests each adds. Where builds cost more than a short wait does, `gather` holds a candidate back while another is building until it would add that many. The builds that find a failure's cause run beside them: three for each pull request the red build added, all at once where `started` or `begins` is set. Set `started` to a word your CI's pending status carries once a build is running (Buildkite's says "started"), so the queue can find a failure's cause in one build time, not three; check runs need no setting. Where the context that judges a build appears only at its end, set `begins` to a context that answers once your CI has queued the build's jobs, such as a pipeline's first step. Where your CI reports as a GitHub App's check runs, set `app` to that App's database ID (`"app": 15368`): every context but the queue's own status is then read only from its check runs, and a commit status or another App's check run answers nothing. The queue's own status is always a commit status, and no build, probe, admit or begins context may share its name. Set `cancels` where your CI stops a build whose branch is deleted; otherwise a build the queue no longer wants keeps its slot until it reports. Set `spare` to fill one slot fewer while a failed candidate of several pull requests has its first round of builds, so the pull requests that round clears are likelier to find one free. `run.Config` in `internal/run/run.go` lists every setting. Set `keep` (for example, `"keep": "3m"`) to leave a landed candidate's branch standing for at least that long after the queue marks it landed. The candidate at the trunk's tip is marked at the landing, or by the next read if that tick died first. A candidate beneath it is marked once its verdict is in. One failed or errored context is a verdict, so another still running, or one that appears later, does not hold the branch longer. Where `cancels` is set, a lower candidate still running is deleted at once, as without `keep`. A retry the trunk holds is a candidate like any other. A probe, a check and a round's build are never marked. A second mark from ticks run beside each other only lengthens the stand; ticks must run one at a time. Without `keep`, the landing push has the remote compare and delete the candidate's branch, so whatever deletes that branch first stops the landing. With `keep`, the queue compares the branch just before pushing the trunk alone, so deleting the branch between that comparison and the push does not stop the landing. diff --git a/doc/design.md b/doc/design.md index d5f7d3b..6a8a378 100644 --- a/doc/design.md +++ b/doc/design.md @@ -32,7 +32,7 @@ The queue uses its own mark because the trunk's own builds report on the same co ## Blame -A red candidate at the bottom of the chain is set aside with the pull requests it added, and the chain goes on without them, built on the tip. A candidate stacked on the red one is dropped once it fails too. Until then it may yet land them all. Where rounds are probes, which answer in a fraction of a build, the chain waits for that answer. Where they are whole builds it does not wait: it is built beside the candidates still running. With `spare`, the wait lasts only until the red candidate's first round has read, and the pull requests that round clears are built beside the stacked candidate. Should the red have been a flake, that is a whole build more, and where the stacked candidate is the slower, the cleared ones land first and it is thrown away. +A red candidate at the bottom of the chain is set aside with the pull requests it added, and the chain goes on without them, built on the tip. A candidate stacked on the red one is dropped once it fails too. Until then it may yet land them all. Where rounds are probes, which answer in a fraction of a build, the chain waits for that answer. Where they are whole builds it does not wait: it is built beside the candidates still running. The red candidate is narrowed by rounds: builds of some of its prefixes. A round's build never lands. What the rounds have said so far is a span, from the highest prefix that passed to the lowest above it that failed. The culprit is in it. A round is read once every build in it has reported, and the rounds are read in order. @@ -60,7 +60,7 @@ A test of five builds, fail, pass, fail, pass and fail, was tried for a CI that Only the pull requests the rounds have not ruled out stay off the chain. The rest go back on it at once. One whose merge there would be the red candidate's own tree waits with them, since a build of that tree is an attempt of the red candidate and not a candidate on the chain. -A red candidate and its rounds stand while the trunk moves under them, since they prove what they prove about the commits they were built on. They go when a pull request in the trees they build leaves the queue, and when their base is no longer the tip and fails by itself. Once a pull request under a red candidate has landed, a round's build from below that landing is a build of another tree, and it goes too. So several red candidates are narrowed at once, each beside a chain that keeps landing. +A red candidate and its rounds stand while the trunk moves under them, since they prove what they prove about the commits they were built on. They go when a pull request in the trees they build leaves the queue, and when their base is no longer the tip and fails by itself. Once a pull request under a red candidate has landed, a round's build from below that landing is a build of another tree, and it goes too. So several red candidates are narrowed at once, each beside a chain that keeps landing. A suspect is judged on the trees its red candidate's rounds build, so one whose red candidate went because the trunk moved past it starts over on the new trunk. A round's builds are whole builds by default. Where a repository's CI offers probes, a round uses them instead: a probe is an empty commit on a prefix that asks CI to run only what failed in the red build. @@ -116,7 +116,7 @@ The queue posts `pending` on everything it pushes, which gives each wait a start ## Dials -`slots` is how many candidates are under test at once. A round's builds are beside them and are not counted. With `spare`, while a red candidate of several pull requests on the trunk has its first round running, the chain takes one slot fewer, so those the round clears find one free as it reads. The round's read ends it, whatever it says, as does the red candidate going; a round that never reports reads as lost after `timeout`. `max_batch` is how many pull requests a candidate adds, and `gather` how few it may add while another is building: fewer builds for each landing, for a wait of at most one build. `eval/main.go` holds the values each shape ships with, and the hillclimb moves them. +`slots` is how many candidates are under test at once. A round's builds are beside them and are not counted. With `spare`, while a red candidate of several pull requests on the trunk has its first round running, the chain takes one slot fewer, so those the round clears are likelier to find one free. It limits how many slots the chain fills and does not push the cleared pull requests: `gather` may still hold one back, and a landing or an ejection in the same tick ends the plan first. The round's read ends the hold, whatever it says, as does the red candidate going. A round that never reports reads as lost after `timeout`; where `timeout` is zero nothing ends the round, and the slot stays held while the red stands. `max_batch` is how many pull requests a candidate adds, and `gather` how few it may add while another is building: fewer builds for each landing, for a wait of at most one build. `eval/main.go` holds the values each shape ships with, and the hillclimb moves them. ## Not in the engine diff --git a/internal/queue/plan.go b/internal/queue/plan.go index 380baa1..de4be8e 100644 --- a/internal/queue/plan.go +++ b/internal/queue/plan.go @@ -300,15 +300,14 @@ func (p *plan) under(b Build) *red { // stackedOn keeps a candidate stacked on a red one while it may still land it, which is until // it fails. Where probes will say within a fraction of a build whether the red is real, the -// chain waits for them before it goes on beside it: with spare, only until the first round -// has read. +// chain waits for them before it goes on beside it. func (p *plan) stackedOn(r *red, b Build) { if b.Verdict == Failure || b.Verdict == Error { p.drop(b) return } r.passed = r.passed || b.Verdict == Success - p.wait = p.wait || (b.Verdict == Pending && !r.whole && (!p.c.Spare || r.read == 0)) + p.wait = p.wait || (b.Verdict == Pending && !r.whole) p.above = append(p.above, b) } @@ -609,8 +608,8 @@ func (p *plan) evidence(r *red, j int, whole bool) Build { // candidate again in their order, whatever commits now carry them. It waits while probes are // deciding whether a red candidate is real. While a candidate is still building, one that // would add fewer pull requests than gather waits for more, or for that build, unless it -// holds one labelled to go first. With spare, it keeps a slot free for the pull requests a -// red candidate's first round is about to clear. +// holds one labelled to go first. With spare, it uses one slot fewer while a red candidate's +// first round on the trunk runs. func (p *plan) fill() { in := map[int]bool{} for _, m := range p.chain { diff --git a/internal/queue/plan_test.go b/internal/queue/plan_test.go index 309f6c2..f8cfb3b 100644 --- a/internal/queue/plan_test.go +++ b/internal/queue/plan_test.go @@ -970,12 +970,13 @@ func TestSpareKeepsNoSlotWhereNoFirstRoundOnTheTrunkIsAboutToClearAnybody(t *tes // The slot spare keeps for #1, #2 and #3 is given back when the red candidate goes, as #3's // label comes off mid-round, and when its round outlives the timeout: the round then reads as -// lost, so the hold lasts no longer than the timeout after the round was pushed. +// lost, so the hold lasts no longer than the timeout after the round was pushed. With no +// timeout, a round that never reports holds the slot for as long as the red stands. func TestTheSlotSpareKeepsIsGivenBackWhenTheRedGoesOrItsRoundOutlivesTheTimeout(t *testing.T) { now := time.Date(2026, 11, 25, 3, 0, 0, 0, time.UTC) three := []Member{member(1, "m1"), member(2, "m2"), member(3, "m3")} - cfg := Config{Slots: 3, MaxBatch: 1, Probes: true, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute, Spare: true} - count := func(after time.Duration, numbers ...int) (n int) { + count := func(timeout, after time.Duration, numbers ...int) (n int) { + cfg := Config{Slots: 3, MaxBatch: 1, Probes: true, Timeout: timeout, Recheck: 10 * time.Minute, Spare: true} w := World{Now: now.Add(after), Trunk: "trunk", Repo: commits{}, PRs: queuedAt(now, numbers...), Builds: []Build{{SHA: "m3", Base: "trunk", Chain: three, Verdict: Failure, Pushed: now, Started: now}}} for j := range 3 { @@ -989,124 +990,23 @@ func TestTheSlotSpareKeepsIsGivenBackWhenTheRedGoesOrItsRoundOutlivesTheTimeout( return n } for _, tc := range []struct { - name string - after time.Duration - numbers []int - want int + name string + timeout, after time.Duration + numbers []int + want int }{ - {"the round running", 5 * time.Minute, []int{1, 2, 3, 4, 5, 6}, 2}, - {"#3 gone", 5 * time.Minute, []int{1, 2, 4, 5, 6}, 3}, - {"the round at the timeout", 90 * time.Minute, []int{1, 2, 3, 4, 5, 6}, 2}, - {"the round past the timeout", 91 * time.Minute, []int{1, 2, 3, 4, 5, 6}, 3}, + {"the round running", 90 * time.Minute, 5 * time.Minute, []int{1, 2, 3, 4, 5, 6}, 2}, + {"#3 gone", 90 * time.Minute, 5 * time.Minute, []int{1, 2, 4, 5, 6}, 3}, + {"the round at the timeout", 90 * time.Minute, 90 * time.Minute, []int{1, 2, 3, 4, 5, 6}, 2}, + {"the round past the timeout", 90 * time.Minute, 91 * time.Minute, []int{1, 2, 3, 4, 5, 6}, 3}, + {"the round a day old with no timeout", 0, 24 * time.Hour, []int{1, 2, 3, 4, 5, 6}, 2}, } { - if got := count(tc.after, tc.numbers...); got != tc.want { + if got := count(tc.timeout, tc.after, tc.numbers...); got != tc.want { t.Errorf("%s: pushed %d candidates, want %d", tc.name, got, tc.want) } } } -// #1, #2 and #3 failed together, and #4's candidate stacked on theirs is building. Probes -// will say within a fraction of a build whether it is doomed, so nothing is pushed while the -// first round runs, with spare or without. Once the round has read, clearing #1 and #3, -// spare builds them beside the stacked candidate, where the queue otherwise waits for it. -func TestWithSpareTheChainGoesOnBesideAStackedCandidateOnceTheFirstRoundReads(t *testing.T) { - now := time.Date(2026, 11, 25, 3, 0, 0, 0, time.UTC) - three := []Member{member(1, "m1"), member(2, "m2"), member(3, "m3")} - probe := func(sha SHA, j int, verdict Verdict) Build { - return Build{SHA: sha, Kind: Probe, Base: "trunk", Chain: three[:j], Round: 1, Of: "m3", Verdict: verdict, Pushed: now, Started: now} - } - stacked := Build{SHA: "m4", Base: "trunk", Chain: append(slices.Clone(three), member(4, "m4")), Verdict: Pending, Pushed: now, Started: now} - pushed := func(spare bool, verdicts ...Verdict) (chains [][]int) { - w := World{Now: now.Add(5 * time.Minute), Trunk: "trunk", Repo: commits{}, PRs: queuedAt(now, 1, 2, 3, 4, 5), - Builds: []Build{{SHA: "m3", Base: "trunk", Chain: three, Verdict: Failure, Pushed: now, Started: now}, stacked, - probe("p0", 0, verdicts[0]), probe("p1", 1, verdicts[1]), probe("p2", 2, verdicts[2])}} - for _, a := range Plan(w, Config{Slots: 3, MaxBatch: 3, Probes: true, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute, Spare: spare}) { - if a.Do == "push" && a.Build.Round == 0 { - chains = append(chains, numbers(a.Build.Chain)) - } - } - return chains - } - for _, spare := range []bool{false, true} { - if got := pushed(spare, Pending, Pending, Pending); len(got) > 0 { - t.Errorf("spare %v: pushed %v while the first round runs, where the chain waits for it", spare, got) - } - } - if got := pushed(false, Success, Success, Failure); len(got) > 0 { - t.Errorf("without spare pushed %v once the round read, where the chain waits for the stacked candidate", got) - } - if got := pushed(true, Success, Success, Failure); fmt.Sprint(got) != "[[1 3 4] [1 3 4 5]]" { - t.Errorf("with spare pushed %v once the round read, where #1 and #3 are built beside the stacked candidate", got) - } -} - -// #1, #2 and #3's candidate failed by chance, and #4's stacked on it will land all four. With -// spare, the first round's read clears #1 and #2, and they are built with #4 beside the stacked -// candidate. Where the stacked one reports first, it lands all four at the same tick as without -// spare, and the build of #1, #2 and #4 is one whole build more, thrown away before it reports. -// Where it builds slowly, #1, #2 and #4 land at tick 23 and #3 at 34, where all four would -// have landed at 36: two whole builds more, and the stacked candidate, which would have -// passed, is thrown away. -func TestWithSpareAFlakeBuildsTheClearedPullRequestsBesideTheCandidateThatLandsThem(t *testing.T) { - play := func(spare bool, slow int) *played { - var red, stacked SHA - takes := func(b Build) int { - switch { - case b.Kind == Probe: - return 2 - case b.SHA == stacked: - return slow - } - return 10 - } - c := Config{Slots: 2, MaxBatch: 3, Probes: true, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute, Spare: spare} - q := newPlayed(t, c, takes, func(b Build) bool { return b.SHA == red }, 1, 2, 3) - q.tick() - red = q.pushed[0].SHA - q.ticks(4) - q.arrive(4) - q.tick() - stacked = q.pushed[1].SHA - q.ticks(60) - return q - } - landings := func(q *played) string { - var out []string - for i, acts := range q.acts { - for _, a := range acts { - if a.Do == "land" { - out = append(out, fmt.Sprintf("%v at %d", numbers(a.Members), i+1)) - } - } - } - return strings.Join(out, ", ") - } - for _, tc := range []struct { - spare bool - slow, whole int - landed, thrown string - }{ - {false, 10, 2, "[1 2 3 4] at 16", ""}, - {true, 10, 3, "[1 2 3 4] at 16", "[1 2 4]"}, - {false, 30, 3, "[1 2 3 4] at 36", ""}, - {true, 30, 5, "[1 2 4] at 23, [3] at 34", "[1 2 3 4]"}, - } { - q := play(tc.spare, tc.slow) - var thrown []string - for _, b := range q.pushed { - if b.Kind == Candidate && !slices.ContainsFunc(q.acts, func(acts []Action) bool { - return slices.ContainsFunc(acts, func(a Action) bool { return a.Do == "land" && a.SHA == b.SHA }) - }) && b.SHA != q.pushed[0].SHA { - thrown = append(thrown, fmt.Sprint(numbers(b.Chain))) - } - } - if got := landings(q); got != tc.landed || q.whole() != tc.whole || strings.Join(thrown, " ") != tc.thrown { - t.Errorf("spare %v, stacked candidate %d ticks: landed %s on %d whole builds, throwing away %v, where it lands %s on %d, throwing away %q:\n%s", - tc.spare, tc.slow, got, q.whole(), thrown, tc.landed, tc.whole, tc.thrown, q.log()) - } - } -} - // #2 breaks the build, and the first round of #1 to #4's red candidate narrows the suspects to // #2 alone. #1, #3 and #4 go back on the chain at once and land at tick 31, while the third // build of #2's fair test is still running. A pull request under the red candidate has left, @@ -1131,28 +1031,31 @@ func TestASuspectWalksBackInWhenAPullRequestBelowItLandsBeforeItsFairTestEnds(t } } +// lateArrivals plays #1 to #4 queued together, with #5, #6 and #7 arriving two ticks apart +// while the first round of their red candidate runs. +func lateArrivals(t *testing.T, spare bool, fails func(Build) bool) *played { + c := Config{Slots: 3, MaxBatch: 4, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute, Spare: spare} + q := newPlayed(t, c, func(Build) int { return 10 }, fails, 1, 2, 3, 4) + q.ticks(12) + q.arrive(5) + q.ticks(2) + q.arrive(6) + q.ticks(2) + q.arrive(7) + q.ticks(50) + return q +} + // As above, with #5, #6 and #7 arriving while the first round runs. Without spare they fill // the slots, #1, #3 and #4 are pushed only at tick 24, and #2's fair test ends first: it leaves // at 32. With spare the third slot waits for them, they are pushed at 21 and land at 31, and // #2 walks back in at 32 and leaves at 54. func TestSpareLetsTheClearedLandBeforeTheSuspectsFairTestEnds(t *testing.T) { - play := func(spare bool) *played { - c := Config{Slots: 3, MaxBatch: 4, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute, Spare: spare} - q := newPlayed(t, c, func(Build) int { return 10 }, func(b Build) bool { return holds(b, 2) }, 1, 2, 3, 4) - q.ticks(12) - q.arrive(5) - q.ticks(2) - q.arrive(6) - q.ticks(2) - q.arrive(7) - q.ticks(50) - return q - } for _, tc := range []struct { spare bool cleared, again, left int }{{false, 24, 0, 32}, {true, 21, 32, 54}} { - q := play(tc.spare) + q := lateArrivals(t, tc.spare, func(b Build) bool { return holds(b, 2) }) again := q.when(func(a Action) bool { return a.Do == "push" && fmt.Sprint(numbers(a.Build.Chain)) == "[2]" }) if q.when(q.pushedAgain(1)) != tc.cleared || again != tc.again || q.when(ejected(2)) != tc.left { t.Errorf("spare %v: #1 pushed at %d, #2 pushed again at %d and ejected at %d, where %d, %d and %d:\n%s", @@ -1160,3 +1063,194 @@ func TestSpareLetsTheClearedLandBeforeTheSuspectsFairTestEnds(t *testing.T) { } } } + +// inTree says pull request n is in the tree b builds: in its chain, or already in its base. +func inTree(b Build, n int) bool { + return holds(b, n) || strings.Contains(b.Base, fmt.Sprintf(",%d,", n)) +} + +// As above, but #7 repairs #2: every tree with #2 fails unless it also holds #7. Without spare, +// #7 lands at 27, yet the red candidate's rounds stand on the trunk they were built on, and #2 +// leaves on them at 32. With spare, #7 lands with #1, #3 and #4 at 31, the red candidate goes, +// and #2's next candidate, built on a trunk that holds #7, passes and lands at 42. +func TestASuspectALaterArrivalRepairsLeavesOnTheOldTreesOrLandsOnTheNewTrunk(t *testing.T) { + fails := func(b Build) bool { return inTree(b, 2) && !inTree(b, 7) } + for _, tc := range []struct { + spare bool + want string + }{ + {false, `1: push [1 2 3 4] +11: round 1 of 1, round 1 of 2, round 1 of 0 +13: push [5] +15: push [5 6] +17: push [5 6 7] +21: round 2 of 1 +22: round 3 of 2 +23: land [5] +24: push [6 7 1 3 4] +25: land [6] +27: land [7] +32: eject 2 +34: land [1 3 4]`}, + {true, `1: push [1 2 3 4] +11: round 1 of 1, round 1 of 2, round 1 of 0 +13: push [5] +15: push [5 6] +21: round 2 of 1, push [5 6 1 3 4 7] +22: round 3 of 2 +23: land [5] +25: land [6] +31: land [1 3 4 7] +32: push [2] +42: land [2]`}, + } { + if got := lateArrivals(t, tc.spare, fails).log(); got != tc.want { + t.Errorf("spare %v: the queue did\n%s\nwhere it does\n%s", tc.spare, got, tc.want) + } + } +} + +// #2 breaks a test and #3, queued next, repairs it, so the candidates of #1 and #2 and of #1 +// and #3 fail where that of all three passes. The candidate of all three is stacked on the red +// one of #1 and #2 and builds slowly, while #2's own rounds outlast it. Probes keep the chain +// waiting for it, with spare or without: #1 and #3 are never built without #2, nobody leaves, +// and the stacked candidate lands all three at tick 20. +func TestTheChainWaitsForACandidateStackedOnARedOneWhereItMayLandAllOfThem(t *testing.T) { + takes := func(b Build) int { + switch { + case b.Kind == Probe && b.Round > 1 && holds(b, 2): + return 25 + case b.Kind == Probe: + return 1 + case holds(b, 2) && holds(b, 3): + return 18 + case holds(b, 2): + return 2 + } + return 1 + } + want := `1: push [1 2] +2: push [1 2 3] +3: round 1 of 1, round 1 of 0 +4: round 2 of 2 +5: round 3 of 1 +6: round 4 of 2 +20: land [1 2 3]` + for _, spare := range []bool{false, true} { + c := Config{Slots: 2, MaxBatch: 2, Probes: true, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute, Spare: spare} + q := newPlayed(t, c, takes, func(b Build) bool { return holds(b, 2) != holds(b, 3) }, 1, 2) + q.tick() + q.arrive(3) + q.ticks(40) + if got := q.log(); got != want { + t.Errorf("spare %v: the queue did\n%s\nwhere it does\n%s", spare, got, want) + } + } +} + +// Spare limits how many slots fill uses; it does not push the cleared at the read. #1 and #2 +// failed together, and #3 and #4 arrive as the first round of whole builds begins, so their +// candidate takes the one slot fill may use. The round's read at tick 21 clears #1, and the +// second slot is free again, but with gather at 2 a candidate of #1 alone waits for #3 and +// #4's build, which lands at 22. Without gather, #1 is built on theirs at 21. +func TestWithSpareAndGatherAPullRequestTheFirstRoundClearsAloneWaitsForTheCandidateBuilding(t *testing.T) { + for _, tc := range []struct { + gather int + want string + }{ + {2, `1: push [1 2] +11: round 1 of 2, round 1 of 1 +12: round 2 of 1, round 2 of 0, push [3 4] +13: round 3 of 2, round 3 of 1 +22: land [3 4] +23: eject 2 +24: push [1] +34: land [1]`}, + {0, `1: push [1 2] +11: round 1 of 2, round 1 of 1 +12: round 2 of 1, round 2 of 0, push [3 4] +13: round 3 of 2, round 3 of 1 +21: push [3 4 1] +22: land [3 4] +23: eject 2 +31: land [1]`}, + } { + c := Config{Slots: 2, MaxBatch: 2, Gather: tc.gather, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute, Spare: true} + q := newPlayed(t, c, func(Build) int { return 10 }, func(b Build) bool { return holds(b, 2) }, 1, 2) + q.ticks(11) + q.arrive(3, 4) + q.ticks(30) + if got := q.log(); got != tc.want { + t.Errorf("gather %d: the queue did\n%s\nwhere it does\n%s", tc.gather, got, tc.want) + } + } +} + +// #1, #2 and #3's candidate failed by chance, and #4, #5 and #6 arrive a tick apart as the first +// round of whole builds begins. The round's build of all three passes at tick 21 and clears +// them. Without spare, #4 to #6 take every slot, and #1 to #3 wait for #6 to land at 24 and land +// at 35. With spare, #6 waits for the slot, #1 to #3 are built at the read and land at 31, and +// #6 lands at 34: the same whole builds in another order. Where rounds are probes, the first +// reads at tick 13, before a third candidate is wanted, and spare builds the same as without. +func TestWithSpareAFlakeOfWholeBuildsLandsTheClearedFirstOnAsManyBuilds(t *testing.T) { + probed := `1: push [1 2 3] +11: round 1 of 1, round 1 of 2, round 1 of 0 +12: push [4] +13: round 2 of 3, push [4 1 2 5] +14: round 3 of 2, push [4 1 2 5 6] +16: round 4 of 3 +22: land [4] +23: land [1 2 5] +24: land [6] +25: push [3] +35: land [3]` + for _, tc := range []struct { + probes, spare bool + whole int + want string + }{ + {false, false, 14, `1: push [1 2 3] +11: round 1 of 3, round 1 of 2, round 1 of 1 +12: round 2 of 2, round 2 of 1, round 2 of 0, push [4] +13: round 3 of 3, round 3 of 2, round 3 of 1, push [4 5] +14: push [4 5 6] +22: land [4] +23: land [5] +24: land [6] +25: push [1 2 3] +35: land [1 2 3]`}, + {false, true, 14, `1: push [1 2 3] +11: round 1 of 3, round 1 of 2, round 1 of 1 +12: round 2 of 2, round 2 of 1, round 2 of 0, push [4] +13: round 3 of 3, round 3 of 2, round 3 of 1, push [4 5] +21: push [4 5 1 2 3] +22: land [4] +23: land [5] +24: push [1 2 3 6] +31: land [1 2 3] +34: land [6]`}, + {true, false, 6, probed}, + {true, true, 6, probed}, + } { + var red SHA + takes := func(b Build) int { + if b.Kind == Probe { + return 2 + } + return 10 + } + c := Config{Slots: 3, MaxBatch: 3, Probes: tc.probes, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute, Spare: tc.spare} + q := newPlayed(t, c, takes, func(b Build) bool { return b.SHA == red }, 1, 2, 3) + q.tick() + red = q.pushed[0].SHA + q.ticks(10) + for _, n := range []int{4, 5, 6} { + q.arrive(n) + q.tick() + } + q.ticks(58) + if got := q.log(); got != tc.want || q.whole() != tc.whole { + t.Errorf("probes %v, spare %v: the queue did on %d whole builds\n%s\nwhere it does on %d\n%s", tc.probes, tc.spare, q.whole(), got, tc.whole, tc.want) + } + } +} diff --git a/internal/queue/world.go b/internal/queue/world.go index 036e86e..c215b58 100644 --- a/internal/queue/world.go +++ b/internal/queue/world.go @@ -153,9 +153,8 @@ type Config struct { Recheck time.Duration // Keep is how long a candidate's branch stands after the queue marks it landed. Keep time.Duration `json:",omitzero"` - // Spare keeps a slot free while a red candidate of several pull requests on the trunk has - // its first round running, for those the round clears, and lets the chain go on beside a - // candidate stacked on it once that round has read. + // Spare has fill use one slot fewer while a red candidate of several pull requests on the + // trunk has its first round running, so those the round clears are likelier to find one. Spare bool `json:",omitzero"` } diff --git a/internal/run/run.go b/internal/run/run.go index 3efdfe5..2d576b4 100644 --- a/internal/run/run.go +++ b/internal/run/run.go @@ -45,7 +45,7 @@ type Config struct { Gather int `json:"gather"` // a candidate built while another is building adds at least this many Timeout string `json:"timeout"` Keep string `json:"keep,omitzero"` - Spare bool `json:"spare,omitzero"` // a slot stays free for the pull requests a red candidate's first round clears + Spare bool `json:"spare,omitzero"` // one slot fewer is filled while a red candidate's first round runs Recheck string `json:"recheck"` Author struct { Name string `json:"name"` From 68f57ab7d079acbcd1c3749b207313cd7b934e2f Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Wed, 25 Nov 2026 01:45:47 -0800 Subject: [PATCH 3/3] Say where spare holds, and let the repair test see the ejection The hold needs two slots or more and a red candidate built on the trunk's tip, and ends when the trunk moves past that base as well as when the round reads or the red goes: the docs now say so. Blame says the chain waits for a pending stacked candidate after the first round has answered, which is what the planner does. In the repair sequence a candidate of #1 alone now builds slowly, so a planner that built #1 and #3 beside the stacked candidate would take #3 out at tick 9 before #1 could land and drop the evidence. --- README.md | 2 +- doc/design.md | 4 ++-- internal/queue/plan_test.go | 5 ++++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 968ca82..53882b1 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ A pull request joins the queue when it's labelled `merge-queue` and leaves when } ``` -`admit` names the statuses or check runs a pull request's own head must pass before it joins the queue, and `threads` keeps one out while a review thread is unresolved (the queue reads its first hundred, and one with more waits). `slots` is how many candidates build at once, and `max_batch` how many pull requests each adds. Where builds cost more than a short wait does, `gather` holds a candidate back while another is building until it would add that many. The builds that find a failure's cause run beside them: three for each pull request the red build added, all at once where `started` or `begins` is set. Set `started` to a word your CI's pending status carries once a build is running (Buildkite's says "started"), so the queue can find a failure's cause in one build time, not three; check runs need no setting. Where the context that judges a build appears only at its end, set `begins` to a context that answers once your CI has queued the build's jobs, such as a pipeline's first step. Where your CI reports as a GitHub App's check runs, set `app` to that App's database ID (`"app": 15368`): every context but the queue's own status is then read only from its check runs, and a commit status or another App's check run answers nothing. The queue's own status is always a commit status, and no build, probe, admit or begins context may share its name. Set `cancels` where your CI stops a build whose branch is deleted; otherwise a build the queue no longer wants keeps its slot until it reports. Set `spare` to fill one slot fewer while a failed candidate of several pull requests has its first round of builds, so the pull requests that round clears are likelier to find one free. `run.Config` in `internal/run/run.go` lists every setting. +`admit` names the statuses or check runs a pull request's own head must pass before it joins the queue, and `threads` keeps one out while a review thread is unresolved (the queue reads its first hundred, and one with more waits). `slots` is how many candidates build at once, and `max_batch` how many pull requests each adds. Where builds cost more than a short wait does, `gather` holds a candidate back while another is building until it would add that many. The builds that find a failure's cause run beside them: three for each pull request the red build added, all at once where `started` or `begins` is set. Set `started` to a word your CI's pending status carries once a build is running (Buildkite's says "started"), so the queue can find a failure's cause in one build time, not three; check runs need no setting. Where the context that judges a build appears only at its end, set `begins` to a context that answers once your CI has queued the build's jobs, such as a pipeline's first step. Where your CI reports as a GitHub App's check runs, set `app` to that App's database ID (`"app": 15368`): every context but the queue's own status is then read only from its check runs, and a commit status or another App's check run answers nothing. The queue's own status is always a commit status, and no build, probe, admit or begins context may share its name. Set `cancels` where your CI stops a build whose branch is deleted; otherwise a build the queue no longer wants keeps its slot until it reports. Set `spare`, where `slots` is two or more, to fill one slot fewer while a failed candidate of several pull requests that sat on the trunk's tip has its first round of builds, so the pull requests that round clears are likelier to find one free. `run.Config` in `internal/run/run.go` lists every setting. Set `keep` (for example, `"keep": "3m"`) to leave a landed candidate's branch standing for at least that long after the queue marks it landed. The candidate at the trunk's tip is marked at the landing, or by the next read if that tick died first. A candidate beneath it is marked once its verdict is in. One failed or errored context is a verdict, so another still running, or one that appears later, does not hold the branch longer. Where `cancels` is set, a lower candidate still running is deleted at once, as without `keep`. A retry the trunk holds is a candidate like any other. A probe, a check and a round's build are never marked. A second mark from ticks run beside each other only lengthens the stand; ticks must run one at a time. Without `keep`, the landing push has the remote compare and delete the candidate's branch, so whatever deletes that branch first stops the landing. With `keep`, the queue compares the branch just before pushing the trunk alone, so deleting the branch between that comparison and the push does not stop the landing. diff --git a/doc/design.md b/doc/design.md index 6a8a378..f5015c6 100644 --- a/doc/design.md +++ b/doc/design.md @@ -32,7 +32,7 @@ The queue uses its own mark because the trunk's own builds report on the same co ## Blame -A red candidate at the bottom of the chain is set aside with the pull requests it added, and the chain goes on without them, built on the tip. A candidate stacked on the red one is dropped once it fails too. Until then it may yet land them all. Where rounds are probes, which answer in a fraction of a build, the chain waits for that answer. Where they are whole builds it does not wait: it is built beside the candidates still running. +A red candidate at the bottom of the chain is set aside with the pull requests it added, and the chain goes on without them, built on the tip. A candidate stacked on the red one is dropped once it fails too. Until then it may yet land them all. Where rounds are probes, which answer in a fraction of a build, the chain waits while a candidate stacked on the red one is still building, after the first round has answered too. Where they are whole builds it does not wait: it is built beside the candidates still running. The red candidate is narrowed by rounds: builds of some of its prefixes. A round's build never lands. What the rounds have said so far is a span, from the highest prefix that passed to the lowest above it that failed. The culprit is in it. A round is read once every build in it has reported, and the rounds are read in order. @@ -116,7 +116,7 @@ The queue posts `pending` on everything it pushes, which gives each wait a start ## Dials -`slots` is how many candidates are under test at once. A round's builds are beside them and are not counted. With `spare`, while a red candidate of several pull requests on the trunk has its first round running, the chain takes one slot fewer, so those the round clears are likelier to find one free. It limits how many slots the chain fills and does not push the cleared pull requests: `gather` may still hold one back, and a landing or an ejection in the same tick ends the plan first. The round's read ends the hold, whatever it says, as does the red candidate going. A round that never reports reads as lost after `timeout`; where `timeout` is zero nothing ends the round, and the slot stays held while the red stands. `max_batch` is how many pull requests a candidate adds, and `gather` how few it may add while another is building: fewer builds for each landing, for a wait of at most one build. `eval/main.go` holds the values each shape ships with, and the hillclimb moves them. +`slots` is how many candidates are under test at once. A round's builds are beside them and are not counted. With `spare` and two slots or more, while a red candidate of several pull requests built on the trunk's tip has its first round running, the chain takes one slot fewer, so those the round clears are likelier to find one free. It limits how many slots the chain fills and does not push the cleared pull requests: `gather` may still hold one back, and a landing or an ejection in the same tick ends the plan first. The round's read ends the hold, whatever it says, as does the red candidate going or the trunk moving past its base. A round that never reports reads as lost after `timeout`; where `timeout` is zero nothing ends the round, and the slot stays held until one of those. `max_batch` is how many pull requests a candidate adds, and `gather` how few it may add while another is building: fewer builds for each landing, for a wait of at most one build. `eval/main.go` holds the values each shape ships with, and the hillclimb moves them. ## Not in the engine diff --git a/internal/queue/plan_test.go b/internal/queue/plan_test.go index f8cfb3b..fb75e68 100644 --- a/internal/queue/plan_test.go +++ b/internal/queue/plan_test.go @@ -1114,7 +1114,8 @@ func TestASuspectALaterArrivalRepairsLeavesOnTheOldTreesOrLandsOnTheNewTrunk(t * // and #3 fail where that of all three passes. The candidate of all three is stacked on the red // one of #1 and #2 and builds slowly, while #2's own rounds outlast it. Probes keep the chain // waiting for it, with spare or without: #1 and #3 are never built without #2, nobody leaves, -// and the stacked candidate lands all three at tick 20. +// and the stacked candidate lands all three at tick 20. A candidate of #1 alone would build +// slowly too, so that were #1 and #3 built without #2, #3 would leave before #1 landed. func TestTheChainWaitsForACandidateStackedOnARedOneWhereItMayLandAllOfThem(t *testing.T) { takes := func(b Build) int { switch { @@ -1126,6 +1127,8 @@ func TestTheChainWaitsForACandidateStackedOnARedOneWhereItMayLandAllOfThem(t *te return 18 case holds(b, 2): return 2 + case !holds(b, 3): + return 20 } return 1 }