From 7ac48fd8824ef639d13a967c9b23ea09c39d4323 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:31:04 +0100 Subject: [PATCH] B2, B3: the forge bridge says head-moved and carries the one model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit B2: on every branch-moved of a watched branch, and at its start for every watched branch's head, the bridge reads the head through the seat git's verb branch, as a kept call that waits on the holder's return while the branch is watched, and writes one head-moved (repository, branch, head, before the head held in its state heads, start) on its stream forge; a head equal to the one held writes nothing. It reads no pull request, no tree, no file and no protection: the merged and pushed messages, their inputs and the state inputs, the call of git.pull, the finding branch-unprotected with its held finding writer, and the ten-second redelivery go. A move whose head cannot be read is left unacknowledged for the bus to deliver again. B3: the pull-message path (Convert, the open pull requests at start and reconnect, the forge-delete message), the groups (internal/groups' group part, forge.group.merged, end-asks, bridge_groups, bridge_group_end_again), bridge_merged_message with the forge-made reader, the checker's owed and the merge act of a switched key (with the switch's bookkeeping read) go, with their consumes, invokes, states and requires. The gate's owed path stays (H-A-71), its catch-up from the gate's verb groups gone with that verb. The two meet in main.go and the package compiles only with both, as it did not since B1. The unit suites pass; the whole test (integration) is rewritten in B4. Builds: design 47 H §739, §764 (the forge bridge; the branch protection); lead answers H-A-23, H-A-27, H-A-48, H-A-71 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../mesh-delivery-forge-bridge-tools/main.go | 187 +----- .../main_test.go | 241 +------- .../mesh-delivery-forge-bridge-tools/owed.go | 2 +- .../mesh-delivery-forge-bridge/bound_test.go | 2 +- .../cmd/mesh-delivery-forge-bridge/call.go | 12 - .../mesh-delivery-forge-bridge/findings.go | 111 ---- .../mesh-delivery-forge-bridge/gate_test.go | 127 +--- .../mesh-delivery-forge-bridge/groupask.go | 67 --- .../cmd/mesh-delivery-forge-bridge/groups.go | 148 ----- .../mesh-delivery-forge-bridge/groups_test.go | 125 ---- .../cmd/mesh-delivery-forge-bridge/held.go | 16 +- .../cmd/mesh-delivery-forge-bridge/kept.go | 79 --- .../cmd/mesh-delivery-forge-bridge/main.go | 172 +----- .../mesh-delivery-forge-bridge/main_test.go | 388 ++++-------- .../cmd/mesh-delivery-forge-bridge/moves.go | 88 ++- .../mesh-delivery-forge-bridge/names_test.go | 6 +- .../cmd/mesh-delivery-forge-bridge/open.go | 143 ----- .../mesh-delivery-forge-bridge/open_test.go | 295 ---------- .../cmd/mesh-delivery-forge-bridge/owed.go | 88 +-- .../forgemsg/owed.go | 3 +- .../internal/bridge/bridge.go | 556 +----------------- .../internal/bridge/bridge_test.go | 423 ------------- .../internal/bridge/call.go | 59 -- .../internal/bridge/files_test.go | 412 ------------- .../internal/bridge/forgemade.go | 46 -- .../internal/bridge/kept.go | 64 -- .../internal/bridge/moved.go | 59 +- .../internal/bridge/moved_test.go | 74 +-- .../internal/bridge/owed.go | 39 +- .../internal/bridge/owed_test.go | 12 +- .../internal/bridge/paths_test.go | 36 -- .../internal/groups/groups.go | 301 ---------- .../internal/groups/owedask.go | 4 +- .../internal/groups/read_test.go | 49 -- .../manifest_test.go | 60 +- .../mesh-delivery-forge-bridge/module.json | 33 -- 36 files changed, 337 insertions(+), 4190 deletions(-) delete mode 100644 modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/findings.go delete mode 100644 modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/groupask.go delete mode 100644 modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/groups.go delete mode 100644 modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/groups_test.go delete mode 100644 modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/kept.go delete mode 100644 modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/open.go delete mode 100644 modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/open_test.go delete mode 100644 modules/mesh-delivery-forge-bridge/internal/bridge/files_test.go delete mode 100644 modules/mesh-delivery-forge-bridge/internal/bridge/forgemade.go delete mode 100644 modules/mesh-delivery-forge-bridge/internal/bridge/kept.go delete mode 100644 modules/mesh-delivery-forge-bridge/internal/bridge/paths_test.go delete mode 100644 modules/mesh-delivery-forge-bridge/internal/groups/groups.go delete mode 100644 modules/mesh-delivery-forge-bridge/internal/groups/read_test.go diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/main.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/main.go index 1add275e..9b246454 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/main.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/main.go @@ -1,8 +1,8 @@ -// Command mesh-delivery-forge-bridge-tools serves the forge bridge's tools (novox/hq ADR 0633 §4, issue 1585; design 51): -// the runtime launches it for a call. bridge_tags reads a package's git tags through the seat git's verb tags, for the -// builder's floor. bridge_groups reads the delivery groups the gate holds and what the bridge kept of their ends; -// bridge_group_end_again writes an ask into the bridge's state end-asks and waits for the bridge's answer there, since the -// bridge is the other program and serves no request subject (ADR 0437 §4). +// Command mesh-delivery-forge-bridge-tools serves the forge bridge's tools (design 51; design 47 *What the gate owes the +// forge*): the runtime launches it for a call. bridge_tags reads a package's git tags through the seat git's verb tags, +// for the builder's floor; bridge_owed, bridge_owed_again and bridge_owed_drop write an ask into the bridge's state +// owed-asks and wait for the bridge's answer there, since the bridge is the other program and serves no request subject +// (ADR 0437 §4). package main import ( @@ -15,28 +15,14 @@ import ( "time" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" stdio "git.novox.be/novox/mesh-sdk/go" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" - "mesh-delivery-forge-bridge/internal/bridge" "mesh-delivery-forge-bridge/internal/groups" ) -// Tool names. -const ( - GroupsTool = "bridge_groups" - EndAgainTool = "bridge_group_end_again" - // MergedTool answers the merged message of a merge on request (issue 1585). - MergedTool = "bridge_merged_message" -) - -// asker writes an end-asks value and waits for the bridge's answer to it. -type asker func(key string, value []byte) (groups.EndAsk, error) - -// Tools are the bridge's tools: groupsRead reads the seat mesh-delivery's groups with what the bridge kept of them. -func Tools(ask asker, now func() time.Time, groupsRead func() ([]groups.Row, []string, error), merged func(repository, merge string, number int) (json.RawMessage, error), forge bridge.Caller) []stdio.Tool { +// Tools are the bridge's reading tools: forge calls the seat git. +func Tools(forge bridge.Caller) []stdio.Tool { str := func(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) } return []stdio.Tool{ {Name: bridge.TagsTool, @@ -49,150 +35,6 @@ func Tools(ask asker, now func() time.Time, groupsRead func() ([]groups.Row, []s Run: func(args map[string]any) (any, error) { return bridge.Tags(context.Background(), forge, str(args, "repository"), str(args, "prefix")) }}, - {Name: MergedTool, - Description: "The merged message of one merge, asked again (issue 1585): the message the bridge wrote on its stream FORGE when the " + - "pull request was merged into the trunk, built the same way from the forge (the merge commit's trees and the manifests " + - "and lock files of its module directories), the pull request's number, branch and head from the seat git's verb pull, and " + - "for a delivery group's member its group and members from the seat mesh-delivery's verb groups. For the indexer, which asks it when it takes in a held merge it does not hold in memory (a restart, a new " + - "build), and the ask-again a lost message lacked. About 0.5 MB for a repository of many modules. Answers {message} " + - "with the forge message whole, or why it cannot: the forge gives no merged pull request with that commit, or a seat did not answer. " + - "A merge made on the forge (a recovery merge, the operator's own) is read the same way.", - Input: map[string]any{"type": "object", "required": []string{"repository", "merge"}, "properties": map[string]any{ - "repository": map[string]any{"type": "string", "description": "the repository, owner/name, as the forge names it"}, - "merge": map[string]any{"type": "string", "description": "the merge commit, 40 hex digits"}, - "number": map[string]any{"type": "integer", "description": "the pull request's number, when known: the bridge reads that pull request and checks its merge commit"}}}, - Run: func(args map[string]any) (any, error) { - number, _ := args["number"].(float64) - raw, err := merged(str(args, "repository"), str(args, "merge"), int(number)) - if err != nil { - return nil, err - } - return map[string]any{"message": raw}, nil - }}, - {Name: GroupsTool, - Description: "The delivery groups the seat mesh-delivery's verb groups answers (more than one member), each with its members' delivery " + - "state and whether each merged message is written on the forge stream (with the merge commit it named), and whether the " + - "group's end (forge.group.merged) is written: ended, or open with why (gathered, or every message written and its end " + - "not written yet). An open group with every member's message written is " + - "what bridge_group_end_again says the end of. Each group names its last end ask (who, why, when) and the bridge's " + - "answer to it, from the state end-asks. A group's name narrows it to that group; open lists only open ones.", - Input: map[string]any{"type": "object", "properties": map[string]any{ - "group": map[string]any{"type": "string", "description": "only this group, by its id (its branch)"}, - "open": map[string]any{"type": "boolean", "description": "only the groups whose end is not written"}}}, - Run: func(args map[string]any) (any, error) { - rows, unread, err := groupsRead() - if err != nil { - return nil, err - } - only, _ := args["open"].(bool) - group := str(args, "group") - kept := rows[:0] - for _, r := range rows { - if (group != "" && r.Group != group) || (only && !r.Open) { - continue - } - kept = append(kept, r) - } - return map[string]any{"groups": kept, "unread": unread}, nil - }}, - {Name: EndAgainTool, - Description: "Say a delivery group's end (forge.group.merged) again, by hand, for a group whose end the indexer did not take " + - "in (a group bridge_groups names as merged with its end not written, or one the indexer waits for). The bridge " + - "writes it again from the seat mesh-delivery's groups, with the time appended to the message id so the stream does not drop it as " + - "a duplicate; the indexer takes an end for a group it already took in without changing anything. Refused while " + - "a member's merged message is not written. Answers whether it was written.", - Input: map[string]any{"type": "object", "required": []string{"group", "base"}, "properties": map[string]any{ - "group": map[string]any{"type": "string", "description": "the group, by its id (its branch)"}, - "base": map[string]any{"type": "string", "description": "the watched branch its members merged into, whose end is said again"}, - "why": map[string]any{"type": "string", "description": "why its end is said again, in plain words"}}}, - Run: func(args map[string]any) (any, error) { - key, value, err := groups.NewEndAsk(str(args, "group"), str(args, "base"), str(args, "why"), stdio.CallerOf(args), now()) - if err != nil { - return nil, err - } - a, err := ask(key, value) - if err != nil { - return nil, err - } - return map[string]any{"group": a.Group, "base": a.Base, "written": a.Written, "answer": a.Answer, "by": a.By, "why": a.Why}, nil - }}, - } -} - -// MergedOf is the merged message of the merge of repository at commit merge, as the bridge writes it: the pull request's -// number, branch and head through the seat git's verb pull (by number when given, else by the merge commit; a merge made -// on the forge is read the same way), the rest from the forge through trees and files (bridge.MergedMessage), and, for a -// member of a delivery group the seat mesh-delivery names (of), the group and its members as the process sets them. -func MergedOf(groupOf func(id string) ([]seat.Group, error), repository, merge string, number int, trees bridge.Trees, files bridge.Files, forge bridge.Caller) (json.RawMessage, error) { - owner, repo, ok := strings.Cut(repository, "/") - if !ok || owner == "" || repo == "" { - return nil, fmt.Errorf("%q is not owner/name", repository) - } - fp, err := bridge.ForgeMade(context.Background(), forge, owner, repo, number, merge) - if err != nil { - return nil, fmt.Errorf("the forge gives no merged pull request of %s with the commit %s: %w", repository, merge, err) - } - msg, err := bridge.MergedMessage(owner, repo, fp.Number, fp.Branch, fp.Head, merge, fp.Base, trees, files) - if err != nil { - return nil, fmt.Errorf("the merged message of %s at %s cannot be built: %w", repository, merge, err) - } - f, err := forgemsg.DecodeForge(msg.Data) - if err != nil { - return nil, err - } - of, err := groupOf(f.Branch) // the one group the pull request's branch names, never the whole list - if err != nil { - return nil, err - } - if g, ok := groups.Named(of, f.Branch, f.Repository, f.Number); ok { - f.Group, f.Members = g.ID, groups.MembersOf(g) - } - return forgemsg.EncodeForge(f) -} - -// read is the groups the seat mesh-delivery answers with what the bridge kept of them: the merged messages written and -// their ends in performed, and the last ask of each in end-asks with the bridge's answer. -func read(of []seat.Group, performed, asks stdio.KeptState) ([]groups.Row, []string, error) { - pkeys, err := performed.Keys() - if err != nil { - return nil, nil, fmt.Errorf("the bridge's performed state cannot be listed: %w", err) - } - published, ended := map[string]string{}, map[string]string{} - for _, k := range pkeys { - into := ended - switch { - case strings.HasPrefix(k, "published."): - into = published - case !strings.HasPrefix(k, "groups."): - continue - } - if e, err := performed.Get(k); err == nil && e != nil { - into[k] = strings.TrimSpace(string(e.Value)) - } - } - akeys, err := asks.Keys() - if err != nil { - return nil, nil, fmt.Errorf("the bridge's state %s cannot be listed: %w", groups.EndAsksState, err) - } - askRaws := map[string][]byte{} - for _, k := range akeys { - if e, err := asks.Get(k); err == nil && e != nil { - askRaws[k] = e.Value - } - } - rows, unread := groups.Rows(of, published, ended, askRaws) - return rows, unread, nil -} - -// askEnd writes the end-asks value at key and waits at most bound for the bridge's answer. -func askEnd(s stdio.KeptState, module string, bound time.Duration) asker { - wait := askState(s, groups.EndAsksBucket(module), bound, func(v []byte) bool { a, err := groups.DecodeEndAsk(v); return err == nil && a.Done() }) - return func(key string, value []byte) (groups.EndAsk, error) { - v, err := wait(key, value) - if err != nil { - return groups.EndAsk{}, err - } - return groups.DecodeEndAsk(v) } } @@ -252,19 +94,8 @@ func askCaller(_ context.Context, key string, body any) (json.RawMessage, error) func main() { // The module the runtime serves these tools as: the bridge's, whose states they ask (design 47 G A-296). served := os.Getenv(stdio.ServedAs) - performed, asks := stdio.State(groups.PerformedState), stdio.State(groups.EndAsksState) - tools := Tools(askEnd(asks, served, 25*time.Second), time.Now, - func() ([]groups.Row, []string, error) { - of, err := groups.ReadAll(context.Background(), askCaller) - if err != nil { - return nil, nil, err - } - return read(of, performed, asks) - }, - func(repository, merge string, number int) (json.RawMessage, error) { - ctx := context.Background() - return MergedOf(func(id string) ([]seat.Group, error) { return groups.ReadGroup(ctx, askCaller, id) }, repository, merge, number, bridge.TreesVia(ctx, askCaller), bridge.FilesVia(ctx, askCaller), askCaller) - }, askCaller) + performed := stdio.State(groups.PerformedState) + tools := Tools(askCaller) owedAsks := stdio.State(groups.OwedAsksState) recorder := meshhandactsclient.Recorder{Ask: stdio.Ask} tools = append(tools, OwedTools(askState(owedAsks, groups.OwedAsksBucket(served), 25*time.Second, func(v []byte) bool { diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/main_test.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/main_test.go index 7d0523ee..ce28c7ed 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/main_test.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/main_test.go @@ -1,226 +1,21 @@ package main import ( - "bytes" - "context" "encoding/json" "errors" - "strings" "testing" "time" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" - git "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" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" - "mesh-delivery-forge-bridge/internal/bridge" - - "mesh-delivery-forge-bridge/internal/gitfixture" "mesh-delivery-forge-bridge/internal/groups" ) -// 1585/T18, T19 (ADR 0633 §4), acceptance 49: bridge_groups names each group the seat mesh-delivery's verb groups answers -// with its members' state and whether its end is written (open or ended, with why), narrowed by group and by open; -// bridge_group_end_again writes an ask naming the group, who asked and why, and answers what the bridge answered. -func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) { - h1, h2 := strings.Repeat("1", 40), strings.Repeat("2", 40) - c1, c2 := strings.Repeat("c", 40), strings.Repeat("d", 40) - grp := func(id string, a, b int, stateB string) seat.Group { - return seat.Group{ID: id, State: "merging", Members: []seat.Member{{Repository: "novox/a", Number: int64(a), Head: h1, State: "merged", Base: "main"}, - {Repository: "novox/b", Number: int64(b), Head: h2, State: stateB, Base: "main"}}} - } - of := []seat.Group{grp("ended", 1, 2, "merged"), grp("ready", 3, 4, "merged"), grp("gathered", 5, 6, "merging"), - {ID: "solo", Members: []seat.Member{{Repository: "novox/a", Number: 7, Head: h1}}}, grp("", 8, 9, "merged")} - published := map[string]string{} - for _, n := range []int{1, 3, 5} { - published[groups.PublishedKey("novox/a", n)] = c1 - } - for _, n := range []int{2, 4} { - published[groups.PublishedKey("novox/b", n)] = c2 - } - // The state end-asks keeps the last ask of the group ready, answered, and one that does not read. - answeredAsk, _ := json.Marshal(groups.EndAsk{V: 1, Group: "ready", Base: "main", Why: "lost", By: "lead@shanks", At: time.Date(2026, 11, 4, 22, 0, 0, 0, time.UTC), - Written: true, Answer: "the end of the group is written again", Answered: time.Date(2026, 11, 4, 22, 0, 1, 0, time.UTC)}) - asks := map[string][]byte{"ready.main": answeredAsk, "gathered.main": []byte("{")} - var asked groups.EndAsk - var askedKey string - ask := func(key string, value []byte) (groups.EndAsk, error) { - askedKey = key - a, err := groups.DecodeEndAsk(value) - a.Written, a.Answer, a.Answered = true, "the end of the group is written again", time.Now() - asked = a - return a, err - } - tools := Tools(ask, func() time.Time { return time.Date(2026, 11, 4, 23, 0, 0, 0, time.UTC) }, - func() ([]groups.Row, []string, error) { - rows, unread := groups.Rows(of, published, map[string]string{"groups.ended.main": c2}, asks) - return rows, unread, nil - }, nil, nil) - call := func(name string, args map[string]any) any { - for _, tl := range tools { - if tl.Name == name { - out, err := tl.Run(args) - if err != nil { - t.Fatalf("%s: %v", name, err) - } - return out - } - } - t.Fatalf("no tool %s", name) - return nil - } - rowsOf := func(args map[string]any) map[string]groups.Row { - raw, _ := json.Marshal(call(GroupsTool, args)) - var out struct { - Groups []groups.Row `json:"groups"` - Unread []string `json:"unread"` - } - if err := json.Unmarshal(raw, &out); err != nil { - t.Fatal(err) - } - m := map[string]groups.Row{} - for _, r := range out.Groups { - m[r.Group] = r - } - if len(args) == 0 && strings.Join(out.Unread, ", ") != "a group with no id, end-asks.gathered.main" { - t.Errorf("unread = %v, want the group with no id and end-asks.gathered", out.Unread) - } - return m - } - all := rowsOf(map[string]any{}) - if len(all) != 3 || !all["ended"].Ended || all["ended"].EndedWith != c2 { - t.Fatalf("groups = %+v", all) - } - if r := all["ready"]; !r.Open || !strings.Contains(r.Words, "bridge_group_end_again") || !r.Members[1].Published || r.Members[1].Commit != c2 { - t.Errorf("ready = %+v, want open and published with its merge commit, its end the thing to say", r) - } - if a := all["ready"].LastEndAsk; a == nil || a.By != "lead@shanks" || a.Why != "lost" || !a.Written || a.Answer == "" || all["ended"].LastEndAsk != nil { - t.Errorf("ready's last end ask = %+v, ended's %+v: want ready's ask and its answer, none for ended", a, all["ended"].LastEndAsk) - } - if r := all["gathered"]; !r.Open || !strings.Contains(r.Words, "gathered") || r.Members[1].State != "merging" || r.Members[1].Published { - t.Errorf("gathered = %+v", r) - } - if open := rowsOf(map[string]any{"open": true}); len(open) != 2 || open["ended"].Group != "" { - t.Errorf("open = %+v, want ready and gathered", open) - } - if one := rowsOf(map[string]any{"group": "ready"}); len(one) != 1 { - t.Errorf("group ready = %+v", one) - } - out := call(EndAgainTool, map[string]any{"group": "ready", "base": "main", "why": "the indexer never took it in"}).(map[string]any) - if askedKey != "ready.main" || asked.Group != "ready" || asked.Base != "main" || asked.Why != "the indexer never took it in" || out["written"] != true { - t.Errorf("the ask %q %+v, answered %v", askedKey, asked, out) - } - for _, tl := range tools { - if tl.Name == EndAgainTool { - if _, err := tl.Run(map[string]any{}); err == nil { - t.Error("an ask naming no group was taken") - } - if _, err := tl.Run(map[string]any{"group": "ready"}); err == nil { - t.Error("an ask naming no base was taken") - } - } - } - - // §715: a group with members on two bases is two rows, one per base, each with its own members and key. - split := seat.Group{ID: "feat/s", State: "merging", Members: []seat.Member{{Repository: "novox/a", Number: 11, Base: "beta"}, {Repository: "novox/b", Number: 12, Base: "main"}, {Repository: "novox/c", Number: 13, Base: "beta"}}} - if rows, unread := groups.Rows([]seat.Group{split}, nil, nil, nil); len(unread) != 0 || len(rows) != 2 || rows[0].Base != "beta" || len(rows[0].Members) != 2 || rows[1].Base != "main" || len(rows[1].Members) != 1 || rows[0].Key == rows[1].Key { - t.Errorf("rows of a group on two bases = %+v unread %v", rows, unread) - } - - // 1585: bridge_merged_message answers the merged message as the bridge publishes it: for a lone pull request the same bytes - // the event gives through Convert, for a member of a delivery group those with its group and members set as the process - // sets them; a merge the gate does not hold, or a forge that does not answer, is said. - t.Run("bridge_merged_message answers the merged message as published", func(t *testing.T) { - h1, h2 := strings.Repeat("1", 40), strings.Repeat("2", 40) - c1, c2 := strings.Repeat("c", 40), strings.Repeat("d", 40) - trees := func(owner, repo, sha string) (string, map[string]string, error) { - return "root-" + sha[:4], map[string]string{"": "r", "modules/a": "ta-" + sha[:4]}, nil - } - files := func(owner, repo, path, ref string) ([]byte, error) { - if strings.HasSuffix(path, "module.json") { - return []byte(`{"module":"a"}`), nil - } - return nil, bridge.ErrNotFound - } - published := func(repo string, number int, head, branch, merge string) forgemsg.Forge { - msg, err := bridge.MergedMessage("novox", repo, number, branch, head, merge, "main", trees, files) - if err != nil { - t.Fatal(err) - } - f, err := forgemsg.DecodeForge(msg.Data) - if err != nil { - t.Fatal(err) - } - return f - } - // The seat mesh-delivery's groups: feat/g of novox/a#9 and novox/b#7; a lone pull request is in none. - of := []seat.Group{{ID: "fix/x", Members: []seat.Member{{Repository: "novox/a", Number: 9, Head: h2}, {Repository: "novox/b", Number: 7, Head: h1}}}} - for _, c := range []struct { - name string - of []seat.Group - repository string - merge string - number int - api bridge.Caller - want forgemsg.Forge - fail string - }{ - {name: "a lone pull request, by number", repository: "novox/a", merge: c2, number: 9, - api: forgeAnswers(t, 9, "", "merged", "fix/x", h2, c2, "main"), want: published("a", 9, h2, "fix/x", c2)}, - {name: "a lone pull request, by the merge commit", repository: "novox/a", merge: c2, - api: forgeAnswers(t, 0, c2, "merged", "fix/x", h2, c2, "main"), want: published("a", 9, h2, "fix/x", c2)}, - {name: "a member of a group", of: of, repository: "novox/a", merge: c2, - api: forgeAnswers(t, 0, c2, "merged", "fix/x", h2, c2, "main"), want: func() forgemsg.Forge { - f := published("a", 9, h2, "fix/x", c2) - f.Group, f.Members = "fix/x", []meshdeliveryclient.Head{{Repository: "novox/a", Number: 9, Head: h2}, {Repository: "novox/b", Number: 7, Head: h1}} - return f - }()}, - {name: "a forge pull request merged at another commit", repository: "novox/a", merge: c2, number: 9, - api: forgeAnswers(t, 9, "", "merged", "fix/x", h2, c1, "main"), fail: "not at " + c2}, - {name: "a forge pull request not merged", repository: "novox/a", merge: c2, number: 9, - api: forgeAnswers(t, 9, "", "open", "fix/x", h2, "", "main"), fail: "not merged"}, - // design 47 G §714: no filter to the trunk alone; the merged message names the branch it was merged into. - {name: "a forge pull request merged into another branch", repository: "novox/a", merge: c2, number: 9, - api: forgeAnswers(t, 9, "", "merged", "fix/x", h2, c2, "dev"), want: func() forgemsg.Forge { - f := published("a", 9, h2, "fix/x", c2) - f.Base = "dev" - return f - }()}, - {name: "a forge pull request with no head branch", repository: "novox/a", merge: c2, number: 9, - api: forgeAnswers(t, 9, "", "merged", "", h2, c2, "main"), fail: "no head branch"}, - {name: "a seat answer that says an error", repository: "novox/a", merge: c2, number: 9, - api: answering(gitfixture.Given(t, "pull answers an error, never none", "verb:pull.answer")), fail: "holds no pull request"}, - {name: "a seat answer with no pull request", repository: "novox/a", merge: c2, number: 9, - api: answering(json.RawMessage(`{}`)), fail: "carries nothing"}, - {name: "a forge that does not answer", repository: "novox/a", merge: c2, - api: func(context.Context, string, any) (json.RawMessage, error) { - return nil, errors.New("the forge is down") - }, fail: "the forge is down"}, - } { - t.Run(c.name, func(t *testing.T) { - raw, err := MergedOf(func(string) ([]seat.Group, error) { return c.of, nil }, c.repository, c.merge, c.number, trees, files, c.api) - if c.fail != "" { - if err == nil || !strings.Contains(err.Error(), c.fail) { - t.Fatalf("err %v, want %q", err, c.fail) - } - return - } - want, _ := forgemsg.EncodeForge(c.want) - if err != nil || !bytes.Equal(raw, want) { - t.Fatalf("err %v, answer\n%s\nwant the published message\n%s", err, raw, want) - } - }) - } - if _, err := MergedOf(func(string) ([]seat.Group, error) { return nil, nil }, "novox/a", c2, 9, func(string, string, string) (string, map[string]string, error) { - return "", nil, errors.New("the forge does not answer") - }, files, forgeAnswers(t, 9, "", "merged", "fix/x", h2, c2, "main")); err == nil || !strings.Contains(err.Error(), "does not answer") { - t.Errorf("a forge that does not answer: %v", err) - } - }) - - // Design 47 *What the gate owes the forge*: bridge_owed reads performed and asks the backlog; bridge_owed_again asks the - // act again; bridge_owed_drop writes the hand-act log first and drops nothing it could not record. +// Design 47 *What the gate owes the forge*: bridge_owed reads what the bridge performed and its consumer's backlog, +// bridge_owed_again and bridge_owed_drop write an ask and answer what the bridge answered, a drop naming why and +// recorded as a hand act first. +func TestTheBridgeTools(t *testing.T) { t.Run("the owed tools", func(t *testing.T) { at := time.Date(2026, 11, 9, 21, 0, 0, 0, time.UTC) var asked []groups.OwedAsk @@ -236,7 +31,7 @@ func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) { } return json.Marshal(a) } - rows := []OwedRow{{Of: seat.Seat, ID: "a", Outcome: forgemsg.OwedPerformed, At: at}, {Of: "mesh-delivery-checker", ID: "b", Outcome: forgemsg.OwedRefused, At: at}} + rows := []OwedRow{{Of: seat.Seat, ID: "a", Outcome: forgemsg.OwedPerformed, At: at}} var recorded []string recordErr := error(nil) record := func(what, why string) error { recorded = append(recorded, what+": "+why); return recordErr } @@ -269,29 +64,3 @@ func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) { } }) } - -// forgeAnswers is the seat git's verb pull for one pull request, asked by number (when number is given) or by the merge -// commit asked: the seat's own fixture with that pull request's state, branch, head, merge commit and base; any other -// ask is refused as the seat refuses it. -func forgeAnswers(t *testing.T, number int, byMerge, state, branch, head, merge, base string) bridge.Caller { - t.Helper() - var a git.PullAnswer - if err := json.Unmarshal(gitfixture.Given(t, "pull answers", "verb:pull.answer"), &a); err != nil || a.Pull == nil { - t.Fatalf("the seat's pull fixture: %v", err) - } - p := *a.Pull - p.Repository, p.Number, p.State, p.Branch, p.Head, p.MergeCommit, p.Base = "novox/a", 9, state, branch, head, merge, base - answer, _ := json.Marshal(git.PullAnswer{Pull: &p}) - return func(_ context.Context, key string, body any) (json.RawMessage, error) { - in, ok := body.(git.PullInput) - if key != "seat:git.pull" || !ok || in.Repository != "novox/a" || int(in.Number) != number || in.MergeCommit != byMerge { - return json.RawMessage(`{"error":"the forge holds no such pull request"}`), nil - } - return answer, nil - } -} - -// answering is a seat that answers raw to every ask. -func answering(raw json.RawMessage) bridge.Caller { - return func(context.Context, string, any) (json.RawMessage, error) { return raw, nil } -} diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/owed.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/owed.go index 2c7459b0..c615f25d 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/owed.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/owed.go @@ -53,7 +53,7 @@ func performedRows(s stdio.KeptState) ([]OwedRow, error) { func OwedTools(ask func(key string, value []byte) ([]byte, error), performed func() ([]OwedRow, error), record func(what, why string) error, now func() time.Time) []stdio.Tool { str := func(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) } act := map[string]any{ - "of": map[string]any{"type": "string", "description": "the seat that said the act: mesh-delivery (the gate) or mesh-delivery-checker"}, + "of": map[string]any{"type": "string", "description": "the seat that said the act: mesh-delivery (the gate)"}, "id": map[string]any{"type": "string", "description": "the act's id, as owed and owed.ended say it"}} asked := func(kind string, args map[string]any) (groups.OwedAsk, error) { key, value, err := groups.NewOwedAsk(kind, str(args, "of"), str(args, "id"), str(args, "why"), stdio.CallerOf(args), now()) diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound_test.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound_test.go index 5f9791f5..d521d33a 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound_test.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound_test.go @@ -47,7 +47,7 @@ func performedIsOpenedByItsBinding(t *testing.T) { f := &seatFake{} o, pub, kept := newOwing(f) o.waits = binding.Waiting - gate := gitfixture.Seat(t, seat.Seat, "events", "owed is said, a merge", "event:owed")[0] + gate := gitfixture.Seat(t, seat.Seat, "events", "owed is said, a status", "event:owed")[0] if _, err := o.heard(context.Background(), seat.Seat, gate); err == nil || len(f.calls) != 0 || len(pub.data) != 0 || len(kept) != 0 { t.Errorf("unbound: %v, called %v, said %d; want the act left unperformed and unsaid", err, f.calls, len(pub.data)) } diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/call.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/call.go index fae52344..41bb218b 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/call.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/call.go @@ -9,8 +9,6 @@ import ( "time" "github.com/nats-io/nats.go" - - "mesh-delivery-forge-bridge/internal/bridge" ) // callTimeout bounds a call to a tool (mesh-sdk go/process/call.go CallTimeout). @@ -82,13 +80,3 @@ func callSubjects(key, token string) (called, fallback string, err error) { } return "mesh.mod." + module + ".call." + tool + "." + token, "mesh.mod." + module + ".tool." + tool, nil } - -// treesVia asks the forge for a commit's trees through call. -func treesVia(ctx context.Context, call caller) bridge.Trees { - return bridge.TreesVia(ctx, bridge.Caller(call)) -} - -// filesVia asks the forge for a file through call. A file the forge says is not there is bridge.ErrNotFound. -func filesVia(ctx context.Context, call caller) bridge.Files { - return bridge.FilesVia(ctx, bridge.Caller(call)) -} diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/findings.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/findings.go deleted file mode 100644 index 6db37c04..00000000 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/findings.go +++ /dev/null @@ -1,111 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "time" - - "github.com/nats-io/nats.go/jetstream" - - condseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" - stdio "git.novox.be/novox/mesh-sdk/go" - "git.novox.be/novox/mesh-sdk/go/keys" -) - -// The bridge's finding branch-unprotected (design 47 G §17, §387, §529): on every move of a watched branch the bridge reads the -// branch's protection again; a branch whose protection was removed starts nothing and is said as this finding, naming the -// branch, which the condition keeper raises and clears once the protection is back. The findings state is a `requires` -// entry opened by its binding (§577), written through mesh-sdk's held finding writer (§689). -const ( - // findingsLocal is the local name of the state findings in the manifest's `requires`. - findingsLocal = "findings" - // kindBranchUnprotected is the finding's kind. - kindBranchUnprotected = "branch-unprotected" - // findingScope is the finding's scope (design 47 G A-251). - findingScope = "merge" -) - -// findingWriter is the bridge's finding writer: mesh-sdk's held writer of the seat mesh-conditions' generated finding. -type findingWriter interface { - Write(ctx context.Context, f condseat.Finding) error - Clear(ctx context.Context, scope, id, kind, why string) error - Held() []condseat.Finding -} - -// findingState is the state findings, a bound bucket, as the held writer's state. -type findingState struct{ b *boundBucket } - -const findingStateTimeout = 10 * time.Second - -func (s findingState) Get(key string) (*stdio.StateEntry, error) { - ctx, cancel := context.WithTimeout(context.Background(), findingStateTimeout) - defer cancel() - e, err := s.b.Get(ctx, key) - if errors.Is(err, jetstream.ErrKeyNotFound) { - return nil, nil - } - if err != nil { - return nil, err - } - return &stdio.StateEntry{Key: key, Value: json.RawMessage(e.Value()), Revision: e.Revision()}, nil -} - -func (s findingState) Put(key string, value any) (uint64, error) { - data, err := json.Marshal(value) - if err != nil { - return 0, err - } - ctx, cancel := context.WithTimeout(context.Background(), findingStateTimeout) - defer cancel() - return s.b.Put(ctx, key, data) -} - -func (s findingState) Delete(key string) error { - ctx, cancel := context.WithTimeout(context.Background(), findingStateTimeout) - defer cancel() - return s.b.Delete(ctx, key) -} - -func (s findingState) Keys() ([]string, error) { - ctx, cancel := context.WithTimeout(context.Background(), findingStateTimeout) - defer cancel() - return s.b.Keys(ctx) -} - -// unprotectedID is the finding's id: `//` escaped as a key's part (A-251): a bus key holds no `~`, and EscapeName keeps `/`. -func unprotectedID(repository, branch string) string { - return keys.EscapeName(repository + "/" + branch) -} - -// protection says what a read of a watched branch's protection means for its finding: a branch not protected is written as -// the finding branch-unprotected (the writer says it only when it is new or changed), a protected one clears it when it is held. -func (mv *mover) protection(ctx context.Context, repository, branch string, protected bool, head string) { - if mv.findings == nil { - return - } - id := unprotectedID(repository, branch) - if protected { - for _, f := range mv.findings.Held() { - if f.Scope == findingScope && f.ID == id && f.Kind == kindBranchUnprotected { - if err := mv.findings.Clear(ctx, findingScope, id, kindBranchUnprotected, "the protection of "+branch+" is back"); err != nil { - mv.logger.Printf("%s of %s: the finding %s is not cleared: %v", branch, repository, kindBranchUnprotected, err) - } - } - } - return - } - now := time.Now() - if mv.now != nil { - now = mv.now() - } - now = now.UTC() - err := mv.findings.Write(ctx, condseat.Finding{Scope: findingScope, ID: id, Kind: kindBranchUnprotected, Severity: condseat.FindingSeverityWarning, - Headline: "the branch " + branch + " of " + repository + " is not protected", - Summary: fmt.Sprintf("the branch %s of %s moved to %s while its protection is removed: no delivering pipeline starts for it until the protection is back", branch, repository, head), - Source: "git.branch", Observer: forgeBridgeModule(), At: now, Subject: condseat.Subject{Scope: findingScope, ID: id}}) - if err != nil { - mv.logger.Printf("%s of %s: the finding %s is not written: %v", branch, repository, kindBranchUnprotected, err) - } -} diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/gate_test.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/gate_test.go index 20c20e48..db0c6332 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/gate_test.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/gate_test.go @@ -10,9 +10,7 @@ import ( "testing" "time" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" "mesh-delivery-forge-bridge/internal/gitfixture" @@ -61,13 +59,14 @@ func newOwing(f *seatFake) (*owing, *fakePub, memPerformed) { } // Design 47 *What the gate owes the forge*: an owed act is the seat git's verb under its id, its outcome said as -// owed.ended on the stream forge before it is kept in performed, an act it cannot read said undecodable; and at its start -// the bridge performs from the seat's groups every act said and not answered. +// owed.ended on the stream forge before it is kept in performed, an act it cannot read said undecodable (a merge among +// them: the bridge performs no merge, design 47 H §764). func TestWhatIsOwedIsPerformedSaidAndKept(t *testing.T) { t.Run("the names are the manifest's", theNamesAreTheManifests) t.Run("performed is opened by its binding", performedIsOpenedByItsBinding) gate := func(name string) []byte { return gitfixture.Seat(t, seat.Seat, "events", name, "event:owed")[0] } answer := func(verb, name string) json.RawMessage { return gitfixture.Given(t, name, "verb:"+verb+".answer") } + statusOK := map[string]json.RawMessage{"seat:git.status": answer("status", "status answers")} for _, c := range []struct { name, of string data []byte @@ -78,19 +77,16 @@ func TestWhatIsOwedIsPerformedSaidAndKept(t *testing.T) { kept bool err string }{ - {"a merge performed", seat.Seat, gate("owed is said, a merge"), - map[string]json.RawMessage{"seat:git.merge-pull": answer("merge-pull", "merge-pull answers")}, nil, - "seat:git.merge-pull", forgemsg.OwedPerformed, true, ""}, + {"a status performed", seat.Seat, gate("owed is said, a status"), statusOK, nil, "seat:git.status", forgemsg.OwedPerformed, true, ""}, {"a status refused", seat.Seat, gate("owed is said, a status"), map[string]json.RawMessage{"seat:git.status": answer("status", "status answers an error, never none")}, nil, "seat:git.status", forgemsg.OwedRefused, true, ""}, - {"the checker's status not answered", checker.Seat, gitfixture.Seat(t, checker.Seat, "events", "owed is said", "event:owed")[0], - nil, nil, "seat:git.status", forgemsg.OwedCouldNotBePerformed, true, ""}, + {"a status not answered", seat.Seat, gate("owed is said, a status"), nil, nil, "seat:git.status", forgemsg.OwedCouldNotBePerformed, true, ""}, + {"a merge, which the bridge does not perform", seat.Seat, gate("owed is said, a merge"), nil, nil, "", forgemsg.OwedUndecodable, true, ""}, {"an act it cannot read", seat.Seat, []byte(`{`), nil, nil, "", forgemsg.OwedUndecodable, false, ""}, - {"a seat that says no owed act", "mesh-wants", gate("owed is said, a merge"), nil, nil, "", forgemsg.OwedUndecodable, false, ""}, - {"the stream forge refusing the outcome", seat.Seat, gate("owed is said, a merge"), - map[string]json.RawMessage{"seat:git.merge-pull": answer("merge-pull", "merge-pull answers")}, errors.New("no responders"), - "seat:git.merge-pull", "", false, "could not be said"}, + {"a seat that says no owed act", "mesh-delivery-checker", gate("owed is said, a status"), nil, nil, "", forgemsg.OwedUndecodable, false, ""}, + {"the stream forge refusing the outcome", seat.Seat, gate("owed is said, a status"), statusOK, errors.New("no responders"), + "seat:git.status", "", false, "could not be said"}, } { f := &seatFake{answers: c.answers} o, pub, kept := newOwing(f) @@ -122,51 +118,10 @@ func TestWhatIsOwedIsPerformedSaidAndKept(t *testing.T) { } } - // Design 47 *The switch carries what is owed*: an act the gate switched from its old state is performed only when the old - // bridge's own bookkeeping does not name its key at its revision or later; one it names is said performed, not done. - t.Run("a switched act the old bridge performed", func(t *testing.T) { - var owed map[string]any - if err := json.Unmarshal(gate("owed is said, a merge"), &owed); err != nil { - t.Fatal(err) - } - id, err := meshdeliveryclient.SwitchedActID(meshdeliveryclient.SwitchedMerges, "feat~x", 7) - if err != nil { - t.Fatal(err) - } - owed["id"] = id - data, _ := json.Marshal(owed) - for _, c := range []struct { - name string - kept string // the old bridge's merges.feat~x, "" for none - called bool - }{ - {"kept at its revision", "7", false}, - {"kept at a later revision", "9", false}, - {"kept at an earlier revision", "6", true}, - {"not kept", "", true}, - } { - f := &seatFake{answers: map[string]json.RawMessage{"seat:git.merge-pull": answer("merge-pull", "merge-pull answers")}} - o, pub, kept := newOwing(f) - if c.kept != "" { - kept["merges.feat~x"] = []byte(c.kept) - } - if _, err := o.heard(context.Background(), seat.Seat, data); err != nil || len(pub.data) != 1 { - t.Fatalf("%s: %v, said %d", c.name, err, len(pub.data)) - } - e, _ := forgemsg.DecodeOwedEnded(pub.data[0]) - if (len(f.calls) == 1) != c.called || e.Outcome != forgemsg.OwedPerformed || e.ID != id { - t.Errorf("%s: called %v, said %+v; want called %v and performed", c.name, f.calls, e, c.called) - } - if _, ok := kept[performedKey(seat.Seat, id)]; !ok { - t.Errorf("%s: the outcome is not kept under the act's id", c.name) - } - } - }) - t.Run("heard from a seat's stream owed", func(t *testing.T) { - f := &seatFake{answers: map[string]json.RawMessage{"seat:git.merge-pull": answer("merge-pull", "merge-pull answers")}} + f := &seatFake{answers: statusOK} o, pub, _ := newOwing(f) - m := &fakeMsg{subject: "mesh.seat.mesh-delivery.event.owed.novox/mesh-sdk.178", data: gate("owed is said, a merge")} + m := &fakeMsg{subject: "mesh.seat.mesh-delivery.event.owed.novox/mesh-sdk.178", data: gate("owed is said, a status")} o.hear(context.Background(), seat.Seat, m) again := &fakeMsg{subject: m.subject, data: m.data} o.hear(context.Background(), seat.Seat, again) @@ -175,7 +130,11 @@ func TestWhatIsOwedIsPerformedSaidAndKept(t *testing.T) { m.acked, again.acked, f.calls, len(pub.data)) } pub.err = errors.New("no responders") - lost := &fakeMsg{subject: m.subject, data: gate("owed is said, a status")} + var other map[string]any + _ = json.Unmarshal(gate("owed is said, a status"), &other) + other["id"] = "status.novox/mesh-sdk#178.aaaaaaaaaaaa.mesh/other" + otherData, _ := json.Marshal(other) + lost := &fakeMsg{subject: m.subject, data: otherData} o.hear(context.Background(), seat.Seat, lost) if lost.acked != 0 || len(lost.nak) != 1 { t.Errorf("an outcome not said: acked %d, nak %v; want it heard again", lost.acked, lost.nak) @@ -183,12 +142,10 @@ func TestWhatIsOwedIsPerformedSaidAndKept(t *testing.T) { }) t.Run("asked again, dropped, and its backlog read by hand", func(t *testing.T) { - groupsAnswer := gitfixture.Seat(t, seat.Seat, "verbs", "groups answers a group with its members and acts", "verb:groups.answer")[0] - f := &seatFake{answers: map[string]json.RawMessage{"seat:git.merge-pull": answer("merge-pull", "merge-pull answers"), - "seat:mesh-delivery.groups": groupsAnswer, "seat:git.status": answer("status", "status answers")}} + f := &seatFake{answers: statusOK} o, pub, kept := newOwing(f) - merge := "merge.feat=47-G.0123456789ab.novox/mesh-sdk#178" - if _, err := o.heard(context.Background(), seat.Seat, gate("owed is said, a merge")); err != nil { + status := "status.novox/mesh-sdk#178.aaaaaaaaaaaa.mesh/merge-gate" + if _, err := o.heard(context.Background(), seat.Seat, gate("owed is said, a status")); err != nil { t.Fatal(err) } backlog := func(context.Context) []groups.Backlog { return []groups.Backlog{{Stream: seat.StreamOwed, Waiting: 2}} } @@ -198,12 +155,10 @@ func TestWhatIsOwedIsPerformedSaidAndKept(t *testing.T) { calls, said int outcome, answers string }{ - {"again, from performed", groups.OwedAsk{Kind: groups.OwedAgain, Of: seat.Seat, ID: merge}, 2, 2, forgemsg.OwedPerformed, "performed"}, - // The fixture's status is in the groups only: the bridge never heard it. - {"again, from the groups", groups.OwedAsk{Kind: groups.OwedAgain, Of: seat.Seat, ID: "status.novox/mesh-sdk#178.aaaaaaaaaaaa.mesh/merge-gate"}, 4, 3, forgemsg.OwedPerformed, "performed"}, - {"dropped", groups.OwedAsk{Kind: groups.OwedDrop, Of: seat.Seat, ID: merge, Why: "merged by hand", By: "lead@shanks"}, 4, 4, forgemsg.OwedDropped, "merged by hand"}, - {"again, an act nobody holds", groups.OwedAsk{Kind: groups.OwedAgain, Of: checker.Seat, ID: "x"}, 4, 4, "", "checker says"}, - {"the backlog", groups.OwedAsk{Kind: groups.OwedBacklog}, 4, 4, "", "backlog"}, + {"again, from performed", groups.OwedAsk{Kind: groups.OwedAgain, Of: seat.Seat, ID: status}, 2, 2, forgemsg.OwedPerformed, "performed"}, + {"dropped", groups.OwedAsk{Kind: groups.OwedDrop, Of: seat.Seat, ID: status, Why: "set by hand", By: "lead@shanks"}, 2, 3, forgemsg.OwedDropped, "set by hand"}, + {"again, an act nobody holds", groups.OwedAsk{Kind: groups.OwedAgain, Of: seat.Seat, ID: "x"}, 2, 3, "", "holds no owed act"}, + {"the backlog", groups.OwedAsk{Kind: groups.OwedBacklog}, 2, 3, "", "backlog"}, } { a := o.answer(context.Background(), c.ask, backlog) if !a.Done() || a.Outcome != c.outcome || !strings.Contains(a.Answer, c.answers) || len(f.calls) != c.calls || len(pub.data) != c.said { @@ -214,42 +169,8 @@ func TestWhatIsOwedIsPerformedSaidAndKept(t *testing.T) { } } var p Performed - if err := json.Unmarshal(kept[performedKey(seat.Seat, merge)], &p); err != nil || p.Ended.Outcome != forgemsg.OwedDropped { + if err := json.Unmarshal(kept[performedKey(seat.Seat, status)], &p); err != nil || p.Ended.Outcome != forgemsg.OwedDropped { t.Errorf("performed after the drop: %+v %v", p, err) } }) - - t.Run("caught up from the groups", func(t *testing.T) { - groups := gitfixture.Seat(t, seat.Seat, "verbs", "groups answers a group with its members and acts", "verb:groups.answer")[0] - f := &seatFake{answers: map[string]json.RawMessage{"seat:mesh-delivery.groups": groups, - "seat:git.status": answer("status", "status answers")}} - o, pub, kept := newOwing(f) - // The fixture's merge has its outcome recorded; its status is said and unanswered. - if n, err := o.catchUp(context.Background()); err != nil || n != 1 || len(pub.data) != 1 || f.calls[1] != "seat:git.status" { - t.Fatalf("catch-up: %d %v, called %v; want the one unanswered status performed", n, err, f.calls) - } - if n, err := o.catchUp(context.Background()); err != nil || n != 0 || len(f.calls) != 3 { - t.Errorf("catch-up again: %d %v, called %v; want nothing performed that performed holds", n, err, f.calls) - } - if len(kept) != 1 { - t.Errorf("kept %v, want the status", kept) - } - // The seat answers a group it does not know with no groups, never an error (W11.10): nothing is performed and - // nothing fails; a groups the seat leaves unanswered is the catch-up's error. - none := gitfixture.Seat(t, seat.Seat, "verbs", "groups answers a group it does not know with no groups", "verb:groups.answer")[0] - for _, row := range []struct { - answer json.RawMessage - fails bool - }{{none, false}, {nil, true}} { - f := &seatFake{answers: map[string]json.RawMessage{}} - if row.answer != nil { - f.answers["seat:mesh-delivery.groups"] = row.answer - } - o, pub, _ := newOwing(f) - n, err := o.catchUp(context.Background()) - if (err != nil) != row.fails || n != 0 || len(pub.data) != 0 { - t.Errorf("catch-up from %s: %d performed, %v; want none performed, failing %v", row.answer, n, err, row.fails) - } - } - }) } diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/groupask.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/groupask.go deleted file mode 100644 index 88158e91..00000000 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/groupask.go +++ /dev/null @@ -1,67 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "errors" - "time" - - "github.com/nats-io/nats.go/jetstream" - - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - - "mesh-delivery-forge-bridge/internal/groups" -) - -// endAsked answers one value of the state end-asks (the tool bridge_group_end_again, ADR 0633 §4): a value not yet answered -// says the group's end again from the seat's groups, with the ask's time appended to the message id so the stream's duplicate -// window does not drop it, and is written back by compare-and-set at its revision with whether it was written. A value -// answered already (this bridge's own write) is left. -func (g *grouper) endAsked(ctx context.Context, kv jetstream.KeyValue, key string, rev uint64, value []byte) { - a, err := groups.DecodeEndAsk(value) - if err != nil { - g.logger.Printf("%s.%s holds what is not an end ask, left: %v", groups.EndAsksState, key, err) - return - } - if a.Done() { - return - } - a = g.answerEnd(ctx, a, time.Now()) - g.logger.Printf("the end of the group %q said again by %s (%s): %s", a.Group, a.By, a.Why, a.Answer) - data, err := json.Marshal(a) - if err != nil { - return - } - if _, err := kv.Update(ctx, key, data, rev); err != nil && !errors.Is(err, jetstream.ErrKeyExists) && !errors.Is(err, jetstream.ErrKeyRevisionMismatch) { - g.logger.Printf("%s.%s: the answer could not be written: %v", groups.EndAsksState, key, err) - } -} - -// answerEnd says the group's end again for an ask and returns the ask with its answer. -func (g *grouper) answerEnd(ctx context.Context, a groups.EndAsk, at time.Time) groups.EndAsk { - a.Answered = at.UTC() - if g.groups == nil { - a.Answer = "the bridge reads no groups" - return a - } - of, err := g.groups(ctx) - if err != nil { - a.Answer = "the groups cannot be read: " + err.Error() - return a - } - m, ok := seat.Group{}, false - for _, c := range of { - if c.ID == a.Group { - m, ok = c, true - } - } - switch { - case !ok || len(m.Members) < 2: - a.Answer = "the seat " + seat.Seat + " holds no group " + a.Group + " of more than one member" - case g.end(ctx, m, a.Base, groups.AgainStamp(a.At)): - a.Written, a.Answer = true, "the end of the group is written again" - default: - a.Answer = "the end on " + a.Base + " is not written: a member's merged message is not written, or FORGE refused it (the bridge's journal says which)" - } - return a -} diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/groups.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/groups.go deleted file mode 100644 index e5aba649..00000000 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/groups.go +++ /dev/null @@ -1,148 +0,0 @@ -package main - -import ( - "context" - "log" - - "github.com/nats-io/nats.go/jetstream" - - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" - - groupkeys "mesh-delivery-forge-bridge/internal/groups" -) - -// A delivery group's merges reach the indexer together (novox/hq ADR 0633 §4): the bridge sets the group on each -// member's merged message, from the seat mesh-delivery's verb groups (design 47 *A group's merges are taken in together*; -// it reads no state of the gate's), and once every member's merged message is written on FORGE it says the group's end, -// forge.group.merged, once, after the last of them. -// -// What it keeps of it is in its state performed: `published.` for each member's merged message written, its value -// the merge commit it named (the hash is the first 16 hex digits of the sha256 of `#`), and -// `groups.` once the group's end is written, its value the last member's merge commit. A group whose end is not -// written yet is open (bridge_groups reads them). A group gathers per watched branch: its end is said once per base, from -// the members whose base the seat says is that branch, and its key is `groups..` (design 47 G §715). - -// groupsRead is every delivery group the seat answers. -type groupsRead func(ctx context.Context) ([]seat.Group, error) - -// strs is performed as string values by key: the grouper's `published.` and `groups.` keys. -type strs interface { - Get(ctx context.Context, key string) (value string, found bool, err error) - Put(ctx context.Context, key, value string) error -} - -// grouper sets a member's group and says a group's end. -type grouper struct { - groups groupsRead - send func(ctx context.Context, subject string, data []byte, id string) error // writes on FORGE with the message id - done strs - logger *log.Logger -} - -// sendVia writes on FORGE through pub, with the message id. -func sendVia(pub publisher) func(context.Context, string, []byte, string) error { - return func(ctx context.Context, subject string, data []byte, id string) error { - _, err := pub.Publish(ctx, subject, data, jetstream.WithMsgID(id)) - return err - } -} - -// groupsVia reads the groups through the seat mesh-delivery's verb groups. -func groupsVia(call caller) groupsRead { - return func(ctx context.Context) ([]seat.Group, error) { return groupkeys.Read(ctx, call) } -} - -// groupOf is the group a merged message's branch names, when the seat names the message's pull request among its -// members and the group has more than one: a lone pull request is no group. -func (g *grouper) groupOf(ctx context.Context, f forgemsg.Forge) (seat.Group, bool) { - if g == nil || g.groups == nil || f.Branch == "" { - return seat.Group{}, false - } - of, err := g.groups(ctx) - if err != nil { - g.logger.Printf("%s#%d: the groups cannot be read, so its merged message names no group: %v", f.Repository, f.Number, err) - return seat.Group{}, false - } - return groupkeys.Named(of, f.Branch, f.Repository, f.Number) -} - -// member sets the group and its members on a merged message's body (ADR 0633 §4); any other body, and a merge of no -// group, is left as it is. -func (g *grouper) member(ctx context.Context, data []byte) ([]byte, forgemsg.Forge, bool) { - f, err := forgemsg.DecodeForge(data) - if err != nil || f.Kind != forgemsg.ForgeMerged { - return data, f, false - } - m, ok := g.groupOf(ctx, f) - if !ok { - return data, f, false - } - f.Group, f.Members = m.ID, groupkeys.MembersOf(m) - out, err := forgemsg.EncodeForge(f) - if err != nil { - g.logger.Printf("%s#%d: its merged message cannot name the group %q, written without: %v", f.Repository, f.Number, m.ID, err) - return data, f, false - } - return out, f, true -} - -// published keeps that a member's merged message is written, and says the group's end when it was the last. -func (g *grouper) published(ctx context.Context, f forgemsg.Forge) { - if err := g.done.Put(ctx, groupkeys.PublishedKey(f.Repository, f.Number), f.Merge); err != nil { - g.logger.Printf("%s#%d: that its merged message is written is not kept, so the group's end waits for bridge_group_end_again: %v", f.Repository, f.Number, err) - return - } - if m, ok := g.groupOf(ctx, f); ok { - g.end(ctx, m, f.Base, "") - } -} - -// end says the group's end (forge.group.merged) once every member's merged message is written, after the last of them; -// never on a refusal (ADR 0441 §3: a held group merges on from it), since a refused member's message is never written. It -// is written once per group and base: `groups..` in performed says it was. again says it is written again by hand -// (bridge_group_end_again), with the time appended to the message id, so the duplicate window does not drop it. -func (g *grouper) end(ctx context.Context, m seat.Group, base, again string) bool { - if len(m.Members) < 2 { - return false - } - onBase := groupkeys.OnBase(m, base) - if len(onBase) == 0 { - g.logger.Printf("the group %q has no member on %q, so no end is said on it", m.ID, base) - return false - } - key, err := groupkeys.Key(m.ID, base) - if err != nil { - g.logger.Printf("the group %q cannot be kept, so its end is not said: %v", m.ID, err) - return false - } - if again == "" { - if _, seen, err := g.done.Get(ctx, key); err == nil && seen { - return false - } - } - // The members in the group's order, which the gate merges them in (ADR 0441 §5), so the last commit names the end. - end := forgemsg.ForgeGroupEnd{Group: m.ID, Base: base} - for _, member := range onBase { - merge, ok, err := g.done.Get(ctx, groupkeys.PublishedKey(member.Repository, int(member.Number))) - if err != nil || !ok || merge == "" { - return false // its merged message is not written yet: said when it is (published) - } - end.Members = append(end.Members, forgemsg.GroupMerge{Repository: member.Repository, Number: int(member.Number), Merge: merge}) - } - data, err := forgemsg.EncodeForgeGroupEnd(end) - if err != nil { - g.logger.Printf("the end of the group %q cannot be written: %v", m.ID, err) - return false - } - last := end.Members[len(end.Members)-1].Merge - if err := g.send(ctx, forgeSubject(forgemsg.ForgeGroupEndEvent), data, groupkeys.EndID(m.ID, base, last, again)); err != nil { - g.logger.Printf("the end of the group %q is not written on FORGE, written by bridge_group_end_again: %v", m.ID, err) - return false - } - if err := g.done.Put(ctx, key, last); err != nil { - g.logger.Printf("the end of the group %q is written and not kept: the stream's duplicate window drops a second one: %v", m.ID, err) - } - g.logger.Printf("the end of the group %q on %q is written: %d members merged, the last %s", m.ID, base, len(end.Members), last) - return true -} diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/groups_test.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/groups_test.go deleted file mode 100644 index ad922071..00000000 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/groups_test.go +++ /dev/null @@ -1,125 +0,0 @@ -package main - -import ( - "bytes" - "context" - "errors" - "log" - "strings" - "testing" - "time" - - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" - - groupkeys "mesh-delivery-forge-bridge/internal/groups" -) - -// idPub records the subjects, bodies and message ids it is handed. -type idPub struct { - subjects, ids []string - data [][]byte -} - -func (p *idPub) send(_ context.Context, subject string, data []byte, id string) error { - p.subjects, p.data, p.ids = append(p.subjects, subject), append(p.data, data), append(p.ids, id) - return nil -} - -// 1585/T16, T17, T19 (ADR 0633 §4), acceptance 49: a member's merged message names its group from the seat -// mesh-delivery's verb groups, the group's end is said once after the last member's merged message, and said again by hand. -func TestAGroupsMembersNameItAndItsEndIsSaidOnce(t *testing.T) { - ctx := context.Background() - h1, h2 := strings.Repeat("1", 40), strings.Repeat("2", 40) - c1, c2 := strings.Repeat("c", 40), strings.Repeat("d", 40) - group := seat.Group{ID: "feat/take-in", State: "merging", Members: []seat.Member{ - {Repository: "novox/mesh-sdk", Number: 37, Head: h1, State: "merged", Base: "beta"}, {Repository: "novox/mesh-delivery", Number: 9, Head: h2, State: "merging", Base: "beta"}, - {Repository: "novox/mesh-tools", Number: 5, Head: h2, State: "merging", Base: "main"}}} - var of []seat.Group - var read error - var logs bytes.Buffer - pub, done := &idPub{}, memStrs{} - g := &grouper{send: pub.send, done: done, logger: log.New(&logs, "", 0), - groups: func(context.Context) ([]seat.Group, error) { return of, read }} - mergedOf := func(repo string, number int, head, commit string) []byte { - data, err := forgemsg.EncodeForge(forgemsg.Forge{Kind: forgemsg.ForgeMerged, Repository: repo, Branch: "feat/take-in", Base: "beta", Number: number, Head: head, Merge: commit, Root: "r"}) - if err != nil { - t.Fatal(err) - } - return data - } - // A lone pull request's merged message carries no group, and neither does one whose groups cannot be read. - of = []seat.Group{group, {ID: "solo", Members: []seat.Member{{Repository: "novox/hq", Number: 4, Head: h1}}}} - lone, _ := forgemsg.EncodeForge(forgemsg.Forge{Kind: forgemsg.ForgeMerged, Repository: "novox/hq", Branch: "solo", Number: 4, Head: h1, Merge: c1, Root: "r"}) - if _, f, ok := g.member(ctx, lone); ok || f.Group != "" { - t.Errorf("a lone pull request names the group %q", f.Group) - } - read = errors.New("the seat did not answer") - if _, f, ok := g.member(ctx, mergedOf("novox/mesh-sdk", 37, h1, c1)); ok || f.Group != "" || !strings.Contains(logs.String(), "the seat did not answer") { - t.Errorf("a group read that failed names the group %q: %s", f.Group, logs.String()) - } - read = nil - // The first member: its message names the group and both members in the group's order; the group is not ended. - data, f, ok := g.member(ctx, mergedOf("novox/mesh-sdk", 37, h1, c1)) - if back, err := forgemsg.DecodeForge(data); !ok || err != nil || back.Group != "feat/take-in" || len(back.Members) != 3 || back.Members[0].Number != 37 { - t.Fatalf("a member's message: %+v %v", back, err) - } - g.published(ctx, f) - if len(pub.subjects) != 0 || done[groupkeys.PublishedKey("novox/mesh-sdk", 37)] != c1 { - t.Fatalf("after the first member: ends %v, performed %v", pub.subjects, done) - } - // The last member's merged message ends the group; a second merged message of it says no second end. - _, f, _ = g.member(ctx, mergedOf("novox/mesh-delivery", 9, h2, c2)) - g.published(ctx, f) - g.published(ctx, f) - if len(pub.subjects) != 1 || pub.subjects[0] != "mesh.stream.mesh-delivery-forge-bridge.event.forge.group.merged" || pub.ids[0] != "group-end.feat/take-in.beta."+c2 { - t.Fatalf("ends %v ids %v, want one forge.group.merged with id group-end.feat/take-in.beta.%s", pub.subjects, pub.ids, c2) - } - end, err := forgemsg.DecodeForgeGroupEnd(pub.data[0]) - if err != nil || end.Base != "beta" || len(end.Members) != 2 || end.Members[0].Merge != c1 || end.Members[1].Merge != c2 { - t.Errorf("the end = %+v %v, want the two members on beta in the group's order", end, err) - } - if key, _ := groupkeys.Key("feat/take-in", "beta"); done[key] != c2 { - t.Errorf("performed %v: want groups.feat~take-in kept with the last merge", done) - } - // §715: the member on main is on another base, and its merged message ends the group on main alone, once. - done[groupkeys.PublishedKey("novox/mesh-tools", 5)] = c1 - g.published(ctx, forgemsg.Forge{Kind: forgemsg.ForgeMerged, Repository: "novox/mesh-tools", Branch: "feat/take-in", Base: "main", Number: 5, Merge: c1}) - g.published(ctx, forgemsg.Forge{Kind: forgemsg.ForgeMerged, Repository: "novox/mesh-tools", Branch: "feat/take-in", Base: "main", Number: 5, Merge: c1}) - if mainEnd, err := forgemsg.DecodeForgeGroupEnd(pub.data[len(pub.data)-1]); len(pub.subjects) != 2 || err != nil || mainEnd.Base != "main" || len(mainEnd.Members) != 1 || mainEnd.Members[0].Number != 5 { - t.Errorf("ends %d, the last %+v %v: want one more end, on main, of the member on main alone", len(pub.subjects), mainEnd, err) - } - // 1585/T19: an ask to say a group's end again is answered from the seat's groups, with the ask's time on the message id; - // not written while a member's merged message is not written, with the reason; not for a group the seat does not hold, - // nor when the groups cannot be read. - ask := groupkeys.EndAsk{V: 1, Group: "feat/take-in", Base: "beta", By: "operator@shanks", Why: "the indexer never took it in", At: time.Date(2026, 11, 4, 23, 0, 0, 123, time.UTC)} - for _, tc := range []struct { - name string - of []seat.Group - read error - published bool - written bool - answer string - }{ - {"every message written: written again", []seat.Group{group}, nil, true, true, "written again"}, - {"a base no member is on", []seat.Group{{ID: group.ID, Members: []seat.Member{group.Members[2], {Repository: "novox/mesh-sdk", Number: 37, Base: "main"}}}}, nil, true, false, "not written"}, - {"a member's message not written", []seat.Group{group}, nil, false, false, "not written"}, - {"no such group at the seat", []seat.Group{{ID: "other", Members: group.Members}}, nil, true, false, "holds no group"}, - {"the groups cannot be read", nil, errors.New("no answer"), true, false, "cannot be read"}, - } { - pub, done := &idPub{}, memStrs{} - if tc.published { - done[groupkeys.PublishedKey("novox/mesh-sdk", 37)], done[groupkeys.PublishedKey("novox/mesh-delivery", 9)] = c1, c2 - done[groupkeys.PublishedKey("novox/mesh-tools", 5)] = c1 - } - g := &grouper{send: pub.send, done: done, logger: log.New(&bytes.Buffer{}, "", 0), - groups: func(context.Context) ([]seat.Group, error) { return tc.of, tc.read }} - got := g.answerEnd(ctx, ask, ask.At.Add(time.Second)) - if got.Written != tc.written || !strings.Contains(got.Answer, tc.answer) || !got.Done() || len(pub.ids) != map[bool]int{true: 1}[tc.written] { - t.Errorf("%s: %+v, ids %v", tc.name, got, pub.ids) - } - if tc.written && pub.ids[0] != groupkeys.EndID("feat/take-in", "beta", c2, groupkeys.AgainStamp(ask.At)) { - t.Errorf("%s: the id %s does not carry the ask's time", tc.name, pub.ids[0]) - } - } -} diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/held.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/held.go index f71b18f6..5ace2802 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/held.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/held.go @@ -3,14 +3,26 @@ package main import ( "context" "errors" + "time" "github.com/nats-io/nats.go/jetstream" ) // headsBucket is the module's state bucket heads, `_`, module its credential's (design 47 G A-296): the head -// last written on FORGE for each pull request (manifest state entry heads, novox/hq ADR 0443 §5). +// held for each watched branch, under `branch...` (design 47 H §739, A-236). func headsBucket(module string) string { return module + "_heads" } +// headStore is the state heads as the mover reads and writes it. +type headStore interface { + // Get is the head kept under the key; found is false when there is none. + Get(ctx context.Context, key string) (head string, found bool, err error) + Put(ctx context.Context, key, head string) error +} + +// handleDeadline bounds the handling of one heard message: its calls and every read and write of the state and the +// stream forge. +const handleDeadline = 30 * time.Second + // kvHeads is the bucket heads as the bridge's headStore. type kvHeads struct{ kv keptStore } @@ -29,5 +41,3 @@ func (s kvHeads) Put(ctx context.Context, key, head string) error { _, err := s.kv.PutString(ctx, key, head) return err } - -func (s kvHeads) Delete(ctx context.Context, key string) error { return s.kv.Delete(ctx, key) } diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/kept.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/kept.go deleted file mode 100644 index a084e5cc..00000000 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/kept.go +++ /dev/null @@ -1,79 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "log" - "regexp" - "sync" - "time" - - "github.com/nats-io/nats.go/jetstream" - - "mesh-delivery-forge-bridge/internal/bridge" -) - -// inputsBucket is the module's state bucket inputs (manifest state entry inputs): what the bridge read of a module -// directory, its manifest and lock-file hashes, by the directory's git tree hash, so a merge reads the forge only for the -// directories it changed (issue 1126; internal/bridge kept.go). A tree hash names its bytes, so a value is never stale; -// a merge adds a value only for each directory it changed (a few kilobytes each). -func inputsBucket(module string) string { return module + "_inputs" } - -// keptTimeout bounds one read or write of the bucket: the bucket is a saving, never a reason to fail a merge. -const keptTimeout = 2 * time.Second - -var treeHash = regexp.MustCompile(`^[0-9a-f]{40}$|^[0-9a-f]{64}$`) - -// kept is the bridge's Kept: in memory always, and in the bucket inputs when it is on the bus, so the first merge after a -// start reads only what changed too. A value the bucket cannot give or take is read from the forge, and said. -type kept struct { - kv jetstream.KeyValue // nil when the bucket is not on the bus - logger *log.Logger - mu sync.Mutex - mem map[string]bridge.Inputs -} - -func newKept(kv jetstream.KeyValue, logger *log.Logger) *kept { - return &kept{kv: kv, logger: logger, mem: map[string]bridge.Inputs{}} -} - -func (k *kept) Get(tree string) (bridge.Inputs, bool) { - k.mu.Lock() - in, ok := k.mem[tree] - k.mu.Unlock() - if ok || k.kv == nil || !treeHash.MatchString(tree) { - return in, ok - } - ctx, cancel := context.WithTimeout(context.Background(), keptTimeout) - defer cancel() - e, err := k.kv.Get(ctx, tree) - if err != nil { - return bridge.Inputs{}, false // not kept (or not readable): read from the forge - } - if err := json.Unmarshal(e.Value(), &in); err != nil || in.Manifest == "" { - k.logger.Printf("inputs.%s cannot be read, so its directory is read from the forge: %v", tree, err) - return bridge.Inputs{}, false - } - k.mu.Lock() - k.mem[tree] = in - k.mu.Unlock() - return in, true -} - -func (k *kept) Put(tree string, in bridge.Inputs) { - k.mu.Lock() - k.mem[tree] = in - k.mu.Unlock() - if k.kv == nil || !treeHash.MatchString(tree) { - return - } - data, err := json.Marshal(in) - if err != nil { - return - } - ctx, cancel := context.WithTimeout(context.Background(), keptTimeout) - defer cancel() - if _, err := k.kv.Put(ctx, tree, data); err != nil { - k.logger.Printf("inputs.%s is not kept, so the next merge reads its directory from the forge again: %v", tree, err) - } -} diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/main.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/main.go index 815830d5..e832e94f 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/main.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/main.go @@ -1,13 +1,12 @@ -// Command mesh-delivery-forge-bridge is delivery's forge bridge (novox/hq ADR 0437 §4, issue 1235): it hears the forge -// module's pull request events and writes each as a pull message on its stream FORGE. It runs beside today's -// mesh-delivery and merges nothing. It serves no request subject and holds no timer: every reaction is an event -// (ADR 0437 §8); a refused act is tried again only on the next event touching its meshdeliveryclient. +// Command mesh-delivery-forge-bridge is delivery's forge bridge: it says one message head-moved on its stream forge for +// every move of a watched branch's head, heard on the seat git's branch-moved and read at its start (novox/hq design 47 H +// §739), and performs what the gate owes the forge through the seat git, its outcome said as owed.ended. It serves no +// request subject and holds no timer: every reaction is an event (ADR 0437 §8). package main import ( "context" "encoding/json" - "errors" "fmt" "io" "log" @@ -23,12 +22,10 @@ import ( "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" - condseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" git "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" mcseat "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" - checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/sourcebranches" "mesh-delivery-forge-bridge/internal/bridge" @@ -73,40 +70,6 @@ type publisher interface { Publish(ctx context.Context, subject string, payload []byte, opts ...jetstream.PublishOpt) (*jetstream.PubAck, error) } -// handle turns one pull request event into its message on FORGE and acknowledges it (bridge.Convert). An event it cannot -// read is terminated, never redelivered, and the log names its subject and why. A message it could not write stays -// unacknowledged: the server delivers it again (the server's ack wait is the only clock). -func handle(ctx context.Context, m delivered, pub publisher, files bridge.Files, watched bridge.Watching, logger *log.Logger) { - msg, err := bridge.Convert(m.Subject(), m.Data(), files, watched) - switch { - case errors.Is(err, bridge.ErrRetry): - logger.Printf("%v; heard again in %s", err, retryDelay) - _ = m.NakWithDelay(retryDelay) - return - case errors.Is(err, bridge.ErrNotHandled): - logger.Printf("%v; nothing written", err) - _ = m.Ack() - return - case err != nil: - logger.Printf("%v; the event is dropped", err) - _ = m.Term() - return - } - var opts []jetstream.PublishOpt - // The forge's event id makes a redelivery after a lost acknowledgement the same message to the stream. - if id := m.Headers().Get("x-event-id"); id != "" { - opts = append(opts, jetstream.WithMsgID(id)) - } - if _, err := pub.Publish(ctx, forgeSubject(msg.Event), msg.Data, opts...); err != nil { - logger.Printf("%s: %s could not be written on FORGE, left for redelivery: %v", m.Subject(), msg.Event, err) - return - } - if msg.Said != "" { - logger.Printf("%s: %s", m.Subject(), msg.Said) - } - _ = m.Ack() -} - // run is the program: it returns when ctx ends or the bus cannot be used. func run(ctx context.Context, getenv func(string) string, logOut io.Writer) error { logger := log.New(logOut, "mesh-delivery-forge-bridge: ", log.LstdFlags) @@ -115,91 +78,49 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro return err } setForgeBridgeModule(cred.Module) // the bridge names its own states and stream from its credential's module (A-296, §428) - again := make(chan struct{}, 1) - conn, js, err := cred.Dial( - nats.ReconnectHandler(func(*nats.Conn) { - select { - case again <- struct{}{}: - default: - } - }), - ) + conn, js, err := cred.Dial() if err != nil { return err } defer conn.Close() - // What the forge holds open, said before any event is heard, and again at each reconnect (ADR 0429 §13). call := callerOn(conn, cred.User) - // No forge event is heard while the open pull requests are read and said (ADR 0443 §5): events and syncs take turns. - var turn sync.Mutex - kv, err := process.EssentialBucket(ctx, js, "bridge", headsBucket(cred.Module), "keep the heads it wrote on the forge stream") + kv, err := process.EssentialBucket(ctx, js, "bridge", headsBucket(cred.Module), "keep the head it holds for each watched branch") if err != nil { return err } heads := kvHeads{kv} - // Everything written on FORGE, an event's and a sync's, keeps the head in the bucket. - rec := &recorder{pub: js, heads: heads, logger: logger} // The branches the bridge watches (design 47 G §4, §507, §532): the registrations' first view and then // registration-changed; before the first registration, the gate's source's branches and its source-moved. Nothing is // written before both first views are held (ready). watched := &bridge.Watched{} - said := func(ctx context.Context) error { return syncOpen(ctx, call, rec, heads, watched, &turn, logger) } // The module's one pull of its durable consumer on the shared events stream is its event hub (below). hub := process.NewEventHub(logger.Printf) // The state performed is a `requires` entry: opened by its binding, asked of the seat mesh-broker's show at this start // as a kept call and again on its recreated and dropped (design 47 G §577). Until show answers, nothing owed is // performed and the bridge says it waits, naming the entry. - binding := process.NewBinding(brokerSeat, forgeBridgeModule(), keptCallOf(ctx, call), logger.Printf, performedLocal, findingsLocal, views.FindingEventsLocal) + binding := process.NewBinding(brokerSeat, forgeBridgeModule(), keptCallOf(ctx, call), logger.Printf, performedLocal) if err := binding.Register(hub); err != nil { return err } binding.Ask(performedLocal) - binding.Ask(findingsLocal) - binding.Ask(views.FindingEventsLocal) if w := binding.Waiting(); w != "" { logger.Printf("the bridge %s: nothing owed to the forge is performed until it is bound", w) } performedKV := &boundBucket{binding: binding, local: performedLocal, js: js} - // The bridge's one finding, branch-unprotected (design 47 G §17, §387): written through mesh-sdk's held finding writer into the - // state findings, a `requires` entry opened by its binding; a writer that cannot start (the entry is not bound, or the stream - // of its asks is not there yet) is said, and what it holds is stated at the next start. - findingsWriter, err := views.NewHeldWriter(views.WriterConfig[condseat.Finding, condseat.FindingCleared, condseat.FindingsStated, condseat.FindingsAskedEvent]{ - Module: cred.Module, State: findingState{&boundBucket{binding: binding, local: findingsLocal, js: js}}, Node: cred.Node, - Asked: condseat.SubjectFindingsAsked, JS: js, Conn: conn, Logf: logger.Printf, - // Its finding events under its binding finding-events (design 47 G A-274, §529). - Bindings: binding}) - if err != nil { - return fmt.Errorf("the bridge's finding writer cannot be made: %w", err) - } - defer findingsWriter.Close() - if err := findingsWriter.Start(ctx); err != nil { - logger.Printf("the finding writer cannot start, the finding branch-unprotected is stated at the next start: %v", err) - } - // What the gate and the checker owe the forge, performed through the seat git (design 47 *What the gate owes the forge*). + // What the gate owes the forge, performed through the seat git (design 47 *What the gate owes the forge*). owe := &owing{call: call, pub: js, performed: kvPerformed{performedKV}, waits: binding.Waiting, now: time.Now, logger: logger} - // A delivery group's member names its group, and its end is said once (ADR 0633 §4), from the seat mesh-delivery's groups. - groups := &grouper{groups: groupsVia(call), send: sendVia(js), done: kvHeads{performedKV}, logger: logger} - // A person says a group's end again with bridge_group_end_again, which writes the state end-asks (ADR 0633 §4): watched, and - // answered there; a state not there at this start is answered from the next start. - if kv := process.OptionalBucket(ctx, js, groupkeys.EndAsksBucket(forgeBridgeModule()), "bridge_group_end_again is not answered until the bridge starts again", logger.Printf); kv != nil { - go func() { - err := watch(ctx, kv, func(key string, rev uint64, value []byte) { groups.endAsked(ctx, kv, key, rev, value) }) - if err != nil && ctx.Err() == nil { - logger.Printf("the end asks are no longer watched, so bridge_group_end_again is not answered until the bridge starts again: %v", err) - } - }() - } keptCall := keptCallOf(ctx, call) reader := &views.ProcessReader{Module: forgeBridgeModule(), Call: keptCall, Logf: logger.Printf, Hub: hub} src := sourcebranches.New(sourcebranches.Call(keptCall)) var regs *meshcatalogclient.Registrations - var mv *mover + turn := &sync.Mutex{} + mv := &mover{call: call, pub: js, heads: heads, watched: watched, turn: turn, logger: logger} recompute := func() { if regs != nil { watched.Set(registeredOf(regs.All()), unescaped(src.Branches())) } } - started := make(chan struct{}) // closed once the watched branches are read and the open pull requests said + started := make(chan struct{}) // closed once the watched branches are read regs, _, err = meshcatalogclient.WireRegistrationsProcess(reader, keptCall, func(mcseat.Registration, bool) { recompute() select { @@ -216,32 +137,11 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro return err } reader.Start(ctx, js, cred, nil) - // What a merge read of each module directory, by its tree (issue 1126): a merge reads only the directories it changed. - inputs := newKept(process.OptionalBucket(ctx, js, inputsBucket(cred.Module), "a merge's module directories are kept in memory only, so the first merge after a start reads every one", logger.Printf), logger) - mv = &mover{call: call, pub: rec, heads: heads, watched: watched, inputs: inputs, groups: groups, findings: findingsWriter, turn: &turn, logger: logger} - // The hub's handler of the forge's events, for every event its consumes filter lets through. Essential: the forge's - // events are what the bridge carries. The forge's events are the seat git's (novox/hq design 47 *The seat `git`*), - // whichever module holds it. - if err := hub.Handle("mesh.seat."+git.Seat+".event.>", func(m jetstream.Msg) { - select { - case <-started: - case <-ctx.Done(): - return - } - hear(&turn, func() { - // Bounded: the turn is held, and no sync or other event goes on without it. - hctx, cancel := context.WithTimeout(ctx, syncDeadline) - defer cancel() - handle(hctx, m, rec, filesVia(hctx, call), watched, logger) - }) - }); err != nil { - return err - } if err := hub.Start(ctx, js, cred); err != nil { - return fmt.Errorf("%w: the bridge hears no forge event without it, so it stops; where the bus refused it or did not answer, this module's consumes (made by the controller with its grant) have not reached the bus yet", err) + return fmt.Errorf("%w: the bridge hears no registration without it, so it stops; where the bus refused it or did not answer, this module's consumes (made by the controller with its grant) have not reached the bus yet", err) } defer hub.Stop() - // Every move of a branch, heard on the seat git's stream branch-moved through this module's durable there (§11), so a + // Every move of a branch, heard on the seat git's stream branch-moved through this module's durable there (§739), so a // move said while the bridge was stopped is heard at its start. Essential: every merge and push reaches the stream so. moves, err := cred.HearSeatEvents(ctx, js, git.StreamBranchMoved, func(m jetstream.Msg) { select { @@ -249,12 +149,10 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro case <-ctx.Done(): return } - hctx, cancel := context.WithTimeout(ctx, syncDeadline) - defer cancel() - mv.hear(hctx, m) + mv.hear(ctx, m) }, logger.Printf) if err != nil { - return fmt.Errorf("the seat git's branch-moved cannot be heard, so no merge or push reaches the forge stream: %w", err) + return fmt.Errorf("the seat git's branch-moved cannot be heard, so no move reaches the forge stream: %w", err) } defer moves.Stop() select { @@ -270,32 +168,25 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro } } recompute() - if err := said(ctx); err != nil { - return err - } close(started) - // What the gate and the checker say they owe, each on its seat's own stream owed through this module's durable there - // (made from its consumes). A stream not there yet is said, and the bridge goes on: its other work does not wait on the - // gate, and what was said meanwhile is caught up from the groups at the next start. - for _, owner := range []struct{ of, kept string }{{seat.Seat, seat.StreamOwed}, {checker.Seat, checker.StreamOwed}} { - cc, err := cred.HearSeatEvents(ctx, js, owner.kept, func(m jetstream.Msg) { - hctx, cancel := context.WithTimeout(ctx, syncDeadline) - defer cancel() - owe.hear(hctx, owner.of, m) - }, logger.Printf) - if err != nil { - logger.Printf("nothing %s owes the forge is performed until the bridge starts again: %v", owner.of, err) - continue - } + // What the gate says it owes, on its seat's stream owed through this module's durable there (made from its consumes). + // A stream not there yet is said, and the bridge goes on: its other work does not wait on the gate. + if cc, err := cred.HearSeatEvents(ctx, js, seat.StreamOwed, func(m jetstream.Msg) { + hctx, cancel := context.WithTimeout(ctx, handleDeadline) + defer cancel() + owe.hear(hctx, seat.Seat, m) + }, logger.Printf); err != nil { + logger.Printf("nothing %s owes the forge is performed until the bridge starts again: %v", seat.Seat, err) + } else { defer cc.Stop() } - // A person performs an act again, drops one or reads the consumers' backlog with the tools bridge_owed_again, + // A person performs an act again, drops one or reads the consumer's backlog with the tools bridge_owed_again, // bridge_owed_drop and bridge_owed, which write the state owed-asks: watched, and answered there. if kv := process.OptionalBucket(ctx, js, groupkeys.OwedAsksBucket(forgeBridgeModule()), "bridge_owed_again, bridge_owed_drop and bridge_owed's backlog are not answered until the bridge starts again", logger.Printf); kv != nil { backlog := owedBacklog(js, cred, time.Now) go func() { err := watch(ctx, kv, func(key string, rev uint64, value []byte) { - hctx, cancel := context.WithTimeout(ctx, syncDeadline) + hctx, cancel := context.WithTimeout(ctx, handleDeadline) defer cancel() owe.asked(hctx, kv, key, rev, value, backlog) }) @@ -304,15 +195,8 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro } }() } - // What was said while the bridge was away and is still unanswered, read from the seat (design 47 *The bridge catches up - // from the seat*). The gate comes after the bridge, so a seat that does not answer yet is said, and the bridge goes on. - if n, err := owe.catchUp(ctx); err != nil { - logger.Printf("nothing owed is caught up from the groups at this start, so an act said while the bridge was away waits for its saying again: %v", err) - } else if n > 0 { - logger.Printf("%d acts owed to the forge, said while the bridge was away, performed from the groups", n) - } - go onReconnect(ctx, again, said, logger) - // Each watched branch's head, read at the start (§11): a head missed while the bridge was stopped is written now. + // Each watched branch's head, read at the start and said with start true (§739): a head moved while the bridge was + // stopped is written now. mv.readHeads(ctx) <-ctx.Done() return nil diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/main_test.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/main_test.go index 6efc180b..80475ec0 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/main_test.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/main_test.go @@ -5,29 +5,20 @@ import ( "context" "encoding/json" "errors" - "fmt" "log" - "slices" "strings" "sync" "testing" "time" - condseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" - git "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" - "git.novox.be/novox/mesh-sdk/go/process" - "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" "mesh-delivery-forge-bridge/internal/bridge" - "mesh-delivery-forge-bridge/internal/gitfixture" ) -const head = "0123456789abcdef0123456789abcdef01234567" - type fakeMsg struct { subject string data []byte @@ -37,7 +28,7 @@ type fakeMsg struct { func (f *fakeMsg) Subject() string { return f.subject } func (f *fakeMsg) Data() []byte { return f.data } -func (f *fakeMsg) Headers() nats.Header { return nats.Header{"X-Event-Id": {"ev-1"}} } +func (f *fakeMsg) Headers() nats.Header { return nats.Header{} } func (f *fakeMsg) Ack() error { f.acked++; return nil } func (f *fakeMsg) NakWithDelay(d time.Duration) error { f.nak = append(f.nak, d); return nil } func (f *fakeMsg) Term() error { f.termd++; return nil } @@ -56,302 +47,123 @@ func (p *fakePub) Publish(_ context.Context, subject string, payload []byte, _ . return &jetstream.PubAck{}, nil } -// body is the seat git's pull.updated (modules/mesh-delivery-contracts/seats/git), the head head_sha laid over it when given. -func body(m map[string]any) []byte { - e := map[string]any{"repository": "novox/mesh-delivery", "number": 12, "branch": "feat/x", "head": head, "base": "main", - "change": map[string]any{"paths": []string{"modules/a/x.go"}, "module-dirs": []string{"modules/a"}}} - if h, ok := m["head_sha"]; ok { - e["head"] = h - } - b, _ := json.Marshal(e) - return b -} +// memHeads is the state heads in memory. +type memHeads map[string]string -func TestReadableEventIsWrittenOnForgeAndAcked(t *testing.T) { // 1235/AC2 (unit part; the bus is the integration test) - t.Run("1945: a refused bucket is said, or stops the start naming its grant", func(t *testing.T) { - refusal := errors.New("nats: permissions violation for publish to $JS.API.STREAM.INFO.KV_x") - var said []string - if kv := process.OptionalBucket(context.Background(), refusingBus{refusal}, "x", "x is not heard", func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) }); kv != nil || len(said) != 1 || !strings.Contains(said[0], "permissions violation") { - t.Errorf("an optional bucket refused: %v, said %v", kv, said) - } - if _, err := process.EssentialBucket(context.Background(), refusingBus{refusal}, "bridge", "x", "work"); err == nil || !strings.Contains(err.Error(), "grant of x") { - t.Errorf("an essential bucket refused: %v, want its grant named", err) - } - }) - var logs bytes.Buffer - m, pub := &fakeMsg{subject: git.SubjectPullUpdated, data: body(nil)}, &fakePub{} - handle(context.Background(), m, pub, someFiles, watchesMain(), log.New(&logs, "", 0)) - if len(pub.subjects) != 1 || pub.subjects[0] != "mesh.stream.mesh-delivery-forge-bridge.event.forge.novox.mesh-delivery.pull" { - t.Fatalf("published on %v", pub.subjects) - } - f, err := forgemsg.DecodeForge(pub.data[0]) - if err != nil || f.Number != 12 || f.Branch != "feat/x" || f.Head != head || f.Action != "updated" || !f.DirsKnown { - t.Errorf("message %+v err %v", f, err) - } - if m.acked != 1 || m.termd != 0 { - t.Errorf("acked %d terminated %d", m.acked, m.termd) - } +func (m memHeads) Get(_ context.Context, key string) (string, bool, error) { + v, ok := m[key] + return v, ok, nil } +func (m memHeads) Put(_ context.Context, key, head string) error { m[key] = head; return nil } -func TestUnreadableEventIsTerminatedAndLogged(t *testing.T) { // 1235/AC5 - var logs bytes.Buffer - m, pub := &fakeMsg{subject: git.SubjectPullUpdated, data: body(map[string]any{"head_sha": "short"})}, &fakePub{} - handle(context.Background(), m, pub, someFiles, watchesMain(), log.New(&logs, "", 0)) - if len(pub.subjects) != 0 { - t.Errorf("a message was written: %v", pub.subjects) - } - if m.termd != 1 || m.acked != 0 { - t.Errorf("terminated %d acked %d, want terminated once", m.termd, m.acked) - } - if !strings.Contains(logs.String(), git.SubjectPullUpdated) || !strings.Contains(logs.String(), "head") { - t.Errorf("log %q names neither the subject nor why", logs.String()) - } - // 1235: an unwritten message is not acked (a failed write is redelivered by the server, not retried here); merged here - // from a test of its own, for the test budget. - logs.Reset() - m, pub = &fakeMsg{subject: git.SubjectPullUpdated, data: body(nil)}, &fakePub{err: errors.New("no stream")} - handle(context.Background(), m, pub, someFiles, watchesMain(), log.New(&logs, "", 0)) - if m.acked != 0 || m.termd != 0 { - t.Errorf("acked %d terminated %d, want neither", m.acked, m.termd) - } - if !strings.Contains(logs.String(), "no stream") { - t.Errorf("log %q", logs.String()) - } -} - -// watchesMain watches main of every repository, as the gate's source names it before the first registration. +// watchesMain watches main of novox/mesh-delivery, as its registration names it. func watchesMain() *bridge.Watched { w := &bridge.Watched{} - w.Set(nil, []string{"main"}) + w.Set([]bridge.Registered{{Origin: "novox/mesh-delivery", Branches: []string{"main"}}}, nil) return w } -// movedEvent is the seat git's branch-moved of main of novox/mesh-delivery, naming a head the bridge never takes. +// movedEvent is the seat git's branch-moved of a branch of novox/mesh-delivery, naming a head the bridge never takes. func movedEvent(branch string) []byte { b, _ := json.Marshal(map[string]any{"repository": "novox/mesh-delivery", "branch": branch, "head": strings.Repeat("f", 40)}) return b } -const landed = "89abcdef0123456789abcdef0123456789abcdef" +// forge answers the seat git's verb branch with the head it holds (an unprotected branch, ADR 0682 §13), and refuses +// every other verb; it records each key asked. +type forge struct { + head string + err error + asked []string +} -// moveCall answers the seat git as a forge whose main of novox/mesh-delivery is protected at landed, merged there by #12; -// trees answers trees, every other verb answers err. -func moveCall(trees json.RawMessage, err error, keys *[]string, args *[]any, mu *sync.Mutex) caller { - return func(_ context.Context, key string, body any) (json.RawMessage, error) { - mu.Lock() - *keys, *args = append(*keys, key), append(*args, body) - mu.Unlock() - switch key { - case "seat:git.branch": - return json.RawMessage(`{"repository":"novox/mesh-delivery","branch":"main","head":"` + landed + `","protected":true}`), nil - case "seat:git.pull": - return json.RawMessage(`{"pull":{"repository":"novox/mesh-delivery","number":12,"branch":"feat/x","head":"` + head + `","base":"main","state":"merged","merge-commit":"` + landed + `"}}`), nil - case "seat:git.trees": - if trees != nil { - return trees, nil +func (f *forge) call(_ context.Context, key string, _ any) (json.RawMessage, error) { + f.asked = append(f.asked, key) + if key != "seat:git.branch" { + return json.RawMessage(`{"error":"not asked of the bridge"}`), nil + } + if f.err != nil { + return nil, f.err + } + return json.RawMessage(`{"repository":"novox/mesh-delivery","branch":"main","head":"` + f.head + `","protected":false}`), nil +} + +// Design 47 H §739 (AC1, AC2, AC3 of B2): every move of a watched branch's head, and every watched head at the start, is +// one head-moved naming the head read through the seat git's verb branch and the head held before, start true only at +// the start; a head equal to the one held writes nothing; nothing but branch is asked (no pull, trees, files or +// protection), and an unprotected branch moves like any other. A move whose head cannot be read is left unacknowledged, +// never redelivered by a timer of the bridge's (H-A-48); one it cannot read is terminated, one not watched acknowledged. +func TestAMoveIsOneHeadMoved(t *testing.T) { + const h1, h2 = "1111111111111111111111111111111111111111", "2222222222222222222222222222222222222222" + f := &forge{head: h1} + pub, heads := &fakePub{}, memHeads{} + var logs bytes.Buffer + mv := &mover{call: f.call, pub: pub, heads: heads, watched: watchesMain(), turn: &sync.Mutex{}, logger: log.New(&logs, "", 0)} + said := func(i int) forgemsg.HeadMoved { + t.Helper() + h, err := forgemsg.DecodeHeadMoved(pub.data[i]) + if err != nil { + t.Fatalf("message %d: %v", i, err) + } + return h + } + subject := forgeSubject("forge.novox.mesh-delivery.main.head-moved") + for _, c := range []struct { + name string + head string + start bool + said int + before string + }{ + {"the first move", h1, false, 1, ""}, + {"the same head heard twice", h1, false, 1, ""}, + {"a later move", h2, false, 2, h1}, + {"a start with the head held", h2, true, 2, ""}, + } { + f.head = c.head + m := &fakeMsg{subject: "mesh.seat.git.event.branch-moved", data: movedEvent("main")} + if c.start { + mv.readHeads(context.Background()) + } else { + mv.hear(context.Background(), m) + if m.acked != 1 { + t.Errorf("%s: acked %d, want 1", c.name, m.acked) } } - return nil, err + if len(pub.data) != c.said { + t.Fatalf("%s: said %d, want %d", c.name, len(pub.data), c.said) + } + if h := said(c.said - 1); pub.subjects[c.said-1] != subject || h.Head != heads["branch.novox.mesh-delivery.main"] || (c.said > 1 && h.Before != h1) { + t.Errorf("%s: %s %+v, heads %v", c.name, pub.subjects[c.said-1], h, heads) + } + } + f.head = "3333333333333333333333333333333333333333" + mv.readHeads(context.Background()) + if len(pub.data) != 3 || !said(2).Start || said(2).Before != h2 { + t.Errorf("a start with a head moved meanwhile: said %d, %+v; want head-moved with start true and before the held head", len(pub.data), said(2)) + } + for _, k := range f.asked { + if k != "seat:git.branch" { + t.Errorf("asked %s; the bridge asks nothing but the seat git's branch", k) + } } -} -func newMover(call caller, pub publisher, heads headStore, logs *bytes.Buffer) *mover { - return &mover{call: call, pub: pub, heads: heads, watched: watchesMain(), turn: &sync.Mutex{}, logger: log.New(logs, "", 0)} -} - -func TestTreesRefusedLeavesTheEventForTenSecondsAndLogs(t *testing.T) { // 1236/AC3 - var logs bytes.Buffer - var keys []string - var args []any - var mu sync.Mutex - m, pub, heads := &fakeMsg{subject: bridge.BranchMovedSubject, data: movedEvent("main")}, &fakePub{}, newHeads() - newMover(moveCall(nil, errors.New("the tree listing of novox/mesh-delivery was cut"), &keys, &args, &mu), pub, heads, &logs).hear(context.Background(), m) - if len(pub.subjects) != 0 || heads.get("branch.novox.mesh-delivery.main") != "" { - t.Errorf("a message was written: %v", pub.subjects) - } - if m.acked != 0 || m.termd != 0 || len(m.nak) != 1 || m.nak[0] < 10*time.Second { - t.Errorf("acked %d terminated %d naks %v, want one nak of at least 10 s", m.acked, m.termd, m.nak) - } - for _, w := range []string{"novox/mesh-delivery", landed, "the tree listing of novox/mesh-delivery was cut"} { - if !strings.Contains(logs.String(), w) { - t.Errorf("log %q does not name %q", logs.String(), w) + for _, c := range []struct { + name string + data []byte + err error + acked, termd, said int + }{ + {"a branch not watched", movedEvent("feat/x"), nil, 1, 0, 0}, + {"an event it cannot read", []byte(`{"repository":"x"}`), nil, 0, 1, 0}, + {"a head the forge refuses", movedEvent("main"), errors.New("the forge says 502"), 0, 0, 0}, + } { + f.err, f.head = c.err, "4444444444444444444444444444444444444444" + n := len(pub.data) + m := &fakeMsg{data: c.data} + mv.hear(context.Background(), m) + if m.acked != c.acked || m.termd != c.termd || len(m.nak) != 0 || len(pub.data)-n != c.said { + t.Errorf("%s: acked %d, terminated %d, nak %v, said %d; want %d, %d, none, %d", c.name, m.acked, m.termd, m.nak, len(pub.data)-n, c.acked, c.termd, c.said) } } } - -// 1236/AC1; design 47 *The seat git* (verbs trees, file); design 47 G §11, §20, §509 (FB1, FB10): a move of a watched branch -// is taken by the seat's verb branch, never the event's head, written once as merged with its pull request and base, and -// its head kept, so the same move heard again writes nothing; a move of a branch not watched writes nothing. -func TestMergeCallsTheToolOnceAndWritesTheMergedMessage(t *testing.T) { - noCodeNamesTheBridgesOwnModule(t) - var logs bytes.Buffer - var keys []string - var args []any - var mu sync.Mutex // the files are asked for concurrently (issue 1458) - trees := gitfixture.Given(t, "trees answers", "verb:trees.answer") // root ddd…, "" ddd…, contracts eee… - call := moveCall(trees, nil, &keys, &args, &mu) - file := gitfixture.Given(t, "file answers", "verb:file.answer") - files := func(ctx context.Context, key string, body any) (json.RawMessage, error) { - if key == "seat:git.file" { // the manifests, 1448; the trees are what this test is about - mu.Lock() - keys = append(keys, key) - mu.Unlock() - return file, nil - } - return call(ctx, key, body) - } - m, pub, heads := &fakeMsg{subject: bridge.BranchMovedSubject, data: movedEvent("main")}, &fakePub{}, newHeads() - mv := newMover(files, pub, heads, &logs) - mv.hear(context.Background(), m) - if len(keys) < 3 || keys[0] != "seat:git.branch" || keys[1] != "seat:git.pull" || keys[2] != "seat:git.trees" || slices.Index(keys[3:], "seat:git.trees") >= 0 { - t.Fatalf("calls %v, want the branch, its pull, then the trees once", keys) - } - if got, _ := json.Marshal(args[2]); !sameJSON(got, []byte(`{"repository":"novox/mesh-delivery","commit":"`+landed+`"}`)) { - t.Errorf("arguments %s", got) - } - if len(pub.subjects) != 1 || pub.subjects[0] != "mesh.stream.mesh-delivery-forge-bridge.event.forge.novox.mesh-delivery.merged" { - t.Fatalf("published on %v", pub.subjects) - } - f, err := forgemsg.DecodeForge(pub.data[0]) - d, e := strings.Repeat("d", 40), strings.Repeat("e", 40) - if err != nil || f.Root != d || f.Trees[""] != d || f.Trees["contracts"] != e || f.Merge != landed || f.Base != "main" || f.Number != 12 || f.Branch != "feat/x" { - t.Errorf("message %+v err %v", f, err) - } - if m.acked != 1 || heads.get("branch.novox.mesh-delivery.main") != landed { - t.Errorf("acked %d, head kept %q", m.acked, heads.get("branch.novox.mesh-delivery.main")) - } - again, other := &fakeMsg{subject: bridge.BranchMovedSubject, data: movedEvent("main")}, &fakeMsg{subject: bridge.BranchMovedSubject, data: movedEvent("feat/x")} - mv.hear(context.Background(), again) - mv.hear(context.Background(), other) - if len(pub.subjects) != 1 || again.acked != 1 || other.acked != 1 { - t.Errorf("the same move again and a move of a branch not watched wrote %d messages (acked %d, %d), want none", len(pub.subjects)-1, again.acked, other.acked) - } -} - -func TestAnAnswerWithoutARootIsNotUsed(t *testing.T) { // 1236 (a shape the seat does not answer); design 47 *A failed read answers an error* - for _, raw := range []json.RawMessage{json.RawMessage(`{"trees":{"a":"t"}}`), json.RawMessage(`[1]`), - gitfixture.Given(t, "trees answers an error, never none", "verb:trees.answer")} { - call := func(context.Context, string, any) (json.RawMessage, error) { return raw, nil } - if _, _, err := treesVia(context.Background(), call)("o", "r", "s"); err == nil { - t.Errorf("the answer %s was taken", raw) - } - } -} - -func TestFilesViaAsksByOwnerRepoPathRefAndTakesTheContent(t *testing.T) { // 1448/AC4; design 47 *The seat git* (verb file) - var keys []string - var args []any - call := func(_ context.Context, key string, body any) (json.RawMessage, error) { - keys, args = append(keys, key), append(args, body) - return gitfixture.Given(t, "file answers", "verb:file.answer"), nil - } - got, err := filesVia(context.Background(), call)("novox", "mesh-contracts", "seats/index.json", strings.Repeat("a", 40)) - if err != nil || string(got) != `{"format":1,"seats":["mesh-broker"]}` { - t.Fatalf("got %q err %v", got, err) - } - if len(keys) != 1 || keys[0] != "seat:git.file" { - t.Fatalf("calls %v", keys) - } - want := gitfixture.Given(t, "file asks", "verb:file.input") - if b, _ := json.Marshal(args[0]); !sameJSON(b, want) { - t.Errorf("arguments %s, want the seat's %s", b, want) - } -} - -func TestFilesViaTellsNotFoundFromOtherErrorsAndOtherShapes(t *testing.T) { // 1448/AC3; design 47 *A failed read answers an error* - call := func(err error, raw json.RawMessage) caller { - return func(context.Context, string, any) (json.RawMessage, error) { return raw, err } - } - absent := gitfixture.All(t, "file answers", "verb:file.answer")[1] - if _, err := filesVia(context.Background(), call(nil, absent))("o", "r", "p", "s"); !errors.Is(err, bridge.ErrNotFound) { - t.Errorf("an absent file is %v, want ErrNotFound", err) - } - if _, err := filesVia(context.Background(), call(errors.New("nats: timeout"), nil))("o", "r", "p", "s"); err == nil || errors.Is(err, bridge.ErrNotFound) { - t.Errorf("a seat that does not answer is %v, want an error that is not ErrNotFound", err) - } - for _, raw := range []json.RawMessage{gitfixture.Given(t, "file answers an error, never none", "verb:file.answer"), json.RawMessage(`{}`), json.RawMessage(`[1]`), json.RawMessage(`{"path":"p"}`)} { - if _, err := filesVia(context.Background(), call(nil, raw))("o", "r", "p", "s"); err == nil || errors.Is(err, bridge.ErrNotFound) { - t.Errorf("the answer %s gave %v, want an error", raw, err) - } - } -} - -// sameJSON is whether two JSON texts say the same value. -func sameJSON(a, b []byte) bool { - var x, y any - if json.Unmarshal(a, &x) != nil || json.Unmarshal(b, &y) != nil { - return false - } - ax, _ := json.Marshal(x) - by, _ := json.Marshal(y) - return bytes.Equal(ax, by) -} - -// someFiles is a forge that has a module.json at every path and no other file, for the tests that are not about the files. -func someFiles(_, _, path, _ string) ([]byte, error) { - if strings.HasSuffix(path, "module.json") { - return []byte("{}"), nil - } - return nil, bridge.ErrNotFound -} - -// refusingBus refuses every bucket with err (issue 1945). -type refusingBus struct{ err error } - -func (r refusingBus) KeyValue(context.Context, string) (jetstream.KeyValue, error) { return nil, r.err } - -// fakeFindings is a finding writer that holds what it is given. -type fakeFindings struct { - held []condseat.Finding - cleared []string -} - -func (f *fakeFindings) Write(_ context.Context, w condseat.Finding) error { - f.held = append(f.held, w) - return nil -} -func (f *fakeFindings) Clear(_ context.Context, scope, id, kind, _ string) error { - f.cleared = append(f.cleared, scope+"."+id+"."+kind) - f.held = nil - return nil -} -func (f *fakeFindings) Held() []condseat.Finding { return f.held } - -// Design 47 G §17, A-251 (FB2): a move of a watched branch whose protection was removed writes nothing and says the finding -// branch-unprotected, scope merge, its id the owner, repository and branch escaped as a key; a move that finds the protection -// back clears it, and a protected branch with no finding held says nothing. -func TestAnUnprotectedBranchIsSaidAsAFindingAndClearedWhenItIsBack(t *testing.T) { - var logs bytes.Buffer - protected := false - call := func(_ context.Context, key string, _ any) (json.RawMessage, error) { - if key == "seat:git.branch" { - return json.RawMessage(fmt.Sprintf(`{"repository":"novox/mesh-delivery","branch":"beta","head":"%s","protected":%t}`, landed, protected)), nil - } - return nil, errors.New("not asked: " + key) - } - pub, heads, findings := &fakePub{}, newHeads(), &fakeFindings{} - mv := newMover(call, pub, heads, &logs) - mv.findings, mv.now = findings, func() time.Time { return time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC) } - mv.watched = &bridge.Watched{} - mv.watched.Set(nil, []string{"beta"}) - mv.hear(context.Background(), &fakeMsg{subject: bridge.BranchMovedSubject, data: movedEvent("beta")}) - if len(pub.subjects) != 0 || len(findings.held) != 1 { - t.Fatalf("written %v, findings %+v: want nothing written and one finding", pub.subjects, findings.held) - } - f := findings.held[0] - if f.Scope != "merge" || f.ID != "novox/mesh-delivery/beta" || f.Kind != "branch-unprotected" || f.Severity != "warning" || f.Observer != "mesh-delivery-forge-bridge" || - f.Subject.Scope != "merge" || f.Subject.ID != "novox/mesh-delivery/beta" || !strings.Contains(f.Summary, "beta") { - t.Errorf("the finding = %+v", f) - } - // The protection is back at a move whose head the bridge wrote already: the finding is cleared. - protected = true - heads.vals["branch.novox.mesh-delivery.beta"] = landed - mv.hear(context.Background(), &fakeMsg{subject: bridge.BranchMovedSubject, data: movedEvent("beta")}) - if len(findings.cleared) != 1 || findings.cleared[0] != "merge.novox/mesh-delivery/beta.branch-unprotected" || len(findings.held) != 0 { - t.Errorf("cleared %v held %+v, want the finding cleared", findings.cleared, findings.held) - } - mv.hear(context.Background(), &fakeMsg{subject: bridge.BranchMovedSubject, data: movedEvent("beta")}) - if len(findings.cleared) != 1 { - t.Errorf("cleared %v: a protected branch with no finding held clears nothing", findings.cleared) - } -} diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/moves.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/moves.go index 7d0d4bed..c482d444 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/moves.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/moves.go @@ -2,42 +2,38 @@ package main import ( "context" + "encoding/json" "errors" "fmt" "log" "strings" "sync" - "time" "github.com/nats-io/nats.go/jetstream" mcseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" + "git.novox.be/novox/mesh-sdk/go/busretry" "git.novox.be/novox/mesh-sdk/go/keys" "mesh-delivery-forge-bridge/internal/bridge" ) -// mover writes the one message of each move of a watched branch (design 47 G §11, §509, §714): heard on the seat git's -// branch-moved through the bridge's durable on SEAT_git_branch-moved, the head read by the seat's verb branch, never the -// event's; merged with its pull request, or pushed; the head written kept in the state heads under the branch's key -// (A-236), so a move heard twice writes once and the next pushed says its head before. +// mover writes the one message of each move of a watched branch's head (design 47 H §739): heard on the seat git's +// branch-moved through the bridge's durable on SEAT_git_branch-moved, and at the start for every watched branch, the +// head read by the seat's verb branch, never the event's; head-moved with before the head held in the state heads under +// the branch's key (A-236), so a head heard twice writes once. It reads no pull request, no tree and no protection. type mover struct { call caller pub publisher heads headStore watched *bridge.Watched - inputs bridge.Kept - groups *grouper - // findings says the finding branch-unprotected (design 47 G §17, A-251); nil says none. - findings findingWriter - now func() time.Time - turn *sync.Mutex - logger *log.Logger + turn *sync.Mutex + logger *log.Logger } // hear takes one branch-moved: one it cannot read is terminated, one of a branch not watched is acknowledged with nothing -// written, one the forge could not say about is heard again. +// written. One whose head could not be read or written is left unacknowledged: the server delivers it again after its +// acknowledgement wait, the bridge holding no clock of its own (design 47 H-A-48). func (mv *mover) hear(ctx context.Context, m delivered) { moved, err := bridge.MovedOf(m.Data()) if err != nil { @@ -50,67 +46,61 @@ func (mv *mover) hear(ctx context.Context, m delivered) { return } mv.turn.Lock() - err = mv.move(ctx, moved) + err = mv.move(ctx, moved, false) mv.turn.Unlock() switch { case errors.Is(err, bridge.ErrUnreadable): mv.logger.Printf("%v; the event is dropped", err) _ = m.Term() case err != nil: - mv.logger.Printf("%v; heard again in %s", err, retryDelay) - _ = m.NakWithDelay(retryDelay) + mv.logger.Printf("%v; left unacknowledged, so the bus delivers it again", err) default: _ = m.Ack() } } -// move reads the branch's head and writes its message, unless that head is the one last written for it. -func (mv *mover) move(ctx context.Context, moved bridge.Moved) error { - b, err := bridge.ReadBranch(ctx, bridge.Caller(mv.call), moved) +// branchCall is the seat git's verb branch as a kept call (design 47 H-A-48): asked again through a timeout or no +// responders while the branch is still watched, so a read waits on the seat holder's return. +func (mv *mover) branchCall(moved bridge.Moved) bridge.Caller { + open := busretry.Open{Key: moved.HeadKey(), Logf: mv.logger.Printf, + Open: func(string) (bool, error) { return mv.watched.Watches(moved.Repository(), moved.Branch), nil }} + return func(ctx context.Context, key string, body any) (json.RawMessage, error) { + return busretry.DoKept("the call "+key, open, func() (json.RawMessage, error) { return mv.call(ctx, key, body) }, nil) + } +} + +// move reads the branch's head and writes its head-moved, unless that head is the one held for it. start says it is read +// at the bridge's start (or at a branch's adoption, which is its start). +func (mv *mover) move(ctx context.Context, moved bridge.Moved, start bool) error { + head, err := bridge.ReadHead(ctx, mv.branchCall(moved), moved) if err != nil { return fmt.Errorf("%s of %s: %w", moved.Branch, moved.Repository(), err) } - // The protection is read at every move, so a finding stands until a move finds it back (§17). - mv.protection(ctx, moved.Repository(), moved.Branch, b.Protected, b.Head) before, _, err := mv.heads.Get(ctx, moved.HeadKey()) if err != nil { - return fmt.Errorf("%s of %s: the head last written cannot be read: %w", moved.Branch, moved.Repository(), err) + return fmt.Errorf("%s of %s: the head held cannot be read: %w", moved.Branch, moved.Repository(), err) } - if before == b.Head { + if before == head { return nil } - if !b.Protected { - // A branch whose protection was removed starts nothing: nothing is written (§17, §509). - mv.logger.Printf("%s of %s moved to %s while it is not protected: nothing is written", moved.Branch, moved.Repository(), b.Head) - return nil - } - msg, err := bridge.MoveMessage(ctx, bridge.Caller(mv.call), moved, b.Head, before, treesVia(ctx, mv.call), filesVia(ctx, mv.call), mv.inputs) + msg, err := bridge.HeadMoved(moved, head, before, start) if err != nil { - return err - } - data, grouped := msg.Data, false - var member forgemsg.Forge - if mv.groups != nil { - data, member, grouped = mv.groups.member(ctx, msg.Data) + return fmt.Errorf("%w: %s of %s: %v", bridge.ErrUnreadable, moved.Branch, moved.Repository(), err) } // The branch's new head makes a write after a lost acknowledgement the same message to the stream. - id := "moved." + keys.EscapeName(moved.Repository()) + "." + keys.EscapeName(moved.Branch) + "." + b.Head - if _, err := mv.pub.Publish(ctx, forgeSubject(msg.Event), data, jetstream.WithMsgID(id)); err != nil { - return fmt.Errorf("%s could not be written on FORGE: %w", msg.Event, err) + id := "moved." + keys.EscapeName(moved.Repository()) + "." + keys.EscapeName(moved.Branch) + "." + head + if _, err := mv.pub.Publish(ctx, forgeSubject(msg.Event), msg.Data, jetstream.WithMsgID(id)); err != nil { + return fmt.Errorf("%s could not be written on the stream forge: %w", msg.Event, err) } - if grouped { - mv.groups.published(ctx, member) - } - if err := mv.heads.Put(ctx, moved.HeadKey(), b.Head); err != nil { - mv.logger.Printf("%s of %s: the head %s written is not kept, so the next move says the older head before: %v", moved.Branch, moved.Repository(), b.Head, err) - } - if msg.Said != "" { - mv.logger.Printf("%s", msg.Said) + if err := mv.heads.Put(ctx, moved.HeadKey(), head); err != nil { + mv.logger.Printf("%s of %s: the head %s written is not kept, so the next move says the older head before: %v", moved.Branch, moved.Repository(), head, err) } + mv.logger.Printf("%s", msg.Said) return nil } -// readHeads reads each watched branch's head (§11): a head missed while the bridge was stopped is written now. +// readHeads reads each watched branch's head and says it with start true (design 47 H §739): a head moved while the +// bridge was stopped is written now, one equal to the head held writes nothing. func (mv *mover) readHeads(ctx context.Context) { for _, repo := range mv.watched.Repositories() { owner, name, ok := strings.Cut(repo, "/") @@ -119,7 +109,7 @@ func (mv *mover) readHeads(ctx context.Context) { } for _, branch := range mv.watched.Branches(repo) { mv.turn.Lock() - err := mv.move(ctx, bridge.Moved{Owner: owner, Repo: name, Branch: branch}) + err := mv.move(ctx, bridge.Moved{Owner: owner, Repo: name, Branch: branch}, true) mv.turn.Unlock() if err != nil { mv.logger.Printf("the head of %s of %s is not read at this start, so a move missed meanwhile waits for its next move: %v", branch, repo, err) diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/names_test.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/names_test.go index d1e0bfff..76ca7b2f 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/names_test.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/names_test.go @@ -45,10 +45,10 @@ func theNamesAreTheManifests(t *testing.T) { } } // Design 47 G A-296: its own buckets are named from its credential's module, never a constant. - if headsBucket("n1-bridge") != "n1-bridge_heads" || inputsBucket("n1-bridge") != "n1-bridge_inputs" { - t.Errorf("the own buckets of n1-bridge are %s and %s", headsBucket("n1-bridge"), inputsBucket("n1-bridge")) + if headsBucket("n1-bridge") != "n1-bridge_heads" { + t.Errorf("the own bucket heads of n1-bridge is %s", headsBucket("n1-bridge")) } - for _, b := range []string{headsBucket(forgeBridgeModule()), inputsBucket(forgeBridgeModule())} { + for _, b := range []string{headsBucket(forgeBridgeModule())} { if !slices.Contains(own, b) { t.Errorf("the bridge keeps %s, its manifest's states are %v", b, own) } diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/open.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/open.go deleted file mode 100644 index 826d5295..00000000 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/open.go +++ /dev/null @@ -1,143 +0,0 @@ -package main - -import ( - "context" - "fmt" - "log" - "slices" - "sync" - "time" - - "github.com/nats-io/nats.go/jetstream" - - git "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" - - "mesh-delivery-forge-bridge/internal/bridge" -) - -// openPullsTool is the seat git's verb for every open pull request (novox/hq design 47 *The seat `git`*): no arguments, -// answers {pulls: [...]} or an error. -var openPullsTool = bridge.GitKey(git.VerbOpenPulls) - -// headStore is the bucket heads: the head last written on FORGE for each pull request, by bridge.Track's key -// (novox/hq ADR 0443 §5). -type headStore interface { - // Get is the head kept under the key; found is false when there is none. - Get(ctx context.Context, key string) (head string, found bool, err error) - Put(ctx context.Context, key, head string) error - Delete(ctx context.Context, key string) error -} - -// recorder writes to FORGE and keeps in the bucket what it wrote: a pull message keeps its head, a closed or merged one -// takes its pull request's key away. Every write of the program goes through it, an event's and a sync's alike. -type recorder struct { - pub publisher - heads headStore - logger *log.Logger -} - -func (r *recorder) Publish(ctx context.Context, subject string, payload []byte, opts ...jetstream.PublishOpt) (*jetstream.PubAck, error) { - ack, err := r.pub.Publish(ctx, subject, payload, opts...) - if err != nil { - return ack, err - } - f, derr := forgemsg.DecodeForge(payload) - if derr != nil { - return ack, nil - } - key, head, ok := bridge.Track(f) - if !ok { - return ack, nil - } - if head == "" { - err = r.heads.Delete(ctx, key) - } else { - err = r.heads.Put(ctx, key, head) - } - if err != nil { - return ack, fmt.Errorf("%s was written on FORGE but its head could not be kept in the bucket: %w", subject, err) - } - return ack, nil -} - -// syncDeadline bounds one sync: the call to the forge and every read and write of the bucket and of FORGE, because -// the sync holds the turn that stops the bridge hearing any event. -const syncDeadline = 30 * time.Second - -// hear runs one reaction to a forge event while no sync of the open pull requests is between reading the forge and -// writing the open message (novox/hq ADR 0443 §5): an event heard meanwhile waits, and is written after it, so a pull -// request opened after the read is added again by its own message and never dropped by the open message. -func hear(mu *sync.Mutex, react func()) { - mu.Lock() - defer mu.Unlock() - react() -} - -// syncOpen reads the open pull requests from the forge and writes a pull message for each whose head differs from the -// one kept in the bucket, then one open message naming every one it read, those whose head did not differ among them: -// what makes the indexer's candidates match the forge after a restart or a gap in the forge's events (ADR 0429 §13, -// ADR 0443 §5). pub keeps the bucket as it writes (recorder). It holds mu from before it reads the forge until the open -// message is written, so no event is heard between, and never past syncDeadline. A bucket that cannot be read is logged -// and every open pull request is written. An error names the call and the forge's words. -func syncOpen(ctx context.Context, call caller, pub publisher, heads headStore, watched bridge.Watching, mu *sync.Mutex, logger *log.Logger) error { - mu.Lock() - defer mu.Unlock() - ctx, cancel := context.WithTimeout(ctx, syncDeadline) - defer cancel() - raw, err := call(ctx, openPullsTool, git.OpenPullsInput{}) - if err != nil { - return fmt.Errorf("%s: %w", openPullsTool, err) - } - msgs, skipped, err := bridge.OpenPulls(raw, watched) - if err != nil { - return fmt.Errorf("%s: %w", openPullsTool, err) - } - open, err := bridge.OpenMessage(raw, watched) - if err != nil { - return fmt.Errorf("%s: %w", openPullsTool, err) - } - for _, s := range skipped { - logger.Printf("an open pull request is left out: %s", s) - } - var differing []bridge.Message - for _, m := range msgs { - f, err := forgemsg.DecodeForge(m.Data) - if err != nil { - differing = append(differing, m) - continue - } - key, head, _ := bridge.Track(f) - kept, found, err := heads.Get(ctx, key) - if err != nil { - logger.Printf("the heads kept could not be read, so every open pull request is written: %v", err) - differing = slices.Clone(msgs) - break - } - if !found || kept != head { - differing = append(differing, m) - } - } - for _, m := range append(differing, open) { - if _, err := pub.Publish(ctx, forgeSubject(m.Event), m.Data); err != nil { - return fmt.Errorf("the open pull request message %s could not be written on FORGE: %w", m.Event, err) - } - } - logger.Printf("%d pull messages and the open message written on FORGE", len(differing)) - return nil -} - -// onReconnect reads the open pull requests again each time the bus connection comes back, until ctx ends; a failure is -// logged and the bridge goes on (the next reconnect or event tries again). -func onReconnect(ctx context.Context, again <-chan struct{}, sync func(context.Context) error, logger *log.Logger) { - for { - select { - case <-ctx.Done(): - return - case <-again: - if err := sync(ctx); err != nil { - logger.Printf("after a reconnect: %v", err) - } - } - } -} diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/open_test.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/open_test.go deleted file mode 100644 index a10a62f3..00000000 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/open_test.go +++ /dev/null @@ -1,295 +0,0 @@ -package main - -import ( - "bytes" - "context" - "encoding/json" - "errors" - "log" - "strconv" - "strings" - "sync" - "testing" - "time" - - "github.com/nats-io/nats.go/jetstream" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" -) - -// openAnswer is the seat git's answer of open-pulls: two into the trunk, one into another branch. -func openAnswer() json.RawMessage { - entry := func(repo string, number int, base, branch, sha string) string { - return `{"pull":{"repository":"novox/` + repo + `","number":` + strconv.Itoa(number) + `,"base":"` + base + `","branch":"` + branch + - `","head":"` + sha + `","state":"open","url":"h"},"required":[],"checks":{}}` - } - return json.RawMessage(`{"pulls":[` + entry("mesh-delivery", 12, "main", "feat/x", head) + `,` + - entry("mesh-catalog", 300, "main", "feat/x", strings.Repeat("a", 40)) + `,` + entry("mesh-catalog", 301, "feat/y", "feat/z", strings.Repeat("b", 40)) + `]}`) -} - -func TestSyncOpenWritesOneUpdatedMessagePerOpenPull(t *testing.T) { // 1237/AC1, AC4 - var logs bytes.Buffer - pub := &fakePub{} - var keys []string - call := func(_ context.Context, key string, _ any) (json.RawMessage, error) { - keys = append(keys, key) - return openAnswer(), nil - } - if err := runSync(call, pub, newHeads(), &sync.Mutex{}, log.New(&logs, "", 0)); err != nil { - t.Fatal(err) - } - if len(keys) != 1 || keys[0] != "seat:git.open-pulls" { - t.Errorf("calls %v", keys) - } - if len(pub.subjects) != 3 { - t.Fatalf("%d messages written (%v), want two pull messages and the open message: the pull request into feat/y gets none", len(pub.subjects), pub.subjects) - } - want := []string{"mesh.stream.mesh-delivery-forge-bridge.event.forge.novox.mesh-delivery.pull", "mesh.stream.mesh-delivery-forge-bridge.event.forge.novox.mesh-catalog.pull"} - for i, w := range want { - f, err := forgemsg.DecodeForge(pub.data[i]) - if pub.subjects[i] != w || err != nil || f.Action != "updated" || f.DirsKnown || f.Branch != "feat/x" { - t.Errorf("message %d on %s = %+v err %v", i, pub.subjects[i], f, err) - } - } -} - -// memHeads is the bucket heads in memory. -type memHeads struct { - mu sync.Mutex - vals map[string]string - getErr error -} - -func newHeads(kv ...string) *memHeads { - m := &memHeads{vals: map[string]string{}} - for i := 0; i+1 < len(kv); i += 2 { - m.vals[kv[i]] = kv[i+1] - } - return m -} - -func (m *memHeads) Get(_ context.Context, key string) (string, bool, error) { - m.mu.Lock() - defer m.mu.Unlock() - v, ok := m.vals[key] - return v, ok, m.getErr -} -func (m *memHeads) Put(_ context.Context, key, head string) error { - m.mu.Lock() - defer m.mu.Unlock() - m.vals[key] = head - return nil -} -func (m *memHeads) Delete(_ context.Context, key string) error { - m.mu.Lock() - defer m.mu.Unlock() - delete(m.vals, key) - return nil -} -func (m *memHeads) get(key string) string { m.mu.Lock(); defer m.mu.Unlock(); return m.vals[key] } - -// runSync runs syncOpen with the recorder the program writes through. -func runSync(call caller, pub publisher, heads headStore, mu *sync.Mutex, logger *log.Logger) error { - return syncOpen(context.Background(), call, &recorder{pub: pub, heads: heads, logger: logger}, heads, watchesMain(), mu, logger) -} - -// 1279/AC6: the sync writes a pull message only for a head that differs from what FORGE holds, then one open message -// naming every open pull request into trunk it read, those whose head did not differ among them. -func TestSyncOpenWritesPullsForNewHeadsThenOneOpenMessageNamingAll(t *testing.T) { // 1279/AC6 - pub := &fakePub{} - call := func(context.Context, string, any) (json.RawMessage, error) { return openAnswer(), nil } - heads := newHeads("novox.mesh-delivery.12", head) - var logs bytes.Buffer - if err := runSync(call, pub, heads, &sync.Mutex{}, log.New(&logs, "", 0)); err != nil { - t.Fatal(err) - } - if heads.get("novox.mesh-catalog.300") != strings.Repeat("a", 40) || heads.get("novox.mesh-delivery.12") != head { - t.Errorf("the bucket after the sync: %v, want the head written for mesh-catalog#300 kept beside the held one", heads.vals) - } - if len(pub.subjects) != 2 { - t.Fatalf("%d messages written (%v), want the one pull message that differs and the open message", len(pub.subjects), pub.subjects) - } - if pub.subjects[0] != "mesh.stream.mesh-delivery-forge-bridge.event.forge.novox.mesh-catalog.pull" { - t.Errorf("first message on %s, want mesh-catalog's pull message", pub.subjects[0]) - } - if pub.subjects[1] != "mesh.stream.mesh-delivery-forge-bridge.event.forge.open" { - t.Fatalf("last message on %s, want the open message after the pull messages", pub.subjects[1]) - } - f, err := forgemsg.DecodeForge(pub.data[1]) - want := []forgemsg.OpenPull{{Repository: "novox/mesh-catalog", Number: 300}, {Repository: "novox/mesh-delivery", Number: 12}} - if err != nil || len(f.Open) != 2 || f.Open[0] != want[0] || f.Open[1] != want[1] { - t.Errorf("open message = %+v err %v, want it to name %v, the pull request whose head did not differ among them", f, err, want) - } -} - -// The bucket cannot be read: every open pull request is written, as before, and the open message follows; the -// indexer's writes are idempotent, so the fault costs messages and no state. -func TestSyncOpenWritesAllWhenTheHeadsCannotBeRead(t *testing.T) { // 1279/AC6 - pub := &fakePub{} - call := func(context.Context, string, any) (json.RawMessage, error) { return openAnswer(), nil } - heads := newHeads("novox.mesh-delivery.12", head) - heads.getErr = errors.New("the bucket is gone") - var logs bytes.Buffer - if err := runSync(call, pub, heads, &sync.Mutex{}, log.New(&logs, "", 0)); err != nil { - t.Fatal(err) - } - if len(pub.subjects) != 3 || !strings.Contains(logs.String(), "the bucket is gone") { - t.Errorf("messages %v, log %q", pub.subjects, logs.String()) - } -} - -// What the bridge writes on FORGE it keeps in the bucket: a head by its pull message, nothing by a closed or merged one. -func TestWhatIsWrittenOnFORGEIsKeptInTheBucket(t *testing.T) { // 1279/AC6 - pub := &fakePub{} - heads := newHeads() - r := &recorder{pub: pub, heads: heads, logger: log.New(&bytes.Buffer{}, "", 0)} - write := func(f forgemsg.Forge) { - t.Helper() - ev, _ := forgemsg.ForgeEvent(f.Repository, f.Kind) - raw, err := forgemsg.EncodeForge(f) - if err != nil { - t.Fatal(err) - } - if _, err := r.Publish(context.Background(), forgeSubject(ev), raw); err != nil { - t.Fatal(err) - } - } - p := forgemsg.Forge{Kind: forgemsg.ForgePull, Repository: "novox/mesh-delivery", Branch: "feat/x", Number: 12, Head: head, Action: forgemsg.PullUpdated} - write(p) - if heads.get("novox.mesh-delivery.12") != head { - t.Fatalf("bucket %v", heads.vals) - } - p.Action = forgemsg.PullClosed - write(p) - if _, kept := heads.vals["novox.mesh-delivery.12"]; kept { - t.Errorf("a closed pull request is still kept: %v", heads.vals) - } - p.Action = forgemsg.PullUpdated - write(p) - write(forgemsg.Forge{Kind: forgemsg.ForgeMerged, Repository: "novox/mesh-delivery", Branch: "feat/x", Number: 12, Head: head, Merge: "m", Root: "r", Trees: map[string]string{"": "t"}}) - if _, kept := heads.vals["novox.mesh-delivery.12"]; kept { - t.Errorf("a merged pull request is still kept: %v", heads.vals) - } - if len(pub.subjects) != 4 { - t.Errorf("%d messages written", len(pub.subjects)) - } -} - -// 1279/AC6: no forge event is heard between reading the forge and writing the open message. -func TestNoForgeEventIsHeardWhileTheOpenPullsAreSaid(t *testing.T) { // 1279/AC6 - pub := &lockedPub{} - reading := make(chan struct{}) - release := make(chan struct{}) - call := func(context.Context, string, any) (json.RawMessage, error) { - close(reading) - <-release - return openAnswer(), nil - } - var mu sync.Mutex - synced := make(chan error, 1) - go func() { - synced <- runSync(call, pub, newHeads(), &mu, log.New(&bytes.Buffer{}, "", 0)) - }() - <-reading - // An event arrives while the forge is being read. - heard := make(chan struct{}) - go func() { - hear(&mu, func() { _, _ = pub.Publish(context.Background(), "event", nil); close(heard) }) - }() - select { - case <-heard: - t.Fatal("an event was heard while the forge was read") - case <-time.After(100 * time.Millisecond): - } - close(release) - if err := <-synced; err != nil { - t.Fatal(err) - } - <-heard - got := pub.snapshot() - if len(got) != 4 || got[2] != "mesh.stream.mesh-delivery-forge-bridge.event.forge.open" || got[3] != "event" { - t.Errorf("order of writes: %v, want the event after the open message", got) - } -} - -type lockedPub struct { - mu sync.Mutex - subjects []string -} - -func (p *lockedPub) Publish(_ context.Context, subject string, _ []byte, _ ...jetstream.PublishOpt) (*jetstream.PubAck, error) { - p.mu.Lock() - defer p.mu.Unlock() - p.subjects = append(p.subjects, subject) - return &jetstream.PubAck{}, nil -} -func (p *lockedPub) snapshot() []string { - p.mu.Lock() - defer p.mu.Unlock() - return append([]string(nil), p.subjects...) -} - -func TestSyncOpenFailureNamesTheCallAndTheForgesWords(t *testing.T) { // 1237/AC3 - pub := &fakePub{} - call := func(context.Context, string, any) (json.RawMessage, error) { - return nil, errors.New("the forge said 502") - } - err := runSync(call, pub, newHeads(), &sync.Mutex{}, log.New(&bytes.Buffer{}, "", 0)) - if err == nil || !strings.Contains(err.Error(), "seat:git.open-pulls") || !strings.Contains(err.Error(), "the forge said 502") { - t.Errorf("err = %v", err) - } - if len(pub.subjects) != 0 { - t.Errorf("messages written: %v", pub.subjects) - } -} - -func TestReconnectReadsTheOpenPullsAgainAndFailureGoesOn(t *testing.T) { // 1237/AC2, AC3 - var logs lockedBuffer - ctx, cancel := context.WithCancel(context.Background()) - again := make(chan struct{}, 1) - done := make(chan struct{}) - var mu sync.Mutex - n := 0 - count := func() int { mu.Lock(); defer mu.Unlock(); return n } - read := func(context.Context) error { - mu.Lock() - defer mu.Unlock() - n++ - if n == 1 { - return errors.New("the forge said 502") - } - return nil - } - go func() { onReconnect(ctx, again, read, log.New(&logs, "", 0)); close(done) }() - again <- struct{}{} - waitFor(t, func() bool { return count() >= 1 }) - again <- struct{}{} // the first read failed; the bridge goes on and reads again at the next reconnect - waitFor(t, func() bool { return count() >= 2 }) - cancel() - <-done - if !strings.Contains(logs.String(), "the forge said 502") { - t.Errorf("log %q does not carry the forge's words", logs.String()) - } -} - -type lockedBuffer struct { - mu sync.Mutex - b bytes.Buffer -} - -func (l *lockedBuffer) Write(p []byte) (int, error) { - l.mu.Lock() - defer l.mu.Unlock() - return l.b.Write(p) -} -func (l *lockedBuffer) String() string { l.mu.Lock(); defer l.mu.Unlock(); return l.b.String() } - -func waitFor(t *testing.T, ok func() bool) { - t.Helper() - for deadline := time.Now().Add(5 * time.Second); time.Now().Before(deadline); time.Sleep(5 * time.Millisecond) { - if ok() { - return - } - } - t.Fatal("it did not happen within 5 s") -} diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/owed.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/owed.go index 0ff09c15..35008636 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/owed.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/owed.go @@ -7,16 +7,12 @@ import ( "errors" "fmt" "log" - "strconv" - "strings" "time" "github.com/nats-io/nats.go/jetstream" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" git "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" - checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" "git.novox.be/novox/mesh-sdk/go/process" @@ -28,7 +24,7 @@ import ( // The state performed (manifest `requires`, the template state, opened by its binding: bound.go) keeps one key per owed act it // performed, the act's outcome and when, with the owed event as heard (novox/hq design 47 *What the gate owes the // forge*). It is a saving and a read, never what keeps an act from happening twice: every act is safe to repeat at the -// forge. The group ends' bookkeeping (`published.`, `groups.`, groups.go) is in it too, under the same 7 days. +// forge. // performedKey is the key of one act in performed: the seat that said it and its id, the id in unpadded base64url, since // an id may hold what a key may not (`#`, `/`). @@ -67,7 +63,7 @@ func (s kvPerformed) Put(ctx context.Context, key string, value []byte) error { return err } -// owing performs what the gate and the checker owe the forge (design 47 *What the gate owes the forge*): each owed act +// owing performs what the gate owes the forge (design 47 *What the gate owes the forge*): each owed act // through the seat git's verb under its id, its outcome said as owed.ended on the stream forge and then kept in // performed. It holds no timer. type owing struct { @@ -85,8 +81,6 @@ func readerOf(of string) (func([]byte) (bridge.OwedCall, error), error) { switch of { case seat.Seat: return bridge.GateOwed, nil - case checker.Seat: - return bridge.CheckerOwed, nil } return nil, fmt.Errorf("the seat %q says no owed act the bridge performs", of) } @@ -110,34 +104,9 @@ func (o *owing) heard(ctx context.Context, of string, data []byte) (forgemsg.Owe // said twice, or said again while its outcome was on its way: performed once, the outcome said once return forgemsg.OwedEnded{Of: c.Of, ID: c.ID}, nil } - if words, done := o.switchedDone(ctx, c.Of, c.ID); done { - // performed by the old bridge before the switch: said performed, never done twice - return o.end(ctx, forgemsg.OwedEnded{Of: c.Of, ID: c.ID, - Outcome: forgemsg.OwedPerformed, Words: words}, data) - } return o.perform(ctx, c, data) } -// switchedDone says whether a switched act (design 47 *The switch carries what is owed*: the gate's act of a key of its -// old state owed or merges, its id meshdeliveryclient.SwitchedActID) was performed by the old bridge: its own bookkeeping -// in performed, `.`, holds that key at the act's revision or a later one. Any other id, and bookkeeping that -// cannot be read, is not: the act is performed, which changes nothing at the forge. -func (o *owing) switchedDone(ctx context.Context, of, id string) (string, bool) { - state, key, rev, ok := meshdeliveryclient.ParseSwitchedActID(id) - if !ok || of != seat.Seat { - return "", false - } - kept, found, err := o.performed.Get(ctx, state+"."+key) - if err != nil || !found { - return "", false - } - at, err := strconv.ParseUint(strings.TrimSpace(string(kept)), 10, 64) - if err != nil || at < rev { - return "", false - } - return fmt.Sprintf("performed by the bridge before the switch: its %s.%s at revision %d", state, key, at), true -} - // hear is one message of a seat's stream owed: performed and acknowledged, or, when its outcome could not be said, // left to come again (design 47: the bridge stopped between an act and its owed.ended performs it again). func (o *owing) hear(ctx context.Context, of string, m delivered) { @@ -206,36 +175,7 @@ func (o *owing) held(ctx context.Context, of, id string) bool { return found } -// catchUp reads the seat mesh-delivery's groups and performs every act said there with no recorded outcome that -// performed does not hold either (design 47 *The bridge catches up from the seat*). It answers how many it performed. -func (o *owing) catchUp(ctx context.Context) (int, error) { - if w := o.waiting(); w != "" { - return 0, fmt.Errorf("the bridge %s", w) - } - of, err := groups.Read(ctx, o.call) - if err != nil { - return 0, err - } - n := 0 - for _, g := range of { - for _, act := range g.Acts { - if act.Outcome != "" || o.held(ctx, seat.Seat, act.Owed.ID) { - continue // answered, or performed and its outcome on its way to the gate - } - data, err := json.Marshal(act.Owed) - if err != nil { - return n, err - } - if _, err := o.heard(ctx, seat.Seat, data); err != nil { - return n, err - } - n++ - } - } - return n, nil -} - -// owedOf is the owed event of an act to perform again: as performed keeps it, else, for the gate's, as its groups say it. +// owedOf is the owed event of an act to perform again, as performed keeps it. func (o *owing) owedOf(ctx context.Context, of, id string) ([]byte, error) { v, found, err := o.performed.Get(ctx, performedKey(of, id)) if err != nil { @@ -247,25 +187,7 @@ func (o *owing) owedOf(ctx context.Context, of, id string) ([]byte, error) { return p.Owed, nil } } - if of != seat.Seat { - return nil, fmt.Errorf("the bridge holds no owed act %s of %s: the checker says its unanswered acts again at its start", id, of) - } - raw, err := o.call(ctx, "seat:"+seat.Seat+"."+seat.VerbGroups, seat.GroupsInput{All: seat.GroupsInputAllTrue}) - if err != nil { - return nil, fmt.Errorf("the seat %s's groups could not be read: %w", seat.Seat, err) - } - var a seat.GroupsAnswer - if err := json.Unmarshal(raw, &a); err != nil || a.Error != "" { - return nil, fmt.Errorf("the seat %s's groups do not answer: %v %s", seat.Seat, err, a.Error) - } - for _, g := range a.Groups { - for _, act := range g.Acts { - if act.Owed.ID == id { - return json.Marshal(act.Owed) - } - } - } - return nil, fmt.Errorf("neither the bridge nor the seat %s's groups hold an owed act %s", seat.Seat, id) + return nil, fmt.Errorf("the bridge holds no owed act %s of %s in its state performed", id, of) } // answer answers one owed ask: an act performed again now, whatever performed holds of it; an act dropped, said as @@ -330,7 +252,7 @@ func (o *owing) asked(ctx context.Context, kv jetstream.KeyValue, key string, re func owedBacklog(js jetstream.JetStream, cred process.Account, now func() time.Time) func(context.Context) []groups.Backlog { return func(ctx context.Context) []groups.Backlog { var out []groups.Backlog - for _, kept := range []string{seat.StreamOwed, checker.StreamOwed} { + for _, kept := range []string{seat.StreamOwed} { row := groups.Backlog{Stream: kept, Consumer: cred.EventConsumerName()} c, err := js.Consumer(ctx, row.Stream, row.Consumer) var info *jetstream.ConsumerInfo diff --git a/modules/mesh-delivery-forge-bridge/forgemsg/owed.go b/modules/mesh-delivery-forge-bridge/forgemsg/owed.go index b8400a36..e67e3bbe 100644 --- a/modules/mesh-delivery-forge-bridge/forgemsg/owed.go +++ b/modules/mesh-delivery-forge-bridge/forgemsg/owed.go @@ -27,8 +27,7 @@ const ( var OwedOutcomes = []string{OwedPerformed, OwedRefused, OwedCouldNotBePerformed, OwedUndecodable, OwedDropped} // OwedEnded is an owed act's outcome (design 47 G §164): the act's id and one outcome, with the words of any outcome -// but performed. Of is the seat that said the act (mesh-delivery for the gate's, mesh-delivery-checker for the -// checker's), so each hears its own ids (§164). An act the bridge cannot decode may name no id. +// but performed. Of is the seat that said the act (mesh-delivery for the gate's), so it hears its own ids (§164). An act the bridge cannot decode may name no id. type OwedEnded struct { V int `json:"v"` Of string `json:"of"` diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/bridge.go b/modules/mesh-delivery-forge-bridge/internal/bridge/bridge.go index 21648ba3..92ab3a1b 100644 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/bridge.go +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/bridge.go @@ -1,65 +1,19 @@ -// Package bridge turns the seat git's pull request events into the messages the forge bridge writes on FORGE. +// Package bridge reads the seat git's branch-moved and the acts owed to the forge, and makes the messages the forge +// bridge writes on its stream forge: head-moved (design 47 H §739) and owed.ended. package bridge import ( - "crypto/sha256" - "encoding/hex" - "encoding/json" "errors" "fmt" - "maps" - "path" "regexp" - "slices" - "strconv" - "strings" - "sync" - - git "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" - "git.novox.be/novox/mesh-sdk/go/catalogue" - "git.novox.be/novox/mesh-sdk/go/keys" ) // ErrUnreadable is wrapped by every error that says an event cannot be turned into a message; it is never // redelivered. var ErrUnreadable = errors.New("unreadable event") -// ErrNotHandled says the event is one the bridge hears but writes nothing for (yet). -var ErrNotHandled = errors.New("event not handled") - -// ErrRetry says the event is readable but the forge could not be asked what the message needs; the event is to be -// heard again later. -var ErrRetry = errors.New("the forge could not say") - -// Watching says whether the bridge watches a branch of a repository (Watched; design 47 G §507, §532). -type Watching interface { - Watches(repository, branch string) bool -} - -// Trees asks the forge for the trees of a commit: the root tree and the tree of every directory holding a module.json -// (the root directory spelled ""), as the seat git's verb trees answers (issue 1234/AC4). -type Trees func(owner, repo, sha string) (root string, trees map[string]string, err error) - -// ErrNotFound says the forge has no file at that path and ref (the forge's "404", issue 1448). -var ErrNotFound = errors.New("the forge has no such file") - -// Files asks the forge for one file's bytes at a ref (a commit), as the seat git's verb file answers; a file the forge -// says is not there is ErrNotFound, any other failure any other error (issue 1448). -type Files func(owner, repo, path, ref string) ([]byte, error) - -// CatalogRepository is the catalogue, whose merged message carries its predefined list, and PredefinedFile that list's -// file at its root (novox/hq ADR 0565 §2, ADR 0348 §7). -const ( - CatalogRepository = "novox/mesh-catalog" - PredefinedFile = "predefined.json" -) - -// LockFiles are the lock files read beside a module's manifest, by name relative to its directory (novox/hq ADR 0465 -// clause 1: go.sum for a Go module, package-lock.json for a Node one). -var LockFiles = []string{"go.sum", "package-lock.json"} - -// Message is a message for FORGE: the event name (forge...) and its JSON body. +// Message is a message for the stream forge: the event name (forge....head-moved) and its +// JSON body. type Message struct { Event string Data []byte @@ -67,510 +21,8 @@ type Message struct { Said string } -// The subjects of the seat git's events the bridge writes pull messages for (novox/hq design 47 *The seat `git`*): the -// seat's holder says them on mesh.seat.git.event., whichever module holds it; the bridge names none. -const ( - UpdatedSubject = git.SubjectPullUpdated - ClosedSubject = git.SubjectPullClosed - MergedSubject = git.SubjectPullMerged - // DeletedSubject is the seat's forge.delete: a branch or a tag deleted (repository, ref, ref-type). Only the seat's - // holder may publish on the seat's subjects (novox/hq ADR 0363 §2), so no other module can end a branch's tagged - // versions. - DeletedSubject = git.SubjectForgeDelete -) - -// ForgeBranchDeleted is the kind of a branch-deleted message on FORGE (novox/hq design 51 *The forge bridge says a -// branch's end*): the builder removes the branch's tagged versions on it, as on a pull request's merged or closed. -// Every existing consumer of FORGE ignores a kind it does not know. -const ForgeBranchDeleted = "branch-deleted" - -// BranchDeleted is the body of a branch-deleted message. -type BranchDeleted struct { - V int `json:"v"` - Kind string `json:"kind"` - Repository string `json:"repository"` - Branch string `json:"branch"` - // Head is the commit the branch named when it was deleted, when the forge said it. - Head string `json:"head,omitempty"` -} - -// branchDeleted turns forge.delete into a branch-deleted message; a deleted tag is not handled. -func branchDeleted(subject string, data []byte, watched Watching) (Message, error) { - var e git.ForgeDeleteEvent - if err := json.Unmarshal(data, &e); err != nil { - return Message{}, unreadable(subject, "the body is not json: %v", err) - } - d := struct{ Owner, Repo, Ref, RefType string }{Ref: e.Ref, RefType: e.RefType} - d.Owner, d.Repo, _ = strings.Cut(e.Repository, "/") - if d.RefType != "branch" { - return Message{}, fmt.Errorf("%w: %s of a %s (%s)", ErrNotHandled, subject, d.RefType, d.Ref) - } - branch := strings.TrimPrefix(d.Ref, "refs/heads/") - switch { - case d.Owner == "" || d.Repo == "" || strings.ContainsAny(d.Owner+d.Repo, "./ *>"): - return Message{}, unreadable(subject, "no owner or repo a forge event can name (%q/%q)", d.Owner, d.Repo) - case branch == "": - return Message{}, unreadable(subject, "no branch") - case watched.Watches(d.Owner+"/"+d.Repo, branch): - return Message{}, fmt.Errorf("%w: %s of the watched branch %s", ErrNotHandled, subject, branch) - } - body, err := json.Marshal(BranchDeleted{V: keys.Version, Kind: ForgeBranchDeleted, Repository: d.Owner + "/" + d.Repo, Branch: branch}) - if err != nil { - return Message{}, unreadable(subject, "%v", err) - } - return Message{Event: "forge." + d.Owner + "." + d.Repo + "." + ForgeBranchDeleted, Data: body, - Said: fmt.Sprintf("branch %s of %s/%s deleted: its tagged versions end", branch, d.Owner, d.Repo)}, nil -} - var sha40 = regexp.MustCompile(`^[0-9a-f]{40}$`) -// pull is the part of the seat git's pull.updated, pull.merged and pull.closed the bridge reads: Head is the branch and -// HeadSHA the commit, Owner and Repo the repository's two halves. -type pull struct { - Owner, Repo string - Number int - Head, HeadSHA string - ModuleDirs []string - ModuleDirsSaid bool - Base string - MergeSHA string -} - -// pullOf reads one of the seat's pull events. The directories are the seat's word only when its change says them: an -// absent `module-dirs` is "could not be read", never "none". -func pullOf(data []byte) (pull, error) { - var e struct { - Repository string `json:"repository"` - Number int64 `json:"number"` - Branch string `json:"branch"` - Head string `json:"head"` - Base string `json:"base"` - MergeCommit string `json:"merge-commit"` - Change json.RawMessage `json:"change"` - } - if err := json.Unmarshal(data, &e); err != nil { - return pull{}, err - } - p := pull{Number: int(e.Number), Head: e.Branch, HeadSHA: e.Head, Base: e.Base, MergeSHA: e.MergeCommit} - p.Owner, p.Repo, _ = strings.Cut(e.Repository, "/") - if len(e.Change) > 0 && string(e.Change) != "null" { - var c map[string]json.RawMessage - if err := json.Unmarshal(e.Change, &c); err != nil { - return pull{}, fmt.Errorf("its change: %w", err) - } - if raw, said := c["module-dirs"]; said { - if err := json.Unmarshal(raw, &p.ModuleDirs); err != nil { - return pull{}, fmt.Errorf("its change's module-dirs: %w", err) - } - p.ModuleDirsSaid = true - } - } - return p, nil -} - func unreadable(subject, why string, args ...any) error { return fmt.Errorf("%w: %s: %s", ErrUnreadable, subject, fmt.Sprintf(why, args...)) } - -// Convert turns one pull request event of the seat git, by its bus subject and body, into a message. An event it cannot -// read is an ErrUnreadable error naming the subject and why; an event it writes nothing for is ErrNotHandled. A pull -// request into a branch the bridge does not watch is no candidate: written closed. A merge into a watched branch writes -// nothing here: its merged message is the move's, heard on branch-moved (MoveMessage; design 47 G §509, §714); a merge -// into another branch is a closed pull message, as the pull request left its group. A forge that cannot say is ErrRetry. -func Convert(subject string, data []byte, files Files, watched Watching) (Message, error) { - var action string - switch subject { - case UpdatedSubject: - action = forgemsg.PullUpdated - case ClosedSubject, MergedSubject: - action = forgemsg.PullClosed - case DeletedSubject: - return branchDeleted(subject, data, watched) - default: - return Message{}, fmt.Errorf("%w: %s", ErrNotHandled, subject) - } - p, err := pullOf(data) - if err != nil { - return Message{}, unreadable(subject, "the body is not the seat's json: %v", err) - } - switch { - case p.Owner == "" || p.Repo == "" || strings.Contains(p.Repo, "/"): - return Message{}, unreadable(subject, "no owner or repo") - case p.Number <= 0: - return Message{}, unreadable(subject, "no pull request number") - case !sha40.MatchString(p.HeadSHA): - return Message{}, unreadable(subject, "the head %q is not 40 hex digits", p.HeadSHA) - case p.Head == "": - return Message{}, unreadable(subject, "no branch") - } - into := p.Base != "" && watched.Watches(p.Owner+"/"+p.Repo, p.Base) - if subject == MergedSubject && into { - return Message{}, fmt.Errorf("%w: %s of %s/%s#%d into the watched branch %s: its merged message is written from the branch's move", ErrNotHandled, subject, p.Owner, p.Repo, p.Number, p.Base) - } - said := "" - if action == forgemsg.PullUpdated && p.Base != "" && !into { - // A pull request into a branch no registration watches is no candidate, as it is none at the start (OpenPulls) and at - // its merge. It is written closed, so a candidate an older head made is taken out, and said (issue 1422). - action = forgemsg.PullClosed - said = fmt.Sprintf("%s/%s#%d at %s is into %s, which is not watched: written closed, so it is no candidate", - p.Owner, p.Repo, p.Number, p.HeadSHA[:12], p.Base) - } - f := forgemsg.Forge{Kind: forgemsg.ForgePull, Repository: p.Owner + "/" + p.Repo, Branch: p.Head, Number: p.Number, - Head: p.HeadSHA, Action: action} - // The directories are the forge's word only when it said it could read them (module_dirs_said). - if action == forgemsg.PullUpdated && p.ModuleDirsSaid { - f.Dirs, f.DirsKnown = p.ModuleDirs, true - var err error - if f.Manifests, f.Locks, f.Needs, _, err = inputsRead(p.Owner, p.Repo, p.HeadSHA, p.ModuleDirs, files); err != nil { - return Message{}, fmt.Errorf("%w: %s: the manifests of %s/%s at the head %s: %v", ErrRetry, subject, p.Owner, p.Repo, p.HeadSHA, err) - } - } - event, err := forgemsg.ForgeEvent(f.Repository, forgemsg.ForgePull) - if err != nil { - return Message{}, unreadable(subject, "%v", err) - } - body, err := forgemsg.EncodeForge(f) - if err != nil { - return Message{}, unreadable(subject, "%v", err) - } - return Message{Event: event, Data: body, Said: said}, nil -} - -// MaxReadsInFlight is how many files of the forge inputsOf asks for at once. A merge into a repository of many -// modules (131 in the catalogue) reads three files for each, one after another inside the bound of a handled event -// (30 s) that leaves about 76 ms a call; eight at once keeps the forge's load small and the read well inside the bound -// (issue 1458). -const MaxReadsInFlight = 8 - -// read is one file asked for: the path in the directory dir, and whether it is the module's manifest. -type read struct { - dir, path string - manifest bool - asked bool - raw []byte - err error -} - -// inputsOf reads, at ref, the module.json of each directory (the root spelled "") and the lock files beside it -// (LockFiles): the manifests as the forge answered them and the lock files' hex sha256 by name. A manifest the forge -// does not have is an error, since the directory was said to hold one; a lock file it does not have is left out; any other -// failure is an error (issue 1448). -// -// The files are asked for with at most MaxReadsInFlight calls at once (issue 1458). The result does not depend on the -// order the calls finish in, and the error is that of the first failing file in the order of dirs and LockFiles. After -// a failure no further file is asked for, and inputsOf returns only when every call it started has returned. -func inputsOf(owner, repo, ref string, dirs []string, files Files) (manifests map[string]string, locks map[string]map[string]string, err error) { - manifests, locks, _, _, err = inputsRead(owner, repo, ref, dirs, files) - return manifests, locks, err -} - -// inputsRead is inputsOf, and also answers, on a failure too, the directories whose manifest and lock files were all -// read, with their manifests and lock hashes: a merge keeps them by their tree, so its next try reads only the rest -// (issue 1126). It reads no mesh package a go.sum names: the indexer alone computes what a fingerprint takes (novox/hq -// ADR 0593 §2), so the message's Packages stay empty. Of a directory whose manifest names templates it reads each -// template too, in a second round, and answers the module's needs whole (catalogue.ModuleNeeds; novox/hq ADR 0632 §2): -// a template's text can need a reading its manifest does not. A template the forge does not have is no need (the judge -// refuses the manifest by name); any other failure to read one is the directory's, as a lock file's is. -func inputsRead(owner, repo, ref string, dirs []string, files Files) (manifests map[string]string, locks map[string]map[string]string, needs map[string][]string, whole []string, err error) { - at := func(dir, name string) string { - if dir == "" { - return name - } - return dir + "/" + name - } - var reads []*read - for _, dir := range dirs { - reads = append(reads, &read{dir: dir, path: at(dir, "module.json"), manifest: true}) - for _, name := range LockFiles { - reads = append(reads, &read{dir: dir, path: at(dir, name)}) - } - } - fetch(owner, repo, ref, reads, files) - manifests = map[string]string{} - locks = map[string]map[string]string{} - broken := map[string]bool{} // a directory with a file not read (failed, or never asked after a failure) - failed := func(r *read) bool { - switch { - case r.err != nil && (r.manifest || !errors.Is(r.err, ErrNotFound)): - broken[r.dir] = true - if err == nil { - err = fmt.Errorf("%s: %w", r.path, r.err) - } - case r.err == nil && r.raw == nil && !r.asked: - broken[r.dir] = true - default: - return false - } - return true - } - for _, r := range reads { - switch { - case failed(r): - case r.err != nil: // a lock file the forge does not have - case r.manifest: - manifests[r.dir] = string(r.raw) - default: - sum := sha256.Sum256(r.raw) - if locks[r.dir] == nil { - locks[r.dir] = map[string]string{} - } - locks[r.dir][path.Base(r.path)] = hex.EncodeToString(sum[:]) - } - } - var templates []*read - if err == nil { - for _, dir := range dirs { - for _, t := range templatePaths(manifests[dir]) { - templates = append(templates, &read{dir: dir, path: at(dir, t)}) - } - } - fetch(owner, repo, ref, templates, files) - } - texts := map[string]map[string]string{} - for _, r := range templates { - if failed(r) || r.err != nil { - continue - } - if texts[r.dir] == nil { - texts[r.dir] = map[string]string{} - } - texts[r.dir][strings.TrimPrefix(r.path, at(r.dir, ""))] = string(r.raw) - } - needs = map[string][]string{} - for _, dir := range dirs { - if raw, ok := manifests[dir]; ok && !broken[dir] && len(templatePaths(raw)) > 0 { - needs[dir], _ = catalogue.ModuleNeeds([]byte(raw), texts[dir]) - } - } - for _, dir := range dirs { - if !broken[dir] { - whole = append(whole, dir) - } - } - if err != nil { - for dir := range broken { - delete(manifests, dir) - delete(locks, dir) - delete(needs, dir) - } - return manifests, locks, needs, whole, err - } - if len(manifests) == 0 { - manifests = nil - } - if len(locks) == 0 { - locks = nil - } - if len(needs) == 0 { - needs = nil - } - return manifests, locks, needs, whole, nil -} - -// templatePaths is the templates a manifest names (catalogue.TemplatePaths), none for one that does not decode. -func templatePaths(raw string) []string { - if raw == "" { - return nil - } - m, err := catalogue.Decode([]byte(raw)) - if err != nil { - return nil - } - return catalogue.TemplatePaths(m) -} - -// fetch asks the forge for each of reads at ref, at most MaxReadsInFlight at once; after a failure (a manifest, or a -// file the forge has and could not give) no further file is asked for, and it returns once every call it started has. -func fetch(owner, repo, ref string, reads []*read, files Files) { - var ( - wg sync.WaitGroup - mu sync.Mutex - failed bool - slots = make(chan struct{}, MaxReadsInFlight) - ) - for _, r := range reads { - slots <- struct{}{} - mu.Lock() - stop := failed - mu.Unlock() - if stop { - <-slots - break - } - r.asked = true - wg.Add(1) - go func(r *read) { - defer func() { <-slots; wg.Done() }() - r.raw, r.err = files(owner, repo, r.path, ref) - if r.err != nil && (r.manifest || !errors.Is(r.err, ErrNotFound)) { - mu.Lock() - failed = true - mu.Unlock() - } - }(r) - } - wg.Wait() -} - -// landed is the message of a commit that landed on a watched branch, merged or pushed (f says which and what names it): -// the trees of the commit, asked once, and the take-in inputs of every module directory at it (design 47 G §509, A-236). -func landed(what string, f forgemsg.Forge, commit string, trees Trees, files Files, kept Kept) (Message, error) { - if !sha40.MatchString(commit) { - return Message{}, unreadable(what, "the commit %q is not 40 hex digits", commit) - } - repository := f.Repository - owner, repo, _ := strings.Cut(repository, "/") - root, dirs, err := trees(owner, repo, commit) - if err != nil { - return Message{}, fmt.Errorf("%w: %s: the trees of %s at %s: %v", ErrRetry, what, repository, commit, err) - } - names := make([]string, 0, len(dirs)) - for dir := range dirs { - names = append(names, dir) - } - slices.Sort(names) - manifests, locks, needs, toRead := keptInputs(names, dirs, kept) - read, readLocks, readNeeds, whole, err := inputsRead(owner, repo, commit, toRead, files) - if err != nil { - keep(whole, dirs, read, readLocks, readNeeds, kept) // what was read whole is not asked again at the next try - return Message{}, fmt.Errorf("%w: %s: the manifests of %s at %s (%d of %d module directories read from the forge, the others kept by their tree): %v", ErrRetry, what, repository, commit, len(toRead), len(names), err) - } - keep(toRead, dirs, read, readLocks, readNeeds, kept) - maps.Copy(manifests, read) - maps.Copy(locks, readLocks) - maps.Copy(needs, readNeeds) - if len(manifests) == 0 { - manifests = nil - } - if len(locks) == 0 { - locks = nil - } - if len(needs) == 0 { - needs = nil - } - event, err := forgemsg.ForgeEvent(repository, f.Kind) - if err != nil { - return Message{}, unreadable(what, "%v", err) - } - f.Root, f.Trees, f.Manifests, f.Locks, f.Needs = root, dirs, manifests, locks, needs - if repository == CatalogRepository { - // The catalogue's predefined list at the commit (novox/hq ADR 0565 §2): the indexer keeps it for mesh-config. - raw, err := files(owner, repo, PredefinedFile, commit) - switch { - case errors.Is(err, ErrNotFound): - f.PredefinedRead = true - case err != nil: - return Message{}, fmt.Errorf("%w: %s: %s of %s at %s: %v", ErrRetry, what, PredefinedFile, repository, commit, err) - default: - f.Predefined = json.RawMessage(raw) - } - } - body, err := forgemsg.EncodeForge(f) - if err != nil { - return Message{}, unreadable(what, "%v", err) - } - msg := Message{Event: event, Data: body} - if len(dirs) == 0 { - msg.Said = fmt.Sprintf("%s at %s holds no module.json: a %s message with no module directories", repository, commit, f.Kind) - } - return msg, nil -} - -// openPulls reads the seat git's answer of open-pulls: an answer saying an error is the error, never none open. -func openPulls(answer []byte) ([]pull, error) { - var a git.OpenPullsAnswer - if err := json.Unmarshal(answer, &a); err != nil { - return nil, fmt.Errorf("the seat git's answer of open-pulls is not its contract's: %v", err) - } - if a.Error != "" { - return nil, fmt.Errorf("the seat git refused open-pulls: %s", a.Error) - } - var said map[string]json.RawMessage - if _ = json.Unmarshal(answer, &said); said["pulls"] == nil || string(said["pulls"]) == "null" { - // None open is `pulls: []`; an answer naming no list would drop every candidate on a holder's fault. - return nil, fmt.Errorf("open-pulls: %w", errNoAnswer) - } - out := make([]pull, 0, len(a.Pulls)) - for _, o := range a.Pulls { - p := pull{Number: int(o.Pull.Number), Head: o.Pull.Branch, HeadSHA: o.Pull.Head, Base: o.Pull.Base} - p.Owner, p.Repo, _ = strings.Cut(o.Pull.Repository, "/") - out = append(out, p) - } - return out, nil -} - -// OpenPulls turns the seat git's answer of open-pulls into one pull message per open pull request into -// a watched branch, action updated and its directories not known (the forge's answer does not say them). A pull request into -// another branch gets none. An entry that cannot be read is named in skipped and gets none; an answer that is not -// the contract's, or that says an error, is an error. -func OpenPulls(answer []byte, watched Watching) (msgs []Message, skipped []string, err error) { - pulls, err := openPulls(answer) - if err != nil { - return nil, nil, err - } - for _, p := range pulls { - if !watched.Watches(p.Owner+"/"+p.Repo, p.Base) { - continue - } - name := fmt.Sprintf("%s/%s#%d", p.Owner, p.Repo, p.Number) - if p.Owner == "" || p.Repo == "" || p.Number <= 0 || p.Head == "" || !sha40.MatchString(p.HeadSHA) { - skipped = append(skipped, name+": it names no owner, repo, number, branch or 40-hex head") - continue - } - f := forgemsg.Forge{Kind: forgemsg.ForgePull, Repository: p.Owner + "/" + p.Repo, Branch: p.Head, Number: p.Number, - Head: p.HeadSHA, Action: forgemsg.PullUpdated} - event, err := forgemsg.ForgeEvent(f.Repository, forgemsg.ForgePull) - var body []byte - if err == nil { - body, err = forgemsg.EncodeForge(f) - } - if err != nil { - skipped = append(skipped, name+": "+err.Error()) - continue - } - msgs = append(msgs, Message{Event: event, Data: body}) - } - return msgs, skipped, nil -} - -// Track says what a message the bridge wrote changes in the head it keeps for each pull request (the bucket heads, -// novox/hq ADR 0443 §5): the key `..`, and the head to keep under it, or "" to take the key away -// (a closed pull request, a merged one). ok is false for a message that keeps no head. -func Track(f forgemsg.Forge) (key, head string, ok bool) { - key = strings.ReplaceAll(f.Repository, "/", ".") + "." + strconv.Itoa(f.Number) - switch { - case f.Kind == forgemsg.ForgePull && f.Action != forgemsg.PullClosed: - return key, f.Head, true - case f.Kind == forgemsg.ForgePull || f.Kind == forgemsg.ForgeMerged: - return key, "", true - } - return "", "", false -} - -// OpenMessage turns the seat git's answer of open-pulls into the open message: every open pull request into a watched branch, by -// repository and number, whether its head reads or not and whether FORGE holds it or not, in repository and number -// order. A pull request into another branch is not named. An answer that names none is a message too: nothing is open. -func OpenMessage(answer []byte, watched Watching) (Message, error) { - pulls, err := openPulls(answer) - if err != nil { - return Message{}, err - } - open := []forgemsg.OpenPull{} - for _, p := range pulls { - if !watched.Watches(p.Owner+"/"+p.Repo, p.Base) || p.Owner == "" || p.Repo == "" || p.Number <= 0 { - continue - } - open = append(open, forgemsg.OpenPull{Repository: p.Owner + "/" + p.Repo, Number: p.Number}) - } - slices.SortFunc(open, func(a, b forgemsg.OpenPull) int { - if c := strings.Compare(a.Repository, b.Repository); c != 0 { - return c - } - return a.Number - b.Number - }) - body, err := forgemsg.EncodeForge(forgemsg.Forge{Kind: forgemsg.ForgeOpen, Open: open}) - if err != nil { - return Message{}, fmt.Errorf("the open pull requests cannot be said: %w", err) - } - return Message{Event: forgemsg.OpenEvent(), Data: body}, nil -} diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/bridge_test.go b/modules/mesh-delivery-forge-bridge/internal/bridge/bridge_test.go index dc0aaa35..23b12b42 100644 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/bridge_test.go +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/bridge_test.go @@ -7,420 +7,9 @@ import ( "strings" "testing" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" - "git.novox.be/novox/mesh-sdk/go/keys" - "mesh-delivery-forge-bridge/internal/gitfixture" ) -const head = "0123456789abcdef0123456789abcdef01234567" - -// updated is the body of the seat git's pull.updated (modules/mesh-delivery-contracts/seats/git): the pull request, its base's head and its -// change. extra names the parts a test changes by their short names (owner, repo, number, head for the branch, -// head_sha for the commit, base, module_dirs, module_dirs_said), a nil value leaving the part out (seatPull). -func updated(extra map[string]any) []byte { - return seatPull(map[string]any{"base_sha": "ffffffffffffffffffffffffffffffffffffffff", - "paths": []string{"modules/a/x.go"}, "module_dirs": []string{"modules/a"}, "module_dirs_said": true}, extra) -} - -// closed is the body of the seat git's pull.closed. -func closed() []byte { return seatPull(nil, nil) } - -// seatPull is a pull event of the seat git: the pull request novox/mesh-delivery#12 of feat/x at head into main, with -// the parts of base and extra laid over it by their short names, a nil value leaving the part out. module_dirs is said -// in the change only when module_dirs_said is true: its absence is "could not be read". -func seatPull(base, extra map[string]any) []byte { - m := map[string]any{"owner": "novox", "repo": "mesh-delivery", "number": 12, "title": "A change", "head": "feat/x", - "head_sha": head, "base": "main", "html_url": "https://forge.example/novox/mesh-delivery/pulls/12"} - for _, over := range []map[string]any{base, extra} { - for k, v := range over { - if v == nil { - delete(m, k) - } else { - m[k] = v - } - } - } - e := map[string]any{} - owner, _ := m["owner"].(string) - repo, _ := m["repo"].(string) - if owner != "" || repo != "" { - e["repository"] = owner + "/" + repo - } - for short, name := range map[string]string{"number": "number", "title": "title", "head": "branch", "head_sha": "head", - "base": "base", "html_url": "url", "merge_commit_sha": "merge-commit", "base_sha": "base-head", "merged_at": "merged-at"} { - if v, ok := m[short]; ok { - e[name] = v - } - } - if paths, ok := m["paths"]; ok { - c := map[string]any{"paths": paths} - if m["module_dirs_said"] == true && m["module_dirs"] != nil { - c["module-dirs"] = m["module_dirs"] - } - e["change"] = c - } - b, _ := json.Marshal(e) - return b -} - -const updatedSubject = "mesh.seat.git.event.pull-updated" -const closedSubject = "mesh.seat.git.event.pull-closed" - -func TestUpdatedBecomesAPullMessage(t *testing.T) { // 1235/AC2 (the conversion; the bus is the integration test) - msg, err := Convert(updatedSubject, updated(nil), anyFiles, watchesMain) - if err != nil { - t.Fatal(err) - } - if msg.Event != "forge.novox.mesh-delivery.pull" { - t.Errorf("event = %q", msg.Event) - } - f, err := forgemsg.DecodeForge(msg.Data) - if err != nil { - t.Fatal(err) - } - want := forgemsg.Forge{V: keys.Version, Kind: forgemsg.ForgePull, Repository: "novox/mesh-delivery", Branch: "feat/x", Number: 12, - Head: head, Action: forgemsg.PullUpdated, Dirs: []string{"modules/a"}, DirsKnown: true, Manifests: map[string]string{"modules/a": "{}"}} - if !forgemsg.SameForge(f, want) { - t.Errorf("message = %+v, want %+v", f, want) - } - - // 1235/AC3, merged into this test for the test budget: dirs not said are not known. - msg, err = Convert(updatedSubject, updated(map[string]any{"module_dirs": nil, "module_dirs_said": nil}), anyFiles, watchesMain) - if err != nil { - t.Fatal(err) - } - f, _ = forgemsg.DecodeForge(msg.Data) - if f.DirsKnown || len(f.Dirs) != 0 { - t.Errorf("dirs = %v known = %v, want not known", f.Dirs, f.DirsKnown) - } - // Said and empty is known and empty, not unknown. - msg, err = Convert(updatedSubject, updated(map[string]any{"module_dirs": []string{}, "module_dirs_said": true}), anyFiles, watchesMain) - if err != nil { - t.Fatal(err) - } - f, _ = forgemsg.DecodeForge(msg.Data) - if !f.DirsKnown || len(f.Dirs) != 0 { - t.Errorf("dirs = %v known = %v, want known and empty", f.Dirs, f.DirsKnown) - } - // Dirs listed without the forge saying it could read them are not trusted. - msg, err = Convert(updatedSubject, updated(map[string]any{"module_dirs_said": nil}), anyFiles, watchesMain) - if err != nil { - t.Fatal(err) - } - if f, _ = forgemsg.DecodeForge(msg.Data); f.DirsKnown || len(f.Dirs) != 0 { - t.Errorf("dirs %v known %v without module_dirs_said", f.Dirs, f.DirsKnown) - } -} - -func TestClosedBecomesAClosedPullMessage(t *testing.T) { // 1235/AC4 - msg, err := Convert(closedSubject, closed(), anyFiles, watchesMain) - if err != nil { - t.Fatal(err) - } - if msg.Event != "forge.novox.mesh-delivery.pull" { - t.Errorf("event = %q", msg.Event) - } - f, err := forgemsg.DecodeForge(msg.Data) - if err != nil { - t.Fatal(err) - } - want := forgemsg.Forge{V: keys.Version, Kind: forgemsg.ForgePull, Repository: "novox/mesh-delivery", Branch: "feat/x", Number: 12, - Head: head, Action: forgemsg.PullClosed} - if !forgemsg.SameForge(f, want) { - t.Errorf("message = %+v, want %+v", f, want) - } - // Issue 1422; design 47 G §507: a head pushed to a pull request into a base no registration watches is written closed, - // read nothing for, and said; one into a watched branch is no closed message, and an event naming no base is written as before. - msg, err = Convert(updatedSubject, updated(map[string]any{"base": "nox-mesh"}), nil, watchesMain) - if err != nil { - t.Fatal(err) - } - if f, err = forgemsg.DecodeForge(msg.Data); err != nil || !forgemsg.SameForge(f, want) || !strings.Contains(msg.Said, "is into nox-mesh, which is not watched") { - t.Errorf("an update into nox-mesh is %+v (%v), said %q; want it closed and said", f, err, msg.Said) - } - for _, base := range []any{"main", nil} { - msg, err = Convert(updatedSubject, updated(map[string]any{"base": base}), anyFiles, watchesMain) - if f, _ = forgemsg.DecodeForge(msg.Data); err != nil || f.Action != forgemsg.PullUpdated || msg.Said != "" { - t.Errorf("an update with base %v is %+v (%v), said %q; want it updated", base, f, err, msg.Said) - } - } -} - -func TestUnreadableEventsWriteNothing(t *testing.T) { // 1235/AC5 - for name, c := range map[string]struct { - subject string - data []byte - why string - }{ - "no owner": {updatedSubject, updated(map[string]any{"owner": nil}), "owner"}, - "no repo": {updatedSubject, updated(map[string]any{"repo": nil}), "repo"}, - "no number": {updatedSubject, updated(map[string]any{"number": nil}), "number"}, - "number zero": {updatedSubject, updated(map[string]any{"number": 0}), "number"}, - "short head": {updatedSubject, updated(map[string]any{"head_sha": "abc123"}), "head"}, - "head not hex": {updatedSubject, updated(map[string]any{"head_sha": strings.Repeat("g", 40)}), "head"}, - "upper-case head": {updatedSubject, updated(map[string]any{"head_sha": strings.ToUpper(head)}), "head"}, - "no branch": {updatedSubject, updated(map[string]any{"head": nil}), "branch"}, - "not json": {updatedSubject, []byte("{"), "json"}, - "closed no head": {closedSubject, mustDrop(closed(), "head"), "head"}, - "closed no repo": {closedSubject, mustDrop(closed(), "repository"), "repo"}, - } { - msg, err := Convert(c.subject, c.data, anyFiles, watchesMain) - if err == nil { - t.Errorf("%s: a message %q was made", name, msg.Event) - continue - } - if !errors.Is(err, ErrUnreadable) { - t.Errorf("%s: error %v is not ErrUnreadable", name, err) - } - if !strings.Contains(err.Error(), c.why) || !strings.Contains(err.Error(), c.subject) { - t.Errorf("%s: error %q names neither its subject nor %q", name, err, c.why) - } - if msg.Data != nil { - t.Errorf("%s: data was made", name) - } - } -} - -func mustDrop(body []byte, key string) []byte { - var m map[string]any - _ = json.Unmarshal(body, &m) - delete(m, key) - b, _ := json.Marshal(m) - return b -} - -func TestOtherSubjectsAreNotHandled(t *testing.T) { // 1235, 1236 - for _, s := range []string{"mesh.seat.git.event.something.else", "mesh.mod.gitea.event.pull.updated", "mesh.mod.other.event.forge.delete"} { - if _, err := Convert(s, updated(nil), anyFiles, watchesMain); !errors.Is(err, ErrNotHandled) { - t.Errorf("%s: err = %v, want ErrNotHandled", s, err) - } - } - // The seat's forge.delete of a branch is a branch-deleted message (DLV-FB1, design 51); of a tag or the - // trunk it is not handled, and a body naming no repository is unreadable. - del := func(refType, ref string) []byte { - b, _ := json.Marshal(map[string]any{"repository": "novox/mesh-sdk", "ref": ref, "ref-type": refType, "sender": "jochen"}) - return b - } - msg, err := Convert(DeletedSubject, del("branch", "feat/x"), anyFiles, watchesMain) - var bd BranchDeleted - if err != nil || msg.Event != "forge.novox.mesh-sdk.branch-deleted" || json.Unmarshal(msg.Data, &bd) != nil || - bd.Kind != ForgeBranchDeleted || bd.Repository != "novox/mesh-sdk" || bd.Branch != "feat/x" || bd.Head != "" || msg.Said == "" { - t.Errorf("forge.delete of a branch = %+v (%s), %v", msg, msg.Data, err) - } - for _, c := range []struct{ refType, ref string }{{"tag", "go/v0.1.13"}, {"branch", "main"}} { - if _, err := Convert(DeletedSubject, del(c.refType, c.ref), anyFiles, watchesMain); !errors.Is(err, ErrNotHandled) { - t.Errorf("forge.delete of %s %s: err = %v, want ErrNotHandled", c.refType, c.ref, err) - } - } - if _, err := Convert(DeletedSubject, []byte(`{"ref":"x","ref-type":"branch"}`), anyFiles, watchesMain); !errors.Is(err, ErrUnreadable) { - t.Errorf("forge.delete naming no repository: err = %v, want ErrUnreadable", err) - } - // 1236: a merged event without a full merge commit is unreadable, and the forge is not asked. - var calls []call - _, err = convertMerged(mergedBody(map[string]any{"merge_commit_sha": "abc"}), treesOf(&calls, "r0", map[string]string{"a": "t"}, nil), anyFiles, nil) - if !errors.Is(err, ErrUnreadable) || len(calls) != 0 { - t.Errorf("merged without a merge commit: err = %v calls = %v", err, calls) - } -} - -const merge = "89abcdef0123456789abcdef0123456789abcdef" - -// mergedBody is the seat git's pull.merged, extra by the short names seatPull reads. -func mergedBody(extra map[string]any) []byte { - return seatPull(map[string]any{"merge_commit_sha": merge, "merged_at": "2026-10-19T12:00:00Z", - "paths": []string{"modules/a/x.go"}, "module_dirs": []string{"modules/a"}, "module_dirs_said": true}, extra) -} - -const mergedSubject = "mesh.seat.git.event.pull-merged" - -type call struct{ owner, repo, sha string } - -// treesOf answers as gitea_commit_trees does (issue 1234/AC4: root, and the directory to tree hash, the root spelled ""). -func treesOf(calls *[]call, root string, trees map[string]string, err error) Trees { - return func(owner, repo, sha string) (string, map[string]string, error) { - *calls = append(*calls, call{owner, repo, sha}) - return root, trees, err - } -} - -func TestMergeIntoMainBecomesAMergedMessage(t *testing.T) { // 1236/AC1 - var calls []call - answer := map[string]string{"modules/a": "t1", "modules/b": "t2"} - msg, err := convertMerged(mergedBody(nil), treesOf(&calls, "r0", answer, nil), anyFiles, nil) - if err != nil { - t.Fatal(err) - } - if msg.Event != "forge.novox.mesh-delivery.merged" { - t.Errorf("event = %q", msg.Event) - } - f, err := forgemsg.DecodeForge(msg.Data) - if err != nil { - t.Fatal(err) - } - want := forgemsg.Forge{V: keys.Version, Kind: forgemsg.ForgeMerged, Repository: "novox/mesh-delivery", Branch: "feat/x", Number: 12, - Head: head, Merge: merge, Base: "main", Root: "r0", Trees: answer, Manifests: map[string]string{"modules/a": "{}", "modules/b": "{}"}} - if !forgemsg.SameForge(f, want) { - t.Errorf("message = %+v, want %+v", f, want) - } - if len(calls) != 1 || calls[0] != (call{"novox", "mesh-delivery", merge}) { - t.Errorf("the tool was called %v, want once with novox, mesh-delivery and the merge commit", calls) - } -} - -// 1236/AC2; design 47 G §509, §714: a pull request merged into a branch no registration watches is a closed pull, and one -// merged into a watched branch writes nothing here: its one message is its branch's move (MoveMessage). -func TestMergeIntoAnotherBranchIsAClosedPull(t *testing.T) { - var calls []call - if _, err := Convert(mergedSubject, mergedBody(nil), anyFiles, watchesMain); !errors.Is(err, ErrNotHandled) { - t.Errorf("a merge into the watched main: %v, want nothing written (ErrNotHandled)", err) - } - msg, err := Convert(mergedSubject, mergedBody(map[string]any{"base": "feat/y"}), anyFiles, watchesMain) - if err != nil { - t.Fatal(err) - } - f, err := forgemsg.DecodeForge(msg.Data) - if err != nil { - t.Fatal(err) - } - want := forgemsg.Forge{V: keys.Version, Kind: forgemsg.ForgePull, Repository: "novox/mesh-delivery", Branch: "feat/x", Number: 12, - Head: head, Action: forgemsg.PullClosed} - if msg.Event != "forge.novox.mesh-delivery.pull" || !forgemsg.SameForge(f, want) { - t.Errorf("event %q message %+v, want a closed pull message", msg.Event, f) - } - if len(calls) != 0 { - t.Errorf("the tool was called for a merge into another branch: %v", calls) - } -} - -func TestTreesRefusedWritesNothingAndSaysWhy(t *testing.T) { // 1236/AC3 (the conversion; the delay is in main_test) - var calls []call - msg, err := convertMerged(mergedBody(nil), treesOf(&calls, "", nil, errors.New("the listing for novox/mesh-delivery was cut")), anyFiles, nil) - if !errors.Is(err, ErrRetry) || msg.Data != nil { - t.Fatalf("err = %v data = %s, want ErrRetry and no message", err, msg.Data) - } - for _, w := range []string{"novox/mesh-delivery", merge, "the listing for novox/mesh-delivery was cut"} { - if !strings.Contains(err.Error(), w) { - t.Errorf("error %q does not name %q", err, w) - } - } -} - -// 1126 (note 94): a merge that leaves no module.json anywhere (hq, or a repository whose last module was removed) is a merged -// message with no trees, so the indexer takes it in and the repository's reach is not held for ever. -func TestNoModuleJSONAnywhereWritesAMergedMessageWithNoTrees(t *testing.T) { - var calls []call - msg, err := convertMerged(mergedBody(nil), treesOf(&calls, "r0", map[string]string{}, nil), anyFiles, nil) - if err != nil { - t.Fatal(err) - } - f, err := forgemsg.DecodeForge(msg.Data) - want := forgemsg.Forge{V: keys.Version, Kind: forgemsg.ForgeMerged, Repository: "novox/mesh-delivery", Branch: "feat/x", Number: 12, - Head: head, Merge: merge, Base: "main", Root: "r0"} - if err != nil || msg.Event != "forge.novox.mesh-delivery.merged" || !forgemsg.SameForge(f, want) || !strings.Contains(msg.Said, "no module.json") { - t.Errorf("event %q message %+v said %q, %v; want a merged message with no trees", msg.Event, f, msg.Said, err) - } -} - -// openAnswer is the seat git's answer of open-pulls. -func openAnswer(pulls ...map[string]any) []byte { - b, _ := json.Marshal(map[string]any{"pulls": pulls}) - return b -} - -func openEntry(repo string, number int, base, branch, sha string) map[string]any { - return map[string]any{"pull": map[string]any{"repository": "novox/" + repo, "number": number, "base": base, "branch": branch, "head": sha, - "state": "open", "url": "https://forge.example/x"}, "required": []string{"mesh/check"}, "checks": map[string]any{}} -} - -func TestOpenPullsAreUpdatedPullMessagesWithUnknownDirs(t *testing.T) { // 1237/AC1 - h2 := strings.Repeat("a", 40) - msgs, skipped, err := OpenPulls(openAnswer(openEntry("mesh-delivery", 12, "main", "feat/x", head), openEntry("mesh-catalog", 300, "main", "feat/x", h2)), watchesMain) - if err != nil || len(skipped) != 0 || len(msgs) != 2 { - t.Fatalf("msgs %d skipped %v err %v", len(msgs), skipped, err) - } - for i, want := range []forgemsg.Forge{ - {V: keys.Version, Kind: forgemsg.ForgePull, Repository: "novox/mesh-delivery", Branch: "feat/x", Number: 12, Head: head, Action: forgemsg.PullUpdated}, - {V: keys.Version, Kind: forgemsg.ForgePull, Repository: "novox/mesh-catalog", Branch: "feat/x", Number: 300, Head: h2, Action: forgemsg.PullUpdated}, - } { - f, err := forgemsg.DecodeForge(msgs[i].Data) - if err != nil || !forgemsg.SameForge(f, want) { - t.Errorf("message %d = %+v err %v, want %+v", i, f, err, want) - } - if f.DirsKnown { - t.Errorf("message %d says its directories are known", i) - } - } - if msgs[0].Event != "forge.novox.mesh-delivery.pull" || msgs[1].Event != "forge.novox.mesh-catalog.pull" { - t.Errorf("events %q %q", msgs[0].Event, msgs[1].Event) - } -} - -func TestOpenPullsUnreadableEntryIsNamedAndTheRestGo(t *testing.T) { // 1237, 1237/AC4 - // 1237/AC4: an open pull request into another branch gets no message. - if msgs, skipped, err := OpenPulls(openAnswer(openEntry("mesh-delivery", 12, "feat/y", "feat/x", head)), watchesMain); err != nil || len(msgs) != 0 || len(skipped) != 0 { - t.Errorf("into another branch: msgs %d skipped %v err %v, want none", len(msgs), skipped, err) - } - msgs, skipped, err := OpenPulls(openAnswer(openEntry("a", 1, "main", "b", "short"), openEntry("mesh-delivery", 12, "main", "feat/x", head)), watchesMain) - if err != nil || len(msgs) != 1 || len(skipped) != 1 || !strings.Contains(skipped[0], "novox/a#1") { - t.Errorf("msgs %d skipped %v err %v", len(msgs), skipped, err) - } - for _, bad := range []string{`{"pulls":null}`, `{}`, `[]`, `x`, string(gitFixture(t, "open-pulls answers an error, never none", "verb:open-pulls.answer"))} { - if _, _, err := OpenPulls([]byte(bad), watchesMain); err == nil { - t.Errorf("%s was taken as an answer", bad) - } - } -} - -func TestOpenMessageNamesEveryOpenPullIntoTheTrunk(t *testing.T) { // 1279/AC6 - h2 := strings.Repeat("a", 40) - answer := openAnswer(openEntry("mesh-delivery", 12, "main", "feat/x", head), openEntry("mesh-catalog", 300, "main", "feat/x", h2), - openEntry("mesh-catalog", 301, "feat/y", "feat/z", h2), openEntry("a", 1, "main", "b", "short")) - m, err := OpenMessage(answer, watchesMain) - if err != nil || m.Event != "forge.open" { - t.Fatalf("message %+v err %v", m, err) - } - f, err := forgemsg.DecodeForge(m.Data) - // Every one into the trunk, whether its head reads or not (it is open); none into another branch; in a fixed order. - want := []forgemsg.OpenPull{{Repository: "novox/a", Number: 1}, {Repository: "novox/mesh-catalog", Number: 300}, {Repository: "novox/mesh-delivery", Number: 12}} - if err != nil || f.Kind != forgemsg.ForgeOpen || len(f.Open) != 3 || f.Open[0] != want[0] || f.Open[1] != want[1] || f.Open[2] != want[2] { - t.Errorf("open message = %+v err %v, want %v", f, err, want) - } - // Nothing open is a message too: the indexer then drops every candidate. - m, err = OpenMessage([]byte(`{"pulls":[]}`), watchesMain) - if f, derr := forgemsg.DecodeForge(m.Data); err != nil || derr != nil || f.Kind != forgemsg.ForgeOpen || len(f.Open) != 0 { - t.Errorf("empty answer: %+v %v %v", f, err, derr) - } - for _, bad := range []string{`{"pulls":null}`, `{}`, `x`, `{"error":"the forge answered 502","pulls":[]}`} { - if _, err := OpenMessage([]byte(bad), watchesMain); err == nil { - t.Errorf("%s was taken as an answer", bad) - } - } -} - -func TestTrackSaysWhichHeadOfWhichPullRequestWasWritten(t *testing.T) { // 1279/AC6 - pullMsg := func(action string) forgemsg.Forge { - return forgemsg.Forge{Kind: forgemsg.ForgePull, Repository: "novox/mesh-delivery", Branch: "feat/x", Number: 12, Head: head, Action: action} - } - for _, a := range []string{forgemsg.PullOpened, forgemsg.PullUpdated} { - if key, h, ok := Track(pullMsg(a)); key != "novox.mesh-delivery.12" || h != head || !ok { - t.Errorf("%s: Track = %q %q %v, want the head kept under a bucket key", a, key, h, ok) - } - } - // A closed pull request and a merged one are no longer kept: their key goes. - if key, h, ok := Track(pullMsg(forgemsg.PullClosed)); key != "novox.mesh-delivery.12" || h != "" || !ok { - t.Errorf("closed: Track = %q %q %v", key, h, ok) - } - merged := forgemsg.Forge{Kind: forgemsg.ForgeMerged, Repository: "novox/mesh-delivery", Branch: "feat/x", Number: 12, Head: head, Merge: "m", Root: "r", Trees: map[string]string{"": "t"}} - if key, h, ok := Track(merged); key != "novox.mesh-delivery.12" || h != "" || !ok { - t.Errorf("merged: Track = %q %q %v", key, h, ok) - } - // The open message and the rest hold no head. - if _, _, ok := Track(forgemsg.Forge{Kind: forgemsg.ForgeOpen}); ok { - t.Error("an open message was tracked") - } -} - // gitFixture is one answer of the seat git's fixtures, by its name and what it is of. func gitFixture(t *testing.T, name, of string) json.RawMessage { t.Helper() @@ -505,15 +94,3 @@ func watchedOf(branches ...string) *Watched { w.Set(nil, branches) return w } - -// convertMerged is the merged message of a pull.merged body as the move of its base writes it (landed, MoveMessage), so the -// tests of what a merge reads keep their bodies. -func convertMerged(data []byte, trees Trees, files Files, kept Kept) (Message, error) { - p, err := pullOf(data) - if err != nil { - return Message{}, unreadable(MergedSubject, "%v", err) - } - f := forgemsg.Forge{Kind: forgemsg.ForgeMerged, Repository: p.Owner + "/" + p.Repo, Branch: p.Head, Number: p.Number, - Head: p.HeadSHA, Merge: p.MergeSHA, Base: p.Base} - return landed(MergedSubject, f, p.MergeSHA, trees, files, kept) -} diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/call.go b/modules/mesh-delivery-forge-bridge/internal/bridge/call.go index 4c51d447..3d209816 100644 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/call.go +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/call.go @@ -3,12 +3,10 @@ package bridge import ( "context" "encoding/json" - "errors" "fmt" "strings" git "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" ) // Caller calls a seat's verb by its manifest key (`seat:.`) with a JSON body and returns its answer: the @@ -44,63 +42,6 @@ func askGit[A any](ctx context.Context, call Caller, verb string, in any) (A, er // repository is `/`, as the seat names a repository. func repository(owner, repo string) string { return owner + "/" + repo } -// TreesVia asks the seat `git` for a commit's trees (verb trees). -func TreesVia(ctx context.Context, call Caller) Trees { - return func(owner, repo, sha string) (string, map[string]string, error) { - a, err := askGit[git.TreesAnswer](ctx, call, git.VerbTrees, git.TreesInput{Repository: repository(owner, repo), Commit: sha}) - if err != nil { - return "", nil, err - } - if a.Root == "" { - return "", nil, fmt.Errorf("the seat git's trees of %s at %s name no root tree", repository(owner, repo), sha) - } - return a.Root, a.Trees, nil - } -} - -// FilesVia asks the seat `git` for one file at a ref (verb file). A file the seat says is absent is ErrNotFound; an -// answer that says neither absent nor content is an error, never an empty file. -func FilesVia(ctx context.Context, call Caller) Files { - return func(owner, repo, path, ref string) ([]byte, error) { - raw, err := call(ctx, GitKey(git.VerbFile), git.FileInput{Repository: repository(owner, repo), Path: path, Ref: ref}) - if err != nil { - return nil, fmt.Errorf("the seat git did not answer file: %w", err) - } - var a struct { - git.FileAnswer - Content *string `json:"content"` - } - if err := json.Unmarshal(raw, &a); err != nil { - return nil, fmt.Errorf("the seat git's answer of file is not its contract's: %w", err) - } - switch { - case a.Error != "": - return nil, fmt.Errorf("the seat git refused file %s at %s: %s", path, ref, a.Error) - case a.Absent: - return nil, fmt.Errorf("%w: %s at %s in %s", ErrNotFound, path, ref, repository(owner, repo)) - case a.Content == nil: - return nil, fmt.Errorf("the seat git's file %s at %s says neither its content nor that it is absent", path, ref) - } - return []byte(*a.Content), nil - } -} - -// errNoAnswer is a read whose answer carries nothing the contract says it must. -var errNoAnswer = errors.New("the seat git's answer carries nothing") - -// MergedMessage is the merged message of a merge into the branch base, asked again: built as the bridge builds the one -// it writes on FORGE for the branch's move (MoveMessage), from the pull request's owner, repository, number, branch, head, -// merge commit and base, which the seat git's verb pull answers. It reads the forge for every module directory (no kept -// inputs). -func MergedMessage(owner, repo string, number int, branch, head, merge, base string, trees Trees, files Files) (Message, error) { - if number <= 0 || branch == "" || base == "" || !sha40.MatchString(head) { - return Message{}, unreadable("bridge_merged_message", "%s#%d names no branch, base or 40-hex head", repository(owner, repo), number) - } - f := forgemsg.Forge{Kind: forgemsg.ForgeMerged, Repository: repository(owner, repo), Branch: branch, Number: number, Head: head, - Merge: merge, Base: base} - return landed(repository(owner, repo)+"#"+fmt.Sprint(number), f, merge, trees, files, nil) -} - // splitRepository is a `/` cut in two, refusing anything else. func splitRepository(repository string) (string, string, error) { owner, name, ok := strings.Cut(strings.TrimSpace(repository), "/") diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/files_test.go b/modules/mesh-delivery-forge-bridge/internal/bridge/files_test.go deleted file mode 100644 index 09b4418e..00000000 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/files_test.go +++ /dev/null @@ -1,412 +0,0 @@ -package bridge - -import ( - "crypto/sha256" - "encoding/hex" - "errors" - "fmt" - "os" - "reflect" - "slices" - "sort" - "strings" - "sync" - "testing" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" -) - -// The bytes the forge answers in these tests are two real files of this repository, read from the working tree: the -// module.json and the go.sum of modules/mesh-delivery-forge-bridge (two directories up). The forge's own answer shape -// is mesh-catalog modules/gitea tools/index.ts gitea_get_file; the bridge sees only the bytes (issue 1448). -func real(t *testing.T, name string) []byte { - t.Helper() - b, err := os.ReadFile("../../" + name) - if err != nil { - t.Fatal(err) - } - return b -} - -func hexSum(b []byte) string { s := sha256.Sum256(b); return hex.EncodeToString(s[:]) } - -type fileCall struct{ owner, repo, path, ref string } - -// forgeFiles answers as the forge does for a path: its bytes, ErrNotFound for a path in none, the error in errs. -type forgeFiles struct { - have map[string][]byte - errs map[string]error - mu sync.Mutex - calls []fileCall -} - -func (f *forgeFiles) get(owner, repo, path, ref string) ([]byte, error) { - f.mu.Lock() - f.calls = append(f.calls, fileCall{owner, repo, path, ref}) - f.mu.Unlock() - if err, ok := f.errs[path]; ok { - return nil, err - } - if b, ok := f.have[path]; ok { - return b, nil - } - return nil, ErrNotFound -} - -// anyFiles is a forge that has a module.json at every path and no lock file, for tests that are not about the files. -func anyFiles(owner, repo, path, ref string) ([]byte, error) { - if strings.HasSuffix(path, "module.json") { - return []byte("{}"), nil - } - return nil, ErrNotFound -} - -func TestMergedCarriesManifestsAndLocks(t *testing.T) { // 1448/AC1 - manifest, sum := real(t, "module.json"), real(t, "go.sum") - forge := &forgeFiles{have: map[string][]byte{"modules/a/module.json": manifest, "modules/a/go.sum": sum, "modules/b/module.json": []byte(`{"module":"b"}`)}} - var calls []call - msg, err := convertMerged(mergedBody(nil), treesOf(&calls, "r0", map[string]string{"modules/a": "t1", "modules/b": "t2"}, nil), forge.get, nil) - if err != nil { - t.Fatal(err) - } - f, err := forgemsg.DecodeForge(msg.Data) - if err != nil { - t.Fatal(err) - } - if want := map[string]string{"modules/a": string(manifest), "modules/b": `{"module":"b"}`}; !reflect.DeepEqual(f.Manifests, want) { - t.Errorf("manifests = %v", f.Manifests) - } - if want := map[string]map[string]string{"modules/a": {"go.sum": hexSum(sum)}}; !reflect.DeepEqual(f.Locks, want) { - t.Errorf("locks = %v, want %v", f.Locks, want) - } - // ADR 0593 §2 (D6): the bridge reads no mesh package; only the indexer computes what a fingerprint takes. - if len(f.Packages) != 0 || strings.Contains(string(msg.Data), `"packages"`) { - t.Errorf("the merged message carries packages: %v", f.Packages) - } - // ADR 0567: assignments is the seat mesh-module-assigner's: a merge of the catalogue carries no assignments file and asks the - // forge for none. - forge.calls = nil - msg, err = convertMerged(mergedBody(map[string]any{"repo": "mesh-catalog"}), treesOf(&calls, "r0", map[string]string{"modules/a": "t1"}, nil), forge.get, nil) - if err != nil { - t.Fatal(err) - } - if f, err = forgemsg.DecodeForge(msg.Data); err != nil { - t.Fatal(err) - } - if len(f.RetiredAssignments) > 0 || f.RetiredRead || strings.Contains(string(msg.Data), `"assignments`) { - t.Errorf("a merge of the catalogue carries assignments: %s", msg.Data) - } - for _, c := range forge.calls { - if strings.HasPrefix(c.path, "assignments") { - t.Errorf("the forge was asked for %s", c.path) - } - } - // 1953/D1 (ADR 0565 §2): the catalogue's merged message carries its predefined.json at the merge, or says it holds - // none; another repository's asks the forge for none. - if !f.PredefinedRead || len(f.Predefined) != 0 { - t.Errorf("a merge of the catalogue with no predefined.json does not say so: %s", msg.Data) - } - forge.have[PredefinedFile] = []byte(`{"font-size":{"type":"integer","value":13}}`) - msg, _ = convertMerged(mergedBody(map[string]any{"repo": "mesh-catalog"}), treesOf(&calls, "r0", map[string]string{"modules/a": "t1"}, nil), forge.get, nil) - if f, err = forgemsg.DecodeForge(msg.Data); err != nil || string(f.Predefined) != string(forge.have[PredefinedFile]) || f.PredefinedRead { - t.Errorf("predefined = %s (%v)", f.Predefined, err) - } - forge.calls = nil - msg, _ = convertMerged(mergedBody(nil), treesOf(&calls, "r0", map[string]string{"modules/a": "t1"}, nil), forge.get, nil) - for _, c := range forge.calls { - if c.path == PredefinedFile { - t.Errorf("another repository's merge asked for %s", c.path) - } - } - forge.errs = map[string]error{PredefinedFile: errors.New("forge down")} - if _, err := convertMerged(mergedBody(map[string]any{"repo": "mesh-catalog"}), treesOf(&calls, "r0", map[string]string{"modules/a": "t1"}, nil), forge.get, nil); !errors.Is(err, ErrRetry) { - t.Errorf("a forge that cannot say is %v, want a retry", err) - } - // ADR 0632 §2: of a manifest naming a template, the template is read at the merge and the module's needs carried - // whole: docker's daemon file names the image seat's reach while its manifest stays the same. A manifest naming - // none carries no needs; a template the forge does not have is no need, and no retry. - forge.errs = nil - forge.have["modules/d/module.json"] = []byte(`{"module":"d","version":"1","resources":[{"id":"daemon","type":"file",` + - `"path":"/etc/d.json","template":"files/daemon.json"},{"id":"gone","type":"file","path":"/etc/g","template":"files/gone"}]}`) - forge.have["modules/d/files/daemon.json"] = []byte(`{"registry":"${seat:container-image-registry:reach}"}`) - msg, err = convertMerged(mergedBody(nil), treesOf(&calls, "r0", map[string]string{"modules/b": "t2", "modules/d": "t4"}, nil), forge.get, nil) - if err != nil { - t.Fatal(err) - } - if f, err = forgemsg.DecodeForge(msg.Data); err != nil || len(f.Needs) != 1 || !slices.Contains(f.Needs["modules/d"], "form:seat-reach") { - t.Errorf("needs = %v (%v), want modules/d's alone, with form:seat-reach from its template", f.Needs, err) - } - // Issue 1126: a merge reads the forge only for the module directories whose tree changed. - t.Run("a merge reads only the directories whose tree changed", mergeReadsOnlyChangedDirs) -} - -func TestPullWithKnownDirsCarriesTheSameAtTheHead(t *testing.T) { // 1448/AC2 - manifest := real(t, "module.json") - forge := &forgeFiles{have: map[string][]byte{"modules/a/module.json": manifest, "modules/a/go.sum": []byte("git.novox.be/novox/mesh-sdk/go v0.1.7 h1:x=\n")}} - msg, err := Convert(updatedSubject, updated(nil), forge.get, watchesMain) - if err != nil { - t.Fatal(err) - } - f, _ := forgemsg.DecodeForge(msg.Data) - if !reflect.DeepEqual(f.Manifests, map[string]string{"modules/a": string(manifest)}) || len(f.Locks) != 1 || len(f.Packages) != 0 { - t.Errorf("manifests %v locks %v packages %v", f.Manifests, f.Locks, f.Packages) - } - if len(forge.calls) == 0 || forge.calls[0].ref != head { - t.Errorf("calls %v, want the head %s", forge.calls, head) - } - // Dirs not known: neither field, and the forge is not asked. - forge = &forgeFiles{} - msg, err = Convert(updatedSubject, updated(map[string]any{"module_dirs": nil, "module_dirs_said": nil}), forge.get, watchesMain) - if err != nil { - t.Fatal(err) - } - f, _ = forgemsg.DecodeForge(msg.Data) - if f.Manifests != nil || f.Locks != nil || len(forge.calls) != 0 { - t.Errorf("manifests %v locks %v calls %v", f.Manifests, f.Locks, forge.calls) - } -} - -func TestAForgeErrorOrAMissingManifestIsRetryAndALockNotFoundIsLeftOut(t *testing.T) { // 1448/AC3 - boom := errors.New("Gitea API /repos/novox/mesh-delivery/contents/modules/a/go.sum?ref=x: 502 bad gateway") - trees := func() Trees { var c []call; return treesOf(&c, "r0", map[string]string{"modules/a": "t1"}, nil) } - for name, forge := range map[string]*forgeFiles{ - "a manifest error": {errs: map[string]error{"modules/a/module.json": boom}}, - "a manifest not found": {}, - "a lock file error": {have: map[string][]byte{"modules/a/module.json": []byte("{}")}, errs: map[string]error{"modules/a/go.sum": boom}}, - "a second lock error": {have: map[string][]byte{"modules/a/module.json": []byte("{}")}, errs: map[string]error{"modules/a/package-lock.json": boom}}, - } { - msg, err := convertMerged(mergedBody(nil), trees(), forge.get, nil) - if !errors.Is(err, ErrRetry) || msg.Data != nil { - t.Errorf("%s: err = %v data = %s, want ErrRetry and no message", name, err, msg.Data) - } - _, err = Convert(updatedSubject, updated(nil), forge.get, watchesMain) - if !errors.Is(err, ErrRetry) { - t.Errorf("%s (pull): err = %v, want ErrRetry", name, err) - } - } - forge := &forgeFiles{have: map[string][]byte{"modules/a/module.json": []byte("{}")}} - msg, err := convertMerged(mergedBody(nil), trees(), forge.get, nil) - if err != nil { - t.Fatal(err) - } - if f, _ := forgemsg.DecodeForge(msg.Data); f.Locks != nil { - t.Errorf("locks = %v, want none for lock files the forge does not have", f.Locks) - } -} - -func TestTheFilesAreAskedAtTheMergeCommitByOwnerRepoPathAndRef(t *testing.T) { // 1448/AC4 (the call's arguments; the manifest is in manifest_test) - forge := &forgeFiles{have: map[string][]byte{"modules/a/module.json": []byte("{}")}} - var calls []call - if _, err := convertMerged(mergedBody(nil), treesOf(&calls, "r0", map[string]string{"modules/a": "t1"}, nil), forge.get, nil); err != nil { - t.Fatal(err) - } - want := []fileCall{{"novox", "mesh-delivery", "modules/a/module.json", merge}, {"novox", "mesh-delivery", "modules/a/go.sum", merge}, - {"novox", "mesh-delivery", "modules/a/package-lock.json", merge}} - sort.Slice(forge.calls, func(i, j int) bool { return forge.calls[i].path < forge.calls[j].path }) // they are asked for concurrently - sort.Slice(want, func(i, j int) bool { return want[i].path < want[j].path }) - if !reflect.DeepEqual(forge.calls, want) { - t.Errorf("calls = %v, want %v", forge.calls, want) - } -} - -// slowForge answers like a forge that takes d a call: a module.json of the directory's name everywhere, a go.sum in -// the directories whose number is even and in the root, and counts the calls in flight. -type slowForge struct { - d time.Duration - mu sync.Mutex - inFlight, most, started, n int - failOn string - finished int -} - -func (f *slowForge) get(_, _, path, _ string) ([]byte, error) { - f.mu.Lock() - f.inFlight++ - f.started++ - if f.inFlight > f.most { - f.most = f.inFlight - } - f.mu.Unlock() - time.Sleep(f.d) - defer func() { f.mu.Lock(); f.inFlight--; f.finished++; f.mu.Unlock() }() - switch { - case path == f.failOn: - return nil, errors.New("Gitea API " + path + ": 502 bad gateway") - case strings.HasSuffix(path, "module.json"): - return []byte(`{"module":"` + path + `"}`), nil - case strings.HasSuffix(path, "go.sum") && (path == "go.sum" || path[len(path)-8]%2 == 0): - return []byte("sum of " + path), nil - } - return nil, ErrNotFound -} - -func manyDirs(n int) []string { - dirs := []string{""} - for i := 1; i < n; i++ { - dirs = append(dirs, fmt.Sprintf("modules/m%02d", i)) - } - return dirs -} - -func TestFortyDirectoriesAreReadEightAtATimeWithTheResultOfOneAtATime(t *testing.T) { // 1458/AC1 - dirs := manyDirs(40) - slow := &slowForge{d: 10 * time.Millisecond} - manifests, locks, err := inputsOf("novox", "mesh-catalog", merge, dirs, slow.get) - if err != nil { - t.Fatal(err) - } - if slow.most > MaxReadsInFlight || slow.most < 2 { - t.Errorf("at most %d calls were in flight, want more than one and at most %d", slow.most, MaxReadsInFlight) - } - if slow.started != 40*3 { - t.Errorf("%d calls, want 120", slow.started) - } - // The same read one file at a time. - wantM, wantL := map[string]string{}, map[string]map[string]string{} - one := &slowForge{} - for _, dir := range dirs { - m, l, err := inputsOfOne(dir, one.get) - if err != nil { - t.Fatal(err) - } - wantM[dir] = m - if l != nil { - wantL[dir] = l - } - } - if !reflect.DeepEqual(manifests, wantM) || !reflect.DeepEqual(locks, wantL) { - t.Errorf("the concurrent read differs from a one-at-a-time read: %d manifests %d locks, want %d and %d", len(manifests), len(locks), len(wantM), len(wantL)) - } - if len(locks) == 0 || len(locks) == 40 { - t.Errorf("%d directories with locks, the fixture should have some and not all", len(locks)) - } -} - -// inputsOfOne reads one directory with a single call at a time, as the bridge did before issue 1458. -func inputsOfOne(dir string, files Files) (string, map[string]string, error) { - at := func(name string) string { - if dir == "" { - return name - } - return dir + "/" + name - } - raw, err := files("o", "r", at("module.json"), "x") - if err != nil { - return "", nil, err - } - var locks map[string]string - for _, name := range LockFiles { - b, err := files("o", "r", at(name), "x") - if errors.Is(err, ErrNotFound) { - continue - } - if err != nil { - return "", nil, err - } - if locks == nil { - locks = map[string]string{} - } - s := sha256.Sum256(b) - locks[name] = hex.EncodeToString(s[:]) - } - return string(raw), locks, nil -} - -func TestOneFailingFileIsRetryAndEveryCallHasReturnedBeforeTheReadDoes(t *testing.T) { // 1458/AC2 - dirs := manyDirs(40) - slow := &slowForge{d: 10 * time.Millisecond, failOn: "modules/m05/go.sum"} - _, _, err := inputsOf("novox", "mesh-catalog", merge, dirs, slow.get) - if err == nil || !strings.Contains(err.Error(), "modules/m05/go.sum") || !strings.Contains(err.Error(), "502") { - t.Fatalf("err = %v, want the failing file named", err) - } - slow.mu.Lock() - defer slow.mu.Unlock() - if slow.inFlight != 0 || slow.finished != slow.started { - t.Errorf("%d calls in flight, %d of %d finished, when the read returned", slow.inFlight, slow.finished, slow.started) - } - if slow.started >= 40*3 { - t.Errorf("%d calls were made, want the read to stop asking after the failure", slow.started) - } - // Through Convert it is ErrRetry. - var calls []call - dirsMap := map[string]string{} - for _, d := range dirs[1:] { - dirsMap[d] = "t" - } - slow2 := &slowForge{failOn: "modules/m05/module.json"} - if _, err := convertMerged(mergedBody(nil), treesOf(&calls, "r0", dirsMap, nil), slow2.get, nil); !errors.Is(err, ErrRetry) { - t.Errorf("err = %v, want ErrRetry", err) - } -} - -// memKept is a Kept in memory. -type memKept map[string]Inputs - -func (k memKept) Get(tree string) (Inputs, bool) { in, ok := k[tree]; return in, ok } -func (k memKept) Put(tree string, in Inputs) { k[tree] = in } - -// Issue 1126 (mesh-catalog#535; issue 1458 met the kind first): a merge reads the forge only for the module directories -// whose tree changed. Its message is the same as one read whole; a directory whose tree is kept costs no read. -func mergeReadsOnlyChangedDirs(t *testing.T) { - manifest, sum := real(t, "module.json"), real(t, "go.sum") - forge := &forgeFiles{have: map[string][]byte{"modules/a/module.json": manifest, "modules/a/go.sum": sum, "modules/b/module.json": []byte(`{"module":"b"}`)}} - kept := memKept{} - var calls []call - first, err := convertMerged(mergedBody(nil), treesOf(&calls, "r0", map[string]string{"modules/a": "t1", "modules/b": "t2"}, nil), forge.get, kept) - if err != nil { - t.Fatal(err) - } - if len(forge.calls) != 6 { - t.Fatalf("the first merge read %d files, want 6 (a manifest and two lock files for each directory)", len(forge.calls)) - } - // The next merge changes modules/b alone: only its three files are read, and the message equals one read whole. - forge.calls = nil - forge.have["modules/b/module.json"] = []byte(`{"module":"b","v":2}`) - trees := map[string]string{"modules/a": "t1", "modules/b": "t3"} - next, err := convertMerged(mergedBody(nil), treesOf(&calls, "r1", trees, nil), forge.get, kept) - if err != nil { - t.Fatal(err) - } - for _, c := range forge.calls { - if !strings.HasPrefix(c.path, "modules/b/") { - t.Errorf("%s was read although its directory's tree is kept", c.path) - } - } - if len(forge.calls) != 3 { - t.Errorf("the second merge read %d files, want 3", len(forge.calls)) - } - whole, err := convertMerged(mergedBody(nil), treesOf(&calls, "r1", trees, nil), forge.get, nil) - if err != nil { - t.Fatal(err) - } - if string(next.Data) != string(whole.Data) { - t.Errorf("the message from kept inputs differs from one read whole:\n%s\n%s", next.Data, whole.Data) - } - if string(first.Data) == string(next.Data) { - t.Error("the second merge's message did not change") - } - // A failed read keeps the directories it read whole and nothing of the one that failed, so the next try reads only the - // rest: a merge too large for one try's bound converges over its tries. - kept2 := memKept{} - failing := &forgeFiles{have: forge.have, errs: map[string]error{"modules/b/module.json": errors.New("context deadline exceeded")}} - if _, err := convertMerged(mergedBody(nil), treesOf(&calls, "r1", trees, nil), failing.get, kept2); !errors.Is(err, ErrRetry) { - t.Fatalf("err = %v, want ErrRetry", err) - } - if _, ok := kept2["t1"]; !ok || len(kept2) != 1 { // modules/a's files are asked first, before the failure is seen - t.Errorf("a failed read kept %v, want modules/a's tree t1 alone", kept2) - } - failing.calls = nil - failing.errs = nil - again, err := convertMerged(mergedBody(nil), treesOf(&calls, "r1", trees, nil), failing.get, kept2) - if err != nil { - t.Fatal(err) - } - if string(again.Data) != string(whole.Data) { - t.Errorf("the message of the try after a failure differs from one read whole") - } - if len(failing.calls) != 3 { - t.Errorf("the try after a failure read %d files, want only modules/b's 3", len(failing.calls)) - } - // ADR 0593 §2: inputs an older bridge kept with the packages it read are taken as they are; their packages are left. -} diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/forgemade.go b/modules/mesh-delivery-forge-bridge/internal/bridge/forgemade.go deleted file mode 100644 index 6cc982b7..00000000 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/forgemade.go +++ /dev/null @@ -1,46 +0,0 @@ -package bridge - -import ( - "context" - "fmt" - - git "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" -) - -// ForgePull is a pull request as the forge itself says it: what the bridge needs of a merge made on the forge, not -// through the gate (a recovery merge or the operator's own, which the gate holds no merge of; issue 1966). -type ForgePull struct { - Number int - Branch string // the pull request's head branch - Head string // the head commit - Merge string // the merge commit - Base string // the branch it was merged into -} - -// ForgeMade reads, through the seat `git`'s verb pull, the merged pull request whose merge commit is merge: by number -// when number is given (its merge commit must then equal merge), else by the merge commit. A pull request not merged, or -// merged at another commit, is refused. -func ForgeMade(ctx context.Context, call Caller, owner, repo string, number int, merge string) (ForgePull, error) { - in := git.PullInput{Repository: repository(owner, repo), MergeCommit: merge} - if number > 0 { - in = git.PullInput{Repository: repository(owner, repo), Number: int64(number)} - } - a, err := askGit[git.PullAnswer](ctx, call, git.VerbPull, in) - if err != nil { - return ForgePull{}, err - } - p := a.Pull - switch { - case p == nil || p.Number == 0: - return ForgePull{}, fmt.Errorf("the pull request of %s at %s: %w", in.Repository, merge, errNoAnswer) - case p.State != git.PullStateMerged: - return ForgePull{}, fmt.Errorf("the forge says the pull request %s#%d is %s, not merged", in.Repository, p.Number, p.State) - case p.MergeCommit != merge: - return ForgePull{}, fmt.Errorf("the forge says %s#%d was merged at %s, not at %s", in.Repository, p.Number, p.MergeCommit, merge) - case p.Base == "": - return ForgePull{}, fmt.Errorf("the forge gives no base branch for %s#%d", in.Repository, p.Number) - case p.Branch == "": - return ForgePull{}, fmt.Errorf("the forge gives no head branch for %s#%d", in.Repository, p.Number) - } - return ForgePull{Number: int(p.Number), Branch: p.Branch, Head: p.Head, Merge: p.MergeCommit, Base: p.Base}, nil -} diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/kept.go b/modules/mesh-delivery-forge-bridge/internal/bridge/kept.go deleted file mode 100644 index 2a384c8b..00000000 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/kept.go +++ /dev/null @@ -1,64 +0,0 @@ -package bridge - -// A merge of a repository of many modules read three files of the forge for every module, whatever the merge changed: -// the catalogue's 140 modules are 420 reads, which no longer fit the 30 s bound of a handled event while the forge is -// busy after a merge, so the merged message of mesh-catalog#535 was never written and the indexer never saw it (issue -// 1126; issue 1458 met the same kind at 131 modules and raised the reads in flight). A directory's git tree hash names -// every byte under it, so the manifest and the lock-file hashes read once at a tree hash are those of every later commit -// whose directory has the same tree: a merge reads the forge only for the directories it changed. - -// Inputs is what the bridge read of one module directory at one tree hash: its manifest and its lock files' hex sha256 -// by name (none when it has none). The bridge reads no mesh package (novox/hq ADR 0593 §2: only the indexer computes -// what a fingerprint takes); a value kept with the packages an older bridge read is taken, and those are left. -type Inputs struct { - Manifest string `json:"manifest"` - Locks map[string]string `json:"locks,omitempty"` - // Needs are the module's needs whole when its manifest names templates (inputsRead): a value of such a manifest kept - // without them, by a bridge before ADR 0632 §2, is read again. - Needs []string `json:"needs,omitempty"` -} - -// Kept holds Inputs by a directory's git tree hash (the bridge's state bucket inputs). Get answers false for a tree it -// does not hold or cannot read; Put is best effort: a value it could not keep is read from the forge next time. -type Kept interface { - Get(tree string) (Inputs, bool) - Put(tree string, in Inputs) -} - -// keptInputs splits dirs into the inputs kept for their tree hash and the directories still to read from the forge. -func keptInputs(dirs []string, trees map[string]string, kept Kept) (manifests map[string]string, locks map[string]map[string]string, needs map[string][]string, read []string) { - manifests, locks, needs = map[string]string{}, map[string]map[string]string{}, map[string][]string{} - for _, dir := range dirs { - tree := trees[dir] - if kept == nil || tree == "" { - read = append(read, dir) - continue - } - in, ok := kept.Get(tree) - if !ok || (in.Needs == nil && len(templatePaths(in.Manifest)) > 0) { - read = append(read, dir) - continue - } - manifests[dir] = in.Manifest - if len(in.Locks) > 0 { - locks[dir] = in.Locks - } - if in.Needs != nil { - needs[dir] = in.Needs - } - } - return manifests, locks, needs, read -} - -// keep puts what was read of each directory under its tree hash. -func keep(dirs []string, trees map[string]string, manifests map[string]string, locks map[string]map[string]string, needs map[string][]string, kept Kept) { - if kept == nil { - return - } - for _, dir := range dirs { - raw, ok := manifests[dir] - if tree := trees[dir]; ok && tree != "" { - kept.Put(tree, Inputs{Manifest: raw, Locks: locks[dir], Needs: needs[dir]}) - } - } -} diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/moved.go b/modules/mesh-delivery-forge-bridge/internal/bridge/moved.go index a559662c..9b25e9b4 100644 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/moved.go +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/moved.go @@ -44,52 +44,37 @@ func (m Moved) HeadKey() string { return "branch." + m.Owner + "." + m.Repo + "." + keys.EscapeName(m.Branch) } -// Branch is a branch as the seat git's verb branch reads it: its head and whether it is protected. -type Branch struct { - Head string - Protected bool -} - -// ReadBranch reads a branch's head and protection through the seat git's verb branch (§11, §17); an answer that says -// an error, or names no 40-hex head, is an error, never a branch with none. -func ReadBranch(ctx context.Context, call Caller, m Moved) (Branch, error) { +// ReadHead reads a branch's head through the seat git's verb branch (design 47 H §739): never the event's body, and no +// protection (ADR 0682 §13). An answer that says an error, or names no 40-hex head, is an error, never a branch with none. +func ReadHead(ctx context.Context, call Caller, m Moved) (string, error) { a, err := askGit[git.BranchAnswer](ctx, call, git.VerbBranch, git.BranchInput{Repository: m.Repository(), Branch: m.Branch}) if err != nil { - return Branch{}, err + return "", err } if !sha40.MatchString(a.Head) { - return Branch{}, fmt.Errorf("the seat git's branch %s of %s names no 40-hex head (%q)", m.Branch, m.Repository(), a.Head) + return "", fmt.Errorf("the seat git's branch %s of %s names no 40-hex head (%q)", m.Branch, m.Repository(), a.Head) } - return Branch{Head: a.Head, Protected: a.Protected}, nil + return a.Head, nil } -// MoveMessage is the one message of a move of a watched branch to head (design 47 G §509, §714, A-236): merged, with -// the pull request and the watched branch as base, when the seat's verb pull names head a pull request's merge; pushed, -// with the branch, head and before (the head last written for the branch, empty for none), when it names none. Both -// carry the take-in inputs at head. A pull the forge could not say is ErrRetry, never read as none. -func MoveMessage(ctx context.Context, call Caller, m Moved, head, before string, trees Trees, files Files, kept Kept) (Message, error) { - what := fmt.Sprintf("%s of %s at %s", m.Branch, m.Repository(), head) - raw, err := call(ctx, GitKey(git.VerbPull), git.PullInput{Repository: m.Repository(), MergeCommit: head}) +// HeadMoved is the one message of a move of a watched branch's head (design 47 H §739): its subject on the stream forge +// and its body, before the head the bridge held for the branch (empty the first time), start true when said at the +// bridge's start. A head equal to before writes nothing, which the caller decides before it asks. +func HeadMoved(m Moved, head, before string, start bool) (Message, error) { + event, err := forgemsg.HeadMovedEvent(m.Repository(), m.Branch) if err != nil { - return Message{}, fmt.Errorf("%w: %s: the seat git did not answer pull: %v", ErrRetry, what, err) + return Message{}, err } - var a git.PullAnswer - if err := json.Unmarshal(raw, &a); err != nil { - return Message{}, fmt.Errorf("%w: %s: the seat git's answer of pull is not its contract's: %v", ErrRetry, what, err) + data, err := forgemsg.EncodeHeadMoved(forgemsg.HeadMoved{Repository: m.Repository(), Branch: m.Branch, Head: head, Before: before, Start: start}) + if err != nil { + return Message{}, err } - f := forgemsg.Forge{Repository: m.Repository()} - switch { - case a.Error != "" && a.ErrorKind == git.ErrorKindNotFound: - f.Kind, f.Branch, f.Head, f.Before = forgemsg.ForgePushed, m.Branch, head, before - case a.Error != "": - return Message{}, fmt.Errorf("%w: %s: the seat git refused pull: %s", ErrRetry, what, a.Error) - case a.Pull == nil || a.Pull.Number <= 0 || a.Pull.MergeCommit != head || a.Pull.State != git.PullStateMerged: - return Message{}, fmt.Errorf("%w: %s: the seat git's pull at that merge commit names no merged pull request merged there", ErrRetry, what) - default: - if !sha40.MatchString(a.Pull.Head) || a.Pull.Branch == "" { - return Message{}, unreadable(BranchMovedSubject, "%s: the pull request %s#%d names no branch or 40-hex head", what, m.Repository(), a.Pull.Number) - } - f.Kind, f.Branch, f.Number, f.Head, f.Merge, f.Base = forgemsg.ForgeMerged, a.Pull.Branch, int(a.Pull.Number), a.Pull.Head, head, m.Branch + said := fmt.Sprintf("%s of %s moved to %s", m.Branch, m.Repository(), head) + if before != "" { + said += " from " + before } - return landed(what, f, head, trees, files, kept) + if start { + said += ", read at the start" + } + return Message{Event: event, Data: data, Said: said}, nil } diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/moved_test.go b/modules/mesh-delivery-forge-bridge/internal/bridge/moved_test.go index 17ed5a68..809ec10f 100644 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/moved_test.go +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/moved_test.go @@ -3,20 +3,17 @@ package bridge import ( "context" "encoding/json" - "errors" "testing" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" - - "mesh-delivery-forge-bridge/internal/gitfixture" + "git.novox.be/novox/mesh-sdk/go/keys" ) -// Design 47 G §4, §11, §507, §509, §714, A-236 (FB1, FB9, FB10): the watched branches are the registrations' by origin, and -// before the first registration the gate's source's, no branch fixed in code; a move of a watched branch is one message: -// merged with its pull request and base when the seat's pull names the new head a pull request's merge, pushed with -// its branch, head and before when it names none, both carrying the commit's take-in inputs; a pull the forge could not -// say is heard again, never read as a push. -func TestAMoveOfAWatchedBranchIsOneMessage(t *testing.T) { +// Design 47 G §4, §507, A-236; H §739: the watched branches are the registrations' by origin, and before the first +// registration the gate's source's, no branch fixed in code; a move of a watched branch is one head-moved naming the +// repository, the branch, the head read through the seat git's verb branch, the head held before and start; the read asks +// nothing but branch, and an unprotected branch's head is read like any other. +func TestAMoveOfAWatchedBranchIsOneHeadMoved(t *testing.T) { w := &Watched{} w.Set([]Registered{{Origin: "novox/mesh-sdk", Branches: []string{"main", "beta"}}}, []string{"main"}) for _, c := range []struct { @@ -31,47 +28,50 @@ func TestAMoveOfAWatchedBranchIsOneMessage(t *testing.T) { t.Errorf("before the first registration the gate's source's branches are watched, of every repository") } - const landedAt, before = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "cccccccccccccccccccccccccccccccccccccccc" - moved := Moved{Owner: "novox", Repo: "mesh-sdk", Branch: "beta"} + const head, before = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "cccccccccccccccccccccccccccccccccccccccc" + moved := Moved{Owner: "novox", Repo: "mesh-sdk", Branch: "feat/47-H"} for _, c := range []struct { name string - pull json.RawMessage - want forgemsg.Forge - retry bool - calls1 int + answer string + head string + fails bool }{ - {"a merge", gitfixture.Given(t, "pull answers", "verb:pull.answer"), forgemsg.Forge{Kind: forgemsg.ForgeMerged, - Branch: "feat/47-G", Number: 178, Head: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Merge: landedAt, Base: "beta"}, false, 1}, - {"an admitted push", json.RawMessage(`{"error":"no pull request merged at that commit","error-kind":"not-found"}`), - forgemsg.Forge{Kind: forgemsg.ForgePushed, Branch: "beta", Head: landedAt, Before: before}, false, 1}, - {"a pull the forge could not say", gitfixture.Given(t, "pull answers an error, never none", "verb:pull.answer"), - forgemsg.Forge{}, true, 0}, + {"a protected branch", `{"head":"` + head + `","protected":true}`, head, false}, + {"an unprotected branch, read like any other (ADR 0682 §13)", `{"head":"` + head + `","protected":false}`, head, false}, + {"an answer naming no head", `{"head":"abc"}`, "", true}, + {"a refusal", `{"error":"no such branch","error-kind":"not-found"}`, "", true}, } { var asked []string ask := func(_ context.Context, key string, body any) (json.RawMessage, error) { b, _ := json.Marshal(body) asked = append(asked, key+" "+string(b)) - return c.pull, nil + return json.RawMessage(c.answer), nil } - var trees []call - msg, err := MoveMessage(context.Background(), ask, moved, landedAt, before, treesOf(&trees, "r0", map[string]string{"modules/a": "t1"}, nil), anyFiles, nil) - if c.retry { - if !errors.Is(err, ErrRetry) || msg.Data != nil { - t.Errorf("%s: %v, %s; want ErrRetry and nothing written", c.name, err, msg.Data) - } - continue + got, err := ReadHead(context.Background(), ask, moved) + if (err != nil) != c.fails || got != c.head { + t.Errorf("%s: %q %v, want %q, failing %v", c.name, got, err, c.head, c.fails) } + if len(asked) != 1 || asked[0] != `seat:git.branch {"branch":"feat/47-H","repository":"novox/mesh-sdk"}` { + t.Errorf("%s: asked %v, want only the seat git's branch", c.name, asked) + } + } + + for _, c := range []struct { + name string + before string + start bool + }{{"the first move", "", false}, {"a later move", before, false}, {"read at the start", before, true}} { + msg, err := HeadMoved(moved, head, c.before, c.start) if err != nil { t.Fatalf("%s: %v", c.name, err) } - f, err := forgemsg.DecodeForge(msg.Data) - c.want.V, c.want.Repository, c.want.Root = forgemsg.Version, "novox/mesh-sdk", "r0" - c.want.Trees, c.want.Manifests = map[string]string{"modules/a": "t1"}, map[string]string{"modules/a": "{}"} - if err != nil || !forgemsg.SameForge(f, c.want) || msg.Event != "forge.novox.mesh-sdk."+c.want.Kind { - t.Errorf("%s: %s %+v (%v), want %+v", c.name, msg.Event, f, err, c.want) - } - if len(trees) != c.calls1 || trees[0].sha != landedAt || asked[0] != `seat:git.pull {"merge-commit":"`+landedAt+`","repository":"novox/mesh-sdk"}` { - t.Errorf("%s: trees asked %v, pull asked %v; want the landed commit's", c.name, trees, asked) + h, err := forgemsg.DecodeHeadMoved(msg.Data) + want := forgemsg.HeadMoved{V: forgemsg.Version, Repository: "novox/mesh-sdk", Branch: "feat/47-H", Head: head, Before: c.before, Start: c.start} + if err != nil || h != want || msg.Event != "forge.novox.mesh-sdk."+keys.EscapeName("feat/47-H")+".head-moved" { + t.Errorf("%s: %s %+v (%v), want %+v", c.name, msg.Event, h, err, want) } } + if _, err := HeadMoved(moved, head, head, false); err == nil { + t.Errorf("a head equal to the one held makes a message; want none") + } } diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/owed.go b/modules/mesh-delivery-forge-bridge/internal/bridge/owed.go index 425865ae..4fc6d2ed 100644 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/owed.go +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/owed.go @@ -7,14 +7,12 @@ import ( git "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" - checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" ) // OwedCall is one act owed to the forge as the seat git's verb that performs it: the seat that said it (Of), the act's // id, and the verb with its input (novox/hq design 47 *What the gate owes the forge*). Every verb is safe to repeat at -// the forge: a status and the view replace what stands, a merge of a pull request merged at that head is answered -// performed, and a comment and a note line carry the id, which the seat's holder skips when it already holds it. +// the forge: a status and the view replace what stands, and a comment and a note line carry the id, which the seat's holder skips when it already holds it. type OwedCall struct { Of string ID string @@ -53,13 +51,6 @@ func GateOwed(data []byte) (OwedCall, error) { return nil } switch o.Act { - case seat.OwedActMerge: - // Only a switched act is a merge (§169): from G's delivery the gate owes none (§167), a session calls merge-pull - // itself; a merge of the old state, not yet performed by the old bridge, is not dropped. - if err := needsPull(); err != nil { - return c, err - } - c.Verb, c.Input = git.VerbMergePull, git.MergePullInput{ID: o.ID, Repository: o.Repository, Number: o.Number, Head: o.Head} case seat.OwedActStatus: if o.Context == "" || o.State == "" { return c, undecodable("the owed status %s names no context or state", o.ID) @@ -87,24 +78,6 @@ func GateOwed(data []byte) (OwedCall, error) { return c, nil } -// CheckerOwed reads the seat mesh-delivery-checker's event owed (its mesh/check status) into the seat git's verb status. -func CheckerOwed(data []byte) (OwedCall, error) { - var o checker.OwedEvent - if err := json.Unmarshal(data, &o); err != nil { - return OwedCall{}, undecodable("the checker's owed is not json: %v", err) - } - c := OwedCall{Of: checker.Seat, ID: o.ID, Act: o.Act, Repository: o.Repository, Number: int(o.Number)} - if err := c.common(o.Head); err != nil { - return c, err - } - if o.Act != checker.OwedActStatus || o.Context == "" || o.State == "" { - return c, undecodable("the checker's owed %s is %q of the context %q in the state %q, not a status", o.ID, o.Act, o.Context, o.State) - } - c.Verb, c.Input = git.VerbStatus, git.StatusInput{ID: o.ID, Repository: o.Repository, Commit: o.Head, Context: o.Context, - State: o.State, Description: o.Description, Target: o.Target} - return c, nil -} - func (c OwedCall) common(head string) error { switch { case c.ID == "": @@ -121,20 +94,18 @@ func (c OwedCall) common(head string) error { } // Outcome is what an act's call of the seat git came to, as owed.ended says it: performed when the forge holds the act -// (done now, or its id already held); refused with the forge's own words when the seat answers a refusal or an error -// of the kind refused; could not be performed with the error when the seat did not answer, answered an error of the +// (done now, or its id already held); refused with the forge's own words when the seat answers an error of the kind +// refused; could not be performed with the error when the seat did not answer, answered an error of the // kind failed, or answered what the contract cannot read. func Outcome(raw json.RawMessage, callErr error) (outcome, words string) { if callErr != nil { return forgemsg.OwedCouldNotBePerformed, callErr.Error() } - var a git.MergePullAnswer // every act verb's answer is this one's fields or fewer + var a git.StatusAnswer // every act verb's answer (status, comment, view, note) has these fields if err := json.Unmarshal(raw, &a); err != nil { return forgemsg.OwedCouldNotBePerformed, "the seat git's answer is not its contract's: " + err.Error() } switch { - case a.Refused != "": - return forgemsg.OwedRefused, a.Refused case a.Error != "" && a.ErrorKind == git.ErrorKindRefused: return forgemsg.OwedRefused, a.Error case a.Error != "": @@ -143,5 +114,5 @@ func Outcome(raw json.RawMessage, callErr error) (outcome, words string) { case a.Performed || a.Held: return forgemsg.OwedPerformed, "" } - return forgemsg.OwedCouldNotBePerformed, "the seat git's answer says neither performed, held, refused nor an error: " + string(raw) + return forgemsg.OwedCouldNotBePerformed, "the seat git's answer says neither performed, held nor an error: " + string(raw) } diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/owed_test.go b/modules/mesh-delivery-forge-bridge/internal/bridge/owed_test.go index 394ab724..50fd163d 100644 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/owed_test.go +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/owed_test.go @@ -17,7 +17,6 @@ import ( // act, under the act's id; what cannot be read is undecodable, and every answer is one outcome. func TestAnOwedEventIsTheSeatGitsVerbAndItsAnswerOneOutcome(t *testing.T) { gate := func(name string) []byte { return gitfixture.Seat(t, "mesh-delivery", "events", name, "event:owed")[0] } - chk := gitfixture.Seat(t, "mesh-delivery-checker", "events", "owed is said", "event:owed")[0] a40 := strings.Repeat("a", 40) edit := func(raw []byte, k string, v any) []byte { var m map[string]any @@ -38,7 +37,7 @@ func TestAnOwedEventIsTheSeatGitsVerbAndItsAnswerOneOutcome(t *testing.T) { input any bad string }{ - {"a merge on no pull request", GateOwed, edit(gate("owed is said, a merge"), "number", 0), "", nil, "no pull request"}, + {"a merge, which the bridge no longer performs (design 47 H §764)", GateOwed, gate("owed is said, a merge"), "", nil, "none the seat git"}, {"a status on the head", GateOwed, gate("owed is said, a status"), "status", git.StatusInput{ID: "status.novox/mesh-sdk#178.aaaaaaaaaaaa.mesh/merge-gate", Repository: "novox/mesh-sdk", Commit: a40, Context: "mesh/merge-gate", State: "pending", Description: "waits for the operator's Approve", Target: "https://forge.test/x"}, ""}, {"a view", GateOwed, edit(edit(gate("owed is said, a status"), "act", "view"), "text", "the view"), "view", @@ -47,13 +46,9 @@ func TestAnOwedEventIsTheSeatGitsVerbAndItsAnswerOneOutcome(t *testing.T) { git.CommentInput{ID: "status.novox/mesh-sdk#178.aaaaaaaaaaaa.mesh/merge-gate", Repository: "novox/mesh-sdk", Number: 178, Text: "hello"}, ""}, {"a note line on its commit", GateOwed, edit(edit(edit(gate("owed is said, a status"), "act", "note"), "line", "a line"), "commit", strings.Repeat("c", 40)), "note", git.NoteInput{ID: "status.novox/mesh-sdk#178.aaaaaaaaaaaa.mesh/merge-gate", Repository: "novox/mesh-sdk", Commit: strings.Repeat("c", 40), Line: "a line"}, ""}, - {"the checker's status", CheckerOwed, chk, "status", git.StatusInput{ID: "status.novox/mesh-sdk#178.aaaaaaaaaaaa.mesh/check", - Repository: "novox/mesh-sdk", Commit: a40, Context: "mesh/check", State: "success", Description: "passed", Target: "https://forge.test/x"}, ""}, {"not json", GateOwed, []byte("{"), "", nil, "not json"}, {"no id", GateOwed, edit(gate("owed is said, a merge"), "id", nil), "", nil, "no id"}, - {"a short head", CheckerOwed, edit(chk, "head", "abc"), "", nil, "40 hex"}, - {"a merge of the switch", GateOwed, gate("owed is said, a merge"), "merge-pull", git.MergePullInput{ID: "merge.feat=47-G.0123456789ab.novox/mesh-sdk#178", - Repository: "novox/mesh-sdk", Number: 178, Head: a40}, ""}, + {"a short head", GateOwed, edit(gate("owed is said, a status"), "head", "abc"), "", nil, "40 hex"}, {"a comment on no pull request", GateOwed, edit(edit(gate("owed is said, a status"), "act", "comment"), "number", 0), "", nil, "no pull request"}, {"an act of no kind", GateOwed, edit(gate("owed is said, a status"), "act", "label"), "", nil, "none the seat git"}, {"a repository not owner/name", GateOwed, edit(gate("owed is said, a status"), "repository", "mesh-sdk"), "", nil, "owner"}, @@ -78,8 +73,7 @@ func TestAnOwedEventIsTheSeatGitsVerbAndItsAnswerOneOutcome(t *testing.T) { err error outcome, words string }{ - {"a merge performed", answers("merge-pull")[0], nil, forgemsg.OwedPerformed, ""}, - {"a merge refused", answers("merge-pull")[1], nil, forgemsg.OwedRefused, "the head moved"}, + {"a status performed", answers("status")[0], nil, forgemsg.OwedPerformed, ""}, {"a comment already held", answers("comment")[0], nil, forgemsg.OwedPerformed, ""}, {"the forge's error, of the kind refused", failed("comment"), nil, forgemsg.OwedRefused, "403"}, {"the forge's error, of the kind failed", failed("view"), nil, forgemsg.OwedCouldNotBePerformed, "502"}, diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/paths_test.go b/modules/mesh-delivery-forge-bridge/internal/bridge/paths_test.go deleted file mode 100644 index 734224fd..00000000 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/paths_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package bridge - -import ( - "os" - "testing" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" -) - -// ADR 0495 clause 1 with the lead's decision L8 of R4e: the forge bridge is no reader of a manifest's `paths`. A merge of a -// module whose artifacts name their paths (mesh-sdk's module.json at 9cb4d59) gives a message that carries the manifest as -// the forge answered it and no named path's object: the indexer asks the forge for those itself. -func TestAMergedMessageCarriesNoNamedPathsTheBridgeDerived(t *testing.T) { - raw, err := os.ReadFile("testdata/mesh-sdk-9cb4d59.module.json") - if err != nil { - t.Fatal(err) - } - files := func(owner, repo, path, ref string) ([]byte, error) { - if path == "module.json" { - return raw, nil - } - return nil, ErrNotFound - } - var calls []call - msg, err := convertMerged(mergedBody(map[string]any{"repo": "mesh-sdk", "module_dirs": []string{""}}), treesOf(&calls, "r0", map[string]string{"": "r0"}, nil), files, nil) - if err != nil { - t.Fatal(err) - } - f, err := forgemsg.DecodeForge(msg.Data) - if err != nil { - t.Fatal(err) - } - if len(f.Paths) != 0 || f.Manifests[""] != string(raw) || len(calls) != 1 { - t.Errorf("message paths %v, manifest carried %v, trees asked %d times; want no paths, the manifest, one ask", f.Paths, f.Manifests[""] == string(raw), len(calls)) - } -} diff --git a/modules/mesh-delivery-forge-bridge/internal/groups/groups.go b/modules/mesh-delivery-forge-bridge/internal/groups/groups.go deleted file mode 100644 index f3f76197..00000000 --- a/modules/mesh-delivery-forge-bridge/internal/groups/groups.go +++ /dev/null @@ -1,301 +0,0 @@ -// Package groups is what the forge bridge's process and its tools bundle share of a delivery group's end (novox/hq ADR -// 0633 §4, issue 1585): the keys of the bridge's state performed, and the asks of the state end-asks, through which the -// tool bridge_group_end_again asks the process to say a group's end again (the bridge serves no request subject, ADR 0437 -// §4, so a tool asks in a state the process watches, as indexer_take_in asks the indexer in takeins). -package groups - -import ( - "context" - "crypto/sha256" - "encoding/hex" - "encoding/json" - "errors" - "fmt" - "slices" - "sort" - "strings" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" - "git.novox.be/novox/mesh-sdk/go/keys" -) - -// The bridge's states the tools read and write. -const ( - // PerformedState is the bridge's state of what it performed and published. - PerformedState = "performed" - // EndAsksState is the state of asks to say a group's end again, keyed by the group escaped as a key. - EndAsksState = "end-asks" -) - -// EndAsksBucket is its bucket on the bus, `_` of the module the bridge runs as, its credential's (A-296). -func EndAsksBucket(module string) string { return module + "_" + EndAsksState } - -// Key is the key of performed that says a group's end on a base is written: `groups..`, each part escaped as -// a key name, its value the last member's merge commit the end named (design 47 G §715: one end per base). -func Key(group, base string) (string, error) { - part, err := KeyOf(group, base) - if err != nil { - return "", err - } - return "groups." + part, nil -} - -// KeyOf is a group's id (its branch) and a base, each escaped as a key name, as the states performed and end-asks key -// them: `.`. -func KeyOf(group, base string) (string, error) { - if strings.TrimSpace(group) == "" { - return "", errors.New("a group's id is empty") - } - if strings.TrimSpace(base) == "" { - return "", errors.New("a group's base is empty") - } - return keys.EscapeName(group) + "." + keys.EscapeName(base), nil -} - -// Bases are the distinct bases of a group's members, in the order the members name them; a member naming none is -// named in missing by its repository and number. -func Bases(g seat.Group) (bases, missing []string) { - for _, m := range g.Members { - switch { - case m.Base == "": - missing = append(missing, fmt.Sprintf("%s#%d", m.Repository, m.Number)) - case !slices.Contains(bases, m.Base): - bases = append(bases, m.Base) - } - } - return bases, missing -} - -// OnBase is the members of a group whose base is base, in the group's order. -func OnBase(g seat.Group, base string) []seat.Member { - var out []seat.Member - for _, m := range g.Members { - if m.Base == base { - out = append(out, m) - } - } - return out -} - -// PublishedKey is the key of performed that says a member's merged message is written: `published.<16 hex digits of the -// sha256 of #>`, its value the merge commit the message named (a pull request merges once). -func PublishedKey(repository string, number int) string { - sum := sha256.Sum256([]byte(fmt.Sprintf("%s#%d", strings.ToLower(repository), number))) - return "published." + hex.EncodeToString(sum[:8]) -} - -// EndID is the message id of a group's end on a base, and of one said again by hand at a time: the stream's duplicate -// window drops a second publish of the same id. -func EndID(group, base, lastMerge, again string) string { - id := "group-end." + group + "." + base + "." + lastMerge - if again != "" { - id += "." + again - } - return id -} - -// AgainStamp is the time a hand ask appends to a group end's message id. -func AgainStamp(at time.Time) string { return at.UTC().Format("20060102T150405.000000000Z") } - -// EndAsk is a value of the end-asks state: which group's end a person asked to be said again, why, who and when, and, once -// the bridge read it, its answer. -type EndAsk struct { - V int `json:"v"` - Group string `json:"group"` - Base string `json:"base"` - Why string `json:"why,omitempty"` - By string `json:"by,omitempty"` - At time.Time `json:"at"` - Written bool `json:"written,omitempty"` - Answer string `json:"answer,omitempty"` - Answered time.Time `json:"answered,omitzero"` -} - -// Done says the ask is answered. -func (a EndAsk) Done() bool { return !a.Answered.IsZero() } - -// NewEndAsk is the value bridge_group_end_again writes for a group: its key (the group escaped as a key) and the value. -func NewEndAsk(group, base, why, by string, at time.Time) (string, []byte, error) { - group, base = strings.TrimSpace(group), strings.TrimSpace(base) - if group == "" { - return "", nil, errors.New("name the group whose end is said again (group)") - } - if base == "" { - return "", nil, errors.New("name the watched branch its end is said again on (base)") - } - key, err := KeyOf(group, base) - if err != nil { - return "", nil, err - } - data, err := json.Marshal(EndAsk{V: 1, Group: group, Base: base, Why: strings.TrimSpace(why), By: by, At: at.UTC()}) - return key, data, err -} - -// DecodeEndAsk reads an end-asks value. -func DecodeEndAsk(data []byte) (EndAsk, error) { - var a EndAsk - if err := json.Unmarshal(data, &a); err != nil { - return a, err - } - if a.V != 1 || a.Group == "" || a.Base == "" { - return a, errors.New("an end-asks value of no version 1 naming no group and base") - } - return a, nil -} - -// Row is one group on one base as bridge_groups answers it: the seat mesh-delivery's group and what the bridge kept of its end. -type Row struct { - Key string `json:"key"` - Group string `json:"group"` - Base string `json:"base"` - State string `json:"state,omitempty"` - Members []RowMember `json:"members"` - Ended bool `json:"ended"` - // EndedWith is the last member's merge commit the end named, when it is written. - EndedWith string `json:"ended-with,omitempty"` - Open bool `json:"open"` - Words string `json:"words"` - // LastEndAsk is the last ask to say the group's end again (bridge_group_end_again), its who, why and when, and the - // bridge's answer once it read it; absent when none was asked. The state end-asks keeps the last per group. - LastEndAsk *EndAsk `json:"last-end-ask,omitempty"` -} - -// RowMember is one member of a group: its delivery state at the seat and, once its merged message is written, the merge -// commit it named. -type RowMember struct { - Repository string `json:"repository"` - Number int `json:"number"` - State string `json:"state"` - Commit string `json:"commit,omitempty"` - Published bool `json:"published"` -} - -// Rows are the groups the seat mesh-delivery's verb groups answers, each with what the bridge kept: ended (its end written, -// with the last merge it named) or open (some member's merged message is not written, or its end not written). published -// is performed's `published.` values by key (the merge commit), ended its `groups.` values by key; asks are the end-asks -// values by the same key, and one that does not read is named unread as `end-asks.`. A group whose id cannot be a -// key is named unread by its id. -func Rows(of []seat.Group, published, ended map[string]string, asks map[string][]byte) (rows []Row, unread []string) { - lastAsk := map[string]*EndAsk{} - for key, raw := range asks { - a, err := DecodeEndAsk(raw) - if err != nil { - unread = append(unread, EndAsksState+"."+key) - continue - } - lastAsk[key] = &a - } - for _, g := range of { - if len(g.Members) < 2 { - continue - } - bases, missing := Bases(g) - for _, m := range missing { - unread = append(unread, "the member "+m+" of the group "+g.ID+" names no base") - } - for _, base := range bases { - key, err := KeyOf(g.ID, base) - if err != nil { - unread = append(unread, "a group with no id") - continue - } - r := Row{Key: key, Group: g.ID, Base: base, State: g.State} - allPublished := true - for _, m := range OnBase(g, base) { - commit, pub := published[PublishedKey(m.Repository, int(m.Number))] - r.Members = append(r.Members, RowMember{Repository: m.Repository, Number: int(m.Number), State: m.State, Commit: commit, Published: pub}) - allPublished = allPublished && pub - } - r.EndedWith, r.Ended = ended["groups."+key] - r.LastEndAsk = lastAsk[key] - r.Open = !r.Ended - switch { - case r.Ended: - r.Words = "its end is written" - case allPublished: - r.Words = "every member's merged message is written, and its end is not written: bridge_group_end_again says it" - default: - r.Words = "gathered: not every member's merged message is written" - } - rows = append(rows, r) - } - } - sortRows(rows) - sort.Strings(unread) - return rows, unread -} - -func sortRows(rows []Row) { sort.Slice(rows, func(i, j int) bool { return rows[i].Key < rows[j].Key }) } - -// MembersOf are a group's members with their heads, in the group's order, each pull request once: a group the seat -// answers with a member twice (feat/47-G-join11 named novox/mesh-sdk#189 twice, 2026-11-14) keeps the later head at the -// first place, since a merged message naming a member twice is refused whole and no merge of that branch is taken in. -func MembersOf(g seat.Group) []meshdeliveryclient.Head { - out := make([]meshdeliveryclient.Head, 0, len(g.Members)) - at := map[string]int{} - for _, m := range g.Members { - h := meshdeliveryclient.Head{Repository: m.Repository, Number: int(m.Number), Head: m.Head} - k := fmt.Sprintf("%s#%d", m.Repository, m.Number) - if i, twice := at[k]; twice { - out[i] = h - continue - } - at[k] = len(out) - out = append(out, h) - } - return out -} - -// Named is the group of id among the groups the seat answered, when it has more than one member and names the pull -// request number of repository among them (a lone pull request is no group). -func Named(of []seat.Group, id, repository string, number int) (seat.Group, bool) { - for _, g := range of { - if g.ID != id || len(g.Members) < 2 { - continue - } - if slices.ContainsFunc(g.Members, func(m seat.Member) bool { - return trunk.SameRepository(m.Repository, repository) && int(m.Number) == number - }) { - return g, true - } - } - return seat.Group{}, false -} - -// Read is the open delivery groups the seat mesh-delivery's verb groups answers by default, called through call by the -// verb's manifest key: the bridge reads a group from the seat, never from the gate's state (design 47 *A group's merges are -// taken in together*). The ended ones are never asked whole: their list outgrows one bus message (lead: principle 7, live -// 02:06); one is read by its id with ReadGroup. An answer saying an error, or one the contract cannot type, is an error: a -// failed read is never read as no group. -func Read(ctx context.Context, call func(ctx context.Context, key string, body any) (json.RawMessage, error)) ([]seat.Group, error) { - return ask(ctx, call, seat.GroupsInput{}) -} - -// ReadAll is every delivery group, the ended ones too (all). Its answer grows with every group that ended, so only a -// tool a person asks reads it, never the take-in or the catch-up. -func ReadAll(ctx context.Context, call func(ctx context.Context, key string, body any) (json.RawMessage, error)) ([]seat.Group, error) { - return ask(ctx, call, seat.GroupsInput{All: seat.GroupsInputAllTrue}) -} - -// ReadGroup is the one group id names, ended or not, as the verb answers with its group: none for a group it does not know. -func ReadGroup(ctx context.Context, call func(ctx context.Context, key string, body any) (json.RawMessage, error), id string) ([]seat.Group, error) { - return ask(ctx, call, seat.GroupsInput{Group: id}) -} - -func ask(ctx context.Context, call func(ctx context.Context, key string, body any) (json.RawMessage, error), in seat.GroupsInput) ([]seat.Group, error) { - raw, err := call(ctx, "seat:"+seat.Seat+"."+seat.VerbGroups, in) - if err != nil { - return nil, fmt.Errorf("the seat %s did not answer %s: %w", seat.Seat, seat.VerbGroups, err) - } - var a seat.GroupsAnswer - if err := json.Unmarshal(raw, &a); err != nil { - return nil, fmt.Errorf("the seat %s's answer of %s is not its contract's: %w", seat.Seat, seat.VerbGroups, err) - } - if a.Error != "" { - return nil, fmt.Errorf("the seat %s refused %s: %s", seat.Seat, seat.VerbGroups, a.Error) - } - return a.Groups, nil -} diff --git a/modules/mesh-delivery-forge-bridge/internal/groups/owedask.go b/modules/mesh-delivery-forge-bridge/internal/groups/owedask.go index ab509455..02e4d96a 100644 --- a/modules/mesh-delivery-forge-bridge/internal/groups/owedask.go +++ b/modules/mesh-delivery-forge-bridge/internal/groups/owedask.go @@ -10,9 +10,11 @@ import ( // What a person asks of the bridge's performing of what is owed (novox/hq design 47 *What the gate owes the forge*): the // tools bridge_owed (its consumers' backlog), bridge_owed_again and bridge_owed_drop write an ask into the state -// owed-asks and wait for the bridge's answer there, as bridge_group_end_again does, since the bridge is the other program +// owed-asks and wait for the bridge's answer there, since the bridge is the other program // and serves no request subject (ADR 0437 §4). const ( + // PerformedState is the bridge's state of the acts it performed, the `requires` entry performed. + PerformedState = "performed" // OwedAsksState is the state of asks about what is owed, one key per kind of ask and act. OwedAsksState = "owed-asks" ) diff --git a/modules/mesh-delivery-forge-bridge/internal/groups/read_test.go b/modules/mesh-delivery-forge-bridge/internal/groups/read_test.go deleted file mode 100644 index 672f4aee..00000000 --- a/modules/mesh-delivery-forge-bridge/internal/groups/read_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package groups - -import ( - "context" - "encoding/json" - "errors" - "strings" - "testing" - - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" -) - -// The seat answers the whole list of groups with more than one bus message carries once many have ended (live 02:06); -// a read of the open ones, or of one by its id, is answered; Read never asks for all (lead: principle 7). -func TestReadsNeverAskTheEndedGroupsWhole(t *testing.T) { - const bus = 1 << 20 - seatOf := func(_ context.Context, _ string, body any) (json.RawMessage, error) { - in := body.(seat.GroupsInput) - groups := []seat.Group{{ID: "feat/open", State: "gathering"}} - switch { - case in.Group != "": - groups = []seat.Group{{ID: in.Group}} - case in.All == seat.GroupsInputAllTrue: - for i := 0; i < 3000; i++ { - groups = append(groups, seat.Group{ID: "ended", Why: strings.Repeat("x", 500)}) - } - } - raw, _ := json.Marshal(seat.GroupsAnswer{Groups: groups}) - if len(raw) > bus { - return nil, errors.New("the answer is more than the bus carries in one message") - } - return raw, nil - } - if got, err := Read(context.Background(), seatOf); err != nil || len(got) != 1 || got[0].ID != "feat/open" { - t.Errorf("Read: %v %v; want the open group alone", got, err) - } - if got, err := ReadGroup(context.Background(), seatOf, "feat/ended"); err != nil || len(got) != 1 || got[0].ID != "feat/ended" { - t.Errorf("ReadGroup: %v %v; want the group by its id", got, err) - } - if _, err := ReadAll(context.Background(), seatOf); err == nil { - t.Errorf("ReadAll: want the seat's refusal to be an error, never read as no group") - } - // A member the seat names twice is said once, at its later head (2026-11-14: the take-in of every repository with a - // branch of that group was refused whole). - twice := seat.Group{ID: "feat/twice", Members: []seat.Member{{Repository: "o/a", Number: 1, Head: "old"}, {Repository: "o/b", Number: 2, Head: "b"}, {Repository: "o/a", Number: 1, Head: "new"}}} - if got := MembersOf(twice); len(got) != 2 || got[0].Repository != "o/a" || got[0].Head != "new" || got[1].Repository != "o/b" { - t.Errorf("MembersOf a group naming o/a#1 twice = %+v, want o/a#1 at new, then o/b#2", got) - } -} diff --git a/modules/mesh-delivery-forge-bridge/manifest_test.go b/modules/mesh-delivery-forge-bridge/manifest_test.go index a83d6332..9760f1bf 100644 --- a/modules/mesh-delivery-forge-bridge/manifest_test.go +++ b/modules/mesh-delivery-forge-bridge/manifest_test.go @@ -99,22 +99,21 @@ func TestManifestDeclarations(t *testing.T) { // 1235/AC1 if !reflect.DeepEqual(m.Streams, []stream{{Name: "forge", Events: []string{"forge.**", "owed.ended"}}}) { t.Errorf("streams = %+v", m.Streams) } - // What the gate and the checker owe the forge is each seat's event owed, heard on its own stream (design 47 *What the - // gate owes the forge*), never read from their state. The forge's events are the seat git's (design 47 *The seat git*); forge.delete: a branch's end, said as a - // branch-deleted kind (design 51, DLV-FB1). The state performed's binding is asked again on the seat mesh-broker's - // recreated and dropped, and asked of its show (design 47 G §577). - // Every move of a watched branch is heard on branch-moved, and the watched branches from the registrations, before the - // first the gate's source (design 47 G §4, §11, §507). - if want := []string{"git.branch-moved", "git.forge-delete", "git.pull-closed", "git.pull-merged", "git.pull-updated", "mesh-broker.dropped", "mesh-broker.recreated", "mesh-catalog.registration-changed", "mesh-conditions.findings-asked", "mesh-delivery-checker.owed", "mesh-delivery.owed", "mesh-delivery.source-moved"}; !reflect.DeepEqual(sorted(m.Consumes), want) { + // What the gate owes the forge is its seat's event owed, heard on its own stream (design 47 *What the gate owes the + // forge*), never read from its state. The state performed's binding is asked again on the seat mesh-broker's recreated + // and dropped, and asked of its show (design 47 G §577). Every move of a watched branch is heard on branch-moved, and + // the watched branches from the registrations, before the first the gate's source (design 47 G §4, §507; H §739). No + // pull request event, no checker and no finding is heard (design 47 H §764, H-A-48). + if want := []string{"git.branch-moved", "mesh-broker.dropped", "mesh-broker.recreated", "mesh-catalog.registration-changed", "mesh-delivery.owed", "mesh-delivery.source-moved"}; !reflect.DeepEqual(sorted(m.Consumes), want) { t.Errorf("consumes = %v, want %v", m.Consumes, want) } - // A group is read from the seat mesh-delivery's verb groups (design 47, acceptance 49): the bridge reads no state of - // another module's. + // The bridge reads no state of another module's. if len(m.Reads) != 0 { t.Errorf("reads = %v, want none", m.Reads) } - if want := []string{"seat:git.branch", "seat:git.comment", "seat:git.file", "seat:git.merge-pull", "seat:git.note", "seat:git.open-pulls", - "seat:git.pull", "seat:git.status", "seat:git.tags", "seat:git.trees", "seat:git.view", "seat:mesh-broker.show", "seat:mesh-catalog.registrations", "seat:mesh-delivery.groups", "seat:mesh-delivery.source", "seat:mesh-hand-acts.hand-act"}; !reflect.DeepEqual(sorted(m.Invokes), want) { + // No pull, tree, file, open pull requests, merge or group is asked (design 47 H §739, §764). + if want := []string{"seat:git.branch", "seat:git.comment", "seat:git.note", "seat:git.status", "seat:git.tags", "seat:git.view", + "seat:mesh-broker.show", "seat:mesh-catalog.registrations", "seat:mesh-delivery.source", "seat:mesh-hand-acts.hand-act"}; !reflect.DeepEqual(sorted(m.Invokes), want) { t.Errorf("invokes = %v, want %v", m.Invokes, want) } if b, ok := m.OwnSecrets["broker"]; !ok || len(m.OwnSecrets) != 1 || b != "${dir:state}/broker" { @@ -123,25 +122,34 @@ func TestManifestDeclarations(t *testing.T) { // 1235/AC1 if m.SecretsOwner != "mesh-delivery-forge-bridge" { t.Errorf("secrets-owner = %q", m.SecretsOwner) } - // The head last written on FORGE for each pull request, so a sync writes only what differs (ADR 0443 §5), and what a - // merge read of each module directory by its tree, so a merge reads the forge only for the directories it changed - // (issue 1126). What it performed of what is owed is kept data in the new form, one key per act, kept 7 days as the - // owners' streams owed are; its size is the template's own (design 47 *What the gate owes the forge*, ADR 0595 §3). - if !reflect.DeepEqual(m.State, []state{{Name: "heads", History: 1}, {Name: "inputs", History: 1}, {Name: "end-asks", History: 1}, {Name: "owed-asks", History: 1}}) { // 1279/AC6, 1126, 1585 - t.Errorf("state = %+v, want the buckets heads, inputs, end-asks and owed-asks", m.State) + // The head held for each watched branch (design 47 H §739), and the asks about what is owed. What it performed of what is + // owed is kept data in the new form, one key per act, kept 7 days as the owner's stream owed is; its size is the + // template's own (design 47 *What the gate owes the forge*, ADR 0595 §3). No inputs, end-asks or findings (H §764, H-A-48). + if !reflect.DeepEqual(m.State, []state{{Name: "heads", History: 1}, {Name: "owed-asks", History: 1}}) { + t.Errorf("state = %+v, want the buckets heads and owed-asks", m.State) } - // Design 47 G §387, §529: the bridge gains the finding branch-unprotected, so it asks the findings state and the finding stream - // by their templates (7 days, 16 MiB) as every part but a foundation part does. - if want := []kept{{Name: "performed", Storage: "key-value", Template: "state", Options: map[string]any{"history": float64(1), "ttl-seconds": float64(604800)}}, - {Name: "findings", Storage: "key-value", Template: "state", Options: map[string]any{"history": float64(5)}}, - {Name: "finding-events", Storage: "stream", Template: "events", Options: map[string]any{ - "events": []any{"finding-written.*", "finding-cleared.*", "findings-stated.*"}, "max-age-days": float64(7), "max-mib": float64(16)}}}; !reflect.DeepEqual(m.Requires, want) { - t.Errorf("requires = %+v, want performed, findings and finding-events as %+v", m.Requires, want) + if want := []kept{{Name: "performed", Storage: "key-value", Template: "state", Options: map[string]any{"history": float64(1), "ttl-seconds": float64(604800)}}}; !reflect.DeepEqual(m.Requires, want) { + t.Errorf("requires = %+v, want performed alone as %+v", m.Requires, want) } - // 1585/T18: the tools read the groups and ask the bridge to say a group's end again, through its state end-asks. The tool bridge_merged_message answers a merge's merged message again (issue 1585); bridge_tags reads a package's tags for the builder's floor (design 51). - if want := []string{"bridge_group_end_again", "bridge_groups", "bridge_merged_message", "bridge_owed", "bridge_owed_again", "bridge_owed_drop", "bridge_tags"}; !reflect.DeepEqual(sorted(m.Tools), want) { + // bridge_tags reads a package's tags for the builder's floor (design 51); the owed tools ask the bridge through owed-asks. + if want := []string{"bridge_owed", "bridge_owed_again", "bridge_owed_drop", "bridge_tags"}; !reflect.DeepEqual(sorted(m.Tools), want) { t.Errorf("tools = %v, want %v", m.Tools, want) } + // Design 47 H §764 (B3 AC1): the earlier model's names are in no file of the bridge but tests asserting their absence. + retired := []string{"pushed", "forge.group.merged", "bridge_groups", "bridge_group_end_again", "bridge_merged_message", + "OwedActMerge", "mesh-delivery-checker", "seat:git.merge-pull", "branch-unprotected", "ForgeMerged", "ForgePushed"} + _ = filepath.WalkDir(".", func(p string, d fs.DirEntry, err error) error { + if err != nil || d.IsDir() || strings.HasSuffix(p, "_test.go") || (!strings.HasSuffix(p, ".go") && !strings.HasSuffix(p, ".json")) { + return err + } + data, _ := os.ReadFile(p) + for _, name := range retired { + if bytes.Contains(data, []byte(name)) { + t.Errorf("%s names %q, which design 47 H §764 retires", p, name) + } + } + return nil + }) // No other state, no seat, no event (ADR 0437 §4; ADR 0521: no check.status); its kept data on the bus is the one entry of requires above. for _, k := range []string{"logs", "emits", "claims", "provides", "uses", "defines-seats", "seat", "seats"} { if _, ok := top[k]; ok { diff --git a/modules/mesh-delivery-forge-bridge/module.json b/modules/mesh-delivery-forge-bridge/module.json index f4317a99..d4243381 100644 --- a/modules/mesh-delivery-forge-bridge/module.json +++ b/modules/mesh-delivery-forge-bridge/module.json @@ -3,16 +3,10 @@ "version": "0.1", "slug": "forge-bridge", "consumes": [ - "git.pull-updated", - "git.pull-merged", - "git.pull-closed", - "git.forge-delete", "git.branch-moved", "mesh-catalog.registration-changed", - "mesh-conditions.findings-asked", "mesh-delivery.source-moved", "mesh-delivery.owed", - "mesh-delivery-checker.owed", "mesh-broker.recreated", "mesh-broker.dropped" ], @@ -21,15 +15,10 @@ ], "state": [ { "name": "heads", "history": 1 }, - { "name": "inputs", "history": 1 }, - { "name": "end-asks", "history": 1 }, { "name": "owed-asks", "history": 1 } ], "tools": [ "bridge_tags", - "bridge_groups", - "bridge_group_end_again", - "bridge_merged_message", "bridge_owed", "bridge_owed_again", "bridge_owed_drop" @@ -39,13 +28,7 @@ "seat:git.comment", "seat:git.view", "seat:git.note", - "seat:git.merge-pull", - "seat:mesh-delivery.groups", "seat:mesh-hand-acts.hand-act", - "seat:git.open-pulls", - "seat:git.trees", - "seat:git.file", - "seat:git.pull", "seat:git.tags", "seat:git.branch", "seat:mesh-catalog.registrations", @@ -58,22 +41,6 @@ "storage": "key-value", "template": "state", "options": { "history": 1, "ttl-seconds": 604800 } - }, - { - "name": "findings", - "storage": "key-value", - "template": "state", - "options": { "history": 5 } - }, - { - "name": "finding-events", - "storage": "stream", - "template": "events", - "options": { - "events": ["finding-written.*", "finding-cleared.*", "findings-stated.*"], - "max-age-days": 7, - "max-mib": 16 - } } ], "own-secrets": {