From 30d1527e6ae7fe6de5ceabf577cd3c4956eb4c2f Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 02:33:51 +0100 Subject: [PATCH 01/43] GATE-1: the gate's switch, its carried approval and its old states go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The switch to the records ran once at G's delivery; from H1 one model holds, so the switch, the carried Approve and its finding, the states merges and the old owed, the ledger's switch key and the checker client nobody imports leave the gate. Builds: design 47 H §764 (the gate: the switch and the carried approval, the states merges, owed, asks, pulls, turns); H-A-28 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/mesh-delivery-gate/deliveries_test.go | 30 -- .../cmd/mesh-delivery-gate/findings.go | 62 +--- .../cmd/mesh-delivery-gate/findings_test.go | 123 -------- .../cmd/mesh-delivery-gate/gate.go | 3 - .../cmd/mesh-delivery-gate/keeps.go | 2 +- .../cmd/mesh-delivery-gate/keeps_test.go | 16 +- .../cmd/mesh-delivery-gate/main.go | 22 +- .../cmd/mesh-delivery-gate/manifest_test.go | 38 ++- .../cmd/mesh-delivery-gate/merges.go | 54 +--- .../cmd/mesh-delivery-gate/record.go | 24 -- .../cmd/mesh-delivery-gate/record_test.go | 2 +- .../mesh-delivery-gate/sourcetools_test.go | 7 +- .../cmd/mesh-delivery-gate/switch.go | 296 ------------------ .../cmd/mesh-delivery-gate/values.go | 130 -------- .../cmd/mesh-delivery-gate/values_test.go | 68 ---- modules/mesh-delivery-gate/go.mod | 1 - modules/mesh-delivery-gate/module.json | 33 -- 17 files changed, 61 insertions(+), 850 deletions(-) delete mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings_test.go delete mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/switch.go delete mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/values.go delete mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/values_test.go diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries_test.go index f523fbda9..ba49f95cf 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries_test.go @@ -243,33 +243,3 @@ func TestAMissedEventIsPutRightAtStart(t *testing.T) { t.Fatal("a forge that cannot be read is said") } } - -// A-281: the switch runs once; a start, then a pull-updated, then a second start opens no record. -func TestTheSwitchRunsOnceSoDeliveriesNeverBecomeRecordsAtAStart(t *testing.T) { - h, k := gateWithDeliveries(t) - h.Old = kvKeptOld{k} - if _, _, err := h.Switch(); err != nil { - t.Fatal(err) - } - if err := h.PullUpdated(updated("novox/app", 3, strings.Repeat("a", 40), "main", "feat")); err != nil { - t.Fatal(err) - } - opened, found, err := h.Switch() - if err != nil || opened != 0 || found != 0 { - t.Fatalf("the second start switched: %d %d %v", opened, found, err) - } - if kept, _ := h.Records.Kept(); len(kept) != 0 { - t.Fatalf("a delivery became a record: %+v", kept) - } - if done, err := h.Records.SwitchDone(); err != nil || !done { - t.Fatalf("switch.done %v %v", done, err) - } -} - -type kvKeptOld struct{ k *memKept } - -func (o kvKeptOld) Keys(state string) ([]string, error) { return o.k.Keys(state) } -func (o kvKeptOld) Get(state, key string) ([]byte, uint64, error) { - v, err := o.k.Get(state, key) - return v, 1, err -} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go index bae72c816..b5b1678f0 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go @@ -2,28 +2,22 @@ package main import ( "context" - "encoding/json" - "fmt" - "strconv" "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" stdio "git.novox.be/novox/mesh-sdk/go" - "git.novox.be/novox/mesh-sdk/go/keys" "git.novox.be/novox/mesh-sdk/go/views" ) // The gate's own findings (design 47 G A-39, A-29, A-37, A-30, §592): they go through one held writer over the gate's own state -// `findings`, whose three events the manifest declares. The first is `approval-carried-open`: for each `approval-carried` the -// gate says, a carried Approve nobody uses is seen, and cleared when the seat `git` says that pull request merged or closed. +// `findings`, whose three events the manifest declares. // Module is the gate's module name, the observer of its findings. const Module = "mesh-delivery-gate" // The scope and the kind of the finding a carried approval nobody uses is (design 47 §592, A-42: the scope the mesh already uses for the delivery's findings). const ( - FindingScope = "delivery" - ApprovalCarriedOpen = "approval-carried-open" + FindingScope = "delivery" ) // pullMergedEvent and pullClosedEvent are the seat git's events that end a pull request, as the gate's manifest consumes them. @@ -44,55 +38,5 @@ type FindingWriter interface { // generated contract of the seat that defines it (A-17). It is started once the runtime serves (A-40). func NewFindingWriter() (*views.HeldWriter[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent], error) { return views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{ - Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{ApprovalCarriedOpen, SourceUncompared}}) -} - -// approvalID is the id of a pull request's finding: its repository and number, each escaped as a key's name is. -func approvalID(repository string, number int64) string { - return keys.EscapeName(repository) + "." + keys.EscapeName(strconv.FormatInt(number, 10)) -} - -// approvalOpen writes the finding for an `approval-carried` the gate says, naming the head the Approve named, unless it holds it. -func (h *Holder) approvalOpen(repository string, number int, head string) error { - if h.Findings == nil { - return nil - } - id := approvalID(repository, int64(number)) - for _, f := range h.Findings.Held() { - if f.Scope == FindingScope && f.ID == id && f.Kind == ApprovalCarriedOpen { - return nil - } - } - at := h.Now().UTC() - pull := fmt.Sprintf("%s#%d", repository, number) - return h.Findings.Write(context.Background(), meshconditions.Finding{Scope: FindingScope, ID: id, Kind: ApprovalCarriedOpen, - Subject: meshconditions.Subject{Scope: FindingScope, ID: id}, - Severity: meshconditions.FindingSeverityWarning, Resolver: "operator", - Summary: fmt.Sprintf("the Approve of %s at %s, given before G, is carried and no merge or close of that pull request followed", pull, shortOf(head, 12)), - Headline: "An Approve carried over waits for its pull request", - Explanation: "The operator's Approve of this pull request, given before the delivery's switch, counts as its approval step. Nobody has merged or closed the pull request since.", - Needs: "merge the pull request through the seat git's merge-pull, or close it; the finding clears when the seat git says either.", - Source: "approval-carried", Observer: Module, At: at, Since: at}) -} - -// pullEnded clears the finding of a pull request the seat git said merged or closed; a pull request with none changes nothing. -func (h *Holder) pullEnded(body []byte, why string) error { - if h.Findings == nil { - return nil - } - var p struct { - Repository string `json:"repository"` - Number int64 `json:"number"` - } - if err := json.Unmarshal(body, &p); err != nil || p.Repository == "" || p.Number <= 0 { - h.Refuse(fmt.Errorf("a %s of the seat git is not readable: %v", why, err)) - return nil - } - id := approvalID(p.Repository, p.Number) - for _, f := range h.Findings.Held() { - if f.Scope == FindingScope && f.ID == id && f.Kind == ApprovalCarriedOpen { - return h.Findings.Clear(context.Background(), FindingScope, id, ApprovalCarriedOpen, "the pull request was "+why) - } - } - return nil + Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{SourceUncompared}}) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings_test.go deleted file mode 100644 index 20a6418a0..000000000 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings_test.go +++ /dev/null @@ -1,123 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "strings" - "sync" - "testing" - "time" - - "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" - mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" - stdio "git.novox.be/novox/mesh-sdk/go" - "git.novox.be/novox/mesh-sdk/go/views" -) - -// memState is the gate's state `findings` in memory. -type memState struct { - mu sync.Mutex - m map[string][]byte -} - -func (s *memState) Get(key string) (*stdio.StateEntry, error) { - s.mu.Lock() - defer s.mu.Unlock() - if v, ok := s.m[key]; ok { - return &stdio.StateEntry{Key: key, Value: v}, nil - } - return nil, nil -} -func (s *memState) Put(key string, value any) (uint64, error) { - raw, _ := json.Marshal(value) - s.mu.Lock() - defer s.mu.Unlock() - s.m[key] = raw - return 1, nil -} -func (s *memState) Delete(key string) error { s.mu.Lock(); delete(s.m, key); s.mu.Unlock(); return nil } -func (s *memState) Keys() (k []string, _ error) { - s.mu.Lock() - defer s.mu.Unlock() - for key := range s.m { - k = append(k, key) - } - return -} - -type oldFake map[string][]byte - -func (o oldFake) Keys(string) (k []string, _ error) { - for key := range o { - k = append(k, key) - } - return -} -func (o oldFake) Get(_, key string) ([]byte, uint64, error) { return o[key], 1, nil } - -// The gate's own finding (A-39, A-29, A-37, §592): written through the held writer for each approval carried, once, and cleared -// when the seat git says that pull request merged or closed; the writer's events are the manifest's. -func TestTheGatesApprovalCarriedOpenIsWrittenThroughItsHeldWriterAndClearedByTheSeatGit(t *testing.T) { - at := time.Date(2026, 11, 11, 21, 0, 0, 0, time.UTC) - head := strings.Repeat("1", 40) - state := &memState{m: map[string][]byte{}} - var mu sync.Mutex - var said []string - w, err := views.NewHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{ - Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: state, Kinds: []string{ApprovalCarriedOpen}, Node: "ace", - Now: func() time.Time { return at }, - Publish: func(_ context.Context, subject string, _ []byte, _ string) error { - mu.Lock() - said = append(said, subject) - mu.Unlock() - return nil - }, - Listen: func(context.Context, string, func([]byte)) (func(), error) { return func() {}, nil }}) - if err != nil { - t.Fatal(err) - } - if err := w.Start(context.Background()); err != nil { - t.Fatal(err) - } - merge, merr := mdc.EncodeMerge(Merge{V: 1, Group: "feat/x", Ask: "merge.feat_x.0123456789ab", Approved: map[string]string{"novox/app@7": head, "novox/web@8": head, "novox/db@9": head}, - Pulls: []MergePull{{Head: mdc.Head{Repository: "novox/db", Number: 9, Head: head}, State: mdc.MergeMerged, Commit: "c0ffee"}}}.stored()) - h := &Holder{Logf: t.Logf, Now: func() time.Time { return at }, Old: oldFake{"feat_x": merge}, Findings: w, SaySeat: func(string, any, string) error { return nil }} - if merr != nil { - t.Fatal(merr) - } - if err := h.carryApprovals(); err != nil { - t.Fatal(err) - } - held := w.Held() - if len(held) != 2 { - t.Fatalf("held %+v", held) - } - for _, f := range held { - if f.Kind != ApprovalCarriedOpen || f.Scope != "delivery" || f.Observer != Module || !strings.Contains(f.Summary, head[:12]) { - t.Fatalf("finding %+v", f) - } - } - if id := approvalID("novox/app", 7); held[0].ID != id && held[1].ID != id { - t.Fatalf("no finding with id %s: %+v", id, held) - } - n := len(said) - if err := h.carryApprovals(); err != nil || len(said) != n { - t.Fatalf("said again: %v, %d events (was %d)", err, len(said), n) - } - // the seat git says one merged and the other closed; one it does not know changes nothing - if err := h.pullEnded([]byte(`{"repository":"novox/app","number":7}`), "merged"); err != nil { - t.Fatal(err) - } - if err := h.pullEnded([]byte(`{"repository":"novox/other","number":1}`), "merged"); err != nil || len(w.Held()) != 1 { - t.Fatalf("held %+v: %v", w.Held(), err) - } - if err := h.pullEnded([]byte(`{"repository":"novox/web","number":8}`), "closed"); err != nil || len(w.Held()) != 0 { - t.Fatalf("held %+v: %v", w.Held(), err) - } - got := strings.Join(said, " ") - for _, ev := range []string{"finding-written", "finding-cleared"} { - if !strings.Contains(got, ev) { - t.Fatalf("no %s said: %v", ev, said) - } - } -} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go index 05222b46c..7522e6dc9 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go @@ -20,8 +20,6 @@ type Holder struct { Settings func() Settings Now func() time.Time Logf func(format string, a ...any) - // Merges and Old are the old sections the switch reads (switch.go); Source is the gate's source (merges.go). - Merges MergeStore // Kept is the gate's states deliveries and groups, and Forge the seat git's reads (deliveries.go, §716). Kept DeliveryStore Forge ForgeReads @@ -29,7 +27,6 @@ type Holder struct { Catalog Registered startMu sync.Mutex dmu sync.Mutex - Old OldState Source SourceStore // Owed says what the gate owes the forge as the seat's event owed (owed.go). Owed OwedSayer diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps.go index 2ee1d5c80..7991c8974 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps.go @@ -6,7 +6,7 @@ import "git.novox.be/novox/mesh-sdk/go/announce" // issue 1790): the module says it itself, from its code, before it serves, and a reader is granted what it said // (clause 3). A state is readable when another module's manifest reads it (keeps_test.go names the readers). keeps_test.go holds the list to the // manifest's `state`, and keeps_integration_test.go that the module announces it on a bus. -var Kept = []announce.State{{Name: "conditions"}, {Name: "deliveries"}, {Name: "findings"}, {Name: "groups"}, {Name: "merges", Readable: true}, {Name: "owed", Readable: true}, {Name: "source", Readable: true}} +var Kept = []announce.State{{Name: "conditions"}, {Name: "deliveries"}, {Name: "findings"}, {Name: "groups"}, {Name: "source", Readable: true}} // announceKept says Kept through keeps, which is stdio.Keeps. func announceKept(keeps func(...announce.State) error) error { return keeps(Kept...) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go index 4694f8f7c..79bddbc3f 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go @@ -13,14 +13,7 @@ import ( // announced the state readable), kept here and not read from other repositories' manifests (clause 1). Each line names // the modules of mesh-delivery whose manifest reads the state, as of R4e. var readers = map[string][]string{ - "asks": {"mesh-delivery-teller", "mesh-delivery-view"}, - "merges": {"mesh-delivery-forge-bridge"}, - "pulls": {"build-agent"}, // in mesh-controller - "owed": {"mesh-delivery-forge-bridge"}, "source": {"mesh-delivery-indexer", "mesh-delivery-checker"}, - // ADR 0548 §3, §4: the checker reads the merge turns and claims no check of a repository one names; the gate alone writes - // them (a state is written by its owner's account alone, ADR 0495 §3: announcing it readable grants a read, never a write). - "turns": {"mesh-delivery-checker"}, } // 1790/S: the states the module announces are the states its manifest declares, no more, no fewer, and the readable @@ -75,8 +68,9 @@ func TestTheStatesTheModuleAnnouncesAreTheStatesItsManifestDeclares(t *testing.T } } -// Design 47 G §613 (A-20): deliveries, groups, owed and conditions are `requires` entries of the template `state` under -// their local names, taking the buckets they already had; merges and source stay in the old `state` section. +// Design 47 G §613 (A-20): deliveries, groups and conditions are `requires` entries of the template `state` under +// their local names, taking the buckets they already had; source stays in the old `state` section, and merges and the old +// owed go (design 47 H §764). func TestTheFourStatesMoveToTheTemplateAndTwoStayOld(t *testing.T) { raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { @@ -97,12 +91,12 @@ func TestTheFourStatesMoveToTheTemplateAndTwoStayOld(t *testing.T) { } old := string(raw[strings.Index(string(raw), `"state": [`):]) old = old[:strings.Index(old, "]")] - for _, n := range []string{"deliveries", "groups", "owed", "conditions"} { + for _, n := range []string{"deliveries", "groups", "conditions"} { if !moved[n] || strings.Contains(old, `"`+n+`"`) { t.Errorf("%s: template state %v, old section lists it %v", n, moved[n], strings.Contains(old, `"`+n+`"`)) } } - for _, n := range []string{"merges", "source"} { + for _, n := range []string{"source"} { if moved[n] || !strings.Contains(old, `"`+n+`"`) { t.Errorf("%s stays in the old section", n) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go index ec423fb96..66ea8c028 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go @@ -54,7 +54,6 @@ func main() { h := &Holder{ Settings: settings, Logf: logf, - Merges: kvMerges{}, Source: kvSource{}, Owed: seatOwed{say: stdio.SeatPublish}, // The gate says what stalled to the operator itself (issue 1275, ADR 0437 §9). @@ -65,7 +64,6 @@ func main() { h.SaySeat = stdio.SeatPublish h.Records = &Records{Ledger: sdkLedger{}, Now: h.Now, Retention: h.recordRetention, Say: stdio.SeatPublish, Logf: logf} h.Records.Changed = h.recordChanged - h.Old = kvOld{} h.Kept = kvKept{} h.Forge = seatGit{ask: stdio.Ask} h.Catalog = seatCatalog{ask: stdio.Ask} @@ -100,7 +98,6 @@ func main() { // tick, no forge poll, no reconcile loop. Each step is retried, each failure said, never given up on quietly. func run(h *Holder, l *listening) { <-stdio.Served() // Serve first: the state is reached through it, and an event is heard from as soon as it serves - switchToRecords(h, l) statePins(h, l) go h.Work() for _, pattern := range followed { @@ -131,21 +128,6 @@ func run(h *Holder, l *listening) { } } -// switchToRecords carries the groups in flight into their delivery records (switch.go), retrying until it can: no event is -// taken before, so no outcome reaches a record the switch has not opened yet. -func switchToRecords(h *Holder, l *listening) { - for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) { - opened, found, err := h.Switch() - if err == nil { - logf("[mesh-delivery] the switch to the delivery records: %d opened, %d already there", opened, found) - return - } - l.set("not yet: " + err.Error()) - logf("[mesh-delivery] the switch to the delivery records cannot be made yet (%v); asking again in %s", err, wait) - time.Sleep(wait) - } -} - // statePins says every pin the records hold once at each start (design 47 G §579), retrying until it can. func statePins(h *Holder, l *listening) { for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) { @@ -179,7 +161,7 @@ func take(h *Holder, e stdio.Envelope) error { return h.PullUpdated(e.Body) } if e.Key == pullMergedEvent { - return errors.Join(h.DeliveryMerged(e.Body), h.pullEnded(e.Body, "merged")) + return h.DeliveryMerged(e.Body) } if e.Key == registrationChangedEvent { _, err := h.StartSources() @@ -189,7 +171,7 @@ func take(h *Holder, e stdio.Envelope) error { return h.ForgeMerged(e.Body) } if e.Key == pullClosedEvent { - return errors.Join(h.DeliveryClosed(e.Body), h.pullEnded(e.Body, "closed")) + return h.DeliveryClosed(e.Body) } return nil } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go index de4dac66d..beaa20794 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go @@ -161,7 +161,7 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { // Two buckets still in the old `state` (merges, source), `pulls` and `asks` gone (§613), the four of the template `state` moved to `requires` (design 47 G §613), and no `asked` (the teller asks, ADR 0437 §6): ADR 0450 §3 and the conditions it raised (issue 1275). No // `turns`, no turn tool and no `check-gates-merge`: no check gates a merge (novox/hq ADR 0661 §1, §2, §6). buckets := m.buckets() - if !reflect.DeepEqual(buckets, []string{"merges", "source"}) { + if !reflect.DeepEqual(buckets, []string{"source"}) { // merges goes (design 47 H §764) t.Fatalf("state %v", buckets) } for _, tool := range m.Tools { @@ -307,3 +307,39 @@ func TestTheGateHoldsTheSeatAsTheSDKsTableOfRelease6HasIt(t *testing.T) { } } } + +// GATE-1/AC1 (design 47 H §764): the earlier model's states leave the gate, as its manifest, its announced states and its +// code say: no `merges`, `owed` (the old bucket), `asks`, `pulls` or `turns`, and no `approval-carried` said. +func TestTheEarlierModelsStatesAndApprovalAreGone(t *testing.T) { + m, _ := readManifest(t) + kept := []string{} + for _, s := range Kept { + kept = append(kept, s.Name) + } + gone := []string{"merges", "owed", "asks", "pulls", "turns"} + for _, row := range []struct { + where string + names []string + }{{"the manifest's state and requires", m.withTemplateStates()}, {"the announced states", kept}} { + for _, n := range gone { + if slices.Contains(row.names, n) { + t.Errorf("%s still name %s", row.where, n) + } + } + } + sources, _ := filepath.Glob("*.go") + for _, f := range sources { + if strings.HasSuffix(f, "_test.go") { + continue + } + raw, err := os.ReadFile(f) + if err != nil { + t.Fatal(err) + } + for _, name := range []string{"approval-carried", "ApprovalCarried", "SwitchedOwed"} { + if strings.Contains(string(raw), name) { + t.Errorf("%s still names %s", f, name) + } + } + } +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go index 12d88bc9e..a90ab75e8 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go @@ -12,38 +12,19 @@ import ( "git.novox.be/novox/mesh-sdk/go/keys" ) -// The gate merges nothing and hands nothing to the forge (design 47 G §592): `merges` stays an old section the switch reads -// (switch.go, §613), never written; `source` is the gate's source revision per watched branch (§15, §291), served by the verb +// The gate merges nothing and hands nothing to the forge (design 47 G §592; H §764: `merges` goes): `source` is the gate's source revision per watched branch (§15, §291), served by the verb // `source` below. +// Source is the state source's value, the seat mesh-delivery's type used as it is (novox/hq ADR 0634 §2). +type ( + Source = seat.SourceByBranchValue + // Owed is what the forge bridge performs, as the seat's event owed says it. + Owed = seat.OwedByRepositoryNumberHeadActValue +) + // changeAttempts is how often a lost compare-and-set is read and applied again before it is said. const changeAttempts = 10 -// MergeStore is the gate's bucket merges. -type MergeStore interface { - Get(group string) (*Merge, error) -} - -// OldState reads the gate's old state owed, which the switch says again as switched acts (switch.go): each key, and one -// key's value with its revision. -type OldState interface { - Keys(state string) ([]string, error) - Get(state, key string) (value []byte, revision uint64, err error) -} - -// kvOld is the old state over the runtime. -type kvOld struct{} - -func (kvOld) Keys(state string) ([]string, error) { return stdio.State(state).Keys() } - -func (kvOld) Get(state, key string) ([]byte, uint64, error) { - e, err := stdio.State(state).Get(key) - if err != nil || e == nil { - return nil, 0, err - } - return e.Value, e.Revision, nil -} - // SourceStore is the gate's bucket source, one key per watched branch (design 47 G §15), moved by compare-and-set. type SourceStore interface { // Set reads a branch's source key and writes what update makes of it, unless update says nothing is to be written, and answers @@ -55,25 +36,6 @@ type SourceStore interface { Branches() ([]string, error) } -// kvMerges is merges over the runtime. -type kvMerges struct{} - -func (kvMerges) Get(group string) (*Merge, error) { - key, err := mdc.MergesKey(group) - if err != nil { - return nil, err - } - e, err := stdio.State("merges").Get(key) - if err != nil || e == nil { - return nil, err - } - m, err := DecodeMerge(e.Value) - if err != nil { - return nil, fmt.Errorf("the merge of %s is not readable: %w", group, err) - } - return &m, nil -} - // kvSource is source over the runtime. type kvSource struct{} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go index 831f3f682..99edd8393 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go @@ -39,8 +39,6 @@ const RecordForm = 1 const ( TypeRecord = "record" TypeHeld = "held" - // TypeSwitch holds the one key `switch.done` (A-281): the gate's fingerprint and the time of the switch to the records. - TypeSwitch = "switch" ) // The operations of a record, each one effect (ADR 0597 §2), and of the month's list. The manifest's `requires` declares @@ -59,7 +57,6 @@ const ( OpAct = "act" // set-entry into acts, by id: an act owed the forge with its outcome OpHold = "hold" // a month's list: set-entry into records, by instance OpDrop = "drop" // a month's list: remove from records, by instance - OpDone = "done" // the switch: set-fields, the gate's fingerprint and the time ) // RecordOperations are a record's operations, in the order the manifest's type allows them. @@ -68,27 +65,6 @@ var RecordOperations = []string{OpOpen, OpGroup, OpTrace, OpEntry, OpOutcome, Op // HeldOperations are the month's list's operations. var HeldOperations = []string{OpHold, OpDrop} -// SwitchOperations are the switch key's operations. -var SwitchOperations = []string{OpDone} - -// switchInstance is the instance of the key `switch.done`, written once the switch has carried today's state in (A-281). -const switchInstance = TypeSwitch + "/done" - -// SwitchDone says whether the key `switch.done` is there: the switch has run, and no later start runs it again (A-281). -func (rs *Records) SwitchDone() (bool, error) { - raw, _, err := rs.Ledger.Get(switchInstance) - if err != nil { - return false, fmt.Errorf("the key switch.done cannot be read: %w", err) - } - return len(raw) > 0 && string(raw) != "null", nil -} - -// MarkSwitched writes the key `switch.done`: the gate's fingerprint and the time (A-281). -func (rs *Records) MarkSwitched(fingerprint string) error { - _, err := rs.Ledger.Apply(switchInstance, OpDone, map[string]any{"fingerprint": fingerprint, "at": rs.now().UTC().Format(time.RFC3339)}) - return err -} - // DefaultRecordRetention is how long a record is kept when the setting record-retention gives none (design 47: 90 days // covers every trace the operator has asked of a past delivery so far). const DefaultRecordRetention = 90 * 24 * time.Hour diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go index 0cb23b623..86e0a44a9 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go @@ -386,7 +386,7 @@ func TestTheDeliveryRecordIsALedgerInstanceKeyedByMilestoneAndHeads(t *testing.T // W11.1: the manifest's ledger types allow exactly the operations the gate applies, and the ledger is capped at 768 MiB. func TestTheManifestsLedgerTypesAreTheRecordsOperations(t *testing.T) { l := newLedgerFake(t) - for name, want := range map[string][]string{TypeRecord: RecordOperations, TypeHeld: HeldOperations, TypeSwitch: SwitchOperations} { + for name, want := range map[string][]string{TypeRecord: RecordOperations, TypeHeld: HeldOperations} { var got []string for _, op := range l.types[name].Operations { got = append(got, op.Name) diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go index aa0b7063c..d181be618 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go @@ -4,13 +4,14 @@ import ( "encoding/json" "errors" "fmt" - meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - stdio "git.novox.be/novox/mesh-sdk/go" "reflect" "strings" "testing" "time" + + meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" + stdio "git.novox.be/novox/mesh-sdk/go" ) type branchSource struct{ memSource } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/switch.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/switch.go deleted file mode 100644 index 730eaea4e..000000000 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/switch.go +++ /dev/null @@ -1,296 +0,0 @@ -package main - -// The switch to the delivery record (design 47 *The delivery record*: "The switch carries today's state in, once"): at -// the new gate's start, every group in flight and its merge as the old sections hold them become records, keyed by the -// milestone and the heads. It is safe to run twice: a group in flight whose record is there already leaves it as it is -// (Records.Open), and a group state the record holds already is not written again (Records.SetGroup), so delivering milestone -// G again after a revert makes no second record and asks nothing again. The old sections are read, never written (§613). - -import ( - "encoding/json" - "errors" - "fmt" - "os" - "sort" - "strings" - - mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - "git.novox.be/novox/mesh-sdk/go/keys" - "git.novox.be/novox/mesh-sdk/go/process" -) - -// oldDelivery is a delivery as the old section `deliveries` kept it, as far as the switch reads it. -type oldDelivery struct { - ID string `json:"id"` - Repository string `json:"repository"` - Commit string `json:"commit"` - Number int `json:"number"` - Branch string `json:"branch"` - Group string `json:"group"` - State string `json:"state"` - InSource bool `json:"in_source"` - NewerHead string `json:"newer_head"` - ClosedUnmerged bool `json:"closed_unmerged"` -} - -// milestone is the name a delivery's record is kept under: its group's, a lone pull request's branch, its id. -func (d oldDelivery) milestone() string { - switch { - case d.Group != "": - return d.Group - case d.Branch != "": - return d.Branch - } - return d.ID -} - -// recordGroupOf is a merge, as the old section merges holds it, as the record's group state. -func recordGroupOf(m Merge, members []string) RecordGroup { - g := RecordGroup{Group: m.Group, Members: members, Ask: m.Ask, Approved: m.Approved} - for _, p := range m.Pulls { - g.Pulls = append(g.Pulls, RecordPull{Repository: p.Repository, Number: p.Number, Head: p.Head.Head, State: string(p.State), - Commit: p.Commit, Words: p.Words}) - } - return g -} - -// switchGroup is one group in flight as the switch finds it: its milestone, its heads and its members. -type switchGroup struct { - milestone string - heads map[string]string - members []string -} - -// inFlight are the groups in flight in the old section deliveries: every milestone with a delivery not final, its live -// members' heads as `@` to the full commit. -func (h *Holder) inFlight() ([]switchGroup, error) { - if h.Old == nil { - return nil, nil - } - keys, err := h.Old.Keys("deliveries") - if err != nil { - return nil, fmt.Errorf("the old section deliveries cannot be read for the switch: %w", err) - } - by := map[string]*switchGroup{} - for _, key := range keys { - raw, _, err := h.Old.Get("deliveries", key) - if err != nil { - return nil, fmt.Errorf("the old delivery %s cannot be read for the switch: %w", key, err) - } - var d oldDelivery - if raw == nil || json.Unmarshal(raw, &d) != nil || d.ID == "" { - continue // a mark an earlier owner kept, or no delivery - } - final := d.State == "merged" || d.State == "held" || d.State == "closed" - if final && !(d.State == "merged" && !d.InSource) || d.Number == 0 || d.NewerHead != "" || d.ClosedUnmerged { - continue - } - name := d.milestone() - g := by[name] - if g == nil { - g = &switchGroup{milestone: name, heads: map[string]string{}} - by[name] = g - } - g.heads[mdc.ApprovedKey(d.Repository, d.Number)] = d.Commit - g.members = append(g.members, d.ID) - } - out := make([]switchGroup, 0, len(by)) - for _, g := range by { - sort.Strings(g.members) - out = append(out, *g) - } - sort.Slice(out, func(i, j int) bool { return out[i].milestone < out[j].milestone }) - return out, nil -} - -// Switch carries today's state into the records, once and safe to run again: each group in flight becomes a record at -// its heads, or at the heads its merge was approved at when `merges` holds one, with that merge as its group state. -// It answers how many records it opened and how many it found there. It runs once (A-281): when it has carried today's state in -// it writes the key `switch.done`, and a start that finds the key runs no switch, so the deliveries the gate writes from the -// seat git's events (§716) never become records at a start. A revert's restore brings back the state without the key, so -// delivering G again switches again. -func (h *Holder) Switch() (opened, found int, err error) { - if h.Records == nil { - return 0, 0, nil - } - if done, err := h.Records.SwitchDone(); err != nil || done { - return 0, 0, err - } - defer func() { - if err == nil { - err = h.Records.MarkSwitched(process.ReadOwnBuild(os.Getenv).String()) - } - }() - groups, err := h.inFlight() - if err != nil { - return 0, 0, err - } - for _, g := range groups { - var merge *Merge - if h.Merges != nil { - if merge, err = h.Merges.Get(g.milestone); err != nil { - return opened, found, fmt.Errorf("the merge of %s cannot be read for the switch: %w", g.milestone, err) - } - } - heads := g.heads - if merge != nil && len(merge.Approved) > 0 { - heads = merge.Approved - } - r, made, err := h.Records.Open(g.milestone, heads, false) - if err != nil { - return opened, found, fmt.Errorf("the switch cannot open the record of %s: %w", g.milestone, err) - } - if made { - opened++ - } else { - found++ - } - if merge != nil { - if err := h.Records.SetGroup(r, recordGroupOf(*merge, g.members)); err != nil { - return opened, found, fmt.Errorf("the switch cannot carry the merge of %s: %w", g.milestone, err) - } - } - } - if err := h.carryOwed(); err != nil { - return opened, found, err - } - err = h.carryApprovals() - return opened, found, err -} - -// oldMerge is a group's merge as the old state merges holds it, with its key and revision; nil when it holds none. -func (h *Holder) oldMerge(group string) (*Merge, uint64, error) { - if h.Old == nil { - return nil, 0, nil - } - key, err := mdc.MergesKey(group) - if err != nil { - return nil, 0, err - } - raw, rev, err := h.Old.Get(mdc.SwitchedMerges, key) - if err != nil || raw == nil { - return nil, 0, err - } - m, err := DecodeMerge(raw) - if err != nil { - return nil, 0, err - } - return &m, rev, nil -} - -// carryOwed says every act the old state owed holds as owed, under its switched id (the state, the key and its revision: -// meshdeliveryclient.SwitchedActID), so the forge bridge performs what the old bridge did not and counts as performed what -// it did (design 47 G §169). No merge act is made from the old state merges (G §167, §542). Said at every switch, it is the same act each time. A key that cannot be read as an act is said -// as refused and skipped. -func (h *Holder) carryOwed() error { - if h.Old == nil || h.Owed == nil { - return nil - } - now := h.Now().UTC() - keys, err := h.Old.Keys(mdc.SwitchedOwed) - if err != nil { - return fmt.Errorf("the state owed cannot be read for the switch: %w", err) - } - for _, key := range keys { - raw, rev, err := h.Old.Get(mdc.SwitchedOwed, key) - if err != nil { - return fmt.Errorf("the owed act %s cannot be read for the switch: %w", key, err) - } - if raw == nil { - continue - } - repository, number, head, _, kerr := mdc.ParseOwedKey(key) - var o Owed - id, ierr := mdc.SwitchedActID(mdc.SwitchedOwed, key, rev) - if derr := json.Unmarshal(raw, &o); kerr != nil || derr != nil || ierr != nil { - h.Refuse(fmt.Errorf("the owed act %s is not carried by the switch: %v", key, errors.Join(kerr, derr, ierr))) - continue - } - said := seatOwedOf(o, id, repository, number, head, &now) - if r := h.recordHolding(repository, head); r != nil { - err = h.owe(r, said) - } else { - err = h.Owed.Say(said) - } - if err != nil { - return fmt.Errorf("the owed act %s cannot be said for the switch: %w", key, err) - } - } - return nil -} - -// recordHolding is the open record that holds a pull request of the repository at the head, nil when none does. -func (h *Holder) recordHolding(repository, head string) *Record { - if h.Records == nil { - return nil - } - current, err := h.Records.Current() - if err != nil { - return nil - } - for _, r := range current { - for _, m := range membersOf(r) { - if strings.EqualFold(m.Repository, repository) && sameCommit(m.Head, head) { - return r - } - } - } - return nil -} - -// approver is who an Approve carried across the switch was given by (design 47 §542). -const approver = "the operator" - -// carryApprovals says, for each key of the old section merges the old bridge had not performed, `approval-carried` of each -// pull request it had not merged, once (design 47 §542, §592): the runner takes it as that pull request's `approval` step. -// The keys stay, read no more. Said at every switch, it is the same event under the same id each time, which the bus keeps once. -func (h *Holder) carryApprovals() error { - if h.Old == nil || h.SaySeat == nil { - return nil - } - names, err := h.Old.Keys(mdc.SwitchedMerges) - if err != nil { - return fmt.Errorf("the old section merges cannot be read for the switch: %w", err) - } - now := h.Now().UTC() - for _, key := range names { - raw, _, err := h.Old.Get(mdc.SwitchedMerges, key) - if err != nil { - return fmt.Errorf("the old merge %s cannot be read for the switch: %w", key, err) - } - if raw == nil { - continue - } - m, err := DecodeMerge(raw) - if err != nil { - h.Refuse(fmt.Errorf("the old merge %s is not carried by the switch: %v", key, err)) - continue - } - for approved, head := range m.Approved { - repository, number, ok := parseApprovedKey(approved) - if !ok || mergedIn(m, repository, number) { - continue - } - body := seat.ApprovalCarriedEvent{Approver: approver, At: now, Commit: head, Pull: int64(number), Repository: repository} - subject := seat.SubjectApprovalCarried(keys.EscapeName(repository), fmt.Sprint(number)) - if err := h.SaySeat(subject, body, fmt.Sprintf("approval-carried.%s.%d", repository, number)); err != nil { - return fmt.Errorf("the approval of %s#%d cannot be said carried: %w", repository, number, err) - } - if err := h.approvalOpen(repository, number, head); err != nil { - return fmt.Errorf("the finding of the approval of %s#%d cannot be written: %w", repository, number, err) - } - } - } - return nil -} - -// mergedIn is whether a merge's pull request of that repository and number was merged. -func mergedIn(m Merge, repository string, number int) bool { - for _, p := range m.Pulls { - if strings.EqualFold(p.Repository, repository) && p.Number == number && p.State == mdc.MergeMerged { - return true - } - } - return false -} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/values.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/values.go deleted file mode 100644 index b1de5f8ce..000000000 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/values.go +++ /dev/null @@ -1,130 +0,0 @@ -package main - -import ( - "encoding/json" - "reflect" - - mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" -) - -// The values the gate keeps in the seat mesh-delivery's states. The seat's types are the layout and meshdeliveryclient -// holds their rules and codecs; Source, Owed and Answer are used as they are, and the three whose seat form differs from the -// form the gate works in (a pull request that holds its head, the router's ask as bytes, a turn's times as plain times) -// are converted at the edge, so a stored value decodes unchanged (novox/hq ADR 0634 §2). -type ( - // Source is the state source's value. - Source = seat.SourceByBranchValue - // Owed is the state owed's value: what the forge bridge performs. - Owed = seat.OwedByRepositoryNumberHeadActValue - // Answer is the operator's answer as the gate acts on it. - Answer = mdc.Answer -) - -// Merge is the state merges' value: a group's merge. -type Merge struct { - V int - Group string - Ask string - Approved map[string]string - Pulls []MergePull -} - -// MergePull is one pull request of a merge: its expected head, its state, the merge commit once merged, the forge's words -// when refused. -type MergePull struct { - mdc.Head - State mdc.MergeState - Commit string - Words string -} - -func (m Merge) stored() seat.MergesByGroupValue { - out := seat.MergesByGroupValue{Approved: m.Approved, Ask: m.Ask, Group: m.Group, V: int64(m.V)} - if m.Pulls != nil { - out.Pulls = make([]seat.MergesByGroupValuePullsItem, len(m.Pulls)) - } - for i, p := range m.Pulls { - out.Pulls[i] = seat.MergesByGroupValuePullsItem{Commit: p.Commit, Head: p.Head.Head, Number: int64(p.Number), - Repository: p.Repository, State: string(p.State), Words: p.Words} - } - return out -} - -func mergeFromSeat(s seat.MergesByGroupValue) Merge { - out := Merge{V: int(s.V), Group: s.Group, Ask: s.Ask, Approved: s.Approved} - for _, p := range s.Pulls { - out.Pulls = append(out.Pulls, MergePull{Head: mdc.Head{Repository: p.Repository, Number: int(p.Number), Head: p.Head}, - State: mdc.MergeState(p.State), Commit: p.Commit, Words: p.Words}) - } - return out -} - -// EncodeMerge is m as the value of a merges key. -func EncodeMerge(m Merge) ([]byte, error) { return mdc.EncodeMerge(m.stored()) } - -// DecodeMerge reads the value of a merges key. -func DecodeMerge(data []byte) (Merge, error) { - s, err := mdc.DecodeMerge(data) - if err != nil { - return Merge{}, err - } - return mergeFromSeat(s), nil -} - -// SameMerge says whether two merges are the same value. -func SameMerge(a, b Merge) bool { return mdc.SameMerge(a.stored(), b.stored()) } - -// Ask is the state asks' value: the router's ask for a group's merge at its heads. -type Ask struct { - V int - Group string - Heads []mdc.Head - Ask json.RawMessage - // Asking counts the times the gate wrote this group's ask at these heads (0 or 1 for the first). - Asking int -} - -func (a Ask) stored() seat.AsksByMergeGroupHeadsValue { - out := seat.AsksByMergeGroupHeadsValue{Asking: int64(a.Asking), Group: a.Group, V: int64(a.V)} - if len(a.Ask) > 0 { - out.Ask = a.Ask - } - if a.Heads != nil { - out.Heads = make([]seat.AsksByMergeGroupHeadsValueHeadsItem, len(a.Heads)) - } - for i, h := range a.Heads { - out.Heads[i] = seat.AsksByMergeGroupHeadsValueHeadsItem{Head: h.Head, Number: int64(h.Number), Repository: h.Repository} - } - return out -} - -func askFromSeat(s seat.AsksByMergeGroupHeadsValue) (Ask, error) { - raw, err := json.Marshal(s.Ask) - if err != nil { - return Ask{}, err - } - return Ask{V: int(s.V), Group: s.Group, Heads: mdc.HeadsOf(s.Heads), Ask: raw, Asking: int(s.Asking)}, nil -} - -// EncodeAsk is a as the value of an asks key. -func EncodeAsk(a Ask) ([]byte, error) { return mdc.EncodeAsk(a.stored()) } - -// DecodeAsk reads the value of an asks key. -func DecodeAsk(data []byte) (Ask, error) { - s, err := mdc.DecodeAsk(data) - if err != nil { - return Ask{}, err - } - return askFromSeat(s) -} - -// SameAsk says whether two asks are the same value, the router's ask by its content. -func SameAsk(a, b Ask) bool { - var x, y any - if json.Unmarshal(a.Ask, &x) != nil || json.Unmarshal(b.Ask, &y) != nil { - return false - } - a.Ask, b.Ask = nil, nil - return reflect.DeepEqual(a, b) && reflect.DeepEqual(x, y) -} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/values_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/values_test.go deleted file mode 100644 index d1b22ce26..000000000 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/values_test.go +++ /dev/null @@ -1,68 +0,0 @@ -package main - -import ( - "encoding/json" - "reflect" - "testing" - - mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" -) - -// What go/delivery wrote into the seat's states, key order and all, must decode as it did and encode to the same -// content (ADR 0634 §2): the gate's working values are converted to the seat's types at the edge. -func TestStoredValuesOfTheEarlierLayoutDecodeUnchanged(t *testing.T) { - head := mdc.Head{Repository: "novox/app", Number: 7, Head: "0123456789abcdef0123456789abcdef01234567"} - rows := []struct { - name string - stored string - decode func(string) (any, error) - encode func(any) ([]byte, error) - want any - }{ - {"merge with a merged and a refused pull", `{"v":1,"group":"feat/x","ask":"merge.feat_x.0123456789ab","approved":{"novox/app@7":"` + head.Head + `","novox/web@8":"abc"},` + - `"pulls":[{"repository":"novox/app","number":7,"head":"` + head.Head + `","state":"merged","commit":"c0ffee"},` + - `{"repository":"novox/web","number":8,"head":"abc","state":"refused","words":"mesh/check (missing)"}]}`, - func(s string) (any, error) { return DecodeMerge([]byte(s)) }, func(v any) ([]byte, error) { return EncodeMerge(v.(Merge)) }, - Merge{V: 1, Group: "feat/x", Ask: "merge.feat_x.0123456789ab", Approved: map[string]string{"novox/app@7": head.Head, "novox/web@8": "abc"}, Pulls: []MergePull{ - {Head: head, State: mdc.MergeMerged, Commit: "c0ffee"}, - {Head: mdc.Head{Repository: "novox/web", Number: 8, Head: "abc"}, State: mdc.MergeRefused, Words: "mesh/check (missing)"}}}}, - {"ask keeps the router's ask as bytes", `{"v":1,"group":"feat/x","heads":[{"repository":"novox/app","number":7,"head":"` + head.Head + `"}],"ask":{"id":"a","options":["Approve"]},"asking":2}`, - func(s string) (any, error) { return DecodeAsk([]byte(s)) }, func(v any) ([]byte, error) { return EncodeAsk(v.(Ask)) }, - Ask{V: 1, Group: "feat/x", Heads: []mdc.Head{head}, Ask: json.RawMessage(`{"id":"a","options":["Approve"]}`), Asking: 2}}, - {"source", `{"v":1,"group":"feat/x","heads":{"novox/app":"c0ffee"}}`, - func(s string) (any, error) { return mdc.DecodeSource([]byte(s)) }, func(v any) ([]byte, error) { return mdc.EncodeSource(v.(Source)) }, - Source{V: 1, Group: "feat/x", Heads: map[string]string{"novox/app": "c0ffee"}}}, - {"owed status", `{"v":1,"act":"status","context":"mesh/delivery","target":"https://x/y","state":"pending"}`, - func(s string) (any, error) { return mdc.DecodeOwed([]byte(s)) }, func(v any) ([]byte, error) { return mdc.EncodeOwed(v.(Owed)) }, - Owed{V: 1, Act: "status", Context: "mesh/delivery", Target: "https://x/y", State: "pending"}}, - {"answer", `{"v":1,"option":"Approve","warrant":"w@2026-11-05T10:00:00Z","words":"chose Approve"}`, - func(s string) (any, error) { return mdc.DecodeAnswer([]byte(s)) }, func(v any) ([]byte, error) { return mdc.EncodeAnswer(v.(Answer)) }, - Answer{V: 1, Option: "Approve", Warrant: "w@2026-11-05T10:00:00Z", Words: "chose Approve"}}, - } - for _, r := range rows { - t.Run(r.name, func(t *testing.T) { - got, err := r.decode(r.stored) - if err != nil { - t.Fatalf("the earlier layout does not decode: %v", err) - } - if !sameValue(got, r.want) { - t.Fatalf("decoded %+v, want %+v", got, r.want) - } - raw, err := r.encode(got) - if err != nil { - t.Fatalf("encode: %v", err) - } - var a, b any - if json.Unmarshal(raw, &a) != nil || json.Unmarshal([]byte(r.stored), &b) != nil || !reflect.DeepEqual(a, b) { - t.Fatalf("encoded %s, the earlier layout was %s", raw, r.stored) - } - }) - } -} - -func sameValue(a, b any) bool { - if x, ok := a.(Ask); ok { - return SameAsk(x, b.(Ask)) - } - return reflect.DeepEqual(a, b) -} diff --git a/modules/mesh-delivery-gate/go.mod b/modules/mesh-delivery-gate/go.mod index 3bf41633a..213a047f8 100644 --- a/modules/mesh-delivery-gate/go.mod +++ b/modules/mesh-delivery-gate/go.mod @@ -7,7 +7,6 @@ require ( git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient 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/git 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-gate/module.json b/modules/mesh-delivery-gate/module.json index ebddcb329..75af30f9a 100644 --- a/modules/mesh-delivery-gate/module.json +++ b/modules/mesh-delivery-gate/module.json @@ -282,27 +282,6 @@ "key": "instance" } ] - }, - { - "name": "switch", - "schema": { - "fingerprint": { - "type": "string" - }, - "at": { - "type": "string" - } - }, - "operations": [ - { - "name": "done", - "effect": "set-fields", - "fields": [ - "fingerprint", - "at" - ] - } - ] } ] }, @@ -316,14 +295,6 @@ "storage": "key-value", "template": "state" }, - { - "name": "owed", - "storage": "key-value", - "template": "state", - "options": { - "history": 5 - } - }, { "name": "conditions", "storage": "key-value", @@ -362,10 +333,6 @@ "seat:node-engine.composition" ], "state": [ - { - "name": "merges", - "history": 10 - }, { "name": "source", "history": 64 -- 2.54.0 From d901c6c19d943142ff51de325e4e20d356ffcd91 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 02:36:11 +0100 Subject: [PATCH 02/43] GATE-2: the gate's groups from branch names and its pull request states go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A merge call is the delivery's group from H1, so the gate no longer keeps deliveries and groups from the seat git's pull events, reads no open pull request at its start, and answers deliveries and show from the records alone. The verb groups stays, answered from the records, until the seat mesh-delivery's definition drops it. Builds: design 47 H §764 (the gate: the groups from a branch name, its states deliveries and groups, its consumption of pull-updated, pull-merged and pull-closed, its start reconciliation); H-A-28 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/mesh-delivery-gate/deliveries.go | 462 ------------------ .../cmd/mesh-delivery-gate/deliveries_test.go | 245 ---------- .../cmd/mesh-delivery-gate/findings.go | 14 +- .../cmd/mesh-delivery-gate/gate.go | 4 +- .../cmd/mesh-delivery-gate/keeps.go | 2 +- .../cmd/mesh-delivery-gate/keeps_test.go | 7 +- .../cmd/mesh-delivery-gate/main.go | 16 +- .../cmd/mesh-delivery-gate/manifest_test.go | 44 +- .../cmd/mesh-delivery-gate/sourcestart.go | 26 + .../mesh-delivery-gate/sourcetools_test.go | 29 ++ .../cmd/mesh-delivery-gate/takenin.go | 25 +- .../cmd/mesh-delivery-gate/verbs.go | 66 +-- modules/mesh-delivery-gate/module.json | 15 - 13 files changed, 106 insertions(+), 849 deletions(-) delete mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries.go delete mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries_test.go diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries.go deleted file mode 100644 index d1363a7d2..000000000 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries.go +++ /dev/null @@ -1,462 +0,0 @@ -package main - -// The gate keeps a group's members from their announcement (design 47 G §716; A-279, A-236): its states `deliveries` and -// `groups`, written from the seat git's pull-updated, pull-merged and pull-closed, deleted when a record carries a member (the -// take-in, takenin.go) or its pull request closes unmerged, and put right at the gate's start from the seat git's open-pulls and -// pull. A delivery's key is its id `@` escaped as a key name; a group's key is its head branch -// escaped as a key name. Only the gate writes either state. - -import ( - "encoding/json" - "errors" - "fmt" - "sort" - "strings" - - gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" - stdio "git.novox.be/novox/mesh-sdk/go" - "git.novox.be/novox/mesh-sdk/go/keys" -) - -// The two states, by their names in the gate's manifest. -const ( - deliveriesState = "deliveries" - groupsState = "groups" -) - -// The states of a delivery the gate writes; any other state an old table kept counts open (§716). -const ( - deliveryOpen = "open" - deliveryMerged = "merged" -) - -// Delivery is one pull request as the state `deliveries` keeps it. -type Delivery struct { - ID string `json:"id"` - Repository string `json:"repository"` - Number int `json:"number"` - Commit string `json:"commit"` - // Base is the pull request's base branch as the seat git says it (A-279); Branch its head branch, which names its group. - Base string `json:"base,omitempty"` - Branch string `json:"branch,omitempty"` - Group string `json:"group,omitempty"` - State string `json:"state"` -} - -// openOrMerged is a delivery's state as the gate reads it: merged, or else open. -func (d Delivery) openOrMerged() string { - if d.State == deliveryMerged { - return deliveryMerged - } - return deliveryOpen -} - -// keptGroup is one key of the state `groups`: the group, its head branch, and its members' delivery ids. -type keptGroup struct { - Group string `json:"group"` - Members []string `json:"members"` -} - -// DeliveryStore is the gate's two states over the runtime. -type DeliveryStore interface { - Keys(state string) ([]string, error) - Get(state, key string) ([]byte, error) - Put(state, key string, value any) error - Delete(state, key string) error -} - -// ForgeReads is what the gate reads of the seat git: open-pulls and pull. -type ForgeReads interface { - OpenPulls() (gitseat.OpenPullsAnswer, error) - Pull(repository string, number int64) (gitseat.PullAnswer, error) - // Branch is a repository branch's head (the seat git's verb branch). - Branch(repository, branch string) (gitseat.BranchAnswer, error) - // Contains is whether commit is in (or is) the commit in (the seat git's verb contains). - Contains(repository, commit, in string) (bool, error) -} - -// kvKept is the store over the runtime. -type kvKept struct{} - -func (kvKept) Keys(state string) ([]string, error) { return stdio.State(state).Keys() } - -func (kvKept) Get(state, key string) ([]byte, error) { - e, err := stdio.State(state).Get(key) - if err != nil || e == nil { - return nil, err - } - return e.Value, nil -} - -func (kvKept) Put(state, key string, value any) error { - _, err := stdio.State(state).Put(key, value) - return err -} - -func (kvKept) Delete(state, key string) error { return stdio.State(state).Delete(key) } - -// seatGit reads the seat git through the tool runner. -type seatGit struct{ ask asking } - -func (s seatGit) call(verb string, in any, out any) error { - raw, err := s.ask("seat:"+gitseat.Seat+"."+verb, in) - if err != nil { - return err - } - answer, said, ok := answerOf(raw) - if !ok { - return fmt.Errorf("%s refused: %s", verb, lastLine(said)) - } - if err := json.Unmarshal(answer, out); err != nil { - return fmt.Errorf("the answer of %s is not readable: %w", verb, err) - } - return nil -} - -func (s seatGit) OpenPulls() (gitseat.OpenPullsAnswer, error) { - var a gitseat.OpenPullsAnswer - return a, s.call(gitseat.VerbOpenPulls, gitseat.OpenPullsInput{}, &a) -} - -func (s seatGit) Pull(repository string, number int64) (gitseat.PullAnswer, error) { - var a gitseat.PullAnswer - return a, s.call(gitseat.VerbPull, gitseat.PullInput{Repository: repository, Number: number}, &a) -} - -func deliveryKey(id string) string { return keys.EscapeName(id) } - -func samePull(d Delivery, repository string, number int) bool { - return strings.EqualFold(d.Repository, repository) && d.Number == number -} - -// deliveries are every delivery the state keeps by its key; a key that is no delivery (a mark an earlier owner kept) is left. -func (h *Holder) deliveries() (map[string]Delivery, error) { - if h.Kept == nil { - return nil, errors.New("this gate keeps no state deliveries") - } - names, err := h.Kept.Keys(deliveriesState) - if err != nil { - return nil, fmt.Errorf("the state deliveries cannot be read: %w", err) - } - out := map[string]Delivery{} - for _, k := range names { - raw, err := h.Kept.Get(deliveriesState, k) - if err != nil { - return nil, fmt.Errorf("the delivery %s cannot be read: %w", k, err) - } - var d Delivery - if raw == nil || json.Unmarshal(raw, &d) != nil || d.ID == "" || d.Number == 0 { - continue - } - out[k] = d - } - return out, nil -} - -// keptGroups are every group the state keeps by its key. -func (h *Holder) keptGroups() (map[string]keptGroup, error) { - if h.Kept == nil { - return nil, errors.New("this gate keeps no state groups") - } - names, err := h.Kept.Keys(groupsState) - if err != nil { - return nil, fmt.Errorf("the state groups cannot be read: %w", err) - } - out := map[string]keptGroup{} - for _, k := range names { - raw, err := h.Kept.Get(groupsState, k) - if err != nil { - return nil, fmt.Errorf("the group %s cannot be read: %w", k, err) - } - var g keptGroup - if raw == nil || json.Unmarshal(raw, &g) != nil || g.Group == "" { - continue - } - out[k] = g - } - return out, nil -} - -// PullUpdated takes one pull-updated: the delivery is written under its id with its base and head branch, and a newer head of the -// same pull request takes the older's place, in its group too. -func (h *Holder) PullUpdated(body []byte) error { - var e gitseat.PullUpdatedEvent - if err := json.Unmarshal(body, &e); err != nil || e.Repository == "" || e.Number <= 0 || e.Head == "" { - h.Refuse(fmt.Errorf("a pull-updated of the seat git is not readable: %v", err)) - return nil - } - h.dmu.Lock() - defer h.dmu.Unlock() - return h.writeDelivery(Delivery{ID: IDOf(e.Repository, e.Head), Repository: e.Repository, Number: int(e.Number), Commit: e.Head, - Base: e.Base, Branch: e.Branch, State: deliveryOpen}) -} - -// writeDelivery writes a delivery in place of any other head of its pull request and puts the groups right; under dmu. -func (h *Holder) writeDelivery(d Delivery) error { - all, err := h.deliveries() - if err != nil { - return err - } - branches := map[string]bool{d.Branch: true} - key := deliveryKey(d.ID) - for k, o := range all { - if k == key || !samePull(o, d.Repository, d.Number) { - continue - } - if err := h.Kept.Delete(deliveriesState, k); err != nil { - return fmt.Errorf("the older delivery %s cannot be deleted: %w", o.ID, err) - } - delete(all, k) - branches[o.Branch] = true - } - if o, ok := all[key]; ok { - d.State = o.openOrMerged() - d.Group = o.Group - } - if err := h.Kept.Put(deliveriesState, key, d); err != nil { - return fmt.Errorf("the delivery %s cannot be written: %w", d.ID, err) - } - return h.refreshGroups(branches) -} - -// DeliveryMerged takes one pull-merged: the delivery of that pull request is merged. A pull request the gate keeps no delivery of is -// left: nothing would delete it. -func (h *Holder) DeliveryMerged(body []byte) error { - var e gitseat.PullMergedEvent - if err := json.Unmarshal(body, &e); err != nil || e.Repository == "" || e.Number <= 0 { - h.Refuse(fmt.Errorf("a pull-merged of the seat git is not readable: %v", err)) - return nil - } - h.dmu.Lock() - defer h.dmu.Unlock() - return h.setMerged(e.Repository, int(e.Number), e.Base) -} - -// setMerged sets the state of a pull request's delivery to merged; under dmu. -func (h *Holder) setMerged(repository string, number int, base string) error { - all, err := h.deliveries() - if err != nil { - return err - } - for k, d := range all { - if !samePull(d, repository, number) || d.State == deliveryMerged { - continue - } - d.State = deliveryMerged - if base != "" { - d.Base = base - } - if err := h.Kept.Put(deliveriesState, k, d); err != nil { - return fmt.Errorf("the delivery %s cannot be set merged: %w", d.ID, err) - } - } - return nil -} - -// DeliveryClosed takes one pull-closed: the delivery of a pull request not merged is deleted, and its group put right. -func (h *Holder) DeliveryClosed(body []byte) error { - var e gitseat.PullClosedEvent - if err := json.Unmarshal(body, &e); err != nil || e.Repository == "" || e.Number <= 0 { - h.Refuse(fmt.Errorf("a pull-closed of the seat git is not readable: %v", err)) - return nil - } - h.dmu.Lock() - defer h.dmu.Unlock() - return h.dropUnmerged(e.Repository, int(e.Number)) -} - -// dropUnmerged deletes the deliveries of a pull request that are not merged and puts their groups right; under dmu. -func (h *Holder) dropUnmerged(repository string, number int) error { - all, err := h.deliveries() - if err != nil { - return err - } - branches := map[string]bool{} - for k, d := range all { - if !samePull(d, repository, number) || d.State == deliveryMerged { - continue - } - if err := h.Kept.Delete(deliveriesState, k); err != nil { - return fmt.Errorf("the delivery %s cannot be deleted: %w", d.ID, err) - } - branches[d.Branch] = true - } - return h.refreshGroups(branches) -} - -// recordGroupsHeld are the group ids the delivery records carry, ended or not. -func (h *Holder) recordGroupsHeld() (map[string]bool, error) { - out := map[string]bool{} - if h.Records == nil { - return out, nil - } - kept, err := h.Records.Kept() - if err != nil { - return nil, fmt.Errorf("the delivery records cannot be read for the groups: %w", err) - } - for _, r := range kept { - if r.Group != nil && r.Group.Group != "" && len(membersOf(r)) > 0 { - out[r.Group.Group] = true - } - } - return out, nil -} - -// refreshGroups writes, for each head branch, the group key of its deliveries and their `group`: pull requests with one head branch -// form one group; a group of one with no member in a record is no group, its key deleted and its member's `group` cleared; a group -// key once no member remains outside a record is deleted (§716). Under dmu. -func (h *Holder) refreshGroups(branches map[string]bool) error { - inRecords, err := h.recordGroupsHeld() - if err != nil { - return err - } - all, err := h.deliveries() - if err != nil { - return err - } - for branch := range branches { - if branch == "" { - continue - } - var members []string - keysOf := map[string]string{} - for k, d := range all { - if d.Branch == branch { - members = append(members, d.ID) - keysOf[d.ID] = k - } - } - sort.Strings(members) - gkey := keys.EscapeName(branch) - isGroup := len(members) > 1 || len(members) == 1 && inRecords[branch] - if !isGroup { - if err := h.Kept.Delete(groupsState, gkey); err != nil { - return fmt.Errorf("the group %s cannot be deleted: %w", branch, err) - } - } else if err := h.Kept.Put(groupsState, gkey, keptGroup{Group: branch, Members: members}); err != nil { - return fmt.Errorf("the group %s cannot be written: %w", branch, err) - } - for _, id := range members { - k := keysOf[id] - d := all[k] - want := "" - if isGroup { - want = branch - } - if d.Group != want { - d.Group = want - all[k] = d - if err := h.Kept.Put(deliveriesState, k, d); err != nil { - return fmt.Errorf("the delivery %s cannot be put in its group: %w", id, err) - } - } - } - } - return nil -} - -// ReadForge puts right what a missed pull event left, once at the gate's start (§716, after §613's copy): each open pull request -// the seat git's open-pulls says that the gate lacks, or keeps at another head, is written; each delivery kept open that the answer -// does not name is read by pull: merged sets merged, closed deletes it. A read that fails is said in the journal and made again at -// the next start. -func (h *Holder) ReadForge() error { - if h.Forge == nil || h.Kept == nil { - return nil - } - answer, err := h.Forge.OpenPulls() - if err != nil { - return fmt.Errorf("open-pulls: %w", err) - } - if answer.Error != "" { - return fmt.Errorf("open-pulls: %s", answer.Error) - } - h.dmu.Lock() - defer h.dmu.Unlock() - listed := map[string]bool{} - var failed []error - for _, o := range answer.Pulls { - p := o.Pull - if p.Repository == "" || p.Number <= 0 || p.Head == "" { - continue - } - listed[strings.ToLower(p.Repository)+"#"+fmt.Sprint(p.Number)] = true - all, err := h.deliveries() - if err != nil { - return err - } - if d, ok := all[deliveryKey(IDOf(p.Repository, p.Head))]; ok && d.Base == p.Base && d.Branch == p.Branch { - continue - } - if err := h.writeDelivery(Delivery{ID: IDOf(p.Repository, p.Head), Repository: p.Repository, Number: int(p.Number), Commit: p.Head, - Base: p.Base, Branch: p.Branch, State: deliveryOpen}); err != nil { - failed = append(failed, err) - } - } - all, err := h.deliveries() - if err != nil { - return err - } - for _, d := range all { - if d.openOrMerged() != deliveryOpen || listed[strings.ToLower(d.Repository)+"#"+fmt.Sprint(d.Number)] { - continue - } - got, err := h.Forge.Pull(d.Repository, int64(d.Number)) - if err == nil && got.Error != "" { - err = errors.New(got.Error) - } - if err == nil && got.Pull == nil { - err = errors.New("the answer names no pull request") - } - if err != nil { - h.Logf("[mesh-delivery] the pull request %s#%d cannot be read at the start: %v", d.Repository, d.Number, err) - failed = append(failed, err) - continue - } - switch got.Pull.State { - case gitseat.PullStateMerged: - err = h.setMerged(d.Repository, d.Number, got.Pull.Base) - case gitseat.PullStateClosed: - err = h.dropUnmerged(d.Repository, d.Number) - } - if err != nil { - failed = append(failed, err) - } - } - return errors.Join(failed...) -} - -// baseOf is the base of a pull request's delivery, and whether the gate keeps one. -func baseOf(all map[string]Delivery, repository string, number int) (string, bool) { - for _, d := range all { - if samePull(d, repository, number) { - return d.Base, true - } - } - return "", false -} - -// carried puts the deliveries of the pull requests a record carries away, once the record holds them with their base: each delivery -// is deleted and the groups put right. A deletion is made in the old section too (§613): the state's keys are the one table. -func (h *Holder) carried(pulls []RecordPull) error { - if h.Kept == nil { - return nil - } - h.dmu.Lock() - defer h.dmu.Unlock() - all, err := h.deliveries() - if err != nil { - return err - } - branches := map[string]bool{} - for k, d := range all { - for _, p := range pulls { - if samePull(d, p.Repository, p.Number) { - if err := h.Kept.Delete(deliveriesState, k); err != nil { - return fmt.Errorf("the delivery %s cannot be deleted: %w", d.ID, err) - } - branches[d.Branch] = true - } - } - } - return h.refreshGroups(branches) -} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries_test.go deleted file mode 100644 index ba49f95cf..000000000 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/deliveries_test.go +++ /dev/null @@ -1,245 +0,0 @@ -package main - -import ( - "encoding/json" - "errors" - "fmt" - "sort" - "strings" - "testing" - "time" - - gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" -) - -// memKept is the gate's states deliveries and groups in memory. -type memKept struct{ m map[string]map[string][]byte } - -func (k *memKept) Keys(state string) ([]string, error) { - var out []string - for key := range k.m[state] { - out = append(out, key) - } - sort.Strings(out) - return out, nil -} -func (k *memKept) Get(state, key string) ([]byte, error) { return k.m[state][key], nil } -func (k *memKept) Put(state, key string, v any) error { - if k.m == nil { - k.m = map[string]map[string][]byte{} - } - if k.m[state] == nil { - k.m[state] = map[string][]byte{} - } - raw, _ := json.Marshal(v) - k.m[state][key] = raw - return nil -} -func (k *memKept) Delete(state, key string) error { delete(k.m[state], key); return nil } - -// fakeForge is the seat git's reads: the open pull requests and each pull request by its number. -type fakeForge struct { - open []gitseat.OpenPull - pulls map[string]gitseat.Pull - heads map[string]string // "@" -> head - // contains says whether commit is in `in`; nil means every commit is in every other (a straight line forward) - contains func(commit, in string) bool - fail bool -} - -func (f *fakeForge) OpenPulls() (gitseat.OpenPullsAnswer, error) { - if f.fail { - return gitseat.OpenPullsAnswer{}, errors.New("the forge is away") - } - return gitseat.OpenPullsAnswer{Pulls: f.open}, nil -} -func (f *fakeForge) Branch(repository, branch string) (gitseat.BranchAnswer, error) { - if f.fail { - return gitseat.BranchAnswer{}, errors.New("the forge is away") - } - head, ok := f.heads[repository+"@"+branch] - if !ok { - return gitseat.BranchAnswer{Error: "no such branch"}, nil - } - return gitseat.BranchAnswer{Branch: branch, Repository: repository, Head: head}, nil -} -func (f *fakeForge) Contains(repository, commit, in string) (bool, error) { - if f.fail { - return false, errors.New("the forge is away") - } - if f.contains == nil { - return true, nil - } - return f.contains(commit, in), nil -} -func (f *fakeForge) Pull(repository string, number int64) (gitseat.PullAnswer, error) { - p, ok := f.pulls[fmt.Sprintf("%s#%d", repository, number)] - if !ok { - return gitseat.PullAnswer{Error: "no such pull request"}, nil - } - return gitseat.PullAnswer{Pull: &p}, nil -} - -func gateWithDeliveries(t *testing.T) (*Holder, *memKept) { - t.Helper() - k := &memKept{} - h := &Holder{Now: func() time.Time { return time.Date(2026, 11, 12, 10, 0, 0, 0, time.UTC) }, Kept: k, Logf: func(string, ...any) {}} - h.Records = &Records{Ledger: newLedgerFake(t), Now: h.Now} - return h, k -} - -func updated(repository string, number int, head, base, branch string) []byte { - raw, _ := json.Marshal(gitseat.PullUpdatedEvent{Repository: repository, Number: int64(number), Head: head, Base: base, Branch: branch}) - return raw -} - -func groupNamed(t *testing.T, h *Holder, id string) []string { - t.Helper() - a := h.Groups(false, id) - if a.Error != "" || len(a.Groups) != 1 { - t.Fatalf("groups %q: %+v", id, a) - } - var out []string - for _, m := range a.Groups[0].Members { - out = append(out, fmt.Sprintf("%s#%d@%s/%s/%s/%v", m.Repository, m.Number, m.Base, m.State, m.Head[:1], m.Open)) - } - return out -} - -// §716, A-279: a group read before any take-in names its members with their base, from the seat git's pull-updated. -func TestAGroupReadBeforeAnyTakeInNamesItsMembersWithTheirBase(t *testing.T) { - h, k := gateWithDeliveries(t) - a, b := strings.Repeat("a", 40), strings.Repeat("b", 40) - for _, body := range [][]byte{updated("novox/app", 3, a, "main", "feat"), updated("novox/lib", 4, b, "beta", "feat")} { - if err := h.PullUpdated(body); err != nil { - t.Fatal(err) - } - } - got := groupNamed(t, h, "feat") - want := []string{"novox/app#3@main/open/a/true", "novox/lib#4@beta/open/b/true"} - if strings.Join(got, " ") != strings.Join(want, " ") { - t.Fatalf("members %v, wanted %v", got, want) - } - if len(k.m[groupsState]) != 1 || len(k.m[deliveriesState]) != 2 { - t.Fatalf("states %v", k.m) - } - // The delivery's key is its id escaped as a key name, with its group set (§42). - var d Delivery - if err := json.Unmarshal(k.m[deliveriesState]["novox/app=40"+strings.Repeat("a", 12)], &d); err != nil || d.Group != "feat" || d.Base != "main" || d.State != "open" || d.Number != 3 { - t.Fatalf("the delivery %+v %v (keys %v)", d, err, k.m[deliveriesState]) - } -} - -// §716: a newer head of the same pull request takes the older's place in its group, and the older key is deleted. -func TestANewerHeadReplacesTheOlder(t *testing.T) { - h, k := gateWithDeliveries(t) - a, b, c := strings.Repeat("a", 40), strings.Repeat("b", 40), strings.Repeat("c", 40) - for _, body := range [][]byte{updated("novox/app", 3, a, "main", "feat"), updated("novox/lib", 4, b, "main", "feat"), updated("novox/app", 3, c, "main", "feat")} { - if err := h.PullUpdated(body); err != nil { - t.Fatal(err) - } - } - got := groupNamed(t, h, "feat") - if strings.Join(got, " ") != "novox/app#3@main/open/c/true novox/lib#4@main/open/b/true" || len(k.m[deliveriesState]) != 2 { - t.Fatalf("members %v, deliveries %d", got, len(k.m[deliveriesState])) - } -} - -// §716: a closed unmerged pull request leaves; a group of one with no member in a record is no group, its key deleted and the -// member's group cleared. -func TestAClosedUnmergedPullRequestLeavesItsGroup(t *testing.T) { - h, k := gateWithDeliveries(t) - a, b := strings.Repeat("a", 40), strings.Repeat("b", 40) - _ = h.PullUpdated(updated("novox/app", 3, a, "main", "feat")) - _ = h.PullUpdated(updated("novox/lib", 4, b, "main", "feat")) - closed, _ := json.Marshal(gitseat.PullClosedEvent{Repository: "novox/lib", Number: 4, Head: b, Base: "main", Branch: "feat"}) - if err := h.DeliveryClosed(closed); err != nil { - t.Fatal(err) - } - if len(k.m[groupsState]) != 0 || len(k.m[deliveriesState]) != 1 { - t.Fatalf("states %v", k.m) - } - for _, raw := range k.m[deliveriesState] { - var d Delivery - _ = json.Unmarshal(raw, &d) - if d.Group != "" || d.Number != 3 { - t.Fatalf("the member of a group of one keeps its group: %+v", d) - } - } -} - -// §716, A-279: the take-in copies the delivery's base onto the record's member and deletes the delivery; the member not taken in stays -// in the group, which names both. -func TestTheTakeInCopiesTheBaseAndKeepsTheRestOfTheGroup(t *testing.T) { - h, k := gateWithDeliveries(t) - merge := strings.Repeat("m", 40) - a, b := strings.Repeat("a", 40), strings.Repeat("b", 40) - _ = h.PullUpdated(updated("novox/app", 3, a, "main", "feat")) - _ = h.PullUpdated(updated("novox/lib", 4, b, "beta", "feat")) - merged, _ := json.Marshal(gitseat.PullMergedEvent{Repository: "novox/app", Number: 3, Head: a, Base: "main", Branch: "feat", MergeCommit: merge}) - if err := h.DeliveryMerged(merged); err != nil { - t.Fatal(err) - } - if got := groupNamed(t, h, "feat"); got[0] != "novox/app#3@main/merged/a/false" { - t.Fatalf("a merged member before its take-in: %v", got) - } - body := json.RawMessage(`{"v":1,"kind":"taken","key":"g.feat","what":"group","group":"feat","branch":"main","source":7,` + - `"merges":[{"repository":"novox/app","number":3,"merge":"` + merge + `"}],` + - `"entries":[{"module":"app","commit":"` + merge + `","fingerprint":"f","repository":"novox/app","pull":3}],"since":"2026-11-12T09:00:00Z"}`) - for i := 0; i < 2; i++ { // heard again: the base the record holds stays - if err := h.TakenIn(body); err != nil { - t.Fatal(err) - } - } - kept, _ := h.Records.Kept() - if len(kept) != 1 || kept[0].Group == nil || len(kept[0].Group.Pulls) != 1 || kept[0].Group.Pulls[0].Base != "main" { - t.Fatalf("the record's member lacks its base: %+v", kept) - } - if len(k.m[deliveriesState]) != 1 || len(k.m[groupsState]) != 1 { - t.Fatalf("states after the take-in: %v", k.m) - } - got := groupNamed(t, h, "feat") - if strings.Join(got, " ") != "novox/app#3@main/merged/m/false novox/lib#4@beta/open/b/true" && strings.Join(got, " ") != "novox/app#3@main/merged/a/false novox/lib#4@beta/open/b/true" { - t.Fatalf("members %v", got) - } -} - -// §716: a missed event is put right at the start: an open pull request the gate lacks is written, one kept at another head is -// replaced, and a kept delivery the answer does not name is read: merged is set, closed is deleted; a read that fails is -// said and left. -func TestAMissedEventIsPutRightAtStart(t *testing.T) { - h, k := gateWithDeliveries(t) - a, a2, b, c, d := strings.Repeat("a", 40), strings.Repeat("1", 40), strings.Repeat("b", 40), strings.Repeat("c", 40), strings.Repeat("d", 40) - _ = h.PullUpdated(updated("novox/app", 3, a, "main", "feat")) - _ = h.PullUpdated(updated("novox/gone", 5, c, "main", "feat")) - _ = h.PullUpdated(updated("novox/done", 6, d, "main", "feat")) - _ = h.PullUpdated(updated("novox/away", 7, d, "main", "other")) - h.Forge = &fakeForge{ - open: []gitseat.OpenPull{ - {Pull: gitseat.Pull{Repository: "novox/app", Number: 3, Head: a2, Base: "main", Branch: "feat"}}, - {Pull: gitseat.Pull{Repository: "novox/lib", Number: 4, Head: b, Base: "beta", Branch: "feat"}}}, - pulls: map[string]gitseat.Pull{ - "novox/gone#5": {Repository: "novox/gone", Number: 5, State: gitseat.PullStateClosed, Base: "main"}, - "novox/done#6": {Repository: "novox/done", Number: 6, State: gitseat.PullStateMerged, Base: "main"}}, - } - err := h.ReadForge() - if err == nil || !strings.Contains(err.Error(), "no such pull request") { - t.Fatalf("a pull request the forge does not know is said: %v", err) - } - all, _ := h.deliveries() - got := map[string]string{} - for _, d := range all { - got[fmt.Sprintf("%s#%d", d.Repository, d.Number)] = d.Commit[:1] + "/" + d.State + "/" + d.Base - } - want := map[string]string{"novox/app#3": "1/open/main", "novox/lib#4": "b/open/beta", "novox/done#6": "d/merged/main", "novox/away#7": "d/open/main"} - if fmt.Sprint(got) != fmt.Sprint(want) { - t.Fatalf("deliveries %v, wanted %v", got, want) - } - if len(k.m[groupsState]) != 1 { - t.Fatalf("groups %v", k.m[groupsState]) - } - h.Forge = &fakeForge{fail: true} - if err := h.ReadForge(); err == nil { - t.Fatal("a forge that cannot be read is said") - } -} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go index b5b1678f0..c769e9a4b 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go @@ -4,7 +4,6 @@ import ( "context" "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" - gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/views" ) @@ -15,17 +14,8 @@ import ( // Module is the gate's module name, the observer of its findings. const Module = "mesh-delivery-gate" -// The scope and the kind of the finding a carried approval nobody uses is (design 47 §592, A-42: the scope the mesh already uses for the delivery's findings). -const ( - FindingScope = "delivery" -) - -// pullMergedEvent and pullClosedEvent are the seat git's events that end a pull request, as the gate's manifest consumes them. -var ( - pullUpdatedEvent = declared(gitseat.EventPullUpdated) - pullMergedEvent = declared(gitseat.EventPullMerged) - pullClosedEvent = declared(gitseat.EventPullClosed) -) +// FindingScope is the scope of the gate's findings (A-42: the scope the mesh already uses for the delivery's findings). +const FindingScope = "delivery" // FindingWriter is what the gate holds of the one held writer (views.NewStdioHeldWriter over its state `findings`). type FindingWriter interface { diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go index 7522e6dc9..4c129bb92 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go @@ -20,13 +20,11 @@ type Holder struct { Settings func() Settings Now func() time.Time Logf func(format string, a ...any) - // Kept is the gate's states deliveries and groups, and Forge the seat git's reads (deliveries.go, §716). - Kept DeliveryStore + // Forge is the seat git's reads (sourcestart.go). Forge ForgeReads // Catalog reads the seat mesh-catalog's registrations (sourcestart.go); startMu keeps one start of the sources at a time. Catalog Registered startMu sync.Mutex - dmu sync.Mutex Source SourceStore // Owed says what the gate owes the forge as the seat's event owed (owed.go). Owed OwedSayer diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps.go index 7991c8974..d5f3a20db 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps.go @@ -6,7 +6,7 @@ import "git.novox.be/novox/mesh-sdk/go/announce" // issue 1790): the module says it itself, from its code, before it serves, and a reader is granted what it said // (clause 3). A state is readable when another module's manifest reads it (keeps_test.go names the readers). keeps_test.go holds the list to the // manifest's `state`, and keeps_integration_test.go that the module announces it on a bus. -var Kept = []announce.State{{Name: "conditions"}, {Name: "deliveries"}, {Name: "findings"}, {Name: "groups"}, {Name: "source", Readable: true}} +var Kept = []announce.State{{Name: "conditions"}, {Name: "findings"}, {Name: "source", Readable: true}} // announceKept says Kept through keeps, which is stdio.Keeps. func announceKept(keeps func(...announce.State) error) error { return keeps(Kept...) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go index 79bddbc3f..59cced012 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go @@ -68,9 +68,8 @@ func TestTheStatesTheModuleAnnouncesAreTheStatesItsManifestDeclares(t *testing.T } } -// Design 47 G §613 (A-20): deliveries, groups and conditions are `requires` entries of the template `state` under -// their local names, taking the buckets they already had; source stays in the old `state` section, and merges and the old -// owed go (design 47 H §764). +// Design 47 G §613 (A-20): conditions is a `requires` entry of the template `state` under its local name, taking the +// bucket it already had; source stays in the old `state` section; merges, the old owed, deliveries and groups go (design 47 H §764). func TestTheFourStatesMoveToTheTemplateAndTwoStayOld(t *testing.T) { raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { @@ -91,7 +90,7 @@ func TestTheFourStatesMoveToTheTemplateAndTwoStayOld(t *testing.T) { } old := string(raw[strings.Index(string(raw), `"state": [`):]) old = old[:strings.Index(old, "]")] - for _, n := range []string{"deliveries", "groups", "conditions"} { + for _, n := range []string{"conditions"} { if !moved[n] || strings.Contains(old, `"`+n+`"`) { t.Errorf("%s: template state %v, old section lists it %v", n, moved[n], strings.Contains(old, `"`+n+`"`)) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go index 66ea8c028..df8d272b5 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go @@ -32,7 +32,7 @@ func (l *listening) get() string { l.mu.Lock(); defer l.mu.Unlock(); return l.n // The events this owner follows: the forge bridge's word on an act owed, and the step events the delivery records are built // from (consumer.go). -var followed = append(append([]string{owedEndedEvent}, stepPatterns()...), pullUpdatedEvent, pullMergedEvent, pullClosedEvent, registrationChangedEvent, forgeMergedEvent) +var followed = append(append([]string{owedEndedEvent}, stepPatterns()...), registrationChangedEvent, forgeMergedEvent) func main() { settingsFile := os.Getenv("MESH_DELIVERY_GATE_SETTINGS") @@ -64,7 +64,6 @@ func main() { h.SaySeat = stdio.SeatPublish h.Records = &Records{Ledger: sdkLedger{}, Now: h.Now, Retention: h.recordRetention, Say: stdio.SeatPublish, Logf: logf} h.Records.Changed = h.recordChanged - h.Kept = kvKept{} h.Forge = seatGit{ask: stdio.Ask} h.Catalog = seatCatalog{ask: stdio.Ask} h.Consumer = &StepConsumer{Records: h.Records, Families: stepFamilies, Now: h.Now, Logf: logf, Taken: h.TakenIn} @@ -122,10 +121,6 @@ func run(h *Holder, l *listening) { logf("[mesh-delivery] the sources were not all compared with the forge's heads: %v", err) } }() - // A missed pull event is put right from the forge's own record, once at each start (§716). - if err := h.ReadForge(); err != nil { - logf("[mesh-delivery] the open pull requests were not read from the forge, they are read again at the next start: %v", err) - } } // statePins says every pin the records hold once at each start (design 47 G §579), retrying until it can. @@ -157,12 +152,6 @@ func take(h *Holder, e stdio.Envelope) error { if e.Key == owedEndedEvent { return h.OwedEnded(e.Body) } - if e.Key == pullUpdatedEvent { - return h.PullUpdated(e.Body) - } - if e.Key == pullMergedEvent { - return h.DeliveryMerged(e.Body) - } if e.Key == registrationChangedEvent { _, err := h.StartSources() return err @@ -170,9 +159,6 @@ func take(h *Holder, e stdio.Envelope) error { if forgeMergedEvent != "" && stdio.TopicMatches(forgeMergedEvent, e.Key) { return h.ForgeMerged(e.Body) } - if e.Key == pullClosedEvent { - return h.DeliveryClosed(e.Body) - } return nil } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go index beaa20794..b0542e949 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go @@ -9,6 +9,7 @@ import ( "sort" "strings" "testing" + "time" "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" "git.novox.be/novox/mesh-sdk/go/buscalls" @@ -150,10 +151,9 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { } } } - // A-30: the events its held writer says are declared in the manifest, and the two events that clear its finding are consumed. - if !strings.Contains(raw, `"finding-written.*"`) || !strings.Contains(raw, `"finding-cleared.*"`) || !strings.Contains(raw, `"findings-stated.*"`) || - pullMergedEvent == "" || pullClosedEvent == "" || pullUpdatedEvent == "" { - t.Fatalf("the finding events are not declared, or the pull events not consumed: %q %q", pullMergedEvent, pullClosedEvent) + // A-30: the events its held writer says are declared in the manifest. + if !strings.Contains(raw, `"finding-written.*"`) || !strings.Contains(raw, `"finding-cleared.*"`) || !strings.Contains(raw, `"findings-stated.*"`) { + t.Fatal("the finding events are not declared") } if !reflect.DeepEqual(m.Emits, []string{"transition"}) { // `group` and `merged` go with the merge states (§431) t.Fatalf("emits %v", m.Emits) @@ -184,7 +184,7 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { } // It calls no controller verb and no forge verb: the forge's reads and writes are the bridge's (ADR 0437 §8; design 47 §592). // It uses operator-channel only to say its conditions (ADR 0437 §9, issue 1275): the teller asks no more (§431). - want := []string{"seat:git.branch", "seat:git.open-pulls", "seat:git.pull", "seat:mesh-catalog.registrations", "seat:mesh-hand-acts.hand-act", WantsEntryVerb, EngineCompositionVerb} + want := []string{"seat:git.branch", "seat:mesh-catalog.registrations", "seat:mesh-hand-acts.hand-act", WantsEntryVerb, EngineCompositionVerb} if !reflect.DeepEqual(m.Uses, []string{"operator-channel"}) { t.Fatalf("uses %v: operator-channel, for its conditions alone", m.Uses) } @@ -343,3 +343,37 @@ func TestTheEarlierModelsStatesAndApprovalAreGone(t *testing.T) { } } } + +// GATE-2/AC2 (design 47 H §764): the per-pull-request model goes: the gate consumes no pull request event, keeps no +// `deliveries` or `groups`, reads no open pull request, and `deliveries` and `show` answer from the +// records alone. +func TestTheGroupsFromBranchNamesAreGone(t *testing.T) { + m, _ := readManifest(t) + for _, c := range m.Consumes { + if strings.HasPrefix(c, "git.pull-") { + t.Errorf("the manifest consumes %s", c) + } + } + for _, n := range []string{"deliveries", "groups"} { + if slices.Contains(m.withTemplateStates(), n) { + t.Errorf("the manifest still keeps %s", n) + } + } + for _, v := range []string{"seat:git.open-pulls", "seat:git.pull"} { + if slices.Contains(m.Invokes, v) { + t.Errorf("the manifest still invokes %s", v) + } + } + h := &Holder{Now: func() time.Time { return time.Date(2026, 11, 20, 10, 0, 0, 0, time.UTC) }, Logf: func(string, ...any) {}} + h.Records = &Records{Ledger: newLedgerFake(t), Now: h.Now} + if _, _, err := h.Records.Open("main.7", map[string]string{"novox/app#3": strings.Repeat("a", 40)}, false); err != nil { + t.Fatal(err) + } + d, err := h.Deliveries("", "", "", true) + if err != nil || len(d["deliveries"].([]recordView)) != 1 { + t.Fatalf("deliveries from the records: %v %v", d, err) + } + if s, err := h.Show("main.7"); err != nil || len(s.(map[string]any)["records"].([]recordView)) != 1 { + t.Fatalf("show from the records: %v %v", s, err) + } +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go index 20341b06c..2d559de34 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go @@ -50,6 +50,32 @@ func (s seatCatalog) Registrations() (meshcatalog.RegistrationsAnswer, error) { } // Branch is the seat git's verb branch: a repository's branch head. +// ForgeReads is what the gate reads of the seat git. +type ForgeReads interface { + // Branch is a repository branch's head (the seat git's verb branch). + Branch(repository, branch string) (gitseat.BranchAnswer, error) + // Contains is whether commit is in (or is) the commit in (the seat git's verb contains). + Contains(repository, commit, in string) (bool, error) +} + +// seatGit reads the seat git through the tool runner. +type seatGit struct{ ask asking } + +func (s seatGit) call(verb string, in any, out any) error { + raw, err := s.ask("seat:"+gitseat.Seat+"."+verb, in) + if err != nil { + return err + } + answer, said, ok := answerOf(raw) + if !ok { + return fmt.Errorf("%s refused: %s", verb, lastLine(said)) + } + if err := json.Unmarshal(answer, out); err != nil { + return fmt.Errorf("the answer of %s is not readable: %w", verb, err) + } + return nil +} + func (s seatGit) Branch(repository, branch string) (gitseat.BranchAnswer, error) { var a gitseat.BranchAnswer return a, s.call(gitseat.VerbBranch, gitseat.BranchInput{Repository: repository, Branch: branch}, &a) diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go index d181be618..6968dee4e 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" stdio "git.novox.be/novox/mesh-sdk/go" @@ -310,3 +311,31 @@ func (f *flakyCatalog) Registrations() (meshcatalog.RegistrationsAnswer, error) } return f.fakeCatalog.Registrations() } + +// fakeForge is the seat git's reads: a branch's head and whether one commit is in another. +type fakeForge struct { + heads map[string]string // "@" -> head + // contains says whether commit is in `in`; nil means every commit is in every other (a straight line forward) + contains func(commit, in string) bool + fail bool +} + +func (f *fakeForge) Branch(repository, branch string) (gitseat.BranchAnswer, error) { + if f.fail { + return gitseat.BranchAnswer{}, errors.New("the forge is away") + } + head, ok := f.heads[repository+"@"+branch] + if !ok { + return gitseat.BranchAnswer{Error: "no such branch"}, nil + } + return gitseat.BranchAnswer{Branch: branch, Repository: repository, Head: head}, nil +} +func (f *fakeForge) Contains(repository, commit, in string) (bool, error) { + if f.fail { + return false, errors.New("the forge is away") + } + if f.contains == nil { + return true, nil + } + return f.contains(commit, in), nil +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go index 11fd7131a..15619be8a 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go @@ -5,6 +5,7 @@ import ( "fmt" "sort" "strconv" + "strings" mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" @@ -59,41 +60,21 @@ func (h *Holder) TakenIn(body json.RawMessage) error { g.Members = append(g.Members, mdc.ApprovedKey(e.Repository, int(e.Pull))) } sort.Strings(g.Members) - // §716: each member's base is copied from its delivery onto the record's member; a record that holds the member already - // (a take-in heard again, its delivery deleted) keeps the base it holds. - var kept map[string]Delivery - if h.Kept != nil { - h.dmu.Lock() - kept, err = h.deliveries() - h.dmu.Unlock() - if err != nil { - return err - } - } for _, m := range t.Merges { p := RecordPull{Repository: m.Repository, Number: int(m.Number), Head: heads[mdc.ApprovedKey(m.Repository, int(m.Number))], State: string(mdc.MergeMerged), Commit: m.Merge} - if base, ok := baseOf(kept, m.Repository, int(m.Number)); ok { - p.Base = base - } else if r.Group != nil { + if r.Group != nil { for _, o := range r.Group.Pulls { - if samePull(Delivery{Repository: o.Repository, Number: o.Number}, m.Repository, int(m.Number)) { + if strings.EqualFold(o.Repository, m.Repository) && o.Number == int(m.Number) { p.Base = o.Base } } } - if p.Base == "" && h.Kept != nil && h.Logf != nil { - h.Logf("[mesh-delivery] the take-in of %s names no delivery of %s#%d: its base is not known, the take-in's branch is %s", t.Group, m.Repository, m.Number, t.Branch) - } g.Pulls = append(g.Pulls, p) } if err := h.Records.SetGroup(r, g); err != nil { return err } - // The record that carries the members now holds their bases: their deliveries, and a group no member of which remains outside a record, go. - if err := h.carried(g.Pulls); err != nil { - return err - } } return h.moveSource(t) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/verbs.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/verbs.go index 52e21c370..2d84649dc 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/verbs.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/verbs.go @@ -164,7 +164,7 @@ func (h *Holder) Show(id string) (any, error) { return map[string]any{"records": out}, nil } -// Groups is the seat's verb groups: every record's group not ended (all: those that ended too), or with only that group +// Groups is the seat's verb groups, from the records alone until the seat's definition drops it (design 47 H §764): every record's group not ended (all: those that ended too), or with only that group // alone, ended or not, and none for a group it does not know. Each with its members, its state, why, and the acts the gate said // for it with the outcome recorded in the record (owedended.go). func (h *Holder) Groups(all bool, only string) seat.GroupsAnswer { @@ -185,74 +185,10 @@ func (h *Holder) Groups(all bool, only string) seat.GroupsAnswer { } out.Groups = append(out.Groups, g) } - if err := h.addKeptMembers(&out, kept, all, only); err != nil { - return seat.GroupsAnswer{Error: err.Error()} - } sort.SliceStable(out.Groups, func(i, j int) bool { return out.Groups[i].ID < out.Groups[j].ID }) return out } -// memberOf is a pull request as one key: the repository in lower case and the number. -func memberOf(repository string, number int64) string { - return fmt.Sprintf("%s#%d", strings.ToLower(repository), number) -} - -// addKeptMembers answers a group from its records and its key in the state `groups` together, each member once, a record's first -// (§716, A-279): the members of a group the gate keeps and no record carries, with their delivery's base, head, state and open -// while not merged, so before any take-in the verb names every member and its base. A group only its key holds is open. -func (h *Holder) addKeptMembers(out *seat.GroupsAnswer, kept []*Record, all bool, only string) error { - if h.Kept == nil { - return nil - } - groups, err := h.keptGroups() - if err != nil { - return err - } - deliveries, err := h.deliveries() - if err != nil { - return err - } - carried := map[string]map[string]bool{} - for _, r := range kept { - id := groupOf(r) - if carried[id] == nil { - carried[id] = map[string]bool{} - } - for _, m := range membersOf(r) { - carried[id][memberOf(m.Repository, m.Number)] = true - } - } - for _, kg := range groups { - if only != "" && kg.Group != only { - continue - } - var add []seat.Member - for _, d := range deliveries { - if d.Branch != kg.Group || carried[kg.Group][memberOf(d.Repository, int64(d.Number))] { - continue - } - add = append(add, seat.Member{ID: d.ID, Repository: d.Repository, State: d.openOrMerged(), Number: int64(d.Number), Head: d.Commit, - Open: d.openOrMerged() != deliveryMerged, Base: d.Base}) - } - if len(add) == 0 { - continue - } - sort.Slice(add, func(i, j int) bool { return add[i].ID < add[j].ID }) - placed := false - for i := range out.Groups { - if out.Groups[i].ID == kg.Group { - out.Groups[i].Members = append(out.Groups[i].Members, add...) - placed = true - } - } - if !placed { - out.Groups = append(out.Groups, seat.Group{ID: kg.Group, State: stateOpen, Members: add, Acts: []seat.Act{}, - Why: "its pull requests are not taken in yet"}) - } - } - return nil -} - // recordedActs are the acts a record holds, each with its outcome when the forge bridge said one, ordered by id. func recordedActs(r *Record) []seat.Act { out := []seat.Act{} diff --git a/modules/mesh-delivery-gate/module.json b/modules/mesh-delivery-gate/module.json index 75af30f9a..1ac0100fd 100644 --- a/modules/mesh-delivery-gate/module.json +++ b/modules/mesh-delivery-gate/module.json @@ -19,9 +19,6 @@ "consumes": [ "mesh-delivery-forge-bridge.owed.ended", "mesh-delivery-indexer.take-in.taken", - "git.pull-updated", - "git.pull-merged", - "git.pull-closed", "mesh-catalog.registration-changed", "mesh-delivery-forge-bridge.forge.*.*.merged", "mesh-conditions.findings-asked" @@ -285,16 +282,6 @@ } ] }, - { - "name": "deliveries", - "storage": "key-value", - "template": "state" - }, - { - "name": "groups", - "storage": "key-value", - "template": "state" - }, { "name": "conditions", "storage": "key-value", @@ -325,8 +312,6 @@ }, "invokes": [ "seat:git.branch", - "seat:git.open-pulls", - "seat:git.pull", "seat:mesh-catalog.registrations", "seat:mesh-hand-acts.hand-act", "seat:mesh-wants.entry", -- 2.54.0 From a943b6a7db026254bbfbfef9dddbac31b2f3a95e Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 02:38:01 +0100 Subject: [PATCH 03/43] GATE-3: one path moves the source: the take-in and the bridge's merged message no longer do MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From H1 only a merge call names a source revision, so the gate stops moving source on take-in.taken and on the forge bridge's merged message, and the start's comparison with the forge's heads, gate_source_follow, its finding source-uncompared and the forge-made record opener nobody calls go with them. Builds: design 47 H §737 (moveSource leaves the take-in.taken handler; ForgeMerged, moveHead, gate_source_follow go), §764 (the gate: the source per merged message, the start's comparison with the forge's heads, the source moved on take-in.taken; forge-made per H-A-25) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/mesh-delivery-gate/findings.go | 2 +- .../cmd/mesh-delivery-gate/main.go | 13 +- .../cmd/mesh-delivery-gate/manifest_test.go | 7 +- .../cmd/mesh-delivery-gate/record.go | 22 +- .../cmd/mesh-delivery-gate/record_test.go | 25 -- .../cmd/mesh-delivery-gate/sourcefollow.go | 278 ------------------ .../mesh-delivery-gate/sourcefollow_test.go | 193 ------------ .../cmd/mesh-delivery-gate/sourcestart.go | 13 - .../cmd/mesh-delivery-gate/sourcetools.go | 11 - .../cmd/mesh-delivery-gate/takenin.go | 82 +----- .../mesh-delivery-gate/takenin_bus_test.go | 16 +- .../cmd/mesh-delivery-gate/takenin_test.go | 21 +- modules/mesh-delivery-gate/module.json | 2 - 13 files changed, 28 insertions(+), 657 deletions(-) delete mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow.go delete mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow_test.go diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go index c769e9a4b..3170fb6e1 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go @@ -28,5 +28,5 @@ type FindingWriter interface { // generated contract of the seat that defines it (A-17). It is started once the runtime serves (A-40). func NewFindingWriter() (*views.HeldWriter[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent], error) { return views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{ - Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{SourceUncompared}}) + Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{}}) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go index df8d272b5..3d8193cdc 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go @@ -32,7 +32,7 @@ func (l *listening) get() string { l.mu.Lock(); defer l.mu.Unlock(); return l.n // The events this owner follows: the forge bridge's word on an act owed, and the step events the delivery records are built // from (consumer.go). -var followed = append(append([]string{owedEndedEvent}, stepPatterns()...), registrationChangedEvent, forgeMergedEvent) +var followed = append(append([]string{owedEndedEvent}, stepPatterns()...), registrationChangedEvent) func main() { settingsFile := os.Getenv("MESH_DELIVERY_GATE_SETTINGS") @@ -114,13 +114,7 @@ func run(h *Holder, l *listening) { l.set("listening") logf("[mesh-delivery] listening: %s", strings.Join(followed, ", ")) // A watched branch no source key holds starts from the forge, once at each start and at each registration change (A-401). - // then each head is compared with the forge's, and a merge made outside the gate is followed (A-464). - go func() { - startSourcesAtStart(h) - if _, err := h.FollowAtStart(); err != nil { - logf("[mesh-delivery] the sources were not all compared with the forge's heads: %v", err) - } - }() + go startSourcesAtStart(h) } // statePins says every pin the records hold once at each start (design 47 G §579), retrying until it can. @@ -156,9 +150,6 @@ func take(h *Holder, e stdio.Envelope) error { _, err := h.StartSources() return err } - if forgeMergedEvent != "" && stdio.TopicMatches(forgeMergedEvent, e.Key) { - return h.ForgeMerged(e.Body) - } return nil } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go index b0542e949..b9df64585 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go @@ -136,15 +136,12 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { } } // From G's delivery the gate hears neither the bridge's pushed messages nor the teller's answers nor the view's reach - // (design 47 G §592, §593); it hears the bridge's merged message alone, to follow every merge (A-464): it consumes the indexer's take-in.taken and the bridge's owed.ended, and the git events that clear - // an approval carried. + // (design 47 G §592, §593), and from H1 not its merged message either (design 47 H §737, §764): it consumes the indexer's + // take-in.taken and the bridge's owed.ended. if want := append(slices.Clone(followed), meshconditions.Seat+"."+meshconditions.EventFindingsAsked); !reflect.DeepEqual(m.Consumes, want) { t.Fatalf("consumes %v, follows %v", m.Consumes, followed) } for _, c := range m.Consumes { - if c == forgeMergedEvent { - continue // A-464: the source follows every merge, so the gate hears the forge bridge's merged message, and it alone - } for _, gone := range []string{".forge.", "teller", "view", "forge-pull", "forge-status"} { if strings.Contains(c, gone) { t.Errorf("the manifest still consumes %s, which G's gate does not hear (§431, §592, §593)", c) diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go index 99edd8393..8df97b3c4 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go @@ -485,25 +485,10 @@ func (rs *Records) Read(instance string) (*Record, error) { // opened now. Each open removes the records past their retention first (the retention needs no timer: records open as // deliveries do). func (rs *Records) Open(milestone string, heads map[string]string, forgeMade bool) (r *Record, opened bool, err error) { - return rs.open(milestone, heads, forgeMade, ForgeMerge{}) + return rs.open(milestone, heads, forgeMade) } -// ForgeMerge is the merge a forge-made record is for: what record-opened says of it (design 47 G A-161). -type ForgeMerge struct { - Repository string - Pull int64 - Commit string - Branch string -} - -// OpenForgeMade opens the record of a merge made at the forge, marked forge-made, and says record-opened with the merge's -// repository, pull request, commit and branch, so the teller makes no call per message (A-161). No path of the gate opens one -// from G's delivery (§151): the teller says none while nothing calls this. -func (rs *Records) OpenForgeMade(milestone string, heads map[string]string, m ForgeMerge) (r *Record, opened bool, err error) { - return rs.open(milestone, heads, true, m) -} - -func (rs *Records) open(milestone string, heads map[string]string, forgeMade bool, m ForgeMerge) (r *Record, opened bool, err error) { +func (rs *Records) open(milestone string, heads map[string]string, forgeMade bool) (r *Record, opened bool, err error) { inst, err := RecordInstance(milestone, heads) if err != nil { return nil, false, err @@ -538,8 +523,7 @@ func (rs *Records) open(milestone string, heads map[string]string, forgeMade boo // Said once, under the record's id: the watchman times it against 24 hours, ended by record-ended. A say that fails is // said again by the next write of the record (the id keeps it once). at := now.UTC() - if serr := rs.say(meshdelivery.SubjectRecordOpened, meshdelivery.RecordOpenedEvent{Record: inst, Milestone: milestone, ForgeMade: forgeMade, At: &at, - Repository: m.Repository, Pull: m.Pull, Commit: m.Commit, Branch: m.Branch}, + if serr := rs.say(meshdelivery.SubjectRecordOpened, meshdelivery.RecordOpenedEvent{Record: inst, Milestone: milestone, ForgeMade: forgeMade, At: &at}, "record-opened."+inst); serr != nil && rs.Logf != nil { rs.Logf("[mesh-delivery] the record %s is opened and its opening is not said: %v", inst, serr) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go index 86e0a44a9..2b51ba7de 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go @@ -773,31 +773,6 @@ func TestTheRecordsToolsReadThemAndEndOneByHand(t *testing.T) { }) } -// A-161: record-opened says what its hearers need to tell a forge-made record: the repository, the pull request, the merge -// commit and the branch; an ordinary record says none of them. -func TestRecordOpenedCarriesAForgeMadeRecordsMerge(t *testing.T) { - var said []meshdelivery.RecordOpenedEvent - rs := &Records{Ledger: newLedgerFake(t), Now: func() time.Time { return time.Date(2026, 11, 12, 5, 0, 0, 0, time.UTC) }, - Say: func(subject string, body any, id string) error { - if subject == meshdelivery.SubjectRecordOpened { - said = append(said, body.(meshdelivery.RecordOpenedEvent)) - } - return nil - }} - commit := strings.Repeat("3", 40) - if _, _, err := rs.OpenForgeMade("main.5", map[string]string{"novox/app@333333333333": commit}, - ForgeMerge{Repository: "novox/app", Pull: 12, Commit: commit, Branch: "main"}); err != nil { - t.Fatal(err) - } - if _, _, err := rs.Open("47/G", map[string]string{"novox/app@111111111111": strings.Repeat("1", 40)}, false); err != nil { - t.Fatal(err) - } - if len(said) != 2 || !said[0].ForgeMade || said[0].Repository != "novox/app" || said[0].Pull != 12 || said[0].Commit != commit || - said[0].Branch != "main" || said[1].ForgeMade || said[1].Repository != "" || said[1].Pull != 0 || said[1].Commit != "" || said[1].Branch != "" { - t.Fatalf("said %+v", said) - } -} - // W11.20 (design 47 *The delivery record*: a store filling is said before it is full): the gate reads its ledger's fill, // says filling when a record takes it past 80 % of its cap, refuses the next record as full and holds its merge, and // says room and hands the merge once the ledger is below 80 % again; the read tool says the fill. diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow.go deleted file mode 100644 index 6fef43a91..000000000 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow.go +++ /dev/null @@ -1,278 +0,0 @@ -package main - -// The gate's source of a watched branch follows every merge into that branch, whoever made it (design 47 G A-464, A-464b, amending -// A-463): on the forge bridge's `merged` of a repository the source names, at the branch the source watches, the gate moves that -// repository's head to the merge and says source-moved, as a take-in moves it; at its start it compares each head with the -// forge's branch head, asked of the seat git, and moves every one behind it the same way, so a merge whose message was lost is -// followed too; gate_source_follow does the same for one repository now. A head the source does not name, or a branch it does -// not watch, is left: only a start or a registration change adds them (sourcestart.go). - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "sort" - "strconv" - "strings" - "time" - - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" - "git.novox.be/novox/mesh-sdk/go/keys" -) - -// forgeMergedEvent is the forge bridge's merged message of any repository, as the manifest consumes it. -var forgeMergedEvent = declared("forge.*.*.merged") - -// moveHead moves the head the branch's source names for repository to head, and says source-moved with the key's revision. It -// answers false, and writes nothing, when the branch has no source key, the key does not name the repository or names the head -// already. group is the group that made the merge, empty for a merge no group of the gate made. -func (h *Holder) moveHead(branch, repository, head, group string) (moved bool, err error) { - if h.Source == nil || head == "" { - return false, nil - } - held, _, err := h.Source.Read(branch) - if err != nil || held == nil { - return false, err - } - name := "" - for r := range held.Heads { - if trunk.SameRepository(r, repository) { - name = r - } - } - if name == "" || held.Heads[name] == head { - return false, nil // not named, or no change: no revision and no source-moved (A-464c) - } - was := held.Heads[name] - // Only forward (A-464c): the head held must be contained in the merge, asked of the seat git. - if h.Forge == nil { - return false, errors.New("this gate reads no forge, so it cannot tell whether the merge is ahead of the head") - } - ahead, err := h.Forge.Contains(name, was, head) - if err != nil { - return false, fmt.Errorf("whether %.12s is in %.12s of %s cannot be read, so the head stays: %w", was, head, name, err) - } - if !ahead { - h.logf("[mesh-delivery] source.%s keeps %s at %.12s: the merge %.12s does not contain it (heard again, late or out of order), left without effect", branch, name, was, head) - return false, nil - } - rev, err := h.Source.Set(branch, func(prev *Source) (Source, bool) { - moved = false - if prev == nil || prev.Heads[name] != was { - return Source{}, false // another writer moved it first - } - next := Source{Group: group, Heads: map[string]string{}} - for r, c := range prev.Heads { - next.Heads[r] = c - } - next.Heads[name] = head - moved = true - return next, true - }) - if err != nil { - return false, fmt.Errorf("source.%s cannot be moved: %w", branch, err) - } - if !moved || h.SaySeat == nil { - return moved, nil - } - at := h.Now().UTC() - return true, h.SaySeat(seat.SubjectSourceMoved(keys.EscapeName(branch)), - seat.SourceMovedEvent{Branch: branch, Group: group, Revision: int64(rev), At: &at}, - "source-moved."+keys.EscapeName(branch)+"."+strconv.FormatUint(rev, 10)) -} - -// ForgeMerged takes one merged message of the forge bridge: the head of its repository in the source of the branch it landed on -// moves to the merge. A merge no delivery group made (the message names no group) is said so. A message that cannot be read, or -// names no branch or merge, is said and taken: heard again it would be refused again. -func (h *Holder) ForgeMerged(body json.RawMessage) error { - f, err := forgemsg.DecodeForge(body) - if err != nil { - h.logf("[mesh-delivery] a merged message of the forge bridge is not readable, the source does not follow it: %v", err) - return nil - } - if f.Kind != forgemsg.ForgeMerged { - return nil - } - if f.Base == "" || f.Merge == "" { - h.logf("[mesh-delivery] the merged message of %s names no branch or no merge, the source does not follow it", f.Repository) - return nil - } - moved, err := h.moveHead(f.Base, f.Repository, f.Merge, f.Group) - if err != nil { - return err - } - if moved { - if f.Group == "" { // A-464b: a merge whose message names no delivery group is one the gate did not make - h.logf("[mesh-delivery] source.%s follows the merge %.12s of %s: not made by a delivery group of the gate", f.Base, f.Merge, f.Repository) - } else { - h.logf("[mesh-delivery] source.%s follows the merge %.12s of %s, made by the delivery group %s", f.Base, f.Merge, f.Repository, f.Group) - } - } - return nil -} - -// SourceUncompared is the finding of a start comparison that left a head uncompared (A-464e). -const SourceUncompared = "source-uncompared" - -// FollowAtStart compares each head of every source key with the forge's head of that branch and moves every one behind it, at -// each start. It ends only when every head was compared (A-464e): a head the forge could not be asked for (the seat git not -// answering yet, as when its bundle starts in the same tool runner) is compared again after a growing pause of at most a -// minute, and the finding source-uncompared names the repositories and the last error while any stands. It answers the heads -// moved as "@". -func (h *Holder) FollowAtStart() ([]string, error) { - if h.Source == nil || h.Forge == nil { - return nil, errors.New("this gate keeps no source or reads no forge") - } - var moved []string - var only map[string]bool - for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) { - got, left := h.followPass(only) - moved = append(moved, got...) - if len(left) == 0 { - h.clearUncompared() - h.logf("[mesh-delivery] the sources compared with the forge's heads at the start: moved %v", moved) - return moved, nil - } - only = map[string]bool{} - var names []string - var last error - for n, err := range left { - only[n] = true - names = append(names, n) - last = err - } - sort.Strings(names) - h.sayUncompared(names, last) - h.logf("[mesh-delivery] %d heads are not compared with the forge's yet (%s; last: %v); asking again in %s", len(names), strings.Join(names, ", "), last, wait) - startSleep(wait) - } -} - -// followPass compares the heads named in only (every head when nil) once; it answers the heads moved and, by "@", -// each head it could not compare with why. A source it cannot list or read is one entry named for itself. -func (h *Holder) followPass(only map[string]bool) (moved []string, left map[string]error) { - left = map[string]error{} - branches, err := h.Source.Branches() - if err != nil { - left["the branches of the source"] = err - return nil, left - } - for _, branch := range branches { - held, _, err := h.Source.Read(branch) - if err != nil { - left["source."+branch] = err - continue - } - if held == nil { - continue - } - for repository, head := range held.Heads { - name := repository + "@" + branch - if only != nil && !only[name] { - continue - } - ok, err := h.followForge(branch, repository, head) - if err != nil { - left[name] = err - continue - } - if ok { - moved = append(moved, name) - } - } - } - return moved, left -} - -func (h *Holder) sayUncompared(names []string, last error) { - if h.Findings == nil { - return - } - at := h.Now().UTC() - err := h.Findings.Write(context.Background(), meshconditions.Finding{Scope: FindingScope, ID: SourceUncompared, Kind: SourceUncompared, - Subject: meshconditions.Subject{Scope: FindingScope, ID: SourceUncompared}, - Severity: meshconditions.FindingSeverityWarning, Resolver: "operator", - Summary: fmt.Sprintf("the gate's source is not compared with the forge's head for %s; the last error: %v", strings.Join(names, ", "), last), - Headline: "The gate's source is not compared with the forge", - Explanation: "At its start the gate compares each head of its source with the forge's head of the branch. A head it could not compare is asked again after a growing pause, and this clears once every head is compared.", - Needs: "the seat git answering; gate_source_follow moves one repository's head by hand.", - Source: SourceUncompared, Observer: Module, At: at, Since: at}) - if err != nil { - h.logf("[mesh-delivery] the finding %s is not written: %v", SourceUncompared, err) - } -} - -func (h *Holder) clearUncompared() { - if h.Findings == nil { - return - } - for _, f := range h.Findings.Held() { - if f.Scope == FindingScope && f.ID == SourceUncompared && f.Kind == SourceUncompared { - if err := h.Findings.Clear(context.Background(), FindingScope, SourceUncompared, SourceUncompared, "every head is compared with the forge's"); err != nil { - h.logf("[mesh-delivery] the finding %s is not cleared: %v", SourceUncompared, err) - } - } - } -} - -// followForge moves one repository's head in a branch's source to the forge's head of the branch when they differ. -func (h *Holder) followForge(branch, repository, held string) (bool, error) { - a, err := h.Forge.Branch(repository, branch) - if err == nil && a.Error != "" { - err = errors.New(a.Error) - } - if err == nil && a.Head == "" { - err = errors.New("the forge named no head") - } - if err != nil { - return false, err - } - if sameCommit(held, a.Head) && len(held) == len(a.Head) { - return false, nil - } - moved, err := h.moveHead(branch, repository, a.Head, "") - if moved { - h.logf("[mesh-delivery] source.%s follows the forge: %s from %.12s to %.12s", branch, repository, held, a.Head) - } - return moved, err -} - -// SourceFollow moves one repository's head to the forge's head of the branch by hand, after the hand-act log took it. -func (h *Holder) SourceFollow(branch, repository, why, by string, acts handActs) (map[string]any, error) { - if strings.TrimSpace(branch) == "" || strings.TrimSpace(repository) == "" || strings.TrimSpace(why) == "" { - return nil, errors.New("say the branch, the repository and why its head follows the forge") - } - if h.Source == nil || h.Forge == nil { - return nil, errors.New("this gate keeps no source or reads no forge") - } - if acts == nil { - return nil, errors.New("the hand-act log cannot be reached here, so nothing was moved") - } - held, _, err := h.Source.Read(branch) - if err != nil { - return nil, err - } - if held == nil { - return nil, fmt.Errorf("the gate keeps no source of the branch %s", branch) - } - name := "" - for r := range held.Heads { - if trunk.SameRepository(r, repository) { - name = r - } - } - if name == "" { - return nil, fmt.Errorf("the source of %s names no head of %s", branch, repository) - } - if _, err := acts.Record(meshhandactsclient.Request{What: "moved the head of " + repository + " in the source of the branch " + branch + " to the forge's head", - Why: why, Cause: "gate_source_follow", Verb: "source-follow"}); err != nil { - return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was moved: %w", err) - } - moved, err := h.followForge(branch, name, held.Heads[name]) - return map[string]any{"branch": branch, "repository": name, "moved": moved, "by": by}, err -} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow_test.go deleted file mode 100644 index 214564098..000000000 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow_test.go +++ /dev/null @@ -1,193 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "errors" - "strings" - "testing" - "time" - - "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" - meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" - stdio "git.novox.be/novox/mesh-sdk/go" -) - -// Design 47 G A-464, A-464b: the forge bridge's merged message moves the head of its repository in the source of the branch it -// landed on and says source-moved; a merge naming no delivery group is said not made by one; a branch no source key holds, a -// repository the key does not name, and a head the key holds already move nothing. -func TestAMergedMessageMovesTheSourceOfItsBranchWhoeverMadeTheMerge(t *testing.T) { - old, merge := strings.Repeat("a", 40), strings.Repeat("b", 40) - body := func(repository, base, group string) []byte { - f := forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: repository, Base: base, Merge: merge, Head: merge, Number: 14, Root: strings.Repeat("d", 40)} - data, err := forgemsg.EncodeForge(f) - if err != nil { - t.Fatal(err) - } - return data - } - for _, c := range []struct { - name, repository, base string - moved bool - }{ - {"watched branch, named repository", "novox/mesh-contracts", "main", true}, - {"a branch no source key holds", "novox/mesh-contracts", "other", false}, - {"a repository the key does not name", "novox/other", "main", false}, - } { - t.Run(c.name, func(t *testing.T) { - src := &memSource{vals: map[string]Source{"main": {Group: "g", Heads: map[string]string{"novox/mesh-contracts": old}}}} - var said []string - var logged []string - h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{}, - Logf: func(f string, a ...any) { logged = append(logged, f) }, - SaySeat: func(subject string, _ any, id string) error { said = append(said, id); return nil }} - if err := h.ForgeMerged(body(c.repository, c.base, "")); err != nil { - t.Fatal(err) - } - got := src.vals["main"] - if c.moved != (got.Heads["novox/mesh-contracts"] == merge) || c.moved != (len(said) == 1) || (c.moved && got.Group != "") { - t.Fatalf("source %+v said %v: moved wanted %v", got, said, c.moved) - } - if c.moved && !strings.Contains(strings.Join(logged, "\n"), "not made by a delivery group of the gate") { - t.Errorf("a merge no group made is not said so: %v", logged) - } - // Heard again, the key names the head already: nothing is moved or said twice. - if err := h.ForgeMerged(body(c.repository, c.base, "")); err != nil || len(said) > 1 { - t.Errorf("heard twice: %v, said %v", err, said) - } - }) - } -} - -// Design 47 G A-464: at its start the gate compares each head with the forge's branch head and moves every one behind it; a head -// the forge cannot read is said and left; gate_source_follow does it for one repository after the hand-act log took it. -func TestTheStartAndTheToolMoveAHeadBehindTheForge(t *testing.T) { - old, now := strings.Repeat("a", 40), strings.Repeat("c", 40) - mk := func() (*Holder, *branchSource, *[]string) { - src := &branchSource{memSource{vals: map[string]Source{"main": {Heads: map[string]string{"o/f1": old, "o/f2": old, "o/f3": now}}}}} - var said []string - return &Holder{Now: time.Now, Source: src, Logf: func(string, ...any) {}, - Forge: &fakeForge{heads: map[string]string{"o/f1@main": now, "o/f3@main": now}}, // o/f2 cannot be read - SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }}, src, &said - } - h, src, said := mk() - fw := &fakeFindings{} - h.Findings = fw - forge := h.Forge.(*fakeForge) - var pauses []time.Duration - var heldWhileWaiting int - prevSleep := startSleep - startSleep = func(d time.Duration) { - pauses = append(pauses, d) - heldWhileWaiting = len(fw.held) // the finding stands while a head is uncompared - forge.heads["o/f2@main"] = now // the seat git answers by the second pass - } - defer func() { startSleep = prevSleep }() - moved, err := h.FollowAtStart() - if err != nil || len(moved) != 2 || src.vals["main"].Heads["o/f1"] != now || src.vals["main"].Heads["o/f2"] != now || len(*said) != 2 { - t.Fatalf("start: moved %v err %v heads %v said %v: want o/f1 and, after the second pass, o/f2 moved", moved, err, src.vals["main"].Heads, *said) - } - if len(pauses) != 1 || heldWhileWaiting != 1 || len(fw.held) != 0 { - t.Fatalf("pauses %v, finding held while waiting %d and after %d: want one pause, the finding raised while a head stood uncompared and cleared after", pauses, heldWhileWaiting, len(fw.held)) - } - h, src, said = mk() - acts := &handActsFake{refuse: errors.New("log down")} - if _, err := h.SourceFollow("main", "o/f1", "missed a merge", "me", acts); err == nil || src.vals["main"].Heads["o/f1"] != old { - t.Fatalf("moved without the log: %v", err) - } - acts.refuse = nil - if _, err := h.SourceFollow("main", "o/f1", "", "me", acts); err == nil { - t.Fatal("moved with no reason") - } - out, err := h.SourceFollow("main", "o/f1", "missed a merge", "me", acts) - if err != nil || out["moved"] != true || len(acts.took) != 1 || acts.took[0].Cause != "gate_source_follow" || src.vals["main"].Heads["o/f1"] != now { - t.Fatalf("tool: %v %v log %+v heads %v", out, err, acts.took, src.vals["main"].Heads) - } -} - -// Design 47 G A-464: the gate takes the forge bridge's merged event of any repository, by the manifest's own pattern. -func TestTheGateTakesTheForgeBridgesMergedEventOfAnyRepository(t *testing.T) { - merge := strings.Repeat("b", 40) - data, err := forgemsg.EncodeForge(forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: "novox/mesh-contracts", Base: "main", Merge: merge, Head: merge, Number: 14, Root: strings.Repeat("d", 40)}) - if err != nil { - t.Fatal(err) - } - src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/mesh-contracts": strings.Repeat("a", 40)}}}} - h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{}, Logf: func(string, ...any) {}} - key := strings.Replace(strings.Replace(forgeMergedEvent, "*", "novox", 1), "*", "mesh-contracts", 1) - if err := take(h, stdio.Envelope{Key: key, Body: data}); err != nil || src.vals["main"].Heads["novox/mesh-contracts"] != merge { - t.Fatalf("event %q: %v, source %+v", key, err, src.vals["main"]) - } -} - -// Design 47 G A-464c: a head moves only forward and only when it changes. A merged message for a commit that does not contain -// the held head (replayed, late, out of order) moves nothing and is said; a second message for the same head writes no -// revision; and a take-in.taken for a head a merged already moved writes none and says no source-moved. -func TestAHeadMovesOnlyForwardAndOnlyWhenItChanges(t *testing.T) { - oldC, newC := strings.Repeat("a", 40), strings.Repeat("b", 40) - msg := func(merge string) []byte { - data, err := forgemsg.EncodeForge(forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: "novox/app", Base: "main", Merge: merge, Head: merge, Number: 3, Root: strings.Repeat("d", 40)}) - if err != nil { - t.Fatal(err) - } - return data - } - src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/app": newC}}}} - var said, logged []string - // newC contains oldC, not the other way round. - forge := &fakeForge{contains: func(commit, in string) bool { return commit == in || (commit == oldC && in == newC) }} - h := &Holder{Now: time.Now, Source: src, Forge: forge, Logf: func(f string, a ...any) { logged = append(logged, f) }, - SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }} - if err := h.ForgeMerged(msg(oldC)); err != nil || src.vals["main"].Heads["novox/app"] != newC || src.rev != 0 || len(said) != 0 || len(logged) == 0 { - t.Fatalf("a replayed old merge: %v, source %+v, revisions %d, said %v, logged %v: want nothing moved and a log line", err, src.vals["main"], src.rev, said, logged) - } - if err := h.ForgeMerged(msg(newC)); err != nil || src.rev != 0 || len(said) != 0 { - t.Fatalf("a second message for the same head wrote revision %d, said %v", src.rev, said) - } - // A take-in.taken naming the head a merged moved already writes no revision and says nothing. - body := []byte(`{"v":1,"kind":"taken","key":"g.feat","what":"group","group":"feat","branch":"main","source":7,` + - `"merges":[{"repository":"novox/app","number":3,"merge":"` + newC + `"}],"entries":[]}`) - if err := h.moveSource(mustTaken(t, body)); err != nil || src.rev != 0 || len(said) != 0 { - t.Fatalf("take-in for a head already moved: %v, revision %d, said %v", err, src.rev, said) - } - // A real forward move still writes one revision. - src.vals["main"] = Source{Heads: map[string]string{"novox/app": oldC}} - if err := h.ForgeMerged(msg(newC)); err != nil || src.rev != 1 || len(said) != 1 { - t.Fatalf("a forward move: %v, revision %d, said %v", err, src.rev, said) - } -} - -func mustTaken(t *testing.T, body []byte) meshcatalog.TakeInTaken { - t.Helper() - var v meshcatalog.TakeInTaken - if err := json.Unmarshal(body, &v); err != nil { - t.Fatal(err) - } - return v -} - -// Design 47 G A-464d: a take-in.taken arriving after a later merge moved the head leaves the head where it is. -func TestATakeInTakenAfterALaterMergeLeavesTheHead(t *testing.T) { - oldC, newC := strings.Repeat("a", 40), strings.Repeat("b", 40) - src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/app": newC}}}} - var said []string - h := &Holder{Now: time.Now, Source: src, Logf: func(string, ...any) {}, - Forge: &fakeForge{contains: func(commit, in string) bool { return commit == in || (commit == oldC && in == newC) }}, - SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }} - body := []byte(`{"v":1,"kind":"taken","key":"g.feat","what":"group","group":"feat","branch":"main","source":7,` + - `"merges":[{"repository":"novox/app","number":3,"merge":"` + oldC + `"}],"entries":[]}`) - if err := h.moveSource(mustTaken(t, body)); err != nil || src.vals["main"].Heads["novox/app"] != newC || src.rev != 0 || len(said) != 0 { - t.Fatalf("take-in of an older merge: %v, source %+v, revision %d, said %v: want the head left at the later merge", err, src.vals["main"], src.rev, said) - } -} - -// fakeFindings is the gate's finding writer in memory. -type fakeFindings struct{ held []meshconditions.Finding } - -func (f *fakeFindings) Write(_ context.Context, x meshconditions.Finding) error { - f.held = append(f.held, x) - return nil -} -func (f *fakeFindings) Clear(_ context.Context, _, _, _, _ string) error { f.held = nil; return nil } -func (f *fakeFindings) Held() []meshconditions.Finding { return f.held } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go index 2d559de34..56ce1c278 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go @@ -54,8 +54,6 @@ func (s seatCatalog) Registrations() (meshcatalog.RegistrationsAnswer, error) { type ForgeReads interface { // Branch is a repository branch's head (the seat git's verb branch). Branch(repository, branch string) (gitseat.BranchAnswer, error) - // Contains is whether commit is in (or is) the commit in (the seat git's verb contains). - Contains(repository, commit, in string) (bool, error) } // seatGit reads the seat git through the tool runner. @@ -82,17 +80,6 @@ func (s seatGit) Branch(repository, branch string) (gitseat.BranchAnswer, error) } // Contains is whether commit is the commit in or an ancestor of it. -func (s seatGit) Contains(repository, commit, in string) (bool, error) { - var a gitseat.ContainsAnswer - if err := s.call(gitseat.VerbContains, gitseat.ContainsInput{Repository: repository, Commit: commit, In: in}, &a); err != nil { - return false, err - } - if a.Error != "" { - return false, errors.New(a.Error) - } - return a.Contains, nil -} - // watchedRepositories are the repositories each watched branch is registered for, each once, sorted. func watchedRepositories(regs []meshcatalog.Registration) map[string][]string { seen := map[string]map[string]bool{} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go index b756d4271..6d3f7586c 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go @@ -89,16 +89,5 @@ func sourceTools(h *Holder, acts handActs) []stdio.Tool { Run: func(a map[string]any) (any, error) { return h.SourceStart(strArg(a, "branch"), strArg(a, "why"), stdio.CallerOf(a), acts) }}, - {Name: "gate_source_follow", - Description: "Move the head one watched branch's source names for one repository to the forge's head of that branch now, as " + - "the gate does when it hears a merge and at its start (design 47 G A-464); recorded in the hand-act log of the seat " + - "mesh-hand-acts first. Nothing is moved when the source already names the forge's head.", - Input: map[string]any{"type": "object", "properties": map[string]any{ - "branch": str("the watched branch"), "repository": str("the repository, owner/name"), - "why": str("why its head follows the forge by hand, in plain words")}, - "required": []string{"branch", "repository", "why"}}, - Run: func(a map[string]any) (any, error) { - return h.SourceFollow(strArg(a, "branch"), strArg(a, "repository"), strArg(a, "why"), stdio.CallerOf(a), acts) - }}, } } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go index 15619be8a..84a1327c7 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go @@ -9,14 +9,11 @@ import ( mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" "git.novox.be/novox/mesh-sdk/go/keys" ) -// What a take-in.taken does at the gate (design 47 G A-33, A-34; §151, §291, §592): it opens one delivery record, keyed by the -// taken body's branch and source revision, and moves that branch's source revision, saying source-moved. Nothing else opens a -// record or moves a source key. +// What a take-in.taken does at the gate (design 47 G A-33; §151, §592): it opens one delivery record, keyed by the taken +// body's branch and source revision. It moves no source key: only a merge call does (design 47 H §737, §764). // recordMilestone is the key of a taken revision's record: `.`, each escaped as a name (A-33). func recordMilestone(branch string, source int64) string { @@ -24,8 +21,8 @@ func recordMilestone(branch string, source int64) string { } // TakenIn takes one take-in.taken body: the record of its source revision is opened (safe to repeat: a record there already -// is left as it is), its members are the entries' pull requests, its heads the merge commits the take-in names; then the -// branch's source key is written and source-moved said. A hand drop took nothing in, so it opens nothing. +// is left as it is), its members are the entries' pull requests, its heads the merge commits the take-in names. A hand drop +// took nothing in, so it opens nothing. func (h *Holder) TakenIn(body json.RawMessage) error { var t meshcatalog.TakeInTaken if err := json.Unmarshal(body, &t); err != nil { @@ -76,74 +73,5 @@ func (h *Holder) TakenIn(body json.RawMessage) error { return err } } - return h.moveSource(t) -} - -// moveSource writes the taken source revision of a branch: the merge commit of each repository it took in, the group that -// moved it, and says source-moved with the key's revision. -func (h *Holder) moveSource(t meshcatalog.TakeInTaken) error { - if h.Source == nil { - return nil - } - heads := map[string]string{} - for _, m := range t.Merges { - heads[m.Repository] = m.Merge - } - // A head moves only forward (A-464d): a repository whose held head is not contained in the merge is left where it is. - if held, _, err := h.Source.Read(t.Branch); err != nil { - return err - } else if held != nil && h.Forge != nil { - for r, c := range heads { - for name, was := range held.Heads { - if !trunk.SameRepository(name, r) || was == c { - continue - } - ahead, err := h.Forge.Contains(name, was, c) - if err != nil { - return fmt.Errorf("whether %.12s is in %.12s of %s cannot be read, so the head stays: %w", was, c, name, err) - } - if !ahead { - h.logf("[mesh-delivery] source.%s keeps %s at %.12s: the take-in's merge %.12s does not contain it, left without effect", t.Branch, name, was, c) - delete(heads, r) - } - } - } - if len(heads) == 0 { - return nil - } - } - wrote := false - rev, err := h.Source.Set(t.Branch, func(prev *Source) (Source, bool) { - wrote = false - next := Source{Group: t.Group, Heads: heads} - if prev != nil { - same := true - for r, c := range heads { - if prev.Heads[r] != c { - same = false - } - } - if same { - return Source{}, false // a merged message moved these heads already: no revision (A-464c) - } - next.Heads = map[string]string{} - for r, c := range prev.Heads { - next.Heads[r] = c - } - for r, c := range heads { - next.Heads[r] = c - } - } - wrote = true - return next, true - }) - if err != nil { - return fmt.Errorf("source.%s cannot be moved: %w", t.Branch, err) - } - if h.SaySeat == nil || !wrote { - return nil - } - at := h.Now().UTC() - return h.SaySeat(seat.SubjectSourceMoved(keys.EscapeName(t.Branch)), - seat.SourceMovedEvent{Branch: t.Branch, Group: t.Group, Revision: int64(rev), At: &at}, "source-moved."+keys.EscapeName(t.Branch)+"."+strconv.FormatUint(rev, 10)) + return nil } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go index a34bf2ef8..35615b4e9 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go @@ -7,16 +7,14 @@ import ( "strings" "testing" "time" - - mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" ) -// The gate whole, over a real bus (design 47 G A-33, A-34): a take-in.taken the indexer says opens a delivery record in the -// gate's ledger with the take-in as its outcome, writes the branch's source key and says source-moved; the gate asks the +// The gate whole, over a real bus (design 47 G A-33): a take-in.taken the indexer says opens a delivery record in the +// gate's ledger with the take-in as its outcome (it moves no source key, design 47 H §737); the gate asks the // controller nothing, and no state it no longer declares is needed (the template states are buckets under their local names). const eventWithin = 20 * time.Second -func TestAGateWholeTakesAnIndexersTakeInIntoARecordAndASourceKeyOverARealBus(t *testing.T) { +func TestAGateWholeTakesAnIndexersTakeInIntoARecordOverARealBus(t *testing.T) { b := newTestBus(t) ctx := context.Background() rt := b.start(t) @@ -29,14 +27,6 @@ func TestAGateWholeTakesAnIndexersTakeInIntoARecordAndASourceKeyOverARealBus(t * "what": "revision", "branch": "main", "source": 3, "since": "2026-11-11T19:00:00Z", "merges": []any{map[string]any{"repository": "novox/app", "number": 5, "merge": merge}}, "entries": []any{map[string]any{"module": "app", "commit": merge, "fingerprint": "f", "repository": "novox/app", "pull": 5}}}) - rt.until("the source key of main", eventWithin, func() bool { - e, err := b.kvs["source"].Get(ctx, mdc.SourceKey("main")) - if err != nil { - return false - } - s, err := mdc.DecodeSource(e.Value()) - return err == nil && s.Heads["novox/app"] == merge - }) rt.until("the record of the take-in", eventWithin, func() bool { b.mu.Lock() defer b.mu.Unlock() diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go index a4c50ec07..abf988058 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go @@ -41,10 +41,11 @@ func (m *memSource) Read(branch string) (*Source, uint64, error) { } func (m *memSource) Branches() ([]string, error) { return nil, nil } -// Design 47 G A-33, A-34: a take-in.taken opens one record keyed `.`, whose members are the entries' pull -// requests and whose heads are the merge commits it names, moves that branch's source key and says source-moved; heard twice -// it opens no second record; a hand drop opens nothing and moves nothing. -func TestATakeInTakenOpensOneRecordAndMovesItsBranchsSource(t *testing.T) { +// Design 47 G A-33: a take-in.taken opens one record keyed `.`, whose members are the entries' pull +// requests and whose heads are the merge commits it names; heard twice it opens no second record; a hand drop opens nothing. +// GATE-3/AC3 (design 47 H §737, §764): neither it nor the forge bridge's merged message moves a source key or says +// source-moved: only a merge call does. +func TestATakeInTakenOpensOneRecordAndMovesNoSource(t *testing.T) { now := time.Date(2026, 11, 11, 20, 0, 0, 0, time.UTC) type said struct{ subject, id string } var events []said @@ -81,18 +82,20 @@ func TestATakeInTakenOpensOneRecordAndMovesItsBranchsSource(t *testing.T) { if got := r.Outcomes[OutcomeKey("", "", "", "take-in")]; got.Outcome != OutcomeTakenIn { t.Fatalf("the take-in is not the record's outcome: %+v", r.Outcomes) } - if s := src.vals["main"]; s.Group != "feat" || s.Heads["novox/app"] != merge { - t.Fatalf("source.main %+v", s) + if len(src.vals) != 0 || len(events) != 0 { + t.Fatalf("a take-in.taken moved source %+v and said %+v", src.vals, events) } - if len(events) != 1 || !strings.HasSuffix(events[0].subject, ".event.source-moved.main") { - t.Fatalf("said %+v", events) + merged := stdio.Envelope{Key: "mesh-delivery-forge-bridge.forge.novox.app.merged", + Body: json.RawMessage(`{"v":1,"kind":"merged","repository":"novox/app","base":"main","head":"` + merge + `"}`)} + if err := take(h, merged); err != nil || len(src.vals) != 0 || len(events) != 0 { + t.Fatalf("a merged message moved source %+v and said %+v (%v)", src.vals, events, err) } drop := json.RawMessage(`{"v":1,"kind":"taken","key":"m.00000000000000000002","what":"revision","dropped":true,"branch":"main","source":8,` + `"merges":[{"repository":"novox/lib","number":4,"merge":"` + merge + `"}],"since":"2026-11-11T19:00:00Z"}`) if err := c.Hear(stdio.Envelope{Key: "mesh-delivery-indexer.take-in.taken", Body: drop}); err != nil { t.Fatal(err) } - if kept, _ := h.Records.Kept(); len(kept) != 1 || len(events) != 1 { + if kept, _ := h.Records.Kept(); len(kept) != 1 || len(events) != 0 { t.Fatalf("a hand drop opened %d records and said %d events", len(kept), len(events)) } } diff --git a/modules/mesh-delivery-gate/module.json b/modules/mesh-delivery-gate/module.json index 1ac0100fd..d7d108ff7 100644 --- a/modules/mesh-delivery-gate/module.json +++ b/modules/mesh-delivery-gate/module.json @@ -20,7 +20,6 @@ "mesh-delivery-forge-bridge.owed.ended", "mesh-delivery-indexer.take-in.taken", "mesh-catalog.registration-changed", - "mesh-delivery-forge-bridge.forge.*.*.merged", "mesh-conditions.findings-asked" ], "emits": [ @@ -336,7 +335,6 @@ "gate_records", "gate_source", "gate_source_again", - "gate_source_follow", "gate_source_start" ], "config": { -- 2.54.0 From 9cfda8d6c7f70d0fe5ea07de9985257e0245b77e Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 19 Nov 2026 22:21:06 +0100 Subject: [PATCH 04/43] mesh-config: read its own states from a watched copy and wake only on an input (issue 1982) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mesh-config sent about 262 requests a second and received about 1,540 since its start: every sources() call (a resolution pass, and a `delivered`, `manifest-reads-changed` or `manifest-accepted` event, each of which read the sources whether or not a window or a retirement was open) read every key of its own states by one get each (pulls 1,176 keys, resolved 447, declared 168, values 101...: about 1,200 requests for an event, 2,800 for a pass), serialised by one lock, so a delivery's burst of events kept it asking the bus back to back. - The module's own states are read from mesh-sdk's WatchedStates (one watch per state, its own writes taken into the copy) instead of a get per key (ownstates.go, main.go runtimeStore). - An event judges what it names: `delivered` and `manifest-reads-changed` read no source when no rename window is open, `manifest-accepted` reads none when every row is already marked or read again (renames.go windowOpen, retiring.go). - Open windows and retirements are judged again after each pass, since an entry that answers after the judgement was made moves the pass, not the event; the slow reads had hidden that race. - No timer polls: the five-minute resolution and sweep of every pull request on the forge, and the minute's read of the router's record, are gone. The sweep and Decide run once at start (an event said while the module was down is not said again), then on git.pull-merged/-closed and operator-channel.decided.mesh-config; an ask ended with no choice also takes the retirements from the router's record. One timer is left, a deadline: for the earliest waiting proposal (ProposalLasts + 1 h) or unanswered retirement (HeldFinding), set again when either is written (NextDeadline). Wake-ups kept, each by its record: `delivered` and `manifest-reads-changed` close a rename window (design 32 *A config value renamed*, ADR 0609 §4); `manifest-accepted` judges one module's retirements (design 32 *A value no longer read*); `git.pull-merged` and `-closed` settle one pull request's held values (ADR 0502 §7); `decided.mesh-config` takes one ask's warrant (ADR 0259); a changed view (assignment, want, predefined list, config candidate, catalog revision, entry) moves a resolution that writes only what moved (ADR 0565 §3). Builds: ADR 0681 §1 (no polling of the forge: the five-minute look ends); ADR 0580 §1; ADR 0426 §3; design 32 (*A config value renamed*, *A value no longer read*); lead answer A-490l (hq main, 03-DESIGN/01-to-be/47-milestones/G-22-lead-answers-2.md) for the wake-ups and the one deadline timer. Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-config/README.md | 2 +- modules/mesh-config/cmd/mesh-config/main.go | 113 +++++--- .../mesh-config/cmd/mesh-config/ownstates.go | 78 ++++++ .../mesh-config/cmd/mesh-config/proposals.go | 38 ++- modules/mesh-config/cmd/mesh-config/pulls.go | 2 +- .../cmd/mesh-config/quiet_integration_test.go | 135 +++++++++ .../mesh-config/cmd/mesh-config/quiet_test.go | 256 ++++++++++++++++++ .../mesh-config/cmd/mesh-config/renames.go | 16 +- .../mesh-config/cmd/mesh-config/retiring.go | 12 +- .../cmd/mesh-config/whole_integration_test.go | 18 +- 10 files changed, 623 insertions(+), 47 deletions(-) create mode 100644 modules/mesh-config/cmd/mesh-config/ownstates.go create mode 100644 modules/mesh-config/cmd/mesh-config/quiet_integration_test.go create mode 100644 modules/mesh-config/cmd/mesh-config/quiet_test.go diff --git a/modules/mesh-config/README.md b/modules/mesh-config/README.md index 99f368a43..df2bc093a 100644 --- a/modules/mesh-config/README.md +++ b/modules/mesh-config/README.md @@ -30,7 +30,7 @@ module none of whose manifests can be read is listed under `unresolved` with why values last resolved for it. Then: the manifest's default; mesh config for the predefined key the value falls back to; node config for that key on the node; the module's own value on that node, the most specific winning. A value naming no predefined key resolves from its default and the module's own value only (ADR 0348 §2). It resolves at -start, whenever an assignment, a want or a trunk module moves, and every five minutes, writing only the values that moved. +start, whenever an assignment, a want or a trunk module moves, writing only the values that moved. ## The verb diff --git a/modules/mesh-config/cmd/mesh-config/main.go b/modules/mesh-config/cmd/mesh-config/main.go index 09f0b32dd..b0d927dd8 100644 --- a/modules/mesh-config/cmd/mesh-config/main.go +++ b/modules/mesh-config/cmd/mesh-config/main.go @@ -38,15 +38,19 @@ func logf(format string, a ...any) { fmt.Fprintf(os.Stderr, format+"\n", a...) } var theViews *ownerViews // runtimeStore is the bus, through the runtime that launched this bundle. A state of the delivery owners that the -// owners' tools answer is read from the picture those keep (ownerviews.go), the rest from the bus. -type runtimeStore struct{} +// owners' tools answer is read from the picture those keep (ownerviews.go), this module's own states from the copy their +// watches keep (ownstates.go), the rest from the bus. +type runtimeStore struct{ own *ownCopy } -func (runtimeStore) Get(state, key string) (*Entry, error) { +func (r runtimeStore) Get(state, key string) (*Entry, error) { if theViews != nil { if e, followed, err := theViews.get(state, key); followed { return e, err } } + if r.own != nil && ownStates[state] { + return r.own.get(state, key) + } e, err := stdio.State(state).Get(key) if err != nil || e == nil { return nil, err @@ -54,19 +58,30 @@ func (runtimeStore) Get(state, key string) (*Entry, error) { return &Entry{Key: e.Key, Value: e.Value, Revision: e.Revision}, nil } -func (runtimeStore) Put(state, key string, value any) error { +func (r runtimeStore) Put(state, key string, value any) error { + if r.own != nil && ownStates[state] { + return r.own.write(state, key, value) + } _, err := stdio.State(state).Put(key, value) return err } -func (runtimeStore) Delete(state, key string) error { return stdio.State(state).Delete(key) } +func (r runtimeStore) Delete(state, key string) error { + if r.own != nil && ownStates[state] { + return r.own.remove(state, key) + } + return stdio.State(state).Delete(key) +} -func (runtimeStore) Keys(state string) ([]string, error) { +func (r runtimeStore) Keys(state string) ([]string, error) { if theViews != nil { if keys, followed, err := theViews.keys(state); followed { return keys, err } } + if r.own != nil && ownStates[state] { + return r.own.keys(state) + } return stdio.State(state).Keys() } @@ -120,7 +135,16 @@ func (runtimeAsks) Warranted(ask string) error { } func main() { - c := &Config{Store: runtimeStore{}, Logf: logf, Asks: runtimeAsks{}, Forge: toolForge{ask: stdio.Ask}, + own, rearm := newOwnCopy(), make(chan struct{}, 1) + own.changed = func(state string) { + if state == ProposalsState || state == RetiringState { + select { + case rearm <- struct{}{}: + default: + } + } + } + c := &Config{Store: runtimeStore{own: own}, Logf: logf, Asks: runtimeAsks{}, Forge: toolForge{ask: stdio.Ask}, Delivery: toolDelivery{ask: stdio.Ask}, Vault: toolVault{ask: stdio.Ask, recorder: recorder}} // The module's one held writer (design 47 G §689, A-16, A-29, A-32): the renames' and retirements' findings and the views reader's go through it. w, err := views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{ @@ -181,6 +205,7 @@ func main() { theViews.Start(context.Background()) go resolving(c, moved) go hearing(c) + go deciding(c, rearm) go hearingDelivery(c) if err := stdio.Serve("", append(Tools(c), branches.EntryAgainTool("config", c.Store.HandAct))); err != nil { logf("%v", err) @@ -188,23 +213,17 @@ func main() { } } -// resolving resolves at start and again whenever a source moves, and every five minutes besides, so a change -// heard nowhere is still resolved. The runtime answers only once Serve runs, so the watches are asked again -// until they are taken. +// resolving resolves at start and again whenever a source moves: an assignment, a want, a catalog revision, an entry +// answered, the predefined list or a config candidate (each reaches `moved` through its view, ownerviews.go and main). It +// reads the forge once for each pull request it keeps something for, so a merge or a close said while this module was down is +// found (ADR 0502); after that it is heard on git.pull-merged and git.pull-closed (hearing), never asked again on a timer +// (novox/hq principle 1, issue 1982). The runtime answers only once Serve runs. func resolving(c *Config, moved chan struct{}) { - // The owners' states reach `moved` through their views (ownerviews.go); the builder's builds, which has no tool yet, - // is read when a want moves, and nothing else is watched. - tick := time.NewTicker(5 * time.Minute) - defer tick.Stop() - time.Sleep(time.Second) - for sweep := true; ; { - if sweep { - // A merge or a close of a pull request a value is held for, heard nowhere, is found here (ADR 0502). - for _, s := range c.SweepPulls() { - logf("[mesh-config] %s", s) - } - } - sweep = false + <-stdio.Served() + for _, s := range c.SweepPulls() { + logf("[mesh-config] %s", s) + } + for { r, err := c.Reconcile() switch { case err != nil: @@ -215,21 +234,23 @@ func resolving(c *Config, moved chan struct{}) { if r.Published != "" { logf("[mesh-config] published %s", r.Published) } + // A window or a retirement judged while the entry of the build it waits on was still asked stays open until the + // entry answers: the answer moves this pass, and the open ones are judged again here (design 32 *A config value + // renamed*). It reads nothing from the bus when none is open. + for _, s := range c.Windows("") { + logf("[mesh-config] %s", s) + } + <-moved + time.Sleep(500 * time.Millisecond) // a burst of writes is one resolution select { case <-moved: - time.Sleep(500 * time.Millisecond) // a burst of writes is one resolution - select { - case <-moved: - default: - } - case <-tick.C: - sweep = true + default: } } } -// hearing takes the router's word on this module's asks, asked again until the runtime takes the subscription, and -// reads the router's record of every waiting ask once a minute besides, so an answer heard nowhere is still taken. +// hearing takes the router's word on this module's asks, asked again until the runtime takes the subscription, and the +// forge's word on a pull request's merge or close. func hearing(c *Config) { go func() { <-stdio.Served() // subscribed as soon as the runtime serves, never after a guessed wait: an event sent meanwhile is heard @@ -274,9 +295,15 @@ func hearing(c *Config) { } }(pattern) } - tick := time.NewTicker(time.Minute) - defer tick.Stop() - for range tick.C { +} + +// deciding reads the router's record of every waiting ask once at start, which finds an answer said while this module was +// down, and then when the router says an ask ended (hearing's `decided` subscription calls Decide itself) or the next +// deadline of a waiting proposal or retirement passes: one timer for the earliest, set again whenever a proposal or a +// retirement is written (issue 1982; novox/hq ADR 0426 §3: a timer is a deadline, never a poll). +func deciding(c *Config, rearm chan struct{}) { + <-stdio.Served() + decide := func() { said, err := c.Decide() if err != nil { logf("[mesh-config] reading the operator's answers: %v", err) @@ -285,6 +312,22 @@ func hearing(c *Config) { logf("[mesh-config] %s", s) } } + decide() + for { + at, ok := c.NextDeadline() + if !ok { + <-rearm + continue + } + // Not sooner than a minute: a deadline that passed and was not settled (an ask the router refused) is tried again, not spun on. + timer := time.NewTimer(max(time.Until(at), time.Minute)) + select { + case <-timer.C: + decide() + case <-rearm: + timer.Stop() + } + } } // hearingDelivery takes the delivery parts' words that close a rename window or retire a value, each asked again until diff --git a/modules/mesh-config/cmd/mesh-config/ownstates.go b/modules/mesh-config/cmd/mesh-config/ownstates.go new file mode 100644 index 000000000..642976427 --- /dev/null +++ b/modules/mesh-config/cmd/mesh-config/ownstates.go @@ -0,0 +1,78 @@ +package main + +import ( + "encoding/json" + + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// The module's own states are read from a copy each one's watch keeps, never key by key (novox/hq ADR 0580 §1, issue 1982): +// a resolution reads every key of values, resolved, pulls, declared and the rest, and a get per key for each of them, once per +// pass and once per delivery event, was about 4,000 requests to the bus every time. A state is watched on its first read, the +// watch hands every current value over before the read is answered, and this process's own writes are taken into the copy +// as they are made, so a read right after a write sees it. + +// ownStates are the states this module keeps and reads through its store (its manifest's `state`, but the findings, which its +// held writer alone reads and writes): the ones read from the copy. +var ownStates = map[string]bool{ + ValuesState: true, ResolvedState: true, PredefinedState: true, ProposalsState: true, PullsState: true, KeptState: true, + DeclaredState: true, RenamesState: true, RetiringState: true, +} + +// ownCopy is the copy of the module's own states over a bus: watch hands every current value of a state and then each change, +// get reads one key, put and del write one. +type ownCopy struct { + copy *stdio.WatchedStates + put func(state, key string, value any) (uint64, error) + del func(state, key string) error + // changed, when not nil, is told of each write or removal after it is in the copy: what waits on a deadline is judged again. + changed func(state string) +} + +// newOwnCopy is the copy over this bundle's runtime. +func newOwnCopy() *ownCopy { + return &ownCopy{copy: stdio.NewWatchedStates(), + put: func(state, key string, value any) (uint64, error) { return stdio.State(state).Put(key, value) }, + del: func(state, key string) error { return stdio.State(state).Delete(key) }} +} + +func (o *ownCopy) get(state, key string) (*Entry, error) { + raw, ok, err := o.copy.Get(state, key) + if err != nil || !ok { + return nil, err + } + return &Entry{Key: key, Value: raw}, nil +} + +func (o *ownCopy) keys(state string) ([]string, error) { return o.copy.Keys(state) } + +// write puts a key and takes it into the copy at the revision the bus answered. +func (o *ownCopy) write(state, key string, value any) error { + rev, err := o.put(state, key, value) + if err != nil { + return err + } + raw, err := json.Marshal(value) + if err != nil { + return err + } + err = o.copy.Wrote(state, key, raw, rev) + o.told(state) + return err +} + +func (o *ownCopy) told(state string) { + if o.changed != nil { + o.changed(state) + } +} + +// remove deletes a key and takes that into the copy. +func (o *ownCopy) remove(state, key string) error { + if err := o.del(state, key); err != nil { + return err + } + err := o.copy.Removed(state, key) + o.told(state) + return err +} diff --git a/modules/mesh-config/cmd/mesh-config/proposals.go b/modules/mesh-config/cmd/mesh-config/proposals.go index e38bee873..358f636e8 100644 --- a/modules/mesh-config/cmd/mesh-config/proposals.go +++ b/modules/mesh-config/cmd/mesh-config/proposals.go @@ -910,19 +910,51 @@ func (c *Config) HeardEnd(w oc.Warrant) []string { c.mu.Lock() defer c.mu.Unlock() defer c.tellEnded(w.Ask, endedWord{Outcome: w.Outcome, Words: w.Words}) + // A retirement's ask ended with no choice is taken from the router's record the same way (retiring.go): the router's word + // is the only thing that moves it, and no timer looks. + said := c.decideRetirements() e, err := c.Store.Get(ProposalsState, w.Ask) if err != nil || e == nil { - return nil + return said } var p Proposal if json.Unmarshal(e.Value, &p) != nil || p.ID != w.Ask || p.State != ProposalWaiting { - return nil + return said } why := "the ask " + string(w.Outcome) if w.Words != "" { why += " (" + w.Words + ")" } - return []string{c.end(p, why+"; nothing was set")} + return append(said, c.end(p, why+"; nothing was set")) +} + +// NextDeadline is when a waiting proposal or retirement is next judged for want of an answer: a proposal with no record +// at the router ends an hour after its ask lapses (Decide), and a retirement left unanswered is asked once more after +// HeldFinding. The earliest of them; false when nothing waits on a time. +func (c *Config) NextDeadline() (time.Time, bool) { + c.mu.Lock() + defer c.mu.Unlock() + var at time.Time + earlier := func(t time.Time) { + if at.IsZero() || t.Before(at) { + at = t + } + } + if all, err := c.proposals(); err == nil { + for _, p := range all { + if p.State == ProposalWaiting { + earlier(p.At.Add(ProposalLasts + time.Hour)) + } + } + } + if held, err := c.readRetiring(); err == nil { + for _, e := range held { + if e.State == RetireUnanswered && !e.AskedAgain { + earlier(e.Asked.Add(HeldFinding)) + } + } + } + return at, !at.IsZero() } // ---- the list ---------------------------------------------------------------------------------------------- diff --git a/modules/mesh-config/cmd/mesh-config/pulls.go b/modules/mesh-config/cmd/mesh-config/pulls.go index 50f01da94..3508a95bf 100644 --- a/modules/mesh-config/cmd/mesh-config/pulls.go +++ b/modules/mesh-config/cmd/mesh-config/pulls.go @@ -534,7 +534,7 @@ func (c *Config) HeardPull(p PullEvent, merged bool) []string { } // SweepPulls looks at the forge for every pull request it keeps something for, so a merge or a close it did not hear -// is found: run with each five-minute resolution. +// is found: run once at start (resolving). func (c *Config) SweepPulls() []string { if c.Forge == nil { return nil diff --git a/modules/mesh-config/cmd/mesh-config/quiet_integration_test.go b/modules/mesh-config/cmd/mesh-config/quiet_integration_test.go new file mode 100644 index 000000000..deb93d759 --- /dev/null +++ b/modules/mesh-config/cmd/mesh-config/quiet_integration_test.go @@ -0,0 +1,135 @@ +//go:build integration + +package main + +// Issue 1982, against a real bus: mesh-config whole asks the bus for nothing while no input of it changes, and a changed input +// resolves only what it touches. The binary runs as the node's tool runtime plays it (whole_integration_test.go), and every +// request it makes of the runtime is counted: what a state read, a watch and a write ask of the bus. + +import ( + "context" + "os" + "os/exec" + "path/filepath" + "slices" + "strings" + "testing" + + "github.com/nats-io/nats.go" + "github.com/nats-io/nats.go/jetstream" + + "git.novox.be/novox/mesh-sdk/go/testbus" +) + +func (p *player) snapshot() (calls map[string]int, puts []string) { + p.mu.Lock() + defer p.mu.Unlock() + calls = map[string]int{} + for k, v := range p.calls { + calls[k] = v + } + return calls, slices.Clone(p.puts) +} + +// asked is how many requests of these methods the module made between two snapshots. +func asked(before, after map[string]int, methods ...string) int { + n := 0 + for _, m := range methods { + n += after[m] - before[m] + } + return n +} + +func TestConfigWholeAsksTheBusForNothingWhileNoInputChanges(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + bin := filepath.Join(t.TempDir(), Module) + build := exec.Command("go", "build", "-o", bin, ".") + build.Stderr = os.Stderr + if err := build.Run(); err != nil { + t.Fatalf("mesh-config does not build: %v", err) + } + nc, err := nats.Connect(testbus.URL(t)) + if err != nil { + t.Fatal(err) + } + t.Cleanup(nc.Close) + js, err := jetstream.New(nc) + if err != nil { + t.Fatal(err) + } + buckets := []string{"mesh-delivery-assigner_assignments", "mesh-delivery-assigner_wants", "mesh-delivery-indexer_predefined", + "mesh-delivery-indexer_config-candidates"} + for _, s := range []string{ValuesState, ResolvedState, PredefinedState, ProposalsState, PullsState, KeptState, DeclaredState, + RenamesState, RetiringState, FindingsState} { + buckets = append(buckets, Module+"_"+s) + } + for _, b := range buckets { + if _, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: b, History: 2}); err != nil { + t.Fatal(err) + } + } + haves := map[string]string{"g14.player": fpOf("player", fp1), "novox.player": fpOf("player", fp1), "g14.radio": fpOf("radio", fp1)} + p := start(t, bin, js, newStandIn(), haves, nil) + p.viewsReady() + + const radio = `{"module":"radio","config":{"band":{"type":"number","default":1}}}` + p.manifestIn("player", oldPlayer) + p.manifestIn("radio", radio) + p.buildIn("g14", "player", fpOf("player", fp1), oldPlayer) + p.buildIn("novox", "player", fpOf("player", fp1), oldPlayer) + p.buildIn("g14", "radio", fpOf("radio", fp1), radio) + p.placed("g14", "player", "radio") + p.placed("novox", "player") + p.hearsTheViews() + eventually(t, "every value is resolved", func() bool { + a, okA := resolvedValue(p, "g14", "player", "volume") + b, okB := resolvedValue(p, "novox", "player", "volume") + c, okC := resolvedValue(p, "g14", "radio", "band") + return okA && okB && okC && a == "5" && b == "5" && c == "1" + }) + // The passes the setup moved have ended when the module answers a tool: it takes its lock for none. + if _, err := p.call(ResyncTool, nil); err != nil { + t.Fatal(err) + } + + // No input changes: the delivery says it delivered, a node-engine says what it reads, the forge says a pull request nothing is + // kept for closed, and the catalog says a manifest was accepted that no retirement waits on. Each is heard and none is a pass: + // nothing is asked of the bus. (The module has no timer: no minute, five or otherwise, asks it anything.) + before, putsBefore := p.snapshot() + for i := 0; i < 20; i++ { + p.event("mesh-delivery-view.delivered", map[string]any{"revision": "r"}) + p.event("node-engine.manifest-reads-changed", map[string]any{"node": "g14"}) + p.event("git.pull-closed", map[string]any{"owner": "novox", "repo": "mesh-catalog", "number": 700 + i}) + } + after, putsAfter := p.snapshot() + if n := asked(before, after, "mesh/state.get", "mesh/state.keys", "mesh/state.watch", "mesh/state.put", "mesh/state.create", + "mesh/state.update", "mesh/state.delete", "mesh/ask"); n != 0 || len(putsAfter) != len(putsBefore) { + t.Errorf("60 events that move nothing asked %d requests of the bus (%v); wrote %v", n, after, putsAfter[len(putsBefore):]) + } + + // One input changes: player's manifest declares a second value. Only player's values on its two nodes are written, and `declared` + // for player; radio's resolved values are not written again, and nothing is read key by key. + before, putsBefore = p.snapshot() + const gain = `{"module":"player","config":{"volume":{"type":"number","default":5},"gain":{"type":"number","default":2}}}` + p.manifestIn("player", gain) + eventually(t, "the new value is resolved on both nodes", func() bool { + a, okA := resolvedValue(p, "g14", "player", "gain") + b, okB := resolvedValue(p, "novox", "player", "gain") + return okA && okB && a == "2" && b == "2" + }) + after, putsAfter = p.snapshot() + var wrote []string + for _, w := range putsAfter[len(putsBefore):] { + wrote = append(wrote, strings.TrimPrefix(w, "mesh/state.")) + } + for _, w := range wrote { + if !strings.Contains(w, ResolvedState+"/g14.player.") && !strings.Contains(w, ResolvedState+"/novox.player.") && + !strings.Contains(w, DeclaredState+"/player") { + t.Errorf("a changed manifest of player wrote %q, which is not player's", w) + } + } + if n := asked(before, after, "mesh/state.get", "mesh/state.keys", "mesh/state.watch"); n != 0 { + t.Errorf("a changed manifest read %d keys of the bus's states one by one (%v)", n, after) + } +} diff --git a/modules/mesh-config/cmd/mesh-config/quiet_test.go b/modules/mesh-config/cmd/mesh-config/quiet_test.go new file mode 100644 index 000000000..d6f89e77c --- /dev/null +++ b/modules/mesh-config/cmd/mesh-config/quiet_test.go @@ -0,0 +1,256 @@ +package main + +// What mesh-config asks of the bus per input (issue 1982): its own states are read from the copy their watches keep, an event +// that moves nothing reads nothing, and the one timer left is a deadline. + +import ( + "encoding/json" + "sync" + "testing" + "time" + + occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient" + oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// countingStore counts the reads of the states by state: what the bus would be asked. +type countingStore struct { + *memStore + mu sync.Mutex + reads map[string]int +} + +func (s *countingStore) Get(state, key string) (*Entry, error) { + s.mu.Lock() + s.reads[state]++ + s.mu.Unlock() + return s.memStore.Get(state, key) +} + +func (s *countingStore) Keys(state string) ([]string, error) { + s.mu.Lock() + s.reads[state]++ + s.mu.Unlock() + return s.memStore.Keys(state) +} + +// readsOf is the reads of every state but those named. +func (s *countingStore) readsExcept(states ...string) map[string]int { + s.mu.Lock() + defer s.mu.Unlock() + out := map[string]int{} + for k, n := range s.reads { + out[k] = n + } + for _, k := range states { + delete(out, k) + } + return out +} + +// **An event that moves nothing reads nothing but what it judges** (design 32 *A config value renamed*, *A value no longer +// read*): with no rename window open, `delivered` and `manifest-reads-changed` read no source, and a `manifest-accepted` +// whose rows are all marked or read again reads none either. Each was a read of every key of every state of the module +// (about 4,000 requests) under a delivery's burst of events. +func TestAnEventThatMovesNothingReadsNoSource(t *testing.T) { + c, m, d, _, _, _ := retiringMesh(t) + cs := &countingStore{memStore: m, reads: map[string]int{}} + c.Store = cs + d.rows = []ReaderRow{{Module: "player", Key: "volume", Kind: KindValue, Readers: []string{"g14"}}} // read: nothing to retire + for name, event := range map[string]func(){ + "delivered": func() { c.Windows("") }, + "manifest-reads-changed": func() { c.Windows("g14") }, + "manifest-accepted": func() { c.Retirements("player") }, + } { + cs.reads = map[string]int{} + event() + if got := cs.readsExcept(RenamesState, RetiringState); len(got) != 0 { + t.Errorf("%s read %v", name, got) + } + } +} + +// An open window is judged, and reads what judging it needs: the same event with a window open reads the sources. +func TestAnEventWithAWindowOpenJudgesIt(t *testing.T) { + c, m, _, _ := aRenamingMesh(t) + m.declare(t, "player", newPlayer) + if _, err := c.Reconcile(); err != nil { + t.Fatal(err) + } + cs := &countingStore{memStore: m, reads: map[string]int{}} + c.Store = cs + c.Windows("") + if cs.reads[ValuesState] == 0 { + t.Errorf("an open window was judged on no source: %v", cs.reads) + } +} + +// **An ask of a retirement that ends with no choice is taken when the router says so** (design 32 *A value no longer read*): +// with no minute's look at the router's record, the `decided` event of an ask that expired moves the retirement. +func TestARetirementWhoseAskExpiredIsTakenOnTheRoutersWord(t *testing.T) { + c, m, d, r, _, _ := retiringMesh(t) + for _, node := range []string{"g14", "novox"} { + m.want(t, node, "player", fp3, donePlayer) + d.haves[node+".player"] = fp3 + } + c.Windows("") + c.Retirements("player") + rows := retiringOf(t, c) + if len(rows) != 1 || rows[0].State != RetireAsked { + t.Fatalf("retiring %+v, want asked", rows) + } + r.choose(t, rows[0].Ask, OptionApprove, func(w *oc.Warrant) { w.Outcome = occ.OutcomeExpired }) + c.HeardEnd(oc.Warrant{Ask: rows[0].Ask, Asker: Module, Outcome: occ.OutcomeExpired}) + if rows = retiringOf(t, c); rows[0].State != RetireUnanswered { + t.Errorf("after the router said the ask expired: %+v", rows) + } +} + +// The next deadline is the earliest of a waiting proposal's (an hour past its ask lapsing, with no record at the router) +// and an unanswered retirement's (asked once more after HeldFinding); none when nothing waits on a time. +func TestTheNextDeadlineIsTheEarliestOfWhatWaitsOnATime(t *testing.T) { + m := newStore() + c := &Config{Store: m, Catalog: m, Logf: t.Logf} + if _, ok := c.NextDeadline(); ok { + t.Fatal("a deadline with nothing waiting") + } + t0 := time.Date(2026, 11, 19, 10, 0, 0, 0, time.UTC) + put := func(state, key string, v any) { + if err := m.Put(state, key, v); err != nil { + t.Fatal(err) + } + } + put(ProposalsState, "p1", Proposal{ID: "p1", State: ProposalWaiting, At: t0.Add(48 * time.Hour)}) + put(ProposalsState, "p2", Proposal{ID: "p2", State: ProposalEnded, At: t0}) + want := t0.Add(48*time.Hour + ProposalLasts + time.Hour) + if at, ok := c.NextDeadline(); !ok || !at.Equal(want) { + t.Errorf("deadline %v, %v; want %v", at, ok, want) + } + put(RetiringState, "player.volume", Retiring{Module: "player", Key: "volume", State: RetireUnanswered, Asked: t0.Add(-6 * 24 * time.Hour)}) + want = t0.Add(HeldFinding - 6*24*time.Hour) + if at, ok := c.NextDeadline(); !ok || !at.Equal(want) { + t.Errorf("deadline %v, %v; want the retirement's %v", at, ok, want) + } +} + +// busOf is a bus of states for the copy's tests: what each state holds, the watches handed out and the requests asked. +type busOf struct { + mu sync.Mutex + held map[string]map[string][]byte + handles map[string]func(stdio.StateChange) error + watches int + gets int + writes int + revision uint64 +} + +func (b *busOf) ownCopy() *ownCopy { + return &ownCopy{ + copy: stdio.WatchedStatesOver(func(state string, handle func(stdio.StateChange) error) error { + b.mu.Lock() + b.watches++ + b.handles[state] = handle + current := map[string][]byte{} + for k, v := range b.held[state] { + current[k] = v + } + b.mu.Unlock() + for k, v := range current { + if err := handle(stdio.StateChange{State: state, Key: k, Op: stdio.OpPut, Value: v, Revision: 1, Current: true}); err != nil { + return err + } + } + return nil + }, func(state, key string) ([]byte, bool, error) { + b.mu.Lock() + defer b.mu.Unlock() + b.gets++ + v, ok := b.held[state][key] + return v, ok, nil + }), + put: func(state, key string, value any) (uint64, error) { + raw, _ := json.Marshal(value) + b.mu.Lock() + defer b.mu.Unlock() + b.writes++ + b.revision++ + if b.held[state] == nil { + b.held[state] = map[string][]byte{} + } + b.held[state][key] = raw + return b.revision + 1, nil + }, + del: func(state, key string) error { + b.mu.Lock() + defer b.mu.Unlock() + b.writes++ + delete(b.held[state], key) + return nil + }} +} + +// **The module's own states are read from a copy, never key by key** (ADR 0580 §1, issue 1982): a whole state read of 1,176 +// keys is one watch and no get, again and again; this process's write is in the copy at once, another's arrives by the watch. +func TestAnOwnStateIsReadFromItsWatchedCopyNotKeyByKey(t *testing.T) { + b := &busOf{held: map[string]map[string][]byte{PullsState: {}}, handles: map[string]func(stdio.StateChange) error{}} + for i := 0; i < 1176; i++ { + b.held[PullsState][pullKey("novox/mesh-catalog#"+string(rune('a'+i%26))+time.Duration(i).String())] = []byte(`{"pull":"x"}`) + } + n := len(b.held[PullsState]) + own := b.ownCopy() + for pass := 0; pass < 3; pass++ { + keys, err := own.keys(PullsState) + if err != nil || len(keys) != n { + t.Fatalf("keys %d, %v; want %d", len(keys), err, n) + } + for _, k := range keys { + if e, err := own.get(PullsState, k); err != nil || e == nil { + t.Fatalf("get %s: %v, %v", k, e, err) + } + } + } + if b.watches != 1 || b.gets != 0 { + t.Errorf("3 passes over %d keys asked the bus for %d watches and %d gets; want 1 and 0", n, b.watches, b.gets) + } + // Its own write and removal are in the copy at once. + if err := own.write(PullsState, "mine", map[string]string{"pull": "mine"}); err != nil { + t.Fatal(err) + } + if e, _ := own.get(PullsState, "mine"); e == nil { + t.Error("a key just written is not read") + } + if err := own.remove(PullsState, "mine"); err != nil { + t.Fatal(err) + } + if e, _ := own.get(PullsState, "mine"); e != nil { + t.Error("a key just removed is still read") + } + // Another writer's change arrives by the watch. + if err := b.handles[PullsState](stdio.StateChange{State: PullsState, Key: "theirs", Op: stdio.OpPut, Value: []byte(`{"pull":"t"}`), Revision: 99}); err != nil { + t.Fatal(err) + } + if e, _ := own.get(PullsState, "theirs"); e == nil { + t.Error("a change another wrote is not read") + } + if b.gets != 0 { + t.Errorf("%d gets", b.gets) + } +} + +// A write and a removal tell what waits on a deadline, once each. +func TestAWriteTellsWhatWaitsOnADeadline(t *testing.T) { + b := &busOf{held: map[string]map[string][]byte{}, handles: map[string]func(stdio.StateChange) error{}} + own := b.ownCopy() + var told []string + own.changed = func(state string) { told = append(told, state) } + if err := own.write(ProposalsState, "p", Proposal{ID: "p"}); err != nil { + t.Fatal(err) + } + if err := own.remove(ProposalsState, "p"); err != nil { + t.Fatal(err) + } + if len(told) != 2 || told[0] != ProposalsState { + t.Errorf("told %v", told) + } +} diff --git a/modules/mesh-config/cmd/mesh-config/renames.go b/modules/mesh-config/cmd/mesh-config/renames.go index f7338a38a..64417796f 100644 --- a/modules/mesh-config/cmd/mesh-config/renames.go +++ b/modules/mesh-config/cmd/mesh-config/renames.go @@ -293,14 +293,26 @@ func (c *Config) Windows(node string) []string { return said } +// windowOpen says whether any rename holds a window open on the node named, or on any node when none is named. +func windowOpen(renames map[string]Rename, only string) bool { + for _, r := range renames { + for _, node := range r.Open() { + if only == "" || only == node { + return true + } + } + } + return false +} + // closeWindows is Windows' rename half; the caller holds the lock. func (c *Config) closeWindows(only string) []string { renames, err := c.readRenames() if err != nil { return []string{"the renames cannot be read: " + err.Error()} } - if c.Delivery == nil { - return nil + if c.Delivery == nil || !windowOpen(renames, only) { + return nil // no window to judge: the delivery's word moves nothing, and no source is read for it (issue 1982) } s, err := c.sources() if err != nil { diff --git a/modules/mesh-config/cmd/mesh-config/retiring.go b/modules/mesh-config/cmd/mesh-config/retiring.go index 6f6bd2c16..3be623f7c 100644 --- a/modules/mesh-config/cmd/mesh-config/retiring.go +++ b/modules/mesh-config/cmd/mesh-config/retiring.go @@ -148,9 +148,15 @@ func (c *Config) Retirements(module string) []string { if err != nil { return []string{"the retirements cannot be read: " + err.Error()} } - s, err := c.sources() - if err != nil { - return []string{"the retirements cannot be judged: " + err.Error()} + // The sources are read only when a row is one to judge: a row already marked, or read again, needs none (issue 1982). + var s Sources + for _, row := range rows { + if _, has := held[retiringKey(module, row.Key)]; (row.Module == "" || row.Module == module) && (len(row.Readers) == 0 && !has) { + if s, err = c.sources(); err != nil { + return []string{"the retirements cannot be judged: " + err.Error()} + } + break + } } var said []string for _, row := range rows { diff --git a/modules/mesh-config/cmd/mesh-config/whole_integration_test.go b/modules/mesh-config/cmd/mesh-config/whole_integration_test.go index 9cc30a578..83d9af240 100644 --- a/modules/mesh-config/cmd/mesh-config/whole_integration_test.go +++ b/modules/mesh-config/cmd/mesh-config/whole_integration_test.go @@ -81,6 +81,11 @@ type player struct { catalog map[string]catalog.CatalogEntry byFp map[string]catalog.CatalogEntry catRev int64 + // calls counts what the module asked of the runtime, by method (and by key for an ask); puts are the keys it wrote, "/". + calls map[string]int + puts []string + // stop ends the module this player runs: the restart of a module is the old process gone and a new one started. + stop func() } // ownedState is one state of an owner (the indexer's modules, the assigner's wants...), kept from its bucket. @@ -232,6 +237,12 @@ func (p *player) handle(m wire) { Bucket string `json:"bucket"` } _ = json.Unmarshal(m.Params, &a) + p.mu.Lock() + p.calls[m.Method]++ + if strings.HasPrefix(m.Method, "mesh/state.") && m.Method != "mesh/state.get" && m.Method != "mesh/state.keys" && m.Method != "mesh/state.watch" { + p.puts = append(p.puts, m.Method+" "+a.State+"/"+a.Key) + } + p.mu.Unlock() kv, kvErr := p.kv(a.State) switch m.Method { case "mesh/state.get": @@ -675,14 +686,16 @@ func start(t *testing.T, bin string, js jetstream.JetStream, router *standIn, ha stdout, _ := cmd.StdoutPipe() cmd.Stderr = os.Stderr p := &player{t: t, ctx: ctx, js: js, router: router, in: stdin, pending: map[string]chan wire{}, haves: haves, readers: readers, - owned: map[string]*ownedState{}, catalog: map[string]catalog.CatalogEntry{}, byFp: map[string]catalog.CatalogEntry{}} + owned: map[string]*ownedState{}, catalog: map[string]catalog.CatalogEntry{}, byFp: map[string]catalog.CatalogEntry{}, + calls: map[string]int{}} for _, f := range followed { p.own(f.Read) } if err := cmd.Start(); err != nil { t.Fatal(err) } - t.Cleanup(func() { _ = stdin.Close(); cancel(); _ = cmd.Wait() }) + p.stop = func() { _ = stdin.Close(); cancel(); _ = cmd.Wait() } + t.Cleanup(p.stop) go p.serve(stdout) t.Cleanup(cancel) return p @@ -785,6 +798,7 @@ func TestConfigWholeRenamesAndRetiresAgainstTheBus(t *testing.T) { // The module restarted inside the window keeps it, and says renamed no more. said := p.publishedCount(EventRenamed) + p.stop() p2 := start(t, bin, js, router, haves, nil) p2.viewsReady() if _, err := p2.call(ResyncTool, nil); err != nil { -- 2.54.0 From a30852939c25dba95ce94df3723bee71c1bf8d01 Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 19 Nov 2026 22:42:28 +0100 Subject: [PATCH 05/43] =?UTF-8?q?mesh-config:=20remove=20the=20pull=20requ?= =?UTF-8?q?est=20path=20(issue=201982;=20ADR=200681=20=C2=A71,=20=C2=A73,?= =?UTF-8?q?=20=C2=A75,=20=C2=A76)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The mesh knows no pull request (design 47 G, G-1 §38-39). Removed: - the states `pulls` and `kept` and all code that reads or writes them (pulls.go, the held values a proposal holds, the listing of held values), SweepPulls, settlePull, HeardPull and the forge call; - the consumes of git.pull-merged and git.pull-closed, the invoke of seat:git.pull; - the config verb's `pull`, `head`, `declares` and `pulls` arguments and its actions `pull-declares` and `held`; - the indexer's config candidates, which only that path used: the consume of mesh-delivery-indexer.views.config-candidates and the invoke of indexer_read_config_candidates (the indexer's state goes in the same group). A stored proposal that names a pull request is judged not one this module makes, so nothing is set on it. Builds: ADR 0681 §1 (no pulls or kept, no git events, no seat:git.pull, no `pull` argument, no pull-declares or held), §3, §5, §6; lead answer A-490l (a stored proposal naming a pull request is refused on its approval). Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-config/README.md | 5 +- modules/mesh-config/cmd/mesh-config/config.go | 24 +- .../mesh-config/cmd/mesh-config/delivery.go | 34 + modules/mesh-config/cmd/mesh-config/list.go | 6 - modules/mesh-config/cmd/mesh-config/main.go | 71 +- .../cmd/mesh-config/manifest_test.go | 6 +- .../mesh-config/cmd/mesh-config/ownerviews.go | 8 +- .../mesh-config/cmd/mesh-config/ownstates.go | 5 +- .../mesh-config/cmd/mesh-config/proposals.go | 88 +-- .../cmd/mesh-config/pullforge_test.go | 54 -- modules/mesh-config/cmd/mesh-config/pulls.go | 644 ------------------ .../mesh-config/cmd/mesh-config/pulls_test.go | 504 -------------- .../cmd/mesh-config/quiet_integration_test.go | 11 +- .../mesh-config/cmd/mesh-config/quiet_test.go | 39 +- .../mesh-config/cmd/mesh-config/resolve.go | 4 - .../mesh-config/cmd/mesh-config/sources.go | 12 +- .../mesh-config/cmd/mesh-config/takeover.go | 90 +-- .../cmd/mesh-config/takeover_test.go | 26 +- .../cmd/mesh-config/whole_integration_test.go | 5 +- modules/mesh-config/module.json | 7 - 20 files changed, 113 insertions(+), 1530 deletions(-) delete mode 100644 modules/mesh-config/cmd/mesh-config/pullforge_test.go delete mode 100644 modules/mesh-config/cmd/mesh-config/pulls.go delete mode 100644 modules/mesh-config/cmd/mesh-config/pulls_test.go diff --git a/modules/mesh-config/README.md b/modules/mesh-config/README.md index df2bc093a..5e5e3dd22 100644 --- a/modules/mesh-config/README.md +++ b/modules/mesh-config/README.md @@ -87,8 +87,9 @@ Each directory or archive this module declares says how it treats a file it did - `mesh-config_declared`: each placed module's declarations at the trunk, with the manifest and commit they come from, written only when they differ. - Its own sources hold always (novox/hq ADR 0590): declarations from the modules' own manifests, the list from the - indexer's `predefined`, pull requests' config from the indexer's `config-candidates`. The controller's - `pull-declares`, the take-over, compare and hand-back went with the controller. `config_declared` and + indexer's `predefined`. The controller's `pull-declares`, the take-over, compare and hand-back went with the + controller, and so did every pull request path (`pulls`, `kept`, `held`, a proposal's `pull`): the mesh knows no pull + request (design 47 G, ADR 0502 ended by the operator's word, record pending). `config_declared` and `config_resync` read and write again what it keeps. - A trusted value is set only through propose and the operator's Approve: no controller relay is trusted (issue 1126). - Its sets, clears and warrants are recorded in the hand-act log of the seat `mesh-hand-acts` alone. diff --git a/modules/mesh-config/cmd/mesh-config/config.go b/modules/mesh-config/cmd/mesh-config/config.go index e50109780..84542fd7a 100644 --- a/modules/mesh-config/cmd/mesh-config/config.go +++ b/modules/mesh-config/cmd/mesh-config/config.go @@ -73,10 +73,6 @@ type Change struct { Approved string `json:"approved,omitempty"` At time.Time `json:"at"` Moved []Moved `json:"moved"` - // Pull and Head name the pull request a value is held for, dropped from, or applied from at its merge (novox/hq - // ADR 0502): the actions hold and drop are always about one, and a set with a pull is its merge. - Pull string `json:"pull,omitempty"` - Head string `json:"head,omitempty"` } // Config is the module: its store, its clock, and one lock, so a set and a resolution never interleave. @@ -94,8 +90,6 @@ type Config struct { // Asks is the operator channel as this module asks on it (proposals.go); nil, nothing can be proposed. Asks AskBus - // Forge is the forge as pulls are asked of it (pulls.go); nil, a merge or a close is heard from its events alone. - Forge PullForge // Delivery is what the delivery parts answer (renames.go, retiring.go); nil, no window closes and nothing retires. Delivery Delivery // Catalog is the catalog half: which modules exist and the manifest each entry carries, and the entry of a build's @@ -130,13 +124,6 @@ type Call struct { // Clear asks a proposal to clear the value instead of setting it; Proposal names one proposal to read. Clear bool Proposal string - // Pull names the open pull request that introduces the key, for propose (a held value) and pull-declares; Head is - // the commit its declarations are read at; Declares the config declarations the head adds or changes, by module; - // Pulls the pull requests held asks about, each at a head. - Pull string - Head string - Declares map[string]any - Pulls []PullAt // approval is the operator's warrant a set or a clear is made on: given by a decided proposal alone, never by // a call's arguments. @@ -177,13 +164,8 @@ func (c *Config) Do(call Call) (any, error) { return c.Propose(call) case "proposals": return c.Proposals(call) - case "pull-declares": - return c.PullDeclares(call) - case "held": - return c.Held(call) } - return nil, fmt.Errorf("config does set, clear, list, history, propose, proposals, pull-declares or held; %q is "+ - "none of them", call.Action) + return nil, fmt.Errorf("config does set, clear, list, history, propose or proposals; %q is none of them", call.Action) } // level is the level a call names, refused as ADR 0348 §3 says: a module's own value is for one node. @@ -211,10 +193,6 @@ func (c *Config) Change(call Call) (*SetAnswer, error) { if call.Clear || call.Proposal != "" { return nil, fmt.Errorf("clear and proposal are a proposal's: %s takes neither", call.Action) } - if call.Pull != "" { - return nil, fmt.Errorf("pull names the pull request a value is held for, which only a proposal does: %s takes "+ - "none", call.Action) - } call.approval = nil // only a decided proposal gives one c.mu.Lock() defer c.mu.Unlock() diff --git a/modules/mesh-config/cmd/mesh-config/delivery.go b/modules/mesh-config/cmd/mesh-config/delivery.go index 2ab1babe1..8490cad49 100644 --- a/modules/mesh-config/cmd/mesh-config/delivery.go +++ b/modules/mesh-config/cmd/mesh-config/delivery.go @@ -131,3 +131,37 @@ func (v toolVault) Retire(r RetireRequest) error { _, err = v.ask(VaultRetireAsk, map[string]any{"node": r.Node, "module": r.Module, "name": r.Name, "warrant": rec.Act.ID, "ask": r.Ask}) return err } + +// unwrapAnswer reads a tool's answer whatever wraps it: the protocol's content list, or {output, ok, answer}. +func unwrapAnswer(raw json.RawMessage) (json.RawMessage, string, bool) { + var s string + if json.Unmarshal(raw, &s) == nil { + return unwrapAnswer(json.RawMessage(s)) + } + var m map[string]json.RawMessage + if json.Unmarshal(raw, &m) != nil { + return raw, string(raw), true + } + if content, has := m["content"]; has { + var items []struct { + Text string `json:"text"` + } + var e struct { + IsError bool `json:"isError"` + } + _ = json.Unmarshal(raw, &e) + if json.Unmarshal(content, &items) == nil && len(items) > 0 { + inner, out, ok := unwrapAnswer(json.RawMessage(items[0].Text)) + return inner, out, ok && !e.IsError + } + } + var v struct { + Output string `json:"output"` + OK *bool `json:"ok"` + Answer json.RawMessage `json:"answer"` + } + if json.Unmarshal(raw, &v) == nil && v.OK != nil { + return v.Answer, v.Output, *v.OK + } + return raw, string(raw), true +} diff --git a/modules/mesh-config/cmd/mesh-config/list.go b/modules/mesh-config/cmd/mesh-config/list.go index 099bafaf9..4aa8912c4 100644 --- a/modules/mesh-config/cmd/mesh-config/list.go +++ b/modules/mesh-config/cmd/mesh-config/list.go @@ -19,8 +19,6 @@ type Listed struct { Withheld string `json:"withheld,omitempty"` // Unresolved is each placed module the call narrows to whose declarations cannot be read, with why (issue 1953). Unresolved []Unresolved `json:"unresolved,omitempty"` - // Held is the approved values held for open pull requests, not yet set (novox/hq ADR 0502). - Held []HeldValue `json:"held,omitempty"` } // SetValue is one value set, at its level. @@ -95,10 +93,6 @@ func (c *Config) List(call Call) (*Listed, error) { if call.Module == "" { out.Predefined = s.Predefined } - out.Held = heldListed(s, call) - if len(out.Held) == 0 { - out.Held = nil - } return out, nil } diff --git a/modules/mesh-config/cmd/mesh-config/main.go b/modules/mesh-config/cmd/mesh-config/main.go index b0d927dd8..3c05a9130 100644 --- a/modules/mesh-config/cmd/mesh-config/main.go +++ b/modules/mesh-config/cmd/mesh-config/main.go @@ -144,7 +144,7 @@ func main() { } } } - c := &Config{Store: runtimeStore{own: own}, Logf: logf, Asks: runtimeAsks{}, Forge: toolForge{ask: stdio.Ask}, + c := &Config{Store: runtimeStore{own: own}, Logf: logf, Asks: runtimeAsks{}, Delivery: toolDelivery{ask: stdio.Ask}, Vault: toolVault{ask: stdio.Ask, recorder: recorder}} // The module's one held writer (design 47 G §689, A-16, A-29, A-32): the renames' and retirements' findings and the views reader's go through it. w, err := views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{ @@ -214,15 +214,10 @@ func main() { } // resolving resolves at start and again whenever a source moves: an assignment, a want, a catalog revision, an entry -// answered, the predefined list or a config candidate (each reaches `moved` through its view, ownerviews.go and main). It -// reads the forge once for each pull request it keeps something for, so a merge or a close said while this module was down is -// found (ADR 0502); after that it is heard on git.pull-merged and git.pull-closed (hearing), never asked again on a timer +// answered or the predefined list (each reaches `moved` through its view, ownerviews.go and main), and never on a timer // (novox/hq principle 1, issue 1982). The runtime answers only once Serve runs. func resolving(c *Config, moved chan struct{}) { <-stdio.Served() - for _, s := range c.SweepPulls() { - logf("[mesh-config] %s", s) - } for { r, err := c.Reconcile() switch { @@ -249,8 +244,7 @@ func resolving(c *Config, moved chan struct{}) { } } -// hearing takes the router's word on this module's asks, asked again until the runtime takes the subscription, and the -// forge's word on a pull request's merge or close. +// hearing takes the router's word on this module's asks, asked again until the runtime takes the subscription. func hearing(c *Config) { go func() { <-stdio.Served() // subscribed as soon as the runtime serves, never after a guessed wait: an event sent meanwhile is heard @@ -272,29 +266,6 @@ func hearing(c *Config) { time.Sleep(wait) } }() - for _, pattern := range []string{"git.pull-merged", "git.pull-closed"} { - go func(pattern string) { - <-stdio.Served() // subscribed as soon as the runtime serves, never after a guessed wait: an event sent meanwhile is heard - for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) { - err := stdio.Subscribe(pattern, func(e stdio.Envelope) error { - p, ok := pullEventOf(e.Body) - if !ok { - logf("[mesh-config] %s is not readable", pattern) - return nil - } - for _, s := range c.HeardPull(p, pattern == "git.pull-merged") { - logf("[mesh-config] %s", s) - } - return nil - }) - if err == nil { - return - } - logf("[mesh-config] does not hear %s yet (%v); asking again in %s", pattern, err, wait) - time.Sleep(wait) - } - }(pattern) - } } // deciding reads the router's record of every waiting ask once at start, which finds an answer said while this module was @@ -422,13 +393,10 @@ func Tools(c *Config) []stdio.Tool { "who and why. A trusted value is set only on the operator's Approve: " + "propose asks the operator, under mesh-config's own name on a channel that proves who answered, and sets " + "the value on Approve if what it was shown against still stands; proposals lists them, or one with " + - "proposal. A proposal naming the open pull request that introduces its key (pull) is judged against what " + - "that pull request declares and asked of the operator, and on Approve is held until the pull request " + - "merges, then set; list shows the held values. pull-declares and held are the controller's. list and " + - "history are narrowed by module, node and key.", + "proposal. list and history are narrowed by module, node and key.", Input: map[string]any{"type": "object", "properties": map[string]any{ "action": map[string]any{"type": "string", - "enum": []string{"list", "set", "clear", "history", "propose", "proposals", "pull-declares", "held"}, + "enum": []string{"list", "set", "clear", "history", "propose", "proposals"}, "description": "list when not given"}, "key": map[string]any{"type": "string", "description": "the config key"}, "value": map[string]any{"description": "for set: the value, of the key's declared type"}, @@ -441,14 +409,6 @@ func Tools(c *Config) []stdio.Tool { "clear": map[string]any{"type": "boolean", "description": "for propose: propose clearing the value at its level instead of setting one"}, "proposal": map[string]any{"type": "string", "description": "for proposals: one proposal's id, read whole"}, - "pull": map[string]any{"type": "string", "description": "owner/repository#number: for propose, the open " + - "pull request that introduces the key, so the value is judged against what it declares and held until " + - "it merges (a module's own value on one node); for pull-declares, the pull request declared for"}, - "head": map[string]any{"type": "string", "description": "for pull-declares: the commit the declarations are read at"}, - "declares": map[string]any{"type": "object", "description": "for pull-declares (the controller's): the " + - "config declarations the head adds or changes against its base, by module and key"}, - "pulls": map[string]any{"type": "array", "description": "for held (the controller's): the pull requests " + - "asked about, each {pull, head}", "items": map[string]any{"type": "object"}}, }}, Run: func(args map[string]any) (any, error) { call, err := callOf(args) @@ -464,8 +424,7 @@ func Tools(c *Config) []stdio.Tool { // callOf reads a call's arguments strictly: an argument it does not know is refused, not ignored. func callOf(args map[string]any) (Call, error) { known := map[string]bool{"action": true, "key": true, "value": true, "module": true, "node": true, "why": true, - "by": true, "limit": true, "clear": true, "proposal": true, "pull": true, "head": true, "declares": true, - "pulls": true} + "by": true, "limit": true, "clear": true, "proposal": true} var unknown []string for k := range args { if !known[k] { @@ -474,7 +433,7 @@ func callOf(args map[string]any) (Call, error) { } if len(unknown) > 0 { return Call{}, fmt.Errorf("config takes no %s; it takes action, key, value, module, node, why, by, limit, "+ - "clear, proposal, pull, head, declares and pulls", + "clear and proposal", strings.Join(unknown, ", ")) } str := func(k string) (string, error) { @@ -491,8 +450,7 @@ func callOf(args map[string]any) (Call, error) { var call Call var problems []error for k, into := range map[string]*string{"action": &call.Action, "key": &call.Key, "module": &call.Module, - "node": &call.Node, "why": &call.Why, "by": &call.By, "proposal": &call.Proposal, "pull": &call.Pull, - "head": &call.Head} { + "node": &call.Node, "why": &call.Why, "by": &call.By, "proposal": &call.Proposal} { s, err := str(k) problems = append(problems, err) *into = s @@ -511,19 +469,6 @@ func callOf(args map[string]any) (Call, error) { } call.Clear = b } - if v, ok := args["declares"]; ok && v != nil { - m, ok := v.(map[string]any) - if !ok { - problems = append(problems, errors.New("declares is an object: a module to its keys' declarations")) - } - call.Declares = m - } - if v, ok := args["pulls"]; ok && v != nil { - raw, _ := json.Marshal(v) - if err := json.Unmarshal(raw, &call.Pulls); err != nil { - problems = append(problems, errors.New("pulls is a list of {pull, head}")) - } - } if v, ok := args["value"]; ok { // The value as given, a JSON value: a number for a number, never its text; the validation judges it. call.Value = v diff --git a/modules/mesh-config/cmd/mesh-config/manifest_test.go b/modules/mesh-config/cmd/mesh-config/manifest_test.go index e561cba26..f2dc32240 100644 --- a/modules/mesh-config/cmd/mesh-config/manifest_test.go +++ b/modules/mesh-config/cmd/mesh-config/manifest_test.go @@ -50,10 +50,10 @@ func TestEveryStateAndActHasItsTool(t *testing.T) { } via := map[string]string{ ValuesState: "config list", ResolvedState: "config list", PredefinedState: "config list", - ProposalsState: "config proposals", PullsState: "config list", KeptState: "config list", HistoryLog: "config history", + ProposalsState: "config proposals", HistoryLog: "config history", DeclaredState: DeclaredTool, - AssignmentsRead: "config list", WantsRead: "config list", IndexerPredefinedRead: "config list", CandidatesRead: ResyncTool, - "a resolution": ResyncTool, "the indexer's candidates taken": ResyncTool, + AssignmentsRead: "config list", WantsRead: "config list", IndexerPredefinedRead: "config list", + "a resolution": ResyncTool, // design 32: each new state with the tool that reads it, each act with the tool that starts it. RenamesState: RenamesTool, RetiringState: RetiringTool, FindingsState: "config_renames and config_retiring", "a move refused": RenameAgainTool, "a window dropped": RenameCloseTool, "an ask again": RetiringTool, diff --git a/modules/mesh-config/cmd/mesh-config/ownerviews.go b/modules/mesh-config/cmd/mesh-config/ownerviews.go index a677a0e8c..8ff66ee26 100644 --- a/modules/mesh-config/cmd/mesh-config/ownerviews.go +++ b/modules/mesh-config/cmd/mesh-config/ownerviews.go @@ -1,8 +1,8 @@ package main // What mesh-config reads of the delivery owners' state, through their tools and views families (novox/hq ADR 0615 §1, -// §4 to §6, ADR 0596): never a bucket of theirs. For each of the four states it follows (the indexer's predefined and -// config-candidates, the assigner's assignments and wants) it asks the owner's read tool for the whole +// §4 to §6, ADR 0596): never a bucket of theirs. For each of the three states it follows (the indexer's predefined, the +// assigner's assignments and wants) it asks the owner's read tool for the whole // state at its start, keeps the picture from the owner's `views.` events, and asks again only on a gap. Started // while an owner is down it keeps asking, and past two minutes writes a finding naming the owner and the tool. // @@ -28,7 +28,6 @@ import ( // The tools of the owners that mesh-config asks, by the state they read. const ( IndexerPredefTool = "indexer_read_predefined" - IndexerCandidatesTool = "indexer_read_config_candidates" AssignerAssignmentsTool = "assigner_read_assignments" AssignerWantsTool = "assigner_read_wants" @@ -40,7 +39,6 @@ const ( // state and its tool. var followed = []struct{ Read, Owner, State, Tool string }{ {IndexerPredefinedRead, indexerModule, catalog.StatePredefined, IndexerPredefTool}, - {CandidatesRead, indexerModule, catalog.StateConfigCandidates, IndexerCandidatesTool}, {AssignmentsRead, assignerModule, "assignments", AssignerAssignmentsTool}, {WantsRead, assignerModule, "wants", AssignerWantsTool}, } @@ -189,7 +187,7 @@ func viewTools(c *Config, o func() *ownerViews) []stdio.Tool { return []stdio.Tool{ {Name: FirstViewsTool, Description: "mesh-config's first views of the delivery owners' state: for each of the indexer's " + - "predefined and config-candidates, the assigner's assignments and wants and the catalog half's registrations and " + + "predefined, the assigner's assignments and wants and the catalog half's registrations and " + "each watched branch's revision, the tool it asks, the revision its " + "picture holds, when it asked and was answered, how many asks (and by hand), and whether its condition is raised.", Run: func(map[string]any) (any, error) { diff --git a/modules/mesh-config/cmd/mesh-config/ownstates.go b/modules/mesh-config/cmd/mesh-config/ownstates.go index 642976427..b27778d50 100644 --- a/modules/mesh-config/cmd/mesh-config/ownstates.go +++ b/modules/mesh-config/cmd/mesh-config/ownstates.go @@ -7,7 +7,7 @@ import ( ) // The module's own states are read from a copy each one's watch keeps, never key by key (novox/hq ADR 0580 §1, issue 1982): -// a resolution reads every key of values, resolved, pulls, declared and the rest, and a get per key for each of them, once per +// a resolution reads every key of values, resolved, declared and the rest, and a get per key for each of them, once per // pass and once per delivery event, was about 4,000 requests to the bus every time. A state is watched on its first read, the // watch hands every current value over before the read is answered, and this process's own writes are taken into the copy // as they are made, so a read right after a write sees it. @@ -15,8 +15,7 @@ import ( // ownStates are the states this module keeps and reads through its store (its manifest's `state`, but the findings, which its // held writer alone reads and writes): the ones read from the copy. var ownStates = map[string]bool{ - ValuesState: true, ResolvedState: true, PredefinedState: true, ProposalsState: true, PullsState: true, KeptState: true, - DeclaredState: true, RenamesState: true, RetiringState: true, + ValuesState: true, ResolvedState: true, PredefinedState: true, ProposalsState: true, DeclaredState: true, RenamesState: true, RetiringState: true, } // ownCopy is the copy of the module's own states over a bus: watch hands every current value of a state and then each change, diff --git a/modules/mesh-config/cmd/mesh-config/proposals.go b/modules/mesh-config/cmd/mesh-config/proposals.go index 358f636e8..33895c2f8 100644 --- a/modules/mesh-config/cmd/mesh-config/proposals.go +++ b/modules/mesh-config/cmd/mesh-config/proposals.go @@ -98,11 +98,9 @@ type Proposal struct { HadValue bool `json:"had-value"` WasDigest string `json:"was-digest"` Trusted bool `json:"trusted,omitempty"` - // Pull is the open pull request the value is held for, when it is (novox/hq ADR 0502): the value is then not set - // on Approve but kept apart until the pull request merges, and DeclDigest is the digest of the pull request's - // declaration of the key it was judged and shown against. - Pull string `json:"pull,omitempty"` - DeclDigest string `json:"declaration,omitempty"` + // Pull is read only: a proposal an earlier mesh-config kept for a pull request (ADR 0502, ended by the operator's word, + // record pending). It is never set on an Approve now, and judged not to be one this module makes (problem). + Pull string `json:"pull,omitempty"` // Reaches is each module on each node whose value it moves, as it was judged when proposed. Reaches []string `json:"reaches"` // From is who proposed it, as the bus proved the caller; Why is theirs; At is when. @@ -175,9 +173,6 @@ func (p Proposal) Fingerprint() string { // about is what its ask is about: the value's place, so a newer proposal for it replaces an older one still open // (ADR 0277 §10, at a level, a key). func (p Proposal) about() string { - if p.Pull != "" { - return "config.held." + pullKey(p.Pull) + "." + p.Level.ValueKey(p.Key) - } return "config." + p.Level.ValueKey(p.Key) } @@ -188,9 +183,6 @@ func (p Proposal) Act(option string) occ.Act { "level": p.Level.Kind, "node": p.Level.Node, "module": p.Level.Module, "key": p.Key, "value": p.Digest, "was": p.WasDigest, "had-value": strconv.FormatBool(p.HadValue), "from": p.From, "at": p.At.UTC().Format(time.RFC3339Nano)} - if p.Pull != "" { - act["pull"], act["declaration"] = p.Pull, p.DeclDigest - } return act } @@ -270,16 +262,6 @@ func (p Proposal) summary() string { if p.Trusted { trusted = ", a trusted value," } - if p.Pull != "" { - s := fmt.Sprintf("Holds %s for %s on %s for %s; approved, it takes effect when that pull request merges, "+ - "and is dropped if it closes unmerged. Nothing reaches a machine before the merge.", p.Key, p.Level.Module, - p.Level.Node, p.Pull) - if len([]rune(s)) > occ.SummaryLength { - s = fmt.Sprintf("Holds %s for %s on %s for %s until it merges; dropped if it closes unmerged.", p.Key, - p.Level.Module, p.Level.Node, p.Pull) - } - return s - } reach := p.reaches() s := fmt.Sprintf("%s%s at %s; approved, it takes effect at once on every node it reaches: %s.", verb, trusted, p.Level, reach) @@ -305,9 +287,6 @@ func (p Proposal) headline() string { if p.Action == "clear" { verb = "Clear" } - if p.Pull != "" { - verb = "Hold" - } where := "for the whole mesh" switch p.Level.Kind { case LevelNode: @@ -359,9 +338,6 @@ func (p Proposal) Ask() oc.Ask { a.WholeDetails = w } does := "the value is set at once, and every node it reaches takes it" - if p.Pull != "" { - does = "the value is held for " + p.Pull + ": it is set when that pull request merges, and dropped if it closes unmerged" - } if p.Action == "clear" { does = "the value is cleared at once, and every node it reaches takes what is below it" } @@ -392,8 +368,8 @@ func (p Proposal) problem() string { return "its value is not the value its digest names: it changed after it was asked" case p.Action == "clear" && (len(p.Value) != 0 || p.Digest != valueDigest(nil, false)): return "a clear carries no value" - case p.Pull != "" && (p.Action != "set" || p.Level.Kind != LevelModule || p.DeclDigest == ""): - return "a value held for a pull request is a set of a module's own value, judged against its declaration" + case p.Pull != "": + return "it was held for a pull request, which the mesh knows no more: nothing is set on it" case valueDigest(p.Was, p.HadValue) != p.WasDigest: return "the value it was shown against is not the one its digest names: it changed after it was asked" } @@ -559,39 +535,16 @@ func (c *Config) propose(call Call, action string) (Proposal, oc.Ask, []string, // **Judged as a set judges it, before anyone is asked** (ADR 0277 §4): no ask for a value the verb would refuse. var r rule var trusted bool - var declDigest string vk := level.ValueKey(call.Key) was, had := s.Values[vk] - if call.Pull != "" { - // **A value held for the open pull request that introduces its key** (novox/hq ADR 0502): judged against what - // the pull request declares at its head, as the controller told it, and asked of the operator whatever the - // key's trust. - if action != "set" { - return fail("a clear is not held for a pull request: only a value to set is") - } - if _, _, _, err := parsePull(call.Pull); err != nil { - return fail("%v", err) - } - call.Pull = strings.TrimSpace(call.Pull) - var herr error - r, _, declDigest, herr = heldRule(s, call, level) - if herr != nil { - return fail("%v", herr) - } - trusted = false - k, kept := s.Kept[keptKey(call.Pull, level.Node, level.Module, call.Key)] - was, had = Value{Value: heldValue(k)}, kept - } else { - var derr error - r, trusted, derr = c.declared(s, level, call.Key) - if derr != nil { - return fail("%v", derr) - } + r, trusted, derr := c.declared(s, level, call.Key) + if derr != nil { + return fail("%v", derr) } now := c.now() prop := Proposal{ID: newProposalID(), Action: action, Level: level, Key: call.Key, HadValue: had, WasDigest: valueDigest(was.Value, had), Trusted: trusted, From: call.Caller, Why: why, At: now, - State: ProposalWaiting, Digest: valueDigest(nil, false), Pull: call.Pull, DeclDigest: declDigest} + State: ProposalWaiting, Digest: valueDigest(nil, false)} if had { prop.Was = compact(was.Value) } @@ -618,19 +571,15 @@ func (c *Config) propose(call Call, action string) (Proposal, oc.Ask, []string, if had && string(compact(was.Value)) == string(prop.Value) { return fail("%s at %s already holds exactly %s", call.Key, level, prop.Value) } - if call.Pull == "" { - after.Values[vk] = Value{Value: prop.Value} - } + after.Values[vk] = Value{Value: prop.Value} case "clear": if !had { return fail("nothing is set for %s at %s, so there is nothing to clear", call.Key, level) } delete(after.Values, vk) } - if call.Pull == "" { - for _, m := range diff(Resolve(s), Resolve(after)) { - prop.Reaches = append(prop.Reaches, m.Module+" on "+m.Node) - } + for _, m := range diff(Resolve(s), Resolve(after)) { + prop.Reaches = append(prop.Reaches, m.Module+" on "+m.Node) } if prop.Reaches == nil { prop.Reaches = []string{} @@ -830,14 +779,6 @@ func (c *Config) decide(p Proposal, w oc.Warrant) string { return fmt.Sprintf("proposal %s: approved by the operator, and not set: the decision could not be kept (%v); "+ "it is read again on the next look", p.ID, err) } - if p.Pull != "" { - // **A held value is kept apart, never set on Approve** (ADR 0502 clause 3). - said, err := c.keep(p) - if err != nil { - return keep(ProposalRefused, "approved by the operator, and refused on Approve, so nothing is held: "+err.Error()) - } - return keep(ProposalApproved, "approved by the operator and held: "+said) - } call := Call{Action: p.Action, Key: p.Key, Module: p.Level.Module, Node: p.Level.Node, Why: p.Why, Caller: p.From, approval: &approval{Proposal: p.ID, By: "the operator", Says: p.Approved, Was: p.Was, HadValue: p.HadValue, WasDigest: p.WasDigest}} @@ -972,8 +913,6 @@ type ProposalRow struct { Expires time.Time `json:"expires"` Fingerprint string `json:"fingerprint"` Approved string `json:"approved,omitempty"` - // Pull is the pull request a value is held for, when it is. - Pull string `json:"pull,omitempty"` // What one proposal read whole says besides. Value json.RawMessage `json:"value,omitempty"` Was json.RawMessage `json:"was,omitempty"` @@ -1002,8 +941,7 @@ func (c *Config) Proposals(call Call) ([]ProposalRow, error) { continue } row := ProposalRow{ID: p.ID, Action: p.Action, Level: p.Level, Key: p.Key, State: p.State, Outcome: p.Outcome, - From: p.From, At: p.At, Expires: p.At.Add(ProposalLasts), Fingerprint: p.Fingerprint(), Approved: p.Approved, - Pull: p.Pull} + From: p.From, At: p.At, Expires: p.At.Add(ProposalLasts), Fingerprint: p.Fingerprint(), Approved: p.Approved} if p.State == ProposalWaiting && c.now().After(row.Expires) { row.State = "expired, not yet heard" } diff --git a/modules/mesh-config/cmd/mesh-config/pullforge_test.go b/modules/mesh-config/cmd/mesh-config/pullforge_test.go deleted file mode 100644 index e2418e65c..000000000 --- a/modules/mesh-config/cmd/mesh-config/pullforge_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package main - -import ( - "encoding/json" - "testing" -) - -// toolForge.Pull reads the seat git's answer of the verb pull (design 47 G §654), bare or in the wrappings the runtime puts -// around an answer; pullEventOf reads the seat's pull-merged and pull-closed bodies. -func TestToolForgePullReadsTheSeatGitAnswersAndEventsAreRead(t *testing.T) { - var asked struct { - key string - body map[string]any - } - for _, tc := range []struct { - answer string - open, merged bool - head string - }{ - {`{"pull":{"repository":"novox/mesh-catalog","number":435,"state":"merged","head":"7b5f7f84acb73aaf8189200548ff69fd9aac04b2"}}`, false, true, "7b5f7f84acb73aaf8189200548ff69fd9aac04b2"}, - {`{"pull":{"repository":"novox/mesh-catalog","number":434,"state":"open","head":"c948f3a61c74257e6e3147effb396d9d6434f491"}}`, true, false, "c948f3a61c74257e6e3147effb396d9d6434f491"}, - {`{"pull":{"state":"closed","head":"c948f3a61c74257e6e3147effb396d9d6434f491"}}`, false, false, "c948f3a61c74257e6e3147effb396d9d6434f491"}, - } { - raw := json.RawMessage(tc.answer) - content, _ := json.Marshal(map[string]any{"content": []map[string]any{{"type": "text", "text": tc.answer}}}) - ok, _ := json.Marshal(map[string]any{"ok": true, "output": "", "answer": raw}) - for name, answer := range map[string]json.RawMessage{"bare": raw, "content list": content, "ok answer": ok} { - f := toolForge{ask: func(key string, body any) (json.RawMessage, error) { - asked.key, asked.body = key, body.(map[string]any) - return answer, nil - }} - open, merged, head, err := f.Pull("novox", "mesh-catalog", 434) - if err != nil || open != tc.open || merged != tc.merged || head != tc.head { - t.Errorf("%s (%s): open %v merged %v head %q, err %v", tc.answer, name, open, merged, head, err) - } - if asked.key != "seat:git.pull" || asked.body["repository"] != "novox/mesh-catalog" || asked.body["number"] != 434 { - t.Errorf("asked %+v", asked) - } - } - } - failed := toolForge{ask: func(string, any) (json.RawMessage, error) { - return json.RawMessage(`{"error":"no such pull request","error-kind":"not-found"}`), nil - }} - if _, _, _, err := failed.Pull("novox", "mesh-catalog", 9); err == nil { - t.Error("an error answer was read as a pull request") - } - p, ok := pullEventOf([]byte(`{"repository":"novox/mesh-catalog","number":435,"branch":"fix/a","head":"7b5f7f84acb73aaf8189200548ff69fd9aac04b2","base":"main"}`)) - if !ok || p.Owner != "novox" || p.Repo != "mesh-catalog" || p.Number != 435 || p.HeadSHA != "7b5f7f84acb73aaf8189200548ff69fd9aac04b2" { - t.Errorf("%+v %v", p, ok) - } - if _, ok := pullEventOf([]byte(`{"repository":"norepo","number":1}`)); ok { - t.Error("a repository with no owner was read") - } -} diff --git a/modules/mesh-config/cmd/mesh-config/pulls.go b/modules/mesh-config/cmd/mesh-config/pulls.go deleted file mode 100644 index 3508a95bf..000000000 --- a/modules/mesh-config/cmd/mesh-config/pulls.go +++ /dev/null @@ -1,644 +0,0 @@ -package main - -import ( - "crypto/sha256" - "encoding/hex" - "encoding/json" - "errors" - "fmt" - "regexp" - "strconv" - "strings" - "time" - - "git.novox.be/novox/mesh-contracts/go/clients/meshconfigclient" -) - -// A config value held for the open pull request that introduces its key (novox/hq ADR 0502, issue 1826). -// -// A key a pull request adds is declared by no registered manifest until it merges, so a value for it, which the -// pull request's composed check needs, was refused. The controller reads each pull request's manifests from the -// forge at its head and tells this module the config declarations the head adds or changes (PullDeclares); mesh-config -// reads no manifest itself (ADR 0495 §1). A proposal that names the pull request is judged against those, asked of the -// operator whatever the key's trust, and on Approve kept apart (KeptState) from the values it resolves. The -// controller asks for the approved held values of a check's pull requests (Held) and hands them to the composed -// check. At the merge the value is written as the module's own value on its node (PullMerged), where it resolves -// once the trunk declares the key; a pull request closed unmerged drops it (PullClosed). Nothing reaches a machine -// before the merge. - -// The states of held values, and the actions their changes carry in the history and in `config-changed`. -const ( - PullsState = "pulls" - KeptState = "kept" - - ActionHold = "hold" - ActionDrop = "drop" -) - -// pullName is a pull request as it is named: owner/repository#number. -var pullName = regexp.MustCompile(`^([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)#([0-9]+)$`) - -func parsePull(s string) (owner, repo string, number int, err error) { - m := pullName.FindStringSubmatch(strings.TrimSpace(s)) - if m == nil { - return "", "", 0, fmt.Errorf("%q is not a pull request: name it owner/repository#number", s) - } - n, _ := strconv.Atoi(m[3]) - if n <= 0 { - return "", "", 0, fmt.Errorf("%q is not a pull request: its number is above zero", s) - } - return m[1], m[2], n, nil -} - -// pullKey is a pull request's key in the states. -func pullKey(pull string) string { - return strings.NewReplacer("/", ".", "#", ".").Replace(strings.TrimSpace(pull)) -} - -// PullDecl is what the controller told: the config declarations a pull request's head adds or changes, by module. -type PullDecl struct { - Pull string `json:"pull"` - Head string `json:"head"` - Declares map[string]map[string]Declaration `json:"declares"` - At time.Time `json:"at"` -} - -// KeptValue is an approved held value, kept apart from the values mesh-config resolves, in the layout of the seat's state -// `kept`, which the checker and the assigner's composition check read (novox/hq ADR 0565 §5, ADR 0634 §4). -type KeptValue = meshconfigclient.Kept - -// keptKey is a held value's key in `kept`: the pull request's key form (pullKey), the node, the module and the config -// key, joined by dots. -func keptKey(pull, node, module, key string) string { - k, _ := meshconfigclient.HeldValueKey(pull, node, module, key) // a held value of no pull request has no key, and is found by none - return k -} - -// heldValue is the value held, as the operator approved it, in its JSON form. -func heldValue(k KeptValue) json.RawMessage { - raw, _ := meshconfigclient.ValueOf(k) // a value that was read from JSON writes as JSON - return raw -} - -func heldAt(t time.Time) *time.Time { return &t } - -func keptStateKey(k KeptValue) string { return keptKey(k.Pull, k.Node, k.Module, k.Key) } - -func keptLevel(k KeptValue) Level { return Level{Kind: LevelModule, Node: k.Node, Module: k.Module} } - -// declDigest is the digest of what a declaration holds a value to: a held value stays only while it is the same. -func declDigest(d Declaration) string { - d.Description, d.Why, d.Kind, d.Runtime = "", "", "", false - if !d.HasDefault() { - d.Default = nil - } else { - d.Default = compact(d.Default) - } - raw, _ := json.Marshal(d) - sum := sha256.Sum256(raw) - return "sha256:" + hex.EncodeToString(sum[:]) -} - -// PullForge is the forge as this module asks it of a pull request: its state, and whether it merged. -type PullForge interface { - Pull(owner, repo string, number int) (open, merged bool, head string, err error) -} - -// pulls reads what is kept for pull requests, and the held values. -func (c *Config) readPulls(s *Sources) error { - s.Pulls, s.Kept = map[string]PullDecl{}, map[string]KeptValue{} - if err := c.each(PullsState, func(key string, raw []byte) error { - var p PullDecl - if err := json.Unmarshal(raw, &p); err != nil || p.Pull == "" { - return nil // not one this module wrote - } - s.Pulls[pullKey(p.Pull)] = p - return nil - }); err != nil { - return fmt.Errorf("the pull requests' declarations cannot be read: %w", err) - } - if err := c.each(KeptState, func(key string, raw []byte) error { - k, err := meshconfigclient.DecodeKept(key, raw) - if err != nil { - return nil // not a held value of the seat's layout under its own key - } - s.Kept[key] = k - return nil - }); err != nil { - return fmt.Errorf("the held values cannot be read: %w", err) - } - return nil -} - -// ---- pull-declares --------------------------------------------------------------------------------------- - -// PullDeclaresAnswer is what pull-declares answers. -type PullDeclaresAnswer struct { - Said string `json:"said"` - Pull string `json:"pull"` - Head string `json:"head"` - Modules []string `json:"modules"` - Dropped []string `json:"dropped,omitempty"` - Unsaid []string `json:"unsaid,omitempty"` -} - -// PullDeclares is gone: a pull request's config declarations are read from the indexer's config-candidates -// (takeCandidates), and the controller that told them is not placed (novox/hq ADR 0565 §4, ADR 0590). -func (c *Config) PullDeclares(Call) (*PullDeclaresAnswer, error) { - return nil, errors.New("pull-declares is gone: mesh-config reads a pull request's config declarations from the " + - "indexer's config-candidates (novox/hq ADR 0565 §4, ADR 0590)") -} - -// declarePull keeps what a pull request's head declares and judges again the values held for it, from the indexer's -// config candidates. The caller holds the lock. -func (c *Config) declarePull(pull, head string, declares map[string]map[string]Declaration) (*PullDeclaresAnswer, error) { - if err := c.Store.Put(PullsState, pullKey(pull), PullDecl{Pull: pull, Head: head, Declares: declares, - At: c.now()}); err != nil { - return nil, fmt.Errorf("the declarations were not kept: %v", err) - } - a := &PullDeclaresAnswer{Pull: pull, Head: head, Modules: sortedKeys(declares)} - var s Sources - if err := c.readPulls(&s); err != nil { - return nil, err - } - for _, k := range sortedKeys(s.Kept) { - kv := s.Kept[k] - if pullKey(kv.Pull) != pullKey(pull) { - continue - } - why := "" - d, declared := declares[kv.Module][kv.Key] - switch { - case !declared: - why = "the head no longer adds or changes its declaration" - case declDigest(d) != kv.Declaration: - why = "the head declares the key otherwise than the value was approved against" - default: - var v any - _ = json.Unmarshal(heldValue(kv), &v) - if p := d.rule().check(v); len(p) > 0 { - why = "the value no longer passes: " + strings.Join(p, "; ") - } - } - if why != "" { - a.Unsaid = append(a.Unsaid, c.dropKept(kv, "the new head "+short(head)+": "+why)...) - a.Dropped = append(a.Dropped, fmt.Sprintf("%s for %s on %s: %s", kv.Key, kv.Module, kv.Node, why)) - continue - } - if kv.Head != head { - kv.Head = head - if err := c.Store.Put(KeptState, k, kv); err != nil { - a.Unsaid = append(a.Unsaid, "the held value "+k+" was not moved to the new head: "+err.Error()) - } - } - } - a.Said = fmt.Sprintf("keeps what %s declares at %s for %d module(s)", pull, short(head), len(declares)) - if len(a.Dropped) > 0 { - a.Said += fmt.Sprintf("; %d held value(s) dropped", len(a.Dropped)) - } - return a, nil -} - -// heldChange records a hold, a drop or a merge's set of a held value in the history, the hand-act log and -// config-changed; what could not be done is answered, not dropped. -func (c *Config) heldChange(kv KeptValue, action, why, approved string) []string { - level := keptLevel(kv) - ch := Change{Action: action, Level: &level, Key: kv.Key, Value: heldValue(kv), By: kv.By, Via: kv.Via, Why: why, - Approved: approved, Pull: kv.Pull, Head: kv.Head, At: c.now(), Moved: []Moved{}} - unsaid := c.record(level.ValueKey(kv.Key), ch) - cause := "a value held for " + kv.Pull - what := fmt.Sprintf("config %s %s of %s for %s, held for %s", action, kv.Key, kv.Module, kv.Node, kv.Pull) - if action != ActionDrop { - what += " (" + string(heldValue(kv)) + ")" - } - if err := c.Store.HandAct(what, why, cause); err != nil { - unsaid = append(unsaid, "the hand-act log does not hold it: "+err.Error()) - } - return unsaid -} - -// dropKept removes a held value, saying why. -func (c *Config) dropKept(kv KeptValue, why string) []string { - if err := c.Store.Delete(KeptState, keptStateKey(kv)); err != nil { - return []string{"the held value " + keptStateKey(kv) + " was not dropped: " + err.Error()} - } - c.logf("[mesh-config] dropped %s of %s on %s held for %s: %s", kv.Key, kv.Module, kv.Node, kv.Pull, why) - return c.heldChange(kv, ActionDrop, "dropped, nothing was set: "+why, "") -} - -// ---- held ------------------------------------------------------------------------------------------------ - -// HeldAnswer is what held answers: the approved held values of the pull requests asked, at the heads asked. -type HeldAnswer struct { - Held []HeldValue `json:"held"` - Skipped []HeldSkipped `json:"skipped"` -} - -// HeldValue is one approved held value as it is handed to a composed check and listed. -type HeldValue struct { - Pull string `json:"pull"` - Head string `json:"head"` - Node string `json:"node"` - Module string `json:"module"` - Key string `json:"key"` - Value json.RawMessage `json:"value"` - Approved string `json:"approved"` - By string `json:"by,omitempty"` - Why string `json:"why,omitempty"` - At time.Time `json:"at"` -} - -// HeldSkipped is a pull request asked for whose held values are not answered, and why. -type HeldSkipped struct { - Pull string `json:"pull"` - Why string `json:"why"` -} - -func heldValueOf(k KeptValue) HeldValue { - return HeldValue{Pull: k.Pull, Head: k.Head, Node: k.Node, Module: k.Module, Key: k.Key, Value: compact(heldValue(k)), - Approved: k.Approved, By: k.By, Why: k.Why, At: timeOf(k.At)} -} - -// Held answers the approved held values for the pull requests asked, at the heads asked, to the controller alone: a -// pull request at another head than the one kept is skipped and said (ADR 0502 clause 4). -func (c *Config) Held(call Call) (*HeldAnswer, error) { - if call.Caller != ControllerCaller { - return nil, fmt.Errorf("held is the controller's: it hands the held values to a composed check; %s is not "+ - "the controller", call.Caller) - } - asked := map[string]string{} - var order []string - for _, p := range call.Pulls { - if _, _, _, err := parsePull(p.Pull); err != nil { - return nil, err - } - if strings.TrimSpace(p.Head) == "" { - return nil, fmt.Errorf("name the head of %s", p.Pull) - } - if _, dup := asked[pullKey(p.Pull)]; !dup { - order = append(order, strings.TrimSpace(p.Pull)) - } - asked[pullKey(p.Pull)] = strings.TrimSpace(p.Head) - } - c.mu.Lock() - defer c.mu.Unlock() - var s Sources - if err := c.readPulls(&s); err != nil { - return nil, err - } - out := &HeldAnswer{Held: []HeldValue{}, Skipped: []HeldSkipped{}} - for _, p := range order { - head := asked[pullKey(p)] - var mine []KeptValue - for _, k := range sortedKeys(s.Kept) { - if pullKey(s.Kept[k].Pull) == pullKey(p) { - mine = append(mine, s.Kept[k]) - } - } - for _, k := range mine { - if k.Head != head { - out.Skipped = append(out.Skipped, HeldSkipped{Pull: p, Why: fmt.Sprintf("%s for %s on %s is held "+ - "at head %s, not %s", k.Key, k.Module, k.Node, short(k.Head), short(head))}) - continue - } - out.Held = append(out.Held, heldValueOf(k)) - } - } - return out, nil -} - -// HeldListed is the held values a listing shows, narrowed by module, node and key. -func heldListed(s Sources, call Call) []HeldValue { - out := []HeldValue{} - for _, k := range sortedKeys(s.Kept) { - kv := s.Kept[k] - if (call.Module == "" || kv.Module == call.Module) && (call.Node == "" || kv.Node == call.Node) && - (call.Key == "" || kv.Key == call.Key) { - out = append(out, heldValueOf(kv)) - } - } - return out -} - -// ---- the proposal's rule ------------------------------------------------------------------------------- - -// heldRule is the rule a value held for a pull request is judged by: the pull request's declaration of the key for -// the module at its head, and the module's assignment to the node. Refused, saying what the pull request does -// declare, when it declares none. -func heldRule(s Sources, call Call, level Level) (rule, PullDecl, string, error) { - if level.Kind != LevelModule { - return rule{}, PullDecl{}, "", fmt.Errorf("a value held for a pull request is a module's own value on one "+ - "node, and %s is not one: a predefined key a pull request adds is set after its merge (novox/hq ADR 0502)", - level) - } - pd, ok := s.Pulls[pullKey(call.Pull)] - if !ok { - return rule{}, PullDecl{}, "", fmt.Errorf("mesh-config holds no declaration for %s: the controller tells it "+ - "what a pull request declares when its head is announced, and none has been", strings.TrimSpace(call.Pull)) - } - d, ok := pd.Declares[level.Module][call.Key] - if !ok { - return rule{}, PullDecl{}, "", fmt.Errorf("%s does not add or change a value %q for %s at %s; it declares "+ - "for %s: %s", pd.Pull, call.Key, level.Module, short(pd.Head), level.Module, joinKeys(pd.Declares[level.Module])) - } - if _, reg := s.Declared[level.Module]; !reg { - return rule{}, PullDecl{}, "", fmt.Errorf("%s is not a registered module, so a value cannot be held for it", level.Module) - } - if !assigned(s, level.Node, level.Module) { - return rule{}, PullDecl{}, "", fmt.Errorf("%s is not placed on %s, and a module's own value is set for a node "+ - "it is placed on (novox/hq ADR 0348 §3); it is placed on: %s", level.Module, level.Node, - strings.Join(nodesOf(s, level.Module), ", ")) - } - return d.rule(), pd, declDigest(d), nil -} - -// keep holds an approved proposal's value apart. The caller holds the lock. -func (c *Config) keep(p Proposal) (string, error) { - var s Sources - if err := c.readPulls(&s); err != nil { - return "", err - } - pd, ok := s.Pulls[pullKey(p.Pull)] - if !ok { - return "", fmt.Errorf("mesh-config holds no declaration for %s any more", p.Pull) - } - d, ok := pd.Declares[p.Level.Module][p.Key] - if !ok || declDigest(d) != p.DeclDigest { - return "", fmt.Errorf("%s declares %s otherwise than the operator was shown, or no longer declares it: "+ - "propose it again", p.Pull, p.Key) - } - was, had := s.Kept[keptKey(p.Pull, p.Level.Node, p.Level.Module, p.Key)] - if had != p.HadValue || valueDigest(heldValue(was), had) != p.WasDigest { - return "", fmt.Errorf("the value held for %s changed since the operator was shown the change: propose it "+ - "again", p.Pull) - } - kv := KeptValue{Pull: p.Pull, Head: pd.Head, Declaration: p.DeclDigest, Node: p.Level.Node, Module: p.Level.Module, - Key: p.Key, Value: p.Value, By: "the operator", Via: p.From, Why: p.Why, Approved: p.Approved, Proposal: p.ID, - At: heldAt(c.now())} - if err := c.Store.Put(KeptState, keptStateKey(kv), kv); err != nil { - return "", fmt.Errorf("the value was not kept: %v", err) - } - unsaid := c.heldChange(kv, ActionHold, "held for "+p.Pull+" until it merges: "+p.Why, p.Approved) - said := fmt.Sprintf("%s of %s on %s is held at %s for %s until it merges", p.Key, p.Level.Module, p.Level.Node, - short(pd.Head), p.Pull) - if len(unsaid) > 0 { - said += " (" + strings.Join(unsaid, "; ") + ")" - } - return said, nil -} - -// ---- the merge and the close --------------------------------------------------------------------------- - -// PullMerged writes each value held for a pull request as the module's own value on its node, with the approval kept -// beside it; it resolves once the trunk declares the key. It answers a line for each. -func (c *Config) PullMerged(pull, head string) []string { - return c.settlePull(pull, true, head) -} - -// PullClosed drops the values held for a pull request closed unmerged, and says that nothing was set. -func (c *Config) PullClosed(pull string) []string { - return c.settlePull(pull, false, "") -} - -func (c *Config) settlePull(pull string, merged bool, head string) []string { - c.mu.Lock() - defer c.mu.Unlock() - var s Sources - if err := c.readPulls(&s); err != nil { - return []string{err.Error()} - } - var said []string - wrote := false - for _, k := range sortedKeys(s.Kept) { - kv := s.Kept[k] - if pullKey(kv.Pull) != pullKey(pull) { - continue - } - if !merged { - said = append(said, fmt.Sprintf("%s of %s on %s was held for %s, which closed unmerged: dropped, "+ - "nothing was set", kv.Key, kv.Module, kv.Node, kv.Pull)) - said = append(said, c.dropKept(kv, kv.Pull+" closed unmerged")...) - continue - } - // **Only a value held at the merged head, whose key that head declares as it was approved** (ADR 0502 clause 7). - why := "" - pd, known := s.Pulls[pullKey(pull)] - d, declared := pd.Declares[kv.Module][kv.Key] - switch { - case head == "": - why = "the merged head is not known, so the value cannot be held to it" - case !strings.EqualFold(head, kv.Head): - why = fmt.Sprintf("it is held at head %s and the pull request merged at %s", short(kv.Head), short(head)) - case !known || !declared || declDigest(d) != kv.Declaration: - why = "the merged head does not declare the key as the operator was shown" - } - if why != "" { - said = append(said, fmt.Sprintf("%s of %s on %s was held for %s, which merged: dropped, nothing was set (%s)", - kv.Key, kv.Module, kv.Node, kv.Pull, why)) - said = append(said, c.dropKept(kv, kv.Pull+" merged, but "+why)...) - continue - } - vk := keptLevel(kv).ValueKey(kv.Key) - v := Value{Value: compact(heldValue(kv)), By: kv.By, Via: kv.Via, Why: kv.Why + " (held for " + kv.Pull + - ", merged)", Approved: kv.Approved, At: c.now()} - if err := c.Store.Put(ValuesState, vk, v); err != nil { - said = append(said, fmt.Sprintf("%s of %s on %s held for %s is not written yet: %v; it is tried "+ - "again", kv.Key, kv.Module, kv.Node, kv.Pull, err)) - continue - } - if err := c.Store.Delete(KeptState, k); err != nil { - said = append(said, "the held value "+k+" is written but not removed: "+err.Error()) - } - wrote = true - said = append(said, c.heldChange(kv, "set", kv.Pull+" merged: the held value is the module's own value now", - kv.Approved)...) - said = append(said, fmt.Sprintf("%s of %s on %s held for %s is set: %s", kv.Key, kv.Module, kv.Node, - kv.Pull, kv.Value)) - } - if wrote { - if s2, err := c.sources(); err == nil { - if _, err := c.writeResolved(s2); err != nil { - said = append(said, "the resolved values are not all written yet ("+err.Error()+"); the next "+ - "resolution writes them") - } - } - } - left := 0 - for _, k := range sortedKeys(s.Kept) { - if pullKey(s.Kept[k].Pull) != pullKey(pull) { - continue - } - if e, err := c.Store.Get(KeptState, k); err != nil || e != nil { - left++ - } - } - if left == 0 { - if err := c.Store.Delete(PullsState, pullKey(pull)); err != nil { - said = append(said, "the declarations of "+pull+" were not removed: "+err.Error()) - } - } - return said -} - -// PullEvent is a pull request merged or closed, as far as this module reads it: the seat `git`'s `pull-merged` or `pull-closed` -// (design 47 G §654), read by pullEventOf. -type PullEvent struct { - Owner string - Repo string - Number int - Merged bool - // HeadSHA is the head the pull request merged at. - HeadSHA string -} - -// pullEventOf reads the seat `git`'s `pull-merged` or `pull-closed` body: the repository `/`, the number and the head. -func pullEventOf(body []byte) (PullEvent, bool) { - var e struct { - Repository string `json:"repository"` - Number int `json:"number"` - Head string `json:"head"` - } - if json.Unmarshal(body, &e) != nil { - return PullEvent{}, false - } - owner, repo, ok := strings.Cut(e.Repository, "/") - return PullEvent{Owner: owner, Repo: repo, Number: e.Number, HeadSHA: e.Head}, ok -} - -func (p PullEvent) name() string { return fmt.Sprintf("%s/%s#%d", p.Owner, p.Repo, p.Number) } - -// HeardPull takes the forge's word that a pull request merged or closed. A close is asked of the forge when it can -// be, because a merged pull request may be announced as closed too. -func (c *Config) HeardPull(p PullEvent, merged bool) []string { - if p.Owner == "" || p.Repo == "" || p.Number <= 0 { - return nil - } - head := p.HeadSHA - if !merged && !p.Merged && c.Forge != nil { - // A close is told from a merge by the forge; when it cannot be asked nothing is dropped, and the five-minute - // look finds it. - open, m, h, err := c.Forge.Pull(p.Owner, p.Repo, p.Number) - if err != nil { - c.logf("[mesh-config] %s closed, and the forge does not say whether it merged (%v); the next look decides", p.name(), err) - return nil - } - if open { - return nil - } - merged = m - if head == "" { - head = h - } - } - return c.settlePull(p.name(), merged || p.Merged, head) -} - -// SweepPulls looks at the forge for every pull request it keeps something for, so a merge or a close it did not hear -// is found: run once at start (resolving). -func (c *Config) SweepPulls() []string { - if c.Forge == nil { - return nil - } - c.mu.Lock() - var s Sources - err := c.readPulls(&s) - c.mu.Unlock() - if err != nil { - return []string{err.Error()} - } - seen := map[string]string{} - for _, p := range s.Pulls { - seen[pullKey(p.Pull)] = p.Pull - } - for _, k := range s.Kept { - seen[pullKey(k.Pull)] = k.Pull - } - var said []string - for _, key := range sortedKeys(seen) { - pull := seen[key] - owner, repo, n, err := parsePull(pull) - if err != nil { - continue - } - open, merged, head, err := c.Forge.Pull(owner, repo, n) - if err != nil { - said = append(said, fmt.Sprintf("the forge does not say whether %s is open: %v", pull, err)) - continue - } - if !open { - said = append(said, c.settlePull(pull, merged, head)...) - } - } - return said -} - -// PullAt is a pull request at a head, as held asks about it. -type PullAt struct { - Pull string `json:"pull"` - Head string `json:"head"` -} - -// toolForge asks the forge's holder through its tools. -type toolForge struct { - ask func(key string, body any) (json.RawMessage, error) -} - -// Pull asks the seat `git`'s verb `pull` whether a pull request is open, and whether it merged. -func (f toolForge) Pull(owner, repo string, number int) (bool, bool, string, error) { - raw, err := f.ask(GitPullAsk, map[string]any{"repository": owner + "/" + repo, "number": number}) - if err != nil { - return false, false, "", err - } - answer, output, ok := unwrapAnswer(raw) - if !ok { - return false, false, "", fmt.Errorf("the seat git refused pull: %s", output) - } - var a struct { - Pull struct { - State string `json:"state"` - Head string `json:"head"` - } `json:"pull"` - Error string `json:"error"` - } - if err := json.Unmarshal(answer, &a); err != nil || a.Error != "" || a.Pull.State == "" { - return false, false, "", fmt.Errorf("the seat git's pull is not readable: %s", output) - } - return a.Pull.State == "open", a.Pull.State == "merged", a.Pull.Head, nil -} - -// GitPullAsk is the seat `git`'s verb that answers one pull request. -const GitPullAsk = "seat:git.pull" - -// unwrapAnswer reads a tool's answer whatever wraps it: the protocol's content list, or {output, ok, answer}. -func unwrapAnswer(raw json.RawMessage) (json.RawMessage, string, bool) { - var s string - if json.Unmarshal(raw, &s) == nil { - return unwrapAnswer(json.RawMessage(s)) - } - var m map[string]json.RawMessage - if json.Unmarshal(raw, &m) != nil { - return raw, string(raw), true - } - if content, has := m["content"]; has { - var items []struct { - Text string `json:"text"` - } - var e struct { - IsError bool `json:"isError"` - } - _ = json.Unmarshal(raw, &e) - if json.Unmarshal(content, &items) == nil && len(items) > 0 { - inner, out, ok := unwrapAnswer(json.RawMessage(items[0].Text)) - return inner, out, ok && !e.IsError - } - } - var v struct { - Output string `json:"output"` - OK *bool `json:"ok"` - Answer json.RawMessage `json:"answer"` - } - if json.Unmarshal(raw, &v) == nil && v.OK != nil { - return v.Answer, v.Output, *v.OK - } - return raw, string(raw), true -} diff --git a/modules/mesh-config/cmd/mesh-config/pulls_test.go b/modules/mesh-config/cmd/mesh-config/pulls_test.go deleted file mode 100644 index 23f0ea9b8..000000000 --- a/modules/mesh-config/cmd/mesh-config/pulls_test.go +++ /dev/null @@ -1,504 +0,0 @@ -package main - -import ( - "encoding/json" - "errors" - "strings" - "testing" - "time" - - "git.novox.be/novox/mesh-contracts/go/clients/meshconfigclient" - - occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient" -) - -// Issue 1826 (novox/hq ADR 0502): a value held for the open pull request that introduces its key. - -const thePull = "novox/mesh-catalog#434" - -// n8nPullDeclares is what the controller tells of the pull request: n8n gets a required number media-gid. -func n8nPullDeclares() map[string]any { - return map[string]any{"n8n": map[string]any{"media-gid": map[string]any{"type": "number", - "description": "the media group", "required": true, "minimum": 1, "maximum": 65535}}} -} - -// aHoldingMesh is a proposing mesh with n8n assigned to ace and declaring nothing on the trunk (the case of the -// issue), and a stand-in forge. -func aHoldingMesh(t *testing.T) (*Config, *memStore, *standIn, *standForge) { - t.Helper() - c, m, r := aProposingMesh(t) - m.declare(t, "n8n", `{"module":"n8n","settings":{}}`) - m.assign(t, "ace", "n8n") - f := &standForge{state: map[string][2]bool{}} - c.Forge = f - if _, err := c.Reconcile(); err != nil { - t.Fatal(err) - } - m.events, m.handActs = nil, nil - return c, m, r, f -} - -type standForge struct { - state map[string][2]bool // pull -> open, merged - head string - err error -} - -func (f *standForge) Pull(owner, repo string, n int) (bool, bool, string, error) { - if f.err != nil { - return false, false, "", f.err - } - st, ok := f.state[(PullEvent{Owner: owner, Repo: repo, Number: n}).name()] - if !ok { - return true, false, f.head, nil - } - return st[0], st[1], f.head, nil -} - -// tell keeps what a pull request's head declares as the indexer's candidate is taken (takeCandidates). -func tell(t *testing.T, c *Config, pull, head string, declares map[string]any) *PullDeclaresAnswer { - t.Helper() - raw, _ := json.Marshal(declares) - d := map[string]map[string]Declaration{} - if err := json.Unmarshal(raw, &d); err != nil { - t.Fatal(err) - } - c.mu.Lock() - defer c.mu.Unlock() - a, err := c.declarePull(pull, head, d) - if err != nil { - t.Fatalf("declare: %v", err) - } - return a -} - -// holdIt proposes media-gid=2000 for n8n on ace held for the pull request and has the operator approve it. -func holdIt(t *testing.T, c *Config, m *memStore, r *standIn) string { - t.Helper() - a := propose(t, c, Call{Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull, - Why: "n8n's container joins the media group, held for #434"}) - r.choose(t, a.Proposal, OptionApprove, nil) - said, err := c.Decide() - if err != nil || len(said) != 1 || !strings.Contains(said[0], "approved by the operator and held") { - t.Fatalf("the Approve decided %v (%v)", said, err) - } - return a.Proposal -} - -func keptCount(m *memStore) int { k, _ := m.Keys(KeptState); return len(k) } - -// 1826/K1: mesh-config keeps what a pull request declares per pull request; pull-declares is gone (ADR 0590). -func TestAPullRequestsDeclarationsAreKeptPerPullRequest(t *testing.T) { - c, m, _, _ := aHoldingMesh(t) - for _, caller := range []string{"g14.node-tools", Unproven, "", ControllerCaller} { - if _, err := c.Do(Call{Action: "pull-declares", Pull: thePull, Head: "abc1234", Declares: n8nPullDeclares(), - Caller: caller}); err == nil || !strings.Contains(err.Error(), "config-candidates") { - t.Errorf("pull-declares from %q: %v", caller, err) - } - } - a := tell(t, c, thePull, "abc1234def", n8nPullDeclares()) - if len(a.Modules) != 1 || a.Modules[0] != "n8n" { - t.Errorf("answered %+v", a) - } - e, _ := m.Get(PullsState, "novox.mesh-catalog.434") - var pd PullDecl - if e == nil || json.Unmarshal(e.Value, &pd) != nil || pd.Head != "abc1234def" || - pd.Declares["n8n"]["media-gid"].Type != "number" || !pd.Declares["n8n"]["media-gid"].Required { - t.Errorf("kept %s", func() string { - if e == nil { - return "nothing" - } - return string(e.Value) - }()) - } - // Nothing is resolved from a pull request's declaration. - if r, _ := m.Get(ResolvedState, ResolvedKey("ace", "n8n", "media-gid")); r != nil { - t.Errorf("a pull request's declaration was resolved: %s", r.Value) - } -} - -// 1826/K2: a proposal for a key only the pull request declares is refused without its declaration, for a mesh or -// node level, for a clear, for an undeclared key, and for a module not placed on the node; and trunk-only -// proposals are judged as before. -func TestAProposalNamingAPullRequestIsJudgedAgainstItsDeclaration(t *testing.T) { - c, m, _, _ := aHoldingMesh(t) - try := func(call Call) error { - call.Action, call.Caller, call.Why = "propose", "g14.node-tools", "held for 434" - _, err := c.Propose(call) - return err - } - base := Call{Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull} - if err := try(base); err == nil || !strings.Contains(err.Error(), "holds no declaration for "+thePull) { - t.Errorf("without the pull request's declaration: %v", err) - } - // And without the pull argument it is the trunk's refusal, as before. - plain := base - plain.Pull = "" - if err := try(plain); err == nil || !strings.Contains(err.Error(), `n8n declares no value "media-gid"`) { - t.Errorf("the trunk's refusal changed: %v", err) - } - tell(t, c, thePull, "abc1234def", n8nPullDeclares()) - for name, tc := range map[string]struct { - mutate func(*Call) - want string - }{ - "a mesh level": {func(c *Call) { c.Module, c.Node = "", "" }, "a module's own value on one node"}, - "a node level": {func(c *Call) { c.Module = "" }, "a module's own value on one node"}, - "a clear": {func(c *Call) { c.Clear, c.Value = true, nil }, "a clear is not held"}, - "a key not added": {func(c *Call) { c.Key = "other" }, "does not add or change a value"}, - "a bad value": {func(c *Call) { c.Value = 0.0 }, "below the minimum"}, - "a wrong type": {func(c *Call) { c.Value = "2000" }, "is not a number"}, - "another node": {func(c *Call) { c.Node = "g14" }, "is not placed on g14"}, - "another pull": {func(c *Call) { c.Pull = "novox/mesh-catalog#435" }, "holds no declaration for"}, - "a malformed pull": {func(c *Call) { c.Pull = "434" }, "owner/repository#number"}, - } { - call := base - tc.mutate(&call) - if err := try(call); err == nil || !strings.Contains(err.Error(), tc.want) { - t.Errorf("%s: %v, want %q", name, err, tc.want) - } - } - // A set or a clear through the verb takes no pull request. - if _, err := c.Change(Call{Action: "set", Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull, - Caller: "g14.node-tools", Why: "w"}); err == nil || !strings.Contains(err.Error(), "only a proposal") { - t.Errorf("a set naming a pull request: %v", err) - } - if keptCount(m) != 0 { - t.Error("something was held") - } -} - -// 1826/K2: every held value is asked whatever the key's trust; the ask says it is held and dropped on a close; on -// Approve the value is kept apart, never set or resolved; history, event and hand-act say it; held answers it. -func TestAHeldValueIsAskedAndKeptApartOnApprove(t *testing.T) { - c, m, r, _ := aHoldingMesh(t) - tell(t, c, thePull, "abc1234def", n8nPullDeclares()) - a := propose(t, c, Call{Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull, Why: "held for #434"}) - if len(a.Reaches) != 0 { - t.Errorf("a held value reaches %v", a.Reaches) - } - ask := r.asks[a.Proposal] - if err := occ.Check(ask, c.now()); err != nil { - t.Fatalf("the ask: %v", err) - } - for _, want := range []string{thePull, "merges", "dropped if it closes unmerged", "media-gid", "n8n", "ace"} { - if !strings.Contains(ask.Explanation, want) { - t.Errorf("the ask does not say %q:\n%s", want, ask.Explanation) - } - } - if strings.Contains(ask.Explanation, "takes effect at once") || !strings.Contains(ask.Headline, "Hold") { - t.Errorf("the ask: %q\n%s", ask.Headline, ask.Explanation) - } - if ap := ask.Options[0]; !strings.Contains(ap.Does, thePull) || ap.Level == "" { - t.Errorf("the approve option does %q", ap.Does) - } - if keptCount(m) != 0 { - t.Fatal("a proposal held something") - } - r.choose(t, a.Proposal, OptionApprove, nil) - said, _ := c.Decide() - if len(said) != 1 || !strings.Contains(said[0], "held at abc1234d for "+thePull) { - t.Fatalf("decided %v", said) - } - // Kept apart: not a value, not resolved. - if _, set := storedValue(t, m, "module.ace.n8n.media-gid"); set { - t.Error("an Approve set the value") - } - if e, _ := m.Get(ResolvedState, ResolvedKey("ace", "n8n", "media-gid")); e != nil { - t.Errorf("an Approve resolved the value: %s", e.Value) - } - if keptCount(m) != 1 { - t.Fatalf("%d values held", keptCount(m)) - } - if len(m.events) != 1 || m.events[0].Action != ActionHold || m.events[0].Pull != thePull || - m.events[0].Key != "media-gid" || m.events[0].Head != "abc1234def" || len(m.events[0].Moved) != 0 || - !strings.Contains(m.events[0].Approved, "approved by the operator") { - t.Errorf("raised %+v", m.events) - } - if len(m.handActs) != 1 || !strings.Contains(m.handActs[0], thePull) { - t.Errorf("hand-acts %q", m.handActs) - } - h, _ := c.History(Call{Module: "n8n", Node: "ace"}) - if len(h) != 1 || h[0].Action != ActionHold || h[0].Pull != thePull { - t.Errorf("history %+v", h) - } - // held answers it, at the head it is kept at, to the controller alone. - if _, err := c.Do(Call{Action: "held", Caller: "g14.node-tools", Pulls: []PullAt{{thePull, "abc1234def"}}}); err == nil { - t.Error("held answered a caller that is not the controller") - } - got, err := c.Do(Call{Action: "held", Caller: ControllerCaller, Pulls: []PullAt{{thePull, "abc1234def"}}}) - if err != nil { - t.Fatal(err) - } - ha := got.(*HeldAnswer) - if len(ha.Held) != 1 || ha.Held[0].Module != "n8n" || ha.Held[0].Node != "ace" || ha.Held[0].Key != "media-gid" || - string(ha.Held[0].Value) != "2000" || !strings.Contains(ha.Held[0].Approved, "approved by the operator") || - ha.Held[0].Pull != thePull || len(ha.Skipped) != 0 { - t.Errorf("held answered %+v", ha) - } - // Another head, and another pull request, are not answered. - got, _ = c.Do(Call{Action: "held", Caller: ControllerCaller, - Pulls: []PullAt{{thePull, "ffff0000"}, {"novox/mesh-catalog#435", "abc"}}}) - if ha = got.(*HeldAnswer); len(ha.Held) != 0 || len(ha.Skipped) != 1 || !strings.Contains(ha.Skipped[0].Why, "held at") { - t.Errorf("held at another head answered %+v", ha) - } - // The listing shows it as held, and the proposal as held for the pull request. - l, _ := c.List(Call{Module: "n8n"}) - if len(l.Held) != 1 || len(l.Set) != 0 { - t.Errorf("the listing: held %+v set %+v", l.Held, l.Set) - } - rows, _ := c.Proposals(Call{}) - if len(rows) != 1 || rows[0].Pull != thePull || rows[0].State != ProposalApproved { - t.Errorf("proposals %+v", rows) - } -} - -// 1826/K2: an Approve is refused when the pull request's declaration changed after the operator was shown it. -func TestAnApproveOfAHeldValueIsRefusedWhenTheDeclarationChanged(t *testing.T) { - c, m, r, _ := aHoldingMesh(t) - tell(t, c, thePull, "abc1234def", n8nPullDeclares()) - a := propose(t, c, Call{Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull, Why: "w"}) - changed := n8nPullDeclares() - changed["n8n"].(map[string]any)["media-gid"].(map[string]any)["maximum"] = 1000.0 - tell(t, c, thePull, "def5678abc", changed) - r.choose(t, a.Proposal, OptionApprove, nil) - said, _ := c.Decide() - if len(said) != 1 || !strings.Contains(said[0], "nothing is held") || keptCount(m) != 0 { - t.Errorf("decided %v; %d held", said, keptCount(m)) - } -} - -// 1826/K1: at a new head a held value is kept if its key is declared the same and it still passes; otherwise it is -// dropped, recorded, raised and said. -func TestANewHeadKeepsAHeldValueOnlyWhileItsDeclarationIsUnchanged(t *testing.T) { - c, m, r, _ := aHoldingMesh(t) - tell(t, c, thePull, "abc1234def", n8nPullDeclares()) - holdIt(t, c, m, r) - m.events = nil - - // The same declaration at a new head (the description may differ): kept, moved to the new head. - same := n8nPullDeclares() - same["n8n"].(map[string]any)["media-gid"].(map[string]any)["description"] = "reworded" - if a := tell(t, c, thePull, "1111111111", same); len(a.Dropped) != 0 { - t.Errorf("an unchanged declaration dropped %v", a.Dropped) - } - got, _ := c.Do(Call{Action: "held", Caller: ControllerCaller, Pulls: []PullAt{{thePull, "1111111111"}}}) - if len(got.(*HeldAnswer).Held) != 1 || len(m.events) != 0 { - t.Errorf("after an unchanged head: %+v, events %+v", got, m.events) - } - - // A changed declaration: dropped. - changed := n8nPullDeclares() - changed["n8n"].(map[string]any)["media-gid"].(map[string]any)["maximum"] = 1000.0 - a := tell(t, c, thePull, "2222222222", changed) - if len(a.Dropped) != 1 || !strings.Contains(a.Dropped[0], "declares the key otherwise") || keptCount(m) != 0 { - t.Fatalf("a changed declaration: %+v, %d held", a, keptCount(m)) - } - if len(m.events) != 1 || m.events[0].Action != ActionDrop || m.events[0].Pull != thePull { - t.Errorf("raised %+v", m.events) - } - got, _ = c.Do(Call{Action: "held", Caller: ControllerCaller, Pulls: []PullAt{{thePull, "2222222222"}}}) - if len(got.(*HeldAnswer).Held) != 0 { - t.Error("a dropped value was answered") - } - - // A head that no longer adds the key drops it too. - tell(t, c, thePull, "3333333333", n8nPullDeclares()) - holdIt(t, c, m, r) - if a = tell(t, c, thePull, "4444444444", map[string]any{}); len(a.Dropped) != 1 || - !strings.Contains(a.Dropped[0], "no longer adds or changes") { - t.Errorf("a head without the key: %+v", a) - } -} - -// 1826/K3: the merge writes the held value as the module's own value with its approval kept, before the trunk -// declares the key; it resolves right after the declared-config mirror moves. -func TestTheMergeAppliesAHeldValueAndItResolvesOnceTheTrunkDeclaresTheKey(t *testing.T) { - c, m, r, _ := aHoldingMesh(t) - tell(t, c, thePull, "abc1234def", n8nPullDeclares()) - holdIt(t, c, m, r) - approved := func() string { - raw, _ := m.Get(KeptState, "novox.mesh-catalog.434.ace.n8n.media-gid") - // the state kept is the seat's layout, under the seat's key: the checker and the assigner read it with this - k, err := meshconfigclient.DecodeKept("novox.mesh-catalog.434.ace.n8n.media-gid", raw.Value) - if err != nil || string(heldValue(k)) != "2000" || k.Declaration == "" || k.At == nil { - t.Fatalf("the held value is not the seat's: %+v, %v", k, err) - } - return k.Approved - }() - m.events, m.handActs = nil, nil - - said := c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434, Merged: true, HeadSHA: "abc1234def"}, true) - if len(said) == 0 { - t.Fatal("the merge said nothing") - } - v, set := storedValue(t, m, "module.ace.n8n.media-gid") - if !set || string(v.Value) != "2000" || v.Approved != approved || v.Approved == "" || v.By != "the operator" || - !strings.Contains(v.Why, thePull) { - t.Fatalf("the merge wrote %+v", v) - } - if keptCount(m) != 0 { - t.Error("the value is still held after the merge") - } - if p, _ := m.Keys(PullsState); len(p) != 0 { - t.Errorf("the pull request's declarations are still kept: %v", p) - } - if len(m.events) != 1 || m.events[0].Action != "set" || m.events[0].Pull != thePull || - m.events[0].Approved != approved || len(m.handActs) != 1 { - t.Errorf("raised %+v, recorded %q", m.events, m.handActs) - } - // Not resolved before the trunk declares it. - if e, _ := m.Get(ResolvedState, ResolvedKey("ace", "n8n", "media-gid")); e != nil { - t.Errorf("resolved before the trunk declares the key: %s", e.Value) - } - m.declare(t, "n8n", `{"module":"n8n","settings":{"media-gid":{"kind":"","default":null,"why":"",`+ - `"type":"number","required":true,"minimum":1,"maximum":65535}}}`) - if _, err := c.Reconcile(); err != nil { - t.Fatal(err) - } - if res := m.resolved(t, "ace", "n8n", "media-gid"); string(res.Value) != "2000" || res.Source != SourceModule { - t.Errorf("media-gid resolves as %+v", res) - } - l, _ := c.List(Call{Module: "n8n", Node: "ace"}) - if len(l.Set) != 1 || l.Set[0].Approved != approved || len(l.Held) != 0 { - t.Errorf("the listing: set %+v held %+v", l.Set, l.Held) - } - // A merge heard twice sets nothing twice. - m.events = nil - c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434, Merged: true, HeadSHA: "abc1234def"}, true) - if len(m.events) != 0 { - t.Errorf("a second merge raised %+v", m.events) - } -} - -// 1826/K3: a pull request closed unmerged drops its held values and says so; nothing is set or resolved. -func TestAClosedPullRequestDropsItsHeldValuesAndSaysSo(t *testing.T) { - c, m, r, f := aHoldingMesh(t) - tell(t, c, thePull, "abc1234def", n8nPullDeclares()) - holdIt(t, c, m, r) - m.events = nil - f.state[thePull] = [2]bool{false, false} - said := c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434}, false) - if len(said) == 0 || !strings.Contains(strings.Join(said, "\n"), "closed unmerged: dropped, nothing was set") { - t.Errorf("said %v", said) - } - if _, set := storedValue(t, m, "module.ace.n8n.media-gid"); set || keptCount(m) != 0 { - t.Error("a closed pull request left a value") - } - if len(m.events) != 1 || m.events[0].Action != ActionDrop || m.events[0].Pull != thePull || - !strings.Contains(m.events[0].Why, "closed unmerged") { - t.Errorf("raised %+v", m.events) - } - if p, _ := m.Keys(PullsState); len(p) != 0 { - t.Errorf("declarations of a closed pull request kept: %v", p) - } -} - -// 1826/K3: a close event for a pull request the forge says merged is the merge. -func TestACloseEventForAMergedPullRequestIsItsMerge(t *testing.T) { - c, m, r, f := aHoldingMesh(t) - tell(t, c, thePull, "abc1234def", n8nPullDeclares()) - holdIt(t, c, m, r) - f.state[thePull] = [2]bool{false, true} - f.head = "abc1234def" - c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434}, false) - if v, set := storedValue(t, m, "module.ace.n8n.media-gid"); !set || string(v.Value) != "2000" { - t.Errorf("the merge of a pull request announced closed set %+v", v) - } -} - -// 1826/K3: the five-minute look finds a merge, a close, and an open pull request, and says a forge that does not -// answer. -func TestTheLookAtTheForgeFindsAMissedMergeOrCloseAndLeavesAnOpenPullRequest(t *testing.T) { - c, m, r, f := aHoldingMesh(t) - tell(t, c, thePull, "abc1234def", n8nPullDeclares()) - holdIt(t, c, m, r) - if said := c.SweepPulls(); len(said) != 0 || keptCount(m) != 1 { - t.Errorf("an open pull request: said %v, %d held", said, keptCount(m)) - } - f.err = errors.New("the forge is down") - if said := c.SweepPulls(); len(said) != 1 || !strings.Contains(said[0], "does not say whether") || keptCount(m) != 1 { - t.Errorf("a silent forge: said %v", said) - } - f.err = nil - f.head = "abc1234def" - f.state[thePull] = [2]bool{false, true} - c.SweepPulls() - if v, set := storedValue(t, m, "module.ace.n8n.media-gid"); !set || string(v.Value) != "2000" || keptCount(m) != 0 { - t.Errorf("a missed merge: %+v", v) - } - // A missed close. - tell(t, c, "novox/mesh-catalog#440", "abc", n8nPullDeclares()) - f.state["novox/mesh-catalog#440"] = [2]bool{false, false} - c.SweepPulls() - if p, _ := m.Keys(PullsState); len(p) != 0 { - t.Errorf("a missed close left %v", p) - } -} - -// 1826/K1: the new arguments are read strictly and the new actions are the verb's. -func TestThePullArgumentsAreRead(t *testing.T) { - call, err := callOf(map[string]any{"action": "pull-declares", "pull": " " + thePull + " ", "head": "abc", - "declares": map[string]any{"n8n": map[string]any{}}, "pulls": []any{map[string]any{"pull": thePull, "head": "h"}}}) - if err != nil || call.Pull != thePull || call.Head != "abc" || len(call.Declares) != 1 || len(call.Pulls) != 1 || - call.Pulls[0].Head != "h" { - t.Errorf("read %+v (%v)", call, err) - } - if _, err := callOf(map[string]any{"declares": "x"}); err == nil { - t.Error("declares that is not an object was read") - } - if _, err := callOf(map[string]any{"pulls": "x"}); err == nil { - t.Error("pulls that is not a list was read") - } - if _, err := callOf(map[string]any{"bogus": 1}); err == nil || !strings.Contains(err.Error(), "pull") { - t.Errorf("an unknown argument: %v", err) - } - _ = time.Second -} - -// 1826 review r1: the merge applies a value only if it is held at the merged head; otherwise it is dropped and said. -func TestTheMergeAppliesOnlyAValueHeldAtTheMergedHead(t *testing.T) { - for name, head := range map[string]string{"another head": "9999999999", "no head told": ""} { - c, m, r, _ := aHoldingMesh(t) - tell(t, c, thePull, "abc1234def", n8nPullDeclares()) - holdIt(t, c, m, r) - said := c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434, Merged: true, HeadSHA: head}, true) - if _, set := storedValue(t, m, "module.ace.n8n.media-gid"); set || keptCount(m) != 0 || - !strings.Contains(strings.Join(said, "\n"), "dropped, nothing was set") { - t.Errorf("%s: set=%v held=%d said %v", name, set, keptCount(m), said) - } - if len(m.events) == 0 || m.events[len(m.events)-1].Action != ActionDrop { - t.Errorf("%s: raised %+v", name, m.events) - } - } -} - -// 1826 review r1: a later head without the declaration drops the value, so the merge has nothing to apply. -func TestALaterHeadWithoutTheDeclarationLeavesNothingToApplyAtTheMerge(t *testing.T) { - c, m, r, _ := aHoldingMesh(t) - tell(t, c, thePull, "abc1234def", n8nPullDeclares()) - holdIt(t, c, m, r) - tell(t, c, thePull, "bbbbbbbbbb", map[string]any{}) - c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434, Merged: true, HeadSHA: "bbbbbbbbbb"}, true) - if _, set := storedValue(t, m, "module.ace.n8n.media-gid"); set { - t.Error("a value held for a declaration the merged head lacks was set") - } -} - -// 1826 review r1: a close the forge cannot be asked about drops nothing; the five-minute look decides. -func TestACloseThatTheForgeCannotBeAskedAboutDropsNothing(t *testing.T) { - c, m, r, f := aHoldingMesh(t) - tell(t, c, thePull, "abc1234def", n8nPullDeclares()) - holdIt(t, c, m, r) - f.err = errors.New("the forge is down") - c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434}, false) - if keptCount(m) != 1 { - t.Fatal("a close the forge could not confirm dropped the value") - } - f.err = nil - f.state[thePull] = [2]bool{false, false} - c.SweepPulls() - if keptCount(m) != 0 { - t.Error("the look did not drop it") - } -} diff --git a/modules/mesh-config/cmd/mesh-config/quiet_integration_test.go b/modules/mesh-config/cmd/mesh-config/quiet_integration_test.go index deb93d759..335e23949 100644 --- a/modules/mesh-config/cmd/mesh-config/quiet_integration_test.go +++ b/modules/mesh-config/cmd/mesh-config/quiet_integration_test.go @@ -58,9 +58,8 @@ func TestConfigWholeAsksTheBusForNothingWhileNoInputChanges(t *testing.T) { if err != nil { t.Fatal(err) } - buckets := []string{"mesh-delivery-assigner_assignments", "mesh-delivery-assigner_wants", "mesh-delivery-indexer_predefined", - "mesh-delivery-indexer_config-candidates"} - for _, s := range []string{ValuesState, ResolvedState, PredefinedState, ProposalsState, PullsState, KeptState, DeclaredState, + buckets := []string{"mesh-delivery-assigner_assignments", "mesh-delivery-assigner_wants", "mesh-delivery-indexer_predefined"} + for _, s := range []string{ValuesState, ResolvedState, PredefinedState, ProposalsState, DeclaredState, RenamesState, RetiringState, FindingsState} { buckets = append(buckets, Module+"_"+s) } @@ -93,19 +92,17 @@ func TestConfigWholeAsksTheBusForNothingWhileNoInputChanges(t *testing.T) { t.Fatal(err) } - // No input changes: the delivery says it delivered, a node-engine says what it reads, the forge says a pull request nothing is - // kept for closed, and the catalog says a manifest was accepted that no retirement waits on. Each is heard and none is a pass: + // No input changes: the delivery says it delivered, a node-engine says what it reads, and the catalog says a manifest was accepted that no retirement waits on. Each is heard and none is a pass: // nothing is asked of the bus. (The module has no timer: no minute, five or otherwise, asks it anything.) before, putsBefore := p.snapshot() for i := 0; i < 20; i++ { p.event("mesh-delivery-view.delivered", map[string]any{"revision": "r"}) p.event("node-engine.manifest-reads-changed", map[string]any{"node": "g14"}) - p.event("git.pull-closed", map[string]any{"owner": "novox", "repo": "mesh-catalog", "number": 700 + i}) } after, putsAfter := p.snapshot() if n := asked(before, after, "mesh/state.get", "mesh/state.keys", "mesh/state.watch", "mesh/state.put", "mesh/state.create", "mesh/state.update", "mesh/state.delete", "mesh/ask"); n != 0 || len(putsAfter) != len(putsBefore) { - t.Errorf("60 events that move nothing asked %d requests of the bus (%v); wrote %v", n, after, putsAfter[len(putsBefore):]) + t.Errorf("40 events that move nothing asked %d requests of the bus (%v); wrote %v", n, after, putsAfter[len(putsBefore):]) } // One input changes: player's manifest declares a second value. Only player's values on its two nodes are written, and `declared` diff --git a/modules/mesh-config/cmd/mesh-config/quiet_test.go b/modules/mesh-config/cmd/mesh-config/quiet_test.go index d6f89e77c..d5e25c429 100644 --- a/modules/mesh-config/cmd/mesh-config/quiet_test.go +++ b/modules/mesh-config/cmd/mesh-config/quiet_test.go @@ -5,6 +5,7 @@ package main import ( "encoding/json" + "strings" "sync" "testing" "time" @@ -193,19 +194,19 @@ func (b *busOf) ownCopy() *ownCopy { // **The module's own states are read from a copy, never key by key** (ADR 0580 §1, issue 1982): a whole state read of 1,176 // keys is one watch and no get, again and again; this process's write is in the copy at once, another's arrives by the watch. func TestAnOwnStateIsReadFromItsWatchedCopyNotKeyByKey(t *testing.T) { - b := &busOf{held: map[string]map[string][]byte{PullsState: {}}, handles: map[string]func(stdio.StateChange) error{}} + b := &busOf{held: map[string]map[string][]byte{ValuesState: {}}, handles: map[string]func(stdio.StateChange) error{}} for i := 0; i < 1176; i++ { - b.held[PullsState][pullKey("novox/mesh-catalog#"+string(rune('a'+i%26))+time.Duration(i).String())] = []byte(`{"pull":"x"}`) + b.held[ValuesState]["mesh.key-"+time.Duration(i).String()] = []byte(`{"value":1}`) } - n := len(b.held[PullsState]) + n := len(b.held[ValuesState]) own := b.ownCopy() for pass := 0; pass < 3; pass++ { - keys, err := own.keys(PullsState) + keys, err := own.keys(ValuesState) if err != nil || len(keys) != n { t.Fatalf("keys %d, %v; want %d", len(keys), err, n) } for _, k := range keys { - if e, err := own.get(PullsState, k); err != nil || e == nil { + if e, err := own.get(ValuesState, k); err != nil || e == nil { t.Fatalf("get %s: %v, %v", k, e, err) } } @@ -214,23 +215,23 @@ func TestAnOwnStateIsReadFromItsWatchedCopyNotKeyByKey(t *testing.T) { t.Errorf("3 passes over %d keys asked the bus for %d watches and %d gets; want 1 and 0", n, b.watches, b.gets) } // Its own write and removal are in the copy at once. - if err := own.write(PullsState, "mine", map[string]string{"pull": "mine"}); err != nil { + if err := own.write(ValuesState, "mine", map[string]string{"x": "mine"}); err != nil { t.Fatal(err) } - if e, _ := own.get(PullsState, "mine"); e == nil { + if e, _ := own.get(ValuesState, "mine"); e == nil { t.Error("a key just written is not read") } - if err := own.remove(PullsState, "mine"); err != nil { + if err := own.remove(ValuesState, "mine"); err != nil { t.Fatal(err) } - if e, _ := own.get(PullsState, "mine"); e != nil { + if e, _ := own.get(ValuesState, "mine"); e != nil { t.Error("a key just removed is still read") } // Another writer's change arrives by the watch. - if err := b.handles[PullsState](stdio.StateChange{State: PullsState, Key: "theirs", Op: stdio.OpPut, Value: []byte(`{"pull":"t"}`), Revision: 99}); err != nil { + if err := b.handles[ValuesState](stdio.StateChange{State: ValuesState, Key: "theirs", Op: stdio.OpPut, Value: []byte(`{"value":2}`), Revision: 99}); err != nil { t.Fatal(err) } - if e, _ := own.get(PullsState, "theirs"); e == nil { + if e, _ := own.get(ValuesState, "theirs"); e == nil { t.Error("a change another wrote is not read") } if b.gets != 0 { @@ -254,3 +255,19 @@ func TestAWriteTellsWhatWaitsOnADeadline(t *testing.T) { t.Errorf("told %v", told) } } + +// The mesh knows no pull request (design 47 G; ADR 0502 ended by the operator's word, record pending): the verb takes no `pull`, +// `head`, `declares` or `pulls`, and does neither `pull-declares` nor `held`. +func TestTheVerbKnowsNoPullRequest(t *testing.T) { + for _, arg := range []string{"pull", "head", "declares", "pulls"} { + if _, err := callOf(map[string]any{"action": "propose", arg: "x"}); err == nil || !strings.Contains(err.Error(), arg) { + t.Errorf("an argument %s was taken: %v", arg, err) + } + } + c, _ := aMesh(t) + for _, action := range []string{"pull-declares", "held"} { + if _, err := c.Do(Call{Action: action}); err == nil || !strings.Contains(err.Error(), action) { + t.Errorf("the action %s was done: %v", action, err) + } + } +} diff --git a/modules/mesh-config/cmd/mesh-config/resolve.go b/modules/mesh-config/cmd/mesh-config/resolve.go index 93cd8191d..070383dff 100644 --- a/modules/mesh-config/cmd/mesh-config/resolve.go +++ b/modules/mesh-config/cmd/mesh-config/resolve.go @@ -140,10 +140,6 @@ type Sources struct { Unresolved []Unresolved Predefined map[string]Published Values map[string]Value - // Pulls is what the controller told of each open pull request's declarations, and Kept the approved values held - // for them (pulls.go); neither is resolved. - Pulls map[string]PullDecl - Kept map[string]KeptValue // Renames are the renames kept, by `.`, whose open windows answer the old name (renames.go). Renames map[string]Rename } diff --git a/modules/mesh-config/cmd/mesh-config/sources.go b/modules/mesh-config/cmd/mesh-config/sources.go index 6a05643c7..a63d1e914 100644 --- a/modules/mesh-config/cmd/mesh-config/sources.go +++ b/modules/mesh-config/cmd/mesh-config/sources.go @@ -51,9 +51,6 @@ func (c *Config) sources() (Sources, error) { }); err != nil { return s, fmt.Errorf("the values cannot be read: %w", err) } - if err := c.readPulls(&s); err != nil { - return s, err - } if renames, err := c.readRenames(); err != nil { return s, fmt.Errorf("the renames cannot be read: %w", err) } else { @@ -339,10 +336,8 @@ type Resolution struct { Moved []Moved `json:"moved"` Published string `json:"published,omitempty"` Withheld string `json:"withheld,omitempty"` - // Declared are the modules whose `declared` key was written, and Candidates what the indexer's config candidates - // did after the take-over. - Declared []string `json:"declared,omitempty"` - Candidates []string `json:"candidates,omitempty"` + // Declared are the modules whose `declared` key was written. + Declared []string `json:"declared,omitempty"` // Renamed is what the renames did in this pass. Renamed []string `json:"renamed,omitempty"` // SourceGone is said while the stored switch still names the controller, whose records are no longer read. @@ -383,9 +378,6 @@ func (c *Config) reconcileLocked() (Resolution, error) { } else if len(written) > 0 { r.Declared = written } - if sw, serr := c.source(); serr == nil && sw.Source == SourceIndexer { - r.Candidates = c.takeCandidates() - } return r, err } diff --git a/modules/mesh-config/cmd/mesh-config/takeover.go b/modules/mesh-config/cmd/mesh-config/takeover.go index cc22ce6d4..f08bb27dd 100644 --- a/modules/mesh-config/cmd/mesh-config/takeover.go +++ b/modules/mesh-config/cmd/mesh-config/takeover.go @@ -2,7 +2,7 @@ package main // Config's records out of the controller (novox/hq ADR 0565 §3, §15, and the progressive insight of 2026-10-29): // mesh-config keeps each placed module's declarations in its own state `declared`, reads the list of predefined keys -// from the indexer's `predefined` and a pull request's config from the indexer's `config-candidates`. +// from the indexer's `predefined`. A pull request's config is not read: the mesh knows no pull request (design 47 G). // // **Its own sources hold always** (ADR 0590, issue 1126): the controller ended by its unassignment, so its pull-declares, // the take-over that switched away from them, compare and hand-back are gone, and the switch is the indexer's whatever @@ -23,12 +23,11 @@ const ( // DeclaredState is each placed module's declarations at the trunk, one key per module (ADR 0565 §3). DeclaredState = "declared" - // IndexerPredefinedRead and CandidatesRead are the indexer's states (ADR 0565 §2). + // IndexerPredefinedRead is the indexer's state of the predefined list (ADR 0565 §2). IndexerPredefinedRead = indexerModule + "." + catalog.StatePredefined - CandidatesRead = indexerModule + "." + catalog.StateConfigCandidates ) -// SourceIndexer is the one source of a pull request's config and of the predefined list: the indexer's. +// SourceIndexer is the one source of the predefined list: the indexer's. const SourceIndexer = "indexer" // Switch is which source holds: the indexer's, always, since the controller ended (ADR 0590). @@ -105,7 +104,7 @@ func (c *Config) writeDeclared(s Sources) ([]string, error) { return written, errors.Join(problems...) } -// ---- the predefined list and the pull requests from the indexer ------------------------------------------ +// ---- the predefined list from the indexer ----------------------------------------------------------------------------------------- // indexerList is the indexer's predefined list as the mirror's list holds one: the keys, and the commit. func (c *Config) indexerList() (PredefinedList, bool, error) { @@ -128,75 +127,6 @@ func (c *Config) indexerList() (PredefinedList, bool, error) { return list, true, nil } -// candidates is every open pull request's config declarations as the indexer keeps them, by pull key. -func (c *Config) candidates() (map[string]PullDecl, error) { - out := map[string]PullDecl{} - err := c.each(CandidatesRead, func(key string, raw []byte) error { - var cand catalog.ConfigCandidatesByPullValue - if err := json.Unmarshal(raw, &cand); err != nil || cand.Pull == "" { - return fmt.Errorf("the indexer's config candidate %s is not one", key) - } - p := PullDecl{Pull: cand.Pull, Head: cand.Head, Declares: map[string]map[string]Declaration{}, At: timeOf(cand.At)} - for module, keys := range cand.Declares { - raw, _ := json.Marshal(keys) - var d map[string]Declaration - if err := json.Unmarshal(raw, &d); err != nil { - return fmt.Errorf("the indexer's config candidate %s declares %s unreadably", key, module) - } - for k, decl := range d { - if !decl.HasDefault() { - decl.Default = nil - } - d[k] = decl - } - p.Declares[module] = d - } - out[pullKey(cand.Pull)] = p - return nil - }) - return out, err -} - -// takeCandidates is the indexer's config candidates taken in place of pull-declares, after the take-over: each one -// whose head or declarations differ from what `pulls` holds is declared again, its held values judged again (ADR 0502 -// §6). The caller holds the lock. -func (c *Config) takeCandidates() []string { - cands, err := c.candidates() - if err != nil { - return []string{"the indexer's config candidates cannot be read: " + err.Error()} - } - var s Sources - if err := c.readPulls(&s); err != nil { - return []string{err.Error()} - } - var said []string - for _, k := range sortedKeys(cands) { - p := cands[k] - if have, ok := s.Pulls[k]; ok && have.Head == p.Head && declaresDigest(have.Declares) == declaresDigest(p.Declares) { - continue - } - a, err := c.declarePull(p.Pull, p.Head, p.Declares) - if err != nil { - said = append(said, fmt.Sprintf("%s from the indexer: %v", p.Pull, err)) - continue - } - said = append(said, a.Said+" (from the indexer)") - } - return said -} - -// declaresDigest is what a pull request's declarations hold a value to, in one encoding. -func declaresDigest(d map[string]map[string]Declaration) string { - flat := map[string]string{} - for m, keys := range d { - for k, decl := range keys { - flat[m+"."+k] = declDigest(decl) - } - } - raw, _ := json.Marshal(flat) - return string(raw) -} - // Declared is the read of `declared`: every module's key, or one whole. func (c *Config) Declared(module string) (any, error) { if module != "" { @@ -221,20 +151,14 @@ func (c *Config) Declared(module string) (any, error) { return map[string]any{"declared": rows, "count": len(rows), "source": sw}, err } -// Resync reads every source again and writes what differs: the published list, the resolved values, `declared`, and -// the indexer's config candidates (ADR 0565 §15). +// Resync reads every source again and writes what differs: the published list, the resolved values and `declared` +// (ADR 0565 §15). func (c *Config) Resync() (any, error) { r, err := c.Reconcile() if err != nil { return r, err } - c.mu.Lock() - defer c.mu.Unlock() - out := map[string]any{"resolution": r} - if sw, err := c.source(); err == nil && sw.Source == SourceIndexer { - out["candidates"] = c.takeCandidates() - } - return out, nil + return map[string]any{"resolution": r}, nil } // timeOf is an optional time of a seat's value as a time, the zero time when it is absent. diff --git a/modules/mesh-config/cmd/mesh-config/takeover_test.go b/modules/mesh-config/cmd/mesh-config/takeover_test.go index 4f5d6bd62..e05fd48ea 100644 --- a/modules/mesh-config/cmd/mesh-config/takeover_test.go +++ b/modules/mesh-config/cmd/mesh-config/takeover_test.go @@ -15,7 +15,7 @@ const ( oldDeclared = `{"module":"old","settings":{"workers":{"kind":"preference","default":4,"why":"two per core"}}}` ) -// anAgreeingMesh is aProposingMesh with the indexer's predefined list in place and no open pull request. +// anAgreeingMesh is aProposingMesh with the indexer's predefined list in place. func anAgreeingMesh(t *testing.T) (*Config, *memStore, *standIn) { t.Helper() c, m, r := aProposingMesh(t) @@ -34,19 +34,9 @@ func indexerList(t *testing.T, m *memStore, commit, list string) { m.mirror(t, IndexerPredefinedRead, catalog.PredefinedByListKey(), string(raw)) } -// candidate is the indexer's config candidate of a pull request. -func candidate(t *testing.T, m *memStore, pull, head, declares string) { - t.Helper() - key, err := catalog.ConfigCandidatesByPullKey(pullKey(pull)), error(nil) - if err != nil { - t.Fatal(err) - } - m.mirror(t, CandidatesRead, key, `{"v":1,"pull":"`+pull+`","head":"`+head+`","declares":`+declares+`}`) -} - // ADR 0590 (issue 1126): config reads its own sources always, with no take-over: declared is written once per -// manifest, pull-declares is refused naming the indexer's candidates, and the list and a pull request's config come from -// the indexer. compare, take-over and hand-back went with the controller. +// manifest, and the list comes from the indexer. compare, take-over, hand-back and a pull request's config went (the +// mesh knows no pull request, design 47 G). func TestConfigReadsItsOwnSourcesAlways(t *testing.T) { c, m, _ := anAgreeingMesh(t) steps := []struct { @@ -60,11 +50,6 @@ func TestConfigReadsItsOwnSourcesAlways(t *testing.T) { again, _ := c.Reconcile() return strings.Join(first.Declared, ",") + "|" + strings.Join(again.Declared, ","), err }, "dunst|"}, - {"pull-declares refused, naming the indexer", func() (string, error) { - _, err := c.PullDeclares(Call{Caller: ControllerCaller, Pull: "novox/mesh-catalog#8", Head: "h1", - Declares: map[string]any{}}) - return "", err - }, "config-candidates"}, {"the list comes from the indexer", func() (string, error) { indexerList(t, m, "cccccccccccc", `{"font-size":{"type":"number","description":"text size","minimum":6,"maximum":72}, "agent-managed-settings":{"type":"object","description":"managed settings","trusted":true}, @@ -72,11 +57,6 @@ func TestConfigReadsItsOwnSourcesAlways(t *testing.T) { res, err := c.Reconcile() return res.Published, err }, "theme"}, - {"a pull request's config comes from the indexer", func() (string, error) { - candidate(t, m, "novox/mesh-catalog#9", "h9", `{"dunst":{"gap":{"kind":"","type":"number","default":2}}}`) - res, err := c.Reconcile() - return strings.Join(res.Candidates, "; "), err - }, "from the indexer"}, } for _, s := range steps { got, err := s.do() diff --git a/modules/mesh-config/cmd/mesh-config/whole_integration_test.go b/modules/mesh-config/cmd/mesh-config/whole_integration_test.go index 83d9af240..f004ff88c 100644 --- a/modules/mesh-config/cmd/mesh-config/whole_integration_test.go +++ b/modules/mesh-config/cmd/mesh-config/whole_integration_test.go @@ -721,9 +721,8 @@ func TestConfigWholeRenamesAndRetiresAgainstTheBus(t *testing.T) { t.Fatal(err) } buckets := []string{"mesh-delivery-assigner_assignments", "mesh-delivery-assigner_wants", - "mesh-delivery-indexer_predefined", - "mesh-delivery-indexer_config-candidates"} - for _, s := range []string{ValuesState, ResolvedState, PredefinedState, ProposalsState, PullsState, KeptState, DeclaredState, + "mesh-delivery-indexer_predefined"} + for _, s := range []string{ValuesState, ResolvedState, PredefinedState, ProposalsState, DeclaredState, RenamesState, RetiringState, FindingsState} { buckets = append(buckets, Module+"_"+s) } diff --git a/modules/mesh-config/module.json b/modules/mesh-config/module.json index 827c0f7cb..c6d12e565 100644 --- a/modules/mesh-config/module.json +++ b/modules/mesh-config/module.json @@ -19,13 +19,10 @@ "operator-channel" ], "consumes": [ - "git.pull-merged", - "git.pull-closed", "mesh-delivery-view.delivered", "node-engine.manifest-reads-changed", "mesh-catalog.manifest-accepted", "mesh-delivery-indexer.views.predefined", - "mesh-delivery-indexer.views.config-candidates", "mesh-delivery-assigner.views.assignments", "mesh-delivery-assigner.views.wants", "mesh-catalog.catalog-revision", @@ -36,13 +33,11 @@ "invokes": [ "seat:mesh-hand-acts.hand-act", "seat:mesh-hand-acts.warranted", - "seat:git.pull", "mesh-delivery-view.node_haves", "seat:node-engine.manifest-reads", "mesh-delivery-indexer.indexer_config_readers", "seat:mesh-vault.retire", "mesh-delivery-indexer.indexer_read_predefined", - "mesh-delivery-indexer.indexer_read_config_candidates", "mesh-delivery-assigner.assigner_read_assignments", "mesh-delivery-assigner.assigner_read_wants", "seat:mesh-catalog.revision", @@ -66,8 +61,6 @@ "resolved", "predefined", "proposals", - "pulls", - "kept", "declared", "renames", "retiring", -- 2.54.0 From c7f54357ad0cbb76ee34180423058da0c355a9d2 Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 19 Nov 2026 22:50:25 +0100 Subject: [PATCH 06/43] =?UTF-8?q?mesh-cli:=20config=20no=20longer=20passes?= =?UTF-8?q?=20`pull`=20(issue=201982;=20ADR=200681=20=C2=A71)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mesh-config's verb takes no pull request argument any more, so `nox config` offers none. Builds: ADR 0681 §1 (the verb `config` takes no `pull`). Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-cli/cmd/mesh-cli/commands.go | 4 ++-- modules/mesh-cli/cmd/mesh-cli/main_test.go | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/mesh-cli/cmd/mesh-cli/commands.go b/modules/mesh-cli/cmd/mesh-cli/commands.go index 71a971df7..53fa35720 100644 --- a/modules/mesh-cli/cmd/mesh-cli/commands.go +++ b/modules/mesh-cli/cmd/mesh-cli/commands.go @@ -289,12 +289,12 @@ var commands = []command{ does: "clear one of a module's config values on one node", allowed: []string{"node", "why"}, build: func(l line) (call, error) { return configChange(l, "clear") }}, {words: []string{"config"}, usage: "[list|set|clear|history|propose|proposals] [--module m] [--node n] [--key k] [--value v] [--why w]", - does: "the mesh's config values, at any level", allowed: []string{"module", "node", "key", "value", "why", "limit", "proposal", "pull", "clear"}, + does: "the mesh's config values, at any level", allowed: []string{"module", "node", "key", "value", "why", "limit", "proposal", "clear"}, build: func(l line) (call, error) { if len(l.pos) > 1 { return call{}, usageErr("give at most one action") } - a := pick(l, "module", "node", "key", "why", "proposal", "pull", "clear", "limit") + a := pick(l, "module", "node", "key", "why", "proposal", "clear", "limit") if len(l.pos) == 1 { a["action"] = l.pos[0] } diff --git a/modules/mesh-cli/cmd/mesh-cli/main_test.go b/modules/mesh-cli/cmd/mesh-cli/main_test.go index 2fca7dfd5..7939ec0f2 100644 --- a/modules/mesh-cli/cmd/mesh-cli/main_test.go +++ b/modules/mesh-cli/cmd/mesh-cli/main_test.go @@ -173,6 +173,7 @@ func TestALineWithNoOwnerOrNotReadCallsNothingAndSaysWhy(t *testing.T) { {"conditions --bogus x", 2, "--bogus is not a flag"}, {"settings set zsh {\"execute\":\"withhold\"} --node g14 --why x", 2, "set each key"}, {"healers --days soon", 2, "takes a number"}, + {"config propose --pull novox/x#1", 2, "--pull is not a flag"}, {"frobnicate", 2, "is not a command"}, } { got := console(t, nil) -- 2.54.0 From 673d1db95a64782a0cfd9d5e9b0ae2835a9993e3 Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 19 Nov 2026 22:50:57 +0100 Subject: [PATCH 07/43] mesh-config: drop the require of the removed meshconfigclient (issue 1982) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The generated client held only the held values' keys and decoder, which the seat no longer has. Builds: ADR 0681 §1, §6 (the seat keeps no held value, so its generated client goes). Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-config/go.mod | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/mesh-config/go.mod b/modules/mesh-config/go.mod index 7c48b3524..b6c257cb8 100644 --- a/modules/mesh-config/go.mod +++ b/modules/mesh-config/go.mod @@ -3,7 +3,6 @@ module mesh-config go 1.26.0 require ( - git.novox.be/novox/mesh-contracts/go/clients/meshconfigclient v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-contracts/go/seats/meshconfig v0.0.0-00010101000000-000000000000 -- 2.54.0 From abcbeed1159908e980acbf0265ac609549c30490 Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 19 Nov 2026 23:07:09 +0100 Subject: [PATCH 08/43] =?UTF-8?q?mesh-config:=20one=20deadline=20timer=20f?= =?UTF-8?q?or=20the=20earliest=20real=20deadline,=20its=20floor=20a=20sett?= =?UTF-8?q?ing=20(issue=201982;=20ADR=200681=20=C2=A71;=20A-490l)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The one timer left is set for the earliest of three deadlines: a waiting proposal's end, an unanswered retirement's second ask, and a rename window's seven-day held finding, which is now judged at its deadline (JudgeHeldWindows) so it comes in a quiet mesh too. Its floor is the module's setting deadline-floor-seconds (default 60), not a constant. Tests: an integration test counts asks by key and fails on any `seat:git.*` over the whole run (ADR 0681, How it is checked, clause 1); a unit test shows an approved proposal records a hand act naming no pull request; another that a stored proposal naming a pull request is refused on approval, saying why. Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-config/cmd/mesh-config/main.go | 24 ++++++++- .../mesh-config/cmd/mesh-config/proposals.go | 14 +++++- .../cmd/mesh-config/proposals_test.go | 39 +++++++++++++++ .../cmd/mesh-config/quiet_integration_test.go | 6 ++- .../mesh-config/cmd/mesh-config/quiet_test.go | 49 +++++++++++++++++++ .../mesh-config/cmd/mesh-config/renames.go | 14 ++++++ .../cmd/mesh-config/whole_integration_test.go | 21 +++++++- modules/mesh-config/module.json | 13 ++++- 8 files changed, 173 insertions(+), 7 deletions(-) diff --git a/modules/mesh-config/cmd/mesh-config/main.go b/modules/mesh-config/cmd/mesh-config/main.go index 3c05a9130..15d4d8df5 100644 --- a/modules/mesh-config/cmd/mesh-config/main.go +++ b/modules/mesh-config/cmd/mesh-config/main.go @@ -268,6 +268,23 @@ func hearing(c *Config) { }() } +// DeadlineFloorKey is this module's setting for the least wait of its one deadline timer, in seconds (module.json `config`). +const DeadlineFloorKey = "deadline-floor-seconds" + +// DefaultDeadlineFloor is the floor when the setting cannot be read. +const DefaultDeadlineFloor = time.Minute + +// floorOf is the floor a setting's value names: its seconds, or the default for a value that is not a positive number. +func floorOf(raw json.RawMessage, err error) time.Duration { + var seconds float64 + if err != nil || json.Unmarshal(raw, &seconds) != nil || seconds < 1 { + return DefaultDeadlineFloor + } + return time.Duration(seconds * float64(time.Second)) +} + +func deadlineFloor() time.Duration { return floorOf(stdio.Config(DeadlineFloorKey)) } + // deciding reads the router's record of every waiting ask once at start, which finds an answer said while this module was // down, and then when the router says an ask ended (hearing's `decided` subscription calls Decide itself) or the next // deadline of a waiting proposal or retirement passes: one timer for the earliest, set again whenever a proposal or a @@ -282,6 +299,9 @@ func deciding(c *Config, rearm chan struct{}) { for _, s := range said { logf("[mesh-config] %s", s) } + if err := c.JudgeHeldWindows(); err != nil { + logf("[mesh-config] the held windows cannot be judged: %v", err) + } } decide() for { @@ -290,8 +310,8 @@ func deciding(c *Config, rearm chan struct{}) { <-rearm continue } - // Not sooner than a minute: a deadline that passed and was not settled (an ask the router refused) is tried again, not spun on. - timer := time.NewTimer(max(time.Until(at), time.Minute)) + // Not sooner than the floor: a deadline that passed and was not settled (an ask the router refused) is tried again, not spun on. + timer := time.NewTimer(max(time.Until(at), deadlineFloor())) select { case <-timer.C: decide() diff --git a/modules/mesh-config/cmd/mesh-config/proposals.go b/modules/mesh-config/cmd/mesh-config/proposals.go index 33895c2f8..f821555bb 100644 --- a/modules/mesh-config/cmd/mesh-config/proposals.go +++ b/modules/mesh-config/cmd/mesh-config/proposals.go @@ -871,7 +871,8 @@ func (c *Config) HeardEnd(w oc.Warrant) []string { // NextDeadline is when a waiting proposal or retirement is next judged for want of an answer: a proposal with no record // at the router ends an hour after its ask lapses (Decide), and a retirement left unanswered is asked once more after -// HeldFinding. The earliest of them; false when nothing waits on a time. +// HeldFinding, and an open rename window's held finding comes after HeldFinding too (lead answer A-490l). The earliest of +// them; false when nothing waits on a time. func (c *Config) NextDeadline() (time.Time, bool) { c.mu.Lock() defer c.mu.Unlock() @@ -895,6 +896,17 @@ func (c *Config) NextDeadline() (time.Time, bool) { } } } + // An open rename window reaches its held finding after HeldFinding (renames.go): its deadline, while it lies ahead. + if renames, err := c.readRenames(); err == nil { + now := c.now() + for _, r := range renames { + for _, node := range r.Open() { + if d := r.Nodes[node].Since.Add(HeldFinding); d.After(now) { + earlier(d) + } + } + } + } return at, !at.IsZero() } diff --git a/modules/mesh-config/cmd/mesh-config/proposals_test.go b/modules/mesh-config/cmd/mesh-config/proposals_test.go index da09744a5..af8c99587 100644 --- a/modules/mesh-config/cmd/mesh-config/proposals_test.go +++ b/modules/mesh-config/cmd/mesh-config/proposals_test.go @@ -590,3 +590,42 @@ func TestAnOwnersListOfNamesWithDigitsIsProposed(t *testing.T) { t.Fatalf("the owners were not put to the operator: %+v, asked on %v", a, r.subjects) } } + +// **An approved proposal records a hand act that names no pull request** (ADR 0681 clause 1: nothing it records as a hand act is a +// held value). +func TestAnApprovedProposalsHandActNamesNoPullRequest(t *testing.T) { + c, m, r := aProposingMesh(t) + a := propose(t, c, Call{Key: "font-size", Value: 12.0, Node: "g14"}) + r.choose(t, a.Proposal, OptionApprove, nil) + m.handActs = nil + if said, err := c.Decide(); err != nil || len(said) == 0 { + t.Fatalf("decide: %v, %v", said, err) + } + if len(m.handActs) == 0 { + t.Fatal("the approval recorded no hand act") + } + for _, act := range m.handActs { + if strings.Contains(strings.ToLower(act), "pull") || strings.Contains(act, "#") { + t.Errorf("a hand act names a pull request: %q", act) + } + } +} + +// A stored proposal that names a pull request is refused on its approval, saying why, and nothing is set (lead answer A-490l). +func TestAStoredProposalNamingAPullRequestIsRefusedOnApproval(t *testing.T) { + c, m, r := aProposingMesh(t) + a := propose(t, c, Call{Key: "font-size", Value: 12.0, Node: "g14"}) + p := storedProposal(t, m, a.Proposal) + p.Pull = "novox/mesh-catalog#7" + if err := m.Put(ProposalsState, p.ID, p); err != nil { + t.Fatal(err) + } + r.choose(t, a.Proposal, OptionApprove, nil) + said, _ := c.Decide() + if got := storedProposal(t, m, a.Proposal); got.State == ProposalApproved || len(said) == 0 || !strings.Contains(strings.Join(said, " "), "pull request") { + t.Errorf("said %v, proposal %+v", said, got) + } + if _, ok := storedValue(t, m, "node.g14.font-size"); ok { + t.Error("a value was set on a proposal that names a pull request") + } +} diff --git a/modules/mesh-config/cmd/mesh-config/quiet_integration_test.go b/modules/mesh-config/cmd/mesh-config/quiet_integration_test.go index 335e23949..3070fd7e9 100644 --- a/modules/mesh-config/cmd/mesh-config/quiet_integration_test.go +++ b/modules/mesh-config/cmd/mesh-config/quiet_integration_test.go @@ -92,14 +92,16 @@ func TestConfigWholeAsksTheBusForNothingWhileNoInputChanges(t *testing.T) { t.Fatal(err) } - // No input changes: the delivery says it delivered, a node-engine says what it reads, and the catalog says a manifest was accepted that no retirement waits on. Each is heard and none is a pass: - // nothing is asked of the bus. (The module has no timer: no minute, five or otherwise, asks it anything.) + // No input changes: the delivery says it delivered and a node-engine says what it reads, with no rename window open and no retirement + // waiting. Each is heard and none is a pass: nothing is asked of the bus. (The module has no polling timer; its one deadline + // timer is set for a real deadline, and none is near.) before, putsBefore := p.snapshot() for i := 0; i < 20; i++ { p.event("mesh-delivery-view.delivered", map[string]any{"revision": "r"}) p.event("node-engine.manifest-reads-changed", map[string]any{"node": "g14"}) } after, putsAfter := p.snapshot() + p.noGitAsk() if n := asked(before, after, "mesh/state.get", "mesh/state.keys", "mesh/state.watch", "mesh/state.put", "mesh/state.create", "mesh/state.update", "mesh/state.delete", "mesh/ask"); n != 0 || len(putsAfter) != len(putsBefore) { t.Errorf("40 events that move nothing asked %d requests of the bus (%v); wrote %v", n, after, putsAfter[len(putsBefore):]) diff --git a/modules/mesh-config/cmd/mesh-config/quiet_test.go b/modules/mesh-config/cmd/mesh-config/quiet_test.go index d5e25c429..1995523e3 100644 --- a/modules/mesh-config/cmd/mesh-config/quiet_test.go +++ b/modules/mesh-config/cmd/mesh-config/quiet_test.go @@ -5,6 +5,7 @@ package main import ( "encoding/json" + "errors" "strings" "sync" "testing" @@ -271,3 +272,51 @@ func TestTheVerbKnowsNoPullRequest(t *testing.T) { } } } + +// The deadline timer's floor is the setting deadline-floor-seconds, the default for a value that is not a positive number. +func TestTheDeadlineFloorIsTheSettingOrItsDefault(t *testing.T) { + for _, c := range []struct { + raw string + err error + want time.Duration + }{ + {`5`, nil, 5 * time.Second}, {`0`, nil, DefaultDeadlineFloor}, {`"x"`, nil, DefaultDeadlineFloor}, + {``, errors.New("not declared"), DefaultDeadlineFloor}, {`90`, nil, 90 * time.Second}, + } { + if got := floorOf(json.RawMessage(c.raw), c.err); got != c.want { + t.Errorf("floor of %q (%v) = %v, want %v", c.raw, c.err, got, c.want) + } + } +} + +// **A rename window held seven days comes as a finding in a quiet mesh** (lead answer A-490l): the deadline is the window's +// seventh day, the finding is written at it with no resolution pass, and a window that is not yet seven days old writes none. +func TestAHeldWindowIsAFindingAtItsDeadlineWithNoPass(t *testing.T) { + c, m, _, clock := aRenamingMesh(t) + m.declare(t, "player", newPlayer) + if _, err := c.Reconcile(); err != nil { + t.Fatal(err) + } + at, ok := c.NextDeadline() + if !ok || !at.Equal(clock.Add(HeldFinding)) { + t.Fatalf("deadline %v, %v; want the window's seventh day %v", at, ok, clock.Add(HeldFinding)) + } + if err := c.JudgeHeldWindows(); err != nil || len(findingsOf(t, m, FindingWindowHeld)) != 0 { + t.Fatalf("a young window wrote a finding (%v)", err) + } + *clock = at + cs := &countingStore{memStore: m, reads: map[string]int{}} + c.Store = cs + if err := c.JudgeHeldWindows(); err != nil { + t.Fatal(err) + } + if fs := findingsOf(t, m, FindingWindowHeld); len(fs) != 2 { + t.Errorf("findings %+v, want one for each node holding the window", fs) + } + if got := cs.readsExcept(RenamesState); len(got) != 0 { + t.Errorf("judging the held windows read %v", got) + } + if _, ok := c.NextDeadline(); ok { + t.Error("a deadline is still set once the finding is written") + } +} diff --git a/modules/mesh-config/cmd/mesh-config/renames.go b/modules/mesh-config/cmd/mesh-config/renames.go index 64417796f..f82a03879 100644 --- a/modules/mesh-config/cmd/mesh-config/renames.go +++ b/modules/mesh-config/cmd/mesh-config/renames.go @@ -487,6 +487,20 @@ func (c *Config) RenameClose(module, to, node, why string) (any, error) { return map[string]any{"closed": node, "open": r.Open()}, nil } +// JudgeHeldWindows writes the finding of each window held open past HeldFinding, and clears one that is not, from the renames +// kept: run at start and at the deadline the next window reaches (NextDeadline), so the finding comes in a quiet mesh too (lead +// answer A-490l); it reads no source and asks nothing. +func (c *Config) JudgeHeldWindows() error { + c.mu.Lock() + defer c.mu.Unlock() + renames, err := c.readRenames() + if err != nil { + return err + } + c.sweepRenameFindings(&Sources{Renames: renames}, c.now()) + return nil +} + // sweepRenameFindings writes a finding for each window held open past HeldFinding, naming the node, and clears one // whose window closed. func (c *Config) sweepRenameFindings(s *Sources, now time.Time) { diff --git a/modules/mesh-config/cmd/mesh-config/whole_integration_test.go b/modules/mesh-config/cmd/mesh-config/whole_integration_test.go index f004ff88c..a32671dc3 100644 --- a/modules/mesh-config/cmd/mesh-config/whole_integration_test.go +++ b/modules/mesh-config/cmd/mesh-config/whole_integration_test.go @@ -83,6 +83,7 @@ type player struct { catRev int64 // calls counts what the module asked of the runtime, by method (and by key for an ask); puts are the keys it wrote, "/". calls map[string]int + asks map[string]int // the asks of tools and seat verbs, by key puts []string // stop ends the module this player runs: the restart of a module is the old process gone and a new one started. stop func() @@ -368,6 +369,9 @@ func (p *player) handle(m wire) { Body json.RawMessage `json:"body"` } _ = json.Unmarshal(m.Params, &q) + p.mu.Lock() + p.asks[q.Key]++ + p.mu.Unlock() p.answer(m.ID, p.asked(q.Key, q.Body), nil) default: p.answer(m.ID, nil, errors.New("nats: no responders available for request")) @@ -687,7 +691,7 @@ func start(t *testing.T, bin string, js jetstream.JetStream, router *standIn, ha cmd.Stderr = os.Stderr p := &player{t: t, ctx: ctx, js: js, router: router, in: stdin, pending: map[string]chan wire{}, haves: haves, readers: readers, owned: map[string]*ownedState{}, catalog: map[string]catalog.CatalogEntry{}, byFp: map[string]catalog.CatalogEntry{}, - calls: map[string]int{}} + calls: map[string]int{}, asks: map[string]int{}} for _, f := range followed { p.own(f.Read) } @@ -882,6 +886,21 @@ func TestConfigWholeRenamesAndRetiresAgainstTheBus(t *testing.T) { p2.conforms(EventRetiring, &meshconfig.RetiringHolderEvent{}, meshconfig.HolderSubjectRetiring) p2.conforms(EventRetired, &meshconfig.RetiredHolderEvent{}, meshconfig.HolderSubjectRetired) eventually(t, "the retirement is dropped", func() bool { _, ok := p2.get(RetiringState, "radio.band"); return !ok }) + p.noGitAsk() + p2.noGitAsk() +} + +// noGitAsk fails when the module asked the seat git for anything over the whole run (ADR 0681 clause 1: it calls no +// `seat:git.pull`, and no other verb of the seat git is its). +func (p *player) noGitAsk() { + p.t.Helper() + p.mu.Lock() + defer p.mu.Unlock() + for key, n := range p.asks { + if strings.HasPrefix(key, "seat:git.") { + p.t.Errorf("the module asked %s %d times: it holds no value for a pull request", key, n) + } + } } // streamCarries says whether the module's manifest names event among its own streams' events. diff --git a/modules/mesh-config/module.json b/modules/mesh-config/module.json index c6d12e565..c54954179 100644 --- a/modules/mesh-config/module.json +++ b/modules/mesh-config/module.json @@ -125,5 +125,16 @@ "max-age-days": 7, "max-mib": 16 } - ] + ], + "config": { + "deadline-floor-seconds": { + "type": "number", + "description": "the least wait, in seconds, of mesh-config's one deadline timer: it is set for the earliest real deadline it holds (a waiting proposal's end, an unanswered retirement's second ask, a rename window's seven-day held finding) and never sooner than this after a look, so a deadline passed and not settled is tried again, not spun on (lead answer A-490l, ADR 0681).", + "minimum": 1, + "maximum": 3600, + "trusted": false, + "default": 60, + "runtime": true + } + } } -- 2.54.0 From a1bc61370207f30b5a36169ea6c176b40c3b895a Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 19 Nov 2026 23:19:59 +0100 Subject: [PATCH 09/43] =?UTF-8?q?mesh-config:=20a=20rename=20window=20writ?= =?UTF-8?q?ten=20after=20start=20rearms=20the=20deadline=20timer=20(issue?= =?UTF-8?q?=201982;=20ADR=200681=20=C2=A71;=20A-490l)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The timer was rearmed on a write of proposals or retiring only, so a rename window opened after start, with nothing else waiting on a time, left it blocked and its seven-day held finding never came; a window opened under a later armed deadline came late. The condition is now armsDeadline (proposals, retiring, renames), and a test shows a written window rearms it. The comment on Proposal.Pull cites ADR 0681 §5. Builds: ADR 0681 §1; lead answer A-490l (hq main, 03-DESIGN/01-to-be/47-milestones/G-22-lead-answers-2.md: the held finding comes in a quiet mesh too). Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-config/cmd/mesh-config/main.go | 7 ++++- .../mesh-config/cmd/mesh-config/proposals.go | 4 +-- .../mesh-config/cmd/mesh-config/quiet_test.go | 31 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/modules/mesh-config/cmd/mesh-config/main.go b/modules/mesh-config/cmd/mesh-config/main.go index 15d4d8df5..aadd6c207 100644 --- a/modules/mesh-config/cmd/mesh-config/main.go +++ b/modules/mesh-config/cmd/mesh-config/main.go @@ -134,10 +134,15 @@ func (runtimeAsks) Warranted(ask string) error { return err } +// armsDeadline says a write to this state can move the deadline timer: a proposal, a retirement or a rename window (A-490l). +func armsDeadline(state string) bool { + return state == ProposalsState || state == RetiringState || state == RenamesState +} + func main() { own, rearm := newOwnCopy(), make(chan struct{}, 1) own.changed = func(state string) { - if state == ProposalsState || state == RetiringState { + if armsDeadline(state) { select { case rearm <- struct{}{}: default: diff --git a/modules/mesh-config/cmd/mesh-config/proposals.go b/modules/mesh-config/cmd/mesh-config/proposals.go index f821555bb..59d31ac8f 100644 --- a/modules/mesh-config/cmd/mesh-config/proposals.go +++ b/modules/mesh-config/cmd/mesh-config/proposals.go @@ -98,8 +98,8 @@ type Proposal struct { HadValue bool `json:"had-value"` WasDigest string `json:"was-digest"` Trusted bool `json:"trusted,omitempty"` - // Pull is read only: a proposal an earlier mesh-config kept for a pull request (ADR 0502, ended by the operator's word, - // record pending). It is never set on an Approve now, and judged not to be one this module makes (problem). + // Pull is read only: a proposal an earlier mesh-config kept for a pull request (ADR 0502, superseded by ADR 0681 §5). + // It is never set on an Approve now, and judged not to be one this module makes (problem). Pull string `json:"pull,omitempty"` // Reaches is each module on each node whose value it moves, as it was judged when proposed. Reaches []string `json:"reaches"` diff --git a/modules/mesh-config/cmd/mesh-config/quiet_test.go b/modules/mesh-config/cmd/mesh-config/quiet_test.go index 1995523e3..0abf7b72e 100644 --- a/modules/mesh-config/cmd/mesh-config/quiet_test.go +++ b/modules/mesh-config/cmd/mesh-config/quiet_test.go @@ -320,3 +320,34 @@ func TestAHeldWindowIsAFindingAtItsDeadlineWithNoPass(t *testing.T) { t.Error("a deadline is still set once the finding is written") } } + +// A write of a proposal, a retirement or a rename window sets the deadline timer again, and no other state does: a window opened +// after start, with nothing else waiting on a time, must not leave the timer blocked (A-490l). +func TestAWriteOfARenameWindowRearmsTheDeadlineTimer(t *testing.T) { + for state, want := range map[string]bool{ProposalsState: true, RetiringState: true, RenamesState: true, + ValuesState: false, ResolvedState: false, DeclaredState: false} { + if got := armsDeadline(state); got != want { + t.Errorf("a write of %s arms the timer: %v, want %v", state, got, want) + } + } + // Wired as main wires it: the copy's write of a window tells the channel the timer waits on. + b := &busOf{held: map[string]map[string][]byte{}, handles: map[string]func(stdio.StateChange) error{}} + own := b.ownCopy() + rearm := make(chan struct{}, 1) + own.changed = func(state string) { + if armsDeadline(state) { + select { + case rearm <- struct{}{}: + default: + } + } + } + if err := own.write(RenamesState, "player.level", Rename{Module: "player"}); err != nil { + t.Fatal(err) + } + select { + case <-rearm: + default: + t.Error("a rename window written after start did not rearm the timer") + } +} -- 2.54.0 From d7dada57aa515ce1e30fbbb04c39490e0dea3daa Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 02:42:16 +0100 Subject: [PATCH 10/43] CONFIG-1: mesh-config judges retirements on catalog-revision; #708 onto main MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #708's six commits are taken onto main (the pull request path and A-490l's sweep go). manifest-accepted goes in H1, so a retiring value is judged for every module a catalog-revision names changed, and the resync tool's words no longer name the indexer's config candidates, which retire too. Builds: design 47 H §763 (G §654), §764 (mesh-config: its pull request path and A-490l's pull sweep; the indexer's manifest-accepted and config candidates); H-A-49, H-A-61 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-config/cmd/mesh-config/main.go | 28 ++++++++++++------- .../mesh-config/cmd/mesh-config/quiet_test.go | 25 +++++++++++++++++ .../mesh-config/cmd/mesh-config/retiring.go | 2 +- .../cmd/mesh-config/switch_tools.go | 2 +- .../cmd/mesh-config/whole_integration_test.go | 2 +- modules/mesh-config/module.json | 1 - 6 files changed, 46 insertions(+), 14 deletions(-) diff --git a/modules/mesh-config/cmd/mesh-config/main.go b/modules/mesh-config/cmd/mesh-config/main.go index aadd6c207..383708344 100644 --- a/modules/mesh-config/cmd/mesh-config/main.go +++ b/modules/mesh-config/cmd/mesh-config/main.go @@ -328,7 +328,7 @@ func deciding(c *Config, rearm chan struct{}) { // hearingDelivery takes the delivery parts' words that close a rename window or retire a value, each asked again until // the runtime takes the subscription: the status view's `delivered`, the node-engine seat's `manifest-reads-changed` -// (a returning node-engine; design 32) and the seat mesh-catalog's `manifest-accepted`. At its start it judges the windows once, +// (a returning node-engine; design 32) and the seat mesh-catalog's `catalog-revision` (design 47 H §764, H-A-49). At its start it judges the windows once, // since an event said while this module was down is not said again. func hearingDelivery(c *Config) { <-stdio.Served() @@ -348,15 +348,7 @@ func hearingDelivery(c *Config) { } return c.Windows(b.Node) }}, - {"mesh-catalog.manifest-accepted", func(e stdio.Envelope) []string { - var b struct { - Module string `json:"module"` - } - if json.Unmarshal(e.Body, &b) != nil { - return nil - } - return c.Retirements(b.Module) - }}, + {"mesh-catalog.catalog-revision.>", func(e stdio.Envelope) []string { return catalogRevisionRetirements(c, e.Body) }}, } { go func(pattern string, handle func(stdio.Envelope) []string) { for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) { @@ -379,6 +371,22 @@ func hearingDelivery(c *Config) { } } +// catalogRevisionRetirements judges the retirements of every module a catalog revision names changed: the seat +// mesh-catalog's `catalog-revision` replaces `manifest-accepted`, which goes in H1 (design 47 H §764, H-A-49). +func catalogRevisionRetirements(c *Config, body []byte) []string { + var b struct { + Changed []string `json:"changed"` + } + if json.Unmarshal(body, &b) != nil { + return nil + } + var said []string + for _, m := range b.Changed { + said = append(said, c.Retirements(m)...) + } + return said +} + // seatRename is one open rename as the seat's definition answers it: module, from, to, at and the nodes holding it open. type seatRename struct { Module string `json:"module"` diff --git a/modules/mesh-config/cmd/mesh-config/quiet_test.go b/modules/mesh-config/cmd/mesh-config/quiet_test.go index 0abf7b72e..77861b841 100644 --- a/modules/mesh-config/cmd/mesh-config/quiet_test.go +++ b/modules/mesh-config/cmd/mesh-config/quiet_test.go @@ -6,6 +6,8 @@ package main import ( "encoding/json" "errors" + "os" + "path/filepath" "strings" "sync" "testing" @@ -351,3 +353,26 @@ func TestAWriteOfARenameWindowRearmsTheDeadlineTimer(t *testing.T) { t.Error("a rename window written after start did not rearm the timer") } } + +// CONFIG-1/AC1 (design 47 H §763, §764; H-A-49): mesh-config consumes no pull request event and no `manifest-accepted`, +// calls no `git.pull`, and judges the retirements of each module a `catalog-revision` names changed. +func TestARetirementIsJudgedOnACatalogRevisionAndNoPullIsHeard(t *testing.T) { + raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + for _, gone := range []string{`"git.pull-merged"`, `"git.pull-closed"`, `"seat:git.pull"`, `"mesh-catalog.manifest-accepted"`} { + if strings.Contains(string(raw), gone) { + t.Errorf("the manifest still names %s", gone) + } + } + c, _, d, _, _, _ := retiringMesh(t) + d.known = false + said := catalogRevisionRetirements(c, []byte(`{"v":1,"revision":4,"kind":"trunk","branch":"main","changed":["player","radio"]}`)) + if len(said) != 2 || !strings.Contains(said[0], "player") || !strings.Contains(said[1], "radio") { + t.Fatalf("a catalog revision naming player and radio judged %q", said) + } + if got := catalogRevisionRetirements(c, []byte(`{"v":1,"revision":5,"kind":"trunk","branch":"main"}`)); len(got) != 0 { + t.Fatalf("a revision naming no module judged %q", got) + } +} diff --git a/modules/mesh-config/cmd/mesh-config/retiring.go b/modules/mesh-config/cmd/mesh-config/retiring.go index 3be623f7c..bc28fc157 100644 --- a/modules/mesh-config/cmd/mesh-config/retiring.go +++ b/modules/mesh-config/cmd/mesh-config/retiring.go @@ -3,7 +3,7 @@ package main // A value no longer read (novox/hq ADR 0609, design 32 *A value no longer read*). // // A config value or secret that no manifest on any trunk reads any more is a **retiring value**. mesh-config learns it -// from the indexer's tool `indexer_config_readers`, asked on each of the indexer's `manifest-accepted` (Retirements); +// from the indexer's tool `indexer_config_readers`, asked for each module a `catalog-revision` names changed (Retirements); // a tool the indexer does not serve yet answers "not known" and nothing retires. A value is marked retiring only once no // node-engine reports applied a build that reads it (State draining until then, judged again on each `delivered`); // mesh-config then says `retiring` and asks the operator once, through the operator channel, whether to remove it. It is diff --git a/modules/mesh-config/cmd/mesh-config/switch_tools.go b/modules/mesh-config/cmd/mesh-config/switch_tools.go index f4de6e738..37dec9a14 100644 --- a/modules/mesh-config/cmd/mesh-config/switch_tools.go +++ b/modules/mesh-config/cmd/mesh-config/switch_tools.go @@ -39,7 +39,7 @@ func ownSwitchTools(c *Config) []stdio.Tool { }}, {Name: ResyncTool, Description: "Read every source again and write what differs: the published list of predefined keys, the " + - "resolved values, declared, and the indexer's config candidates.", + "resolved values and declared.", Run: func(map[string]any) (any, error) { return c.Resync() }}, {Name: RenamesTool, Description: "Each config value renamed (renamed-from): from which name to which, when, the nodes its value " + diff --git a/modules/mesh-config/cmd/mesh-config/whole_integration_test.go b/modules/mesh-config/cmd/mesh-config/whole_integration_test.go index a32671dc3..1609cda12 100644 --- a/modules/mesh-config/cmd/mesh-config/whole_integration_test.go +++ b/modules/mesh-config/cmd/mesh-config/whole_integration_test.go @@ -850,7 +850,7 @@ func TestConfigWholeRenamesAndRetiresAgainstTheBus(t *testing.T) { p2.mu.Lock() p2.readers = []ReaderRow{{Module: "radio", Key: "band", Kind: KindValue, Readers: []string{}}} p2.mu.Unlock() - p2.event("mesh-catalog.manifest-accepted", map[string]any{"module": "radio"}) + p2.event(catalog.Seat+"."+catalog.EventCatalogRevision+".main", map[string]any{"v": 1, "revision": p2.catRev, "kind": "trunk", "branch": "main", "changed": []string{"radio"}}) eventually(t, "the retirement waits on g14", func() bool { raw, ok := p2.get(RetiringState, "radio.band") return ok && strings.Contains(string(raw), RetireDraining) diff --git a/modules/mesh-config/module.json b/modules/mesh-config/module.json index c54954179..58d0d9540 100644 --- a/modules/mesh-config/module.json +++ b/modules/mesh-config/module.json @@ -21,7 +21,6 @@ "consumes": [ "mesh-delivery-view.delivered", "node-engine.manifest-reads-changed", - "mesh-catalog.manifest-accepted", "mesh-delivery-indexer.views.predefined", "mesh-delivery-assigner.views.assignments", "mesh-delivery-assigner.views.wants", -- 2.54.0 From 0cafb62d0e48fcd56465b2cc9eb6e573107b47e2 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 02:51:02 +0100 Subject: [PATCH 11/43] CALLERS-1: claude-code and mesh-recovery keep no callers list for the seat list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mesh-sdk's callers set goes in H1, so the seat list's two tools of claude-code answer every caller, and mesh-recovery's seat list part is made with its prefix alone. Builds: design 47 H §752, §764 (every seat contract's callers); H-A-41, H-A-66 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/claude-code/insteadof_test.go | 22 ++++++++++++++----- modules/claude-code/cmd/claude-code/main.go | 15 +++++-------- .../mesh-recovery/cmd/mesh-recovery/main.go | 6 ++--- .../cmd/mesh-recovery/main_test.go | 4 ++-- modules/mesh-vault/files/retire-callers.json | 3 --- 5 files changed, 26 insertions(+), 24 deletions(-) delete mode 100644 modules/mesh-vault/files/retire-callers.json diff --git a/modules/claude-code/cmd/claude-code/insteadof_test.go b/modules/claude-code/cmd/claude-code/insteadof_test.go index a403dddf0..6901578a4 100644 --- a/modules/claude-code/cmd/claude-code/insteadof_test.go +++ b/modules/claude-code/cmd/claude-code/insteadof_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "os" + "path/filepath" "reflect" "slices" "strings" @@ -118,12 +119,21 @@ func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) { if got := strings.Join(addresses, ","); got != "mesh-module-assigner.assignments,node-service-manager.status,node-service-manager.journal,docker.docker_logs" { t.Errorf("replacements: %s", got) } - t.Run("the again tool answers the operator and the healers only", func(t *testing.T) { - // An operator's session through the mesh MCP server is proved as its machine's tool runner (issue 627, issue 1966). - for caller, refused := range map[string]bool{"person.jochen": false, "shanks.node-tools": false, "shanks.mesh-healers": false, - "shanks.mesh-config": true} { - if err := seatlist.Refusal("claude_code_seat_list_again", seatListAgainCallers, caller); (err != nil) != refused { - t.Errorf("%s: refused=%v, err %v", caller, refused, err) + t.Run("the again tool answers every caller", func(t *testing.T) { + // H-A-66 (design 47 H §752): no holder keeps a callers list; the seat list's tools answer every caller. + files, _ := filepath.Glob("*.go") + for _, f := range files { + if strings.HasSuffix(f, "_test.go") { + continue + } + raw, err := os.ReadFile(f) + if err != nil { + t.Fatal(err) + } + for _, gone := range []string{"AnyPerson", "seatListAgainCallers", "seatlist.Refusal"} { + if strings.Contains(string(raw), gone) { + t.Errorf("%s still names %s", f, gone) + } } } }) diff --git a/modules/claude-code/cmd/claude-code/main.go b/modules/claude-code/cmd/claude-code/main.go index 407b94b61..c6736cba8 100644 --- a/modules/claude-code/cmd/claude-code/main.go +++ b/modules/claude-code/cmd/claude-code/main.go @@ -734,17 +734,12 @@ func refusals(n int) string { return fmt.Sprintf(" (after %d refusal(s))", n) } -// seatListAgainCallers may read the seat list again: every operator's session and the healers (ADR 0636 §7). A call through -// the mesh MCP server is proved only as its machine's tool runner (.node-tools, issue 627), so node-tools stands for -// an operator's session until a session is proved (issue 1966). -var seatListAgainCallers = []string{stdio.AnyPerson, "node-tools", "mesh-healers"} - -// seatListTools are the seat list reader's two tools: what the list holds and what it waits for, and a read now. The -// reader starts once the runtime serves, after the tools are listed, so each tool finds it when it is called. +// seatListTools are the seat list reader's two tools: what the list holds and what it waits for, and a read now, each +// answering every caller (design 47 H §752, H-A-66). The reader starts once the runtime serves, after the tools are +// listed, so each tool finds it when it is called. func seatListTools() []stdio.Tool { names := []string{"claude_code_seat_list", "claude_code_seat_list_again"} - callers := seatListAgainCallers - shape := seatlist.Tools(&seatlist.Reader{}, names[0], names[1], callers) + shape := seatlist.Tools(&seatlist.Reader{}, names[0], names[1]) for i := range shape { run := i shape[i].Run = func(args map[string]any) (any, error) { @@ -754,7 +749,7 @@ func seatListTools() []stdio.Tool { if r == nil { return nil, errors.New("the seat list reader has not started yet: the runtime has not served this module") } - return seatlist.Tools(r, names[0], names[1], callers)[run].Run(args) + return seatlist.Tools(r, names[0], names[1])[run].Run(args) } } return shape diff --git a/modules/mesh-recovery/cmd/mesh-recovery/main.go b/modules/mesh-recovery/cmd/mesh-recovery/main.go index a713dfdc1..2f9ad82db 100644 --- a/modules/mesh-recovery/cmd/mesh-recovery/main.go +++ b/modules/mesh-recovery/cmd/mesh-recovery/main.go @@ -177,9 +177,9 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro for _, x := range e.Start(ctx) { logger.Printf("the aside %s of %s's %s holds data the live bus lacks: recovery_restore_aside puts it back", x.Aside.ID, x.Module, x.Kept) } - // The seat list, read over the connection and heard on the module's event hub (seatlist.Part), which the seat's verbs check - // their callers against (A-179, A-186): registered before the hub starts, read again once its events are live. - seats := seatlist.NewPart("recovery", nil) + // The seat list, read over the connection and heard on the module's event hub (seatlist.Part), from which the seat's verbs + // are answered (A-179, A-186): registered before the hub starts, read again once its events are live. + seats := seatlist.NewPart("recovery") seats.Follow(seatlist.ProcessBus(conn.Call, hub), nil, logger.Printf) if err := hub.StartUntilBound(ctx, js, account, nil, seats.EventsLive); err != nil { logger.Printf("the consumer %s is not bound yet (%v): bound once the bus makes it, the seat list read then", account.EventConsumerName(), err) diff --git a/modules/mesh-recovery/cmd/mesh-recovery/main_test.go b/modules/mesh-recovery/cmd/mesh-recovery/main_test.go index 413076145..5acca9376 100644 --- a/modules/mesh-recovery/cmd/mesh-recovery/main_test.go +++ b/modules/mesh-recovery/cmd/mesh-recovery/main_test.go @@ -40,13 +40,13 @@ func TestWhatTheProcessReads(t *testing.T) { t.Run("the seat's definition from the seat list once read (A-186)", func(t *testing.T) { t.Cleanup(busretry.Waits(20*time.Millisecond, time.Millisecond, 5*time.Millisecond)) follow := func(silent bool) *seatlist.Part { - p := seatlist.NewPart("recovery", nil) + p := seatlist.NewPart("recovery") if r := p.Follow(listingCatalog{silent: silent}, nil, t.Logf); r != nil { <-r.Ready() } return p } - if _, ok := seatlist.Definition(seatlist.NewPart("recovery", nil).Reader, meshbroker.Seat)(); ok { + if _, ok := seatlist.Definition(seatlist.NewPart("recovery").Reader, meshbroker.Seat)(); ok { t.Error("a definition is answered before the seat list's reader started") } if _, ok := seatlist.Definition(follow(true).Reader, meshbroker.Seat)(); ok { diff --git a/modules/mesh-vault/files/retire-callers.json b/modules/mesh-vault/files/retire-callers.json deleted file mode 100644 index 6d0792abe..000000000 --- a/modules/mesh-vault/files/retire-callers.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "callers": ["mesh-config"] -} -- 2.54.0 From f9857ab71212539a5265d2cccea8e77fb4704a20 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 02:51:02 +0100 Subject: [PATCH 12/43] CALLERS-2: the vault's secret-ask and retire callers lists go; its enrolment check stays MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every account may call every verb from H1, so secret-ask answers every caller the runtime proved and retire every caller whose warrant holds; the manifest's two lists, the config retire-callers and its file go. The enrolment verbs keep their one list, now a small type of the vault's own since mesh-sdk's Callers goes. Builds: design 47 H §752 (ADR 0682 §10's exception for the enrolment caller, H-A-11); H-A-41, H-A-66 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-vault/README.md | 5 +-- modules/mesh-vault/cmd/mesh-vault/adopt.go | 3 +- .../mesh-vault/cmd/mesh-vault/enrolment.go | 36 +++++++++++++++--- .../cmd/mesh-vault/enrolment_test.go | 9 +++-- .../cmd/mesh-vault/enrolment_tools.go | 7 ++-- modules/mesh-vault/cmd/mesh-vault/intake.go | 14 +++---- .../mesh-vault/cmd/mesh-vault/intake_test.go | 26 +++++-------- modules/mesh-vault/cmd/mesh-vault/main.go | 32 +++------------- .../cmd/mesh-vault/mint_integration_test.go | 6 +-- .../mesh-vault/cmd/mesh-vault/move_test.go | 7 ++-- .../mesh-vault/cmd/mesh-vault/noderotation.go | 5 ++- .../cmd/mesh-vault/owncalls_test.go | 12 ++---- .../mesh-vault/cmd/mesh-vault/ownretire.go | 24 +----------- modules/mesh-vault/cmd/mesh-vault/owntake.go | 7 ++-- modules/mesh-vault/cmd/mesh-vault/placed.go | 5 ++- .../mesh-vault/cmd/mesh-vault/retire_test.go | 14 +++---- .../mesh-vault/rotation_integration_test.go | 11 +++--- modules/mesh-vault/cmd/mesh-vault/signing.go | 5 ++- .../mesh-vault/cmd/mesh-vault/signing_test.go | 5 ++- modules/mesh-vault/cmd/mesh-vault/tools.go | 3 +- .../mesh-vault/cmd/mesh-vault/tools_test.go | 5 ++- modules/mesh-vault/cmd/mesh-vault/vault.go | 13 ++----- modules/mesh-vault/module.json | 38 +++---------------- 23 files changed, 118 insertions(+), 174 deletions(-) diff --git a/modules/mesh-vault/README.md b/modules/mesh-vault/README.md index cd22dca7f..4d03694e6 100644 --- a/modules/mesh-vault/README.md +++ b/modules/mesh-vault/README.md @@ -247,9 +247,8 @@ restart; it is on until switched off, and `own_drift` shows it. ## Retiring an own secret: `retire` (design 32) `retire` removes one module's own secret on a node because no manifest reads it any more, and says `own-retired` -(node, module, name, ask, caller). Two proofs: the caller is on the vault's trusted list `retire-callers` (the holder of -the seat `mesh-config` alone, a file the mesh writes from the config value, read at start; a list that cannot be read -allows nobody), and `warrant` is the id of a hand act that caller recorded through `mesh-hand-acts` for this verb, +(node, module, name, ask, caller). Every caller may ask it (design 47 H §752); one proof: `warrant` is the id of a hand +act that caller recorded through `mesh-hand-acts` for this verb, naming exactly the node, module, name and the ask of the operator's Approve, within a day. A bus account and the tool runner's `ledgers` are never retired this way. A waiter on `rotated` or `own-replaced` does not wake on it. diff --git a/modules/mesh-vault/cmd/mesh-vault/adopt.go b/modules/mesh-vault/cmd/mesh-vault/adopt.go index 86fb76d25..041a02ba6 100644 --- a/modules/mesh-vault/cmd/mesh-vault/adopt.go +++ b/modules/mesh-vault/cmd/mesh-vault/adopt.go @@ -19,10 +19,11 @@ package main import ( "errors" "fmt" - engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" "sort" "strings" "time" + + engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" ) // HaveRead is the node-engines' have bucket as the vault reads it (its manifest's `reads`). diff --git a/modules/mesh-vault/cmd/mesh-vault/enrolment.go b/modules/mesh-vault/cmd/mesh-vault/enrolment.go index a562add1a..110f26366 100644 --- a/modules/mesh-vault/cmd/mesh-vault/enrolment.go +++ b/modules/mesh-vault/cmd/mesh-vault/enrolment.go @@ -21,12 +21,13 @@ import ( "encoding/pem" "errors" "fmt" - enrollerclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshnodeenrollerclient" "os" "path/filepath" "strings" "time" + enrollerclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshnodeenrollerclient" + "git.novox.be/novox/mesh-contracts/identity-and-access/go/seats/meshvault" stdio "git.novox.be/novox/mesh-sdk/go" "golang.org/x/crypto/bcrypt" @@ -117,13 +118,38 @@ func (v *Vault) loginKeyRefusal(caller, publicKey string) string { return "" } +// callers is the vault's one list of callers, of its enrolment verbs (ADR 0682 §10's exception, H-A-11, H-A-41): each +// entry a module on any machine, . or person., matched against the caller the runtime proved. +type callers []string + +// Allows says whether caller, as the runtime proved it, is one the list names. +func (c callers) Allows(caller string) bool { + if caller == "" || caller == stdio.Unproven { + return false + } + for _, entry := range c { + if entry == "" || strings.ContainsAny(entry, "*> ") { + continue + } + if entry == caller { + return true + } + if !strings.Contains(entry, ".") { + if _, module, ok := strings.Cut(caller, "."); ok && module == entry && !strings.HasPrefix(caller, "person.") { + return true + } + } + } + return false +} + // fromEnrolment refuses every caller but the seat mesh-node-enroller, as the runtime proved it. func (v *Vault) fromEnrolment(caller, verb string) error { - callers := v.EnrolCallers - if callers == nil { - callers = stdio.Callers{EnrolmentModule} + list := v.EnrolCallers + if list == nil { + list = callers{EnrolmentModule} } - if !callers.Allows(caller) { + if !list.Allows(caller) { if caller == "" { caller = stdio.Unproven } diff --git a/modules/mesh-vault/cmd/mesh-vault/enrolment_test.go b/modules/mesh-vault/cmd/mesh-vault/enrolment_test.go index 7b1154325..6ccec7229 100644 --- a/modules/mesh-vault/cmd/mesh-vault/enrolment_test.go +++ b/modules/mesh-vault/cmd/mesh-vault/enrolment_test.go @@ -9,10 +9,6 @@ import ( "encoding/json" "encoding/pem" "fmt" - assignerclient "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" - placing "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" - enrollerclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshnodeenrollerclient" - vaultclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshvaultclient" "slices" "sort" "strings" @@ -20,6 +16,11 @@ import ( "testing" "time" + assignerclient "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" + placing "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" + enrollerclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshnodeenrollerclient" + vaultclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshvaultclient" + "git.novox.be/novox/mesh-contracts/identity-and-access/go/seats/meshvault" "golang.org/x/crypto/bcrypt" "golang.org/x/crypto/nacl/box" diff --git a/modules/mesh-vault/cmd/mesh-vault/enrolment_tools.go b/modules/mesh-vault/cmd/mesh-vault/enrolment_tools.go index 59160165f..16e11647c 100644 --- a/modules/mesh-vault/cmd/mesh-vault/enrolment_tools.go +++ b/modules/mesh-vault/cmd/mesh-vault/enrolment_tools.go @@ -9,10 +9,11 @@ import ( "encoding/json" "errors" "fmt" - enrollerclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshnodeenrollerclient" "sort" "strings" "time" + + enrollerclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshnodeenrollerclient" ) // EnrolmentHeld is one enrolment token the vault holds, as secret_holders lists it: never the token or its hash. @@ -129,8 +130,8 @@ func enrolmentTools(env *Env) []toolDef { }}, {Name: Seat + "." + RetireVerb, Description: "Retire one module's own secret on a node because no manifest reads it any more (design 32): removed " + - "whole, and own-retired said (node, module, name, the ask, the caller; never a value). Answered to the holder of " + - "the seat mesh-config alone, as the vault's list retire-callers names it, and only for a hand act it recorded " + + "whole, and own-retired said (node, module, name, the ask, the caller; never a value). Answered to any caller, " + + "and only for a hand act it recorded " + "through mesh-hand-acts for this verb naming exactly this node, module and name and the ask of the operator's Approve " + "(`warrant`, the act's id), read back here.", Input: map[string]any{"node": node, "module": str("the module whose secret it is"), "name": str("the secret's name"), diff --git a/modules/mesh-vault/cmd/mesh-vault/intake.go b/modules/mesh-vault/cmd/mesh-vault/intake.go index 38d49d00a..04971c921 100644 --- a/modules/mesh-vault/cmd/mesh-vault/intake.go +++ b/modules/mesh-vault/cmd/mesh-vault/intake.go @@ -269,17 +269,15 @@ type AskOf struct { Node, Module, Name, Desk string } -// askedByFor is who asked a secret-ask as the bus says it (novox/hq ADR 0563 §8): a caller the manifest names, as the -// runtime proved it. Any other caller is refused: no caller names itself, and none relays another's word. +// askedByFor is who asked a secret-ask as the bus says it (novox/hq ADR 0563 §8): the caller the runtime proved, any +// caller (design 47 H §752, H-A-41: no list of callers). A caller the runtime did not prove is refused: no caller names +// itself, and none relays another's word. func (v *Vault) askedByFor(caller string, a AskOf) (string, error) { - if v.AskCallers.Allows(caller) { + if caller != "" && caller != stdio.Unproven { return askedByName(caller), nil } - if caller == "" { - caller = stdio.Unproven - } - return "", fmt.Errorf("secret-ask answers the callers the vault's manifest names, and %s is not one; nobody was "+ - "asked to type anything", caller) + return "", fmt.Errorf("secret-ask answers a caller the runtime proved, and this one is %s; nobody was asked to type "+ + "anything", stdio.Unproven) } // AskOpen records an ask for a module's own secret before any prompt opens, holding its bounds — one open ask diff --git a/modules/mesh-vault/cmd/mesh-vault/intake_test.go b/modules/mesh-vault/cmd/mesh-vault/intake_test.go index 610b84d08..8ae73ecf7 100644 --- a/modules/mesh-vault/cmd/mesh-vault/intake_test.go +++ b/modules/mesh-vault/cmd/mesh-vault/intake_test.go @@ -13,7 +13,6 @@ import ( "encoding/base64" "encoding/json" "errors" - stdio "git.novox.be/novox/mesh-sdk/go" "os" "path/filepath" "strings" @@ -84,7 +83,6 @@ func telegramAsk(by string) AskOf { // it; the typed value opens only in the vault; the controller carries ciphertext only. func TestAnAskIsRecordedBeforeThePromptAndTheTypedValueOpensOnlyInTheVault(t *testing.T) { v := newVault(t, AllowList{"anchor.node-provisioner": {"own/anchor/telegram/telegram-token"}}) - v.AskCallers = stdio.Callers{"g14.claude-code"} opened, err := v.AskOpen("g14.claude-code", telegramAsk("g14/claude-code, via the console")) if err != nil { t.Fatal(err) @@ -126,7 +124,6 @@ func TestAnAskIsRecordedBeforeThePromptAndTheTypedValueOpensOnlyInTheVault(t *te // One open ask per secret, and three an hour (ADR 0277 §24), read from the vault's own record. func TestAnAskIsBoundedToOneOpenPerSecretAndThreeAnHour(t *testing.T) { v := newVault(t, nil) - v.AskCallers = stdio.Callers{"g14.claude-code"} first, err := v.AskOpen("g14.claude-code", telegramAsk("g14.claude-code")) if err != nil { t.Fatal(err) @@ -167,7 +164,6 @@ func TestAnAskIsBoundedToOneOpenPerSecretAndThreeAnHour(t *testing.T) { } // An ask left open counts as open for SecretAskOpenFor only: a prompt that died does not hold the secret. w := newVault(t, nil) - w.AskCallers = stdio.Callers{"g14.claude-code"} clock := time.Date(2026, 10, 14, 9, 0, 0, 0, time.UTC) w.Now = func() time.Time { return clock } if _, err := w.AskOpen("g14.claude-code", telegramAsk("g14.claude-code")); err != nil { @@ -177,13 +173,12 @@ func TestAnAskIsBoundedToOneOpenPerSecretAndThreeAnHour(t *testing.T) { if _, err := w.AskOpen("g14.claude-code", telegramAsk("g14.claude-code")); err != nil { t.Errorf("an ask left open past its time still held the secret: %v", err) } - // 774 (ADR 0563 §8): a caller the manifest names asks directly, is named by its proven caller and never by the - // asked_by it gives, and is held to the same bounds; one it does not name is refused. - t.Run("a named caller asks itself, within the bounds", func(t *testing.T) { + // 774 (ADR 0563 §8): a proved caller asks directly, is named by its proven caller and never by the asked_by it + // gives, and is held to the same bounds; one the runtime did not prove is refused (design 47 H §752, H-A-41). + t.Run("a proved caller asks itself, within the bounds", func(t *testing.T) { x := newVault(t, nil) - x.AskCallers = stdio.Callers{"node-tools"} - if _, err := x.AskOpen("ace.claude-code", telegramAsk("x")); err == nil || !strings.Contains(err.Error(), "ace.claude-code") { - t.Errorf("a caller the manifest does not name opened an ask: %v", err) + if _, err := x.AskOpen("unproven", telegramAsk("x")); err == nil { + t.Errorf("an unproven caller opened an ask: %v", err) } opened, err := x.AskOpen("g14.node-tools", telegramAsk("person.operator")) if err != nil || opened.AskedBy != "g14.node-tools" { @@ -202,12 +197,11 @@ func TestAnAskIsBoundedToOneOpenPerSecretAndThreeAnHour(t *testing.T) { }) } -// Only a caller the vault's manifest names opens an ask, and only it ends it; a value not sealed to the call's key, or -// a call that waited too long, keeps nothing. -func TestAnAskIsTakenFromANamedCallerAloneAndOnlySealedToItsCall(t *testing.T) { +// Only a caller the runtime proved opens an ask, and only it ends it; a value not sealed to the call's key, or a call +// that waited too long, keeps nothing. +func TestAnAskIsTakenFromAProvedCallerAloneAndOnlySealedToItsCall(t *testing.T) { v := newVault(t, nil) - v.AskCallers = stdio.Callers{"g14.claude-code"} - for _, caller := range []string{"g14.node-tools", "unproven", "", "person.jochen"} { + for _, caller := range []string{"unproven", ""} { if _, err := v.AskOpen(caller, telegramAsk("x")); err == nil { t.Errorf("%q opened an ask", caller) } @@ -448,7 +442,6 @@ func TestTheNamesOfWhatIsTakenIn(t *testing.T) { // Each verb takes its caller from the runtime, never from its arguments, and names its secret one way only. func TestTheVerbsTakeTheirCallerFromTheRuntime(t *testing.T) { v := newVault(t, nil) - v.AskCallers = stdio.Callers{"g14.claude-code"} env := &Env{Vault: v.Vault} defs := definitions(env) var asked []map[string]any @@ -493,7 +486,6 @@ func TestTheVerbsTakeTheirCallerFromTheRuntime(t *testing.T) { // With the lock held elsewhere, AskEnd waits, and writes nothing until it is let go. func TestTheEndOfAnAskIsWrittenUnderTheRecordsLock(t *testing.T) { v := newVault(t, nil) - v.AskCallers = stdio.Callers{"g14.claude-code"} opened, err := v.AskOpen("g14.claude-code", telegramAsk("g14.claude-code")) if err != nil { t.Fatal(err) diff --git a/modules/mesh-vault/cmd/mesh-vault/main.go b/modules/mesh-vault/cmd/mesh-vault/main.go index a52660395..7919f2faa 100644 --- a/modules/mesh-vault/cmd/mesh-vault/main.go +++ b/modules/mesh-vault/cmd/mesh-vault/main.go @@ -65,8 +65,8 @@ func build() *Env { Assignments: Mirror{Keys: assignmentKeys, Read: readAssignment}, Emit: emit, Ask: stdio.Ask, Now: time.Now, Log: say, Accounts: busAccounts{}, Haves: readHave, WantRead: readWant, HaveKeys: func() ([]string, error) { return stdio.State(HaveRead).Keys() }, Assigner: seatAssignments, Signers: busSigners{}, - AskCallers: askCallers(os.Getenv("MESH_VAULT_SECRET_ASK_CALLERS")), EnrolCallers: askCallers(os.Getenv("MESH_VAULT_ENROLMENT_CALLERS")), - RetireCallers: retireCallers(os.Getenv("MESH_VAULT_RETIRE_CALLERS")), Logins: busNodeLogins{}, ReadAct: readHandAct, TokenBase: TokenBaseFromFile(os.Getenv("MESH_VAULT_STATE")), + EnrolCallers: callerList(os.Getenv("MESH_VAULT_ENROLMENT_CALLERS")), + Logins: busNodeLogins{}, ReadAct: readHandAct, TokenBase: TokenBaseFromFile(os.Getenv("MESH_VAULT_STATE")), Bound: Bound{ Get: func(key string) ([]byte, error) { e, err := stdio.State(BoundState).Get(key) @@ -414,30 +414,10 @@ func main() { } } -// retireCallers reads the vault's list `retire-callers`, the file the mesh writes from its trusted config value -// (stdio.ParseCallers): a list that cannot be read allows nobody, and says why. -func retireCallers(path string) stdio.Callers { - if path == "" { - say("the vault was started without its retire-callers list (MESH_VAULT_RETIRE_CALLERS): retire answers nobody") - return nil - } - raw, err := os.ReadFile(path) - if err != nil { - say("the retire-callers list cannot be read, so retire answers nobody: %v", err) - return nil - } - callers, err := stdio.ParseCallers(raw) - if err != nil { - say("%v: retire answers nobody", err) - return nil - } - return callers -} - -// askCallers are the callers the vault's manifest names for secret-ask besides the controller (novox/hq ADR 0563 §8): -// MESH_VAULT_SECRET_ASK_CALLERS, comma-separated, each a module (on any machine), . or person.. -func askCallers(list string) stdio.Callers { - var out stdio.Callers +// callerList is the vault's manifest's list of the enrolment verbs' callers (MESH_VAULT_ENROLMENT_CALLERS), +// comma-separated, each a module (on any machine), . or person.. +func callerList(list string) callers { + var out callers for _, c := range strings.Split(list, ",") { if c = strings.TrimSpace(c); c != "" { out = append(out, c) diff --git a/modules/mesh-vault/cmd/mesh-vault/mint_integration_test.go b/modules/mesh-vault/cmd/mesh-vault/mint_integration_test.go index f4c9bef9a..63e9364c1 100644 --- a/modules/mesh-vault/cmd/mesh-vault/mint_integration_test.go +++ b/modules/mesh-vault/cmd/mesh-vault/mint_integration_test.go @@ -524,14 +524,10 @@ func TestTheVaultWholeMintsOnTheBusWhoeverHoldsTheLease(t *testing.T) { if _, err := seed.keep("own/g14/dunst/token", "a-token-value", "the test", "g14.dunst", OriginMade); err != nil { t.Fatal(err) } - callers := filepath.Join(dir, "retire-callers.json") - if err := os.WriteFile(callers, []byte(`{"callers":["mesh-config"]}`), 0o600); err != nil { - t.Fatal(err) - } cmd := exec.CommandContext(ctx, bin) cmd.Env = append(os.Environ(), "MESH_VAULT_STORE="+filepath.Join(dir, "store"), "MESH_VAULT_STATE="+filepath.Join(dir, "state"), "MESH_VAULT_ROOT="+root, "MESH_VAULT_LEDGER="+filepath.Join(dir, "ledger"), "MESH_RECEIVES="+filepath.Join(dir, "mesh.json"), - "MESH_VAULT_RETIRE_CALLERS="+callers, "MESH_NODE=novox", "MESH_MODULE=mesh-vault") + "MESH_NODE=novox", "MESH_MODULE=mesh-vault") stdin, _ := cmd.StdinPipe() stdout, _ := cmd.StdoutPipe() cmd.Stderr = os.Stderr diff --git a/modules/mesh-vault/cmd/mesh-vault/move_test.go b/modules/mesh-vault/cmd/mesh-vault/move_test.go index 2f55ed3bc..5d70a69c0 100644 --- a/modules/mesh-vault/cmd/mesh-vault/move_test.go +++ b/modules/mesh-vault/cmd/mesh-vault/move_test.go @@ -10,13 +10,14 @@ package main import ( "crypto/rand" "encoding/base64" - engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - nodeseat "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - vaultclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshvaultclient" "strings" "testing" "time" + engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" + nodeseat "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" + vaultclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshvaultclient" + "git.novox.be/novox/mesh-sdk/go/seal" "golang.org/x/crypto/nacl/box" ) diff --git a/modules/mesh-vault/cmd/mesh-vault/noderotation.go b/modules/mesh-vault/cmd/mesh-vault/noderotation.go index 0c8dbc8be..99b292983 100644 --- a/modules/mesh-vault/cmd/mesh-vault/noderotation.go +++ b/modules/mesh-vault/cmd/mesh-vault/noderotation.go @@ -22,14 +22,15 @@ import ( "encoding/json" "errors" "fmt" - vaultclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshvaultclient" - "git.novox.be/novox/mesh-contracts/identity-and-access/go/seats/meshvault" "slices" "sort" "strings" "sync" "time" + vaultclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshvaultclient" + "git.novox.be/novox/mesh-contracts/identity-and-access/go/seats/meshvault" + stdio "git.novox.be/novox/mesh-sdk/go" "golang.org/x/crypto/bcrypt" ) diff --git a/modules/mesh-vault/cmd/mesh-vault/owncalls_test.go b/modules/mesh-vault/cmd/mesh-vault/owncalls_test.go index 74352692d..6607146b3 100644 --- a/modules/mesh-vault/cmd/mesh-vault/owncalls_test.go +++ b/modules/mesh-vault/cmd/mesh-vault/owncalls_test.go @@ -10,7 +10,6 @@ import ( "encoding/base64" "encoding/json" "errors" - stdio "git.novox.be/novox/mesh-sdk/go" "strings" "sync" "testing" @@ -94,7 +93,6 @@ func aDesk(typed string, asked *[]map[string]any) func(string, map[string]any) ( // is typed opened here alone; what crossed is a key and a sealed box. func TestTheVaultAsksTheDesksPromptItselfAndOnlyASealedValueCrosses(t *testing.T) { v := newVault(t, AllowList{"anchor.node-provisioner": {"own/anchor/telegram/telegram-token"}}) - v.AskCallers = stdio.Callers{"g14.claude-code"} var asked []map[string]any mesh := &aMesh{desk: aDesk(typedAtTheDesk, &asked)} v.Ask = mesh.ask @@ -133,7 +131,6 @@ func TestTheVaultAsksTheDesksPromptItselfAndOnlyASealedValueCrosses(t *testing.T // Dismissed and timed out end the ask so, and keep nothing. for typed, outcome := range map[string]string{"": "dismissed", "\x00timeout": "timed-out"} { w := newVault(t, nil) - w.AskCallers = stdio.Callers{"g14.claude-code"} var a []map[string]any w.Ask = (&aMesh{desk: aDesk(typed, &a)}).ask got, err := w.AskAtDesk("g14.claude-code", telegramAsk("g14.claude-code")) @@ -149,7 +146,6 @@ func TestTheVaultAsksTheDesksPromptItselfAndOnlyASealedValueCrosses(t *testing.T } // Answered empty: the operator's answer, said so, nothing kept. e := newVault(t, nil) - e.AskCallers = stdio.Callers{"g14.claude-code"} var ea []map[string]any e.Ask = (&aMesh{desk: aDesk(" ", &ea)}).ask if got, err := e.AskAtDesk("g14.claude-code", telegramAsk("g14.claude-code")); err != nil || got.Outcome != "empty" { @@ -157,7 +153,6 @@ func TestTheVaultAsksTheDesksPromptItselfAndOnlyASealedValueCrosses(t *testing.T } // The desk cannot be asked: refused, recorded so. r := newVault(t, nil) - r.AskCallers = stdio.Callers{"g14.claude-code"} r.Ask = (&aMesh{}).ask if _, err := r.AskAtDesk("g14.claude-code", telegramAsk("g14.claude-code")); err == nil { t.Error("an ask nobody answered was said to be done") @@ -165,17 +160,18 @@ func TestTheVaultAsksTheDesksPromptItselfAndOnlyASealedValueCrosses(t *testing.T if asks, _ := r.Store.Asks(1); asks[0].Outcome != "refused" { t.Errorf("an unanswered desk recorded %+v", asks[0]) } - // Only the controller asks the vault to ask: anybody else is refused before any prompt. + // Every caller the runtime proved may ask the vault to ask (design 47 H §752, H-A-41); one it did not prove is refused + // before any prompt. n := newVault(t, nil) var na []map[string]any n.Ask = (&aMesh{desk: aDesk(typedAtTheDesk, &na)}).ask - for _, caller := range []string{"g14.node-tools", "g14.claude-code", "unproven", ""} { + for _, caller := range []string{"unproven", ""} { if _, err := n.AskAtDesk(caller, telegramAsk("x")); err == nil { t.Errorf("%q had the vault ask a desk", caller) } } if len(na) != 0 { - t.Fatal("a desk was asked for a caller that is not the controller") + t.Fatal("a desk was asked for a caller the runtime did not prove") } } diff --git a/modules/mesh-vault/cmd/mesh-vault/ownretire.go b/modules/mesh-vault/cmd/mesh-vault/ownretire.go index b85d19363..1a86636a2 100644 --- a/modules/mesh-vault/cmd/mesh-vault/ownretire.go +++ b/modules/mesh-vault/cmd/mesh-vault/ownretire.go @@ -1,12 +1,9 @@ package main // The seat's verb `retire` (design 32 *A value no longer read*, novox/hq ADR 0609): one module's own secret on one node -// removed because no manifest reads it any more, asked by the holder of the seat mesh-config alone after the operator's -// Approve. Two proofs, both required: +// removed because no manifest reads it any more, asked after the operator's Approve; every caller may ask it (design 47 +// H §752, H-A-41). One proof is required: // -// - the caller is on the vault's list `retire-callers`, a trusted config value of the vault (ADR 0363's form, as -// mesh-mail-reader's `callers`), which names the holder of mesh-config alone, checked against the caller the tool -// runner proved (stdio.Callers); and // - the warrant (the verb's argument, as the seat's definition names it): the id of a hand act the caller recorded // through mesh-hand-acts for this verb, read back here, recent, naming exactly this node, module and secret and the // ask whose Approve it answers (ADR 0259 §6). An act for another name warrants nothing. @@ -17,8 +14,6 @@ package main import ( "fmt" "strings" - - stdio "git.novox.be/novox/mesh-sdk/go" ) // OwnRetiredEvent is what the vault says when it retired an own secret. @@ -47,18 +42,6 @@ type OwnRetired struct { Retired bool `json:"retired"` } -// fromRetireCallers refuses every caller the vault's list `retire-callers` does not name. -func (v *Vault) fromRetireCallers(caller string) error { - if !v.RetireCallers.Allows(caller) { - if strings.TrimSpace(caller) == "" { - caller = stdio.Unproven - } - return fmt.Errorf("retire is asked by the holder of the seat mesh-config alone, as the vault's list retire-callers "+ - "names it; %s is not on it (design 32)", caller) - } - return nil -} - // warrantsRetire reads the hand act back and refuses what it does not warrant for exactly this secret; it answers the // ask the act names. func (v *Vault) warrantsRetire(caller string, a RetireArgs) (ask string, err error) { @@ -99,9 +82,6 @@ func (v *Vault) warrantsRetire(caller string, a RetireArgs) (ask string, err err // RetireOwn removes one module's own secret on a node on the warrant of a hand act, and says `own-retired`. func (v *Vault) RetireOwn(caller string, a RetireArgs) (OwnRetired, error) { - if err := v.fromRetireCallers(caller); err != nil { - return OwnRetired{}, err - } secret, err := OwnSecret(a.Node, a.Module, a.Name) if err != nil { return OwnRetired{}, err diff --git a/modules/mesh-vault/cmd/mesh-vault/owntake.go b/modules/mesh-vault/cmd/mesh-vault/owntake.go index e5c0ca07e..f250c9d96 100644 --- a/modules/mesh-vault/cmd/mesh-vault/owntake.go +++ b/modules/mesh-vault/cmd/mesh-vault/owntake.go @@ -26,15 +26,16 @@ import ( "encoding/json" "errors" "fmt" - engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - nodeseat "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - vaultclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshvaultclient" "os" "path/filepath" "sort" "strings" "time" + engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" + nodeseat "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" + vaultclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshvaultclient" + stdio "git.novox.be/novox/mesh-sdk/go" ) diff --git a/modules/mesh-vault/cmd/mesh-vault/placed.go b/modules/mesh-vault/cmd/mesh-vault/placed.go index fb37a1411..733206fd6 100644 --- a/modules/mesh-vault/cmd/mesh-vault/placed.go +++ b/modules/mesh-vault/cmd/mesh-vault/placed.go @@ -9,11 +9,12 @@ package main import ( "errors" "fmt" - assignerclient "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" - placing "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" "slices" "sort" "strings" + + assignerclient "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" + placing "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" ) // AssignerReader reads the seat's assignments: every key, and one key's value (nil when there is none). diff --git a/modules/mesh-vault/cmd/mesh-vault/retire_test.go b/modules/mesh-vault/cmd/mesh-vault/retire_test.go index 613764804..a40be7a25 100644 --- a/modules/mesh-vault/cmd/mesh-vault/retire_test.go +++ b/modules/mesh-vault/cmd/mesh-vault/retire_test.go @@ -83,14 +83,13 @@ func TestAWantGoneRetiresItsAccountOnlyWithinTheBounds(t *testing.T) { } } -// 1966/CAT-V1 (design 32): `retire` removes one module's own secret on a node, and says own-retired, only for a caller on -// the vault's list retire-callers who carries a hand act it recorded for this verb naming exactly this node, module and -// secret. Without the act, with another name's, with another caller's or an older one, from a caller not on the list, or -// for a bus account, nothing is removed and nothing is said; a waiter on rotated or own-replaced is not woken. -func TestRetireRemovesAnOwnSecretOnlyOnTheWarrantOfTheListedCaller(t *testing.T) { +// 1966/CAT-V1 (design 32): `retire` removes one module's own secret on a node, and says own-retired, only for a caller +// who carries a hand act it recorded for this verb naming exactly this node, module and secret; every caller may ask it +// (design 47 H §752, H-A-41: no list of callers). Without the act, with another name's, with another caller's or an older +// one, or for a bus account, nothing is removed and nothing is said; a waiter on rotated or own-replaced is not woken. +func TestRetireRemovesAnOwnSecretOnlyOnTheCallersWarrant(t *testing.T) { v := newVault(t, nil) const caller = "novox.mesh-config" - v.RetireCallers = stdio.Callers{"mesh-config"} acts := map[string]*HandActSeen{} v.ReadAct = func(id string) (*HandActSeen, error) { return acts[id], nil } act := func(id, by, verb, what string, age time.Duration) { @@ -125,8 +124,7 @@ func TestRetireRemovesAnOwnSecretOnlyOnTheWarrantOfTheListedCaller(t *testing.T) {"no ask", caller, RetireArgs{Node: "g14", Module: "dunst", Name: "token", Warrant: "ok"}, "names the ask"}, {"an act that names no ask", caller, RetireArgs{Node: "g14", Module: "dunst", Name: "token", Warrant: "no-ask", Ask: "ret-1"}, "a warrant retires nothing else"}, {"an act older than a day", caller, RetireArgs{Node: "g14", Module: "dunst", Name: "token", Warrant: "old", Ask: "ret-1"}, "older than"}, - {"a caller not on the list", "g14.claude-code", RetireArgs{Node: "g14", Module: "dunst", Name: "token", Warrant: "other-caller", Ask: "ret-1"}, "retire-callers"}, - {"an unproven caller", stdio.Unproven, RetireArgs{Node: "g14", Module: "dunst", Name: "token", Warrant: "ok", Ask: "ret-1"}, "retire-callers"}, + {"an unproven caller", stdio.Unproven, RetireArgs{Node: "g14", Module: "dunst", Name: "token", Warrant: "ok", Ask: "ret-1"}, "was recorded by novox.mesh-config"}, {"the tool runner's ledgers", caller, RetireArgs{Node: "g14", Module: "node-tools", Name: "ledgers", Warrant: "ledgers", Ask: "ret-1"}, "bus account or the tool runner's ledgers"}, } { t.Run(tc.name, func(t *testing.T) { diff --git a/modules/mesh-vault/cmd/mesh-vault/rotation_integration_test.go b/modules/mesh-vault/cmd/mesh-vault/rotation_integration_test.go index 4feb85df4..5a274a589 100644 --- a/modules/mesh-vault/cmd/mesh-vault/rotation_integration_test.go +++ b/modules/mesh-vault/cmd/mesh-vault/rotation_integration_test.go @@ -15,11 +15,6 @@ import ( "encoding/base64" "encoding/json" "errors" - assignerclient "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" - engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - placing "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" - nodeseat "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - vaultclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshvaultclient" "os" "os/exec" "path/filepath" @@ -28,6 +23,12 @@ import ( "testing" "time" + assignerclient "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" + engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" + placing "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" + nodeseat "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" + vaultclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshvaultclient" + "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" "golang.org/x/crypto/bcrypt" diff --git a/modules/mesh-vault/cmd/mesh-vault/signing.go b/modules/mesh-vault/cmd/mesh-vault/signing.go index db4cd9ec2..0ba246247 100644 --- a/modules/mesh-vault/cmd/mesh-vault/signing.go +++ b/modules/mesh-vault/cmd/mesh-vault/signing.go @@ -16,11 +16,12 @@ import ( "encoding/pem" "errors" "fmt" + "strings" + "time" + engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" enrollerclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshnodeenrollerclient" vaultclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshvaultclient" - "strings" - "time" "git.novox.be/novox/mesh-contracts/identity-and-access/go/seats/meshvault" "git.novox.be/novox/mesh-networking/modules/mesh-wireguard/go/overlay" diff --git a/modules/mesh-vault/cmd/mesh-vault/signing_test.go b/modules/mesh-vault/cmd/mesh-vault/signing_test.go index 8abf89c51..980f02182 100644 --- a/modules/mesh-vault/cmd/mesh-vault/signing_test.go +++ b/modules/mesh-vault/cmd/mesh-vault/signing_test.go @@ -5,14 +5,15 @@ import ( "crypto/rand" "encoding/base64" "encoding/json" + "strings" + "testing" + assignerclient "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" placing "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" nodeseat "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" vaultclient "git.novox.be/novox/mesh-contracts/identity-and-access/go/clients/meshvaultclient" "git.novox.be/novox/mesh-contracts/identity-and-access/go/seats/meshvault" - "strings" - "testing" "git.novox.be/novox/mesh-networking/modules/mesh-wireguard/go/overlay" "git.novox.be/novox/mesh-sdk/go/seal" diff --git a/modules/mesh-vault/cmd/mesh-vault/tools.go b/modules/mesh-vault/cmd/mesh-vault/tools.go index ad424f8fa..930905e7c 100644 --- a/modules/mesh-vault/cmd/mesh-vault/tools.go +++ b/modules/mesh-vault/cmd/mesh-vault/tools.go @@ -12,13 +12,14 @@ import ( "encoding/json" "errors" "fmt" - "git.novox.be/novox/mesh-sdk/go/seal" "os" "path/filepath" "strings" "sync" "time" + "git.novox.be/novox/mesh-sdk/go/seal" + stdio "git.novox.be/novox/mesh-sdk/go" ) diff --git a/modules/mesh-vault/cmd/mesh-vault/tools_test.go b/modules/mesh-vault/cmd/mesh-vault/tools_test.go index da747cdea..daabdb6ad 100644 --- a/modules/mesh-vault/cmd/mesh-vault/tools_test.go +++ b/modules/mesh-vault/cmd/mesh-vault/tools_test.go @@ -6,14 +6,15 @@ package main import ( "encoding/base64" "encoding/json" - engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - nodeseat "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" "os" "path/filepath" "strings" "testing" "time" + engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" + nodeseat "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" + "git.novox.be/novox/mesh-sdk/go/seal" ) diff --git a/modules/mesh-vault/cmd/mesh-vault/vault.go b/modules/mesh-vault/cmd/mesh-vault/vault.go index aac01cdf1..35c4b5d9b 100644 --- a/modules/mesh-vault/cmd/mesh-vault/vault.go +++ b/modules/mesh-vault/cmd/mesh-vault/vault.go @@ -46,12 +46,8 @@ type Vault struct { // account (ADR 0259 §8's runs-as, issue 723): the desks' secret prompt and each issuing provider's tool, which // the bus lets the vault's account alone call in its own name. Nil asks nobody. Ask func(key string, body any) (json.RawMessage, error) - // AskCallers are the callers besides the controller that may ask secret-ask themselves, as the vault's manifest names - // them (MESH_VAULT_SECRET_ASK_CALLERS; novox/hq ADR 0563 §8): each is named by the runtime's proof. Empty: the - // controller alone. - AskCallers stdio.Callers - Now func() time.Time - Log func(format string, args ...any) + Now func() time.Time + Log func(format string, args ...any) // Accounts is the vault's state of bus-account hashes (accounts.go, ADR 0547 clause 9): nil writes none. Accounts AccountStore // HashCost is the bcrypt cost of those hashes: zero is the controller's, bcrypt's default. @@ -82,10 +78,7 @@ type Vault struct { Signers SignersStore // EnrolCallers are the callers of the enrolment verbs `enrolment-token` and `node-account` and of `sign` for an // enrolment: the seat mesh-node-enroller (enrolment.go). Nil: that module alone. - EnrolCallers stdio.Callers - // RetireCallers are the callers of the seat's verb `retire`: the vault's trusted config value `retire-callers`, the - // holder of the seat mesh-config alone (ownretire.go). Nil: nobody. - RetireCallers stdio.Callers + EnrolCallers callers // TokenBase says where the bus is and its certificate's fingerprint, for the token shown (enrolment.go): nil shows none. TokenBase func() (TokenBase, error) // Logins is the vault's state node-logins (noderotation.go): nil writes none. diff --git a/modules/mesh-vault/module.json b/modules/mesh-vault/module.json index a29064c11..bbf58cfb5 100644 --- a/modules/mesh-vault/module.json +++ b/modules/mesh-vault/module.json @@ -149,25 +149,16 @@ "mode": "0700", "owner": "mesh-vault", "unknown": "program" - }, - { - "id": "retire-callers", - "trusted": true, - "type": "file", - "path": "${dir:state}/retire-callers.json", - "mode": "0600", - "owner": "mesh-vault", - "merge": "json", - "template": "files/retire-callers.json", - "takes-config": [ - "retire-callers" - ] } ], "streams": [ { "name": "mesh-vault-findings", - "events": ["finding-written.*", "finding-cleared.*", "findings-stated.*"], + "events": [ + "finding-written.*", + "finding-cleared.*", + "findings-stated.*" + ], "max-age-days": 7, "max-mib": 16 } @@ -190,15 +181,8 @@ "MESH_VAULT_ROOT": "${dir:root}", "MESH_VAULT_STORE": "${dir:store}", "MESH_VAULT_STATE": "${dir:state}", - "MESH_VAULT_SECRET_ASK_CALLERS": "node-tools", - "MESH_VAULT_ENROLMENT_CALLERS": "mesh-enrolment", - "MESH_VAULT_RETIRE_CALLERS": "${dir:state}/retire-callers.json" + "MESH_VAULT_ENROLMENT_CALLERS": "mesh-enrolment" } - }, - { - "name": "files", - "kind": "archive", - "from": "files" } ] }, @@ -253,16 +237,6 @@ ] } ], - "config": { - "retire-callers": { - "type": "list", - "description": "Who may call the seat's verb retire, which removes one module's own secret on a node because no manifest reads it any more: a module's name (on every machine), ., or person.. The holder of the seat mesh-config alone by default (design 32); the verb also needs a hand act the caller recorded for exactly that node, module and secret.", - "trusted": true, - "default": [ - "mesh-config" - ] - } - }, "consumes": [ "mesh-delivery-assigner.views.assignments", "mesh-conditions.findings-asked", -- 2.54.0 From e44a2d140d738ce48c103b0fb69575f09bdcfe12 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:12:57 +0100 Subject: [PATCH 13/43] GATE-4, GATE-5: a merge call names one source revision per branch and opens its record MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The gate consumes the seat git's merge-called and, per watched branch its members name, writes source. once by compare-and-set keyed by the call, numbered one above the branch's newest; opens that revision's record keyed . with the call, by and account in its trace; says record-opened on record-opened. and source-moved on source-moved... The take-in.taken writes its movers into the record read by its key, and a take-in with no mover ends it as nothing to deliver. The verb groups, the record's pins and pins-stated, forge-made and the emitted transition go. One commit for both subtasks: the record's opening is the handler's second half and neither compiles alone against the contracts. Builds: design 47 H §737, §738 (a take-in with no movers ends its record), §749 (the pins are the indexer's), §764 (the gate: the source moved on take-in.taken, the verb groups); H-A-13, H-A-19, H-A-24, H-A-25, H-A-28, H-A-29, H-A-33, H-A-47 (the start writes through the seed's path, no record), H-A-48 (pins, pins-stated, transition, members @), H-A-76 (Q-G1: the say-again carries the value's heads, start false) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/mesh-delivery-gate/consumer.go | 33 +-- .../cmd/mesh-delivery-gate/main.go | 29 +-- .../cmd/mesh-delivery-gate/manifest_test.go | 13 +- .../cmd/mesh-delivery-gate/mergecalled.go | 193 ++++++++++++++ .../mesh-delivery-gate/mergecalled_test.go | 127 ++++++++++ .../cmd/mesh-delivery-gate/merges.go | 6 +- .../cmd/mesh-delivery-gate/owedacts_test.go | 2 +- .../cmd/mesh-delivery-gate/record.go | 167 +++++++------ .../cmd/mesh-delivery-gate/record_test.go | 235 ++++++++---------- .../cmd/mesh-delivery-gate/recordevents.go | 155 +----------- .../cmd/mesh-delivery-gate/recordtools.go | 7 +- .../cmd/mesh-delivery-gate/sourcestart.go | 44 ++-- .../cmd/mesh-delivery-gate/sourcetools.go | 30 ++- .../mesh-delivery-gate/sourcetools_test.go | 49 +--- .../cmd/mesh-delivery-gate/takenin.go | 80 +++--- .../mesh-delivery-gate/takenin_bus_test.go | 46 ++-- .../cmd/mesh-delivery-gate/takenin_test.go | 45 +--- .../cmd/mesh-delivery-gate/verbs.go | 67 ++--- modules/mesh-delivery-gate/module.json | 41 +-- 19 files changed, 685 insertions(+), 684 deletions(-) create mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled.go create mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled_test.go diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go index 2559e6c2c..62bd2b339 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go @@ -10,6 +10,7 @@ package main // (ADR 0363 §2), and an event a node publishes names that node in its subject and in its x-node header. import ( + "bytes" "encoding/json" "fmt" ownmanifest "mesh-delivery-gate" @@ -17,6 +18,7 @@ import ( "sync" "time" + meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-indexer/takein" stdio "git.novox.be/novox/mesh-sdk/go" ) @@ -90,28 +92,19 @@ func stepPatterns() []string { return out } -// decodeTakenIn reads a `take-in.taken`: each merge taken in is an outcome of the record that merged it, unless the wait -// was dropped by hand, which took nothing in. -func decodeTakenIn(body json.RawMessage, at time.Time) ([]StepOutcome, error) { - t, err := takein.DecodeTakeIn(body) - if err != nil { +// decodeTakenIn reads a take-in.taken in the seat mesh-catalog's form, refusing a field it does not know, and answers no +// outcome: the take-in is written into its record by its key (takenin.go, H-A-29). +func decodeTakenIn(body json.RawMessage, _ time.Time) ([]StepOutcome, error) { + var t meshcatalog.TakeInTaken + dec := json.NewDecoder(bytes.NewReader(body)) + dec.DisallowUnknownFields() + if err := dec.Decode(&t); err != nil { return nil, err } - if t.Kind != takein.TakeInTaken { - return nil, fmt.Errorf("a take-in.taken of kind %q", t.Kind) + if t.Branch == "" || t.Source < 1 { + return nil, fmt.Errorf("a take-in.taken names no branch or source revision") } - outcome, why := OutcomeTakenIn, t.Why - if t.Dropped { - outcome, why = OutcomeHeld, "its take-in was dropped: "+t.Why - } - var commits []string - for _, m := range t.Merges { - commits = append(commits, m.Merge) - } - if len(commits) == 0 { - return nil, nil - } - return []StepOutcome{{Commits: commits, Step: "take-in", Outcome: outcome, Why: why, At: at}}, nil + return nil, nil } // StepConsumer applies the step events to the records. @@ -120,7 +113,7 @@ type StepConsumer struct { Families []stepFamily Now func() time.Time Logf func(format string, a ...any) - // Taken opens the record of a take-in.taken and moves its branch's source (takenin.go), before its outcomes are applied. + // Taken writes a take-in.taken into the record of its source revision (takenin.go). Taken func(body json.RawMessage) error mu sync.Mutex diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go index 3d8193cdc..2a6b3c3c7 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go @@ -32,7 +32,7 @@ func (l *listening) get() string { l.mu.Lock(); defer l.mu.Unlock(); return l.n // The events this owner follows: the forge bridge's word on an act owed, and the step events the delivery records are built // from (consumer.go). -var followed = append(append([]string{owedEndedEvent}, stepPatterns()...), registrationChangedEvent) +var followed = append(append([]string{owedEndedEvent, mergeCalledEvent}, stepPatterns()...), registrationChangedEvent) func main() { settingsFile := os.Getenv("MESH_DELIVERY_GATE_SETTINGS") @@ -97,7 +97,6 @@ func main() { // tick, no forge poll, no reconcile loop. Each step is retried, each failure said, never given up on quietly. func run(h *Holder, l *listening) { <-stdio.Served() // Serve first: the state is reached through it, and an event is heard from as soon as it serves - statePins(h, l) go h.Work() for _, pattern := range followed { pattern := pattern @@ -117,20 +116,6 @@ func run(h *Holder, l *listening) { go startSourcesAtStart(h) } -// statePins says every pin the records hold once at each start (design 47 G §579), retrying until it can. -func statePins(h *Holder, l *listening) { - for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) { - n, err := h.Records.StatePins() - if err == nil { - logf("[mesh-delivery] pins stated: %d", n) - return - } - l.set("not yet: " + err.Error()) - logf("[mesh-delivery] the pins cannot be stated yet (%v); asking again in %s", err, wait) - time.Sleep(wait) - } -} - // handle takes one event, then asks for a look at every step the event makes possible (Work, React). An event that cannot be read is // said and acknowledged: read again it would fail again. func handle(h *Holder, e stdio.Envelope) error { @@ -143,6 +128,9 @@ func take(h *Holder, e stdio.Envelope) error { if h.Consumer != nil && h.Consumer.Takes(e.Key) { return h.Consumer.Hear(e) } + if stdio.TopicMatches(mergeCalledEvent, e.Key) { + return h.MergeCalled(e) + } if e.Key == owedEndedEvent { return h.OwedEnded(e.Body) } @@ -220,15 +208,6 @@ func tools(h *Holder, l *listening) []stdio.Tool { Input: map[string]any{"type": "object", "properties": map[string]any{"id": str("a delivery's id, or a group's")}, "required": []string{"id"}}, Run: func(a map[string]any) (any, error) { return h.Show(strArg(a, "id")) }}, - {Name: seat + "groups", - Description: "Every delivery group, from the delivery records: its members, its state (open or ended), why, and the acts the " + - "gate said it owes the forge for it, each with its id and its recorded outcome (design 47 *What the gate owes the " + - "forge*). With a group, that group alone, ended or not, and no groups for a group it does not know. What reached the " + - "machines is the delivery view's.", - Input: map[string]any{"type": "object", "properties": map[string]any{ - "all": map[string]any{"type": "string", "enum": []string{"true", "false"}, "description": "\"true\": the groups that ended too"}, - "group": str("one group's id: that group alone, ended or not")}}, - Run: func(a map[string]any) (any, error) { return h.Groups(boolArg(a, "all"), strArg(a, "group")), nil }}, {Name: seat + "source", Description: "The source revision the gate holds: every repository it names, its trunk branch and that trunk's head, the " + "group that moved it last and its revision. Read, never written: a node-engine reads the repositories here to call a " + diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go index b9df64585..11296cdeb 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go @@ -95,8 +95,9 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { t.Fatal("the manifest declares a seat; mesh-delivery is the mesh's own") } // The verbs the seat keeps: times, what-if, release, stop and retire-history went with the plans (ADR 0539 §17), and - // settle and checks with the merge states (design 47 G §431); recovery is G's (§81, A-23). - if want := []string{"deliveries", "show", "groups", "source", "recovery", "stalled"}; !reflect.DeepEqual(m.Claims[0].Serves, want) { + // settle and checks with the merge states (design 47 G §431); recovery is G's (§81, A-23); groups with the group model + // (H §764, H-A-28). + if want := []string{"deliveries", "show", "source", "recovery", "stalled"}; !reflect.DeepEqual(m.Claims[0].Serves, want) { t.Fatalf("serves %v, wanted %v", m.Claims[0].Serves, want) } // The forge messages' layout is the bridge's published package forgemsg, taken by its series (ADR 0634 §3, design 32). @@ -152,7 +153,7 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { if !strings.Contains(raw, `"finding-written.*"`) || !strings.Contains(raw, `"finding-cleared.*"`) || !strings.Contains(raw, `"findings-stated.*"`) { t.Fatal("the finding events are not declared") } - if !reflect.DeepEqual(m.Emits, []string{"transition"}) { // `group` and `merged` go with the merge states (§431) + if len(m.Emits) != 0 { // `group` and `merged` go with the merge states (§431), `transition` with H (H-A-48) t.Fatalf("emits %v", m.Emits) } // Two buckets still in the old `state` (merges, source), `pulls` and `asks` gone (§613), the four of the template `state` moved to `requires` (design 47 G §613), and no `asked` (the teller asks, ADR 0437 §6): ADR 0450 §3 and the conditions it raised (issue 1275). No @@ -290,7 +291,7 @@ func TestTheGateHoldsTheSeatAsTheSDKsTableOfRelease6HasIt(t *testing.T) { served[verb] = true } } - for _, removed := range []string{"settle", "checks", "table", "recheck", "close"} { // design 47 G §431 + for _, removed := range []string{"settle", "checks", "table", "recheck", "close", "groups"} { // design 47 G §431, H §764 if served[removed] { t.Errorf("the gate serves %s, which G removed with the merge states", removed) } @@ -298,7 +299,7 @@ func TestTheGateHoldsTheSeatAsTheSDKsTableOfRelease6HasIt(t *testing.T) { t.Errorf("the manifest serves %s, which G removed with the merge states", removed) } } - for _, verb := range []string{"deliveries", "show", "groups", "stalled", "recovery"} { + for _, verb := range []string{"deliveries", "show", "stalled", "recovery"} { if !served[verb] { t.Errorf("the gate does not serve %s, which the seat keeps", verb) } @@ -363,7 +364,7 @@ func TestTheGroupsFromBranchNamesAreGone(t *testing.T) { } h := &Holder{Now: func() time.Time { return time.Date(2026, 11, 20, 10, 0, 0, 0, time.UTC) }, Logf: func(string, ...any) {}} h.Records = &Records{Ledger: newLedgerFake(t), Now: h.Now} - if _, _, err := h.Records.Open("main.7", map[string]string{"novox/app#3": strings.Repeat("a", 40)}, false); err != nil { + if _, _, err := h.Records.Open(opening("main", 7, map[string]string{"novox/app#3": strings.Repeat("a", 40)})); err != nil { t.Fatal(err) } d, err := h.Deliveries("", "", "", true) diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled.go new file mode 100644 index 000000000..dcb2695b2 --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled.go @@ -0,0 +1,193 @@ +package main + +// The merge call names the merged heads one source revision per watched branch (design 47 H §737; ADR 0682 §1, §2; +// H-A-13, H-A-19, H-A-33): the gate consumes the seat git's merge-called on a durable consumer of its own, writes +// source. once per call by compare-and-set keyed by the call, opens that revision's delivery record and says +// record-opened and source-moved. The gate numbers every source revision; no other part keeps a number of its own. + +import ( + "crypto/rand" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "regexp" + "strconv" + "strings" + + gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" + stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/keys" +) + +// mergeCalledEvent is the seat git's merge-called as the gate's manifest consumes it (`git.merge-called.*`). +var mergeCalledEvent = consumedFamily(gitseat.EventMergeCalled) + +// consumedFamily is the pattern the manifest consumes an event by whose subject has parts after its name +// (`..*`), or the event itself; empty when the manifest consumes none. +func consumedFamily(event string) string { + for _, c := range consumed { + if _, name, ok := strings.Cut(c, "."); ok && (name == event || strings.HasPrefix(name, event+".")) { + return c + } + } + return "" +} + +// callRe is a merge call: 16 lowercase hex characters, one subject token (H-A-24). +var callRe = regexp.MustCompile(`^[0-9a-f]{16}$`) + +// revisionHead is one repository a source revision moves, at its commit. +type revisionHead struct { + Repository string + Commit string +} + +// nameRevision writes source. once for a call (§737, H-A-33): the branch's previous heads with each head's +// repository at its commit, numbered one above the branch's newest, carrying the call. A newest revision already carrying +// the call writes nothing and is answered as it is, so a call heard again numbers nothing twice (H-A-13). Wrote says it +// was written now. +func (h *Holder) nameRevision(branch, call string, heads []revisionHead) (named Source, wrote bool, err error) { + if h.Source == nil { + return Source{}, false, errors.New("this gate keeps no source") + } + _, err = h.Source.Set(branch, func(prev *Source) (Source, bool) { + if prev != nil && prev.Call == call { + named, wrote = *prev, false + return Source{}, false + } + next := Source{Call: call, Source: 1, Heads: map[string]string{}} + if prev != nil { + next.Source = prev.Source + 1 + for r, c := range prev.Heads { + next.Heads[r] = c + } + } + for _, hd := range heads { + next.Heads[hd.Repository] = hd.Commit + } + named, wrote = next, true + return next, true + }) + if err != nil { + return Source{}, false, fmt.Errorf("source.%s cannot be written for the call %s: %w", branch, call, err) + } + return named, wrote, nil +} + +// saySourceMoved says source-moved on source-moved.. (§737) under the revision's id, so said again for a +// call heard again it is kept once. +func (h *Holder) saySourceMoved(branch string, s Source, start bool, heads []revisionHead) error { + if h.SaySeat == nil { + return nil + } + escaped, number := keys.EscapeName(branch), strconv.FormatInt(s.Source, 10) + e := seat.SourceMovedEvent{V: 1, Branch: branch, Source: s.Source, Call: s.Call, Start: start, + Heads: make([]seat.SourceMovedEventHeadsItem, 0, len(heads))} + for _, hd := range heads { + e.Heads = append(e.Heads, seat.SourceMovedEventHeadsItem{Repository: hd.Repository, Commit: hd.Commit}) + } + return h.SaySeat(seat.SubjectSourceMoved(escaped, number), e, "source-moved."+escaped+"."+number) +} + +// branchOfCall is one watched branch a merge call's members name: its heads in call order, each repository once at its +// last member's commit (H-A-19), and its members by id `@` (H-A-48). +type branchOfCall struct { + Branch string + Heads []revisionHead + Members map[string]string + Pulls []string + Commits []string +} + +// branchesOf groups a merge call's landed members by their base, in the order the call first names each branch. +func branchesOf(m gitseat.MergeCalled) ([]*branchOfCall, error) { + var out []*branchOfCall + by := map[string]*branchOfCall{} + for _, mb := range m.Members { + if mb.Branch == "" || mb.Repository == "" || mb.Commit == "" || mb.Number < 1 { + return nil, fmt.Errorf("a member names no branch, repository, number or commit (%+v)", mb) + } + b := by[mb.Branch] + if b == nil { + b = &branchOfCall{Branch: mb.Branch, Members: map[string]string{}} + by[mb.Branch] = b + out = append(out, b) + } + id := strings.ToLower(mb.Repository) + "@" + strconv.FormatInt(mb.Number, 10) + b.Members[id] = mb.Commit + b.Pulls = append(b.Pulls, id) + b.Commits = append(b.Commits, mb.Commit) + replaced := false + for i := range b.Heads { + if b.Heads[i].Repository == mb.Repository { + b.Heads[i].Commit, replaced = mb.Commit, true + } + } + if !replaced { + b.Heads = append(b.Heads, revisionHead{Repository: mb.Repository, Commit: mb.Commit}) + } + } + return out, nil +} + +// MergeCalled takes one merge-called: for each watched branch its members name, one source revision, one delivery record and +// one source-moved (§737). It answers an error only when a write failed, so the event is offered again; a body that cannot be +// read is said and taken, since offered again it would be refused again. A call that landed no member names nothing. +func (h *Holder) MergeCalled(e stdio.Envelope) error { + var m gitseat.MergeCalled + if err := json.Unmarshal(e.Body, &m); err != nil { + h.Refuse(fmt.Errorf("a merge-called (%s) is not readable, so it names no source revision: %w", e.Key, err)) + return nil + } + if !callRe.MatchString(m.Call) { + h.Refuse(fmt.Errorf("a merge-called (%s) names the call %q, not 16 lowercase hex characters, so it names no source revision", e.Key, m.Call)) + return nil + } + branches, err := branchesOf(m) + if err != nil { + h.Refuse(fmt.Errorf("the merge call %s names no source revision: %w", m.Call, err)) + return nil + } + var errs []error + for _, b := range branches { + trace := RecordTrace{Call: m.Call, By: m.By, Account: m.Account, At: m.At.UTC(), Pulls: b.Pulls, Commits: b.Commits} + if err := h.deliverRevision(b.Branch, m.Call, b.Heads, b.Members, trace); err != nil { + errs = append(errs, err) + } + } + return errors.Join(errs...) +} + +// deliverRevision names a branch's heads one source revision for a call, opens its record and says source-moved: the one +// path a merge call and a hand take (§742) move a source by. +func (h *Holder) deliverRevision(branch, call string, heads []revisionHead, members map[string]string, trace RecordTrace) error { + named, wrote, err := h.nameRevision(branch, call, heads) + if err != nil { + return err + } + if h.Records != nil { + trace.Source = strconv.FormatInt(named.Source, 10) + if _, _, err := h.Records.Open(RecordOpening{Branch: branch, Source: named.Source, Call: call, Heads: members, Trace: trace}); err != nil { + return fmt.Errorf("the record of %s at %d cannot be opened: %w", branch, named.Source, err) + } + } + if err := h.saySourceMoved(branch, named, false, heads); err != nil { + return fmt.Errorf("source.%s is at %d and its source-moved is not said: %w", branch, named.Source, err) + } + if wrote { + h.logf("[mesh-delivery] source.%s is at %d for the call %s", branch, named.Source, call) + } + return nil +} + +// newCall is a call made by the gate itself, for a start or a seed (H-A-24: 16 lowercase hex characters; who and when +// live in the hand-act log and the trace). +func newCall() (string, error) { + var b [8]byte + if _, err := rand.Read(b[:]); err != nil { + return "", fmt.Errorf("no call can be made: %w", err) + } + return hex.EncodeToString(b[:]), nil +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled_test.go new file mode 100644 index 000000000..cfec0a652 --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled_test.go @@ -0,0 +1,127 @@ +package main + +import ( + "encoding/json" + "reflect" + "strconv" + "strings" + "testing" + "time" + + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// GATE-4 and GATE-5 (design 47 H §737; H-A-13, H-A-19, H-A-25, H-A-33): a merge-called names its landed heads one source +// revision per watched branch, written once per call, numbered one above the branch's newest, opens that revision's record +// keyed `.` and says record-opened on record-opened. and source-moved on source-moved... +func TestAMergeCallNamesOneSourceRevisionAndOneRecordPerBranch(t *testing.T) { + at := time.Date(2026, 11, 20, 3, 0, 0, 0, time.UTC) + c1, c2, c3 := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("3", 40) + type said struct { + subject, id string + body any + } + setup := func(t *testing.T) (*Holder, *memSource, *[]said) { + src := &memSource{vals: map[string]Source{"main": {Source: 9, Call: "00000000000000aa", Heads: map[string]string{"novox/old": "c0"}}}} + var events []said + h := &Holder{Now: func() time.Time { return at }, Source: src} + h.init() + h.SaySeat = func(subject string, body any, id string) error { + events = append(events, said{subject, id, body}) + return nil + } + h.Records = &Records{Ledger: newLedgerFake(t), Now: h.Now, Say: h.SaySeat} + return h, src, &events + } + called := func(call, members string) stdio.Envelope { + return stdio.Envelope{Key: "git.merge-called." + call, Body: json.RawMessage(`{"call":"` + call + + `","by":"lead@shanks","account":"acct-s","at":"2026-11-20T02:59:00Z","members":[` + members + `]}`)} + } + member := func(repository string, number int, branch, commit string) string { + return `{"repository":"` + repository + `","number":` + strconv.Itoa(number) + + `,"branch":"` + branch + `","head":"h","commit":"` + commit + `"}` + } + + t.Run("two members on one branch: one revision, one record, one source-moved; heard twice, one", func(t *testing.T) { + h, src, events := setup(t) + e := called("0123456789abcdef", member("novox/app", 3, "main", c1)+","+member("novox/lib", 4, "main", c2)) + for range 2 { + if err := h.MergeCalled(e); err != nil { + t.Fatal(err) + } + } + got := src.vals["main"] + if got.Source != 10 || got.Call != "0123456789abcdef" || + !reflect.DeepEqual(got.Heads, map[string]string{"novox/old": "c0", "novox/app": c1, "novox/lib": c2}) || src.rev != 1 { + t.Fatalf("source.main %+v (%d writes)", got, src.rev) + } + kept, _ := h.Records.Kept() + if len(kept) != 1 || kept[0].Milestone != recordMilestone("main", 10) || kept[0].Call != "0123456789abcdef" || + !reflect.DeepEqual(kept[0].Heads, map[string]string{"novox/app@3": c1, "novox/lib@4": c2}) || + kept[0].Trace == nil || kept[0].Trace.By != "lead@shanks" || kept[0].Trace.Account != "acct-s" { + t.Fatalf("records %+v", kept) + } + var moved []seat.SourceMovedEvent + ids := map[string]bool{} + for _, s := range *events { + ids[s.id] = true + if m, ok := s.body.(seat.SourceMovedEvent); ok { + if s.subject != seat.SubjectSourceMoved("main", "10") { + t.Fatalf("source-moved on %s", s.subject) + } + moved = append(moved, m) + } else if s.subject != seat.SubjectRecordOpened("0123456789abcdef") { + t.Fatalf("said %s", s.subject) + } + } + want := seat.SourceMovedEvent{V: 1, Branch: "main", Source: 10, Call: "0123456789abcdef", + Heads: []seat.SourceMovedEventHeadsItem{{Repository: "novox/app", Commit: c1}, {Repository: "novox/lib", Commit: c2}}} + if len(moved) != 2 || !reflect.DeepEqual(moved[0], want) || len(ids) != 2 { + t.Fatalf("source-moved %+v under %v: heard twice it is said under the one id", moved, ids) + } + }) + + t.Run("two members of one repository name the last member's commit in call order", func(t *testing.T) { + h, src, _ := setup(t) + if err := h.MergeCalled(called("1111111111111111", member("novox/app", 3, "main", c1)+","+member("novox/app", 5, "main", c3))); err != nil { + t.Fatal(err) + } + if got := src.vals["main"].Heads["novox/app"]; got != c3 { + t.Fatalf("novox/app at %s", got) + } + }) + + t.Run("members on two branches are two revisions and two records, each naming the call", func(t *testing.T) { + h, src, events := setup(t) + if err := h.MergeCalled(called("2222222222222222", member("novox/app", 3, "main", c1)+","+member("novox/app", 6, "feat/x", c2))); err != nil { + t.Fatal(err) + } + if src.vals["main"].Source != 10 || src.vals["feat/x"].Source != 1 || src.vals["feat/x"].Call != "2222222222222222" { + t.Fatalf("sources %+v", src.vals) + } + kept, _ := h.Records.Kept() + if len(kept) != 2 || kept[0].Call != kept[1].Call || len(*events) != 4 { + t.Fatalf("records %+v, %d events", kept, len(*events)) + } + }) + + t.Run("a call that cannot be read, or not 16 lowercase hex, names nothing and is taken", func(t *testing.T) { + h, src, events := setup(t) + for _, e := range []stdio.Envelope{called("NOTHEX", member("novox/app", 3, "main", c1)), + {Key: "git.merge-called.x", Body: json.RawMessage(`{`)}, called("3333333333333333", member("novox/app", 3, "", c1))} { + if err := h.MergeCalled(e); err != nil { + t.Fatal(err) + } + } + if src.rev != 0 || len(*events) != 0 { + t.Fatalf("wrote %d, said %d", src.rev, len(*events)) + } + }) + + t.Run("the manifest consumes merge-called and the gate follows it", func(t *testing.T) { + if mergeCalledEvent != "git.merge-called.*" || !stdio.TopicMatches(mergeCalledEvent, "git.merge-called.0123456789abcdef") { + t.Fatalf("merge-called followed as %q", mergeCalledEvent) + } + }) +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go index a90ab75e8..265960ee5 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go @@ -113,7 +113,7 @@ func (kvSource) Branches() ([]string, error) { } // SourceRevision is the seat's verb source (§690): given a branch, every repository that branch's source key names, its -// trunk branch and head, the group that moved it last and its revision, or why it cannot be read; given none, `branches`, +// trunk branch and head, the merge call that moved it last (H-A-25) and its source revision, or why it cannot be read; given none, `branches`, // the branch of each source key the gate keeps. The trunk of each repository is the branch asked, the key's own (§428, §507). Outside the lock. func (h *Holder) SourceRevision(branch string) seat.SourceAnswer { if h.Source == nil { @@ -129,14 +129,14 @@ func (h *Holder) SourceRevision(branch string) seat.SourceAnswer { } return seat.SourceAnswer{Branches: branches} } - held, revision, err := h.Source.Read(branch) + held, _, err := h.Source.Read(branch) if err != nil { return seat.SourceAnswer{Error: err.Error()} } if held == nil { return seat.SourceAnswer{Branch: branch} } - out := seat.SourceAnswer{Branch: branch, Group: held.Group, Revision: int64(revision)} + out := seat.SourceAnswer{Branch: branch, Call: held.Call, Revision: held.Source} for repository, head := range held.Heads { out.Repositories = append(out.Repositories, seat.SourceHead{Repository: repository, Trunk: branch, Head: head}) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/owedacts_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/owedacts_test.go index 7e8a04cb5..29bea18c0 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/owedacts_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/owedacts_test.go @@ -22,7 +22,7 @@ func TestARecordsChangesOweTheNoteTheViewAndWhenFailedOrHeldAComment(t *testing. f := &sayerFake{} h := &Holder{Now: func() time.Time { return at }, Owed: f, Records: rs} rs.Changed = h.recordChanged - r, _, err := rs.Open("47/G", map[string]string{"novox/app@7": head}, false) + r, _, err := rs.Open(opening("main", 47, map[string]string{"novox/app@7": head})) if err != nil { t.Fatal(err) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go index 8df97b3c4..b5d481263 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go @@ -44,15 +44,13 @@ const ( // The operations of a record, each one effect (ADR 0597 §2), and of the month's list. The manifest's `requires` declares // exactly these (record_test.go). const ( - OpOpen = "open" // set-fields: the milestone, the heads, when, forge-made, the form + OpOpen = "open" // set-fields: the milestone, its branch, source revision and merge call, the heads, when, the form OpGroup = "group" // set-fields: the group's state OpTrace = "trace" // set-fields: the trace OpEntry = "entry" // set-entry into entries: one entry of the graph difference, by its module, fingerprint and node OpOutcome = "outcome" // set-entry into outcomes, by the entry, the node and the step OpAway = "away" // set-entry into away, by node OpBack = "back" // remove from away, by node - OpPin = "pin" // set-entry into pinned, by revision - OpFree = "free" // remove from pinned, by revision OpEnd = "end" // set-fields: ended OpAct = "act" // set-entry into acts, by id: an act owed the forge with its outcome OpHold = "hold" // a month's list: set-entry into records, by instance @@ -60,7 +58,7 @@ const ( ) // RecordOperations are a record's operations, in the order the manifest's type allows them. -var RecordOperations = []string{OpOpen, OpGroup, OpTrace, OpEntry, OpOutcome, OpAway, OpBack, OpPin, OpFree, OpAct, OpEnd} +var RecordOperations = []string{OpOpen, OpGroup, OpTrace, OpEntry, OpOutcome, OpAway, OpBack, OpAct, OpEnd} // HeldOperations are the month's list's operations. var HeldOperations = []string{OpHold, OpDrop} @@ -76,14 +74,17 @@ type Record struct { // Sequence is the last operation the state holds; not in the state. Sequence uint64 `json:"-"` + // Milestone is the record's key, `.`, each escaped as a name (design 47 H §737, A-33). Milestone string `json:"milestone"` - // Heads are the heads the record is for: each member's id (owner/repository@commit) to its full commit. + // Branch, Source and Call are the watched branch, its source revision and the merge call that named it (§737). + Branch string `json:"branch,omitempty"` + Source int64 `json:"source,omitempty"` + Call string `json:"call,omitempty"` + // Heads are the members the record is for: each member's id (`@` for a merge call's member, + // `@` for a hand take, H-A-48) to its full commit. Heads map[string]string `json:"heads"` Opened time.Time `json:"opened"` - // ForgeMade is a record opened for a merge the forge made with no record's merge-pull (design 47 *What the gate owes - // the forge*): it has no Approve. - ForgeMade bool `json:"forge-made,omitempty"` - Form int `json:"form,omitempty"` + Form int `json:"form,omitempty"` Group *RecordGroup `json:"group,omitempty"` Trace *RecordTrace `json:"trace,omitempty"` @@ -93,8 +94,6 @@ type Record struct { Outcomes map[string]RecordOutcome `json:"outcomes,omitempty"` // Away are the nodes away, by node, each with since when. Away map[string]NodeAway `json:"away,omitempty"` - // Pinned are the graph revisions the record points at, by revision. - Pinned map[string]PinnedRevision `json:"pinned,omitempty"` // Acts are the acts owed the forge for the record's pull requests, by id, with their outcomes (owedended.go). Acts map[string]RecordAct `json:"acts,omitempty"` Ended *RecordEnded `json:"ended,omitempty"` @@ -142,6 +141,13 @@ type RecordTrace struct { Source string `json:"source,omitempty"` From string `json:"from,omitempty"` To string `json:"to,omitempty"` + // Call is the merge call, By the caller as who@node and Account the bus account the tool runner proved for it + // (design 47 H §737); Hand marks a head taken in by hand (§742), At when (H-A-24: hand, by and the time live here). + Call string `json:"call,omitempty"` + By string `json:"by,omitempty"` + Account string `json:"account,omitempty"` + Hand bool `json:"hand,omitempty"` + At time.Time `json:"at,omitzero"` } // The outcomes an entry has on a node (ADR 0656 §7, design 47 *The delivery record*). @@ -196,14 +202,6 @@ type NodeAway struct { Since time.Time `json:"since"` } -// PinnedRevision is a graph revision the record points at: its half (catalog or assignment), its revision and, for a catalog -// revision, the watched branch it is numbered on. -type PinnedRevision struct { - Revision string `json:"revision"` - Half string `json:"half"` - Branch string `json:"branch,omitempty"` -} - // RecordEnded is how a record ended: when, why, by whom, and the nodes away it ended without. type RecordEnded struct { At time.Time `json:"at"` @@ -244,34 +242,13 @@ func milestoneToken(milestone string) string { return b.String() + "_" + hex.EncodeToString(sum[:4]) } -// HeadsDigest is the digest of a set of heads: each member's id and full commit, sorted, sixteen hex characters. -func HeadsDigest(heads map[string]string) string { - ids := make([]string, 0, len(heads)) - for id := range heads { - ids = append(ids, id) - } - sort.Strings(ids) - h := sha256.New() - for _, id := range ids { - fmt.Fprintf(h, "%s=%s\n", strings.ToLower(id), strings.ToLower(heads[id])) - } - return hex.EncodeToString(h.Sum(nil))[:16] -} - -// RecordInstance is the instance of the record of a milestone at its heads: `record//`. -func RecordInstance(milestone string, heads map[string]string) (string, error) { +// RecordInstance is the instance of the record of a source revision, by its key `.` alone: +// `record/` (design 47 H §737; H-A-29: a record is read by its key, never by reading every record). +func RecordInstance(milestone string) (string, error) { if strings.TrimSpace(milestone) == "" { return "", errors.New("a delivery record names its milestone") } - if len(heads) == 0 { - return "", fmt.Errorf("the delivery record of %s names no heads", milestone) - } - for id, sha := range heads { - if id == "" || sha == "" { - return "", fmt.Errorf("the delivery record of %s names a head with no id or commit (%q=%q)", milestone, id, sha) - } - } - inst := TypeRecord + "/" + milestoneToken(milestone) + "/" + HeadsDigest(heads) + inst := TypeRecord + "/" + milestoneToken(milestone) for _, p := range strings.Split(inst, "/") { if !segmentRe.MatchString(p) { return "", fmt.Errorf("the delivery record of %s cannot be named: %q", milestone, inst) @@ -480,21 +457,43 @@ func (rs *Records) Read(instance string) (*Record, error) { return &r, nil } -// Open opens the record of a milestone at its heads, unless one is there already, which is left as it is (the switch -// and a delivery of milestone G again after a revert make no second record and ask nothing again). Opened says it was -// opened now. Each open removes the records past their retention first (the retention needs no timer: records open as -// deliveries do). -func (rs *Records) Open(milestone string, heads map[string]string, forgeMade bool) (r *Record, opened bool, err error) { - return rs.open(milestone, heads, forgeMade) +// RecordOpening is what a record opens with: the source revision it is the record of, the merge call that named it, its +// members and its trace (design 47 H §737). +type RecordOpening struct { + Branch string + Source int64 + Call string + // Heads are the members, each id to its full commit. + Heads map[string]string + Trace RecordTrace } -func (rs *Records) open(milestone string, heads map[string]string, forgeMade bool) (r *Record, opened bool, err error) { - inst, err := RecordInstance(milestone, heads) +// Open opens the record of a source revision, unless one is there already, which is left as it is (a merge call heard +// again opens no second record and says nothing new). Opened says it was opened now. Each open removes the records past +// their retention first (the retention needs no timer: records open as deliveries do). +func (rs *Records) Open(o RecordOpening) (r *Record, opened bool, err error) { + if o.Branch == "" || o.Source < 1 || o.Call == "" { + return nil, false, fmt.Errorf("a delivery record names its branch, its source revision and its merge call (%q, %d, %q)", o.Branch, o.Source, o.Call) + } + if len(o.Heads) == 0 { + return nil, false, fmt.Errorf("the delivery record of %s at %d names no member", o.Branch, o.Source) + } + for id, sha := range o.Heads { + if id == "" || sha == "" { + return nil, false, fmt.Errorf("the delivery record of %s at %d names a member with no id or commit (%q=%q)", o.Branch, o.Source, id, sha) + } + } + milestone := recordMilestone(o.Branch, o.Source) + inst, err := RecordInstance(milestone) if err != nil { return nil, false, err } - if r, err = rs.Read(inst); err != nil || r != nil { - return r, false, err + if r, err = rs.Read(inst); err != nil { + return nil, false, err + } + if r != nil { + rs.sayOpened(r) + return r, false, nil } now := rs.now() if err := rs.Sweep(now); err != nil { @@ -504,10 +503,8 @@ func (rs *Records) open(milestone string, heads map[string]string, forgeMade boo if err != nil { return nil, false, err } - body := map[string]any{"milestone": milestone, "heads": heads, "opened": now, "form": RecordForm} - if forgeMade { - body["forge-made"] = true - } + body := map[string]any{"milestone": milestone, "branch": o.Branch, "source": o.Source, "call": o.Call, "heads": o.Heads, + "opened": now, "form": RecordForm} if _, err := rs.Ledger.Apply(inst, OpOpen, body); err != nil { return nil, false, err } @@ -517,18 +514,34 @@ func (rs *Records) open(milestone string, heads map[string]string, forgeMade boo if read { rs.sayFilling(inst, before) } - r, err = rs.Read(inst) - if err == nil && r != nil { - rs.keep(r) - // Said once, under the record's id: the watchman times it against 24 hours, ended by record-ended. A say that fails is - // said again by the next write of the record (the id keeps it once). - at := now.UTC() - if serr := rs.say(meshdelivery.SubjectRecordOpened, meshdelivery.RecordOpenedEvent{Record: inst, Milestone: milestone, ForgeMade: forgeMade, At: &at}, - "record-opened."+inst); serr != nil && rs.Logf != nil { - rs.Logf("[mesh-delivery] the record %s is opened and its opening is not said: %v", inst, serr) - } + if r, err = rs.Read(inst); err != nil || r == nil { + return r, false, err + } + if err := rs.SetTrace(r, o.Trace); err != nil { + return r, true, fmt.Errorf("the record %s is opened and its trace is not written: %w", inst, err) + } + rs.keep(r) + rs.sayOpened(r) + return r, true, nil +} + +// ByKey is the record of a source revision, read by its key `.` (H-A-29); nil when none is kept. +func (rs *Records) ByKey(branch string, source int64) (*Record, error) { + inst, err := RecordInstance(recordMilestone(branch, source)) + if err != nil { + return nil, err + } + return rs.Read(inst) +} + +// sayOpened says record-opened on record-opened., so the session that called waits on its own call (§737). Said under +// the record's id, so a merge call heard again says it once: a say that fails is said again when the call is heard again. +func (rs *Records) sayOpened(r *Record) { + at := r.Opened.UTC() + if err := rs.say(meshdelivery.SubjectRecordOpened(r.Call), meshdelivery.RecordOpenedEvent{Record: r.Instance, + Milestone: r.Milestone, Branch: r.Branch, Source: r.Source, Call: r.Call, At: &at}, "record-opened."+r.Instance); err != nil && rs.Logf != nil { + rs.Logf("[mesh-delivery] the record %s is opened and its opening is not said: %v", r.Instance, err) } - return r, err == nil && r != nil, err } // List is every record kept, opened within the retention and a month more, oldest first. @@ -581,9 +594,7 @@ func (rs *Records) heldIn(month string) ([]heldEntry, error) { return out, nil } -// Sweep removes every record opened longer ago than the retention, and says `freed` for each revision it pinned -// (design 47: "When the retention removes a record, the gate says freed for each revision it pinned"). Nothing else is -// removed to make room. +// Sweep removes every record opened longer ago than the retention. Nothing else is removed to make room. func (rs *Records) Sweep(now time.Time) error { cut := now.Add(-rs.retention()) for _, m := range rs.months(now) { @@ -600,9 +611,6 @@ func (rs *Records) Sweep(now time.Time) error { return err } if r != nil { - if err := rs.sayFreedAll(r, "the record is past its retention"); err != nil { - return err - } if err := rs.Ledger.Remove(e.Instance); err != nil { return err } @@ -616,15 +624,6 @@ func (rs *Records) Sweep(now time.Time) error { return nil } -func sortedPins(r *Record) []PinnedRevision { - out := make([]PinnedRevision, 0, len(r.Pinned)) - for _, p := range r.Pinned { - out = append(out, p) - } - sort.Slice(out, func(i, j int) bool { return out[i].Revision < out[j].Revision }) - return out -} - // SetGroup writes the record's group state, unless it holds that state already. func (rs *Records) SetGroup(r *Record, g RecordGroup) error { if r.Group != nil && reflect.DeepEqual(*r.Group, g) { diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go index 2b51ba7de..204dd6c22 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go @@ -6,7 +6,6 @@ import ( "fmt" "os" "path/filepath" - "reflect" "slices" "strings" "testing" @@ -159,50 +158,70 @@ func (f *ledgerFake) Get(instance string) (json.RawMessage, uint64, error) { return s, f.seq[instance], nil } -// W11.1 (ADR 0656 §7, design 47 *The delivery record*): one record per milestone's delivery, a ledger instance keyed by its -// milestone and its heads, kept for its retention, its operations those the manifest's ledger types allow. -func TestTheDeliveryRecordIsALedgerInstanceKeyedByMilestoneAndHeads(t *testing.T) { - heads := map[string]string{"novox/app@111111111111": strings.Repeat("1", 40), "novox/lib@222222222222": strings.Repeat("2", 40)} - same := map[string]string{"novox/lib@222222222222": strings.Repeat("2", 40), "novox/app@111111111111": strings.Repeat("1", 40)} - other := map[string]string{"novox/app@111111111111": strings.Repeat("3", 40), "novox/lib@222222222222": strings.Repeat("2", 40)} +// W11.1 (ADR 0656 §7, design 47 *The delivery record*; H §737, H-A-29): one record per source revision, a ledger +// instance keyed by `.` alone, kept for its retention, its operations those the manifest's ledger types allow. +func TestTheDeliveryRecordIsALedgerInstanceKeyedByItsSourceRevision(t *testing.T) { + heads := map[string]string{"novox/app@3": strings.Repeat("1", 40), "novox/lib@4": strings.Repeat("2", 40)} + same := map[string]string{"novox/lib@4": strings.Repeat("2", 40), "novox/app@3": strings.Repeat("1", 40)} at := time.Date(2026, 11, 9, 20, 0, 0, 0, time.UTC) - t.Run("an instance per milestone and heads, a path the tool runner takes", func(t *testing.T) { - a, err := RecordInstance("47/G", heads) + t.Run("an instance per source revision, a path the tool runner takes", func(t *testing.T) { + a, err := RecordInstance(recordMilestone("feat/x", 7)) if err != nil { t.Fatal(err) } - b, _ := RecordInstance("47/G", same) - c, _ := RecordInstance("47/G", other) - d, _ := RecordInstance("47_G", heads) - if a != b || a == c || a == d { - t.Fatalf("instances %s %s %s %s: one per milestone and heads, whatever the heads' order", a, b, c, d) + b, _ := RecordInstance(recordMilestone("feat/x", 8)) + c, _ := RecordInstance(recordMilestone("feat_x", 7)) + if a == b || a == c { + t.Fatalf("instances %s %s %s: one per branch and source revision", a, b, c) } for _, p := range strings.Split(a, "/") { if !segmentRe.MatchString(p) { t.Fatalf("%s has a part the tool runner refuses", a) } } - if _, err := RecordInstance("", heads); err == nil { + if _, err := RecordInstance(""); err == nil { t.Fatal("a record with no milestone is named") } - if _, err := RecordInstance("47/G", nil); err == nil { - t.Fatal("a record with no heads is named") + rs := &Records{Ledger: newLedgerFake(t), Now: func() time.Time { return at }} + for _, o := range []RecordOpening{{Branch: "main", Source: 1, Call: "0000000000000001"}, {Branch: "main", Call: "0000000000000001", Heads: heads}, + {Branch: "main", Source: 1, Heads: heads}, {Source: 1, Call: "0000000000000001", Heads: heads}} { + if _, _, err := rs.Open(o); err == nil { + t.Fatalf("a record is opened with %+v", o) + } } }) - t.Run("opened once; a second open leaves it as it is", func(t *testing.T) { + t.Run("opened once, said on record-opened.; a second open leaves it as it is and says the same", func(t *testing.T) { l := newLedgerFake(t) now := at - rs := &Records{Ledger: l, Now: func() time.Time { return now }} - r, opened, err := rs.Open("47/G", heads, false) - if err != nil || !opened || r == nil || r.Milestone != "47/G" || len(r.Heads) != 2 || !r.Opened.Equal(at) || r.Form != RecordForm { + type said struct { + subject, id string + body meshdelivery.RecordOpenedEvent + } + var got []said + rs := &Records{Ledger: l, Now: func() time.Time { return now }, Say: func(subject string, body any, id string) error { + got = append(got, said{subject, id, body.(meshdelivery.RecordOpenedEvent)}) + return nil + }} + o := opening("main", 47, heads) + o.Trace = RecordTrace{Call: o.Call, By: "lead@shanks", Account: "acct-s"} + r, opened, err := rs.Open(o) + if err != nil || !opened || r == nil || r.Milestone != recordMilestone("main", 47) || r.Branch != "main" || r.Source != 47 || + r.Call != o.Call || len(r.Heads) != 2 || !r.Opened.Equal(at) || r.Form != RecordForm || r.Trace == nil || r.Trace.By != "lead@shanks" { t.Fatalf("open: %+v %v %v", r, opened, err) } now = at.Add(time.Hour) - again, opened, err := rs.Open("47/G", same, false) + again, opened, err := rs.Open(opening("main", 47, same)) if err != nil || opened || again.Instance != r.Instance || !again.Opened.Equal(at) { - t.Fatalf("a second open of the same heads: %+v %v %v", again, opened, err) + t.Fatalf("a second open of the same revision: %+v %v %v", again, opened, err) + } + if len(got) != 2 || got[0].id != got[1].id || *got[0].body.At != *got[1].body.At || got[0].subject != meshdelivery.SubjectRecordOpened(o.Call) || + got[0].body.Branch != "main" || got[0].body.Source != 47 || got[0].body.Call != o.Call || got[0].body.Record != r.Instance { + t.Fatalf("said %+v", got) + } + if byKey, err := rs.ByKey("main", 47); err != nil || byKey == nil || byKey.Instance != r.Instance { + t.Fatalf("read by its key: %+v %v", byKey, err) } list, err := rs.List() if err != nil || len(list) != 1 || list[0].Instance != r.Instance { @@ -213,7 +232,7 @@ func TestTheDeliveryRecordIsALedgerInstanceKeyedByMilestoneAndHeads(t *testing.T t.Run("an outcome applied twice changes nothing", func(t *testing.T) { l := newLedgerFake(t) rs := &Records{Ledger: l, Now: func() time.Time { return at }} - r, _, _ := rs.Open("47/G", heads, false) + r, _, _ := rs.Open(opening("main", 47, heads)) o := RecordOutcome{Module: "mesh-delivery-gate", Fingerprint: "src2:abcd", Node: "ace", Step: "build", Outcome: OutcomeBuilt, At: at, Event: "e1"} if applied, err := rs.Outcome(r, o); err != nil || !applied { t.Fatalf("first: %v %v", applied, err) @@ -245,112 +264,29 @@ func TestTheDeliveryRecordIsALedgerInstanceKeyedByMilestoneAndHeads(t *testing.T } }) - t.Run("a record past its retention is removed at the next open and frees what it pinned", func(t *testing.T) { + t.Run("a record past its retention is removed at the next open", func(t *testing.T) { l := newLedgerFake(t) now := at - var freed []string - rs := &Records{Ledger: l, Now: func() time.Time { return now }, Retention: func() time.Duration { return 90 * 24 * time.Hour }, - Say: func(subject string, body any, id string) error { - if subject == meshdelivery.SubjectFreed { - p := body.(meshdelivery.Pins) - freed = append(freed, fmt.Sprint(p.Milestone, " ", p.Catalog, " ", p.Why)) - } - return nil - }} - old, _, _ := rs.Open("46/F", heads, false) - if err := rs.Pin(old, HalfCatalog, "main", 1); err != nil { - t.Fatal(err) - } + rs := &Records{Ledger: l, Now: func() time.Time { return now }, Retention: func() time.Duration { return 90 * 24 * time.Hour }} + old, _, _ := rs.Open(opening("main", 46, heads)) now = at.Add(89 * 24 * time.Hour) - kept, _, _ := rs.Open("47/G", heads, false) - if r, _ := rs.Read(old.Instance); r == nil || len(freed) != 0 { - t.Fatalf("a record within its retention is removed (%v)", freed) + kept, _, _ := rs.Open(opening("main", 47, heads)) + if r, _ := rs.Read(old.Instance); r == nil { + t.Fatal("a record within its retention is removed") } now = at.Add(91 * 24 * time.Hour) - if _, _, err := rs.Open("47/H", heads, false); err != nil { + if _, _, err := rs.Open(opening("main", 48, heads)); err != nil { t.Fatal(err) } if r, _ := rs.Read(old.Instance); r != nil { t.Fatal("a record past its retention is kept") } - if !slices.Equal(freed, []string{"46/F [{main 1}] the record is past its retention"}) { - t.Fatalf("freed %v", freed) - } list, _ := rs.List() if len(list) != 2 || list[0].Instance != kept.Instance { t.Fatalf("list %+v", list) } }) - // W11.9: pinned and freed, said under an id of what they say; only a catalog or an assignment revision is pinned. - t.Run("a pin is written once and said as pinned; a free says freed with why", func(t *testing.T) { - l := newLedgerFake(t) - type said struct { - subject, id string - pins meshdelivery.Pins - } - var got []said - rs := &Records{Ledger: l, Now: func() time.Time { return at }, Say: func(subject string, body any, id string) error { - if subject == meshdelivery.SubjectRecordOpened { - return nil - } - got = append(got, said{subject, id, body.(meshdelivery.Pins)}) - return nil - }} - r, _, _ := rs.Open("47/G", heads, false) - for range 2 { - if err := rs.Pin(r, HalfAssignment, "", 9); err != nil { - t.Fatal(err) - } - } - if err := rs.Pin(r, HalfCatalog, "main", 9); err != nil { - t.Fatal(err) - } - // Revisions are numbered per watched branch: the same number on another branch is another pin. - if err := rs.Pin(r, HalfCatalog, "feat/x", 9); err != nil { - t.Fatal(err) - } - pins := 0 - for _, op := range l.history { - if strings.HasSuffix(op, " "+OpPin) { - pins++ - } - } - back, _ := rs.Read(r.Instance) - if pins != 3 || len(back.Pinned) != 3 { - t.Fatalf("%d pins written, the record holds %+v", pins, back.Pinned) - } - if len(got) != 4 || !reflect.DeepEqual(got[0], got[1]) || got[0].subject != meshdelivery.SubjectPinned || - !slices.Equal(got[0].pins.Assignment, []int64{9}) || !slices.Equal(got[2].pins.Catalog, []meshdelivery.PinsCatalogItem{{Branch: "main", Revision: 9}}) || - !slices.Equal(got[3].pins.Catalog, []meshdelivery.PinsCatalogItem{{Branch: "feat/x", Revision: 9}}) || got[0].pins.Record != r.Instance { - t.Fatalf("said %+v", got) - } - got = nil - if err := rs.Free(back, HalfAssignment, "", 9, "removed by hand"); err != nil { - t.Fatal(err) - } - if err := rs.Free(back, HalfAssignment, "", 9, "again"); err != nil { - t.Fatal(err) - } - if len(got) != 1 || got[0].subject != meshdelivery.SubjectFreed || got[0].pins.Why != "removed by hand" || - !slices.Equal(got[0].pins.Assignment, []int64{9}) { - t.Fatalf("said %+v", got) - } - got = nil - if err := rs.Free(back, HalfCatalog, "feat/x", 9, "removed"); err != nil { - t.Fatal(err) - } - if len(got) != 1 || !slices.Equal(got[0].pins.Catalog, []meshdelivery.PinsCatalogItem{{Branch: "feat/x", Revision: 9}}) { - t.Fatalf("said %+v", got) - } - if again, _ := rs.Read(r.Instance); len(again.Pinned) != 1 { - t.Fatalf("pinned after the free %+v", again.Pinned) - } - if err := rs.Pin(r, "trunk", "", 1); err == nil { - t.Fatal("a pin of no half is taken") - } - }) - t.Run("the setting record-retention, in days", func(t *testing.T) { dir := t.TempDir() for _, c := range []struct { @@ -410,7 +346,7 @@ func TestTheRecordsOutcomesArriveThroughTheGatesConsumer(t *testing.T) { setup := func(t *testing.T) (*ledgerFake, *Records, *Record, *StepConsumer) { l := newLedgerFake(t) rs := &Records{Ledger: l, Now: func() time.Time { return at }} - r, _, err := rs.Open("47/G", map[string]string{"novox/app@111111111111": strings.Repeat("1", 40)}, false) + r, _, err := rs.Open(opening("main", 47, map[string]string{"novox/app@111111111111": strings.Repeat("1", 40)})) if err != nil { t.Fatal(err) } @@ -429,15 +365,20 @@ func TestTheRecordsOutcomesArriveThroughTheGatesConsumer(t *testing.T) { } return []StepOutcome{{Module: b.Module, Fingerprint: b.Fingerprint, Step: StepRunning, Outcome: OutcomeRunning, At: at}}, nil }} - c := &StepConsumer{Records: rs, Families: append(slices.Clone(stepFamilies), running), Now: func() time.Time { return at }} + h := &Holder{Records: rs, Now: func() time.Time { return at }} + h.init() + c := &StepConsumer{Records: rs, Families: append(slices.Clone(stepFamilies), running), Now: func() time.Time { return at }, Taken: h.TakenIn} return l, rs, r, c } - taken := func(id string) stdio.Envelope { - body := `{"v":1,"kind":"taken","key":"m.00000000000000000001","what":"revision","merges":[{"repository":"novox/app","number":7,"merge":"` + - merge + `"}],"since":"2026-11-09T20:00:00Z"}` + takenOf := func(id, body string) stdio.Envelope { return stdio.Envelope{Key: "mesh-delivery-indexer.take-in.taken", Body: json.RawMessage(body), Headers: map[string]string{"x-event-id": id}} } - t.Run("a take-in of the record's merge is its outcome, once", func(t *testing.T) { + taken := func(id string) stdio.Envelope { + return takenOf(id, `{"branch":"main","source":47,"revision":47,"call":"000000000000002f","entries":[{"module":"mesh-y",`+ + `"commit":"`+merge+`","fingerprint":"src2:f2","repository":"novox/app"}]}`) + } + // H §738, H-A-29: the take-in's movers are its record's entries, read by the key .; none ends it. + t.Run("a take-in's movers are its record's entries, taken in, once; a take-in with no mover ends it", func(t *testing.T) { l, rs, r, c := setup(t) if err := c.Hear(taken("e1")); err != nil { t.Fatal(err) @@ -447,12 +388,25 @@ func TestTheRecordsOutcomesArriveThroughTheGatesConsumer(t *testing.T) { t.Fatalf("the same event again: %v, %d operations (was %d)", err, len(l.history), n) } back, _ := rs.Read(r.Instance) - if got := back.Outcomes[OutcomeKey("", "", "", "take-in")]; got.Outcome != OutcomeTakenIn || got.Event != "e1" { - t.Fatalf("outcomes %+v", back.Outcomes) + if _, ok := back.Entries[EntryKey("mesh-y", "src2:f2", "")]; !ok { + t.Fatalf("entries %+v", back.Entries) + } + if got := back.Outcomes[OutcomeKey("mesh-y", "src2:f2", "", "take-in")]; got.Outcome != OutcomeTakenIn || back.Ended != nil { + t.Fatalf("outcomes %+v, ended %+v", back.Outcomes, back.Ended) } if c.Heard()["mesh-delivery-indexer.take-in.taken"].Applied != 2 { t.Fatalf("heard %+v", c.Heard()) } + other, _, _ := rs.Open(opening("main", 48, map[string]string{"novox/app@8": merge})) + if err := c.Hear(takenOf("e4", `{"branch":"main","source":48,"revision":48,"call":"0000000000000030"}`)); err != nil { + t.Fatal(err) + } + if back, _ := rs.Read(other.Instance); back.Ended == nil || back.Ended.Why != WhyNothingToDeliver { + t.Fatalf("a take-in with no mover leaves its record %+v", back.Ended) + } + if back, _ := rs.Read(r.Instance); back.Ended != nil { + t.Fatal("another revision's take-in ended this record") + } }) t.Run("a node's event from its own node is the entry's outcome; from another node it is refused", func(t *testing.T) { _, rs, r, c := setup(t) @@ -479,10 +433,10 @@ func TestTheRecordsOutcomesArriveThroughTheGatesConsumer(t *testing.T) { // away that wakes adds its outcomes and is no longer shown away. t.Run("it ends once every present node runs its entries, and a node away that wakes adds its outcomes", func(t *testing.T) { _, rs, r, c := setup(t) - var ended []recordEnded + var ended []meshdelivery.RecordEndedEvent rs.Say = func(subject string, body any, id string) error { if subject == meshdelivery.SubjectRecordEnded && id == "record-ended."+r.Instance { - ended = append(ended, body.(recordEnded)) + ended = append(ended, body.(meshdelivery.RecordEndedEvent)) } return nil } @@ -518,7 +472,7 @@ func TestTheRecordsOutcomesArriveThroughTheGatesConsumer(t *testing.T) { !back.Ended.Away[0].Since.Equal(at.Add(-time.Hour)) { t.Fatalf("ended %+v", back.Ended) } - if len(ended) != 1 || ended[0].Milestone != "47/G" || len(ended[0].Away) != 1 || ended[0].Away[0].Node != "g14" { + if len(ended) != 1 || ended[0].Milestone != recordMilestone("main", 47) || ended[0].Branch != "main" || ended[0].Source != 47 || ended[0].Call != r.Call || len(ended[0].Away) != 1 || ended[0].Away[0].Node != "g14" { t.Fatalf("record-ended said %+v", ended) } if err := c.Hear(running("g14")); err != nil { @@ -542,7 +496,7 @@ func TestTheRecordsOutcomesArriveThroughTheGatesConsumer(t *testing.T) { if h := c.Heard()["mesh-delivery-indexer.take-in.taken"]; h.Refused != 1 { t.Fatalf("heard %+v", h) } - l.failOn = OpOutcome + l.failOn = OpEntry if err := c.Hear(taken("e3")); err == nil { t.Fatal("an outcome the ledger did not take is taken") } @@ -581,7 +535,7 @@ func TestTheRecordsToolsReadThemAndEndOneByHand(t *testing.T) { var said []string rs := &Records{Ledger: newLedgerFake(t), Now: func() time.Time { return at }, Say: func(subject string, body any, id string) error { said = append(said, subject+" "+id); return nil }} - r, _, err := rs.Open("47/G", heads, false) + r, _, err := rs.Open(opening("main", 47, heads)) if err != nil { t.Fatal(err) } @@ -596,18 +550,18 @@ func TestTheRecordsToolsReadThemAndEndOneByHand(t *testing.T) { t.Run("gate_record and gate_records read a record whole and its fill", func(t *testing.T) { rs, r, said := setup(t) // W9.3: the record's opening is said once, under its id, for the watchman to time against 24 hours. - if !slices.Contains(*said, meshdelivery.SubjectRecordOpened+" record-opened."+r.Instance) { + if !slices.Contains(*said, meshdelivery.SubjectRecordOpened(r.Call)+" record-opened."+r.Instance) { t.Fatalf("the record's opening is not said: %v", *said) } got, err := rs.RecordOf(r.Instance, "") - if err != nil || len(got) != 1 || got[0].Record.Milestone != "47/G" || got[0].Waits != "waiting for mesh-x on ace" || + if err != nil || len(got) != 1 || got[0].Record.Milestone != recordMilestone("main", 47) || got[0].Waits != "waiting for mesh-x on ace" || got[0].Bytes == 0 || got[0].Of != 256<<10 { t.Fatalf("%+v %v", got, err) } - if byMilestone, err := rs.RecordOf("", "47/G"); err != nil || len(byMilestone) != 1 { + if byMilestone, err := rs.RecordOf("", recordMilestone("main", 47)); err != nil || len(byMilestone) != 1 { t.Fatalf("by milestone %+v %v", byMilestone, err) } - if _, err := rs.RecordOf("", "46/F"); err == nil { + if _, err := rs.RecordOf("", recordMilestone("main", 46)); err == nil { t.Fatal("a milestone with no record answers nothing") } lines, err := rs.Lines() @@ -725,7 +679,7 @@ func TestTheRecordsToolsReadThemAndEndOneByHand(t *testing.T) { return nil }} commit := strings.Repeat("2", 40) - r, _, err := rs.Open(recordMilestone("feat/x", 4), map[string]string{"novox/app@222222222222": commit}, false) + r, _, err := rs.Open(opening("feat/x", 4, map[string]string{"novox/app@222222222222": commit})) if err != nil { t.Fatal(err) } @@ -789,7 +743,7 @@ func TestTheRecordsLedgerSaysFillingFullAndRoom(t *testing.T) { } saying := func(out *[]said) SeatSay { return func(subject string, body any, id string) error { - if subject == meshdelivery.SubjectRecordOpened { + if strings.HasPrefix(subject, meshdelivery.SubjectRecordOpened("")) { return nil // the opening of a record, not a fill } f, _ := body.(meshdelivery.Fill) @@ -803,7 +757,7 @@ func TestTheRecordsLedgerSaysFillingFullAndRoom(t *testing.T) { var events []said rs := &Records{Ledger: l, Now: func() time.Time { return at }, Say: saying(&events)} l.fill = filled(capBytes * 7 / 10) - r, opened, err := rs.Open("47/G", heads, false) + r, opened, err := rs.Open(opening("main", 47, heads)) if err != nil || !opened { t.Fatalf("open below the mark: %v %v", opened, err) } @@ -819,16 +773,16 @@ func TestTheRecordsLedgerSaysFillingFullAndRoom(t *testing.T) { } // Already past the mark: a next record is opened and says nothing again. events = nil - if _, opened, err := rs.Open("47/H", heads, false); err != nil || !opened || len(events) != 0 { + if _, opened, err := rs.Open(opening("main", 48, heads)); err != nil || !opened || len(events) != 0 { t.Fatalf("a record past the mark: %v %v, said %+v", opened, err, events) } // No room for one more record: refused, said as full naming what it refuses, nothing opened. l.fill = filled(capBytes - RecordRoom/2) n := len(l.history) - if _, opened, err := rs.Open("47/I", heads, false); !errors.Is(err, ErrRecordsFull) || opened || len(l.history) != n { + if _, opened, err := rs.Open(opening("main", 49, heads)); !errors.Is(err, ErrRecordsFull) || opened || len(l.history) != n { t.Fatalf("a full ledger opened %v (%v), %d operations", opened, err, len(l.history)-n) } - if len(events) != 1 || events[0].subject != meshdelivery.SubjectFull || !strings.Contains(events[0].body.Refused, "47/I") { + if len(events) != 1 || events[0].subject != meshdelivery.SubjectFull || !strings.Contains(events[0].body.Refused, recordMilestone("main", 49)) { t.Fatalf("full said as %+v", events) } if line, _ := rs.Lines(); !strings.HasPrefix(fmt.Sprint(line["fill"].(map[string]any)["state"]), "full") { @@ -836,7 +790,7 @@ func TestTheRecordsLedgerSaysFillingFullAndRoom(t *testing.T) { } // A ledger that says no fill (a runner before the call): the record is opened, and the read tool says why. l.fill = nil - if _, opened, err := rs.Open("47/J", heads, false); err != nil || !opened { + if _, opened, err := rs.Open(opening("main", 50, heads)); err != nil || !opened { t.Fatalf("an unread fill refused the record: %v %v", opened, err) } if line, _ := rs.Lines(); line["fill"].(map[string]any)["unread"] == nil { @@ -860,3 +814,8 @@ func (o *ownersFake) Running(node, module string) (string, string, error) { o.asked = append(o.asked, "running "+node+" "+module) return o.fp, o.phase, nil } + +// opening is a record's opening for a test: the source revision of a branch, its call made of its number. +func opening(branch string, source int64, heads map[string]string) RecordOpening { + return RecordOpening{Branch: branch, Source: source, Call: fmt.Sprintf("%016x", source), Heads: heads} +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordevents.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordevents.go index d5a34a740..5546b825f 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordevents.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordevents.go @@ -1,26 +1,17 @@ package main -// What a delivery record says on the seat `mesh-delivery` (design 47 *The delivery record*): `pinned` and `freed` for the -// graph revisions it starts and stops pointing at, `heads-replaced` for a candidate whose heads were replaced or whose pull -// request closed, and `record-ended` once every present node runs its entries. Only the gate's account publishes them +// What a delivery record says on the seat `mesh-delivery` (design 47 *The delivery record*): `record-ended` once every +// present node runs its entries; the pins are the indexer's (design 47 H §749, H-A-48). Only the gate's account publishes them // (ADR 0363 §2); each is said under an id of what it says, so said again it is kept once, and a say that failed is said // again at the next write of the record. import ( "fmt" "sort" - "strconv" "strings" "time" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - "git.novox.be/novox/mesh-sdk/go/keys" -) - -// The halves of the revision view a record pins (design 47 *Two graphs, one source of truth*). -const ( - HalfCatalog = "catalog" - HalfAssignment = "assignment" ) // StepRunning is the step whose outcome OutcomeRunning says an entry runs healthy on its node: from the node-engine seat's @@ -40,110 +31,6 @@ func (rs *Records) say(subject string, body any, id string) error { return rs.Say(subject, body, id) } -// pinKey is a pinned revision's key in the record: its half, for a catalog revision its watched branch (revisions are numbered -// per branch, A-171) escaped as a name with its slashes escaped too, and its number, so no two revisions' keys meet. -func pinKey(half, branch string, revision int64) string { - key := half + "-" - if half == HalfCatalog { - key += strings.ReplaceAll(keys.EscapeName(branch), "/", "=2f") + "-" - } - return key + strconv.FormatInt(revision, 10) -} - -// number is the revision's number on its half's seat: what follows the key's last dash. -func (p PinnedRevision) number() (int64, bool) { - i := strings.LastIndex(p.Revision, "-") - if i < 0 || !strings.HasPrefix(p.Revision, p.Half+"-") { - return 0, false - } - n, err := strconv.ParseInt(p.Revision[i+1:], 10, 64) - return n, err == nil -} - -// pinsOf is the seat's Pins of a record for these revisions. -func pinsOf(r *Record, pins []PinnedRevision, why string) meshdelivery.Pins { - out := meshdelivery.Pins{Record: r.Instance, Milestone: r.Milestone, Why: why} - for _, p := range pins { - n, ok := p.number() - if !ok { - continue - } - switch p.Half { - case HalfCatalog: - out.Catalog = append(out.Catalog, meshdelivery.PinsCatalogItem{Branch: p.Branch, Revision: n}) - case HalfAssignment: - out.Assignment = append(out.Assignment, n) - } - } - return out -} - -func checkHalf(half string) error { - if half != HalfCatalog && half != HalfAssignment { - return fmt.Errorf("a record pins a catalog or an assignment graph revision, not a %q one", half) - } - return nil -} - -// Pin writes that the record points at a graph revision, unless it does already, and says `pinned` (again, when written -// before: the id keeps it once). A catalog revision names its watched branch, which the record keeps and the event says. -func (rs *Records) Pin(r *Record, half, branch string, revision int64) error { - if err := checkHalf(half); err != nil { - return err - } - if half == HalfCatalog && branch == "" { - return fmt.Errorf("a catalog revision is pinned by its watched branch and its number") - } - if half != HalfCatalog { - branch = "" - } - p := PinnedRevision{Revision: pinKey(half, branch, revision), Half: half, Branch: branch} - if _, ok := r.Pinned[p.Revision]; !ok { - seq, err := rs.Ledger.Apply(r.Instance, OpPin, p) - if err != nil { - return err - } - if r.Pinned == nil { - r.Pinned = map[string]PinnedRevision{} - } - r.Pinned[p.Revision], r.Sequence = p, seq - } - return rs.say(meshdelivery.SubjectPinned, pinsOf(r, []PinnedRevision{p}, ""), "pinned."+r.Instance+"."+p.Revision) -} - -// Free writes that the record no longer points at a graph revision and says `freed` with why; a revision it does not -// point at frees nothing and says nothing. -func (rs *Records) Free(r *Record, half, branch string, revision int64, why string) error { - if err := checkHalf(half); err != nil { - return err - } - p, ok := r.Pinned[pinKey(half, branch, revision)] - if !ok { - return nil - } - if err := rs.say(meshdelivery.SubjectFreed, pinsOf(r, []PinnedRevision{p}, why), "freed."+r.Instance+"."+p.Revision); err != nil { - return err - } - seq, err := rs.Ledger.Apply(r.Instance, OpFree, map[string]any{"revision": p.Revision}) - if err != nil { - return err - } - delete(r.Pinned, p.Revision) - r.Sequence = seq - return nil -} - -// sayFreedAll says `freed` for every revision a record pins, before the record is removed: said first, so a removal never -// leaves a revision pinned by nobody that the indexer or the assigner still keeps. -func (rs *Records) sayFreedAll(r *Record, why string) error { - pins := sortedPins(r) - if len(pins) == 0 { - return nil - } - return rs.say(meshdelivery.SubjectFreed, pinsOf(r, pins, why), "freed."+r.Instance) -} - -// NodeAway writes a node away since when, unless the record holds it already, and ends the record when every other node // runs its entries: a node away holds nothing (ADR 0603 §7). func (rs *Records) NodeAway(r *Record, node string, since time.Time) error { if node == "" { @@ -257,41 +144,15 @@ func (rs *Records) end(r *Record, why, by string) error { return nil } -// recordEnded is the seat's `record-ended` body. -type recordEnded struct { - Record string `json:"record"` - Milestone string `json:"milestone"` - ForgeMade bool `json:"forge-made,omitempty"` - Away []NodeAway `json:"away,omitempty"` - At time.Time `json:"at"` -} - func (rs *Records) sayEnded(r *Record) error { if r.Ended == nil { return nil } - return rs.say(meshdelivery.SubjectRecordEnded, recordEnded{Record: r.Instance, Milestone: r.Milestone, - ForgeMade: r.ForgeMade, Away: r.Ended.Away, At: r.Ended.At}, "record-ended."+r.Instance) -} - -// StatePins says `pinned` again for every revision each record it keeps pins, then once `pins-stated` (design 47 G §579): the -// indexer's consumer of the seat's events hears pins said before it existed or older than the stream keeps, and the set form -// absorbs the repeats. Each repeat has an id of its own, since the bus keeps one message under one id inside its duplicate window. -func (rs *Records) StatePins() (int, error) { - kept, err := rs.Kept() - if err != nil { - return 0, err + at := r.Ended.At + e := meshdelivery.RecordEndedEvent{Record: r.Instance, Milestone: r.Milestone, Branch: r.Branch, Source: r.Source, Call: r.Call, At: &at} + for _, a := range r.Ended.Away { + since := a.Since + e.Away = append(e.Away, meshdelivery.RecordEndedEventAwayItem{Node: a.Node, Since: &since}) } - at := rs.Now().UTC() - stated := 0 - for _, r := range kept { - for _, p := range sortedPins(r) { - id := "pinned." + r.Instance + "." + p.Revision + ".stated." + strconv.FormatInt(at.UnixNano(), 10) - if err := rs.say(meshdelivery.SubjectPinned, pinsOf(r, []PinnedRevision{p}, ""), id); err != nil { - return stated, err - } - stated++ - } - } - return stated, rs.say(meshdelivery.SubjectPinsStated, meshdelivery.PinsStatedEvent{At: at}, "pins-stated."+strconv.FormatInt(at.UnixNano(), 10)) + return rs.say(meshdelivery.SubjectRecordEnded, e, "record-ended."+r.Instance) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordtools.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordtools.go index a0077a8a7..531f078ed 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordtools.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordtools.go @@ -76,11 +76,10 @@ type recordLine struct { Milestone string `json:"milestone"` Opened string `json:"opened"` Ended string `json:"ended,omitempty"` - ForgeMade bool `json:"forge-made,omitempty"` + Call string `json:"call,omitempty"` Entries int `json:"entries"` Outcomes int `json:"outcomes"` Away int `json:"away,omitempty"` - Pinned int `json:"pinned,omitempty"` Bytes int `json:"bytes"` Of int `json:"of-bytes"` Waits string `json:"waits,omitempty"` @@ -96,8 +95,8 @@ func (rs *Records) Lines() (map[string]any, error) { open := 0 for _, r := range list { v := viewOf(r) - l := recordLine{Instance: r.Instance, Milestone: r.Milestone, Opened: timeText(&r.Opened), ForgeMade: r.ForgeMade, - Entries: len(r.Entries), Outcomes: len(r.Outcomes), Away: len(r.Away), Pinned: len(r.Pinned), Bytes: v.Bytes, + l := recordLine{Instance: r.Instance, Milestone: r.Milestone, Opened: timeText(&r.Opened), Call: r.Call, + Entries: len(r.Entries), Outcomes: len(r.Outcomes), Away: len(r.Away), Bytes: v.Bytes, Of: v.Of, Waits: v.Waits} if r.Ended != nil { l.Ended = timeText(&r.Ended.At) + ": " + r.Ended.Why diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go index 56ce1c278..1535f950b 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcestart.go @@ -11,15 +11,12 @@ import ( "errors" "fmt" "sort" - "strconv" "strings" "time" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - "git.novox.be/novox/mesh-sdk/go/keys" ) // registrationChangedEvent is the seat mesh-catalog's event, as the gate's manifest consumes it. @@ -172,7 +169,7 @@ func (h *Holder) startSources(only string) (started, leftOut []string, err error for _, repository := range watched[branch] { if held != nil { if _, has := held.Heads[repository]; has { - continue // a head a key holds moves by a take-in alone (A-34) + continue // a head a key holds moves by a merge call alone (§737) } } a, err := h.Forge.Branch(repository, branch) @@ -192,24 +189,18 @@ func (h *Holder) startSources(only string) (started, leftOut []string, err error if len(heads) == 0 { continue } - wrote := false - rev, err := h.Source.Set(branch, func(prev *Source) (Source, bool) { - next := Source{Heads: map[string]string{}} - if prev != nil { - next.Group = prev.Group - for r, c := range prev.Heads { - next.Heads[r] = c - } + var moved []revisionHead + for _, repository := range watched[branch] { + if c, ok := heads[repository]; ok { + moved = append(moved, revisionHead{Repository: repository, Commit: c}) } - wrote = false - for r, c := range heads { - if _, has := next.Heads[r]; !has { // a held head is never changed - next.Heads[r] = c - wrote = true - } - } - return next, wrote - }) + } + call, err := newCall() + if err != nil { + errs = append(errs, err) + continue + } + named, wrote, err := h.nameRevision(branch, call, moved) if err != nil { errs = append(errs, fmt.Errorf("source.%s cannot be started: %w", branch, err)) continue @@ -218,13 +209,8 @@ func (h *Holder) startSources(only string) (started, leftOut []string, err error continue } started = append(started, branch) - if h.SaySeat == nil { - continue - } - at := h.Now().UTC() - if err := h.SaySeat(seat.SubjectSourceMoved(keys.EscapeName(branch)), - seat.SourceMovedEvent{Branch: branch, Revision: int64(rev), At: &at}, - "source-moved."+keys.EscapeName(branch)+"."+strconv.FormatUint(rev, 10)); err != nil { + // start is true only for a branch's first revision (§737, §768). + if err := h.saySourceMoved(branch, named, held == nil, moved); err != nil { errs = append(errs, err) } } @@ -255,7 +241,7 @@ func (h *Holder) SourceStart(branch, why, by string, acts handActs) (map[string] started, leftOut, err := h.startSources(branch) out := map[string]any{"branch": branch, "started": started, "left-out": leftOut, "by": by} if err == nil && len(started) == 0 && len(leftOut) == 0 { - out["note"] = "the key names every registered repository already: only a take-in moves a head" + out["note"] = "the key names every registered repository already: only a merge call moves a head" } return out, err } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go index 6d3f7586c..421a02441 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go @@ -7,6 +7,7 @@ package main import ( "errors" "fmt" + "sort" "strconv" "strings" @@ -39,7 +40,7 @@ func (h *Holder) SourceAgain(branch, why, by string, acts handActs) (map[string] if h.Source == nil || h.SaySeat == nil { return nil, errors.New("this gate keeps no source or says no events") } - held, rev, err := h.Source.Read(branch) + held, _, err := h.Source.Read(branch) if err != nil { return nil, err } @@ -54,13 +55,16 @@ func (h *Holder) SourceAgain(branch, why, by string, acts handActs) (map[string] return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was said: %w", err) } at := h.Now().UTC() - escaped := keys.EscapeName(branch) - id := "source-moved." + escaped + "." + strconv.FormatUint(rev, 10) + ".again." + strconv.FormatInt(at.UnixNano(), 10) - if err := h.SaySeat(seat.SubjectSourceMoved(escaped), - seat.SourceMovedEvent{Branch: branch, Group: held.Group, Revision: int64(rev), At: &at}, id); err != nil { + escaped, number := keys.EscapeName(branch), strconv.FormatInt(held.Source, 10) + id := "source-moved." + escaped + "." + number + ".again." + strconv.FormatInt(at.UnixNano(), 10) + e := seat.SourceMovedEvent{V: 1, Branch: branch, Source: held.Source, Call: held.Call, Heads: []seat.SourceMovedEventHeadsItem{}} + for _, r := range sortedKeys(held.Heads) { + e.Heads = append(e.Heads, seat.SourceMovedEventHeadsItem{Repository: r, Commit: held.Heads[r]}) + } + if err := h.SaySeat(seat.SubjectSourceMoved(escaped, number), e, id); err != nil { return nil, err } - return map[string]any{"said": "source-moved", "branch": branch, "revision": rev, "by": by}, nil + return map[string]any{"said": "source-moved", "branch": branch, "source": held.Source, "call": held.Call, "by": by}, nil } // sourceTools are the source's tools; acts is the hand-act log. @@ -68,7 +72,7 @@ func sourceTools(h *Holder, acts handActs) []stdio.Tool { return []stdio.Tool{ {Name: "gate_source", Description: "Every branch's source revision the gate keeps, each whole: its repositories with their trunk branch and head, " + - "the group that moved it last and the key's revision. Read, never written.", + "the merge call that moved it last and its source revision. Read, never written.", Run: func(map[string]any) (any, error) { return h.SourceAll(), nil }}, {Name: "gate_source_again", Description: "Say source-moved again for one branch, with the revision its source key holds now, for a part that missed it; " + @@ -81,7 +85,7 @@ func sourceTools(h *Holder, acts handActs) []stdio.Tool { }}, {Name: "gate_source_start", Description: "Start the source of one watched branch the registrations name and no source key holds yet, from the forge: each " + - "registered repository at the branch's head, the group empty, then source-moved; recorded in the hand-act log of the seat " + + "registered repository at the branch's head, one source revision with a call of its own, then source-moved; recorded in the hand-act log of the seat " + "mesh-hand-acts first. A key the gate holds is never rewritten.", Input: map[string]any{"type": "object", "properties": map[string]any{ "branch": str("the watched branch"), "why": str("why it is started by hand, in plain words")}, @@ -91,3 +95,13 @@ func sourceTools(h *Holder, acts handActs) []stdio.Tool { }}, } } + +// sortedKeys are a map's keys, sorted. +func sortedKeys(m map[string]string) []string { + out := make([]string, 0, len(m)) + for k := range m { + out = append(out, k) + } + sort.Strings(out) + return out +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go index 6968dee4e..48e9c668e 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go @@ -31,7 +31,7 @@ func TestTheSourceToolsReadEveryBranchAndSayItAgainAfterTheLog(t *testing.T) { src := &branchSource{} for _, b := range []string{"main", "feat"} { _, _ = src.Set(b, func(*Source) (Source, bool) { - return Source{Group: "g-" + b, Heads: map[string]string{"novox/app": "c1"}}, true + return Source{Source: 3, Call: "00000000000000c1", Heads: map[string]string{"novox/app": "c1"}}, true }) } type said struct{ subject, id string } @@ -61,44 +61,18 @@ func TestTheSourceToolsReadEveryBranchAndSayItAgainAfterTheLog(t *testing.T) { t.Fatal(err) } if len(acts.took) != 1 || acts.took[0].Cause != "gate_source_again" || len(events) != 1 || - !strings.HasSuffix(events[0].subject, ".event.source-moved.main") || !strings.Contains(events[0].id, ".again.") { + !strings.HasSuffix(events[0].subject, ".event.source-moved.main.3") || !strings.Contains(events[0].id, ".again.") { t.Fatalf("log %+v events %+v", acts.took, events) } } -// Design 47 G §579: at its start the gate says `pinned` again for every revision each record pins, each under an id of its own, -// then `pins-stated` once. -func TestTheGateStatesItsPinsThenSaysPinsStated(t *testing.T) { - at := time.Date(2026, 11, 12, 4, 0, 0, 0, time.UTC) - type said struct{ subject, id string } - var events []said - rs := &Records{Ledger: newLedgerFake(t), Now: func() time.Time { return at }, - Say: func(subject string, body any, id string) error { - events = append(events, said{subject, id}) - return nil - }} - r, _, err := rs.Open("47/G", map[string]string{"novox/app@1": "c1"}, false) - if err != nil { - t.Fatal(err) - } - if err := rs.Pin(r, HalfCatalog, "main", 3); err != nil { - t.Fatal(err) - } - events = nil - n, err := rs.StatePins() - if err != nil || n != 1 || len(events) != 2 || !strings.HasSuffix(events[0].subject, ".event.pinned") || - !strings.Contains(events[0].id, ".stated.") || !strings.HasSuffix(events[1].subject, ".event.pins-stated") { - t.Fatalf("%d %v %+v", n, err, events) - } -} - // Design 47 G §428, §507: no part's code holds a branch's name; the trunk of each repository a source revision names is the // branch whose key was read, and delivery_status says every branch's source, not one fixed branch's. func TestTheSourceAnswerNamesTheBranchItWasAskedAndStatusSaysEveryBranch(t *testing.T) { src := &branchSource{} for _, b := range []string{"release", "feat"} { _, _ = src.Set(b, func(*Source) (Source, bool) { - return Source{Group: "g-" + b, Heads: map[string]string{"novox/app": "c1"}}, true + return Source{Source: 3, Call: "00000000000000c1", Heads: map[string]string{"novox/app": "c1"}}, true }) } h := &Holder{Now: time.Now, Source: src} @@ -150,15 +124,15 @@ func TestAWatchedBranchStartsFromTheForge(t *testing.T) { heads: map[string]map[string]string{"main": {"o/car-hunter": "c1", "o/mesh": "c2", "o/photos": "c6"}, "master": {"o/f1-circuits": "c3"}, "nox-mesh": {"o/link2pay": "c4"}}}, {name: "a held key lacking repositories gets them, its heads unchanged", catalog: fakeCatalog{regs: regs}, - held: map[string]Source{"main": {Group: "g", Heads: map[string]string{"o/mesh": "old"}}}, + held: map[string]Source{"main": {Source: 4, Call: "00000000000000aa", Heads: map[string]string{"o/mesh": "old"}}}, started: []string{"main", "master", "nox-mesh"}, events: 3, heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "c1", "o/photos": "c6"}, "master": {"o/f1-circuits": "c3"}, "nox-mesh": {"o/link2pay": "c4"}}}, {name: "a held key lacking one repository", catalog: fakeCatalog{regs: regs[:2]}, - held: map[string]Source{"main": {Group: "g", Heads: map[string]string{"o/mesh": "old"}}}, + held: map[string]Source{"main": {Source: 4, Call: "00000000000000aa", Heads: map[string]string{"o/mesh": "old"}}}, started: []string{"main"}, events: 1, heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "c1"}}}, {name: "a held key naming all: nothing written or said", catalog: fakeCatalog{regs: regs[:2]}, - held: map[string]Source{"main": {Group: "g", Heads: map[string]string{"o/mesh": "old", "o/car-hunter": "older"}}}, + held: map[string]Source{"main": {Source: 4, Call: "00000000000000aa", Heads: map[string]string{"o/mesh": "old", "o/car-hunter": "older"}}}, events: 0, heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "older"}}}, {name: "by hand, one branch", catalog: fakeCatalog{regs: regs}, only: "master", @@ -177,8 +151,9 @@ func TestAWatchedBranchStartsFromTheForge(t *testing.T) { Logf: func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) }, SaySeat: func(subject string, body any, id string) error { events++ - if ev := body.(seat.SourceMovedEvent); ev.Group != "" { - t.Errorf("a source-moved for started heads names no group (A-404), got %q", ev.Group) + ev := body.(seat.SourceMovedEvent) + if held, had := c.held[ev.Branch]; ev.Start == had || ev.Source != held.Source+1 || len(ev.Call) != 16 || len(ev.Heads) == 0 { + t.Errorf("a start's source-moved: %+v (start only for a branch's first revision, numbered above its newest)", ev) } return nil }} @@ -198,8 +173,8 @@ func TestAWatchedBranchStartsFromTheForge(t *testing.T) { } } for b, h := range c.held { - if src.vals[b].Group != h.Group { - t.Errorf("source.%s keeps the group %q a take-in wrote, got %q", b, h.Group, src.vals[b].Group) + if c.events == 0 && !reflect.DeepEqual(src.vals[b], h) { + t.Errorf("source.%s naming every repository is rewritten: %+v", b, src.vals[b]) } } if c.only == "" && c.held == nil && !strings.Contains(strings.Join(logged, "\n"), "o/photos") { @@ -255,7 +230,7 @@ func TestRegistrationChangedAndTheStartPassStartABranch(t *testing.T) { if err := c.run(h); err != nil { t.Fatal(err) } - if src.vals["master"].Heads["o/f1"] != "c3" || len(subjects) != 1 || !strings.HasSuffix(subjects[0], ".event.source-moved.master") { + if src.vals["master"].Heads["o/f1"] != "c3" || len(subjects) != 1 || !strings.HasSuffix(subjects[0], ".event.source-moved.master.1") { t.Fatalf("source %+v events %v", src.vals, subjects) } }) diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go index 84a1327c7..b6060d9c6 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin.go @@ -3,75 +3,61 @@ package main import ( "encoding/json" "fmt" - "sort" "strconv" - "strings" - mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" "git.novox.be/novox/mesh-sdk/go/keys" ) -// What a take-in.taken does at the gate (design 47 G A-33; §151, §592): it opens one delivery record, keyed by the taken -// body's branch and source revision. It moves no source key: only a merge call does (design 47 H §737, §764). +// What a take-in.taken does at the gate (design 47 H §738, H-A-29): the record of its source revision, opened by the merge +// call (§737) and read by its key `.`, never by reading every record, gains the take-in's movers as its +// entries, each taken in; a take-in with no mover ends its record at once as nothing to deliver. It moves no source key: +// only a merge call does (§737, §764). -// recordMilestone is the key of a taken revision's record: `.`, each escaped as a name (A-33). +// recordMilestone is the key of a source revision's record: `.`, each escaped as a name (A-33). func recordMilestone(branch string, source int64) string { return keys.EscapeName(branch) + "." + keys.EscapeName(strconv.FormatInt(source, 10)) } -// TakenIn takes one take-in.taken body: the record of its source revision is opened (safe to repeat: a record there already -// is left as it is), its members are the entries' pull requests, its heads the merge commits the take-in names. A hand drop -// took nothing in, so it opens nothing. +// WhyNothingToDeliver is why a record whose take-in named no mover ends (§738). +const WhyNothingToDeliver = "nothing to deliver" + +// TakenIn takes one take-in.taken body. A revision with no record (a seed's or a start's, which open none, H-A-47) is +// taken with nothing written; safe to repeat: an entry or outcome the record holds is not written again. func (h *Holder) TakenIn(body json.RawMessage) error { var t meshcatalog.TakeInTaken if err := json.Unmarshal(body, &t); err != nil { return fmt.Errorf("a take-in.taken is not readable: %w", err) } - if t.Dropped || len(t.Merges) == 0 || t.Branch == "" { + if h.Records == nil || t.Branch == "" || t.Source < 1 { return nil } - if h.Records != nil { - heads := map[string]string{} - for _, m := range t.Merges { - heads[mdc.ApprovedKey(m.Repository, int(m.Number))] = m.Merge + r, err := h.Records.ByKey(t.Branch, t.Source) + if err != nil { + return fmt.Errorf("the record of %s at %d cannot be read: %w", t.Branch, t.Source, err) + } + if r == nil || r.Ended != nil { + return nil + } + at := h.Now().UTC() + for _, e := range t.Entries { + if e.Module == "" { + continue } - milestone := recordMilestone(t.Branch, t.Source) - r, _, err := h.Records.Open(milestone, heads, false) - if err != nil { - return fmt.Errorf("the record of %s cannot be opened: %w", milestone, err) + if err := h.Records.AddEntry(r, RecordEntry{Module: e.Module, Fingerprint: e.Fingerprint}); err != nil { + return err } - g := RecordGroup{Group: t.Group} - seen := map[string]bool{} - for _, e := range t.Entries { - // A-188: the take-in's entries, each with its module, are written into the record. - if e.Module != "" { - if err := h.Records.AddEntry(r, RecordEntry{Module: e.Module, Fingerprint: e.Fingerprint}); err != nil { - return err - } - } - if e.Repository == "" || seen[mdc.ApprovedKey(e.Repository, int(e.Pull))] { - continue - } - seen[mdc.ApprovedKey(e.Repository, int(e.Pull))] = true - g.Members = append(g.Members, mdc.ApprovedKey(e.Repository, int(e.Pull))) - } - sort.Strings(g.Members) - for _, m := range t.Merges { - p := RecordPull{Repository: m.Repository, Number: int(m.Number), Head: heads[mdc.ApprovedKey(m.Repository, int(m.Number))], - State: string(mdc.MergeMerged), Commit: m.Merge} - if r.Group != nil { - for _, o := range r.Group.Pulls { - if strings.EqualFold(o.Repository, m.Repository) && o.Number == int(m.Number) { - p.Base = o.Base - } - } - } - g.Pulls = append(g.Pulls, p) - } - if err := h.Records.SetGroup(r, g); err != nil { + if _, err := h.Records.Outcome(r, RecordOutcome{Module: e.Module, Fingerprint: e.Fingerprint, Step: "take-in", + Outcome: OutcomeTakenIn, Why: t.Why, At: at}); err != nil { return err } } + if len(t.Entries) == 0 { + why := WhyNothingToDeliver + if t.Skipped { + why += ": its take-in was skipped by hand: " + t.Why + } + return h.Records.End(r, why, t.By) + } return nil } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go index 35615b4e9..0edbfcc53 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go @@ -9,12 +9,13 @@ import ( "time" ) -// The gate whole, over a real bus (design 47 G A-33): a take-in.taken the indexer says opens a delivery record in the -// gate's ledger with the take-in as its outcome (it moves no source key, design 47 H §737); the gate asks the -// controller nothing, and no state it no longer declares is needed (the template states are buckets under their local names). +// The gate whole, over a real bus (design 47 H §737, §738; acceptance test 110's gate half): a merge-called the forge module +// says names one source revision, opens its delivery record and says source-moved; the indexer's take-in.taken of that +// revision writes its movers into the record; the gate asks the controller nothing, and no state it no longer declares is +// needed (the template states are buckets under their local names). const eventWithin = 20 * time.Second -func TestAGateWholeTakesAnIndexersTakeInIntoARecordOverARealBus(t *testing.T) { +func TestAGateWholeTakesAMergeCallIntoASourceRevisionAndARecordOverARealBus(t *testing.T) { b := newTestBus(t) ctx := context.Background() rt := b.start(t) @@ -23,22 +24,31 @@ func TestAGateWholeTakesAnIndexersTakeInIntoARecordOverARealBus(t *testing.T) { rt.deliverEvents(evctx) rt.untilListening() merge := strings.Repeat("c", 40) - b.publish(t, "mesh.mod.mesh-delivery-indexer.event.take-in.taken", map[string]any{"v": 1, "kind": "taken", "key": "m.00000000000000000001", - "what": "revision", "branch": "main", "source": 3, "since": "2026-11-11T19:00:00Z", - "merges": []any{map[string]any{"repository": "novox/app", "number": 5, "merge": merge}}, - "entries": []any{map[string]any{"module": "app", "commit": merge, "fingerprint": "f", "repository": "novox/app", "pull": 5}}}) - rt.until("the record of the take-in", eventWithin, func() bool { - b.mu.Lock() - defer b.mu.Unlock() - if b.ledger == nil { + call := "0123456789abcdef" + instance, _ := RecordInstance(recordMilestone("main", 1)) + member := map[string]any{"repository": "novox/app", "number": 5, "branch": "main", "head": strings.Repeat("d", 40), "commit": merge} + for range 2 { // heard twice: one revision + b.publish(t, "mesh.seat.git.event.merge-called."+call, map[string]any{"call": call, "by": "lead@shanks", "account": "acct-s", + "at": "2026-11-20T03:00:00Z", "members": []any{member}}) + } + rt.until("the record of the merge call and its source revision", eventWithin, func() bool { + e, err := b.kvs["source"].Get(ctx, "main") + if err != nil || !strings.Contains(string(e.Value()), `"call":"`+call+`"`) || !strings.Contains(string(e.Value()), `"source":1`) { return false } - for instance := range b.ledger.states { - if strings.Contains(instance, "main") { - return true - } - } - return false + b.mu.Lock() + defer b.mu.Unlock() + return b.ledger != nil && b.ledger.states[instance] != nil }) + b.publish(t, "mesh.mod.mesh-delivery-indexer.event.take-in.taken", map[string]any{"branch": "main", "source": 1, "revision": 1, + "call": call, "entries": []any{map[string]any{"module": "app", "commit": merge, "fingerprint": "f", "repository": "novox/app"}}}) + rt.until("the take-in's mover in the record", eventWithin, func() bool { + b.mu.Lock() + defer b.mu.Unlock() + return strings.Contains(string(b.ledger.states[instance]), `"module":"app"`) + }) + if e, _ := b.kvs["source"].Get(ctx, "main"); e.Revision() != 1 { + t.Fatalf("the merge call heard twice wrote source.main %d times", e.Revision()) + } rt.noControllerAsk() } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go index abf988058..b145844b9 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go @@ -7,7 +7,6 @@ import ( "time" stdio "git.novox.be/novox/mesh-sdk/go" - "git.novox.be/novox/mesh-sdk/go/keys" ) // memSource is the gate's source bucket in memory: one value and revision per branch key. @@ -41,11 +40,9 @@ func (m *memSource) Read(branch string) (*Source, uint64, error) { } func (m *memSource) Branches() ([]string, error) { return nil, nil } -// Design 47 G A-33: a take-in.taken opens one record keyed `.`, whose members are the entries' pull -// requests and whose heads are the merge commits it names; heard twice it opens no second record; a hand drop opens nothing. -// GATE-3/AC3 (design 47 H §737, §764): neither it nor the forge bridge's merged message moves a source key or says -// source-moved: only a merge call does. -func TestATakeInTakenOpensOneRecordAndMovesNoSource(t *testing.T) { +// GATE-3/AC3 (design 47 H §737, §764): neither a take-in.taken nor the forge bridge's merged message moves a source key, +// says source-moved or opens a record: only a merge call does (mergecalled_test.go). +func TestATakeInTakenOpensNoRecordAndMovesNoSource(t *testing.T) { now := time.Date(2026, 11, 11, 20, 0, 0, 0, time.UTC) type said struct{ subject, id string } var events []said @@ -57,30 +54,14 @@ func TestATakeInTakenOpensOneRecordAndMovesNoSource(t *testing.T) { }} h.Records = &Records{Ledger: newLedgerFake(t), Now: h.Now} merge := strings.Repeat("a", 40) - body := json.RawMessage(`{"v":1,"kind":"taken","key":"g.feat","what":"group","group":"feat","branch":"main","source":7,` + - `"merges":[{"repository":"novox/app","number":3,"merge":"` + merge + `"}],` + - `"entries":[{"module":"app","commit":"` + merge + `","fingerprint":"f","repository":"novox/app","pull":3}],"since":"2026-11-11T19:00:00Z"}`) + body := json.RawMessage(`{"branch":"main","source":7,"revision":7,"call":"0000000000000007",` + + `"entries":[{"module":"app","commit":"` + merge + `","fingerprint":"f","repository":"novox/app"}]}`) c := &StepConsumer{Records: h.Records, Families: stepFamilies, Now: h.Now, Taken: h.TakenIn} - for i := 0; i < 2; i++ { - if err := c.Hear(stdio.Envelope{Key: "mesh-delivery-indexer.take-in.taken", Body: body}); err != nil { - t.Fatal(err) - } + if err := c.Hear(stdio.Envelope{Key: "mesh-delivery-indexer.take-in.taken", Body: body}); err != nil { + t.Fatal(err) } - kept, err := h.Records.Kept() - if err != nil || len(kept) != 1 { - t.Fatalf("records %v %v: one per take-in", kept, err) - } - r := kept[0] - if r.Milestone != keys.EscapeName("main")+".7" || r.Heads["novox/app@3"] != merge || r.Group == nil || - len(r.Group.Members) != 1 || r.Group.Members[0] != "novox/app@3" || r.Group.Group != "feat" { - t.Fatalf("the record %+v group %+v", r, r.Group) - } - // A-188: the take-in's entries are in the record, each with its module. - if e, ok := r.Entries[EntryKey("app", "f", "")]; !ok || e.Module != "app" { - t.Fatalf("the take-in's entries are not in the record: %+v", r.Entries) - } - if got := r.Outcomes[OutcomeKey("", "", "", "take-in")]; got.Outcome != OutcomeTakenIn { - t.Fatalf("the take-in is not the record's outcome: %+v", r.Outcomes) + if kept, err := h.Records.Kept(); err != nil || len(kept) != 0 { + t.Fatalf("records %v %v: a take-in opens none", kept, err) } if len(src.vals) != 0 || len(events) != 0 { t.Fatalf("a take-in.taken moved source %+v and said %+v", src.vals, events) @@ -90,12 +71,4 @@ func TestATakeInTakenOpensOneRecordAndMovesNoSource(t *testing.T) { if err := take(h, merged); err != nil || len(src.vals) != 0 || len(events) != 0 { t.Fatalf("a merged message moved source %+v and said %+v (%v)", src.vals, events, err) } - drop := json.RawMessage(`{"v":1,"kind":"taken","key":"m.00000000000000000002","what":"revision","dropped":true,"branch":"main","source":8,` + - `"merges":[{"repository":"novox/lib","number":4,"merge":"` + merge + `"}],"since":"2026-11-11T19:00:00Z"}`) - if err := c.Hear(stdio.Envelope{Key: "mesh-delivery-indexer.take-in.taken", Body: drop}); err != nil { - t.Fatal(err) - } - if kept, _ := h.Records.Kept(); len(kept) != 1 || len(events) != 0 { - t.Fatalf("a hand drop opened %d records and said %d events", len(kept), len(events)) - } } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/verbs.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/verbs.go index 2d84649dc..ecf5170d9 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/verbs.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/verbs.go @@ -5,8 +5,6 @@ import ( "sort" "strings" "time" - - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" ) // The seat's verbs that read (novox/hq ADR 0239; design 47 G §431, §172, A-24): from G's delivery the gate keeps no merge @@ -52,14 +50,27 @@ func holdsRepository(r *Record, repository string) bool { return false } +// Member is one pull request of a record as the verbs deliveries and show answer it: its id, repository, number, head, +// state, whether it is open and its base (the seat's type goes with the verb groups, design 47 H §764; the verbs answer +// from the records, A-24). +type Member struct { + ID string `json:"id"` + Repository string `json:"repository"` + Number int64 `json:"number"` + Head string `json:"head"` + State string `json:"state"` + Open bool `json:"open,omitempty"` + Base string `json:"base,omitempty"` +} + // membersOf are a record's pull requests: the group's pulls as kept, or else the members its heads name (`@` // to the commit, as an Approve names them). -func membersOf(r *Record) []seat.Member { +func membersOf(r *Record) []Member { open := r.Ended == nil - var out []seat.Member + var out []Member if r.Group != nil && len(r.Group.Pulls) > 0 { for _, p := range r.Group.Pulls { - out = append(out, seat.Member{ID: IDOf(p.Repository, firstOf(p.Commit, p.Head)), Repository: p.Repository, State: p.State, + out = append(out, Member{ID: IDOf(p.Repository, firstOf(p.Commit, p.Head)), Repository: p.Repository, State: p.State, Number: int64(p.Number), Head: p.Head, Open: open && p.State != "merged", Base: p.Base}) } return out @@ -74,7 +85,7 @@ func membersOf(r *Record) []seat.Member { if !ok { repository = id } - out = append(out, seat.Member{ID: IDOf(repository, r.Heads[id]), Repository: repository, Number: int64(number), Head: r.Heads[id], + out = append(out, Member{ID: IDOf(repository, r.Heads[id]), Repository: repository, Number: int64(number), Head: r.Heads[id], State: stateOf(r), Open: open}) } return out @@ -164,50 +175,6 @@ func (h *Holder) Show(id string) (any, error) { return map[string]any{"records": out}, nil } -// Groups is the seat's verb groups, from the records alone until the seat's definition drops it (design 47 H §764): every record's group not ended (all: those that ended too), or with only that group -// alone, ended or not, and none for a group it does not know. Each with its members, its state, why, and the acts the gate said -// for it with the outcome recorded in the record (owedended.go). -func (h *Holder) Groups(all bool, only string) seat.GroupsAnswer { - kept, err := h.keptRecords() - if err != nil { - return seat.GroupsAnswer{Error: err.Error()} - } - out := seat.GroupsAnswer{Groups: []seat.Group{}} - for _, r := range kept { - if only != "" && groupOf(r) != only || only == "" && !all && r.Ended != nil { - continue - } - g := seat.Group{ID: groupOf(r), State: stateOf(r), Members: membersOf(r), Acts: recordedActs(r)} - if r.Ended != nil { - g.Why = r.Ended.Why - } else { - g.Why = allRun(r) - } - out.Groups = append(out.Groups, g) - } - sort.SliceStable(out.Groups, func(i, j int) bool { return out.Groups[i].ID < out.Groups[j].ID }) - return out -} - -// recordedActs are the acts a record holds, each with its outcome when the forge bridge said one, ordered by id. -func recordedActs(r *Record) []seat.Act { - out := []seat.Act{} - for _, a := range r.Acts { - o := a.Owed - if o.ID == "" { - o = seat.Owed{ID: a.ID, Act: a.Act, Repository: a.Repository, Number: int64(a.Number), Head: a.Head} - } - act := seat.Act{Outcome: a.Outcome, Words: a.Words, Owed: o} - if a.Outcome != "" { - at := a.At - act.At = &at - } - out = append(out, act) - } - sort.Slice(out, func(i, j int) bool { return out[i].Owed.ID < out[j].Owed.ID }) - return out -} - // StalledLine is one record stalled: its open wait past its bound, or an act owed the forge unanswered past its. type StalledLine struct { // ID is the record's instance. diff --git a/modules/mesh-delivery-gate/module.json b/modules/mesh-delivery-gate/module.json index d7d108ff7..10cc389f7 100644 --- a/modules/mesh-delivery-gate/module.json +++ b/modules/mesh-delivery-gate/module.json @@ -9,7 +9,6 @@ "serves": [ "deliveries", "show", - "groups", "source", "recovery", "stalled" @@ -18,13 +17,11 @@ ], "consumes": [ "mesh-delivery-forge-bridge.owed.ended", + "git.merge-called.*", "mesh-delivery-indexer.take-in.taken", "mesh-catalog.registration-changed", "mesh-conditions.findings-asked" ], - "emits": [ - "transition" - ], "uses": [ "operator-channel" ], @@ -43,15 +40,18 @@ "milestone": { "type": "string" }, + "source": { + "type": "integer" + }, + "call": { + "type": "string" + }, "heads": { "type": "object" }, "opened": { "type": "string" }, - "forge-made": { - "type": "boolean" - }, "form": { "type": "integer" }, @@ -91,12 +91,6 @@ "since": { "type": "string" }, - "revision": { - "type": "string" - }, - "half": { - "type": "string" - }, "branch": { "type": "string" }, @@ -134,9 +128,11 @@ "effect": "set-fields", "fields": [ "milestone", + "branch", + "source", + "call", "heads", "opened", - "forge-made", "form" ], "sequence": true @@ -205,23 +201,6 @@ "into": "away", "key": "node" }, - { - "name": "pin", - "effect": "set-entry", - "fields": [ - "revision", - "half", - "branch" - ], - "into": "pinned", - "key": "revision" - }, - { - "name": "free", - "effect": "remove", - "into": "pinned", - "key": "revision" - }, { "name": "act", "effect": "set-entry", -- 2.54.0 From 6d398264db3981e68fe3347fd977ca5fc500160d Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:14:00 +0100 Subject: [PATCH 14/43] GATE-8: gate_source_seed seeds every watched branch's first source revision MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For every watched branch the registrations name, one source revision at each registered repository's head read through git.branch, start true, a call of its own, source-moved said and no record opened; recorded in the hand-act log first, the counts answered. It refuses to run while a key of the earlier form is held, naming it (the source bucket is dropped first). Builds: design 47 H §768; H-A-15, H-A-47 (the seed opens no record; the start pass writes through its path), H-A-76 (Q-G2: numbers from 1, refuses an earlier-form key) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/mesh-delivery-gate/seed.go | 98 +++++++++++++++++++ .../cmd/mesh-delivery-gate/sourcetools.go | 8 ++ .../mesh-delivery-gate/sourcetools_test.go | 41 ++++++++ .../cmd/mesh-delivery-gate/takenin_test.go | 6 ++ modules/mesh-delivery-gate/module.json | 1 + 5 files changed, 154 insertions(+) create mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/seed.go diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/seed.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/seed.go new file mode 100644 index 000000000..7681a11a9 --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/seed.go @@ -0,0 +1,98 @@ +package main + +// The clean state seeds every watched branch's first source revision (design 47 H §768; H-A-15, H-A-76; ADR 0682 §12): +// gate_source_seed, with why, written in the hand-act log first, writes for every watched branch the registrations name +// one source revision at each registered repository's current head, read through the seat git's verb branch, with start +// true and a call of its own, and says source-moved; it opens no record (H-A-47). It numbers from 1, the source bucket +// dropped before it, and refuses to run while a key of the earlier form (or any key it cannot read) is held, naming it. + +import ( + "errors" + "fmt" + "sort" + "strings" + + "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" +) + +// SourceSeed is the tool gate_source_seed: it answers the branches and the repositories it wrote, counted, and those left +// out because their head could not be read. +func (h *Holder) SourceSeed(why, by string, acts handActs) (map[string]any, error) { + if strings.TrimSpace(why) == "" { + return nil, errors.New("say why the sources are seeded") + } + if h.Source == nil || h.Catalog == nil || h.Forge == nil { + return nil, errors.New("this gate keeps no source, reads no registrations or reads no forge") + } + if acts == nil { + return nil, errors.New("the hand-act log cannot be reached here, so nothing was seeded") + } + h.startMu.Lock() + defer h.startMu.Unlock() + regs, err := h.Catalog.Registrations() + if err != nil { + return nil, fmt.Errorf("%w: %w", errRegistrationsUnread, err) + } + watched := watchedRepositories(regs.Registrations) + branches := make([]string, 0, len(watched)) + for b := range watched { + branches = append(branches, b) + } + sort.Strings(branches) + var unreadable []string + for _, b := range branches { + if _, _, err := h.Source.Read(b); err != nil { + unreadable = append(unreadable, fmt.Sprintf("source.%s (%v)", b, err)) + } + } + if len(unreadable) > 0 { + return nil, fmt.Errorf("nothing was seeded: a key of the earlier form is held, drop the source bucket first (§765): %s", + strings.Join(unreadable, "; ")) + } + at := h.Now().UTC() + if _, err := acts.Record(meshhandactsclient.Request{What: fmt.Sprintf("seeded the first source revision of every watched branch "+ + "(seed by %s at %s)", by, at.Format("2006-01-02T15:04:05Z")), Why: why, Cause: "gate_source_seed", Verb: "source-seed"}); err != nil { + return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was seeded: %w", err) + } + var written, leftOut []string + repositories := 0 + var errs []error + for _, branch := range branches { + var heads []revisionHead + for _, repository := range watched[branch] { + a, err := h.Forge.Branch(repository, branch) + if err == nil && a.Error != "" { + err = errors.New(a.Error) + } + if err == nil && a.Head == "" { + err = errors.New("the forge named no head") + } + if err != nil { + leftOut = append(leftOut, repository+"@"+branch) + h.logf("[mesh-delivery] the seed of source.%s leaves out %s: its head cannot be read (%v)", branch, repository, err) + continue + } + heads = append(heads, revisionHead{Repository: repository, Commit: a.Head}) + } + if len(heads) == 0 { + continue + } + call, err := newCall() + if err != nil { + errs = append(errs, err) + continue + } + named, _, err := h.nameRevision(branch, call, heads) + if err != nil { + errs = append(errs, err) + continue + } + if err := h.saySourceMoved(branch, named, true, heads); err != nil { + errs = append(errs, fmt.Errorf("source.%s is seeded at %d and its source-moved is not said: %w", branch, named.Source, err)) + } + written = append(written, branch) + repositories += len(heads) + } + return map[string]any{"branches": len(written), "repositories": repositories, "written": written, "left-out": leftOut, + "by": by}, errors.Join(errs...) +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go index 421a02441..726b3e416 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools.go @@ -83,6 +83,14 @@ func sourceTools(h *Holder, acts handActs) []stdio.Tool { Run: func(a map[string]any) (any, error) { return h.SourceAgain(strArg(a, "branch"), strArg(a, "why"), stdio.CallerOf(a), acts) }}, + {Name: "gate_source_seed", + Description: "Seed every watched branch's first source revision at the clean state (design 47 H §768): for every watched " + + "branch the registrations name, one source revision at each registered repository's current head read from the forge, " + + "start true, then source-moved; no record. Recorded in the hand-act log of the seat mesh-hand-acts first. It refuses " + + "to run while a source key of the earlier form is held, naming it. Answers the branches and repositories written, counted.", + Input: map[string]any{"type": "object", "properties": map[string]any{"why": str("why the sources are seeded, in plain words")}, + "required": []string{"why"}}, + Run: func(a map[string]any) (any, error) { return h.SourceSeed(strArg(a, "why"), stdio.CallerOf(a), acts) }}, {Name: "gate_source_start", Description: "Start the source of one watched branch the registrations name and no source key holds yet, from the forge: each " + "registered repository at the branch's head, one source revision with a call of its own, then source-moved; recorded in the hand-act log of the seat " + diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go index 48e9c668e..ddbf95276 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcetools_test.go @@ -314,3 +314,44 @@ func (f *fakeForge) Contains(repository, commit, in string) (bool, error) { } return f.contains(commit, in), nil } + +// GATE-8/AC14 (design 47 H §768; H-A-47, H-A-76): gate_source_seed writes one revision per watched branch at each registered +// repository's head, numbered from 1, start true, a call of its own, says source-moved, opens no record, answers the counts, +// and is recorded in the hand-act log first; it refuses to run while a key of the earlier form is held, naming it. +func TestTheSeedWritesEveryWatchedBranchsFirstRevision(t *testing.T) { + regs := []meshcatalog.Registration{{Module: "a", Origin: "o/a", Branches: []string{"main"}}, {Module: "b", Origin: "o/b", Branches: []string{"main"}}, + {Module: "c", Origin: "o/c", Branches: []string{"master"}}} + forge := &fakeForge{heads: map[string]string{"o/a@main": "ca", "o/b@main": "cb", "o/c@master": "cc"}} + setup := func() (*Holder, *branchSource, *[]seat.SourceMovedEvent) { + src := &branchSource{memSource{vals: map[string]Source{}}} + var moved []seat.SourceMovedEvent + h := &Holder{Now: time.Now, Source: src, Forge: forge, Catalog: fakeCatalog{regs: regs}, Logf: func(string, ...any) {}, + SaySeat: func(subject string, body any, id string) error { + moved = append(moved, body.(seat.SourceMovedEvent)) + return nil + }} + return h, src, &moved + } + h, src, moved := setup() + acts := &handActsFake{refuse: errors.New("log down")} + if _, err := h.SourceSeed("clean state", "lead@shanks", acts); err == nil || len(src.vals) != 0 { + t.Fatalf("seeded without the log: %v %+v", err, src.vals) + } + acts.refuse = nil + out, err := h.SourceSeed("clean state", "lead@shanks", acts) + if err != nil || out["branches"] != 2 || out["repositories"] != 3 || len(acts.took) != 1 || acts.took[0].Cause != "gate_source_seed" { + t.Fatalf("seed %+v %v, log %+v", out, err, acts.took) + } + if got := src.vals["main"]; got.Source != 1 || len(got.Call) != 16 || got.Heads["o/a"] != "ca" || got.Heads["o/b"] != "cb" { + t.Fatalf("source.main %+v", got) + } + if len(*moved) != 2 || !(*moved)[0].Start || !(*moved)[1].Start || (*moved)[0].Source != 1 || len((*moved)[0].Heads) != 2 { + t.Fatalf("source-moved %+v", *moved) + } + h, src, moved = setup() + src.unreadable = map[string]bool{"master": true} + if _, err := h.SourceSeed("clean state", "lead@shanks", &handActsFake{}); err == nil || !strings.Contains(err.Error(), "source.master") || + len(src.vals) != 0 || len(*moved) != 0 { + t.Fatalf("seeded over a key of the earlier form: %v %+v", err, src.vals) + } +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go index b145844b9..7a32bd3db 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_test.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "errors" "strings" "testing" "time" @@ -13,6 +14,8 @@ import ( type memSource struct { vals map[string]Source rev uint64 + // unreadable are the branches whose key holds a value the gate cannot read (the earlier form). + unreadable map[string]bool } func (m *memSource) Set(branch string, update func(prev *Source) (Source, bool)) (uint64, error) { @@ -32,6 +35,9 @@ func (m *memSource) Set(branch string, update func(prev *Source) (Source, bool)) return m.rev, nil } func (m *memSource) Read(branch string) (*Source, uint64, error) { + if m.unreadable[branch] { + return nil, 0, errors.New("a source does not fit its shape") + } v, ok := m.vals[branch] if !ok { return nil, 0, nil diff --git a/modules/mesh-delivery-gate/module.json b/modules/mesh-delivery-gate/module.json index 10cc389f7..e220e13b5 100644 --- a/modules/mesh-delivery-gate/module.json +++ b/modules/mesh-delivery-gate/module.json @@ -314,6 +314,7 @@ "gate_records", "gate_source", "gate_source_again", + "gate_source_seed", "gate_source_start" ], "config": { -- 2.54.0 From 4f10af81b0764773dbb38d52b8aa4c86e4279f7d Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:17:42 +0100 Subject: [PATCH 15/43] GATE-6: the watchdog holds a head no merge call delivered and raises head-untaken MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The gate consumes the forge bridge's head-moved, keeps in its template state heads each head source. does not name, and raises head-untaken (scope delivery, id ., warning, the operator its resolver) once untaken-bound passes, on its one deadline timer; a later source revision naming the head lets it go and clears the finding. The stalled conditions, their state and the operator-channel use go: the watchman's record-open is the one signal. Builds: design 47 H §740; H-A-27 (head-moved's form, forgemsg), H-A-48 (the stalled conditions go; untaken-bound on the gate's one timer), H-A-81 (warning, resolver operator, no owner) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/mesh-delivery-gate/conditions.go | 211 --------------- .../cmd/mesh-delivery-gate/findings.go | 2 +- .../cmd/mesh-delivery-gate/gate.go | 11 +- .../cmd/mesh-delivery-gate/heads.go | 256 ++++++++++++++++++ .../cmd/mesh-delivery-gate/heads_test.go | 137 ++++++++++ .../cmd/mesh-delivery-gate/keeps.go | 2 +- .../cmd/mesh-delivery-gate/keeps_test.go | 2 +- .../cmd/mesh-delivery-gate/main.go | 9 +- .../cmd/mesh-delivery-gate/manifest_test.go | 8 +- .../cmd/mesh-delivery-gate/mergecalled.go | 4 + .../cmd/mesh-delivery-gate/react.go | 6 +- .../cmd/mesh-delivery-gate/settings.go | 17 ++ .../mesh-delivery-gate/files/settings.json | 3 +- modules/mesh-delivery-gate/module.json | 16 +- 14 files changed, 445 insertions(+), 239 deletions(-) delete mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/conditions.go create mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads.go create mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads_test.go diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/conditions.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/conditions.go deleted file mode 100644 index 78f637800..000000000 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/conditions.go +++ /dev/null @@ -1,211 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "sort" - "strings" - "time" - - occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient" - oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" - stdio "git.novox.be/novox/mesh-sdk/go" -) - -// The gate says its own stalled lines to the operator as conditions (novox/hq issue 1275, met again 2026-10-25; ADR 0437 -// §9: any module says a condition on the operator-channel seat's one subject, and the router alone chooses the channels). -// Until release 6 the controller's self-check read the `stalled` verb and raised each line as `delivery..stalled`; -// release 6 deleted that self-check (mesh-controller dc456d56) and nothing read the verb since, so a half-merged group was -// said to no one. Now, after every look, the gate raises each line `stalled` lists, raises it again when its severity or -// its need changes, and clears each it raised that is listed no more. What it raised is kept in its bucket conditions, so a -// gate that restarts clears what ended while it was away. - -// ConditionStore keeps the conditions the gate has raised: the condition's key to what it said (conditionSaid). -type ConditionStore interface { - Said() (map[string]string, error) - PutSaid(said map[string]string) error -} - -// saidKey is the one key of the bucket conditions. -const saidKey = "said" - -// kvConditions is the bucket conditions over the runtime. -type kvConditions struct{} - -func (kvConditions) Said() (map[string]string, error) { - e, err := stdio.State("conditions").Get(saidKey) - if err != nil || e == nil { - return map[string]string{}, err - } - said := map[string]string{} - if err := json.Unmarshal(e.Value, &said); err != nil { - return nil, fmt.Errorf("the conditions the gate raised are not readable: %w", err) - } - return said, nil -} - -func (kvConditions) PutSaid(said map[string]string) error { - _, err := stdio.State("conditions").Put(saidKey, said) - return err -} - -// sayOnSeat says a condition on the operator-channel seat under the gate's own name. -func sayOnSeat(c oc.Condition) error { - subject, err := occ.ConditionSubject("mesh-delivery-gate") - if err != nil { - return err - } - raw, err := occ.EncodeCondition(c) - if err != nil { - return err - } - return stdio.SeatPublish(subject, json.RawMessage(raw), "") -} - -// conditionKeyOf is the key of a stalled line's condition: its last part is the condition's kind. -func conditionKeyOf(id string) string { return "mesh-delivery-gate.delivery." + id + ".stalled" } - -// conditionSaid is what a raised condition said that, when it changes, raises it again: its severity and what it needs. -// The words of a line hold durations, which change at every look and are no reason to tell the operator again. -func conditionSaid(l StalledLine) string { return severityOfLine(l) + "|" + l.H2 } - -func severityOfLine(l StalledLine) string { - if l.Severity == occ.SeverityUrgent { - return occ.SeverityUrgent - } - return occ.SeverityWarning -} - -// conditionOf is a stalled line as the condition the operator is told. -func conditionOf(l StalledLine) oc.Condition { - headline := "stalled: " + l.ID - if r := []rune(headline); len(r) > occ.HeadlineLength { - headline = string(r[:occ.HeadlineLength-1]) + "…" - } - needs := "read mesh-delivery.show " + l.ID + " and decide" - return oc.Condition{Key: conditionKeyOf(l.ID), Severity: severityOfLine(l), State: occ.StateRaised, - Subject: oc.ConditionSubject{Scope: "delivery", ID: l.ID}, Summary: l.Says, Headline: headline, Needs: needs, - Explanation: occ.NeedsYou + needs + ". " + l.Says + " What ends it: " + l.H2 + ".", - Resolved: "The gate lists " + l.ID + " as stalled no more."} -} - -// sayConditions raises and clears the conditions of the stalled lines (see above). Outside the lock; React runs it after -// the look, so it follows every event. A condition that could not be said is said again at the next look, after -// askAgainAfter at the latest. -func (h *Holder) sayConditions() { - if h.Say == nil || h.Conditions == nil { - return - } - if h.said == nil { - said, err := h.Conditions.Said() - if err != nil { - h.Logf("[mesh-delivery] the conditions raised before cannot be read, so none is said yet: %v; asked again in %s", err, askAgainAfter) - h.again("the conditions the gate raised, read") - return - } - h.said = said - } - if at := h.nextStall(h.Now()); !at.IsZero() { - h.lookAt(at, "a record whose stalled line comes due with no event") - } - lines := map[string]StalledLine{} - for _, l := range h.Stalled() { - // One condition for an id: the most severe of its lines. - if o, ok := lines[l.ID]; !ok || (severityOfLine(o) != occ.SeverityUrgent && severityOfLine(l) == occ.SeverityUrgent) { - lines[l.ID] = l - } - } - ids := make([]string, 0, len(lines)) - for id := range lines { - ids = append(ids, id) - } - sort.Strings(ids) - changed, failed := false, false - for _, id := range ids { - l, key := lines[id], conditionKeyOf(id) - said := conditionSaid(l) - if h.said[key] == said { - continue - } - if err := h.Say(conditionOf(l)); err != nil { - h.Logf("[mesh-delivery] the condition of %s could not be said: %v", id, err) - failed = true - continue - } - h.Logf("[mesh-delivery] %s is said to the operator as %s: %s", id, severityOfLine(l), l.Says) - h.said[key], changed = said, true - } - // Cleared when the line is gone: what it raised is kept in the bucket conditions, so a gate that restarts clears what ended. - var gone []string - for key := range h.said { - id := strings.TrimSuffix(strings.TrimPrefix(key, "mesh-delivery-gate.delivery."), ".stalled") - if _, ok := lines[id]; !ok { - gone = append(gone, key) - } - } - sort.Strings(gone) - for _, key := range gone { - id := strings.TrimSuffix(strings.TrimPrefix(key, "mesh-delivery-gate.delivery."), ".stalled") - c := oc.Condition{Key: key, Subject: oc.ConditionSubject{Scope: "delivery", ID: id}, State: occ.StateCleared, - Resolved: "The gate lists " + id + " as stalled no more."} - if err := h.Say(c); err != nil { - h.Logf("[mesh-delivery] the condition of %s could not be cleared: %v", id, err) - failed = true - continue - } - h.Logf("[mesh-delivery] the condition of %s is cleared: it is stalled no more", id) - delete(h.said, key) - changed = true - } - if changed { - if err := h.Conditions.PutSaid(h.said); err != nil { - h.Logf("[mesh-delivery] the conditions raised cannot be kept: %v", err) - } - } - if !failed { - h.sayFailures = 0 - return - } - // A seat that keeps refusing is tried again after askAgainAfter, up to sayTries looks in a row; then only at the - // next event, and said once, loudly, in the log (the operator hears nothing while the seat refuses). - h.sayFailures++ - switch { - case h.sayFailures < sayTries: - h.again("a condition that could not be said to the operator") - case h.sayFailures == sayTries: - h.Logf("[mesh-delivery] NO CONDITION REACHES THE OPERATOR: the operator-channel seat refused %d looks in a row; "+ - "tried again at each event from now on", sayTries) - } -} - -// sayTries is how many looks in a row a condition that could not be said is tried again on the clock. -const sayTries = 10 - -// nextStall is the earliest time after now when a record's stalled line comes due with nothing else happening: a record open -// past its bound, an act unanswered past its. Zero when none. -func (h *Holder) nextStall(now time.Time) time.Time { - var next time.Time - if h.Records == nil { - return next - } - consider := func(at time.Time) { - if at.After(now) && (next.IsZero() || at.Before(next)) { - next = at - } - } - kept, err := h.Records.Kept() - if err != nil { - return next - } - for _, r := range kept { - if r.Ended == nil { - consider(r.Opened.Add(recordOpenBound).Add(time.Second)) - } - for _, a := range r.Acts { - if a.Outcome == "" { - consider(a.At.Add(owedBound).Add(time.Second)) - } - } - } - return next -} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go index 3170fb6e1..194f71865 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go @@ -28,5 +28,5 @@ type FindingWriter interface { // generated contract of the seat that defines it (A-17). It is started once the runtime serves (A-40). func NewFindingWriter() (*views.HeldWriter[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent], error) { return views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{ - Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{}}) + Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{HeadUntaken}}) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go index 4c129bb92..62cddfe41 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go @@ -3,8 +3,6 @@ package main import ( "sync" "time" - - oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" ) // The holder of the seat mesh-delivery (novox/hq ADR 0239, to-be 47): from milestone G's delivery it keeps no merge states @@ -36,13 +34,8 @@ type Holder struct { SaySeat SeatSay // Findings is the gate's one held writer of its own findings (findings.go); nil writes none. Findings FindingWriter - // Say says a condition to the operator, and Conditions keeps which the gate raised (conditions.go); nil says none. - Say func(c oc.Condition) error - Conditions ConditionStore - // said is what Conditions keeps, read at the first look; sayFailures the looks in a row a condition could not be said. - // Touched by React alone. - said map[string]string - sayFailures int + // Heads is the state heads of the watchdog (heads.go); nil watches none. + Heads HeadStore mu sync.Mutex started time.Time diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads.go new file mode 100644 index 000000000..1eaa9656b --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads.go @@ -0,0 +1,256 @@ +package main + +// The watchdog (design 47 H §740; ADR 0682 §3; H-A-48): the gate consumes the forge bridge's head-moved and keeps, in its +// template state heads (one key `.`, value {head, since}), each head source. does not name for +// that repository, deleting the key once source. names that head. When untaken-bound passes and source. +// still does not name it, the gate raises head-untaken (scope delivery, id `.`), and clears it when a +// later revision names the repository at the newest head. The gate keeps one deadline timer (react.go), set to the +// earliest since plus the bound, and no other clock. Nothing but a merge call or gate_head_take (§742) delivers a head. + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "sort" + "time" + + "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" + stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/keys" +) + +// HeadUntaken is the finding of a head no merge call delivered; warning, the operator its resolver (H-A-81). +const HeadUntaken = "head-untaken" + +const untakenSeverity = meshconditions.FindingSeverityWarning + +// WordsUntaken are the words the finding names its cause with (§740). +const WordsUntaken = "moved outside the mesh: delivered by nothing" + +// headMovedEvent is the bridge's head-moved as the gate's manifest consumes it. +var headMovedEvent = consumedFamily(forgemsg.HeadMovedEvents()) + +// HeldHead is one value of the state heads. +type HeldHead = seat.HeadsByRepositoryBranchValue + +// HeadStore is the gate's state heads. +type HeadStore interface { + Get(key string) (*HeldHead, error) + Put(key string, v HeldHead) error + Delete(key string) error + // All are every key held with its value. + All() (map[string]HeldHead, error) +} + +// kvHeads is the state heads over the runtime. +type kvHeads struct{} + +func (kvHeads) Get(key string) (*HeldHead, error) { + e, err := stdio.State(seat.StateHeads).Get(key) + if err != nil || e == nil { + return nil, err + } + v, err := seat.DecodeHeadsByRepositoryBranchValue(e.Value) + if err != nil { + return nil, fmt.Errorf("heads %s is not readable: %w", key, err) + } + return &v, nil +} + +func (kvHeads) Put(key string, v HeldHead) error { + _, err := stdio.State(seat.StateHeads).Put(key, v) + return err +} + +func (kvHeads) Delete(key string) error { return stdio.State(seat.StateHeads).Delete(key) } + +func (s kvHeads) All() (map[string]HeldHead, error) { + names, err := stdio.State(seat.StateHeads).Keys() + if err != nil { + return nil, err + } + out := map[string]HeldHead{} + for _, k := range names { + v, err := s.Get(k) + if err != nil { + return nil, err + } + if v != nil { + out[k] = *v + } + } + return out, nil +} + +// headKey is a held head's key and the finding's id: `.`, the branch escaped as a key's name is. +func headKey(repository, branch string) string { + return seat.HeadsByRepositoryBranchKey(repository, keys.EscapeName(branch)) +} + +// HeadMoved takes one head-moved: a head source. names is let go, another is held from when it was first heard. +// A body that cannot be read is said and taken; an error only when the state could not be read or written. +func (h *Holder) HeadMoved(e stdio.Envelope) error { + m, err := forgemsg.DecodeHeadMoved(e.Body) + if err != nil { + h.Refuse(fmt.Errorf("a head-moved (%s) is not readable, so no head is watched: %w", e.Key, err)) + return nil + } + return h.holdHead(m.Repository, m.Branch, m.Head) +} + +// holdHead holds a branch's head of a repository until source. names it. +func (h *Holder) holdHead(repository, branch, head string) error { + if h.Heads == nil || h.Source == nil { + return nil + } + src, _, err := h.Source.Read(branch) + if err != nil { + return err + } + key := headKey(repository, branch) + if src != nil && sameCommit(src.Heads[repository], head) { + return h.letHeadGo(key, "source."+branch+" names it") + } + prev, err := h.Heads.Get(key) + if err != nil { + return err + } + next := HeldHead{Head: head, Since: h.Now().UTC()} + if prev != nil { + if prev.Head == head { + return nil + } + next.Since = prev.Since // untaken since the first head no call delivered + } + if err := h.Heads.Put(key, next); err != nil { + return err + } + h.Poke() + return nil +} + +// letHeadGo deletes a held head and clears its finding. +func (h *Holder) letHeadGo(key, why string) error { + held, err := h.Heads.Get(key) + if err != nil || held == nil { + return err + } + if err := h.Heads.Delete(key); err != nil { + return err + } + return h.clearUntaken(key, why) +} + +// sourceNamed lets go every held head of a branch its newest source revision names. +func (h *Holder) sourceNamed(branch string, s Source) error { + if h.Heads == nil { + return nil + } + var errs []error + for repository, commit := range s.Heads { + key := headKey(repository, branch) + held, err := h.Heads.Get(key) + if err != nil { + errs = append(errs, err) + continue + } + if held != nil && sameCommit(commit, held.Head) { + errs = append(errs, h.letHeadGo(key, fmt.Sprintf("source.%s at %d names it", branch, s.Source))) + } + } + return errors.Join(errs...) +} + +// untakenBound is how long a head may stand untaken before head-untaken is raised. +func (h *Holder) untakenBound() time.Duration { + if h.Settings != nil { + if n := h.Settings().UntakenBoundMinutes; n != nil { + return time.Duration(*n) * time.Minute + } + } + return DefaultUntakenBound +} + +// lookAtHeads raises head-untaken for every held head past the bound that its branch's source still does not name, and +// asks the one timer for the earliest one not yet past it; taken by React. +func (h *Holder) lookAtHeads() { + if h.Heads == nil || h.Source == nil { + return + } + held, err := h.Heads.All() + if err != nil { + h.Refuse(fmt.Errorf("the held heads cannot be read: %w", err)) + h.again("heads") + return + } + names := make([]string, 0, len(held)) + for k := range held { + names = append(names, k) + } + sort.Strings(names) + now := h.Now() + for _, key := range names { + v := held[key] + repository, escaped, ok := seat.ParseHeadsByRepositoryBranchKey(key) + branch, uerr := keys.UnescapeName(escaped) + if !ok || uerr != nil { + continue + } + src, _, err := h.Source.Read(branch) + if err != nil { + h.again("heads") + continue + } + if src != nil && sameCommit(src.Heads[repository], v.Head) { + if err := h.letHeadGo(key, "source."+branch+" names it"); err != nil { + h.again("heads") + } + continue + } + due := v.Since.Add(h.untakenBound()) + if now.Before(due) { + h.lookAt(due, "head-untaken") + continue + } + if err := h.raiseUntaken(key, repository, branch, v); err != nil { + h.Refuse(err) + h.again("heads") + } + } +} + +// raiseUntaken writes head-untaken for a held head, unless the writer holds it for that head already. +func (h *Holder) raiseUntaken(key, repository, branch string, v HeldHead) error { + if h.Findings == nil { + return nil + } + summary := fmt.Sprintf("%s %s at %s since %s: %s", repository, branch, shortOf(v.Head, 12), v.Since.UTC().Format(time.RFC3339), WordsUntaken) + for _, f := range h.Findings.Held() { + if f.Scope == FindingScope && f.ID == key && f.Kind == HeadUntaken && f.Summary == summary { + return nil + } + } + raw, _ := json.Marshal(map[string]any{"repository": repository, "branch": branch, "head": v.Head, "since": v.Since}) + f := meshconditions.Finding{Scope: FindingScope, ID: key, Kind: HeadUntaken, Observer: Module, Source: headMovedEvent, + Severity: untakenSeverity, Resolver: "operator", Since: v.Since, At: h.Now().UTC(), Summary: summary, + Headline: "A head no merge call delivered", Subject: meshconditions.Subject{Scope: FindingScope, ID: key}, + Explanation: "The head of " + repository + " on " + branch + " moved outside the mesh and nothing delivers it; " + + "gate_head_take takes it in by hand (" + string(raw) + ")."} + return h.Findings.Write(context.Background(), f) +} + +// clearUntaken clears head-untaken for a key, when the writer holds it. +func (h *Holder) clearUntaken(key, why string) error { + if h.Findings == nil { + return nil + } + for _, f := range h.Findings.Held() { + if f.Scope == FindingScope && f.ID == key && f.Kind == HeadUntaken { + return h.Findings.Clear(context.Background(), FindingScope, key, HeadUntaken, why) + } + } + return nil +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads_test.go new file mode 100644 index 000000000..714052180 --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads_test.go @@ -0,0 +1,137 @@ +package main + +import ( + "context" + "encoding/json" + "os" + "strings" + "testing" + "time" + + "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// memHeads is the state heads in memory. +type memHeads map[string]HeldHead + +func (m memHeads) Get(key string) (*HeldHead, error) { + if v, ok := m[key]; ok { + return &v, nil + } + return nil, nil +} +func (m memHeads) Put(key string, v HeldHead) error { m[key] = v; return nil } +func (m memHeads) Delete(key string) error { delete(m, key); return nil } +func (m memHeads) All() (map[string]HeldHead, error) { + out := map[string]HeldHead{} + for k, v := range m { + out[k] = v + } + return out, nil +} + +// findingsFake is the gate's held writer: what it holds. +type findingsFake struct { + held map[string]meshconditions.Finding + written int +} + +func (f *findingsFake) Write(_ context.Context, x meshconditions.Finding) error { + f.held[x.Scope+"."+x.ID+"."+x.Kind] = x + f.written++ + return nil +} +func (f *findingsFake) Clear(_ context.Context, scope, id, kind, _ string) error { + delete(f.held, scope+"."+id+"."+kind) + return nil +} +func (f *findingsFake) Held() []meshconditions.Finding { + var out []meshconditions.Finding + for _, x := range f.held { + out = append(out, x) + } + return out +} + +// GATE-6 (design 47 H §740; acceptance test 111's gate half): a head a merge call moved raises nothing; a head pushed outside +// the mesh is held and raises head-untaken once untaken-bound passes, naming the repository, the branch, the head, since when +// and why, and starts nothing; a later merge call taking its repository in clears it. The one timer is asked for the earliest +// held head. +func TestAHeadNoMergeCallDeliveredIsAnUntakenHead(t *testing.T) { + at := time.Date(2026, 11, 20, 4, 0, 0, 0, time.UTC) + c1, c2 := strings.Repeat("1", 40), strings.Repeat("2", 40) + setup := func() (*Holder, memHeads, *findingsFake, *time.Time) { + now := at + heads := memHeads{} + fw := &findingsFake{held: map[string]meshconditions.Finding{}} + h := &Holder{Now: func() time.Time { return now }, Heads: heads, Findings: fw, + Source: &memSource{vals: map[string]Source{"main": {Source: 4, Call: "00000000000000aa", Heads: map[string]string{"novox/app": c1}}}}} + h.init() + h.SaySeat = func(string, any, string) error { return nil } + return h, heads, fw, &now + } + moved := func(head string) stdio.Envelope { + return stdio.Envelope{Key: "mesh-delivery-forge-bridge.forge.novox.app.main.head-moved", + Body: json.RawMessage(`{"v":1,"repository":"novox/app","branch":"main","head":"` + head + `"}`)} + } + t.Run("a head the source names raises nothing", func(t *testing.T) { + h, heads, fw, now := setup() + if err := h.HeadMoved(moved(c1)); err != nil { + t.Fatal(err) + } + *now = at.Add(time.Hour) + h.React() + if len(heads) != 0 || len(fw.held) != 0 { + t.Fatalf("held %+v, findings %+v", heads, fw.held) + } + }) + t.Run("a head pushed outside the mesh raises head-untaken past the bound and clears on a later merge call", func(t *testing.T) { + h, heads, fw, now := setup() + if err := h.HeadMoved(moved(c2)); err != nil { + t.Fatal(err) + } + h.React() + if heads["novox/app.main"].Head != c2 || len(fw.held) != 0 || !h.wanted.at.Equal(at.Add(DefaultUntakenBound)) { + t.Fatalf("held %+v, findings %+v, next look %s", heads, fw.held, h.wanted.at) + } + *now = at.Add(DefaultUntakenBound) + h.React() + f, ok := fw.held["delivery.novox/app.main."+HeadUntaken] + if !ok || f.Severity != meshconditions.FindingSeverityWarning || f.Resolver != "operator" || !f.Since.Equal(at) || + !strings.Contains(f.Summary, "novox/app main at 222222222222") || !strings.Contains(f.Summary, WordsUntaken) { + t.Fatalf("findings %+v", fw.held) + } + h.React() + if fw.written != 1 { + t.Fatalf("head-untaken written %d times", fw.written) + } + member := `{"repository":"novox/app","number":9,"branch":"main","head":"h","commit":"` + c2 + `"}` + if err := h.MergeCalled(stdio.Envelope{Key: "git.merge-called.0123456789abcdef", Body: json.RawMessage( + `{"call":"0123456789abcdef","by":"lead@shanks","account":"a","at":"2026-11-20T04:30:00Z","members":[` + member + `]}`)}); err != nil { + t.Fatal(err) + } + if len(heads) != 0 || len(fw.held) != 0 { + t.Fatalf("after the merge call: held %+v, findings %+v", heads, fw.held) + } + }) + t.Run("the setting untaken-bound, at least one minute", func(t *testing.T) { + h, _, _, _ := setup() + n := 3 + h.Settings = func() Settings { return Settings{UntakenBoundMinutes: &n} } + if h.untakenBound() != 3*time.Minute { + t.Fatalf("bound %s", h.untakenBound()) + } + for raw, ok := range map[string]bool{`{"untaken-bound":1}`: true, `{"untaken-bound":0}`: false, `{"untaken-bound":1.5}`: false} { + p := t.TempDir() + "/s.json" + if err := writeFile(p, raw); err != nil { + t.Fatal(err) + } + if _, err := readSettings(p); (err == nil) != ok { + t.Errorf("%s: %v", raw, err) + } + } + }) +} + +func writeFile(p, s string) error { return os.WriteFile(p, []byte(s), 0o600) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps.go index d5f3a20db..1c271c859 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps.go @@ -6,7 +6,7 @@ import "git.novox.be/novox/mesh-sdk/go/announce" // issue 1790): the module says it itself, from its code, before it serves, and a reader is granted what it said // (clause 3). A state is readable when another module's manifest reads it (keeps_test.go names the readers). keeps_test.go holds the list to the // manifest's `state`, and keeps_integration_test.go that the module announces it on a bus. -var Kept = []announce.State{{Name: "conditions"}, {Name: "findings"}, {Name: "source", Readable: true}} +var Kept = []announce.State{{Name: "findings"}, {Name: "heads"}, {Name: "source", Readable: true}} // announceKept says Kept through keeps, which is stdio.Keeps. func announceKept(keeps func(...announce.State) error) error { return keeps(Kept...) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go index 59cced012..b68129f73 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go @@ -90,7 +90,7 @@ func TestTheFourStatesMoveToTheTemplateAndTwoStayOld(t *testing.T) { } old := string(raw[strings.Index(string(raw), `"state": [`):]) old = old[:strings.Index(old, "]")] - for _, n := range []string{"conditions"} { + for _, n := range []string{"heads"} { // the stalled conditions' state went (H-A-48); heads is the watchdog's (§740) if !moved[n] || strings.Contains(old, `"`+n+`"`) { t.Errorf("%s: template state %v, old section lists it %v", n, moved[n], strings.Contains(old, `"`+n+`"`)) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go index 2a6b3c3c7..ed8d1c7cb 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go @@ -32,7 +32,7 @@ func (l *listening) get() string { l.mu.Lock(); defer l.mu.Unlock(); return l.n // The events this owner follows: the forge bridge's word on an act owed, and the step events the delivery records are built // from (consumer.go). -var followed = append(append([]string{owedEndedEvent, mergeCalledEvent}, stepPatterns()...), registrationChangedEvent) +var followed = append(append([]string{owedEndedEvent, mergeCalledEvent, headMovedEvent}, stepPatterns()...), registrationChangedEvent) func main() { settingsFile := os.Getenv("MESH_DELIVERY_GATE_SETTINGS") @@ -57,8 +57,7 @@ func main() { Source: kvSource{}, Owed: seatOwed{say: stdio.SeatPublish}, // The gate says what stalled to the operator itself (issue 1275, ADR 0437 §9). - Say: sayOnSeat, - Conditions: kvConditions{}, + Heads: kvHeads{}, } h.init() h.SaySeat = stdio.SeatPublish @@ -111,6 +110,7 @@ func run(h *Holder, l *listening) { } } l.set("listening") + h.Poke() // the heads held before a stop are looked at, and the one timer armed for them (§740) logf("[mesh-delivery] listening: %s", strings.Join(followed, ", ")) // A watched branch no source key holds starts from the forge, once at each start and at each registration change (A-401). go startSourcesAtStart(h) @@ -128,6 +128,9 @@ func take(h *Holder, e stdio.Envelope) error { if h.Consumer != nil && h.Consumer.Takes(e.Key) { return h.Consumer.Hear(e) } + if stdio.TopicMatches(headMovedEvent, e.Key) { + return h.HeadMoved(e) + } if stdio.TopicMatches(mergeCalledEvent, e.Key) { return h.MergeCalled(e) } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go index 11296cdeb..9dce11fa2 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go @@ -144,7 +144,7 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { } for _, c := range m.Consumes { for _, gone := range []string{".forge.", "teller", "view", "forge-pull", "forge-status"} { - if strings.Contains(c, gone) { + if strings.Contains(c, gone) && c != headMovedEvent { // the bridge's one message is the watchdog's (H §740) t.Errorf("the manifest still consumes %s, which G's gate does not hear (§431, §592, §593)", c) } } @@ -181,10 +181,10 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { t.Fatalf("reads %v", m.Reads) } // It calls no controller verb and no forge verb: the forge's reads and writes are the bridge's (ADR 0437 §8; design 47 §592). - // It uses operator-channel only to say its conditions (ADR 0437 §9, issue 1275): the teller asks no more (§431). + // It uses no operator-channel: its stalled conditions went, the watchman's record-open the one signal (H-A-48). want := []string{"seat:git.branch", "seat:mesh-catalog.registrations", "seat:mesh-hand-acts.hand-act", WantsEntryVerb, EngineCompositionVerb} - if !reflect.DeepEqual(m.Uses, []string{"operator-channel"}) { - t.Fatalf("uses %v: operator-channel, for its conditions alone", m.Uses) + if len(m.Uses) != 0 { + t.Fatalf("uses %v", m.Uses) } if !reflect.DeepEqual(m.Invokes, want) { t.Fatalf("invokes %v, wanted %v", m.Invokes, want) diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled.go index dcb2695b2..e27c34bc0 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecalled.go @@ -73,6 +73,10 @@ func (h *Holder) nameRevision(branch, call string, heads []revisionHead) (named if err != nil { return Source{}, false, fmt.Errorf("source.%s cannot be written for the call %s: %w", branch, call, err) } + // The heads the revision names are delivered: their held heads go and their head-untaken clears (§740). + if err := h.sourceNamed(branch, named); err != nil { + return named, wrote, fmt.Errorf("source.%s is at %d and its held heads are not let go: %w", branch, named.Source, err) + } return named, wrote, nil } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/react.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/react.go index 22846e925..aa37b98d1 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/react.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/react.go @@ -55,15 +55,15 @@ func (h *Holder) Work() { } } -// React takes every step the facts allow, after an event that can change what they are: the conditions of what stalled -// (conditions.go), and then the time the next look is wanted at, if any step waits on the clock. Never concurrent with itself. +// React takes every step the facts allow, after an event that can change what they are: the held heads past untaken-bound +// (heads.go), and then the time the next look is wanted at, if any step waits on the clock. Never concurrent with itself. func (h *Holder) React() { h.reactMu.Lock() defer h.reactMu.Unlock() h.wanted.mu.Lock() h.wanted.at, h.wanted.step = time.Time{}, "" h.wanted.mu.Unlock() - h.sayConditions() + h.lookAtHeads() h.arm() } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/settings.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/settings.go index 289049ce0..becf0c8f7 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/settings.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/settings.go @@ -17,8 +17,14 @@ type Settings struct { // RecordRetentionDays is how many days a delivery record is kept from when it opened (design 47 *The delivery // record*); nil keeps DefaultRecordRetention. RecordRetentionDays *int `json:"record-retention"` + // UntakenBoundMinutes is how many minutes a head may stand untaken before head-untaken is raised (design 47 H §740); nil + // keeps DefaultUntakenBound. + UntakenBoundMinutes *int `json:"untaken-bound"` } +// DefaultUntakenBound is untaken-bound when the setting gives none (§740). +const DefaultUntakenBound = 10 * time.Minute + // readSettings reads the settings file; a value that is not a whole number of days, from 1 to 3650, is refused. func readSettings(path string) (Settings, error) { var s Settings @@ -44,6 +50,17 @@ func readSettings(path string) (Settings, error) { default: return Settings{}, errors.New("record-retention is not a number") } + switch v := loose["untaken-bound"].(type) { + case nil: + case float64: + n := int(v) + if v < 1 || v != float64(n) { + return Settings{}, errors.New("untaken-bound is not a whole number of minutes, at least 1") + } + s.UntakenBoundMinutes = &n + default: + return Settings{}, errors.New("untaken-bound is not a number") + } return s, nil } diff --git a/modules/mesh-delivery-gate/files/settings.json b/modules/mesh-delivery-gate/files/settings.json index bff20f51c..63414735c 100644 --- a/modules/mesh-delivery-gate/files/settings.json +++ b/modules/mesh-delivery-gate/files/settings.json @@ -1,3 +1,4 @@ { - "record-retention": 90 + "record-retention": 90, + "untaken-bound": 10 } diff --git a/modules/mesh-delivery-gate/module.json b/modules/mesh-delivery-gate/module.json index e220e13b5..694495e92 100644 --- a/modules/mesh-delivery-gate/module.json +++ b/modules/mesh-delivery-gate/module.json @@ -18,13 +18,11 @@ "consumes": [ "mesh-delivery-forge-bridge.owed.ended", "git.merge-called.*", + "mesh-delivery-forge-bridge.forge.*.*.*.head-moved", "mesh-delivery-indexer.take-in.taken", "mesh-catalog.registration-changed", "mesh-conditions.findings-asked" ], - "uses": [ - "operator-channel" - ], "requires": [ { "name": "records", @@ -261,7 +259,7 @@ ] }, { - "name": "conditions", + "name": "heads", "storage": "key-value", "template": "state" }, @@ -325,6 +323,13 @@ "maximum": 3650, "trusted": false, "default": 90 + }, + "untaken-bound": { + "type": "number", + "description": "minutes a watched branch's head may stand that no merge call delivered before the gate raises head-untaken (design 47 H §740); the gate keeps one deadline timer for the earliest.", + "minimum": 1, + "trusted": false, + "default": 10 } }, "resources": [ @@ -341,7 +346,8 @@ "path": "${dir:state}/settings.json", "mode": "0600", "takes-config": [ - "record-retention" + "record-retention", + "untaken-bound" ], "merge": "json", "template": "files/settings.json" -- 2.54.0 From e534f667da2079964e2461aba4fbe7f2c03773ae Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:18:27 +0100 Subject: [PATCH 16/43] GATE-7: gate_heads reads the untaken heads; gate_head_take takes one in by hand MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gate_heads reads the state heads with each head's since and finding; gate_head_take, recorded in the hand-act log first, names the held head a source revision as a merge call does, with a call of its own, its record marked by hand with who and when and its member @, and the finding clears. Builds: design 47 H §742; H-A-24 (hand, by and the time in the trace), H-A-48 (a hand take's member @) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/mesh-delivery-gate/heads_test.go | 36 ++++++ .../cmd/mesh-delivery-gate/headtools.go | 119 ++++++++++++++++++ .../cmd/mesh-delivery-gate/main.go | 1 + modules/mesh-delivery-gate/module.json | 2 + 4 files changed, 158 insertions(+) create mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/headtools.go diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads_test.go index 714052180..314ae747e 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads_test.go @@ -3,6 +3,7 @@ package main import ( "context" "encoding/json" + "errors" "os" "strings" "testing" @@ -115,6 +116,41 @@ func TestAHeadNoMergeCallDeliveredIsAnUntakenHead(t *testing.T) { t.Fatalf("after the merge call: held %+v, findings %+v", heads, fw.held) } }) + // GATE-7 (§742): gate_heads reads each held head with since and finding; gate_head_take writes one revision of its own + // call, opens its record marked by hand, writes the hand-act log first and clears the finding. + t.Run("gate_heads reads the held heads; gate_head_take takes one in after the log and clears head-untaken", func(t *testing.T) { + h, heads, fw, now := setup() + h.Records = &Records{Ledger: newLedgerFake(t), Now: h.Now} + if err := h.HeadMoved(moved(c2)); err != nil { + t.Fatal(err) + } + *now = at.Add(DefaultUntakenBound) + h.React() + out, err := h.HeadsHeld() + lines, _ := out["heads"].([]HeadLine) + if err != nil || len(lines) != 1 || lines[0].Repository != "novox/app" || lines[0].Branch != "main" || lines[0].Head != c2 || + !strings.Contains(lines[0].Finding, WordsUntaken) || lines[0].Since == "" { + t.Fatalf("gate_heads %+v %v", out, err) + } + acts := &handActsFake{refuse: errors.New("log down")} + if _, err := h.HeadTake("novox/app", "main", "pushed by hand", "lead@shanks", acts); err == nil || len(heads) != 1 { + t.Fatalf("taken without the log: %v", err) + } + acts.refuse = nil + if _, err := h.HeadTake("novox/other", "main", "x", "lead@shanks", acts); err == nil { + t.Fatal("a head the gate does not hold is taken") + } + got, err := h.HeadTake("novox/app", "main", "pushed by hand", "lead@shanks", acts) + if err != nil || got["source"] != int64(5) || len(acts.took) != 1 || acts.took[0].Cause != "gate_head_take" { + t.Fatalf("take %+v %v, log %+v", got, err, acts.took) + } + src, _, _ := h.Source.Read("main") + kept, _ := h.Records.Kept() + if src.Heads["novox/app"] != c2 || len(heads) != 0 || len(fw.held) != 0 || len(kept) != 1 || kept[0].Trace == nil || + !kept[0].Trace.Hand || kept[0].Trace.By != "lead@shanks" || kept[0].Heads["novox/app@222222222222"] != c2 { + t.Fatalf("source %+v, held %+v, findings %+v, records %+v", src, heads, fw.held, kept) + } + }) t.Run("the setting untaken-bound, at least one minute", func(t *testing.T) { h, _, _, _ := setup() n := 3 diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/headtools.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/headtools.go new file mode 100644 index 000000000..9af28caae --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/headtools.go @@ -0,0 +1,119 @@ +package main + +// Taking an untaken head in by hand (design 47 H §742; ADR 0682 §3; H-A-24, H-A-48): gate_heads reads the state heads with +// each head's since and finding; gate_head_take, with repository, branch and why, written in the hand-act log first, names +// that head a source revision as a merge call does (§737), its call one of its own and its trace marked by hand with who and +// when, its record's member `@`, and the chain follows. Nothing else delivers an untaken head. + +import ( + "errors" + "fmt" + "sort" + "strings" + + "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" + stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/keys" +) + +// HeadLine is one held head as gate_heads reads it. +type HeadLine struct { + Repository string `json:"repository"` + Branch string `json:"branch"` + Head string `json:"head"` + Since string `json:"since"` + // Finding is head-untaken's summary when it is raised. + Finding string `json:"finding,omitempty"` +} + +// HeadsHeld is gate_heads: every held head, ordered by key. +func (h *Holder) HeadsHeld() (map[string]any, error) { + if h.Heads == nil { + return nil, errors.New("this gate watches no heads") + } + held, err := h.Heads.All() + if err != nil { + return nil, err + } + raised := map[string]string{} + if h.Findings != nil { + for _, f := range h.Findings.Held() { + if f.Scope == FindingScope && f.Kind == HeadUntaken { + raised[f.ID] = f.Summary + } + } + } + lines := []HeadLine{} + for key, v := range held { + repository, escaped, ok := seat.ParseHeadsByRepositoryBranchKey(key) + branch, err := keys.UnescapeName(escaped) + if !ok || err != nil { + continue + } + lines = append(lines, HeadLine{Repository: repository, Branch: branch, Head: v.Head, Since: timeText(&v.Since), Finding: raised[key]}) + } + sort.Slice(lines, func(i, j int) bool { return headKey(lines[i].Repository, lines[i].Branch) < headKey(lines[j].Repository, lines[j].Branch) }) + return map[string]any{"heads": lines}, nil +} + +// HeadTake is gate_head_take: the held head of a repository on a branch named one source revision, after the hand-act log took it. +func (h *Holder) HeadTake(repository, branch, why, by string, acts handActs) (map[string]any, error) { + if strings.TrimSpace(repository) == "" || strings.TrimSpace(branch) == "" || strings.TrimSpace(why) == "" { + return nil, errors.New("say the repository, the branch and why its head is taken in by hand") + } + if h.Heads == nil || h.Source == nil { + return nil, errors.New("this gate watches no heads or keeps no source") + } + held, err := h.Heads.Get(headKey(repository, branch)) + if err != nil { + return nil, err + } + if held == nil { + return nil, fmt.Errorf("the gate holds no untaken head of %s on %s: gate_heads lists them", repository, branch) + } + if acts == nil { + return nil, errors.New("the hand-act log cannot be reached here, so nothing was taken in") + } + at := h.Now().UTC() + if _, err := acts.Record(meshhandactsclient.Request{What: fmt.Sprintf("took the head %s of %s on %s in by hand (hand by %s at %s)", + shortOf(held.Head, 12), repository, branch, by, at.Format("2006-01-02T15:04:05Z")), Why: why, Cause: "gate_head_take", + Verb: "head-take"}); err != nil { + return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was taken in: %w", err) + } + call, err := newCall() + if err != nil { + return nil, err + } + member := IDOf(repository, held.Head) + trace := RecordTrace{Call: call, By: by, Hand: true, At: at, Pulls: []string{member}, Commits: []string{held.Head}} + if err := h.deliverRevision(branch, call, []revisionHead{{Repository: repository, Commit: held.Head}}, + map[string]string{member: held.Head}, trace); err != nil { + return nil, err + } + named, _, err := h.Source.Read(branch) + if err != nil || named == nil { + return nil, fmt.Errorf("the head is taken in and source.%s cannot be read back: %v", branch, err) + } + return map[string]any{"repository": repository, "branch": branch, "head": held.Head, "source": named.Source, "call": call, "by": by}, nil +} + +// headTools are the watchdog's tools; acts is the hand-act log. +func headTools(h *Holder, acts handActs) []stdio.Tool { + return []stdio.Tool{ + {Name: "gate_heads", + Description: "Every watched branch's head the gate holds as untaken (design 47 H §740, §742): moved, and named by no source " + + "revision yet, each with its repository, branch, head, since when, and head-untaken's summary once raised. Read, never written.", + Run: func(map[string]any) (any, error) { return h.HeadsHeld() }}, + {Name: "gate_head_take", + Description: "Take one untaken head in by hand (design 47 H §742): the head the gate holds for the repository on the branch is " + + "named one source revision as a merge call does, with its record, and the chain follows; recorded in the hand-act log of " + + "the seat mesh-hand-acts first. Nothing else delivers a head moved outside the mesh.", + Input: map[string]any{"type": "object", "properties": map[string]any{"repository": str("owner/repository"), + "branch": str("the watched branch"), "why": str("why it is taken in by hand, in plain words")}, + "required": []string{"repository", "branch", "why"}}, + Run: func(a map[string]any) (any, error) { + return h.HeadTake(strArg(a, "repository"), strArg(a, "branch"), strArg(a, "why"), stdio.CallerOf(a), acts) + }}, + } +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go index ed8d1c7cb..62f8c4d8b 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go @@ -249,6 +249,7 @@ func tools(h *Holder, l *listening) []stdio.Tool { } list = append(list, recordTools(h, meshhandactsclient.Recorder{Ask: stdio.Ask})...) list = append(list, sourceTools(h, meshhandactsclient.Recorder{Ask: stdio.Ask})...) + list = append(list, headTools(h, meshhandactsclient.Recorder{Ask: stdio.Ask})...) // A verb that changes a delivery asks for a look at what it makes possible (react.go). for i := range list { run := list[i].Run diff --git a/modules/mesh-delivery-gate/module.json b/modules/mesh-delivery-gate/module.json index 694495e92..0351d0b12 100644 --- a/modules/mesh-delivery-gate/module.json +++ b/modules/mesh-delivery-gate/module.json @@ -306,6 +306,8 @@ "tools": [ "delivery_status", "gate_consumer", + "gate_head_take", + "gate_heads", "gate_record", "gate_record_end", "gate_record_repair", -- 2.54.0 From 6cde12bf2fc8404797f46bac84e58a4a6d04d0fd Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:19:16 +0100 Subject: [PATCH 17/43] BUILD-1: the build modules wire no asker judgement of the step runner MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The three build modules no longer give the step runner a callers reader of the seat list (steprun.SeatListCallers and Puller.Callers go): no seat definition names callers and every account may call every verb. Builds: design 47 H §752, §764 (every seat contract: the callers lists); H-A-41, H-A-66, H-A-71 (3) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-build-go/cmd/mesh-build-go/asks.go | 5 +---- modules/mesh-build-go/cmd/mesh-build-go/asks_say_test.go | 4 ---- modules/mesh-build-go/cmd/mesh-build-go/module.go | 3 --- modules/mesh-build-image/cmd/mesh-build-image/asks.go | 5 +---- .../mesh-build-image/cmd/mesh-build-image/asks_say_test.go | 4 ---- modules/mesh-build-image/cmd/mesh-build-image/module.go | 3 --- modules/mesh-build-node/cmd/mesh-build-node/asks.go | 5 +---- modules/mesh-build-node/cmd/mesh-build-node/asks_say_test.go | 4 ---- modules/mesh-build-node/cmd/mesh-build-node/module.go | 3 --- 9 files changed, 3 insertions(+), 33 deletions(-) diff --git a/modules/mesh-build-go/cmd/mesh-build-go/asks.go b/modules/mesh-build-go/cmd/mesh-build-go/asks.go index 6b9093461..7d8315498 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/asks.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/asks.go @@ -57,9 +57,6 @@ func (m *Module) askSteps(steps []StepTool) steprun.AskSteps { func (m *Module) Puller(steps []StepTool, source steprun.AskSource) *steprun.Puller { if source == nil { source = &steprun.SeatAsks{Language: sdkstep.AskGo, Logf: m.Say} - if m.Callers == nil { - m.Callers = steprun.SeatListCallers(sdkstep.AskGo, m.Say) - } } capacity := m.Capacity // The capacity's own change wakes the puller (issue 1887): the runtime's watch of the config value as mesh-config @@ -91,7 +88,7 @@ func (m *Module) Puller(steps []StepTool, source steprun.AskSource) *steprun.Pul if m.SayEvent != nil { say = steprun.SayOn(sdkstep.AskGo, m.Node, m.SayEvent) } - return &steprun.Puller{KeepOutput: steprun.KeepOutputIn(m.Outputs), Source: source, Language: sdkstep.AskGo, Say: say, Callers: m.Callers, Prior: steprun.PriorRuns(m.ReadSteps.Get), Node: m.Node, Put: m.Steps, PausePut: m.Pauses, Load: load, ForgetPause: m.ForgetPause, WatchPauses: m.WatchPauses, PauseKeys: m.ReadPauses.Keys, + return &steprun.Puller{KeepOutput: steprun.KeepOutputIn(m.Outputs), Source: source, Language: sdkstep.AskGo, Say: say, Prior: steprun.PriorRuns(m.ReadSteps.Get), Node: m.Node, Put: m.Steps, PausePut: m.Pauses, Load: load, ForgetPause: m.ForgetPause, WatchPauses: m.WatchPauses, PauseKeys: m.ReadPauses.Keys, Capacity: capacity, SettingsFile: settings, WatchConfig: watch, CoresPerStep: cores, BuildHours: hours, Logf: m.Say, Has: m.Has, Run: func(ctx context.Context, a sdkstep.StepAsk) (any, error) { if err := m.Check(ctx); err != nil { diff --git a/modules/mesh-build-go/cmd/mesh-build-go/asks_say_test.go b/modules/mesh-build-go/cmd/mesh-build-go/asks_say_test.go index f10e55ab4..2c8f1ccdd 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/asks_say_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/asks_say_test.go @@ -11,15 +11,11 @@ import ( func TestPullerSaysTheSeatsEventsOnItsNodesSubject(t *testing.T) { var subjects []string m := &Module{Node: "ace", Capacity: func() int { return 0 }, - Callers: func(kind string) ([]string, bool) { return []string{"mesh-pipeline-runner"}, kind == "step" }, SayEvent: func(subject string, _ any, _ string) error { subjects = append(subjects, subject); return nil }} p := m.Puller(nil, steprun.NewFakeAsks()) if p.Say == nil { t.Fatal("the puller says no event") } - if got, ok := p.Callers("step"); !ok || len(got) != 1 { - t.Fatalf("the puller checks the callers of %v, %v, not the module's", got, ok) - } seat, _ := sdkstep.BuildSeat(sdkstep.AskGo) for _, event := range []string{"ask-taken", "step-progress", "step-ended"} { if err := p.Say(event, nil, "id"); err != nil { diff --git a/modules/mesh-build-go/cmd/mesh-build-go/module.go b/modules/mesh-build-go/cmd/mesh-build-go/module.go index 38329cb9a..439bb08bf 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/module.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/module.go @@ -25,9 +25,6 @@ const CheckRound = 10 * time.Minute // Module is the Go build module's one running state: the node it runs on, its recipe, the toolchain guard and the // states it writes (toolchains, steps). type Module struct { - // Callers says which seats the build seat's definition lists as callers of an accepted kind (steprun.SeatListCallers when - // the module pulls through its runtime; nil in a test checks no named asker): design 47 G §543, A-163. - Callers func(kind string) ([]string, bool) // SayEvent publishes an event of the go build seat on a seat subject (stdio.SeatPublish; nil says none, a test's): the // puller's ask-taken, step-progress and step-ended (design 47 G §496, §602). SayEvent func(subject string, body any, id string) error diff --git a/modules/mesh-build-image/cmd/mesh-build-image/asks.go b/modules/mesh-build-image/cmd/mesh-build-image/asks.go index 761209beb..30ade441c 100644 --- a/modules/mesh-build-image/cmd/mesh-build-image/asks.go +++ b/modules/mesh-build-image/cmd/mesh-build-image/asks.go @@ -49,9 +49,6 @@ func (m *Module) sayStep() func(sdkstep.StepEnded) error { func (m *Module) Puller(steps []StepTool, source steprun.AskSource) *steprun.Puller { if source == nil { source = &steprun.SeatAsks{Language: sdkstep.AskImage, Logf: m.Say} - if m.Callers == nil { - m.Callers = steprun.SeatListCallers(sdkstep.AskImage, m.Say) - } } capacity := m.Capacity // The capacity's own change wakes the puller (issue 1887): the runtime's watch of the config value as mesh-config @@ -80,7 +77,7 @@ func (m *Module) Puller(steps []StepTool, source steprun.AskSource) *steprun.Pul if m.SayEvent != nil { say = steprun.SayOn(sdkstep.AskImage, m.Node, m.SayEvent) } - return &steprun.Puller{KeepOutput: steprun.KeepOutputIn(m.Outputs), Source: source, Language: sdkstep.AskImage, Say: say, Callers: m.Callers, Prior: steprun.PriorRuns(m.ReadSteps.Get), Node: m.Node, Put: m.Steps, PausePut: m.Pauses, Load: load, ForgetPause: m.ForgetPause, WatchPauses: m.WatchPauses, PauseKeys: m.ReadPauses.Keys, + return &steprun.Puller{KeepOutput: steprun.KeepOutputIn(m.Outputs), Source: source, Language: sdkstep.AskImage, Say: say, Prior: steprun.PriorRuns(m.ReadSteps.Get), Node: m.Node, Put: m.Steps, PausePut: m.Pauses, Load: load, ForgetPause: m.ForgetPause, WatchPauses: m.WatchPauses, PauseKeys: m.ReadPauses.Keys, Capacity: capacity, SettingsFile: settings, WatchConfig: watch, CoresPerStep: cores, BuildHours: hours, Eligible: m.Eligible, Logf: m.Say, Run: func(ctx context.Context, a sdkstep.StepAsk) (any, error) { if err := m.Check(ctx); err != nil { diff --git a/modules/mesh-build-image/cmd/mesh-build-image/asks_say_test.go b/modules/mesh-build-image/cmd/mesh-build-image/asks_say_test.go index dd42d4a9a..afe9e94e5 100644 --- a/modules/mesh-build-image/cmd/mesh-build-image/asks_say_test.go +++ b/modules/mesh-build-image/cmd/mesh-build-image/asks_say_test.go @@ -11,15 +11,11 @@ import ( func TestPullerSaysTheSeatsEventsOnItsNodesSubject(t *testing.T) { var subjects []string m := &Module{Node: "ace", Capacity: func() int { return 0 }, - Callers: func(kind string) ([]string, bool) { return []string{"mesh-pipeline-runner"}, kind == "step" }, SayEvent: func(subject string, _ any, _ string) error { subjects = append(subjects, subject); return nil }} p := m.Puller(nil, steprun.NewFakeAsks()) if p.Say == nil { t.Fatal("the puller says no event") } - if got, ok := p.Callers("step"); !ok || len(got) != 1 { - t.Fatalf("the puller checks the callers of %v, %v, not the module's", got, ok) - } seat, _ := sdkstep.BuildSeat(sdkstep.AskImage) for _, event := range []string{"ask-taken", "step-progress", "step-ended"} { if err := p.Say(event, nil, "id"); err != nil { diff --git a/modules/mesh-build-image/cmd/mesh-build-image/module.go b/modules/mesh-build-image/cmd/mesh-build-image/module.go index 87b2eca49..a5679adca 100644 --- a/modules/mesh-build-image/cmd/mesh-build-image/module.go +++ b/modules/mesh-build-image/cmd/mesh-build-image/module.go @@ -20,9 +20,6 @@ const CheckRound = 10 * time.Minute // Module is the image build module's one running state. type Module struct { - // Callers says which seats the build seat's definition lists as callers of an accepted kind (steprun.SeatListCallers when - // the module pulls through its runtime; nil in a test checks no named asker): design 47 G §543, A-163. - Callers func(kind string) ([]string, bool) // Outputs is the object store of a run's answers over what step-ended carries inline (steprun.OutputsStore); nil keeps none. Outputs steprun.OutputObjects // SayEvent publishes an event of the image build seat on a seat subject (stdio.SeatPublish; nil says none, a test's): the diff --git a/modules/mesh-build-node/cmd/mesh-build-node/asks.go b/modules/mesh-build-node/cmd/mesh-build-node/asks.go index 34a1d46c7..dc864e5e6 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/asks.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/asks.go @@ -61,9 +61,6 @@ func (m *Module) sayStep() func(sdkstep.StepEnded) error { func (m *Module) Puller(steps []StepTool, source steprun.AskSource) *steprun.Puller { if source == nil { source = &steprun.SeatAsks{Language: sdkstep.AskNode, Logf: m.Say} - if m.Callers == nil { - m.Callers = steprun.SeatListCallers(sdkstep.AskNode, m.Say) - } } capacity := m.Capacity // The capacity's own change wakes the puller (issue 1887): the runtime's watch of the config value as mesh-config @@ -92,7 +89,7 @@ func (m *Module) Puller(steps []StepTool, source steprun.AskSource) *steprun.Pul if m.SayEvent != nil { say = steprun.SayOn(sdkstep.AskNode, m.Node, m.SayEvent) } - return &steprun.Puller{KeepOutput: steprun.KeepOutputIn(m.Outputs), Source: source, Language: sdkstep.AskNode, Say: say, Callers: m.Callers, Prior: steprun.PriorRuns(m.ReadSteps.Get), Node: m.Node, Put: m.Steps, PausePut: m.Pauses, Load: load, ForgetPause: m.ForgetPause, WatchPauses: m.WatchPauses, PauseKeys: m.ReadPauses.Keys, + return &steprun.Puller{KeepOutput: steprun.KeepOutputIn(m.Outputs), Source: source, Language: sdkstep.AskNode, Say: say, Prior: steprun.PriorRuns(m.ReadSteps.Get), Node: m.Node, Put: m.Steps, PausePut: m.Pauses, Load: load, ForgetPause: m.ForgetPause, WatchPauses: m.WatchPauses, PauseKeys: m.ReadPauses.Keys, Capacity: capacity, SettingsFile: settings, WatchConfig: watch, CoresPerStep: cores, BuildHours: hours, Logf: m.Say, Run: func(ctx context.Context, a sdkstep.StepAsk) (any, error) { if err := m.Prepare(ctx); err != nil { diff --git a/modules/mesh-build-node/cmd/mesh-build-node/asks_say_test.go b/modules/mesh-build-node/cmd/mesh-build-node/asks_say_test.go index 3ab0b2618..5869cb558 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/asks_say_test.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/asks_say_test.go @@ -11,15 +11,11 @@ import ( func TestPullerSaysTheSeatsEventsOnItsNodesSubject(t *testing.T) { var subjects []string m := &Module{Node: "ace", Capacity: func() int { return 0 }, - Callers: func(kind string) ([]string, bool) { return []string{"mesh-pipeline-runner"}, kind == "step" }, SayEvent: func(subject string, _ any, _ string) error { subjects = append(subjects, subject); return nil }} p := m.Puller(nil, steprun.NewFakeAsks()) if p.Say == nil { t.Fatal("the puller says no event") } - if got, ok := p.Callers("step"); !ok || len(got) != 1 { - t.Fatalf("the puller checks the callers of %v, %v, not the module's", got, ok) - } seat, _ := sdkstep.BuildSeat(sdkstep.AskNode) for _, event := range []string{"ask-taken", "step-progress", "step-ended"} { if err := p.Say(event, nil, "id"); err != nil { diff --git a/modules/mesh-build-node/cmd/mesh-build-node/module.go b/modules/mesh-build-node/cmd/mesh-build-node/module.go index 4b83bc294..ac30b5810 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/module.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/module.go @@ -25,9 +25,6 @@ const CheckRound = 10 * time.Minute // Module is the Node build module's one running state: the node it runs on, its recipe, the toolchain guard, the // toolchain directory and the states it writes (toolchains, steps). type Module struct { - // Callers says which seats the build seat's definition lists as callers of an accepted kind (steprun.SeatListCallers when - // the module pulls through its runtime; nil in a test checks no named asker): design 47 G §543, A-163. - Callers func(kind string) ([]string, bool) // Outputs is the object store of a run's answers over what step-ended carries inline (steprun.OutputsStore); nil keeps none. Outputs steprun.OutputObjects // SayEvent publishes an event of the node build seat on a seat subject (stdio.SeatPublish; nil says none, a test's): the -- 2.54.0 From 2f471edc0fcc77f4caacb307a827d868a3e974c7 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:22:22 +0100 Subject: [PATCH 18/43] GATE-6, GATE-7: gofmt over the watchdog and its tools MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The repository's gofmt check refused heads.go, headtools.go and main.go as written; a stale comment of the stalled conditions goes with them. Builds: design 47 H §740, §742 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads.go | 2 +- .../mesh-delivery-gate/cmd/mesh-delivery-gate/headtools.go | 4 +++- modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go | 1 - 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads.go index 1eaa9656b..7e400fd1d 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/heads.go @@ -16,8 +16,8 @@ import ( "time" "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/keys" ) diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/headtools.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/headtools.go index 9af28caae..3e26a3d01 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/headtools.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/headtools.go @@ -53,7 +53,9 @@ func (h *Holder) HeadsHeld() (map[string]any, error) { } lines = append(lines, HeadLine{Repository: repository, Branch: branch, Head: v.Head, Since: timeText(&v.Since), Finding: raised[key]}) } - sort.Slice(lines, func(i, j int) bool { return headKey(lines[i].Repository, lines[i].Branch) < headKey(lines[j].Repository, lines[j].Branch) }) + sort.Slice(lines, func(i, j int) bool { + return headKey(lines[i].Repository, lines[i].Branch) < headKey(lines[j].Repository, lines[j].Branch) + }) return map[string]any{"heads": lines}, nil } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go index 62f8c4d8b..3cf879c7c 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go @@ -56,7 +56,6 @@ func main() { Logf: logf, Source: kvSource{}, Owed: seatOwed{say: stdio.SeatPublish}, - // The gate says what stalled to the operator itself (issue 1275, ADR 0437 §9). Heads: kvHeads{}, } h.init() -- 2.54.0 From 57984e9b98b141dca259b18f50051c2d161526c2 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:33:56 +0100 Subject: [PATCH 19/43] GATE-5: the delivery record follows its pipeline to the machines MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The record a merge call opened takes the runner's pipeline-started, pipeline-ended, wait-held and wait-ended, the assigner's wanted and pipeline-wanted and the node-engine seat's running, read by its key, and ends on pipeline-wanted (running: every tier runs; tier-late: failed) or failed on a pipeline that did not pass. Builds: design 47 H §738 (G §141 to §147); H-A-29, H-A-75, H-A-89 (1) to (5) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/mesh-delivery-gate/consumer.go | 42 ++- .../cmd/mesh-delivery-gate/main.go | 3 +- .../cmd/mesh-delivery-gate/manifest_test.go | 8 +- .../cmd/mesh-delivery-gate/record.go | 21 +- .../cmd/mesh-delivery-gate/recordpipeline.go | 308 ++++++++++++++++++ .../mesh-delivery-gate/recordpipeline_test.go | 130 ++++++++ modules/mesh-delivery-gate/go.mod | 4 +- modules/mesh-delivery-gate/module.json | 7 + 8 files changed, 511 insertions(+), 12 deletions(-) create mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordpipeline.go create mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordpipeline_test.go diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go index 62bd2b339..f61195ae0 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go @@ -18,7 +18,10 @@ import ( "sync" "time" + nodeengine "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + runner "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" + wants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-indexer/takein" stdio "git.novox.be/novox/mesh-sdk/go" ) @@ -45,7 +48,9 @@ type stepFamily struct { // Event is the event's name after its owner; the node is the token after it when NodeScoped. Event string NodeScoped bool - Decode func(body json.RawMessage, at time.Time) ([]StepOutcome, error) + // Kind is the event's name on its owner's seat, with no subject tokens: what Follow is told it is. + Kind string + Decode func(body json.RawMessage, at time.Time) ([]StepOutcome, error) } // The step events on main the record is built from. A family is added here, with its `consumes` line, when its owner @@ -54,13 +59,34 @@ var stepFamilies = []stepFamily{ // An entry taken in (the holder of the seat mesh-catalog's own `take-in.taken`, ADR 0633 §1): the merges a source // revision took in. Its owner is the one the manifest consumes it from. family(takein.TakeInTakenEvent, decodeTakenIn), + // The record follows its pipeline to the machines (design 47 H §738, H-A-29, H-A-75, H-A-89; recordpipeline.go): the + // runner's pipeline-started, pipeline-ended, wait-held and wait-ended on ., the assigner's wanted on + // . and pipeline-wanted on ., and the node-engine seat's running on .. + seatFamily(runner.EventPipelineStarted, 2, false, decodePipelineStarted), + seatFamily(runner.EventPipelineEnded, 2, false, decodePipelineEnded), + seatFamily(runner.EventWaitHeld, 2, false, decodeWait), + seatFamily(runner.EventWaitEnded, 2, false, decodeWait), + seatFamily(wants.EventWanted, 2, false, decodeWanted), + seatFamily(wants.EventPipelineWanted, 2, false, decodePipelineWanted), + seatFamily(nodeengine.EventRunning, 2, true, decodeRunning), +} + +// seatFamily is a step family of a seat's event its manifest consumes with each token of its subject a wildcard +// (`..*.*`); of a node's event, the node is the first token after the event. +func seatFamily(event string, tokens int, nodeScoped bool, decode func(json.RawMessage, time.Time) ([]StepOutcome, error)) stepFamily { + f := family(event+strings.Repeat(".*", tokens), decode) + f.Kind = event + if nodeScoped { + f.Event, f.NodeScoped = event, true + } + return f } // family is a step family of an event as the manifest consumes it: `.`. func family(event string, decode func(json.RawMessage, time.Time) ([]StepOutcome, error)) stepFamily { pattern := declared(event) owner, _, _ := strings.Cut(pattern, ".") - return stepFamily{Pattern: pattern, Owner: owner, Event: event, Decode: decode} + return stepFamily{Pattern: pattern, Owner: owner, Event: event, Kind: event, Decode: decode} } // consumed is what the gate's manifest consumes (its own manifest): every event it follows is one of them. @@ -115,6 +141,9 @@ type StepConsumer struct { Logf func(format string, a ...any) // Taken writes a take-in.taken into the record of its source revision (takenin.go). Taken func(body json.RawMessage) error + // Follow writes an event of the record's pipeline families into the record of its source revision, told the event's + // kind (recordpipeline.go). + Follow func(kind string, body json.RawMessage) error mu sync.Mutex // heard counts, per family, the events applied, refused and the last applied, since this owner started. @@ -179,6 +208,10 @@ func (c *StepConsumer) Hear(e stdio.Envelope) error { if err := c.Taken(e.Body); err != nil { return fmt.Errorf("the record of %s is not opened yet: %w", e.Key, err) } + } else if f.Event != takein.TakeInTakenEvent && c.Follow != nil { + if err := c.Follow(f.Kind, e.Body); err != nil { + return fmt.Errorf("the record of %s is not written yet: %w", e.Key, err) + } } id := e.Headers["x-event-id"] for _, o := range outcomes { @@ -216,7 +249,7 @@ func ownerRefusal(f stepFamily, e stdio.Envelope) string { // apply writes an outcome into every record that holds it, by its merge commits or by its entry: an open record, or one // that ended without the node the outcome comes from (a node away that wakes adds its outcomes, and is no longer shown -// away). An open record whose present nodes then all run their entries ends. +// away). It ends no record: the assigner's pipeline-wanted does (design 47 H §738, H-A-89 (4)). func (c *StepConsumer) apply(o StepOutcome, event string) error { records, err := c.Records.Kept() if err != nil { @@ -238,9 +271,6 @@ func (c *StepConsumer) apply(o StepOutcome, event string) error { return err } } - if _, err := c.Records.EndIfRun(r); err != nil { - return err - } } return nil } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go index 3cf879c7c..546065b58 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/main.go @@ -64,7 +64,8 @@ func main() { h.Records.Changed = h.recordChanged h.Forge = seatGit{ask: stdio.Ask} h.Catalog = seatCatalog{ask: stdio.Ask} - h.Consumer = &StepConsumer{Records: h.Records, Families: stepFamilies, Now: h.Now, Logf: logf, Taken: h.TakenIn} + h.Consumer = &StepConsumer{Records: h.Records, Families: stepFamilies, Now: h.Now, Logf: logf, Taken: h.TakenIn, + Follow: h.Follow} // The gate's one held writer of its own findings (design 47 G A-39, A-37, A-40): made at once, started once the runtime serves. fw, err := NewFindingWriter() if err != nil { diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go index 9dce11fa2..90c0d1751 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/manifest_test.go @@ -117,10 +117,14 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { } // The names move (milestone 47/G): the families heard from another module are its manifest's consumes, and no Go // source of the gate names those modules. - if !slices.Contains(m.Consumes, owedEndedEvent) || owedEndedEvent == "" || len(stepFamilies) != 1 || - !slices.Contains(m.Consumes, stepFamilies[0].Pattern) || stepFamilies[0].Owner+"."+stepFamilies[0].Event != stepFamilies[0].Pattern { + if !slices.Contains(m.Consumes, owedEndedEvent) || owedEndedEvent == "" || len(stepFamilies) != 8 { t.Fatalf("owed.ended is %q and the step families %+v, not the manifest's consumes %v", owedEndedEvent, stepFamilies, m.Consumes) } + for _, f := range stepFamilies { + if f.Pattern == "" || !slices.Contains(m.Consumes, f.Pattern) || !strings.HasPrefix(f.Pattern, f.Owner+"."+f.Kind) { + t.Errorf("the step family %+v is not one the manifest consumes %v", f, m.Consumes) + } + } sources, _ := filepath.Glob("*.go") for _, f := range sources { if strings.HasSuffix(f, "_test.go") { diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go index b5d481263..9a1c5da6a 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go @@ -527,11 +527,28 @@ func (rs *Records) Open(o RecordOpening) (r *Record, opened bool, err error) { // ByKey is the record of a source revision, read by its key `.` (H-A-29); nil when none is kept. func (rs *Records) ByKey(branch string, source int64) (*Record, error) { - inst, err := RecordInstance(recordMilestone(branch, source)) + return rs.ByMilestone(recordMilestone(branch, source)) +} + +// ByMilestone is the record of a key `.` (a pipeline's key of its source revision, H-A-89 (5)): the one +// the gate keeps as it writes it when it holds it, so a write through it is seen by every reader, else read from the +// ledger; nil when none is kept. +func (rs *Records) ByMilestone(milestone string) (*Record, error) { + inst, err := RecordInstance(milestone) if err != nil { return nil, err } - return rs.Read(inst) + rs.mu.Lock() + held := rs.held[inst] + rs.mu.Unlock() + if held != nil { + return held, nil + } + r, err := rs.Read(inst) + if r != nil { + rs.keep(r) + } + return r, err } // sayOpened says record-opened on record-opened., so the session that called waits on its own call (§737). Said under diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordpipeline.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordpipeline.go new file mode 100644 index 000000000..be806d965 --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordpipeline.go @@ -0,0 +1,308 @@ +package main + +// A delivery record follows its pipeline to the machines (design 47 H §738; G §141 to §147; H-A-29, H-A-75, H-A-89). +// The record a merge call opened (§737), read by its key `.`, takes the runner's `pipeline-started` and +// `pipeline-ended` of its source revision and of the pipelines a package's publish starts from it (§747), the runner's +// `wait-held` and `wait-ended`, the assigner's `wanted` and `pipeline-wanted`, and the node-engine seat's `running` +// (consumer.go), on the gate's own durable consumer: +// +// - `pipeline-started`: each mover is an entry (module, fingerprint, no node); +// - `pipeline-ended`: each mover's step result is one outcome of its entry, its step the result's step, its outcome the +// result's own word, its why the result's (H-A-89 (1)); a pipeline that did not pass ends the record failed, naming +// the pipeline's why (H-A-89 (3)); +// - `wait-held`: the waiting step's outcome is held, with what it waits on; `wait-ended` writes over it the wait's how +// (H-A-89 (2)); +// - `wanted`: each want is an entry (module, fingerprint, node) with the outcome wanted, and the trace's from and to are +// the lowest and the highest assignment graph revision of the record's wants; a removed want writes nothing +// (H-A-89 (5)); +// - `running`: a healthy running is the running-healthy outcome of every entry it is (consumer.go apply), and ends +// nothing (H-A-89 (4)); +// - `pipeline-wanted`: `running` ends the record, every tier's movers running on every present placing machine; +// `tier-late` ends it failed, naming the movers and machines not running (H-A-75, H-A-89 (4); §751). +// +// A pipeline of a run on request delivers nothing, so its events write nothing. An event of a source revision with no +// record (a seed's or a start's, H-A-47), or of a record ended, writes nothing; each write is safe to repeat. + +import ( + "bytes" + "encoding/json" + "fmt" + "sort" + "strconv" + "strings" + "time" + + nodeengine "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" + runner "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" + wants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" +) + +// WhyFailed opens the why of a record that ended failed (H-A-89 (3), (4)). +const WhyFailed = "failed: " + +// strictly reads an event's body in its owner's form, refusing a field it does not know. +func strictly(body json.RawMessage, into any) error { + dec := json.NewDecoder(bytes.NewReader(body)) + dec.DisallowUnknownFields() + return dec.Decode(into) +} + +func decodePipelineStarted(body json.RawMessage, _ time.Time) ([]StepOutcome, error) { + var p runner.PipelineStartedEvent + if err := strictly(body, &p); err != nil { + return nil, err + } + return nil, namesSource(p.Branch, p.Source, p.Run) +} + +func decodePipelineEnded(body json.RawMessage, _ time.Time) ([]StepOutcome, error) { + var p runner.PipelineEndedEvent + if err := strictly(body, &p); err != nil { + return nil, err + } + return nil, namesSource(p.Branch, p.Source, p.Run) +} + +func decodeWait(body json.RawMessage, _ time.Time) ([]StepOutcome, error) { + var w runner.WaitHeldEvent + if err := strictly(body, &w); err != nil { + return nil, err + } + if w.Step == "" { + return nil, fmt.Errorf("a wait names no step") + } + return nil, namesSource(w.Branch, w.Source, w.Run) +} + +func decodeWanted(body json.RawMessage, _ time.Time) ([]StepOutcome, error) { + var w wants.WantedEvent + if err := strictly(body, &w); err != nil { + return nil, err + } + if w.Module == "" || w.Node == "" { + return nil, fmt.Errorf("a wanted names no module or node") + } + return nil, nil +} + +func decodePipelineWanted(body json.RawMessage, _ time.Time) ([]StepOutcome, error) { + var p wants.PipelineWantedEvent + if err := strictly(body, &p); err != nil { + return nil, err + } + if p.Outcome != wants.PipelineWantedOutcomeRunning && p.Outcome != wants.PipelineWantedOutcomeTierLate { + return nil, fmt.Errorf("a pipeline-wanted says the outcome %q", p.Outcome) + } + return nil, namesSource(p.Branch, p.Source, "") +} + +// decodeRunning is a healthy running's outcome, running-healthy, of the entry it is on its node (the subject's, which the +// consumer checks against the node it came from); a running unhealthy or stopped writes nothing. +func decodeRunning(body json.RawMessage, at time.Time) ([]StepOutcome, error) { + var r nodeengine.RunningEvent + if err := strictly(body, &r); err != nil { + return nil, err + } + if r.Module == "" { + return nil, fmt.Errorf("a running names no module") + } + if r.Stopped || r.Fingerprint == "" || r.Healthy != nil && !*r.Healthy { + return nil, nil + } + return []StepOutcome{{Module: r.Module, Fingerprint: r.Fingerprint, Step: StepRunning, + Outcome: OutcomeRunning, At: at}}, nil +} + +// namesSource refuses an event of a pipeline that names no branch or source revision, unless it is a run on request's. +func namesSource(branch string, source int64, run string) error { + if run == "" && (branch == "" || source < 1) { + return fmt.Errorf("it names no branch or source revision") + } + return nil +} + +// Follow writes one event of the record's pipeline families into the record of its source revision. Called with an +// event its family decoded; an error only when the record could not be read or written, so the event is offered again. +func (h *Holder) Follow(event string, body json.RawMessage) error { + if h.Records == nil { + return nil + } + switch event { + case runner.EventPipelineStarted: + var p runner.PipelineStartedEvent + if err := json.Unmarshal(body, &p); err != nil || p.Run != "" { + return nil + } + return h.pipelineStarted(p) + case runner.EventPipelineEnded: + var p runner.PipelineEndedEvent + if err := json.Unmarshal(body, &p); err != nil || p.Run != "" { + return nil + } + return h.pipelineEnded(p) + case runner.EventWaitHeld, runner.EventWaitEnded: + var w runner.WaitHeldEvent + if err := json.Unmarshal(body, &w); err != nil || w.Run != "" { + return nil + } + return h.wait(w, event == runner.EventWaitEnded) + case wants.EventWanted: + var w wants.WantedEvent + if err := json.Unmarshal(body, &w); err != nil || w.Removed || w.Pipeline == "" { + return nil + } + return h.wantWritten(w) + case wants.EventPipelineWanted: + var p wants.PipelineWantedEvent + if err := json.Unmarshal(body, &p); err != nil { + return nil + } + return h.pipelineWanted(p) + } + return nil +} + +// openRecord is the record of a source revision while it is open, or nil. +func (h *Holder) openRecord(branch string, source int64) (*Record, error) { + r, err := h.Records.ByKey(branch, source) + if err != nil { + return nil, fmt.Errorf("the record of %s at %d cannot be read: %w", branch, source, err) + } + if r == nil || r.Ended != nil { + return nil, nil + } + return r, nil +} + +func (h *Holder) pipelineStarted(p runner.PipelineStartedEvent) error { + r, err := h.openRecord(p.Branch, p.Source) + if err != nil || r == nil { + return err + } + for _, m := range p.Movers { + if m.Module == "" { + continue + } + if err := h.Records.AddEntry(r, RecordEntry{Module: m.Module, Fingerprint: m.Fingerprint}); err != nil { + return err + } + } + return nil +} + +func (h *Holder) pipelineEnded(p runner.PipelineEndedEvent) error { + r, err := h.openRecord(p.Branch, p.Source) + if err != nil || r == nil { + return err + } + at := h.Now().UTC() + if p.Ended != nil { + at = p.Ended.UTC() + } + for _, m := range p.Movers { + if m.Module == "" { + continue + } + if err := h.Records.AddEntry(r, RecordEntry{Module: m.Module, Fingerprint: m.Fingerprint}); err != nil { + return err + } + for _, s := range m.Steps { + if s.Step == "" || s.Outcome == "" { + continue + } + when := at + if s.Ended != nil { + when = s.Ended.UTC() + } + if _, err := h.Records.Outcome(r, RecordOutcome{Module: m.Module, Fingerprint: m.Fingerprint, Step: s.Step, + Outcome: s.Outcome, Why: s.Why, At: when}); err != nil { + return err + } + } + } + if p.Outcome == runner.PipelineEndedOutcomePassed { + return nil + } + why := strings.TrimSpace(p.Why) + if why == "" { + why = "the pipeline " + p.Pipeline + " ended " + p.Outcome + } + return h.Records.End(r, WhyFailed+why, "") +} + +func (h *Holder) wait(w runner.WaitHeldEvent, ended bool) error { + r, err := h.openRecord(w.Branch, w.Source) + if err != nil || r == nil { + return err + } + o := RecordOutcome{Module: w.Module, Fingerprint: w.Fingerprint, Step: w.Step, Outcome: OutcomeHeld, Why: w.On, + At: w.Since.UTC()} + if w.Who != "" { + o.Why += " (" + w.Who + ")" + } + if ended { + if w.How == "" { + h.logf("[mesh-delivery] a wait-ended of %s says no how, so the record %s keeps its wait", w.Pipeline, r.Instance) + return nil + } + o.Outcome, o.At = w.How, h.Now().UTC() + } + _, err = h.Records.Outcome(r, o) + return err +} + +func (h *Holder) wantWritten(w wants.WantedEvent) error { + milestone, _, _ := strings.Cut(w.Pipeline, ".published.") + r, err := h.Records.ByMilestone(milestone) + if err != nil { + return fmt.Errorf("the record of the pipeline %s cannot be read: %w", w.Pipeline, err) + } + if r == nil || r.Ended != nil { + return nil + } + if err := h.Records.AddEntry(r, RecordEntry{Module: w.Module, Fingerprint: w.Fp, Node: w.Node}); err != nil { + return err + } + at := h.Now().UTC() + if w.At != nil { + at = w.At.UTC() + } + if _, err := h.Records.Outcome(r, RecordOutcome{Module: w.Module, Fingerprint: w.Fp, Node: w.Node, Step: StepWanted, + Outcome: OutcomeWanted, At: at}); err != nil { + return err + } + var t RecordTrace + if r.Trace != nil { + t = *r.Trace + } + rev := strconv.FormatInt(w.Revision, 10) + if from, err := strconv.ParseInt(t.From, 10, 64); err != nil || w.Revision < from { + t.From = rev + } + if to, err := strconv.ParseInt(t.To, 10, 64); err != nil || w.Revision > to { + t.To = rev + } + return h.Records.SetTrace(r, t) +} + +func (h *Holder) pipelineWanted(p wants.PipelineWantedEvent) error { + r, err := h.openRecord(p.Branch, p.Source) + if err != nil || r == nil { + return err + } + if p.Outcome == wants.PipelineWantedOutcomeRunning { + return h.Records.End(r, WhyAllRun, "") + } + var late []string + for _, n := range p.NotRunning { + machines := append([]string(nil), n.Machines...) + sort.Strings(machines) + late = append(late, n.Module+" on "+strings.Join(machines, ", ")) + } + sort.Strings(late) + why := "a tier is late" + if len(late) > 0 { + why += ": not running " + strings.Join(late, "; ") + } + return h.Records.End(r, WhyFailed+why, "") +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordpipeline_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordpipeline_test.go new file mode 100644 index 000000000..1de92e662 --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordpipeline_test.go @@ -0,0 +1,130 @@ +package main + +import ( + "encoding/json" + "strings" + "testing" + "time" + + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// pipelineBed is a gate with one record open, of main at source revision 1, and its consumer. +func pipelineBed(t *testing.T) (*Holder, *StepConsumer, *Record, *[]string) { + t.Helper() + now := time.Date(2026, 11, 20, 3, 0, 0, 0, time.UTC) + var ended []string + h := &Holder{Now: func() time.Time { return now }} + h.Records = &Records{Ledger: newLedgerFake(t), Now: h.Now, Say: func(subject string, body any, id string) error { + if strings.Contains(subject, ".record-ended") { + ended = append(ended, id) + } + return nil + }} + if _, _, err := h.Records.Open(opening("main", 1, map[string]string{"novox/app@5": strings.Repeat("a", 40)})); err != nil { + t.Fatal(err) + } + if _, err := h.Records.Kept(); err != nil { // the records as the gate keeps them, read once + t.Fatal(err) + } + r, err := h.Records.ByKey("main", 1) + if err != nil || r == nil { + t.Fatalf("the record is %v (%v)", r, err) + } + c := &StepConsumer{Records: h.Records, Families: stepFamilies, Now: h.Now, Taken: h.TakenIn, Follow: h.Follow} + return h, c, r, &ended +} + +func hear(t *testing.T, c *StepConsumer, key, node, body string) { + t.Helper() + if !c.Takes(key) { + t.Fatalf("the consumer takes no %s", key) + } + if err := c.Hear(stdio.Envelope{Key: key, Node: node, Body: json.RawMessage(body)}); err != nil { + t.Fatalf("%s: %v", key, err) + } +} + +// GATE-5/AC (design 47 H §738, H-A-89): the record follows its pipeline to the machines: pipeline-started's movers are +// entries, a wait is held then ended with its how, pipeline-ended's step results are outcomes, each want is an entry with +// wanted and moves the trace's from and to, a healthy running is running-healthy and ends nothing, and pipeline-wanted +// running ends the record; a run on request's events and a removed want write nothing. +func TestARecordFollowsItsPipelineToTheMachines(t *testing.T) { + h, c, r, ended := pipelineBed(t) + hear(t, c, "mesh-pipeline-runner.pipeline-started.main.1", "", + `{"v":1,"branch":"main","source":1,"pipeline":"main.1","call":"0000000000000001","movers":[{"module":"app","commit":"c","fingerprint":"f","repository":"novox/app","tier":1}]}`) + hear(t, c, "mesh-pipeline-runner.pipeline-started.main.1", "", + `{"v":1,"branch":"main","source":1,"pipeline":"main.1","run":"r1","movers":[{"module":"other","commit":"c","fingerprint":"g","repository":"novox/o","tier":1}]}`) + if _, ok := r.Entries[EntryKey("app", "f", "")]; !ok || len(r.Entries) != 1 { + t.Fatalf("entries %v: the mover is one, a run's none", r.Entries) + } + hear(t, c, "mesh-pipeline-runner.wait-held.main.1", "", + `{"v":1,"branch":"main","source":1,"pipeline":"main.1","module":"app","fingerprint":"f","step":"approval","on":"person","who":"the operator","since":"2026-11-20T03:01:00Z"}`) + held := r.Outcomes[OutcomeKey("app", "f", "", "approval")] + if held.Outcome != OutcomeHeld || held.Why != "person (the operator)" { + t.Fatalf("the wait is %+v", held) + } + hear(t, c, "mesh-pipeline-runner.wait-ended.main.1", "", + `{"v":1,"branch":"main","source":1,"pipeline":"main.1","module":"app","fingerprint":"f","step":"approval","on":"person","how":"answered","since":"2026-11-20T03:01:00Z"}`) + if o := r.Outcomes[OutcomeKey("app", "f", "", "approval")]; o.Outcome != "answered" { + t.Fatalf("the wait ended as %+v", o) + } + hear(t, c, "mesh-pipeline-runner.pipeline-ended.main.1", "", + `{"v":1,"branch":"main","source":1,"pipeline":"main.1","outcome":"passed","movers":[{"module":"app","fingerprint":"f","tier":1,"steps":[{"step":"build","status":"required","outcome":"passed"},{"step":"review","status":"optional","outcome":"nothing to run","why":"no reviewer"}]}]}`) + if o := r.Outcomes[OutcomeKey("app", "f", "", "review")]; o.Outcome != "nothing to run" || o.Why != "no reviewer" || + r.Outcomes[OutcomeKey("app", "f", "", "build")].Outcome != "passed" || r.Ended != nil { + t.Fatalf("the steps are %+v and the record ended %+v", r.Outcomes, r.Ended) + } + for _, w := range []string{ + `{"v":1,"node":"ace","module":"app","fp":"f","pipeline":"main.1","source":1,"revision":12}`, + `{"v":1,"node":"g14","module":"app","fp":"f","pipeline":"main.1","source":1,"revision":11}`, + `{"v":1,"node":"novox","module":"app","fp":"f","pipeline":"main.1","source":1,"revision":30,"removed":true}`, + } { + hear(t, c, "mesh-wants.wanted.x.app", "", w) + } + if r.Trace == nil || r.Trace.From != "11" || r.Trace.To != "12" { + t.Fatalf("the trace is %+v: from 11 to 12, a removed want moves nothing", r.Trace) + } + if o := r.Outcomes[OutcomeKey("app", "f", "ace", StepWanted)]; o.Outcome != OutcomeWanted { + t.Fatalf("the want on ace is %+v", o) + } + if _, ok := r.Entries[EntryKey("app", "f", "novox")]; ok { + t.Fatal("a removed want is an entry") + } + hear(t, c, "node-engine.running.ace.app", "ace", `{"v":1,"at":"2026-11-20T03:05:00Z","module":"app","node":"ace","fingerprint":"f","healthy":true}`) + hear(t, c, "node-engine.running.g14.app", "g14", `{"v":1,"at":"2026-11-20T03:05:00Z","module":"app","node":"g14","fingerprint":"f","healthy":true}`) + if o := r.Outcomes[OutcomeKey("app", "f", "g14", StepRunning)]; o.Outcome != OutcomeRunning || r.Ended != nil || len(*ended) != 0 { + t.Fatalf("running is %+v and the record ended %+v: running ends nothing", o, r.Ended) + } + hear(t, c, "mesh-wants.pipeline-wanted.main.1", "", `{"v":1,"pipeline":"main.1","branch":"main","source":1,"outcome":"running"}`) + if r.Ended == nil || r.Ended.Why != WhyAllRun || len(*ended) != 1 { + t.Fatalf("the record ended %+v, said %v", r.Ended, *ended) + } + if got, _ := h.Records.ByKey("main", 1); got != r { + t.Fatal("the record read by its key is not the one the gate keeps") + } +} + +// GATE-5/AC (design 47 H §738, §751; H-A-75, H-A-89 (3), (4)): a pipeline-ended not passed ends the record failed naming +// the pipeline's why, and pipeline-wanted tier-late ends it failed naming the movers and machines not running. +func TestARecordEndsFailedOnAPipelineNotPassedOrATierLate(t *testing.T) { + for _, tc := range []struct{ name, key, body, why string }{ + {"pipeline failed", "mesh-pipeline-runner.pipeline-ended.main.1", + `{"v":1,"branch":"main","source":1,"pipeline":"main.1","outcome":"failed","why":"build failed","movers":[{"module":"app","fingerprint":"f","tier":1,"steps":[{"step":"build","status":"required","outcome":"failed"}]}]}`, + "failed: build failed"}, + {"pipeline cancelled, no why", "mesh-pipeline-runner.pipeline-ended.main.1", + `{"v":1,"branch":"main","source":1,"pipeline":"main.1","outcome":"cancelled","movers":[]}`, + "failed: the pipeline main.1 ended cancelled"}, + {"tier late", "mesh-wants.pipeline-wanted.main.1", + `{"v":1,"pipeline":"main.1","branch":"main","source":1,"outcome":"tier-late","not-running":[{"module":"app","machines":["g14","ace"]}]}`, + "failed: a tier is late: not running app on ace, g14"}, + } { + t.Run(tc.name, func(t *testing.T) { + _, c, r, ended := pipelineBed(t) + hear(t, c, tc.key, "", tc.body) + if r.Ended == nil || r.Ended.Why != tc.why || len(*ended) != 1 { + t.Fatalf("the record ended %+v, said %v; want %q", r.Ended, *ended, tc.why) + } + }) + } +} diff --git a/modules/mesh-delivery-gate/go.mod b/modules/mesh-delivery-gate/go.mod index 213a047f8..b7c2633af 100644 --- a/modules/mesh-delivery-gate/go.mod +++ b/modules/mesh-delivery-gate/go.mod @@ -3,6 +3,7 @@ module mesh-delivery-gate go 1.26.0 require ( + git.novox.be/novox/mesh-contracts/go/seats/nodeengine v0.0.0-00010101000000-000000000000 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-contracts/operator-and-conversation/go/clients/operatorchannelclient v0.0.0-00010101000000-000000000000 @@ -10,6 +11,8 @@ require ( 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/git v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery v0.0.0-00010101000000-000000000000 + git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner v0.0.0-00010101000000-000000000000 + git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-indexer/takein v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-view/viewstate v0.0.0-00010101000000-000000000000 @@ -37,7 +40,6 @@ require ( git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-contracts/go/seats/nodeengine v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/containerimageregistryclient v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry v0.0.0-00010101000000-000000000000 // indirect diff --git a/modules/mesh-delivery-gate/module.json b/modules/mesh-delivery-gate/module.json index 0351d0b12..e01d2a89a 100644 --- a/modules/mesh-delivery-gate/module.json +++ b/modules/mesh-delivery-gate/module.json @@ -20,6 +20,13 @@ "git.merge-called.*", "mesh-delivery-forge-bridge.forge.*.*.*.head-moved", "mesh-delivery-indexer.take-in.taken", + "mesh-pipeline-runner.pipeline-started.*.*", + "mesh-pipeline-runner.pipeline-ended.*.*", + "mesh-pipeline-runner.wait-held.*.*", + "mesh-pipeline-runner.wait-ended.*.*", + "mesh-wants.wanted.*.*", + "mesh-wants.pipeline-wanted.*.*", + "node-engine.running.*.*", "mesh-catalog.registration-changed", "mesh-conditions.findings-asked" ], -- 2.54.0 From 82d568847737a1449fb3b4bf7a96758363d82d6e Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:35:43 +0100 Subject: [PATCH 20/43] GATE-9: the gate's whole test holds the heads and the seed; the source key read as written MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The source key is read with the escaping it is written with, so a merge call's head on a branch holding a dot is no untaken head. The whole test raises head-untaken for a head held past the bound, takes a merge call on release/1.2, holds a push no call delivered, and holds nothing after the seed. Builds: design 47 H §774 (gate: tests 110, 111), §737, §740, §742, §768; H-A-87 (2) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/mesh-delivery-gate/bus_test.go | 21 +++ .../mesh-delivery-gate/mergecall_bus_test.go | 132 ++++++++++++++++++ .../cmd/mesh-delivery-gate/merges.go | 3 +- .../mesh-delivery-gate/takenin_bus_test.go | 54 ------- 4 files changed, 155 insertions(+), 55 deletions(-) create mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecall_bus_test.go delete mode 100644 modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/bus_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/bus_test.go index 7d6726788..4e29de34f 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/bus_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/bus_test.go @@ -19,6 +19,7 @@ import ( "time" oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" + meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" stdio "git.novox.be/novox/mesh-sdk/go" "github.com/nats-io/nats-server/v2/server" "github.com/nats-io/nats.go" @@ -54,6 +55,10 @@ type testBus struct { answers map[string][]byte // the gate's ledger `records`, as the tool runner keeps it: it outlives one run of the gate, as the bus's does. ledger *ledgerFake + // registrations are what the seat mesh-catalog's verb registrations answers, and heads what the seat git's verb branch + // answers, by `@` (the start pass and the seed read them, design 47 H §768). + registrations []meshcatalog.Registration + heads map[string]string } func newTestBus(t *testing.T) *testBus { @@ -355,6 +360,22 @@ func (r *runtime) answer(m rpcMessage) { switch a.Key { case "": return nil, errors.New("an ask with no key") + case "seat:" + meshcatalog.Seat + "." + meshcatalog.VerbRegistrations: + r.b.mu.Lock() + defer r.b.mu.Unlock() + return toolAnswer(meshcatalog.RegistrationsAnswer{Registrations: append([]meshcatalog.Registration{}, r.b.registrations...)}), nil + case "seat:git.branch": + repository, _ := a.Body["repository"].(string) + branch, _ := a.Body["branch"].(string) + r.b.mu.Lock() + defer r.b.mu.Unlock() + head, ok := r.b.heads[repository+"@"+branch] + if !ok { + return nil, fmt.Errorf("the forge of this test holds no %s@%s", repository, branch) + } + return toolAnswer(map[string]any{"repository": repository, "branch": branch, "head": head}), nil + case "seat:mesh-hand-acts.hand-act": + return toolAnswer(map[string]any{"id": "hand-act-1"}), nil case "gitea.gitea_note_append", "gitea.gitea_delivery_view", "gitea.gitea_commit_status", "gitea.gitea_merge_pull_request", "gitea.gitea_add_comment": return toolAnswer(map[string]any{}), nil diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecall_bus_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecall_bus_test.go new file mode 100644 index 000000000..129567db4 --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecall_bus_test.go @@ -0,0 +1,132 @@ +//go:build integration + +package main + +import ( + "context" + "encoding/json" + "strings" + "testing" + "time" + + mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" + meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" + "git.novox.be/novox/mesh-sdk/go/keys" + "github.com/nats-io/nats.go/jetstream" +) + +// The gate whole, over a real bus (design 47 H §737, §738, §740, §742, §768; acceptance tests 110 and 111's gate half): +// a head held past untaken-bound from before a stop raises head-untaken at the start; a merge-called the forge module says +// names one source revision of a branch holding `/` and `.`, opens its delivery record and says source-moved, and the +// head-moved of that merge then holds nothing, the source key read with the escaping it is written with (H-A-87 (2)); a +// head-moved no call delivered is held; the indexer's take-in.taken of the revision writes its movers into the record; +// after gate_source_seed a head-moved at the seeded head holds nothing. The gate asks the controller nothing. +const eventWithin = 20 * time.Second + +func TestAGateWholeTakesAMergeCallAndWatchesTheHeadsOverARealBus(t *testing.T) { + b := newTestBus(t) + ctx := context.Background() + branch := "release/1.2" // a `/` and a `.`: the dot is escaped in every key + escaped := keys.EscapeName(branch) + heldKey := func(repository, branch string) string { return headKey(repository, branch) } + old := strings.Repeat("0", 40) + // A head held from before a stop, past the bound: the state outlives the gate, as the bus's does. + raw, _ := json.Marshal(HeldHead{Head: old, Since: time.Now().Add(-time.Hour).UTC()}) + if _, err := b.kvs["heads"].Put(ctx, heldKey("novox/old", branch), raw); err != nil { + t.Fatal(err) + } + rt := b.start(t) + evctx, cancel := context.WithCancel(ctx) + defer cancel() + rt.deliverEvents(evctx) + rt.untilListening() + rt.until("head-untaken for the head held past the bound", eventWithin, func() bool { + return findingHeld(ctx, b, "head-untaken", heldKey("novox/old", branch)) + }) + + merge := strings.Repeat("c", 40) + call := "0123456789abcdef" + instance, _ := RecordInstance(recordMilestone(branch, 1)) + member := map[string]any{"repository": "novox/app", "number": 5, "branch": branch, "head": strings.Repeat("d", 40), "commit": merge} + for range 2 { // heard twice: one revision + b.publish(t, "mesh.seat.git.event.merge-called."+call, map[string]any{"call": call, "by": "lead@shanks", "account": "acct-s", + "at": "2026-11-20T03:00:00Z", "members": []any{member}}) + } + sourceKey := mdc.SourceKey(escaped) + rt.until("the record of the merge call and its source revision", eventWithin, func() bool { + e, err := b.kvs["source"].Get(ctx, sourceKey) + if err != nil || !strings.Contains(string(e.Value()), `"call":"`+call+`"`) || !strings.Contains(string(e.Value()), `"source":1`) { + return false + } + b.mu.Lock() + defer b.mu.Unlock() + return b.ledger != nil && b.ledger.states[instance] != nil + }) + headMoved(t, b, "novox/app", branch, merge) // the merge call's head: taken + headMoved(t, b, "novox/push", branch, merge) // a push no call delivered: held + rt.until("the pushed head held", eventWithin, func() bool { return kvHas(ctx, b, "heads", heldKey("novox/push", branch)) }) + if kvHas(ctx, b, "heads", heldKey("novox/app", branch)) { + t.Fatalf("the head of the merge call on %s is held: its source key is not read as it is written", branch) + } + + b.publish(t, "mesh.mod.mesh-delivery-indexer.event.take-in.taken", map[string]any{"branch": branch, "source": 1, "revision": 1, + "call": call, "entries": []any{map[string]any{"module": "app", "commit": merge, "fingerprint": "f", "repository": "novox/app"}}}) + rt.until("the take-in's mover in the record", eventWithin, func() bool { + b.mu.Lock() + defer b.mu.Unlock() + return strings.Contains(string(b.ledger.states[instance]), `"module":"app"`) + }) + if e, _ := b.kvs["source"].Get(ctx, sourceKey); e.Revision() != 1 { + t.Fatalf("the merge call heard twice wrote source.%s %d times", escaped, e.Revision()) + } + + // The seed (§768): every watched branch's first source revision at its head; its head-moved then holds nothing. + seeded := strings.Repeat("5", 40) + b.mu.Lock() + b.registrations = []meshcatalog.Registration{{Module: "seeded", Origin: "novox/seeded", Branches: []string{"main"}}} + b.heads = map[string]string{"novox/seeded@main": seeded} + b.mu.Unlock() + if out := rt.call(t, "gate_source_seed", map[string]any{"why": "the clean state", "_caller": "lead@shanks"}); !strings.Contains(out, `\"branches\":1`) { + t.Fatalf("gate_source_seed answered %s", out) + } + headMoved(t, b, "novox/seeded", "main", seeded) + headMoved(t, b, "novox/push", "main", seeded) + rt.until("the barrier head held", eventWithin, func() bool { return kvHas(ctx, b, "heads", heldKey("novox/push", "main")) }) + if kvHas(ctx, b, "heads", heldKey("novox/seeded", "main")) { + t.Fatal("the seeded head is held after the seed") + } + rt.noControllerAsk() +} + +// headMoved publishes the forge bridge's head-moved of a repository's branch. +func headMoved(t *testing.T, b *testBus, repository, branch, head string) { + t.Helper() + name, err := forgemsg.HeadMovedEvent(repository, branch) + if err != nil { + t.Fatal(err) + } + b.publish(t, "mesh.mod.mesh-delivery-forge-bridge.event."+name, forgemsg.HeadMoved{V: 1, Repository: repository, Branch: branch, Head: head}) +} + +func kvHas(ctx context.Context, b *testBus, state, key string) bool { + _, err := b.kvs[state].Get(ctx, key) + return err == nil +} + +// findingHeld is whether the gate's state findings holds a finding of a kind and id. +func findingHeld(ctx context.Context, b *testBus, kind, id string) bool { + names, err := b.kvs["findings"].Keys(ctx) + if err != nil && err != jetstream.ErrNoKeysFound { + return false + } + for _, k := range names { + if strings.Contains(k, kind) { + e, err := b.kvs["findings"].Get(ctx, k) + if err == nil && strings.Contains(string(e.Value()), id) { + return true + } + } + } + return false +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go index 265960ee5..0f7098c5f 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go @@ -79,8 +79,9 @@ func (kvSource) Set(branch string, update func(prev *Source) (Source, bool)) (ui return 0, fmt.Errorf("source.%s was written by another writer first %d times: %w", branch, changeAttempts, stdio.ErrChanged) } +// Read reads a branch's key with the escaping Set writes it with (design 47 H H-A-87 (2)). func (kvSource) Read(branch string) (*Source, uint64, error) { - e, err := stdio.State("source").Get(mdc.SourceKey(branch)) + e, err := stdio.State("source").Get(mdc.SourceKey(keys.EscapeName(branch))) if err != nil || e == nil { return nil, 0, err } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go deleted file mode 100644 index 0edbfcc53..000000000 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go +++ /dev/null @@ -1,54 +0,0 @@ -//go:build integration - -package main - -import ( - "context" - "strings" - "testing" - "time" -) - -// The gate whole, over a real bus (design 47 H §737, §738; acceptance test 110's gate half): a merge-called the forge module -// says names one source revision, opens its delivery record and says source-moved; the indexer's take-in.taken of that -// revision writes its movers into the record; the gate asks the controller nothing, and no state it no longer declares is -// needed (the template states are buckets under their local names). -const eventWithin = 20 * time.Second - -func TestAGateWholeTakesAMergeCallIntoASourceRevisionAndARecordOverARealBus(t *testing.T) { - b := newTestBus(t) - ctx := context.Background() - rt := b.start(t) - evctx, cancel := context.WithCancel(ctx) - defer cancel() - rt.deliverEvents(evctx) - rt.untilListening() - merge := strings.Repeat("c", 40) - call := "0123456789abcdef" - instance, _ := RecordInstance(recordMilestone("main", 1)) - member := map[string]any{"repository": "novox/app", "number": 5, "branch": "main", "head": strings.Repeat("d", 40), "commit": merge} - for range 2 { // heard twice: one revision - b.publish(t, "mesh.seat.git.event.merge-called."+call, map[string]any{"call": call, "by": "lead@shanks", "account": "acct-s", - "at": "2026-11-20T03:00:00Z", "members": []any{member}}) - } - rt.until("the record of the merge call and its source revision", eventWithin, func() bool { - e, err := b.kvs["source"].Get(ctx, "main") - if err != nil || !strings.Contains(string(e.Value()), `"call":"`+call+`"`) || !strings.Contains(string(e.Value()), `"source":1`) { - return false - } - b.mu.Lock() - defer b.mu.Unlock() - return b.ledger != nil && b.ledger.states[instance] != nil - }) - b.publish(t, "mesh.mod.mesh-delivery-indexer.event.take-in.taken", map[string]any{"branch": "main", "source": 1, "revision": 1, - "call": call, "entries": []any{map[string]any{"module": "app", "commit": merge, "fingerprint": "f", "repository": "novox/app"}}}) - rt.until("the take-in's mover in the record", eventWithin, func() bool { - b.mu.Lock() - defer b.mu.Unlock() - return strings.Contains(string(b.ledger.states[instance]), `"module":"app"`) - }) - if e, _ := b.kvs["source"].Get(ctx, "main"); e.Revision() != 1 { - t.Fatalf("the merge call heard twice wrote source.main %d times", e.Revision()) - } - rt.noControllerAsk() -} -- 2.54.0 From 40dc5fe11d8b06b0152514ce9be567b25377aa89 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:42:00 +0100 Subject: [PATCH 21/43] FORGE-1: merge-pull is one merge call, every member judged before any is merged MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit merge-pull takes pulls and by, refuses a call with no proved caller, judges each member (its head, its base a watched branch, not merged already, no required step failed at its fingerprint) and no branch protection, merges none when one is refused, else merges in the order named and answers the call and each member's outcome once. The seat's branch answers no protection. The seen-expiry test no longer depends on today's date. Builds: design 47 H §734, §735, §764 (the single-pull form; merge-pull's protection judgement); ADR 0682 §13; H-A-21, H-A-22, H-A-24, H-A-47 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/gitea/index.ts | 3 +- modules/gitea/mergecall.ts | 151 +++++++++++++++++++++++++ modules/gitea/seat.ts | 79 ++----------- modules/gitea/test/failedsteps.test.ts | 3 +- modules/gitea/test/guard.test.ts | 59 ++-------- modules/gitea/test/merge-call.test.ts | 106 +++++++++++++++++ 6 files changed, 279 insertions(+), 122 deletions(-) create mode 100644 modules/gitea/mergecall.ts create mode 100644 modules/gitea/test/merge-call.test.ts diff --git a/modules/gitea/index.ts b/modules/gitea/index.ts index d26359575..24daa21f7 100644 --- a/modules/gitea/index.ts +++ b/modules/gitea/index.ts @@ -410,7 +410,8 @@ void failedSteps.expireSeen().catch((err) => console.log(`[gitea] the seen keys // announcement's pending and a trunk's move, so a recheck's pending never lands over a later verdict (issue 456). registerModuleTools("gitea", () => [eventsTool(pace), ...readAllTools(readAll), ...writeTools(writes), ...statusTools("gitea", "gitea_first_views", "gitea_first_view_again", followers, firstViews, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }), - ...(gitea ? seatTools(gitea, { run, container: FORGE_CONTAINER, statuses: gates, registered: () => registrations.written, judge: (head) => failedSteps.judge(head) }) : []), + ...(gitea ? seatTools(gitea, { run, container: FORGE_CONTAINER, statuses: gates, registered: () => registrations.written, judge: (head) => failedSteps.judge(head), + watched: () => registrations.watched }) : []), ...failedStepsTools(failedSteps, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); })]); /** What the forge's event asks of the announcer at once (issue 729, 1422): see watch.ts. */ diff --git a/modules/gitea/mergecall.ts b/modules/gitea/mergecall.ts new file mode 100644 index 000000000..500541908 --- /dev/null +++ b/modules/gitea/mergecall.ts @@ -0,0 +1,151 @@ +// **One merge call is a delivery's start and its group** (novox/hq design 47 H §734, §735; ADR 0682 §1, §2, §13; H-A-21, +// H-A-22, H-A-47): the seat git's `merge-pull` takes `pulls`, one or more members `{repository, number, head}`, and `by`, +// the caller as who@node, recorded beside the bus account the tool runner proved for the call. Every member is judged +// before any is merged: still the pull request's head, its base a watched branch, not merged already (refused +// `already-merged` with its commit), and no run at the head's build-input fingerprint failed a `required` step (the +// module's own `failed-steps`). No branch protection is judged (ADR 0682 §13). When one member is refused, none is merged +// and the answer names every refused member and why; else the members are merged in the order the call names them. The +// verb answers once, at the call's end: `call` and each member's outcome. + +import { randomBytes } from "node:crypto"; +import { UNPROVEN, callerOf } from "@novox/mesh-sdk/callers"; +import type { MemberOutcome, MergePullAnswer } from "@novox/mesh-delivery-contracts-git"; +import type { GiteaClient } from "./client.js"; +import { Refused, checkName } from "./guard.js"; +import type { Judged } from "./failedsteps.js"; + +/** What a merge call reaches beside the forge's API. */ +export interface CallDeps { + /** Whether the first registrations were written: until then no merge is made (design 47 §95). */ + registered: () => boolean; + /** The watched branches the registrations name, by their origin repository `/`. */ + watched: () => ReadonlyMap>; + /** The failed `required` step that refuses a head, or null (failedsteps.ts). */ + judge: (head: string) => Promise; + /** A new call's id: 16 lowercase hex characters (H-A-24). */ + newCall?: () => string; +} + +/** The forge's API a merge call uses. */ +export type CallForge = Pick; + +/** One member as the call names it, and as it was judged. */ +export interface Member { + repository: string; + owner: string; + repo: string; + number: number; + head: string; + /** The pull request's base, read at judging. */ + branch: string; + state: MemberOutcome["state"]; + commit?: string; + why?: string; +} + +const SHA = /^[0-9a-f]{40}$/; +export const CALL = /^[0-9a-f]{16}$/; + +/** A new call's id. */ +export const newCallId = (): string => randomBytes(8).toString("hex"); + +/** The members a call names, each checked, or the reason the call is refused whole. */ +export function membersOf(raw: unknown): Member[] { + if (!Array.isArray(raw) || raw.length === 0) throw new Refused("pulls names one or more members {repository, number, head}"); + const seen = new Set(); + return raw.map((p: any) => { + const parts = String(p?.repository ?? "").split("/"); + if (parts.length !== 2) throw new Refused(`repository ${JSON.stringify(p?.repository)} is not /`); + const owner = checkName("owner", parts[0]), repo = checkName("name", parts[1]); + const number = Number(p?.number); + if (!Number.isInteger(number) || number < 1) throw new Refused(`${JSON.stringify(p?.number)} is not a pull request's number`); + const head = String(p?.head ?? "").trim(); + if (!SHA.test(head)) throw new Refused("head is a commit's full 40-hex sha"); + const repository = `${owner}/${repo}`; + if (seen.has(`${repository}#${number}`)) throw new Refused(`${repository}#${number} is named twice`); + seen.add(`${repository}#${number}`); + return { repository, owner, repo, number, head, branch: "", state: "judged" as const }; + }); +} + +/** Why a member is refused at judging, or "" when it is judged. */ +async function judgeMember(forge: CallForge, deps: CallDeps, m: Member): Promise { + const pull = await forge.getPullRequest(m.owner, m.repo, m.number); + m.branch = String(pull.base ?? ""); + if (pull.merged) { + if (pull.merge_commit_sha) m.commit = pull.merge_commit_sha; + return `already-merged: ${m.repository}#${m.number} was merged${pull.merge_commit_sha ? ` as ${pull.merge_commit_sha}` : ""}`; + } + if (pull.state !== "open") return `${m.repository}#${m.number} is closed`; + if (pull.head_sha !== m.head) { + return `head-moved: its head is ${String(pull.head_sha).slice(0, 12)}, not the ${m.head.slice(0, 12)} the caller names`; + } + if (!deps.watched().get(m.repository)?.has(m.branch)) return `its base ${m.branch} is no watched branch`; + const failed = await deps.judge(m.head); + if (failed) { + return `the ${failed.step} step of ${failed.module} failed in run ${failed.run} at its build-input fingerprint ${failed.fingerprint}`; + } + return ""; +} + +/** One member merged at its head, its merge commit read back; throws when the forge refused or did not name it. */ +async function mergeMember(forge: CallForge, m: Member): Promise { + await forge.mergePullRequest(m.owner, m.repo, m.number, "merge", true, m.head); + const merged = await forge.getPullRequest(m.owner, m.repo, m.number); + if (!merged.merge_commit_sha) throw new Error(`${m.repository}#${m.number} WAS merged, but the forge named no merge commit`); + // The merge is done once the forge named its commit: a read-back of its parents that fails is said, never a failed act. + try { + const parents = await forge.commitParents(m.owner, m.repo, merged.merge_commit_sha); + if (parents.length === 2 && parents[1] !== m.head) { + console.error(`[gitea] ${m.repository}#${m.number} WAS merged (${merged.merge_commit_sha}), but the forge merged ` + + `${parents[1].slice(0, 12)}, not the ${m.head.slice(0, 12)} the caller names (novox/hq issue 381)`); + } + } catch (err) { + console.error(`[gitea] ${m.repository}#${m.number} was merged (${merged.merge_commit_sha}); which head it joined could not be read back — ${err instanceof Error ? err.message : err}`); + } + return merged.merge_commit_sha; +} + +/** A member's outcome as the answer says it. */ +export const outcomeOf = (m: Member): MemberOutcome => ({ repository: m.repository, number: m.number, state: m.state, + ...(m.commit ? { commit: m.commit } : {}), ...(m.why ? { why: m.why } : {}) }); + +/** merge-pull: one merge call (design 47 H §734, §735). */ +export async function mergeCall(forge: CallForge, args: Record, deps: CallDeps): Promise { + const by = String(args.by ?? "").trim(); + if (!/^[^\s@]+@[^\s@]+$/.test(by)) throw new Refused("by is the caller as who@node"); + const account = callerOf(args); + if (account === UNPROVEN) throw new Refused("the tool runner proved no caller account for this call, so nothing is merged"); + const members = membersOf(args.pulls); + // Until the first registrations are written no merge is made (design 47 §95). + if (!deps.registered()) throw new Refused("no module is registered yet: the catalog has said no registration-changed, so no merge is made until the first registrations are written"); + const call = (deps.newCall ?? newCallId)(); + if (!CALL.test(call)) throw new Error(`the call's id ${JSON.stringify(call)} is not 16 lowercase hex characters`); + // Every member is judged before any is merged (§735). + let refused = false; + for (const m of members) { + const why = await judgeMember(forge, deps, m); + if (why) { + m.state = "refused"; + m.why = why; + refused = true; + } + } + if (refused) return { call, members: members.map(outcomeOf) }; + // Then merged in the order the call names them; a member the forge refuses stops the call (§767). + for (const m of members) { + try { + m.commit = await mergeMember(forge, m); + m.state = "merged"; + } catch (err) { + m.state = "refused"; + m.why = `the forge refused the merge: ${err instanceof Error ? err.message : String(err)}`; + for (const rest of members.slice(members.indexOf(m) + 1)) { + rest.state = "refused"; + rest.why = `not merged: ${m.repository}#${m.number} before it was refused by the forge`; + } + break; + } + } + return { call, members: members.map(outcomeOf) }; +} diff --git a/modules/gitea/seat.ts b/modules/gitea/seat.ts index 1c0c97124..ea228255d 100644 --- a/modules/gitea/seat.ts +++ b/modules/gitea/seat.ts @@ -9,9 +9,9 @@ // 4xx, or a refusal of this holder) the kind `refused`, a forge that failed or did not answer the kind `failed`; an empty list or an absent file is answered // only when the forge said so. // -// **The acts carry the caller's id** (status, comment, view, note, merge-pull): an id the pull request or the note already -// holds is answered `held` and nothing is done again; `merge-pull` merges the head the caller names, only into a base -// whose protection refuses a push, and refuses a head at whose fingerprint a run failed a `required` step (design 47 §39, §314). +// **The acts carry the caller's id** (status, comment, view, note): an id the pull request or the note already holds is +// answered `held` and nothing is done again. `merge-pull` is one merge call of one or more pull requests, every member +// judged before any is merged, no branch protection judged (mergecall.ts; design 47 H §734, §735; ADR 0682 §13). import type { ToolDefinition } from "@novox/mesh-sdk/tools"; import { @@ -26,6 +26,7 @@ import { containedFrom, newestByContext } from "./checks.js"; import { Refused, checkName } from "./guard.js"; import { appendNote, viewBody, viewComment, type Runner } from "./delivery.js"; import type { Judged } from "./failedsteps.js"; +import { mergeCall } from "./mergecall.js"; /** The verbs of the seat, in its definition's order. */ export const VERBS: readonly string[] = Object.values(GIT_VERBS); @@ -53,6 +54,8 @@ export interface SeatDeps { registered: () => boolean; /** The failed `required` step that refuses a head, judged from the module's own state `failed-steps` (failedsteps.ts), or null. */ judge: (head: string) => Promise; + /** The watched branches the registrations name, by their origin repository (registrations.ts): a member's base is one. */ + watched: () => ReadonlyMap>; } /** A failure as the contract says it: the forge's or this holder's refusal, or a forge that failed. */ @@ -149,7 +152,7 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[] const pull = pullOf(full, p); return pull.state === "open" ? { pull, merging: await mergingOf(gitea, owner, repo, p) } : { pull }; }), - verb(GIT_VERBS.branch, "The seat git's branch: a branch's head, and whether a protection rule refuses a direct push and admits only merges.", + verb(GIT_VERBS.branch, "The seat git's branch: a branch's head.", async (args): Promise => { const { owner, repo, full } = repositoryOf(args.repository); // No branch named is the repository's default branch, read from the forge, as the verb file reads it (A-399). @@ -158,7 +161,7 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[] if (branch === "") throw new Error(`the forge names no default branch of ${full}`); // The head first: a branch the forge does not hold is its 404, the kind not-found, never an unprotected branch. const head = await gitea.branchHead(owner, repo, branch); - return { repository: full, branch, head, protected: pushRefused(await gitea.appliedProtection(owner, repo, branch)) }; + return { repository: full, branch, head }; }), verb(GIT_VERBS.openPulls, "The seat git's open-pulls: every open pull request of every repository, with the statuses its base requires and those its head has.", async (): Promise => { @@ -258,8 +261,8 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[] const said = await appendNote(deps.run, deps.container, owner, repo, commit, "mesh-plan", `${String(args.line ?? "")} [${id}]`, id); return said.added ? { id, performed: true } : { id, held: true, performed: true }; }), - verb(GIT_VERBS.mergePull, "The seat git's merge-pull: an act of the forge bridge, a pull request merged at the head the caller names; it refuses a head at whose fingerprint a required step failed.", - async (args): Promise => mergePull(gitea, args, deps.registered, deps.judge)), + verb(GIT_VERBS.mergePull, "The seat git's merge-pull: one merge call, a delivery's start and its group: the pull requests named (pulls, each {repository, number, head}) and by (who@node); every member is judged before any is merged (still its head, its base a watched branch, no required step failed at its fingerprint), none merged when one is refused, then each merged in the order named; it answers the call and each member's outcome.", + async (args): Promise => mergeCall(gitea, args, deps)), verb(GIT_VERBS.tag, "The seat git's tag: the git tag go/vMAJOR.MINOR.PATCH of a released version, written at the commit its build was made from; a tag that is at that commit is performed.", async (args): Promise => { const { owner, repo } = repositoryOf(args.repository); @@ -275,68 +278,6 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[] ]; } -/** Whether a protection rule refuses a direct push and admits only merges: the rule exists and takes no push. */ -export function pushRefused(rule: { enable_push?: boolean } | null): boolean { - return !!rule && rule.enable_push === false; -} - function holds(comments: GiteaComment[], id: string): boolean { return comments.some((c) => String(c.body ?? "").includes(actMarker(id))); } - -/** merge-pull (design 47 §314): only the head the caller names, only into a base that takes no push, no check judged. */ -export async function mergePull(gitea: GiteaClient, args: Record, registered: () => boolean, - judge: (head: string) => Promise): Promise { - const id = idOf(args.id); - const { owner, repo, full } = repositoryOf(args.repository); - const number = numberOf(args.number); - const head = commitOf("head", args.head); - const no = (why: string, kind: NonNullable): MergePullAnswer => - ({ id, refused: `${full}#${number} is not merged: ${why}`, "refused-kind": kind }); - // Until the first registrations are written no merge is made: the old assigner would write a want from the trunk with no - // pipeline (design 47 §95). It names what is missing. - if (!registered()) return no("no module is registered yet: the catalog has said no registration-changed, so no merge is made until the first registrations are written", "no-registrations"); - const pull = await gitea.getPullRequest(owner, repo, number); - if (pull.merged) { - // Already merged at that head: performed (the act said again after its answer was lost); at another, refused. - return pull.head_sha === head ? { id, performed: true, held: true, ...(pull.merge_commit_sha ? { "merge-commit": pull.merge_commit_sha } : {}) } - : no(`it was merged at ${String(pull.head_sha).slice(0, 12)}, not the ${head.slice(0, 12)} the caller names`, "head-moved"); - } - if (pull.state !== "open") return no("it is closed", "git-refused"); - if (pull.head_sha !== head) { - return no(`its head is ${String(pull.head_sha).slice(0, 12)}, not the ${head.slice(0, 12)} the caller names`, "head-moved"); - } - const rule = await gitea.appliedProtection(owner, repo, String(pull.base ?? "")); - if (!pushRefused(rule)) { - return no(`its base ${pull.base} is guarded by no protection rule that refuses a push`, "unprotected"); - } - // The head's runs, judged from the module's own state (design 47 §39): a step that failed at its fingerprint in any run refuses it, - // naming the run, the module and the step; a head no run named merges, and the runner being down changes nothing. - const failed = await judge(head); - if (failed) { - return { id, refused: `${full}#${number} is not merged: the ${failed.step} step of ${failed.module} failed in run ${failed.run} at its build-input fingerprint ${failed.fingerprint}`, - "refused-kind": "policy-failed", run: failed.run, module: failed.module, step: failed.step }; - } - try { - await gitea.mergePullRequest(owner, repo, number, "merge", true, head); - } catch (err) { - const f = failure(err); - if (f["error-kind"] === "refused") return no(`the forge refused the merge: ${f.error}`, "git-refused"); - throw err; - } - const merged = await gitea.getPullRequest(owner, repo, number); - if (!merged.merge_commit_sha) throw new Error(`${full}#${number} WAS merged, but the forge named no merge commit`); - // The merge is done once the forge named its commit: a read-back that fails is said, never answered as a failed act. - let parents: string[]; - try { - parents = await gitea.commitParents(owner, repo, merged.merge_commit_sha); - } catch (err) { - console.error(`[gitea] ${full}#${number} was merged (${merged.merge_commit_sha}); which head it joined could not be read back — ${err instanceof Error ? err.message : err}`); - return { id, performed: true, "merge-commit": merged.merge_commit_sha }; - } - if (parents.length === 2 && parents[1] !== head) { - throw new Error(`${full}#${number} WAS merged (${merged.merge_commit_sha}), but the forge merged ${parents[1].slice(0, 12)}, ` + - `not the ${head.slice(0, 12)} the caller names (novox/hq issue 381)`); - } - return { id, performed: true, "merge-commit": merged.merge_commit_sha }; -} diff --git a/modules/gitea/test/failedsteps.test.ts b/modules/gitea/test/failedsteps.test.ts index 07e8f32dc..f3b703a94 100644 --- a/modules/gitea/test/failedsteps.test.ts +++ b/modules/gitea/test/failedsteps.test.ts @@ -109,7 +109,8 @@ test("a seen key past 30 days is deleted at the next step-ended, a younger one k const now = new Date("2026-11-11T12:00:00Z"); const old = (n: number) => new Date(Date.parse("2026-10-01T00:00:00Z") - n * 60000).toISOString(); await st.put(seenKey(C1, "old"), { v: 1, commit: C1, module: "old", fingerprints: ["x"], at: old(0) }); - await st.put(seenKey(C2, "young"), { v: 1, commit: C2, module: "young", fingerprints: ["x"], at: "2026-10-20T00:00:00Z" }); + // Younger than 30 days both before the step-ended and before the tool's own clock, today's: kept by both. + await st.put(seenKey(C2, "young"), { v: 1, commit: C2, module: "young", fingerprints: ["x"], at: new Date(Date.now() - 86_400_000).toISOString() }); await f.stepEnded(ended(C3, "passed")); assert.equal(kv.has(seenKey(C1, "old")), false, "past 30 days: deleted at the next step-ended"); assert.equal(kv.has(seenKey(C2, "young")), true, "younger: kept"); diff --git a/modules/gitea/test/guard.test.ts b/modules/gitea/test/guard.test.ts index de835a654..424b5076e 100644 --- a/modules/gitea/test/guard.test.ts +++ b/modules/gitea/test/guard.test.ts @@ -431,15 +431,12 @@ test("the two new tools are named for what mesh_search is asked, and gitea_api s assert.ok("refused" in judgeApiCall("PATCH", "/repos/a/b/pulls/7", { base: "x" })); }); -// **merge-pull judges the head it is asked at and no check** (design 47 *The seat git*, §314): merge-pull merges the -// head it is named into a base that takes no push, whatever its statuses say, and an id said again after the merge is -// answered performed. -test("merge-pull merges the head the caller names, and refuses one at whose fingerprint a required step failed", async () => { +// The seat git's pull and status (design 47 *The seat git*): an open pull request answers what the forge would merge now, +// and two writes of one head's status land in order. merge-pull is the merge call's (merge-call.test.ts). +test("the seat's pull answers what the forge would merge now, and a head's statuses land in order", async () => { const { seatTools } = await import("../dist/seat.js"); const { runAs } = await import("@novox/mesh-sdk/callers"); const bridge = "ace.mesh-delivery-forge-bridge"; - let registered = true; - let failedStep: { run: string; module: string; step: string; fingerprint: string } | null = null; // The head's one writer of its statuses (gate.ts), over a forge whose first write is still in flight when the next is asked. const { GateWriter } = await import("../dist/gate.js"); const landed: string[] = []; @@ -453,48 +450,10 @@ test("merge-pull merges the head the caller names, and refuses one at whose fing }; const gates = new GateWriter(slow); const seat = seatTools(Object.assign(Object.create(client), { setCommitStatus: slow.setCommitStatus }), - { run: async () => ({ stdout: "", code: 0 }), container: "gitea", statuses: gates, registered: () => registered, judge: async () => failedStep }); + { run: async () => ({ stdout: "", code: 0 }), container: "gitea", statuses: gates, registered: () => true, judge: async () => null, + watched: () => new Map() }); const call = (verb: string, args: Record, caller = bridge) => runAs({ "mesh/caller": caller }, args, () => seat.find((t: any) => t.name === `git.${verb}`)!.run(args)) as Promise; - const merge = (extra: Record = {}, caller?: string) => - call("merge-pull", { id: "act-1", repository: "novox/mesh-catalog", number: 7, head: REVIEWED, ...extra }, caller); - const rows: { name: string; set: () => void; caller?: string; extra?: Record; want: RegExp | "merged" | "held"; loud?: boolean; kind?: string }[] = [ - { name: "no registration written yet", set: () => { registered = false; }, want: /no module is registered yet/, kind: "no-registrations" }, - { name: "a head that moved", set: () => { state.head = MOVED; }, want: /its head is 3c4d5e6f7a8b, not the abc1234def56/, kind: "head-moved" }, - { name: "a base no rule guards", set: () => { state.rules = []; }, want: /guarded by no protection rule/, kind: "unprotected" }, - { name: "a base whose protection was removed after the last branch-moved", set: () => { state.rules = [protectedMain()]; state.rules = []; }, want: /guarded by no protection rule/, kind: "unprotected" }, - { name: "a base that takes a push", set: () => { state.rules = [{ rule_name: "main", enable_push: true }]; }, want: /guarded by no protection rule/, kind: "unprotected" }, - { name: "a required step failed at the head's fingerprint", set: () => { failedStep = { run: "main.7.nats", module: "nats", step: "unit", fingerprint: "fp-1" }; }, - want: /the unit step of nats failed in run main\.7\.nats/, kind: "policy-failed" }, - { name: "a failed check", set: () => { state.statuses = [{ context: "mesh/check", state: "failure", description: "fail" }]; }, want: "merged" }, - { name: "no status at all", set: () => { state.statuses = []; }, want: "merged" }, - { name: "a check of another trunk", set: () => { state.statuses = judgedAt(MOVED); }, want: "merged" }, - { name: "the id said again after the merge", set: () => { state.merged = true; }, want: "held" }, - { name: "a pull request merged at another head", set: () => { state.merged = true; state.head = MOVED; }, want: /merged at 3c4d5e6f7a8b, not the abc1234def56/ }, - { name: "a closed pull request", set: () => { state.prState = "closed"; }, want: /it is closed/, kind: "git-refused" }, - { name: "a fast-forward", set: () => { state.parents = [REVIEWED]; }, want: "merged" }, - { name: "the merge read back fails", set: () => { state.commitsFail = 502; }, want: "merged" }, - { name: "the forge merged another head", set: () => { state.landed = MOVED; }, want: /WAS merged .*the forge merged 3c4d5e6f7a8b, not the abc1234def56/, loud: true }, - ]; - for (const r of rows) { - registered = true; failedStep = null; state.merged = false; state.head = REVIEWED; state.statuses = judgedAt(TRUNK); state.sent = []; - state.rules = [protectedMain()]; state.prState = "open"; state.parents = null; state.commitsFail = 0; state.landed = ""; - r.set(); - const got = await merge(r.extra, r.caller); - if (r.want === "merged") { - assert.equal(got.performed, true, `${r.name}: ${JSON.stringify(got)}`); - assert.equal(got["merge-commit"], state.mergeCommit, r.name); - assert.equal(state.sent.find((s) => s.path.endsWith("/pulls/7/merge"))?.body.head_commit_id, REVIEWED, r.name); - } else if (r.want === "held") { - assert.deepEqual([got.performed, got.held], [true, true], r.name); - assert.deepEqual(writes(), [], `${r.name}: nothing merged again`); - } else { - assert.match(String(got.refused ?? got.error), r.want, r.name); - if (r.kind) assert.equal(got["refused-kind"], r.kind, r.name); - if (r.kind === "policy-failed") assert.deepEqual([got.run, got.module, got.step], ["main.7.nats", "nats", "unit"], r.name); - assert.deepEqual(writes(), r.loud ? ["POST /repos/novox/mesh-catalog/pulls/7/merge"] : [], `${r.name}: what was merged`); - } - } // A read: an open pull request answers what the forge would merge now; a forge that fails is the kind failed. state.merged = false; state.head = REVIEWED; state.rules = [protectedMain()]; const pull = await call("pull", { repository: "novox/mesh-catalog", number: 7 }); @@ -552,10 +511,10 @@ test("every read and act of the seat git answers its contract, and an error is a }; const run = async (_f: string, args: string[]) => args.includes("show") || args.includes("--ref") && !args.includes("append") ? { stdout: noteShown, code: noteShown ? 0 : 1 } : (noteWritten++, { stdout: "", code: 0 }); - const seat = seatTools(forge as any, { run: run as any, container: "gitea", statuses: { status: async () => {} }, registered: () => true, judge: async () => null }); + const seat = seatTools(forge as any, { run: run as any, container: "gitea", statuses: { status: async () => {} }, registered: () => true, judge: async () => null, watched: () => new Map() }); const call = (verb: string, args: Record, over: Record = {}) => { const t = seat.find((x: any) => x.name === `git.${verb}`)!; - const wrapped = seatTools(Object.assign(Object.create(forge), over) as any, { run: run as any, container: "gitea", statuses: { status: async () => {} }, registered: () => true, judge: async () => null }); + const wrapped = seatTools(Object.assign(Object.create(forge), over) as any, { run: run as any, container: "gitea", statuses: { status: async () => {} }, registered: () => true, judge: async () => null, watched: () => new Map() }); return runAs({ "mesh/caller": "ace.x" }, args, () => wrapped.find((x: any) => x.name === t.name)!.run(args)) as Promise; }; const rows: [string, string, Record, Record, (a: any) => unknown, unknown][] = [ @@ -563,9 +522,7 @@ test("every read and act of the seat git answers its contract, and an error is a ["pull", "by merge commit", { repository: "novox/app", "merge-commit": OTHER }, {}, (a) => [a.pull.state, a.pull["merge-commit"], a.merging], ["merged", OTHER, undefined]], ["pull", "unknown", { repository: "novox/app", number: 404 }, {}, (a) => [a.pull, a["error-kind"]], [undefined, "not-found"]], ["pull", "forge down", { repository: "novox/app", number: 9 }, { getPullRequest: down }, (a) => [a.pull, a["error-kind"]], [undefined, "failed"]], - ["branch", "a branch a rule guards", { repository: "novox/app", branch: "main" }, { branchHead: async () => SHA, appliedProtection: async () => ({ rule_name: "main", enable_push: false }) }, (a) => [a.head, a.protected], [SHA, true]], - ["branch", "a branch no rule guards", { repository: "novox/app", branch: "scratch" }, { branchHead: async () => SHA, appliedProtection: async () => null }, (a) => [a.head, a.protected], [SHA, false]], - ["branch", "a rule that takes a push", { repository: "novox/app", branch: "main" }, { branchHead: async () => SHA, appliedProtection: async () => ({ rule_name: "main", enable_push: true }) }, (a) => [a.protected], [false]], + ["branch", "a branch's head, no protection read (ADR 0682 §13)", { repository: "novox/app", branch: "main" }, { branchHead: async () => SHA, appliedProtection: async () => { throw new Error("read"); } }, (a) => [a.head, a.protected, a.error], [SHA, undefined, undefined]], ["branch", "no such branch", { repository: "novox/app", branch: "nope" }, { branchHead: gone }, (a) => [a.head, a["error-kind"]], [undefined, "not-found"]], ["open-pulls", "one", {}, {}, (a) => [a.pulls.length, a.pulls[0].required], [1, ["mesh/check"]]], ["open-pulls", "forge down", {}, { openPulls: down }, (a) => [a.pulls, a["error-kind"]], [undefined, "failed"]], diff --git a/modules/gitea/test/merge-call.test.ts b/modules/gitea/test/merge-call.test.ts new file mode 100644 index 000000000..21024e059 --- /dev/null +++ b/modules/gitea/test/merge-call.test.ts @@ -0,0 +1,106 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +// **One merge call, every member judged before any is merged** (novox/hq design 47 H §734, §735; ADR 0682 §13; H-A-21, +// H-A-22, H-A-47): the seat git's merge-pull takes pulls and by, judges each member (its head, its base a watched branch, +// not merged already, no required step failed at its fingerprint) and no branch protection, merges none when one is +// refused, else merges them in the order named, and answers the call and each member's outcome once. + +const A = "a".repeat(40), B = "b".repeat(40), MOVED = "c".repeat(40); +const CALL = "0123456789abcdef"; + +interface FakePull { base: string; head: string; state: string; merged: boolean; merge?: string; refuse?: string } + +/** A forge of pull requests by `#`, recording each merge asked. It has no protection API: a call that + * read a branch's protection would throw. */ +function forge(pulls: Record) { + const merges: string[] = []; + const key = (o: string, r: string, n: number) => `${o}/${r}#${n}`; + return { + merges, + getPullRequest: async (o: string, r: string, n: number) => { + const p = pulls[key(o, r, n)]; + if (!p) throw new Error(`Gitea API GET /repos/${o}/${r}/pulls/${n}: 404 {}`); + return { number: n, title: "", state: p.state, merged: p.merged, base: p.base, head: "b", head_sha: p.head, + ...(p.merged ? { merge_commit_sha: p.merge ?? "m".repeat(40) } : {}) } as any; + }, + mergePullRequest: async (o: string, r: string, n: number, _m: string, _d: boolean, head: string) => { + const p = pulls[key(o, r, n)]; + merges.push(key(o, r, n)); + if (p.refuse) throw new Error(`Gitea API POST /repos/${o}/${r}/pulls/${n}/merge: 409 ${p.refuse}`); + assert.equal(head, p.head); + p.merged = true; + p.merge = `${n}`.repeat(40).slice(0, 40); + }, + commitParents: async (_o: string, _r: string, sha: string) => [sha, "x"], + }; +} + +const watched = new Map([["novox/app", new Set(["main"])], ["novox/lib", new Set(["main"])]]); + +async function call(f: ReturnType, args: Record, over: Record = {}, caller = "person.jochen") { + const { mergeCall } = await import("../dist/mergecall.js"); + const { runAs } = await import("@novox/mesh-sdk/callers"); + const deps = { registered: () => true, watched: () => watched, judge: async () => null, newCall: () => CALL, ...over }; + return runAs({ "mesh/caller": caller }, args, () => mergeCall(f as any, args, deps as any)) as Promise; +} + +const two = { by: "lead@shanks", pulls: [{ repository: "novox/app", number: 5, head: A }, { repository: "novox/lib", number: 9, head: B }] }; +const open = (): Record => ({ "novox/app#5": { base: "main", head: A, state: "open", merged: false }, + "novox/lib#9": { base: "main", head: B, state: "open", merged: false } }); + +test("a merge call merges its members in the order named and answers each with its merge commit; one pull request is a list of one", async () => { + const f = forge(open()); + const got = await call(f, two); + assert.equal(got.call, CALL); + assert.deepEqual(f.merges, ["novox/app#5", "novox/lib#9"]); + assert.deepEqual(got.members.map((m: any) => [m.repository, m.number, m.state, m.commit]), + [["novox/app", 5, "merged", "5".repeat(40)], ["novox/lib", 9, "merged", "9".repeat(40)]]); + const one = forge(open()); + const single = await call(one, { by: "lead@shanks", pulls: [two.pulls[0]] }); + assert.deepEqual([one.merges, single.members.length, single.members[0].state], [["novox/app#5"], 1, "merged"]); +}); + +test("a member refused at judging merges none and names every refused member and why", async () => { + const rows: { name: string; set: (p: Record) => void; over?: Record; why: RegExp }[] = [ + { name: "the second member's head moved", set: (p) => { p["novox/lib#9"].head = MOVED; }, why: /head-moved: its head is cccccccccccc, not the bbbbbbbbbbbb/ }, + { name: "already merged", set: (p) => { Object.assign(p["novox/lib#9"], { merged: true, merge: "e".repeat(40) }); }, why: /already-merged: .* as e{40}/ }, + { name: "closed", set: (p) => { p["novox/lib#9"].state = "closed"; }, why: /is closed/ }, + { name: "a base no registration watches", set: (p) => { p["novox/lib#9"].base = "feat/x"; }, why: /its base feat\/x is no watched branch/ }, + { name: "a required step failed at its head's fingerprint", + set: () => {}, over: { judge: async (h: string) => (h === B ? { run: "main.7", module: "lib", step: "unit", fingerprint: "fp" } : null) }, + why: /the unit step of lib failed in run main\.7 at its build-input fingerprint fp/ }, + ]; + for (const r of rows) { + const pulls = open(); + r.set(pulls); + const f = forge(pulls); + const got = await call(f, two, r.over); + assert.deepEqual(f.merges, [], `${r.name}: nothing merged`); + assert.equal(got.members[0].state, "judged", r.name); + assert.equal(got.members[1].state, "refused", r.name); + assert.match(got.members[1].why, r.why, r.name); + } +}); + +test("a call is refused whole with no proved caller, no by, no registration or no pull request", async () => { + const f = forge(open()); + await assert.rejects(call(f, two, {}, ""), /proved no caller account/); + await assert.rejects(call(f, { pulls: two.pulls }), /by is the caller as who@node/); + await assert.rejects(call(f, two, { registered: () => false }), /no module is registered yet/); + await assert.rejects(call(f, { by: "lead@shanks", pulls: [] }), /one or more members/); + await assert.rejects(call(f, { by: "lead@shanks", pulls: [two.pulls[0], two.pulls[0]] }), /named twice/); + assert.deepEqual(f.merges, []); +}); + +test("the forge refusing a member after every member was judged stops the call: the landed member merged, the rest refused", async () => { + const pulls = open(); + pulls["novox/lib#9"].refuse = "merge conflict"; + const three = { by: "lead@shanks", pulls: [...two.pulls, { repository: "novox/app", number: 6, head: A }] }; + pulls["novox/app#6"] = { base: "main", head: A, state: "open", merged: false }; + const f = forge(pulls); + const got = await call(f, three); + assert.deepEqual(f.merges, ["novox/app#5", "novox/lib#9"], "the third is never asked"); + assert.deepEqual(got.members.map((m: any) => m.state), ["merged", "refused", "refused"]); + assert.match(got.members[1].why, /the forge refused the merge: .*merge conflict/); +}); -- 2.54.0 From f0bf4578a9df9fca8d620ad735be66761c993820 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:44:20 +0100 Subject: [PATCH 22/43] FORGE-2: the merge call is written in the state calls before its first merge MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The key call. holds by, the proved account, at, each member with its base and state (judged, then merged with its commit as it merges, or refused with why) and said; a call past call-retention (a setting, 30 days) is deleted on a new call's write, oldest first, with no timer. Builds: design 47 H §766 (the state calls, call-retention); H-A-18, H-A-47 (said, deletion on each new call's write), H-A-71 (2) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/gitea/calls.ts | 72 ++++++++++++++++++++++++++++++++ modules/gitea/index.ts | 5 ++- modules/gitea/kept.ts | 1 + modules/gitea/mergecall.ts | 17 +++++++- modules/gitea/module.json | 14 ++++++- modules/gitea/seat.ts | 3 ++ modules/gitea/test/calls.test.ts | 62 +++++++++++++++++++++++++++ modules/gitea/test/kept.test.ts | 4 +- 8 files changed, 173 insertions(+), 5 deletions(-) create mode 100644 modules/gitea/calls.ts create mode 100644 modules/gitea/test/calls.test.ts diff --git a/modules/gitea/calls.ts b/modules/gitea/calls.ts new file mode 100644 index 000000000..4f22c968f --- /dev/null +++ b/modules/gitea/calls.ts @@ -0,0 +1,72 @@ +// **The call is written before its first merge** (novox/hq design 47 H §766; H-A-18, H-A-47, H-A-71): before it merges +// anything, the forge module writes the call's key `call.` in its own template state `calls` (the seat git's +// definition declares it): `by`, the bus account the tool runner proved, `at`, the members, each with its base `branch` +// and `state` judged, merged with its commit, or refused with why, and `said`, whether its `merge-called` was said. Each +// member is updated as it merges. A call is kept `call-retention` (a setting, default 30 days), then deleted by the forge +// module on each new call's write, oldest first, with no timer (H-A-47). + +import { readFileSync } from "node:fs"; +import type { State } from "@novox/mesh-sdk/state"; +import { callsByCallCallKey, parseCallsByCallCallKey, type Call, type CallMember } from "@novox/mesh-delivery-contracts-git"; + +/** call-retention's default, in days (§766). */ +export const CALL_RETENTION_DAYS = 30; + +/** call-retention as the module's config file says it, in days; the default when it says none (mainrules.ts' form). */ +export function callRetentionDays(env: NodeJS.ProcessEnv = process.env): number { + const file = env.MESH_GITEA_CONFIG_FILE; + if (!file) return CALL_RETENTION_DAYS; + try { + const v = Number((JSON.parse(readFileSync(file, "utf8")) as Record)["call-retention"]); + return Number.isInteger(v) && v >= 1 ? v : CALL_RETENTION_DAYS; + } catch { return CALL_RETENTION_DAYS; } +} + +/** One kept call with its id. */ +export interface KeptCall extends Call { + call: string; +} + +/** The state `calls`: one key per merge call. */ +export class Calls { + constructor(private readonly st: State, private readonly retentionDays: () => number = () => CALL_RETENTION_DAYS, + private readonly log: (line: string) => void = () => {}) {} + + /** Every call kept, newest first. */ + async all(): Promise { + const out: KeptCall[] = []; + for (const key of await this.st.keys()) { + const parsed = parseCallsByCallCallKey(key); + if (!parsed) continue; + const e = await this.st.get(key); + if (e?.value) out.push({ ...(e.value as Call), call: parsed.call }); + } + return out.sort((a, b) => (a.at < b.at ? 1 : a.at > b.at ? -1 : a.call < b.call ? 1 : -1)); + } + + /** One call, or undefined. */ + async get(call: string): Promise { + const e = await this.st.get(callsByCallCallKey(call)); + return e?.value ? { ...(e.value as Call), call } : undefined; + } + + /** Writes a call. A new call (none kept under its key) first deletes the calls past call-retention, oldest first. */ + async put(call: string, value: Call, now: Date = new Date()): Promise { + if (!(await this.st.get(callsByCallCallKey(call)))?.value) await this.expire(now); + await this.st.put(callsByCallCallKey(call), value); + } + + /** Deletes the calls whose `at` is past call-retention before now, oldest first; answers the calls deleted. */ + async expire(now: Date): Promise { + const before = now.getTime() - this.retentionDays() * 86_400_000; + const old = (await this.all()).filter((c) => (Date.parse(c.at) || 0) < before).reverse(); + for (const c of old) await this.st.delete(callsByCallCallKey(c.call)); + if (old.length) this.log(`${old.length} merge calls past ${this.retentionDays()} days deleted: ${old.map((c) => c.call).join(", ")}`); + return old.map((c) => c.call); + } +} + +/** A kept member of a call. */ +export const keptMember = (m: { repository: string; number: number; branch: string; head: string; state: CallMember["state"]; + commit?: string; why?: string }): CallMember => ({ repository: m.repository, number: m.number, branch: m.branch, head: m.head, + state: m.state, ...(m.commit ? { commit: m.commit } : {}), ...(m.why ? { why: m.why } : {}) }); diff --git a/modules/gitea/index.ts b/modules/gitea/index.ts index 24daa21f7..db90a36a5 100644 --- a/modules/gitea/index.ts +++ b/modules/gitea/index.ts @@ -50,6 +50,7 @@ import { FORGE_CONTAINER, seatTools } from "./seat.js"; import { REGISTRATION_CHANGED_EVENT, Registrations } from "./registrations.js"; import { FailedSteps, Heads, readHeads } from "./failedsteps.js"; import { failedStepsTools } from "./tools/failedsteps.js"; +import { Calls, callRetentionDays } from "./calls.js"; import { seat as GIT, events as GIT_EVENTS, type BranchMovedEvent, type PullClosedEvent, type PullMergedEvent, type PullUpdatedEvent } from "@novox/mesh-delivery-contracts-git"; import { seat as RUNNER, events as RUNNER_EVENTS, type StepEndedEvent } from "@novox/mesh-delivery-contracts-mesh-pipeline-runner"; import { run } from "./delivery.js"; @@ -400,6 +401,8 @@ const registrations = new Registrations((verb, input) => broker().request(verb, // The failed steps the runner's step-ended said, and the heads that keep them (failedsteps.ts; design 47 §39, §475, §487, §135). const heldHeads = new Heads(); let reloadHeads: () => Promise = async () => {}; +// The merge calls, each written before its first merge (calls.ts; design 47 H §766). +const calls = new Calls(state("calls"), () => callRetentionDays(), (line) => console.log(`[gitea] ${line}`)); const failedSteps = new FailedSteps(state("failed-steps"), (line) => console.log(`[gitea] ${line}`), (commit) => heldHeads.has(commit)); // The `seen` keys past their 30 days are deleted at the start, as on each step-ended (design 47 §581). @@ -411,7 +414,7 @@ void failedSteps.expireSeen().catch((err) => console.log(`[gitea] the seen keys registerModuleTools("gitea", () => [eventsTool(pace), ...readAllTools(readAll), ...writeTools(writes), ...statusTools("gitea", "gitea_first_views", "gitea_first_view_again", followers, firstViews, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }), ...(gitea ? seatTools(gitea, { run, container: FORGE_CONTAINER, statuses: gates, registered: () => registrations.written, judge: (head) => failedSteps.judge(head), - watched: () => registrations.watched }) : []), + watched: () => registrations.watched, calls }) : []), ...failedStepsTools(failedSteps, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); })]); /** What the forge's event asks of the announcer at once (issue 729, 1422): see watch.ts. */ diff --git a/modules/gitea/kept.ts b/modules/gitea/kept.ts index 9288b8aa5..790780182 100644 --- a/modules/gitea/kept.ts +++ b/modules/gitea/kept.ts @@ -6,6 +6,7 @@ // checks/readable every state another manifest reads to a readable announcement. export const KEPT: readonly { readonly name: string; readonly readable?: boolean }[] = [ { name: "announced" }, + { name: "calls" }, { name: "bound", readable: true }, { name: "findings", readable: true }, { name: "failed-steps" }, diff --git a/modules/gitea/mergecall.ts b/modules/gitea/mergecall.ts index 500541908..29fc96005 100644 --- a/modules/gitea/mergecall.ts +++ b/modules/gitea/mergecall.ts @@ -13,6 +13,7 @@ import type { MemberOutcome, MergePullAnswer } from "@novox/mesh-delivery-contra import type { GiteaClient } from "./client.js"; import { Refused, checkName } from "./guard.js"; import type { Judged } from "./failedsteps.js"; +import { keptMember, type Calls } from "./calls.js"; /** What a merge call reaches beside the forge's API. */ export interface CallDeps { @@ -24,6 +25,9 @@ export interface CallDeps { judge: (head: string) => Promise; /** A new call's id: 16 lowercase hex characters (H-A-24). */ newCall?: () => string; + /** The state calls (calls.ts): the call is written before its first merge and as each member merges (§766). */ + calls?: Calls; + now?: () => Date; } /** The forge's API a merge call uses. */ @@ -131,12 +135,22 @@ export async function mergeCall(forge: CallForge, args: Record, refused = true; } } - if (refused) return { call, members: members.map(outcomeOf) }; + const now = deps.now ?? (() => new Date()); + const at = now().toISOString(); + // The call as kept (§766): a call refused at judging merged nothing and has nothing to say. + const keep = (said: boolean) => deps.calls?.put(call, { by, account, at, members: members.map(keptMember), said }, now()); + if (refused) { + await keep(true); + return { call, members: members.map(outcomeOf) }; + } + // Written before the first merge, and again as each member merges. + await keep(false); // Then merged in the order the call names them; a member the forge refuses stops the call (§767). for (const m of members) { try { m.commit = await mergeMember(forge, m); m.state = "merged"; + await keep(false); } catch (err) { m.state = "refused"; m.why = `the forge refused the merge: ${err instanceof Error ? err.message : String(err)}`; @@ -144,6 +158,7 @@ export async function mergeCall(forge: CallForge, args: Record, rest.state = "refused"; rest.why = `not merged: ${m.repository}#${m.number} before it was refused by the forge`; } + await keep(false); break; } } diff --git a/modules/gitea/module.json b/modules/gitea/module.json index 24da1123d..55bc097c4 100644 --- a/modules/gitea/module.json +++ b/modules/gitea/module.json @@ -211,6 +211,13 @@ "trusted": true, "default": {} }, + "call-retention": { + "type": "number", + "description": "How many days the forge module keeps a merge call in its state calls (design 47 H §766): a call older than this is deleted when a new call is written, oldest first.", + "trusted": false, + "minimum": 1, + "default": 30 + }, "keep-main-rules": { "type": "boolean", "description": "Whether the module keeps main's branch protection as the mesh wants it. True: every ten minutes, and at the start, main's rule in every repository of the organisation novox is put to no push and the tools' account alone merging, and each apply of the `repositories` value narrows the main rule of the repositories it lists, requiring no status. False (the default): the module changes no existing repository's branch protection, whoever set it at the forge, but takes away a required status it finds on a rule covering main, once (ADR 0534 §1); a repository listed in `repositories` that does not exist is still made, with its first protection.", @@ -358,7 +365,8 @@ "takes-config": [ "url", "keep-main-rules", - "main-mergers" + "main-mergers", + "call-retention" ] }, { @@ -623,6 +631,10 @@ { "name": "failed-steps", "history": 1 + }, + { + "name": "calls", + "history": 1 } ], "reads": [ diff --git a/modules/gitea/seat.ts b/modules/gitea/seat.ts index ea228255d..969bfff0f 100644 --- a/modules/gitea/seat.ts +++ b/modules/gitea/seat.ts @@ -27,6 +27,7 @@ import { Refused, checkName } from "./guard.js"; import { appendNote, viewBody, viewComment, type Runner } from "./delivery.js"; import type { Judged } from "./failedsteps.js"; import { mergeCall } from "./mergecall.js"; +import type { Calls } from "./calls.js"; /** The verbs of the seat, in its definition's order. */ export const VERBS: readonly string[] = Object.values(GIT_VERBS); @@ -56,6 +57,8 @@ export interface SeatDeps { judge: (head: string) => Promise; /** The watched branches the registrations name, by their origin repository (registrations.ts): a member's base is one. */ watched: () => ReadonlyMap>; + /** The state calls (calls.ts): each merge call written before its first merge (design 47 H §766). */ + calls?: Calls; } /** A failure as the contract says it: the forge's or this holder's refusal, or a forge that failed. */ diff --git a/modules/gitea/test/calls.test.ts b/modules/gitea/test/calls.test.ts new file mode 100644 index 000000000..27a0c8f21 --- /dev/null +++ b/modules/gitea/test/calls.test.ts @@ -0,0 +1,62 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +// **The call is written before its first merge** (novox/hq design 47 H §766; H-A-18, H-A-47, H-A-71): the key +// call. of the state calls holds by, the proved account, at, each member with its base and its state (judged before +// the first merge, merged with its commit as it merges) and said; a call past call-retention is deleted on a new call's +// write, oldest first. + +const A = "a".repeat(40), B = "b".repeat(40); + +export function memory() { + const kv = new Map(); + return { kv, st: { + get: async (k: string) => (kv.has(k) ? { key: k, value: structuredClone(kv.get(k)), revision: 1 } : null), + put: async (k: string, v: unknown) => { kv.set(k, structuredClone(v)); return 1; }, + delete: async (k: string) => { kv.delete(k); }, + keys: async () => [...kv.keys()].sort(), + watch: async () => {}, + } }; +} + +test("a merge call is kept before its first merge, each member judged, then merged with its commit", async () => { + const { mergeCall } = await import("../dist/mergecall.js"); + const { Calls } = await import("../dist/calls.js"); + const { runAs } = await import("@novox/mesh-sdk/callers"); + const { kv, st } = memory(); + const calls = new Calls(st as any); + const seenAtMerge: any[] = []; + const pulls: Record = { 5: { head: A, merged: false }, 9: { head: B, merged: false } }; + const forge = { + getPullRequest: async (_o: string, r: string, n: number) => ({ number: n, state: "open", merged: pulls[n].merged, base: "main", + head_sha: pulls[n].head, ...(pulls[n].merged ? { merge_commit_sha: String(n).repeat(40) } : {}) }), + mergePullRequest: async (_o: string, _r: string, n: number) => { + seenAtMerge.push(structuredClone(kv.get("call.0123456789abcdef"))); + pulls[n].merged = true; + }, + commitParents: async () => [], + }; + const args = { by: "lead@shanks", pulls: [{ repository: "novox/app", number: 5, head: A }, { repository: "novox/app", number: 9, head: B }] }; + const at = new Date("2026-11-20T03:00:00Z"); + await runAs({ "mesh/caller": "shanks.claude-code" }, args, () => mergeCall(forge as any, args, { registered: () => true, + watched: () => new Map([["novox/app", new Set(["main"])]]), judge: async () => null, newCall: () => "0123456789abcdef", + calls, now: () => at } as any)); + const first = seenAtMerge[0]; + assert.deepEqual([first.by, first.account, first.at, first.said], ["lead@shanks", "shanks.claude-code", at.toISOString(), false]); + assert.deepEqual(first.members.map((m: any) => [m.number, m.branch, m.head, m.state]), [[5, "main", A, "judged"], [9, "main", B, "judged"]]); + assert.deepEqual(seenAtMerge[1].members.map((m: any) => [m.state, m.commit]), [["merged", "5".repeat(40)], ["judged", undefined]]); + const kept = await calls.get("0123456789abcdef"); + assert.deepEqual(kept?.members.map((m: any) => m.state), ["merged", "merged"]); +}); + +test("a call past call-retention is deleted on a new call's write, oldest first; a younger one is kept", async () => { + const { Calls } = await import("../dist/calls.js"); + const { kv, st } = memory(); + const calls = new Calls(st as any, () => 30); + const call = (at: string) => ({ by: "a@b", account: "x", at, members: [], said: true }); + await calls.put("1111111111111111", call("2026-09-01T00:00:00Z"), new Date("2026-09-01T00:00:00Z")); + await calls.put("2222222222222222", call("2026-10-25T00:00:00Z"), new Date("2026-10-25T00:00:00Z")); + await calls.put("3333333333333333", call("2026-11-20T00:00:00Z"), new Date("2026-11-20T00:00:00Z")); + assert.deepEqual([...kv.keys()].sort(), ["call.2222222222222222", "call.3333333333333333"]); + assert.deepEqual((await calls.all()).map((c) => c.call), ["3333333333333333", "2222222222222222"], "newest first"); +}); diff --git a/modules/gitea/test/kept.test.ts b/modules/gitea/test/kept.test.ts index b39114667..d4a1cb604 100644 --- a/modules/gitea/test/kept.test.ts +++ b/modules/gitea/test/kept.test.ts @@ -16,8 +16,8 @@ test("the states the module announces are the states its manifest declares", asy test("the announcement has the layout mesh-sdk's go/announce reads: version 1, the module, the node, the states sorted by name", async () => { const { statesAnnouncement } = await import("../kept.ts"); - assert.deepEqual(statesAnnouncement("gitea", "ace"), { v: 1, module: "gitea", node: "ace", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "transfers" }] }); - assert.deepEqual(statesAnnouncement("gitea", undefined), { v: 1, module: "gitea", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "transfers" }] }); + assert.deepEqual(statesAnnouncement("gitea", "ace"), { v: 1, module: "gitea", node: "ace", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "calls" }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "transfers" }] }); + assert.deepEqual(statesAnnouncement("gitea", undefined), { v: 1, module: "gitea", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "calls" }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "transfers" }] }); }); test("index.ts says the announcement as the event `states` before the tools are served", () => { -- 2.54.0 From 7f5f285b60571323467405194849840fc3db7495 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:45:27 +0100 Subject: [PATCH 23/43] FORGE-3: a merge call says one merge-called, and the start says an unsaid one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Once its members are merged or refused the call says merge-called on merge-called. with its landed members (base and merge commit) and its refused ones, and is kept said; a call that landed none says none. At the start every call not said says it for what landed. Builds: design 47 H §736, §766 (said again at the start); H-A-18, H-A-47 (said), H-A-71 (2) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/gitea/calls.ts | 41 ++++++++++++++++++++++++++++++- modules/gitea/index.ts | 8 ++++-- modules/gitea/mergecall.ts | 24 ++++++++++++++---- modules/gitea/seat.ts | 4 ++- modules/gitea/test/calls.test.ts | 42 ++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 9 deletions(-) diff --git a/modules/gitea/calls.ts b/modules/gitea/calls.ts index 4f22c968f..106f156d0 100644 --- a/modules/gitea/calls.ts +++ b/modules/gitea/calls.ts @@ -7,7 +7,9 @@ import { readFileSync } from "node:fs"; import type { State } from "@novox/mesh-sdk/state"; -import { callsByCallCallKey, parseCallsByCallCallKey, type Call, type CallMember } from "@novox/mesh-delivery-contracts-git"; +import { + callsByCallCallKey, mergeCalledSubject, parseCallsByCallCallKey, type Call, type CallMember, type MergeCalledEvent, +} from "@novox/mesh-delivery-contracts-git"; /** call-retention's default, in days (§766). */ export const CALL_RETENTION_DAYS = 30; @@ -70,3 +72,40 @@ export class Calls { export const keptMember = (m: { repository: string; number: number; branch: string; head: string; state: CallMember["state"]; commit?: string; why?: string }): CallMember => ({ repository: m.repository, number: m.number, branch: m.branch, head: m.head, state: m.state, ...(m.commit ? { commit: m.commit } : {}), ...(m.why ? { why: m.why } : {}) }); + +/** Says a seat event on its subject, under an id the bus keeps once. */ +export type Say = (subject: string, body: unknown, id?: string) => Promise; + +/** The merge-called of a kept call (design 47 H §736, §767): its landed members, each with its base and merge commit, and its + * refused ones; undefined for a call that landed no member, which says none. */ +export function mergeCalledOf(c: KeptCall): MergeCalledEvent | undefined { + const landed = c.members.filter((m) => m.state === "merged" && m.commit); + if (landed.length === 0) return undefined; + // A member judged and not merged (a stop mid-call) is in neither: its head is an untaken head (§740). + const refused = c.members.filter((m) => m.state === "refused"); + return { call: c.call, by: c.by, account: c.account, at: c.at, + members: landed.map((m) => ({ repository: m.repository, number: m.number, branch: m.branch, head: m.head, commit: m.commit! })), + ...(refused.length ? { refused: refused.map((m) => ({ repository: m.repository, number: m.number, why: m.why ?? "not merged" })) } : {}) }; +} + +/** Says a call's merge-called, when it landed a member; answers whether it said one. */ +export async function sayCall(c: KeptCall, say: Say): Promise { + const body = mergeCalledOf(c); + if (!body) return false; + await say(mergeCalledSubject(c.call), body, `merge-called.${c.call}`); + return true; +} + +/** At the start (§766): every call whose event is not said says merge-called for what landed, and is kept said, so a stop + * between the last merge and the event loses nothing it recorded. Answers the calls said. */ +export async function sayUnsaid(calls: Calls, say: Say, log: (line: string) => void = () => {}): Promise { + const said: string[] = []; + for (const c of await calls.all()) { + if (c.said) continue; + if (await sayCall(c, say)) said.push(c.call); + const { call, ...value } = c; + await calls.put(call, { ...value, said: true }); + } + if (said.length) log(`merge-called said again at the start for ${said.join(", ")}`); + return said; +} diff --git a/modules/gitea/index.ts b/modules/gitea/index.ts index db90a36a5..6096a9720 100644 --- a/modules/gitea/index.ts +++ b/modules/gitea/index.ts @@ -50,7 +50,8 @@ import { FORGE_CONTAINER, seatTools } from "./seat.js"; import { REGISTRATION_CHANGED_EVENT, Registrations } from "./registrations.js"; import { FailedSteps, Heads, readHeads } from "./failedsteps.js"; import { failedStepsTools } from "./tools/failedsteps.js"; -import { Calls, callRetentionDays } from "./calls.js"; +import { Calls, callRetentionDays, sayUnsaid } from "./calls.js"; +import { seatPublish } from "@novox/mesh-sdk/runtime"; import { seat as GIT, events as GIT_EVENTS, type BranchMovedEvent, type PullClosedEvent, type PullMergedEvent, type PullUpdatedEvent } from "@novox/mesh-delivery-contracts-git"; import { seat as RUNNER, events as RUNNER_EVENTS, type StepEndedEvent } from "@novox/mesh-delivery-contracts-mesh-pipeline-runner"; import { run } from "./delivery.js"; @@ -403,6 +404,9 @@ const heldHeads = new Heads(); let reloadHeads: () => Promise = async () => {}; // The merge calls, each written before its first merge (calls.ts; design 47 H §766). const calls = new Calls(state("calls"), () => callRetentionDays(), (line) => console.log(`[gitea] ${line}`)); +// At the start, every call whose merge-called is not said says it for what landed (§766). +void sayUnsaid(calls, seatPublish, (line) => console.log(`[gitea] ${line}`)).catch((err) => + console.error(`[gitea] the merge calls not said could not be said at the start — ${err instanceof Error ? err.message : err}`)); const failedSteps = new FailedSteps(state("failed-steps"), (line) => console.log(`[gitea] ${line}`), (commit) => heldHeads.has(commit)); // The `seen` keys past their 30 days are deleted at the start, as on each step-ended (design 47 §581). @@ -414,7 +418,7 @@ void failedSteps.expireSeen().catch((err) => console.log(`[gitea] the seen keys registerModuleTools("gitea", () => [eventsTool(pace), ...readAllTools(readAll), ...writeTools(writes), ...statusTools("gitea", "gitea_first_views", "gitea_first_view_again", followers, firstViews, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }), ...(gitea ? seatTools(gitea, { run, container: FORGE_CONTAINER, statuses: gates, registered: () => registrations.written, judge: (head) => failedSteps.judge(head), - watched: () => registrations.watched, calls }) : []), + watched: () => registrations.watched, calls, say: seatPublish }) : []), ...failedStepsTools(failedSteps, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); })]); /** What the forge's event asks of the announcer at once (issue 729, 1422): see watch.ts. */ diff --git a/modules/gitea/mergecall.ts b/modules/gitea/mergecall.ts index 29fc96005..11c772c29 100644 --- a/modules/gitea/mergecall.ts +++ b/modules/gitea/mergecall.ts @@ -9,11 +9,11 @@ import { randomBytes } from "node:crypto"; import { UNPROVEN, callerOf } from "@novox/mesh-sdk/callers"; -import type { MemberOutcome, MergePullAnswer } from "@novox/mesh-delivery-contracts-git"; +import type { Call, MemberOutcome, MergePullAnswer } from "@novox/mesh-delivery-contracts-git"; import type { GiteaClient } from "./client.js"; import { Refused, checkName } from "./guard.js"; import type { Judged } from "./failedsteps.js"; -import { keptMember, type Calls } from "./calls.js"; +import { keptMember, sayCall, type Calls, type Say } from "./calls.js"; /** What a merge call reaches beside the forge's API. */ export interface CallDeps { @@ -28,6 +28,8 @@ export interface CallDeps { /** The state calls (calls.ts): the call is written before its first merge and as each member merges (§766). */ calls?: Calls; now?: () => Date; + /** Says a seat event on its subject under an id the bus keeps once (mesh-sdk runtime.seatPublish): merge-called (§736). */ + say?: Say; } /** The forge's API a merge call uses. */ @@ -136,9 +138,11 @@ export async function mergeCall(forge: CallForge, args: Record, } } const now = deps.now ?? (() => new Date()); - const at = now().toISOString(); - // The call as kept (§766): a call refused at judging merged nothing and has nothing to say. - const keep = (said: boolean) => deps.calls?.put(call, { by, account, at, members: members.map(keptMember), said }, now()); + // The call as kept (§766), its `at` the time of its write, then of its last merge (the event's `at`, §736). + let at = now().toISOString(); + const value = (said: boolean): Call => ({ by, account, at, members: members.map(keptMember), said }); + const keep = async (said: boolean) => { await deps.calls?.put(call, value(said), now()); }; + // A call refused at judging merged nothing and has nothing to say. if (refused) { await keep(true); return { call, members: members.map(outcomeOf) }; @@ -150,6 +154,7 @@ export async function mergeCall(forge: CallForge, args: Record, try { m.commit = await mergeMember(forge, m); m.state = "merged"; + at = now().toISOString(); await keep(false); } catch (err) { m.state = "refused"; @@ -162,5 +167,14 @@ export async function mergeCall(forge: CallForge, args: Record, break; } } + // Once its members are merged or refused, the call says merge-called, then is kept said (§736, §766). + if (deps.say) { + try { + await sayCall({ ...value(false), call }, deps.say); + await keep(true); + } catch (err) { + console.error(`[gitea] the merge call ${call} landed and its merge-called is not said yet; it is said at the next start — ${err instanceof Error ? err.message : err}`); + } + } return { call, members: members.map(outcomeOf) }; } diff --git a/modules/gitea/seat.ts b/modules/gitea/seat.ts index 969bfff0f..baa7e5bb1 100644 --- a/modules/gitea/seat.ts +++ b/modules/gitea/seat.ts @@ -27,7 +27,7 @@ import { Refused, checkName } from "./guard.js"; import { appendNote, viewBody, viewComment, type Runner } from "./delivery.js"; import type { Judged } from "./failedsteps.js"; import { mergeCall } from "./mergecall.js"; -import type { Calls } from "./calls.js"; +import type { Calls, Say } from "./calls.js"; /** The verbs of the seat, in its definition's order. */ export const VERBS: readonly string[] = Object.values(GIT_VERBS); @@ -59,6 +59,8 @@ export interface SeatDeps { watched: () => ReadonlyMap>; /** The state calls (calls.ts): each merge call written before its first merge (design 47 H §766). */ calls?: Calls; + /** Says merge-called on the seat (mesh-sdk runtime.seatPublish; design 47 H §736). */ + say?: Say; } /** A failure as the contract says it: the forge's or this holder's refusal, or a forge that failed. */ diff --git a/modules/gitea/test/calls.test.ts b/modules/gitea/test/calls.test.ts index 27a0c8f21..d45afdb06 100644 --- a/modules/gitea/test/calls.test.ts +++ b/modules/gitea/test/calls.test.ts @@ -60,3 +60,45 @@ test("a call past call-retention is deleted on a new call's write, oldest first; assert.deepEqual([...kv.keys()].sort(), ["call.2222222222222222", "call.3333333333333333"]); assert.deepEqual((await calls.all()).map((c) => c.call), ["3333333333333333", "2222222222222222"], "newest first"); }); + +// **The merge call says one event** (design 47 H §736, §766; test 111's stop case): merge-called on merge-called. with +// the landed members, each with its base and merge commit; a call that landed none says none; at the start a call whose +// event is not said (a stop after the second of three merges) says one naming the two that landed, and is kept said. +test("a call says one merge-called of what landed; none for a call that landed none; the start says an unsaid one", async () => { + const { mergeCall } = await import("../dist/mergecall.js"); + const { Calls, sayUnsaid } = await import("../dist/calls.js"); + const { runAs } = await import("@novox/mesh-sdk/callers"); + const said: { subject: string; body: any; id?: string }[] = []; + const say = async (subject: string, body: unknown, id?: string) => { said.push({ subject, body, id }); }; + const { st } = memory(); + const calls = new Calls(st as any); + const pulls: Record = { 5: { head: A, merged: false, state: "open" }, 9: { head: B, merged: false, state: "closed" } }; + const forge = { + getPullRequest: async (_o: string, _r: string, n: number) => ({ number: n, state: pulls[n].state, merged: pulls[n].merged, base: "main", + head_sha: pulls[n].head, ...(pulls[n].merged ? { merge_commit_sha: String(n).repeat(40) } : {}) }), + mergePullRequest: async (_o: string, _r: string, n: number) => { pulls[n].merged = true; }, + commitParents: async () => [], + }; + const deps = (call: string) => ({ registered: () => true, watched: () => new Map([["novox/app", new Set(["main"])]]), + judge: async () => null, newCall: () => call, calls, say, now: () => new Date("2026-11-20T03:00:00Z") }); + const run = (call: string, numbers: number[]) => { + const args = { by: "lead@shanks", pulls: numbers.map((n) => ({ repository: "novox/app", number: n, head: pulls[n].head })) }; + return runAs({ "mesh/caller": "shanks.claude-code" }, args, () => mergeCall(forge as any, args, deps(call) as any)); + }; + await run("00000000000000aa", [5, 9]); // refused at judging: no event + assert.deepEqual(said, []); + await run("00000000000000bb", [5]); + assert.equal(said.length, 1); + assert.deepEqual([said[0].subject, said[0].id], ["mesh.seat.git.event.merge-called.00000000000000bb", "merge-called.00000000000000bb"]); + assert.deepEqual(said[0].body, { call: "00000000000000bb", by: "lead@shanks", account: "shanks.claude-code", at: "2026-11-20T03:00:00.000Z", + members: [{ repository: "novox/app", number: 5, branch: "main", head: A, commit: "5".repeat(40) }] }); + assert.equal((await calls.get("00000000000000bb"))?.said, true); + // A stop after the second of three merges: kept, not said. + const m = (n: number, state: string) => ({ repository: "novox/app", number: n, branch: "main", head: A, state, ...(state === "merged" ? { commit: String(n).repeat(40) } : {}) }); + await calls.put("00000000000000cc", { by: "lead@shanks", account: "x", at: "2026-11-20T03:01:00Z", members: [m(1, "merged"), m(2, "merged"), m(3, "judged")], said: false } as any); + said.length = 0; + assert.deepEqual(await sayUnsaid(calls, say), ["00000000000000cc"]); + assert.deepEqual(said.map((s) => s.body.members.map((x: any) => x.number)), [[1, 2]]); + assert.equal(said[0].body.refused, undefined, "a member not merged after a stop is no refused member"); + assert.deepEqual(await sayUnsaid(calls, say), [], "said once"); +}); -- 2.54.0 From 3ba309c5e40f9dab0affe6ac112bf8d617a14e56 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:46:59 +0100 Subject: [PATCH 24/43] FORGE-4: a member the forge refuses after judging raises merge-partial MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The call stops merging, says merge-called with what landed and refused, and raises merge-partial (scope delivery, id the call, urgent, resolver operator) in the module's findings; a later call landing every refused member clears it, and forge_partial_clear clears one by hand after the hand-act log took it. Builds: design 47 H §767; H-A-1, H-A-18, H-A-81 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/gitea/index.ts | 6 +- modules/gitea/mergecall.ts | 9 +++ modules/gitea/partials.ts | 78 ++++++++++++++++++++++++ modules/gitea/seat.ts | 3 + modules/gitea/test/calls.test.ts | 12 +--- modules/gitea/test/memstate.ts | 11 ++++ modules/gitea/test/merge-partial.test.ts | 67 ++++++++++++++++++++ modules/gitea/tools/calls.ts | 33 ++++++++++ 8 files changed, 207 insertions(+), 12 deletions(-) create mode 100644 modules/gitea/partials.ts create mode 100644 modules/gitea/test/memstate.ts create mode 100644 modules/gitea/test/merge-partial.test.ts create mode 100644 modules/gitea/tools/calls.ts diff --git a/modules/gitea/index.ts b/modules/gitea/index.ts index 6096a9720..11bf2f3d9 100644 --- a/modules/gitea/index.ts +++ b/modules/gitea/index.ts @@ -51,6 +51,8 @@ import { REGISTRATION_CHANGED_EVENT, Registrations } from "./registrations.js"; import { FailedSteps, Heads, readHeads } from "./failedsteps.js"; import { failedStepsTools } from "./tools/failedsteps.js"; import { Calls, callRetentionDays, sayUnsaid } from "./calls.js"; +import { Partials } from "./partials.js"; +import { callTools } from "./tools/calls.js"; import { seatPublish } from "@novox/mesh-sdk/runtime"; import { seat as GIT, events as GIT_EVENTS, type BranchMovedEvent, type PullClosedEvent, type PullMergedEvent, type PullUpdatedEvent } from "@novox/mesh-delivery-contracts-git"; import { seat as RUNNER, events as RUNNER_EVENTS, type StepEndedEvent } from "@novox/mesh-delivery-contracts-mesh-pipeline-runner"; @@ -405,6 +407,7 @@ let reloadHeads: () => Promise = async () => {}; // The merge calls, each written before its first merge (calls.ts; design 47 H §766). const calls = new Calls(state("calls"), () => callRetentionDays(), (line) => console.log(`[gitea] ${line}`)); // At the start, every call whose merge-called is not said says it for what landed (§766). +const partials = new Partials(state("findings"), calls, (line) => console.log(`[gitea] ${line}`)); void sayUnsaid(calls, seatPublish, (line) => console.log(`[gitea] ${line}`)).catch((err) => console.error(`[gitea] the merge calls not said could not be said at the start — ${err instanceof Error ? err.message : err}`)); const failedSteps = new FailedSteps(state("failed-steps"), (line) => console.log(`[gitea] ${line}`), (commit) => heldHeads.has(commit)); @@ -418,7 +421,8 @@ void failedSteps.expireSeen().catch((err) => console.log(`[gitea] the seen keys registerModuleTools("gitea", () => [eventsTool(pace), ...readAllTools(readAll), ...writeTools(writes), ...statusTools("gitea", "gitea_first_views", "gitea_first_view_again", followers, firstViews, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }), ...(gitea ? seatTools(gitea, { run, container: FORGE_CONTAINER, statuses: gates, registered: () => registrations.written, judge: (head) => failedSteps.judge(head), - watched: () => registrations.watched, calls, say: seatPublish }) : []), + watched: () => registrations.watched, calls, say: seatPublish, partials }) : []), + ...callTools(partials, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }), ...failedStepsTools(failedSteps, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); })]); /** What the forge's event asks of the announcer at once (issue 729, 1422): see watch.ts. */ diff --git a/modules/gitea/mergecall.ts b/modules/gitea/mergecall.ts index 11c772c29..8525d1645 100644 --- a/modules/gitea/mergecall.ts +++ b/modules/gitea/mergecall.ts @@ -14,6 +14,7 @@ import type { GiteaClient } from "./client.js"; import { Refused, checkName } from "./guard.js"; import type { Judged } from "./failedsteps.js"; import { keptMember, sayCall, type Calls, type Say } from "./calls.js"; +import type { Partials } from "./partials.js"; /** What a merge call reaches beside the forge's API. */ export interface CallDeps { @@ -30,6 +31,8 @@ export interface CallDeps { now?: () => Date; /** Says a seat event on its subject under an id the bus keeps once (mesh-sdk runtime.seatPublish): merge-called (§736). */ say?: Say; + /** The findings merge-partial (partials.ts; §767). */ + partials?: Partials; } /** The forge's API a merge call uses. */ @@ -164,6 +167,8 @@ export async function mergeCall(forge: CallForge, args: Record, rest.why = `not merged: ${m.repository}#${m.number} before it was refused by the forge`; } await keep(false); + // Raised for the refused members, the landed ones delivered as any call's (§767). + await deps.partials?.raise({ ...value(false), call }); break; } } @@ -176,5 +181,9 @@ export async function mergeCall(forge: CallForge, args: Record, console.error(`[gitea] the merge call ${call} landed and its merge-called is not said yet; it is said at the next start — ${err instanceof Error ? err.message : err}`); } } + // A call that landed members clears every merge-partial whose refused members are now all landed (§767). + if (members.some((m) => m.state === "merged")) { + await deps.partials?.landed().catch((err) => console.error(`[gitea] the merge-partial findings could not be judged after the call ${call} — ${err instanceof Error ? err.message : err}`)); + } return { call, members: members.map(outcomeOf) }; } diff --git a/modules/gitea/partials.ts b/modules/gitea/partials.ts new file mode 100644 index 000000000..25d0ea0bd --- /dev/null +++ b/modules/gitea/partials.ts @@ -0,0 +1,78 @@ +// **A member refused after every member was judged is said and raised** (novox/hq design 47 H §767; H-A-1, H-A-18, +// H-A-81): when the forge refuses a member after all were judged, the call stops merging and the forge module raises the +// finding `merge-partial`, scope `delivery`, id the call, naming each refused member, severity urgent, resolver operator, +// in its own state `findings`, which the condition keeper reads. It clears it when a later call lands every refused member +// (read from its own `calls`), or by its tool `forge_partial_clear` with `call` and `why`, written in the hand-act log. + +import type { State } from "@novox/mesh-sdk/state"; +import { findingKey } from "./catalog.js"; +import type { Calls, KeptCall } from "./calls.js"; + +export const MERGE_PARTIAL = "merge-partial"; +const SCOPE = "delivery"; + +/** The key of a call's merge-partial in the state findings. */ +export const partialKey = (call: string): string => findingKey(SCOPE, call, MERGE_PARTIAL); + +/** A member's name in a finding, `#`. */ +const named = (m: { repository: string; number: number }) => `${m.repository}#${m.number}`; + +/** The merge-partial of a call the forge refused a member of after judging, as the condition keeper reads it. */ +export function partialValue(c: KeptCall, observer = "gitea"): { key: string; value: Record } { + const key = partialKey(c.call); + const landed = c.members.filter((m) => m.state === "merged").map(named); + const refused = c.members.filter((m) => m.state === "refused"); + const summary = `the merge call ${c.call} by ${c.by} landed ${landed.length ? landed.join(", ") : "none"} and not ` + + refused.map((m) => `${named(m)} (${m.why ?? "refused"})`).join(", "); + return { key, value: { + v: 1, key, kind: MERGE_PARTIAL, subject: { scope: SCOPE, id: c.call }, severity: "urgent", resolver: "operator", summary, + headline: `A merge call landed in part: ${refused.map(named).join(", ")} not merged`, + explanation: `The forge refused ${refused.map(named).join(", ")} after every member of the merge call ${c.call} was judged, so ` + + `the repositories stand half-merged. A later merge call that lands them clears this, or forge_partial_clear with why.`, + resolved: "", evidence: [{ at: c.at, said: summary }], source: `${observer}.${MERGE_PARTIAL}`, observer, at: c.at, + } }; +} + +export class Partials { + constructor(private readonly findings: State, private readonly calls: Calls, + private readonly log: (line: string) => void = () => {}) {} + + /** Raises merge-partial for a call the forge refused a member of after judging. */ + async raise(c: KeptCall): Promise { + const f = partialValue(c); + await this.findings.put(f.key, f.value); + this.log(`merge-partial raised: ${String(f.value.summary)}`); + } + + /** The calls whose merge-partial is held, newest first. */ + async held(): Promise { + const out: KeptCall[] = []; + for (const c of await this.calls.all()) if ((await this.findings.get(partialKey(c.call)))?.value) out.push(c); + return out; + } + + /** After a call landed members: every held merge-partial whose refused members a later call landed, each, is cleared. + * Answers the calls cleared. */ + async landed(): Promise { + const all = await this.calls.all(); // newest first + const cleared: string[] = []; + for (const [i, c] of all.entries()) { + if (!(await this.findings.get(partialKey(c.call)))?.value) continue; + const later = new Set(all.slice(0, i).flatMap((x) => x.members.filter((m) => m.state === "merged").map(named))); + const refused = c.members.filter((m) => m.state === "refused").map(named); + if (refused.every((r) => later.has(r))) { + await this.findings.delete(partialKey(c.call)); + cleared.push(c.call); + this.log(`merge-partial of ${c.call} cleared: a later call landed ${refused.join(", ")}`); + } + } + return cleared; + } + + /** Clears one call's merge-partial by hand (forge_partial_clear); answers whether one was held. */ + async clear(call: string): Promise { + const held = !!(await this.findings.get(partialKey(call)))?.value; + if (held) await this.findings.delete(partialKey(call)); + return held; + } +} diff --git a/modules/gitea/seat.ts b/modules/gitea/seat.ts index baa7e5bb1..2e9cda399 100644 --- a/modules/gitea/seat.ts +++ b/modules/gitea/seat.ts @@ -28,6 +28,7 @@ import { appendNote, viewBody, viewComment, type Runner } from "./delivery.js"; import type { Judged } from "./failedsteps.js"; import { mergeCall } from "./mergecall.js"; import type { Calls, Say } from "./calls.js"; +import type { Partials } from "./partials.js"; /** The verbs of the seat, in its definition's order. */ export const VERBS: readonly string[] = Object.values(GIT_VERBS); @@ -61,6 +62,8 @@ export interface SeatDeps { calls?: Calls; /** Says merge-called on the seat (mesh-sdk runtime.seatPublish; design 47 H §736). */ say?: Say; + /** The findings merge-partial (partials.ts; design 47 H §767). */ + partials?: Partials; } /** A failure as the contract says it: the forge's or this holder's refusal, or a forge that failed. */ diff --git a/modules/gitea/test/calls.test.ts b/modules/gitea/test/calls.test.ts index d45afdb06..5caeb15b0 100644 --- a/modules/gitea/test/calls.test.ts +++ b/modules/gitea/test/calls.test.ts @@ -1,5 +1,6 @@ import assert from "node:assert/strict"; import { test } from "node:test"; +import { memory } from "./memstate.ts"; // **The call is written before its first merge** (novox/hq design 47 H §766; H-A-18, H-A-47, H-A-71): the key // call. of the state calls holds by, the proved account, at, each member with its base and its state (judged before @@ -8,17 +9,6 @@ import { test } from "node:test"; const A = "a".repeat(40), B = "b".repeat(40); -export function memory() { - const kv = new Map(); - return { kv, st: { - get: async (k: string) => (kv.has(k) ? { key: k, value: structuredClone(kv.get(k)), revision: 1 } : null), - put: async (k: string, v: unknown) => { kv.set(k, structuredClone(v)); return 1; }, - delete: async (k: string) => { kv.delete(k); }, - keys: async () => [...kv.keys()].sort(), - watch: async () => {}, - } }; -} - test("a merge call is kept before its first merge, each member judged, then merged with its commit", async () => { const { mergeCall } = await import("../dist/mergecall.js"); const { Calls } = await import("../dist/calls.js"); diff --git a/modules/gitea/test/memstate.ts b/modules/gitea/test/memstate.ts new file mode 100644 index 000000000..afddeed9c --- /dev/null +++ b/modules/gitea/test/memstate.ts @@ -0,0 +1,11 @@ +// A state of the runtime in memory, for the tests of the merge calls (calls.ts, partials.ts). +export function memory() { + const kv = new Map(); + return { kv, st: { + get: async (k: string) => (kv.has(k) ? { key: k, value: structuredClone(kv.get(k)), revision: 1 } : null), + put: async (k: string, v: unknown) => { kv.set(k, structuredClone(v)); return 1; }, + delete: async (k: string) => { kv.delete(k); }, + keys: async () => [...kv.keys()].sort(), + watch: async () => {}, + } }; +} diff --git a/modules/gitea/test/merge-partial.test.ts b/modules/gitea/test/merge-partial.test.ts new file mode 100644 index 000000000..fbb1b1ffd --- /dev/null +++ b/modules/gitea/test/merge-partial.test.ts @@ -0,0 +1,67 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { memory } from "./memstate.ts"; + +// **A member refused after every member was judged is said and raised** (novox/hq design 47 H §767; acceptance test 115; +// H-A-81): a forge that refuses the second merge gives one merge-called with the first member and `refused`, and +// merge-partial (scope delivery, id the call, urgent, resolver operator) raised; a later call landing the refused member +// clears it, and forge_partial_clear clears one by hand after the hand-act log took it. + +const A = "a".repeat(40), B = "b".repeat(40); + +test("the forge refusing the second merge says what landed, raises merge-partial, and a later call landing it clears it", async () => { + const { mergeCall } = await import("../dist/mergecall.js"); + const { Calls } = await import("../dist/calls.js"); + const { Partials, partialKey } = await import("../dist/partials.js"); + const { callTools } = await import("../dist/tools/calls.js"); + const { runAs } = await import("@novox/mesh-sdk/callers"); + const { st } = memory(); + const findings = memory(); + const calls = new Calls(st as any); + const partials = new Partials(findings.st as any, calls); + const said: any[] = []; + const pulls: Record = { 5: { head: A, merged: false, refuse: false }, 9: { head: B, merged: false, refuse: true } }; + const forge = { + getPullRequest: async (_o: string, _r: string, n: number) => ({ number: n, state: "open", merged: pulls[n].merged, base: "main", + head_sha: pulls[n].head, ...(pulls[n].merged ? { merge_commit_sha: String(n).repeat(40) } : {}) }), + mergePullRequest: async (_o: string, r: string, n: number) => { + if (pulls[n].refuse) throw new Error(`Gitea API POST /repos/novox/${r}/pulls/${n}/merge: 409 merge conflict`); + pulls[n].merged = true; + }, + commitParents: async () => [], + }; + let clock = Date.parse("2026-11-20T03:00:00Z"); + const run = (call: string, numbers: number[]) => { + const args = { by: "lead@shanks", pulls: numbers.map((n) => ({ repository: n === 5 ? "novox/app" : "novox/lib", number: n, head: pulls[n].head })) }; + return runAs({ "mesh/caller": "shanks.claude-code" }, args, () => mergeCall(forge as any, args, { registered: () => true, + watched: () => new Map([["novox/app", new Set(["main"])], ["novox/lib", new Set(["main"])]]), judge: async () => null, + newCall: () => call, calls, partials, say: async (_s: string, body: unknown) => { said.push(body); }, + now: () => new Date((clock += 1000)) } as any)) as Promise; + }; + const got = await run("00000000000000aa", [5, 9]); + assert.deepEqual(got.members.map((m: any) => m.state), ["merged", "refused"]); + assert.equal(said.length, 1); + assert.deepEqual(said[0].members.map((m: any) => m.number), [5]); + assert.deepEqual(said[0].refused.map((m: any) => [m.repository, m.number]), [["novox/lib", 9]]); + assert.match(said[0].refused[0].why, /merge conflict/); + const raised: any = findings.kv.get(partialKey("00000000000000aa")); + assert.deepEqual([raised?.kind, raised?.subject, raised?.severity, raised?.resolver], ["merge-partial", { scope: "delivery", id: "00000000000000aa" }, "urgent", "operator"]); + assert.match(raised.summary, /landed novox\/app#5 and not novox\/lib#9/); + // A later call landing the refused member clears it. + pulls[9].refuse = false; + await run("00000000000000bb", [9]); + assert.equal(findings.kv.has(partialKey("00000000000000aa")), false, "cleared by the later call"); + // By hand: written in the hand-act log first, then cleared. + pulls[9] = { head: B, merged: false, refuse: true }; + pulls[5] = { head: A, merged: false, refuse: false }; + await run("00000000000000cc", [5, 9]); + assert.equal(findings.kv.has(partialKey("00000000000000cc")), true); + const acts: any[] = []; + const tool = callTools(partials, async (a) => { acts.push(a); }).find((t) => t.name === "forge_partial_clear")!; + await assert.rejects(tool.run({ call: "00000000000000cc" }), /names the call .* and why/); + const failing = callTools(partials, async () => { throw new Error("down"); }).find((t) => t.name === "forge_partial_clear")!; + await assert.rejects(failing.run({ call: "00000000000000cc", why: "dropped" }), /not recorded, so nothing was cleared/); + assert.equal(findings.kv.has(partialKey("00000000000000cc")), true); + const done: any = await runAs({ "mesh/caller": "person.jochen" }, {}, () => tool.run({ call: "00000000000000cc", why: "the lib change is dropped" })); + assert.deepEqual([done.cleared, acts[0].verb, findings.kv.has(partialKey("00000000000000cc"))], [true, "forge_partial_clear", false]); +}); diff --git a/modules/gitea/tools/calls.ts b/modules/gitea/tools/calls.ts new file mode 100644 index 000000000..15ddaa5a4 --- /dev/null +++ b/modules/gitea/tools/calls.ts @@ -0,0 +1,33 @@ +// The tools of the forge module's merge calls (novox/hq design 47 H §766, §767): forge_partial_clear clears a call's +// merge-partial by hand, written in the hand-act log before it is done. + +import type { ToolDefinition } from "@novox/mesh-sdk/tools"; +import { callerOf } from "@novox/mesh-sdk/callers"; +import type { HandAct } from "@novox/mesh-sdk/views"; +import type { Partials } from "../partials.js"; + +const word = (v: unknown): string => (typeof v === "string" ? v.trim() : ""); + +export function callTools(partials: Partials, acts: (act: HandAct) => Promise): ToolDefinition[] { + return [ + { + name: "forge_partial_clear", + description: "Clears the finding merge-partial of one merge call (a call the forge refused a member of after every member was judged), " + + "with why, when its refused members are not to be merged. Written in the hand-act log first (design 47 H §767).", + input: { type: "object", required: ["call", "why"], properties: { + call: { type: "string", description: "the merge call, 16 lowercase hex characters" }, + why: { type: "string", description: "why it is cleared, in plain words" } } }, + run: async (args: Record) => { + const call = word(args.call), why = word(args.why); + if (!/^[0-9a-f]{16}$/.test(call) || !why) throw new Error("forge_partial_clear names the call (16 lowercase hex characters) and why"); + const by = callerOf(args); + try { + await acts({ what: `cleared the merge-partial of the merge call ${call}`, why: `${why} (${by})`, cause: "merge-partial-cleared", verb: "forge_partial_clear" }); + } catch (err) { + throw new Error(`the hand act was not recorded, so nothing was cleared: ${err instanceof Error ? err.message : String(err)}`); + } + return { call, cleared: await partials.clear(call), why, by }; + }, + }, + ]; +} -- 2.54.0 From 0400d7bbe7a2c6f4bf65b5242b993a2c508b28aa Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:47:41 +0100 Subject: [PATCH 25/43] FORGE-5: forge_calls reads the merge calls; forge_call_say_again says one again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit forge_calls answers the kept calls newest first with each member's base, head and state; forge_call_say_again, with call and why, says a call's merge-called again once the hand-act log took it. Builds: design 47 H §766 (its tools); H-A-18 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/gitea/index.ts | 2 +- modules/gitea/test/calls.test.ts | 25 +++++++++++++ modules/gitea/test/merge-partial.test.ts | 4 +-- modules/gitea/tools/calls.ts | 45 ++++++++++++++++++++++-- 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/modules/gitea/index.ts b/modules/gitea/index.ts index 11bf2f3d9..03d24ebd4 100644 --- a/modules/gitea/index.ts +++ b/modules/gitea/index.ts @@ -422,7 +422,7 @@ registerModuleTools("gitea", () => [eventsTool(pace), ...readAllTools(readAll), async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }), ...(gitea ? seatTools(gitea, { run, container: FORGE_CONTAINER, statuses: gates, registered: () => registrations.written, judge: (head) => failedSteps.judge(head), watched: () => registrations.watched, calls, say: seatPublish, partials }) : []), - ...callTools(partials, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }), + ...callTools(calls, partials, seatPublish, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }), ...failedStepsTools(failedSteps, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); })]); /** What the forge's event asks of the announcer at once (issue 729, 1422): see watch.ts. */ diff --git a/modules/gitea/test/calls.test.ts b/modules/gitea/test/calls.test.ts index 5caeb15b0..fddb0574a 100644 --- a/modules/gitea/test/calls.test.ts +++ b/modules/gitea/test/calls.test.ts @@ -92,3 +92,28 @@ test("a call says one merge-called of what landed; none for a call that landed n assert.equal(said[0].body.refused, undefined, "a member not merged after a stop is no refused member"); assert.deepEqual(await sayUnsaid(calls, say), [], "said once"); }); + +// The merge calls' tools (design 47 H §766): forge_calls answers the calls newest first with each member's state, and +// forge_call_say_again says a call's merge-called again once the hand-act log took it. +test("forge_calls reads the calls newest first; forge_call_say_again says one again after the hand-act log", async () => { + const { Calls } = await import("../dist/calls.js"); + const { Partials } = await import("../dist/partials.js"); + const { callTools } = await import("../dist/tools/calls.js"); + const { st } = memory(); + const calls = new Calls(st as any); + const m = (n: number, state: string) => ({ repository: "novox/app", number: n, branch: "main", head: A, state, ...(state === "merged" ? { commit: String(n).repeat(40) } : {}) }); + await calls.put("00000000000000aa", { by: "a@b", account: "x", at: "2026-11-20T01:00:00Z", members: [m(1, "merged")], said: true } as any); + await calls.put("00000000000000bb", { by: "a@b", account: "x", at: "2026-11-20T02:00:00Z", members: [m(2, "refused")], said: true } as any); + const said: any[] = [], acts: any[] = []; + const tools = callTools(calls, new Partials(memory().st as any, calls), async (subject: string, body: unknown) => { said.push([subject, body]); }, + async (a) => { acts.push(a); }); + const tool = (name: string) => tools.find((t) => t.name === name)!; + const read: any = await tool("forge_calls").run({}); + assert.deepEqual(read.calls.map((c: any) => [c.call, c.members[0].state]), [["00000000000000bb", "refused"], ["00000000000000aa", "merged"]]); + await assert.rejects(tool("forge_call_say_again").run({ call: "00000000000000aa" }), /names the call .* and why/); + await assert.rejects(tool("forge_call_say_again").run({ call: "00000000000000ff", why: "x" }), /keeps no merge call/); + const again: any = await tool("forge_call_say_again").run({ call: "00000000000000aa", why: "the gate missed it" }); + assert.deepEqual([again.said, acts[0].verb, said[0][0]], [true, "forge_call_say_again", "mesh.seat.git.event.merge-called.00000000000000aa"]); + const none: any = await tool("forge_call_say_again").run({ call: "00000000000000bb", why: "x" }); + assert.equal(none.said, false, "a call that landed none says none"); +}); diff --git a/modules/gitea/test/merge-partial.test.ts b/modules/gitea/test/merge-partial.test.ts index fbb1b1ffd..12d6a5b7d 100644 --- a/modules/gitea/test/merge-partial.test.ts +++ b/modules/gitea/test/merge-partial.test.ts @@ -57,9 +57,9 @@ test("the forge refusing the second merge says what landed, raises merge-partial await run("00000000000000cc", [5, 9]); assert.equal(findings.kv.has(partialKey("00000000000000cc")), true); const acts: any[] = []; - const tool = callTools(partials, async (a) => { acts.push(a); }).find((t) => t.name === "forge_partial_clear")!; + const tool = callTools(calls, partials, async () => {}, async (a) => { acts.push(a); }).find((t) => t.name === "forge_partial_clear")!; await assert.rejects(tool.run({ call: "00000000000000cc" }), /names the call .* and why/); - const failing = callTools(partials, async () => { throw new Error("down"); }).find((t) => t.name === "forge_partial_clear")!; + const failing = callTools(calls, partials, async () => {}, async () => { throw new Error("down"); }).find((t) => t.name === "forge_partial_clear")!; await assert.rejects(failing.run({ call: "00000000000000cc", why: "dropped" }), /not recorded, so nothing was cleared/); assert.equal(findings.kv.has(partialKey("00000000000000cc")), true); const done: any = await runAs({ "mesh/caller": "person.jochen" }, {}, () => tool.run({ call: "00000000000000cc", why: "the lib change is dropped" })); diff --git a/modules/gitea/tools/calls.ts b/modules/gitea/tools/calls.ts index 15ddaa5a4..5737a80eb 100644 --- a/modules/gitea/tools/calls.ts +++ b/modules/gitea/tools/calls.ts @@ -1,15 +1,54 @@ -// The tools of the forge module's merge calls (novox/hq design 47 H §766, §767): forge_partial_clear clears a call's -// merge-partial by hand, written in the hand-act log before it is done. +// The tools of the forge module's merge calls (novox/hq design 47 H §766, §767): forge_calls reads the calls, newest first, +// with each member's state; forge_call_say_again says a call's merge-called again; forge_partial_clear clears a call's +// merge-partial by hand. Each act is written in the hand-act log before it is done. import type { ToolDefinition } from "@novox/mesh-sdk/tools"; import { callerOf } from "@novox/mesh-sdk/callers"; import type { HandAct } from "@novox/mesh-sdk/views"; import type { Partials } from "../partials.js"; +import { sayCall, type Calls, type Say } from "../calls.js"; const word = (v: unknown): string => (typeof v === "string" ? v.trim() : ""); -export function callTools(partials: Partials, acts: (act: HandAct) => Promise): ToolDefinition[] { +export function callTools(calls: Calls, partials: Partials, say: Say, acts: (act: HandAct) => Promise): ToolDefinition[] { return [ + { + name: "forge_calls", + description: "The merge calls the forge module keeps (its state calls), newest first: each call's by, account, when, whether its merge-called " + + "was said, and each member with its base, head and state (judged, merged with its commit, or refused with why) (design 47 H §766).", + input: { type: "object", properties: { call: { type: "string", description: "only this merge call" } } }, + run: async (args: Record) => { + const only = word(args.call); + const all = await calls.all(); + return { calls: only ? all.filter((c) => c.call === only) : all }; + }, + }, + { + name: "forge_call_say_again", + description: "Says one merge call's merge-called again, for what it landed, with why: a gate that missed it takes the call in. " + + "Written in the hand-act log first (design 47 H §766).", + input: { type: "object", required: ["call", "why"], properties: { + call: { type: "string", description: "the merge call, 16 lowercase hex characters" }, + why: { type: "string", description: "why it is said again, in plain words" } } }, + run: async (args: Record) => { + const call = word(args.call), why = word(args.why); + if (!/^[0-9a-f]{16}$/.test(call) || !why) throw new Error("forge_call_say_again names the call (16 lowercase hex characters) and why"); + const kept = await calls.get(call); + if (!kept) throw new Error(`the forge module keeps no merge call ${call}`); + const by = callerOf(args); + try { + await acts({ what: `said the merge-called of the merge call ${call} again`, why: `${why} (${by})`, cause: "merge-called-said-again", verb: "forge_call_say_again" }); + } catch (err) { + throw new Error(`the hand act was not recorded, so nothing was said: ${err instanceof Error ? err.message : String(err)}`); + } + const said = await sayCall(kept, say); + if (said && !kept.said) { + const { call: _c, ...value } = kept; + await calls.put(call, { ...value, said: true }); + } + return { call, said, why, by, ...(said ? {} : { "said-none": "the call landed no member, so it says no merge-called" }) }; + }, + }, { name: "forge_partial_clear", description: "Clears the finding merge-partial of one merge call (a call the forge refused a member of after every member was judged), " + -- 2.54.0 From f85f639afe86e09bf6a0dea70d32defc74a18407 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:49:12 +0100 Subject: [PATCH 26/43] FORGE-1: failed-steps reads the body's fingerprint; a step-ended without it writes step-unread MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The forge module keeps a step's failure at the step-ended body's fingerprint alone, consumes step-ended on its .. subject, and writes step-unread (scope module, keyed .., warning, resolver operator) for a step-ended without its fingerprint, cleared by the step's later step-ended with it. Builds: design 47 H §735 (failed-steps from step-ended); G §585; H-A-18, H-A-47 (FORGE-1 builds step-unread), H-A-93 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/gitea/failedsteps.ts | 49 ++++++++++++++++++++++++-- modules/gitea/index.ts | 7 ++-- modules/gitea/module.json | 2 +- modules/gitea/test/failedsteps.test.ts | 27 +++++++++++--- 4 files changed, 75 insertions(+), 10 deletions(-) diff --git a/modules/gitea/failedsteps.ts b/modules/gitea/failedsteps.ts index 84473b12f..8e69a721b 100644 --- a/modules/gitea/failedsteps.ts +++ b/modules/gitea/failedsteps.ts @@ -19,6 +19,7 @@ import { failedStepsByModuleFingerprintStepKey as failedKey, type FailedStep, type PullClosedEvent, type PullMergedEvent, type PullUpdatedEvent, } from "@novox/mesh-delivery-contracts-git"; import type { StepEndedEvent } from "@novox/mesh-delivery-contracts-mesh-pipeline-runner"; +import { findingKey } from "./catalog.js"; /** The key prefix of the commits' fingerprints (§487). */ export const SEEN = "seen."; @@ -58,6 +59,22 @@ const safe = (s: string): string => s.replace(/[^A-Za-z0-9_=/-]/g, "_"); export const seenKey = (commit: string, module: string): string => `${SEEN}${commit}.${safe(module)}`; export const failedStepKey = (module: string, fingerprint: string, step: string): string => failedKey(safe(module), safe(fingerprint), safe(step)); +/** The finding of a step-ended the forge module could not record (G §585). */ +export const STEP_UNREAD = "step-unread"; + +/** step-unread of one step of one run (H-A-93): its key in the state findings and its value. */ +export function stepUnreadFinding(run: string, module: string, step: string, at: string): { key: string; value: Record } { + const key = findingKey("module", `${run}.${module}.${step}`, STEP_UNREAD); + const summary = `the step-ended of ${step} of ${module} in run ${run} names no build-input fingerprint, so a failure of it cannot be recorded`; + return { key, value: { + v: 1, key, kind: STEP_UNREAD, subject: { scope: "module", id: module }, severity: "warning", resolver: "operator", summary, + headline: `A step the forge module could not read: ${step} of ${module}`, + explanation: `merge-pull judges a head by the failed required steps at its fingerprint; this step-ended of run ${run} carries no fingerprint, ` + + "so the forge module could not keep it. A later step-ended of the step with its fingerprint clears this.", + resolved: "", evidence: [{ at, said: summary }], source: `gitea.${STEP_UNREAD}`, observer: "gitea", at, + } }; +} + export class FailedSteps { /** Every act runs after the one before it, so a step-ended and a head's loss never interleave their reads and writes. */ private chain: Promise = Promise.resolve(); @@ -66,7 +83,9 @@ export class FailedSteps { /** Whether a commit is now a watched branch's head or an open pull request's head. */ private readonly current: (commit: string) => boolean = () => false, /** The state's fill against its caps: the bundle's state call (§623). */ - private readonly fillOf: () => Promise = askFill) {} + private readonly fillOf: () => Promise = askFill, + /** The module's state findings, where step-unread is written (G §585, H-A-93); none writes no finding. */ + private readonly findings?: State) {} private turn(act: () => Promise): Promise { const r = this.chain.then(act, act); @@ -98,9 +117,13 @@ export class FailedSteps { private record(ev: StepEndedEvent): Promise { const r = ev?.result; - const fingerprint = r?.fingerprint || ev?.fingerprint || ""; - if (!r || !SHA.test(String(ev.commit ?? "")) || !fingerprint || !ev.module) return Promise.resolve(); + // The fingerprint is the body's, the build-input fingerprint the runner fills in every step-ended; no field of the step's + // result is read for it (G §585, H-A-93). + const fingerprint = ev?.fingerprint || ""; + if (!r || !SHA.test(String(ev.commit ?? "")) || !ev.module) return Promise.resolve(); + if (!fingerprint) return this.turn(() => this.unread(ev, true)); return this.turn(async () => { + await this.unread(ev, false); const at = ev.at ?? new Date().toISOString(); const sk = seenKey(ev.commit, ev.module); const held = (await this.st.get(sk))?.value as Seen | undefined; @@ -137,6 +160,26 @@ export class FailedSteps { }); } + /** + * step-unread (G §585, H-A-93): a step-ended without its fingerprint writes the finding, scope module, its subject the step's + * module, keyed `..`, naming the run, the module and the step, severity warning, resolver operator; a + * later step-ended of that step with its fingerprint clears it, so a failure that could not be recorded is seen. + */ + private async unread(ev: StepEndedEvent, raise: boolean): Promise { + if (!this.findings) return; + const step = String(ev.result?.step ?? ""), run = String(ev.pipeline ?? ""); + const { key, value } = stepUnreadFinding(run, ev.module, step, String(ev.at ?? new Date().toISOString())); + if (!raise) { + if ((await this.findings.get(key))?.value) { + await this.findings.delete(key); + this.log(`${ev.module} ${step} of ${run} is read with its fingerprint: step-unread cleared`); + } + return; + } + await this.findings.put(key, value); + this.log(String(value.summary)); + } + /** The failed `required` step that refuses this head, or null: a head with a `seen` key is judged by it, one with none merges (§487). */ judge(head: string): Promise { return this.turn(async () => { diff --git a/modules/gitea/index.ts b/modules/gitea/index.ts index 03d24ebd4..793738a33 100644 --- a/modules/gitea/index.ts +++ b/modules/gitea/index.ts @@ -410,7 +410,8 @@ const calls = new Calls(state("calls"), () => callRetentionDays(), (lin const partials = new Partials(state("findings"), calls, (line) => console.log(`[gitea] ${line}`)); void sayUnsaid(calls, seatPublish, (line) => console.log(`[gitea] ${line}`)).catch((err) => console.error(`[gitea] the merge calls not said could not be said at the start — ${err instanceof Error ? err.message : err}`)); -const failedSteps = new FailedSteps(state("failed-steps"), (line) => console.log(`[gitea] ${line}`), (commit) => heldHeads.has(commit)); +const failedSteps = new FailedSteps(state("failed-steps"), (line) => console.log(`[gitea] ${line}`), (commit) => heldHeads.has(commit), + undefined, state("findings")); // The `seen` keys past their 30 days are deleted at the start, as on each step-ended (design 47 §581). void failedSteps.expireSeen().catch((err) => console.log(`[gitea] the seen keys past 30 days could not be deleted at the start: ${err instanceof Error ? err.message : String(err)}`)); @@ -578,7 +579,9 @@ if (gitea) { const read = await readHeads(forge, registrations.watched, (line) => console.log(`[gitea] ${line}`)); await failedSteps.lost(heldHeads.replace(read.names, read.branches, read.pulls)); }; - void bindUntilHeld(`${RUNNER}.${RUNNER_EVENTS.stepEnded}`, () => on(`${RUNNER}.${RUNNER_EVENTS.stepEnded}`, (event) => failedSteps.stepEnded(event.body))); + // The runner's step-ended is said on step-ended... (the seat's definition). + const stepEnded = `${RUNNER}.${RUNNER_EVENTS.stepEnded}.*.*.*`; + void bindUntilHeld(stepEnded, () => on(stepEnded, (event) => failedSteps.stepEnded(event.body))); // A branch's head is read from the forge, never taken from the event (the seat's definition of branch-moved). void bindUntilHeld(`${GIT}.${GIT_EVENTS.branchMoved}`, () => on(`${GIT}.${GIT_EVENTS.branchMoved}`, async (event) => { const { repository, branch } = event.body; diff --git a/modules/gitea/module.json b/modules/gitea/module.json index 55bc097c4..9941bc4e5 100644 --- a/modules/gitea/module.json +++ b/modules/gitea/module.json @@ -654,7 +654,7 @@ "consumes": [ "mesh-catalog.catalog-revision", "mesh-catalog.registration-changed", - "mesh-pipeline-runner.step-ended", + "mesh-pipeline-runner.step-ended.*.*.*", "git.branch-moved", "git.pull-updated", "git.pull-closed", diff --git a/modules/gitea/test/failedsteps.test.ts b/modules/gitea/test/failedsteps.test.ts index f3b703a94..35347cb0c 100644 --- a/modules/gitea/test/failedsteps.test.ts +++ b/modules/gitea/test/failedsteps.test.ts @@ -20,7 +20,7 @@ function memory() { } const ended = (commit: string, outcome: string, extra: Record = {}, over: Record = {}) => ({ v: 1, pipeline: `main.7.nats-${commit.slice(0, 2)}`, module: "nats", branch: "main", commit, at: "2026-11-11T12:00:00Z", - result: { step: "unit", status: "required", outcome, fingerprint: "fp-1", ...extra }, ...over }) as any; + fingerprint: "fp-1", result: { step: "unit", status: "required", outcome, ...extra }, ...over }) as any; test("a head is refused by a required step that failed at its fingerprint in any run, until a rerun passes it", async () => { const { kv, st } = memory(); @@ -45,12 +45,13 @@ test("a head is refused by a required step that failed at its fingerprint in any // A later commit at the fingerprint is refused too, and a failed optional step refuses nothing. await f.stepEnded(ended(C3, "passed")); await judged("a later commit of the same content", C3, "run-on-request nats unit"); - await f.stepEnded(ended(C3, "failed", { step: "lint", status: "optional", fingerprint: "fp-2" })); + await f.stepEnded(ended(C3, "failed", { step: "lint", status: "optional" }, { fingerprint: "fp-2" })); await judged("a failed optional step", C3, "run-on-request nats unit"); assert.equal(kv.has(failedStepKey("nats", "fp-2", "lint")), false); - // A result that names no fingerprint is kept at the one the seat's step event carries (`fingerprint`). - await f.stepEnded(ended(C3, "failed", { step: "lint", fingerprint: undefined }, { fingerprint: "fp-3" })); + // The fingerprint is the body's alone, never the result's (G §585, H-A-93). + await f.stepEnded(ended(C3, "failed", { step: "lint", fingerprint: "fp-x" }, { fingerprint: "fp-3" })); assert.ok(kv.has(failedStepKey("nats", "fp-3", "lint")), "the event's fingerprint is kept"); + assert.equal(kv.has(failedStepKey("nats", "fp-x", "lint")), false, "the result's is never read"); // A plain pass clears nothing; a rerun that passes clears it. await f.stepEnded(ended(C2, "passed")); await judged("a plain pass", C2, "run-on-request nats unit"); @@ -122,3 +123,21 @@ test("a seen key past 30 days is deleted at the next step-ended, a younger one k const done: any = await runAs({ "mesh/caller": "operator" }, {}, () => tool.run({ why: "stale" })); assert.deepEqual([done.deleted.length, acts[0].verb], [1, "git_seen_expire"]); }); + +// step-unread (G §585, H-A-93): a step-ended of a pipeline's commit without the body's fingerprint records nothing and writes +// the finding, scope module, its subject the step's module, keyed .., warning, resolver operator; a later +// step-ended of that step with its fingerprint clears it. +test("a step-ended without its fingerprint writes step-unread, cleared by the step's later step-ended with it", async () => { + const { stepUnreadFinding } = await import("../failedsteps.js"); + const { kv, st } = memory(); + const findings = memory(); + const f = new FailedSteps(st as any, () => {}, () => true, async () => ({ bytes: 0, keys: 0, max_bytes: 0 }), findings.st as any); + await f.stepEnded(ended(C1, "failed", { fingerprint: "fp-in-result" }, { fingerprint: undefined })); + const { key } = stepUnreadFinding("main.7.nats-" + C1.slice(0, 2), "nats", "unit", ""); + const raised: any = findings.kv.get(key); + assert.deepEqual([raised?.kind, raised?.subject, raised?.severity, raised?.resolver], ["step-unread", { scope: "module", id: "nats" }, "warning", "operator"]); + assert.match(raised.summary, /unit of nats in run main\.7\.nats-/); + assert.equal([...kv.keys()].some((k) => !k.startsWith("seen.")), false, "no failure recorded"); + await f.stepEnded(ended(C1, "failed")); + assert.equal(findings.kv.has(key), false, "cleared by the step's step-ended with its fingerprint"); +}); -- 2.54.0 From 815be6bdcebc2f61ddec1b24d9fc39c8fdd4fca1 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:54:09 +0100 Subject: [PATCH 27/43] FORGE-6: the forge module's merge-check machinery and its polls go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit announceHead with its mesh/merge-gate pending, the weighing of trunk and sibling moves (passPulls, trunksMoved, siblingMoves, GateWriter) and their files on disk, the pass's pull-passing half with its 60 s retry, and the 10-minute keep-main-rules step go. A pull request's new head is said as pull.updated from the forge's event alone; the seat's status writes through the client; pollRepos, pollMerged and readAllAtStart stay. Builds: design 47 H §764 (the forge module: the merge-check machinery, its poll and timer), §762; H-A-28, H-A-100 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/gitea/gate.ts | 103 -------- modules/gitea/index.ts | 159 ++---------- modules/gitea/mainrules.ts | 18 -- modules/gitea/pulls.ts | 94 +------ modules/gitea/seat.ts | 15 +- modules/gitea/siblings.ts | 298 ----------------------- modules/gitea/test/announcing.test.ts | 16 -- modules/gitea/test/gate.test.ts | 69 ------ modules/gitea/test/guard.test.ts | 39 +-- modules/gitea/test/mainrules.test.ts | 21 +- modules/gitea/test/paging.test.ts | 27 -- modules/gitea/test/pulls.test.ts | 59 +---- modules/gitea/test/siblings.test.ts | 273 --------------------- modules/gitea/test/tools-account.test.ts | 6 +- modules/gitea/test/trunk.test.ts | 167 ------------- modules/gitea/test/watch.test.ts | 49 +--- modules/gitea/trunk.ts | 211 ---------------- modules/gitea/updated.ts | 42 ++++ modules/gitea/watch.ts | 60 +---- 19 files changed, 92 insertions(+), 1634 deletions(-) delete mode 100644 modules/gitea/gate.ts delete mode 100644 modules/gitea/siblings.ts delete mode 100644 modules/gitea/test/gate.test.ts delete mode 100644 modules/gitea/test/siblings.test.ts delete mode 100644 modules/gitea/test/trunk.test.ts delete mode 100644 modules/gitea/trunk.ts create mode 100644 modules/gitea/updated.ts diff --git a/modules/gitea/gate.ts b/modules/gitea/gate.ts deleted file mode 100644 index e738bc70e..000000000 --- a/modules/gitea/gate.ts +++ /dev/null @@ -1,103 +0,0 @@ -// **One writer of a head's mesh/merge-gate** (novox/hq issue 446). -// -// A pending check says where it is: "waiting in the build queue since …" when the head is announced. The forge keeps only -// the newest status of a context, so two writers cannot order their writes; this module is the one writer of -// mesh/merge-gate, and every write of one head's gate goes through one chain, in the order it was asked. A recheck's -// pending (novox/hq issue 456) and a trunk's move (ADR 0323 §4) take their turns on the same chain. No verdict is written -// here any more: the controller's `checked` is no event (ADR 0571 clause 13), and main requires the checker's mesh/check. - -import { CHECK_CONTEXT, pendingWords } from "./pulls.js"; -import type { CommitStatus, HeadCheck } from "./client.js"; - -/** What the gate writer needs of the forge: a status set, and a head's statuses as the forge holds them. */ -export interface GateForge { - setCommitStatus(owner: string, repo: string, sha: string, status: CommitStatus): Promise; - /** The head's statuses by context, the newest of each, with their words (novox/hq ADR 0323). */ - headChecks(owner: string, repo: string, sha: string): Promise>; -} - -/** Whether a repository as the mesh clones it (`http://forge/novox/hq.git`) is owner/repo. */ -export function clonedAs(url: string | undefined, owner: string, repo: string): boolean { - const u = (url ?? "").trim().toLowerCase().replace(/\.git$/, ""); - const full = `${owner}/${repo}`.toLowerCase(); - return u === full || u.endsWith("/" + full); -} - -interface Head { - /** The chain the head's writes take their turns on. */ - chain: Promise; -} - -export class GateWriter { - private heads = new Map(); - private forge: GateForge; - private now: () => Date; - - constructor(forge: GateForge, now: () => Date = () => new Date()) { - this.forge = forge; - this.now = now; - } - - private head(owner: string, repo: string, sha: string): Head { - const key = `${owner}/${repo}@${sha}`.toLowerCase(); - let h = this.heads.get(key); - if (!h) { - h = { chain: Promise.resolve() }; - this.heads.set(key, h); - // Kept for the heads still in play; the oldest go first, past what any forge holds open at once. - if (this.heads.size > 2000) this.heads.delete(this.heads.keys().next().value as string); - } - return h; - } - - /** One turn on the head's chain; a turn that fails never stops the next. Its failure is the caller's. */ - private turn(h: Head, fn: () => Promise): Promise { - const run = h.chain.then(fn); - h.chain = run.catch(() => {}); - return run; - } - - /** A turn whose failure is said here: a pending word not written is not worth the event again. */ - private said(h: Head, what: string, fn: () => Promise): Promise { - return this.turn(h, fn).catch((err) => { - console.error(`[gitea] ${what} — ${err instanceof Error ? err.message : err}`); - }); - } - - /** The head announced: it waits in the build queue from now. A new check of the head, so no verdict yet. */ - pending(owner: string, repo: string, sha: string): Promise { - const h = this.head(owner, repo, sha); - const at = this.now(); - return this.said(h, `${owner}/${repo}@${sha.slice(0, 8)}: could not say a check is pending`, () => - this.forge.setCommitStatus(owner, repo, sha, { state: "pending", context: CHECK_CONTEXT, description: pendingWords(at) })); - } - - /** - * A status the seat git's `status` verb sets (seat.ts), a recheck's pending among them (novox/hq issue 456): it takes - * its turn after every write of the head already asked for, so it lands over them, and a later write lands over it. A - * write that fails fails the caller. - */ - status(owner: string, repo: string, sha: string, status: CommitStatus): Promise { - return this.turn(this.head(owner, repo, sha), () => this.forge.setCommitStatus(owner, repo, sha, status)); - } - - /** - * **A trunk's move puts the head's verdict out of date** (novox/hq ADR 0323 §4): its merge check statuses are put back - * to pending, with the reason, in their turn after every write of the head already asked for — a verdict in flight - * lands first. What is written is decided in the turn, from the statuses the forge holds then (`decide`, trunk.ts's - * outOfDateStatuses): a check on its way, or a verdict already judged against the trunk's head, is left as it is. It - * opens no check — nothing is asked — so a verdict of a check asked before it keeps its weight. Answers what it wrote; - * a write that fails fails the caller, and the move is weighed again on the next pass. - */ - outOfDate(owner: string, repo: string, sha: string, - decide: (checks: Record) => CommitStatus[] | Promise, target?: string): Promise { - const h = this.head(owner, repo, sha); - const written: CommitStatus[] = []; - return this.turn(h, async () => { - for (const s of await decide(await this.forge.headChecks(owner, repo, sha))) { - await this.forge.setCommitStatus(owner, repo, sha, target ? { ...s, target_url: target } : s); - written.push(s); - } - }).then(() => written); - } -} diff --git a/modules/gitea/index.ts b/modules/gitea/index.ts index 793738a33..4e52c8c3b 100644 --- a/modules/gitea/index.ts +++ b/modules/gitea/index.ts @@ -4,8 +4,7 @@ // Emits (novox/hq ADR 0041/0042): // module.gitea.repo.created — a repository appeared, however it was made (push, web UI, or tool) // module.gitea.pull.merged — a pull request was merged, however it was merged (web UI, API, or tool) -// module.gitea.pull.updated — an open pull request's head moved, opened or pushed to: the mesh checks it -// before it merges (novox/hq to-be 45 §9) +// module.gitea.pull.updated — an open pull request's head moved, opened or pushed to, said from the forge's event alone // // module.gitea.pull.closed — a pull request closed unmerged: the delivery it was is stopped (novox/hq ADR 0239) // @@ -16,13 +15,8 @@ // It reads no other module's state: mesh/check, and every act of a delivery part on the forge, arrive as verbs of the seat // git (seat.ts), asked by the forge bridge (novox/hq design 47 *The seat git*). // -// This module is the one writer of mesh/merge-gate. It hears no build seat's event: since R4g a check's steps are asks -// on the build seats' streams, whose backlog the delivery view shows (novox/hq ADR 0525 §9; issue 1887). -// -// **A trunk's move puts every open verdict into it out of date** (novox/hq ADR 0323 §4, trunk.ts): each pass reads the -// head of every trunk an open pull request merges into, and when one moved, the merge check statuses of every open pull -// request into it go back to pending with the reason. Nothing is run for it. A verdict that lands judged against a trunk -// commit that is no longer the trunk's head is said out of date in place of its pass or failure. +// No merge check is asked, weighed or written here (design 47 H §764; H-A-28, H-A-100): a merge is one merge call of the +// seat git (mergecall.ts), its members judged by their heads and failed steps alone. // // Every pull request the forge holds is announced, whatever its repository: the controller holds the // module graph and decides what is checked — a repository is never asked to opt in. @@ -58,14 +52,10 @@ import { seat as GIT, events as GIT_EVENTS, type BranchMovedEvent, type PullClos import { seat as RUNNER, events as RUNNER_EVENTS, type StepEndedEvent } from "@novox/mesh-delivery-contracts-mesh-pipeline-runner"; import { run } from "./delivery.js"; import { GiteaAdmin, GiteaClient, movedSince } from "./client.js"; -import { keepMainRules, mainRulesStep } from "./mainrules.js"; -import { CHECK_CONTEXT, CHECK_STATUS, REPO_CHECK_CONTEXT, headKey, requiredOf, writesGate, type Announced } from "./pulls.js"; -import { actOn, announceNewHeads, startPass } from "./watch.js"; -import { GateWriter } from "./gate.js"; -import { HOOK_PATH, HOOK_PORT, Pace, claimHead, ensureSystemHook, eventsTool, keptSecret, receiver, type ForgeEvent } from "./webhook.js"; -import { keepSiblings, readSiblings, siblingsFile, weighSiblings } from "./siblings.js"; +import { actOn } from "./watch.js"; +import { HOOK_PATH, HOOK_PORT, Pace, ensureSystemHook, eventsTool, keptSecret, receiver, type ForgeEvent } from "./webhook.js"; import { bindInBackground, bindUntilHeld } from "./bind.js"; -import { announcementOf, weighTrunks } from "./trunk.js"; +import { announcementOf } from "./updated.js"; import { ReadAll, readAllTools } from "./readall.js"; import { DeliveredWrites, writeTools } from "./writes.js"; import { TOOLS_ACCOUNT, TOOLS_ORG, meshAccounts } from "./tools-account.js"; @@ -111,7 +101,7 @@ async function pollRepos(client: GiteaClient): Promise { // What has been announced is kept beside the module's state, so a restart does not announce the // whole history again — and the first tick on a machine with no record announces nothing, because // everything it sees then predates the watching. -import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; + import { join } from "node:path"; // What has been announced is no longer kept in a file (novox/hq ADR 0406 §6, §7, issue 976): an announcement carries a // message id (announce.ts), so the same merge announced by the poll and by the forge's event, or again after a restart, @@ -183,7 +173,6 @@ async function announceClosed(client: GiteaClient, repo: RepoOf, pull: PullOf): throw err; } console.log(`[gitea] announced ${repo.full_name}#${pull.number} closed unmerged`); - await siblingMoves(client, repo.full_name, pull.number); return true; } @@ -243,8 +232,6 @@ async function announceMerge(client: GiteaClient, repo: RepoOf, pull: PullOf): P // Said, because a trigger that fires silently is indistinguishable from one that did not // fire (novox/hq 04-ISSUES/131) — this line is how an operator knows the mesh was told. console.log(`[gitea] announced merge ${repo.full_name}#${pull.number} (${pull.merge_commit_sha.slice(0, 8)}) into ${pull.base}`); - // Its own merge of the head that was read is no sibling's move; any other is (ADR 0382 §5). - await siblingMoves(client, repo.full_name, pull.number); } catch (err) { announced.delete(pull.merge_commit_sha); throw err; @@ -252,123 +239,12 @@ async function announceMerge(client: GiteaClient, repo: RepoOf, pull: PullOf): P return true; } -// **Every new head of an open pull request is announced** (novox/hq to-be 45 §9): the mesh checks it against every -// machine of its facts before it merges. What this process announced is remembered in memory only (novox/hq ADR 0406 -// §6, §7, issue 976): the announcement carries a message id (announce.ts), so the poll's, the forge event's and the -// merge tool's are one to the bus. After a start, the first look announces a head only if the forge shows no merge check -// of it yet — a head with one was announced — and only what moved in the last day, so a forge's whole backlog is not -// checked at once and a restart asks no check again against a trunk that moved. -let heads: Announced = {}; -let primedPulls = false; -/** **A head, announced once, whichever path saw it first** (novox/hq issue 729): the poll or the forge's event. */ -async function announceHead(client: GiteaClient, repo: RepoOf, pull: OpenOf): Promise { - const key = headKey(repo.full_name, pull.number, pull.base); - const sha = String(pull.head_sha ?? ""); - const before = heads[key]; - // Claimed before the first wait, so the other path finds it taken while this one is still announcing. - if (!claimHead(heads, key, sha)) return false; - try { - // The announcement the merge tool makes too when it asks a check (trunk.ts, novox/hq ADR 0323 §5). - await announce("pull.updated", await announcementOf(client, repo.owner, repo.name, repo.clone_url ?? "", pull)); - } catch (err) { - if (before === undefined) delete heads[key]; - else heads[key] = before; - throw err; - } - // A sibling's new head puts out of date every verdict that read the sibling at another head (ADR 0382 §5). - await siblingMoves(client, repo.full_name, pull.number); - // Said, so an operator knows the mesh was asked to check it. - console.log(`[gitea] announced ${key} at ${sha.slice(0, 8)} to be checked before it merges`); - // Pending until the verdict comes, so the pull request says a check is running rather than nothing — and - // where it is: in the build queue from now, until a build machine takes it (novox/hq issue 446). Only where the base - // still requires mesh/merge-gate (issue 1301): a main that requires mesh/check is the checker's; - // a rule that cannot be read is said pending, as before. - const rule = pull.base ? await client.appliedProtection(repo.owner, repo.name, String(pull.base)).catch(() => undefined) : undefined; - if (writesGate(requiredOf(rule))) await gates.pending(repo.owner, repo.name, sha); - return true; +/** **A pull request's new head, said as pull.updated from the forge's event alone** (design 47 H §764; H-A-100): no + * poll, no pending status and no sibling's weighing. */ +async function announceUpdated(client: GiteaClient, repo: RepoOf, pull: PullOf): Promise { + await announce("pull.updated", await announcementOf(client, repo.owner, repo.name, repo.clone_url ?? "", pull)); + console.log(`[gitea] said ${repo.full_name}#${pull.number} at ${String(pull.head_sha ?? "").slice(0, 8)} updated`); } -type OpenOf = Awaited>[number]; - -/** Whether the forge shows a merge check of the head, in any state: only an announced head has one. */ -async function alreadyChecked(client: GiteaClient, repo: RepoOf, pull: OpenOf): Promise { - try { - const checks = await client.headChecks(repo.owner, repo.name, String(pull.head_sha)); - return CHECK_CONTEXT in checks || REPO_CHECK_CONTEXT in checks || CHECK_STATUS in checks; - } catch { - return false; // the forge cannot say: announced, and the bus's message id collapses it if it was - } -} - -let lastPullLook = ""; -/** The pass over every open pull request: run at the start and when the receiver listens, never on a timer (issue 1422). */ -async function passPulls(client: GiteaClient): Promise { - const began = new Date().toISOString(); - const dayAgo = new Date(Date.now() - 24 * 3600_000).toISOString(); - for (const repo of await client.listAllRepos()) { - // Every open pull request, paged (novox/hq issue 579). - const open = await client.listOpenPullsSince(repo.owner, repo.name, ""); - await announceNewHeads(repo.full_name, open, heads, primedPulls, dayAgo, { - alreadyChecked: (pull) => alreadyChecked(client, repo, pull), - announceHead: (pull) => announceHead(client, repo, pull), - missed: (what) => pace.miss(what), - }); - if (await trunksMoved(client, repo.owner, repo.name)) changedTrunks = true; - } - if (changedTrunks) keepTrunks(); - changedTrunks = false; - primedPulls = true; - lastPullLook = began; -} - -// **The trunks' heads, as this module last read them** (novox/hq ADR 0323 §4), by `owner/repo:branch`: kept beside the -// other records, so a move made while this module was away is weighed on its first pass back. With no record — the -// first pass after this rolls out — a trunk's head is taken as it is and nothing is put out of date: the verdicts -// then standing are weighed on the trunk's next move, and the merge tool compares commits whatever this holds. -const trunksRecord = process.env.MESH_GITEA_STATE_DIR ? join(process.env.MESH_GITEA_STATE_DIR, "trunks-seen.json") : null; -let trunks: Record = {}; -let changedTrunks = false; -if (trunksRecord && existsSync(trunksRecord)) { - try { - trunks = (JSON.parse(readFileSync(trunksRecord, "utf8")) as { trunks: Record }).trunks ?? {}; - } catch { - // An unreadable record is no record: the next pass takes each trunk's head as it is. - } -} -function keepTrunks(): void { - if (!trunksRecord) return; - mkdirSync(join(trunksRecord, ".."), { recursive: true }); - const tmp = trunksRecord + ".tmp"; - writeFileSync(tmp, JSON.stringify({ trunks })); - renameSync(tmp, trunksRecord); -} - -/** Whether a trunk of one repository moved, weighed with this module's forge and its one writer (trunk.ts). */ -function trunksMoved(client: GiteaClient, owner: string, name: string): Promise { - return weighTrunks(client, gates, trunks, owner, name, () => siblingMoves(client, `${owner}/${name}`)); -} - -// **The siblings each open pull request's latest verdict names** (novox/hq ADR 0382 §5, siblings.ts), by member, kept beside -// the other records. A sibling's new head, close unmerged, merge, or a move of its base puts the merge check of every -// member whose verdict names it back to pending, with the reason; nothing is run for it. -const siblingsRecord = siblingsFile(process.env.MESH_GITEA_STATE_DIR); -const siblingNamed = readSiblings(siblingsRecord); - -/** A sibling moved, weighed (ADR 0382 §5): never fatal to what heard it. */ -async function siblingMoves(client: GiteaClient, repository: string, number?: number): Promise { - try { - if (await weighSiblings(client, gates, siblingNamed, repository, number)) keepSiblings(siblingsRecord, siblingNamed); - } catch (err) { - console.error(`[gitea] whether a sibling of ${repository}${number ? "#" + number : ""} moved could not be weighed — ${err instanceof Error ? err.message : err}`); - } -} - -// **This module is the one writer of a head's mesh/merge-gate** (novox/hq issue 446): the announcement's pending words, -// where the check is when a build machine takes it, and the verdict — each head's writes in one chain, the verdict last -// (gate.ts). A second writer elsewhere raced the verdict and could leave a pass reading pending. -const gates = new GateWriter({ - setCommitStatus: (owner, repo, sha, status) => (gitea as GiteaClient).setCommitStatus(owner, repo, sha, status), - headChecks: (owner, repo, sha) => (gitea as GiteaClient).headChecks(owner, repo, sha), -}); /** How fast the polls look: 30 s until the forge's events are heard, five minutes while they are; and what * gitea_events says of them (webhook.ts). */ @@ -417,11 +293,10 @@ const failedSteps = new FailedSteps(state("failed-steps"), (line) => co void failedSteps.expireSeen().catch((err) => console.log(`[gitea] the seen keys past 30 days could not be deleted at the start: ${err instanceof Error ? err.message : String(err)}`)); // gitea_events is served whether or not the forge is watched: it says so (novox/hq issue 729). The seat git's verbs are -// served here, beside the one writer of a head's statuses: the seat's status takes its turn on the head's chain, after the -// announcement's pending and a trunk's move, so a recheck's pending never lands over a later verdict (issue 456). +// served here. registerModuleTools("gitea", () => [eventsTool(pace), ...readAllTools(readAll), ...writeTools(writes), ...statusTools("gitea", "gitea_first_views", "gitea_first_view_again", followers, firstViews, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }), - ...(gitea ? seatTools(gitea, { run, container: FORGE_CONTAINER, statuses: gates, registered: () => registrations.written, judge: (head) => failedSteps.judge(head), + ...(gitea ? seatTools(gitea, { run, container: FORGE_CONTAINER, registered: () => registrations.written, judge: (head) => failedSteps.judge(head), watched: () => registrations.watched, calls, say: seatPublish, partials }) : []), ...callTools(calls, partials, seatPublish, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }), ...failedStepsTools(failedSteps, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); })]); @@ -430,10 +305,9 @@ registerModuleTools("gitea", () => [eventsTool(pace), ...readAllTools(readAll), const actOnEvent = (client: GiteaClient, event: ForgeEvent): Promise => actOn(event, { getRepo: (o, r) => client.getRepo(o, r), getPull: (o, r, n) => client.getPullRequest(o, r, n) as Promise, - announceHead: (repo, pull) => announceHead(client, repo, pull as OpenOf), + announceUpdated: (repo, pull) => announceUpdated(client, repo, pull), announceMerge: (repo, pull) => announceMerge(client, repo, pull), announceClosed: (repo, pull) => announceClosed(client, repo, pull), - siblingMoves: (full, n) => siblingMoves(client, full, n), }); /** @@ -610,14 +484,13 @@ if (gitea) { } catch (err) { console.error(`[gitea] the package seats cannot write — ${err instanceof Error ? err.message : err}`); } - tick(mainRulesStep(keepMainRules(), () => GiteaAdmin.fromEnv().ensureMainMerges("novox"), (line) => console.log(line)), 600_000); tick(() => pollRepos(client), 60_000); // **No timer asks the forge for pull requests or merges** (issue 1422): the forge's events announce them. One pass over // every repository runs at the start and again once the receiver listens, so what happened while the module was away or // not yet listening is announced; passes run one at a time, a failed one is asked again a minute later. let passes: Promise = Promise.resolve(); const pass = (): Promise => (passes = passes.catch(() => {}).then(() => - startPass(async () => { await pollMerged(client); await passPulls(client); }, { retryMs: 60_000 }))); + pollMerged(client).catch((err) => console.error(`[gitea] the pass over every repository's merges failed — ${err instanceof Error ? err.message : err}`)))); void pass(); let catchUpAsked = false; catchUp = () => { diff --git a/modules/gitea/mainrules.ts b/modules/gitea/mainrules.ts index e8ac249f7..7e51bcab8 100644 --- a/modules/gitea/mainrules.ts +++ b/modules/gitea/mainrules.ts @@ -3,8 +3,6 @@ // False, the default, leaves every repository's branch protection as people set it at the forge. import { readFileSync } from "node:fs"; -export const LEFT_AS_SET = "[gitea] main's rules are left as set at the forge (keep-main-rules is false)"; - /** The setting from the settings-merged config file the mesh delivers; absent, unreadable or not true is false. */ export function keepMainRules(env: NodeJS.ProcessEnv = process.env): boolean { const file = env.MESH_GITEA_CONFIG_FILE; @@ -14,19 +12,3 @@ export function keepMainRules(env: NodeJS.ProcessEnv = process.env): boolean { return v === true || v === "true"; } catch { return false; } } - -/** The 10-minute step: puts the rules right only when kept; else says once that they are left and writes nothing. */ -export function mainRulesStep(keep: boolean, put: () => Promise, say: (line: string) => void): () => Promise { - let said = false; - let touched = -1; - return async () => { - if (!keep) { - if (!said) say(LEFT_AS_SET); - said = true; - return; - } - const repos = await put(); - if (repos.length !== touched) say(`[gitea] main's rule is right in ${repos.length} repositories`); - touched = repos.length; - }; -} diff --git a/modules/gitea/pulls.ts b/modules/gitea/pulls.ts index 16a011834..364173ac8 100644 --- a/modules/gitea/pulls.ts +++ b/modules/gitea/pulls.ts @@ -1,15 +1,5 @@ -// A pull request's merge check (novox/hq to-be 45 §9): what the forge's announcer says when a pull -// request's head moves, and the status contexts the check is kept under. -// -// **Before merge, never after.** Each new head of an open pull request is announced as `pull.updated`, and the mesh -// checks it against every machine of its facts before it merges. The verdict is the checker's `mesh/check`, which this -// module writes from the checker's owed state (check.ts); the controller's `checked`, which once set the two older -// statuses, is no event any more (ADR 0571 clause 13). -// -// Pure functions here, so they are tested without a forge. - -import type { GiteaPull, GiteaRepo } from "./client.js"; -import { movedSince } from "./client.js"; +// The status contexts the forge's protections named for a merge check, read by the account's writes (writes.ts) to take +// them off a protection: no merge check is asked, weighed or written any more (design 47 H §764; H-A-28, H-A-100). /** The status context a merge check's gate is kept under: one per commit, the newest replacing the last. */ export const CHECK_CONTEXT = "mesh/merge-gate"; @@ -20,83 +10,3 @@ export const REPO_CHECK_CONTEXT = "mesh/repo-check"; /** The one status main requires (novox/hq ADR 0447 §1, issue 1301): the checker's verdict, written by this module from the * checker's owed state (check.ts, ADR 0521). Its line names the head and the trunk commit it judged: "pass on …". */ export const CHECK_STATUS = "mesh/check"; - -/** The statuses a branch's rule requires, from the rule the forge applies to it: none when it is unprotected or checks - * no status; undefined when the rule could not be read. */ -export function requiredOf(rule: { enable_status_check?: boolean; status_check_contexts?: string[] | null } | null | undefined): string[] | undefined { - if (rule === undefined) return undefined; - if (!rule || !rule.enable_status_check) return []; - return (rule.status_check_contexts ?? []).filter(Boolean); -} - -/** - * **The controller's statuses are written only where a base still requires them** (issue 1301; ADR 0503 §3, §5): of - * mesh/merge-gate and mesh/repo-check, those the base requires — hq's mesh/repo-check alone once the forge requires - * mesh/check everywhere else, so no mesh/merge-gate stands on a head that nothing will set. A base whose rule could not - * be read keeps both, as before: a status nothing requires blocks nothing, and one that is required is never left out. - */ -export function maintained(required: string[] | undefined): string[] { - const both = [CHECK_CONTEXT, REPO_CHECK_CONTEXT]; - return required === undefined ? both : both.filter((c) => required.includes(c)); -} - -/** Whether mesh/merge-gate is written on a head of this base (maintained). */ -export function writesGate(required: string[] | undefined): boolean { - return maintained(required).includes(CHECK_CONTEXT); -} - -/** - * The pending words a head's merge check is announced with (novox/hq issue 446): where the check is, and since when, - * in the time the operator reads. They stood as "the mesh is mapping this head onto its module graph" for the whole - * check — the few seconds the controller maps the head and then the queue wait and the build — and readers took the - * forge for lagging (issue 423). The controller maps and queues the check in seconds, so from the announcement it waits - * in the build queue; mesh-delivery says "building on since …" when a build machine takes it. - */ -export function pendingWords(announced: Date): string { - const two = (n: number) => String(n).padStart(2, "0"); - return `waiting in the build queue since ${two(announced.getHours())}:${two(announced.getMinutes())}`; -} - -/** The heads already announced, keyed by `headKey`, so a restart announces nothing twice. */ -export type Announced = Record; - -/** The key a pull request's announced head is kept under: `owner/repo#number@base`. The base is in it because a pull - * request retargeted keeps its head, and a head judged into one base is not judged into another (issue 1422, de-spiegel #6). */ -export function headKey(full: string, number: number, base?: string): string { - return `${full}#${number}@${base ?? ""}`; -} - -/** Which open pull requests have a head not yet announced. A pull request merged or closed is not - * open and is never asked about. */ -export function headsToAnnounce(full: string, pulls: GiteaPull[], announced: Announced): GiteaPull[] { - return pulls.filter((p) => p.state === "open" && !!p.head_sha && announced[headKey(full, p.number, p.base)] !== p.head_sha); -} - -/** A pull request the forge found updated, by its repository: what `searchOpenPulls` answers. */ -export interface PullSeen { - owner: string; - name: string; - updated_at?: string; -} - -/** **The repositories a pass looks at** (novox/hq issue 347): every one that moved since the floor, every one - * holding a pull request updated since the floor — opening, reopening or retargeting a pull request moves its own - * update time and not its repository's — and every one on a sweep or the first pass. */ -export function reposToLook(repos: GiteaRepo[], floor: string, pulls: PullSeen[], sweep: boolean): GiteaRepo[] { - if (sweep || !floor) return repos; - const moved = new Set(movedSince(repos, floor).map((r) => r.full_name)); - const at = Date.parse(floor); - for (const p of pulls) { - if (!p.updated_at || !(Date.parse(p.updated_at) < at)) moved.add(`${p.owner}/${p.name}`); - } - return repos.filter((r) => moved.has(r.full_name)); -} - -export function clipped(d: string): string { - d = d.replace(/\s+/g, " ").trim(); - return d.length > 140 ? d.slice(0, 139) + "…" : d; -} - -/** What a group member's gate says while the group's composed check is the one that judges it: the member's own check can say - * nothing more of it, so asking that check again brings no verdict that names a trunk (novox/hq issue 1106). */ -export const WAITS_FOR_COMPOSED = "waits for its group's composed check"; diff --git a/modules/gitea/seat.ts b/modules/gitea/seat.ts index 2e9cda399..dee5185b3 100644 --- a/modules/gitea/seat.ts +++ b/modules/gitea/seat.ts @@ -40,18 +40,11 @@ export const TAGS_PAGE = 50; * is the manifest's container's; no variable names it (a MESH_* word nothing sets is refused, checks/environment). */ export const FORGE_CONTAINER = "gitea"; -/** The writer every status of a head takes its turn on (gate.ts): the seat's status lands after every write of the head - * already asked for, so a recheck's pending never lands over a later verdict (novox/hq issue 456). */ -export interface HeadStatuses { - status(owner: string, repo: string, sha: string, status: CommitStatus): Promise; -} - -/** What the holder reaches beside the forge's API: git in the forge's container (the note), the head's one writer of its - * statuses, and whether the first registrations are written. */ +/** What the holder reaches beside the forge's API: git in the forge's container (the note), and whether the first + * registrations are written. */ export interface SeatDeps { run: Runner; container: string; - statuses: HeadStatuses; /** Whether the first registrations were written (registrations.ts): until then merge-pull refuses every merge (design 47 §95). */ registered: () => boolean; /** The failed `required` step that refuses a head, judged from the module's own state `failed-steps` (failedsteps.ts), or null. */ @@ -229,14 +222,14 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[] const combined = await gitea.combinedStatus(owner, repo, commit); return { commit, combined: combined.state, statuses: newestByContext(combined.statuses).map(statusOf) }; }), - verb(GIT_VERBS.status, "The seat git's status: an act of the forge bridge, a commit status set in its turn after every write of the head already asked for; the same id said again sets it again to the same.", + verb(GIT_VERBS.status, "The seat git's status: an act of the forge bridge, a commit status set on a commit; the same id said again sets it again to the same.", async (args): Promise => { const id = idOf(args.id); const { owner, repo } = repositoryOf(args.repository); const commit = commitOf("commit", args.commit); const state = String(args.state ?? ""); if (!STATES.has(state)) throw new Refused(`state ${JSON.stringify(state)} is none of ${[...STATES].join(", ")}`); - await deps.statuses.status(owner, repo, commit, { state: state as CommitStatus["state"], context: String(args.context ?? ""), + await gitea.setCommitStatus(owner, repo, commit, { state: state as CommitStatus["state"], context: String(args.context ?? ""), description: String(args.description ?? ""), ...(args.target ? { target_url: String(args.target) } : {}) }); return { id, performed: true }; }), diff --git a/modules/gitea/siblings.ts b/modules/gitea/siblings.ts deleted file mode 100644 index 5dcff478a..000000000 --- a/modules/gitea/siblings.ts +++ /dev/null @@ -1,298 +0,0 @@ -// **A delivery group's member is checked beside the other members' heads, and a sibling's move puts that verdict out of -// date** (novox/hq ADR 0382 §5, §6; issue 867). -// -// The controller's verdict (`checked`) names the siblings its check read: each as the sibling's repository, pull -// request, head, base branch and the base commit the sibling's head was merged onto. This module, the forge's holder, -// keeps them per open pull request, beside its other records, and: -// -// - on its own hearing of a sibling's new head, of its close unmerged, or of a move of its base, sets -// mesh/merge-gate and mesh/repo-check of every member whose verdict names that sibling back to pending, with the -// reason. Nothing is run for it. The one move that does not count is the sibling's own merge of the head that was -// read onto the base commit that was read (in either order of the two merges); -// - lets the merge tool refuse a verdict that read siblings unless mesh/delivery-group passed at the very heads it -// read, and every sibling head it read carries an approving review at that head (merge.ts). -// -// A verdict without siblings (a controller from before ADR 0382) is none of this: nothing is kept and nothing changes. -// Pure functions and one small file of the module's own state, so this is tested without a forge. - -import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; -import { dirname } from "node:path"; -import type { CommitStatus, GiteaPull, HeadCheck } from "./client.js"; -import { Refused } from "./guard.js"; -import { CHECK_CONTEXT, REPO_CHECK_CONTEXT, clipped } from "./pulls.js"; -import { sameCommit, twelve, type OutOfDateWriter } from "./trunk.js"; - -/** The status the delivery group's owner sets on each member's head (novox/hq ADR 0239). */ -export const GROUP_CONTEXT = "mesh/delivery-group"; - -/** One sibling a verdict read, as the controller names it (contract of ADR 0382 §4). */ -export interface SiblingNamed { - repository: string; - number: number; - head: string; - base: string; - base_commit: string; - /** Set here, never by the controller: the sibling merged its head onto its base commit read, so it is no sibling's - * move any more. */ - merged?: boolean; -} - -/** What is kept of one member's latest verdict that read siblings: the head it was of and the siblings it named. */ -export interface SiblingEntry { - head: string; - siblings: SiblingNamed[]; -} - -/** The records, by `owner/repo#number` of the member. */ -export type SiblingRecord = Record; - -/** The member's key. */ -export function memberKey(owner: string, repo: string, number: number): string { - return `${owner}/${repo}#${number}`.toLowerCase(); -} - -/** The siblings a verdict names, trimmed to what is kept; none when it names none or names one badly. */ -export function siblingsOf(named: unknown): SiblingNamed[] { - if (!Array.isArray(named)) return []; - const out: SiblingNamed[] = []; - for (const s of named) { - const r = s as Record; - const repository = String(r?.repository ?? ""), head = String(r?.head ?? ""), base = String(r?.base ?? ""); - const number = Number(r?.number); - if (!repository || !head || !base || !Number.isInteger(number) || number <= 0) continue; - out.push({ repository, number, head, base, base_commit: String(r?.base_commit ?? "") }); - } - return out; -} - -/** The siblings as a status names them: "mesh-host#114@abcdef123456, …". */ -export function siblingWords(siblings: SiblingNamed[]): string { - return siblings.map((s) => `${s.repository.split("/").pop()}#${s.number}@${twelve(s.head)}`).join(", "); -} - -/** What a status of a verdict that read siblings carries, after the commits it judged. */ -export function besideWords(siblings: SiblingNamed[]): string { - // Short, so that the verdict word and the start of its reason survive the forge's 140 characters: the first sibling - // by its pull request and 8 characters of its head, and how many more. The whole list is the holder's record and the comment's. - const first = siblings[0]; - const more = siblings.length > 1 ? ` +${siblings.length - 1} more` : ""; - return `beside ${first.repository.split("/").pop()}#${first.number}@${twelve(first.head).slice(0, 8)}${more}`; -} - -/** The mark a status carries when the siblings could not be learned (ADR 0382 §1): said, never silent. */ -export const UNKNOWN_MARK = "siblings unknown"; - -/** Whether a status names that its verdict read siblings (so that a record lost does not read as none). */ -export function namesSiblings(description: string | undefined): boolean { - return / beside [\w.-]+#\d+@[0-9a-f]{8}\b/.test(description ?? ""); -} - -/** Whether a status is a verdict put out of date by a sibling's move. */ -export function siblingOutOfDate(description: string | undefined): boolean { - return /^out of date: judged beside /.test(description ?? ""); -} - -// --- the record on disk ---------------------------------------------------------------------------------------- - -/** The file the holder keeps the siblings its verdicts name in, beside its other records. */ -export function siblingsFile(stateDir: string | undefined): string | null { - return stateDir ? `${stateDir.replace(/\/+$/, "")}/siblings-named.json` : null; -} - -export function readSiblings(file: string | null): SiblingRecord { - if (!file || !existsSync(file)) return {}; - try { - return (JSON.parse(readFileSync(file, "utf8")) as { members?: SiblingRecord }).members ?? {}; - } catch { - return {}; - } -} - -export function keepSiblings(file: string | null, record: SiblingRecord): void { - if (!file) return; - try { - mkdirSync(dirname(file), { recursive: true }); - writeFileSync(file + ".tmp", JSON.stringify({ members: record })); - renameSync(file + ".tmp", file); - } catch (err) { - console.error(`[gitea] the siblings a verdict named could not be kept — ${err instanceof Error ? err.message : err}`); - } -} - -// --- what a sibling's move is --------------------------------------------------------------------------------- - -/** What weighing a sibling reads of the forge. */ -export interface SiblingForge { - getPullRequest(owner: string, repo: string, index: number): Promise; - branchHead(owner: string, repo: string, branch: string): Promise; - commitParents(owner: string, repo: string, sha: string): Promise; -} - -/** The reason a verdict that named this sibling is out of date, from what the forge holds now, or nothing; and whether - * the sibling has merged its head read onto the base commit read, which ends it as a sibling. */ -export async function siblingMoved(forge: SiblingForge, s: SiblingNamed): Promise<{ reason?: string; merged?: boolean }> { - const [owner, repo] = s.repository.split("/"); - const who = `${repo}#${s.number}`; - const pull = await forge.getPullRequest(owner, repo, s.number); - if (pull.merged) { - // Its own merge of the head that was read onto the base commit that was read: the head the merge joined is the head - // read, and the commit it was made onto is the base commit read. Any other merge is a move of the base. - const parents = pull.merge_commit_sha ? await forge.commitParents(owner, repo, pull.merge_commit_sha).catch(() => []) : []; - const own = sameCommit(pull.head_sha, s.head) && parents.length >= 1 && sameCommit(parents[0], s.base_commit) && - (parents.length === 1 || sameCommit(parents[1], s.head)); - if (own) return { merged: true }; - return { reason: `${who} was merged otherwise than its head ${twelve(s.head)} onto ${twelve(s.base_commit)}` }; - } - if (pull.state !== "open") return { reason: `${who} was closed unmerged` }; - if (!sameCommit(pull.head_sha, s.head)) { - return { reason: `${who} at ${twelve(s.head)}, it is now at ${twelve(pull.head_sha)}` }; - } - const base = await forge.branchHead(owner, repo, s.base); - if (!sameCommit(base, s.base_commit)) { - return { reason: `${who} merged onto ${twelve(s.base_commit)}, its ${s.base} is now ${twelve(base)}` }; - } - return {}; -} - -/** A verdict out of date because a sibling moved, as its statuses say it (ADR 0382 §5; the wording of ADR 0323 §4). */ -export function siblingOutOfDateWords(reason: string): string { - return clipped(`out of date: judged beside ${reason}; judged again on a recheck or when it is merged`); -} - -/** The statuses a sibling's move puts back to pending on one member's head, from what the forge holds on it now: none - * when it has no merge check, or its gate is pending (a check on its way, or already out of date). */ -export function siblingStatuses(checks: Record, reason: string): CommitStatus[] { - const gate = checks[CHECK_CONTEXT]; - if (!gate || gate.state === "pending") return []; - const description = siblingOutOfDateWords(reason); - const out: CommitStatus[] = [{ state: "pending", context: CHECK_CONTEXT, description }]; - if (checks[REPO_CHECK_CONTEXT]) out.push({ state: "pending", context: REPO_CHECK_CONTEXT, description }); - return out; -} - -/** What weighing a sibling's move writes with, beside the forge's reads. */ -export interface SiblingWeighing extends SiblingForge {} - -/** - * A sibling moved (a new head, a close, a base move, a merge): every member whose latest verdict names it has its - * merge check put back to pending with the reason. `repository` and, when the event names one, `number` say which - * sibling. A member whose head is no longer the one its verdict was of is dropped from the record: its new head is - * checked afresh. Nothing is run. Answers whether the record changed. A read that fails leaves that member for the next - * move to weigh, and is said. - */ -export async function weighSiblings(forge: SiblingWeighing, gates: OutOfDateWriter, record: SiblingRecord, - repository: string, number?: number): Promise { - let changed = false; - const repo = repository.toLowerCase(); - for (const [key, entry] of Object.entries(record)) { - const [path] = key.split("#"); - const [owner, name] = path.split("/"); - const memberNumber = Number(key.split("#")[1]); - // A member that is merged or no longer open has no verdict to put out of date: its entry goes (the record must not - // grow without bound, and a merged head is never re-pended by a sibling's later push). Weighed when the event is the - // member's own, or names one of its siblings. - const own = path === repo && (number === undefined || number === memberNumber); - if (own || entry.siblings.some((s) => !s.merged && s.repository.toLowerCase() === repo && (number === undefined || s.number === number))) { - try { - const member = await forge.getPullRequest(owner, name, memberNumber); - if (member.merged || member.state !== "open") { - delete record[key]; - changed = true; - continue; - } - } catch (err) { - console.error(`[gitea] ${key}: whether it is still open could not be read — ${err instanceof Error ? err.message : err}`); - } - } - for (const s of entry.siblings) { - if (s.merged || s.repository.toLowerCase() !== repo || (number !== undefined && s.number !== number)) continue; - try { - const moved = await siblingMoved(forge, s); - if (moved.merged) { - s.merged = true; - changed = true; - continue; - } - if (!moved.reason) continue; - const member = await forge.getPullRequest(owner, name, memberNumber); - if (!sameCommit(member.head_sha, entry.head)) { - delete record[key]; - changed = true; - break; - } - const written = await gates.outOfDate(owner, name, entry.head, (checks) => siblingStatuses(checks, moved.reason!), - member.html_url || undefined); - if (written.length) { - console.log(`[gitea] ${key}: its verdict was judged beside ${moved.reason}: out of date, nothing run (novox/hq ADR 0382)`); - } - break; - } catch (err) { - console.error(`[gitea] ${key}: whether ${s.repository}#${s.number} moved could not be weighed, so it is weighed again ` + - `on its next move — ${err instanceof Error ? err.message : err}`); - } - } - } - return changed; -} - -// --- the merge tool's rule (ADR 0382 §6) ---------------------------------------------------------------------- - -/** What the rule reads beside the pull request being merged. */ -export interface GroupForge { - headChecks(owner: string, repo: string, sha: string): Promise>; -} - -/** The heads the operator's Approve covers (novox/hq ADR 0421 §9, issue 1022), as mesh-delivery hands them to the merge - * tool: `owner/repo@`, one per member, by repository. A malformed entry is refused, never skipped. */ -export function approvedHeadsOf(given: unknown): Map | undefined { - if (given === undefined || given === null) return undefined; - if (!Array.isArray(given)) throw new Refused("approved_heads must be a list of owner/repo@. Nothing was merged"); - const out = new Map(); - for (const entry of given) { - const m = /^([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+)@([0-9a-f]{40}|[0-9a-f]{64})$/i.exec(String(entry)); - if (!m) throw new Refused(`approved_heads holds ${JSON.stringify(entry)}, which is not owner/repo@. Nothing was merged`); - out.set(m[1].toLowerCase(), m[2].toLowerCase()); - } - return out; -} - -/** Why a pull request's head is not one the Approve covers, in words, or nothing: its repository is not named, or names - * another head. */ -export function notApproved(approved: Map, repository: string, number: number, head: string): string | undefined { - const covered = approved.get(repository.toLowerCase()); - const at = `${repository.split("/")[1]}#${number}@${twelve(head)}`; - if (covered === undefined) return `${at} is not among the heads the Approve covers`; - if (covered !== head.toLowerCase()) return `${at} is not the head the Approve covers (${twelve(covered)})`; - return undefined; -} - -/** - * What stops a pull request whose verdict read siblings from merging, in words, or nothing: mesh/delivery-group must be - * a success at its own head and at every sibling head the verdict read, and every head read must be one the operator's - * Approve covers (ADR 0421 §9: the Approve is the review; the merge tool takes the approved heads from mesh-delivery). - * Every missing thing is named. - */ -export async function groupRefusal(forge: GroupForge, self: { owner: string; repo: string; number: number; head: string }, - siblings: SiblingNamed[], approved: Map | undefined): Promise { - const missing: string[] = []; - const members = [{ repository: `${self.owner}/${self.repo}`, number: self.number, head: self.head }, - ...siblings.map((s) => ({ repository: s.repository, number: s.number, head: s.head }))]; - for (const m of members) { - const [owner, repo] = m.repository.split("/"); - const at = `${repo}#${m.number}@${twelve(m.head)}`; - let group: HeadCheck | undefined; - try { - group = (await forge.headChecks(owner, repo, m.head))[GROUP_CONTEXT]; - } catch (err) { - missing.push(`${GROUP_CONTEXT} at ${at} could not be read (${err instanceof Error ? err.message : err})`); - continue; - } - if (group?.state !== "success") missing.push(`${GROUP_CONTEXT} is ${group?.state ?? "missing"} at ${at}`); - if (!approved) missing.push(`${at} is covered by no Approve: the merge was handed no approved heads`); - else { - const why = notApproved(approved, m.repository, m.number, m.head); - if (why) missing.push(why); - } - } - return missing.length ? missing.join("; ") : undefined; -} diff --git a/modules/gitea/test/announcing.test.ts b/modules/gitea/test/announcing.test.ts index 375124d71..d47881a12 100644 --- a/modules/gitea/test/announcing.test.ts +++ b/modules/gitea/test/announcing.test.ts @@ -12,22 +12,6 @@ const repo = (full: string, updated: string) => { return { full_name: full, owner, name, private: false, html_url: "", updated_at: updated }; }; -test("a pull request opened after the margin is looked at, by its own update time", async () => { - const { reposToLook } = await import("../pulls.ts"); - const pushed = "2026-10-09T10:00:00Z"; // the branch pushed: the repository's update time - // The pull request opened two minutes later; the last pass was at 10:02:30, so the floor is 10:01:30. - const floor = "2026-10-09T10:01:30Z"; - const repos = [repo("novox/mesh-sdk", pushed), repo("novox/hq", "2026-10-09T09:00:00Z")]; - const opened = [{ owner: "novox", name: "mesh-sdk", updated_at: "2026-10-09T10:02:00Z" }]; - assert.deepEqual(reposToLook(repos, floor, opened, false).map((r) => r.full_name), ["novox/mesh-sdk"], - "the repository of a pull request updated since the floor, though the repository itself did not move"); - assert.deepEqual(reposToLook(repos, floor, [], false).map((r) => r.full_name), [], - "without the pull request's own time nothing is looked at — the fault"); - assert.deepEqual(reposToLook(repos, floor, [], true).map((r) => r.full_name).sort(), ["novox/hq", "novox/mesh-sdk"], - "a sweep looks at every repository"); - assert.deepEqual(reposToLook(repos, "", [], false).length, 2, "the first pass looks at every repository"); -}); - test("every open pull request is read with what its base requires and what its head has (gitea_open_pulls)", async () => { const { GiteaClient } = await import("../client.ts"); const asked: string[] = []; diff --git a/modules/gitea/test/gate.test.ts b/modules/gitea/test/gate.test.ts deleted file mode 100644 index 404452c84..000000000 --- a/modules/gitea/test/gate.test.ts +++ /dev/null @@ -1,69 +0,0 @@ -import assert from "node:assert/strict"; -import { test } from "node:test"; - -// **One writer of a head's mesh/merge-gate** (novox/hq issue 446): the announcement's pending words, a recheck's pending and -// a trunk's move, each write of a head in the order it was asked. - -const SHA = "a6b08de8507c1703528277e5cefc80548dbc9bb3"; - -/** A forge that keeps the newest status per context, as gitea does, and whose writes take as long as a test says. */ -function aForge() { - const kept: Record = {}; - const written: string[] = []; - let failNext = 0; - let hold: Promise | null = null; - let release = () => {}; - return { - kept, written, - /** The next writes fail: a request the forge did not take. */ - failWrites(n = 1) { - failNext = n; - }, - /** The next writes wait until released: a request in flight. */ - holdWrites() { - hold = new Promise((r) => (release = r)); - }, - release() { - hold = null; - release(); - }, - async headChecks() { - return {}; - }, - async setCommitStatus(_o: string, _r: string, _sha: string, s: { state: string; context: string; description: string }) { - if (hold) await hold; - if (failNext > 0) { - failNext--; - throw new Error("the forge answered 502"); - } - kept[s.context] = { state: s.state, description: s.description, updated_at: new Date().toISOString() }; - written.push(`${s.state} ${s.description}`); - }, - }; -} - -// **A recheck's pending goes through the one writer** (novox/hq issue 456). - -const recheckAt = new Date("2026-10-11T01:30:00Z"); -const checkingAgain = { state: "pending" as const, context: "mesh/merge-gate", description: "checking again: main moved on" }; -const gateOf = (forge: ReturnType) => - `${forge.kept["mesh/merge-gate"].state} ${forge.kept["mesh/merge-gate"].description}`; - -test("a repository as the mesh clones it is told from another", async () => { - const { clonedAs } = await import("../gate.ts"); - assert.equal(clonedAs("http://127.0.0.1:20000/novox/hq.git", "novox", "hq"), true); - assert.equal(clonedAs("http://127.0.0.1:20000/novox/mesh-hq.git", "novox", "hq"), false); - assert.equal(clonedAs("http://127.0.0.1:20000/novox/mesh-controller.git", "novox", "hq"), false); -}); - -test("a recheck the forge does not take fails its caller, and the next write still takes its turn", async () => { - const { GateWriter } = await import("../gate.ts"); - const forge = aForge(); - const gates = new GateWriter(forge, () => recheckAt); - await gates.pending("novox", "hq", SHA); - forge.failWrites(); - await assert.rejects(gates.status("novox", "hq", SHA, checkingAgain)); - await gates.status("novox", "hq", SHA, checkingAgain); - assert.equal(gateOf(forge), "pending checking again: main moved on"); -}); - diff --git a/modules/gitea/test/guard.test.ts b/modules/gitea/test/guard.test.ts index 424b5076e..ae7348ab7 100644 --- a/modules/gitea/test/guard.test.ts +++ b/modules/gitea/test/guard.test.ts @@ -433,25 +433,16 @@ test("the two new tools are named for what mesh_search is asked, and gitea_api s // The seat git's pull and status (design 47 *The seat git*): an open pull request answers what the forge would merge now, // and two writes of one head's status land in order. merge-pull is the merge call's (merge-call.test.ts). -test("the seat's pull answers what the forge would merge now, and a head's statuses land in order", async () => { +test("the seat's pull answers what the forge would merge now, and its status is set on the commit", async () => { const { seatTools } = await import("../dist/seat.js"); const { runAs } = await import("@novox/mesh-sdk/callers"); const bridge = "ace.mesh-delivery-forge-bridge"; - // The head's one writer of its statuses (gate.ts), over a forge whose first write is still in flight when the next is asked. - const { GateWriter } = await import("../dist/gate.js"); + // The seat's status is set through the forge's client directly (design 47 H §764; H-A-100): no chain of a head's writes. const landed: string[] = []; - let inFlight: Promise | null = null, land = () => {}; - const slow = { - headChecks: async () => ({}), - setCommitStatus: async (_o: string, _r: string, _sha: string, s: { state: string; description: string }) => { - if (inFlight) { const w = inFlight; inFlight = null; await w; } - landed.push(`${s.state} ${s.description}`); - }, - }; - const gates = new GateWriter(slow); - const seat = seatTools(Object.assign(Object.create(client), { setCommitStatus: slow.setCommitStatus }), - { run: async () => ({ stdout: "", code: 0 }), container: "gitea", statuses: gates, registered: () => true, judge: async () => null, - watched: () => new Map() }); + const seat = seatTools(Object.assign(Object.create(client), { + setCommitStatus: async (_o: string, _r: string, _sha: string, s: { state: string; description: string }) => { landed.push(`${s.state} ${s.description}`); }, + }), { run: async () => ({ stdout: "", code: 0 }), container: "gitea", registered: () => true, judge: async () => null, + watched: () => new Map() }); const call = (verb: string, args: Record, caller = bridge) => runAs({ "mesh/caller": caller }, args, () => seat.find((t: any) => t.name === `git.${verb}`)!.run(args)) as Promise; // A read: an open pull request answers what the forge would merge now; a forge that fails is the kind failed. @@ -466,17 +457,9 @@ test("the seat's pull answers what the forge would merge now, and a head's statu assert.deepEqual([open.pull?.head, open.pull?.state, open.merging?.protected, open.merging?.required], [REVIEWED, "open", true, ["mesh/merge-gate", "mesh/repo-check"]], JSON.stringify(open)); assert.equal("body" in (open.pull ?? {}), false, "never its description"); - // Two writes of one head out of order (issue 456): a recheck's pending still in flight when the verdict is set through the - // seat; the verdict takes its turn after it and is what the head carries. - inFlight = new Promise((r) => (land = r)); - const recheck = gates.pending("novox", "mesh-catalog", REVIEWED); - const verdict = call("status", { id: "act-s", repository: "novox/mesh-catalog", commit: REVIEWED, state: "success", - context: "mesh/merge-gate", description: "passed" }); - await new Promise((r) => setImmediate(r)); - land(); - await recheck; - assert.equal((await verdict).performed, true); - assert.equal(landed.at(-1), "success passed", `the verdict lands last: ${JSON.stringify(landed)}`); + const verdict = await call("status", { id: "act-s", repository: "novox/mesh-catalog", commit: REVIEWED, state: "success", + context: "mesh/check", description: "passed" }); + assert.deepEqual([verdict.performed, landed], [true, ["success passed"]]); // A pull request the forge does not have is the kind not-found, so a caller never reads it out of the words. const gone = await call("pull", { repository: "novox/mesh-catalog", number: 404 }); assert.deepEqual([gone.pull, gone["error-kind"]], [undefined, "not-found"], JSON.stringify(gone)); @@ -511,10 +494,10 @@ test("every read and act of the seat git answers its contract, and an error is a }; const run = async (_f: string, args: string[]) => args.includes("show") || args.includes("--ref") && !args.includes("append") ? { stdout: noteShown, code: noteShown ? 0 : 1 } : (noteWritten++, { stdout: "", code: 0 }); - const seat = seatTools(forge as any, { run: run as any, container: "gitea", statuses: { status: async () => {} }, registered: () => true, judge: async () => null, watched: () => new Map() }); + const seat = seatTools(forge as any, { run: run as any, container: "gitea", registered: () => true, judge: async () => null, watched: () => new Map() }); const call = (verb: string, args: Record, over: Record = {}) => { const t = seat.find((x: any) => x.name === `git.${verb}`)!; - const wrapped = seatTools(Object.assign(Object.create(forge), over) as any, { run: run as any, container: "gitea", statuses: { status: async () => {} }, registered: () => true, judge: async () => null, watched: () => new Map() }); + const wrapped = seatTools(Object.assign(Object.create(forge), over) as any, { run: run as any, container: "gitea", registered: () => true, judge: async () => null, watched: () => new Map() }); return runAs({ "mesh/caller": "ace.x" }, args, () => wrapped.find((x: any) => x.name === t.name)!.run(args)) as Promise; }; const rows: [string, string, Record, Record, (a: any) => unknown, unknown][] = [ diff --git a/modules/gitea/test/mainrules.test.ts b/modules/gitea/test/mainrules.test.ts index 37916cd70..9575b4361 100644 --- a/modules/gitea/test/mainrules.test.ts +++ b/modules/gitea/test/mainrules.test.ts @@ -6,7 +6,7 @@ import { mkdtempSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { keepMainRules, mainRulesStep, LEFT_AS_SET } from "../dist/mainrules.js"; +import { keepMainRules } from "../dist/mainrules.js"; function env(body?: string): NodeJS.ProcessEnv { if (body === undefined) return {}; @@ -23,25 +23,6 @@ test("the setting is false unless the config says true", () => { assert.equal(keepMainRules(env('{"keep-main-rules":true}')), true); }); -test("false: the step never writes and says once that the rules are left", async () => { - let writes = 0; - const said: string[] = []; - const step = mainRulesStep(false, async () => { writes++; return ["a"]; }, (l) => said.push(l)); - await step(); await step(); await step(); - assert.equal(writes, 0); - assert.deepEqual(said, [LEFT_AS_SET]); - assert.match(LEFT_AS_SET, /main's rules are left as set at the forge \(keep-main-rules is false\)/); -}); - -test("true: every step puts the rules right", async () => { - let writes = 0; - const said: string[] = []; - const step = mainRulesStep(true, async () => { writes++; return ["a", "b"]; }, (l) => said.push(l)); - await step(); await step(); - assert.equal(writes, 2); - assert.deepEqual(said, ["[gitea] main's rule is right in 2 repositories"]); -}); - test("a token mint's account setup writes no protection while keep-main-rules is false, and does when true", async () => { const { setUpToolsAccount } = await import("../dist/tools-account.js"); for (const [keep, want] of [[false, 0], [true, 1]] as const) { diff --git a/modules/gitea/test/paging.test.ts b/modules/gitea/test/paging.test.ts index aefda7dee..8bf0bcca9 100644 --- a/modules/gitea/test/paging.test.ts +++ b/modules/gitea/test/paging.test.ts @@ -41,33 +41,6 @@ async function serve(count: number, heads: Record) { return { client, ...f }; } -test("a new head beyond the newest 20 open pull requests is announced, exactly once", async () => { - const { headsToAnnounce } = await import("../pulls.ts"); - // 25 open, every head announced before; #3 — the 23rd most recently updated — got a new head. - const announced: Record = {}; - for (let n = 1; n <= 25; n++) announced[`novox/hq#${n}@main`] = sha(n, "a"); - const { client, server, asked } = await serve(25, { 3: sha(3, "b") }); - try { - // The fault: the newest 20 alone do not hold it. - const newest = await client.listPullRequests("novox", "hq", { state: "open", sort: "recentupdate", limit: "20" }); - assert.deepEqual(headsToAnnounce("novox/hq", newest, announced), [], "the newest 20 miss #3's new head"); - asked.length = 0; - const said: string[] = []; - for (let pass = 0; pass < 2; pass++) { - // The sweep, as index.ts's pollPulls asks: no floor, every page. - const open = await client.listOpenPullsSince("novox", "hq", ""); - for (const p of headsToAnnounce("novox/hq", open, announced)) { - said.push(`#${p.number}@${p.head_sha}`); - announced[`novox/hq#${p.number}@${p.base}`] = String(p.head_sha); - } - } - assert.deepEqual(said, [`#3@${sha(3, "b")}`], "the new head past the newest 20, announced once over two passes"); - assert.ok(asked.every((q) => q.get("state") === "open" && q.get("sort") === "recentupdate"), "asked newest first"); - } finally { - server.close(); - } -}); - test("a pass pages until it reaches back past its floor, and no further", async () => { // 130 open pull requests, #n updated n hours after T0: page 1 holds #130–#81, page 2 #80–#31, page 3 #30–#1. const { client, server, asked } = await serve(130, {}); diff --git a/modules/gitea/test/pulls.test.ts b/modules/gitea/test/pulls.test.ts index bbc0daf13..3e62b2581 100644 --- a/modules/gitea/test/pulls.test.ts +++ b/modules/gitea/test/pulls.test.ts @@ -2,32 +2,7 @@ import assert from "node:assert/strict"; import { test } from "node:test"; import { createServer } from "node:http"; -// A pull request's merge check (novox/hq to-be 45 §9): each new head announced once, and the verdict set -// where the pull request shows it — an error never as a success. - -test("each open pull request's new head is announced once, and nothing closed or merged", async () => { - const { headsToAnnounce } = await import("../pulls.ts"); - const pulls = [ - { number: 1, title: "a", state: "open", merged: false, head_sha: "aaa", base: "main", html_url: "" }, - { number: 2, title: "b", state: "open", merged: false, head_sha: "bbb", html_url: "" }, - { number: 3, title: "c", state: "closed", merged: true, head_sha: "ccc", html_url: "" }, - { number: 4, title: "d", state: "open", merged: false, html_url: "" }, - ]; - const announced = { "novox/mesh-catalog#1@main": "aaa", "novox/mesh-catalog#2@": "old" }; - assert.deepEqual(headsToAnnounce("novox/mesh-catalog", pulls, announced).map((p) => p.number), [2], - "only a head not announced, of a pull request that is open"); -}); - -test("a pull request retargeted keeps its head and is announced again, into its new base (de-spiegel #6)", async () => { - const { headsToAnnounce, headKey } = await import("../pulls.ts"); - const was = { number: 6, title: "a", state: "open", merged: false, head_sha: "aaa", base: "old-trunk", html_url: "" }; - const announced: Record = { [headKey("novox/de-spiegel", 6, "old-trunk")]: "aaa" }; - assert.deepEqual(headsToAnnounce("novox/de-spiegel", [was], announced), [], "the same head into the same base is announced once"); - const retargeted = { ...was, base: "main" }; - assert.deepEqual(headsToAnnounce("novox/de-spiegel", [retargeted], announced).map((p) => p.number), [6], - "the same head into a new base is judged afresh"); - assert.notEqual(headKey("novox/de-spiegel", 6, "main"), headKey("novox/de-spiegel", 6, "old-trunk")); -}); +// A commit status, set where the pull request shows it (the seat git's status). test("a commit status is set on the commit, under the merge check's context", async () => { const { GiteaClient } = await import("../client.ts"); @@ -53,35 +28,3 @@ test("a commit status is set on the commit, under the merge check's context", as assert.equal(seen!.body.state, "failure"); assert.equal(seen!.body.context, "mesh/merge-gate"); }); - -// **A pending check says where it is** (novox/hq issue 446): announced, it waits in the build queue, since the time the -// operator reads — not "mapping this head onto its module graph" for the ten minutes of a controller check. -test("a head announced is pending in the build queue, since the local time it was announced", async () => { - const { pendingWords } = await import("../pulls.ts"); - assert.equal(pendingWords(new Date(2026, 9, 11, 3, 8, 50)), "waiting in the build queue since 03:08"); - assert.equal(pendingWords(new Date(2026, 9, 11, 14, 0, 0)), "waiting in the build queue since 14:00"); -}); - -// Issue 1301 (ADR 0503 §3, §5): gitea writes the controller's statuses only where the base still requires them. hq's main -// requires mesh/repo-check alone, so no mesh/merge-gate is written on its heads. -test("a base's required statuses decide which of the controller's statuses are written", async () => { - const { maintained, writesGate, CHECK_STATUS } = await import("../pulls.ts"); - assert.equal(CHECK_STATUS, "mesh/check"); - assert.deepEqual(maintained(undefined), ["mesh/merge-gate", "mesh/repo-check"], "a rule not read: as before"); - assert.deepEqual(maintained(["mesh/repo-check"]), ["mesh/repo-check"]); - assert.deepEqual(maintained(["mesh/check", "other/x"]), []); - assert.deepEqual(maintained([]), []); - assert.equal(writesGate(undefined), true); - assert.equal(writesGate(["mesh/repo-check"]), false); - assert.equal(writesGate(["mesh/check"]), false); - assert.equal(writesGate(["mesh/merge-gate", "mesh/repo-check"]), true); -}); - -test("a rule the forge gives with no contexts (null) requires nothing, and an unread rule is unknown", async () => { - const { requiredOf } = await import("../pulls.ts"); - // The forge's BranchProtection says status_check_contexts may be null (protection.ts); requiredOf takes it as it comes. - assert.deepEqual(requiredOf({ enable_status_check: true, status_check_contexts: null }), []); - assert.deepEqual(requiredOf({ enable_status_check: true, status_check_contexts: ["mesh/check", ""] }), ["mesh/check"]); - assert.deepEqual(requiredOf(null), []); - assert.equal(requiredOf(undefined), undefined); -}); diff --git a/modules/gitea/test/siblings.test.ts b/modules/gitea/test/siblings.test.ts deleted file mode 100644 index 0562b6351..000000000 --- a/modules/gitea/test/siblings.test.ts +++ /dev/null @@ -1,273 +0,0 @@ -// **A delivery group's member is judged beside its siblings, and a sibling's move puts that verdict out of date** -// (novox/hq ADR 0382 §4-§6, issue 867): the holder keeps the siblings each verdict names; a sibling's new head, close -// unmerged or base move (other than its own merge of the head that was read) sets both statuses of every member whose -// verdict names it back to pending, and runs nothing; a member merges only with its group passed and every sibling head -// read approved. - -import { test } from "node:test"; -import assert from "node:assert/strict"; -import { mkdtempSync } from "node:fs"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; - -import { approvedHeadsOf, besideWords, groupRefusal, keepSiblings, namesSiblings, readSiblings, siblingOutOfDate, siblingsFile, - siblingsOf, siblingStatuses, weighSiblings, type SiblingRecord } from "../dist/siblings.js"; -import { weighTrunks } from "../dist/trunk.js"; - -const h = (c: string) => c.repeat(40); -const MEMBER_HEAD = h("a"), HOST_HEAD = h("b"), HOST_BASE = h("c"), HOST_NEW = h("d"), HOST_MOVED = h("e"), MERGE = h("f"); -const sibling = { repository: "novox/mesh-host", number: 114, head: HOST_HEAD, base: "main", base_commit: HOST_BASE }; -const verdict = { owner: "novox", repo: "mesh-controller", number: 346, commit: MEMBER_HEAD, verdict: "pass", summary: "x", id: "build-1", - trunk: h("1").slice(0, 12), siblings: [sibling], judge_from: "sibling novox/mesh-controller#346 merge 123456789abc", - gate: { verdict: "pass", summary: "every machine composes ".repeat(10) }, "repo-check": { verdict: "pass", summary: "ok" } }; - -test("the siblings a verdict names are kept per member, and read back from where the holder keeps its state", () => { - const file = siblingsFile(mkdtempSync(join(tmpdir(), "gitea-siblings-"))); - const record: SiblingRecord = { "novox/mesh-controller#346": { head: MEMBER_HEAD, siblings: siblingsOf([sibling]) } }; - keepSiblings(file, record); - assert.deepEqual(readSiblings(file), record, "kept where the holder keeps its state"); - assert.deepEqual(readSiblings(null), {}); -}); - -// --- a sibling's move ----------------------------------------------------------------------------------------- - -interface World { - host: { merged: boolean; state: string; head_sha: string; merge_commit_sha?: string }; - hostBase: string; - parents: string[]; - member: { head_sha: string; state: string; merged: boolean }; - checks: Record; - written: { sha: string; context: string; state: string; description: string }[]; - reads: string[]; -} -const world = (): World => ({ host: { merged: false, state: "open", head_sha: HOST_HEAD }, hostBase: HOST_BASE, parents: [], - member: { head_sha: MEMBER_HEAD, state: "open", merged: false }, - checks: { "mesh/merge-gate": { state: "success", description: "head aaaaaaaaaaaa merged into trunk 111111111111 beside mesh-host#114@bbbbbbbb — pass: x" }, - "mesh/repo-check": { state: "success", description: "pass: y" } }, - written: [], reads: [] }); -const forgeOf = (w: World) => ({ - async getPullRequest(owner: string, repo: string, n: number) { - w.reads.push(`${owner}/${repo}#${n}`); - return repo === "mesh-host" ? { number: n, title: "", html_url: "", ...w.host } : { number: n, title: "", html_url: "", merged: false, ...w.member }; - }, - async branchHead(_o: string, _r: string, _b: string) { return w.hostBase; }, - async commitParents() { return w.parents; }, -}); -const gatesOf = (w: World) => ({ - async outOfDate(_o: string, _r: string, sha: string, decide: (c: any) => any[] | Promise) { - const put = await decide(w.checks); - for (const s of put) { w.written.push({ sha, ...s }); w.checks[s.context] = { state: s.state, description: s.description }; } - return put; - }, -}); -const recordOf = (): SiblingRecord => ({ "novox/mesh-controller#346": { head: MEMBER_HEAD, siblings: siblingsOf([sibling]) } }); -const weigh = (w: World, record: SiblingRecord, repository = "novox/mesh-host", number?: number) => - weighSiblings(forgeOf(w) as any, gatesOf(w) as any, record, repository, number); - -test("a sibling's new head puts both statuses of the member back to pending, with the reason, and runs nothing", async () => { - const w = world(); - w.host.head_sha = HOST_NEW; - await weigh(w, recordOf(), "novox/mesh-host", 114); - assert.deepEqual(w.written.map((s) => [s.sha, s.context, s.state]), - [[MEMBER_HEAD, "mesh/merge-gate", "pending"], [MEMBER_HEAD, "mesh/repo-check", "pending"]]); - assert.equal(w.written[0].description, - "out of date: judged beside mesh-host#114 at bbbbbbbbbbbb, it is now at dddddddddddd; judged again on a recheck or when it is merged"); - assert.ok(siblingOutOfDate(w.written[0].description)); - // Already pending (out of date, or a check on its way): not written again. - w.written.length = 0; - await weigh(w, recordOf(), "novox/mesh-host", 114); - assert.deepEqual(w.written, []); -}); - -test("a sibling closed unmerged puts the member's verdict out of date", async () => { - const w = world(); - w.host.state = "closed"; - await weigh(w, recordOf(), "novox/mesh-host", 114); - assert.equal(w.written.length, 2); - assert.match(w.written[0].description, /^out of date: judged beside mesh-host#114 was closed unmerged/); -}); - -test("a move of the sibling's base puts it out of date, naming both commits", async () => { - const w = world(); - w.hostBase = HOST_MOVED; - await weigh(w, recordOf()); - assert.equal(w.written.length, 2); - assert.match(w.written[0].description, /mesh-host#114 merged onto cccccccccccc, its main is now eeeeeeeeeeee/); -}); - -test("the sibling's own merge of the head read onto the base read does not, in either order of the two merges", async () => { - // The sibling merges first (a merge commit of the base commit read and the head read). - const w = world(); - Object.assign(w.host, { merged: true, state: "closed", merge_commit_sha: MERGE }); - w.hostBase = MERGE; - w.parents = [HOST_BASE, HOST_HEAD]; - const record = recordOf(); - assert.equal(await weigh(w, record), true, "the record changes: the sibling is marked merged"); - assert.deepEqual(w.written, []); - assert.equal(record["novox/mesh-controller#346"].siblings[0].merged, true); - // A move of that base afterwards is no sibling's move: the sibling is merged. - w.hostBase = HOST_MOVED; - await weigh(w, record); - assert.deepEqual(w.written, []); - - // The member merges first: its own repository moves, which no member's verdict names as a sibling. - const m = world(); - m.member.merged = true; - await weigh(m, recordOf(), "novox/mesh-controller"); - assert.deepEqual(m.written, []); - // And the sibling then merges: still not a move. - Object.assign(m.host, { merged: true, state: "closed", merge_commit_sha: MERGE }); - m.parents = [HOST_BASE, HOST_HEAD]; - await weigh(m, recordOf()); - assert.deepEqual(m.written, []); - - // A squash is one parent: the base commit read. - const q = world(); - Object.assign(q.host, { merged: true, state: "closed", merge_commit_sha: MERGE }); - q.parents = [HOST_BASE]; - await weigh(q, recordOf()); - assert.deepEqual(q.written, []); -}); - -test("a merge of another head, or onto another base, is a move", async () => { - const other = world(); - Object.assign(other.host, { merged: true, state: "closed", merge_commit_sha: MERGE, head_sha: HOST_NEW }); - other.parents = [HOST_BASE, HOST_NEW]; - await weigh(other, recordOf()); - assert.equal(other.written.length, 2); - const onto = world(); - Object.assign(onto.host, { merged: true, state: "closed", merge_commit_sha: MERGE }); - onto.parents = [HOST_MOVED, HOST_HEAD]; - await weigh(onto, recordOf()); - assert.equal(onto.written.length, 2); -}); - -test("only the members whose verdict names the sibling are touched, and a member that moved on is dropped", async () => { - // Another sibling's move, or another repository's: nothing read, nothing written. - const w = world(); - w.host.head_sha = HOST_NEW; - await weigh(w, recordOf(), "novox/mesh-lab"); - await weigh(w, recordOf(), "novox/mesh-host", 999); - assert.deepEqual(w.written, []); - // A verdict that is none (a gate not yet judged) is left alone. - const none = world(); - none.host.head_sha = HOST_NEW; - none.checks = {}; - await weigh(none, recordOf()); - assert.deepEqual(none.written, []); - // The member has a new head: its old verdict is no verdict; the record is dropped, nothing written to the old head. - const moved = world(); - moved.host.head_sha = HOST_NEW; - moved.member.head_sha = h("9"); - const record = recordOf(); - assert.equal(await weigh(moved, record), true); - assert.deepEqual(moved.written, []); - assert.deepEqual(record, {}); - // A repository without a repo-check gets no pending one a verdict would never set. - assert.deepEqual(siblingStatuses({ "mesh/merge-gate": { state: "success" } }, "x").map((s) => s.context), ["mesh/merge-gate"]); -}); - -test("a trunk that moved tells the siblings' weighing; one first seen does not", async () => { - const told: string[] = []; - const forge = { listOpenPulls: async () => [{ number: 1, title: "", state: "open", merged: false, head_sha: h("2"), base: "main", html_url: "" }], - branchHead: async () => h("3") }; - const gates = { outOfDate: async () => [] }; - const trunks: Record = {}; - await weighTrunks(forge as any, gates as any, trunks, "novox", "mesh-host", async (b) => { told.push(b); }); - assert.deepEqual(told, [], "first seen"); - trunks["novox/mesh-host:main"] = h("4"); - await weighTrunks(forge as any, gates as any, trunks, "novox", "mesh-host", async (b) => { told.push(b); }); - assert.deepEqual(told, ["main"]); -}); - -// --- the merge rule ------------------------------------------------------------------------------------------- - -test("the group rule names every missing thing: the group status at each head read, and each head the Approve does not cover", async () => { - const self = { owner: "novox", repo: "mesh-controller", number: 346, head: MEMBER_HEAD }; - const statuses: Record = {}; - const forge = { - headChecks: async (_o: string, r: string, sha: string) => (statuses[`${r}@${sha}`] ? { "mesh/delivery-group": { state: statuses[`${r}@${sha}`] } } : {}), - }; - const siblings = siblingsOf([sibling]); - const none = await groupRefusal(forge, self, siblings, undefined); - assert.match(none!, /mesh\/delivery-group is missing at mesh-controller#346@aaaaaaaaaaaa; mesh-controller#346@aaaaaaaaaaaa is covered by no Approve: the merge was handed no approved heads; mesh\/delivery-group is missing at mesh-host#114@bbbbbbbbbbbb; mesh-host#114@bbbbbbbbbbbb is covered by no Approve/); - statuses[`mesh-controller@${MEMBER_HEAD}`] = "success"; - statuses[`mesh-host@${HOST_HEAD}`] = "pending"; - const approved = approvedHeadsOf([`novox/mesh-controller@${MEMBER_HEAD}`])!; - assert.match((await groupRefusal(forge, self, siblings, approved))!, /^mesh\/delivery-group is pending at mesh-host#114@bbbbbbbbbbbb; mesh-host#114@bbbbbbbbbbbb is not among the heads the Approve covers$/); - statuses[`mesh-host@${HOST_HEAD}`] = "success"; - assert.match((await groupRefusal(forge, self, siblings, approved))!, /^mesh-host#114@bbbbbbbbbbbb is not among the heads the Approve covers$/); - approved.set("novox/mesh-host", "c".repeat(40)); - assert.match((await groupRefusal(forge, self, siblings, approved))!, /^mesh-host#114@bbbbbbbbbbbb is not the head the Approve covers \(cccccccccccc\)$/); - approved.set("novox/mesh-host", HOST_HEAD.toLowerCase()); - assert.equal(await groupRefusal(forge, self, siblings, approved), undefined); - // A read that fails is named, never read as a pass. - const broken = { headChecks: async () => { throw new Error("the forge is away"); } }; - assert.match((await groupRefusal(broken, self, siblings, approved))!, /could not be read \(the forge is away\)/); - assert.equal(besideWords(siblings), "beside mesh-host#114@bbbbbbbb"); -}); - -test("a merged or closed member's entry is dropped, so a sibling's later push never re-pends its merged head", async () => { - // mesh-host#114 judged beside mesh-controller#346; #114 then merged; a push to #346 follows. - const w = world(); - w.host.merged = true; // the member (mesh-host#114) is merged - w.member.head_sha = h("9"); // the sibling (mesh-controller#346) has a new head - const record: SiblingRecord = { "novox/mesh-host#114": { head: HOST_HEAD, siblings: siblingsOf([ - { repository: "novox/mesh-controller", number: 346, head: MEMBER_HEAD, base: "main", base_commit: h("2") }]) } }; - assert.equal(await weigh(w, record, "novox/mesh-controller", 346), true); - assert.deepEqual(w.written, [], "the merged head is not put back to pending"); - assert.deepEqual(record, {}, "and its entry is gone"); - // On its own close or merge the member's entry goes too, whatever the siblings do. - const own = world(); - own.host.state = "closed"; - const kept = recordOf(); - const mine: SiblingRecord = { "novox/mesh-host#114": { head: HOST_HEAD, siblings: kept["novox/mesh-controller#346"].siblings } }; - assert.equal(await weigh(own, mine, "novox/mesh-host", 114), true); - assert.deepEqual(mine, {}); - assert.deepEqual(own.written, []); -}); - -// --- the composed verdict names the siblings of each member (issue 1049) -------------------------------------- - -// Issue 1049: the 2026-10-17 23:48 group (mesh-controller #375 beside mesh-catalog #353). #375's own verdict named #353 -// at its old head; #353 then got a new head, and the group's composed verdict passed for both new heads. The record still -// held the old head, so #353's own merge of the NEW head read as "merged otherwise than its head" and put #375's gate back -// to pending, and the group merged in two passes. -const composedMembers = () => [ - { owner: "novox", repo: "mesh-controller", number: 346, commit: MEMBER_HEAD, trunk: "111111111111", base: "main" }, - { owner: "novox", repo: "mesh-host", number: 114, commit: HOST_NEW, trunk: HOST_BASE.slice(0, 12), base: "main" }, -]; -/** The record a group's composed verdict once left: for each member, the other members at the heads and base commits it composed. */ -const composedRecord = (members: ReturnType): SiblingRecord => Object.fromEntries(members.map((m) => [ - `${m.owner}/${m.repo}#${m.number}`.toLowerCase(), { head: m.commit, siblings: members.filter((o) => o !== m).map((o) => ({ - repository: `${o.owner}/${o.repo}`, number: o.number, head: o.commit, base: o.base, base_commit: o.trunk })) }])); -const staleRecord = (): SiblingRecord => ({ "novox/mesh-controller#346": { head: MEMBER_HEAD, siblings: siblingsOf([sibling]) } }); - -test("without the composed verdict's siblings, a sibling's own merge of its new head is a move (the pin of issue 1049)", async () => { - const w = world(); - Object.assign(w.host, { merged: true, state: "closed", merge_commit_sha: MERGE, head_sha: HOST_NEW }); - w.parents = [HOST_BASE, HOST_NEW]; - await weigh(w, staleRecord()); - assert.equal(w.written.length, 2, "the record names the old head: the merge reads as another head's"); -}); - -test("a composed verdict names the siblings of each member at the heads and base commits it composed, so a sibling's own merge of its new head is no move (issue 1049)", async () => { - const record = composedRecord(composedMembers()); - assert.deepEqual(record["novox/mesh-controller#346"], { head: MEMBER_HEAD, - siblings: [{ repository: "novox/mesh-host", number: 114, head: HOST_NEW, base: "main", base_commit: HOST_BASE.slice(0, 12) }] }); - assert.deepEqual(record["novox/mesh-host#114"], { head: HOST_NEW, - siblings: [{ repository: "novox/mesh-controller", number: 346, head: MEMBER_HEAD, base: "main", base_commit: "111111111111" }] }); - const w = world(); - Object.assign(w.host, { merged: true, state: "closed", merge_commit_sha: MERGE, head_sha: HOST_NEW }); - w.parents = [HOST_BASE, HOST_NEW]; - await weigh(w, record); - assert.deepEqual(w.written, [], "its own merge of the head the composed check judged leaves the member's gate alone"); - assert.equal(record["novox/mesh-controller#346"].siblings[0].merged, true); - // A merge of any other head, or onto any other base, is still a move. - const other = world(); - const again = composedRecord(composedMembers()); - Object.assign(other.host, { merged: true, state: "closed", merge_commit_sha: MERGE, head_sha: HOST_NEW }); - other.parents = [HOST_MOVED, HOST_NEW]; - await weigh(other, again); - assert.equal(other.written.length, 2); -}); diff --git a/modules/gitea/test/tools-account.test.ts b/modules/gitea/test/tools-account.test.ts index f77b199ae..637d9ed1a 100644 --- a/modules/gitea/test/tools-account.test.ts +++ b/modules/gitea/test/tools-account.test.ts @@ -487,9 +487,9 @@ test("a glob rule covering main gives its approvals and override, not its status assert.deepEqual(rule.merge_whitelist_usernames, [TOOLS_ACCOUNT]); }); -test("a start with the token kept still puts main's rules right: the module's start asks for it each time (issue 948, review round 1)", async () => { - const src = await readFile(new URL("../index.ts", import.meta.url), "utf8"); - assert.match(src, /ensureMainMerges\("novox"\)/, "index.ts calls it outside the token's mint"); +// The module's start no longer puts main's rules right every ten minutes (design 47 H §764; H-A-100): ensureMainMerges is +// the mint's alone. +test("ensureMainMerges makes a main rule gone again with no token minted", async () => { const { GiteaAdmin } = await import("../dist/client.js"); const env = await environment(); forge.mainRules.delete("hq"); diff --git a/modules/gitea/test/trunk.test.ts b/modules/gitea/test/trunk.test.ts deleted file mode 100644 index 2323f0afd..000000000 --- a/modules/gitea/test/trunk.test.ts +++ /dev/null @@ -1,167 +0,0 @@ -// **A merge check's verdict holds for the trunk commit it judged** (novox/hq ADR 0323, issue 341): both statuses name -// the head and the trunk commit; a trunk's move puts the merge check of every open pull request into that trunk — and -// of none into another — back to pending with the reason, and runs nothing; a verdict that lands judged against a trunk -// that has moved since is said out of date. - -import { test } from "node:test"; -import assert from "node:assert/strict"; - -import { checkOnItsWay, outOfDateStatuses, outOfDateWords, trunkNamed, verdictAtTrunk, weighTrunks } from "../dist/trunk.js"; -import { GateWriter } from "../dist/gate.js"; - -const HEAD = "dfb1415aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; -const TRUNK = "a2c2c35bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; -const MOVED = "d28ca95ccccccccccccccccccccccccccccccccc"; - -test("a trunk's move puts an open head's merge check out of date, naming both commits, unless a check is on its way", () => { - const verdict = (trunk: string, state = "success") => ({ - "mesh/merge-gate": { state, description: `head ${HEAD.slice(0, 12)} merged into trunk ${trunk.slice(0, 12)} — pass: x` }, - "mesh/repo-check": { state, description: `head ${HEAD.slice(0, 12)} merged into trunk ${trunk.slice(0, 12)} — pass: y` } }); - const put = outOfDateStatuses(verdict(TRUNK), MOVED); - assert.deepEqual(put.map((s) => [s.context, s.state]), [["mesh/merge-gate", "pending"], ["mesh/repo-check", "pending"]]); - assert.equal(put[0].description, "out of date: judged against trunk a2c2c35bbbbb, the trunk is now d28ca95ccccc; " + - "judged again on a recheck or when it is merged"); - assert.equal(put[0].description, outOfDateWords(TRUNK, MOVED)); - assert.equal(trunkNamed(put[0].description), "a2c2c35bbbbb", "an out-of-date status still names what it judged"); - // A failure is out of date too: it judged another merge. - assert.equal(outOfDateStatuses(verdict(TRUNK, "failure"), MOVED).length, 2); - // Already judged against the trunk's head; a check on its way or already out of date; no check yet: left alone. - assert.deepEqual(outOfDateStatuses(verdict(MOVED), MOVED), []); - assert.deepEqual(outOfDateStatuses({ "mesh/merge-gate": { state: "pending", description: "building on ace since 14:02" } }, MOVED), []); - assert.deepEqual(outOfDateStatuses({ "mesh/merge-gate": { state: "pending", description: put[0].description } }, MOVED), []); - assert.deepEqual(outOfDateStatuses({}, MOVED), []); - // A head with no repository check of its own gets no pending one a verdict would never set. - assert.deepEqual(outOfDateStatuses({ "mesh/merge-gate": verdict(TRUNK)["mesh/merge-gate"] }, MOVED).map((s) => s.context), - ["mesh/merge-gate"]); - // A verdict naming no trunk, of a build seat from before the rule, judged an earlier trunk. - assert.match(outOfDateStatuses({ "mesh/merge-gate": { state: "success", description: "pass: x" } }, MOVED)[0].description, - /judged against a trunk it does not name, the trunk is now d28ca95ccccc/); - - assert.ok(checkOnItsWay({ state: "pending", description: "waiting in the build queue since 14:00" })); - assert.ok(checkOnItsWay({ state: "pending", description: "checking again: a person asked" })); - assert.ok(!checkOnItsWay({ state: "pending", description: put[0].description })); - assert.deepEqual(verdictAtTrunk(verdict(MOVED), MOVED), { current: true, onItsWay: false, judged: "d28ca95ccccc" }); - assert.equal(verdictAtTrunk(verdict(TRUNK), MOVED).current, false); -}); - -/** A forge holding open pull requests into two branches, each head's statuses, and what was written. */ -function aForge(pulls: { number: number; base: string; head: string }[], heads: Record) { - const statuses = new Map>(); - const writes: string[] = []; - const verdict = (trunk: string) => ({ - "mesh/merge-gate": { state: "success", description: `head x merged into trunk ${trunk.slice(0, 12)} — pass` }, - "mesh/repo-check": { state: "success", description: `head x merged into trunk ${trunk.slice(0, 12)} — pass` } }); - for (const p of pulls) statuses.set(p.head, verdict(heads[p.base])); - const forge = { - listOpenPulls: async () => pulls.map((p) => ({ number: p.number, title: "t", state: "open", merged: false, base: p.base, - head: "feat", head_sha: p.head, html_url: `http://forge/pulls/${p.number}` })), - branchHead: async (_o: string, _r: string, branch: string) => heads[branch], - setCommitStatus: async (_o: string, _r: string, sha: string, s: { context: string; state: string; description: string }) => { - writes.push(`${sha} ${s.context} ${s.state}`); - statuses.set(sha, { ...statuses.get(sha), [s.context]: { state: s.state, description: s.description } }); - }, - headChecks: async (_o: string, _r: string, sha: string) => statuses.get(sha) ?? {}, - }; - return { forge, writes, statuses }; -} - -test("a push to a trunk puts every open pull request into it out of date — twenty-five of them — and none into another", async () => { - const heads: Record = { main: TRUNK, release: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" }; - const pulls = Array.from({ length: 25 }, (_, i) => ({ number: i + 1, base: "main", head: `${String(i).padStart(2, "0")}`.repeat(20) })); - pulls.push({ number: 99, base: "release", head: "9".repeat(40) }); - const { forge, writes, statuses } = aForge(pulls, heads); - const gates = new GateWriter(forge); - const trunks: Record = {}; - - // First sight: each trunk is taken as it is, and nothing is put out of date. - assert.equal(await weighTrunks(forge, gates, trunks, "novox", "mesh-host"), true); - assert.deepEqual(writes, []); - assert.equal(trunks["novox/mesh-host:main"], TRUNK); - // Nothing moved: nothing read past the trunks, nothing written. - assert.equal(await weighTrunks(forge, gates, trunks, "novox", "mesh-host"), false); - - heads.main = MOVED; - assert.equal(await weighTrunks(forge, gates, trunks, "novox", "mesh-host"), true); - assert.equal(writes.length, 50, "both statuses of each of the 25 pull requests into main"); - for (const p of pulls.filter((p) => p.base === "main")) { - const s = statuses.get(p.head)!; - assert.equal(s["mesh/merge-gate"].state, "pending"); - assert.match(s["mesh/merge-gate"].description!, /judged against trunk a2c2c35bbbbb, the trunk is now d28ca95ccccc/); - assert.equal(s["mesh/repo-check"].state, "pending"); - } - assert.equal(statuses.get("9".repeat(40))!["mesh/merge-gate"].state, "success", "a pull request into another branch is left"); - assert.equal(trunks["novox/mesh-host:main"], MOVED); - - // Weighed again, nothing more: the move was taken once, and the out-of-date statuses are left as they are. - writes.length = 0; - heads.main = "f".repeat(40); - await weighTrunks(forge, gates, trunks, "novox", "mesh-host"); - assert.deepEqual(writes, [], "a check already out of date is not written again"); -}); - -test("a trunk that cannot be read is weighed again on the next pass, never recorded half", async () => { - const heads: Record = { main: TRUNK }; - const { forge } = aForge([{ number: 1, base: "main", head: "1".repeat(40) }], heads); - const trunks: Record = { "novox/mesh-host:main": TRUNK }; - heads.main = MOVED; - const failing = { ...forge, setCommitStatus: async () => { throw new Error("the forge is away"); } }; - assert.equal(await weighTrunks(failing, new GateWriter(failing), trunks, "novox", "mesh-host"), false); - assert.equal(trunks["novox/mesh-host:main"], TRUNK, "not recorded: the next pass weighs it again"); -}); - -test("a verdict judged against a trunk newer than the pass's first read is not put out of date", async () => { - const NEWER = "ffffffffffffffffffffffffffffffffffffffff"; - const heads: Record = { main: TRUNK }; - const { forge, statuses } = aForge([{ number: 1, base: "main", head: HEAD }], heads); - const trunks: Record = { "novox/mesh-host:main": TRUNK }; - // The pass reads main at MOVED; by the head's turn the trunk moved again, and the head's check answered for it. - let reads = 0; - const moving = { ...forge, branchHead: async () => (reads++ === 0 ? MOVED : NEWER) }; - statuses.set(HEAD, { "mesh/merge-gate": { state: "success", description: `head x merged into trunk ${NEWER.slice(0, 12)} — pass` } }); - await weighTrunks(moving, new GateWriter(moving), trunks, "novox", "mesh-host"); - assert.equal(statuses.get(HEAD)!["mesh/merge-gate"].state, "success", "a fresh verdict was put out of date"); -}); - -test("a release branch's move puts every open pull request into it out of date, as a trunk's does (ADR 0358 §5)", async () => { - // novox/hq ADR 0358 §5: for a pull request into a release branch, the release branch takes the trunk's place in ADR - // 0323 — the check merges the head onto the release branch's head and names it as the trunk commit, so the release - // branch's move puts its open verdicts out of date, and leaves those into main alone. - const release = "feat/57-config-and-provisioning"; - const heads: Record = { main: TRUNK, [release]: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" }; - const pulls = [{ number: 315, base: release, head: "3".repeat(40) }, { number: 1, base: "main", head: "1".repeat(40) }]; - const { forge, statuses } = aForge(pulls, heads); - const gates = new GateWriter(forge); - const trunks: Record = {}; - await weighTrunks(forge, gates, trunks, "novox", "mesh-controller"); - - heads[release] = MOVED; - assert.equal(await weighTrunks(forge, gates, trunks, "novox", "mesh-controller"), true); - const s = statuses.get("3".repeat(40))!; - assert.equal(s["mesh/merge-gate"].state, "pending"); - assert.match(s["mesh/merge-gate"].description!, /judged against trunk eeeeeeeeeeee, the trunk is now d28ca95ccccc/); - assert.equal(s["mesh/repo-check"].state, "pending"); - assert.equal(statuses.get("1".repeat(40))!["mesh/merge-gate"].state, "success", "a pull request into main is left"); - assert.equal(trunks[`novox/mesh-controller:${release}`], MOVED); - - // The merge tool's rule reads the same: a verdict judged against the release branch's old head is not current. - assert.equal(verdictAtTrunk({ "mesh/merge-gate": { state: "success", - description: "head x merged into trunk eeeeeeeeeeee — pass" } }, MOVED).current, false); -}); - -// Issue 1301: a trunk's move puts out of date only the controller's statuses the base still requires; on hq that is -// mesh/repo-check alone, judged by its own words; on a main requiring mesh/check, none (the checker's, ADR 0447 §4). -test("a trunk's move writes only the statuses the base still requires", async () => { - const { outOfDateStatuses, trunkNamed } = await import("../trunk.ts"); - const old = "1".repeat(40), now = "2".repeat(40); - const repo = { state: "success", description: `head ${"a".repeat(12)} merged into trunk ${old.slice(0, 12)} — pass: y` }; - const gate = { state: "success", description: `head ${"a".repeat(12)} merged into trunk ${old.slice(0, 12)} — pass: x` }; - assert.deepEqual(outOfDateStatuses({ "mesh/repo-check": repo }, now, ["mesh/repo-check"]).map((s: any) => s.context), ["mesh/repo-check"]); - assert.deepEqual(outOfDateStatuses({ "mesh/merge-gate": gate, "mesh/repo-check": repo }, now, ["mesh/repo-check"]).map((s: any) => s.context), - ["mesh/repo-check"]); - assert.deepEqual(outOfDateStatuses({ "mesh/merge-gate": gate, "mesh/repo-check": repo }, now, ["mesh/check"]), []); - assert.deepEqual(outOfDateStatuses({ "mesh/merge-gate": gate, "mesh/repo-check": repo }, now).map((s: any) => s.context), - ["mesh/merge-gate", "mesh/repo-check"], "a rule not read: as before"); - // The checker's line names its trunk as " on ". - assert.equal(trunkNamed(`pass ${"a".repeat(12)} on ${old.slice(0, 12)} | unit 3 run`), old.slice(0, 12)); - assert.equal(trunkNamed("building on shanks since 10:00"), undefined); -}); diff --git a/modules/gitea/test/watch.test.ts b/modules/gitea/test/watch.test.ts index 39572a399..5680f693c 100644 --- a/modules/gitea/test/watch.test.ts +++ b/modules/gitea/test/watch.test.ts @@ -1,9 +1,9 @@ import assert from "node:assert/strict"; -import { mock, test } from "node:test"; +import { test } from "node:test"; import type { GiteaPull } from "../client.ts"; -// **No poll of the forge** (issue 1422, notes 10 and 11): the forge's events announce heads and merges; a pass runs once at -// the start and when the receiver listens. +// **No poll of the forge for heads** (issue 1422; design 47 H §764, H-A-100): the forge's events say a pull request's new +// head as pull.updated, a merge and a close; nothing weighs a sibling or asks a merge check. const edited = (base: string) => ({ name: "forge.pull-request.edited", body: { owner: "novox", repo: "hq", number: 7, base, merged: false } }); @@ -12,10 +12,9 @@ function deps() { return { calls, d: { getRepo: async () => ({ r: 1 }), getPull: async () => ({ state: "open", base: "trunk" }) as any, - announceHead: async () => { calls.push("head"); }, + announceUpdated: async () => { calls.push("updated"); }, announceMerge: async () => { calls.push("merge"); }, announceClosed: async () => { calls.push("closed"); }, - siblingMoves: async () => { calls.push("siblings"); }, } }; } @@ -23,51 +22,15 @@ test("an edited event announces the head into the base it now has, without a pol const { actOn } = await import("../watch.ts"); const { calls, d } = deps(); await actOn(edited("trunk"), d); - assert.deepEqual(calls, ["head"]); + assert.deepEqual(calls, ["updated"]); }); test("a close without a merge is announced by its event, a merged close is left to the merge event", async () => { const { actOn } = await import("../watch.ts"); const { calls, d } = deps(); await actOn({ name: "forge.pull-request.closed", body: { owner: "novox", repo: "hq", number: 7, merged: false } }, d); - assert.deepEqual(calls, ["closed", "siblings"]); + assert.deepEqual(calls, ["closed"]); calls.length = 0; await actOn({ name: "forge.pull-request.closed", body: { owner: "novox", repo: "hq", number: 7, merged: true } }, d); assert.deepEqual(calls, []); }); - -test("no timer polls after the start: one pass ends and nothing is scheduled; a failed one is asked again", async () => { - const { startPass } = await import("../watch.ts"); - mock.timers.enable({ apis: ["setTimeout", "setInterval"] }); - try { - let runs = 0; - await startPass(async () => { runs++; }, { retryMs: 60_000, log: () => {} }); - mock.timers.tick(24 * 3600_000); - assert.equal(runs, 1, "a day later the forge was not asked again"); - let tries = 0; - const retried = startPass(async () => { if (++tries < 2) throw new Error("forge down"); }, { retryMs: 60_000, log: () => {} }); - await Promise.resolve(); await Promise.resolve(); - mock.timers.tick(60_000); - await retried; - assert.equal(tries, 2); - } finally { - mock.timers.reset(); - } -}); - -test("a start pass announces a head nobody announced, and only remembers one the forge shows a check of", async () => { - const { announceNewHeads } = await import("../watch.ts"); - const pull = (n: number, sha: string): GiteaPull => ({ number: n, state: "open", head_sha: sha, base: "main", - updated_at: new Date().toISOString() } as unknown as GiteaPull); - const announced: number[] = [], said: string[] = []; - const heads: Record = {}; - await announceNewHeads("novox/hq", [pull(1, "a".repeat(40)), pull(2, "b".repeat(40))], heads, false, - new Date(Date.now() - 24 * 3600_000).toISOString(), { - alreadyChecked: async (p) => p.number === 2, - announceHead: async (p) => { announced.push(p.number); return true; }, - missed: (w) => said.push(w), - }); - assert.deepEqual(announced, [1]); - assert.equal(heads["novox/hq#2@main"], "b".repeat(40), "the checked head is remembered, not announced"); - assert.equal(said.length, 1); -}); diff --git a/modules/gitea/trunk.ts b/modules/gitea/trunk.ts deleted file mode 100644 index c1e2d98da..000000000 --- a/modules/gitea/trunk.ts +++ /dev/null @@ -1,211 +0,0 @@ -// **A merge check's verdict holds for the trunk commit it judged** (novox/hq ADR 0323, issue 341). -// -// The build seat checks a pull request's head merged into its trunk's head at that moment — the verdict's trunk -// commit — and the verdict names both: "head 1a2b3c4d5e6f merged into trunk 7a8b9c0d1e2f", in both statuses' -// descriptions. When the trunk moves, every open pull request's verdict into it is out of date: -// -// - **the forge's holder** (index.ts) hears the move on its own — it reads each trunk's head on every pass that -// looks at the repository, never through mesh-delivery (ADR 0239 §36) — and sets mesh/merge-gate and -// mesh/repo-check on the head of every open pull request into that trunk back to pending, with the reason. -// Nothing is run for it: a recheck, a new head, or the merge tool asks the check again; -// - **the merge tool** (tools/index.ts) compares the trunk commit the head's verdict names with the trunk's head at -// the moment it is asked, merges nothing when they differ, asks the head's check once — the same announcement a -// new head makes — and never merges by itself after. -// -// Since issue 1301 (ADR 0447 §4) a main that requires mesh/check is the checker's: its line names the trunk commit it judged -// ("pass on "), the checker sets it out of date when the trunk moves, and the merge tool refuses a verdict -// of another trunk and asks nothing. The two statuses above are kept only where a base still requires them (a revert, -// pulls.ts maintained). -// -// Pure functions and two small files of the module's own state, so this is tested without a forge. - -import type { CommitStatus, GiteaClient, GiteaPull, HeadCheck } from "./client.js"; -import { CHECK_CONTEXT, WAITS_FOR_COMPOSED, maintained, requiredOf } from "./pulls.js"; - -/** A commit as a verdict names it: its first twelve characters. */ -export function twelve(sha: string | undefined): string { - return String(sha ?? "").slice(0, 12).toLowerCase(); -} - -/** Whether two commits are one, however either is abbreviated (seven characters at least). */ -export function sameCommit(a: string | undefined, b: string | undefined): boolean { - const x = String(a ?? "").toLowerCase(), y = String(b ?? "").toLowerCase(); - if (x.length < 7 || y.length < 7) return false; - return x.length <= y.length ? y.startsWith(x) : x.startsWith(y); -} - -/** What a verdict judged, as both statuses' descriptions begin (ADR 0323 §3). */ -export function judgedWords(head: string, trunk: string): string { - return `head ${twelve(head)} merged into trunk ${twelve(trunk)}`; -} - -/** The trunk commit a merge check's status names: the one its verdict judged ("merged into trunk …"), or, for a - * verdict put out of date, the one it had judged ("judged against trunk …"). Undefined when it names none, as a - * verdict of a build seat from before ADR 0323 does. */ -export function trunkNamed(description: string | undefined): string | undefined { - const m = /(?:merged into|judged against) trunk ([0-9a-f]{7,64})\b/i.exec(description ?? "") - // The checker's mesh/check line (ADR 0447 §3): " on ". - ?? /\b[0-9a-f]{12} on ([0-9a-f]{12})\b/i.exec(description ?? ""); - return m ? m[1].toLowerCase() : undefined; -} - -/** A verdict out of date, as its statuses say it (ADR 0323 §4). */ -export function outOfDateWords(judged: string | undefined, now: string): string { - const was = judged ? `judged against trunk ${twelve(judged)}` : "judged against a trunk it does not name"; - return `out of date: ${was}, the trunk is now ${twelve(now)}; judged again on a recheck or when it is merged`; -} - -/** Whether a pending gate says a check of the head is asked and not yet answered: waiting in the build queue, - * building on a machine (gate.ts), or asked again by a recheck (mesh-delivery). Such a check is not asked again. */ -export function checkOnItsWay(gate: HeadCheck | undefined): boolean { - if (!gate || gate.state !== "pending") return false; - const d = gate.description ?? ""; - return d.startsWith("waiting in the build queue") || d.startsWith("building on ") || d.startsWith("checking again"); -} - -/** Whether a gate says it waits for its group's composed check. */ -export function waitsForComposed(gate: HeadCheck | undefined): boolean { - return !!gate && gate.state === "pending" && (gate.description ?? "").startsWith(WAITS_FOR_COMPOSED); -} - -/** - * The statuses a trunk's move puts back to pending on one open pull request's head, from what the forge holds on it - * now: none when it has no merge check yet, when a check is pending (on its way, or already out of date — its - * verdict is weighed against the trunk when it lands), or when its verdict judged the trunk's head. Otherwise the - * gate, and the repository's own check when the head has one — a pending repo-check a verdict will not set would - * stand for ever (novox/hq issue 305's rule). - */ -export function outOfDateStatuses(checks: Record, trunkHead: string, required?: string[]): CommitStatus[] { - // Only the statuses the base still requires (issue 1301): the first of them the head holds judges. - const kept = maintained(required).filter((c) => checks[c]); - const judge = kept.length ? checks[kept[0]] : undefined; - if (!judge || judge.state === "pending") return []; - const judged = trunkNamed(judge.description); - if (judged && sameCommit(judged, trunkHead)) return []; - const description = outOfDateWords(judged, trunkHead); - return kept.map((context) => ({ state: "pending" as const, context, description })); -} - -/** Where the merge tool stands with a head's verdict and the trunk's head (ADR 0323 §5). */ -export interface VerdictAtTrunk { - /** The verdict judged the trunk's head: the required statuses decide, as before. */ - current: boolean; - /** A check of the head is asked and not yet answered: nothing is asked again. */ - onItsWay: boolean; - /** The trunk commit the verdict names, when it names one. */ - judged?: string; -} - -export function verdictAtTrunk(checks: Record, trunkHead: string, context = CHECK_CONTEXT): VerdictAtTrunk { - const gate = checks[context]; - const judged = trunkNamed(gate?.description); - if (checkOnItsWay(gate)) return { current: false, onItsWay: true, judged }; - return { current: !!gate && gate.state !== "pending" && !!judged && sameCommit(judged, trunkHead), onItsWay: false, judged }; -} - -/** The pending words the merge tool's ask of a check is said with: in the build queue from now, as an announced head's. */ -export function askedWords(at: Date, trunkHead: string): string { - const two = (n: number) => String(n).padStart(2, "0"); - return `waiting in the build queue since ${two(at.getHours())}:${two(at.getMinutes())}: asked by the merge tool ` + - `against trunk ${twelve(trunkHead)}`; -} - -// --- a trunk's move, weighed -------------------------------------------------------------------------------------- - -/** What weighing a trunk's move reads of the forge. */ -export interface TrunkForge { - listOpenPulls(owner: string, repo: string): Promise; - branchHead(owner: string, repo: string, branch: string): Promise; - /** The rule the forge applies to a branch: which of the controller's statuses are kept there (issue 1301). Not given, - * or not read: both, as before. */ - appliedProtection?(owner: string, repo: string, branch: string): Promise<{ enable_status_check?: boolean; status_check_contexts?: string[] | null } | null>; -} - -/** What it writes with: the merge check's one writer (gate.ts), which decides in the head's own turn. */ -export interface OutOfDateWriter { - outOfDate(owner: string, repo: string, sha: string, - decide: (checks: Record) => CommitStatus[] | Promise, target?: string): Promise; -} - -/** - * Every trunk of one repository that an open pull request merges into, read; for one that moved since it was last read - * (`trunks`, by `owner/repo:branch`, which this keeps current), the merge check of every open pull request into it put - * out of date (ADR 0323 §4). A trunk first seen is recorded as it is and nothing is put out of date. Bounded however - * many are open: one listing of the open pull requests (paged), one read of each trunk, then per open pull request into - * a trunk that moved one read and at most two writes, one at a time. A trunk that could not be weighed whole is not - * recorded, so the next pass weighs it again. Answers whether the record changed. `moved` is told of each base that moved, for the siblings whose base it is. - */ -export async function weighTrunks(forge: TrunkForge, gates: OutOfDateWriter, trunks: Record, owner: string, - name: string, moved?: (base: string) => Promise): Promise { - let changed = false; - try { - const open = (await forge.listOpenPulls(owner, name)).filter((p) => p.state === "open" && !!p.head_sha && !!p.base); - for (const base of new Set(open.map((p) => String(p.base)))) { - const key = `${owner}/${name}:${base}`.toLowerCase(); - const head = await forge.branchHead(owner, name, base); - const before = trunks[key]; - if (before && sameCommit(before, head)) continue; - if (before) { - let put = 0; - const required = forge.appliedProtection - ? requiredOf(await forge.appliedProtection(owner, name, base).catch(() => undefined)) : undefined; - for (const pull of open.filter((p) => p.base === base)) { - // The trunk is read again in the head's own turn: a verdict judged against a head newer than the pass's first - // read — the trunk moved again meanwhile, and a check answered — is not put out of date. - const written = await gates.outOfDate(owner, name, String(pull.head_sha), - async (checks) => outOfDateStatuses(checks, await forge.branchHead(owner, name, base), required), pull.html_url || undefined); - if (written.length) put++; - } - console.log(`[gitea] ${owner}/${name}'s ${base} moved from ${twelve(before)} to ${twelve(head)}: ${put} open pull ` + - `request(s) into it put out of date, nothing run (novox/hq ADR 0323)`); - // A verdict that read a pull request into this trunk as a sibling is out of date by its move too (ADR 0382 §5). - if (moved) await moved(base); - } - trunks[key] = head; - changed = true; - } - } catch (err) { - console.error(`[gitea] ${owner}/${name}: whether a trunk moved could not be weighed, so it is weighed again on the next ` + - `pass — ${err instanceof Error ? err.message : err}`); - } - return changed; -} - -// --- the announcement of a head, shared by the watcher and the merge tool --------------------------------------- - -/** `pull.updated` for one open pull request's head: what the controller asks the build seat to check (novox/hq to-be - * 45 §9). The merge tool's ask is this same announcement (ADR 0323 §5). */ -export async function announcementOf(client: GiteaClient, owner: string, name: string, cloneURL: string | undefined, - pull: GiteaPull): Promise> { - const files = await client.listPullFiles(owner, name, pull.number); - const head = String(pull.head_sha); - // What the controller maps the change onto the mesh's module graph with (novox/hq ADR 0237 as amended): which - // changed directories hold a module at the head — the merge's rule (issue 278). Not said when it could not be read; the controller then reads the change as - // touching everything built from the repository. - let moduleDirs: string[] | null = null; - try { - if (!files.truncated) moduleDirs = await client.moduleDirsAt(owner, name, head, files.paths); - } catch (err) { - console.error(`[gitea] ${owner}/${name}#${pull.number}: what the head holds could not be read — ${err instanceof Error ? err.message : err}`); - } - // The base branch's head as the head is announced: the trunk commit a verdict no build judges is said against - // (novox/hq ADR 0323 §3). Not said when it cannot be read; that verdict then names none. - const baseSHA = pull.base ? await client.branchHead(owner, name, String(pull.base)).catch(() => undefined) : undefined; - return { - owner, - repo: name, - number: pull.number, - ...(baseSHA ? { base_sha: baseSHA } : {}), - title: pull.title, - body: pull.body, - base: pull.base, - head: pull.head, - head_sha: pull.head_sha, - clone_url: cloneURL, - html_url: pull.html_url, - paths: files.paths, - paths_truncated: files.truncated, - removed: files.removed, - ...(moduleDirs ? { module_dirs: moduleDirs, module_dirs_said: true } : {}), - }; -} diff --git a/modules/gitea/updated.ts b/modules/gitea/updated.ts new file mode 100644 index 000000000..8ed934da8 --- /dev/null +++ b/modules/gitea/updated.ts @@ -0,0 +1,42 @@ +// **A pull request's new head is said as the seat git's pull.updated** (novox/hq design 47 H §764; H-A-100): from the forge's +// event of an opened, pushed, reopened or retargeted pull request alone; nothing polls for heads and no merge check is +// asked or written (the merge-check machinery went with milestone H). + +import type { GiteaClient, GiteaPull } from "./client.js"; + +/** `pull.updated` for one open pull request's head: what the controller asks the build seat to check (novox/hq to-be + * 45 §9). The merge tool's ask is this same announcement (ADR 0323 §5). */ +export async function announcementOf(client: GiteaClient, owner: string, name: string, cloneURL: string | undefined, + pull: GiteaPull): Promise> { + const files = await client.listPullFiles(owner, name, pull.number); + const head = String(pull.head_sha); + // What the controller maps the change onto the mesh's module graph with (novox/hq ADR 0237 as amended): which + // changed directories hold a module at the head — the merge's rule (issue 278). Not said when it could not be read; the controller then reads the change as + // touching everything built from the repository. + let moduleDirs: string[] | null = null; + try { + if (!files.truncated) moduleDirs = await client.moduleDirsAt(owner, name, head, files.paths); + } catch (err) { + console.error(`[gitea] ${owner}/${name}#${pull.number}: what the head holds could not be read — ${err instanceof Error ? err.message : err}`); + } + // The base branch's head as the head is announced: the trunk commit a verdict no build judges is said against + // (novox/hq ADR 0323 §3). Not said when it cannot be read; that verdict then names none. + const baseSHA = pull.base ? await client.branchHead(owner, name, String(pull.base)).catch(() => undefined) : undefined; + return { + owner, + repo: name, + number: pull.number, + ...(baseSHA ? { base_sha: baseSHA } : {}), + title: pull.title, + body: pull.body, + base: pull.base, + head: pull.head, + head_sha: pull.head_sha, + clone_url: cloneURL, + html_url: pull.html_url, + paths: files.paths, + paths_truncated: files.truncated, + removed: files.removed, + ...(moduleDirs ? { module_dirs: moduleDirs, module_dirs_said: true } : {}), + }; +} diff --git a/modules/gitea/watch.ts b/modules/gitea/watch.ts index 7dc1f135e..dc0f97468 100644 --- a/modules/gitea/watch.ts +++ b/modules/gitea/watch.ts @@ -1,29 +1,24 @@ // What the forge's events ask of the announcer, and the one pass at a start (issue 1422, notes 10 and 11). Nothing polls the -// forge: a head or merge is announced when its event is heard, and a pass over every repository runs once when the module -// starts and again when the receiver is listening, so what happened while it was down is announced too. +// forge for heads: a pull request's new head is said as pull.updated when its event is heard, a merge or a close by its +// event and by the pass over every repository's merges at the start (design 47 H §764; H-A-100). // // Apart from index.ts, which has side effects at import, so these are tested with the announcers injected. -import type { GiteaPull, GiteaRepo } from "./client.js"; -import type { Announced } from "./pulls.js"; -import { headKey, headsToAnnounce } from "./pulls.js"; import type { ForgeEvent } from "./webhook.js"; export interface ActDeps { getRepo(owner: string, repo: string): Promise; getPull(owner: string, repo: string, number: number): Promise

; - announceHead(repo: R, pull: P): Promise; + announceUpdated(repo: R, pull: P): Promise; announceMerge(repo: R, pull: P): Promise; announceClosed(repo: R, pull: P): Promise; - siblingMoves(full: string, number: number): Promise; } const HEAD_EVENTS = ["forge.pull-request.opened", "forge.pull-request.synchronized", "forge.pull-request.reopened"]; /** - * What the forge's event asks of the announcer at once: a new head is announced, a merge, a close without a merge, and a - * pull request retargeted (an edit: the head is announced into the base it now has — the base is in the head's key, so an - * edit that changed nothing about the base announces nothing). + * What the forge's event asks of the announcer at once: a new head is said, a merge, a close without a merge, and a pull + * request retargeted (an edit: the head is said into the base it now has). */ export async function actOn(event: ForgeEvent, d: ActDeps): Promise { const b = event.body as { owner?: string; repo?: string; number?: number; merged?: boolean }; @@ -37,51 +32,8 @@ export async function actOn(event: ForgeEvent, d: ActDeps): Promise< const [repo, pull] = await Promise.all([d.getRepo(b.owner, b.repo), d.getPull(b.owner, b.repo, b.number)]); if (closed) { await d.announceClosed(repo, pull); - await d.siblingMoves(`${b.owner}/${b.repo}`, b.number); return; } - if (head && pull.state === "open") await d.announceHead(repo, pull); + if (head && pull.state === "open") await d.announceUpdated(repo, pull); if (merged) await d.announceMerge(repo, pull); } - -/** - * The one pass at a start, and its retry: run now; if it fails, say so and run again after `retryMs`, until one pass has - * ended. A retry of a failed pass, never a poll: after a pass has ended nothing is scheduled. - */ -export function startPass(run: () => Promise, opts: { retryMs: number; log?: (l: string) => void }): Promise { - const log = opts.log ?? ((l) => console.error(`[gitea] ${l}`)); - return run().catch((err) => { - log(`the pass over every repository failed, asked again in ${opts.retryMs / 1000} s — ${err instanceof Error ? err.message : err}`); - return new Promise((resolve) => setTimeout(() => resolve(startPass(run, opts)), opts.retryMs)); - }); -} - -export interface HeadDeps { - /** Whether the forge shows a merge check of the head: only an announced head has one. */ - alreadyChecked(pull: GiteaPull): Promise; - /** Announce it; true when it was this call that announced it (not an event before it). */ - announceHead(pull: GiteaPull): Promise; - /** Told of a head this pass announced that no event had. */ - missed(what: string): void; -} - -/** - * The heads of one repository's open pull requests a pass announces. A head is announced when it is new and fresh (moved in - * the last day); at the first pass after a start (`primed` false) one the forge shows a merge check of was announced - * already, and is only remembered. - */ -export async function announceNewHeads(full: string, open: GiteaPull[], heads: Announced, primed: boolean, dayAgo: string, d: HeadDeps): Promise { - for (const pull of headsToAnnounce(full, open, heads)) { - const key = headKey(full, pull.number, pull.base); - const fresh = primed || (!!pull.updated_at && pull.updated_at > dayAgo); - if (fresh && !primed && await d.alreadyChecked(pull)) { - heads[key] = String(pull.head_sha); - continue; - } - if (fresh) { - if (await d.announceHead(pull)) d.missed(`${key} at ${String(pull.head_sha).slice(0, 8)}`); - } else heads[key] = String(pull.head_sha); - } -} - -export type { GiteaRepo }; -- 2.54.0 From 15458aeb612f44f6be9c40af7b5e2da074078b52 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:58:00 +0100 Subject: [PATCH 28/43] FORGE-6: the seat git's whole test runs the forge module's bundle on a real bus MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A Go integration test builds the TypeScript bundle and plays its tool runner over stdin and stdout against a bus of its own and a fixture forge: a call stopped after two of three merges says one merge-called at the start; a failed required step refuses its head; two pull requests in two repositories merge and say one merge-called; a forge refusing the second merge says what landed and raises merge-partial, cleared by a later call. Builds: design 47 H §774 (forge module: tests 111, 114, 115), §734, §735, §736, §766, §767; H-A-61, H-A-87 (1) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/gitea/internal/seatgit/doc.go | 3 + .../seatgit/whole_integration_test.go | 483 ++++++++++++++++++ 2 files changed, 486 insertions(+) create mode 100644 modules/gitea/internal/seatgit/doc.go create mode 100644 modules/gitea/internal/seatgit/whole_integration_test.go diff --git a/modules/gitea/internal/seatgit/doc.go b/modules/gitea/internal/seatgit/doc.go new file mode 100644 index 000000000..d718a85ea --- /dev/null +++ b/modules/gitea/internal/seatgit/doc.go @@ -0,0 +1,3 @@ +// Package seatgit holds the whole test of the seat git as the forge module serves it (novox/hq design 47 H §774, H-A-87): +// the module's TypeScript bundle run against a bus of the test alone and a fixture forge. It has no code of its own. +package seatgit diff --git a/modules/gitea/internal/seatgit/whole_integration_test.go b/modules/gitea/internal/seatgit/whole_integration_test.go new file mode 100644 index 000000000..9c26d7a6d --- /dev/null +++ b/modules/gitea/internal/seatgit/whole_integration_test.go @@ -0,0 +1,483 @@ +//go:build integration + +package seatgit + +// The seat git whole, as the forge module serves it (novox/hq design 47 H §734 to §736, §766, §767, §774; acceptance tests +// 111, 114 and 115; H-A-61, H-A-87): the module's TypeScript bundle, built here and played over its real stdin and stdout +// as the node's tool runner plays it, its states key-value buckets of a nats-server of this test alone, its seat events +// published on that bus and read back from its stream, and the forge an httptest server. +// +// - At its start a call kept unsaid, stopped after the second of three merges, says one merge-called naming the two +// that landed, and is kept said (§766, test 111's stop case). +// - A runner's step-ended of a failed required step at a pull request's head and fingerprint is kept, and a merge call +// naming that head merges nothing and names the step (§735, test 114). +// - A call naming two pull requests in two repositories merges both, in order, and says one merge-called (§734, §736). +// - A forge that refuses the second merge gives one merge-called with the first member and `refused`, and merge-partial +// raised; a later call landing the refused member clears it (§767, test 115). + +import ( + "bufio" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "path/filepath" + "regexp" + "strings" + "sync" + "testing" + "time" + + "git.novox.be/novox/mesh-sdk/go/testbus" + "github.com/nats-io/nats.go" + "github.com/nats-io/nats.go/jetstream" +) + +const within = 30 * time.Second + +// pull is one pull request of the fixture forge. +type pull struct { + repo, base, head string + number int + merged, refuse bool + merge string +} + +// forge is the fixture forge: the pull requests by `/#` and every merge asked. +type forge struct { + mu sync.Mutex + pulls map[string]*pull + merges []string + paths []string +} + +func (f *forge) serve(w http.ResponseWriter, r *http.Request) { + f.mu.Lock() + defer f.mu.Unlock() + f.paths = append(f.paths, r.Method+" "+r.URL.Path) + m := regexp.MustCompile(`^/api/v1/repos/([^/]+/[^/]+)/pulls/(\d+)(/merge)?$`).FindStringSubmatch(r.URL.Path) + if m != nil { + p := f.pulls[m[1]+"#"+m[2]] + if p == nil { + http.Error(w, `{"message":"not found"}`, http.StatusNotFound) + return + } + if m[3] != "" && r.Method == http.MethodPost { + f.merges = append(f.merges, m[1]+"#"+m[2]) + if p.refuse { + http.Error(w, `{"message":"merge conflict"}`, http.StatusConflict) + return + } + p.merged, p.merge = true, strings.Repeat(fmt.Sprint(p.number%10), 40) + return + } + body := map[string]any{"number": p.number, "title": "t", "state": "open", "merged": p.merged, + "base": map[string]any{"ref": p.base}, "head": map[string]any{"ref": "feat", "sha": p.head}} + if p.merged { + body["state"], body["merge_commit_sha"] = "closed", p.merge + } + _ = json.NewEncoder(w).Encode(body) + return + } + if c := regexp.MustCompile(`^/api/v1/repos/[^/]+/[^/]+/git/commits/([0-9a-f]+)$`).FindStringSubmatch(r.URL.Path); c != nil { + _ = json.NewEncoder(w).Encode(map[string]any{"sha": c[1], "parents": []any{map[string]any{"sha": c[1]}}}) + return + } + http.Error(w, `{"message":"the fixture forge serves no `+r.URL.Path+`"}`, http.StatusNotFound) +} + +func (f *forge) merged() []string { + f.mu.Lock() + defer f.mu.Unlock() + return append([]string(nil), f.merges...) +} + +func (f *forge) asked(prefix string) bool { + f.mu.Lock() + defer f.mu.Unlock() + for _, p := range f.paths { + if strings.HasPrefix(p, prefix) { + return true + } + } + return false +} + +// runtime is the half of the conversation the tool runner plays for the bundle. +type runtime struct { + t *testing.T + in io.WriteCloser + js jetstream.JetStream + kvs map[string]jetstream.KeyValue + mu sync.Mutex + next int + wait map[string]chan json.RawMessage + // regs are what the seat mesh-catalog's registrations answers; subs the patterns the bundle subscribed. + regs []map[string]any + subs []string +} + +func (r *runtime) subscribed(pattern string) bool { + r.mu.Lock() + defer r.mu.Unlock() + for _, s := range r.subs { + if s == pattern { + return true + } + } + return false +} + +func (r *runtime) say(m map[string]any) { + m["jsonrpc"] = "2.0" + raw, _ := json.Marshal(m) + r.mu.Lock() + defer r.mu.Unlock() + _, _ = r.in.Write(append(raw, '\n')) +} + +func (r *runtime) read(out io.Reader) { + sc := bufio.NewScanner(out) + sc.Buffer(make([]byte, 1<<20), 1<<24) + for sc.Scan() { + var m struct { + ID json.RawMessage `json:"id"` + Method string `json:"method"` + Params json.RawMessage `json:"params"` + Result json.RawMessage `json:"result"` + } + if json.Unmarshal(sc.Bytes(), &m) != nil { + continue + } + if m.Method == "" { + var id string + _ = json.Unmarshal(m.ID, &id) + r.mu.Lock() + ch := r.wait[id] + delete(r.wait, id) + r.mu.Unlock() + if ch != nil { + ch <- m.Result + } + continue + } + go r.answer(m.ID, m.Method, m.Params) + } +} + +// answer is the tool runner's answer to one method the bundle asks. +func (r *runtime) answer(id json.RawMessage, method string, params json.RawMessage) { + var p struct { + State string `json:"state"` + Key string `json:"key"` + Value json.RawMessage `json:"value"` + Subject string `json:"subject"` + Pattern string `json:"pattern"` + Body json.RawMessage `json:"body"` + ID string `json:"id"` + } + _ = json.Unmarshal(params, &p) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + result, err := func() (any, error) { + kv := r.kvs[p.State] + switch method { + case "mesh/subscribe": + r.mu.Lock() + r.subs = append(r.subs, p.Pattern) + r.mu.Unlock() + return map[string]any{}, nil + case "mesh/publish", "mesh/state.watch", "mesh/seat.record": + return map[string]any{}, nil + case "mesh/capabilities": + return map[string]any{"words": []string{}}, nil + case "mesh/seat.publish": + // The seat's event, published on the bus under its id, as the runtime does. + _, err := r.js.Publish(ctx, p.Subject, p.Body, jetstream.WithMsgID(p.ID)) + return map[string]any{}, err + case "mesh/ask": + var a struct { + Key string `json:"key"` + } + _ = json.Unmarshal(params, &a) + switch a.Key { + case "seat:mesh-catalog.registrations": + return map[string]any{"registrations": r.regs}, nil + case "seat:mesh-hand-acts.hand-act": + return map[string]any{}, nil + } + return nil, fmt.Errorf("the runtime answers no ask %s in this test", a.Key) + case "mesh/state.get": + if kv == nil { + return nil, fmt.Errorf("the state %q is not declared", p.State) + } + e, err := kv.Get(ctx, p.Key) + if errors.Is(err, jetstream.ErrKeyNotFound) { + return nil, nil + } + if err != nil { + return nil, err + } + return map[string]any{"key": p.Key, "value": json.RawMessage(e.Value()), "revision": e.Revision()}, nil + case "mesh/state.put": + if kv == nil { + return nil, fmt.Errorf("the state %q is not declared", p.State) + } + rev, err := kv.Put(ctx, p.Key, p.Value) + return map[string]any{"revision": rev}, err + case "mesh/state.delete": + if kv == nil { + return nil, fmt.Errorf("the state %q is not declared", p.State) + } + return map[string]any{}, kv.Delete(ctx, p.Key) + case "mesh/state.keys": + if kv == nil { + return nil, fmt.Errorf("the state %q is not declared", p.State) + } + keys, err := kv.Keys(ctx) + if errors.Is(err, jetstream.ErrNoKeysFound) { + return []string{}, nil + } + return keys, err + case "mesh/state.info": + return map[string]any{"bytes": 0, "keys": 0, "max_bytes": 0}, nil + } + return nil, fmt.Errorf("the runtime offers no %s to this bundle", method) + }() + if id == nil { + return + } + if err != nil { + r.say(map[string]any{"id": id, "error": map[string]any{"code": -32000, "message": err.Error()}}) + return + } + r.say(map[string]any{"id": id, "result": result}) +} + +// ask sends the bundle one request and answers its result. +func (r *runtime) ask(method string, params map[string]any) json.RawMessage { + r.t.Helper() + r.mu.Lock() + r.next++ + id := fmt.Sprintf("rt-%d", r.next) + ch := make(chan json.RawMessage, 1) + r.wait[id] = ch + r.mu.Unlock() + r.say(map[string]any{"id": id, "method": method, "params": params}) + select { + case raw := <-ch: + return raw + case <-time.After(within): + r.t.Fatalf("%s was not answered in %s", method, within) + } + return nil +} + +// mergePull calls the seat's verb as a proved caller and answers its result. +func (r *runtime) mergePull(pulls ...map[string]any) map[string]any { + r.t.Helper() + raw := r.ask("tools/call", map[string]any{"name": "git.merge-pull", "_meta": map[string]any{"mesh/caller": "shanks.claude-code"}, + "arguments": map[string]any{"by": "lead@shanks", "pulls": pulls}}) + var res struct { + Content []struct { + Text string `json:"text"` + } `json:"content"` + } + var out map[string]any + if json.Unmarshal(raw, &res) != nil || len(res.Content) == 0 || json.Unmarshal([]byte(res.Content[0].Text), &out) != nil { + r.t.Fatalf("merge-pull answered %s", raw) + } + return out +} + +func until(t *testing.T, what string, ok func() bool) { + t.Helper() + deadline := time.Now().Add(within) + for !ok() { + if time.Now().After(deadline) { + t.Fatalf("within %s: %s did not happen", within, what) + } + select { + case <-time.After(20 * time.Millisecond): + } + } +} + +// said is every merge-called on the bus so far, by call. +func said(t *testing.T, js jetstream.JetStream) map[string]map[string]any { + t.Helper() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + out := map[string]map[string]any{} + s, err := js.Stream(ctx, "SEAT_git_merge-called") + if err != nil { + t.Fatal(err) + } + info, err := s.Info(ctx) + if err != nil { + t.Fatal(err) + } + for seq := info.State.FirstSeq; seq <= info.State.LastSeq && info.State.Msgs > 0; seq++ { + m, err := s.GetMsg(ctx, seq) + if err != nil { + continue + } + var body map[string]any + _ = json.Unmarshal(m.Data, &body) + out[strings.TrimPrefix(m.Subject, "mesh.seat.git.event.merge-called.")] = body + } + return out +} + +func numbers(v any) []float64 { + var out []float64 + list, _ := v.([]any) + for _, m := range list { + n, _ := m.(map[string]any)["number"].(float64) + out = append(out, n) + } + return out +} + +func TestTheSeatGitWholeOverARealBus(t *testing.T) { + dir, err := filepath.Abs(filepath.Join("..", "..")) + if err != nil { + t.Fatal(err) + } + if _, err := os.Stat(filepath.Join(dir, "node_modules", "@novox", "mesh-sdk")); err != nil { + t.Fatalf("the forge module's packages are not installed in %s (npm install first): %v", dir, err) + } + build := exec.Command("npm", "run", "build") + build.Dir = dir + if out, err := build.CombinedOutput(); err != nil { + t.Fatalf("the bundle does not build: %v\n%s", err, out) + } + + nc, err := nats.Connect(testbus.URL(t)) + if err != nil { + t.Fatal(err) + } + t.Cleanup(nc.Close) + js, _ := jetstream.New(nc) + ctx := context.Background() + // The seat's stream merge-called is kept on (the definition's keep); the template states as buckets. + if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "SEAT_git_merge-called", Subjects: []string{"mesh.seat.git.event.merge-called.*"}}); err != nil { + t.Fatal(err) + } + kvs := map[string]jetstream.KeyValue{} + for _, name := range []string{"bound", "announced", "findings", "first-views", "transfers", "idle", "failed-steps", "calls"} { + kv, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: "gitea_" + name}) + if err != nil { + t.Fatal(err) + } + kvs[name] = kv + } + A, B, X := strings.Repeat("a", 40), strings.Repeat("b", 40), strings.Repeat("e", 40) + // A call stopped after the second of three merges, kept and not said (§766). + stopped, _ := json.Marshal(map[string]any{"by": "lead@shanks", "account": "shanks.claude-code", "at": "2026-11-20T03:00:00Z", "said": false, + "members": []any{ + map[string]any{"repository": "novox/app", "number": 1, "branch": "main", "head": A, "state": "merged", "commit": strings.Repeat("1", 40)}, + map[string]any{"repository": "novox/app", "number": 2, "branch": "main", "head": A, "state": "merged", "commit": strings.Repeat("2", 40)}, + map[string]any{"repository": "novox/app", "number": 3, "branch": "main", "head": A, "state": "judged"}}}) + if _, err := kvs["calls"].Put(ctx, "call.00000000000000cc", stopped); err != nil { + t.Fatal(err) + } + + f := &forge{pulls: map[string]*pull{ + "novox/app#5": {repo: "novox/app", base: "main", head: A, number: 5}, + "novox/app#6": {repo: "novox/app", base: "main", head: X, number: 6}, + "novox/app#7": {repo: "novox/app", base: "main", head: A, number: 7}, + "novox/lib#9": {repo: "novox/lib", base: "main", head: B, number: 9, refuse: true}, + "novox/lib#10": {repo: "novox/lib", base: "main", head: B, number: 10}, + }} + srv := httptest.NewServer(http.HandlerFunc(f.serve)) + t.Cleanup(srv.Close) + + state := t.TempDir() + launch := `const sdk = await import("@novox/mesh-sdk/stdio"); await import("./dist/index.js"); await import("./dist/tools/index.js"); await sdk.serveRegisteredOverStdio("gitea");` + cmd := exec.Command("node", "--input-type=module", "-e", launch) + cmd.Dir = dir + cmd.Env = append(os.Environ(), "MESH_SERVED_MODULE=gitea", "MESH_MODULE=gitea", "MESH_NODE=test", "MESH_GITEA_URL="+srv.URL, + "GITEA_TOKEN=t", "MESH_GITEA_STATE_DIR="+state, "MESH_GITEA_HOOK_PORT=0", "MESH_GITEA_CONFIG_FILE="+filepath.Join(state, "config.json")) + in, _ := cmd.StdinPipe() + out, _ := cmd.StdoutPipe() + cmd.Stderr = testLog{t} + if err := cmd.Start(); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = in.Close(); _ = cmd.Process.Kill(); _ = cmd.Wait() }) + rt := &runtime{t: t, in: in, js: js, kvs: kvs, wait: map[string]chan json.RawMessage{}, regs: []map[string]any{ + {"module": "app", "origin": "novox/app", "branches": []string{"main"}}, + {"module": "lib", "origin": "novox/lib", "branches": []string{"main"}}}} + go rt.read(out) + + // At the start the stopped call says one merge-called naming the two that landed (§766). + until(t, "the stopped call's merge-called at the start", func() bool { return said(t, js)["00000000000000cc"] != nil }) + if got := numbers(said(t, js)["00000000000000cc"]["members"]); fmt.Sprint(got) != "[1 2]" { + t.Fatalf("the stopped call names %v, not the two that landed", got) + } + until(t, "the stopped call kept said", func() bool { + e, err := kvs["calls"].Get(ctx, "call.00000000000000cc") + return err == nil && strings.Contains(string(e.Value()), `"said":true`) + }) + // The registrations are read, and the heads with them, before a merge is judged against them. + until(t, "the watched heads read after the registrations", func() bool { return f.asked("GET /api/v1/repos/novox/app/branches") }) + + // A failed required step at a head's fingerprint refuses that head (§735, test 114), heard on the module's consumer. + until(t, "the runner's step-ended followed", func() bool { return rt.subscribed("mesh-pipeline-runner.step-ended.*.*.*") }) + rt.ask("mesh/event", map[string]any{"envelope": map[string]any{"key": "mesh-pipeline-runner.step-ended.app.main." + X, "node": "test", + "body": map[string]any{"v": 1, "pipeline": "main.4", "module": "app", "branch": "main", "commit": X, "fingerprint": "fp-x", + "at": "2026-11-20T03:00:00Z", "result": map[string]any{"step": "unit", "status": "required", "outcome": "failed"}}}}) + refused := rt.mergePull(map[string]any{"repository": "novox/app", "number": 6, "head": X}) + members, _ := refused["members"].([]any) + if len(members) != 1 || members[0].(map[string]any)["state"] != "refused" || + !strings.Contains(fmt.Sprint(members[0].(map[string]any)["why"]), "the unit step of app failed in run main.4") { + t.Fatalf("a head at a failed required step's fingerprint answered %v", refused) + } + if got := f.merged(); len(got) != 0 { + t.Fatalf("merged %v", got) + } + + // Two pull requests in two repositories merge, in order, and say one merge-called (§734, §736). + both := rt.mergePull(map[string]any{"repository": "novox/app", "number": 7, "head": A}, map[string]any{"repository": "novox/lib", "number": 10, "head": B}) + call, _ := both["call"].(string) + until(t, "the merge-called of the two", func() bool { return said(t, js)[call] != nil }) + if got := numbers(said(t, js)[call]["members"]); fmt.Sprint(got) != "[7 10]" || fmt.Sprint(f.merged()) != "[novox/app#7 novox/lib#10]" { + t.Fatalf("the call of two said %v, merged %v", got, f.merged()) + } + + // The forge refuses the second merge: merge-called with the first and refused, merge-partial raised (§767, test 115). + partial := rt.mergePull(map[string]any{"repository": "novox/app", "number": 5, "head": A}, map[string]any{"repository": "novox/lib", "number": 9, "head": B}) + pc, _ := partial["call"].(string) + until(t, "the partial call's merge-called", func() bool { return said(t, js)[pc] != nil }) + body := said(t, js)[pc] + if fmt.Sprint(numbers(body["members"])) != "[5]" || fmt.Sprint(numbers(body["refused"])) != "[9]" { + t.Fatalf("the partial call said %v", body) + } + key := "delivery." + pc + ".merge-partial" + if e, err := kvs["findings"].Get(ctx, key); err != nil || !strings.Contains(string(e.Value()), `"severity":"urgent"`) { + t.Fatalf("merge-partial %s is not raised: %v", key, err) + } + // A later call landing the refused member clears it. + f.mu.Lock() + f.pulls["novox/lib#9"].refuse = false + f.mu.Unlock() + rt.mergePull(map[string]any{"repository": "novox/lib", "number": 9, "head": B}) + until(t, "merge-partial cleared by the later call", func() bool { + _, err := kvs["findings"].Get(ctx, key) + return errors.Is(err, jetstream.ErrKeyNotFound) + }) +} + +type testLog struct{ t *testing.T } + +func (w testLog) Write(p []byte) (int, error) { + defer func() { _ = recover() }() + w.t.Logf("bundle: %s", strings.TrimRight(string(p), "\n")) + return len(p), nil +} -- 2.54.0 From ba0330c205e0652225317b2b836d0a175108a045 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 04:14:18 +0100 Subject: [PATCH 29/43] mesh-cli: nox head take calls gate_head_take; indexer_take_in is gone MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds: design 47 H §742, lead answer H-A-105 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-cli/cmd/mesh-cli/commands.go | 13 +++++++------ modules/mesh-cli/cmd/mesh-cli/main_test.go | 2 ++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/modules/mesh-cli/cmd/mesh-cli/commands.go b/modules/mesh-cli/cmd/mesh-cli/commands.go index 53fa35720..b9ebdb13d 100644 --- a/modules/mesh-cli/cmd/mesh-cli/commands.go +++ b/modules/mesh-cli/cmd/mesh-cli/commands.go @@ -387,15 +387,16 @@ var commands = []command{ {words: []string{"licence"}, instead: "anthropic-licence-manager.adopt takes an API key from a file on the manager's node; nox licence list|use|refresh|key"}, {words: []string{"module", "list"}, does: "every module the indexer holds", build: fixed("mesh-delivery-view.indexer_modules")}, {words: []string{"module", "show"}, usage: "", does: "what the indexer holds of one module", build: needArg("mesh-delivery-view.indexer_module", "module")}, - {words: []string{"module", "moved"}, usage: " --why ", does: "take a registered module in now", allowed: []string{"why"}, + {words: []string{"head", "take"}, usage: " --why ", does: "take a held head in by hand (design 47 H §742, H-A-105)", allowed: []string{"why"}, build: func(l line) (call, error) { - if len(l.pos) < 1 { - return call{}, usageErr("give the module") + if len(l.pos) != 2 { + return call{}, usageErr("give the repository and the branch") } - return call{"mesh-delivery-indexer.indexer_take_in", map[string]any{"module": l.pos[0], "why": l.flags["why"]}}, nil + return call{"mesh-delivery-gate.gate_head_take", map[string]any{"repository": l.pos[0], "branch": l.pos[1], "why": l.flags["why"]}}, nil }}, - {words: []string{"module"}, instead: "the indexer takes modules in from their repositories; nox module list|show|moved, and nothing forgets one by hand yet"}, - {words: []string{"register"}, instead: "no seat registers a module from another repository yet (novox/hq ADR 0501 §4); nox module moved takes a registered one in"}, + {words: []string{"head", "list"}, does: "every untaken head the gate holds", build: fixed("mesh-delivery-gate.gate_heads")}, + {words: []string{"module"}, instead: "the indexer takes modules in from their repositories; nox module list|show, nox head take for a held head, and nothing forgets one by hand yet"}, + {words: []string{"register"}, instead: "no seat registers a module from another repository yet (novox/hq ADR 0501 §4); nox head take takes a held head in"}, {words: []string{"account", "issue"}, usage: "", does: "mint one bus account the grants part wants, now", build: needArg("mesh-vault.issue-account", "user")}, // Issues and records. diff --git a/modules/mesh-cli/cmd/mesh-cli/main_test.go b/modules/mesh-cli/cmd/mesh-cli/main_test.go index 7939ec0f2..59e5664fc 100644 --- a/modules/mesh-cli/cmd/mesh-cli/main_test.go +++ b/modules/mesh-cli/cmd/mesh-cli/main_test.go @@ -88,6 +88,8 @@ func TestEachLineCallsItsOwner(t *testing.T) { args string }{ {"node show ace", "mesh-module-assigner.assignments", `{"node":"ace"}`}, + {"head take novox/x main --why moved", "mesh-delivery-gate.gate_head_take", `{"branch":"main","repository":"novox/x","why":"moved"}`}, + {"head list", "mesh-delivery-gate.gate_heads", `{}`}, {"node public-domain ace", "mesh-module-assigner.assignments", `{"node":"ace"}`}, {"node public-domain ace example.org --why moved", "mesh-module-assigner.propose", `{"changes":[{"field":"public-domain","node":"ace","op":"decide","value":"example.org"}],"why":"moved"}`}, {"plan ace", "mesh-delivery-view.assigner_wants", `{"node":"ace"}`}, -- 2.54.0 From c91ab3acfaae7879b4c9863269815e85d3ca41be Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 04:59:05 +0100 Subject: [PATCH 30/43] build modules: the checker seat's invoke, its client and its fixtures go; a run's input is asked of the seat mesh-wants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The kinds check and recheck stay in H1 (H-A-117). Builds: design 47 H §764 (the checker (H1)); lead answers H-A-57, H-A-117 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/mesh-build-go/announce.go | 2 +- .../mesh-build-go/asks_integration_test.go | 4 +-- .../cmd/mesh-build-go/asks_test.go | 2 +- .../cmd/mesh-build-go/compose_test.go | 7 ++-- .../mesh-build-go/cmd/mesh-build-go/module.go | 2 +- .../cmd/mesh-build-go/packages_shape_test.go | 2 +- .../mesh-build-go/cmd/mesh-build-go/run.go | 15 +++----- .../cmd/mesh-build-go/run_integration_test.go | 15 ++++---- .../cmd/mesh-build-go/run_test.go | 34 +++++++++++-------- modules/mesh-build-go/go.mod | 3 +- modules/mesh-build-go/module.json | 1 - .../cmd/mesh-build-node/announce.go | 2 +- .../cmd/mesh-build-node/asks_test.go | 2 +- 13 files changed, 45 insertions(+), 46 deletions(-) diff --git a/modules/mesh-build-go/cmd/mesh-build-go/announce.go b/modules/mesh-build-go/cmd/mesh-build-go/announce.go index e4145bbf7..605792692 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/announce.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/announce.go @@ -1,7 +1,7 @@ package main // What this module keeps on the bus, announced from its code (novox/hq ADR 0495 §2 §5; issue 1790): every state it -// keeps, each readable by others, since another module reads each today (the builder, the checker and the indexer read toolchains; the builder and the checker read steps; gitea reads keys). A state of its own that it +// keeps, each readable by others, since another module reads each today (the builder and the indexer read toolchains; the builder reads steps; gitea reads keys). A state of its own that it // did not name here is refused at the call (stdio.Keeps). import ( diff --git a/modules/mesh-build-go/cmd/mesh-build-go/asks_integration_test.go b/modules/mesh-build-go/cmd/mesh-build-go/asks_integration_test.go index b1b9bc13c..7c978b93f 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/asks_integration_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/asks_integration_test.go @@ -55,7 +55,7 @@ func TestTheModuleTakesADeliveryAskBeforeAnEarlierCheck(t *testing.T) { // A build rides main's subject, any other kind a named asker's (design 47 G §543). subject, _ := sdkstep.AskSubject(a.Language, a.Kind) if a.Kind != sdkstep.AskDelivery { - subject, _ = sdkstep.AskSubjectBy(a.Language, a.Kind, "mesh-delivery-checker") + subject, _ = sdkstep.AskSubjectBy(a.Language, a.Kind, "mesh-delivery-runner") } if _, err := js.Publish(ctx, subject, raw); err != nil { t.Fatal(err) @@ -87,7 +87,7 @@ func TestTheModuleTakesADeliveryAskBeforeAnEarlierCheck(t *testing.T) { ended := 0 states.mu.Lock() for _, a := range []sdkstep.StepAsk{first, check} { - asker := "mesh-delivery-checker" // a named ask's step is keyed under its asker, main's under its node (§481) + asker := "mesh-delivery-runner" // a named ask's step is keyed under its asker, main's under its node (§481) if a.Kind == sdkstep.AskDelivery { asker = "" } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/asks_test.go b/modules/mesh-build-go/cmd/mesh-build-go/asks_test.go index b8e3f6802..485f0b1df 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/asks_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/asks_test.go @@ -198,7 +198,7 @@ func TestACheckAsksDigestIsTheToolsAnswerInTheCall(t *testing.T) { } } ask := m.askSteps(steps) - a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskCheck, Language: sdkstep.AskGo, Asker: "mesh-delivery-checker", + a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskCheck, Language: sdkstep.AskGo, Asker: "mesh-delivery-runner", At: time.Now(), Check: &sdkstep.CheckJob{Fetch: sdkstep.Fetch{Repository: "novox/x", Commit: askCommit}, Levels: []string{sdkstep.LevelUnit}}} out, err := steprun.RunAsk(context.Background(), ask, a) if err != nil { diff --git a/modules/mesh-build-go/cmd/mesh-build-go/compose_test.go b/modules/mesh-build-go/cmd/mesh-build-go/compose_test.go index 20a5c522a..f2d708c2c 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/compose_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/compose_test.go @@ -166,14 +166,15 @@ func TestAComposeAskFailsLoudly(t *testing.T) { } // Design 47 G §615: the module reads the manifests a compose ask names from the catalog graph's entry, by the seat's verb -// entry, and invokes no tool of the delivery indexer. 1921 (B0): a run's input is asked of the checker seat's verb input, as -// the manifest invokes it, and no state of the checker's inputs is read (ADR 0596 §2). +// entry, and invokes no tool of the delivery indexer. 1921 (B0): a run's input is asked of the seat mesh-wants' verb input, as +// the manifest invokes it, and no state of the checker's inputs is read (ADR 0596 §2); the seat mesh-delivery-checker is no +// longer invoked (design 47 H §764, H-A-57). func TestTheModuleReadsTheCatalogEntries(t *testing.T) { m := readManifest(t) if !slices.Contains(m.Invokes, catalogclient.EntryVerb) || slices.ContainsFunc(m.Invokes, func(i string) bool { return strings.HasPrefix(i, "mesh-delivery-indexer") }) { t.Errorf("invokes = %v: want %s and no tool of the indexer", m.Invokes, catalogclient.EntryVerb) } - if !slices.Contains(m.Invokes, runInputVerb) || slices.Contains(m.Reads, "mesh-delivery-checker.inputs") { + if !slices.Contains(m.Invokes, runInputVerb) || slices.ContainsFunc(m.Invokes, func(i string) bool { return strings.HasPrefix(i, "seat:mesh-delivery-checker") }) || slices.Contains(m.Reads, "mesh-delivery-checker.inputs") { t.Errorf("invokes = %v, reads = %v: want %s invoked and no inputs state read", m.Invokes, m.Reads, runInputVerb) } } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/module.go b/modules/mesh-build-go/cmd/mesh-build-go/module.go index 439bb08bf..7e67a8b1b 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/module.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/module.go @@ -116,7 +116,7 @@ type Module struct { // request names each manifest it gives by digest with (compose.go, design 47 G §615); GoModCache, when set, is the Go module cache in place of Cache's (a test). ReadEntry func(fingerprint string) (meshcat.CatalogEntry, error) // AskInput asks a seat verb the module invokes (module.json) as the runtime calls it (stdio.Ask): the verb a run ask's - // input names, answering the input by its digest (meshdeliverycheckerclient.ReadInput). + // input names, answering the input by its digest (the seat mesh-wants' verb input). AskInput func(key string, body any) (json.RawMessage, error) // PlaceHead, when set, lays a run's head at a directory instead of fetching it (a test). PlaceHead func(ctx context.Context, h sdkstep.RunHead, dir string) error diff --git a/modules/mesh-build-go/cmd/mesh-build-go/packages_shape_test.go b/modules/mesh-build-go/cmd/mesh-build-go/packages_shape_test.go index 62e813fb2..80154656b 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/packages_shape_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/packages_shape_test.go @@ -63,7 +63,7 @@ func TestACheckAskReachesTheTestStepPastItsPackages(t *testing.T) { } } } - a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskCheck, Language: sdkstep.AskGo, Asker: "mesh-delivery-checker", + a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskCheck, Language: sdkstep.AskGo, Asker: "mesh-delivery-runner", At: time.Now(), Check: &sdkstep.CheckJob{Fetch: sdkstep.Fetch{Repository: "novox/x", Commit: askCommit}, Levels: []string{sdkstep.LevelUnit}}} out, err := steprun.RunAsk(context.Background(), m.askSteps(steps), a) got := "" diff --git a/modules/mesh-build-go/cmd/mesh-build-go/run.go b/modules/mesh-build-go/cmd/mesh-build-go/run.go index 24c9e50eb..d89976cf1 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/run.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/run.go @@ -19,7 +19,7 @@ import ( "path/filepath" "strings" - checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" + meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun" ) @@ -50,22 +50,21 @@ func (m *Module) run(ctx context.Context, fetch steprun.StepFunc, a sdkstep.Step } // inputOf is the input a run names, asked of the seat verb the ask names (module.json invokes it) and checked against -// its digest and size: bytes that are not the input named refuse the ask. A verb marked chunks (the seat mesh-catalog's +// its digest and size: bytes that are not the input named refuse the ask. A verb marked chunks (the seats mesh-catalog's and mesh-wants' // input, of any size) is asked by the plain seat call: the tool runner receives the chunks and hands this module the -// whole file in the member `file` (design 47 G A-237, A-249); another seat's input answers its bytes in `input`. +// whole file in the member `file` (design 47 G A-237, A-249). func (m *Module) inputOf(in sdkstep.RunInput) ([]byte, error) { if m.AskInput == nil { return nil, errors.New("this module asks no seat verb for a run's input") } // A verb that does not answer fails the step; an answer that is not the input named refuses the ask. - raw, err := m.AskInput(in.Verb, checker.InputInput{Digest: in.Digest}) + raw, err := m.AskInput(in.Verb, meshwants.InputInput{Digest: in.Digest}) if err != nil { return nil, fmt.Errorf("asking %s for the input %s: %w", in.Verb, in.Digest, err) } var a struct { Digest string `json:"digest"` File string `json:"file"` - Input string `json:"input"` } if err := json.Unmarshal(raw, &a); err != nil { return nil, steprun.Refuse("%s answered no input for %s: %v", in.Verb, in.Digest, err) @@ -73,11 +72,7 @@ func (m *Module) inputOf(in sdkstep.RunInput) ([]byte, error) { if a.Digest != in.Digest { return nil, steprun.Refuse("%s answered the input %q, asked %s", in.Verb, a.Digest, in.Digest) } - text := a.File - if text == "" { - text = a.Input - } - body, err := base64.StdEncoding.DecodeString(text) + body, err := base64.StdEncoding.DecodeString(a.File) if err != nil { return nil, steprun.Refuse("%s answered the input %s in no base64: %v", in.Verb, in.Digest, err) } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/run_integration_test.go b/modules/mesh-build-go/cmd/mesh-build-go/run_integration_test.go index e09f87c49..369d93599 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/run_integration_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/run_integration_test.go @@ -16,8 +16,7 @@ import ( "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" - checkerclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" + meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun" "git.novox.be/novox/mesh-sdk/go/testbus" @@ -25,7 +24,7 @@ import ( // 1921/C3: a run ask put on the Go build seat's check stream is pulled by the module started whole on the real bus and // answered on its step key .: the program, run shut off over its heads and its input (asked by digest of the -// seat verb the ask names, which a fake checker answers on the bus as the checker's verb input does), answers its last +// seat verb the ask names, which a fake assigner answers on the bus as the seat's verb input does), answers its last // line and exit; a program cannot write under its heads, and one // that dials the machine's network is shut off, and its key ends failed, the answer kept (design 47 G A-290). func TestARunAskIsAnsweredOnTheBusAndItsProgramIsShutOff(t *testing.T) { @@ -63,13 +62,13 @@ func TestARunAskIsAnsweredOnTheBusAndItsProgramIsShutOff(t *testing.T) { } m.PlaceHead = func(_ context.Context, h sdkstep.RunHead, dir string) error { return os.MkdirAll(dir, 0o700) } body := `{"v":1}` - // The checker's verb input on the bus, and the module asking it as the runtime calls a seat verb it invokes. + // The wants seat's verb input on the bus, and the module asking it as the runtime calls a seat verb it invokes. asked := make(chan string, 4) - sub, err := nc.Subscribe("mesh.seat.mesh-delivery-checker.tool.input", func(msg *nats.Msg) { - var ask checker.InputInput + sub, err := nc.Subscribe("mesh.seat.mesh-wants.tool.input", func(msg *nats.Msg) { + var ask meshwants.InputInput _ = json.Unmarshal(msg.Data, &ask) asked <- ask.Digest - reply, _ := json.Marshal(map[string]any{"result": checkerclient.InputAnswerOf([]byte(body))}) + reply, _ := json.Marshal(map[string]any{"result": inputAnswerOf([]byte(body))}) _ = msg.Respond(reply) }) if err != nil { @@ -81,7 +80,7 @@ func TestARunAskIsAnsweredOnTheBusAndItsProgramIsShutOff(t *testing.T) { return nil, errors.New("asked " + key) } data, _ := json.Marshal(b) - msg, err := nc.Request("mesh.seat.mesh-delivery-checker.tool.input", data, 5*time.Second) + msg, err := nc.Request("mesh.seat.mesh-wants.tool.input", data, 5*time.Second) if err != nil { return nil, err } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/run_test.go b/modules/mesh-build-go/cmd/mesh-build-go/run_test.go index 599c796c8..c88ca5284 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/run_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/run_test.go @@ -15,8 +15,7 @@ import ( "testing" "time" - checkerclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" + meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun" @@ -25,17 +24,24 @@ import ( const runCommit = "5555555555555555555555555555555555555555" -// runInputVerb is the seat verb a run ask's input is served on, as the checker names it in the ask. -const runInputVerb = checkerclient.InputVerb +// runInputVerb is the seat verb a run ask's input is served on, as the ask names it. +const runInputVerb = "seat:mesh-wants.input" -// answersInput is a seat verb serving body by its digest, as the checker's verb input answers (sdkstep.RunInputAnswer). +// inputAnswerOf is the seat mesh-wants's answer of the input body as the tool runner hands it over: the digest and +// the whole file. +func inputAnswerOf(body []byte) map[string]any { + sum := sha256.Sum256(body) + return map[string]any{"digest": hex.EncodeToString(sum[:]), "chunks": 1, "file": base64.StdEncoding.EncodeToString(body)} +} + +// answersInput is a seat verb serving body by its digest, as the seat mesh-wants' verb input answers. func answersInput(body string) func(key string, b any) (json.RawMessage, error) { return func(key string, b any) (json.RawMessage, error) { - ask, _ := b.(checker.InputInput) + ask, _ := b.(meshwants.InputInput) if sum := sha256.Sum256([]byte(body)); key != runInputVerb || ask.Digest != hex.EncodeToString(sum[:]) { - return nil, fmt.Errorf("the checker keeps no input %s", ask.Digest) + return nil, fmt.Errorf("the assigner keeps no input %s", ask.Digest) } - return json.Marshal(checkerclient.InputAnswerOf([]byte(body))) + return json.Marshal(inputAnswerOf([]byte(body))) } } @@ -45,7 +51,7 @@ func runAskOf(t *testing.T, body string) sdkstep.StepAsk { if err != nil { t.Fatal(err) } - return sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskCheck, Language: sdkstep.AskGo, Asker: "mesh-delivery-checker", At: time.Now(), + return sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskCheck, Language: sdkstep.AskGo, Asker: "mesh-delivery-runner", At: time.Now(), Run: &sdkstep.RunJob{Program: sdkstep.RunProgram{Repository: "novox/mesh-delivery", Commit: runCommit, Dir: ".", Package: "./cmd/prog"}, Heads: []sdkstep.RunHead{{Repository: "novox/mesh-sdk", Commit: runCommit}, {Repository: "novox/mesh-host", Commit: runCommit}}, Input: in, Args: []string{"-seats"}}} @@ -112,7 +118,7 @@ func TestAKeptProgramIsRunOverItsHeadsAndInput(t *testing.T) { m.Inside, m.Run = true, StepRunner(t.TempDir()) // An input other than the one named is refused, naming it; one the verb does not answer fails the step, saying why. m.AskInput = func(_ string, b any) (json.RawMessage, error) { - return json.Marshal(checker.InputAnswer{Digest: b.(checker.InputInput).Digest, Input: base64.StdEncoding.EncodeToString([]byte(`{"other":1}`))}) + return json.Marshal(map[string]any{"digest": b.(meshwants.InputInput).Digest, "chunks": 1, "file": base64.StdEncoding.EncodeToString([]byte(`{"other":1}`))}) } if _, err := m.run(context.Background(), fetch, a); err == nil || !strings.Contains(err.Error(), "is not the one the ask names") || !errors.As(err, new(*steprun.Refusal)) { t.Errorf("a wrong input: %v", err) @@ -182,22 +188,22 @@ func TestARunsInputIsAskedOfTheVerbTheAskNames(t *testing.T) { // answersInputOn is answersInput for the seat verb wanted. func answersInputOn(wanted, body string) func(key string, b any) (json.RawMessage, error) { return func(key string, b any) (json.RawMessage, error) { - ask, _ := b.(checker.InputInput) + ask, _ := b.(meshwants.InputInput) if sum := sha256.Sum256([]byte(body)); key != wanted || ask.Digest != hex.EncodeToString(sum[:]) { return nil, fmt.Errorf("%s keeps no input %s", key, ask.Digest) } - return json.Marshal(checkerclient.InputAnswerOf([]byte(body))) + return json.Marshal(inputAnswerOf([]byte(body))) } } -// Design 47 G A-237, A-249: the input of a run is of any size: the mesh-catalog seat's verb input, marked chunks, is asked by the +// Design 47 G A-237, A-249: the input of a run is of any size: the mesh-wants seat's verb input, marked chunks, is asked by the // plain seat call and answers {digest, chunks, file}, the file whole as the tool runner received it; the bytes are checked. func TestARunsInputOfAnySizeIsTakenFromTheChunkedVerb(t *testing.T) { body := strings.Repeat("0123456789", 300000) // 3 MB sum := sha256.Sum256([]byte(body)) in := sdkstep.RunInput{Verb: "seat:mesh-catalog.input", Digest: hex.EncodeToString(sum[:]), Size: len(body)} m := &Module{AskInput: func(key string, b any) (json.RawMessage, error) { - if ask, _ := b.(checker.InputInput); key != in.Verb || ask.Digest != in.Digest { + if ask, _ := b.(meshwants.InputInput); key != in.Verb || ask.Digest != in.Digest { return nil, fmt.Errorf("%s keeps no input %v", key, b) } return json.Marshal(map[string]any{"digest": in.Digest, "chunks": 6, "file": base64.StdEncoding.EncodeToString([]byte(body))}) diff --git a/modules/mesh-build-go/go.mod b/modules/mesh-build-go/go.mod index c1a5aeead..51cca3973 100644 --- a/modules/mesh-build-go/go.mod +++ b/modules/mesh-build-go/go.mod @@ -9,10 +9,9 @@ require ( git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/containerimageregistryclient 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/meshdeliverycheckerclient v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry 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/meshdeliverychecker v0.0.0-00010101000000-000000000000 + git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000 diff --git a/modules/mesh-build-go/module.json b/modules/mesh-build-go/module.json index e73f26bb6..48acd5e4f 100644 --- a/modules/mesh-build-go/module.json +++ b/modules/mesh-build-go/module.json @@ -43,7 +43,6 @@ "seat:oci-artifact-registry.publish-part", "seat:go-package-registry.publish", "seat:go-package-registry.publish-part", - "seat:mesh-delivery-checker.input", "seat:mesh-catalog.input", "seat:mesh-wants.input", "seat:mesh-module-assigner.assignments", diff --git a/modules/mesh-build-node/cmd/mesh-build-node/announce.go b/modules/mesh-build-node/cmd/mesh-build-node/announce.go index 642d81767..8342594ac 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/announce.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/announce.go @@ -1,7 +1,7 @@ package main // What this module keeps on the bus, announced from its code (novox/hq ADR 0495 §2 §5; issue 1790): every state it -// keeps, each readable by others, since another module reads each today (the builder, the checker and the indexer read toolchains; the builder and the checker read steps; gitea reads keys). A state of its own that it +// keeps, each readable by others, since another module reads each today (the builder and the indexer read toolchains; the builder reads steps; gitea reads keys). A state of its own that it // did not name here is refused at the call (stdio.Keeps). import ( diff --git a/modules/mesh-build-node/cmd/mesh-build-node/asks_test.go b/modules/mesh-build-node/cmd/mesh-build-node/asks_test.go index 8bafad60c..f0794b26f 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/asks_test.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/asks_test.go @@ -32,7 +32,7 @@ func fakeSteps(calls *[]string) []StepTool { } func nodeAsk(kind string) sdkstep.StepAsk { - a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: kind, Language: sdkstep.AskNode, Asker: "mesh-delivery-checker", At: time.Now()} + a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: kind, Language: sdkstep.AskNode, Asker: "mesh-delivery-runner", At: time.Now()} fetch := sdkstep.Fetch{Repository: "novox/mesh-catalog", Commit: askCommit} if kind == sdkstep.AskDelivery { a.Asker = "mesh-delivery-builder" -- 2.54.0 From a6a6135f74d065a5ab663867fc7c7fe4f6346b51 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 05:52:00 +0100 Subject: [PATCH 31/43] gate: drop the require of the checker's seat contract, which is gone MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review round 1, Must 1: commit 11a22fdf removed the checker's seat package; the require left the gate unbuildable against the H1 heads. Builds: design 47 H §764 (the checker whole goes); H-A-57 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-delivery-gate/go.mod | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/mesh-delivery-gate/go.mod b/modules/mesh-delivery-gate/go.mod index b7c2633af..1cfa2af70 100644 --- a/modules/mesh-delivery-gate/go.mod +++ b/modules/mesh-delivery-gate/go.mod @@ -44,7 +44,6 @@ require ( git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-assigner/asks v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds v0.0.0-00010101000000-000000000000 // indirect -- 2.54.0 From 401289aface56420151f2a6441824e4a3b999db7 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 05:52:52 +0100 Subject: [PATCH 32/43] mesh-conditions: the hold reads a want's fingerprint alone Review round 1, Must 2: the want's composed digest left mesh-sdk's motion.Want (H-A-78); the hold read it and no longer built. Builds: design 47 H H-A-78 (1) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-conditions/cmd/mesh-conditions/hold.go | 2 +- .../mesh-conditions/cmd/mesh-conditions/hold_test.go | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/modules/mesh-conditions/cmd/mesh-conditions/hold.go b/modules/mesh-conditions/cmd/mesh-conditions/hold.go index cbf90c8e5..9b99add42 100644 --- a/modules/mesh-conditions/cmd/mesh-conditions/hold.go +++ b/modules/mesh-conditions/cmd/mesh-conditions/hold.go @@ -321,5 +321,5 @@ func decodeWant(value []byte) (motion.Want, error) { if err := json.Unmarshal(value, &w); err != nil { return motion.Want{}, err } - return motion.Want{FP: w.Fp, Composed: w.Composed, Removed: w.Removed}, nil + return motion.Want{FP: w.Fp, Removed: w.Removed}, nil } diff --git a/modules/mesh-conditions/cmd/mesh-conditions/hold_test.go b/modules/mesh-conditions/cmd/mesh-conditions/hold_test.go index 6850411c6..34c17cc39 100644 --- a/modules/mesh-conditions/cmd/mesh-conditions/hold_test.go +++ b/modules/mesh-conditions/cmd/mesh-conditions/hold_test.go @@ -34,8 +34,9 @@ const ( // failed delivery raises what stands at once; another module's and the machine's own are raised at once; compare lists a // held key as held. func TestADeliveryHoldsItsOwnFindingsUntilItEnds(t *testing.T) { - want := func(fp, composed string) []byte { - raw, _ := json.Marshal(meshwants.WantsByWantNodeModuleValue{V: 1, Fp: fp, Composed: composed}) + // A want names the fingerprint alone (design 47 H, H-A-78): a have at it is the want applied. + want := func(fp string) []byte { + raw, _ := json.Marshal(meshwants.WantsByWantNodeModuleValue{V: 1, Fp: fp}) return raw } have := func(fp, composed string, phase, h string) haveSpec { return haveSpec{fp, composed, phase, h} } @@ -55,10 +56,10 @@ func TestADeliveryHoldsItsOwnFindingsUntilItEnds(t *testing.T) { said: "raised module.mesh-controller.novox.unhealthy", line: "as the delivery of version 0d1e2f3a4b5c"}, } { r := newRig() - _ = r.k.Want("want.novox.mesh-controller", want(ctrlRunning, ctrlComposed), false) + _ = r.k.Want("want.novox.mesh-controller", want(ctrlRunning), false) r.engineHave("novox.mesh-controller", have(ctrlRunning, ctrlComposed, motion.PhaseRunning, engineclient.HealthOK)) // The delivery starts: the assigner wants the next fingerprint; the node applies it. - _ = r.k.Want("want.novox.mesh-controller", want(ctrlNext, ctrlNextComp), false) + _ = r.k.Want("want.novox.mesh-controller", want(ctrlNext), false) r.engineHave("novox.mesh-controller", have(ctrlNext, ctrlNextComp, motion.PhaseApplying, engineclient.HealthUnknown)) own, ownRaw := finding(t, watchman, cc.ScopeModule, "mesh-controller.novox", "unhealthy", "health", cc.Warning, r.now) other, otherRaw := finding(t, watchman, cc.ScopeModule, "postgres.novox", "unhealthy", "health", cc.Warning, r.now) @@ -109,7 +110,7 @@ func TestADeliveryHoldsItsOwnFindingsUntilItEnds(t *testing.T) { } r.k.Assignments("ace.mesh-delivery-assigner", placed["ace.mesh-delivery-assigner"], false) r.k.Assignments("ace.node", placed["ace.node"], false) - _ = r.k.Want("want.ace.mesh-delivery-assigner", want(ctrlNext, ctrlNextComp), false) + _ = r.k.Want("want.ace.mesh-delivery-assigner", want(ctrlNext), false) r.engineHave("ace.mesh-delivery-assigner", have(ctrlRunning, ctrlComposed, motion.PhaseRunning, engineclient.HealthOK)) seatKey, seatRaw := finding(t, selfCheck, cc.ScopeSeat, "mesh-module-assigner.ace", "holder-silent", "D3", cc.Warning, r.now) _ = r.k.Finding(selfCheck, seatKey, seatRaw, false) -- 2.54.0 From c99ad7763361d3a17e1fce9e00dfbc77f46137d4 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 05:54:54 +0100 Subject: [PATCH 33/43] mesh-cli: nox try and nox groups go with the checker and the groups MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review round 1, Must 4: both called verbs no part serves. Builds: design 47 H §764 (the checker whole; the gate's verb groups) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-cli/cmd/mesh-cli/commands.go | 12 ------------ modules/mesh-cli/cmd/mesh-cli/main_test.go | 4 +++- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/modules/mesh-cli/cmd/mesh-cli/commands.go b/modules/mesh-cli/cmd/mesh-cli/commands.go index b9ebdb13d..968bcbc6e 100644 --- a/modules/mesh-cli/cmd/mesh-cli/commands.go +++ b/modules/mesh-cli/cmd/mesh-cli/commands.go @@ -166,8 +166,6 @@ var commands = []command{ return call{"mesh-delivery.deliveries", pick(l, "state", "repository", "group", "all")}, nil }}, {words: []string{"delivery"}, usage: "", does: "one delivery or group whole", build: needArg("mesh-delivery.show", "id")}, - {words: []string{"groups"}, usage: "[--all]", does: "every delivery group", allowed: []string{"all"}, - build: func(l line) (call, error) { return call{"mesh-delivery.groups", pick(l, "all")}, nil }}, {words: []string{"stalled"}, does: "every delivery held past its state's bound", build: fixed("mesh-delivery.stalled")}, {words: []string{"checks"}, usage: " [--number n | --commit c]", does: "what the mesh's checks said of a head", allowed: []string{"number", "commit"}, build: func(l line) (call, error) { @@ -178,16 +176,6 @@ var commands = []command{ a["repository"] = l.pos[0] return call{"mesh-delivery.checks", a}, nil }}, - {words: []string{"try"}, usage: " [--level l] [--trunk t] [--tests t]", - does: "try a pushed head before its pull request is opened", allowed: []string{"level", "trunk", "tests"}, - build: func(l line) (call, error) { - if len(l.pos) != 2 { - return call{}, usageErr("give the repository and the head") - } - a := pick(l, "level", "trunk", "tests") - a["repository"], a["head"] = l.pos[0], l.pos[1] - return call{"mesh-delivery-checker.try", a}, nil - }}, {words: []string{"builds"}, usage: "[--since ]", does: "every build that ended since (the last day by default)", allowed: []string{"since"}, build: func(l line) (call, error) { since := l.flags["since"] diff --git a/modules/mesh-cli/cmd/mesh-cli/main_test.go b/modules/mesh-cli/cmd/mesh-cli/main_test.go index 59e5664fc..155f7ec87 100644 --- a/modules/mesh-cli/cmd/mesh-cli/main_test.go +++ b/modules/mesh-cli/cmd/mesh-cli/main_test.go @@ -103,7 +103,6 @@ func TestEachLineCallsItsOwner(t *testing.T) { {"deliveries --state merged --json", "mesh-delivery.deliveries", `{"state":"merged"}`}, {"delivery novox/mesh-sdk@dff0ce2abb21", "mesh-delivery.show", `{"id":"novox/mesh-sdk@dff0ce2abb21"}`}, {"checks novox/mesh-catalog --number 591", "mesh-delivery.checks", `{"number":"591","repository":"novox/mesh-catalog"}`}, - {"try novox/mesh-catalog abc123 --level unit", "mesh-delivery-checker.try", `{"head":"abc123","level":"unit","repository":"novox/mesh-catalog"}`}, {"builds", "mesh-delivery-builder.builder_builds_since", `{"since":"2026-11-01T10:00:00Z"}`}, {"upgrade nats --policy record --why bus", "mesh-wants.upgrade", `{"module":"nats","policy":"record","why":"bus"}`}, {"bus", "mesh-wants.bus", `{}`}, @@ -177,6 +176,9 @@ func TestALineWithNoOwnerOrNotReadCallsNothingAndSaysWhy(t *testing.T) { {"healers --days soon", 2, "takes a number"}, {"config propose --pull novox/x#1", 2, "--pull is not a flag"}, {"frobnicate", 2, "is not a command"}, + // The checker and the groups go whole (design 47 H §764): nox calls neither. + {"try novox/mesh-catalog abc123 --level unit", 2, "is not a command"}, + {"groups", 2, "is not a command"}, } { got := console(t, nil) args := strings.Fields(c.line) -- 2.54.0 From 5b06dd7e43ac692455e5c984f138ae40681b0282 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 05:57:16 +0100 Subject: [PATCH 34/43] messenger: the operator channel's holder keeps no held-back asks and serves no ask-again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review round 1, Must 5: the seat lost held-back and ask-again (mesh-contracts 9db2e1d); the holder still offered held-back asks of before, refused choices on them, listed them in asks and served ask-again. Builds: design 47 H §764 (The operator channel's limit); H-A-57 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/messenger/cmd/messenger/asks.go | 27 +-- modules/messenger/cmd/messenger/holdback.go | 177 ------------------ .../messenger/cmd/messenger/holdback_test.go | 63 ------- modules/messenger/cmd/messenger/holder.go | 1 - modules/messenger/cmd/messenger/main.go | 11 +- .../messenger/cmd/messenger/manifest_test.go | 2 +- modules/messenger/module.json | 6 +- 7 files changed, 12 insertions(+), 275 deletions(-) delete mode 100644 modules/messenger/cmd/messenger/holdback.go delete mode 100644 modules/messenger/cmd/messenger/holdback_test.go diff --git a/modules/messenger/cmd/messenger/asks.go b/modules/messenger/cmd/messenger/asks.go index 729ee7af9..8045547f3 100644 --- a/modules/messenger/cmd/messenger/asks.go +++ b/modules/messenger/cmd/messenger/asks.go @@ -46,6 +46,12 @@ const StateOpen = "open" // expiry, and offered the moment a channel can carry it (novox/hq ADR 0397, issue 926). It counts as open. const StateHeld = "held" +// liveState is whether an ask has not ended: offered, or held for want of a channel. The channel holds no ask back behind +// its asker's newer ones (design 47 H §764, H-A-57). +func liveState(state string) bool { + return state == StateOpen || state == StateHeld +} + // Ticket is one option's one-time ticket on one channel kind. type Ticket struct { Option string `json:"option"` @@ -65,9 +71,6 @@ type AskRecord struct { Warrant *oc.Warrant `json:"warrant,omitempty"` // Told: the warrant, or the end, was published to the asker. Told bool `json:"told,omitempty"` - // HeldBackSince is, while the ask is held back, since when: kept open behind newer asks of its asker, its tickets - // withdrawn, offered again when one of its asker's offered asks ends (novox/hq ADR 0666 §12). - HeldBackSince time.Time `json:"held-back-since,omitempty"` // HeldSince and HeldWhy say, while the ask is held, since when and why no channel can carry it: per kind, the // reason from the judgement that judges an answer. The asker reads them in this record, as it reads // the state (the asks protocol has no word for a held ask on `decided`, which means an ask ended). @@ -564,7 +567,7 @@ func (h *Holder) TakeAsk(verb, asker string, body json.RawMessage) error { return nil } // Every ask is offered when it is raised: the channel keeps no count of an asker's open asks and holds none back for room - // (design 47 G A-424). An ask with the same `about` replaces the open one, a held-back one of before too. + // (design 47 G A-424; H §764). An ask with the same `about` replaces the open one. replaced := "" h.mu.Lock() for k, e := range h.asks { @@ -963,9 +966,7 @@ func (h *Holder) closeAsk(key string, outcome string, words string, w *oc.Warran return ErrChanged } now := h.Now() - freed := rec.State == StateOpen || rec.State == StateHeld // a slot of the asker's offered asks, unless a newer ask takes it rec.State = string(outcome) - rec.HeldBackSince = time.Time{} rec.HeldWhy = "" rec.Closed = now if t, ok := rec.Tickets[used]; ok { @@ -989,9 +990,6 @@ func (h *Holder) closeAsk(key string, outcome string, words string, w *oc.Warran rev = newRev } h.keepAsk(key, rec, rev) - if freed && outcome != occ.OutcomeReplaced { - defer h.offerHeldBack(rec.Asker) - } h.sayClosed(key, rec, outcome, words) if rec.Asker == SelfAsker { // The router's own ask: it does what was decided, and tells nobody else (selfact.go). @@ -1261,8 +1259,6 @@ func (h *Holder) judge(kind string, c oc.Choice, key string, root func(KindInfo) return "chosen", nil } switch { - case rec.State == StateHeldBack: - return h.refuseChoice(kind, c, "the question waits behind newer questions of its asker, and is offered again when one of them ends") case !issued: return h.refuseChoice(kind, c, "it belongs to no ask this router holds") case t.Kind != kind: @@ -1454,10 +1450,6 @@ type AskView struct { // HeldSince and HeldWhy: while the ask is held, since when and why no channel can carry it yet (ADR 0397 §4). HeldSince string `json:"held_since,omitempty"` HeldWhy string `json:"held_why,omitempty"` - // HeldBackSince and Behind: while the ask is held back (ADR 0666 §12), since when and the keys of the newer asks of its - // asker it waits behind, in the names of the seat operator-channel's asks answer. - HeldBackSince string `json:"held-since,omitempty"` - Behind []string `json:"behind,omitempty"` } // AsksNow is every ask, open first, newest first. @@ -1472,9 +1464,6 @@ func (h *Holder) AsksNow() []AskView { if e.rec.State == StateHeld { v.HeldSince, v.HeldWhy = e.rec.HeldSince.UTC().Format(time.RFC3339), e.rec.HeldWhy } - if e.rec.State == StateHeldBack { - v.HeldBackSince, v.Behind = e.rec.HeldBackSince.UTC().Format(time.RFC3339), h.behindLocked(k, e.rec) - } kinds := map[string]bool{} for _, t := range e.rec.Tickets { kinds[t.Kind] = true @@ -1554,7 +1543,7 @@ func (h *Holder) sayClosed(key string, rec AskRecord, outcome string, words stri } } -// AsksQuery narrows what the tool `operator-channel.asks` answers (issue 595): Key one ask, State open (open, held and held-back) or +// AsksQuery narrows what the tool `operator-channel.asks` answers (issue 595): Key one ask, State open (open and held) or // ended (all takes both), Since the asks opened at or after that time, Bound the most bytes of the answer's asks. With no // argument at all the query is the open asks within DefaultAsksBound (issue 1974), and the answer says so. type AsksQuery struct { diff --git a/modules/messenger/cmd/messenger/holdback.go b/modules/messenger/cmd/messenger/holdback.go deleted file mode 100644 index d08d22b6c..000000000 --- a/modules/messenger/cmd/messenger/holdback.go +++ /dev/null @@ -1,177 +0,0 @@ -package main - -import ( - "fmt" - oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" - "sort" - "time" -) - -// An ask held back (novox/hq ADR 0666 §12, design 46 § 6) was one kept open behind newer asks of its asker, offered again when -// one of them ended. The channel holds none back any more (design 47 G A-424): every ask is offered when it is raised, and one -// held back before the change is offered at the router's start (offerAllHeldBack) and by ask-again. `held` is another state, an -// ask no ready channel can carry (ADR 0397). - -// StateHeldBack is an ask kept open behind newer asks of its asker. -const StateHeldBack = "held-back" - -// liveState is whether an ask has not ended: offered, held for want of a channel, or held back. -func liveState(state string) bool { - return state == StateOpen || state == StateHeld || state == StateHeldBack -} - -// behindLocked are the keys of the newer offered asks of the asker of rec, oldest first; h.mu is held. -func (h *Holder) behindLocked(key string, rec AskRecord) []string { - var behind []string - for k, e := range h.asks { - if k != key && e.rec.Asker == rec.Asker && (e.rec.State == StateOpen || e.rec.State == StateHeld) && e.rec.Opened.After(rec.Opened) { - behind = append(behind, k) - } - } - sort.Slice(behind, func(i, j int) bool { - a, b := h.asks[behind[i]].rec, h.asks[behind[j]].rec - if !a.Opened.Equal(b.Opened) { - return a.Opened.Before(b.Opened) - } - return behind[i] < behind[j] - }) - return behind -} - -// pickLocked is the key of the asker's asks in one of the states, the oldest first (by opening, then key); "" when -// there is none, never except; h.mu is held. -func (h *Holder) pickLocked(asker, except string, states ...string) string { - pick := "" - for k, e := range h.asks { - if e.rec.Asker != asker || k == except { - continue - } - in := false - for _, s := range states { - in = in || e.rec.State == s - } - if !in { - continue - } - if pick == "" || e.rec.Opened.Before(h.asks[pick].rec.Opened) || (e.rec.Opened.Equal(h.asks[pick].rec.Opened) && k < pick) { - pick = k - } - } - return pick -} - -// offeredCount is how many of the asker's asks are offered now: open, or held for want of a channel. -func (h *Holder) offeredCount(asker string) int { - h.mu.Lock() - defer h.mu.Unlock() - n := 0 - for _, e := range h.asks { - if e.rec.Asker == asker && (e.rec.State == StateOpen || e.rec.State == StateHeld) { - n++ - } - } - return n -} - -// freshAsk is an ask's record read again from the store, where there is one. -func (h *Holder) freshAsk(key string) (AskRecord, uint64, bool) { - e := h.askOf(key) - if e == nil { - return AskRecord{}, 0, false - } - rec, rev := e.rec, e.rev - if h.Asks != nil { - if got, r, err := h.Asks.GetAsk(key); err == nil && got != nil { - rec, rev = *got, r - } - } - return rec, rev, true -} - -// offerHeldBack offers the asker's held-back asks of before the change again (A-424): there is no room to wait for. The work -// lock is held. -func (h *Holder) offerHeldBack(asker string) { - for { - h.mu.Lock() - key := h.pickLocked(asker, "", StateHeldBack) - h.mu.Unlock() - if key == "" { - return - } - h.offerOneHeldBack(key) - if after := h.askOf(key); after == nil || after.rec.State == StateHeldBack { - return // not offered (its record changed meanwhile): left for the next start rather than tried forever - } - } -} - -// offerAllHeldBack offers every ask held back before the change, at each tick of the router (A-424). The work lock is held. -func (h *Holder) offerAllHeldBack() { - h.mu.Lock() - askers := map[string]bool{} - for _, e := range h.asks { - if e.rec.State == StateHeldBack { - askers[e.rec.Asker] = true - } - } - h.mu.Unlock() - for asker := range askers { - h.offerHeldBack(asker) - } -} - -// offerOneHeldBack offers one held-back ask again now. -func (h *Holder) offerOneHeldBack(key string) { - rec, rev, ok := h.freshAsk(key) - if !ok || rec.State != StateHeldBack { - return - } - now := h.Now() - rec.HeldBackSince = time.Time{} - rec.Tickets = h.ticketsFor(rec.Ask) - rec.State = StateOpen - if len(rec.Tickets) == 0 { - rec.Tickets, rec.State, rec.HeldSince, rec.HeldWhy = nil, StateHeld, now, h.holdWhy(rec.Ask) - } - if !h.saveHeld(key, rec, rev) { - return - } - h.Logf("[messenger] %s's ask %q (%s) is offered again", rec.Asker, rec.Ask.Headline, rec.Ask.ID) - if rec.State != StateOpen { - return - } - h.mu.Lock() - r := h.open[rec.Record] - h.mu.Unlock() - if r != nil && r.Origin == "ask" { - h.mu.Lock() - r.Answered, r.Ask = "", key - h.mu.Unlock() - h.offerAsk(r) - h.persist(r) - return - } - rec.Record = h.recordFor(rec.Asker, key, rec.Ask) - h.showAsk(key, rec, now) -} - -// AskAgain is the seat operator-channel's verb ask-again (G §394, §450): it offers an ask held back before the channel stopped -// holding any back (A-424), and holds back nothing in its place. It answers the ask's state after it, or the error that says why -// it was not offered. -func (h *Holder) AskAgain(key string) oc.AskAgainAnswer { - h.work.Lock() - defer h.work.Unlock() - h.mu.Lock() - h.init() - h.mu.Unlock() - rec, _, ok := h.freshAsk(key) - switch { - case !ok: - return oc.AskAgainAnswer{Error: fmt.Sprintf("this router holds no ask %q: its key is ., as operator-channel.asks lists", key)} - case rec.State != StateHeldBack: - return oc.AskAgainAnswer{Error: fmt.Sprintf("the ask %s is %s, not held back: only a held-back ask is offered again, and the channel holds none back any more", key, rec.State)} - } - h.offerOneHeldBack(key) - got, _, _ := h.freshAsk(key) - return oc.AskAgainAnswer{State: string(got.State)} -} diff --git a/modules/messenger/cmd/messenger/holdback_test.go b/modules/messenger/cmd/messenger/holdback_test.go deleted file mode 100644 index 9e0d8d4e1..000000000 --- a/modules/messenger/cmd/messenger/holdback_test.go +++ /dev/null @@ -1,63 +0,0 @@ -package main - -import ( - "encoding/json" - "strings" - "testing" - "time" - - occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient" -) - -// Design 47 G A-424: the channel keeps no count of an asker's open asks and holds none back: every ask is offered when it is -// raised, five of one asker at once too, and none is refused. An ask held back before the change is offered again at the -// router's next tick, with new tickets; ask-again offers one now and holds nothing back in its place. -func TestEveryAskIsOfferedAndOneHeldBackBeforeIsOfferedAgain(t *testing.T) { - h, _, _, c, store, w := newRouter(t) - link(h, store, c, "42", c.now().Add(-2*time.Hour)) - state := func(id string) string { return store.asks["mesh-delivery."+id].State } - for i, id := range []string{"d-1", "d-2", "d-3", "d-4", "d-5"} { - a := ackAsk(c, id) - a.About = "about-" + string(rune('1'+i)) - submit(t, h, "mesh-delivery", a) - c.pass(time.Second) - settle(h, c) - } - for _, id := range []string{"d-1", "d-2", "d-3", "d-4", "d-5"} { - if state(id) != StateOpen || len(store.asks["mesh-delivery."+id].Tickets) == 0 { - t.Fatalf("%s is %s with %d tickets: every ask is offered", id, state(id), len(store.asks["mesh-delivery."+id].Tickets)) - } - } - // One held back before the change, as the store kept it. - key := "mesh-delivery.d-2" - got, rev, _ := store.GetAsk(key) - got.State, got.Tickets, got.HeldBackSince = StateHeldBack, nil, c.now() - rev, err := store.UpdateAsk(key, *got, rev) - if err != nil { - t.Fatal(err) - } - h.keepAsk(key, *got, rev) - if a := h.AskAgain("mesh-delivery.d-1"); !strings.Contains(a.Error, "not held back") { - t.Errorf("ask-again of an offered ask: %+v", a) - } - if a := h.AskAgain("mesh-delivery.d-none"); a.Error == "" || a.State != "" { - t.Errorf("ask-again of an ask the router holds none of: %+v", a) - } - h.Tick() - settle(h, c) - if rec := store.asks[key]; rec.State != StateOpen || len(rec.Tickets) == 0 || !rec.HeldBackSince.IsZero() { - t.Fatalf("the ask held back before the change, after a tick: %+v", rec) - } - for _, id := range []string{"d-1", "d-3", "d-4", "d-5"} { - if state(id) != StateOpen { - t.Errorf("%s is %s after the held-back one was offered: nothing is held back in its place", id, state(id)) - } - } - for _, wt := range w.of("mesh-delivery") { - if wt.Outcome == occ.OutcomeRefused { - t.Errorf("refused: %+v", wt) - } - } -} - -func mustJSON(v any) []byte { raw, _ := json.Marshal(v); return raw } diff --git a/modules/messenger/cmd/messenger/holder.go b/modules/messenger/cmd/messenger/holder.go index 09debb721..d87b60e68 100644 --- a/modules/messenger/cmd/messenger/holder.go +++ b/modules/messenger/cmd/messenger/holder.go @@ -778,7 +778,6 @@ func (h *Holder) Tick() { h.mu.Unlock() sort.Slice(recs, func(i, j int) bool { return recs[i].Raised.Before(recs[j].Raised) }) h.sweepAsks(now) - h.offerAllHeldBack() // held back before the channel stopped holding any back, offered at the first tick (design 47 G A-424) h.releaseHeld() h.offerUnsent() h.breakGlass() diff --git a/modules/messenger/cmd/messenger/main.go b/modules/messenger/cmd/messenger/main.go index e74264b9f..ac39a618a 100644 --- a/modules/messenger/cmd/messenger/main.go +++ b/modules/messenger/cmd/messenger/main.go @@ -761,7 +761,7 @@ func tools(h *Holder, l *listening) []stdio.Tool { "most bytes of the answer.", Input: map[string]any{ "key": str("one ask, by its key (asker.id); none for the open asks"), - "state": str("open (open, held and held-back), ended or all; none is open when no key or since is given, else both"), + "state": str("open (open and held), ended or all; none is open when no key or since is given, else both"), "since": str("only the asks opened at or after this time, RFC 3339"), "bound": map[string]any{"type": "integer", "description": "the most bytes of the asks in the answer; said when it cuts the answer short"}}, Run: func(a map[string]any) (any, error) { @@ -771,15 +771,6 @@ func tools(h *Holder, l *listening) []stdio.Tool { } return q.Narrow(h.AsksNow()), nil }}, - {Name: "operator-channel." + oc.VerbAskAgain, - Description: "Offer a held-back ask again now (G §394, §450): an asker has at most three asks offered at once, and a fourth " + - "holds its oldest back behind it (operator-channel.asks lists it with state held-back, since when and the newer asks it waits behind). " + - "It is offered again by itself when one of the asker's asks ends; this offers it now, holding back the asker's oldest offered ask in its " + - "place. Answers the ask's state, or why it was not offered (an ask that is not held back).", - Input: map[string]any{"key": str("the held-back ask, by its key (asker.id)")}, - Run: func(a map[string]any) (any, error) { - return h.AskAgain(strArg(a, "key")), nil - }}, {Name: "messenger_identities", Description: "The accounts linked as the operator, one per channel kind: the account, the name its " + "service shows, when it was linked, and from when it can approve (an hour after linking).", diff --git a/modules/messenger/cmd/messenger/manifest_test.go b/modules/messenger/cmd/messenger/manifest_test.go index 354ed6f63..3145cc45e 100644 --- a/modules/messenger/cmd/messenger/manifest_test.go +++ b/modules/messenger/cmd/messenger/manifest_test.go @@ -68,7 +68,7 @@ func readManifest(t *testing.T) (manifest, string) { func TestItDeclaresTheSeatsOfAnAskAndHoldsTheOperatorChannel(t *testing.T) { // 1217/AC6: condition beside ask and cancel m, _ := readManifest(t) - verbs := []string{"open", "history", "notify", "tell", "asks", oc.VerbAskAgain} + verbs := oc.Verbs() // every verb of the seat, and no more: ask-again went with the channel's limit (design 47 H §764) want := []seat{ {Name: oc.Seat, Scope: "mesh", Serves: verbs, Accepts: []string{"ask", "cancel", "condition"}, Emits: []string{"decided"}, ByCaller: []string{"ask", "cancel", "condition", "decided"}, Records: []string{"asks"}}, diff --git a/modules/messenger/module.json b/modules/messenger/module.json index f3b66dd47..020de147d 100644 --- a/modules/messenger/module.json +++ b/modules/messenger/module.json @@ -12,8 +12,7 @@ "history", "notify", "tell", - "asks", - "ask-again" + "asks" ], "accepts": [ "ask", @@ -67,8 +66,7 @@ "history", "notify", "tell", - "asks", - "ask-again" + "asks" ] } ], -- 2.54.0 From df79ab8769fe319ba42aa0e489f97036c14e84f6 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 06:04:16 +0100 Subject: [PATCH 35/43] mesh-knowledge: the overview's delivery block and the progress verb's groups lines go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review round 1, Must 3: the groups and group-ended are retired; the overview showed the deliveries, another module's state, which the status view owns. The overview keeps two sources (the tracker, the asks), consumes no merge, transition or pipeline's end, and the progress verb asks no groups. Builds: design 47 H §764 (the gate's verb groups; the runner's group-ended); H-A-28; H-A-125 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/mesh-knowledge/manifest_test.go | 10 ++- .../cmd/mesh-knowledge/overview.go | 65 +++--------------- .../cmd/mesh-knowledge/overview_test.go | 66 +++++++++---------- .../mesh-knowledge/cmd/mesh-knowledge/page.go | 4 +- .../cmd/mesh-knowledge/page_test.go | 2 +- .../cmd/mesh-knowledge/progress.go | 57 +++------------- .../cmd/mesh-knowledge/progress_bus_test.go | 6 +- .../cmd/mesh-knowledge/progress_test.go | 10 +-- .../testdata/progress/README.md | 2 +- .../testdata/progress/groups.json | 3 - modules/mesh-knowledge/module.json | 4 -- 11 files changed, 68 insertions(+), 161 deletions(-) delete mode 100644 modules/mesh-knowledge/cmd/mesh-knowledge/testdata/progress/groups.json diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/manifest_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/manifest_test.go index ac295944b..3a7852b72 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/manifest_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/manifest_test.go @@ -84,9 +84,15 @@ func TestItDeclaresAndHoldsTheKnowledgeBaseSeatOnceForTheMesh(t *testing.T) { } if !reflect.DeepEqual(m.Invokes, []string{"seat:issue-tracker.reserve", "claude-code.claude_code_judge", "records.records_status", "records.records_list", "records.records_read", "seat:operator-channel.notify", "seat:issue-tracker.list", "seat:issue-tracker.get", - "seat:issue-tracker.milestones", "seat:issue-tracker.changes", "seat:mesh-delivery.deliveries", "seat:mesh-delivery.show", "seat:mesh-delivery.groups", "seat:operator-channel.asks", "seat:mesh-hand-acts.hand-act"}) { + "seat:issue-tracker.milestones", "seat:issue-tracker.changes", "seat:mesh-delivery.deliveries", "seat:mesh-delivery.show", "seat:operator-channel.asks", "seat:mesh-hand-acts.hand-act"}) { t.Fatalf("invokes %v", m.Invokes) } + // The overview keeps no delivery block (design 47 H, H-A-125): it hears no merge, transition or pipeline's end. + for _, e := range m.Consumes { + if strings.HasPrefix(e, "git.") || strings.HasPrefix(e, "mesh-delivery") || strings.HasPrefix(e, "mesh-pipeline-runner.") { + t.Errorf("it consumes %s, and the overview shows no deliveries", e) + } + } src := "" for _, f := range []string{"main.go", "hq.go", "progress.go", "progress_since.go", "milestones.go", "overview.go", "tools.go"} { src += readFile(t, f) @@ -282,7 +288,7 @@ func TestItAsksForItsBusStateInTheTypedForm(t *testing.T) { // overviewConsumes are the events the overview page hears, in the order the manifest lists them. func overviewConsumes() []string { var out []string - for _, s := range []string{SourceTracker, SourceDelivery, SourceAsks} { + for _, s := range []string{SourceTracker, SourceAsks} { out = append(out, OverviewEvents[s]...) } return out diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/overview.go b/modules/mesh-knowledge/cmd/mesh-knowledge/overview.go index 88bbf9a39..0a8985729 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/overview.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/overview.go @@ -2,9 +2,9 @@ package main // The overview page (novox/hq to-be 55 §11, ADR 0616): one read-only page for the operator with three blocks: every design in // progress with its milestones and their issues; the decisions and research efforts accepted in the last seven days and those -// waiting; and what waits on the operator (records waiting for an Approve, the operator channel's open asks, the delivery -// groups waiting). Built from mesh paths alone: the module's own ledger, and three other modules' verbs for the first view and -// their events after it. It never polls: a source is asked again only when it is marked unanswered, on a bounded backoff. +// waiting; and what waits on the operator (records waiting for an Approve, the operator channel's open asks). No delivery +// block: the deliveries are another module's state, read through the status view (design 47 H, H-A-125). Built from mesh +// paths alone: the module's own ledger, and two other modules' verbs for the first view and their events after it. It never polls: a source is asked again only when it is marked unanswered, on a bounded backoff. import ( "encoding/json" @@ -19,24 +19,21 @@ import ( // The sources of the page, besides the module's own ledger. const ( - SourceTracker = "issue-tracker" - SourceDelivery = "mesh-delivery" - SourceAsks = "operator-channel" + SourceTracker = "issue-tracker" + SourceAsks = "operator-channel" ) // The verbs of the first view, and the events that keep each source current. const ( TrackerMilestonesVerb = "seat:issue-tracker.milestones" TrackerListVerb = "seat:issue-tracker.list" - DeliveryGroupsVerb = "seat:mesh-delivery.groups" AsksVerb = "seat:operator-channel.asks" ) // OverviewEvents are the events the page consumes, by source, as the manifest declares them. var OverviewEvents = map[string][]string{ - SourceTracker: {"mesh-issues.opened", "mesh-issues.moved", "mesh-issues.milestone-set", "mesh-issues.milestone-moved"}, - SourceDelivery: {"git.pull-merged", "mesh-delivery-gate.transition", "mesh-pipeline-runner.group-ended"}, - SourceAsks: {"messenger.offered", "messenger.closed"}, + SourceTracker: {"mesh-issues.opened", "mesh-issues.moved", "mesh-issues.milestone-set", "mesh-issues.milestone-moved"}, + SourceAsks: {"messenger.offered", "messenger.closed"}, } // The unanswered marks: a verb that did not answer, or a subscription that missed its heartbeat. @@ -123,7 +120,6 @@ type OverviewModel struct { type OperatorBlock struct { Records []RecordRow `json:"records"` Asks []askRow `json:"asks"` - Groups []groupRow `json:"delivery-groups"` } // Overview is the page's model in memory, kept by events. @@ -136,7 +132,6 @@ type Overview struct { mu sync.Mutex milestones map[string]trackerMilestone issues map[int]trackerRow - groups map[string]groupRow asks map[string]askRow state map[string]*SourceState rebuilt time.Time @@ -147,8 +142,8 @@ type Overview struct { // NewOverview is a model with no source answered: Start asks each source's verb once. func NewOverview(ask SeatAsk, records func() ([]*Entry, error), now func() time.Time) *Overview { o := &Overview{Ask: ask, Records: records, Now: now, milestones: map[string]trackerMilestone{}, issues: map[int]trackerRow{}, - groups: map[string]groupRow{}, asks: map[string]askRow{}, state: map[string]*SourceState{}, verbCalls: map[string]int{}} - for _, s := range []string{SourceTracker, SourceDelivery, SourceAsks} { + asks: map[string]askRow{}, state: map[string]*SourceState{}, verbCalls: map[string]int{}} + for _, s := range []string{SourceTracker, SourceAsks} { o.state[s] = &SourceState{Name: s} } return o @@ -234,19 +229,6 @@ func (o *Overview) rebuildLocked(source string) error { for _, r := range rows { o.issues[r.Number] = r } - case SourceDelivery: - raw, err := call(DeliveryGroupsVerb, map[string]any{}) - if err != nil { - return fail(err.Error()) - } - var gs []groupRow - if err := rowsOf(raw, &gs, "groups"); err != nil { - return fail("groups answered something else: " + err.Error()) - } - o.groups = map[string]groupRow{} - for _, g := range gs { - o.groups[g.ID] = g - } case SourceAsks: raw, err := call(AsksVerb, map[string]any{}) if err != nil { @@ -279,7 +261,7 @@ func (o *Overview) sourceNames() []string { // Start is the first view: every source's verb asked once. A verb that does not answer marks its source. func (o *Overview) Start() { - for _, s := range []string{SourceTracker, SourceDelivery, SourceAsks} { + for _, s := range []string{SourceTracker, SourceAsks} { _ = o.Rebuild(s) } } @@ -339,16 +321,11 @@ func (o *Overview) Heard(source, event string, body json.RawMessage) { Number int `json:"number"` Status string `json:"status"` To string `json:"to"` - State string `json:"state"` Title string `json:"title"` Design int `json:"design"` ID string `json:"id"` - Group string `json:"group"` - Branch string `json:"branch"` Outcome string `json:"outcome"` Issues []int `json:"issues"` - Members []string `json:"members"` - Why string `json:"why"` Key string `json:"key"` Asker string `json:"asker"` Headline string `json:"headline"` @@ -381,22 +358,6 @@ func (o *Overview) Heard(source, event string, body json.RawMessage) { m.Status = firstNonEmpty(f.To, f.Status, m.Status) o.milestones[k] = m } - case SourceDelivery: - id := firstNonEmpty(f.Group, f.ID) - switch name { - case "pull-merged": - // A merged pull request leaves the group named by its head branch (a group's id is its branch name): it waits for - // the operator no longer (design 47 G §431). - delete(o.groups, f.Branch) - case "transition": - if g, ok := o.groups[id]; ok { - g.State = firstNonEmpty(f.To, f.State, g.State) - g.Why = firstNonEmpty(f.Why, g.Why) - o.groups[id] = g - } - case "group-ended": - delete(o.groups, id) - } case SourceAsks: switch name { case "offered": @@ -481,12 +442,6 @@ func (o *Overview) Model() (*OverviewModel, error) { m.Operator.Asks = append(m.Operator.Asks, a) } sort.Slice(m.Operator.Asks, func(i, j int) bool { return m.Operator.Asks[i].Key < m.Operator.Asks[j].Key }) - for _, g := range o.groups { - if g.State != "merged" { - m.Operator.Groups = append(m.Operator.Groups, g) - } - } - sort.Slice(m.Operator.Groups, func(i, j int) bool { return m.Operator.Groups[i].ID < m.Operator.Groups[j].ID }) for _, n := range o.sourceNames() { m.Sources = append(m.Sources, *o.state[n]) } diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/overview_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/overview_test.go index 646ac6f92..37023e142 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/overview_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/overview_test.go @@ -25,7 +25,6 @@ func newOverviewWorld(t *testing.T) *overviewWorld { w := &overviewWorld{now: time.Date(2026, 11, 4, 12, 0, 0, 0, time.UTC), fail: map[string]bool{}, answers: map[string]string{ TrackerMilestonesVerb: `{"milestones":[{"design":47,"id":"1","outcome":"A merge is never dropped","status":"open","issues":[973,525]},{"design":47,"id":"2","outcome":"Done thing","status":"done","issues":[1]}]}`, TrackerListVerb: `[{"number":973,"title":"The window","status":"fixing"},{"number":525,"title":"The queue","status":"open"}]`, - DeliveryGroupsVerb: `[{"id":"fix/a","state":"held","why":"a member is held","members":["x@1"]},{"id":"fix/b","state":"checking","members":["y@2"]}]`, AsksVerb: `{"asks":[{"key":"mesh-knowledge.1","asker":"mesh-knowledge","headline":"Approve ADR 0301","state":"open","offered_on":["telegram"]},{"key":"ask-0","asker":"x","headline":"old","state":"answered"}]}`, }} w.o = NewOverview(func(key string, body any) (json.RawMessage, error) { @@ -45,8 +44,8 @@ func recordEntry(n int, status, date string, accepted time.Time, mut func(*Recor return &Entry{Current: r, CurrentVersion: 1, Versions: 1} } -// The page's model (ADR 0616 §1-3, AC P1): three blocks built from the ledger, the tracker's milestones and issues, the delivery -// groups and the operator channel's asks; events change the blocks and ask no verb; a record accepted eight days before the +// The page's model (ADR 0616 §1-3, AC P1): three blocks built from the ledger, the tracker's milestones and issues and the +// operator channel's asks, and no delivery block (design 47 H, H-A-125); events change the blocks and ask no verb; a record accepted eight days before the // answer is not in the seven-day block, with no event between. func TestThePageModelHasThreeBlocksKeptByEventsAndAsksNoVerbAgain(t *testing.T) { w := newOverviewWorld(t) @@ -68,28 +67,25 @@ func TestThePageModelHasThreeBlocksKeptByEventsAndAsksNoVerbAgain(t *testing.T) t.Fatalf("the design block: %+v", m.Designs) } if len(m.Accepted) != 2 || len(m.Waiting) != 2 || len(m.Operator.Records) != 1 || m.Operator.Records[0].Record != "ADR 0303" || - len(m.Operator.Asks) != 1 || m.Operator.Asks[0].Key != "mesh-knowledge.1" || len(m.Operator.Groups) != 2 { + len(m.Operator.Asks) != 1 || m.Operator.Asks[0].Key != "mesh-knowledge.1" || len(m.Sources) != 2 { t.Fatalf("%+v", m) } asked := map[string]int{} - for _, v := range []string{TrackerMilestonesVerb, TrackerListVerb, DeliveryGroupsVerb, AsksVerb} { + for _, v := range []string{TrackerMilestonesVerb, TrackerListVerb, AsksVerb} { asked[v] = w.o.Calls(v) } // Events change the blocks. - w.o.Heard(SourceDelivery, "git.pull-merged", json.RawMessage(`{"repository":"novox/y","number":2,"branch":"fix/b"}`)) - w.o.Heard(SourceDelivery, "mesh-delivery-gate.transition", json.RawMessage(`{"group":"fix/a","to":"approved"}`)) w.o.Heard(SourceAsks, "messenger.offered", json.RawMessage(`{"id":"2","asker":"mesh-delivery","headline":"Approve the group","channels":["telegram"]}`)) w.o.Heard(SourceTracker, "mesh-issues.opened", json.RawMessage(`{"number":1999,"title":"New","status":"open"}`)) w.o.Heard(SourceTracker, "mesh-issues.milestone-moved", json.RawMessage(`{"design":47,"id":"1","to":"done","issues":[973,525,1999]}`)) m, _ = w.o.Model() - if len(m.Operator.Groups) != 1 || m.Operator.Groups[0].State != "approved" || len(m.Operator.Asks) != 2 || len(m.Designs) != 0 { + if len(m.Operator.Asks) != 2 || len(m.Designs) != 0 { t.Fatalf("after the events: %+v", m) } - w.o.Heard(SourceDelivery, "mesh-pipeline-runner.group-ended", json.RawMessage(`{"group":"fix/a","branch":"main","outcome":"passed"}`)) w.o.Heard(SourceAsks, "messenger.closed", json.RawMessage(`{"id":"1","asker":"mesh-knowledge","how":"answered"}`)) m, _ = w.o.Model() - if len(m.Operator.Groups) != 0 || len(m.Operator.Asks) != 1 || m.Operator.Asks[0].Key != "mesh-delivery.2" { - t.Fatalf("after the group ended and the ask closed: %+v", m.Operator) + if len(m.Operator.Asks) != 1 || m.Operator.Asks[0].Key != "mesh-delivery.2" { + t.Fatalf("after the ask closed: %+v", m.Operator) } for v, n := range asked { if w.o.Calls(v) != n { @@ -109,56 +105,56 @@ func TestThePageModelHasThreeBlocksKeptByEventsAndAsksNoVerbAgain(t *testing.T) // next event; the model as a tool equals the page's model; the re-read tool asks one source and records the hand act. func TestASourceThatDoesNotAnswerIsSaidAndAskedAgainOnABackoff(t *testing.T) { w := newOverviewWorld(t) - w.fail[DeliveryGroupsVerb] = true + w.fail[AsksVerb] = true w.o.Start() m, _ := w.o.Model() - var delivery SourceState + var asks SourceState for _, s := range m.Sources { - if s.Name == SourceDelivery { - delivery = s + if s.Name == SourceAsks { + asks = s } } - if delivery.Answered || delivery.Mark == nil || delivery.Mark.Cause != MarkVerb || len(m.Operator.Groups) != 0 || !strings.Contains(delivery.Mark.Why, "no answer in time") { - t.Fatalf("%+v", delivery) + if asks.Answered || asks.Mark == nil || asks.Mark.Cause != MarkVerb || len(m.Operator.Asks) != 0 || !strings.Contains(asks.Mark.Why, "no answer in time") { + t.Fatalf("%+v", asks) } if len(m.Designs) != 1 { t.Fatalf("another source's block was lost: %+v", m.Designs) } // Asked again on the backoff alone: 10 s, then doubling, at most 5 minutes; no other source is asked. - calls := w.o.Calls(DeliveryGroupsVerb) + calls := w.o.Calls(AsksVerb) if got := w.o.Due(); len(got) != 0 { t.Fatalf("asked before the backoff ran out: %v", got) } var waits []time.Duration for i := 0; i < 8; i++ { before := w.now - w.now = w.o.state[SourceDelivery].Mark.Next + w.now = w.o.state[SourceAsks].Mark.Next waits = append(waits, w.now.Sub(before)) - if got := w.o.Due(); len(got) != 1 || got[0] != SourceDelivery { + if got := w.o.Due(); len(got) != 1 || got[0] != SourceAsks { t.Fatalf("%v", got) } } - if waits[0] != BackoffFrom || waits[1] != 2*BackoffFrom || waits[7] != BackoffMost || w.o.Calls(DeliveryGroupsVerb) != calls+8 || w.o.Calls(AsksVerb) != 1 { - t.Fatalf("waits %v, delivery asked %d, asks asked %d", waits, w.o.Calls(DeliveryGroupsVerb), w.o.Calls(AsksVerb)) + if waits[0] != BackoffFrom || waits[1] != 2*BackoffFrom || waits[7] != BackoffMost || w.o.Calls(AsksVerb) != calls+8 || w.o.Calls(TrackerListVerb) != 1 { + t.Fatalf("waits %v, asks asked %d, tracker asked %d", waits, w.o.Calls(AsksVerb), w.o.Calls(TrackerListVerb)) } // Its first answer rebuilds the block and clears the mark. - w.fail[DeliveryGroupsVerb] = false - w.now = w.o.state[SourceDelivery].Mark.Next + w.fail[AsksVerb] = false + w.now = w.o.state[SourceAsks].Mark.Next w.o.Due() m, _ = w.o.Model() - if len(w.o.Unanswered()) != 0 || len(m.Operator.Groups) != 2 { + if len(w.o.Unanswered()) != 0 || len(m.Operator.Asks) != 1 { t.Fatalf("%+v", m) } // A subscription that goes silent marks the source from the subscription and asks nothing while it is silent. asked := w.o.Calls(AsksVerb) w.now = w.now.Add(HeartbeatWithin + time.Second) - if got := w.o.Silent(); len(got) != 3 { + if got := w.o.Silent(); len(got) != 2 { t.Fatalf("%v", got) } if got := w.o.Due(); len(got) != 0 || w.o.Calls(AsksVerb) != asked { t.Fatalf("a silent subscription was asked: %v", got) } - if u := w.o.Unanswered(); len(u) != 3 || u[0].Mark.Cause != MarkSubscription { + if u := w.o.Unanswered(); len(u) != 2 || u[0].Mark.Cause != MarkSubscription { t.Fatalf("%+v", u) } // Its next event (or heartbeat) makes the module ask the verb once, and that answer clears the mark. @@ -167,19 +163,19 @@ func TestASourceThatDoesNotAnswerIsSaidAndAskedAgainOnABackoff(t *testing.T) { t.Fatalf("asked %d, mark %+v", w.o.Calls(AsksVerb), w.o.state[SourceAsks].Mark) } // An unanswered verb then turns the mark into one from the verb. - w.fail[DeliveryGroupsVerb] = true - w.o.Heard(SourceDelivery, "", nil) - if mk := w.o.state[SourceDelivery].Mark; mk == nil || mk.Cause != MarkVerb { + w.fail[TrackerMilestonesVerb] = true + w.o.Heard(SourceTracker, "", nil) + if mk := w.o.state[SourceTracker].Mark; mk == nil || mk.Cause != MarkVerb { t.Fatalf("%+v", mk) } // The runtime's word on the module's consumer (stdio.OnHeartbeat): a missed heartbeat marks every subscribed source // at once, before the bound passes, with the runtime's why; the next beat asks each verb once and clears the marks. - w.fail[DeliveryGroupsVerb] = false - w.now = w.o.state[SourceDelivery].Mark.Next + w.fail[TrackerMilestonesVerb] = false + w.now = w.o.state[SourceTracker].Mark.Next w.o.Due() asked = w.o.Calls(AsksVerb) w.o.Beat(stdio.Heartbeat{At: w.now, Missed: true, Why: "the bus's heartbeat stopped arriving"}) - if u := w.o.Unanswered(); len(u) != 3 || u[0].Mark.Cause != MarkSubscription || !strings.Contains(u[0].Mark.Why, "stopped arriving") { + if u := w.o.Unanswered(); len(u) != 2 || u[0].Mark.Cause != MarkSubscription || !strings.Contains(u[0].Mark.Why, "stopped arriving") { t.Fatalf("a missed heartbeat marked %+v", u) } w.o.Beat(stdio.Heartbeat{At: w.now}) @@ -218,12 +214,12 @@ func TestThePagesModelIsAToolAndItsRereadIsAHandAct(t *testing.T) { } // The re-read asks the one source and no other, and records itself. before := map[string]int{} - for _, v := range []string{TrackerListVerb, DeliveryGroupsVerb, AsksVerb} { + for _, v := range []string{TrackerListVerb, AsksVerb} { before[v] = w.o.Calls(v) } w.fail[AsksVerb] = true res, err := callTool(t, tools, RereadTool, map[string]any{"source": SourceAsks, "why": "the page showed it stale"}) - if err != nil || res["answered"] != false || w.o.Calls(AsksVerb) != before[AsksVerb]+1 || w.o.Calls(DeliveryGroupsVerb) != before[DeliveryGroupsVerb] || w.o.Calls(TrackerListVerb) != before[TrackerListVerb] { + if err != nil || res["answered"] != false || w.o.Calls(AsksVerb) != before[AsksVerb]+1 || w.o.Calls(TrackerListVerb) != before[TrackerListVerb] { t.Fatalf("%v %v", err, res) } if len(acts) != 1 || acts[0] != "seat:mesh-hand-acts.hand-act" { diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/page.go b/modules/mesh-knowledge/cmd/mesh-knowledge/page.go index 2279a37ae..bd73ff881 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/page.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/page.go @@ -88,13 +88,11 @@ code{font-size:.9em}.src{display:inline-block;margin-right:1rem} {{if .Waiting}}

    {{range .Waiting}}
  • {{.Record}}: {{.Title}} waits for {{.Waits}}
  • {{end}}
{{else}}

None.

{{end}}

Waiting on you

-{{range .Sources}}{{if and (eq .Name "operator-channel") (not .Answered)}}

The operator channel does not answer: the asks below are not complete (last answer {{clock .Last}}).

{{end}}{{if and (eq .Name "mesh-delivery") (not .Answered)}}

Delivery does not answer: the groups below are not complete (last answer {{clock .Last}}).

{{end}}{{end}} +{{range .Sources}}{{if and (eq .Name "operator-channel") (not .Answered)}}

The operator channel does not answer: the asks below are not complete (last answer {{clock .Last}}).

{{end}}{{end}}

Records waiting for your Approve

{{if .Operator.Records}}
    {{range .Operator.Records}}
  • {{.Record}}: {{.Title}}
  • {{end}}
{{else}}

None.

{{end}}

Open asks

{{if .Operator.Asks}}
    {{range .Operator.Asks}}
  • {{.Headline}} from {{.Asker}}{{if .Offered}}, offered on {{join .Offered ", "}}{{end}}
  • {{end}}
{{else}}

None.

{{end}} -

Delivery groups waiting

-{{if .Operator.Groups}}
    {{range .Operator.Groups}}
  • {{.ID}} {{.State}}{{if .Why}}: {{.Why}}{{end}}
  • {{end}}
{{else}}

None.

{{end}} `)) // ServePage starts the overview: the model's first view, the events kept, the unanswered sources asked again on their backoff, diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/page_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/page_test.go index 1cb57e8ef..0532cc2f1 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/page_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/page_test.go @@ -22,7 +22,7 @@ func TestThePageAnswersGetOnlyAndTheManifestKeepsItToThisMachine(t *testing.T) { h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil)) body := rec.Body.String() if rec.Code != 200 || !strings.Contains(body, "Design 47, milestone 1") || !strings.Contains(body, "ADR 0301") || !strings.Contains(body, "Approve ADR 0301") || - !strings.Contains(body, "fix/a") { + strings.Contains(body, "Delivery") { // no delivery block (design 47 H, H-A-125) t.Fatalf("%d\n%.600s", rec.Code, body) } if strings.Contains(body, " Date: Fri, 20 Nov 2026 06:06:57 +0100 Subject: [PATCH 36/43] gitea, mesh-issues: no timer said for main's rules; the checker is no owner MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review round 1, notes: keep-main-rules acts at the token's mint alone since the ten-minute step went (H-A-100), and the issue tracker's owners named the checker's path, which a search for the checker must not find (§764). Builds: design 47 H §764 (the forge module's ten-minute timer; the checker whole); H-A-100 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/gitea/index.ts | 5 ++--- modules/gitea/module.json | 2 +- modules/gitea/test/mainrules.test.ts | 10 +++++++++- modules/mesh-issues/cmd/mesh-issues/core_test.go | 13 ++++++++++++- .../mesh-issues/cmd/mesh-issues/manifest_test.go | 2 +- modules/mesh-issues/files/settings.json | 1 - modules/mesh-issues/module.json | 1 - 7 files changed, 25 insertions(+), 9 deletions(-) diff --git a/modules/gitea/index.ts b/modules/gitea/index.ts index 4e52c8c3b..6418924f7 100644 --- a/modules/gitea/index.ts +++ b/modules/gitea/index.ts @@ -469,9 +469,8 @@ if (gitea) { } void registrations.start().then(() => reloadHeads()).catch(say); } - // Main takes no push and only the tools' account merges, in every repository, kept and not only when a token is - // minted: at the start and every ten minutes after, so a repository made later gets it too (issue 948, ADR 0398 §2). - // Only while the setting keep-main-rules is true (mainrules.ts); false, the default, leaves every repository's rule as set. + // Main's rule is put right only when the tools' account's token is minted, and only while the setting keep-main-rules is + // true (mainrules.ts); no timer keeps it (design 47 H §764, H-A-100). // The package seats' write token (packages-account.ts): minted at the start; after that renewed only when the Go // bundle that writes with it says the forge refused it (the finding PACKAGES_TOKEN_REFUSED), never on a timer. try { diff --git a/modules/gitea/module.json b/modules/gitea/module.json index 9941bc4e5..40de8e851 100644 --- a/modules/gitea/module.json +++ b/modules/gitea/module.json @@ -220,7 +220,7 @@ }, "keep-main-rules": { "type": "boolean", - "description": "Whether the module keeps main's branch protection as the mesh wants it. True: every ten minutes, and at the start, main's rule in every repository of the organisation novox is put to no push and the tools' account alone merging, and each apply of the `repositories` value narrows the main rule of the repositories it lists, requiring no status. False (the default): the module changes no existing repository's branch protection, whoever set it at the forge, but takes away a required status it finds on a rule covering main, once (ADR 0534 §1); a repository listed in `repositories` that does not exist is still made, with its first protection.", + "description": "Whether the module keeps main's branch protection as the mesh wants it. True: when the tools' account's token is minted, main's rule in every repository of the organisation novox is put to no push and the tools' account alone merging, and each apply of the `repositories` value narrows the main rule of the repositories it lists, requiring no status. False (the default): the module changes no existing repository's branch protection, whoever set it at the forge, but takes away a required status it finds on a rule covering main, once (ADR 0534 §1); a repository listed in `repositories` that does not exist is still made, with its first protection.", "trusted": true, "default": false }, diff --git a/modules/gitea/test/mainrules.test.ts b/modules/gitea/test/mainrules.test.ts index 9575b4361..1d26b745c 100644 --- a/modules/gitea/test/mainrules.test.ts +++ b/modules/gitea/test/mainrules.test.ts @@ -2,7 +2,7 @@ // writes nothing at the forge, true puts main's rule right as before. import { test } from "node:test"; import assert from "node:assert/strict"; -import { mkdtempSync, writeFileSync } from "node:fs"; +import { mkdtempSync, readFileSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; @@ -15,6 +15,14 @@ function env(body?: string): NodeJS.ProcessEnv { return { MESH_GITEA_CONFIG_FILE: f }; } +// No timer keeps main's rules (design 47 H §764, H-A-100): the setting's description says when it acts, and names none. +test("the setting's description names no timer", () => { + const manifest = JSON.parse(readFileSync(new URL("../module.json", import.meta.url), "utf8")); + const said: string = manifest.config["keep-main-rules"].description; + assert.match(said, /token is minted/); + assert.doesNotMatch(said, /every ten minutes|every \w+ minutes/); +}); + test("the setting is false unless the config says true", () => { assert.equal(keepMainRules(env()), false); assert.equal(keepMainRules(env("{}")), false); diff --git a/modules/mesh-issues/cmd/mesh-issues/core_test.go b/modules/mesh-issues/cmd/mesh-issues/core_test.go index 237801475..49b3527ea 100644 --- a/modules/mesh-issues/cmd/mesh-issues/core_test.go +++ b/modules/mesh-issues/cmd/mesh-issues/core_test.go @@ -13,7 +13,7 @@ func TestIsCoreNamesTheBuildModulesAndTheDeliveryParts(t *testing.T) { for _, e := range []string{ "mesh-catalog modules/mesh-build-go", "mesh-catalog modules/mesh-build-node", "mesh-catalog modules/mesh-build-image", "mesh-catalog modules/mesh-build-go/cmd/mesh-build-go", - "mesh-delivery", "mesh-delivery modules/mesh-delivery-checker", "mesh-catalog modules/mesh-delivery-gate", + "mesh-delivery", "mesh-catalog modules/mesh-delivery-gate", "mesh-catalog modules/nats", "mesh-catalog modules/gitea", "mesh-sdk", } { if !IsCore([]string{e}) { @@ -43,5 +43,16 @@ func TestNoOwnerNamesBuildAgent(t *testing.T) { if strings.Contains(o, "build-agent") { t.Errorf("the owner %q is registered: build-agent is gone (ADR 0524 §5)", o) } + if strings.Contains(o, "mesh-delivery-checker") { + t.Errorf("the owner %q is registered: the checker is gone whole (design 47 H §764)", o) + } + } + // The manifest's default owners name it no more either. + raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(raw), "mesh-delivery-checker") { + t.Error("the manifest names mesh-delivery-checker: the checker is gone whole (design 47 H §764)") } } diff --git a/modules/mesh-issues/cmd/mesh-issues/manifest_test.go b/modules/mesh-issues/cmd/mesh-issues/manifest_test.go index 9cd24d027..9588b3ed6 100644 --- a/modules/mesh-issues/cmd/mesh-issues/manifest_test.go +++ b/modules/mesh-issues/cmd/mesh-issues/manifest_test.go @@ -264,7 +264,7 @@ func TestEveryCodeModuleOfTheCatalogueIsAnOwner(t *testing.T) { } // 1815/AC2: the delivery repository's modules cannot ride on its repository entry (owns refuses a folder // of a repository that is registered only as a whole), so each is named. - for _, m := range []string{"builder", "checker", "indexer"} { + for _, m := range []string{"builder", "indexer"} { // the checker went whole (design 47 H §764) if Owners(s.Owners).owns("mesh-delivery modules/mesh-delivery-"+m) == "" { t.Errorf("mesh-delivery-%s is not an owner", m) } diff --git a/modules/mesh-issues/files/settings.json b/modules/mesh-issues/files/settings.json index b833058f6..8c2f04378 100644 --- a/modules/mesh-issues/files/settings.json +++ b/modules/mesh-issues/files/settings.json @@ -147,7 +147,6 @@ "mesh-catalog modules/zsh-syntax-highlighting", "mesh-delivery", "mesh-delivery modules/mesh-delivery-builder", - "mesh-delivery modules/mesh-delivery-checker", "mesh-delivery modules/mesh-delivery-forge-bridge", "mesh-delivery modules/mesh-delivery-indexer", "mesh-delivery modules/mesh-delivery-teller", diff --git a/modules/mesh-issues/module.json b/modules/mesh-issues/module.json index ebf31730c..7e1f48975 100644 --- a/modules/mesh-issues/module.json +++ b/modules/mesh-issues/module.json @@ -259,7 +259,6 @@ "mesh-catalog modules/zsh-syntax-highlighting", "mesh-delivery", "mesh-delivery modules/mesh-delivery-builder", - "mesh-delivery modules/mesh-delivery-checker", "mesh-delivery modules/mesh-delivery-forge-bridge", "mesh-delivery modules/mesh-delivery-indexer", "mesh-delivery modules/mesh-delivery-teller", -- 2.54.0 From 8c8b4367f1baa6d1a0eeda6ed21a6b38f381b6f7 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 06:10:56 +0100 Subject: [PATCH 37/43] mesh-mail-reader: its callers list goes; every verb answers every caller MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every account may call every verb; what protects the mail is what the verbs answer (ADR 0459 §2), not who may call them. Builds: design 47 H §764 (every callers list, §752); H-A-122 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-mail-reader/README.md | 6 +- .../cmd/mesh-mail-reader/callers.go | 84 ------------------- .../cmd/mesh-mail-reader/callers_test.go | 52 ------------ .../cmd/mesh-mail-reader/correct_test.go | 4 +- .../cmd/mesh-mail-reader/entries_test.go | 12 +-- .../cmd/mesh-mail-reader/health_test.go | 4 +- .../cmd/mesh-mail-reader/main.go | 6 +- .../runtime_player_integration_test.go | 6 -- .../cmd/mesh-mail-reader/tools.go | 79 +++-------------- .../cmd/mesh-mail-reader/tools_test.go | 78 +++-------------- modules/mesh-mail-reader/files/callers.json | 3 - modules/mesh-mail-reader/module.json | 22 +---- 12 files changed, 31 insertions(+), 325 deletions(-) delete mode 100644 modules/mesh-mail-reader/cmd/mesh-mail-reader/callers.go delete mode 100644 modules/mesh-mail-reader/cmd/mesh-mail-reader/callers_test.go delete mode 100644 modules/mesh-mail-reader/files/callers.json diff --git a/modules/mesh-mail-reader/README.md b/modules/mesh-mail-reader/README.md index c6e06e2d5..da4e73b9e 100644 --- a/modules/mesh-mail-reader/README.md +++ b/modules/mesh-mail-reader/README.md @@ -9,8 +9,7 @@ account never does. It reads mail; it moves, flags, deletes and sends nothing (A - **Claims `mail-reading`** (held once for the mesh), declared in mesh-mail's own manifest beside its three mail seats, serving six verbs: `entries`, `groups`, `correct`, `lessons`, `undo`, `status` (ADR 0459 §2). - Each answers only a caller the trusted setting `callers` names (default `["mesh-mail-web"]`); any other - caller is refused, naming why. + Each answers every caller: the module keeps no callers list (design 47 H, H-A-122). - **Hears mesh-mail's added events** and judges a batch of up to 10 at a time against a question put to `claude_code_judge`, keeping an entry per mail — never its body — in its own database (issue 1518, 1427; not yet built). @@ -19,8 +18,7 @@ account never does. It reads mail; it moves, flags, deletes and sends nothing (A - **Tells the operator** at most once a batch when a mail matters, and corrects itself at once, keeping a reading lesson, when the operator says it judged wrong (issue 1428, 1429, 1522; not yet built). -Until each of those issues lands, its own verb answers that it is not built yet, once the caller check above -has already passed or refused. +Until each of those issues lands, its own verb answers that it is not built yet. ## Its database diff --git a/modules/mesh-mail-reader/cmd/mesh-mail-reader/callers.go b/modules/mesh-mail-reader/cmd/mesh-mail-reader/callers.go deleted file mode 100644 index 6d666e913..000000000 --- a/modules/mesh-mail-reader/cmd/mesh-mail-reader/callers.go +++ /dev/null @@ -1,84 +0,0 @@ -package main - -// Callers is the trusted setting `callers` (lead-m2@g14's decision at this milestone's prepare step, said to -// the operator; novox/hq ADR 0459 §2: "each answers only a caller the operator grants it"): who may call one -// of mail-reading's six verbs. An entry is a bare module name (granted wherever it runs, on any machine), -// exactly ., or exactly person. — the same shapes mesh-mail's own trusted setting -// `grants` matches (modules/mesh-mail/cmd/mesh-mail/config.go, Grants.Allows), copied here in miniature (one -// flat list, not one per account) so mesh-mail-reader never imports mesh-mail. Nobody beyond the list is -// granted, a wildcard in the list is no entry, and an unproven caller never matches. The setting's default is -// ["mesh-mail-web"]. - -import ( - "encoding/json" - "errors" - "fmt" - "os" - "strings" -) - -// Unproven is the caller the runtime proved nobody to be (stdio.Unproven; copied so this file reads alone). -const Unproven = "unproven" - -// Callers is the setting's shape once read: a flat list of entries. -type Callers []string - -// callersFile is the shape of the setting as the mesh writes it. -type callersFile struct { - Callers Callers `json:"callers"` -} - -// ReadCallers reads the callers setting. A setting that cannot be read allows nobody, and says why: a caller -// refused for a reason it can read is better than one let through because the setting was unreadable. -func ReadCallers(path string) (Callers, error) { - if path == "" { - return nil, errors.New("the module was started without its callers setting (MESH_MAIL_READER_CALLERS)") - } - raw, err := os.ReadFile(path) - if err != nil { - return nil, fmt.Errorf("the callers setting cannot be read: %w", err) - } - return ParseCallers(raw) -} - -// ParseCallers reads the callers setting's content. -func ParseCallers(raw []byte) (Callers, error) { - var f callersFile - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - if err := dec.Decode(&f); err != nil { - return nil, fmt.Errorf("the callers setting is not the module's: %v", err) - } - return f.Callers, nil -} - -// Allows says whether the caller may call a mail-reading verb: a bare module name (on any machine, as long as -// the caller is not person.), exactly ., or exactly person.. Nobody is granted by -// default, an unproven caller never, and a wildcard entry is no entry (mesh-mail's own Grants.Allows mirrored). -func (c Callers) Allows(caller string) bool { - if caller == "" || caller == Unproven { - return false - } - for _, entry := range c { - if entry == "" || strings.ContainsAny(entry, "*>") { - continue - } - if entry == caller { - return true - } - if !strings.Contains(entry, ".") { - if _, module, ok := strings.Cut(caller, "."); ok && module == entry && !strings.HasPrefix(caller, "person.") { - return true - } - } - } - return false -} - -// refuseCaller says why the caller is refused a mail-reading verb: the setting unread, or simply not named in it. -func refuseCaller(readErr error, caller string) error { - if readErr != nil { - return fmt.Errorf("refused: the setting callers cannot be read, so it allows nobody: %w", readErr) - } - return fmt.Errorf("refused: %s is not named in the setting callers; the operator grants it there", caller) -} diff --git a/modules/mesh-mail-reader/cmd/mesh-mail-reader/callers_test.go b/modules/mesh-mail-reader/cmd/mesh-mail-reader/callers_test.go deleted file mode 100644 index 19349c7f1..000000000 --- a/modules/mesh-mail-reader/cmd/mesh-mail-reader/callers_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package main - -// 1426/AC4: the trusted setting callers, per shape, and an unproven caller. - -import "testing" - -func TestCallersAllowsEachShape(t *testing.T) { - c := Callers{"mesh-mail-web", "novox.mesh-knowledge", "person.jochen"} - cases := []struct { - caller string - want bool - }{ - {"mesh-mail-web", true}, // a bare module name - {"shanks.mesh-mail-web", true}, // ... on any machine - {"novox.mesh-knowledge", true}, // exactly . - {"shanks.mesh-knowledge", false}, // ... but not on another machine - {"person.jochen", true}, // exactly person. - {"person.someoneelse", false}, // ... but not another person - {"mesh-mail-notifier", false}, // not named at all - {"", false}, - {Unproven, false}, // 1426/AC4: unproven never - } - for _, c2 := range cases { - if got := c.Allows(c2.caller); got != c2.want { - t.Errorf("Allows(%q) = %v, want %v", c2.caller, got, c2.want) - } - } -} - -// 1426/AC4: a wildcard entry is no entry (mirrors mesh-mail's own Grants.Allows). -func TestCallersWildcardEntryIsNoEntry(t *testing.T) { - c := Callers{"*", "mesh-mail.>"} - if c.Allows("anything") { - t.Fatal("a wildcard entry granted a caller") - } -} - -func TestParseCallersDefault(t *testing.T) { - c, err := ParseCallers([]byte(`{"callers": ["mesh-mail-web"]}`)) - if err != nil { - t.Fatal(err) - } - if len(c) != 1 || c[0] != "mesh-mail-web" { - t.Fatalf("callers = %v", c) - } -} - -func TestReadCallersEmptyPath(t *testing.T) { - if _, err := ReadCallers(""); err == nil { - t.Fatal("no error reading callers with no path") - } -} diff --git a/modules/mesh-mail-reader/cmd/mesh-mail-reader/correct_test.go b/modules/mesh-mail-reader/cmd/mesh-mail-reader/correct_test.go index 7aed3a06b..4d55e48d0 100644 --- a/modules/mesh-mail-reader/cmd/mesh-mail-reader/correct_test.go +++ b/modules/mesh-mail-reader/cmd/mesh-mail-reader/correct_test.go @@ -9,9 +9,7 @@ import ( func testBundle(store Store, caller string) *Bundle { return &Bundle{ - Caller: func(map[string]any) string { return caller }, - callersOverride: func() (Callers, error) { return Callers{"mesh-mail-web"}, nil }, - Store: func(context.Context) (Store, error) { return store, nil }, + Store: func(context.Context) (Store, error) { return store, nil }, } } diff --git a/modules/mesh-mail-reader/cmd/mesh-mail-reader/entries_test.go b/modules/mesh-mail-reader/cmd/mesh-mail-reader/entries_test.go index 00b6c071e..3270111a2 100644 --- a/modules/mesh-mail-reader/cmd/mesh-mail-reader/entries_test.go +++ b/modules/mesh-mail-reader/cmd/mesh-mail-reader/entries_test.go @@ -115,9 +115,7 @@ func TestEntriesToolAnswersFilteredAndSorted(t *testing.T) { store.AddEntry(ctx, Entry{Account: "bob", Subject: "b", Mark: MarkMatters, DueDate: time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC), Date: time.Date(2026, 10, 2, 0, 0, 0, 0, time.UTC)}) b := &Bundle{ - Caller: func(map[string]any) string { return "mesh-mail-web" }, - callersOverride: func() (Callers, error) { return Callers{"mesh-mail-web"}, nil }, - Store: func(context.Context) (Store, error) { return store, nil }, + Store: func(context.Context) (Store, error) { return store, nil }, } out, err := b.entriesTool(map[string]any{"group": "newsletters"}) if err != nil { @@ -150,9 +148,7 @@ func TestGroupsToolAnswersLargestFirst(t *testing.T) { store.AddEntry(ctx, Entry{Group: big.ID}) b := &Bundle{ - Caller: func(map[string]any) string { return "mesh-mail-web" }, - callersOverride: func() (Callers, error) { return Callers{"mesh-mail-web"}, nil }, - Store: func(context.Context) (Store, error) { return store, nil }, + Store: func(context.Context) (Store, error) { return store, nil }, } out, err := b.groupsTool(map[string]any{}) if err != nil { @@ -172,9 +168,7 @@ func TestStatusToolAnswersHealthAndWeeklyMeasure(t *testing.T) { store.SetReaderHealth(ctx, ReaderHealth{Healthy: false, Cause: OutcomeRefused, Why: "holds no licence", Since: since}) b := &Bundle{ - Caller: func(map[string]any) string { return "mesh-mail-web" }, - callersOverride: func() (Callers, error) { return Callers{"mesh-mail-web"}, nil }, - Store: func(context.Context) (Store, error) { return store, nil }, + Store: func(context.Context) (Store, error) { return store, nil }, } out, err := b.statusTool(map[string]any{}) if err != nil { diff --git a/modules/mesh-mail-reader/cmd/mesh-mail-reader/health_test.go b/modules/mesh-mail-reader/cmd/mesh-mail-reader/health_test.go index bc0a71e1c..143fda8ac 100644 --- a/modules/mesh-mail-reader/cmd/mesh-mail-reader/health_test.go +++ b/modules/mesh-mail-reader/cmd/mesh-mail-reader/health_test.go @@ -82,9 +82,7 @@ func TestHealthToolReadsTheKeptState(t *testing.T) { // A fresh Bundle, as a restarted process would build one, reading the same store. b := &Bundle{ - Caller: func(map[string]any) string { return "mesh-mail-web" }, - callersOverride: func() (Callers, error) { return Callers{"mesh-mail-web"}, nil }, - Store: func(context.Context) (Store, error) { return store, nil }, + Store: func(context.Context) (Store, error) { return store, nil }, } got, err := b.healthTool(map[string]any{}) if err != nil { diff --git a/modules/mesh-mail-reader/cmd/mesh-mail-reader/main.go b/modules/mesh-mail-reader/cmd/mesh-mail-reader/main.go index e85b95a2d..8c53a4482 100644 --- a/modules/mesh-mail-reader/cmd/mesh-mail-reader/main.go +++ b/modules/mesh-mail-reader/cmd/mesh-mail-reader/main.go @@ -30,10 +30,6 @@ const Module = "mesh-mail-reader" func logf(format string, a ...any) { fmt.Fprintf(os.Stderr, format+"\n", a...) } -func envFromOS() Env { - return Env{Callers: os.Getenv("MESH_MAIL_READER_CALLERS")} -} - // cachedStore opens the index once, lazily, and hands every later caller that SAME store (review round 1 // finding 2, issue 1517/1518): the Bundle's own tool calls (one per mesh/ask, tools.go), hearLoop's handler // (one per event heard) and batchLoop. Before this, each of those called storeFor itself, which opened a @@ -71,7 +67,7 @@ func main() { os.Exit(prepare()) } store := newCachedStore(func(ctx context.Context) (Store, error) { return PgStoreFromEnv(ctx) }) - b := &Bundle{Env: envFromOS(), Store: store.get} + b := &Bundle{Store: store.get} go hearLoop(store.get, stdio.Subscribe, time.Sleep) go batchLoop(store.get) if err := stdio.ServeOn("", b.Tools(), os.Stdin, os.Stdout); err != nil { diff --git a/modules/mesh-mail-reader/cmd/mesh-mail-reader/runtime_player_integration_test.go b/modules/mesh-mail-reader/cmd/mesh-mail-reader/runtime_player_integration_test.go index f75322f5c..f6f429894 100644 --- a/modules/mesh-mail-reader/cmd/mesh-mail-reader/runtime_player_integration_test.go +++ b/modules/mesh-mail-reader/cmd/mesh-mail-reader/runtime_player_integration_test.go @@ -518,18 +518,12 @@ func startReader(t *testing.T) *startedReader { if err := os.WriteFile(dbPath, []byte(dbURL), 0o600); err != nil { t.Fatal(err) } - callersPath := filepath.Join(dir, "callers.json") - callersJSON, _ := json.Marshal(map[string]any{"callers": []string{"mesh-mail-web"}}) - if err := os.WriteFile(callersPath, callersJSON, 0o600); err != nil { - t.Fatal(err) - } stand := &standIns{} env := append(os.Environ(), "MESH_NODE="+testNode, "MESH_SERVED_MODULE=mesh-mail-reader", "DATABASE_URL_FILE="+dbPath, - "MESH_MAIL_READER_CALLERS="+callersPath, ) player := startReaderPlayer(t, binary, env, stand, nc, nil) return &startedReader{player: player, stand: stand, nc: nc, dbURL: dbURL} diff --git a/modules/mesh-mail-reader/cmd/mesh-mail-reader/tools.go b/modules/mesh-mail-reader/cmd/mesh-mail-reader/tools.go index 3137b7ae6..4100581d4 100644 --- a/modules/mesh-mail-reader/cmd/mesh-mail-reader/tools.go +++ b/modules/mesh-mail-reader/cmd/mesh-mail-reader/tools.go @@ -1,10 +1,9 @@ package main // The module's tools (novox/hq ADR 0459 §2, issue 1426): the six mail-reading seat verbs — entries, groups, -// correct, lessons, undo, status — and the module's own health tool, mail_reader_health (1519). Who calls is -// the runtime's word (stdio.CallerOf), never an argument; a mail-reading verb answers only a caller the -// trusted setting `callers` names (callers.go), and refuses any other, naming why. mail_reader_health is -// asked by the node-engine itself and carries no caller check. +// correct, lessons, undo, status — and the module's own health tool, mail_reader_health (1519). Every verb +// answers every caller: the module keeps no callers list (design 47 H, H-A-122; every account may call every +// verb); what protects the mail is what the verbs answer, never a body (ADR 0459 §2). // // The six verbs' own behaviour — reading and writing mail entries, mail groups and reading lessons — is // 1521 (entries, groups, status) and 1428 (correct, lessons, undo): all six now answer for real. @@ -31,46 +30,9 @@ const ( StatusTool = "mail-reading.status" ) -// Bundle is what the tools reach: the trusted setting `callers`, the runtime's own caller, and the index -// (store.go), which mail_reader_health reads (1519). Caller is nil outside a test (CallerOf is read from the -// runtime). +// Bundle is what the tools reach: the index (store.go), which mail_reader_health reads too (1519). type Bundle struct { - Env Env - Caller func(args map[string]any) string - Store func(ctx context.Context) (Store, error) - - // callersOverride, when set, is used instead of reading Env.Callers — the tests' own seam. - callersOverride func() (Callers, error) -} - -// Env is where the bundle reads its settings from. -type Env struct { - Callers string // the path MESH_MAIL_READER_CALLERS names. -} - -func (b *Bundle) caller(args map[string]any) string { - if b.Caller != nil { - return b.Caller(args) - } - return stdio.CallerOf(args) -} - -func (b *Bundle) callers() (Callers, error) { - if b.callersOverride != nil { - return b.callersOverride() - } - return ReadCallers(b.Env.Callers) -} - -// checkCaller refuses args' caller unless the trusted setting `callers` names it (1426/AC4). It is the first -// thing every mail-reading verb does: a caller learns it is refused before it learns anything about the verb. -func (b *Bundle) checkCaller(args map[string]any) (string, error) { - caller := b.caller(args) - callers, err := b.callers() - if err != nil || !callers.Allows(caller) { - return caller, refuseCaller(err, caller) - } - return caller, nil + Store func(ctx context.Context) (Store, error) } // schema, text, integer and boolean mirror mesh-mail's own tools.go helpers (modules/mesh-mail/cmd/mesh-mail, @@ -125,7 +87,7 @@ func (b *Bundle) Tools() []stdio.Tool { Description: "Every mail entry (novox/hq ADR 0459 §4): the mail's own headers, its mail group, a " + "one-line summary, its mark and why, its due date — never a mail body. Newest first, except a " + "mail that matters comes first, nearest due date first among those (none last). Filtered by mail " + - "group (its label) or by mark when asked. Answered only to a caller the setting callers names. " + + "group (its label) or by mark when asked. " + "Read-only.", Input: schema(nil, map[string]any{ "limit": integer("how many to answer; a default bounds it when left out"), @@ -135,7 +97,7 @@ func (b *Bundle) Tools() []stdio.Tool { Run: b.entriesTool}, {Name: GroupsTool, Description: "Every mail group a batch has made (novox/hq ADR 0459 §4, §5), with how many mails it " + - "holds, largest first. Answered only to a caller the setting callers names. Read-only.", + "holds, largest first. Read-only.", Input: schema(nil, map[string]any{ "limit": integer("how many to answer; a default bounds it when left out"), }), @@ -144,7 +106,7 @@ func (b *Bundle) Tools() []stdio.Tool { Description: "Correct one mail entry's mark, mail group, why or due date (novox/hq ADR 0459 §8): " + "kept at once as a reading lesson (the mail's own headers, the change made, when), no operator " + "Approve needed, and outranking that mail's own words in any later batch. Refused for an unknown " + - "mail id or mark. Answered only to a caller the setting callers names.", + "mail id or mark.", Input: schema([]string{"id"}, map[string]any{ "id": integer("the mail entry's id, as entries answered it"), "mark": text("the corrected mark: matters, scam warning or neither"), @@ -156,8 +118,7 @@ func (b *Bundle) Tools() []stdio.Tool { {Name: LessonsTool, Description: "The reading lessons kept (novox/hq ADR 0459 §9): newest first, at most 40 (fewer when " + "asked); undone ones left out unless include-undone asks for them, each then marked undone. A " + - "lesson outranks a mail it would otherwise contradict. Answered only to a caller the setting " + - "callers names. Read-only.", + "lesson outranks a mail it would otherwise contradict. Read-only.", Input: schema(nil, map[string]any{ "limit": integer("how many to answer, at most 40; a default bounds it when left out"), "include-undone": boolean("also answer undone lessons, each marked undone; left out by default"), @@ -165,7 +126,7 @@ func (b *Bundle) Tools() []stdio.Tool { Run: b.lessonsTool}, {Name: UndoTool, Description: "Undo one reading lesson (novox/hq ADR 0459 §9, issue 1523): it no longer outranks a " + - "mail. Answered only to a caller the setting callers names.", + "mail.", Input: schema([]string{"id"}, map[string]any{ "id": integer("the reading lesson's id, as lessons answered it"), }), @@ -173,7 +134,7 @@ func (b *Bundle) Tools() []stdio.Tool { {Name: StatusTool, Description: "How mail reading stands for the page (novox/hq ADR 0459 §10): healthy or not, the cause " + "and since when, and reading lessons per 100 read mails for each of the last 8 weeks (a week with " + - "no mail read says so). Answered only to a caller the setting callers names. Read-only.", + "no mail read says so). Read-only.", Input: schema(nil, map[string]any{}), Run: b.statusTool}, {Name: HealthTool, @@ -200,9 +161,6 @@ func entryOut(e Entry, labels map[int64]string) map[string]any { } func (b *Bundle) entriesTool(args map[string]any) (any, error) { - if _, err := b.checkCaller(args); err != nil { - return nil, err - } if err := only(args, "limit", "group", "mark"); err != nil { return nil, err } @@ -254,9 +212,6 @@ func (b *Bundle) entriesTool(args map[string]any) (any, error) { } func (b *Bundle) groupsTool(args map[string]any) (any, error) { - if _, err := b.checkCaller(args); err != nil { - return nil, err - } if err := only(args, "limit"); err != nil { return nil, err } @@ -294,9 +249,6 @@ func validMark(s string) (Mark, error) { } func (b *Bundle) correctTool(args map[string]any) (any, error) { - if _, err := b.checkCaller(args); err != nil { - return nil, err - } if err := only(args, "id", "mark", "group", "why", "due-date"); err != nil { return nil, err } @@ -397,9 +349,6 @@ func groupLabels(ctx context.Context, store Store) (map[int64]string, error) { } func (b *Bundle) lessonsTool(args map[string]any) (any, error) { - if _, err := b.checkCaller(args); err != nil { - return nil, err - } if err := only(args, "limit", "include-undone"); err != nil { return nil, err } @@ -443,9 +392,6 @@ func (b *Bundle) lessonsTool(args map[string]any) (any, error) { } func (b *Bundle) undoTool(args map[string]any) (any, error) { - if _, err := b.checkCaller(args); err != nil { - return nil, err - } if err := only(args, "id"); err != nil { return nil, err } @@ -473,9 +419,6 @@ func (b *Bundle) undoTool(args map[string]any) (any, error) { } func (b *Bundle) statusTool(args map[string]any) (any, error) { - if _, err := b.checkCaller(args); err != nil { - return nil, err - } if err := only(args); err != nil { return nil, err } diff --git a/modules/mesh-mail-reader/cmd/mesh-mail-reader/tools_test.go b/modules/mesh-mail-reader/cmd/mesh-mail-reader/tools_test.go index fc794f23d..68269244a 100644 --- a/modules/mesh-mail-reader/cmd/mesh-mail-reader/tools_test.go +++ b/modules/mesh-mail-reader/cmd/mesh-mail-reader/tools_test.go @@ -2,7 +2,6 @@ package main import ( "context" - "strings" "testing" ) @@ -34,76 +33,21 @@ func TestToolsListsTheSixVerbsAndHealth(t *testing.T) { } } -// 1426/AC4, 1428/AC5: a mail-reading verb answers a caller the setting callers names, and refuses any other, -// naming why — before anything else about the verb (business errors such as an unknown id are not caller -// refusals, and are exercised by correct_test.go and 1521's own tests, not here). -func TestAMailReadingVerbChecksItsCaller(t *testing.T) { - granted := func(map[string]any) string { return "mesh-mail-web" } - refused := func(map[string]any) string { return "person.someoneelse" } - unproven := func(map[string]any) string { return Unproven } - storeOf := func(context.Context) (Store, error) { return newMemStore(), nil } - +// Design 47 H, H-A-122: a mail-reading verb answers every caller, even one the runtime proved nobody to be (the +// caller of arguments no call was handed): the module keeps no callers list. +func TestAMailReadingVerbAnswersEveryCaller(t *testing.T) { + b := &Bundle{Store: func(context.Context) (Store, error) { return newMemStore(), nil }} for _, tool := range []struct { name string - run func(*Bundle) (any, error) + run func() (any, error) }{ - {EntriesTool, func(b *Bundle) (any, error) { return b.entriesTool(map[string]any{}) }}, - {GroupsTool, func(b *Bundle) (any, error) { return b.groupsTool(map[string]any{}) }}, - {CorrectTool, func(b *Bundle) (any, error) { - return b.correctTool(map[string]any{"id": float64(1), "mark": "matters"}) - }}, - {LessonsTool, func(b *Bundle) (any, error) { return b.lessonsTool(map[string]any{}) }}, - {UndoTool, func(b *Bundle) (any, error) { return b.undoTool(map[string]any{"id": float64(1)}) }}, - {StatusTool, func(b *Bundle) (any, error) { return b.statusTool(map[string]any{}) }}, + {EntriesTool, func() (any, error) { return b.entriesTool(map[string]any{}) }}, + {GroupsTool, func() (any, error) { return b.groupsTool(map[string]any{}) }}, + {LessonsTool, func() (any, error) { return b.lessonsTool(map[string]any{}) }}, + {StatusTool, func() (any, error) { return b.statusTool(map[string]any{}) }}, } { - callers := func() (Callers, error) { return Callers{"mesh-mail-web"}, nil } - - b := &Bundle{Caller: granted, callersOverride: callers, Store: storeOf} - _, err := tool.run(b) - if err != nil && strings.Contains(err.Error(), "is not named in the setting callers") { - t.Errorf("%s granted: %v, want it to pass the caller check (any other error is its own business, not a caller refusal)", tool.name, err) - } - - b = &Bundle{Caller: refused, callersOverride: callers, Store: storeOf} - _, err = tool.run(b) - if err == nil || !strings.Contains(err.Error(), "refused") || !strings.Contains(err.Error(), "person.someoneelse") { - t.Errorf("%s refused caller: %v, want a refusal naming person.someoneelse", tool.name, err) - } - - b = &Bundle{Caller: unproven, callersOverride: callers, Store: storeOf} - _, err = tool.run(b) - if err == nil || !strings.Contains(err.Error(), "refused") { - t.Errorf("%s unproven caller: %v, want a refusal", tool.name, err) + if _, err := tool.run(); err != nil { + t.Errorf("%s: %v, want an answer to any caller", tool.name, err) } } } - -// 1426/AC4: a setting that cannot be read allows nobody, and says why — before the store is ever reached. -func TestAMailReadingVerbRefusesWhenCallersCannotBeRead(t *testing.T) { - b := &Bundle{ - Caller: func(map[string]any) string { return "mesh-mail-web" }, - callersOverride: func() (Callers, error) { return nil, errFake }, - } - _, err := b.entriesTool(map[string]any{}) - if err == nil || !strings.Contains(err.Error(), "cannot be read") { - t.Fatalf("entriesTool with an unreadable setting: %v", err) - } -} - -// mail_reader_health carries no caller check (1426: "mail_reader_health is asked by the node-engine itself"). -func TestHealthToolHasNoCallerCheck(t *testing.T) { - b := &Bundle{ - callersOverride: func() (Callers, error) { return nil, errFake }, - Store: func(context.Context) (Store, error) { return newMemStore(), nil }, - } - _, err := b.healthTool(map[string]any{}) - if err != nil { - t.Fatalf("mail_reader_health: %v, want it to skip the caller check entirely and answer", err) - } -} - -type fakeErr struct{} - -func (fakeErr) Error() string { return "the setting cannot be read (fake)" } - -var errFake = fakeErr{} diff --git a/modules/mesh-mail-reader/files/callers.json b/modules/mesh-mail-reader/files/callers.json deleted file mode 100644 index 84dd80034..000000000 --- a/modules/mesh-mail-reader/files/callers.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "callers": ["mesh-mail-web"] -} diff --git a/modules/mesh-mail-reader/module.json b/modules/mesh-mail-reader/module.json index 5829145a1..5ec31bd2b 100644 --- a/modules/mesh-mail-reader/module.json +++ b/modules/mesh-mail-reader/module.json @@ -32,14 +32,6 @@ "secrets": { "postgres-database": "${dir:state}/database.secret" }, - "config": { - "callers": { - "type": "list", - "description": "Who may call one of mail-reading's six verbs (entries, groups, correct, lessons, undo, status): a module's name (on every machine), ., or person. (novox/hq ADR 0459 §2). mesh-mail-web alone by default.", - "trusted": true, - "default": ["mesh-mail-web"] - } - }, "tools": [ "mail-reading.entries", "mail-reading.groups", @@ -77,17 +69,6 @@ "place": ".", "unknown": "program" }, - { - "id": "callers", - "trusted": true, - "type": "file", - "path": "${dir:state}/callers.json", - "mode": "0600", - "owner": "mesh-mail-reader", - "merge": "json", - "template": "files/callers.json", - "takes-config": ["callers"] - }, { "id": "database-url", "type": "file", @@ -127,8 +108,7 @@ "mesh-mail-reader" ], "env": { - "DATABASE_URL_FILE": "${dir:state}/database.url", - "MESH_MAIL_READER_CALLERS": "${dir:state}/callers.json" + "DATABASE_URL_FILE": "${dir:state}/database.url" } }, { -- 2.54.0 From 01543d91211d7316b171a3a7a79657aee67c6f95 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 06:12:40 +0100 Subject: [PATCH 38/43] gate: gate_record_end takes an all form for the clean state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every open record ends after one hand act naming how many; the answer says what it ended, counted. Builds: design 47 H H-A-121 (2), §765 (the gate's open records go at the clean state) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../cmd/mesh-delivery-gate/record_test.go | 36 ++++++++++++ .../cmd/mesh-delivery-gate/recordtools.go | 56 ++++++++++++++++++- 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go index 204dd6c22..487886b12 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go @@ -593,6 +593,42 @@ func TestTheRecordsToolsReadThemAndEndOneByHand(t *testing.T) { t.Fatal("a record ended is ended again") } }) + // Design 47 H, H-A-121: the clean state's all form ends every open record after one hand act, and answers them counted. + t.Run("gate_record_end with all ends every open record once the hand-act log took it, counted", func(t *testing.T) { + rs, r, said := setup(t) + other, _, err := rs.Open(opening("main", 48, map[string]string{"novox/app@222222222222": strings.Repeat("2", 40)})) + if err != nil { + t.Fatal(err) + } + log := &handActsFake{refuse: errors.New("no responders")} + if _, err := rs.EndAllByHand("", "jochen@shanks", log); err == nil { + t.Fatal("ended all with no reason") + } + if _, err := rs.EndAllByHand("the clean state", "jochen@shanks", log); err == nil { + t.Fatal("ended all though the hand-act log refused it") + } + for _, inst := range []string{r.Instance, other.Instance} { + if back, _ := rs.Read(inst); back.Ended != nil { + t.Fatalf("%s ended while no hand act was recorded", inst) + } + } + log.refuse = nil + got, err := rs.EndAllByHand("the clean state", "jochen@shanks", log) + if err != nil || got["count"] != 2 || len(got["ended"].([]string)) != 2 || len(log.took) != 1 { + t.Fatalf("%+v %v (log %+v)", got, err, log.took) + } + for _, inst := range []string{r.Instance, other.Instance} { + if back, _ := rs.Read(inst); back.Ended == nil || back.Ended.Why != "the clean state" { + t.Fatalf("%s not ended: %+v", inst, back.Ended) + } + if !slices.Contains(*said, meshdelivery.SubjectRecordEnded+" record-ended."+inst) { + t.Fatalf("%s's end is not said: %v", inst, *said) + } + } + if again, err := rs.EndAllByHand("again", "jochen@shanks", log); err != nil || again["count"] != 0 || len(log.took) != 1 { + t.Fatalf("with none open: %+v %v, log %d", again, err, len(log.took)) + } + }) t.Run("gate_record_repair asks the owners for the outcomes an entry lacks, applies what they answer, and ends the record", func(t *testing.T) { rs, r, said := setup(t) owners := &ownersFake{wanted: "src2:f1", fp: "src2:f1", phase: "running"} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordtools.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordtools.go index 531f078ed..3437a9907 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordtools.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordtools.go @@ -139,6 +139,44 @@ func (rs *Records) EndByHand(instance, why, by string, acts handActs) (recordVie return viewOf(r), nil } +// EndAllByHand ends every open record by hand, with the reason (design 47 H, H-A-121: the clean state's `all` form), +// recorded in the hand-act log first as one act naming how many: nothing is ended when the log did not take it. It answers +// what it ended, counted; a record that could not be ended stops it, said with what was ended before it. +func (rs *Records) EndAllByHand(why, by string, acts handActs) (map[string]any, error) { + if strings.TrimSpace(why) == "" { + return nil, errors.New("say why every open record is ended by hand, such as the clean state") + } + list, err := rs.List() + if err != nil { + return nil, err + } + var open []*Record + for _, r := range list { + if r.Ended == nil { + open = append(open, r) + } + } + ended := []string{} + if len(open) == 0 { + return map[string]any{"ended": ended, "count": 0}, nil + } + if acts == nil { + return nil, errors.New("the hand-act log cannot be reached here, so nothing was ended") + } + if _, err := acts.Record(meshhandactsclient.Request{What: fmt.Sprintf("ended every open delivery record by hand (%d)", len(open)), + Why: why, Cause: "gate_record_end", Verb: "record-end"}); err != nil { + return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was ended: %w", err) + } + for _, r := range open { + if err := rs.End(r, why, by); err != nil { + return map[string]any{"ended": ended, "count": len(ended)}, fmt.Errorf("the record %s was not ended, after %d were: %w", r.Instance, len(ended), err) + } + rs.keep(r) + ended = append(ended, r.Instance) + } + return map[string]any{"ended": ended, "count": len(ended)}, nil +} + // handActs records an act in the hand-act log (meshhandactsclient.Recorder). type handActs interface { Record(q meshhandactsclient.Request) (meshhandactsclient.Taken, error) @@ -196,14 +234,26 @@ func recordTools(h *Holder, acts handActs) []stdio.Tool { }}, {Name: "gate_record_end", Description: "End a delivery record left open by hand (a node gone for good, an entry that never runs), with the " + - "reason; recorded in the hand-act log of the seat mesh-hand-acts first, and said as record-ended.", + "reason; or, with all, every open record (the clean state), answering what it ended, counted. Recorded in the " + + "hand-act log of the seat mesh-hand-acts first, and each said as record-ended.", Input: map[string]any{"type": "object", "properties": map[string]any{ - "instance": str("the record's instance"), "why": str("why it is ended by hand, in plain words")}, - "required": []string{"instance", "why"}}, + "instance": str("the record's instance; or give all"), + "all": map[string]any{"type": "boolean", "description": "end every open record instead of one"}, + "why": str("why it is ended by hand, in plain words")}, + "required": []string{"why"}}, Run: func(a map[string]any) (any, error) { if h.Records == nil { return none() } + all, _ := a["all"].(bool) + switch instance := strArg(a, "instance"); { + case all && instance != "": + return nil, errors.New("give an instance or all, not both") + case all: + return h.Records.EndAllByHand(strArg(a, "why"), stdio.CallerOf(a), acts) + case instance == "": + return nil, errors.New("give the record's instance, or all for every open record") + } return h.Records.EndByHand(strArg(a, "instance"), strArg(a, "why"), stdio.CallerOf(a), acts) }}, } -- 2.54.0 From 959e8d6d302468b70b21e08ca5110dd076570cba Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 06:20:30 +0100 Subject: [PATCH 39/43] nats-tools: nats_consumer_remove judges a consumer by its name and records the hand act An accepted module's durable (_) is refused on any stream; any other durable is an orphan and goes, why its warrant, recorded through mesh-hand-acts. It no longer refuses every consumer of a kept-data stream. Builds: design 47 H H-A-126, H-A-127 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/nats/cmd/nats-tools/main.go | 2 +- modules/nats/cmd/nats-tools/removal.go | 50 +++++++++++++++++++-- modules/nats/cmd/nats-tools/removal_test.go | 14 +++++- modules/nats/cmd/nats-tools/tools_test.go | 18 +++++++- 4 files changed, 77 insertions(+), 7 deletions(-) diff --git a/modules/nats/cmd/nats-tools/main.go b/modules/nats/cmd/nats-tools/main.go index 9b43093f5..c2c000df5 100644 --- a/modules/nats/cmd/nats-tools/main.go +++ b/modules/nats/cmd/nats-tools/main.go @@ -191,7 +191,7 @@ func allTools() []stdio.Tool { defer cancel() return monitor(ctx, heldJsz) } - out := append(tools(monitor, ReadUsers, UsersWritten), removalTools(LiveBus(), SaidByTheRuntime, HeldInState)...) + out := append(tools(monitor, ReadUsers, UsersWritten), removalTools(LiveBus(), SaidByTheRuntime, HeldInState, RecordedBySeat)...) out = append(out, expiryTools(LiveBus(), SaidByTheRuntime, RecordedBySeat)...) out = append(out, brokerTools(HeldInState, StatusInState, jsz, StreamHistory(LiveBus()))...) out = append(out, keeperHealthTool(StatusInState, HeldInState, jsz)) diff --git a/modules/nats/cmd/nats-tools/removal.go b/modules/nats/cmd/nats-tools/removal.go index d6a9e0ae5..81597d4d0 100644 --- a/modules/nats/cmd/nats-tools/removal.go +++ b/modules/nats/cmd/nats-tools/removal.go @@ -20,6 +20,7 @@ import ( "strings" "time" + "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" stdio "git.novox.be/novox/mesh-sdk/go" "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" @@ -338,7 +339,30 @@ func RemoveStream(ctx context.Context, bus Bus, said Said, a map[string]any) (an // recreate, so a stream, a bucket or a consumer of a stream the keeper made (its state asked, read through newForm) is // refused, naming nats_drop, before the bus is reached; the old sections' entries are removed as before. A newForm // that cannot be read refuses too: what cannot be told apart is not removed. -func removalTools(bus Bus, said Said, newForm heldEntries) []stdio.Tool { +// +// **A consumer is judged by its name, not by its stream** (design 47 H, H-A-126, H-A-127): the mesh names a module's +// durable _, so a durable of that form for a module of an accepted manifest (the keeper's accepted set) is +// refused, on any stream; every other durable is an orphan and goes, `why` its warrant, at once, and its removal is +// recorded as a hand act through mesh-hand-acts, as nats_bucket_expiry's change is. +func removalTools(bus Bus, said Said, newForm heldEntries, record Recorded) []stdio.Tool { + removeConsumer := func(a map[string]any) (any, error) { + if err := refuseAcceptedDurable(newForm, arg(a, "consumer")); err != nil { + return nil, err + } + out, err := RemoveConsumer(context.Background(), bus, said, a) + if err != nil { + return nil, err + } + r := out.(ConsumerRemoved) + what := fmt.Sprintf("removed the consumer %s of the stream %s", r.Consumer, r.Stream) + if record == nil { + return nil, fmt.Errorf("%s WAS done (%s) but the hand-act log cannot be reached here", what, r.Why) + } + if err := record(meshhandactsclient.Request{What: what, Why: r.Why, Cause: "consumer-remove"}); err != nil { + return nil, fmt.Errorf("%s WAS done (%s) but the hand act could not be recorded: %w", what, r.Why, err) + } + return r, nil + } run := func(f func(context.Context, Bus, Said, map[string]any) (any, error)) func(map[string]any) (any, error) { return func(a map[string]any) (any, error) { stream := arg(a, "stream") @@ -353,9 +377,9 @@ func removalTools(bus Bus, said Said, newForm heldEntries) []stdio.Tool { } return []stdio.Tool{ {Name: "nats_consumer_remove", - Description: "Removes (deletes) one named JetStream consumer of one stream, e.g. a consumer the controller left, such as `controller` of DEAD_LETTER_NOTICES or `controller-merges` of EVENTS (novox/hq ADR 0590 §6, recorded as a hand act under issue 1126). Needs `why`, kept in the audit trail; refuses an unknown stream or consumer; reads the consumer first and answers what it was (filters, pending, unacknowledged); the stream and its other consumers are not touched. The bus refuses any consumer the module's credential is not granted.", + Description: "Removes (deletes) one named JetStream consumer of one stream, e.g. a consumer the controller left, such as `controller` of DEAD_LETTER_NOTICES or `controller-merges` of EVENTS (novox/hq ADR 0590 §6), or an orphan on a module's kept-data stream. Refuses a durable named _ for a module of an accepted manifest, on any stream; any other durable goes. Needs `why`, its warrant, kept in the audit trail and recorded as a hand act through mesh-hand-acts (design 47 H, H-A-127); refuses an unknown stream or consumer; reads the consumer first and answers what it was (filters, pending, unacknowledged); the stream and its other consumers are not touched. The bus refuses any consumer the module's credential is not granted.", Input: map[string]any{"stream": str("the stream, e.g. DEAD_LETTER_NOTICES"), "consumer": str("the consumer's name, e.g. controller"), "why": str("why it is removed, for the audit trail (required)")}, - Run: run(RemoveConsumer)}, + Run: removeConsumer}, {Name: "nats_stream_remove", Description: "Removes (deletes) one named JetStream stream whole, e.g. a stream the controller left: CONTROL, ASSIGNMENTS, one of its buckets as KV_ or a node's declaration files as OBJ_declaration-files- (novox/hq ADR 0590 §6; a bucket's whole removal is this tool's, never nats_bucket_remove's). Needs `why`, kept in the audit trail; reads the stream first and answers its messages, bytes and consumers; refuses a stream that still has a consumer; a stream already gone is answered as such and is no error, so asking again is safe. `dry_run` only reads. The bus refuses any stream the module's credential is not granted.", Input: map[string]any{"stream": str("the stream, e.g. ASSIGNMENTS"), "why": str("why it is removed, for the audit trail (required)"), "dry_run": map[string]any{"type": "boolean", "description": "read the stream, remove nothing"}}, @@ -373,6 +397,26 @@ func removalTools(bus Bus, said Said, newForm heldEntries) []stdio.Tool { } } +// refuseAcceptedDurable refuses a durable named _ for a module of the keeper's accepted set (design 47 H, +// H-A-127); a set that cannot be read refuses too: what cannot be told apart is not removed. +func refuseAcceptedDurable(newForm heldEntries, consumer string) error { + if newForm == nil { + return nil + } + node, module, ok := strings.Cut(consumer, "_") + if !ok || node == "" || module == "" { + return nil + } + held, err := newForm() + if err != nil { + return fmt.Errorf("whether %s is the durable of a module of an accepted manifest cannot be read (%v), so nothing was removed", consumer, err) + } + if _, ok := held[module]; ok { + return fmt.Errorf("%s is %s's durable on %s, a module of an accepted manifest: it is not removed; nothing was removed", consumer, module, node) + } + return nil +} + // refuseNewForm refuses a stream the keeper made for a module's bus state, naming the tool that removes it. func refuseNewForm(newForm heldEntries, stream string) error { if newForm == nil || stream == "" { diff --git a/modules/nats/cmd/nats-tools/removal_test.go b/modules/nats/cmd/nats-tools/removal_test.go index 680fee08c..3e5d47433 100644 --- a/modules/nats/cmd/nats-tools/removal_test.go +++ b/modules/nats/cmd/nats-tools/removal_test.go @@ -5,6 +5,7 @@ package main import ( "context" "fmt" + "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" "os" "path/filepath" "sort" @@ -191,6 +192,14 @@ accounts { MESH { jetstream: enabled, users: [ } // Issue 1126: the controller's leftovers were push consumers too, which js.Consumer refuses ("not a pull consumer"): // a pull and a push consumer are read, removed and confirmed alike. + // Through the tool, `why` its warrant: each removal is recorded as a hand act (design 47 H, H-A-127). + var handActs []meshhandactsclient.Request + var removeConsumer func(map[string]any) (any, error) + for _, tl := range removalTools(bus, say, nil, func(q meshhandactsclient.Request) error { handActs = append(handActs, q); return nil }) { + if tl.Name == "nats_consumer_remove" { + removeConsumer = tl.Run + } + } for _, row := range []struct{ name, kind, deliver string }{ {"controller-merges", "pull", ""}, {"controller", "push", "deliver.controller"}, @@ -200,7 +209,7 @@ accounts { MESH { jetstream: enabled, users: [ t.Fatal(err) } } - got, err := RemoveConsumer(ctx, bus, say, map[string]any{"stream": "EVENTS", "consumer": row.name, "why": "the controller ended"}) + got, err := removeConsumer(map[string]any{"stream": "EVENTS", "consumer": row.name, "why": "the controller ended"}) if err != nil { t.Fatalf("the controller's %s consumer %s of EVENTS: %v", row.kind, row.name, err) } @@ -214,6 +223,9 @@ accounts { MESH { jetstream: enabled, users: [ if _, err := ajs.Consumer(ctx, "EVENTS", "mesh-grants"); err != nil { t.Fatalf("the other consumer of EVENTS went: %v", err) } + if len(handActs) != 2 || handActs[0].Why != "the controller ended" || !strings.Contains(handActs[1].What, "controller of the stream EVENTS") { + t.Fatalf("the hand acts: %+v", handActs) + } if r, err := RemoveStream(ctx, bus, say, map[string]any{"stream": "KV_mesh-controller_rollouts", "why": "the controller ended"}); err != nil || !r.(StreamRemoved).Removed || r.(StreamRemoved).Messages != 1 { t.Fatalf("the controller's bucket: %+v %v", r, err) } diff --git a/modules/nats/cmd/nats-tools/tools_test.go b/modules/nats/cmd/nats-tools/tools_test.go index ce2b827d1..31880ebec 100644 --- a/modules/nats/cmd/nats-tools/tools_test.go +++ b/modules/nats/cmd/nats-tools/tools_test.go @@ -255,10 +255,12 @@ func TestTheRemovalToolsRefuseBeforeTheyReachTheBus(t *testing.T) { // CAT-N9: an entry of the new form goes only by drop or recreate; an old section's goes as before. {"nats_stream_remove", map[string]any{"stream": "m_log", "why": "x"}, "removed only by nats_drop"}, {"nats_bucket_remove", map[string]any{"bucket": "m_drafts", "node": "g14", "why": "x"}, "m's kept data \"drafts\""}, - {"nats_consumer_remove", map[string]any{"stream": "m_log", "consumer": "c", "why": "x"}, "removed only by nats_drop"}, + // Design 47 H, H-A-127: a consumer is judged by its name: an accepted module's durable is refused on any stream. + {"nats_consumer_remove", map[string]any{"stream": "EVENTS", "consumer": "novox_m", "why": "x"}, "a module of an accepted manifest"}, + {"nats_consumer_remove", map[string]any{"stream": "m_log", "consumer": "ace_m", "why": "x"}, "a module of an accepted manifest"}, } { var run func(map[string]any) (any, error) - for _, tl := range removalTools(bus, said, newForm) { + for _, tl := range removalTools(bus, said, newForm, nil) { if tl.Name == c.tool { run = tl.Run } @@ -270,6 +272,18 @@ func TestTheRemovalToolsRefuseBeforeTheyReachTheBus(t *testing.T) { t.Errorf("%s %v: %v, want a refusal saying %q", c.tool, c.args, err, c.want) } } + // An orphan on a module's kept-data stream is not refused by its stream (H-A-126): the call reaches the bus. + unreachable := func(context.Context) (jetstream.JetStream, func(), error) { + return nil, nil, errors.New("the bus is not reached here") + } + for _, tl := range removalTools(unreachable, said, newForm, nil) { + if tl.Name != "nats_consumer_remove" { + continue + } + if _, err := tl.Run(map[string]any{"stream": "m_log", "consumer": "c", "why": "an orphan"}); err == nil || !strings.Contains(err.Error(), "not reached") { + t.Errorf("an orphan on m_log: %v, want the call to reach the bus", err) + } + } // The read tools stay read tools: none of the first ten takes a `why`, and the list is these ten. for _, tl := range tools(nil, nil, nil) { if _, ok := tl.Input["why"]; ok || strings.Contains(tl.Name, "remove") { -- 2.54.0 From 04eda6130d595e7d346f39be7c01b1868eef57d0 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 06:20:40 +0100 Subject: [PATCH 40/43] mesh-cli: nox checks goes; the seat mesh-delivery no longer serves checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds: design 47 H §764 (the checker and its verbs retire) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/mesh-cli/cmd/mesh-cli/commands.go | 9 --------- modules/mesh-cli/cmd/mesh-cli/main_test.go | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/modules/mesh-cli/cmd/mesh-cli/commands.go b/modules/mesh-cli/cmd/mesh-cli/commands.go index 968bcbc6e..d28149b3a 100644 --- a/modules/mesh-cli/cmd/mesh-cli/commands.go +++ b/modules/mesh-cli/cmd/mesh-cli/commands.go @@ -167,15 +167,6 @@ var commands = []command{ }}, {words: []string{"delivery"}, usage: "", does: "one delivery or group whole", build: needArg("mesh-delivery.show", "id")}, {words: []string{"stalled"}, does: "every delivery held past its state's bound", build: fixed("mesh-delivery.stalled")}, - {words: []string{"checks"}, usage: " [--number n | --commit c]", does: "what the mesh's checks said of a head", - allowed: []string{"number", "commit"}, build: func(l line) (call, error) { - if len(l.pos) != 1 { - return call{}, usageErr("give the repository, owner/repo") - } - a := pick(l, "number", "commit") - a["repository"] = l.pos[0] - return call{"mesh-delivery.checks", a}, nil - }}, {words: []string{"builds"}, usage: "[--since ]", does: "every build that ended since (the last day by default)", allowed: []string{"since"}, build: func(l line) (call, error) { since := l.flags["since"] diff --git a/modules/mesh-cli/cmd/mesh-cli/main_test.go b/modules/mesh-cli/cmd/mesh-cli/main_test.go index 155f7ec87..df8821545 100644 --- a/modules/mesh-cli/cmd/mesh-cli/main_test.go +++ b/modules/mesh-cli/cmd/mesh-cli/main_test.go @@ -102,7 +102,6 @@ func TestEachLineCallsItsOwner(t *testing.T) { {"unpin ace postgres-database --why back", "mesh-module-assigner.propose", `{"changes":[{"node":"ace","op":"unpin","provision":"postgres-database"}],"why":"back"}`}, {"deliveries --state merged --json", "mesh-delivery.deliveries", `{"state":"merged"}`}, {"delivery novox/mesh-sdk@dff0ce2abb21", "mesh-delivery.show", `{"id":"novox/mesh-sdk@dff0ce2abb21"}`}, - {"checks novox/mesh-catalog --number 591", "mesh-delivery.checks", `{"number":"591","repository":"novox/mesh-catalog"}`}, {"builds", "mesh-delivery-builder.builder_builds_since", `{"since":"2026-11-01T10:00:00Z"}`}, {"upgrade nats --policy record --why bus", "mesh-wants.upgrade", `{"module":"nats","policy":"record","why":"bus"}`}, {"bus", "mesh-wants.bus", `{}`}, @@ -179,6 +178,7 @@ func TestALineWithNoOwnerOrNotReadCallsNothingAndSaysWhy(t *testing.T) { // The checker and the groups go whole (design 47 H §764): nox calls neither. {"try novox/mesh-catalog abc123 --level unit", 2, "is not a command"}, {"groups", 2, "is not a command"}, + {"checks novox/mesh-catalog --number 591", 2, "is not a command"}, } { got := console(t, nil) args := strings.Fields(c.line) -- 2.54.0 From 1568877f357d3dde95f940811d45cfd5332a4ef2 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 06:22:51 +0100 Subject: [PATCH 41/43] nats-tools: a durable beginning __ is the module's too Builds: design 47 H H-A-128 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/nats/cmd/nats-tools/removal.go | 12 +++++++----- modules/nats/cmd/nats-tools/tools_test.go | 8 ++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/modules/nats/cmd/nats-tools/removal.go b/modules/nats/cmd/nats-tools/removal.go index 81597d4d0..e179a3e43 100644 --- a/modules/nats/cmd/nats-tools/removal.go +++ b/modules/nats/cmd/nats-tools/removal.go @@ -377,7 +377,7 @@ func removalTools(bus Bus, said Said, newForm heldEntries, record Recorded) []st } return []stdio.Tool{ {Name: "nats_consumer_remove", - Description: "Removes (deletes) one named JetStream consumer of one stream, e.g. a consumer the controller left, such as `controller` of DEAD_LETTER_NOTICES or `controller-merges` of EVENTS (novox/hq ADR 0590 §6), or an orphan on a module's kept-data stream. Refuses a durable named _ for a module of an accepted manifest, on any stream; any other durable goes. Needs `why`, its warrant, kept in the audit trail and recorded as a hand act through mesh-hand-acts (design 47 H, H-A-127); refuses an unknown stream or consumer; reads the consumer first and answers what it was (filters, pending, unacknowledged); the stream and its other consumers are not touched. The bus refuses any consumer the module's credential is not granted.", + Description: "Removes (deletes) one named JetStream consumer of one stream, e.g. a consumer the controller left, such as `controller` of DEAD_LETTER_NOTICES or `controller-merges` of EVENTS (novox/hq ADR 0590 §6), or an orphan on a module's kept-data stream. Refuses a durable named _ or beginning __ for a module of an accepted manifest, on any stream; any other durable goes. Needs `why`, its warrant, kept in the audit trail and recorded as a hand act through mesh-hand-acts (design 47 H, H-A-127); refuses an unknown stream or consumer; reads the consumer first and answers what it was (filters, pending, unacknowledged); the stream and its other consumers are not touched. The bus refuses any consumer the module's credential is not granted.", Input: map[string]any{"stream": str("the stream, e.g. DEAD_LETTER_NOTICES"), "consumer": str("the consumer's name, e.g. controller"), "why": str("why it is removed, for the audit trail (required)")}, Run: removeConsumer}, {Name: "nats_stream_remove", @@ -397,8 +397,8 @@ func removalTools(bus Bus, said Said, newForm heldEntries, record Recorded) []st } } -// refuseAcceptedDurable refuses a durable named _ for a module of the keeper's accepted set (design 47 H, -// H-A-127); a set that cannot be read refuses too: what cannot be told apart is not removed. +// refuseAcceptedDurable refuses a durable named _, or beginning __, for a module of the +// keeper's accepted set, on any node (design 47 H, H-A-127, H-A-128); a set that cannot be read refuses too: what cannot be told apart is not removed. func refuseAcceptedDurable(newForm heldEntries, consumer string) error { if newForm == nil { return nil @@ -411,8 +411,10 @@ func refuseAcceptedDurable(newForm heldEntries, consumer string) error { if err != nil { return fmt.Errorf("whether %s is the durable of a module of an accepted manifest cannot be read (%v), so nothing was removed", consumer, err) } - if _, ok := held[module]; ok { - return fmt.Errorf("%s is %s's durable on %s, a module of an accepted manifest: it is not removed; nothing was removed", consumer, module, node) + for m := range held { + if module == m || strings.HasPrefix(module, m+"_") { + return fmt.Errorf("%s is %s's durable on %s, a module of an accepted manifest: it is not removed; nothing was removed", consumer, m, node) + } } return nil } diff --git a/modules/nats/cmd/nats-tools/tools_test.go b/modules/nats/cmd/nats-tools/tools_test.go index 31880ebec..053b57655 100644 --- a/modules/nats/cmd/nats-tools/tools_test.go +++ b/modules/nats/cmd/nats-tools/tools_test.go @@ -258,6 +258,8 @@ func TestTheRemovalToolsRefuseBeforeTheyReachTheBus(t *testing.T) { // Design 47 H, H-A-127: a consumer is judged by its name: an accepted module's durable is refused on any stream. {"nats_consumer_remove", map[string]any{"stream": "EVENTS", "consumer": "novox_m", "why": "x"}, "a module of an accepted manifest"}, {"nats_consumer_remove", map[string]any{"stream": "m_log", "consumer": "ace_m", "why": "x"}, "a module of an accepted manifest"}, + // H-A-128: a durable beginning __ is the module's too. + {"nats_consumer_remove", map[string]any{"stream": "EVENTS", "consumer": "novox_m_keeper", "why": "x"}, "a module of an accepted manifest"}, } { var run func(map[string]any) (any, error) for _, tl := range removalTools(bus, said, newForm, nil) { @@ -280,8 +282,10 @@ func TestTheRemovalToolsRefuseBeforeTheyReachTheBus(t *testing.T) { if tl.Name != "nats_consumer_remove" { continue } - if _, err := tl.Run(map[string]any{"stream": "m_log", "consumer": "c", "why": "an orphan"}); err == nil || !strings.Contains(err.Error(), "not reached") { - t.Errorf("an orphan on m_log: %v, want the call to reach the bus", err) + for _, orphan := range []string{"c", "novox_mx", "novox_mx_keeper"} { + if _, err := tl.Run(map[string]any{"stream": "m_log", "consumer": orphan, "why": "an orphan"}); err == nil || !strings.Contains(err.Error(), "not reached") { + t.Errorf("the orphan %s on m_log: %v, want the call to reach the bus", orphan, err) + } } } // The read tools stay read tools: none of the first ten takes a `why`, and the list is these ten. -- 2.54.0 From ae8b0c65bcd18557a6fc6ca22c9310973b41920c Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 06:35:44 +0100 Subject: [PATCH 42/43] NATS-R2: nats_consumer_remove judges durables by every accepted module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The refusal read the keeper's state asked, which holds only a module that asks for kept data, so a running module keeping no bus data would lose its live durables. It now reads the keeper's state plans, one key per module of a watched branch's newest revision; the keeper no longer deletes a plan key when a module's asked key goes, so plans stays that accepted set. Builds: design 47 H H-A-126, H-A-127, H-A-128; design 47 G §538 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/nats/cmd/nats-tools/main.go | 2 +- modules/nats/cmd/nats-tools/removal.go | 25 ++++++++++++++------- modules/nats/cmd/nats-tools/removal_test.go | 2 +- modules/nats/cmd/nats-tools/tools_test.go | 12 ++++++++-- modules/nats/internal/keeper/keeper.go | 5 +++-- modules/nats/internal/keeper/keeper_test.go | 24 ++++++++++++++++++++ 6 files changed, 56 insertions(+), 14 deletions(-) diff --git a/modules/nats/cmd/nats-tools/main.go b/modules/nats/cmd/nats-tools/main.go index c2c000df5..d84305c97 100644 --- a/modules/nats/cmd/nats-tools/main.go +++ b/modules/nats/cmd/nats-tools/main.go @@ -191,7 +191,7 @@ func allTools() []stdio.Tool { defer cancel() return monitor(ctx, heldJsz) } - out := append(tools(monitor, ReadUsers, UsersWritten), removalTools(LiveBus(), SaidByTheRuntime, HeldInState, RecordedBySeat)...) + out := append(tools(monitor, ReadUsers, UsersWritten), removalTools(LiveBus(), SaidByTheRuntime, HeldInState, PlansInState, RecordedBySeat)...) out = append(out, expiryTools(LiveBus(), SaidByTheRuntime, RecordedBySeat)...) out = append(out, brokerTools(HeldInState, StatusInState, jsz, StreamHistory(LiveBus()))...) out = append(out, keeperHealthTool(StatusInState, HeldInState, jsz)) diff --git a/modules/nats/cmd/nats-tools/removal.go b/modules/nats/cmd/nats-tools/removal.go index e179a3e43..c1fd5c86e 100644 --- a/modules/nats/cmd/nats-tools/removal.go +++ b/modules/nats/cmd/nats-tools/removal.go @@ -24,6 +24,8 @@ import ( stdio "git.novox.be/novox/mesh-sdk/go" "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" + + "nats-tools/internal/keeper" ) // Bus opens JetStream on the bus as the module's credential for one call; close ends the connection. @@ -341,12 +343,13 @@ func RemoveStream(ctx context.Context, bus Bus, said Said, a map[string]any) (an // that cannot be read refuses too: what cannot be told apart is not removed. // // **A consumer is judged by its name, not by its stream** (design 47 H, H-A-126, H-A-127): the mesh names a module's -// durable _, so a durable of that form for a module of an accepted manifest (the keeper's accepted set) is -// refused, on any stream; every other durable is an orphan and goes, `why` its warrant, at once, and its removal is +// durable _, so a durable of that form for a module of an accepted manifest (the keeper's accepted set: +// every module it heard as accepted, its state plans, one key per module of a watched branch's newest revision whether +// it asks for kept data or not, design 47 G §538) is refused, on any stream; every other durable is an orphan and goes, `why` its warrant, at once, and its removal is // recorded as a hand act through mesh-hand-acts, as nats_bucket_expiry's change is. -func removalTools(bus Bus, said Said, newForm heldEntries, record Recorded) []stdio.Tool { +func removalTools(bus Bus, said Said, newForm heldEntries, accepted acceptedModules, record Recorded) []stdio.Tool { removeConsumer := func(a map[string]any) (any, error) { - if err := refuseAcceptedDurable(newForm, arg(a, "consumer")); err != nil { + if err := refuseAcceptedDurable(accepted, arg(a, "consumer")); err != nil { return nil, err } out, err := RemoveConsumer(context.Background(), bus, said, a) @@ -398,16 +401,19 @@ func removalTools(bus Bus, said Said, newForm heldEntries, record Recorded) []st } // refuseAcceptedDurable refuses a durable named _, or beginning __, for a module of the -// keeper's accepted set, on any node (design 47 H, H-A-127, H-A-128); a set that cannot be read refuses too: what cannot be told apart is not removed. -func refuseAcceptedDurable(newForm heldEntries, consumer string) error { - if newForm == nil { +// keeper's accepted set, on any node (design 47 H, H-A-127, H-A-128): every module it heard as accepted, read from its +// state plans (design 47 G §538), never from its state asked, which holds only a module that asks for kept data (a +// module keeping none would lose its live durables: H-A-128's silent failure, P3). A set that cannot be read refuses +// too: what cannot be told apart is not removed. +func refuseAcceptedDurable(accepted acceptedModules, consumer string) error { + if accepted == nil { return nil } node, module, ok := strings.Cut(consumer, "_") if !ok || node == "" || module == "" { return nil } - held, err := newForm() + held, err := accepted() if err != nil { return fmt.Errorf("whether %s is the durable of a module of an accepted manifest cannot be read (%v), so nothing was removed", consumer, err) } @@ -419,6 +425,9 @@ func refuseAcceptedDurable(newForm heldEntries, consumer string) error { return nil } +// acceptedModules reads the keeper's accepted set: every module it heard as accepted, by name (its state plans, §538). +type acceptedModules func() (map[string]keeper.Planned, error) + // refuseNewForm refuses a stream the keeper made for a module's bus state, naming the tool that removes it. func refuseNewForm(newForm heldEntries, stream string) error { if newForm == nil || stream == "" { diff --git a/modules/nats/cmd/nats-tools/removal_test.go b/modules/nats/cmd/nats-tools/removal_test.go index 3e5d47433..f924f8f02 100644 --- a/modules/nats/cmd/nats-tools/removal_test.go +++ b/modules/nats/cmd/nats-tools/removal_test.go @@ -195,7 +195,7 @@ accounts { MESH { jetstream: enabled, users: [ // Through the tool, `why` its warrant: each removal is recorded as a hand act (design 47 H, H-A-127). var handActs []meshhandactsclient.Request var removeConsumer func(map[string]any) (any, error) - for _, tl := range removalTools(bus, say, nil, func(q meshhandactsclient.Request) error { handActs = append(handActs, q); return nil }) { + for _, tl := range removalTools(bus, say, nil, nil, func(q meshhandactsclient.Request) error { handActs = append(handActs, q); return nil }) { if tl.Name == "nats_consumer_remove" { removeConsumer = tl.Run } diff --git a/modules/nats/cmd/nats-tools/tools_test.go b/modules/nats/cmd/nats-tools/tools_test.go index 053b57655..bc233d76e 100644 --- a/modules/nats/cmd/nats-tools/tools_test.go +++ b/modules/nats/cmd/nats-tools/tools_test.go @@ -234,6 +234,11 @@ func TestTheRemovalToolsRefuseBeforeTheyReachTheBus(t *testing.T) { } return map[string]keeper.Asked{"m": {Module: "m", Made: made}}, nil } + // The keeper's accepted set (its state plans, §538): m, which asks for kept data, and quiet, which asks for none and + // so has no key in asked (review round 2: a module keeping no bus data still has live durables). + accepted := func() (map[string]keeper.Planned, error) { + return map[string]keeper.Planned{"m": {Module: "m"}, "quiet": {Module: "quiet"}}, nil + } for _, c := range []struct { tool string args map[string]any @@ -260,9 +265,12 @@ func TestTheRemovalToolsRefuseBeforeTheyReachTheBus(t *testing.T) { {"nats_consumer_remove", map[string]any{"stream": "m_log", "consumer": "ace_m", "why": "x"}, "a module of an accepted manifest"}, // H-A-128: a durable beginning __ is the module's too. {"nats_consumer_remove", map[string]any{"stream": "EVENTS", "consumer": "novox_m_keeper", "why": "x"}, "a module of an accepted manifest"}, + // H-A-128: an accepted module that asks for no kept data is refused too, its durables and its suffixed ones. + {"nats_consumer_remove", map[string]any{"stream": "EVENTS", "consumer": "novox_quiet", "why": "x"}, "a module of an accepted manifest"}, + {"nats_consumer_remove", map[string]any{"stream": "EVENTS", "consumer": "novox_quiet_tools", "why": "x"}, "a module of an accepted manifest"}, } { var run func(map[string]any) (any, error) - for _, tl := range removalTools(bus, said, newForm, nil) { + for _, tl := range removalTools(bus, said, newForm, accepted, nil) { if tl.Name == c.tool { run = tl.Run } @@ -278,7 +286,7 @@ func TestTheRemovalToolsRefuseBeforeTheyReachTheBus(t *testing.T) { unreachable := func(context.Context) (jetstream.JetStream, func(), error) { return nil, nil, errors.New("the bus is not reached here") } - for _, tl := range removalTools(unreachable, said, newForm, nil) { + for _, tl := range removalTools(unreachable, said, newForm, accepted, nil) { if tl.Name != "nats_consumer_remove" { continue } diff --git a/modules/nats/internal/keeper/keeper.go b/modules/nats/internal/keeper/keeper.go index 68788bed4..b214641cd 100644 --- a/modules/nats/internal/keeper/keeper.go +++ b/modules/nats/internal/keeper/keeper.go @@ -1005,13 +1005,14 @@ func (k *Keeper) asked(module string) (Asked, uint64, bool) { } // write keeps a module's key, or removes it when its manifest asks for nothing and nothing it made is held: a key goes -// only so, never because a module is missing from a first view. +// only so, never because a module is missing from a first view. Its plan key stays: plans is the accepted set, one key +// per module of a watched branch's newest revision, deleted only when no such revision lists it (design 47 G §538; +// H-A-128: nats_consumer_remove judges a module's durables by it). func (k *Keeper) write(a Asked) { if len(a.Entries) == 0 && len(a.Made) == 0 && len(a.Drifts) == 0 { if err := k.Store.Delete(StateAsked, a.Module); err != nil { k.logf("the key of %s cannot be removed from its own %s: %v", a.Module, StateAsked, err) } - _ = k.Store.Delete(StatePlans, a.Module) return } raw, _ := json.Marshal(a) diff --git a/modules/nats/internal/keeper/keeper_test.go b/modules/nats/internal/keeper/keeper_test.go index 500eb3d04..944ed4fc6 100644 --- a/modules/nats/internal/keeper/keeper_test.go +++ b/modules/nats/internal/keeper/keeper_test.go @@ -544,6 +544,30 @@ func TestTheKeeperMakesWhatAVerdictAsksForAndSaysIt(t *testing.T) { t.Errorf("old sections %d: 8 MiB counted", k.Status().OldBytes) } }}, + // §538, H-A-128: plans is the accepted set: a module that asks for no kept data keeps its plan key (no key in + // asked), even when a drift it had clears; the key goes only when no watched branch's newest revision lists it. + {"a module asking for no kept data keeps its plan key while it is listed", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) { + quiet := CatalogModule{Accepted: Accepted{Module: "quiet", Revision: 1}} + k.Catalog = func() (CatalogView, error) { + return CatalogView{Modules: []CatalogModule{quiet}, Drifts: []Drift{{Module: "quiet", Local: "x", Branch: "feature", From: "main"}}}, nil + } + _ = k.PlanCatalog() + k.Catalog = func() (CatalogView, error) { return CatalogView{Modules: []CatalogModule{quiet}}, nil } + _ = k.PlanCatalog() + if _, _, found, _ := k.Store.Get(StatePlans, "quiet"); !found { + t.Errorf("quiet is listed but has no plan key: the accepted set lost a module that keeps no bus data") + } + if _, ok := k.Held()["quiet"]; ok { + t.Errorf("quiet asks for nothing and has a key in asked: %+v", k.Held()["quiet"]) + } + k.Catalog = func() (CatalogView, error) { + return CatalogView{Modules: []CatalogModule{{Accepted: Accepted{Module: "m", Revision: 2}}}}, nil + } + _ = k.PlanCatalog() + if _, _, found, _ := k.Store.Get(StatePlans, "quiet"); found { + t.Errorf("quiet is listed nowhere and keeps its plan key") + } + }}, // A-367: a trunk with no revision yet (none-yet) is not yet viewed: nothing is planned, deleted or orphaned. {"a none-yet catalog half plans nothing and orphans nothing until a module is listed", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) { k.Heard(Accepted{Module: "m", Revision: 9, BusState: []catalogue.BusStateEntry{drafts}}) -- 2.54.0 From 6ecadf294e2c5619e5589ffce244aa389eb67ead Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 06:35:44 +0100 Subject: [PATCH 43/43] GATE-R2: the retired checker no longer reads the gate's state source MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds: design 47 H §764 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- .../mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go index b68129f73..6dc6bd231 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go @@ -11,9 +11,10 @@ import ( // readers is the gate's own declaration of who reads its states (ADR 0495 clause 3: a read is granted only when the owner // announced the state readable), kept here and not read from other repositories' manifests (clause 1). Each line names -// the modules of mesh-delivery whose manifest reads the state, as of R4e. +// the modules of mesh-delivery whose manifest reads the state; the checker, retired whole in H1 (design 47 H §764), reads +// none. var readers = map[string][]string{ - "source": {"mesh-delivery-indexer", "mesh-delivery-checker"}, + "source": {"mesh-delivery-indexer"}, } // 1790/S: the states the module announces are the states its manifest declares, no more, no fewer, and the readable -- 2.54.0