From 538f5b0b8342610f5fdcfbd414c1de6a6a873959 Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Sat, 28 Nov 2026 09:23:24 -0800 Subject: [PATCH 1/3] A probe's pass counts only if it ran every job its red failed on A probe runs only the jobs named in its Banyan-Probe-Failed trailers, the red candidate's Failed as read on the tick that pushed it. Where a verdict can go red before the build ends, Failed grows on later ticks, and a probe pushed before it grew ran fewer jobs than the build failed: its pass cleared prefixes, read the red as a flake and stood as the middle build of a fair test, and an innocent pull request could leave on one flake. A build now carries Runs, the contexts a probe was asked to run, read back from its trailers in order. A probe covers its red when every context in the red's Failed, as this tick reads it, is among them. One that does not is read as a build with no verdict, as a lost one is, without the recheck wait a lost one gets: its pass and its failure say nothing, nothing waits for it while it runs, and the next round, pushed with the list as it now stands, goes out on the tick that reads the longer list. A whole build and a candidate always cover. Runs is nil on a probe recorded before probes named their jobs, which covers as before, and empty on one asked to run none, which covers only a red whose Failed is still empty. Where Failed never changes, and where probes are off, every tick plans as before. --- internal/git/git.go | 6 +- internal/git/git_test.go | 35 +++ internal/queue/plan.go | 11 +- internal/queue/plan_test.go | 444 ++++++++++++++++++++++++++++++++++++ internal/queue/world.go | 18 +- 5 files changed, 511 insertions(+), 3 deletions(-) diff --git a/internal/git/git.go b/internal/git/git.go index 528eb2a..24b364b 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -52,6 +52,7 @@ type commit struct { merges int // the pull request it merges, by its last line trailer bool // an older release wrote the number as a Banyan-Merges trailer instead trailers map[string]string + probes []string // the contexts its Banyan-Probe-Failed trailers name, in order } func (r *Repo) git(args ...string) (string, error) { return r.run("", "", args...) } @@ -152,6 +153,9 @@ func (r *Repo) read(sha string) (commit, error) { for _, l := range own { if k, v, ok := strings.Cut(l, ": "); ok && strings.HasPrefix(k, "Banyan-") { c.trailers[k] = v + if k == "Banyan-Probe-Failed" { + c.probes = append(c.probes, v) + } } } if n, err := strconv.Atoi(c.trailers["Banyan-Merges"]); err == nil && c.merges == 0 { @@ -294,7 +298,7 @@ func (r *Repo) build(sha, trunk queue.SHA) (queue.Build, error) { at := sha switch { case top.trailers["Banyan-Probe-Of"] != "": - b.Kind, at = queue.Probe, top.parents[0] + b.Kind, b.Runs, at = queue.Probe, append([]string{}, top.probes...), top.parents[0] case top.trailers["Banyan-Check"] != "": b.Kind, at = queue.Check, top.parents[0] case top.trailers["Banyan-Try"] != "" && top.merges > 0 && len(top.parents) == 2: diff --git a/internal/git/git_test.go b/internal/git/git_test.go index 0411e87..804276d 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -186,6 +186,41 @@ func TestBuildsAreReadBackAsPushed(t *testing.T) { } } +// A probe reads back the contexts it was asked to run, each of its trailers in order, and one +// that names none reads back as asked to run none: an empty list, not a missing one. +func TestAProbeReadsBackEveryContextItWasAskedToRun(t *testing.T) { + o := newOrigin(t) + a := o.pr(1, "a", "a\n") + trunk := o.fetch() + one, _ := o.Repo.Merge(trunk, a, "") + note := "Banyan-Try: 1\nBanyan-Round: 1\nBanyan-Of: " + one + "\nBanyan-Probe-Of: " + one + two := o.Repo.Mark(one, note+"\nBanyan-Probe-Failed: linux (ci)\nBanyan-Probe-Failed: build: mac") + none := o.Repo.Mark(trunk, note) + for _, sha := range []queue.SHA{two, none} { + if err := o.Repo.Push(sha); err != nil { + t.Fatal(err) + } + } + o.fetch() + builds, err := o.Repo.Builds(trunk) + if err != nil { + t.Fatal(err) + } + runs := map[queue.SHA][]string{} + for _, b := range builds { + if b.Kind != queue.Probe { + t.Errorf("%.12s read back as kind %d, where it is a probe", b.SHA, b.Kind) + } + runs[b.SHA] = b.Runs + } + if want := []string{"linux (ci)", "build: mac"}; !slices.Equal(runs[two], want) { + t.Errorf("a probe of two contexts reads back as running %q, want %q", runs[two], want) + } + if runs[none] == nil || len(runs[none]) != 0 { + t.Errorf("a probe that names no context reads back as running %#v, want an empty list", runs[none]) + } +} + func TestLandingMovesTheTrunkAndTakesTheBranch(t *testing.T) { o := newOrigin(t) a, b := o.pr(1, "a", "a\n"), o.pr(2, "b", "b\n") diff --git a/internal/queue/plan.go b/internal/queue/plan.go index 1c6e657..2b9e69e 100644 --- a/internal/queue/plan.go +++ b/internal/queue/plan.go @@ -316,6 +316,13 @@ func (p *plan) weigh(r *red) { if b.Round == 0 && b.Verdict == Failure { r.sha, r.failed = b.SHA, b.Failed } + } + for _, b := range r.builds { + // A probe pushed before more of the red's jobs had failed says nothing of it, and the + // next round, pushed with every one, is what the red is judged on. + if p.c.Probes && b.Round > 0 && !b.covers(r.failed) { + b.Verdict, b.blind = Error, true + } if b.Round > 0 && b.Pushed.After(r.pushed) { r.pushed = b.Pushed } @@ -332,7 +339,7 @@ func (p *plan) weigh(r *red) { r.running = true return case b.Verdict == Error: - r.void = true + r.void = r.void || !b.blind case b.Verdict == Success: g = max(g, j) } @@ -593,6 +600,8 @@ func (p *plan) evidence(r *red, j int, whole bool) Build { } if whole { b.Kind, red = Check, "" + } else { + b.Runs = append([]string{}, r.failed...) } b.SHA = p.fresh(func(n int) SHA { return p.w.Repo.Mark(parent, note(b.Kind, b.Round, r.of, red, r.failed, n)) }) return b diff --git a/internal/queue/plan_test.go b/internal/queue/plan_test.go index dfe6def..33d61fb 100644 --- a/internal/queue/plan_test.go +++ b/internal/queue/plan_test.go @@ -724,3 +724,447 @@ func TestKeepZeroAddsNothingToARecording(t *testing.T) { t.Fatalf("zero config JSON = %s, want %s", c, want) } } + +// ticks is a queue played tick by tick: each world is the last one with the planner's own +// pushes, deletes, landings and ejections applied, and whatever CI said since. Its git +// remembers what was pushed, so a build begun again is a new commit. +type ticks struct { + t *testing.T + w World + c Config + seen map[SHA]bool + acts [][]Action +} + +func (s *ticks) Merge(parent SHA, pr PR, note string) (SHA, bool) { + return commits{}.Merge(parent, pr, note) +} +func (s *ticks) Mark(parent SHA, note string) SHA { return commits{}.Mark(parent, note) } +func (s *ticks) Seen(sha SHA) bool { return s.seen[sha] } + +var probing = Config{Slots: 2, MaxBatch: 3, Probes: true, Timeout: 90 * time.Minute, Recheck: 10 * time.Minute} + +func newTicks(t *testing.T, numbers ...int) *ticks { + now := time.Date(2026, 11, 28, 9, 0, 0, 0, time.UTC) + s := &ticks{t: t, c: probing, seen: map[SHA]bool{}} + s.w = World{Now: now, Trunk: "trunk", Repo: s, PRs: queuedAt(now.Add(-time.Hour), numbers...)} + return s +} + +// tick plans the world a minute on, and applies what the plan says. CI begins every build +// it is pushed at once. +func (s *ticks) tick() []Action { + s.w.Now = s.w.Now.Add(time.Minute) + acts := Plan(s.w, s.c) + for _, a := range acts { + switch a.Do { + case "push": + b := a.Build + b.Started = s.w.Now + s.seen[b.SHA] = true + s.w.Builds = append(s.w.Builds, b) + case "delete": + s.w.Builds = slices.DeleteFunc(s.w.Builds, func(b Build) bool { return b.SHA == a.SHA }) + case "land": + s.w.Trunk = a.SHA + s.w.Builds = slices.DeleteFunc(s.w.Builds, func(b Build) bool { return b.SHA == a.SHA }) + s.w.PRs = slices.DeleteFunc(s.w.PRs, func(pr PR) bool { + return slices.ContainsFunc(a.Members, func(m Member) bool { return m.Number == pr.Number }) + }) + case "eject": + s.w.PRs = slices.DeleteFunc(s.w.PRs, func(pr PR) bool { return pr.Number == a.PR }) + } + } + s.acts = append(s.acts, acts) + return acts +} + +// candidate is the first candidate of the pull requests named that stands. +func (s *ticks) candidate(numbers ...int) Build { + s.t.Helper() + for _, b := range s.w.Builds { + if b.Kind == Candidate && b.Round == 0 && fmt.Sprint(chainNumbers(b.Chain)) == fmt.Sprint(numbers) { + return b + } + } + s.t.Fatalf("no candidate of %v stands: %+v", numbers, s.w.Builds) + return Build{} +} + +// fail says the build failed on the jobs named, as CI reads it now. +func (s *ticks) fail(sha SHA, jobs ...string) { + for i, b := range s.w.Builds { + if b.SHA == sha { + s.w.Builds[i].Verdict, s.w.Builds[i].Failed = Failure, jobs + } + } +} + +// ci reports on every build still running but the one named: it fails a build on the jobs +// fails names for it, and passes it where they are none. +func (s *ticks) ci(except SHA, fails func(Build) []string) { + for i, b := range s.w.Builds { + if b.SHA == except || b.Verdict != Pending { + continue + } + s.w.Builds[i].Verdict, s.w.Builds[i].Failed = Success, nil + if jobs := fails(b); len(jobs) > 0 { + s.w.Builds[i].Verdict, s.w.Builds[i].Failed = Failure, jobs + } + } +} + +func chainNumbers(chain []Member) (ns []int) { + for _, m := range chain { + ns = append(ns, m.Number) + } + return ns +} + +// ran says CI ran job on the build, by what its commit says: a whole build runs every job, +// and a probe the jobs its note names. +func ran(b Build, job string) bool { + return b.Kind != Probe || strings.Contains(b.SHA, "Banyan-Probe-Failed: "+job) +} + +// breaks fails a build that holds the culprit on job, where it ran it. +func breaks(culprit int, job string) func(Build) []string { + return func(b Build) []string { + if slices.ContainsFunc(b.Chain, func(m Member) bool { return m.Number == culprit }) && ran(b, job) { + return []string{job} + } + return nil + } +} + +func (s *ticks) outcome() (landed []int, ejected []int) { + for _, acts := range s.acts { + for _, a := range acts { + switch a.Do { + case "land": + landed = append(landed, chainNumbers(a.Members)...) + case "eject": + ejected = append(ejected, a.PR) + } + } + } + slices.Sort(landed) + return landed, ejected +} + +// pushed is every build the queue pushed, by its SHA. +func (s *ticks) pushed() map[SHA]Build { + builds := map[SHA]Build{} + for _, acts := range s.acts { + for _, a := range acts { + if a.Do == "push" { + builds[a.Build.SHA] = a.Build + } + } + } + return builds +} + +// fewer is the evidence of each ejection whose pass without the pull request ran fewer of +// the jobs than the failure with it after. +func (s *ticks) fewer(jobs ...string) (bad [][]SHA) { + builds := s.pushed() + for _, acts := range s.acts { + for _, a := range acts { + if a.Do != "eject" { + continue + } + pass, after := builds[a.Evidence[0]], builds[a.Evidence[2]] + if slices.ContainsFunc(jobs, func(j string) bool { return ran(after, j) && !ran(pass, j) }) { + bad = append(bad, a.Evidence) + } + } + } + return bad +} + +// pushedRound is the prefixes of the red candidate's round pushed on the tick, with the jobs each +// probe among them ran, as "1:linux,mac" and a whole build as "1:whole". +func pushedRound(acts []Action, of SHA, jobs ...string) (out []string) { + for _, a := range acts { + if a.Do != "push" || a.Build.Round == 0 || a.Build.Of != of { + continue + } + var ran []string + for _, j := range jobs { + if strings.Contains(a.Build.SHA, "Banyan-Probe-Failed: "+j) { + ran = append(ran, j) + } + } + if a.Build.Kind != Probe { + ran = []string{"whole"} + } + out = append(out, fmt.Sprintf("%d:%s", len(a.Build.Chain), strings.Join(ran, ","))) + } + return out +} + +// #1 breaks mac. The candidate of #1 and #2 reads failed on linux, a flake, and two ticks later +// on linux and mac. Its first probes ran linux alone, and a pass among them says nothing of a +// failure on mac. Where linux flakes once more on #2's first probe, #2's test would read: #1 +// and #2 failed on linux, #1 alone passed running linux alone, and #1 and #2 failed again on +// mac. It is not read so, and only #1 leaves, as it does where linux does not flake again. +func TestAPassThatRanFewerJobsThanTheRedFailedOnBlamesNobody(t *testing.T) { + for _, tc := range []struct { + name string + flakes int + }{ + {"linux flakes once more", 1}, + {"linux does not flake again", 0}, + } { + t.Run(tc.name, func(t *testing.T) { + s := newTicks(t, 1, 2) + s.c.MaxBatch = 2 + s.tick() + c := s.candidate(1, 2) + s.fail(c.SHA, "linux") + flakes := tc.flakes + for i := 1; i < 30; i++ { + s.tick() + if i == 3 { + s.fail(c.SHA, "linux", "mac") + } + s.ci(c.SHA, func(b Build) []string { + if b.Kind == Probe && len(b.Chain) == 2 && flakes > 0 && !ran(b, "mac") { + flakes-- + return []string{"linux"} + } + return breaks(1, "mac")(b) + }) + } + if flakes > 0 { + t.Fatalf("linux flaked %d times fewer than the case says", flakes) + } + if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[2] [1]" { + t.Errorf("landed %v and ejected %v, where #2 lands and only #1 leaves", landed, ejected) + } + if bad := s.fewer("linux", "mac"); len(bad) > 0 { + t.Errorf("ejected on %v, where the pass without it ran fewer jobs than the failure after it", bad) + } + }) + } +} + +// #1 of three breaks mac. The candidate reads failed on linux, and its first round of probes, +// running linux alone, all pass. By the tick that reads them the candidate has failed on mac +// too, so they clear nobody: the round that tick pushes spans every suspect again, the trunk +// alone included, rather than putting #3 to a test on their pass. #1 leaves on probes that ran +// both, and #2 and #3 land. +func TestAPassThatRanFewerJobsClearsNothing(t *testing.T) { + s := newTicks(t, 1, 2, 3) + s.tick() + c := s.candidate(1, 2, 3) + s.fail(c.SHA, "linux") + var grown []string + for i := 1; i < 40; i++ { + acts := s.tick() + if i == 2 { + grown = pushedRound(acts, c.SHA, "linux", "mac") + } + if i == 1 { + s.fail(c.SHA, "linux", "mac") + } + s.ci(c.SHA, breaks(1, "mac")) + } + if want := "[1:linux,mac 2:linux,mac 0:linux,mac]"; fmt.Sprint(grown) != want { + t.Errorf("the tick that read the mac failure pushed %v, want %s", grown, want) + } + if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[2 3] [1]" { + t.Errorf("landed %v and ejected %v, where #2 and #3 land and #1 leaves", landed, ejected) + } + if bad := s.fewer("linux", "mac"); len(bad) > 0 { + t.Errorf("ejected on %v, where the pass without it ran fewer jobs than the failure after it", bad) + } +} + +// #1 breaks mac, and its candidate reads failed on linux. A probe of #1's tree, running linux +// alone, passes, and by the tick that reads it the candidate has failed on mac too. That pass +// is no reading of a flake, so no whole build of the red tree is pushed: the next probe runs +// both, fails, and #1 leaves. +func TestProbesThatPassedRunningFewerJobsAreNoFlake(t *testing.T) { + s := newTicks(t, 1) + s.tick() + c := s.candidate(1) + s.fail(c.SHA, "linux") + var grown []string + for i := 1; i < 20; i++ { + acts := s.tick() + if i == 2 { + grown = pushedRound(acts, c.SHA, "linux", "mac") + } + s.ci(c.SHA, breaks(1, "mac")) + if i == 1 { + s.fail(c.SHA, "linux", "mac") + } + } + if fmt.Sprint(grown) != "[1:linux,mac]" { + t.Errorf("the tick that read the mac failure pushed %v, where #1's tree is probed on both", grown) + } + for _, b := range s.pushed() { + if b.Kind == Check { + t.Errorf("pushed a whole build of %v in round %d, where no probe that ran mac passed", chainNumbers(b.Chain), b.Round) + } + } + if _, ejected := s.outcome(); fmt.Sprint(ejected) != "[1]" { + t.Errorf("ejected %v, where #1 leaves", ejected) + } +} + +// #2 of three breaks windows. The candidate reads failed on linux, then on mac too, then on +// windows too, and each tick that reads the longer list pushes a round that runs all of it. +// #2 leaves, #1 and #3 land, and the queue is empty. +func TestAListThatGrowsTwiceIsJudgedOnTheLast(t *testing.T) { + s := newTicks(t, 1, 2, 3) + s.tick() + c := s.candidate(1, 2, 3) + s.fail(c.SHA, "linux") + jobs := []string{"linux", "mac", "windows"} + grows := map[int][]string{1: jobs[:2], 3: jobs} + for i := 1; i < 60; i++ { + acts := s.tick() + if read := grows[i-1]; read != nil { + want := strings.Join(read, ",") + pushed := pushedRound(acts, c.SHA, jobs...) + if len(pushed) == 0 || slices.ContainsFunc(pushed, func(p string) bool { return !strings.HasSuffix(p, ":"+want) }) { + t.Errorf("the tick that read the failure on %s pushed %v, where each probe runs all of it", want, pushed) + } + } + if read := grows[i]; read != nil { + s.fail(c.SHA, read...) + } + s.ci(c.SHA, breaks(2, "windows")) + } + if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[1 3] [2]" { + t.Errorf("landed %v and ejected %v, where #1 and #3 land and #2 leaves", landed, ejected) + } + if len(s.w.PRs) > 0 || len(s.w.Builds) > 0 { + t.Errorf("the queue still holds %v and builds %+v", s.w.PRs, s.w.Builds) + } + if bad := s.fewer(jobs...); len(bad) > 0 { + t.Errorf("ejected on %v, where the pass without it ran fewer jobs than the failure after it", bad) + } +} + +// #1 breaks mac, and its candidate reads failed on linux. Two rounds of #1's test are out, +// running linux alone, when the candidate fails on mac too. The tick that reads it pushes the +// next round at once, running both, without waiting for them, as it does where three are out +// and the rounds have gone as far past the last one read as they may. When they report, #1's +// tree passing on linux alone among them, none is read: no whole build of the red tree is +// pushed on that pass, and #1 leaves on builds pushed since. +func TestRoundsOutWhenTheListGrowsAreNotRead(t *testing.T) { + for _, rounds := range []int{2, 3} { + t.Run(fmt.Sprint(rounds, " out"), func(t *testing.T) { + s := newTicks(t, 1) + s.tick() + c := s.candidate(1) + s.fail(c.SHA, "linux") + for range rounds { + s.tick() + } + out := map[SHA]bool{} + for _, b := range s.w.Builds { + if b.Round > 0 { + out[b.SHA] = true + } + } + if len(out) != rounds { + t.Fatalf("%d rounds' builds are out, want %d: %+v", len(out), rounds, s.w.Builds) + } + s.fail(c.SHA, "linux", "mac") + if pushed := pushedRound(s.tick(), c.SHA, "linux", "mac"); fmt.Sprint(pushed) != "[1:linux,mac]" { + t.Errorf("the tick that read the mac failure pushed %v, where #1's test begins again at once on both", pushed) + } + for range 20 { + s.ci(c.SHA, breaks(1, "mac")) + s.tick() + } + for _, b := range s.pushed() { + if b.Kind == Check { + t.Errorf("pushed a whole build of %v in round %d, on a pass that ran linux alone", chainNumbers(b.Chain), b.Round) + } + } + for _, acts := range s.acts { + for _, a := range acts { + if a.Do == "eject" && slices.ContainsFunc(a.Evidence, func(sha SHA) bool { return out[sha] }) { + t.Errorf("ejected #%d on %v, which holds a round pushed before the list grew", a.PR, a.Evidence) + } + } + } + if _, ejected := s.outcome(); fmt.Sprint(ejected) != "[1]" { + t.Errorf("ejected %v, where #1 leaves", ejected) + } + }) + } +} + +// #1's candidate failed on linux, and a probe of its tree passed. Where the probe was recorded +// before probes said what they ran, it is taken to have run linux, as it was: the pass reads +// as a flake and a whole build of the tree decides. Where it was asked to run nothing, as one +// pushed while the candidate had named no job, it says nothing of linux, and #1's tree is +// probed again; of a candidate that still names no job it says all there is, as before. Each +// holds through a recording's JSON. Where probes are off, what a probe ran changes nothing: +// one of the trunk alone that failed still says the trunk is red. +func TestAProbeThatSaysNothingOfWhatItRanIsNotOneAskedToRunNothing(t *testing.T) { + now := time.Date(2026, 11, 28, 12, 0, 0, 0, time.UTC) + one := []Member{member(1, "m1")} + plan := func(c Config, failed, runs []string, probed []Member, v Verdict) (pushed, decoded []string) { + w := World{Now: now.Add(time.Hour), Trunk: "trunk", PRs: queuedAt(now, 1), + Builds: []Build{ + {SHA: "m1", Base: "trunk", Chain: one, Verdict: Failure, Failed: failed, Pushed: now, Started: now}, + {SHA: "p1", Kind: Probe, Base: "trunk", Chain: probed, Round: 1, Of: "m1", Verdict: v, Runs: runs, Pushed: now, Started: now}, + }} + data, err := json.Marshal(w) + if err != nil { + t.Fatal(err) + } + var read World + if err := json.Unmarshal(data, &read); err != nil { + t.Fatal(err) + } + read.Repo = commits{} + for _, a := range Plan(read, c) { + switch a.Do { + case "push": + pushed = append(pushed, fmt.Sprintf("kind %d of %d", a.Build.Kind, len(a.Build.Chain))) + case "note": + if !c.Probes { + pushed = append(pushed, a.Text) + } + } + } + return pushed, read.Builds[1].Runs + } + linux := []string{"linux"} + absent, runs := plan(probing, linux, nil, one, Success) + if runs != nil { + t.Errorf("a probe with no list read back as running %#v, where it says nothing", runs) + } + if want := fmt.Sprintf("[kind %d of 1]", Check); fmt.Sprint(absent) != want { + t.Errorf("a probe that says nothing of what it ran pushed %v, want %s: a whole build, as before", absent, want) + } + empty, runs := plan(probing, linux, []string{}, one, Success) + if runs == nil || len(runs) != 0 { + t.Errorf("a probe asked to run nothing read back as running %#v, where it is an empty list", runs) + } + if want := fmt.Sprintf("[kind %d of 1]", Probe); fmt.Sprint(empty) != want { + t.Errorf("a probe asked to run nothing pushed %v, want %s: #1's tree probed again on linux", empty, want) + } + if covering, _ := plan(probing, linux, linux, one, Success); fmt.Sprint(covering) != fmt.Sprint(absent) { + t.Errorf("a probe that ran linux pushed %v, where one that says nothing pushed %v", covering, absent) + } + said, _ := plan(probing, nil, nil, one, Success) + if nothing, _ := plan(probing, nil, []string{}, one, Success); fmt.Sprint(nothing) != fmt.Sprint(said) { + t.Errorf("a probe asked to run nothing of a candidate that named no job pushed %v, where one that says nothing pushed %v", nothing, said) + } + off := probing + off.Probes = false + said, _ = plan(off, linux, nil, nil, Failure) + if unsaid, _ := plan(off, linux, []string{}, nil, Failure); fmt.Sprint(unsaid) != fmt.Sprint(said) { + t.Errorf("with probes off, a failed probe of the trunk asked to run nothing did %v, where one that says nothing did %v", unsaid, said) + } +} diff --git a/internal/queue/world.go b/internal/queue/world.go index 1137ae2..ef052ad 100644 --- a/internal/queue/world.go +++ b/internal/queue/world.go @@ -4,7 +4,10 @@ // the same world always gives the same actions, and a recorded world replays exactly. package queue -import "time" +import ( + "slices" + "time" +) // SHA names a commit. type SHA = string @@ -79,6 +82,10 @@ type Build struct { // Failed names the contexts that failed on the commit beside its verdict's own: the jobs // of its build, where the repository's CI reports each as a context. Failed []string + // Runs names the contexts a probe was asked to run, as its commit says. Nil is a probe + // that says nothing of them, as one recorded before probes named them, and is taken to + // run every one; empty is a probe asked to run none. + Runs []string `json:",omitzero"` // Pushed is the time of the queue's latest status on this commit. Pushed time.Time // Started is when CI began running the build: zero until it has, and always zero where @@ -86,6 +93,15 @@ type Build struct { Started time.Time // late marks a build read as lost for outliving the timeout with no verdict of its own. late bool + // blind marks a probe read as giving no verdict for running fewer contexts than its red + // candidate has failed on. + blind bool +} + +// covers says the build ran every context in failed: a whole build does, and so does a probe +// that does not say what it ran. +func (b Build) covers(failed []string) bool { + return b.Kind != Probe || b.Runs == nil || !slices.ContainsFunc(failed, func(f string) bool { return !slices.Contains(b.Runs, f) }) } // Tests is the prefix whose tree this build has. From 775d6b2c03ad06e6ec0994f1cb1f2a298f9fab2e Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Sat, 28 Nov 2026 10:06:14 -0800 Subject: [PATCH 2/3] A probe counts only if it was asked to run exactly what its red failed on A probe that ran a subset of its red's Failed was set aside, and one that ran a superset counted. Where Failed shrinks again, as a job retried to green leaves it, that let rounds count beside each other that never ran the same jobs: a failure that ran linux and mac stood with a pass that ran linux alone. And a round set aside while CI had not begun it let the next round go out past it, so when the list shrank back the three could read as a fair test though they began out of order, and an innocent pull request left. A probe now counts for its red only where the contexts it was asked to run are the same set as the red's Failed on the tick that reads it, whatever their order. Growth and shrinkage are alike: a probe that does not count gives no verdict, nothing waits for it, and the next round goes out with the list as it stands. Three rounds in a row that count ran the same list, so each was pushed when the round before ran the list then standing and so had truly begun. Names are compared as a probe's trailers read them back: up to the first line break, without the whitespace at either end, which git loses from the end of a message, and none left empty. The trailers are written the same way, so a context whose name ends in a space no longer sends a round out on every tick. The rule holds whatever the Probes setting, which chooses only what is pushed next: a probe in flight when probes are turned off counts only for its own list. A whole build and a candidate always count, as does a probe recorded before probes named their jobs. --- internal/git/git_test.go | 58 +++++++ internal/queue/plan.go | 14 +- internal/queue/plan_test.go | 327 +++++++++++++++++++++++++++++++++--- internal/queue/world.go | 29 +++- 4 files changed, 392 insertions(+), 36 deletions(-) diff --git a/internal/git/git_test.go b/internal/git/git_test.go index 804276d..772fe13 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -221,6 +221,64 @@ func TestAProbeReadsBackEveryContextItWasAskedToRun(t *testing.T) { } } +// The candidate of #1 failed on contexts whose names end in a space, or hold nothing else, or +// a line break. The probe a tick pushes for it reads back as asked to run them as its trailers +// can say them, wherever they stand, and while CI has not begun it, a tick on the unchanged +// world pushes nothing more. +func TestAProbeOfContextsWhoseNamesEndInASpaceReadsBackAsAskedToRunThem(t *testing.T) { + for _, tc := range []struct{ failed, want []string }{ + {[]string{"linux "}, []string{"linux"}}, + {[]string{"mac ", "linux "}, []string{"mac", "linux"}}, + {[]string{" "}, []string{}}, + {[]string{"build\nlinux"}, []string{"build"}}, + } { + failed, want := tc.failed, tc.want + t.Run(fmt.Sprintf("%q", failed), func(t *testing.T) { + o := newOrigin(t) + a := o.pr(1, "a", "a\n") + trunk := o.fetch() + one, _ := o.Repo.Merge(trunk, a, "") + if err := o.Repo.Push(one); err != nil { + t.Fatal(err) + } + c := queue.Config{Slots: 2, MaxBatch: 3, Probes: true} + tick := func() (pushed []queue.Build) { + o.fetch() + builds, err := o.Repo.Builds(trunk) + if err != nil { + t.Fatal(err) + } + for i, b := range builds { + if b.SHA == one { + builds[i].Verdict, builds[i].Failed = queue.Failure, failed + } + } + for _, act := range queue.Plan(queue.World{Trunk: trunk, PRs: []queue.PR{a}, Builds: builds, Repo: o.Repo}, c) { + if act.Do == "push" { + if err := o.Repo.Push(act.Build.SHA); err != nil { + t.Fatal(err) + } + pushed = append(pushed, act.Build) + } + } + return pushed + } + if first := tick(); len(first) != 1 || first[0].Kind != queue.Probe { + t.Fatalf("the first tick pushed %+v, want one probe", first) + } + if again := tick(); len(again) > 0 { + t.Errorf("a tick on the unchanged world pushed %d more builds", len(again)) + } + builds, _ := o.Repo.Builds(trunk) + for _, b := range builds { + if b.Kind == queue.Probe && !slices.Equal(b.Runs, want) { + t.Errorf("the probe reads back as asked to run %q, want %q", b.Runs, want) + } + } + }) + } +} + func TestLandingMovesTheTrunkAndTakesTheBranch(t *testing.T) { o := newOrigin(t) a, b := o.pr(1, "a", "a\n"), o.pr(2, "b", "b\n") diff --git a/internal/queue/plan.go b/internal/queue/plan.go index 2b9e69e..025564a 100644 --- a/internal/queue/plan.go +++ b/internal/queue/plan.go @@ -318,10 +318,10 @@ func (p *plan) weigh(r *red) { } } for _, b := range r.builds { - // A probe pushed before more of the red's jobs had failed says nothing of it, and the - // next round, pushed with every one, is what the red is judged on. - if p.c.Probes && b.Round > 0 && !b.covers(r.failed) { - b.Verdict, b.blind = Error, true + // A probe asked to run other jobs than the red's says nothing of it, and the next + // round, pushed with the jobs as they now stand, is what the red is judged on. + if !b.counts(r.failed) { + b.Verdict = Error } if b.Round > 0 && b.Pushed.After(r.pushed) { r.pushed = b.Pushed @@ -339,7 +339,7 @@ func (p *plan) weigh(r *red) { r.running = true return case b.Verdict == Error: - r.void = r.void || !b.blind + r.void = r.void || b.counts(r.failed) case b.Verdict == Success: g = max(g, j) } @@ -601,7 +601,7 @@ func (p *plan) evidence(r *red, j int, whole bool) Build { if whole { b.Kind, red = Check, "" } else { - b.Runs = append([]string{}, r.failed...) + b.Runs = append([]string{}, named(r.failed)...) } b.SHA = p.fresh(func(n int) SHA { return p.w.Repo.Mark(parent, note(b.Kind, b.Round, r.of, red, r.failed, n)) }) return b @@ -691,7 +691,7 @@ func note(kind Kind, round int, of, red SHA, failed []string, try int) string { s += "\nBanyan-Check: whole" case Probe: s += "\nBanyan-Probe-Of: " + red - for _, context := range failed { + for _, context := range named(failed) { s += "\nBanyan-Probe-Failed: " + context } } diff --git a/internal/queue/plan_test.go b/internal/queue/plan_test.go index 33d61fb..4ec3e54 100644 --- a/internal/queue/plan_test.go +++ b/internal/queue/plan_test.go @@ -5,6 +5,7 @@ import ( "fmt" "reflect" "slices" + "strconv" "strings" "testing" "time" @@ -727,13 +728,15 @@ func TestKeepZeroAddsNothingToARecording(t *testing.T) { // ticks is a queue played tick by tick: each world is the last one with the planner's own // pushes, deletes, landings and ejections applied, and whatever CI said since. Its git -// remembers what was pushed, so a build begun again is a new commit. +// remembers what was pushed, so a build begun again is a new commit. While held, CI begins +// none of the builds it is pushed. type ticks struct { t *testing.T w World c Config seen map[SHA]bool acts [][]Action + held bool } func (s *ticks) Merge(parent SHA, pr PR, note string) (SHA, bool) { @@ -752,7 +755,7 @@ func newTicks(t *testing.T, numbers ...int) *ticks { } // tick plans the world a minute on, and applies what the plan says. CI begins every build -// it is pushed at once. +// it is pushed at once, unless held. A probe reads back the contexts its commit names. func (s *ticks) tick() []Action { s.w.Now = s.w.Now.Add(time.Minute) acts := Plan(s.w, s.c) @@ -760,7 +763,12 @@ func (s *ticks) tick() []Action { switch a.Do { case "push": b := a.Build - b.Started = s.w.Now + if !s.held { + b.Started = s.w.Now + } + if b.Kind == Probe { + b.Runs = asked(b.SHA) + } s.seen[b.SHA] = true s.w.Builds = append(s.w.Builds, b) case "delete": @@ -800,6 +808,41 @@ func (s *ticks) fail(sha SHA, jobs ...string) { } } +// begin says CI has begun the build. +func (s *ticks) begin(sha SHA) { + for i, b := range s.w.Builds { + if b.SHA == sha { + s.w.Builds[i].Started = s.w.Now + } + } +} + +// asked is the contexts a probe's commit names, as git reads them back: a message loses the +// whitespace at its end, which is the end of its last trailer. +func asked(sha SHA) []string { + note, err := strconv.Unquote(sha[strings.LastIndex(sha, `,"`)+1 : len(sha)-1]) + if err != nil { + panic(err) + } + runs := []string{} + for _, l := range strings.Split(strings.TrimSpace(note), "\n") { + if context, ok := strings.CutPrefix(l, "Banyan-Probe-Failed: "); ok { + runs = append(runs, context) + } + } + return runs +} + +// evidence is the round's builds pushed among the actions. +func evidence(acts []Action) (builds []Build) { + for _, a := range acts { + if a.Do == "push" && a.Build.Round > 0 { + builds = append(builds, a.Build) + } + } + return builds +} + // ci reports on every build still running but the one named: it fails a build on the jobs // fails names for it, and passes it where they are none. func (s *ticks) ci(except SHA, fails func(Build) []string) { @@ -865,17 +908,14 @@ func (s *ticks) pushed() map[SHA]Build { return builds } -// fewer is the evidence of each ejection whose pass without the pull request ran fewer of -// the jobs than the failure with it after. -func (s *ticks) fewer(jobs ...string) (bad [][]SHA) { +// differ is the evidence of each ejection whose builds did not all run the same jobs. +func (s *ticks) differ(jobs ...string) (bad [][]SHA) { builds := s.pushed() for _, acts := range s.acts { for _, a := range acts { - if a.Do != "eject" { - continue - } - pass, after := builds[a.Evidence[0]], builds[a.Evidence[2]] - if slices.ContainsFunc(jobs, func(j string) bool { return ran(after, j) && !ran(pass, j) }) { + if a.Do == "eject" && slices.ContainsFunc(jobs, func(j string) bool { + return slices.ContainsFunc(a.Evidence, func(sha SHA) bool { return ran(builds[sha], j) != ran(builds[a.Evidence[0]], j) }) + }) { bad = append(bad, a.Evidence) } } @@ -943,8 +983,8 @@ func TestAPassThatRanFewerJobsThanTheRedFailedOnBlamesNobody(t *testing.T) { if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[2] [1]" { t.Errorf("landed %v and ejected %v, where #2 lands and only #1 leaves", landed, ejected) } - if bad := s.fewer("linux", "mac"); len(bad) > 0 { - t.Errorf("ejected on %v, where the pass without it ran fewer jobs than the failure after it", bad) + if bad := s.differ("linux", "mac"); len(bad) > 0 { + t.Errorf("ejected on %v, whose builds ran different jobs", bad) } }) } @@ -977,8 +1017,8 @@ func TestAPassThatRanFewerJobsClearsNothing(t *testing.T) { if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[2 3] [1]" { t.Errorf("landed %v and ejected %v, where #2 and #3 land and #1 leaves", landed, ejected) } - if bad := s.fewer("linux", "mac"); len(bad) > 0 { - t.Errorf("ejected on %v, where the pass without it ran fewer jobs than the failure after it", bad) + if bad := s.differ("linux", "mac"); len(bad) > 0 { + t.Errorf("ejected on %v, whose builds ran different jobs", bad) } } @@ -1045,8 +1085,8 @@ func TestAListThatGrowsTwiceIsJudgedOnTheLast(t *testing.T) { if len(s.w.PRs) > 0 || len(s.w.Builds) > 0 { t.Errorf("the queue still holds %v and builds %+v", s.w.PRs, s.w.Builds) } - if bad := s.fewer(jobs...); len(bad) > 0 { - t.Errorf("ejected on %v, where the pass without it ran fewer jobs than the failure after it", bad) + if bad := s.differ(jobs...); len(bad) > 0 { + t.Errorf("ejected on %v, whose builds ran different jobs", bad) } } @@ -1107,8 +1147,9 @@ func TestRoundsOutWhenTheListGrowsAreNotRead(t *testing.T) { // as a flake and a whole build of the tree decides. Where it was asked to run nothing, as one // pushed while the candidate had named no job, it says nothing of linux, and #1's tree is // probed again; of a candidate that still names no job it says all there is, as before. Each -// holds through a recording's JSON. Where probes are off, what a probe ran changes nothing: -// one of the trunk alone that failed still says the trunk is red. +// holds through a recording's JSON. Where probes are off, what a probe ran still counts: one +// of the trunk alone that failed and says nothing of what it ran says the trunk is red, and +// one asked to run nothing says nothing, so a whole build of #1's tree is pushed. func TestAProbeThatSaysNothingOfWhatItRanIsNotOneAskedToRunNothing(t *testing.T) { now := time.Date(2026, 11, 28, 12, 0, 0, 0, time.UTC) one := []Member{member(1, "m1")} @@ -1163,8 +1204,250 @@ func TestAProbeThatSaysNothingOfWhatItRanIsNotOneAskedToRunNothing(t *testing.T) } off := probing off.Probes = false - said, _ = plan(off, linux, nil, nil, Failure) - if unsaid, _ := plan(off, linux, []string{}, nil, Failure); fmt.Sprint(unsaid) != fmt.Sprint(said) { - t.Errorf("with probes off, a failed probe of the trunk asked to run nothing did %v, where one that says nothing did %v", unsaid, said) + red := fmt.Sprintf("[kind %d of 0 kind %d of 1 waits: %s]", Check, Check, redTrunk) + if said, _ = plan(off, linux, nil, nil, Failure); fmt.Sprint(said) != red { + t.Errorf("with probes off, a failed probe of the trunk that says nothing of what it ran did %v, want %s", said, red) + } + none := fmt.Sprintf("[kind %d of 1 waits: %s]", Check, suspected) + if unsaid, _ := plan(off, linux, []string{}, nil, Failure); fmt.Sprint(unsaid) != none { + t.Errorf("with probes off, a failed probe of the trunk asked to run nothing did %v, want %s", unsaid, none) + } +} + +// passing is a CI where every build passes. +func passing(Build) []string { return nil } + +// #1 is innocent, and CI is out. Its candidate failed on linux, and so did round 1's probe of +// #1. Round 2's probe, of the trunk alone, waits for CI to begin it when the candidate fails on +// mac too, and round 3's, of #1 on both, begins and fails in the outage. CI recovers, round 2 +// begins and passes, and mac, retried, passes, so the candidate's list is linux alone again. +// Read so, rounds 1, 2 and 3 failed, passed and failed, though round 3 began before round 2: +// round 3 ran another list, so they are no fair test. Nobody leaves: round 4 probes #1 on +// linux, its pass reads as a flake, and the whole build of #1's tree that decides passes. +func TestRoundsBegunOutOfOrderAreNoFairTestWhenTheListShrinksBack(t *testing.T) { + s := newTicks(t, 1) + s.tick() + c := s.candidate(1) + s.fail(c.SHA, "linux") + jobs := []string{"linux", "mac"} + var rounds []string + round := func() Build { + t.Helper() + acts := s.tick() + rounds = append(rounds, pushedRound(acts, c.SHA, jobs...)...) + builds := evidence(acts) + if len(builds) != 1 { + t.Fatalf("the round pushed %d builds, want one: %v", len(builds), rounds) + } + return builds[0] + } + one := round() + s.fail(one.SHA, "linux") + s.held = true + two := round() + s.held = false + s.fail(c.SHA, "linux", "mac") + three := round() + s.fail(three.SHA, "linux", "mac") + s.begin(two.SHA) + s.ci(c.SHA, passing) + s.fail(c.SHA, "linux") + if want := "[1:linux 0:linux 1:linux,mac]"; fmt.Sprint(rounds) != want { + t.Fatalf("rounds 1 to 3 pushed %v, want %s", rounds, want) + } + var after []string + for range 20 { + after = append(after, pushedRound(s.tick(), c.SHA, jobs...)...) + s.ci(c.SHA, passing) + } + if want := "[1:linux 1:whole]"; fmt.Sprint(after) != want { + t.Errorf("the rounds after pushed %v, want %s: #1 probed on linux, then built whole on its pass", after, want) + } + if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[1] []" { + t.Errorf("landed %v and ejected %v, where #1 lands", landed, ejected) + } + if bad := s.differ(jobs...); len(bad) > 0 { + t.Errorf("ejected on %v, whose builds ran different jobs", bad) + } +} + +// #1 breaks linux, and the candidate of #1 and #2 fails on a context whose name ends in a +// space. Git reads the last trailer of a probe's commit back without it. The probes of the +// first round wait for CI to begin them, and a tick on the unchanged world pushes nothing. +// #1 leaves, and #2 lands. +func TestAContextWhoseNameEndsInASpaceIsTheOneAProbeRan(t *testing.T) { + s := newTicks(t, 1, 2) + s.tick() + c := s.candidate(1, 2) + s.fail(c.SHA, "linux ") + s.held = true + first := evidence(s.tick()) + if len(first) == 0 { + t.Fatal("the red candidate's first round pushed nothing") + } + if again := evidence(s.tick()); len(again) > 0 { + t.Errorf("a tick on the unchanged world pushed %d more builds", len(again)) + } + s.held = false + for _, b := range first { + s.begin(b.SHA) + } + for range 30 { + s.ci(c.SHA, func(b Build) []string { + if slices.ContainsFunc(b.Chain, func(m Member) bool { return m.Number == 1 }) && ran(b, "linux") { + return []string{"linux "} + } + return nil + }) + s.tick() + } + if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[2] [1]" { + t.Errorf("landed %v and ejected %v, where #2 lands and #1 leaves", landed, ejected) + } + if len(s.w.PRs) > 0 || len(s.w.Builds) > 0 { + t.Errorf("the queue still holds %d pull requests and %d builds", len(s.w.PRs), len(s.w.Builds)) + } +} + +// #1 of three breaks linux, and the candidate fails on linux and on mac, a flake. The first +// round's probes run both, and mac, retried, passes while they run, so the candidate's list is +// linux alone. They say nothing: the tick that reads the shorter list pushes a round over the +// same prefixes on linux. #1 leaves, #2 and #3 land, and the queue is empty. +func TestAListThatShrinksIsJudgedOnWhatItHoldsNow(t *testing.T) { + s := newTicks(t, 1, 2, 3) + s.tick() + c := s.candidate(1, 2, 3) + s.fail(c.SHA, "linux", "mac") + jobs := []string{"linux", "mac"} + first := pushedRound(s.tick(), c.SHA, jobs...) + s.fail(c.SHA, "linux") + shrunk := pushedRound(s.tick(), c.SHA, jobs...) + if want := "[1:linux,mac 2:linux,mac 0:linux,mac] [1:linux 2:linux 0:linux]"; fmt.Sprint(first, shrunk) != want { + t.Errorf("the first round and the tick that read the shorter list pushed %v %v, want %s", first, shrunk, want) + } + for range 40 { + s.ci(c.SHA, breaks(1, "linux")) + s.tick() + } + if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[2 3] [1]" { + t.Errorf("landed %v and ejected %v, where #2 and #3 land and #1 leaves", landed, ejected) + } + if len(s.w.PRs) > 0 || len(s.w.Builds) > 0 { + t.Errorf("the queue still holds %d pull requests and %d builds", len(s.w.PRs), len(s.w.Builds)) + } + if bad := s.differ(jobs...); len(bad) > 0 { + t.Errorf("ejected on %v, whose builds ran different jobs", bad) + } +} + +// #1's candidate failed on linux. Round 1's probe of #1 and round 3's ran linux and mac and +// failed, and round 2's of the trunk alone ran linux and passed. Read on linux they would be +// #1's fair test, but its failures ran another list than its pass. Nobody leaves, and the +// round pushed probes #1 again on linux. +func TestNoFairTestHoldsBuildsAskedToRunDifferentLists(t *testing.T) { + var now time.Time + at := func(m int) time.Time { return now.Add(time.Duration(m) * time.Minute) } + one, linux, both := []Member{member(1, "m1")}, []string{"linux"}, []string{"linux", "mac"} + w := World{Now: at(10), Trunk: "trunk", Repo: commits{}, PRs: queuedAt(now, 1), Builds: []Build{ + {SHA: "m1", Base: "trunk", Chain: one, Verdict: Failure, Failed: linux, Pushed: now, Started: now}, + {SHA: "p1", Kind: Probe, Base: "trunk", Chain: one, Round: 1, Of: "m1", Verdict: Failure, Failed: linux, Runs: both, Pushed: at(1), Started: at(1)}, + {SHA: "p2", Kind: Probe, Base: "trunk", Round: 2, Of: "m1", Verdict: Success, Runs: linux, Pushed: at(2), Started: at(2)}, + {SHA: "p3", Kind: Probe, Base: "trunk", Chain: one, Round: 3, Of: "m1", Verdict: Failure, Failed: linux, Runs: both, Pushed: at(3), Started: at(3)}, + }} + acts := Plan(w, probing) + if ejected, evidence := ejections(acts); len(ejected) > 0 { + t.Errorf("ejected %v on %v, whose failures ran linux and mac and whose pass ran linux", ejected, evidence) + } + var pushed []string + for _, b := range evidence(acts) { + pushed = append(pushed, fmt.Sprintf("round %d of %d on %v", b.Round, len(b.Chain), b.Runs)) + } + if want := "[round 4 of 1 on [linux]]"; fmt.Sprint(pushed) != want { + t.Errorf("pushed %v, want %s", pushed, want) + } +} + +// #1's candidate failed on linux, and so did round 1's probe of #1; by the tick that reads it +// the candidate has failed on mac too. That failure counts for nothing: the round the tick +// pushes probes #1 again, on both, not the trunk alone. +func TestAFailureThatRanFewerJobsCountsForNothing(t *testing.T) { + s := newTicks(t, 1) + s.tick() + c := s.candidate(1) + s.fail(c.SHA, "linux") + jobs := []string{"linux", "mac"} + one := s.tick() + s.fail(evidence(one)[0].SHA, "linux") + s.fail(c.SHA, "linux", "mac") + if pushed := fmt.Sprint(pushedRound(one, c.SHA, jobs...), pushedRound(s.tick(), c.SHA, jobs...)); pushed != "[1:linux] [1:linux,mac]" { + t.Errorf("rounds 1 and 2 pushed %s, want [1:linux] [1:linux,mac]", pushed) + } +} + +// The candidate of #1 failed with no context named, and round 1 probes #1 asked to run none; +// by the tick that reads it the candidate has failed on linux, so the probe, still running, +// says nothing, and the round that tick pushes probes #1 again on linux. The same holds the +// other way: a probe pushed on linux says nothing once the candidate names no context. +func TestAProbeCountsOnlyForTheListItWasPushedOn(t *testing.T) { + for _, tc := range []struct { + name string + before, after []string + want string + }{ + {"none then linux", nil, []string{"linux"}, "[1:] [1:linux]"}, + {"linux then none", []string{"linux"}, nil, "[1:linux] [1:]"}, + } { + t.Run(tc.name, func(t *testing.T) { + s := newTicks(t, 1) + s.tick() + c := s.candidate(1) + s.fail(c.SHA, tc.before...) + first := pushedRound(s.tick(), c.SHA, "linux") + s.fail(c.SHA, tc.after...) + if pushed := fmt.Sprint(first, pushedRound(s.tick(), c.SHA, "linux")); pushed != tc.want { + t.Errorf("rounds 1 and 2 pushed %s, want %s", pushed, tc.want) + } + }) + } +} + +// #1 breaks linux, and its candidate failed on linux. Round 1 probes #1 on linux, and probes +// are turned off while it runs. Where the candidate has since failed on mac too, the probe says +// nothing, and the next round builds #1's tree whole; where its list is still linux, the probe +// counts, and the next round, its test's pass, builds the trunk alone whole. Either way the +// rounds go on as whole builds, and #1 leaves. +func TestAProbeInFlightWhenProbesTurnOffCountsOnlyForItsList(t *testing.T) { + for _, tc := range []struct { + name string + failed []string + want string + }{ + {"the list grew", []string{"linux", "mac"}, "[1:whole]"}, + {"the list stands", []string{"linux"}, "[0:whole]"}, + } { + t.Run(tc.name, func(t *testing.T) { + s := newTicks(t, 1) + s.tick() + c := s.candidate(1) + s.fail(c.SHA, "linux") + first := pushedRound(s.tick(), c.SHA, "linux") + s.c.Probes = false + s.fail(c.SHA, tc.failed...) + if pushed := fmt.Sprint(first, pushedRound(s.tick(), c.SHA, "linux")); pushed != "[1:linux] "+tc.want { + t.Errorf("rounds 1 and 2 pushed %s, want [1:linux] %s", pushed, tc.want) + } + for range 20 { + s.ci(c.SHA, breaks(1, "linux")) + s.tick() + } + for _, b := range s.pushed() { + if b.Round > 1 && b.Kind != Check { + t.Errorf("round %d pushed a build of kind %d, where probes are off", b.Round, b.Kind) + } + } + if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[] [1]" { + t.Errorf("landed %v and ejected %v, where #1 leaves", landed, ejected) + } + }) } } diff --git a/internal/queue/world.go b/internal/queue/world.go index ef052ad..6c50c7a 100644 --- a/internal/queue/world.go +++ b/internal/queue/world.go @@ -6,6 +6,7 @@ package queue import ( "slices" + "strings" "time" ) @@ -93,15 +94,29 @@ type Build struct { Started time.Time // late marks a build read as lost for outliving the timeout with no verdict of its own. late bool - // blind marks a probe read as giving no verdict for running fewer contexts than its red - // candidate has failed on. - blind bool } -// covers says the build ran every context in failed: a whole build does, and so does a probe -// that does not say what it ran. -func (b Build) covers(failed []string) bool { - return b.Kind != Probe || b.Runs == nil || !slices.ContainsFunc(failed, func(f string) bool { return !slices.Contains(b.Runs, f) }) +// counts says the build is evidence about a red candidate that failed on the contexts named: +// a whole build is, and so is a probe that does not say what it ran. Any other probe is only +// where it was asked to run the same contexts, whatever their order, as their names read back. +func (b Build) counts(failed []string) bool { + ran, want := named(b.Runs), named(failed) + slices.Sort(ran) + slices.Sort(want) + return b.Kind != Probe || b.Runs == nil || slices.Equal(slices.Compact(ran), slices.Compact(want)) +} + +// named is the names of contexts as a probe's trailers read them back, in order: each up to +// its first line break, which would end its trailer, and without the whitespace at either end, +// which git loses from the end of a commit's message, and none left empty. +func named(contexts []string) (names []string) { + for _, c := range contexts { + c, _, _ = strings.Cut(c, "\n") + if c = strings.TrimSpace(c); c != "" { + names = append(names, c) + } + } + return names } // Tests is the prefix whose tree this build has. From d3ba5a81f90e03b2be340b0410aba67e98a96739 Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Sat, 28 Nov 2026 10:19:17 -0800 Subject: [PATCH 3/3] A round waits for CI to begin the round before, whether that round counts or not A probe that was not asked to run what its red's list now holds gives no verdict, and the rounds went on past it at once, begun or not. Two things let them. The check that CI had begun the last round read the copies the rounds are weighed on, where a probe that does not count reads as lost and so as begun. And it was asked only while a build that counts was running, so with none running, on a flake, on the tip, or on a list that grew past every round out, the next round went out without it. An innocent pull request could leave on rounds 1, 2 and 3 read as a fair test though round 3, a whole build once probes were turned off, began before round 2. The next round now goes out only once CI has begun every build of the round before: its build has a start, or has reported, whether it counts or not. A build that does not count is still never waited on to answer, brings no recheck wait and does not hold the bound of three rounds past the last one read. One that outlives the timeout before CI begins it is lost, as before, and the round after skips a number. Where the repository's CI does not say when a build began, begun is reported, so there the next round waits for a build set aside to report. With this, three rounds in a row that count began in order, whatever kind of build each is: each was pushed once CI had begun every build of the round before. --- internal/queue/plan.go | 14 +-- internal/queue/plan_test.go | 221 ++++++++++++++++++++++++++++++------ 2 files changed, 191 insertions(+), 44 deletions(-) diff --git a/internal/queue/plan.go b/internal/queue/plan.go index 025564a..72a1f90 100644 --- a/internal/queue/plan.go +++ b/internal/queue/plan.go @@ -375,11 +375,11 @@ func (p *plan) weigh(r *red) { // test is how far the fair test of the pull request that prefix s adds has got: the builds, // in order, of its prefix that failed, of the prefix below that passed in the next round, and // of its own that failed in the round after that. A round is pushed only once CI has begun -// every build of the round before, so each began after the last, and a trunk or a CI that -// was broken for a while and failed both would have failed the pass between them. A test -// that is not whole counts only if it ends at the newest round, where it can still go on. -// No test reaches back past the round at which the rounds started over, nor across a round -// number that was skipped. +// every build of the round before, whether that build counts or not, so each began after the +// last, and a trunk or a CI that was broken for a while and failed both would have failed the +// pass between them. A test that is not whole counts only if it ends at the newest round, +// where it can still go on. No test reaches back past the round at which the rounds started +// over, nor across a round number that was skipped. // With hope, a build still running counts as the verdict the test wants of it. func (r *red) test(s int, hope bool) (t []Build) { for n := r.over + 1; n <= r.last && len(t) < 3; n++ { @@ -546,10 +546,10 @@ func (p *plan) rounds() { // three rounds, so no more than three are begun past the last round read: until // that one has reported whole, what its half says restarts no test. test := span == 1 || (whole && span <= max(p.c.Slots, 2)) - begun := !slices.ContainsFunc(r.rounds[r.last], func(b Build) bool { return !b.begun() }) + begun := !slices.ContainsFunc(r.builds, func(b Build) bool { return b.Round > 0 && b.Round == r.last && !b.begun() }) var points []int switch { - case r.passed, (r.tip || r.void) && p.w.Now.Sub(r.pushed) < p.c.Recheck, r.running && (r.flake || r.tip || !test || !begun || r.last >= r.read+3): + case r.passed, !begun, (r.tip || r.void) && p.w.Now.Sub(r.pushed) < p.c.Recheck, r.running && (r.flake || r.tip || !test || r.last >= r.read+3): continue case r.flake: // The probes passed on the red tree itself, so a whole build of it decides. diff --git a/internal/queue/plan_test.go b/internal/queue/plan_test.go index 4ec3e54..bf3bfb2 100644 --- a/internal/queue/plan_test.go +++ b/internal/queue/plan_test.go @@ -1217,57 +1217,204 @@ func TestAProbeThatSaysNothingOfWhatItRanIsNotOneAskedToRunNothing(t *testing.T) // passing is a CI where every build passes. func passing(Build) []string { return nil } -// #1 is innocent, and CI is out. Its candidate failed on linux, and so did round 1's probe of -// #1. Round 2's probe, of the trunk alone, waits for CI to begin it when the candidate fails on -// mac too, and round 3's, of #1 on both, begins and fails in the outage. CI recovers, round 2 -// begins and passes, and mac, retried, passes, so the candidate's list is linux alone again. -// Read so, rounds 1, 2 and 3 failed, passed and failed, though round 3 began before round 2: -// round 3 ran another list, so they are no fair test. Nobody leaves: round 4 probes #1 on -// linux, its pass reads as a flake, and the whole build of #1's tree that decides passes. -func TestRoundsBegunOutOfOrderAreNoFairTestWhenTheListShrinksBack(t *testing.T) { +// #1 is innocent, and CI is out: whatever it begins fails on linux and mac. Its candidate +// failed on linux, and so did round 1's probe of #1. Round 2's probe, of the trunk alone, waits +// for CI to begin it when the candidate fails on mac too, and probes stay on or are turned off. +// Round 2 says nothing of the longer list, but it is still the round before, so no round goes +// out in the outage. CI recovers, round 2 begins and passes, and mac, retried, passes, so the +// candidate's list is linux alone again. The tick after pushes round 3 on what that list says: +// #1 on linux, then whole on its pass, or whole at once where probes are off. Nobody leaves, +// #1 lands, and the queue is empty. +func TestNoRoundGoesOutBeforeTheRoundBeforeBeginsThoughItDoesNotCount(t *testing.T) { + for _, tc := range []struct { + name string + probes bool + want string + }{ + {"probes stay on", true, "[[] [1:linux] [1:whole] []]"}, + {"probes off before round 3", false, "[[] [1:whole] [] []]"}, + } { + t.Run(tc.name, func(t *testing.T) { + s := newTicks(t, 1) + s.tick() + c := s.candidate(1) + s.fail(c.SHA, "linux") + jobs := []string{"linux", "mac"} + one := evidence(s.tick()) + s.fail(one[0].SHA, "linux") + s.held = true + two := evidence(s.tick()) + s.held = false + if len(two) != 1 || len(two[0].Chain) != 0 { + t.Fatalf("round 2 pushed %+v, want the trunk alone", two) + } + s.fail(c.SHA, "linux", "mac") + s.c.Probes = tc.probes + var ticks [][]string + acts := s.tick() + ticks = append(ticks, pushedRound(acts, c.SHA, jobs...)) + for _, b := range evidence(acts) { + s.fail(b.SHA, "linux", "mac") + } + s.begin(two[0].SHA) + s.ci(c.SHA, passing) + s.fail(c.SHA, "linux") + for range 20 { + ticks = append(ticks, pushedRound(s.tick(), c.SHA, jobs...)) + s.ci(c.SHA, passing) + } + if got := fmt.Sprint(ticks[:4]); got != tc.want { + t.Errorf("the tick in the outage and the three after it pushed %s, want %s", got, tc.want) + } + if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[1] []" { + t.Errorf("landed %v and ejected %v, where #1 lands", landed, ejected) + } + if len(s.w.PRs) > 0 || len(s.w.Builds) > 0 { + t.Errorf("the queue still holds %d pull requests and %d builds", len(s.w.PRs), len(s.w.Builds)) + } + }) + } +} + +// #1 is innocent. Its candidate failed on linux, and round 1 probes #1 on linux. The candidate +// fails on mac too, and round 2 probes #1 on both, waiting for CI to begin it. Mac, retried, +// passes, and round 1 passes: a flake, by the list that stands, which a whole build of #1's tree +// decides. Round 2 says nothing of linux alone, but it is the round before, so the whole build +// waits for CI to begin it and goes out on the tick after. #1 lands, and the queue is empty. +func TestAWholeBuildOnAFlakeWaitsForTheRoundBeforeToBegin(t *testing.T) { s := newTicks(t, 1) s.tick() c := s.candidate(1) s.fail(c.SHA, "linux") jobs := []string{"linux", "mac"} - var rounds []string - round := func() Build { - t.Helper() - acts := s.tick() - rounds = append(rounds, pushedRound(acts, c.SHA, jobs...)...) - builds := evidence(acts) - if len(builds) != 1 { - t.Fatalf("the round pushed %d builds, want one: %v", len(builds), rounds) + ticks := [][]string{pushedRound(s.tick(), c.SHA, jobs...)} + s.fail(c.SHA, "linux", "mac") + s.held = true + acts := s.tick() + s.held = false + ticks = append(ticks, pushedRound(acts, c.SHA, jobs...)) + two := evidence(acts) + if len(two) != 1 { + t.Fatalf("round 2 pushed %d builds, want one", len(two)) + } + s.fail(c.SHA, "linux") + for i := range 20 { + s.ci(two[0].SHA, passing) + if i == 2 { + s.begin(two[0].SHA) } - return builds[0] + ticks = append(ticks, pushedRound(s.tick(), c.SHA, jobs...)) } - one := round() - s.fail(one.SHA, "linux") + if want := "[[1:linux] [1:linux,mac] [] [] [1:whole] [] []]"; fmt.Sprint(ticks[:7]) != want { + t.Errorf("the ticks pushed %v, want %s", ticks[:7], want) + } + if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[1] []" { + t.Errorf("landed %v and ejected %v, where #1 lands", landed, ejected) + } + if len(s.w.PRs) > 0 || len(s.w.Builds) > 0 { + t.Errorf("the queue still holds %d pull requests and %d builds", len(s.w.PRs), len(s.w.Builds)) + } +} + +// #1 breaks mac. Its candidate failed on linux, and a round waits for CI to begin it when the +// candidate fails on mac too, and then on windows. No round goes out until CI begins it, and the +// tick after it does pushes one on all three. Where round 1, on linux, has begun and round 2, on +// both, waits, the candidate's list is linux again: round 1 counts and runs, and round 3 waits +// for round 2 all the same, then probes #1 on linux, as round 2 gives no test a build. #1 +// leaves, and the queue is empty. +func TestAListThatGrowsPastARoundNotBegunWaitsForIt(t *testing.T) { + jobs := []string{"linux", "mac", "windows"} + for _, tc := range []struct { + name string + want string + }{ + {"nothing running", "[[1:linux] [] [] [1:linux,mac,windows]]"}, + {"round 1 running", "[[1:linux] [1:linux,mac] [] [] [1:linux]]"}, + } { + t.Run(tc.name, func(t *testing.T) { + s := newTicks(t, 1) + s.tick() + c := s.candidate(1) + s.fail(c.SHA, "linux") + running := tc.name == "round 1 running" + s.held = !running + acts := s.tick() + s.held = false + ticks := [][]string{pushedRound(acts, c.SHA, jobs...)} + waiting := evidence(acts) + s.fail(c.SHA, "linux", "mac") + if running { + s.held = true + acts = s.tick() + s.held = false + ticks = append(ticks, pushedRound(acts, c.SHA, jobs...)) + waiting = evidence(acts) + s.fail(c.SHA, "linux") + } + ticks = append(ticks, pushedRound(s.tick(), c.SHA, jobs...)) + if !running { + s.fail(c.SHA, "linux", "mac", "windows") + } + ticks = append(ticks, pushedRound(s.tick(), c.SHA, jobs...)) + for _, b := range waiting { + s.begin(b.SHA) + } + ticks = append(ticks, pushedRound(s.tick(), c.SHA, jobs...)) + if fmt.Sprint(ticks) != tc.want { + t.Errorf("the ticks pushed %v, want %s", ticks, tc.want) + } + for range 40 { + s.ci(c.SHA, breaks(1, "mac")) + s.tick() + } + if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[] [1]" { + t.Errorf("landed %v and ejected %v, where #1 leaves", landed, ejected) + } + if len(s.w.PRs) > 0 || len(s.w.Builds) > 0 { + t.Errorf("the queue still holds %d pull requests and %d builds", len(s.w.PRs), len(s.w.Builds)) + } + }) + } +} + +// #1 breaks mac. Its candidate failed on linux, and round 1, a probe of #1 on linux, waits for +// CI to begin it when the candidate fails on mac too. CI never does. Once it has outlived the +// timeout the rounds go on, as they do past a lost build that counts: the next is round 3, on +// both, and no test reaches across round 2. #1 leaves, and the queue is empty. +func TestARoundNotBegunThatDoesNotCountIsLostAtTheTimeout(t *testing.T) { + s := newTicks(t, 1) + s.tick() + c := s.candidate(1) + s.fail(c.SHA, "linux") s.held = true - two := round() + first := evidence(s.tick()) s.held = false + if len(first) != 1 { + t.Fatalf("round 1 pushed %d builds, want one", len(first)) + } s.fail(c.SHA, "linux", "mac") - three := round() - s.fail(three.SHA, "linux", "mac") - s.begin(two.SHA) - s.ci(c.SHA, passing) - s.fail(c.SHA, "linux") - if want := "[1:linux 0:linux 1:linux,mac]"; fmt.Sprint(rounds) != want { - t.Fatalf("rounds 1 to 3 pushed %v, want %s", rounds, want) + waited := 0 + var next []Build + for next == nil && waited < 120 { + if next = evidence(s.tick()); next == nil { + waited++ + } } - var after []string - for range 20 { - after = append(after, pushedRound(s.tick(), c.SHA, jobs...)...) - s.ci(c.SHA, passing) + if late := s.w.Now.Sub(first[0].Pushed); len(next) != 1 || next[0].Round != 3 || late <= s.c.Timeout || late > s.c.Timeout+time.Minute { + t.Fatalf("after %v, %d ticks with no push, pushed %+v, want round 3 once round 1 outlived the timeout", late, waited, next) } - if want := "[1:linux 1:whole]"; fmt.Sprint(after) != want { - t.Errorf("the rounds after pushed %v, want %s: #1 probed on linux, then built whole on its pass", after, want) + if got := fmt.Sprint(next[0].Runs); got != "[linux mac]" { + t.Errorf("round 3 runs %s, want [linux mac]", got) } - if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[1] []" { - t.Errorf("landed %v and ejected %v, where #1 lands", landed, ejected) + for range 40 { + s.ci(first[0].SHA, breaks(1, "mac")) + s.tick() } - if bad := s.differ(jobs...); len(bad) > 0 { - t.Errorf("ejected on %v, whose builds ran different jobs", bad) + if landed, ejected := s.outcome(); fmt.Sprint(landed, ejected) != "[] [1]" { + t.Errorf("landed %v and ejected %v, where #1 leaves", landed, ejected) + } + if len(s.w.PRs) > 0 { + t.Errorf("the queue still holds %v", s.w.PRs) } }