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": {