From dcb7ba016cbac879a0cf4b797108c166289306da Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 12 Nov 2026 06:17:01 +0100 Subject: [PATCH 1/5] FB12: the forge bridge opens its state performed by its binding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The state performed is a requires entry, so the bridge reaches it by mesh-sdk's process.Binding: the seat mesh-broker's show asked at the start as a kept call and again on its recreated and dropped, the bucket opened by the name show answers. Until it answers, nothing owed is performed and the bridge says it waits, naming the entry. The old name _performed goes. mesh-sdk's Binding is cherry-picked on feat/47-G-fix11-b3c from feat/47-G-fix11-a7 (fa0a107). Builds: design 47 G §577 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h --- .../cmd/mesh-delivery-forge-bridge/bound.go | 100 ++++++++++++++++++ .../mesh-delivery-forge-bridge/bound_test.go | 61 +++++++++++ .../mesh-delivery-forge-bridge/gate_test.go | 1 + .../cmd/mesh-delivery-forge-bridge/held.go | 2 +- .../integration_test.go | 26 +++-- .../cmd/mesh-delivery-forge-bridge/main.go | 21 ++-- .../mesh-delivery-forge-bridge/names_test.go | 2 +- .../cmd/mesh-delivery-forge-bridge/owed.go | 25 ++++- .../manifest_test.go | 7 +- .../mesh-delivery-forge-bridge/module.json | 7 +- 10 files changed, 225 insertions(+), 27 deletions(-) create mode 100644 modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound.go create mode 100644 modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound_test.go diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound.go new file mode 100644 index 00000000..ab5b8c77 --- /dev/null +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound.go @@ -0,0 +1,100 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "sync" + + "github.com/nats-io/nats.go/jetstream" + + "git.novox.be/novox/mesh-sdk/go/busretry" + "git.novox.be/novox/mesh-sdk/go/process" +) + +// brokerSeat is the seat whose verb `show` answers a `requires` entry's bus names, and whose `recreated` and `dropped` +// say it changed (design 47 G §577): the manifest names `seat:mesh-broker.show` in `invokes` and the two events in +// `consumes`. +const brokerSeat = "mesh-broker" + +// performedLocal is the local name of the state performed in the manifest's `requires`. +const performedLocal = "performed" + +// keptStore is the part of a key-value bucket the bridge's stores use. +type keptStore interface { + Get(ctx context.Context, key string) (jetstream.KeyValueEntry, error) + Put(ctx context.Context, key string, value []byte) (uint64, error) + PutString(ctx context.Context, key string, value string) (uint64, error) + Delete(ctx context.Context, key string, opts ...jetstream.KVDeleteOpt) error +} + +// boundBucket is a `requires` entry's bucket opened by its binding, never by an old name (design 47 G §577): each use +// asks the Binding for the bucket's bus name and opens it, again after `show` answers another name (a recreated entry). +// While `show` has not answered, every use fails with the Binding's error naming the entry, so nothing that needs it is +// done. +type boundBucket struct { + binding *process.Binding + local string + js process.BucketOpener + + mu sync.Mutex + name string + kv jetstream.KeyValue +} + +func (s *boundBucket) open(ctx context.Context) (jetstream.KeyValue, error) { + names, err := s.binding.Names(s.local) + if err != nil { + return nil, err + } + if names.Bucket == "" { + return nil, fmt.Errorf("the bus entry %q is bound to no bucket", s.local) + } + s.mu.Lock() + defer s.mu.Unlock() + if s.kv != nil && s.name == names.Bucket { + return s.kv, nil + } + kv, err := s.js.KeyValue(ctx, names.Bucket) + if err != nil { + return nil, fmt.Errorf("the bus entry %q, bucket %s: %w", s.local, names.Bucket, err) + } + s.name, s.kv = names.Bucket, kv + return kv, nil +} + +func (s *boundBucket) Get(ctx context.Context, key string) (jetstream.KeyValueEntry, error) { + kv, err := s.open(ctx) + if err != nil { + return nil, err + } + return kv.Get(ctx, key) +} + +func (s *boundBucket) Put(ctx context.Context, key string, value []byte) (uint64, error) { + kv, err := s.open(ctx) + if err != nil { + return 0, err + } + return kv.Put(ctx, key, value) +} + +func (s *boundBucket) PutString(ctx context.Context, key string, value string) (uint64, error) { + return s.Put(ctx, key, []byte(value)) +} + +func (s *boundBucket) Delete(ctx context.Context, key string, opts ...jetstream.KVDeleteOpt) error { + kv, err := s.open(ctx) + if err != nil { + return err + } + return kv.Delete(ctx, key, opts...) +} + +// keptCallOf is call as a kept call (ADR 0668 §1): asked again through a timeout or no responders until answered or +// busretry.Bound has passed. +func keptCallOf(ctx context.Context, call caller) func(key string, body any) (json.RawMessage, error) { + return func(key string, body any) (json.RawMessage, error) { + return busretry.DoRepeatable("the call "+key, func() (json.RawMessage, error) { return call(ctx, key, body) }, nil) + } +} 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 new file mode 100644 index 00000000..ab1c150c --- /dev/null +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound_test.go @@ -0,0 +1,61 @@ +package main + +import ( + "context" + "encoding/json" + "errors" + "strings" + "testing" + + "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-sdk/go/process" + + "mesh-delivery-forge-bridge/internal/gitfixture" +) + +// openerFake is a bus that opens no bucket and records each name asked. +type openerFake struct{ asked []string } + +func (o *openerFake) KeyValue(_ context.Context, bucket string) (jetstream.KeyValue, error) { + o.asked = append(o.asked, bucket) + return nil, errors.New("no bucket in a unit test") +} + +// performedIsOpenedByItsBinding: design 47 G §577, unit (forge bridge): the state performed is opened by the bus name +// the seat mesh-broker's show answers for its local name, never by an old name; until show answers no bucket is opened, +// the use names the entry, and nothing owed is performed. +func performedIsOpenedByItsBinding(t *testing.T) { + var shows []string + answered := false + show := func(key string, body any) (json.RawMessage, error) { + b, _ := json.Marshal(body) + shows = append(shows, key+" "+string(b)) + if !answered { + return nil, errors.New("nats: no responders available for request") + } + return json.RawMessage(`{"state":"created","binding":{"local":"performed","storage":"key-value","bucket":"bound-performed"}}`), nil + } + binding := process.NewBinding(brokerSeat, forgeBridgeModule, show, nil, performedLocal) + bus := &openerFake{} + kv := &boundBucket{binding: binding, local: performedLocal, js: bus} + binding.Ask(performedLocal) + if _, err := kv.Get(context.Background(), "k"); err == nil || !strings.Contains(err.Error(), `"performed"`) || len(bus.asked) != 0 { + t.Errorf("unbound: %v, opened %v; want an error naming the entry and no bucket opened", err, bus.asked) + } + 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] + 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)) + } + answered = true + binding.Ask(performedLocal) + _, _ = kv.Get(context.Background(), "k") + want := `seat:mesh-broker.show {"local-name":"performed","module":"mesh-delivery-forge-bridge"}` + if len(bus.asked) != 1 || bus.asked[0] != "bound-performed" || shows[len(shows)-1] != want { + t.Errorf("bound: opened %v after %v; want bound-performed, asked as %s", bus.asked, shows, want) + } +} 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 8ab15e0c..20c20e48 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 @@ -65,6 +65,7 @@ func newOwing(f *seatFake) (*owing, *fakePub, memPerformed) { // the bridge performs from the seat's groups every act said and not answered. 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") } for _, c := range []struct { 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 a0338539..a5b72909 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 @@ -12,7 +12,7 @@ import ( const headsBucket = forgeBridgeModule + "_heads" // kvHeads is the bucket heads as the bridge's headStore. -type kvHeads struct{ kv jetstream.KeyValue } +type kvHeads struct{ kv keptStore } func (s kvHeads) Get(ctx context.Context, key string) (string, bool, error) { e, err := s.kv.Get(ctx, key) diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go index bfb7b416..34fa9523 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go @@ -39,7 +39,8 @@ import ( // From module.json: the seat git's events heard through its durable on EVENTS; what the gate and the checker owe heard // through its durable on each seat's own stream owed (SEAT__owed); the seat git's verbs, the seat mesh-delivery's // groups and the seat mesh-hand-acts' hand-act called on mesh.seat..call.. (or its plain fallback); -// its own states heads, performed, inputs, end-asks and owed-asks; its stream forge. It reads no state of the gate's or +// its own states heads, inputs, end-asks and owed-asks, and performed under the name its binding gives (the seat +// mesh-broker's show); its stream forge. It reads no state of the gate's or // the checker's (design 47, acceptance 49 and 53). `**` is written `>` on the wire. The controller's own user keeps all // rights, as it has. const busConf = ` @@ -59,12 +60,12 @@ accounts { "$JS.API.STREAM.INFO.KV_mesh-delivery-forge-bridge_heads" "$JS.FC.KV_mesh-delivery-forge-bridge_heads.>" "$KV.mesh-delivery-forge-bridge_heads.>" - "$JS.API.CONSUMER.CREATE.KV_mesh-delivery-forge-bridge_performed.>" - "$JS.API.CONSUMER.DELETE.KV_mesh-delivery-forge-bridge_performed.>" - "$JS.API.DIRECT.GET.KV_mesh-delivery-forge-bridge_performed.>" - "$JS.API.STREAM.INFO.KV_mesh-delivery-forge-bridge_performed" - "$JS.FC.KV_mesh-delivery-forge-bridge_performed.>" - "$KV.mesh-delivery-forge-bridge_performed.>" + "$JS.API.CONSUMER.CREATE.KV_mod_mesh-delivery-forge-bridge_performed_b1.>" + "$JS.API.CONSUMER.DELETE.KV_mod_mesh-delivery-forge-bridge_performed_b1.>" + "$JS.API.DIRECT.GET.KV_mod_mesh-delivery-forge-bridge_performed_b1.>" + "$JS.API.STREAM.INFO.KV_mod_mesh-delivery-forge-bridge_performed_b1" + "$JS.FC.KV_mod_mesh-delivery-forge-bridge_performed_b1.>" + "$KV.mod_mesh-delivery-forge-bridge_performed_b1.>" "$JS.API.CONSUMER.CREATE.KV_mesh-delivery-forge-bridge_inputs.>" "$JS.API.CONSUMER.DELETE.KV_mesh-delivery-forge-bridge_inputs.>" "$JS.API.DIRECT.GET.KV_mesh-delivery-forge-bridge_inputs.>" @@ -94,6 +95,8 @@ accounts { "$JS.ACK.SEAT_mesh-delivery-checker_owed.n1_mesh-delivery-forge-bridge.>" "$JS.API.CONSUMER.INFO.SEAT_mesh-delivery-checker_owed.n1_mesh-delivery-forge-bridge" "$JS.API.CONSUMER.MSG.NEXT.SEAT_mesh-delivery-checker_owed.n1_mesh-delivery-forge-bridge" + "mesh.seat.mesh-broker.call.show.n1~mesh-delivery-forge-bridge" + "mesh.seat.mesh-broker.tool.show" "mesh.seat.git.call.status.n1~mesh-delivery-forge-bridge" "mesh.seat.git.tool.status" "mesh.seat.git.call.comment.n1~mesh-delivery-forge-bridge" @@ -201,6 +204,9 @@ func fileAnswer(path, ref, content string, found bool) string { return string(b) } +// boundPerformed is the bus name the stand-in seat mesh-broker's show answers for the state performed. +const boundPerformed = "mod_mesh-delivery-forge-bridge_performed_b1" + // startBed is startBridge with a step between the streams and the consumers: prepare writes what the streams hold before // the bridge's consumers are made, as the grants part makes them, from now (ADR 0443 §1), and reply sets the answer of the // seat git's open-pulls from then on. A stand-in holds the seats the bridge calls: git (every verb, trees r0 by default, @@ -232,8 +238,9 @@ func startBed(t *testing.T, openReply string, prepare func(ctx context.Context, t.Fatal(err) } } - // The bridge's states, made from the manifest's state entries and its requires before the process starts. - for _, name := range []string{headsBucket, performedBucket, inputsBucket, groupkeys.EndAsksBucket, groupkeys.OwedAsksBucket} { + // The bridge's states, made from the manifest's state entries and its requires before the process starts; the state + // performed under the bus name the seat mesh-broker's show answers for it (boundPerformed), never an old name (§577). + for _, name := range []string{headsBucket, boundPerformed, inputsBucket, groupkeys.EndAsksBucket, groupkeys.OwedAsksBucket} { if _, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: name, History: 1}); err != nil { t.Fatal(err) } @@ -259,6 +266,7 @@ func startBed(t *testing.T, openReply string, prepare func(ctx context.Context, answers := map[string]string{ "git.open-pulls": openReply, "git.trees": `{"root":"r0","trees":{"":"t0","modules/a":"t1"}}`, "mesh-delivery.groups": `{"groups":[]}`, "mesh-hand-acts.hand-act": `{"recorded":true}`, + "mesh-broker.show": `{"state":"created","binding":{"local":"performed","storage":"key-value","template":"state","bucket":"` + boundPerformed + `"}}`, } for _, verb := range []string{"status", "comment", "view", "note", "merge-pull"} { answers["git."+verb] = string(gitfixture.Given(t, verb+" answers", "verb:"+verb+".answer")) 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 6a9c63ea..91ff4208 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 @@ -148,12 +148,22 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro // 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} said := func(ctx context.Context) error { return syncOpen(ctx, call, rec, heads, &turn, logger) } - performedKV, err := process.EssentialBucket(ctx, js, "bridge", performedBucket, "keep what it performed of what is owed to the forge") - if err != nil { + // 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) + if err := binding.Register(hub); err != nil { return err } + binding.Ask(performedLocal) + 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} // What the gate and the checker owe the forge, performed through the seat git (design 47 *What the gate owes the forge*). - owe := &owing{call: call, pub: js, performed: kvPerformed{performedKV}, now: time.Now, logger: logger} + 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 @@ -209,9 +219,8 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro go onReconnect(ctx, again, said, logger) // 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, "a merge's module directories are kept in memory only, so the first merge after a start reads every one", logger.Printf), logger) - // The module's one pull of its durable consumer on the shared events stream is its event hub; the bridge has the one - // handler, for every event its consumes filter lets through. Essential: the forge's events are what the bridge carries. - hub := process.NewEventHub(logger.Printf) + // 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) { hear(&turn, func() { 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 f2e9e83f..43162be9 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 @@ -44,7 +44,7 @@ func theNamesAreTheManifests(t *testing.T) { own = append(own, forgeBridgeModule+"_"+kept.Name) } } - for _, b := range []string{headsBucket, performedBucket, inputsBucket} { + for _, b := range []string{headsBucket, inputsBucket} { 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/owed.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/owed.go index 9384e1c4..0ff09c15 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 @@ -25,11 +25,10 @@ import ( "mesh-delivery-forge-bridge/internal/groups" ) -// performedBucket is the module's state performed (manifest `requires`, the template state): one key per owed act it +// 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. -const performedBucket = forgeBridgeModule + "_performed" // 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 (`#`, `/`). @@ -50,7 +49,7 @@ type performedStore interface { Put(ctx context.Context, key string, value []byte) error } -type kvPerformed struct{ kv jetstream.KeyValue } +type kvPerformed struct{ kv keptStore } func (s kvPerformed) Get(ctx context.Context, key string) ([]byte, bool, error) { e, err := s.kv.Get(ctx, key) @@ -75,8 +74,10 @@ type owing struct { call caller pub publisher performed performedStore - now func() time.Time - logger *log.Logger + // waits, when set, says why the state performed cannot be used yet (its binding, design 47 G §577); empty when it can. + waits func() string + now func() time.Time + logger *log.Logger } // readerOf is the reading of the seat of's owed event into the seat git's verb. @@ -102,6 +103,9 @@ func (o *owing) heard(ctx context.Context, of string, data []byte) (forgemsg.Owe return o.end(ctx, forgemsg.OwedEnded{Of: of, ID: c.ID, Outcome: forgemsg.OwedUndecodable, Words: err.Error()}, data) } + if w := o.waiting(); w != "" { + return forgemsg.OwedEnded{Of: c.Of, ID: c.ID}, fmt.Errorf("the bridge %s", w) + } if o.held(ctx, c.Of, c.ID) { // 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 @@ -145,6 +149,14 @@ func (o *owing) hear(ctx context.Context, of string, m delivered) { _ = m.Ack() } +// waiting is why the state performed cannot be used yet, or empty. +func (o *owing) waiting() string { + if o.waits == nil { + return "" + } + return o.waits() +} + // perform calls the seat git's verb of one act and says what it came to. func (o *owing) perform(ctx context.Context, c bridge.OwedCall, data []byte) (forgemsg.OwedEnded, error) { answer, callErr := o.call(ctx, "seat:"+git.Seat+"."+c.Verb, c.Input) @@ -197,6 +209,9 @@ func (o *owing) held(ctx context.Context, of, id string) bool { // 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 diff --git a/modules/mesh-delivery-forge-bridge/manifest_test.go b/modules/mesh-delivery-forge-bridge/manifest_test.go index 034778b3..cdd979e1 100644 --- a/modules/mesh-delivery-forge-bridge/manifest_test.go +++ b/modules/mesh-delivery-forge-bridge/manifest_test.go @@ -101,8 +101,9 @@ func TestManifestDeclarations(t *testing.T) { // 1235/AC1 } // 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). - if want := []string{"git.forge-delete", "git.pull-closed", "git.pull-merged", "git.pull-updated", "mesh-delivery-checker.owed", "mesh-delivery.owed"}; !reflect.DeepEqual(sorted(m.Consumes), want) { + // 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). + if want := []string{"git.forge-delete", "git.pull-closed", "git.pull-merged", "git.pull-updated", "mesh-broker.dropped", "mesh-broker.recreated", "mesh-delivery-checker.owed", "mesh-delivery.owed"}; !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 @@ -111,7 +112,7 @@ func TestManifestDeclarations(t *testing.T) { // 1235/AC1 t.Errorf("reads = %v, want none", m.Reads) } if want := []string{"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-delivery.groups", "seat:mesh-hand-acts.hand-act"}; !reflect.DeepEqual(sorted(m.Invokes), want) { + "seat:git.pull", "seat:git.status", "seat:git.tags", "seat:git.trees", "seat:git.view", "seat:mesh-broker.show", "seat:mesh-delivery.groups", "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" { diff --git a/modules/mesh-delivery-forge-bridge/module.json b/modules/mesh-delivery-forge-bridge/module.json index 99fcd8f7..a98c4328 100644 --- a/modules/mesh-delivery-forge-bridge/module.json +++ b/modules/mesh-delivery-forge-bridge/module.json @@ -8,7 +8,9 @@ "git.pull-closed", "git.forge-delete", "mesh-delivery.owed", - "mesh-delivery-checker.owed" + "mesh-delivery-checker.owed", + "mesh-broker.recreated", + "mesh-broker.dropped" ], "streams": [ { "name": "forge", "events": ["forge.**", "owed.ended"] } @@ -40,7 +42,8 @@ "seat:git.trees", "seat:git.file", "seat:git.pull", - "seat:git.tags" + "seat:git.tags", + "seat:mesh-broker.show" ], "requires": [ { From dce4d01a584687181851608355b26fa2a2d3fb8a Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 12 Nov 2026 09:15:04 +0100 Subject: [PATCH 2/5] FB1, FB9, FB10: the forge bridge writes one message per move of every watched branch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bridge watches the registrations' branches by origin (a first view of registrations, then registration-changed), before the first registration the gate's source's (source-moved); no branch is named in its code, Trunk goes. Every move is heard on the seat git's branch-moved through the bridge's durable on SEAT_git_branch-moved, its head read by the seat's verb branch, never the event's: merged with its pull request and `base` (the watched branch) when the seat's pull names the head a pull request's merge, else `pushed` with branch, head and `before`, both with the take-in inputs at the head. The last head written per watched branch is kept in the state heads under branch..., so a move heard twice writes once, and each watched branch's head is read at the start and when a branch is registered. A move of an unprotected branch writes nothing. pull.merged into a watched branch writes nothing (its message is the move's); the forge-made merge's trunk filter goes. Builds: design 47 G §4, §7, §11, §20, §41, §507, §509, §532, §714, A-227, A-236 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h --- .../mesh-delivery-forge-bridge-tools/main.go | 2 +- .../main_test.go | 10 +- .../integration_test.go | 66 +++++--- .../cmd/mesh-delivery-forge-bridge/main.go | 139 ++++++++++++----- .../mesh-delivery-forge-bridge/main_test.go | 100 ++++++++---- .../cmd/mesh-delivery-forge-bridge/moves.go | 143 ++++++++++++++++++ .../cmd/mesh-delivery-forge-bridge/open.go | 6 +- .../mesh-delivery-forge-bridge/open_test.go | 2 +- .../forgemsg/forge.go | 65 +++++--- .../forgemsg/forge_test.go | 9 +- modules/mesh-delivery-forge-bridge/go.mod | 2 + .../internal/bridge/bridge.go | 102 ++++++------- .../internal/bridge/bridge_test.go | 87 +++++++---- .../internal/bridge/call.go | 19 ++- .../internal/bridge/files_test.go | 36 ++--- .../internal/bridge/forgemade.go | 13 +- .../internal/bridge/moved.go | 95 ++++++++++++ .../internal/bridge/moved_test.go | 77 ++++++++++ .../internal/bridge/paths_test.go | 2 +- .../internal/bridge/watched.go | 71 +++++++++ .../manifest_test.go | 8 +- .../mesh-delivery-forge-bridge/module.json | 6 + 22 files changed, 819 insertions(+), 241 deletions(-) create mode 100644 modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/moves.go create mode 100644 modules/mesh-delivery-forge-bridge/internal/bridge/moved.go create mode 100644 modules/mesh-delivery-forge-bridge/internal/bridge/moved_test.go create mode 100644 modules/mesh-delivery-forge-bridge/internal/bridge/watched.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 6a381e25..31ab8967 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 @@ -131,7 +131,7 @@ func MergedOf(of []seat.Group, repository, merge string, number int, trees bridg 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, trees, files) + 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) } 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 c39fecb5..83d8a758 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 @@ -135,8 +135,7 @@ func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) { return nil, bridge.ErrNotFound } published := func(repo string, number int, head, branch, merge string) forgemsg.Forge { - body, _ := json.Marshal(git.PullMergedEvent{Repository: "novox/" + repo, Number: int64(number), Branch: branch, Head: head, Base: "main", MergeCommit: merge}) - msg, err := bridge.Convert(bridge.MergedSubject, body, trees, files) + msg, err := bridge.MergedMessage("novox", repo, number, branch, head, merge, "main", trees, files) if err != nil { t.Fatal(err) } @@ -172,8 +171,13 @@ func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) { 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"), fail: "not the trunk"}, + 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, diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go index 34fa9523..6f658a15 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go @@ -97,6 +97,16 @@ accounts { "$JS.API.CONSUMER.MSG.NEXT.SEAT_mesh-delivery-checker_owed.n1_mesh-delivery-forge-bridge" "mesh.seat.mesh-broker.call.show.n1~mesh-delivery-forge-bridge" "mesh.seat.mesh-broker.tool.show" + "mesh.seat.mesh-catalog.call.registrations.n1~mesh-delivery-forge-bridge" + "mesh.seat.mesh-catalog.tool.registrations" + "mesh.seat.mesh-delivery.call.source.n1~mesh-delivery-forge-bridge" + "mesh.seat.mesh-delivery.tool.source" + "mesh.seat.git.call.branch.n1~mesh-delivery-forge-bridge" + "mesh.seat.git.tool.branch" + "$JS.API.STREAM.INFO.SEAT_git_branch-moved" + "$JS.ACK.SEAT_git_branch-moved.n1_mesh-delivery-forge-bridge.>" + "$JS.API.CONSUMER.INFO.SEAT_git_branch-moved.n1_mesh-delivery-forge-bridge" + "$JS.API.CONSUMER.MSG.NEXT.SEAT_git_branch-moved.n1_mesh-delivery-forge-bridge" "mesh.seat.git.call.status.n1~mesh-delivery-forge-bridge" "mesh.seat.git.tool.status" "mesh.seat.git.call.comment.n1~mesh-delivery-forge-bridge" @@ -232,7 +242,7 @@ func startBed(t *testing.T, openReply string, prepare func(ctx context.Context, } // Each seat's own stream owed, as its contract keeps it (Stream, SEAT__owed), as the grants part makes it: no // subjects of its own, sourced from EVENTS filtered by the event's subject. - for name, subjects := range map[string]string{seat.StreamOwed: seat.SubjectOwedAll, checker.StreamOwed: checker.SubjectOwedAll} { + for name, subjects := range map[string]string{seat.StreamOwed: seat.SubjectOwedAll, checker.StreamOwed: checker.SubjectOwedAll, git.StreamBranchMoved: git.SubjectBranchMoved} { if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: name, MaxAge: 7 * 24 * time.Hour, Sources: []*jetstream.StreamSource{{Name: "EVENTS", FilterSubject: subjects}}}); err != nil { t.Fatal(err) @@ -251,11 +261,12 @@ func startBed(t *testing.T, openReply string, prepare func(ctx context.Context, acct := process.Account{Node: "n1", Module: "mesh-delivery-forge-bridge"} if _, err := js.CreateOrUpdateConsumer(ctx, "EVENTS", jetstream.ConsumerConfig{ Durable: acct.EventConsumerName(), AckPolicy: jetstream.AckExplicitPolicy, DeliverPolicy: jetstream.DeliverNewPolicy, - FilterSubjects: []string{git.SubjectPullUpdated, git.SubjectPullMerged, git.SubjectPullClosed, git.SubjectForgeDelete}, + FilterSubjects: []string{git.SubjectPullUpdated, git.SubjectPullMerged, git.SubjectPullClosed, git.SubjectForgeDelete, + "mesh.seat.mesh-catalog.event.registration-changed", "mesh.seat.mesh-delivery.event.source-moved"}, }); err != nil { t.Fatal(err) } - for name, subjects := range map[string]string{seat.StreamOwed: seat.SubjectOwedAll, checker.StreamOwed: checker.SubjectOwedAll} { + for name, subjects := range map[string]string{seat.StreamOwed: seat.SubjectOwedAll, checker.StreamOwed: checker.SubjectOwedAll, git.StreamBranchMoved: git.SubjectBranchMoved} { if _, err := js.CreateOrUpdateConsumer(ctx, name, acct.SeatEventConsumerConfig([]string{subjects}, false)); err != nil { t.Fatal(err) } @@ -266,6 +277,9 @@ func startBed(t *testing.T, openReply string, prepare func(ctx context.Context, answers := map[string]string{ "git.open-pulls": openReply, "git.trees": `{"root":"r0","trees":{"":"t0","modules/a":"t1"}}`, "mesh-delivery.groups": `{"groups":[]}`, "mesh-hand-acts.hand-act": `{"recorded":true}`, + // No registration yet: the watched branches are the gate's source's, main (design 47 G §507). + "mesh-catalog.registrations": `{"registrations":[]}`, "mesh-delivery.source": `{"branches":["main"]}`, + "git.pull": `{"error":"no pull request merged at that commit","error-kind":"not-found"}`, "mesh-broker.show": `{"state":"created","binding":{"local":"performed","storage":"key-value","template":"state","bucket":"` + boundPerformed + `"}}`, } for _, verb := range []string{"status", "comment", "view", "note", "merge-pull"} { @@ -280,6 +294,10 @@ func startBed(t *testing.T, openReply string, prepare func(ctx context.Context, mu.Lock() called = append(called, verb+" "+string(m.Data)) answer, files := answers[verb], filesOf + var in struct{ Repository string } + if json.Unmarshal(m.Data, &in) == nil && answers[verb+" "+in.Repository] != "" { + answer = answers[verb+" "+in.Repository] // a move's branch and pull, set per repository (landedOn) + } mu.Unlock() if verb == "git.file" { var in git.FileInput @@ -326,6 +344,23 @@ func startBed(t *testing.T, openReply string, prepare func(ctx context.Context, }} } +// landedOn sets the stand-in seat git so main of repository is protected at at, which the pull request number on branch +// at prHead merged. +func landedOn(b *bed, repository string, number int, branch, prHead, at string) { + b.answer("git.branch "+repository, `{"repository":"`+repository+`","branch":"main","head":"`+at+`","protected":true}`) + b.answer("git.pull "+repository, fmt.Sprintf(`{"pull":{"repository":%q,"number":%d,"branch":%q,"head":%q,"base":"main","state":"merged","merge-commit":%q}}`, + repository, number, branch, prHead, at)) +} + +// moveMain says main of repository moved, as the seat git says it, naming a head the bridge never takes (§11). +func moveMain(t *testing.T, b *bed, repository string) { + t.Helper() + ev, _ := json.Marshal(git.BranchMovedEvent{Repository: repository, Branch: "main", Head: strings.Repeat("f", 40)}) + if _, err := b.js.Publish(b.ctx, git.SubjectBranchMoved, ev); err != nil { + t.Fatal(err) + } +} + // TestBridgeWritesPullMessages starts the program the way the node does (its entry, run, with MESH_BROKER_FILE). func TestBridgeWritesPullMessages(t *testing.T) { // 1235/AC2 ctl, js, ctx, _ := startBridge(t, `{"pulls":[]}`) @@ -452,12 +487,9 @@ func TestBridgeWritesMergedMessage(t *testing.T) { // 1236/AC4, 1448/AC5 } return "", false }) - // pull.merged as the seat git says it (its contract's PullMergedEvent). - ev, _ := json.Marshal(git.PullMergedEvent{Repository: "novox/mesh-delivery", Number: 12, Title: "t", Head: head, Branch: "feat/x", Base: "main", - MergeCommit: merge, Change: &git.Change{Paths: []string{"modules/a/x.go"}, ModuleDirs: []string{"modules/a"}}}) - if _, err := js.Publish(ctx, git.SubjectPullMerged, ev); err != nil { - t.Fatal(err) - } + // design 47 G §11, §509, §714: main moved, read by the seat's branch, and the forge names #12 merged at its head. + landedOn(bed, "novox/mesh-delivery", 12, "feat/x", head, merge) + moveMain(t, bed, "novox/mesh-delivery") forge, err := js.Stream(ctx, "FORGE") if err != nil { @@ -482,7 +514,7 @@ func TestBridgeWritesMergedMessage(t *testing.T) { // 1236/AC4, 1448/AC5 t.Fatal(err) } want := forgemsg.Forge{V: keys.Version, Kind: "merged", Repository: "novox/mesh-delivery", Branch: "feat/x", Number: 12, Head: head, - Merge: merge, Root: "r0", Trees: map[string]string{"": "t0", "modules/a": "t1"}, + Merge: merge, Base: "main", Root: "r0", Trees: map[string]string{"": "t0", "modules/a": "t1"}, Manifests: map[string]string{"": string(manifest), "modules/a": string(manifest)}, // No mesh package: only the indexer computes what a fingerprint takes (ADR 0593 §2). Locks: map[string]map[string]string{"modules/a": {"go.sum": hex.EncodeToString(sha256Sum(sum))}}} @@ -499,7 +531,7 @@ func TestBridgeWritesMergedMessage(t *testing.T) { // 1236/AC4, 1448/AC5 if len(trees) != 1 || trees[0] != "git.trees "+string(in) { t.Errorf("the seat git was asked for trees %v, want once at the merge commit", trees) } - cons, err := js.Consumer(ctx, "EVENTS", "n1_mesh-delivery-forge-bridge") + cons, err := js.Consumer(ctx, git.StreamBranchMoved, "n1_mesh-delivery-forge-bridge") if err != nil { t.Fatal(err) } @@ -725,14 +757,10 @@ func TestBridgeSaysAGroupFromTheSeatsGroups(t *testing.T) { // 1585, ADR 0633 § {Repository: "novox/mesh-delivery", Number: 12, Head: head, State: "merged"}, {Repository: "novox/mesh-sdk", Number: 4, Head: headB, State: "merging"}}}}}) b.answer("mesh-delivery.groups", string(groups)) ch := heard(t, b) - for _, m := range []git.PullMergedEvent{ - {Repository: "novox/mesh-delivery", Number: 12, Head: head, Branch: "feat/x", Base: "main", MergeCommit: merge, Change: &git.Change{Paths: []string{"modules/a/x.go"}, ModuleDirs: []string{"modules/a"}}}, - {Repository: "novox/mesh-sdk", Number: 4, Head: headB, Branch: "feat/x", Base: "main", MergeCommit: mergeB, Change: &git.Change{Paths: []string{"go/x.go"}}}, - } { - ev, _ := json.Marshal(m) - if _, err := b.js.Publish(b.ctx, git.SubjectPullMerged, ev); err != nil { - t.Fatal(err) - } + landedOn(b, "novox/mesh-delivery", 12, "feat/x", head, merge) + landedOn(b, "novox/mesh-sdk", 4, "feat/x", headB, mergeB) + for _, m := range []git.PullMergedEvent{{Repository: "novox/mesh-delivery"}, {Repository: "novox/mesh-sdk"}} { + moveMain(t, b, m.Repository) repo := strings.TrimPrefix(m.Repository, "novox/") f, err := forgemsg.DecodeForge(next(t, ch, "mesh.stream.mesh-delivery-forge-bridge.event.forge.novox."+repo+".merged")) if err != nil || f.Group != "feat/x" || len(f.Members) != 2 || f.Members[1].Number != 4 { 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 91ff4208..0adb3881 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 @@ -6,6 +6,7 @@ package main import ( "context" + "encoding/json" "errors" "fmt" "io" @@ -17,14 +18,17 @@ import ( "time" "git.novox.be/novox/mesh-sdk/go/process" + "git.novox.be/novox/mesh-sdk/go/views" "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" + "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-forge-bridge/forgemsg" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/sourcebranches" "mesh-delivery-forge-bridge/internal/bridge" groupkeys "mesh-delivery-forge-bridge/internal/groups" @@ -68,17 +72,11 @@ type publisher interface { Publish(ctx context.Context, subject string, payload []byte, opts ...jetstream.PublishOpt) (*jetstream.PubAck, error) } -// handle turns one event into its message on FORGE and acknowledges it. 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, trees bridge.Trees, files bridge.Files, kept bridge.Kept, logger *log.Logger) { - handleGrouped(ctx, m, pub, trees, files, kept, nil, logger) -} - -// handleGrouped is handle; a merged message of a delivery group's member names its group (g, ADR 0633 §4; nil names -// none), and the member's merged message written may end the group. -func handleGrouped(ctx context.Context, m delivered, pub publisher, trees bridge.Trees, files bridge.Files, kept bridge.Kept, g *grouper, logger *log.Logger) { - msg, err := bridge.ConvertKept(m.Subject(), m.Data(), trees, files, kept) +// 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) @@ -98,18 +96,10 @@ func handleGrouped(ctx context.Context, m delivered, pub publisher, trees bridge if id := m.Headers().Get("x-event-id"); id != "" { opts = append(opts, jetstream.WithMsgID(id)) } - var member forgemsg.Forge - grouped := false - if g != nil { - msg.Data, member, grouped = g.member(ctx, msg.Data) - } 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 grouped { - g.published(ctx, member) - } if msg.Said != "" { logger.Printf("%s: %s", m.Subject(), msg.Said) } @@ -147,7 +137,11 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro 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} - said := func(ctx context.Context) error { return syncOpen(ctx, call, rec, heads, &turn, 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 @@ -176,9 +170,91 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro } }() } + 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 + 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 + regs, _, err = meshcatalogclient.WireRegistrationsProcess(reader, keptCall, func(mcseat.Registration, bool) { + recompute() + select { + case <-started: // a branch registered while the bridge runs is adopted: its head read (§532) + go mv.readHeads(ctx) + default: + } + }) + if err != nil { + return err + } + src.Follower.Changed = func(string, json.RawMessage, bool, time.Time) { recompute() } + if _, err := reader.Adopt(src.Follower, sourcebranches.Seat, sourcebranches.EventSourceMoved, src.Hear); err != nil { + 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, "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, 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) + } + 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 + // 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 { + case <-started: + case <-ctx.Done(): + return + } + hctx, cancel := context.WithTimeout(ctx, syncDeadline) + defer cancel() + mv.hear(hctx, 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) + } + defer moves.Stop() + select { + case <-regs.Ready(): + case <-ctx.Done(): + return nil + } + if len(regs.All()) == 0 { + select { + case <-src.Follower.Ready(): + case <-ctx.Done(): + return nil + } + } + 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. @@ -217,25 +293,8 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro 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) - // 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, "a merge's module directories are kept in memory only, so the first merge after a start reads every one", logger.Printf), 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) { - 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() - handleGrouped(hctx, m, rec, treesVia(hctx, call), filesVia(hctx, call), inputs, groups, 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) - } - defer hub.Stop() + // Each watched branch's head, read at the start (§11): a head missed 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 da888759..b4874634 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 @@ -79,7 +79,7 @@ func TestReadableEventIsWrittenOnForgeAndAcked(t *testing.T) { // 1235/AC2 (unit }) var logs bytes.Buffer m, pub := &fakeMsg{subject: git.SubjectPullUpdated, data: body(nil)}, &fakePub{} - handle(context.Background(), m, pub, nil, someFiles, nil, log.New(&logs, "", 0)) + 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) } @@ -95,7 +95,7 @@ func TestReadableEventIsWrittenOnForgeAndAcked(t *testing.T) { // 1235/AC2 (unit 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, nil, someFiles, nil, log.New(&logs, "", 0)) + 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) } @@ -109,7 +109,7 @@ func TestUnreadableEventIsTerminatedAndLogged(t *testing.T) { // 1235/AC5 // 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, nil, someFiles, nil, log.New(&logs, "", 0)) + 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) } @@ -118,55 +118,93 @@ func TestUnreadableEventIsTerminatedAndLogged(t *testing.T) { // 1235/AC5 } } -// mergedEvent is the seat git's pull.merged. -func mergedEvent() []byte { - b, _ := json.Marshal(map[string]any{"repository": "novox/mesh-delivery", "number": 12, "branch": "feat/x", "head": head, - "base": "main", "merge-commit": "89abcdef0123456789abcdef0123456789abcdef"}) +// watchesMain watches main of every repository, as the gate's source names it before the first registration. +func watchesMain() *bridge.Watched { + w := &bridge.Watched{} + w.Set(nil, []string{"main"}) + return w +} + +// movedEvent is the seat git's branch-moved of main 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" + +// 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 + } + } + return nil, err + } +} + +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 - m, pub := &fakeMsg{subject: git.SubjectPullMerged, data: mergedEvent()}, &fakePub{} - call := func(context.Context, string, any) (json.RawMessage, error) { - return nil, errors.New("the tree listing of novox/mesh-delivery was cut") - } - handle(context.Background(), m, pub, treesVia(context.Background(), call), filesVia(context.Background(), call), nil, log.New(&logs, "", 0)) - if len(pub.subjects) != 0 { + 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", "89abcdef0123456789abcdef0123456789abcdef", "the tree listing of novox/mesh-delivery was cut"} { + 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) } } } -func TestMergeCallsTheToolOnceAndWritesTheMergedMessage(t *testing.T) { // 1236/AC1; design 47 *The seat git* (verbs trees, file) +// 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) { var logs bytes.Buffer - m, pub := &fakeMsg{subject: git.SubjectPullMerged, data: mergedEvent()}, &fakePub{} 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") - call := func(_ context.Context, key string, body any) (json.RawMessage, error) { - mu.Lock() - keys, args = append(keys, key), append(args, body) - mu.Unlock() + 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 trees, nil + return call(ctx, key, body) } - handle(context.Background(), m, pub, treesVia(context.Background(), call), filesVia(context.Background(), call), nil, log.New(&logs, "", 0)) - if len(keys) == 0 || keys[0] != "seat:git.trees" || slices.Index(keys[1:], "seat:git.trees") >= 0 { - t.Fatalf("calls %v, want the trees once and first", keys) + 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[0]); !sameJSON(got, []byte(`{"repository":"novox/mesh-delivery","commit":"89abcdef0123456789abcdef0123456789abcdef"}`)) { + 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" { @@ -174,11 +212,17 @@ func TestMergeCallsTheToolOnceAndWritesTheMergedMessage(t *testing.T) { // 1236/ } 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 == "" { + 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 { - t.Errorf("acked %d", m.acked) + 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) } } 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 new file mode 100644 index 00000000..40b6c060 --- /dev/null +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/moves.go @@ -0,0 +1,143 @@ +package main + +import ( + "context" + "errors" + "fmt" + "log" + "strings" + "sync" + + "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/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. +type mover struct { + call caller + pub publisher + heads headStore + watched *bridge.Watched + inputs bridge.Kept + groups *grouper + 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. +func (mv *mover) hear(ctx context.Context, m delivered) { + moved, err := bridge.MovedOf(m.Data()) + if err != nil { + mv.logger.Printf("%v; the event is dropped", err) + _ = m.Term() + return + } + if !mv.watched.Watches(moved.Repository(), moved.Branch) { + _ = m.Ack() + return + } + mv.turn.Lock() + err = mv.move(ctx, moved) + 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) + 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) + if err != nil { + return fmt.Errorf("%s of %s: %w", moved.Branch, moved.Repository(), err) + } + 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) + } + if before == b.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) + 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) + } + // 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) + } + 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) + } + return nil +} + +// readHeads reads each watched branch's head (§11): a head missed while the bridge was stopped is written now. +func (mv *mover) readHeads(ctx context.Context) { + for _, repo := range mv.watched.Repositories() { + owner, name, ok := strings.Cut(repo, "/") + if !ok { + continue + } + for _, branch := range mv.watched.Branches(repo) { + mv.turn.Lock() + err := mv.move(ctx, bridge.Moved{Owner: owner, Repo: name, Branch: branch}) + 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) + } + } + } +} + +// registeredOf is the registrations as the watched branches read them. +func registeredOf(regs []mcseat.Registration) []bridge.Registered { + out := make([]bridge.Registered, 0, len(regs)) + for _, r := range regs { + out = append(out, bridge.Registered{Origin: r.Origin, Branches: r.Branches}) + } + return out +} + +// unescaped is the gate's source's branches by name (they are kept by their escaped name). +func unescaped(branches []string) []string { + out := make([]string, 0, len(branches)) + for _, b := range branches { + if name, err := keys.UnescapeName(b); err == nil { + out = append(out, name) + } + } + return out +} 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 index 9441a818..826d5295 100644 --- 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 @@ -80,7 +80,7 @@ func hear(mu *sync.Mutex, react func()) { // 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, mu *sync.Mutex, logger *log.Logger) error { +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) @@ -89,11 +89,11 @@ func syncOpen(ctx context.Context, call caller, pub publisher, heads headStore, if err != nil { return fmt.Errorf("%s: %w", openPullsTool, err) } - msgs, skipped, err := bridge.OpenPulls(raw) + msgs, skipped, err := bridge.OpenPulls(raw, watched) if err != nil { return fmt.Errorf("%s: %w", openPullsTool, err) } - open, err := bridge.OpenMessage(raw) + open, err := bridge.OpenMessage(raw, watched) if err != nil { return fmt.Errorf("%s: %w", openPullsTool, 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 index 5861b9b7..a10a62f3 100644 --- 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 @@ -90,7 +90,7 @@ func (m *memHeads) get(key string) string { m.mu.Lock(); defer m.mu.Unlock(); re // 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, mu, logger) + 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 diff --git a/modules/mesh-delivery-forge-bridge/forgemsg/forge.go b/modules/mesh-delivery-forge-bridge/forgemsg/forge.go index ccba891b..027a891c 100644 --- a/modules/mesh-delivery-forge-bridge/forgemsg/forge.go +++ b/modules/mesh-delivery-forge-bridge/forgemsg/forge.go @@ -24,8 +24,11 @@ const ForgeStream = "forge" // The kinds of forge message, the last part of a forge event's name. const ( - ForgePull = "pull" - ForgeMerged = "merged" + ForgePull = "pull" + ForgeMerged = "merged" + // ForgePushed is a move of a watched branch the forge names no pull request's merge for (design 47 G §509): an + // admitted push, taken in as a merged message is. + ForgePushed = "pushed" ForgeRefused = "refused" ForgeOpen = "open" ) @@ -67,10 +70,10 @@ func checkRepository(repository string) error { func checkForgeKind(kind string) error { switch kind { - case ForgePull, ForgeMerged, ForgeRefused, ForgeOpen: + case ForgePull, ForgeMerged, ForgePushed, ForgeRefused, ForgeOpen: return nil } - return fmt.Errorf("delivery: the forge kind %q is not pull, merged, refused or open", kind) + return fmt.Errorf("delivery: the forge kind %q is not pull, merged, pushed, refused or open", kind) } // ForgeEvent is the name of a forge message of the given kind: `forge...`. @@ -111,18 +114,24 @@ func ParseForgeEvent(event string) (repository, kind string, err error) { // DirsKnown, true when the forge could say them: known and empty is DirsKnown with no Dirs, not known is neither // (the forge's module_dirs and module_dirs_said, mesh-catalog modules/gitea/trunk.ts). Other messages carry neither. type Forge struct { - V int `json:"v"` - Kind string `json:"kind"` - Repository string `json:"repository"` - Branch string `json:"branch,omitempty"` - Number int `json:"number,omitempty"` - Head string `json:"head,omitempty"` - Action string `json:"action,omitempty"` - Dirs []string `json:"dirs,omitempty"` - DirsKnown bool `json:"dirs-known,omitempty"` - Merge string `json:"merge,omitempty"` - Trees map[string]string `json:"trees,omitempty"` - Root string `json:"root,omitempty"` + V int `json:"v"` + Kind string `json:"kind"` + Repository string `json:"repository"` + // Branch is a pull or merged message's pull request branch, and a pushed message's branch pushed. + Branch string `json:"branch,omitempty"` + // Base is the watched branch a merged message's commit landed on (design 47 G §714). + Base string `json:"base,omitempty"` + Number int `json:"number,omitempty"` + // Head is a pull or merged message's pull request head commit, and a pushed message's new head of its branch (A-236). + Head string `json:"head,omitempty"` + // Before is a pushed message's head of its branch before, as the forge bridge last wrote it (A-236), empty for none. + Before string `json:"before,omitempty"` + Action string `json:"action,omitempty"` + Dirs []string `json:"dirs,omitempty"` + DirsKnown bool `json:"dirs-known,omitempty"` + Merge string `json:"merge,omitempty"` + Trees map[string]string `json:"trees,omitempty"` + Root string `json:"root,omitempty"` // Manifests are the module.json bytes, exact, of each module directory: of the merge's trees on a merged message, // of Dirs at the head on a pull message. Locks are, of each such directory, its lock files' hex sha256 by path // relative to it (novox/hq ADR 0465 clause 1). Older writers send neither. @@ -203,8 +212,8 @@ func (f Forge) checkPredefined() error { if len(f.Predefined) == 0 && !f.PredefinedRead { return nil } - if f.Kind != ForgeMerged { - return fmt.Errorf("delivery: only a merged message carries the predefined list, not a %s message", f.Kind) + if f.Kind != ForgeMerged && f.Kind != ForgePushed { + return fmt.Errorf("delivery: only a merged or pushed message carries the predefined list, not a %s message", f.Kind) } if len(f.Predefined) > 0 && f.PredefinedRead { return fmt.Errorf("delivery: a merged message carries the predefined list and says the merge holds none (predefined-read)") @@ -262,21 +271,27 @@ func (f Forge) check() error { if err := f.checkInputs(func(d string) bool { return dirs[d] }, "its module directories"); err != nil { return err } - case ForgeMerged: + case ForgeMerged, ForgePushed: if len(f.Dirs) > 0 || f.DirsKnown { - return fmt.Errorf("delivery: a merged message carries no module directories") + return fmt.Errorf("delivery: a %s message carries no module directories", f.Kind) } - if f.Number <= 0 { + if f.Kind == ForgeMerged && f.Number <= 0 { return fmt.Errorf("delivery: a merged message needs its pull request number") } - if f.Head == "" { - return fmt.Errorf("delivery: a merged message needs its head") + if f.Kind == ForgePushed && (f.Number != 0 || f.Merge != "" || f.Base != "") { + return fmt.Errorf("delivery: a pushed message names no pull request, merge commit or base: its branch is the one pushed") } - if f.Merge == "" { + if f.Kind == ForgePushed && f.Branch == "" { + return fmt.Errorf("delivery: a pushed message needs its branch") + } + if f.Head == "" { + return fmt.Errorf("delivery: a %s message needs its head", f.Kind) + } + if f.Kind == ForgeMerged && f.Merge == "" { return fmt.Errorf("delivery: a merged message needs its merge commit") } if f.Root == "" { - return fmt.Errorf("delivery: a merged message needs the repository's root tree") + return fmt.Errorf("delivery: a %s message needs the repository's root tree", f.Kind) } // No trees is a merge that leaves the repository with no module.json anywhere (a documents repository, or one // whose last module was removed): it is written all the same, so the indexer takes the merge in and drops the diff --git a/modules/mesh-delivery-forge-bridge/forgemsg/forge_test.go b/modules/mesh-delivery-forge-bridge/forgemsg/forge_test.go index 41fde65b..c31695e5 100644 --- a/modules/mesh-delivery-forge-bridge/forgemsg/forge_test.go +++ b/modules/mesh-delivery-forge-bridge/forgemsg/forge_test.go @@ -23,6 +23,10 @@ func sampleForges() []Forge { // 1585/T1: a group member's merged message names its group and every approved member (ADR 0633 §4). {Kind: ForgeMerged, Repository: "novox/mesh-sdk", Number: 37, Head: "dddd", Merge: "eeee", Root: "t0", Group: "feat/take-in", Members: []meshdeliveryclient.Head{{Repository: "novox/mesh-sdk", Number: 37, Head: "dddd"}, {Repository: "novox/mesh-delivery", Number: 9, Head: "ffff"}}}, + // design 47 G §509, §714, A-236: a merged message names the watched branch it landed on; a pushed one its branch, new + // head and head before. + {Kind: ForgeMerged, Repository: "novox/mesh-sdk", Number: 38, Branch: "feat/y", Head: "aaaa", Merge: "bbbb", Base: "beta", Root: "t0"}, + {Kind: ForgePushed, Repository: "novox/mesh-sdk", Branch: "beta", Head: "cccc", Before: "bbbb", Root: "t0", Trees: map[string]string{"modules/a": "t1"}}, } } @@ -153,7 +157,7 @@ func TestForge(t *testing.T) { {"assignments read", "mesh-module-assigner", in(func(f *Forge) { f.RetiredRead = true })}, {"predefined not an object", "one JSON object", in(func(f *Forge) { f.Predefined = []byte(`["domain"]`) })}, {"predefined both", "says the merge holds none", in(func(f *Forge) { f.Predefined = []byte(`{}`); f.PredefinedRead = true })}, - {"pull with the predefined list", "only a merged message", Forge{Kind: ForgePull, Repository: "novox/mesh-sdk", Number: 1, Head: "h", Action: PullOpened, PredefinedRead: true}}, + {"pull with the predefined list", "only a merged or pushed message", Forge{Kind: ForgePull, Repository: "novox/mesh-sdk", Number: 1, Head: "h", Action: PullOpened, PredefinedRead: true}}, {"pull manifest outside dirs", `"b"`, pullIn(func(f *Forge) { f.Manifests["b"] = "{}" })}, {"pull locks outside dirs", `"c"`, pullIn(func(f *Forge) { f.Locks["c"] = map[string]string{"go.sum": "h"} })}, {"refused with manifests", "no manifests", Forge{Kind: ForgeRefused, Repository: "a/b", Act: "x", Words: "no", Manifests: map[string]string{"a": "{}"}}}, @@ -167,6 +171,9 @@ func TestForge(t *testing.T) { {"merged with members leaving itself out", "leave out", at(4, func(f *Forge) { f.Members = f.Members[1:] })}, {"merged with a member twice", "twice", at(4, func(f *Forge) { f.Members = append(f.Members, f.Members[0]) })}, {"open with a group", "only the open pull requests", Forge{Kind: ForgeOpen, Group: "g"}}, + // design 47 G §509, A-236: a pushed message names its branch and no pull request, merge commit or base. + {"pushed with a pull request", "names no pull request", Forge{Kind: ForgePushed, Repository: "a/b", Branch: "beta", Head: "h", Root: "r", Number: 3}}, + {"pushed with no branch", "needs its branch", Forge{Kind: ForgePushed, Repository: "a/b", Head: "h", Root: "r"}}, } { if _, err := EncodeForge(c.f); err == nil || !strings.Contains(err.Error(), c.want) { t.Errorf("%s: %v, want a refusal naming %q", c.name, err, c.want) diff --git a/modules/mesh-delivery-forge-bridge/go.mod b/modules/mesh-delivery-forge-bridge/go.mod index e8f3740d..ea862348 100644 --- a/modules/mesh-delivery-forge-bridge/go.mod +++ b/modules/mesh-delivery-forge-bridge/go.mod @@ -4,7 +4,9 @@ go 1.26.0 require ( git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient v0.0.0-00010101000000-000000000000 + git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient v0.0.0-00010101000000-000000000000 + git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery v0.0.0-00010101000000-000000000000 diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/bridge.go b/modules/mesh-delivery-forge-bridge/internal/bridge/bridge.go index d27bb022..21648ba3 100644 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/bridge.go +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/bridge.go @@ -32,8 +32,10 @@ var ErrNotHandled = errors.New("event not handled") // heard again later. var ErrRetry = errors.New("the forge could not say") -// Trunk is the branch a merged message is written for. -const Trunk = "main" +// 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). @@ -93,7 +95,7 @@ type BranchDeleted struct { } // branchDeleted turns forge.delete into a branch-deleted message; a deleted tag is not handled. -func branchDeleted(subject string, data []byte) (Message, error) { +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) @@ -109,8 +111,8 @@ func branchDeleted(subject string, data []byte) (Message, error) { 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 branch == Trunk: - return Message{}, fmt.Errorf("%w: %s of the trunk %s", ErrNotHandled, subject, Trunk) + 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 { @@ -170,29 +172,20 @@ func unreadable(subject, why string, args ...any) error { return fmt.Errorf("%w: %s: %s", ErrUnreadable, subject, fmt.Sprintf(why, args...)) } -// Convert turns one event of the forge module, 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 merge into -// the trunk asks trees once and becomes a merged message; a merge into any other branch is a closed pull message, as the -// pull request left its group. A forge that cannot say is ErrRetry. A merge that leaves no module.json anywhere is a merged message with no -// trees all the same (issue 1126): the indexer takes it in and drops the repository's modules, and the view reads the -// repository as reached at it. -func Convert(subject string, data []byte, trees Trees, files Files) (Message, error) { - return ConvertKept(subject, data, trees, files, nil) -} - -// ConvertKept is Convert; with kept, a merge reads the forge only for the module directories whose tree hash kept -// does not hold, and keeps what it read (kept.go). -func ConvertKept(subject string, data []byte, trees Trees, files Files, kept Kept) (Message, error) { +// 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: + case ClosedSubject, MergedSubject: action = forgemsg.PullClosed - case MergedSubject: - action = forgemsg.PullClosed // unless it merged into the trunk, below case DeletedSubject: - return branchDeleted(subject, data) + return branchDeleted(subject, data, watched) default: return Message{}, fmt.Errorf("%w: %s", ErrNotHandled, subject) } @@ -210,17 +203,17 @@ func ConvertKept(subject string, data []byte, trees Trees, files Files, kept Kep case p.Head == "": return Message{}, unreadable(subject, "no branch") } - if subject == MergedSubject && p.Base == Trunk { - return merged(subject, p, trees, files, kept) + 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 != "" && p.Base != Trunk { - // The checker judges pull requests into main only, and source.main names main's head only (novox/hq ADR 0484 §11): a - // pull request into another base 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). + 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, not %s: written closed, so it is no candidate; the checker judges pull requests into %s only (novox/hq ADR 0484 §11)", - p.Owner, p.Repo, p.Number, p.HeadSHA[:12], p.Base, Trunk, Trunk) + 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} @@ -419,15 +412,17 @@ func fetch(owner, repo, ref string, reads []*read, files Files) { wg.Wait() } -// merged is the merged message of a merge into the trunk: the trees of the merge commit, asked once. -func merged(subject string, p pull, trees Trees, files Files, kept Kept) (Message, error) { - if !sha40.MatchString(p.MergeSHA) { - return Message{}, unreadable(subject, "the merge commit %q is not 40 hex digits", p.MergeSHA) +// 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 := p.Owner + "/" + p.Repo - root, dirs, err := trees(p.Owner, p.Repo, p.MergeSHA) + 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 the merge commit %s: %v", ErrRetry, subject, repository, p.MergeSHA, err) + 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 { @@ -435,10 +430,10 @@ func merged(subject string, p pull, trees Trees, files Files, kept Kept) (Messag } slices.Sort(names) manifests, locks, needs, toRead := keptInputs(names, dirs, kept) - read, readLocks, readNeeds, whole, err := inputsRead(p.Owner, p.Repo, p.MergeSHA, toRead, files) + 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 the merge commit %s (%d of %d module directories read from the forge, the others kept by their tree): %v", ErrRetry, subject, repository, p.MergeSHA, len(toRead), len(names), err) + 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) @@ -453,31 +448,30 @@ func merged(subject string, p pull, trees Trees, files Files, kept Kept) (Messag if len(needs) == 0 { needs = nil } - event, err := forgemsg.ForgeEvent(repository, forgemsg.ForgeMerged) + event, err := forgemsg.ForgeEvent(repository, f.Kind) if err != nil { - return Message{}, unreadable(subject, "%v", err) + return Message{}, unreadable(what, "%v", err) } - f := forgemsg.Forge{Kind: forgemsg.ForgeMerged, Repository: repository, Branch: p.Head, Number: p.Number, - Head: p.HeadSHA, Merge: p.MergeSHA, Root: root, Trees: dirs, Manifests: manifests, Locks: locks, Needs: needs} + 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 merge (novox/hq ADR 0565 §2): the indexer keeps it for mesh-config. - raw, err := files(p.Owner, p.Repo, PredefinedFile, p.MergeSHA) + // 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 the merge commit %s: %v", ErrRetry, subject, PredefinedFile, repository, p.MergeSHA, err) + 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(subject, "%v", err) + return Message{}, unreadable(what, "%v", err) } msg := Message{Event: event, Data: body} if len(dirs) == 0 { - msg.Said = fmt.Sprintf("%s at the merge commit %s holds no module.json: a merged message with no module directories", repository, p.MergeSHA) + 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 } @@ -506,16 +500,16 @@ func openPulls(answer []byte) ([]pull, error) { } // OpenPulls turns the seat git's answer of open-pulls into one pull message per open pull request into -// the trunk, action updated and its directories not known (the forge's answer does not say them). A 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) (msgs []Message, skipped []string, err 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 p.Base != Trunk { + if !watched.Watches(p.Owner+"/"+p.Repo, p.Base) { continue } name := fmt.Sprintf("%s/%s#%d", p.Owner, p.Repo, p.Number) @@ -553,17 +547,17 @@ func Track(f forgemsg.Forge) (key, head string, ok bool) { return "", "", false } -// OpenMessage turns the seat git's answer of open-pulls into the open message: every open pull request into the trunk, by +// 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) (Message, error) { +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 p.Base != Trunk || p.Owner == "" || p.Repo == "" || p.Number <= 0 { + 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}) 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 5b6b2efb..dc0aaa35 100644 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/bridge_test.go +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/bridge_test.go @@ -68,7 +68,7 @@ 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), nil, anyFiles) + msg, err := Convert(updatedSubject, updated(nil), anyFiles, watchesMain) if err != nil { t.Fatal(err) } @@ -86,7 +86,7 @@ func TestUpdatedBecomesAPullMessage(t *testing.T) { // 1235/AC2 (the conversion; } // 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}), nil, anyFiles) + msg, err = Convert(updatedSubject, updated(map[string]any{"module_dirs": nil, "module_dirs_said": nil}), anyFiles, watchesMain) if err != nil { t.Fatal(err) } @@ -95,7 +95,7 @@ func TestUpdatedBecomesAPullMessage(t *testing.T) { // 1235/AC2 (the conversion; 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}), nil, anyFiles) + msg, err = Convert(updatedSubject, updated(map[string]any{"module_dirs": []string{}, "module_dirs_said": true}), anyFiles, watchesMain) if err != nil { t.Fatal(err) } @@ -104,7 +104,7 @@ func TestUpdatedBecomesAPullMessage(t *testing.T) { // 1235/AC2 (the conversion; 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}), nil, anyFiles) + msg, err = Convert(updatedSubject, updated(map[string]any{"module_dirs_said": nil}), anyFiles, watchesMain) if err != nil { t.Fatal(err) } @@ -114,7 +114,7 @@ func TestUpdatedBecomesAPullMessage(t *testing.T) { // 1235/AC2 (the conversion; } func TestClosedBecomesAClosedPullMessage(t *testing.T) { // 1235/AC4 - msg, err := Convert(closedSubject, closed(), nil, anyFiles) + msg, err := Convert(closedSubject, closed(), anyFiles, watchesMain) if err != nil { t.Fatal(err) } @@ -130,17 +130,17 @@ func TestClosedBecomesAClosedPullMessage(t *testing.T) { // 1235/AC4 if !forgemsg.SameForge(f, want) { t.Errorf("message = %+v, want %+v", f, want) } - // Issue 1422, ADR 0484 §11: a head pushed to a pull request into another base is written closed, read nothing for, and said; - // one into main 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, nil) + // 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, not main") { + 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}), nil, anyFiles) + 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) } @@ -165,7 +165,7 @@ func TestUnreadableEventsWriteNothing(t *testing.T) { // 1235/AC5 "closed no head": {closedSubject, mustDrop(closed(), "head"), "head"}, "closed no repo": {closedSubject, mustDrop(closed(), "repository"), "repo"}, } { - msg, err := Convert(c.subject, c.data, nil, anyFiles) + msg, err := Convert(c.subject, c.data, anyFiles, watchesMain) if err == nil { t.Errorf("%s: a message %q was made", name, msg.Event) continue @@ -192,7 +192,7 @@ func mustDrop(body []byte, key string) []byte { 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), nil, anyFiles); !errors.Is(err, ErrNotHandled) { + if _, err := Convert(s, updated(nil), anyFiles, watchesMain); !errors.Is(err, ErrNotHandled) { t.Errorf("%s: err = %v, want ErrNotHandled", s, err) } } @@ -202,23 +202,23 @@ func TestOtherSubjectsAreNotHandled(t *testing.T) { // 1235, 1236 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"), nil, anyFiles) + 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), nil, anyFiles); !errors.Is(err, ErrNotHandled) { + 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"}`), nil, anyFiles); !errors.Is(err, ErrUnreadable) { + 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 = Convert(mergedSubject, mergedBody(map[string]any{"merge_commit_sha": "abc"}), treesOf(&calls, "r0", map[string]string{"a": "t"}, nil), anyFiles) + _, 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) } @@ -247,7 +247,7 @@ func treesOf(calls *[]call, root string, trees map[string]string, err error) Tre func TestMergeIntoMainBecomesAMergedMessage(t *testing.T) { // 1236/AC1 var calls []call answer := map[string]string{"modules/a": "t1", "modules/b": "t2"} - msg, err := Convert(mergedSubject, mergedBody(nil), treesOf(&calls, "r0", answer, nil), anyFiles) + msg, err := convertMerged(mergedBody(nil), treesOf(&calls, "r0", answer, nil), anyFiles, nil) if err != nil { t.Fatal(err) } @@ -259,7 +259,7 @@ func TestMergeIntoMainBecomesAMergedMessage(t *testing.T) { // 1236/AC1 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, Root: "r0", Trees: answer, Manifests: map[string]string{"modules/a": "{}", "modules/b": "{}"}} + 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) } @@ -268,9 +268,14 @@ func TestMergeIntoMainBecomesAMergedMessage(t *testing.T) { // 1236/AC1 } } -func TestMergeIntoAnotherBranchIsAClosedPull(t *testing.T) { // 1236/AC2 +// 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 - msg, err := Convert(mergedSubject, mergedBody(map[string]any{"base": "feat/y"}), treesOf(&calls, "r0", map[string]string{"a": "t"}, nil), anyFiles) + 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) } @@ -290,7 +295,7 @@ func TestMergeIntoAnotherBranchIsAClosedPull(t *testing.T) { // 1236/AC2 func TestTreesRefusedWritesNothingAndSaysWhy(t *testing.T) { // 1236/AC3 (the conversion; the delay is in main_test) var calls []call - msg, err := Convert(mergedSubject, mergedBody(nil), treesOf(&calls, "", nil, errors.New("the listing for novox/mesh-delivery was cut")), anyFiles) + 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) } @@ -305,13 +310,13 @@ func TestTreesRefusedWritesNothingAndSaysWhy(t *testing.T) { // 1236/AC3 (the co // 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 := Convert(mergedSubject, mergedBody(nil), treesOf(&calls, "r0", map[string]string{}, nil), anyFiles) + 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, Root: "r0"} + 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) } @@ -330,7 +335,7 @@ func openEntry(repo string, number int, base, branch, sha string) 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))) + 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) } @@ -353,15 +358,15 @@ func TestOpenPullsAreUpdatedPullMessagesWithUnknownDirs(t *testing.T) { // 1237/ 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))); err != nil || len(msgs) != 0 || len(skipped) != 0 { + 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))) + 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)); err == nil { + if _, _, err := OpenPulls([]byte(bad), watchesMain); err == nil { t.Errorf("%s was taken as an answer", bad) } } @@ -371,7 +376,7 @@ 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) + m, err := OpenMessage(answer, watchesMain) if err != nil || m.Event != "forge.open" { t.Fatalf("message %+v err %v", m, err) } @@ -382,12 +387,12 @@ func TestOpenMessageNamesEveryOpenPullIntoTheTrunk(t *testing.T) { // 1279/AC6 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":[]}`)) + 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)); err == nil { + if _, err := OpenMessage([]byte(bad), watchesMain); err == nil { t.Errorf("%s was taken as an answer", bad) } } @@ -490,3 +495,25 @@ func slicesEqual(a, b []string) bool { } return true } + +// watchesMain watches the branch main of every repository, as the registrations of a test name it. +var watchesMain = watchedOf("main") + +// watchedOf watches the branches of every repository, before the first registration, as the gate's source names them. +func watchedOf(branches ...string) *Watched { + w := &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 012fdea5..4c51d447 100644 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/call.go +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/call.go @@ -8,6 +8,7 @@ import ( "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 @@ -87,15 +88,17 @@ func FilesVia(ctx context.Context, call Caller) Files { // 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 trunk, asked again: built as the bridge builds the one it writes -// on FORGE (it is that event's body through ConvertKept), from the pull request's owner, repository, number, branch, head -// and merge commit, 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 string, trees Trees, files Files) (Message, error) { - body, err := json.Marshal(git.PullMergedEvent{Repository: repository(owner, repo), Number: int64(number), Branch: branch, Head: head, Base: Trunk, MergeCommit: merge}) - if err != nil { - return Message{}, err +// 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) } - return ConvertKept(MergedSubject, body, trees, files, nil) + 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. diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/files_test.go b/modules/mesh-delivery-forge-bridge/internal/bridge/files_test.go index aab23cd9..09b4418e 100644 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/files_test.go +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/files_test.go @@ -66,7 +66,7 @@ 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 := Convert(mergedSubject, mergedBody(nil), treesOf(&calls, "r0", map[string]string{"modules/a": "t1", "modules/b": "t2"}, nil), forge.get) + 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) } @@ -87,7 +87,7 @@ func TestMergedCarriesManifestsAndLocks(t *testing.T) { // 1448/AC1 // 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 = Convert(mergedSubject, mergedBody(map[string]any{"repo": "mesh-catalog"}), treesOf(&calls, "r0", map[string]string{"modules/a": "t1"}, nil), forge.get) + 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) } @@ -108,19 +108,19 @@ func TestMergedCarriesManifestsAndLocks(t *testing.T) { // 1448/AC1 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, _ = Convert(mergedSubject, mergedBody(map[string]any{"repo": "mesh-catalog"}), treesOf(&calls, "r0", map[string]string{"modules/a": "t1"}, nil), forge.get) + 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, _ = Convert(mergedSubject, mergedBody(nil), treesOf(&calls, "r0", map[string]string{"modules/a": "t1"}, nil), forge.get) + 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 := Convert(mergedSubject, mergedBody(map[string]any{"repo": "mesh-catalog"}), treesOf(&calls, "r0", map[string]string{"modules/a": "t1"}, nil), forge.get); !errors.Is(err, ErrRetry) { + 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 @@ -130,7 +130,7 @@ func TestMergedCarriesManifestsAndLocks(t *testing.T) { // 1448/AC1 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 = Convert(mergedSubject, mergedBody(nil), treesOf(&calls, "r0", map[string]string{"modules/b": "t2", "modules/d": "t4"}, nil), forge.get) + 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) } @@ -144,7 +144,7 @@ func TestMergedCarriesManifestsAndLocks(t *testing.T) { // 1448/AC1 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), nil, forge.get) + msg, err := Convert(updatedSubject, updated(nil), forge.get, watchesMain) if err != nil { t.Fatal(err) } @@ -157,7 +157,7 @@ func TestPullWithKnownDirsCarriesTheSameAtTheHead(t *testing.T) { // 1448/AC2 } // 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}), nil, forge.get) + msg, err = Convert(updatedSubject, updated(map[string]any{"module_dirs": nil, "module_dirs_said": nil}), forge.get, watchesMain) if err != nil { t.Fatal(err) } @@ -176,17 +176,17 @@ func TestAForgeErrorOrAMissingManifestIsRetryAndALockNotFoundIsLeftOut(t *testin "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 := Convert(mergedSubject, mergedBody(nil), trees(), forge.get) + 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), nil, forge.get) + _, 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 := Convert(mergedSubject, mergedBody(nil), trees(), forge.get) + msg, err := convertMerged(mergedBody(nil), trees(), forge.get, nil) if err != nil { t.Fatal(err) } @@ -198,7 +198,7 @@ func TestAForgeErrorOrAMissingManifestIsRetryAndALockNotFoundIsLeftOut(t *testin 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 := Convert(mergedSubject, mergedBody(nil), treesOf(&calls, "r0", map[string]string{"modules/a": "t1"}, nil), forge.get); err != nil { + 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}, @@ -335,7 +335,7 @@ func TestOneFailingFileIsRetryAndEveryCallHasReturnedBeforeTheReadDoes(t *testin dirsMap[d] = "t" } slow2 := &slowForge{failOn: "modules/m05/module.json"} - if _, err := Convert(mergedSubject, mergedBody(nil), treesOf(&calls, "r0", dirsMap, nil), slow2.get); !errors.Is(err, ErrRetry) { + if _, err := convertMerged(mergedBody(nil), treesOf(&calls, "r0", dirsMap, nil), slow2.get, nil); !errors.Is(err, ErrRetry) { t.Errorf("err = %v, want ErrRetry", err) } } @@ -353,7 +353,7 @@ func mergeReadsOnlyChangedDirs(t *testing.T) { 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 := ConvertKept(mergedSubject, mergedBody(nil), treesOf(&calls, "r0", map[string]string{"modules/a": "t1", "modules/b": "t2"}, nil), forge.get, kept) + 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) } @@ -364,7 +364,7 @@ func mergeReadsOnlyChangedDirs(t *testing.T) { 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 := ConvertKept(mergedSubject, mergedBody(nil), treesOf(&calls, "r1", trees, nil), forge.get, kept) + next, err := convertMerged(mergedBody(nil), treesOf(&calls, "r1", trees, nil), forge.get, kept) if err != nil { t.Fatal(err) } @@ -376,7 +376,7 @@ func mergeReadsOnlyChangedDirs(t *testing.T) { if len(forge.calls) != 3 { t.Errorf("the second merge read %d files, want 3", len(forge.calls)) } - whole, err := Convert(mergedSubject, mergedBody(nil), treesOf(&calls, "r1", trees, nil), forge.get) + whole, err := convertMerged(mergedBody(nil), treesOf(&calls, "r1", trees, nil), forge.get, nil) if err != nil { t.Fatal(err) } @@ -390,7 +390,7 @@ func mergeReadsOnlyChangedDirs(t *testing.T) { // 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 := ConvertKept(mergedSubject, mergedBody(nil), treesOf(&calls, "r1", trees, nil), failing.get, kept2); !errors.Is(err, ErrRetry) { + 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 @@ -398,7 +398,7 @@ func mergeReadsOnlyChangedDirs(t *testing.T) { } failing.calls = nil failing.errs = nil - again, err := ConvertKept(mergedSubject, mergedBody(nil), treesOf(&calls, "r1", trees, nil), failing.get, kept2) + again, err := convertMerged(mergedBody(nil), treesOf(&calls, "r1", trees, nil), failing.get, kept2) if err != nil { t.Fatal(err) } diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/forgemade.go b/modules/mesh-delivery-forge-bridge/internal/bridge/forgemade.go index 487fa233..6cc982b7 100644 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/forgemade.go +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/forgemade.go @@ -13,12 +13,13 @@ type ForgePull struct { Number int Branch string // the pull request's head branch Head string // the head commit - Merge string // the merge commit into the trunk + 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, -// merged into another branch than the trunk, or merged at another commit is refused. +// 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 { @@ -36,10 +37,10 @@ func ForgeMade(ctx context.Context, call Caller, owner, repo string, number int, 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 != Trunk: - return ForgePull{}, fmt.Errorf("the forge says %s#%d was merged into %s, not the trunk %s", in.Repository, p.Number, p.Base, Trunk) + 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}, nil + 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/moved.go b/modules/mesh-delivery-forge-bridge/internal/bridge/moved.go new file mode 100644 index 00000000..a559662c --- /dev/null +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/moved.go @@ -0,0 +1,95 @@ +package bridge + +import ( + "context" + "encoding/json" + "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" + "git.novox.be/novox/mesh-sdk/go/keys" +) + +// BranchMovedSubject is the seat git's branch-moved (design 47 G §11): a branch's new head, which a merge and an admitted +// push alike say, kept on the seat's stream SEAT_git_branch-moved and heard there through a durable consumer. +const BranchMovedSubject = git.SubjectBranchMoved + +// Moved is a branch-moved as the bridge reads it: the repository's two halves and the branch. The head the event says is +// never taken: the commit is read from the forge's own record by the seat's verb branch (§11). +type Moved struct { + Owner, Repo, Branch string +} + +// MovedOf reads a branch-moved's body. +func MovedOf(data []byte) (Moved, error) { + var e git.BranchMovedEvent + if err := json.Unmarshal(data, &e); err != nil { + return Moved{}, unreadable(BranchMovedSubject, "the body is not json: %v", err) + } + m := Moved{Branch: e.Branch} + m.Owner, m.Repo, _ = strings.Cut(e.Repository, "/") + if m.Owner == "" || m.Repo == "" || strings.ContainsAny(m.Owner+m.Repo, "./ *>") || m.Branch == "" { + return Moved{}, unreadable(BranchMovedSubject, "no repository / or branch (%q, %q)", e.Repository, e.Branch) + } + return m, nil +} + +// Repository is `/`. +func (m Moved) Repository() string { return repository(m.Owner, m.Repo) } + +// HeadKey is the key of a watched branch's last head written in the state heads: `branch...` (A-236), apart from a pull request's `..`. +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) { + a, err := askGit[git.BranchAnswer](ctx, call, git.VerbBranch, git.BranchInput{Repository: m.Repository(), Branch: m.Branch}) + if err != nil { + return Branch{}, 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 Branch{Head: a.Head, Protected: a.Protected}, 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}) + if err != nil { + return Message{}, fmt.Errorf("%w: %s: the seat git did not answer pull: %v", ErrRetry, what, 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) + } + 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 + } + return landed(what, f, head, trees, files, kept) +} diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/moved_test.go b/modules/mesh-delivery-forge-bridge/internal/bridge/moved_test.go new file mode 100644 index 00000000..17ed5a68 --- /dev/null +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/moved_test.go @@ -0,0 +1,77 @@ +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" +) + +// 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) { + w := &Watched{} + w.Set([]Registered{{Origin: "novox/mesh-sdk", Branches: []string{"main", "beta"}}}, []string{"main"}) + for _, c := range []struct { + repo, branch string + want bool + }{{"novox/mesh-sdk", "beta", true}, {"novox/mesh-sdk", "feat/x", false}, {"novox/other", "main", false}} { + if got := w.Watches(c.repo, c.branch); got != c.want { + t.Errorf("registered: %s of %s watched %v, want %v", c.branch, c.repo, got, c.want) + } + } + if !watchedOf("main").Watches("novox/other", "main") || watchedOf("main").Watches("novox/other", "beta") { + 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"} + for _, c := range []struct { + name string + pull json.RawMessage + want forgemsg.Forge + retry bool + calls1 int + }{ + {"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}, + } { + 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 + } + 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 + } + 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) + } + } +} diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/paths_test.go b/modules/mesh-delivery-forge-bridge/internal/bridge/paths_test.go index c4ddac59..734224fd 100644 --- a/modules/mesh-delivery-forge-bridge/internal/bridge/paths_test.go +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/paths_test.go @@ -22,7 +22,7 @@ func TestAMergedMessageCarriesNoNamedPathsTheBridgeDerived(t *testing.T) { return nil, ErrNotFound } var calls []call - msg, err := Convert(mergedSubject, mergedBody(map[string]any{"repo": "mesh-sdk", "module_dirs": []string{""}}), treesOf(&calls, "r0", map[string]string{"": "r0"}, nil), files) + 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) } diff --git a/modules/mesh-delivery-forge-bridge/internal/bridge/watched.go b/modules/mesh-delivery-forge-bridge/internal/bridge/watched.go new file mode 100644 index 00000000..27016255 --- /dev/null +++ b/modules/mesh-delivery-forge-bridge/internal/bridge/watched.go @@ -0,0 +1,71 @@ +package bridge + +import ( + "slices" + "sync" +) + +// Watched is the branches the bridge watches, by repository (design 47 G §4, §11, §507, §532): the registrations' watched +// branches by their origin, from a first view of `registrations` and then `registration-changed`; before the first +// registration, the branches the gate's `source` answers, of every repository. No branch is fixed in code (§7). +type Watched struct { + mu sync.Mutex + byRepo map[string][]string + source []string + registered bool +} + +// Registered is one registration as the bridge reads it: its origin `/` and its watched branches. +type Registered struct { + Origin string + Branches []string +} + +// Set takes what the bridge read: the registrations (none before the first) and the gate's source's branches by name. +func (w *Watched) Set(regs []Registered, source []string) { + by := map[string][]string{} + for _, r := range regs { + for _, b := range r.Branches { + if b != "" && !slices.Contains(by[r.Origin], b) { + by[r.Origin] = append(by[r.Origin], b) + } + } + } + w.mu.Lock() + defer w.mu.Unlock() + w.byRepo, w.registered = by, len(regs) > 0 + w.source = slices.Clone(source) +} + +// Watches says whether a move of branch in repository is one the bridge writes. +func (w *Watched) Watches(repository, branch string) bool { + w.mu.Lock() + defer w.mu.Unlock() + if !w.registered { + return slices.Contains(w.source, branch) + } + return slices.Contains(w.byRepo[repository], branch) +} + +// Branches are the watched branches of repository, in the order the registrations name them; before the first +// registration, the gate's source's. +func (w *Watched) Branches(repository string) []string { + w.mu.Lock() + defer w.mu.Unlock() + if !w.registered { + return slices.Clone(w.source) + } + return slices.Clone(w.byRepo[repository]) +} + +// Repositories are the repositories the registrations name, sorted; none before the first registration. +func (w *Watched) Repositories() []string { + w.mu.Lock() + defer w.mu.Unlock() + out := make([]string, 0, len(w.byRepo)) + for r := range w.byRepo { + out = append(out, r) + } + slices.Sort(out) + return out +} diff --git a/modules/mesh-delivery-forge-bridge/manifest_test.go b/modules/mesh-delivery-forge-bridge/manifest_test.go index cdd979e1..be2ceab2 100644 --- a/modules/mesh-delivery-forge-bridge/manifest_test.go +++ b/modules/mesh-delivery-forge-bridge/manifest_test.go @@ -103,7 +103,9 @@ func TestManifestDeclarations(t *testing.T) { // 1235/AC1 // 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). - if want := []string{"git.forge-delete", "git.pull-closed", "git.pull-merged", "git.pull-updated", "mesh-broker.dropped", "mesh-broker.recreated", "mesh-delivery-checker.owed", "mesh-delivery.owed"}; !reflect.DeepEqual(sorted(m.Consumes), want) { + // 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-delivery-checker.owed", "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 @@ -111,8 +113,8 @@ func TestManifestDeclarations(t *testing.T) { // 1235/AC1 if len(m.Reads) != 0 { t.Errorf("reads = %v, want none", m.Reads) } - if want := []string{"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-delivery.groups", "seat:mesh-hand-acts.hand-act"}; !reflect.DeepEqual(sorted(m.Invokes), want) { + 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) { t.Errorf("invokes = %v, want %v", m.Invokes, want) } if b, ok := m.OwnSecrets["broker"]; !ok || len(m.OwnSecrets) != 1 || b != "${dir:state}/broker" { diff --git a/modules/mesh-delivery-forge-bridge/module.json b/modules/mesh-delivery-forge-bridge/module.json index a98c4328..0cbf07f2 100644 --- a/modules/mesh-delivery-forge-bridge/module.json +++ b/modules/mesh-delivery-forge-bridge/module.json @@ -7,6 +7,9 @@ "git.pull-merged", "git.pull-closed", "git.forge-delete", + "git.branch-moved", + "mesh-catalog.registration-changed", + "mesh-delivery.source-moved", "mesh-delivery.owed", "mesh-delivery-checker.owed", "mesh-broker.recreated", @@ -43,6 +46,9 @@ "seat:git.file", "seat:git.pull", "seat:git.tags", + "seat:git.branch", + "seat:mesh-catalog.registrations", + "seat:mesh-delivery.source", "seat:mesh-broker.show" ], "requires": [ From a600090fcd8777114e22285cb372a97611eadc42 Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 12 Nov 2026 09:19:46 +0100 Subject: [PATCH 3/5] G-715: the forge bridge says a group's end once per base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ForgeGroupEnd gains base; the bridge reads each member's base from the seat's verb groups (Member.base is already in the contract and seatgen's output), keeps groups.., says one end per base from that base's members, and bridge_group_end_again takes the base. Builds: design 47 G §715, A-236 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h --- .../mesh-delivery-forge-bridge-tools/main.go | 7 +- .../main_test.go | 25 ++-- .../mesh-delivery-forge-bridge/groupask.go | 4 +- .../cmd/mesh-delivery-forge-bridge/groups.go | 24 ++-- .../mesh-delivery-forge-bridge/groups_test.go | 30 +++-- .../integration_test.go | 4 +- .../forgemsg/group.go | 8 +- .../internal/groups/groups.go | 120 ++++++++++++------ 8 files changed, 145 insertions(+), 77 deletions(-) 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 31ab8967..3a62f468 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 @@ -101,11 +101,12 @@ func Tools(ask asker, now func() time.Time, groupsRead func() ([]groups.Row, []s "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"}, "properties": map[string]any{ + 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, "why"), stdio.CallerOf(args), now()) + key, value, err := groups.NewEndAsk(str(args, "group"), str(args, "base"), str(args, "why"), stdio.CallerOf(args), now()) if err != nil { return nil, err } @@ -113,7 +114,7 @@ func Tools(ask asker, now func() time.Time, groupsRead func() ([]groups.Row, []s if err != nil { return nil, err } - return map[string]any{"group": a.Group, "written": a.Written, "answer": a.Answer, "by": a.By, "why": a.Why}, nil + return map[string]any{"group": a.Group, "base": a.Base, "written": a.Written, "answer": a.Answer, "by": a.By, "why": a.Why}, nil }}, } } 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 83d8a758..c88ff795 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 @@ -27,8 +27,8 @@ 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"}, - {Repository: "novox/b", Number: int64(b), Head: h2, State: stateB}}} + 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")} @@ -40,9 +40,9 @@ func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) { 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", Why: "lost", By: "lead@shanks", At: time.Date(2026, 11, 4, 22, 0, 0, 0, time.UTC), + 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": answeredAsk, "gathered": []byte("{")} + 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) { @@ -54,7 +54,7 @@ func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) { } 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": c2}, asks) + 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 { @@ -83,7 +83,7 @@ func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) { 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" { + 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 @@ -107,8 +107,8 @@ func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) { 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", "why": "the indexer never took it in"}).(map[string]any) - if askedKey != "ready" || asked.Group != "ready" || asked.Why != "the indexer never took it in" || out["written"] != true { + 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 { @@ -116,9 +116,18 @@ func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) { 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. 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 index ef54a70f..88158e91 100644 --- 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 @@ -58,10 +58,10 @@ func (g *grouper) answerEnd(ctx context.Context, a groups.EndAsk, at time.Time) 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, groups.AgainStamp(a.At)): + 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 is not written: a member's merged message is not written, or FORGE refused it (the bridge's journal says which)" + 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 index aade6a14..e5aba649 100644 --- 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 @@ -20,7 +20,8 @@ import ( // 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). +// 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) @@ -93,19 +94,24 @@ func (g *grouper) published(ctx context.Context, f forgemsg.Forge) { return } if m, ok := g.groupOf(ctx, f); ok { - g.end(ctx, m, "") + 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: `groups.` in performed says it was. again says it is written again by hand +// 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, again string) bool { +func (g *grouper) end(ctx context.Context, m seat.Group, base, again string) bool { if len(m.Members) < 2 { return false } - key, err := groupkeys.Key(m.ID) + 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 @@ -116,8 +122,8 @@ func (g *grouper) end(ctx context.Context, m seat.Group, again string) bool { } } // 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} - for _, member := range m.Members { + 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) @@ -130,13 +136,13 @@ func (g *grouper) end(ctx context.Context, m seat.Group, again string) bool { return false } last := end.Members[len(end.Members)-1].Merge - if err := g.send(ctx, forgeSubject(forgemsg.ForgeGroupEndEvent), data, groupkeys.EndID(m.ID, last, again)); err != nil { + 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 is written: %d members merged, the last %s", m.ID, len(end.Members), last) + 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 index 34b8b28f..ad922071 100644 --- 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 @@ -33,7 +33,8 @@ func TestAGroupsMembersNameItAndItsEndIsSaidOnce(t *testing.T) { 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"}, {Repository: "novox/mesh-delivery", Number: 9, Head: h2, State: "merging"}}} + {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 @@ -41,7 +42,7 @@ func TestAGroupsMembersNameItAndItsEndIsSaidOnce(t *testing.T) { 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", Number: number, Head: head, Merge: commit, Root: "r"}) + 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) } @@ -60,7 +61,7 @@ func TestAGroupsMembersNameItAndItsEndIsSaidOnce(t *testing.T) { 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) != 2 || back.Members[0].Number != 37 { + 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) @@ -71,20 +72,27 @@ func TestAGroupsMembersNameItAndItsEndIsSaidOnce(t *testing.T) { _, 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."+c2 { - t.Fatalf("ends %v ids %v, want one forge.group.merged with id group-end.feat/take-in.%s", pub.subjects, pub.ids, c2) + 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 || len(end.Members) != 2 || end.Members[0].Merge != c1 || end.Members[1].Merge != c2 { - t.Errorf("the end = %+v %v, want both members in the group's order", end, err) + 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"); done[key] != c2 { + 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", By: "operator@shanks", Why: "the indexer never took it in", At: time.Date(2026, 11, 4, 23, 0, 0, 123, time.UTC)} + 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 @@ -94,6 +102,7 @@ func TestAGroupsMembersNameItAndItsEndIsSaidOnce(t *testing.T) { 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"}, @@ -101,6 +110,7 @@ func TestAGroupsMembersNameItAndItsEndIsSaidOnce(t *testing.T) { 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 }} @@ -108,7 +118,7 @@ func TestAGroupsMembersNameItAndItsEndIsSaidOnce(t *testing.T) { 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", c2, groupkeys.AgainStamp(ask.At)) { + 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/integration_test.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go index 6f658a15..8c3147a7 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go @@ -754,7 +754,7 @@ func TestBridgeSaysAGroupFromTheSeatsGroups(t *testing.T) { // 1585, ADR 0633 § headB := strings.Repeat("b", 40) mergeB := strings.Repeat("e", 40) groups, _ := json.Marshal(seat.GroupsAnswer{Groups: []seat.Group{{ID: "feat/x", State: "merging", Members: []seat.Member{ - {Repository: "novox/mesh-delivery", Number: 12, Head: head, State: "merged"}, {Repository: "novox/mesh-sdk", Number: 4, Head: headB, State: "merging"}}}}}) + {Repository: "novox/mesh-delivery", Number: 12, Head: head, State: "merged", Base: "main"}, {Repository: "novox/mesh-sdk", Number: 4, Head: headB, State: "merging", Base: "main"}}}}}) b.answer("mesh-delivery.groups", string(groups)) ch := heard(t, b) landedOn(b, "novox/mesh-delivery", 12, "feat/x", head, merge) @@ -768,7 +768,7 @@ func TestBridgeSaysAGroupFromTheSeatsGroups(t *testing.T) { // 1585, ADR 0633 § } } end, err := forgemsg.DecodeForgeGroupEnd(next(t, ch, forgeSubject(forgemsg.ForgeGroupEndEvent))) - if err != nil || end.Group != "feat/x" || len(end.Members) != 2 || end.Members[0].Merge != merge || end.Members[1].Merge != mergeB { + if err != nil || end.Group != "feat/x" || end.Base != "main" || len(end.Members) != 2 || end.Members[0].Merge != merge || end.Members[1].Merge != mergeB { t.Errorf("the group's end: %+v %v", end, err) } refusedRead(t, b, "mesh-delivery-gate_merges") diff --git a/modules/mesh-delivery-forge-bridge/forgemsg/group.go b/modules/mesh-delivery-forge-bridge/forgemsg/group.go index a36702c5..527c3b7f 100644 --- a/modules/mesh-delivery-forge-bridge/forgemsg/group.go +++ b/modules/mesh-delivery-forge-bridge/forgemsg/group.go @@ -19,11 +19,13 @@ type GroupMerge struct { Merge string `json:"merge"` } -// ForgeGroupEnd is the body of ForgeGroupEndEvent: the group and every member merged in it, with its merge commit. +// ForgeGroupEnd is the body of ForgeGroupEndEvent: the group and every member merged in it on its base, with its merge commit. // The indexer takes the members it holds in as one source revision once it hears it (ADR 0633 §4). type ForgeGroupEnd struct { - V int `json:"v"` - Group string `json:"group"` + V int `json:"v"` + Group string `json:"group"` + // Base is the watched branch the members merged into: a group's end is said once per base (design 47 G §715). + Base string `json:"base,omitempty"` Members []GroupMerge `json:"members"` } diff --git a/modules/mesh-delivery-forge-bridge/internal/groups/groups.go b/modules/mesh-delivery-forge-bridge/internal/groups/groups.go index ca759043..ca15a0ec 100644 --- a/modules/mesh-delivery-forge-bridge/internal/groups/groups.go +++ b/modules/mesh-delivery-forge-bridge/internal/groups/groups.go @@ -34,22 +34,51 @@ const ( EndAsksBucket = Module + "_" + EndAsksState ) -// Key is the key of performed that says a group's end is written: `groups.`, its value the last -// member's merge commit the end named. -func Key(group string) (string, error) { - part, err := KeyOf(group) +// 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) escaped as a key, as the states performed and end-asks key it. -func KeyOf(group string) (string, error) { +// 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") } - return keys.EscapeName(group), nil + 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 @@ -59,10 +88,10 @@ func PublishedKey(repository string, number int) string { return "published." + hex.EncodeToString(sum[:8]) } -// EndID is the message id of a group's end, 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, lastMerge, again string) string { - id := "group-end." + group + "." + lastMerge +// 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 } @@ -77,6 +106,7 @@ func AgainStamp(at time.Time) string { return at.UTC().Format("20060102T150405.0 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"` @@ -89,16 +119,19 @@ type EndAsk struct { 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, why, by string, at time.Time) (string, []byte, error) { - group = strings.TrimSpace(group) +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)") } - key, err := KeyOf(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, Why: strings.TrimSpace(why), By: by, At: at.UTC()}) + data, err := json.Marshal(EndAsk{V: 1, Group: group, Base: base, Why: strings.TrimSpace(why), By: by, At: at.UTC()}) return key, data, err } @@ -108,16 +141,17 @@ func DecodeEndAsk(data []byte) (EndAsk, error) { if err := json.Unmarshal(data, &a); err != nil { return a, err } - if a.V != 1 || a.Group == "" { - return a, errors.New("an end-asks value of no version 1 naming no group") + 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 as bridge_groups answers it: the seat mesh-delivery's group and what the bridge kept of its end. +// 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"` @@ -159,30 +193,36 @@ func Rows(of []seat.Group, published, ended map[string]string, asks map[string][ if len(g.Members) < 2 { continue } - key, err := KeyOf(g.ID) - if err != nil { - unread = append(unread, "a group with no id") - continue + bases, missing := Bases(g) + for _, m := range missing { + unread = append(unread, "the member "+m+" of the group "+g.ID+" names no base") } - r := Row{Key: key, Group: g.ID, State: g.State} - allPublished := true - for _, m := range g.Members { - 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 + 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) } - 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) From ad561e485fb1a1569e6753340dee2708a722f12b Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 12 Nov 2026 09:21:51 +0100 Subject: [PATCH 4/5] FB7, FB8: the forge bridge asks its findings state and finding stream in the template form MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The manifest's requires gain findings (state, history 5) and finding-events (events, 7 days, 16 MiB) and its consumes mesh-conditions.findings-asked, as every part but a foundation part declares them; the bridge gains the finding branch-unprotected (next commit). Builds: design 47 G §387, §529, §383 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h --- .../mesh-delivery-forge-bridge/manifest_test.go | 13 +++++++++---- modules/mesh-delivery-forge-bridge/module.json | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/modules/mesh-delivery-forge-bridge/manifest_test.go b/modules/mesh-delivery-forge-bridge/manifest_test.go index be2ceab2..a83d6332 100644 --- a/modules/mesh-delivery-forge-bridge/manifest_test.go +++ b/modules/mesh-delivery-forge-bridge/manifest_test.go @@ -32,7 +32,7 @@ type kept struct { Name string `json:"name"` Storage string `json:"storage"` Template string `json:"template"` - Options map[string]int `json:"options"` + Options map[string]any `json:"options"` } type artifact struct { @@ -105,7 +105,7 @@ func TestManifestDeclarations(t *testing.T) { // 1235/AC1 // 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-delivery-checker.owed", "mesh-delivery.owed", "mesh-delivery.source-moved"}; !reflect.DeepEqual(sorted(m.Consumes), want) { + 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) { 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 @@ -130,8 +130,13 @@ func TestManifestDeclarations(t *testing.T) { // 1235/AC1 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) } - if want := []kept{{Name: "performed", Storage: "key-value", Template: "state", Options: map[string]int{"history": 1, "ttl-seconds": 604800}}}; !reflect.DeepEqual(m.Requires, want) { - t.Errorf("requires = %+v, want performed as %+v", m.Requires, want) + // 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) } // 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) { diff --git a/modules/mesh-delivery-forge-bridge/module.json b/modules/mesh-delivery-forge-bridge/module.json index 0cbf07f2..f4317a99 100644 --- a/modules/mesh-delivery-forge-bridge/module.json +++ b/modules/mesh-delivery-forge-bridge/module.json @@ -9,6 +9,7 @@ "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", @@ -57,6 +58,22 @@ "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": { From 103174e44cf79166e3e76cc90e5eb3c7278037de Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 12 Nov 2026 09:32:36 +0100 Subject: [PATCH 5/5] FB2: the forge bridge says the finding branch-unprotected MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A move of a watched branch whose protection was removed writes nothing and is said as the finding branch-unprotected (scope merge, id the repository and branch as a key, warning) through mesh-sdk's held finding writer into the bound state findings; a move that finds the protection back clears it. The findings state is bound by the Binding with performed. Builds: design 47 G §17, §131, §387, A-251 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h --- .../cmd/mesh-delivery-forge-bridge/bound.go | 16 +++ .../mesh-delivery-forge-bridge/findings.go | 111 ++++++++++++++++++ .../integration_test.go | 71 ++++++++++- .../cmd/mesh-delivery-forge-bridge/main.go | 19 ++- .../mesh-delivery-forge-bridge/main_test.go | 57 +++++++++ .../cmd/mesh-delivery-forge-bridge/moves.go | 10 +- modules/mesh-delivery-forge-bridge/go.mod | 1 + 7 files changed, 276 insertions(+), 9 deletions(-) create mode 100644 modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/findings.go diff --git a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound.go index ab5b8c77..f92d1ca7 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound.go @@ -3,7 +3,9 @@ package main import ( "context" "encoding/json" + "errors" "fmt" + "sort" "sync" "github.com/nats-io/nats.go/jetstream" @@ -91,6 +93,20 @@ func (s *boundBucket) Delete(ctx context.Context, key string, opts ...jetstream. return kv.Delete(ctx, key, opts...) } +// Keys are the bucket's keys, sorted; none is an empty list. +func (s *boundBucket) Keys(ctx context.Context) ([]string, error) { + kv, err := s.open(ctx) + if err != nil { + return nil, err + } + keys, err := kv.Keys(ctx) + if errors.Is(err, jetstream.ErrNoKeysFound) { + return []string{}, nil + } + sort.Strings(keys) + return keys, err +} + // keptCallOf is call as a kept call (ADR 0668 §1): asked again through a timeout or no responders until answered or // busretry.Bound has passed. func keptCallOf(ctx context.Context, call caller) func(key string, body any) (json.RawMessage, error) { 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 new file mode 100644 index 00000000..89690656 --- /dev/null +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/findings.go @@ -0,0 +1,111 @@ +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/integration_test.go b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go index 8c3147a7..ca8dd51e 100644 --- a/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go +++ b/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/integration_test.go @@ -66,6 +66,20 @@ accounts { "$JS.API.STREAM.INFO.KV_mod_mesh-delivery-forge-bridge_performed_b1" "$JS.FC.KV_mod_mesh-delivery-forge-bridge_performed_b1.>" "$KV.mod_mesh-delivery-forge-bridge_performed_b1.>" + "$JS.API.CONSUMER.CREATE.KV_mod_mesh-delivery-forge-bridge_findings_b1.>" + "$JS.API.CONSUMER.DELETE.KV_mod_mesh-delivery-forge-bridge_findings_b1.>" + "$JS.API.DIRECT.GET.KV_mod_mesh-delivery-forge-bridge_findings_b1.>" + "$JS.API.STREAM.INFO.KV_mod_mesh-delivery-forge-bridge_findings_b1" + "$JS.FC.KV_mod_mesh-delivery-forge-bridge_findings_b1.>" + "$KV.mod_mesh-delivery-forge-bridge_findings_b1.>" + "$JS.API.STREAM.NAMES" + "$JS.API.CONSUMER.CREATE.EVENTS" + "$JS.API.CONSUMER.CREATE.EVENTS.>" + "$JS.API.CONSUMER.DELETE.EVENTS.>" + "$JS.API.CONSUMER.MSG.NEXT.EVENTS.>" + "mesh.stream.mesh-delivery-forge-bridge.event.finding-written.n1" + "mesh.stream.mesh-delivery-forge-bridge.event.finding-cleared.n1" + "mesh.stream.mesh-delivery-forge-bridge.event.findings-stated.n1" "$JS.API.CONSUMER.CREATE.KV_mesh-delivery-forge-bridge_inputs.>" "$JS.API.CONSUMER.DELETE.KV_mesh-delivery-forge-bridge_inputs.>" "$JS.API.DIRECT.GET.KV_mesh-delivery-forge-bridge_inputs.>" @@ -217,6 +231,9 @@ func fileAnswer(path, ref, content string, found bool) string { // boundPerformed is the bus name the stand-in seat mesh-broker's show answers for the state performed. const boundPerformed = "mod_mesh-delivery-forge-bridge_performed_b1" +// boundFindings is the bus name the stand-in seat mesh-broker's show answers for the state findings. +const boundFindings = "mod_mesh-delivery-forge-bridge_findings_b1" + // startBed is startBridge with a step between the streams and the consumers: prepare writes what the streams hold before // the bridge's consumers are made, as the grants part makes them, from now (ADR 0443 §1), and reply sets the answer of the // seat git's open-pulls from then on. A stand-in holds the seats the bridge calls: git (every verb, trees r0 by default, @@ -237,7 +254,12 @@ func startBed(t *testing.T, openReply string, prepare func(ctx context.Context, if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "EVENTS", Subjects: []string{"mesh.mod.*.event.>", "mesh.seat.*.event.>"}}); err != nil { t.Fatal(err) } - if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "FORGE", Subjects: []string{"mesh.stream.mesh-delivery-forge-bridge.event.>"}}); err != nil { + if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "FORGE", Subjects: []string{"mesh.stream.mesh-delivery-forge-bridge.event.forge.>", "mesh.stream.mesh-delivery-forge-bridge.event.owed.ended"}}); err != nil { + t.Fatal(err) + } + // The bridge's own stream of its findings' events (design 47 G §383, §529), which its finding writer says into. + if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "mesh-delivery-forge-bridge-findings", Subjects: []string{"mesh.stream.mesh-delivery-forge-bridge.event.finding-written.*", + "mesh.stream.mesh-delivery-forge-bridge.event.finding-cleared.*", "mesh.stream.mesh-delivery-forge-bridge.event.findings-stated.*"}}); err != nil { t.Fatal(err) } // Each seat's own stream owed, as its contract keeps it (Stream, SEAT__owed), as the grants part makes it: no @@ -250,7 +272,7 @@ func startBed(t *testing.T, openReply string, prepare func(ctx context.Context, } // The bridge's states, made from the manifest's state entries and its requires before the process starts; the state // performed under the bus name the seat mesh-broker's show answers for it (boundPerformed), never an old name (§577). - for _, name := range []string{headsBucket, boundPerformed, inputsBucket, groupkeys.EndAsksBucket, groupkeys.OwedAsksBucket} { + for _, name := range []string{headsBucket, boundPerformed, boundFindings, inputsBucket, groupkeys.EndAsksBucket, groupkeys.OwedAsksBucket} { if _, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: name, History: 1}); err != nil { t.Fatal(err) } @@ -279,8 +301,9 @@ func startBed(t *testing.T, openReply string, prepare func(ctx context.Context, "mesh-delivery.groups": `{"groups":[]}`, "mesh-hand-acts.hand-act": `{"recorded":true}`, // No registration yet: the watched branches are the gate's source's, main (design 47 G §507). "mesh-catalog.registrations": `{"registrations":[]}`, "mesh-delivery.source": `{"branches":["main"]}`, - "git.pull": `{"error":"no pull request merged at that commit","error-kind":"not-found"}`, - "mesh-broker.show": `{"state":"created","binding":{"local":"performed","storage":"key-value","template":"state","bucket":"` + boundPerformed + `"}}`, + "git.pull": `{"error":"no pull request merged at that commit","error-kind":"not-found"}`, + "mesh-broker.show": `{"state":"created","binding":{"local":"performed","storage":"key-value","template":"state","bucket":"` + boundPerformed + `"}}`, + "mesh-broker.show findings": `{"state":"created","binding":{"local":"findings","storage":"key-value","template":"state","bucket":"` + boundFindings + `"}}`, } for _, verb := range []string{"status", "comment", "view", "note", "merge-pull"} { answers["git."+verb] = string(gitfixture.Given(t, verb+" answers", "verb:"+verb+".answer")) @@ -294,10 +317,16 @@ func startBed(t *testing.T, openReply string, prepare func(ctx context.Context, mu.Lock() called = append(called, verb+" "+string(m.Data)) answer, files := answers[verb], filesOf - var in struct{ Repository string } + var in struct { + Repository string + Local string `json:"local-name"` + } if json.Unmarshal(m.Data, &in) == nil && answers[verb+" "+in.Repository] != "" { answer = answers[verb+" "+in.Repository] // a move's branch and pull, set per repository (landedOn) } + if in.Local != "" && answers[verb+" "+in.Local] != "" { + answer = answers[verb+" "+in.Local] // show answers each entry of the module's requires by its local name + } mu.Unlock() if verb == "git.file" { var in git.FileInput @@ -873,3 +902,35 @@ func TestBridgePerformsWhatIsOwedOnce(t *testing.T) { } func sha256Sum(b []byte) []byte { s := sha256.Sum256(b); return s[:] } + +// Design 47 G §17, §383, §529, A-251 (FB2): the bridge started whole against the real bus says a move of a branch whose protection +// was removed as the finding branch-unprotected, into its bound state findings and its finding stream, and writes no message. +func TestBridgeSaysAnUnprotectedBranchAsAFinding(t *testing.T) { + b := startBed(t, `{"pulls":[]}`, nil) + b.answer("git.branch novox/mesh-delivery", `{"repository":"novox/mesh-delivery","branch":"main","head":"`+merge+`","protected":false}`) + moveMain(t, b, "novox/mesh-delivery") + kv, err := b.js.KeyValue(b.ctx, boundFindings) + if err != nil { + t.Fatal(err) + } + var e jetstream.KeyValueEntry + for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) { + if e, err = kv.Get(b.ctx, "merge.novox/mesh-delivery/main.branch-unprotected"); err == nil { + break + } + } + if err != nil { + t.Fatalf("the finding is not in the bound state findings: %v", err) + } + var f struct{ Kind, Scope, ID, Observer string } + if err := json.Unmarshal(e.Value(), &f); err != nil || f.Kind != "branch-unprotected" || f.Observer != "mesh-delivery-forge-bridge" { + t.Errorf("the finding = %s %v", e.Value(), err) + } + forge, err := b.js.Stream(b.ctx, "FORGE") + if err != nil { + t.Fatal(err) + } + if info, err := forge.Info(b.ctx); err != nil || info.State.Msgs != 1 { // the open message of its start only + t.Errorf("FORGE holds %+v %v: an unprotected branch's move writes no message", info, 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 0adb3881..bedda22d 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 @@ -23,6 +23,7 @@ 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" @@ -147,15 +148,29 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro // 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) + binding := process.NewBinding(brokerSeat, forgeBridgeModule, keptCallOf(ctx, call), logger.Printf, performedLocal, findingsLocal) if err := binding.Register(hub); err != nil { return err } binding.Ask(performedLocal) + binding.Ask(findingsLocal) 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}) + 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*). 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. @@ -199,7 +214,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro 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, "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, turn: &turn, logger: 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. 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 b4874634..31ded396 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 @@ -13,6 +13,7 @@ import ( "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" @@ -297,3 +298,59 @@ func someFiles(_, _, path, _ string) ([]byte, error) { 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 40b6c060..7d0d4bed 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 @@ -7,6 +7,7 @@ import ( "log" "strings" "sync" + "time" "github.com/nats-io/nats.go/jetstream" @@ -28,8 +29,11 @@ type mover struct { watched *bridge.Watched inputs bridge.Kept groups *grouper - turn *sync.Mutex - logger *log.Logger + // 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 } // hear takes one branch-moved: one it cannot read is terminated, one of a branch not watched is acknowledged with nothing @@ -66,6 +70,8 @@ func (mv *mover) move(ctx context.Context, moved bridge.Moved) error { 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) diff --git a/modules/mesh-delivery-forge-bridge/go.mod b/modules/mesh-delivery-forge-bridge/go.mod index ea862348..331ce363 100644 --- a/modules/mesh-delivery-forge-bridge/go.mod +++ b/modules/mesh-delivery-forge-bridge/go.mod @@ -4,6 +4,7 @@ go 1.26.0 require ( git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient v0.0.0-00010101000000-000000000000 + git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog v0.0.0-00010101000000-000000000000