diff --git a/cmd/mesh-controller/batches.go b/cmd/mesh-controller/batches.go index 1acbe182..20240682 100644 --- a/cmd/mesh-controller/batches.go +++ b/cmd/mesh-controller/batches.go @@ -176,6 +176,11 @@ func (f following) hearMerge(ctx context.Context, m link.SourceMoved, now time.T repository, m.Base, m.Commit, already) return nil } + if already == 0 { + if done, err := f.firstBuilds(ctx, m, entries, read, now); err != nil || done { + return err + } + } fmt.Print(nothingReadsIt(repository, m)) return nil } @@ -742,6 +747,9 @@ func carriedOf(merges []inventory.BatchedMerge) ([]inventory.PlanCommit, []inven type keptMerge struct { link.SourceMoved Moves []string `json:"moves,omitempty"` + // FirstBuild says the merge is a first merge into a repository the mesh builds nothing from, kept to ask the first + // build of its modules once mesh-delivery's word on it is kept (novox/hq ADR 0432, issue 1146). + FirstBuild bool `json:"first_build,omitempty"` } // announcedOf is a kept merge as the forge announced it, and what it moved; empty where the record says none. diff --git a/cmd/mesh-controller/doctor.go b/cmd/mesh-controller/doctor.go index 4358b135..d9e436c4 100644 --- a/cmd/mesh-controller/doctor.go +++ b/cmd/mesh-controller/doctor.go @@ -120,6 +120,11 @@ var probeRegistry = []probe{ {ID: probeDeliveriesID, Asserts: "no delivery is held past its state's bound unsaid: mesh-delivery's " + "`stalled`, each with the transition its table lets healer H2 take", From: "ADR 0239", Kind: kindDeliveryStalled, Phase: 3, run: probeDeliveries}, + // A first merge into a repository the mesh builds nothing from, kept waiting for mesh-delivery's word on the + // operator's Approve (novox/hq ADR 0432, issue 1146): said past a bound, never only in a log line (ADR 0426 §3, §8). + {ID: probeFirstBuildsID, Asserts: "no first merge into a repository the mesh builds nothing from waits more than ten " + + "minutes for mesh-delivery's announcement of the operator's Approve of its head", From: "ADR 0432, issue 1146", + Kind: kindFirstBuildUnasked, Phase: 3, run: probeFirstBuilds}, // The delivery budgets (novox/hq ADR 0282 decision 7): the newest delivery of each class within its budget, // read from mesh-delivery's `times`; a measurement said, never a delivery held. {ID: probeBudgetsID, Asserts: "the newest delivery of a leaf module ran on every machine within three minutes of " + diff --git a/cmd/mesh-controller/first_builds.go b/cmd/mesh-controller/first_builds.go new file mode 100644 index 00000000..7283c44f --- /dev/null +++ b/cmd/mesh-controller/first_builds.go @@ -0,0 +1,212 @@ +package main + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "strings" + "time" + + "github.com/novox/mesh-controller/internal/conditions" + "github.com/novox/mesh-controller/internal/inventory" + "github.com/novox/mesh-controller/internal/link" +) + +// firstBuilds asks the first build of every module a merge adds to a repository no registered module is built from, when +// the merge was announced as made on the operator's Approve of that head (novox/hq ADR 0432, issue 1146): the modules' +// directories are the ones the announcement says hold a module.json at the merge commit, as touchedBy reads them. The +// merge is kept, so the build's outcome is judged by the rule that registers it (mayRegisterFrom, approvedMove). +// +// **Complete in either order, with no timer** (ADR 0426): when mesh-delivery's word is kept first, it is found here; when +// the forge's merge is heard first, the merge is kept as a first merge waiting for the word, and keepingDeliveryMerges +// runs this again when the word is kept (resumeFirstBuilds). A word that says no (a Decline, another head) asks nothing and +// says why. Answers whether the merge was dealt with here. +func (f following) firstBuilds(ctx context.Context, m link.SourceMoved, entries []inventory.Entry, + read map[string][]inventory.ReadRepository, now time.Time) (bool, error) { + inv := f.open.inventory + repository := m.Owner + "/" + m.Repo + added, why, err := firstBuildsOf(ctx, inv, m, entries, read) + switch { + case errors.Is(err, errFirstNotApproved): + fmt.Printf("%s merged into %s (%.8s) adds %s, and no build is asked: %v\n", repository, m.Base, m.Commit, + strings.Join(addedDirs(m, entries, read), ", "), err) + return true, nil + case errors.Is(err, errNotAnnounced): + // Kept, waiting for mesh-delivery's word: a restart keeps it, and the word's arrival asks the builds. + if kerr := keepFirstMerge(ctx, inv, m, now, true); kerr != nil { + return true, notNow(kerr) + } + fmt.Printf("%s merged into %s (%.8s) adds %s; it is kept, and the first build is asked when mesh-delivery "+ + "announces the merge on the operator's Approve\n", repository, m.Base, m.Commit, + strings.Join(addedDirs(m, entries, read), ", ")) + return true, nil + case err != nil: + return true, notNow(err) + case len(added) == 0: + return false, nil + } + if err := askFirstBuilds(ctx, inv, m, now, added, why); err != nil { + return true, notNow(err) + } + return true, nil +} + +// keepFirstMerge keeps a first merge as the controller heard it, waiting (no plan answers it). +func keepFirstMerge(ctx context.Context, inv *inventory.Inventory, m link.SourceMoved, now time.Time, waitingForWord bool) error { + merged, err := time.Parse(time.RFC3339Nano, m.MergedAt) + if err != nil { + merged = now + } + event, err := json.Marshal(keptMerge{SourceMoved: m, FirstBuild: waitingForWord}) + if err != nil { + return err + } + _, err = inv.AddMerge(ctx, inventory.BatchedMerge{Repository: m.Owner + "/" + m.Repo, Branch: m.Base, Commit: m.Commit, + Merged: merged.UTC(), Heard: now, Event: event}) + return err +} + +// askFirstBuilds keeps the merge, asks the builds, and marks the merge as no longer waiting for them. +func askFirstBuilds(ctx context.Context, inv *inventory.Inventory, m link.SourceMoved, now time.Time, added []string, + why string) error { + repository := m.Owner + "/" + m.Repo + if err := keepFirstMerge(ctx, inv, m, now, false); err != nil { + return err + } + fmt.Printf("%s merged into %s (%.8s) on the operator's Approve (%s): the mesh builds nothing from it yet, so the "+ + "first build of %s is asked\n", repository, m.Base, m.Commit, why, strings.Join(added, ", ")) + askBuilds(ctx, inv, m, buildSource{Repository: repository, Seat: gitSeat}, added) + // Answered: it no longer waits, so the word's arrival again asks nothing twice. + return inv.AnswerMerge(ctx, repository, m.Commit, inventory.WaitingRecord, false) +} + +// resumeFirstBuilds runs firstBuilds for the first merges waiting for the word mesh-delivery just announced for a pull +// request's head: the other order of the same two events. +func (f following) resumeFirstBuilds(ctx context.Context, d deliveryMerge) error { + actingOnMerges.Lock() + defer actingOnMerges.Unlock() + inv := f.open.inventory + waiting, err := inv.WaitingMerges(ctx) + if err != nil { + return notNow(err) + } + for _, w := range waiting { + k := announcedOf(w) + if !k.FirstBuild || !strings.EqualFold(k.Owner+"/"+k.Repo, d.Owner+"/"+d.Repo) || k.Number != d.Number || + k.HeadSHA != d.HeadSHA { + continue + } + entries, err := inv.Catalogued(ctx) + if err != nil { + return notNow(err) + } + read, err := readForPlanning(ctx, inv) + if err != nil { + return notNow(err) + } + added, why, err := firstBuildsOf(ctx, inv, k.SourceMoved, entries, read) + switch { + case errors.Is(err, errFirstNotApproved): + fmt.Printf("%s/%s merged into %s (%.8s) was waiting for mesh-delivery's word, and no build is asked: %v\n", + k.Owner, k.Repo, k.Base, k.Commit, err) + if aerr := inv.AnswerMerge(ctx, w.Repository, w.Commit, inventory.WaitingRecord, false); aerr != nil { + return notNow(aerr) + } + case err != nil: + return notNow(err) + case len(added) > 0: + if err := askFirstBuilds(ctx, inv, k.SourceMoved, time.Now().UTC(), added, why); err != nil { + return notNow(err) + } + } + } + return nil +} + +// errFirstNotApproved says a merge into a repository the mesh builds nothing from is not one the operator approved. +var errFirstNotApproved = errors.New("it is no merge made on the operator's Approve of its head, so a hand `build` " + + "registers a module from a repository the mesh does not build yet") + +// addedDirs are the module directories the merge adds to a repository no registered module is built from, none where +// the merge is not into the trunk or some module is built from the repository already. +func addedDirs(m link.SourceMoved, entries []inventory.Entry, read map[string][]inventory.ReadRepository) []string { + if m.Base != "main" { + return nil + } + for _, e := range entries { + if !e.Provided && sameRepository(e.Source.Repository, m) { + return nil + } + } + _, added, _ := touchedBy(nil, entries, m, read) + return added +} + +// firstBuildsOf is the directories whose first build a merge asks, and the warrant it was made on; none when the merge +// is no first merge into a repository the mesh builds nothing from. errFirstNotApproved when it is and the word on record +// is no Approve of its head; errNotAnnounced when mesh-delivery's word is not kept yet. +func firstBuildsOf(ctx context.Context, inv catalogueReader, m link.SourceMoved, entries []inventory.Entry, + read map[string][]inventory.ReadRepository) ([]string, string, error) { + added := addedDirs(m, entries, read) + if len(added) == 0 { + return nil, "", nil + } + why, err := approvedAnnounced(ctx, inv, m.Owner, m.Repo, keptMerge{SourceMoved: m}) + switch { + case err == nil: + return added, why, nil + case errors.Is(err, errWordNotAnApprove): + return nil, "", fmt.Errorf("%w (%v)", errFirstNotApproved, err) + } + // Not announced yet, or the bus or the store could not be asked: the caller waits or tries again, and asks nothing. + return nil, "", err +} + +// probeFirstBuildsID is the probe that reads the first merges waiting for mesh-delivery's word. +const probeFirstBuildsID = "D18" + +// kindFirstBuildUnasked is what a first merge waiting too long for mesh-delivery's word raises. +const kindFirstBuildUnasked = "first-build-unasked" + +// firstBuildWaitBound is how long a first merge waits for mesh-delivery's announcement of the operator's Approve before +// it is said: a bound in the sense of ADR 0426 §3, not a delay. Nothing waits on it: the word's arrival asks the builds +// at once, and the merge is said, never dropped, once it is past this (issue 1146). +const firstBuildWaitBound = 10 * time.Minute + +// firstBuildObservations are the conditions of the first merges waiting past the bound, one for each; a merge answered is +// no longer listed as waiting, so its condition clears with the next look. +func firstBuildObservations(waiting []inventory.BatchedMerge, now time.Time) []conditions.Observation { + var out []conditions.Observation + for _, w := range waiting { + k := announcedOf(w) + if !k.FirstBuild || now.Sub(w.Heard) <= firstBuildWaitBound { + continue + } + repository := k.Owner + "/" + k.Repo + for_ := now.Sub(w.Heard).Round(time.Minute) + out = append(out, conditions.Observation{Scope: conditions.ScopeMerge, + ID: fmt.Sprintf("%s_%.8s", repository, k.Commit), Kind: kindFirstBuildUnasked, Severity: conditions.Warning, + Summary: fmt.Sprintf("the merge %.8s of %s adds modules the mesh builds nothing from yet, and has waited %s for "+ + "mesh-delivery's announcement of the operator's Approve of its head: no build is asked", k.Commit, repository, for_), + Said: fmt.Sprintf("%s at %s waits for mesh-delivery's word on pull request %d (head %.8s)", repository, k.Commit, + k.Number, k.HeadSHA), + Headline: fmt.Sprintf("The first build of %s is not asked: no Approve was announced", repository), + Explanation: fmt.Sprintf("The merge %.8s into %s adds modules the mesh builds nothing from yet. It registers them "+ + "only when mesh-delivery announces that it merged the pull request on your Approve of that head, and no such "+ + "announcement has come in %s. The merge is kept, and the builds are asked the moment the announcement is kept.", + k.Commit, repository, for_), + Resolved: "mesh-delivery announces the merge, or a hand `build` registers the modules", + }) + } + return out +} + +// probeFirstBuilds is D18: no first merge waits past its bound for mesh-delivery's word unsaid. +func probeFirstBuilds(ctx context.Context, d *doctor) ([]conditions.Observation, error) { + waiting, err := d.open.inventory.WaitingMerges(ctx) + if err != nil { + return nil, err + } + return firstBuildObservations(waiting, time.Now().UTC()), nil +} diff --git a/cmd/mesh-controller/first_builds_integration_test.go b/cmd/mesh-controller/first_builds_integration_test.go new file mode 100644 index 00000000..c1b4adb4 --- /dev/null +++ b/cmd/mesh-controller/first_builds_integration_test.go @@ -0,0 +1,160 @@ +//go:build integration + +package main + +import ( + "context" + "encoding/json" + "testing" + "time" + + "github.com/nats-io/nats.go/jetstream" + + "github.com/novox/mesh-controller/internal/broker" + "github.com/novox/mesh-controller/internal/catalogue" + "github.com/novox/mesh-controller/internal/inventory" + "github.com/novox/mesh-controller/internal/link" + "github.com/novox/mesh-controller/internal/testbus" +) + +// 1146/AC7, whole against a real bus and the store, in both orders of the two events (novox/hq ADR 0432, issue 1146): +// mesh-delivery #7 merged on the operator's Approve into a repository the mesh builds nothing from. The forge's merge, +// heard as the controller hears it (SourceMoved), and mesh-delivery's word, published on the subject the controller +// consumes with the router's record of the ask in its bucket, come in either order; each way the first build of the +// module the merge commit holds is asked exactly once, its outcome registers the module, and it is in the catalogue. +// Without the Approve in the record nothing is asked. Store-backed: skips without one. +func TestAMergeOnTheApproveIntoANewRepositoryAsksTheFirstBuildAndItsOutcomeRegistersTheModule(t *testing.T) { + const head, commit = "a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1", "0177ea7a00000000" + for _, c := range []struct { + what string + wordFirst bool + approved bool // the router's record holds the operator's Approve of the merged head + wantRegiter bool + }{ + {what: "the forge's merge before mesh-delivery's word", approved: true, wantRegiter: true}, + {what: "mesh-delivery's word before the forge's merge", wordFirst: true, approved: true, wantRegiter: true}, + {what: "a word whose record holds no Approve", wordFirst: true}, + } { + t.Run(c.what, func(t *testing.T) { + open := aMesh(t) + ctx := t.Context() + inv := open.inventory + asked := asksWithPaths(t) + if err := inv.RegisterModule(ctx, catalogue.Manifest{Module: "operator-channel", Version: "1", + DefinesSeats: []catalogue.SeatDeclaration{{Name: broker.AsksSeat, Records: []string{"asks"}}}}, + inventory.Source{Repository: "novox/mesh-catalog", Seat: "git", Path: "modules/operator-channel", BuiltFrom: "c0ffee"}); err != nil { + t.Fatal(err) + } + js, err := broker.Dial(testbus.URL(t)) + if err != nil { + t.Fatal(err) + } + t.Cleanup(js.Close) + if err := broker.AssertMeshStreams(js); err != nil { + t.Fatal(err) + } + if err := js.EnsureControllerBuckets(); err != nil { + t.Fatal(err) + } + api, err := jetstream.New(js.Conn()) + if err != nil { + t.Fatal(err) + } + records, err := api.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: broker.BucketName("operator-channel", "asks")}) + if err != nil { + t.Fatal(err) + } + a := aDeliveryMergeOf(t, head, map[string]string{"novox/mesh-delivery@a1a1a1a1a1a1": head}) + if c.approved { + record, _ := json.Marshal(map[string]any{"state": "closed", "ask": a.ask, "warrant": a.warrant}) + if _, err := records.Put(ctx, theDelivery+"."+a.merge.AskID, record); err != nil { + t.Fatal(err) + } + } + + following := following{open: open} + server := link.ConnectNats(js, nil, noListener{}) + if err := server.KeepsDeliveryMerges(keepingDeliveryMerges{js: server.JetStream, kept: following.resumeFirstBuilds}); err != nil { + t.Fatal(err) + } + if err := broker.AssertServingConsumers(js); err != nil { + t.Fatal(err) + } + serving, stop := context.WithCancel(context.Background()) + done := make(chan struct{}) + go func() { defer close(done); _ = server.Serve(serving) }() + t.Cleanup(func() { stop(); <-done }) + servingBus.Store(js) + t.Cleanup(func() { servingBus.Store(nil) }) + + word := func() { + t.Helper() + body, _ := json.Marshal(a.merge) + if _, err := js.Context().Publish(broker.DeliveryMergedSubject, body); err != nil { + t.Fatal(err) + } + becomes(t, 15*time.Second, "the word being kept", func() bool { + _, kept, err := keptDeliveryMerge(ctx, "novox", "mesh-delivery", 7, head) + return err == nil && kept + }) + time.Sleep(500 * time.Millisecond) // the keeper's hook runs as the word is kept + } + forge := func() { + t.Helper() + m := link.SourceMoved{Owner: "novox", Repo: "mesh-delivery", Base: "main", HeadSHA: head, Commit: commit, Number: 7, + MergedAt: time.Now().Add(-time.Second).UTC().Format(time.RFC3339Nano), Paths: []string{"module.json", "main.go"}, + ModuleDirs: []string{"."}, ModuleDirsSaid: true} + if err := following.SourceMoved(ctx, m); err != nil { + t.Fatal(err) + } + } + if c.wordFirst { + word() + forge() + } else { + forge() + if len(*asked) != 0 { + t.Fatalf("1146/AC7: the merge asked %v before mesh-delivery's word was kept", *asked) + } + word() + } + if !c.wantRegiter { + if len(*asked) != 0 { + t.Fatalf("1146/AC7: a merge whose record holds no Approve asked %v", *asked) + } + return + } + want := [3]string{"novox/mesh-delivery", "", "main"} + becomes(t, 10*time.Second, "the first build asked", func() bool { return len(*asked) >= 1 }) + if len(*asked) != 1 || (*asked)[0] != want { + t.Fatalf("1146/AC7: the merge asked %v, want %v once", *asked, want) + } + build := onTrunk("b-", "novox/mesh-delivery", "git", "", map[string]any{"module": "mesh-delivery-view", "version": "1"}) + build.Commit = commit + if _, _, err := takeIn(ctx, inv, build); err != nil { + t.Fatalf("1146/AC7: the outcome of the first build was refused: %v", err) + } + src, err := inv.SourceOf(ctx, "mesh-delivery-view") + if err != nil || src.Repository != "novox/mesh-delivery" { + t.Fatalf("1146/AC7: the module is built from %q (%v)", src.Repository, err) + } + if _, known := inventoryHolds(t, inv, "mesh-delivery-view"); !known { + t.Fatal("1146/AC7: the module is not in the catalogue") + } + }) + } +} + +func inventoryHolds(t *testing.T, inv *inventory.Inventory, module string) (inventory.Entry, bool) { + t.Helper() + entries, err := inv.Catalogued(t.Context()) + if err != nil { + t.Fatal(err) + } + for _, e := range entries { + if e.Manifest.Module == module { + return e, true + } + } + return inventory.Entry{}, false +} diff --git a/cmd/mesh-controller/first_builds_test.go b/cmd/mesh-controller/first_builds_test.go new file mode 100644 index 00000000..7a6b2d14 --- /dev/null +++ b/cmd/mesh-controller/first_builds_test.go @@ -0,0 +1,106 @@ +package main + +import ( + "context" + "encoding/json" + "errors" + "slices" + "strings" + "testing" + "time" + + "git.novox.be/novox/mesh-sdk/go/asks" + "github.com/novox/mesh-controller/internal/conditions" + "github.com/novox/mesh-controller/internal/inventory" + "github.com/novox/mesh-controller/internal/link" +) + +// 1146/AC6: the first merge into a repository no registered module is built from asks the first build of every module +// the merge commit holds when it was made on the operator's Approve of that head, and asks nothing without it. +func TestAFirstMergeIntoANewRepositoryAsksItsModulesBuildsOnlyOnTheApprove(t *testing.T) { + const head = "a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1" + now := time.Date(2026, 10, 19, 0, 6, 0, 0, time.UTC) + mergedAt := now.Add(-time.Hour).Format(time.RFC3339Nano) + heard := link.SourceMoved{Owner: "novox", Repo: "mesh-delivery", Base: "main", HeadSHA: head, Commit: "0177ea7a", + Number: 1, MergedAt: mergedAt, ModuleDirsSaid: true, ModuleDirs: []string{"watchman", "view"}, + Paths: []string{"view/module.json", "watchman/module.json", "README.md"}} + built := inventory.Entry{Source: inventory.Source{Repository: "novox/mesh-catalog", Seat: "git"}} + for _, c := range []struct { + what string + change func(*anApprovedMerge, *link.SourceMoved, *[]inventory.Entry) + unkept bool + dirs []string + refused bool + waits bool + }{ + {what: "the Approve of the merged head", dirs: []string{"view", "watchman"}}, + {what: "a Decline", refused: true, change: func(a *anApprovedMerge, _ *link.SourceMoved, _ *[]inventory.Entry) { + a.warrant.Option, a.warrant.Label, a.merge.Option = "decline", "Decline", "decline" + }}, + {what: "no word of mesh-delivery yet, however long ago the merge was made", unkept: true, waits: true}, + {what: "a module already built from the repository", change: func(_ *anApprovedMerge, _ *link.SourceMoved, e *[]inventory.Entry) { + *e = append(*e, inventory.Entry{Source: inventory.Source{Repository: "novox/mesh-delivery", Seat: "git"}}) + }}, + {what: "a merge into another branch", change: func(_ *anApprovedMerge, m *link.SourceMoved, _ *[]inventory.Entry) { m.Base = "feat" }}, + {what: "a merge that holds no module.json", change: func(_ *anApprovedMerge, m *link.SourceMoved, _ *[]inventory.Entry) { + m.ModuleDirs, m.Paths = nil, []string{"README.md"} + }}, + } { + t.Run(c.what, func(t *testing.T) { + a := aDeliveryMergeOf(t, head, map[string]string{"novox/mesh-delivery@a1a1a1a1a1a1": head}) + a.merge.Number = 1 + m, entries := heard, []inventory.Entry{built} + if c.change != nil { + c.change(&a, &m, &entries) + } + wasKept, wasAsk := keptDeliveryMerge, routerAskOf + t.Cleanup(func() { keptDeliveryMerge, routerAskOf = wasKept, wasAsk }) + keptDeliveryMerge = func(context.Context, string, string, int, string) (deliveryMerge, bool, error) { + return a.merge, !c.unkept, nil + } + routerAskOf = func(context.Context, catalogueReader, string) (*asks.Ask, *asks.Warrant, error) { + return &a.ask, &a.warrant, nil + } + dirs, _, err := firstBuildsOf(t.Context(), nil, m, entries, nil) + switch { + case c.waits && !errors.Is(err, errNotAnnounced): + t.Fatalf("1146/AC6: %s: want a wait for the word, got %v", c.what, err) + case c.refused && !errors.Is(err, errFirstNotApproved): + t.Fatalf("1146/AC6: %s: want the Approve missing, got %v (dirs %v)", c.what, err, dirs) + case !c.refused && !c.waits && err != nil: + t.Fatalf("1146/AC6: %s: %v", c.what, err) + case !slices.Equal(dirs, c.dirs): + t.Fatalf("1146/AC6: %s: first builds asked of %v, want %v", c.what, dirs, c.dirs) + } + }) + } +} + +// 1146/AC8: a first merge waiting past the bound for mesh-delivery's word is said, naming the repository, the commit and +// the missing word; one inside the bound, one that is no first merge, and one answered (no longer waiting) are not. +func TestAFirstMergeWaitingPastItsBoundIsSaidAndClearsWhenAnswered(t *testing.T) { + now := time.Date(2026, 10, 19, 1, 0, 0, 0, time.UTC) + waiting := func(commit string, first bool, heard time.Time) inventory.BatchedMerge { + event, _ := json.Marshal(keptMerge{SourceMoved: link.SourceMoved{Owner: "novox", Repo: "mesh-delivery", Base: "main", + Commit: commit, Number: 1, HeadSHA: "a1a1a1a1a1a1"}, FirstBuild: first}) + return inventory.BatchedMerge{Repository: "novox/mesh-delivery", Branch: "main", Commit: commit, Heard: heard, Event: event} + } + got := firstBuildObservations([]inventory.BatchedMerge{ + waiting("0177ea7a00", true, now.Add(-firstBuildWaitBound-time.Minute)), + waiting("1111111100", true, now.Add(-time.Minute)), + waiting("2222222200", false, now.Add(-time.Hour)), + }, now) + if len(got) != 1 { + t.Fatalf("1146/AC8: %d conditions raised, want the one merge past the bound: %+v", len(got), got) + } + o := got[0] + if o.Kind != kindFirstBuildUnasked || o.Scope != conditions.ScopeMerge || o.Severity != conditions.Warning || + !strings.Contains(o.Summary, "novox/mesh-delivery") || !strings.Contains(o.Summary, "0177ea7a") || + !strings.Contains(o.Summary, "announcement of the operator's Approve") { + t.Fatalf("1146/AC8: the condition is %+v", o) + } + // Answered, the merge is no longer listed as waiting: nothing is observed, and the condition clears. + if cleared := firstBuildObservations(nil, now); len(cleared) != 0 { + t.Fatalf("1146/AC8: an answered merge still raises %+v", cleared) + } +} diff --git a/cmd/mesh-controller/moved.go b/cmd/mesh-controller/moved.go index 456f166b..b0a5276e 100644 --- a/cmd/mesh-controller/moved.go +++ b/cmd/mesh-controller/moved.go @@ -115,6 +115,9 @@ func movedOnApprove(d deliveryMerge, ask *asks.Ask, w *asks.Warrant) (string, er // keepingDeliveryMerges keeps each merge mesh-delivery announces in the controller's bucket for them. type keepingDeliveryMerges struct { js func() *broker.JetStream + // kept, when set, is told each merge once it is kept: a first merge waiting for the word asks its builds then + // (novox/hq ADR 0432, issue 1146). + kept func(ctx context.Context, d deliveryMerge) error } func (k keepingDeliveryMerges) KeepDeliveryMerge(ctx context.Context, body []byte) error { @@ -137,6 +140,9 @@ func (k keepingDeliveryMerges) KeepDeliveryMerge(ctx context.Context, body []byt if _, err := kv.Put(ctx, deliveryMergeKey(d.Owner, d.Repo, d.Number, d.HeadSHA), body); err != nil { return fmt.Errorf("%w: %w", link.ErrTryAgain, err) } + if k.kept != nil { + return k.kept(ctx, d) + } return nil } @@ -236,16 +242,30 @@ func approvedMove(ctx context.Context, inv registrations, forms *sourceForms, bu if !known { return "", fmt.Errorf("the controller kept no merge of %s/%s at %.8s", owner, name, r.Commit) } - k := announcedOf(merged) + return approvedAnnounced(ctx, inv, owner, name, announcedOf(merged)) +} + +// errWordNotAnApprove says the merge cannot be shown to be the operator's Approve of its head: its announcement names no +// pull request, or the word on record is not that Approve. +var errWordNotAnApprove = errors.New("the word on record is no Approve of the head merged") + +// errNotAnnounced says mesh-delivery's word on a merge is not kept (yet): the merge may have been made by someone else, +// or its word may still be on its way. +var errNotAnnounced = errors.New("mesh-delivery announced no such merge") + +// approvedAnnounced is the warrant of the merge the forge announced as k: mesh-delivery announced it, and the ask it made +// it on holds the operator's Approve of that head. Why it holds, or why not. Shared by the approved move of a known +// module and the first build of a new repository's modules (ADR 0420 §7, ADR 0432). +func approvedAnnounced(ctx context.Context, inv catalogueReader, owner, name string, k keptMerge) (string, error) { if k.Number <= 0 || k.HeadSHA == "" { - return "", errors.New("the forge's announcement of the merge names no pull request or head") + return "", fmt.Errorf("%w: the forge's announcement of the merge names no pull request or head", errWordNotAnApprove) } d, kept, err := keptDeliveryMerge(ctx, owner, name, k.Number, k.HeadSHA) if err != nil { return "", err } if !kept { - return "", fmt.Errorf("mesh-delivery announced no merge of %s/%s#%d at %.8s", owner, name, k.Number, k.HeadSHA) + return "", fmt.Errorf("%w: %s/%s#%d at %.8s", errNotAnnounced, owner, name, k.Number, k.HeadSHA) } ask, w, err := routerAskOf(ctx, inv, d.AskID) if err != nil { @@ -253,7 +273,7 @@ func approvedMove(ctx context.Context, inv registrations, forms *sourceForms, bu } why, err := movedOnApprove(d, ask, w) if err != nil { - return "", err + return "", fmt.Errorf("%w: %w", errWordNotAnApprove, err) } return fmt.Sprintf("%s/%s#%d merged at %.8s on %s", owner, name, k.Number, k.HeadSHA, why), nil } diff --git a/cmd/mesh-controller/push.go b/cmd/mesh-controller/push.go index 2ae089e6..0b5fc231 100644 --- a/cmd/mesh-controller/push.go +++ b/cmd/mesh-controller/push.go @@ -209,7 +209,8 @@ func serve(ctx context.Context) (err error) { } // And the merges mesh-delivery made, kept a week with the ask each was made on: what registers a module that // moved between repositories on the operator's Approve (novox/hq ADR 0420 §7, issue 1025). - if err := server.KeepsDeliveryMerges(keepingDeliveryMerges{js: server.JetStream}); err != nil { + if err := server.KeepsDeliveryMerges(keepingDeliveryMerges{js: server.JetStream, + kept: following{open: open}.resumeFirstBuilds}); err != nil { return err } // No timer reads the merges back (novox/hq ADR 0407 decision 3, issue 973): the forge's event and the durable diff --git a/cmd/mesh-controller/upgrades.go b/cmd/mesh-controller/upgrades.go index 94d25d4d..95be4d23 100644 --- a/cmd/mesh-controller/upgrades.go +++ b/cmd/mesh-controller/upgrades.go @@ -406,7 +406,12 @@ func askNewModules(ctx context.Context, inv *inventory.Inventory, m link.SourceM if len(added) == 0 || len(from) == 0 { return nil } - source := buildSource{Repository: from[0].Source.Repository, Seat: from[0].Source.Seat} + return askBuilds(ctx, inv, m, buildSource{Repository: from[0].Source.Repository, Seat: from[0].Source.Seat}, added) +} + +// askBuilds asks the first build of each directory of a merge's repository, as askNewModules does, from the source given. +func askBuilds(ctx context.Context, inv *inventory.Inventory, m link.SourceMoved, source buildSource, + added []string) []string { var asked []string for _, dir := range added { path := dir diff --git a/internal/inventory/batches.go b/internal/inventory/batches.go index 294c5f00..e870a676 100644 --- a/internal/inventory/batches.go +++ b/internal/inventory/batches.go @@ -84,6 +84,16 @@ func (i *Inventory) AloneMerges(ctx context.Context) ([]BatchedMerge, error) { return i.chainAloneMerges(ctx, c) } +// WaitingMerges is every merge no plan answers that is not walked alone: kept, and waiting for something else to +// happen to it (a first merge into a repository, waiting for mesh-delivery's word on it, issue 1146). +func (i *Inventory) WaitingMerges(ctx context.Context) ([]BatchedMerge, error) { + c, err := i.theChain(ctx) + if err != nil { + return nil, err + } + return i.chainWaitingMerges(ctx, c) +} + // AnswerMerge moves a merge to the plan that answers it now; an empty plan with alone leaves it waiting to // be walked on its own commit. func (i *Inventory) AnswerMerge(ctx context.Context, repository, commit, plan string, alone bool) error { diff --git a/internal/inventory/chain_rollouts.go b/internal/inventory/chain_rollouts.go index b7a9e839..6c242b6d 100644 --- a/internal/inventory/chain_rollouts.go +++ b/internal/inventory/chain_rollouts.go @@ -452,6 +452,22 @@ func (i *Inventory) chainAloneMerges(ctx context.Context, c *chain.Chain) ([]Bat return out, nil } +// chainWaitingMerges is every merge no plan answers that waits and is not walked alone, the newest first. +func (i *Inventory) chainWaitingMerges(ctx context.Context, c *chain.Chain) ([]BatchedMerge, error) { + w, err := i.chainWinners(ctx, c) + if err != nil { + return nil, err + } + var out []BatchedMerge + for _, h := range w { + if h.m.Waiting && !h.m.Alone { + out = append(out, batchedOf(h)) + } + } + sortMerges(out, true) + return out, nil +} + // pendingMerge is a merge added to a plan that is not saved yet: carried until that plan's save writes it. type pendingMerge struct { plan string diff --git a/internal/inventory/waiting_merges_test.go b/internal/inventory/waiting_merges_test.go new file mode 100644 index 00000000..4601e937 --- /dev/null +++ b/internal/inventory/waiting_merges_test.go @@ -0,0 +1,33 @@ +package inventory + +import ( + "context" + "testing" + "time" +) + +// A merge no plan answers waits, and is listed as waiting until it is answered where it is kept (novox/hq issue 1146): +// what a first merge into a new repository does while it waits for mesh-delivery's word. +func TestAMergeNoPlanAnswersIsListedWaitingUntilItIsAnswered(t *testing.T) { + inv, c := anInventoryOnTheChain(t) + ctx := context.Background() + now := time.Now().UTC() + if added, err := inv.chainAddMerge(ctx, c, BatchedMerge{Repository: "novox/mesh-delivery", Branch: "main", Commit: "c1", + Merged: now, Heard: now}); err != nil || !added { + t.Fatalf("added %v, %v", added, err) + } + waiting, err := inv.chainWaitingMerges(ctx, c) + if err != nil || len(waiting) != 1 || waiting[0].Commit != "c1" { + t.Fatalf("waiting %+v, %v", waiting, err) + } + move := []MergeMove{{Repository: "novox/mesh-delivery", Commit: "c1", Plan: WaitingRecord}} + if err := inv.chainSavePlans(ctx, c, nil, move); err != nil { + t.Fatal(err) + } + if waiting, err = inv.chainWaitingMerges(ctx, c); err != nil || len(waiting) != 0 { + t.Fatalf("answered, it still waits: %+v, %v", waiting, err) + } + if _, known, err := inv.chainMergeOf(ctx, c, "novox/mesh-delivery", "c1"); err != nil || !known { + t.Fatalf("an answered merge is no longer kept: %v %v", known, err) + } +} diff --git a/testdata/store-tests b/testdata/store-tests index 970640e6..3ba3497a 100644 --- a/testdata/store-tests +++ b/testdata/store-tests @@ -164,3 +164,4 @@ ./internal/inventory TestWhetherAMachineReadsModulesIsItsLatestWord ./cmd/mesh-controller TestAComposedDeclarationCarriesModulesAndBuildsOnlyForANodeThatReadsThem ./cmd/mesh-controller TestAPlanPassesATierWhoseMachineNeverReportsAfterTheBoundAndSendsTheNextToTheOthers +./cmd/mesh-controller TestAMergeOnTheApproveIntoANewRepositoryAsksTheFirstBuildAndItsOutcomeRegistersTheModule