diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go index c769e9a4b..3170fb6e1 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go @@ -28,5 +28,5 @@ type FindingWriter interface { // generated contract of the seat that defines it (A-17). It is started once the runtime serves (A-40). func NewFindingWriter() (*views.HeldWriter[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent], error) { return views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{ - Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{SourceUncompared}}) + Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{}}) } 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 df8d272b5..3d8193cdc 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()...), registrationChangedEvent, forgeMergedEvent) +var followed = append(append([]string{owedEndedEvent}, stepPatterns()...), registrationChangedEvent) func main() { settingsFile := os.Getenv("MESH_DELIVERY_GATE_SETTINGS") @@ -114,13 +114,7 @@ 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). - // 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) - } - }() + go startSourcesAtStart(h) } // statePins says every pin the records hold once at each start (design 47 G §579), retrying until it can. @@ -156,9 +150,6 @@ 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) - } return nil } 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 b0542e949..b9df64585 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 @@ -136,15 +136,12 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { } } // 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. + // (design 47 G §592, §593), and from H1 not its merged message either (design 47 H §737, §764): it consumes the indexer's + // take-in.taken and the bridge's owed.ended. 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/record.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go index 99edd8393..8df97b3c4 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go @@ -485,25 +485,10 @@ func (rs *Records) Read(instance string) (*Record, error) { // opened now. Each open removes the records past their retention first (the retention needs no timer: records open as // deliveries do). func (rs *Records) Open(milestone string, heads map[string]string, forgeMade bool) (r *Record, opened bool, err error) { - return rs.open(milestone, heads, forgeMade, ForgeMerge{}) + return rs.open(milestone, heads, forgeMade) } -// ForgeMerge is the merge a forge-made record is for: what record-opened says of it (design 47 G A-161). -type ForgeMerge struct { - Repository string - Pull int64 - Commit string - Branch string -} - -// OpenForgeMade opens the record of a merge made at the forge, marked forge-made, and says record-opened with the merge's -// repository, pull request, commit and branch, so the teller makes no call per message (A-161). No path of the gate opens one -// from G's delivery (§151): the teller says none while nothing calls this. -func (rs *Records) OpenForgeMade(milestone string, heads map[string]string, m ForgeMerge) (r *Record, opened bool, err error) { - return rs.open(milestone, heads, true, m) -} - -func (rs *Records) open(milestone string, heads map[string]string, forgeMade bool, m ForgeMerge) (r *Record, opened bool, err error) { +func (rs *Records) open(milestone string, heads map[string]string, forgeMade bool) (r *Record, opened bool, err error) { inst, err := RecordInstance(milestone, heads) if err != nil { return nil, false, err @@ -538,8 +523,7 @@ func (rs *Records) open(milestone string, heads map[string]string, forgeMade boo // Said once, under the record's id: the watchman times it against 24 hours, ended by record-ended. A say that fails is // said again by the next write of the record (the id keeps it once). at := now.UTC() - if serr := rs.say(meshdelivery.SubjectRecordOpened, meshdelivery.RecordOpenedEvent{Record: inst, Milestone: milestone, ForgeMade: forgeMade, At: &at, - Repository: m.Repository, Pull: m.Pull, Commit: m.Commit, Branch: m.Branch}, + if serr := rs.say(meshdelivery.SubjectRecordOpened, meshdelivery.RecordOpenedEvent{Record: inst, Milestone: milestone, ForgeMade: forgeMade, At: &at}, "record-opened."+inst); serr != nil && rs.Logf != nil { rs.Logf("[mesh-delivery] the record %s is opened and its opening is not said: %v", inst, serr) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go index 86e0a44a9..2b51ba7de 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go @@ -773,31 +773,6 @@ func TestTheRecordsToolsReadThemAndEndOneByHand(t *testing.T) { }) } -// A-161: record-opened says what its hearers need to tell a forge-made record: the repository, the pull request, the merge -// commit and the branch; an ordinary record says none of them. -func TestRecordOpenedCarriesAForgeMadeRecordsMerge(t *testing.T) { - var said []meshdelivery.RecordOpenedEvent - rs := &Records{Ledger: newLedgerFake(t), Now: func() time.Time { return time.Date(2026, 11, 12, 5, 0, 0, 0, time.UTC) }, - Say: func(subject string, body any, id string) error { - if subject == meshdelivery.SubjectRecordOpened { - said = append(said, body.(meshdelivery.RecordOpenedEvent)) - } - return nil - }} - commit := strings.Repeat("3", 40) - if _, _, err := rs.OpenForgeMade("main.5", map[string]string{"novox/app@333333333333": commit}, - ForgeMerge{Repository: "novox/app", Pull: 12, Commit: commit, Branch: "main"}); err != nil { - t.Fatal(err) - } - if _, _, err := rs.Open("47/G", map[string]string{"novox/app@111111111111": strings.Repeat("1", 40)}, false); err != nil { - t.Fatal(err) - } - if len(said) != 2 || !said[0].ForgeMade || said[0].Repository != "novox/app" || said[0].Pull != 12 || said[0].Commit != commit || - said[0].Branch != "main" || said[1].ForgeMade || said[1].Repository != "" || said[1].Pull != 0 || said[1].Commit != "" || said[1].Branch != "" { - t.Fatalf("said %+v", said) - } -} - // W11.20 (design 47 *The delivery record*: a store filling is said before it is full): the gate reads its ledger's fill, // says filling when a record takes it past 80 % of its cap, refuses the next record as full and holds its merge, and // says room and hands the merge once the ledger is below 80 % again; the read tool says the fill. diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow.go deleted file mode 100644 index 6fef43a91..000000000 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow.go +++ /dev/null @@ -1,278 +0,0 @@ -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 ( - "context" - "encoding/json" - "errors" - "fmt" - "sort" - "strconv" - "strings" - "time" - - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" - 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 -} - -// SourceUncompared is the finding of a start comparison that left a head uncompared (A-464e). -const SourceUncompared = "source-uncompared" - -// FollowAtStart compares each head of every source key with the forge's head of that branch and moves every one behind it, at -// each start. It ends only when every head was compared (A-464e): a head the forge could not be asked for (the seat git not -// answering yet, as when its bundle starts in the same tool runner) is compared again after a growing pause of at most a -// minute, and the finding source-uncompared names the repositories and the last error while any stands. 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") - } - var moved []string - var only map[string]bool - for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) { - got, left := h.followPass(only) - moved = append(moved, got...) - if len(left) == 0 { - h.clearUncompared() - h.logf("[mesh-delivery] the sources compared with the forge's heads at the start: moved %v", moved) - return moved, nil - } - only = map[string]bool{} - var names []string - var last error - for n, err := range left { - only[n] = true - names = append(names, n) - last = err - } - sort.Strings(names) - h.sayUncompared(names, last) - h.logf("[mesh-delivery] %d heads are not compared with the forge's yet (%s; last: %v); asking again in %s", len(names), strings.Join(names, ", "), last, wait) - startSleep(wait) - } -} - -// followPass compares the heads named in only (every head when nil) once; it answers the heads moved and, by "@", -// each head it could not compare with why. A source it cannot list or read is one entry named for itself. -func (h *Holder) followPass(only map[string]bool) (moved []string, left map[string]error) { - left = map[string]error{} - branches, err := h.Source.Branches() - if err != nil { - left["the branches of the source"] = err - return nil, left - } - for _, branch := range branches { - held, _, err := h.Source.Read(branch) - if err != nil { - left["source."+branch] = err - continue - } - if held == nil { - continue - } - for repository, head := range held.Heads { - name := repository + "@" + branch - if only != nil && !only[name] { - continue - } - ok, err := h.followForge(branch, repository, head) - if err != nil { - left[name] = err - continue - } - if ok { - moved = append(moved, name) - } - } - } - return moved, left -} - -func (h *Holder) sayUncompared(names []string, last error) { - if h.Findings == nil { - return - } - at := h.Now().UTC() - err := h.Findings.Write(context.Background(), meshconditions.Finding{Scope: FindingScope, ID: SourceUncompared, Kind: SourceUncompared, - Subject: meshconditions.Subject{Scope: FindingScope, ID: SourceUncompared}, - Severity: meshconditions.FindingSeverityWarning, Resolver: "operator", - Summary: fmt.Sprintf("the gate's source is not compared with the forge's head for %s; the last error: %v", strings.Join(names, ", "), last), - Headline: "The gate's source is not compared with the forge", - Explanation: "At its start the gate compares each head of its source with the forge's head of the branch. A head it could not compare is asked again after a growing pause, and this clears once every head is compared.", - Needs: "the seat git answering; gate_source_follow moves one repository's head by hand.", - Source: SourceUncompared, Observer: Module, At: at, Since: at}) - if err != nil { - h.logf("[mesh-delivery] the finding %s is not written: %v", SourceUncompared, err) - } -} - -func (h *Holder) clearUncompared() { - if h.Findings == nil { - return - } - for _, f := range h.Findings.Held() { - if f.Scope == FindingScope && f.ID == SourceUncompared && f.Kind == SourceUncompared { - if err := h.Findings.Clear(context.Background(), FindingScope, SourceUncompared, SourceUncompared, "every head is compared with the forge's"); err != nil { - h.logf("[mesh-delivery] the finding %s is not cleared: %v", SourceUncompared, err) - } - } - } -} - -// 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 deleted file mode 100644 index 214564098..000000000 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow_test.go +++ /dev/null @@ -1,193 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "errors" - "strings" - "testing" - "time" - - "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" - 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() - fw := &fakeFindings{} - h.Findings = fw - forge := h.Forge.(*fakeForge) - var pauses []time.Duration - var heldWhileWaiting int - prevSleep := startSleep - startSleep = func(d time.Duration) { - pauses = append(pauses, d) - heldWhileWaiting = len(fw.held) // the finding stands while a head is uncompared - forge.heads["o/f2@main"] = now // the seat git answers by the second pass - } - defer func() { startSleep = prevSleep }() - moved, err := h.FollowAtStart() - if err != nil || len(moved) != 2 || src.vals["main"].Heads["o/f1"] != now || src.vals["main"].Heads["o/f2"] != now || len(*said) != 2 { - t.Fatalf("start: moved %v err %v heads %v said %v: want o/f1 and, after the second pass, o/f2 moved", moved, err, src.vals["main"].Heads, *said) - } - if len(pauses) != 1 || heldWhileWaiting != 1 || len(fw.held) != 0 { - t.Fatalf("pauses %v, finding held while waiting %d and after %d: want one pause, the finding raised while a head stood uncompared and cleared after", pauses, heldWhileWaiting, len(fw.held)) - } - 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) - } -} - -// fakeFindings is the gate's finding writer in memory. -type fakeFindings struct{ held []meshconditions.Finding } - -func (f *fakeFindings) Write(_ context.Context, x meshconditions.Finding) error { - f.held = append(f.held, x) - return nil -} -func (f *fakeFindings) Clear(_ context.Context, _, _, _, _ string) error { f.held = nil; return nil } -func (f *fakeFindings) Held() []meshconditions.Finding { return f.held } 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 2d559de34..56ce1c278 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go @@ -54,8 +54,6 @@ func (s seatCatalog) Registrations() (meshcatalog.RegistrationsAnswer, error) { type ForgeReads interface { // 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) } // seatGit reads the seat git through the tool runner. @@ -82,17 +80,6 @@ func (s seatGit) Branch(repository, branch string) (gitseat.BranchAnswer, error) } // 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 b756d4271..6d3f7586c 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go @@ -89,16 +89,5 @@ 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 15619be8a..84a1327c7 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go @@ -9,14 +9,11 @@ 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" ) -// What a take-in.taken does at the gate (design 47 G A-33, A-34; §151, §291, §592): it opens one delivery record, keyed by the -// taken body's branch and source revision, and moves that branch's source revision, saying source-moved. Nothing else opens a -// record or moves a source key. +// What a take-in.taken does at the gate (design 47 G A-33; §151, §592): it opens one delivery record, keyed by the taken +// body's branch and source revision. It moves no source key: only a merge call does (design 47 H §737, §764). // recordMilestone is the key of a taken revision's record: `.`, each escaped as a name (A-33). func recordMilestone(branch string, source int64) string { @@ -24,8 +21,8 @@ func recordMilestone(branch string, source int64) string { } // TakenIn takes one take-in.taken body: the record of its source revision is opened (safe to repeat: a record there already -// is left as it is), its members are the entries' pull requests, its heads the merge commits the take-in names; then the -// branch's source key is written and source-moved said. A hand drop took nothing in, so it opens nothing. +// is left as it is), its members are the entries' pull requests, its heads the merge commits the take-in names. A hand drop +// took nothing in, so it opens nothing. func (h *Holder) TakenIn(body json.RawMessage) error { var t meshcatalog.TakeInTaken if err := json.Unmarshal(body, &t); err != nil { @@ -76,74 +73,5 @@ func (h *Holder) TakenIn(body json.RawMessage) error { return err } } - return h.moveSource(t) -} - -// moveSource writes the taken source revision of a branch: the merge commit of each repository it took in, the group that -// moved it, and says source-moved with the key's revision. -func (h *Holder) moveSource(t meshcatalog.TakeInTaken) error { - if h.Source == nil { - return nil - } - heads := map[string]string{} - 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 - } - for r, c := range heads { - 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 || !wrote { - return nil - } - at := h.Now().UTC() - return h.SaySeat(seat.SubjectSourceMoved(keys.EscapeName(t.Branch)), - seat.SourceMovedEvent{Branch: t.Branch, Group: t.Group, Revision: int64(rev), At: &at}, "source-moved."+keys.EscapeName(t.Branch)+"."+strconv.FormatUint(rev, 10)) + return nil } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go index a34bf2ef8..35615b4e9 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go @@ -7,16 +7,14 @@ import ( "strings" "testing" "time" - - mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" ) -// The gate whole, over a real bus (design 47 G A-33, A-34): a take-in.taken the indexer says opens a delivery record in the -// gate's ledger with the take-in as its outcome, writes the branch's source key and says source-moved; the gate asks the +// The gate whole, over a real bus (design 47 G A-33): a take-in.taken the indexer says opens a delivery record in the +// gate's ledger with the take-in as its outcome (it moves no source key, design 47 H §737); the gate asks the // controller nothing, and no state it no longer declares is needed (the template states are buckets under their local names). const eventWithin = 20 * time.Second -func TestAGateWholeTakesAnIndexersTakeInIntoARecordAndASourceKeyOverARealBus(t *testing.T) { +func TestAGateWholeTakesAnIndexersTakeInIntoARecordOverARealBus(t *testing.T) { b := newTestBus(t) ctx := context.Background() rt := b.start(t) @@ -29,14 +27,6 @@ func TestAGateWholeTakesAnIndexersTakeInIntoARecordAndASourceKeyOverARealBus(t * "what": "revision", "branch": "main", "source": 3, "since": "2026-11-11T19:00:00Z", "merges": []any{map[string]any{"repository": "novox/app", "number": 5, "merge": merge}}, "entries": []any{map[string]any{"module": "app", "commit": merge, "fingerprint": "f", "repository": "novox/app", "pull": 5}}}) - rt.until("the source key of main", eventWithin, func() bool { - e, err := b.kvs["source"].Get(ctx, mdc.SourceKey("main")) - if err != nil { - return false - } - s, err := mdc.DecodeSource(e.Value()) - return err == nil && s.Heads["novox/app"] == merge - }) rt.until("the record of the take-in", eventWithin, func() bool { b.mu.Lock() defer b.mu.Unlock() 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 a4c50ec07..abf988058 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 @@ -41,10 +41,11 @@ func (m *memSource) Read(branch string) (*Source, uint64, error) { } func (m *memSource) Branches() ([]string, error) { return nil, nil } -// Design 47 G A-33, A-34: a take-in.taken opens one record keyed `.`, whose members are the entries' pull -// requests and whose heads are the merge commits it names, moves that branch's source key and says source-moved; heard twice -// it opens no second record; a hand drop opens nothing and moves nothing. -func TestATakeInTakenOpensOneRecordAndMovesItsBranchsSource(t *testing.T) { +// Design 47 G A-33: a take-in.taken opens one record keyed `.`, whose members are the entries' pull +// requests and whose heads are the merge commits it names; heard twice it opens no second record; a hand drop opens nothing. +// GATE-3/AC3 (design 47 H §737, §764): neither it nor the forge bridge's merged message moves a source key or says +// source-moved: only a merge call does. +func TestATakeInTakenOpensOneRecordAndMovesNoSource(t *testing.T) { now := time.Date(2026, 11, 11, 20, 0, 0, 0, time.UTC) type said struct{ subject, id string } var events []said @@ -81,18 +82,20 @@ func TestATakeInTakenOpensOneRecordAndMovesItsBranchsSource(t *testing.T) { if got := r.Outcomes[OutcomeKey("", "", "", "take-in")]; got.Outcome != OutcomeTakenIn { t.Fatalf("the take-in is not the record's outcome: %+v", r.Outcomes) } - if s := src.vals["main"]; s.Group != "feat" || s.Heads["novox/app"] != merge { - t.Fatalf("source.main %+v", s) + if len(src.vals) != 0 || len(events) != 0 { + t.Fatalf("a take-in.taken moved source %+v and said %+v", src.vals, events) } - if len(events) != 1 || !strings.HasSuffix(events[0].subject, ".event.source-moved.main") { - t.Fatalf("said %+v", events) + merged := stdio.Envelope{Key: "mesh-delivery-forge-bridge.forge.novox.app.merged", + Body: json.RawMessage(`{"v":1,"kind":"merged","repository":"novox/app","base":"main","head":"` + merge + `"}`)} + if err := take(h, merged); err != nil || len(src.vals) != 0 || len(events) != 0 { + t.Fatalf("a merged message moved source %+v and said %+v (%v)", src.vals, events, err) } drop := json.RawMessage(`{"v":1,"kind":"taken","key":"m.00000000000000000002","what":"revision","dropped":true,"branch":"main","source":8,` + `"merges":[{"repository":"novox/lib","number":4,"merge":"` + merge + `"}],"since":"2026-11-11T19:00:00Z"}`) 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) != 1 { + if kept, _ := h.Records.Kept(); len(kept) != 1 || len(events) != 0 { 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 1ac0100fd..d7d108ff7 100644 --- a/modules/mesh-delivery-gate/module.json +++ b/modules/mesh-delivery-gate/module.json @@ -20,7 +20,6 @@ "mesh-delivery-forge-bridge.owed.ended", "mesh-delivery-indexer.take-in.taken", "mesh-catalog.registration-changed", - "mesh-delivery-forge-bridge.forge.*.*.merged", "mesh-conditions.findings-asked" ], "emits": [ @@ -336,7 +335,6 @@ "gate_records", "gate_source", "gate_source_again", - "gate_source_follow", "gate_source_start" ], "config": {