diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries.go index 42f700bd5..d1363a7d2 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries.go @@ -71,6 +71,8 @@ type ForgeReads interface { Pull(repository string, number int64) (gitseat.PullAnswer, error) // Branch is a repository branch's head (the seat git's verb branch). Branch(repository, branch string) (gitseat.BranchAnswer, error) + // Contains is whether commit is in (or is) the commit in (the seat git's verb contains). + Contains(repository, commit, in string) (bool, error) } // kvKept is the store over the runtime. diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries_test.go index 7fd363538..f523fbda9 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries_test.go @@ -42,7 +42,9 @@ type fakeForge struct { open []gitseat.OpenPull pulls map[string]gitseat.Pull heads map[string]string // "@" -> head - fail bool + // contains says whether commit is in `in`; nil means every commit is in every other (a straight line forward) + contains func(commit, in string) bool + fail bool } func (f *fakeForge) OpenPulls() (gitseat.OpenPullsAnswer, error) { @@ -61,6 +63,15 @@ func (f *fakeForge) Branch(repository, branch string) (gitseat.BranchAnswer, err } return gitseat.BranchAnswer{Branch: branch, Repository: repository, Head: head}, nil } +func (f *fakeForge) Contains(repository, commit, in string) (bool, error) { + if f.fail { + return false, errors.New("the forge is away") + } + if f.contains == nil { + return true, nil + } + return f.contains(commit, in), nil +} func (f *fakeForge) Pull(repository string, number int64) (gitseat.PullAnswer, error) { p, ok := f.pulls[fmt.Sprintf("%s#%d", repository, number)] if !ok { diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go index 1992a61e6..ec423fb96 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go @@ -32,7 +32,7 @@ func (l *listening) get() string { l.mu.Lock(); defer l.mu.Unlock(); return l.n // The events this owner follows: the forge bridge's word on an act owed, and the step events the delivery records are built // from (consumer.go). -var followed = append(append([]string{owedEndedEvent}, stepPatterns()...), pullUpdatedEvent, pullMergedEvent, pullClosedEvent, registrationChangedEvent) +var followed = append(append([]string{owedEndedEvent}, stepPatterns()...), pullUpdatedEvent, pullMergedEvent, pullClosedEvent, registrationChangedEvent, forgeMergedEvent) func main() { settingsFile := os.Getenv("MESH_DELIVERY_GATE_SETTINGS") @@ -118,7 +118,13 @@ func run(h *Holder, l *listening) { l.set("listening") logf("[mesh-delivery] listening: %s", strings.Join(followed, ", ")) // A watched branch no source key holds starts from the forge, once at each start and at each registration change (A-401). - go startSourcesAtStart(h) + // then each head is compared with the forge's, and a merge made outside the gate is followed (A-464). + go func() { + startSourcesAtStart(h) + if _, err := h.FollowAtStart(); err != nil { + logf("[mesh-delivery] the sources were not all compared with the forge's heads: %v", err) + } + }() // A missed pull event is put right from the forge's own record, once at each start (§716). if err := h.ReadForge(); err != nil { logf("[mesh-delivery] the open pull requests were not read from the forge, they are read again at the next start: %v", err) @@ -179,6 +185,9 @@ func take(h *Holder, e stdio.Envelope) error { _, err := h.StartSources() return err } + if forgeMergedEvent != "" && stdio.TopicMatches(forgeMergedEvent, e.Key) { + return h.ForgeMerged(e.Body) + } if e.Key == pullClosedEvent { return errors.Join(h.DeliveryClosed(e.Body), h.pullEnded(e.Body, "closed")) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go index 7e060757d..de4dac66d 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go @@ -134,13 +134,16 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { } } } - // From G's delivery the gate hears neither the bridge's merged and pushed messages nor the teller's answers nor the view's - // reach (design 47 G §592, §593): it consumes the indexer's take-in.taken and the bridge's owed.ended, and the git events that clear + // From G's delivery the gate hears neither the bridge's pushed messages nor the teller's answers nor the view's reach + // (design 47 G §592, §593); it hears the bridge's merged message alone, to follow every merge (A-464): it consumes the indexer's take-in.taken and the bridge's owed.ended, and the git events that clear // an approval carried. if want := append(slices.Clone(followed), meshconditions.Seat+"."+meshconditions.EventFindingsAsked); !reflect.DeepEqual(m.Consumes, want) { t.Fatalf("consumes %v, follows %v", m.Consumes, followed) } for _, c := range m.Consumes { + if c == forgeMergedEvent { + continue // A-464: the source follows every merge, so the gate hears the forge bridge's merged message, and it alone + } for _, gone := range []string{".forge.", "teller", "view", "forge-pull", "forge-status"} { if strings.Contains(c, gone) { t.Errorf("the manifest still consumes %s, which G's gate does not hear (§431, §592, §593)", c) diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow.go new file mode 100644 index 000000000..0cba15c8d --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow.go @@ -0,0 +1,204 @@ +package main + +// The gate's source of a watched branch follows every merge into that branch, whoever made it (design 47 G A-464, A-464b, amending +// A-463): on the forge bridge's `merged` of a repository the source names, at the branch the source watches, the gate moves that +// repository's head to the merge and says source-moved, as a take-in moves it; at its start it compares each head with the +// forge's branch head, asked of the seat git, and moves every one behind it the same way, so a merge whose message was lost is +// followed too; gate_source_follow does the same for one repository now. A head the source does not name, or a branch it does +// not watch, is left: only a start or a registration change adds them (sourcestart.go). + +import ( + "encoding/json" + "errors" + "fmt" + "strconv" + "strings" + + "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" + "git.novox.be/novox/mesh-sdk/go/keys" +) + +// forgeMergedEvent is the forge bridge's merged message of any repository, as the manifest consumes it. +var forgeMergedEvent = declared("forge.*.*.merged") + +// moveHead moves the head the branch's source names for repository to head, and says source-moved with the key's revision. It +// answers false, and writes nothing, when the branch has no source key, the key does not name the repository or names the head +// already. group is the group that made the merge, empty for a merge no group of the gate made. +func (h *Holder) moveHead(branch, repository, head, group string) (moved bool, err error) { + if h.Source == nil || head == "" { + return false, nil + } + held, _, err := h.Source.Read(branch) + if err != nil || held == nil { + return false, err + } + name := "" + for r := range held.Heads { + if trunk.SameRepository(r, repository) { + name = r + } + } + if name == "" || held.Heads[name] == head { + return false, nil // not named, or no change: no revision and no source-moved (A-464c) + } + was := held.Heads[name] + // Only forward (A-464c): the head held must be contained in the merge, asked of the seat git. + if h.Forge == nil { + return false, errors.New("this gate reads no forge, so it cannot tell whether the merge is ahead of the head") + } + ahead, err := h.Forge.Contains(name, was, head) + if err != nil { + return false, fmt.Errorf("whether %.12s is in %.12s of %s cannot be read, so the head stays: %w", was, head, name, err) + } + if !ahead { + h.logf("[mesh-delivery] source.%s keeps %s at %.12s: the merge %.12s does not contain it (heard again, late or out of order), left without effect", branch, name, was, head) + return false, nil + } + rev, err := h.Source.Set(branch, func(prev *Source) (Source, bool) { + moved = false + if prev == nil || prev.Heads[name] != was { + return Source{}, false // another writer moved it first + } + next := Source{Group: group, Heads: map[string]string{}} + for r, c := range prev.Heads { + next.Heads[r] = c + } + next.Heads[name] = head + moved = true + return next, true + }) + if err != nil { + return false, fmt.Errorf("source.%s cannot be moved: %w", branch, err) + } + if !moved || h.SaySeat == nil { + return moved, nil + } + at := h.Now().UTC() + return true, h.SaySeat(seat.SubjectSourceMoved(keys.EscapeName(branch)), + seat.SourceMovedEvent{Branch: branch, Group: group, Revision: int64(rev), At: &at}, + "source-moved."+keys.EscapeName(branch)+"."+strconv.FormatUint(rev, 10)) +} + +// ForgeMerged takes one merged message of the forge bridge: the head of its repository in the source of the branch it landed on +// moves to the merge. A merge no delivery group made (the message names no group) is said so. A message that cannot be read, or +// names no branch or merge, is said and taken: heard again it would be refused again. +func (h *Holder) ForgeMerged(body json.RawMessage) error { + f, err := forgemsg.DecodeForge(body) + if err != nil { + h.logf("[mesh-delivery] a merged message of the forge bridge is not readable, the source does not follow it: %v", err) + return nil + } + if f.Kind != forgemsg.ForgeMerged { + return nil + } + if f.Base == "" || f.Merge == "" { + h.logf("[mesh-delivery] the merged message of %s names no branch or no merge, the source does not follow it", f.Repository) + return nil + } + moved, err := h.moveHead(f.Base, f.Repository, f.Merge, f.Group) + if err != nil { + return err + } + if moved { + if f.Group == "" { // A-464b: a merge whose message names no delivery group is one the gate did not make + h.logf("[mesh-delivery] source.%s follows the merge %.12s of %s: not made by a delivery group of the gate", f.Base, f.Merge, f.Repository) + } else { + h.logf("[mesh-delivery] source.%s follows the merge %.12s of %s, made by the delivery group %s", f.Base, f.Merge, f.Repository, f.Group) + } + } + return nil +} + +// FollowAtStart compares each head of every source key with the forge's head of that branch and moves every one behind it, once +// at each start. A head the forge cannot read is said and left. It answers the heads moved as "@". +func (h *Holder) FollowAtStart() ([]string, error) { + if h.Source == nil || h.Forge == nil { + return nil, errors.New("this gate keeps no source or reads no forge") + } + branches, err := h.Source.Branches() + if err != nil { + return nil, err + } + var moved []string + var errs []error + for _, branch := range branches { + held, _, err := h.Source.Read(branch) + if err != nil || held == nil { + errs = append(errs, err) + continue + } + for repository, head := range held.Heads { + ok, err := h.followForge(branch, repository, head) + if err != nil { + errs = append(errs, err) + h.logf("[mesh-delivery] source.%s: %s is not compared with the forge's head: %v", branch, repository, err) + continue + } + if ok { + moved = append(moved, repository+"@"+branch) + } + } + } + h.logf("[mesh-delivery] the sources compared with the forge's heads at the start: moved %v", moved) + return moved, errors.Join(errs...) +} + +// followForge moves one repository's head in a branch's source to the forge's head of the branch when they differ. +func (h *Holder) followForge(branch, repository, held string) (bool, error) { + a, err := h.Forge.Branch(repository, branch) + if err == nil && a.Error != "" { + err = errors.New(a.Error) + } + if err == nil && a.Head == "" { + err = errors.New("the forge named no head") + } + if err != nil { + return false, err + } + if sameCommit(held, a.Head) && len(held) == len(a.Head) { + return false, nil + } + moved, err := h.moveHead(branch, repository, a.Head, "") + if moved { + h.logf("[mesh-delivery] source.%s follows the forge: %s from %.12s to %.12s", branch, repository, held, a.Head) + } + return moved, err +} + +// SourceFollow moves one repository's head to the forge's head of the branch by hand, after the hand-act log took it. +func (h *Holder) SourceFollow(branch, repository, why, by string, acts handActs) (map[string]any, error) { + if strings.TrimSpace(branch) == "" || strings.TrimSpace(repository) == "" || strings.TrimSpace(why) == "" { + return nil, errors.New("say the branch, the repository and why its head follows the forge") + } + if h.Source == nil || h.Forge == nil { + return nil, errors.New("this gate keeps no source or reads no forge") + } + if acts == nil { + return nil, errors.New("the hand-act log cannot be reached here, so nothing was moved") + } + held, _, err := h.Source.Read(branch) + if err != nil { + return nil, err + } + if held == nil { + return nil, fmt.Errorf("the gate keeps no source of the branch %s", branch) + } + name := "" + for r := range held.Heads { + if trunk.SameRepository(r, repository) { + name = r + } + } + if name == "" { + return nil, fmt.Errorf("the source of %s names no head of %s", branch, repository) + } + if _, err := acts.Record(meshhandactsclient.Request{What: "moved the head of " + repository + " in the source of the branch " + branch + " to the forge's head", + Why: why, Cause: "gate_source_follow", Verb: "source-follow"}); err != nil { + return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was moved: %w", err) + } + moved, err := h.followForge(branch, name, held.Heads[name]) + return map[string]any{"branch": branch, "repository": name, "moved": moved, "by": by}, err +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow_test.go new file mode 100644 index 000000000..7d98cec58 --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow_test.go @@ -0,0 +1,166 @@ +package main + +import ( + "encoding/json" + "errors" + "strings" + "testing" + "time" + + meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// Design 47 G A-464, A-464b: the forge bridge's merged message moves the head of its repository in the source of the branch it +// landed on and says source-moved; a merge naming no delivery group is said not made by one; a branch no source key holds, a +// repository the key does not name, and a head the key holds already move nothing. +func TestAMergedMessageMovesTheSourceOfItsBranchWhoeverMadeTheMerge(t *testing.T) { + old, merge := strings.Repeat("a", 40), strings.Repeat("b", 40) + body := func(repository, base, group string) []byte { + f := forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: repository, Base: base, Merge: merge, Head: merge, Number: 14, Root: strings.Repeat("d", 40)} + data, err := forgemsg.EncodeForge(f) + if err != nil { + t.Fatal(err) + } + return data + } + for _, c := range []struct { + name, repository, base string + moved bool + }{ + {"watched branch, named repository", "novox/mesh-contracts", "main", true}, + {"a branch no source key holds", "novox/mesh-contracts", "other", false}, + {"a repository the key does not name", "novox/other", "main", false}, + } { + t.Run(c.name, func(t *testing.T) { + src := &memSource{vals: map[string]Source{"main": {Group: "g", Heads: map[string]string{"novox/mesh-contracts": old}}}} + var said []string + var logged []string + h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{}, + Logf: func(f string, a ...any) { logged = append(logged, f) }, + SaySeat: func(subject string, _ any, id string) error { said = append(said, id); return nil }} + if err := h.ForgeMerged(body(c.repository, c.base, "")); err != nil { + t.Fatal(err) + } + got := src.vals["main"] + if c.moved != (got.Heads["novox/mesh-contracts"] == merge) || c.moved != (len(said) == 1) || (c.moved && got.Group != "") { + t.Fatalf("source %+v said %v: moved wanted %v", got, said, c.moved) + } + if c.moved && !strings.Contains(strings.Join(logged, "\n"), "not made by a delivery group of the gate") { + t.Errorf("a merge no group made is not said so: %v", logged) + } + // Heard again, the key names the head already: nothing is moved or said twice. + if err := h.ForgeMerged(body(c.repository, c.base, "")); err != nil || len(said) > 1 { + t.Errorf("heard twice: %v, said %v", err, said) + } + }) + } +} + +// Design 47 G A-464: at its start the gate compares each head with the forge's branch head and moves every one behind it; a head +// the forge cannot read is said and left; gate_source_follow does it for one repository after the hand-act log took it. +func TestTheStartAndTheToolMoveAHeadBehindTheForge(t *testing.T) { + old, now := strings.Repeat("a", 40), strings.Repeat("c", 40) + mk := func() (*Holder, *branchSource, *[]string) { + src := &branchSource{memSource{vals: map[string]Source{"main": {Heads: map[string]string{"o/f1": old, "o/f2": old, "o/f3": now}}}}} + var said []string + return &Holder{Now: time.Now, Source: src, Logf: func(string, ...any) {}, + Forge: &fakeForge{heads: map[string]string{"o/f1@main": now, "o/f3@main": now}}, // o/f2 cannot be read + SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }}, src, &said + } + h, src, said := mk() + moved, err := h.FollowAtStart() + if err == nil || len(moved) != 1 || moved[0] != "o/f1@main" || src.vals["main"].Heads["o/f1"] != now || src.vals["main"].Heads["o/f2"] != old || len(*said) != 1 { + t.Fatalf("start: moved %v err %v heads %v said %v: want only o/f1 moved, o/f2 left with an error said", moved, err, src.vals["main"].Heads, *said) + } + h, src, said = mk() + acts := &handActsFake{refuse: errors.New("log down")} + if _, err := h.SourceFollow("main", "o/f1", "missed a merge", "me", acts); err == nil || src.vals["main"].Heads["o/f1"] != old { + t.Fatalf("moved without the log: %v", err) + } + acts.refuse = nil + if _, err := h.SourceFollow("main", "o/f1", "", "me", acts); err == nil { + t.Fatal("moved with no reason") + } + out, err := h.SourceFollow("main", "o/f1", "missed a merge", "me", acts) + if err != nil || out["moved"] != true || len(acts.took) != 1 || acts.took[0].Cause != "gate_source_follow" || src.vals["main"].Heads["o/f1"] != now { + t.Fatalf("tool: %v %v log %+v heads %v", out, err, acts.took, src.vals["main"].Heads) + } +} + +// Design 47 G A-464: the gate takes the forge bridge's merged event of any repository, by the manifest's own pattern. +func TestTheGateTakesTheForgeBridgesMergedEventOfAnyRepository(t *testing.T) { + merge := strings.Repeat("b", 40) + data, err := forgemsg.EncodeForge(forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: "novox/mesh-contracts", Base: "main", Merge: merge, Head: merge, Number: 14, Root: strings.Repeat("d", 40)}) + if err != nil { + t.Fatal(err) + } + src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/mesh-contracts": strings.Repeat("a", 40)}}}} + h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{}, Logf: func(string, ...any) {}} + key := strings.Replace(strings.Replace(forgeMergedEvent, "*", "novox", 1), "*", "mesh-contracts", 1) + if err := take(h, stdio.Envelope{Key: key, Body: data}); err != nil || src.vals["main"].Heads["novox/mesh-contracts"] != merge { + t.Fatalf("event %q: %v, source %+v", key, err, src.vals["main"]) + } +} + +// Design 47 G A-464c: a head moves only forward and only when it changes. A merged message for a commit that does not contain +// the held head (replayed, late, out of order) moves nothing and is said; a second message for the same head writes no +// revision; and a take-in.taken for a head a merged already moved writes none and says no source-moved. +func TestAHeadMovesOnlyForwardAndOnlyWhenItChanges(t *testing.T) { + oldC, newC := strings.Repeat("a", 40), strings.Repeat("b", 40) + msg := func(merge string) []byte { + data, err := forgemsg.EncodeForge(forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: "novox/app", Base: "main", Merge: merge, Head: merge, Number: 3, Root: strings.Repeat("d", 40)}) + if err != nil { + t.Fatal(err) + } + return data + } + src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/app": newC}}}} + var said, logged []string + // newC contains oldC, not the other way round. + forge := &fakeForge{contains: func(commit, in string) bool { return commit == in || (commit == oldC && in == newC) }} + h := &Holder{Now: time.Now, Source: src, Forge: forge, Logf: func(f string, a ...any) { logged = append(logged, f) }, + SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }} + if err := h.ForgeMerged(msg(oldC)); err != nil || src.vals["main"].Heads["novox/app"] != newC || src.rev != 0 || len(said) != 0 || len(logged) == 0 { + t.Fatalf("a replayed old merge: %v, source %+v, revisions %d, said %v, logged %v: want nothing moved and a log line", err, src.vals["main"], src.rev, said, logged) + } + if err := h.ForgeMerged(msg(newC)); err != nil || src.rev != 0 || len(said) != 0 { + t.Fatalf("a second message for the same head wrote revision %d, said %v", src.rev, said) + } + // A take-in.taken naming the head a merged moved already writes no revision and says nothing. + body := []byte(`{"v":1,"kind":"taken","key":"g.feat","what":"group","group":"feat","branch":"main","source":7,` + + `"merges":[{"repository":"novox/app","number":3,"merge":"` + newC + `"}],"entries":[]}`) + if err := h.moveSource(mustTaken(t, body)); err != nil || src.rev != 0 || len(said) != 0 { + t.Fatalf("take-in for a head already moved: %v, revision %d, said %v", err, src.rev, said) + } + // A real forward move still writes one revision. + src.vals["main"] = Source{Heads: map[string]string{"novox/app": oldC}} + if err := h.ForgeMerged(msg(newC)); err != nil || src.rev != 1 || len(said) != 1 { + t.Fatalf("a forward move: %v, revision %d, said %v", err, src.rev, said) + } +} + +func mustTaken(t *testing.T, body []byte) meshcatalog.TakeInTaken { + t.Helper() + var v meshcatalog.TakeInTaken + if err := json.Unmarshal(body, &v); err != nil { + t.Fatal(err) + } + return v +} + +// Design 47 G A-464d: a take-in.taken arriving after a later merge moved the head leaves the head where it is. +func TestATakeInTakenAfterALaterMergeLeavesTheHead(t *testing.T) { + oldC, newC := strings.Repeat("a", 40), strings.Repeat("b", 40) + src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/app": newC}}}} + var said []string + h := &Holder{Now: time.Now, Source: src, Logf: func(string, ...any) {}, + Forge: &fakeForge{contains: func(commit, in string) bool { return commit == in || (commit == oldC && in == newC) }}, + SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }} + body := []byte(`{"v":1,"kind":"taken","key":"g.feat","what":"group","group":"feat","branch":"main","source":7,` + + `"merges":[{"repository":"novox/app","number":3,"merge":"` + oldC + `"}],"entries":[]}`) + if err := h.moveSource(mustTaken(t, body)); err != nil || src.vals["main"].Heads["novox/app"] != newC || src.rev != 0 || len(said) != 0 { + t.Fatalf("take-in of an older merge: %v, source %+v, revision %d, said %v: want the head left at the later merge", err, src.vals["main"], src.rev, said) + } +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go index f7538a412..20341b06c 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go @@ -55,6 +55,18 @@ func (s seatGit) Branch(repository, branch string) (gitseat.BranchAnswer, error) return a, s.call(gitseat.VerbBranch, gitseat.BranchInput{Repository: repository, Branch: branch}, &a) } +// Contains is whether commit is the commit in or an ancestor of it. +func (s seatGit) Contains(repository, commit, in string) (bool, error) { + var a gitseat.ContainsAnswer + if err := s.call(gitseat.VerbContains, gitseat.ContainsInput{Repository: repository, Commit: commit, In: in}, &a); err != nil { + return false, err + } + if a.Error != "" { + return false, errors.New(a.Error) + } + return a.Contains, nil +} + // watchedRepositories are the repositories each watched branch is registered for, each once, sorted. func watchedRepositories(regs []meshcatalog.Registration) map[string][]string { seen := map[string]map[string]bool{} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go index 6d3f7586c..b756d4271 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go @@ -89,5 +89,16 @@ func sourceTools(h *Holder, acts handActs) []stdio.Tool { Run: func(a map[string]any) (any, error) { return h.SourceStart(strArg(a, "branch"), strArg(a, "why"), stdio.CallerOf(a), acts) }}, + {Name: "gate_source_follow", + Description: "Move the head one watched branch's source names for one repository to the forge's head of that branch now, as " + + "the gate does when it hears a merge and at its start (design 47 G A-464); recorded in the hand-act log of the seat " + + "mesh-hand-acts first. Nothing is moved when the source already names the forge's head.", + Input: map[string]any{"type": "object", "properties": map[string]any{ + "branch": str("the watched branch"), "repository": str("the repository, owner/name"), + "why": str("why its head follows the forge by hand, in plain words")}, + "required": []string{"branch", "repository", "why"}}, + Run: func(a map[string]any) (any, error) { + return h.SourceFollow(strArg(a, "branch"), strArg(a, "repository"), strArg(a, "why"), stdio.CallerOf(a), acts) + }}, } } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go index d69ae8dc1..11fd7131a 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go @@ -9,6 +9,7 @@ import ( mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" "git.novox.be/novox/mesh-sdk/go/keys" ) @@ -107,9 +108,43 @@ func (h *Holder) moveSource(t meshcatalog.TakeInTaken) error { for _, m := range t.Merges { heads[m.Repository] = m.Merge } + // A head moves only forward (A-464d): a repository whose held head is not contained in the merge is left where it is. + if held, _, err := h.Source.Read(t.Branch); err != nil { + return err + } else if held != nil && h.Forge != nil { + for r, c := range heads { + for name, was := range held.Heads { + if !trunk.SameRepository(name, r) || was == c { + continue + } + ahead, err := h.Forge.Contains(name, was, c) + if err != nil { + return fmt.Errorf("whether %.12s is in %.12s of %s cannot be read, so the head stays: %w", was, c, name, err) + } + if !ahead { + h.logf("[mesh-delivery] source.%s keeps %s at %.12s: the take-in's merge %.12s does not contain it, left without effect", t.Branch, name, was, c) + delete(heads, r) + } + } + } + if len(heads) == 0 { + return nil + } + } + wrote := false rev, err := h.Source.Set(t.Branch, func(prev *Source) (Source, bool) { + wrote = false next := Source{Group: t.Group, Heads: heads} if prev != nil { + same := true + for r, c := range heads { + if prev.Heads[r] != c { + same = false + } + } + if same { + return Source{}, false // a merged message moved these heads already: no revision (A-464c) + } next.Heads = map[string]string{} for r, c := range prev.Heads { next.Heads[r] = c @@ -118,12 +153,13 @@ func (h *Holder) moveSource(t meshcatalog.TakeInTaken) error { next.Heads[r] = c } } + wrote = true return next, true }) if err != nil { return fmt.Errorf("source.%s cannot be moved: %w", t.Branch, err) } - if h.SaySeat == nil { + if h.SaySeat == nil || !wrote { return nil } at := h.Now().UTC() diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go index 543a183bf..a4c50ec07 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go @@ -84,7 +84,7 @@ func TestATakeInTakenOpensOneRecordAndMovesItsBranchsSource(t *testing.T) { if s := src.vals["main"]; s.Group != "feat" || s.Heads["novox/app"] != merge { t.Fatalf("source.main %+v", s) } - if len(events) != 2 || !strings.HasSuffix(events[0].subject, ".event.source-moved.main") { + if len(events) != 1 || !strings.HasSuffix(events[0].subject, ".event.source-moved.main") { t.Fatalf("said %+v", events) } drop := json.RawMessage(`{"v":1,"kind":"taken","key":"m.00000000000000000002","what":"revision","dropped":true,"branch":"main","source":8,` + @@ -92,7 +92,7 @@ func TestATakeInTakenOpensOneRecordAndMovesItsBranchsSource(t *testing.T) { if err := c.Hear(stdio.Envelope{Key: "mesh-delivery-indexer.take-in.taken", Body: drop}); err != nil { t.Fatal(err) } - if kept, _ := h.Records.Kept(); len(kept) != 1 || len(events) != 2 { + if kept, _ := h.Records.Kept(); len(kept) != 1 || len(events) != 1 { t.Fatalf("a hand drop opened %d records and said %d events", len(kept), len(events)) } } diff --git a/modules/mesh-delivery-gate/module.json b/modules/mesh-delivery-gate/module.json index 2d170922d..ebddcb329 100644 --- a/modules/mesh-delivery-gate/module.json +++ b/modules/mesh-delivery-gate/module.json @@ -23,6 +23,7 @@ "git.pull-merged", "git.pull-closed", "mesh-catalog.registration-changed", + "mesh-delivery-forge-bridge.forge.*.*.merged", "mesh-conditions.findings-asked" ], "emits": [ @@ -383,6 +384,7 @@ "gate_records", "gate_source", "gate_source_again", + "gate_source_follow", "gate_source_start" ], "config": {