From e44a2d140d738ce48c103b0fb69575f09bdcfe12 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:12:57 +0100 Subject: [PATCH] GATE-4, GATE-5: a merge call names one source revision per branch and opens its record MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The gate consumes the seat git's merge-called and, per watched branch its members name, writes source. once by compare-and-set keyed by the call, numbered one above the branch's newest; opens that revision's record keyed . with the call, by and account in its trace; says record-opened on record-opened. and source-moved on source-moved... The take-in.taken writes its movers into the record read by its key, and a take-in with no mover ends it as nothing to deliver. The verb groups, the record's pins and pins-stated, forge-made and the emitted transition go. One commit for both subtasks: the record's opening is the handler's second half and neither compiles alone against the contracts. Builds: design 47 H §737, §738 (a take-in with no movers ends its record), §749 (the pins are the indexer's), §764 (the gate: the source moved on take-in.taken, the verb groups); H-A-13, H-A-19, H-A-24, H-A-25, H-A-28, H-A-29, H-A-33, H-A-47 (the start writes through the seed's path, no record), H-A-48 (pins, pins-stated, transition, members @), H-A-76 (Q-G1: the say-again carries the value's heads, start false) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/mesh-delivery-gate/consumer.go | 33 +-- .../cmd/mesh-delivery-gate/main.go | 29 +-- .../cmd/mesh-delivery-gate/manifest_test.go | 13 +- .../cmd/mesh-delivery-gate/mergecalled.go | 193 ++++++++++++++ .../mesh-delivery-gate/mergecalled_test.go | 127 ++++++++++ .../cmd/mesh-delivery-gate/merges.go | 6 +- .../cmd/mesh-delivery-gate/owedacts_test.go | 2 +- .../cmd/mesh-delivery-gate/record.go | 167 +++++++------ .../cmd/mesh-delivery-gate/record_test.go | 235 ++++++++---------- .../cmd/mesh-delivery-gate/recordevents.go | 155 +----------- .../cmd/mesh-delivery-gate/recordtools.go | 7 +- .../cmd/mesh-delivery-gate/sourcestart.go | 44 ++-- .../cmd/mesh-delivery-gate/sourcetools.go | 30 ++- .../mesh-delivery-gate/sourcetools_test.go | 49 +--- .../cmd/mesh-delivery-gate/takenin.go | 80 +++--- .../mesh-delivery-gate/takenin_bus_test.go | 46 ++-- .../cmd/mesh-delivery-gate/takenin_test.go | 45 +--- .../cmd/mesh-delivery-gate/verbs.go | 67 ++--- modules/mesh-delivery-gate/module.json | 41 +-- 19 files changed, 685 insertions(+), 684 deletions(-) create mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled.go create mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled_test.go diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go index 2559e6c2c..62bd2b339 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go @@ -10,6 +10,7 @@ package main // (ADR 0363 §2), and an event a node publishes names that node in its subject and in its x-node header. import ( + "bytes" "encoding/json" "fmt" ownmanifest "mesh-delivery-gate" @@ -17,6 +18,7 @@ import ( "sync" "time" + meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-indexer/takein" stdio "git.novox.be/novox/mesh-sdk/go" ) @@ -90,28 +92,19 @@ func stepPatterns() []string { return out } -// decodeTakenIn reads a `take-in.taken`: each merge taken in is an outcome of the record that merged it, unless the wait -// was dropped by hand, which took nothing in. -func decodeTakenIn(body json.RawMessage, at time.Time) ([]StepOutcome, error) { - t, err := takein.DecodeTakeIn(body) - if err != nil { +// decodeTakenIn reads a take-in.taken in the seat mesh-catalog's form, refusing a field it does not know, and answers no +// outcome: the take-in is written into its record by its key (takenin.go, H-A-29). +func decodeTakenIn(body json.RawMessage, _ time.Time) ([]StepOutcome, error) { + var t meshcatalog.TakeInTaken + dec := json.NewDecoder(bytes.NewReader(body)) + dec.DisallowUnknownFields() + if err := dec.Decode(&t); err != nil { return nil, err } - if t.Kind != takein.TakeInTaken { - return nil, fmt.Errorf("a take-in.taken of kind %q", t.Kind) + if t.Branch == "" || t.Source < 1 { + return nil, fmt.Errorf("a take-in.taken names no branch or source revision") } - outcome, why := OutcomeTakenIn, t.Why - if t.Dropped { - outcome, why = OutcomeHeld, "its take-in was dropped: "+t.Why - } - var commits []string - for _, m := range t.Merges { - commits = append(commits, m.Merge) - } - if len(commits) == 0 { - return nil, nil - } - return []StepOutcome{{Commits: commits, Step: "take-in", Outcome: outcome, Why: why, At: at}}, nil + return nil, nil } // StepConsumer applies the step events to the records. @@ -120,7 +113,7 @@ type StepConsumer struct { Families []stepFamily Now func() time.Time Logf func(format string, a ...any) - // Taken opens the record of a take-in.taken and moves its branch's source (takenin.go), before its outcomes are applied. + // Taken writes a take-in.taken into the record of its source revision (takenin.go). Taken func(body json.RawMessage) error mu sync.Mutex 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 3d8193cdc..2a6b3c3c7 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) +var followed = append(append([]string{owedEndedEvent, mergeCalledEvent}, stepPatterns()...), registrationChangedEvent) func main() { settingsFile := os.Getenv("MESH_DELIVERY_GATE_SETTINGS") @@ -97,7 +97,6 @@ func main() { // tick, no forge poll, no reconcile loop. Each step is retried, each failure said, never given up on quietly. func run(h *Holder, l *listening) { <-stdio.Served() // Serve first: the state is reached through it, and an event is heard from as soon as it serves - statePins(h, l) go h.Work() for _, pattern := range followed { pattern := pattern @@ -117,20 +116,6 @@ func run(h *Holder, l *listening) { go startSourcesAtStart(h) } -// statePins says every pin the records hold once at each start (design 47 G §579), retrying until it can. -func statePins(h *Holder, l *listening) { - for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) { - n, err := h.Records.StatePins() - if err == nil { - logf("[mesh-delivery] pins stated: %d", n) - return - } - l.set("not yet: " + err.Error()) - logf("[mesh-delivery] the pins cannot be stated yet (%v); asking again in %s", err, wait) - time.Sleep(wait) - } -} - // handle takes one event, then asks for a look at every step the event makes possible (Work, React). An event that cannot be read is // said and acknowledged: read again it would fail again. func handle(h *Holder, e stdio.Envelope) error { @@ -143,6 +128,9 @@ func take(h *Holder, e stdio.Envelope) error { if h.Consumer != nil && h.Consumer.Takes(e.Key) { return h.Consumer.Hear(e) } + if stdio.TopicMatches(mergeCalledEvent, e.Key) { + return h.MergeCalled(e) + } if e.Key == owedEndedEvent { return h.OwedEnded(e.Body) } @@ -220,15 +208,6 @@ func tools(h *Holder, l *listening) []stdio.Tool { Input: map[string]any{"type": "object", "properties": map[string]any{"id": str("a delivery's id, or a group's")}, "required": []string{"id"}}, Run: func(a map[string]any) (any, error) { return h.Show(strArg(a, "id")) }}, - {Name: seat + "groups", - Description: "Every delivery group, from the delivery records: its members, its state (open or ended), why, and the acts the " + - "gate said it owes the forge for it, each with its id and its recorded outcome (design 47 *What the gate owes the " + - "forge*). With a group, that group alone, ended or not, and no groups for a group it does not know. What reached the " + - "machines is the delivery view's.", - Input: map[string]any{"type": "object", "properties": map[string]any{ - "all": map[string]any{"type": "string", "enum": []string{"true", "false"}, "description": "\"true\": the groups that ended too"}, - "group": str("one group's id: that group alone, ended or not")}}, - Run: func(a map[string]any) (any, error) { return h.Groups(boolArg(a, "all"), strArg(a, "group")), nil }}, {Name: seat + "source", Description: "The source revision the gate holds: every repository it names, its trunk branch and that trunk's head, the " + "group that moved it last and its revision. Read, never written: a node-engine reads the repositories here to call a " + 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 b9df64585..11296cdeb 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 @@ -95,8 +95,9 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { t.Fatal("the manifest declares a seat; mesh-delivery is the mesh's own") } // The verbs the seat keeps: times, what-if, release, stop and retire-history went with the plans (ADR 0539 §17), and - // settle and checks with the merge states (design 47 G §431); recovery is G's (§81, A-23). - if want := []string{"deliveries", "show", "groups", "source", "recovery", "stalled"}; !reflect.DeepEqual(m.Claims[0].Serves, want) { + // settle and checks with the merge states (design 47 G §431); recovery is G's (§81, A-23); groups with the group model + // (H §764, H-A-28). + if want := []string{"deliveries", "show", "source", "recovery", "stalled"}; !reflect.DeepEqual(m.Claims[0].Serves, want) { t.Fatalf("serves %v, wanted %v", m.Claims[0].Serves, want) } // The forge messages' layout is the bridge's published package forgemsg, taken by its series (ADR 0634 §3, design 32). @@ -152,7 +153,7 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { if !strings.Contains(raw, `"finding-written.*"`) || !strings.Contains(raw, `"finding-cleared.*"`) || !strings.Contains(raw, `"findings-stated.*"`) { t.Fatal("the finding events are not declared") } - if !reflect.DeepEqual(m.Emits, []string{"transition"}) { // `group` and `merged` go with the merge states (§431) + if len(m.Emits) != 0 { // `group` and `merged` go with the merge states (§431), `transition` with H (H-A-48) t.Fatalf("emits %v", m.Emits) } // Two buckets still in the old `state` (merges, source), `pulls` and `asks` gone (§613), the four of the template `state` moved to `requires` (design 47 G §613), and no `asked` (the teller asks, ADR 0437 §6): ADR 0450 §3 and the conditions it raised (issue 1275). No @@ -290,7 +291,7 @@ func TestTheGateHoldsTheSeatAsTheSDKsTableOfRelease6HasIt(t *testing.T) { served[verb] = true } } - for _, removed := range []string{"settle", "checks", "table", "recheck", "close"} { // design 47 G §431 + for _, removed := range []string{"settle", "checks", "table", "recheck", "close", "groups"} { // design 47 G §431, H §764 if served[removed] { t.Errorf("the gate serves %s, which G removed with the merge states", removed) } @@ -298,7 +299,7 @@ func TestTheGateHoldsTheSeatAsTheSDKsTableOfRelease6HasIt(t *testing.T) { t.Errorf("the manifest serves %s, which G removed with the merge states", removed) } } - for _, verb := range []string{"deliveries", "show", "groups", "stalled", "recovery"} { + for _, verb := range []string{"deliveries", "show", "stalled", "recovery"} { if !served[verb] { t.Errorf("the gate does not serve %s, which the seat keeps", verb) } @@ -363,7 +364,7 @@ func TestTheGroupsFromBranchNamesAreGone(t *testing.T) { } h := &Holder{Now: func() time.Time { return time.Date(2026, 11, 20, 10, 0, 0, 0, time.UTC) }, Logf: func(string, ...any) {}} h.Records = &Records{Ledger: newLedgerFake(t), Now: h.Now} - if _, _, err := h.Records.Open("main.7", map[string]string{"novox/app#3": strings.Repeat("a", 40)}, false); err != nil { + if _, _, err := h.Records.Open(opening("main", 7, map[string]string{"novox/app#3": strings.Repeat("a", 40)})); err != nil { t.Fatal(err) } d, err := h.Deliveries("", "", "", true) diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled.go new file mode 100644 index 000000000..dcb2695b2 --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled.go @@ -0,0 +1,193 @@ +package main + +// The merge call names the merged heads one source revision per watched branch (design 47 H §737; ADR 0682 §1, §2; +// H-A-13, H-A-19, H-A-33): the gate consumes the seat git's merge-called on a durable consumer of its own, writes +// source. once per call by compare-and-set keyed by the call, opens that revision's delivery record and says +// record-opened and source-moved. The gate numbers every source revision; no other part keeps a number of its own. + +import ( + "crypto/rand" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "regexp" + "strconv" + "strings" + + gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" + stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/keys" +) + +// mergeCalledEvent is the seat git's merge-called as the gate's manifest consumes it (`git.merge-called.*`). +var mergeCalledEvent = consumedFamily(gitseat.EventMergeCalled) + +// consumedFamily is the pattern the manifest consumes an event by whose subject has parts after its name +// (`..*`), or the event itself; empty when the manifest consumes none. +func consumedFamily(event string) string { + for _, c := range consumed { + if _, name, ok := strings.Cut(c, "."); ok && (name == event || strings.HasPrefix(name, event+".")) { + return c + } + } + return "" +} + +// callRe is a merge call: 16 lowercase hex characters, one subject token (H-A-24). +var callRe = regexp.MustCompile(`^[0-9a-f]{16}$`) + +// revisionHead is one repository a source revision moves, at its commit. +type revisionHead struct { + Repository string + Commit string +} + +// nameRevision writes source. once for a call (§737, H-A-33): the branch's previous heads with each head's +// repository at its commit, numbered one above the branch's newest, carrying the call. A newest revision already carrying +// the call writes nothing and is answered as it is, so a call heard again numbers nothing twice (H-A-13). Wrote says it +// was written now. +func (h *Holder) nameRevision(branch, call string, heads []revisionHead) (named Source, wrote bool, err error) { + if h.Source == nil { + return Source{}, false, errors.New("this gate keeps no source") + } + _, err = h.Source.Set(branch, func(prev *Source) (Source, bool) { + if prev != nil && prev.Call == call { + named, wrote = *prev, false + return Source{}, false + } + next := Source{Call: call, Source: 1, Heads: map[string]string{}} + if prev != nil { + next.Source = prev.Source + 1 + for r, c := range prev.Heads { + next.Heads[r] = c + } + } + for _, hd := range heads { + next.Heads[hd.Repository] = hd.Commit + } + named, wrote = next, true + return next, true + }) + if err != nil { + return Source{}, false, fmt.Errorf("source.%s cannot be written for the call %s: %w", branch, call, err) + } + return named, wrote, nil +} + +// saySourceMoved says source-moved on source-moved.. (§737) under the revision's id, so said again for a +// call heard again it is kept once. +func (h *Holder) saySourceMoved(branch string, s Source, start bool, heads []revisionHead) error { + if h.SaySeat == nil { + return nil + } + escaped, number := keys.EscapeName(branch), strconv.FormatInt(s.Source, 10) + e := seat.SourceMovedEvent{V: 1, Branch: branch, Source: s.Source, Call: s.Call, Start: start, + Heads: make([]seat.SourceMovedEventHeadsItem, 0, len(heads))} + for _, hd := range heads { + e.Heads = append(e.Heads, seat.SourceMovedEventHeadsItem{Repository: hd.Repository, Commit: hd.Commit}) + } + return h.SaySeat(seat.SubjectSourceMoved(escaped, number), e, "source-moved."+escaped+"."+number) +} + +// branchOfCall is one watched branch a merge call's members name: its heads in call order, each repository once at its +// last member's commit (H-A-19), and its members by id `@` (H-A-48). +type branchOfCall struct { + Branch string + Heads []revisionHead + Members map[string]string + Pulls []string + Commits []string +} + +// branchesOf groups a merge call's landed members by their base, in the order the call first names each branch. +func branchesOf(m gitseat.MergeCalled) ([]*branchOfCall, error) { + var out []*branchOfCall + by := map[string]*branchOfCall{} + for _, mb := range m.Members { + if mb.Branch == "" || mb.Repository == "" || mb.Commit == "" || mb.Number < 1 { + return nil, fmt.Errorf("a member names no branch, repository, number or commit (%+v)", mb) + } + b := by[mb.Branch] + if b == nil { + b = &branchOfCall{Branch: mb.Branch, Members: map[string]string{}} + by[mb.Branch] = b + out = append(out, b) + } + id := strings.ToLower(mb.Repository) + "@" + strconv.FormatInt(mb.Number, 10) + b.Members[id] = mb.Commit + b.Pulls = append(b.Pulls, id) + b.Commits = append(b.Commits, mb.Commit) + replaced := false + for i := range b.Heads { + if b.Heads[i].Repository == mb.Repository { + b.Heads[i].Commit, replaced = mb.Commit, true + } + } + if !replaced { + b.Heads = append(b.Heads, revisionHead{Repository: mb.Repository, Commit: mb.Commit}) + } + } + return out, nil +} + +// MergeCalled takes one merge-called: for each watched branch its members name, one source revision, one delivery record and +// one source-moved (§737). It answers an error only when a write failed, so the event is offered again; a body that cannot be +// read is said and taken, since offered again it would be refused again. A call that landed no member names nothing. +func (h *Holder) MergeCalled(e stdio.Envelope) error { + var m gitseat.MergeCalled + if err := json.Unmarshal(e.Body, &m); err != nil { + h.Refuse(fmt.Errorf("a merge-called (%s) is not readable, so it names no source revision: %w", e.Key, err)) + return nil + } + if !callRe.MatchString(m.Call) { + h.Refuse(fmt.Errorf("a merge-called (%s) names the call %q, not 16 lowercase hex characters, so it names no source revision", e.Key, m.Call)) + return nil + } + branches, err := branchesOf(m) + if err != nil { + h.Refuse(fmt.Errorf("the merge call %s names no source revision: %w", m.Call, err)) + return nil + } + var errs []error + for _, b := range branches { + trace := RecordTrace{Call: m.Call, By: m.By, Account: m.Account, At: m.At.UTC(), Pulls: b.Pulls, Commits: b.Commits} + if err := h.deliverRevision(b.Branch, m.Call, b.Heads, b.Members, trace); err != nil { + errs = append(errs, err) + } + } + return errors.Join(errs...) +} + +// deliverRevision names a branch's heads one source revision for a call, opens its record and says source-moved: the one +// path a merge call and a hand take (§742) move a source by. +func (h *Holder) deliverRevision(branch, call string, heads []revisionHead, members map[string]string, trace RecordTrace) error { + named, wrote, err := h.nameRevision(branch, call, heads) + if err != nil { + return err + } + if h.Records != nil { + trace.Source = strconv.FormatInt(named.Source, 10) + if _, _, err := h.Records.Open(RecordOpening{Branch: branch, Source: named.Source, Call: call, Heads: members, Trace: trace}); err != nil { + return fmt.Errorf("the record of %s at %d cannot be opened: %w", branch, named.Source, err) + } + } + if err := h.saySourceMoved(branch, named, false, heads); err != nil { + return fmt.Errorf("source.%s is at %d and its source-moved is not said: %w", branch, named.Source, err) + } + if wrote { + h.logf("[mesh-delivery] source.%s is at %d for the call %s", branch, named.Source, call) + } + return nil +} + +// newCall is a call made by the gate itself, for a start or a seed (H-A-24: 16 lowercase hex characters; who and when +// live in the hand-act log and the trace). +func newCall() (string, error) { + var b [8]byte + if _, err := rand.Read(b[:]); err != nil { + return "", fmt.Errorf("no call can be made: %w", err) + } + return hex.EncodeToString(b[:]), nil +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled_test.go new file mode 100644 index 000000000..cfec0a652 --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled_test.go @@ -0,0 +1,127 @@ +package main + +import ( + "encoding/json" + "reflect" + "strconv" + "strings" + "testing" + "time" + + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// GATE-4 and GATE-5 (design 47 H §737; H-A-13, H-A-19, H-A-25, H-A-33): a merge-called names its landed heads one source +// revision per watched branch, written once per call, numbered one above the branch's newest, opens that revision's record +// keyed `.` and says record-opened on record-opened. and source-moved on source-moved... +func TestAMergeCallNamesOneSourceRevisionAndOneRecordPerBranch(t *testing.T) { + at := time.Date(2026, 11, 20, 3, 0, 0, 0, time.UTC) + c1, c2, c3 := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("3", 40) + type said struct { + subject, id string + body any + } + setup := func(t *testing.T) (*Holder, *memSource, *[]said) { + src := &memSource{vals: map[string]Source{"main": {Source: 9, Call: "00000000000000aa", Heads: map[string]string{"novox/old": "c0"}}}} + var events []said + h := &Holder{Now: func() time.Time { return at }, Source: src} + h.init() + h.SaySeat = func(subject string, body any, id string) error { + events = append(events, said{subject, id, body}) + return nil + } + h.Records = &Records{Ledger: newLedgerFake(t), Now: h.Now, Say: h.SaySeat} + return h, src, &events + } + called := func(call, members string) stdio.Envelope { + return stdio.Envelope{Key: "git.merge-called." + call, Body: json.RawMessage(`{"call":"` + call + + `","by":"lead@shanks","account":"acct-s","at":"2026-11-20T02:59:00Z","members":[` + members + `]}`)} + } + member := func(repository string, number int, branch, commit string) string { + return `{"repository":"` + repository + `","number":` + strconv.Itoa(number) + + `,"branch":"` + branch + `","head":"h","commit":"` + commit + `"}` + } + + t.Run("two members on one branch: one revision, one record, one source-moved; heard twice, one", func(t *testing.T) { + h, src, events := setup(t) + e := called("0123456789abcdef", member("novox/app", 3, "main", c1)+","+member("novox/lib", 4, "main", c2)) + for range 2 { + if err := h.MergeCalled(e); err != nil { + t.Fatal(err) + } + } + got := src.vals["main"] + if got.Source != 10 || got.Call != "0123456789abcdef" || + !reflect.DeepEqual(got.Heads, map[string]string{"novox/old": "c0", "novox/app": c1, "novox/lib": c2}) || src.rev != 1 { + t.Fatalf("source.main %+v (%d writes)", got, src.rev) + } + kept, _ := h.Records.Kept() + if len(kept) != 1 || kept[0].Milestone != recordMilestone("main", 10) || kept[0].Call != "0123456789abcdef" || + !reflect.DeepEqual(kept[0].Heads, map[string]string{"novox/app@3": c1, "novox/lib@4": c2}) || + kept[0].Trace == nil || kept[0].Trace.By != "lead@shanks" || kept[0].Trace.Account != "acct-s" { + t.Fatalf("records %+v", kept) + } + var moved []seat.SourceMovedEvent + ids := map[string]bool{} + for _, s := range *events { + ids[s.id] = true + if m, ok := s.body.(seat.SourceMovedEvent); ok { + if s.subject != seat.SubjectSourceMoved("main", "10") { + t.Fatalf("source-moved on %s", s.subject) + } + moved = append(moved, m) + } else if s.subject != seat.SubjectRecordOpened("0123456789abcdef") { + t.Fatalf("said %s", s.subject) + } + } + want := seat.SourceMovedEvent{V: 1, Branch: "main", Source: 10, Call: "0123456789abcdef", + Heads: []seat.SourceMovedEventHeadsItem{{Repository: "novox/app", Commit: c1}, {Repository: "novox/lib", Commit: c2}}} + if len(moved) != 2 || !reflect.DeepEqual(moved[0], want) || len(ids) != 2 { + t.Fatalf("source-moved %+v under %v: heard twice it is said under the one id", moved, ids) + } + }) + + t.Run("two members of one repository name the last member's commit in call order", func(t *testing.T) { + h, src, _ := setup(t) + if err := h.MergeCalled(called("1111111111111111", member("novox/app", 3, "main", c1)+","+member("novox/app", 5, "main", c3))); err != nil { + t.Fatal(err) + } + if got := src.vals["main"].Heads["novox/app"]; got != c3 { + t.Fatalf("novox/app at %s", got) + } + }) + + t.Run("members on two branches are two revisions and two records, each naming the call", func(t *testing.T) { + h, src, events := setup(t) + if err := h.MergeCalled(called("2222222222222222", member("novox/app", 3, "main", c1)+","+member("novox/app", 6, "feat/x", c2))); err != nil { + t.Fatal(err) + } + if src.vals["main"].Source != 10 || src.vals["feat/x"].Source != 1 || src.vals["feat/x"].Call != "2222222222222222" { + t.Fatalf("sources %+v", src.vals) + } + kept, _ := h.Records.Kept() + if len(kept) != 2 || kept[0].Call != kept[1].Call || len(*events) != 4 { + t.Fatalf("records %+v, %d events", kept, len(*events)) + } + }) + + t.Run("a call that cannot be read, or not 16 lowercase hex, names nothing and is taken", func(t *testing.T) { + h, src, events := setup(t) + for _, e := range []stdio.Envelope{called("NOTHEX", member("novox/app", 3, "main", c1)), + {Key: "git.merge-called.x", Body: json.RawMessage(`{`)}, called("3333333333333333", member("novox/app", 3, "", c1))} { + if err := h.MergeCalled(e); err != nil { + t.Fatal(err) + } + } + if src.rev != 0 || len(*events) != 0 { + t.Fatalf("wrote %d, said %d", src.rev, len(*events)) + } + }) + + t.Run("the manifest consumes merge-called and the gate follows it", func(t *testing.T) { + if mergeCalledEvent != "git.merge-called.*" || !stdio.TopicMatches(mergeCalledEvent, "git.merge-called.0123456789abcdef") { + t.Fatalf("merge-called followed as %q", mergeCalledEvent) + } + }) +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go index a90ab75e8..265960ee5 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go @@ -113,7 +113,7 @@ func (kvSource) Branches() ([]string, error) { } // SourceRevision is the seat's verb source (§690): given a branch, every repository that branch's source key names, its -// trunk branch and head, the group that moved it last and its revision, or why it cannot be read; given none, `branches`, +// trunk branch and head, the merge call that moved it last (H-A-25) and its source revision, or why it cannot be read; given none, `branches`, // the branch of each source key the gate keeps. The trunk of each repository is the branch asked, the key's own (§428, §507). Outside the lock. func (h *Holder) SourceRevision(branch string) seat.SourceAnswer { if h.Source == nil { @@ -129,14 +129,14 @@ func (h *Holder) SourceRevision(branch string) seat.SourceAnswer { } return seat.SourceAnswer{Branches: branches} } - held, revision, err := h.Source.Read(branch) + held, _, err := h.Source.Read(branch) if err != nil { return seat.SourceAnswer{Error: err.Error()} } if held == nil { return seat.SourceAnswer{Branch: branch} } - out := seat.SourceAnswer{Branch: branch, Group: held.Group, Revision: int64(revision)} + out := seat.SourceAnswer{Branch: branch, Call: held.Call, Revision: held.Source} for repository, head := range held.Heads { out.Repositories = append(out.Repositories, seat.SourceHead{Repository: repository, Trunk: branch, Head: head}) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/owedacts_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/owedacts_test.go index 7e8a04cb5..29bea18c0 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/owedacts_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/owedacts_test.go @@ -22,7 +22,7 @@ func TestARecordsChangesOweTheNoteTheViewAndWhenFailedOrHeldAComment(t *testing. f := &sayerFake{} h := &Holder{Now: func() time.Time { return at }, Owed: f, Records: rs} rs.Changed = h.recordChanged - r, _, err := rs.Open("47/G", map[string]string{"novox/app@7": head}, false) + r, _, err := rs.Open(opening("main", 47, map[string]string{"novox/app@7": head})) if err != nil { t.Fatal(err) } 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 8df97b3c4..b5d481263 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go @@ -44,15 +44,13 @@ const ( // The operations of a record, each one effect (ADR 0597 §2), and of the month's list. The manifest's `requires` declares // exactly these (record_test.go). const ( - OpOpen = "open" // set-fields: the milestone, the heads, when, forge-made, the form + OpOpen = "open" // set-fields: the milestone, its branch, source revision and merge call, the heads, when, the form OpGroup = "group" // set-fields: the group's state OpTrace = "trace" // set-fields: the trace OpEntry = "entry" // set-entry into entries: one entry of the graph difference, by its module, fingerprint and node OpOutcome = "outcome" // set-entry into outcomes, by the entry, the node and the step OpAway = "away" // set-entry into away, by node OpBack = "back" // remove from away, by node - OpPin = "pin" // set-entry into pinned, by revision - OpFree = "free" // remove from pinned, by revision OpEnd = "end" // set-fields: ended OpAct = "act" // set-entry into acts, by id: an act owed the forge with its outcome OpHold = "hold" // a month's list: set-entry into records, by instance @@ -60,7 +58,7 @@ const ( ) // RecordOperations are a record's operations, in the order the manifest's type allows them. -var RecordOperations = []string{OpOpen, OpGroup, OpTrace, OpEntry, OpOutcome, OpAway, OpBack, OpPin, OpFree, OpAct, OpEnd} +var RecordOperations = []string{OpOpen, OpGroup, OpTrace, OpEntry, OpOutcome, OpAway, OpBack, OpAct, OpEnd} // HeldOperations are the month's list's operations. var HeldOperations = []string{OpHold, OpDrop} @@ -76,14 +74,17 @@ type Record struct { // Sequence is the last operation the state holds; not in the state. Sequence uint64 `json:"-"` + // Milestone is the record's key, `.`, each escaped as a name (design 47 H §737, A-33). Milestone string `json:"milestone"` - // Heads are the heads the record is for: each member's id (owner/repository@commit) to its full commit. + // Branch, Source and Call are the watched branch, its source revision and the merge call that named it (§737). + Branch string `json:"branch,omitempty"` + Source int64 `json:"source,omitempty"` + Call string `json:"call,omitempty"` + // Heads are the members the record is for: each member's id (`@` for a merge call's member, + // `@` for a hand take, H-A-48) to its full commit. Heads map[string]string `json:"heads"` Opened time.Time `json:"opened"` - // ForgeMade is a record opened for a merge the forge made with no record's merge-pull (design 47 *What the gate owes - // the forge*): it has no Approve. - ForgeMade bool `json:"forge-made,omitempty"` - Form int `json:"form,omitempty"` + Form int `json:"form,omitempty"` Group *RecordGroup `json:"group,omitempty"` Trace *RecordTrace `json:"trace,omitempty"` @@ -93,8 +94,6 @@ type Record struct { Outcomes map[string]RecordOutcome `json:"outcomes,omitempty"` // Away are the nodes away, by node, each with since when. Away map[string]NodeAway `json:"away,omitempty"` - // Pinned are the graph revisions the record points at, by revision. - Pinned map[string]PinnedRevision `json:"pinned,omitempty"` // Acts are the acts owed the forge for the record's pull requests, by id, with their outcomes (owedended.go). Acts map[string]RecordAct `json:"acts,omitempty"` Ended *RecordEnded `json:"ended,omitempty"` @@ -142,6 +141,13 @@ type RecordTrace struct { Source string `json:"source,omitempty"` From string `json:"from,omitempty"` To string `json:"to,omitempty"` + // Call is the merge call, By the caller as who@node and Account the bus account the tool runner proved for it + // (design 47 H §737); Hand marks a head taken in by hand (§742), At when (H-A-24: hand, by and the time live here). + Call string `json:"call,omitempty"` + By string `json:"by,omitempty"` + Account string `json:"account,omitempty"` + Hand bool `json:"hand,omitempty"` + At time.Time `json:"at,omitzero"` } // The outcomes an entry has on a node (ADR 0656 §7, design 47 *The delivery record*). @@ -196,14 +202,6 @@ type NodeAway struct { Since time.Time `json:"since"` } -// PinnedRevision is a graph revision the record points at: its half (catalog or assignment), its revision and, for a catalog -// revision, the watched branch it is numbered on. -type PinnedRevision struct { - Revision string `json:"revision"` - Half string `json:"half"` - Branch string `json:"branch,omitempty"` -} - // RecordEnded is how a record ended: when, why, by whom, and the nodes away it ended without. type RecordEnded struct { At time.Time `json:"at"` @@ -244,34 +242,13 @@ func milestoneToken(milestone string) string { return b.String() + "_" + hex.EncodeToString(sum[:4]) } -// HeadsDigest is the digest of a set of heads: each member's id and full commit, sorted, sixteen hex characters. -func HeadsDigest(heads map[string]string) string { - ids := make([]string, 0, len(heads)) - for id := range heads { - ids = append(ids, id) - } - sort.Strings(ids) - h := sha256.New() - for _, id := range ids { - fmt.Fprintf(h, "%s=%s\n", strings.ToLower(id), strings.ToLower(heads[id])) - } - return hex.EncodeToString(h.Sum(nil))[:16] -} - -// RecordInstance is the instance of the record of a milestone at its heads: `record//`. -func RecordInstance(milestone string, heads map[string]string) (string, error) { +// RecordInstance is the instance of the record of a source revision, by its key `.` alone: +// `record/` (design 47 H §737; H-A-29: a record is read by its key, never by reading every record). +func RecordInstance(milestone string) (string, error) { if strings.TrimSpace(milestone) == "" { return "", errors.New("a delivery record names its milestone") } - if len(heads) == 0 { - return "", fmt.Errorf("the delivery record of %s names no heads", milestone) - } - for id, sha := range heads { - if id == "" || sha == "" { - return "", fmt.Errorf("the delivery record of %s names a head with no id or commit (%q=%q)", milestone, id, sha) - } - } - inst := TypeRecord + "/" + milestoneToken(milestone) + "/" + HeadsDigest(heads) + inst := TypeRecord + "/" + milestoneToken(milestone) for _, p := range strings.Split(inst, "/") { if !segmentRe.MatchString(p) { return "", fmt.Errorf("the delivery record of %s cannot be named: %q", milestone, inst) @@ -480,21 +457,43 @@ func (rs *Records) Read(instance string) (*Record, error) { return &r, nil } -// Open opens the record of a milestone at its heads, unless one is there already, which is left as it is (the switch -// and a delivery of milestone G again after a revert make no second record and ask nothing again). Opened says it was -// 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) +// RecordOpening is what a record opens with: the source revision it is the record of, the merge call that named it, its +// members and its trace (design 47 H §737). +type RecordOpening struct { + Branch string + Source int64 + Call string + // Heads are the members, each id to its full commit. + Heads map[string]string + Trace RecordTrace } -func (rs *Records) open(milestone string, heads map[string]string, forgeMade bool) (r *Record, opened bool, err error) { - inst, err := RecordInstance(milestone, heads) +// Open opens the record of a source revision, unless one is there already, which is left as it is (a merge call heard +// again opens no second record and says nothing new). Opened says it was 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(o RecordOpening) (r *Record, opened bool, err error) { + if o.Branch == "" || o.Source < 1 || o.Call == "" { + return nil, false, fmt.Errorf("a delivery record names its branch, its source revision and its merge call (%q, %d, %q)", o.Branch, o.Source, o.Call) + } + if len(o.Heads) == 0 { + return nil, false, fmt.Errorf("the delivery record of %s at %d names no member", o.Branch, o.Source) + } + for id, sha := range o.Heads { + if id == "" || sha == "" { + return nil, false, fmt.Errorf("the delivery record of %s at %d names a member with no id or commit (%q=%q)", o.Branch, o.Source, id, sha) + } + } + milestone := recordMilestone(o.Branch, o.Source) + inst, err := RecordInstance(milestone) if err != nil { return nil, false, err } - if r, err = rs.Read(inst); err != nil || r != nil { - return r, false, err + if r, err = rs.Read(inst); err != nil { + return nil, false, err + } + if r != nil { + rs.sayOpened(r) + return r, false, nil } now := rs.now() if err := rs.Sweep(now); err != nil { @@ -504,10 +503,8 @@ func (rs *Records) open(milestone string, heads map[string]string, forgeMade boo if err != nil { return nil, false, err } - body := map[string]any{"milestone": milestone, "heads": heads, "opened": now, "form": RecordForm} - if forgeMade { - body["forge-made"] = true - } + body := map[string]any{"milestone": milestone, "branch": o.Branch, "source": o.Source, "call": o.Call, "heads": o.Heads, + "opened": now, "form": RecordForm} if _, err := rs.Ledger.Apply(inst, OpOpen, body); err != nil { return nil, false, err } @@ -517,18 +514,34 @@ func (rs *Records) open(milestone string, heads map[string]string, forgeMade boo if read { rs.sayFilling(inst, before) } - r, err = rs.Read(inst) - if err == nil && r != nil { - rs.keep(r) - // 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}, - "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) - } + if r, err = rs.Read(inst); err != nil || r == nil { + return r, false, err + } + if err := rs.SetTrace(r, o.Trace); err != nil { + return r, true, fmt.Errorf("the record %s is opened and its trace is not written: %w", inst, err) + } + rs.keep(r) + rs.sayOpened(r) + return r, true, nil +} + +// ByKey is the record of a source revision, read by its key `.` (H-A-29); nil when none is kept. +func (rs *Records) ByKey(branch string, source int64) (*Record, error) { + inst, err := RecordInstance(recordMilestone(branch, source)) + if err != nil { + return nil, err + } + return rs.Read(inst) +} + +// sayOpened says record-opened on record-opened., so the session that called waits on its own call (§737). Said under +// the record's id, so a merge call heard again says it once: a say that fails is said again when the call is heard again. +func (rs *Records) sayOpened(r *Record) { + at := r.Opened.UTC() + if err := rs.say(meshdelivery.SubjectRecordOpened(r.Call), meshdelivery.RecordOpenedEvent{Record: r.Instance, + Milestone: r.Milestone, Branch: r.Branch, Source: r.Source, Call: r.Call, At: &at}, "record-opened."+r.Instance); err != nil && rs.Logf != nil { + rs.Logf("[mesh-delivery] the record %s is opened and its opening is not said: %v", r.Instance, err) } - return r, err == nil && r != nil, err } // List is every record kept, opened within the retention and a month more, oldest first. @@ -581,9 +594,7 @@ func (rs *Records) heldIn(month string) ([]heldEntry, error) { return out, nil } -// Sweep removes every record opened longer ago than the retention, and says `freed` for each revision it pinned -// (design 47: "When the retention removes a record, the gate says freed for each revision it pinned"). Nothing else is -// removed to make room. +// Sweep removes every record opened longer ago than the retention. Nothing else is removed to make room. func (rs *Records) Sweep(now time.Time) error { cut := now.Add(-rs.retention()) for _, m := range rs.months(now) { @@ -600,9 +611,6 @@ func (rs *Records) Sweep(now time.Time) error { return err } if r != nil { - if err := rs.sayFreedAll(r, "the record is past its retention"); err != nil { - return err - } if err := rs.Ledger.Remove(e.Instance); err != nil { return err } @@ -616,15 +624,6 @@ func (rs *Records) Sweep(now time.Time) error { return nil } -func sortedPins(r *Record) []PinnedRevision { - out := make([]PinnedRevision, 0, len(r.Pinned)) - for _, p := range r.Pinned { - out = append(out, p) - } - sort.Slice(out, func(i, j int) bool { return out[i].Revision < out[j].Revision }) - return out -} - // SetGroup writes the record's group state, unless it holds that state already. func (rs *Records) SetGroup(r *Record, g RecordGroup) error { if r.Group != nil && reflect.DeepEqual(*r.Group, g) { 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 2b51ba7de..204dd6c22 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 @@ -6,7 +6,6 @@ import ( "fmt" "os" "path/filepath" - "reflect" "slices" "strings" "testing" @@ -159,50 +158,70 @@ func (f *ledgerFake) Get(instance string) (json.RawMessage, uint64, error) { return s, f.seq[instance], nil } -// W11.1 (ADR 0656 §7, design 47 *The delivery record*): one record per milestone's delivery, a ledger instance keyed by its -// milestone and its heads, kept for its retention, its operations those the manifest's ledger types allow. -func TestTheDeliveryRecordIsALedgerInstanceKeyedByMilestoneAndHeads(t *testing.T) { - heads := map[string]string{"novox/app@111111111111": strings.Repeat("1", 40), "novox/lib@222222222222": strings.Repeat("2", 40)} - same := map[string]string{"novox/lib@222222222222": strings.Repeat("2", 40), "novox/app@111111111111": strings.Repeat("1", 40)} - other := map[string]string{"novox/app@111111111111": strings.Repeat("3", 40), "novox/lib@222222222222": strings.Repeat("2", 40)} +// W11.1 (ADR 0656 §7, design 47 *The delivery record*; H §737, H-A-29): one record per source revision, a ledger +// instance keyed by `.` alone, kept for its retention, its operations those the manifest's ledger types allow. +func TestTheDeliveryRecordIsALedgerInstanceKeyedByItsSourceRevision(t *testing.T) { + heads := map[string]string{"novox/app@3": strings.Repeat("1", 40), "novox/lib@4": strings.Repeat("2", 40)} + same := map[string]string{"novox/lib@4": strings.Repeat("2", 40), "novox/app@3": strings.Repeat("1", 40)} at := time.Date(2026, 11, 9, 20, 0, 0, 0, time.UTC) - t.Run("an instance per milestone and heads, a path the tool runner takes", func(t *testing.T) { - a, err := RecordInstance("47/G", heads) + t.Run("an instance per source revision, a path the tool runner takes", func(t *testing.T) { + a, err := RecordInstance(recordMilestone("feat/x", 7)) if err != nil { t.Fatal(err) } - b, _ := RecordInstance("47/G", same) - c, _ := RecordInstance("47/G", other) - d, _ := RecordInstance("47_G", heads) - if a != b || a == c || a == d { - t.Fatalf("instances %s %s %s %s: one per milestone and heads, whatever the heads' order", a, b, c, d) + b, _ := RecordInstance(recordMilestone("feat/x", 8)) + c, _ := RecordInstance(recordMilestone("feat_x", 7)) + if a == b || a == c { + t.Fatalf("instances %s %s %s: one per branch and source revision", a, b, c) } for _, p := range strings.Split(a, "/") { if !segmentRe.MatchString(p) { t.Fatalf("%s has a part the tool runner refuses", a) } } - if _, err := RecordInstance("", heads); err == nil { + if _, err := RecordInstance(""); err == nil { t.Fatal("a record with no milestone is named") } - if _, err := RecordInstance("47/G", nil); err == nil { - t.Fatal("a record with no heads is named") + rs := &Records{Ledger: newLedgerFake(t), Now: func() time.Time { return at }} + for _, o := range []RecordOpening{{Branch: "main", Source: 1, Call: "0000000000000001"}, {Branch: "main", Call: "0000000000000001", Heads: heads}, + {Branch: "main", Source: 1, Heads: heads}, {Source: 1, Call: "0000000000000001", Heads: heads}} { + if _, _, err := rs.Open(o); err == nil { + t.Fatalf("a record is opened with %+v", o) + } } }) - t.Run("opened once; a second open leaves it as it is", func(t *testing.T) { + t.Run("opened once, said on record-opened.; a second open leaves it as it is and says the same", func(t *testing.T) { l := newLedgerFake(t) now := at - rs := &Records{Ledger: l, Now: func() time.Time { return now }} - r, opened, err := rs.Open("47/G", heads, false) - if err != nil || !opened || r == nil || r.Milestone != "47/G" || len(r.Heads) != 2 || !r.Opened.Equal(at) || r.Form != RecordForm { + type said struct { + subject, id string + body meshdelivery.RecordOpenedEvent + } + var got []said + rs := &Records{Ledger: l, Now: func() time.Time { return now }, Say: func(subject string, body any, id string) error { + got = append(got, said{subject, id, body.(meshdelivery.RecordOpenedEvent)}) + return nil + }} + o := opening("main", 47, heads) + o.Trace = RecordTrace{Call: o.Call, By: "lead@shanks", Account: "acct-s"} + r, opened, err := rs.Open(o) + if err != nil || !opened || r == nil || r.Milestone != recordMilestone("main", 47) || r.Branch != "main" || r.Source != 47 || + r.Call != o.Call || len(r.Heads) != 2 || !r.Opened.Equal(at) || r.Form != RecordForm || r.Trace == nil || r.Trace.By != "lead@shanks" { t.Fatalf("open: %+v %v %v", r, opened, err) } now = at.Add(time.Hour) - again, opened, err := rs.Open("47/G", same, false) + again, opened, err := rs.Open(opening("main", 47, same)) if err != nil || opened || again.Instance != r.Instance || !again.Opened.Equal(at) { - t.Fatalf("a second open of the same heads: %+v %v %v", again, opened, err) + t.Fatalf("a second open of the same revision: %+v %v %v", again, opened, err) + } + if len(got) != 2 || got[0].id != got[1].id || *got[0].body.At != *got[1].body.At || got[0].subject != meshdelivery.SubjectRecordOpened(o.Call) || + got[0].body.Branch != "main" || got[0].body.Source != 47 || got[0].body.Call != o.Call || got[0].body.Record != r.Instance { + t.Fatalf("said %+v", got) + } + if byKey, err := rs.ByKey("main", 47); err != nil || byKey == nil || byKey.Instance != r.Instance { + t.Fatalf("read by its key: %+v %v", byKey, err) } list, err := rs.List() if err != nil || len(list) != 1 || list[0].Instance != r.Instance { @@ -213,7 +232,7 @@ func TestTheDeliveryRecordIsALedgerInstanceKeyedByMilestoneAndHeads(t *testing.T t.Run("an outcome applied twice changes nothing", func(t *testing.T) { l := newLedgerFake(t) rs := &Records{Ledger: l, Now: func() time.Time { return at }} - r, _, _ := rs.Open("47/G", heads, false) + r, _, _ := rs.Open(opening("main", 47, heads)) o := RecordOutcome{Module: "mesh-delivery-gate", Fingerprint: "src2:abcd", Node: "ace", Step: "build", Outcome: OutcomeBuilt, At: at, Event: "e1"} if applied, err := rs.Outcome(r, o); err != nil || !applied { t.Fatalf("first: %v %v", applied, err) @@ -245,112 +264,29 @@ func TestTheDeliveryRecordIsALedgerInstanceKeyedByMilestoneAndHeads(t *testing.T } }) - t.Run("a record past its retention is removed at the next open and frees what it pinned", func(t *testing.T) { + t.Run("a record past its retention is removed at the next open", func(t *testing.T) { l := newLedgerFake(t) now := at - var freed []string - rs := &Records{Ledger: l, Now: func() time.Time { return now }, Retention: func() time.Duration { return 90 * 24 * time.Hour }, - Say: func(subject string, body any, id string) error { - if subject == meshdelivery.SubjectFreed { - p := body.(meshdelivery.Pins) - freed = append(freed, fmt.Sprint(p.Milestone, " ", p.Catalog, " ", p.Why)) - } - return nil - }} - old, _, _ := rs.Open("46/F", heads, false) - if err := rs.Pin(old, HalfCatalog, "main", 1); err != nil { - t.Fatal(err) - } + rs := &Records{Ledger: l, Now: func() time.Time { return now }, Retention: func() time.Duration { return 90 * 24 * time.Hour }} + old, _, _ := rs.Open(opening("main", 46, heads)) now = at.Add(89 * 24 * time.Hour) - kept, _, _ := rs.Open("47/G", heads, false) - if r, _ := rs.Read(old.Instance); r == nil || len(freed) != 0 { - t.Fatalf("a record within its retention is removed (%v)", freed) + kept, _, _ := rs.Open(opening("main", 47, heads)) + if r, _ := rs.Read(old.Instance); r == nil { + t.Fatal("a record within its retention is removed") } now = at.Add(91 * 24 * time.Hour) - if _, _, err := rs.Open("47/H", heads, false); err != nil { + if _, _, err := rs.Open(opening("main", 48, heads)); err != nil { t.Fatal(err) } if r, _ := rs.Read(old.Instance); r != nil { t.Fatal("a record past its retention is kept") } - if !slices.Equal(freed, []string{"46/F [{main 1}] the record is past its retention"}) { - t.Fatalf("freed %v", freed) - } list, _ := rs.List() if len(list) != 2 || list[0].Instance != kept.Instance { t.Fatalf("list %+v", list) } }) - // W11.9: pinned and freed, said under an id of what they say; only a catalog or an assignment revision is pinned. - t.Run("a pin is written once and said as pinned; a free says freed with why", func(t *testing.T) { - l := newLedgerFake(t) - type said struct { - subject, id string - pins meshdelivery.Pins - } - var got []said - rs := &Records{Ledger: l, Now: func() time.Time { return at }, Say: func(subject string, body any, id string) error { - if subject == meshdelivery.SubjectRecordOpened { - return nil - } - got = append(got, said{subject, id, body.(meshdelivery.Pins)}) - return nil - }} - r, _, _ := rs.Open("47/G", heads, false) - for range 2 { - if err := rs.Pin(r, HalfAssignment, "", 9); err != nil { - t.Fatal(err) - } - } - if err := rs.Pin(r, HalfCatalog, "main", 9); err != nil { - t.Fatal(err) - } - // Revisions are numbered per watched branch: the same number on another branch is another pin. - if err := rs.Pin(r, HalfCatalog, "feat/x", 9); err != nil { - t.Fatal(err) - } - pins := 0 - for _, op := range l.history { - if strings.HasSuffix(op, " "+OpPin) { - pins++ - } - } - back, _ := rs.Read(r.Instance) - if pins != 3 || len(back.Pinned) != 3 { - t.Fatalf("%d pins written, the record holds %+v", pins, back.Pinned) - } - if len(got) != 4 || !reflect.DeepEqual(got[0], got[1]) || got[0].subject != meshdelivery.SubjectPinned || - !slices.Equal(got[0].pins.Assignment, []int64{9}) || !slices.Equal(got[2].pins.Catalog, []meshdelivery.PinsCatalogItem{{Branch: "main", Revision: 9}}) || - !slices.Equal(got[3].pins.Catalog, []meshdelivery.PinsCatalogItem{{Branch: "feat/x", Revision: 9}}) || got[0].pins.Record != r.Instance { - t.Fatalf("said %+v", got) - } - got = nil - if err := rs.Free(back, HalfAssignment, "", 9, "removed by hand"); err != nil { - t.Fatal(err) - } - if err := rs.Free(back, HalfAssignment, "", 9, "again"); err != nil { - t.Fatal(err) - } - if len(got) != 1 || got[0].subject != meshdelivery.SubjectFreed || got[0].pins.Why != "removed by hand" || - !slices.Equal(got[0].pins.Assignment, []int64{9}) { - t.Fatalf("said %+v", got) - } - got = nil - if err := rs.Free(back, HalfCatalog, "feat/x", 9, "removed"); err != nil { - t.Fatal(err) - } - if len(got) != 1 || !slices.Equal(got[0].pins.Catalog, []meshdelivery.PinsCatalogItem{{Branch: "feat/x", Revision: 9}}) { - t.Fatalf("said %+v", got) - } - if again, _ := rs.Read(r.Instance); len(again.Pinned) != 1 { - t.Fatalf("pinned after the free %+v", again.Pinned) - } - if err := rs.Pin(r, "trunk", "", 1); err == nil { - t.Fatal("a pin of no half is taken") - } - }) - t.Run("the setting record-retention, in days", func(t *testing.T) { dir := t.TempDir() for _, c := range []struct { @@ -410,7 +346,7 @@ func TestTheRecordsOutcomesArriveThroughTheGatesConsumer(t *testing.T) { setup := func(t *testing.T) (*ledgerFake, *Records, *Record, *StepConsumer) { l := newLedgerFake(t) rs := &Records{Ledger: l, Now: func() time.Time { return at }} - r, _, err := rs.Open("47/G", map[string]string{"novox/app@111111111111": strings.Repeat("1", 40)}, false) + r, _, err := rs.Open(opening("main", 47, map[string]string{"novox/app@111111111111": strings.Repeat("1", 40)})) if err != nil { t.Fatal(err) } @@ -429,15 +365,20 @@ func TestTheRecordsOutcomesArriveThroughTheGatesConsumer(t *testing.T) { } return []StepOutcome{{Module: b.Module, Fingerprint: b.Fingerprint, Step: StepRunning, Outcome: OutcomeRunning, At: at}}, nil }} - c := &StepConsumer{Records: rs, Families: append(slices.Clone(stepFamilies), running), Now: func() time.Time { return at }} + h := &Holder{Records: rs, Now: func() time.Time { return at }} + h.init() + c := &StepConsumer{Records: rs, Families: append(slices.Clone(stepFamilies), running), Now: func() time.Time { return at }, Taken: h.TakenIn} return l, rs, r, c } - taken := func(id string) stdio.Envelope { - body := `{"v":1,"kind":"taken","key":"m.00000000000000000001","what":"revision","merges":[{"repository":"novox/app","number":7,"merge":"` + - merge + `"}],"since":"2026-11-09T20:00:00Z"}` + takenOf := func(id, body string) stdio.Envelope { return stdio.Envelope{Key: "mesh-delivery-indexer.take-in.taken", Body: json.RawMessage(body), Headers: map[string]string{"x-event-id": id}} } - t.Run("a take-in of the record's merge is its outcome, once", func(t *testing.T) { + taken := func(id string) stdio.Envelope { + return takenOf(id, `{"branch":"main","source":47,"revision":47,"call":"000000000000002f","entries":[{"module":"mesh-y",`+ + `"commit":"`+merge+`","fingerprint":"src2:f2","repository":"novox/app"}]}`) + } + // H §738, H-A-29: the take-in's movers are its record's entries, read by the key .; none ends it. + t.Run("a take-in's movers are its record's entries, taken in, once; a take-in with no mover ends it", func(t *testing.T) { l, rs, r, c := setup(t) if err := c.Hear(taken("e1")); err != nil { t.Fatal(err) @@ -447,12 +388,25 @@ func TestTheRecordsOutcomesArriveThroughTheGatesConsumer(t *testing.T) { t.Fatalf("the same event again: %v, %d operations (was %d)", err, len(l.history), n) } back, _ := rs.Read(r.Instance) - if got := back.Outcomes[OutcomeKey("", "", "", "take-in")]; got.Outcome != OutcomeTakenIn || got.Event != "e1" { - t.Fatalf("outcomes %+v", back.Outcomes) + if _, ok := back.Entries[EntryKey("mesh-y", "src2:f2", "")]; !ok { + t.Fatalf("entries %+v", back.Entries) + } + if got := back.Outcomes[OutcomeKey("mesh-y", "src2:f2", "", "take-in")]; got.Outcome != OutcomeTakenIn || back.Ended != nil { + t.Fatalf("outcomes %+v, ended %+v", back.Outcomes, back.Ended) } if c.Heard()["mesh-delivery-indexer.take-in.taken"].Applied != 2 { t.Fatalf("heard %+v", c.Heard()) } + other, _, _ := rs.Open(opening("main", 48, map[string]string{"novox/app@8": merge})) + if err := c.Hear(takenOf("e4", `{"branch":"main","source":48,"revision":48,"call":"0000000000000030"}`)); err != nil { + t.Fatal(err) + } + if back, _ := rs.Read(other.Instance); back.Ended == nil || back.Ended.Why != WhyNothingToDeliver { + t.Fatalf("a take-in with no mover leaves its record %+v", back.Ended) + } + if back, _ := rs.Read(r.Instance); back.Ended != nil { + t.Fatal("another revision's take-in ended this record") + } }) t.Run("a node's event from its own node is the entry's outcome; from another node it is refused", func(t *testing.T) { _, rs, r, c := setup(t) @@ -479,10 +433,10 @@ func TestTheRecordsOutcomesArriveThroughTheGatesConsumer(t *testing.T) { // away that wakes adds its outcomes and is no longer shown away. t.Run("it ends once every present node runs its entries, and a node away that wakes adds its outcomes", func(t *testing.T) { _, rs, r, c := setup(t) - var ended []recordEnded + var ended []meshdelivery.RecordEndedEvent rs.Say = func(subject string, body any, id string) error { if subject == meshdelivery.SubjectRecordEnded && id == "record-ended."+r.Instance { - ended = append(ended, body.(recordEnded)) + ended = append(ended, body.(meshdelivery.RecordEndedEvent)) } return nil } @@ -518,7 +472,7 @@ func TestTheRecordsOutcomesArriveThroughTheGatesConsumer(t *testing.T) { !back.Ended.Away[0].Since.Equal(at.Add(-time.Hour)) { t.Fatalf("ended %+v", back.Ended) } - if len(ended) != 1 || ended[0].Milestone != "47/G" || len(ended[0].Away) != 1 || ended[0].Away[0].Node != "g14" { + if len(ended) != 1 || ended[0].Milestone != recordMilestone("main", 47) || ended[0].Branch != "main" || ended[0].Source != 47 || ended[0].Call != r.Call || len(ended[0].Away) != 1 || ended[0].Away[0].Node != "g14" { t.Fatalf("record-ended said %+v", ended) } if err := c.Hear(running("g14")); err != nil { @@ -542,7 +496,7 @@ func TestTheRecordsOutcomesArriveThroughTheGatesConsumer(t *testing.T) { if h := c.Heard()["mesh-delivery-indexer.take-in.taken"]; h.Refused != 1 { t.Fatalf("heard %+v", h) } - l.failOn = OpOutcome + l.failOn = OpEntry if err := c.Hear(taken("e3")); err == nil { t.Fatal("an outcome the ledger did not take is taken") } @@ -581,7 +535,7 @@ func TestTheRecordsToolsReadThemAndEndOneByHand(t *testing.T) { var said []string rs := &Records{Ledger: newLedgerFake(t), Now: func() time.Time { return at }, Say: func(subject string, body any, id string) error { said = append(said, subject+" "+id); return nil }} - r, _, err := rs.Open("47/G", heads, false) + r, _, err := rs.Open(opening("main", 47, heads)) if err != nil { t.Fatal(err) } @@ -596,18 +550,18 @@ func TestTheRecordsToolsReadThemAndEndOneByHand(t *testing.T) { t.Run("gate_record and gate_records read a record whole and its fill", func(t *testing.T) { rs, r, said := setup(t) // W9.3: the record's opening is said once, under its id, for the watchman to time against 24 hours. - if !slices.Contains(*said, meshdelivery.SubjectRecordOpened+" record-opened."+r.Instance) { + if !slices.Contains(*said, meshdelivery.SubjectRecordOpened(r.Call)+" record-opened."+r.Instance) { t.Fatalf("the record's opening is not said: %v", *said) } got, err := rs.RecordOf(r.Instance, "") - if err != nil || len(got) != 1 || got[0].Record.Milestone != "47/G" || got[0].Waits != "waiting for mesh-x on ace" || + if err != nil || len(got) != 1 || got[0].Record.Milestone != recordMilestone("main", 47) || got[0].Waits != "waiting for mesh-x on ace" || got[0].Bytes == 0 || got[0].Of != 256<<10 { t.Fatalf("%+v %v", got, err) } - if byMilestone, err := rs.RecordOf("", "47/G"); err != nil || len(byMilestone) != 1 { + if byMilestone, err := rs.RecordOf("", recordMilestone("main", 47)); err != nil || len(byMilestone) != 1 { t.Fatalf("by milestone %+v %v", byMilestone, err) } - if _, err := rs.RecordOf("", "46/F"); err == nil { + if _, err := rs.RecordOf("", recordMilestone("main", 46)); err == nil { t.Fatal("a milestone with no record answers nothing") } lines, err := rs.Lines() @@ -725,7 +679,7 @@ func TestTheRecordsToolsReadThemAndEndOneByHand(t *testing.T) { return nil }} commit := strings.Repeat("2", 40) - r, _, err := rs.Open(recordMilestone("feat/x", 4), map[string]string{"novox/app@222222222222": commit}, false) + r, _, err := rs.Open(opening("feat/x", 4, map[string]string{"novox/app@222222222222": commit})) if err != nil { t.Fatal(err) } @@ -789,7 +743,7 @@ func TestTheRecordsLedgerSaysFillingFullAndRoom(t *testing.T) { } saying := func(out *[]said) SeatSay { return func(subject string, body any, id string) error { - if subject == meshdelivery.SubjectRecordOpened { + if strings.HasPrefix(subject, meshdelivery.SubjectRecordOpened("")) { return nil // the opening of a record, not a fill } f, _ := body.(meshdelivery.Fill) @@ -803,7 +757,7 @@ func TestTheRecordsLedgerSaysFillingFullAndRoom(t *testing.T) { var events []said rs := &Records{Ledger: l, Now: func() time.Time { return at }, Say: saying(&events)} l.fill = filled(capBytes * 7 / 10) - r, opened, err := rs.Open("47/G", heads, false) + r, opened, err := rs.Open(opening("main", 47, heads)) if err != nil || !opened { t.Fatalf("open below the mark: %v %v", opened, err) } @@ -819,16 +773,16 @@ func TestTheRecordsLedgerSaysFillingFullAndRoom(t *testing.T) { } // Already past the mark: a next record is opened and says nothing again. events = nil - if _, opened, err := rs.Open("47/H", heads, false); err != nil || !opened || len(events) != 0 { + if _, opened, err := rs.Open(opening("main", 48, heads)); err != nil || !opened || len(events) != 0 { t.Fatalf("a record past the mark: %v %v, said %+v", opened, err, events) } // No room for one more record: refused, said as full naming what it refuses, nothing opened. l.fill = filled(capBytes - RecordRoom/2) n := len(l.history) - if _, opened, err := rs.Open("47/I", heads, false); !errors.Is(err, ErrRecordsFull) || opened || len(l.history) != n { + if _, opened, err := rs.Open(opening("main", 49, heads)); !errors.Is(err, ErrRecordsFull) || opened || len(l.history) != n { t.Fatalf("a full ledger opened %v (%v), %d operations", opened, err, len(l.history)-n) } - if len(events) != 1 || events[0].subject != meshdelivery.SubjectFull || !strings.Contains(events[0].body.Refused, "47/I") { + if len(events) != 1 || events[0].subject != meshdelivery.SubjectFull || !strings.Contains(events[0].body.Refused, recordMilestone("main", 49)) { t.Fatalf("full said as %+v", events) } if line, _ := rs.Lines(); !strings.HasPrefix(fmt.Sprint(line["fill"].(map[string]any)["state"]), "full") { @@ -836,7 +790,7 @@ func TestTheRecordsLedgerSaysFillingFullAndRoom(t *testing.T) { } // A ledger that says no fill (a runner before the call): the record is opened, and the read tool says why. l.fill = nil - if _, opened, err := rs.Open("47/J", heads, false); err != nil || !opened { + if _, opened, err := rs.Open(opening("main", 50, heads)); err != nil || !opened { t.Fatalf("an unread fill refused the record: %v %v", opened, err) } if line, _ := rs.Lines(); line["fill"].(map[string]any)["unread"] == nil { @@ -860,3 +814,8 @@ func (o *ownersFake) Running(node, module string) (string, string, error) { o.asked = append(o.asked, "running "+node+" "+module) return o.fp, o.phase, nil } + +// opening is a record's opening for a test: the source revision of a branch, its call made of its number. +func opening(branch string, source int64, heads map[string]string) RecordOpening { + return RecordOpening{Branch: branch, Source: source, Call: fmt.Sprintf("%016x", source), Heads: heads} +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordevents.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordevents.go index d5a34a740..5546b825f 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordevents.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordevents.go @@ -1,26 +1,17 @@ package main -// What a delivery record says on the seat `mesh-delivery` (design 47 *The delivery record*): `pinned` and `freed` for the -// graph revisions it starts and stops pointing at, `heads-replaced` for a candidate whose heads were replaced or whose pull -// request closed, and `record-ended` once every present node runs its entries. Only the gate's account publishes them +// What a delivery record says on the seat `mesh-delivery` (design 47 *The delivery record*): `record-ended` once every +// present node runs its entries; the pins are the indexer's (design 47 H §749, H-A-48). Only the gate's account publishes them // (ADR 0363 §2); each is said under an id of what it says, so said again it is kept once, and a say that failed is said // again at the next write of the record. import ( "fmt" "sort" - "strconv" "strings" "time" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - "git.novox.be/novox/mesh-sdk/go/keys" -) - -// The halves of the revision view a record pins (design 47 *Two graphs, one source of truth*). -const ( - HalfCatalog = "catalog" - HalfAssignment = "assignment" ) // StepRunning is the step whose outcome OutcomeRunning says an entry runs healthy on its node: from the node-engine seat's @@ -40,110 +31,6 @@ func (rs *Records) say(subject string, body any, id string) error { return rs.Say(subject, body, id) } -// pinKey is a pinned revision's key in the record: its half, for a catalog revision its watched branch (revisions are numbered -// per branch, A-171) escaped as a name with its slashes escaped too, and its number, so no two revisions' keys meet. -func pinKey(half, branch string, revision int64) string { - key := half + "-" - if half == HalfCatalog { - key += strings.ReplaceAll(keys.EscapeName(branch), "/", "=2f") + "-" - } - return key + strconv.FormatInt(revision, 10) -} - -// number is the revision's number on its half's seat: what follows the key's last dash. -func (p PinnedRevision) number() (int64, bool) { - i := strings.LastIndex(p.Revision, "-") - if i < 0 || !strings.HasPrefix(p.Revision, p.Half+"-") { - return 0, false - } - n, err := strconv.ParseInt(p.Revision[i+1:], 10, 64) - return n, err == nil -} - -// pinsOf is the seat's Pins of a record for these revisions. -func pinsOf(r *Record, pins []PinnedRevision, why string) meshdelivery.Pins { - out := meshdelivery.Pins{Record: r.Instance, Milestone: r.Milestone, Why: why} - for _, p := range pins { - n, ok := p.number() - if !ok { - continue - } - switch p.Half { - case HalfCatalog: - out.Catalog = append(out.Catalog, meshdelivery.PinsCatalogItem{Branch: p.Branch, Revision: n}) - case HalfAssignment: - out.Assignment = append(out.Assignment, n) - } - } - return out -} - -func checkHalf(half string) error { - if half != HalfCatalog && half != HalfAssignment { - return fmt.Errorf("a record pins a catalog or an assignment graph revision, not a %q one", half) - } - return nil -} - -// Pin writes that the record points at a graph revision, unless it does already, and says `pinned` (again, when written -// before: the id keeps it once). A catalog revision names its watched branch, which the record keeps and the event says. -func (rs *Records) Pin(r *Record, half, branch string, revision int64) error { - if err := checkHalf(half); err != nil { - return err - } - if half == HalfCatalog && branch == "" { - return fmt.Errorf("a catalog revision is pinned by its watched branch and its number") - } - if half != HalfCatalog { - branch = "" - } - p := PinnedRevision{Revision: pinKey(half, branch, revision), Half: half, Branch: branch} - if _, ok := r.Pinned[p.Revision]; !ok { - seq, err := rs.Ledger.Apply(r.Instance, OpPin, p) - if err != nil { - return err - } - if r.Pinned == nil { - r.Pinned = map[string]PinnedRevision{} - } - r.Pinned[p.Revision], r.Sequence = p, seq - } - return rs.say(meshdelivery.SubjectPinned, pinsOf(r, []PinnedRevision{p}, ""), "pinned."+r.Instance+"."+p.Revision) -} - -// Free writes that the record no longer points at a graph revision and says `freed` with why; a revision it does not -// point at frees nothing and says nothing. -func (rs *Records) Free(r *Record, half, branch string, revision int64, why string) error { - if err := checkHalf(half); err != nil { - return err - } - p, ok := r.Pinned[pinKey(half, branch, revision)] - if !ok { - return nil - } - if err := rs.say(meshdelivery.SubjectFreed, pinsOf(r, []PinnedRevision{p}, why), "freed."+r.Instance+"."+p.Revision); err != nil { - return err - } - seq, err := rs.Ledger.Apply(r.Instance, OpFree, map[string]any{"revision": p.Revision}) - if err != nil { - return err - } - delete(r.Pinned, p.Revision) - r.Sequence = seq - return nil -} - -// sayFreedAll says `freed` for every revision a record pins, before the record is removed: said first, so a removal never -// leaves a revision pinned by nobody that the indexer or the assigner still keeps. -func (rs *Records) sayFreedAll(r *Record, why string) error { - pins := sortedPins(r) - if len(pins) == 0 { - return nil - } - return rs.say(meshdelivery.SubjectFreed, pinsOf(r, pins, why), "freed."+r.Instance) -} - -// NodeAway writes a node away since when, unless the record holds it already, and ends the record when every other node // runs its entries: a node away holds nothing (ADR 0603 §7). func (rs *Records) NodeAway(r *Record, node string, since time.Time) error { if node == "" { @@ -257,41 +144,15 @@ func (rs *Records) end(r *Record, why, by string) error { return nil } -// recordEnded is the seat's `record-ended` body. -type recordEnded struct { - Record string `json:"record"` - Milestone string `json:"milestone"` - ForgeMade bool `json:"forge-made,omitempty"` - Away []NodeAway `json:"away,omitempty"` - At time.Time `json:"at"` -} - func (rs *Records) sayEnded(r *Record) error { if r.Ended == nil { return nil } - return rs.say(meshdelivery.SubjectRecordEnded, recordEnded{Record: r.Instance, Milestone: r.Milestone, - ForgeMade: r.ForgeMade, Away: r.Ended.Away, At: r.Ended.At}, "record-ended."+r.Instance) -} - -// StatePins says `pinned` again for every revision each record it keeps pins, then once `pins-stated` (design 47 G §579): the -// indexer's consumer of the seat's events hears pins said before it existed or older than the stream keeps, and the set form -// absorbs the repeats. Each repeat has an id of its own, since the bus keeps one message under one id inside its duplicate window. -func (rs *Records) StatePins() (int, error) { - kept, err := rs.Kept() - if err != nil { - return 0, err + at := r.Ended.At + e := meshdelivery.RecordEndedEvent{Record: r.Instance, Milestone: r.Milestone, Branch: r.Branch, Source: r.Source, Call: r.Call, At: &at} + for _, a := range r.Ended.Away { + since := a.Since + e.Away = append(e.Away, meshdelivery.RecordEndedEventAwayItem{Node: a.Node, Since: &since}) } - at := rs.Now().UTC() - stated := 0 - for _, r := range kept { - for _, p := range sortedPins(r) { - id := "pinned." + r.Instance + "." + p.Revision + ".stated." + strconv.FormatInt(at.UnixNano(), 10) - if err := rs.say(meshdelivery.SubjectPinned, pinsOf(r, []PinnedRevision{p}, ""), id); err != nil { - return stated, err - } - stated++ - } - } - return stated, rs.say(meshdelivery.SubjectPinsStated, meshdelivery.PinsStatedEvent{At: at}, "pins-stated."+strconv.FormatInt(at.UnixNano(), 10)) + return rs.say(meshdelivery.SubjectRecordEnded, e, "record-ended."+r.Instance) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordtools.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordtools.go index a0077a8a7..531f078ed 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordtools.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordtools.go @@ -76,11 +76,10 @@ type recordLine struct { Milestone string `json:"milestone"` Opened string `json:"opened"` Ended string `json:"ended,omitempty"` - ForgeMade bool `json:"forge-made,omitempty"` + Call string `json:"call,omitempty"` Entries int `json:"entries"` Outcomes int `json:"outcomes"` Away int `json:"away,omitempty"` - Pinned int `json:"pinned,omitempty"` Bytes int `json:"bytes"` Of int `json:"of-bytes"` Waits string `json:"waits,omitempty"` @@ -96,8 +95,8 @@ func (rs *Records) Lines() (map[string]any, error) { open := 0 for _, r := range list { v := viewOf(r) - l := recordLine{Instance: r.Instance, Milestone: r.Milestone, Opened: timeText(&r.Opened), ForgeMade: r.ForgeMade, - Entries: len(r.Entries), Outcomes: len(r.Outcomes), Away: len(r.Away), Pinned: len(r.Pinned), Bytes: v.Bytes, + l := recordLine{Instance: r.Instance, Milestone: r.Milestone, Opened: timeText(&r.Opened), Call: r.Call, + Entries: len(r.Entries), Outcomes: len(r.Outcomes), Away: len(r.Away), Bytes: v.Bytes, Of: v.Of, Waits: v.Waits} if r.Ended != nil { l.Ended = timeText(&r.Ended.At) + ": " + r.Ended.Why 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 56ce1c278..1535f950b 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go @@ -11,15 +11,12 @@ import ( "errors" "fmt" "sort" - "strconv" "strings" "time" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" 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-sdk/go/keys" ) // registrationChangedEvent is the seat mesh-catalog's event, as the gate's manifest consumes it. @@ -172,7 +169,7 @@ func (h *Holder) startSources(only string) (started, leftOut []string, err error for _, repository := range watched[branch] { if held != nil { if _, has := held.Heads[repository]; has { - continue // a head a key holds moves by a take-in alone (A-34) + continue // a head a key holds moves by a merge call alone (§737) } } a, err := h.Forge.Branch(repository, branch) @@ -192,24 +189,18 @@ func (h *Holder) startSources(only string) (started, leftOut []string, err error if len(heads) == 0 { continue } - wrote := false - rev, err := h.Source.Set(branch, func(prev *Source) (Source, bool) { - next := Source{Heads: map[string]string{}} - if prev != nil { - next.Group = prev.Group - for r, c := range prev.Heads { - next.Heads[r] = c - } + var moved []revisionHead + for _, repository := range watched[branch] { + if c, ok := heads[repository]; ok { + moved = append(moved, revisionHead{Repository: repository, Commit: c}) } - wrote = false - for r, c := range heads { - if _, has := next.Heads[r]; !has { // a held head is never changed - next.Heads[r] = c - wrote = true - } - } - return next, wrote - }) + } + call, err := newCall() + if err != nil { + errs = append(errs, err) + continue + } + named, wrote, err := h.nameRevision(branch, call, moved) if err != nil { errs = append(errs, fmt.Errorf("source.%s cannot be started: %w", branch, err)) continue @@ -218,13 +209,8 @@ func (h *Holder) startSources(only string) (started, leftOut []string, err error continue } started = append(started, branch) - if h.SaySeat == nil { - continue - } - at := h.Now().UTC() - if err := h.SaySeat(seat.SubjectSourceMoved(keys.EscapeName(branch)), - seat.SourceMovedEvent{Branch: branch, Revision: int64(rev), At: &at}, - "source-moved."+keys.EscapeName(branch)+"."+strconv.FormatUint(rev, 10)); err != nil { + // start is true only for a branch's first revision (§737, §768). + if err := h.saySourceMoved(branch, named, held == nil, moved); err != nil { errs = append(errs, err) } } @@ -255,7 +241,7 @@ func (h *Holder) SourceStart(branch, why, by string, acts handActs) (map[string] started, leftOut, err := h.startSources(branch) out := map[string]any{"branch": branch, "started": started, "left-out": leftOut, "by": by} if err == nil && len(started) == 0 && len(leftOut) == 0 { - out["note"] = "the key names every registered repository already: only a take-in moves a head" + out["note"] = "the key names every registered repository already: only a merge call moves a head" } return out, err } 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..421a02441 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go @@ -7,6 +7,7 @@ package main import ( "errors" "fmt" + "sort" "strconv" "strings" @@ -39,7 +40,7 @@ func (h *Holder) SourceAgain(branch, why, by string, acts handActs) (map[string] if h.Source == nil || h.SaySeat == nil { return nil, errors.New("this gate keeps no source or says no events") } - held, rev, err := h.Source.Read(branch) + held, _, err := h.Source.Read(branch) if err != nil { return nil, err } @@ -54,13 +55,16 @@ func (h *Holder) SourceAgain(branch, why, by string, acts handActs) (map[string] return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was said: %w", err) } at := h.Now().UTC() - escaped := keys.EscapeName(branch) - id := "source-moved." + escaped + "." + strconv.FormatUint(rev, 10) + ".again." + strconv.FormatInt(at.UnixNano(), 10) - if err := h.SaySeat(seat.SubjectSourceMoved(escaped), - seat.SourceMovedEvent{Branch: branch, Group: held.Group, Revision: int64(rev), At: &at}, id); err != nil { + escaped, number := keys.EscapeName(branch), strconv.FormatInt(held.Source, 10) + id := "source-moved." + escaped + "." + number + ".again." + strconv.FormatInt(at.UnixNano(), 10) + e := seat.SourceMovedEvent{V: 1, Branch: branch, Source: held.Source, Call: held.Call, Heads: []seat.SourceMovedEventHeadsItem{}} + for _, r := range sortedKeys(held.Heads) { + e.Heads = append(e.Heads, seat.SourceMovedEventHeadsItem{Repository: r, Commit: held.Heads[r]}) + } + if err := h.SaySeat(seat.SubjectSourceMoved(escaped, number), e, id); err != nil { return nil, err } - return map[string]any{"said": "source-moved", "branch": branch, "revision": rev, "by": by}, nil + return map[string]any{"said": "source-moved", "branch": branch, "source": held.Source, "call": held.Call, "by": by}, nil } // sourceTools are the source's tools; acts is the hand-act log. @@ -68,7 +72,7 @@ func sourceTools(h *Holder, acts handActs) []stdio.Tool { return []stdio.Tool{ {Name: "gate_source", Description: "Every branch's source revision the gate keeps, each whole: its repositories with their trunk branch and head, " + - "the group that moved it last and the key's revision. Read, never written.", + "the merge call that moved it last and its source revision. Read, never written.", Run: func(map[string]any) (any, error) { return h.SourceAll(), nil }}, {Name: "gate_source_again", Description: "Say source-moved again for one branch, with the revision its source key holds now, for a part that missed it; " + @@ -81,7 +85,7 @@ func sourceTools(h *Holder, acts handActs) []stdio.Tool { }}, {Name: "gate_source_start", Description: "Start the source of one watched branch the registrations name and no source key holds yet, from the forge: each " + - "registered repository at the branch's head, the group empty, then source-moved; recorded in the hand-act log of the seat " + + "registered repository at the branch's head, one source revision with a call of its own, then source-moved; recorded in the hand-act log of the seat " + "mesh-hand-acts first. A key the gate holds is never rewritten.", Input: map[string]any{"type": "object", "properties": map[string]any{ "branch": str("the watched branch"), "why": str("why it is started by hand, in plain words")}, @@ -91,3 +95,13 @@ func sourceTools(h *Holder, acts handActs) []stdio.Tool { }}, } } + +// sortedKeys are a map's keys, sorted. +func sortedKeys(m map[string]string) []string { + out := make([]string, 0, len(m)) + for k := range m { + out = append(out, k) + } + sort.Strings(out) + return out +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go index 6968dee4e..48e9c668e 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go @@ -31,7 +31,7 @@ func TestTheSourceToolsReadEveryBranchAndSayItAgainAfterTheLog(t *testing.T) { src := &branchSource{} for _, b := range []string{"main", "feat"} { _, _ = src.Set(b, func(*Source) (Source, bool) { - return Source{Group: "g-" + b, Heads: map[string]string{"novox/app": "c1"}}, true + return Source{Source: 3, Call: "00000000000000c1", Heads: map[string]string{"novox/app": "c1"}}, true }) } type said struct{ subject, id string } @@ -61,44 +61,18 @@ func TestTheSourceToolsReadEveryBranchAndSayItAgainAfterTheLog(t *testing.T) { t.Fatal(err) } if len(acts.took) != 1 || acts.took[0].Cause != "gate_source_again" || len(events) != 1 || - !strings.HasSuffix(events[0].subject, ".event.source-moved.main") || !strings.Contains(events[0].id, ".again.") { + !strings.HasSuffix(events[0].subject, ".event.source-moved.main.3") || !strings.Contains(events[0].id, ".again.") { t.Fatalf("log %+v events %+v", acts.took, events) } } -// Design 47 G §579: at its start the gate says `pinned` again for every revision each record pins, each under an id of its own, -// then `pins-stated` once. -func TestTheGateStatesItsPinsThenSaysPinsStated(t *testing.T) { - at := time.Date(2026, 11, 12, 4, 0, 0, 0, time.UTC) - type said struct{ subject, id string } - var events []said - rs := &Records{Ledger: newLedgerFake(t), Now: func() time.Time { return at }, - Say: func(subject string, body any, id string) error { - events = append(events, said{subject, id}) - return nil - }} - r, _, err := rs.Open("47/G", map[string]string{"novox/app@1": "c1"}, false) - if err != nil { - t.Fatal(err) - } - if err := rs.Pin(r, HalfCatalog, "main", 3); err != nil { - t.Fatal(err) - } - events = nil - n, err := rs.StatePins() - if err != nil || n != 1 || len(events) != 2 || !strings.HasSuffix(events[0].subject, ".event.pinned") || - !strings.Contains(events[0].id, ".stated.") || !strings.HasSuffix(events[1].subject, ".event.pins-stated") { - t.Fatalf("%d %v %+v", n, err, events) - } -} - // Design 47 G §428, §507: no part's code holds a branch's name; the trunk of each repository a source revision names is the // branch whose key was read, and delivery_status says every branch's source, not one fixed branch's. func TestTheSourceAnswerNamesTheBranchItWasAskedAndStatusSaysEveryBranch(t *testing.T) { src := &branchSource{} for _, b := range []string{"release", "feat"} { _, _ = src.Set(b, func(*Source) (Source, bool) { - return Source{Group: "g-" + b, Heads: map[string]string{"novox/app": "c1"}}, true + return Source{Source: 3, Call: "00000000000000c1", Heads: map[string]string{"novox/app": "c1"}}, true }) } h := &Holder{Now: time.Now, Source: src} @@ -150,15 +124,15 @@ func TestAWatchedBranchStartsFromTheForge(t *testing.T) { heads: map[string]map[string]string{"main": {"o/car-hunter": "c1", "o/mesh": "c2", "o/photos": "c6"}, "master": {"o/f1-circuits": "c3"}, "nox-mesh": {"o/link2pay": "c4"}}}, {name: "a held key lacking repositories gets them, its heads unchanged", catalog: fakeCatalog{regs: regs}, - held: map[string]Source{"main": {Group: "g", Heads: map[string]string{"o/mesh": "old"}}}, + held: map[string]Source{"main": {Source: 4, Call: "00000000000000aa", Heads: map[string]string{"o/mesh": "old"}}}, started: []string{"main", "master", "nox-mesh"}, events: 3, heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "c1", "o/photos": "c6"}, "master": {"o/f1-circuits": "c3"}, "nox-mesh": {"o/link2pay": "c4"}}}, {name: "a held key lacking one repository", catalog: fakeCatalog{regs: regs[:2]}, - held: map[string]Source{"main": {Group: "g", Heads: map[string]string{"o/mesh": "old"}}}, + held: map[string]Source{"main": {Source: 4, Call: "00000000000000aa", Heads: map[string]string{"o/mesh": "old"}}}, started: []string{"main"}, events: 1, heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "c1"}}}, {name: "a held key naming all: nothing written or said", catalog: fakeCatalog{regs: regs[:2]}, - held: map[string]Source{"main": {Group: "g", Heads: map[string]string{"o/mesh": "old", "o/car-hunter": "older"}}}, + held: map[string]Source{"main": {Source: 4, Call: "00000000000000aa", Heads: map[string]string{"o/mesh": "old", "o/car-hunter": "older"}}}, events: 0, heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "older"}}}, {name: "by hand, one branch", catalog: fakeCatalog{regs: regs}, only: "master", @@ -177,8 +151,9 @@ func TestAWatchedBranchStartsFromTheForge(t *testing.T) { Logf: func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) }, SaySeat: func(subject string, body any, id string) error { events++ - if ev := body.(seat.SourceMovedEvent); ev.Group != "" { - t.Errorf("a source-moved for started heads names no group (A-404), got %q", ev.Group) + ev := body.(seat.SourceMovedEvent) + if held, had := c.held[ev.Branch]; ev.Start == had || ev.Source != held.Source+1 || len(ev.Call) != 16 || len(ev.Heads) == 0 { + t.Errorf("a start's source-moved: %+v (start only for a branch's first revision, numbered above its newest)", ev) } return nil }} @@ -198,8 +173,8 @@ func TestAWatchedBranchStartsFromTheForge(t *testing.T) { } } for b, h := range c.held { - if src.vals[b].Group != h.Group { - t.Errorf("source.%s keeps the group %q a take-in wrote, got %q", b, h.Group, src.vals[b].Group) + if c.events == 0 && !reflect.DeepEqual(src.vals[b], h) { + t.Errorf("source.%s naming every repository is rewritten: %+v", b, src.vals[b]) } } if c.only == "" && c.held == nil && !strings.Contains(strings.Join(logged, "\n"), "o/photos") { @@ -255,7 +230,7 @@ func TestRegistrationChangedAndTheStartPassStartABranch(t *testing.T) { if err := c.run(h); err != nil { t.Fatal(err) } - if src.vals["master"].Heads["o/f1"] != "c3" || len(subjects) != 1 || !strings.HasSuffix(subjects[0], ".event.source-moved.master") { + if src.vals["master"].Heads["o/f1"] != "c3" || len(subjects) != 1 || !strings.HasSuffix(subjects[0], ".event.source-moved.master.1") { t.Fatalf("source %+v events %v", src.vals, subjects) } }) 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 84a1327c7..b6060d9c6 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go @@ -3,75 +3,61 @@ package main import ( "encoding/json" "fmt" - "sort" "strconv" - "strings" - 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" "git.novox.be/novox/mesh-sdk/go/keys" ) -// 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). +// What a take-in.taken does at the gate (design 47 H §738, H-A-29): the record of its source revision, opened by the merge +// call (§737) and read by its key `.`, never by reading every record, gains the take-in's movers as its +// entries, each taken in; a take-in with no mover ends its record at once as nothing to deliver. It moves no source key: +// only a merge call does (§737, §764). -// recordMilestone is the key of a taken revision's record: `.`, each escaped as a name (A-33). +// recordMilestone is the key of a source revision's record: `.`, each escaped as a name (A-33). func recordMilestone(branch string, source int64) string { return keys.EscapeName(branch) + "." + keys.EscapeName(strconv.FormatInt(source, 10)) } -// 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. A hand drop -// took nothing in, so it opens nothing. +// WhyNothingToDeliver is why a record whose take-in named no mover ends (§738). +const WhyNothingToDeliver = "nothing to deliver" + +// TakenIn takes one take-in.taken body. A revision with no record (a seed's or a start's, which open none, H-A-47) is +// taken with nothing written; safe to repeat: an entry or outcome the record holds is not written again. func (h *Holder) TakenIn(body json.RawMessage) error { var t meshcatalog.TakeInTaken if err := json.Unmarshal(body, &t); err != nil { return fmt.Errorf("a take-in.taken is not readable: %w", err) } - if t.Dropped || len(t.Merges) == 0 || t.Branch == "" { + if h.Records == nil || t.Branch == "" || t.Source < 1 { return nil } - if h.Records != nil { - heads := map[string]string{} - for _, m := range t.Merges { - heads[mdc.ApprovedKey(m.Repository, int(m.Number))] = m.Merge + r, err := h.Records.ByKey(t.Branch, t.Source) + if err != nil { + return fmt.Errorf("the record of %s at %d cannot be read: %w", t.Branch, t.Source, err) + } + if r == nil || r.Ended != nil { + return nil + } + at := h.Now().UTC() + for _, e := range t.Entries { + if e.Module == "" { + continue } - milestone := recordMilestone(t.Branch, t.Source) - r, _, err := h.Records.Open(milestone, heads, false) - if err != nil { - return fmt.Errorf("the record of %s cannot be opened: %w", milestone, err) + if err := h.Records.AddEntry(r, RecordEntry{Module: e.Module, Fingerprint: e.Fingerprint}); err != nil { + return err } - g := RecordGroup{Group: t.Group} - seen := map[string]bool{} - for _, e := range t.Entries { - // A-188: the take-in's entries, each with its module, are written into the record. - if e.Module != "" { - if err := h.Records.AddEntry(r, RecordEntry{Module: e.Module, Fingerprint: e.Fingerprint}); err != nil { - return err - } - } - if e.Repository == "" || seen[mdc.ApprovedKey(e.Repository, int(e.Pull))] { - continue - } - seen[mdc.ApprovedKey(e.Repository, int(e.Pull))] = true - g.Members = append(g.Members, mdc.ApprovedKey(e.Repository, int(e.Pull))) - } - sort.Strings(g.Members) - for _, m := range t.Merges { - p := RecordPull{Repository: m.Repository, Number: int(m.Number), Head: heads[mdc.ApprovedKey(m.Repository, int(m.Number))], - State: string(mdc.MergeMerged), Commit: m.Merge} - if r.Group != nil { - for _, o := range r.Group.Pulls { - if strings.EqualFold(o.Repository, m.Repository) && o.Number == int(m.Number) { - p.Base = o.Base - } - } - } - g.Pulls = append(g.Pulls, p) - } - if err := h.Records.SetGroup(r, g); err != nil { + if _, err := h.Records.Outcome(r, RecordOutcome{Module: e.Module, Fingerprint: e.Fingerprint, Step: "take-in", + Outcome: OutcomeTakenIn, Why: t.Why, At: at}); err != nil { return err } } + if len(t.Entries) == 0 { + why := WhyNothingToDeliver + if t.Skipped { + why += ": its take-in was skipped by hand: " + t.Why + } + return h.Records.End(r, why, t.By) + } 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 35615b4e9..0edbfcc53 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 @@ -9,12 +9,13 @@ import ( "time" ) -// 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). +// The gate whole, over a real bus (design 47 H §737, §738; acceptance test 110's gate half): a merge-called the forge module +// says names one source revision, opens its delivery record and says source-moved; the indexer's take-in.taken of that +// revision writes its movers into the record; 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 TestAGateWholeTakesAnIndexersTakeInIntoARecordOverARealBus(t *testing.T) { +func TestAGateWholeTakesAMergeCallIntoASourceRevisionAndARecordOverARealBus(t *testing.T) { b := newTestBus(t) ctx := context.Background() rt := b.start(t) @@ -23,22 +24,31 @@ func TestAGateWholeTakesAnIndexersTakeInIntoARecordOverARealBus(t *testing.T) { rt.deliverEvents(evctx) rt.untilListening() merge := strings.Repeat("c", 40) - b.publish(t, "mesh.mod.mesh-delivery-indexer.event.take-in.taken", map[string]any{"v": 1, "kind": "taken", "key": "m.00000000000000000001", - "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 record of the take-in", eventWithin, func() bool { - b.mu.Lock() - defer b.mu.Unlock() - if b.ledger == nil { + call := "0123456789abcdef" + instance, _ := RecordInstance(recordMilestone("main", 1)) + member := map[string]any{"repository": "novox/app", "number": 5, "branch": "main", "head": strings.Repeat("d", 40), "commit": merge} + for range 2 { // heard twice: one revision + b.publish(t, "mesh.seat.git.event.merge-called."+call, map[string]any{"call": call, "by": "lead@shanks", "account": "acct-s", + "at": "2026-11-20T03:00:00Z", "members": []any{member}}) + } + rt.until("the record of the merge call and its source revision", eventWithin, func() bool { + e, err := b.kvs["source"].Get(ctx, "main") + if err != nil || !strings.Contains(string(e.Value()), `"call":"`+call+`"`) || !strings.Contains(string(e.Value()), `"source":1`) { return false } - for instance := range b.ledger.states { - if strings.Contains(instance, "main") { - return true - } - } - return false + b.mu.Lock() + defer b.mu.Unlock() + return b.ledger != nil && b.ledger.states[instance] != nil }) + b.publish(t, "mesh.mod.mesh-delivery-indexer.event.take-in.taken", map[string]any{"branch": "main", "source": 1, "revision": 1, + "call": call, "entries": []any{map[string]any{"module": "app", "commit": merge, "fingerprint": "f", "repository": "novox/app"}}}) + rt.until("the take-in's mover in the record", eventWithin, func() bool { + b.mu.Lock() + defer b.mu.Unlock() + return strings.Contains(string(b.ledger.states[instance]), `"module":"app"`) + }) + if e, _ := b.kvs["source"].Get(ctx, "main"); e.Revision() != 1 { + t.Fatalf("the merge call heard twice wrote source.main %d times", e.Revision()) + } rt.noControllerAsk() } 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 abf988058..b145844b9 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 @@ -7,7 +7,6 @@ import ( "time" stdio "git.novox.be/novox/mesh-sdk/go" - "git.novox.be/novox/mesh-sdk/go/keys" ) // memSource is the gate's source bucket in memory: one value and revision per branch key. @@ -41,11 +40,9 @@ func (m *memSource) Read(branch string) (*Source, uint64, error) { } func (m *memSource) Branches() ([]string, error) { return nil, nil } -// 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) { +// GATE-3/AC3 (design 47 H §737, §764): neither a take-in.taken nor the forge bridge's merged message moves a source key, +// says source-moved or opens a record: only a merge call does (mergecalled_test.go). +func TestATakeInTakenOpensNoRecordAndMovesNoSource(t *testing.T) { now := time.Date(2026, 11, 11, 20, 0, 0, 0, time.UTC) type said struct{ subject, id string } var events []said @@ -57,30 +54,14 @@ func TestATakeInTakenOpensOneRecordAndMovesNoSource(t *testing.T) { }} h.Records = &Records{Ledger: newLedgerFake(t), Now: h.Now} merge := strings.Repeat("a", 40) - body := json.RawMessage(`{"v":1,"kind":"taken","key":"g.feat","what":"group","group":"feat","branch":"main","source":7,` + - `"merges":[{"repository":"novox/app","number":3,"merge":"` + merge + `"}],` + - `"entries":[{"module":"app","commit":"` + merge + `","fingerprint":"f","repository":"novox/app","pull":3}],"since":"2026-11-11T19:00:00Z"}`) + body := json.RawMessage(`{"branch":"main","source":7,"revision":7,"call":"0000000000000007",` + + `"entries":[{"module":"app","commit":"` + merge + `","fingerprint":"f","repository":"novox/app"}]}`) c := &StepConsumer{Records: h.Records, Families: stepFamilies, Now: h.Now, Taken: h.TakenIn} - for i := 0; i < 2; i++ { - if err := c.Hear(stdio.Envelope{Key: "mesh-delivery-indexer.take-in.taken", Body: body}); err != nil { - t.Fatal(err) - } + if err := c.Hear(stdio.Envelope{Key: "mesh-delivery-indexer.take-in.taken", Body: body}); err != nil { + t.Fatal(err) } - kept, err := h.Records.Kept() - if err != nil || len(kept) != 1 { - t.Fatalf("records %v %v: one per take-in", kept, err) - } - r := kept[0] - if r.Milestone != keys.EscapeName("main")+".7" || r.Heads["novox/app@3"] != merge || r.Group == nil || - len(r.Group.Members) != 1 || r.Group.Members[0] != "novox/app@3" || r.Group.Group != "feat" { - t.Fatalf("the record %+v group %+v", r, r.Group) - } - // A-188: the take-in's entries are in the record, each with its module. - if e, ok := r.Entries[EntryKey("app", "f", "")]; !ok || e.Module != "app" { - t.Fatalf("the take-in's entries are not in the record: %+v", r.Entries) - } - 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 kept, err := h.Records.Kept(); err != nil || len(kept) != 0 { + t.Fatalf("records %v %v: a take-in opens none", kept, err) } if len(src.vals) != 0 || len(events) != 0 { t.Fatalf("a take-in.taken moved source %+v and said %+v", src.vals, events) @@ -90,12 +71,4 @@ func TestATakeInTakenOpensOneRecordAndMovesNoSource(t *testing.T) { 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) != 0 { - t.Fatalf("a hand drop opened %d records and said %d events", len(kept), len(events)) - } } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/verbs.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/verbs.go index 2d84649dc..ecf5170d9 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/verbs.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/verbs.go @@ -5,8 +5,6 @@ import ( "sort" "strings" "time" - - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" ) // The seat's verbs that read (novox/hq ADR 0239; design 47 G §431, §172, A-24): from G's delivery the gate keeps no merge @@ -52,14 +50,27 @@ func holdsRepository(r *Record, repository string) bool { return false } +// Member is one pull request of a record as the verbs deliveries and show answer it: its id, repository, number, head, +// state, whether it is open and its base (the seat's type goes with the verb groups, design 47 H §764; the verbs answer +// from the records, A-24). +type Member struct { + ID string `json:"id"` + Repository string `json:"repository"` + Number int64 `json:"number"` + Head string `json:"head"` + State string `json:"state"` + Open bool `json:"open,omitempty"` + Base string `json:"base,omitempty"` +} + // membersOf are a record's pull requests: the group's pulls as kept, or else the members its heads name (`@` // to the commit, as an Approve names them). -func membersOf(r *Record) []seat.Member { +func membersOf(r *Record) []Member { open := r.Ended == nil - var out []seat.Member + var out []Member if r.Group != nil && len(r.Group.Pulls) > 0 { for _, p := range r.Group.Pulls { - out = append(out, seat.Member{ID: IDOf(p.Repository, firstOf(p.Commit, p.Head)), Repository: p.Repository, State: p.State, + out = append(out, Member{ID: IDOf(p.Repository, firstOf(p.Commit, p.Head)), Repository: p.Repository, State: p.State, Number: int64(p.Number), Head: p.Head, Open: open && p.State != "merged", Base: p.Base}) } return out @@ -74,7 +85,7 @@ func membersOf(r *Record) []seat.Member { if !ok { repository = id } - out = append(out, seat.Member{ID: IDOf(repository, r.Heads[id]), Repository: repository, Number: int64(number), Head: r.Heads[id], + out = append(out, Member{ID: IDOf(repository, r.Heads[id]), Repository: repository, Number: int64(number), Head: r.Heads[id], State: stateOf(r), Open: open}) } return out @@ -164,50 +175,6 @@ func (h *Holder) Show(id string) (any, error) { return map[string]any{"records": out}, nil } -// Groups is the seat's verb groups, from the records alone until the seat's definition drops it (design 47 H §764): every record's group not ended (all: those that ended too), or with only that group -// alone, ended or not, and none for a group it does not know. Each with its members, its state, why, and the acts the gate said -// for it with the outcome recorded in the record (owedended.go). -func (h *Holder) Groups(all bool, only string) seat.GroupsAnswer { - kept, err := h.keptRecords() - if err != nil { - return seat.GroupsAnswer{Error: err.Error()} - } - out := seat.GroupsAnswer{Groups: []seat.Group{}} - for _, r := range kept { - if only != "" && groupOf(r) != only || only == "" && !all && r.Ended != nil { - continue - } - g := seat.Group{ID: groupOf(r), State: stateOf(r), Members: membersOf(r), Acts: recordedActs(r)} - if r.Ended != nil { - g.Why = r.Ended.Why - } else { - g.Why = allRun(r) - } - out.Groups = append(out.Groups, g) - } - sort.SliceStable(out.Groups, func(i, j int) bool { return out.Groups[i].ID < out.Groups[j].ID }) - return out -} - -// recordedActs are the acts a record holds, each with its outcome when the forge bridge said one, ordered by id. -func recordedActs(r *Record) []seat.Act { - out := []seat.Act{} - for _, a := range r.Acts { - o := a.Owed - if o.ID == "" { - o = seat.Owed{ID: a.ID, Act: a.Act, Repository: a.Repository, Number: int64(a.Number), Head: a.Head} - } - act := seat.Act{Outcome: a.Outcome, Words: a.Words, Owed: o} - if a.Outcome != "" { - at := a.At - act.At = &at - } - out = append(out, act) - } - sort.Slice(out, func(i, j int) bool { return out[i].Owed.ID < out[j].Owed.ID }) - return out -} - // StalledLine is one record stalled: its open wait past its bound, or an act owed the forge unanswered past its. type StalledLine struct { // ID is the record's instance. diff --git a/modules/mesh-delivery-gate/module.json b/modules/mesh-delivery-gate/module.json index d7d108ff7..10cc389f7 100644 --- a/modules/mesh-delivery-gate/module.json +++ b/modules/mesh-delivery-gate/module.json @@ -9,7 +9,6 @@ "serves": [ "deliveries", "show", - "groups", "source", "recovery", "stalled" @@ -18,13 +17,11 @@ ], "consumes": [ "mesh-delivery-forge-bridge.owed.ended", + "git.merge-called.*", "mesh-delivery-indexer.take-in.taken", "mesh-catalog.registration-changed", "mesh-conditions.findings-asked" ], - "emits": [ - "transition" - ], "uses": [ "operator-channel" ], @@ -43,15 +40,18 @@ "milestone": { "type": "string" }, + "source": { + "type": "integer" + }, + "call": { + "type": "string" + }, "heads": { "type": "object" }, "opened": { "type": "string" }, - "forge-made": { - "type": "boolean" - }, "form": { "type": "integer" }, @@ -91,12 +91,6 @@ "since": { "type": "string" }, - "revision": { - "type": "string" - }, - "half": { - "type": "string" - }, "branch": { "type": "string" }, @@ -134,9 +128,11 @@ "effect": "set-fields", "fields": [ "milestone", + "branch", + "source", + "call", "heads", "opened", - "forge-made", "form" ], "sequence": true @@ -205,23 +201,6 @@ "into": "away", "key": "node" }, - { - "name": "pin", - "effect": "set-entry", - "fields": [ - "revision", - "half", - "branch" - ], - "into": "pinned", - "key": "revision" - }, - { - "name": "free", - "effect": "remove", - "into": "pinned", - "key": "revision" - }, { "name": "act", "effect": "set-entry",