diff --git a/modules/artifact-store-tools/cmd/artifact-store-tools/holder.go b/modules/artifact-store-tools/cmd/artifact-store-tools/holder.go index fc598c633..e6e626cb9 100644 --- a/modules/artifact-store-tools/cmd/artifact-store-tools/holder.go +++ b/modules/artifact-store-tools/cmd/artifact-store-tools/holder.go @@ -29,12 +29,12 @@ import ( "sync" "time" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" artifactref "git.novox.be/novox/mesh-delivery/contracts/go/clients/containerimageregistryclient/reference" stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/keys" "git.novox.be/novox/mesh-sdk/go/views" ) @@ -45,8 +45,6 @@ const ( HavesRead = "node-engine.have" // ManifestsRead is the indexer's manifests: the images a manifest pins by itself. ManifestsRead = indexerModule + "." + catalog.StateManifests - // FindingsState is the holder's own findings (meshconditions.StateFindings): image-missing. - FindingsState = meshconditions.StateFindings // Module is this module, the observer its findings name. Module = "artifact-store-tools" ) @@ -83,8 +81,10 @@ type Door interface { // Holder is the seat's holder. type Holder struct { - Bus Bus - Door func() (Door, error) + Bus Bus + // Findings is the module's finding writer: S25's and the sweep's findings go through it. + Findings Findings + Door func() (Door, error) // Listed is the store as its seats list it now (Store.View); nil reads none, and then nothing is said gone or collected. Listed func(ctx context.Context) (*View, error) Acts meshhandactsclient.Recorder @@ -93,9 +93,12 @@ type Holder struct { Now func() time.Time Logf func(string, ...any) - sweep sync.Mutex // one sweep at a time - mu sync.Mutex - ended map[string]time.Time // a done key's end, read once from its history + sweep sync.Mutex // one sweep at a time + mu sync.Mutex + fmu sync.Mutex // the findings said and the sweep's refusal + said map[string]meshconditions.Finding // S25's findings as last written, by key + refused *meshconditions.Finding // the sweep's finding while a seat's refusal stands + ended map[string]time.Time // a done key's end, read once from its history } // Build is one done build of a module, as its key holds it, with when it ended. @@ -852,16 +855,75 @@ func inStoreAt(image, address string) string { return r } -// Observe writes S25's findings (image-missing): each want whose build names an image the store does not hold, on the -// want's machine, and deletes each finding that no longer holds. Nothing is said without a listing. +// Observe says S25's findings (image-missing): each want whose build names an image the store does not hold, on the +// want's machine, written once and cleared once it no longer holds. Nothing is said without a listing. func (h *Holder) Observe(ctx context.Context) error { - j, err := h.judge(ctx) + now, err := h.observed(ctx) if err != nil { return err } - if j.view == nil { + if now == nil { return nil } + h.fmu.Lock() + defer h.fmu.Unlock() + if h.said == nil { + h.said = map[string]meshconditions.Finding{} + } + for k, was := range h.said { + if _, still := now[k]; !still { + if err := h.Findings.Clear(ctx, was.Scope, was.ID, was.Kind, "the store holds the image again"); err != nil { + return err + } + delete(h.said, k) + } + } + for k, f := range now { + if was, ok := h.said[k]; ok && was.Summary == f.Summary { + continue // written only when what it says changes + } + if err := h.Findings.Write(ctx, f); err != nil { + return err + } + h.said[k] = f + } + return nil +} + +// Findings is the module's finding writer (views.Writer): the holder writes and clears through it (design 47 G §689). +type Findings interface { + Write(ctx context.Context, finding meshconditions.Finding) error + Clear(ctx context.Context, scope, id, kind, why string) error +} + +// FindingsObserved answers the findings this module observes now, for its writer's statement (views.WriterConfig.Observe): +// S25's findings and the sweep's, when a seat refused it and no clean sweep cleared it. +func (h *Holder) FindingsObserved(ctx context.Context) (views.Observed[meshconditions.Finding], error) { + now, err := h.observed(ctx) + if err != nil { + return views.Observed[meshconditions.Finding]{}, err + } + var out views.Observed[meshconditions.Finding] + for _, f := range now { + out.Findings = append(out.Findings, f) + } + h.fmu.Lock() + defer h.fmu.Unlock() + if h.refused != nil { + out.Findings = append(out.Findings, *h.refused) + } + return out, nil +} + +// observed is S25's findings now by key, nil without a listing. +func (h *Holder) observed(ctx context.Context) (map[string]meshconditions.Finding, error) { + j, err := h.judge(ctx) + if err != nil { + return nil, err + } + if j.view == nil { + return nil, nil + } address := "" if d, err := h.Door(); err == nil { address = d.Address() @@ -870,7 +932,7 @@ func (h *Holder) Observe(ctx context.Context) error { for _, b := range j.built { builds[b.Module+" "+b.FP] = b } - now := map[string]meshconditions.FindingsByKeyValue{} + now := map[string]meshconditions.Finding{} for _, w := range j.wants { b, ok := builds[w.Module+" "+w.FP] if !ok { @@ -895,63 +957,26 @@ func (h *Holder) Observe(ctx context.Context) error { continue } f := imageMissing(w.Node, w.Module, reference, h.now()) - now[f.Key] = f + now[views.FindingKey(f.Scope, f.ID, f.Kind)] = f break } } - keys, err := h.Bus.Keys(FindingsState) - if err != nil { - return fmt.Errorf("the findings: %w", err) - } - for _, k := range keys { - if k == sweepFindingKey { - continue // the sweep's own finding, which the sweep clears - } - if _, still := now[k]; !still { - // A finding of a first view unanswered is the reader's (views.Reader), cleared by its answer, not by a sweep. - if raw, found, err := h.Bus.Get(FindingsState, k); err == nil && found { - if f, err := meshconditions.DecodeFindingsByKeyValue(raw); err == nil && f.Kind == views.UnansweredKind { - continue - } - } - if err := h.Bus.Delete(FindingsState, k); err != nil { - return err - } - } - } - for k, f := range now { - raw, found, err := h.Bus.Get(FindingsState, k) - if err != nil { - return err - } - if found { - if was, err := meshconditions.DecodeFindingsByKeyValue(raw); err == nil && was.Summary == f.Summary { - continue // written only when what it says changes - } - } - if err := meshconditionsclient.CheckFinding(f); err != nil { - return err - } - if err := h.Bus.Put(FindingsState, k, f); err != nil { - return err - } - } - return nil + return now, nil } -// imageMissing is S25's finding, in the controller's words (cmd/mesh-controller/image_missing.go watchMissingImages). -func imageMissing(node, module, image string, at time.Time) meshconditions.FindingsByKeyValue { - id := module + "." + node - return meshconditions.FindingsByKeyValue{V: meshconditionsclient.FindingVersion, Key: meshconditionsclient.Key(meshconditionsclient.ScopeModule, id, ImageMissing), Kind: ImageMissing, - Subject: meshconditions.Subject{Scope: meshconditionsclient.ScopeModule, ID: id, Machine: node}, - Severity: meshconditionsclient.Warning, Resolver: meshconditionsclient.ResolverOperator, +// imageMissing is S25's finding, in the controller's words (cmd/mesh-controller/image_missing.go watchMissingImages); its id +// is the module's and the machine's, each escaped (design 47 G A-14). +func imageMissing(node, module, image string, at time.Time) meshconditions.Finding { + id := keys.EscapeName(module) + "." + keys.EscapeName(node) + return meshconditions.Finding{Scope: "module", ID: id, Kind: ImageMissing, + Subject: meshconditions.Subject{Scope: "module", ID: id, Machine: node}, + Severity: meshconditions.FindingSeverityWarning, Resolver: "operator", Summary: fmt.Sprintf("%s is left out of every send to %s: its declaration names an image that the artifact store does "+ "not hold (the evidence names it); the running container is untouched", module, node), Headline: capital(module) + " is not updated on " + node, Explanation: fmt.Sprintf("The new version of %s names a container image that the mesh's store does not hold, so it "+ "was not sent to %s. What runs there is untouched, and the rest of %s is sent as usual.", module, node, node), Needs: fmt.Sprintf("build %s again; the details name the image.", module), - Resolved: "Resolved: the store holds the image of " + module + " again", Evidence: []meshconditions.Evidence{{At: at.UTC(), Said: "image " + image + " is not in the store"}}, Source: "S25", Observer: Module, At: at.UTC()} } diff --git a/modules/artifact-store-tools/cmd/artifact-store-tools/holder_integration_test.go b/modules/artifact-store-tools/cmd/artifact-store-tools/holder_integration_test.go index 575dfcc00..3963d3749 100644 --- a/modules/artifact-store-tools/cmd/artifact-store-tools/holder_integration_test.go +++ b/modules/artifact-store-tools/cmd/artifact-store-tools/holder_integration_test.go @@ -198,7 +198,7 @@ func TestTheSeatSweepsAtABuildsEndOnARealBusWithNoLease(t *testing.T) { } return m.Data, nil } - h := &Holder{Bus: findingsAt{realBus: bus}, Door: mem.Door, Listed: mem.Listed, Seats: ask, Now: time.Now, Logf: t.Logf} + h := &Holder{Bus: findingsAt{realBus: bus}, Findings: findingsThrough{findingsAt{realBus: bus}}, Door: mem.Door, Listed: mem.Listed, Seats: ask, Now: time.Now, Logf: t.Logf} h.Acts = meshhandactsclient.Recorder{Ask: ask} // A build's end heard from the builds bucket, as follow hands it over. diff --git a/modules/artifact-store-tools/cmd/artifact-store-tools/holder_test.go b/modules/artifact-store-tools/cmd/artifact-store-tools/holder_test.go index 6aa10a50f..a5df807cd 100644 --- a/modules/artifact-store-tools/cmd/artifact-store-tools/holder_test.go +++ b/modules/artifact-store-tools/cmd/artifact-store-tools/holder_test.go @@ -17,10 +17,10 @@ import ( "testing" "time" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/views" artifactref "git.novox.be/novox/mesh-delivery/contracts/go/clients/containerimageregistryclient/reference" reg "git.novox.be/novox/mesh-delivery/contracts/go/seats/containerimageregistry" @@ -229,7 +229,7 @@ func aMesh(t *testing.T) (*Holder, *memBus, *memDoor, *[]string) { want, _ := json.Marshal(meshwants.WantsByWantNodeModuleValue{V: 1, Fp: fp(1), Self: true}) _ = b.Put(WantsRead, "want.ace.letta", json.RawMessage(want)) var acts []string - h := &Holder{Bus: b, Door: func() (Door, error) { return door, nil }, Seats: door.seat, + h := &Holder{Bus: b, Findings: findingsThrough{b}, Door: func() (Door, error) { return door, nil }, Seats: door.seat, Listed: func(context.Context) (*View, error) { return &View{L: listing}, nil }, Now: func() time.Time { return day.Add(48 * time.Hour) }, Acts: meshhandactsclient.Recorder{Ask: func(key string, body any) (json.RawMessage, error) { @@ -370,7 +370,7 @@ func TestTheSeatKeepsTheFiveNewestAndTheWantedAndSweepsWithNoLease(t *testing.T) if c.letGo[0] == archiveRef("letta", 2) && !slices.Contains(door.calls, "seat:oci-artifact-registry.remove letta/files@"+digestOf(archiveRef("letta", 2))+" "+Retention) { t.Errorf("the seats were called %v", door.calls) } - _, finding, _ := b.Get(FindingsState, sweepFindingKey) + _, finding, _ := b.Get(FindingsState, views.FindingKey("module", Module, SweepRefused)) if finding != (c.refuse != "") { t.Errorf("the sweep's finding is written: %v, want %v", finding, c.refuse != "") } @@ -379,7 +379,7 @@ func TestTheSeatKeepsTheFiveNewestAndTheWantedAndSweepsWithNoLease(t *testing.T) if _, err := h.Sweep(context.Background(), Sweeping{Most: MostPerSweep, By: "again"}); err != nil { t.Fatal(err) } - if _, still, _ := b.Get(FindingsState, sweepFindingKey); still { + if _, still, _ := b.Get(FindingsState, views.FindingKey("module", Module, SweepRefused)); still { t.Error("a clean sweep left the sweep's finding") } return @@ -410,9 +410,10 @@ func TestAWantedImageTheStoreLacksIsAFindingAndImagesNamesWantsAndHaves(t *testi if err := h.Observe(context.Background()); err != nil { t.Fatal(err) } - key := meshconditionsclient.Key(meshconditionsclient.ScopeModule, "letta.ace", ImageMissing) + key := views.FindingKey("module", "letta.ace", ImageMissing) raw, found, _ := b.Get(FindingsState, key) - f, err := meshconditions.DecodeFindingsByKeyValue(raw) + var f meshconditions.Finding + err := json.Unmarshal(raw, &f) if !found || err != nil || f.Subject.Machine != "ace" || f.Source != "S25" || !strings.Contains(f.Evidence[0].Said, imageRef("letta", 1)) { t.Fatalf("the finding is %+v (%v, %v)", f, found, err) } @@ -441,3 +442,23 @@ func TestAWantedImageTheStoreLacksIsAFindingAndImagesNamesWantsAndHaves(t *testi t.Fatalf("the images of ace are %+v: %v", images, err) } } + +// FindingsState is the module's state `findings`, where the tests' writer keeps what the holder writes through it. +const FindingsState = "findings" + +// findingsThrough is the holder's finding writer in a test: it keeps a finding in its key and deletes it as the module's +// writer does (views.Writer), without the bus. +type findingsThrough struct { + bus interface { + Put(state, key string, value any) error + Delete(state, key string) error + } +} + +func (f findingsThrough) Write(_ context.Context, finding meshconditions.Finding) error { + return f.bus.Put(FindingsState, views.FindingKey(finding.Scope, finding.ID, finding.Kind), finding) +} + +func (f findingsThrough) Clear(_ context.Context, scope, id, kind, _ string) error { + return f.bus.Delete(FindingsState, views.FindingKey(scope, id, kind)) +} diff --git a/modules/artifact-store-tools/cmd/artifact-store-tools/main.go b/modules/artifact-store-tools/cmd/artifact-store-tools/main.go index 3af7eacc5..611ef1130 100644 --- a/modules/artifact-store-tools/cmd/artifact-store-tools/main.go +++ b/modules/artifact-store-tools/cmd/artifact-store-tools/main.go @@ -13,8 +13,8 @@ import ( "os" "time" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" + meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/views" @@ -24,12 +24,29 @@ func logf(format string, a ...any) { fmt.Fprintf(os.Stderr, format+"\n", a...) } func main() { s := &Store{Ask: stdio.Ask} - v := &views.Reader{Module: "artifact-store-tools", Logf: logf, Findings: meshconditionsclient.FindingWriter{State: stdio.State("findings")}, Acts: meshhandactsclient.Recorder{Ask: stdio.Ask}} + v := &views.Reader{Module: "artifact-store-tools", Logf: logf, Acts: meshhandactsclient.Recorder{Ask: stdio.Ask}} v.Follow(assignerModule, "wants", WantsTool) v.Follow(indexerModule, "manifests", ManifestsTool) h := &Holder{Bus: runtimeBus{views: v}, Door: DoorAt(os.Getenv("MESH_ARTIFACT_STORE_ADDRESS_FILE"), s), Listed: s.View, Seats: stdio.Ask, Now: time.Now, Logf: logf} h.Acts = meshhandactsclient.Recorder{Ask: stdio.Ask} + // The module's one writer (design 47 G §689, A-16): the holder's findings and the reader's go through it. + w, err := views.NewStdioWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{ + Module: Module, State: stdio.State("findings"), Kinds: []string{ImageMissing, SweepRefused}, Observe: h.FindingsObserved}) + if err != nil { + logf("%v", err) + os.Exit(1) + } + h.Findings = w + v.Findings = views.WriterFindings{Writer: w} + // the runtime answers the writer's subscription only once Serve has its channel + go func() { + <-stdio.Served() + if err := w.Start(context.Background()); err != nil { + logf("%v", err) + os.Exit(1) + } + }() v.Start(context.Background()) go follow(h, v) // An empty name serves as the module the runtime names (MESH_SERVED_MODULE): artifact-store-tools. diff --git a/modules/artifact-store-tools/cmd/artifact-store-tools/sweepseats.go b/modules/artifact-store-tools/cmd/artifact-store-tools/sweepseats.go index 568b62781..34c85d483 100644 --- a/modules/artifact-store-tools/cmd/artifact-store-tools/sweepseats.go +++ b/modules/artifact-store-tools/cmd/artifact-store-tools/sweepseats.go @@ -10,15 +10,16 @@ package main // sweep's finding, sweep-refused; the next sweep with no refusal clears it. import ( + "context" "encoding/json" "fmt" "strings" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" artifactref "git.novox.be/novox/mesh-delivery/contracts/go/clients/containerimageregistryclient/reference" ociseat "git.novox.be/novox/mesh-delivery/contracts/go/seats/ociartifactregistry" "git.novox.be/novox/mesh-sdk/go/catalogue" + "git.novox.be/novox/mesh-sdk/go/keys" reg "git.novox.be/novox/mesh-delivery/contracts/go/seats/containerimageregistry" ) @@ -26,9 +27,6 @@ import ( // SweepRefused is the sweep's finding: a seat refused a hold or a remove the sweep asked. const SweepRefused = "sweep-refused" -// sweepFindingKey is the one key of the sweep's finding in the module's findings. -var sweepFindingKey = meshconditionsclient.Key(meshconditionsclient.ScopeModule, Module, SweepRefused) - // canonical is a kept reference in the older form, `artifact-store://`, whichever form it came in; another is as // it was. func canonical(reference string) string { @@ -115,28 +113,35 @@ func (h *Holder) removeOnSeat(reference, why string) error { // sweepFinding writes the sweep's finding: what the seat refused, and why. func (h *Holder) sweepFinding(what string, err error) { at := h.now().UTC() - f := meshconditions.FindingsByKeyValue{V: meshconditionsclient.FindingVersion, Key: sweepFindingKey, Kind: SweepRefused, - Subject: meshconditions.Subject{Scope: meshconditionsclient.ScopeModule, ID: Module}, - Severity: meshconditionsclient.Warning, Resolver: meshconditionsclient.ResolverOperator, + f := meshconditions.Finding{Scope: "module", ID: keys.EscapeName(Module), Kind: SweepRefused, + Subject: meshconditions.Subject{Scope: "module", ID: keys.EscapeName(Module)}, + Severity: meshconditions.FindingSeverityWarning, Resolver: "operator", Summary: fmt.Sprintf("the artifact sweep stopped: a registry seat refused %s (the evidence names why)", what), Headline: "The artifact sweep stopped", Explanation: "The sweep keeps what the mesh still needs and removes the rest through the registry seats. A seat " + "refused one of its asks, so it stopped before anything more was removed; what it kept stays kept.", Needs: "read the seat holder's journal for the refusal; the next sweep asks again.", - Resolved: "Resolved: the artifact sweep ran through without a refusal", Evidence: []meshconditions.Evidence{{At: at.UTC(), Said: fmt.Sprintf("%s: %v", what, err)}}, Source: "CAT-AS1", Observer: Module, At: at} - if werr := h.Bus.Put(FindingsState, sweepFindingKey, f); werr != nil { + h.fmu.Lock() + h.refused = &f + h.fmu.Unlock() + if werr := h.Findings.Write(context.Background(), f); werr != nil { h.logf("the sweep's finding (%s: %v) could not be written: %v", what, err, werr) } } // sweepClean clears the sweep's finding after a sweep no seat refused. func (h *Holder) sweepClean() { - if _, found, err := h.Bus.Get(FindingsState, sweepFindingKey); err == nil && found { - if err := h.Bus.Delete(FindingsState, sweepFindingKey); err != nil { - h.logf("the sweep's finding could not be cleared: %v", err) - } + h.fmu.Lock() + was := h.refused + h.refused = nil + h.fmu.Unlock() + if was == nil { + return + } + if err := h.Findings.Clear(context.Background(), was.Scope, was.ID, was.Kind, "a sweep ran through without a refusal"); err != nil { + h.logf("the sweep's finding could not be cleared: %v", err) } } diff --git a/modules/artifact-store-tools/go.mod b/modules/artifact-store-tools/go.mod index 2e3263e2d..8b71604f0 100644 --- a/modules/artifact-store-tools/go.mod +++ b/modules/artifact-store-tools/go.mod @@ -5,7 +5,6 @@ go 1.26.0 require ( git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient v0.0.0-00010101000000-000000000000 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/meshconditionsclient 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/health-and-repair/go/seats/meshhandacts v0.0.0-00010101000000-000000000000 diff --git a/modules/claude-code/cmd/claude-code/main.go b/modules/claude-code/cmd/claude-code/main.go index 9ab8baa7b..2ed3b5b03 100644 --- a/modules/claude-code/cmd/claude-code/main.go +++ b/modules/claude-code/cmd/claude-code/main.go @@ -504,10 +504,14 @@ func main() { os.Exit(1) } meshViews.Findings = views.WriterFindings{Writer: findings} - if err := findings.Start(context.Background()); err != nil { - say("%v", err) - os.Exit(1) - } + // the runtime answers the writer's subscription only once Serve has its channel + go func() { + <-stdio.Served() + if err := findings.Start(context.Background()); err != nil { + say("%v", err) + os.Exit(1) + } + }() meshViews.Start(context.Background()) go refreshMeshTools(p) servers := stateOf{stdio.State("servers")} diff --git a/modules/distribution/cmd/distribution-tools/main.go b/modules/distribution/cmd/distribution-tools/main.go index 41c949a4a..e43ca6b53 100644 --- a/modules/distribution/cmd/distribution-tools/main.go +++ b/modules/distribution/cmd/distribution-tools/main.go @@ -18,12 +18,12 @@ import ( "sync" "time" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" reg "git.novox.be/novox/mesh-delivery/contracts/go/seats/containerimageregistry" "git.novox.be/novox/mesh-delivery/sdk/parts" stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/views" "distribution/internal/holder" ) @@ -67,21 +67,6 @@ func say(seat, event string, body holder.Event) error { return stdio.SeatPublish("mesh.seat."+seat+".event."+event, eventBody(event, body), body.ID) } -func finding(kind, subject, words string, raised bool) { - key := meshconditionsclient.Key(meshconditionsclient.ScopeSeat, "distribution", kind+"."+strings.NewReplacer(".", "_", "@", "_", "/", "_").Replace(subject)) - if !raised { - _ = stdio.State(meshconditions.StateFindings).Delete(key) - return - } - f := meshconditions.FindingsByKeyValue{V: meshconditionsclient.FindingVersion, Key: key, Kind: kind, Ref: key[strings.LastIndex(key, ".")+1:], - Subject: meshconditions.Subject{Scope: meshconditionsclient.ScopeSeat, ID: "distribution"}, Severity: meshconditionsclient.Warning, - Summary: words, Headline: words, Explanation: words, Resolved: "cleared once it no longer holds", - Source: kind, Observer: "distribution", At: time.Now().UTC()} - if _, err := stdio.State(meshconditions.StateFindings).Put(key, f); err != nil { - logf("the finding %s could not be written: %v", key, err) - } -} - // learned are the proved callers whose build this process recorded as publishing through the seats. var learned sync.Map @@ -420,7 +405,7 @@ func tools(v *holder.Verbs, windows kept[holder.Window], callers func() (holder. Input: obj(map[string]any{}), Run: func(map[string]any) (any, error) { front, _ := url.Parse("http://127.0.0.1:5100") - return holder.Health(&holder.OCI{Base: front}, finding) + return holder.Health(&holder.OCI{Base: front}, findings.Say) }}, stdio.Tool{Name: "registry_incoming", Description: "Open transfers of incoming and its fill; with drop, drops one by hand (a hand act, why required).", Input: obj(map[string]any{"drop": s("a transfer id to drop"), "why": s("why it is dropped")}), @@ -516,10 +501,30 @@ func tools(v *holder.Verbs, windows kept[holder.Window], callers func() (holder. return out } +// findings keeps the open findings of this bundle for its writer (design 47 G §689). +var findings = &holder.Findings{Logf: logf} + func main() { + // The tools bundle's findings go through the module's writer (design 47 G §689, A-16); it owns the transfers' kinds, the + // program's writer the driver's. + w, err := views.NewStdioWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{ + Module: holder.Self, State: stdio.State("findings"), Kinds: holder.TransferFindingKinds, Observe: findings.Observed, Logf: logf}) + if err != nil { + logf("%v", err) + os.Exit(1) + } + findings.Writer = w + // the runtime answers the writer's subscription only once Serve has its channel + go func() { + <-stdio.Served() + if err := w.Start(context.Background()); err != nil { + logf("%v", err) + os.Exit(1) + } + }() base, _ := url.Parse("http://127.0.0.1:5001") store := &holder.OCI{Base: base, Upstream: &holder.Upstream{}} - h := &holder.Holder{Objects: parts.BusObjects(holder.IncomingObjects), Builder: holder.Reserved{Ask: stdio.Ask}, Store: store, Say: say, Finding: finding, + h := &holder.Holder{Objects: parts.BusObjects(holder.IncomingObjects), Builder: holder.Reserved{Ask: stdio.Ask}, Store: store, Say: say, Finding: findings.Say, Transfers: parts.BusTransfers[holder.Opening](holder.TransfersState), Cap: parts.BusCap(holder.IncomingObjects), Record: recordAct, Idle: parts.BusArm(holder.IdleState)} files := &holder.Files{Container: or(os.Getenv("MESH_REGISTRY_CONTAINER"), "mesh-registry"), Run: holder.ExecRunner, UID: os.Getuid()} v := &holder.Verbs{Holder: h, Catalog: store, Holds: kept[holder.Hold]{state: "holds", key: holder.Hold.Key}, Mirrors: mirrors{}, diff --git a/modules/distribution/cmd/distribution/main.go b/modules/distribution/cmd/distribution/main.go index 8db8da6a4..4cedc92d9 100644 --- a/modules/distribution/cmd/distribution/main.go +++ b/modules/distribution/cmd/distribution/main.go @@ -20,7 +20,9 @@ import ( "syscall" "time" + meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" "git.novox.be/novox/mesh-sdk/go/process" + "git.novox.be/novox/mesh-sdk/go/views" "github.com/nats-io/nats.go/jetstream" "distribution/internal/holder" @@ -62,9 +64,22 @@ func run(ctx context.Context, getenv func(string) string) error { return err } defer calls.Close() - findings := process.OptionalBucket(ctx, js, "distribution_findings", "a window held for seven days is said in the log only", logger.Printf) + // The program's findings go through the module's writer (design 47 G §689); it owns the driver's kinds, the tools + // bundle's writer the transfers'. + findings := &holder.Findings{Logf: logger.Printf} + w, err := views.NewWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{ + Module: holder.Self, Node: cred.Node, State: calls.State("findings"), Kinds: holder.DriverFindingKinds, + JS: js, Conn: conn, Observe: findings.Observed, Logf: logger.Printf}) + if err != nil { + return err + } + findings.Writer = w + if err := w.Start(ctx); err != nil { + return err + } + defer w.Close() go drive(ctx, js, cred, &holder.Driver{Delivery: busDelivery{calls}, State: kvWindows{windows}, Callers: busCallers{calls}.Read, - Closed: sayClosed(js, cred), Finding: findingsKV(findings, logger.Printf)}, logger) + Closed: sayClosed(js, cred), Finding: findings.Say}, logger) } front := &holder.Front{Store: store, Window: &open, Logf: logger.Printf, Published: func(name, tag string) (bool, error) { diff --git a/modules/distribution/cmd/distribution/windows.go b/modules/distribution/cmd/distribution/windows.go index 15b166776..e7a06e185 100644 --- a/modules/distribution/cmd/distribution/windows.go +++ b/modules/distribution/cmd/distribution/windows.go @@ -18,8 +18,6 @@ import ( "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" - meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" "git.novox.be/novox/mesh-sdk/go/process" "distribution/internal/holder" @@ -173,30 +171,6 @@ func (k kvWindows) Publishing() (holder.Publishing, error) { return p, k.get(holder.PublishingKey, &p) } -// findingsKV writes the driver's findings into distribution's state findings, as the tools bundle writes its own. -func findingsKV(kv jetstream.KeyValue, logf func(string, ...any)) func(kind, subject, words string, raised bool) { - return func(kind, subject, words string, raised bool) { - if kv == nil { - return - } - key := meshconditionsclient.Key(meshconditionsclient.ScopeSeat, holder.Self, kind+"."+strings.NewReplacer(".", "_", "@", "_", "/", "_").Replace(subject)) - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - if !raised { - _ = kv.Delete(ctx, key) - return - } - f := meshconditions.FindingsByKeyValue{V: meshconditionsclient.FindingVersion, Key: key, Kind: kind, Ref: key[strings.LastIndex(key, ".")+1:], - Subject: meshconditions.Subject{Scope: meshconditionsclient.ScopeSeat, ID: holder.Self}, Severity: meshconditionsclient.Warning, - Summary: words, Headline: words, Explanation: words + "; registry_old_name or registry_direct_writes drops a retired node by hand", - Resolved: "cleared when the window closes", Source: kind, Observer: holder.Self, At: time.Now().UTC()} - b, _ := json.Marshal(f) - if _, err := kv.Put(ctx, key, b); err != nil { - logf("the finding %s could not be written: %v", key, err) - } - } -} - // sayClosed says a window's closing on distribution's own family distribution.events, once (its id is the window's). func sayClosed(js jetstream.JetStream, cred process.Account) func(holder.Closed) error { return func(c holder.Closed) error { diff --git a/modules/distribution/go.mod b/modules/distribution/go.mod index 4fbf8a54a..d5b6b1f8d 100644 --- a/modules/distribution/go.mod +++ b/modules/distribution/go.mod @@ -3,7 +3,6 @@ module distribution go 1.26.0 require ( - git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient 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-delivery/contracts/go/clients/containerimageregistryclient v0.0.0-00010101000000-000000000000 diff --git a/modules/distribution/internal/holder/findings.go b/modules/distribution/internal/holder/findings.go new file mode 100644 index 000000000..b99472d9b --- /dev/null +++ b/modules/distribution/internal/holder/findings.go @@ -0,0 +1,77 @@ +package holder + +import ( + "context" + "sync" + "time" + + meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" + keyparts "git.novox.be/novox/mesh-sdk/go/keys" + "git.novox.be/novox/mesh-sdk/go/views" +) + +// FindingKinds are the kinds the driver says (the program) and the transfers say (the tools bundle): each bundle's writer +// owns its own, since the two share one state `findings` (design 47 G §580, §689). +var ( + DriverFindingKinds = []string{FindingWindowHeld} + TransferFindingKinds = []string{"publish-refused", "incoming-full"} +) + +// FindingWriter is the module's writer (views.Writer) as the keeper writes and clears through it. +type FindingWriter interface { + Write(ctx context.Context, finding meshconditions.Finding) error + Clear(ctx context.Context, scope, id, kind, why string) error +} + +// Findings keeps a bundle's open findings and says them through the module's writer (design 47 G §689, A-14): its `Say` is +// the Finding callback of the Driver and the Holder, and its `Observed` is the writer's observe, so a statement covers +// what is open. +type Findings struct { + Writer FindingWriter + Logf func(string, ...any) + Now func() time.Time + + mu sync.Mutex + open map[string]meshconditions.Finding +} + +// Say raises (raised) or clears the finding of a kind about a subject. Its id is the seat's and the subject's, each escaped +// as a key's name is (design 47 G A-14); the kind stays the bundle's own word. +func (k *Findings) Say(kind, subject, words string, raised bool) { + id := keyparts.EscapeName(Self) + "." + keyparts.EscapeName(subject) + k.mu.Lock() + defer k.mu.Unlock() + key := views.FindingKey("seat", id, kind) + if !raised { + delete(k.open, key) + if err := k.Writer.Clear(context.Background(), "seat", id, kind, "it no longer holds"); err != nil { + k.Logf("the finding %s could not be cleared: %v", key, err) + } + return + } + at := time.Now() + if k.Now != nil { + at = k.Now() + } + f := meshconditions.Finding{Scope: "seat", ID: id, Kind: kind, Subject: meshconditions.Subject{Scope: "seat", ID: id}, + Severity: meshconditions.FindingSeverityWarning, Summary: words, Headline: words, Explanation: words, + Source: kind, Observer: Self, At: at.UTC()} + if k.open == nil { + k.open = map[string]meshconditions.Finding{} + } + k.open[key] = f + if err := k.Writer.Write(context.Background(), f); err != nil { + k.Logf("the finding %s could not be written: %v", key, err) + } +} + +// Observed is the findings open now, for the writer's statement (views.WriterConfig.Observe). +func (k *Findings) Observed(context.Context) (views.Observed[meshconditions.Finding], error) { + k.mu.Lock() + defer k.mu.Unlock() + var out views.Observed[meshconditions.Finding] + for _, f := range k.open { + out.Findings = append(out.Findings, f) + } + return out, nil +} diff --git a/modules/gitea/cmd/package-registry/findings.go b/modules/gitea/cmd/package-registry/findings.go new file mode 100644 index 000000000..544e699f1 --- /dev/null +++ b/modules/gitea/cmd/package-registry/findings.go @@ -0,0 +1,76 @@ +package main + +import ( + "context" + "sync" + "time" + + meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" + keyparts "git.novox.be/novox/mesh-sdk/go/keys" + "git.novox.be/novox/mesh-sdk/go/views" + + "gitea/internal/packageseats" +) + +// findingKinds are the kinds this bundle says: the token refusal and the transfers' (design 47 G §580, §689); the module's +// TypeScript bundle shares the state `findings` and owns its own. +var findingKinds = []string{PackagesTokenRefused, "publish-refused", "incoming-full"} + +// findingWriter is the module's writer (views.Writer) as the keeper writes and clears through it. +type findingWriter interface { + Write(ctx context.Context, finding meshconditions.Finding) error + Clear(ctx context.Context, scope, id, kind, why string) error +} + +// Findings keeps a bundle's open findings and says them through the module's writer (design 47 G §689, A-14): its `Say` is +// the Finding callback of the Driver and the Holder, and its `Observed` is the writer's observe, so a statement covers +// what is open. +type Findings struct { + Writer findingWriter + Logf func(string, ...any) + Now func() time.Time + + mu sync.Mutex + open map[string]meshconditions.Finding +} + +// Say raises (raised) or clears the finding of a kind about a subject. Its id is the seat's and the subject's, each escaped +// as a key's name is (design 47 G A-14); the kind stays the bundle's own word. +func (k *Findings) Say(kind, subject, words string, raised bool) { + id := keyparts.EscapeName(packageseats.Self) + "." + keyparts.EscapeName(subject) + k.mu.Lock() + defer k.mu.Unlock() + key := views.FindingKey("seat", id, kind) + if !raised { + delete(k.open, key) + if err := k.Writer.Clear(context.Background(), "seat", id, kind, "it no longer holds"); err != nil { + k.Logf("the finding %s could not be cleared: %v", key, err) + } + return + } + at := time.Now() + if k.Now != nil { + at = k.Now() + } + f := meshconditions.Finding{Scope: "seat", ID: id, Kind: kind, Subject: meshconditions.Subject{Scope: "seat", ID: id}, + Severity: meshconditions.FindingSeverityWarning, Summary: words, Headline: words, Explanation: words, + Source: kind, Observer: packageseats.Self, At: at.UTC()} + if k.open == nil { + k.open = map[string]meshconditions.Finding{} + } + k.open[key] = f + if err := k.Writer.Write(context.Background(), f); err != nil { + k.Logf("the finding %s could not be written: %v", key, err) + } +} + +// Observed is the findings open now, for the writer's statement (views.WriterConfig.Observe). +func (k *Findings) Observed(context.Context) (views.Observed[meshconditions.Finding], error) { + k.mu.Lock() + defer k.mu.Unlock() + var out views.Observed[meshconditions.Finding] + for _, f := range k.open { + out.Findings = append(out.Findings, f) + } + return out, nil +} diff --git a/modules/gitea/cmd/package-registry/forge.go b/modules/gitea/cmd/package-registry/forge.go index f752a4116..82fb0e69a 100644 --- a/modules/gitea/cmd/package-registry/forge.go +++ b/modules/gitea/cmd/package-registry/forge.go @@ -37,7 +37,7 @@ type Forge struct { // key in the module's findings state (packages-account.ts PACKAGES_TOKEN_REFUSED watches that key). const ( PackagesTokenRefused = "packages-token-refused" - PackagesTokenRefusedKey = "seat.gitea.packages-token-refused_write" + PackagesTokenRefusedKey = "seat.gitea.write.packages-token-refused" ) // wrote is a write's answer, said on Refused: a 401 raises the finding, a write the forge took clears it. @@ -86,7 +86,7 @@ func ForgeFromEnv() (*Forge, error) { } } f := &Forge{Client: c, Writer: &w, Owner: os.Getenv("MESH_NPM_OWNER"), Refused: func(raised bool) { - finding(PackagesTokenRefused, "write", "the forge refused the package seats' write token; the module mints it afresh", raised) + findings.Say(PackagesTokenRefused, "write", "the forge refused the package seats' write token; the module mints it afresh", raised) }} if f.Owner == "" { missing = append(missing, "MESH_NPM_OWNER") diff --git a/modules/gitea/cmd/package-registry/main.go b/modules/gitea/cmd/package-registry/main.go index 0869cfca1..ebcbed3ab 100644 --- a/modules/gitea/cmd/package-registry/main.go +++ b/modules/gitea/cmd/package-registry/main.go @@ -13,7 +13,9 @@ import ( "os" "strings" + meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/views" "gitea/internal/packageseats" ) @@ -23,7 +25,26 @@ import ( // of the module's seats as their verbs. The README names these two, and a test holds the two together. var ToolNames = []string{"go_packages", "go_retention", "go_incoming", "npm_packages", "npm_retention", "npm_incoming"} +// findings keeps this bundle's open findings for its writer (design 47 G §689). +var findings = &Findings{Logf: func(f string, a ...any) { fmt.Fprintf(os.Stderr, f+"\n", a...) }} + func main() { + // The bundle's findings go through the module's writer (design 47 G A-16). + w, err := views.NewStdioWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{ + Module: packageseats.Self, State: stdio.State("findings"), Kinds: findingKinds, Observe: findings.Observed}) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + findings.Writer = w + // the runtime answers the writer's subscription only once Serve has its channel + go func() { + <-stdio.Served() + if err := w.Start(context.Background()); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + }() forge := func() (*Forge, error) { return ForgeFromEnv() } holder := func() (*packageseats.Holder, error) { return busHolder(forge) } go followIdle(holder) diff --git a/modules/gitea/cmd/package-registry/seats.go b/modules/gitea/cmd/package-registry/seats.go index 4ec5770e8..336e473c7 100644 --- a/modules/gitea/cmd/package-registry/seats.go +++ b/modules/gitea/cmd/package-registry/seats.go @@ -22,14 +22,10 @@ import ( "io" "net/http" "net/url" - "os" "path" "strings" - "time" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" - meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" goreg "git.novox.be/novox/mesh-delivery/contracts/go/seats/gopackageregistry" npmreg "git.novox.be/novox/mesh-delivery/contracts/go/seats/npmpackageregistry" "git.novox.be/novox/mesh-delivery/sdk/parts" @@ -249,21 +245,6 @@ func sayOn(seat, event string, o packageseats.Outcome) error { return stdio.SeatPublish("mesh.seat."+seat+".event."+event, body, "") } -func finding(kind, subject, words string, raised bool) { - key := meshconditionsclient.Key(meshconditionsclient.ScopeSeat, packageseats.Self, kind+"."+strings.NewReplacer(".", "_", "@", "_", "/", "_").Replace(subject)) - if !raised { - _ = stdio.State(meshconditions.StateFindings).Delete(key) - return - } - f := meshconditions.FindingsByKeyValue{V: meshconditionsclient.FindingVersion, Key: key, Kind: kind, Ref: key[strings.LastIndex(key, ".")+1:], - Subject: meshconditions.Subject{Scope: meshconditionsclient.ScopeSeat, ID: packageseats.Self}, Severity: meshconditionsclient.Warning, - Summary: words, Headline: words, Explanation: words, Resolved: "cleared once it no longer holds", - Source: kind, Observer: packageseats.Self, At: time.Now().UTC()} - if _, err := stdio.State(meshconditions.StateFindings).Put(key, f); err != nil { - fmt.Fprintf(os.Stderr, "the finding %s could not be written: %v\n", key, err) - } -} - // recordAct records a hand act through the seat mesh-hand-acts alone. func recordAct(q meshhandactsclient.Request) error { _, err := meshhandactsclient.Recorder{Ask: stdio.Ask}.Record(q) @@ -277,7 +258,7 @@ var boundCap = parts.BusCap(packageseats.IncomingObjects) func busHolder(forge func() (*Forge, error)) (*packageseats.Holder, error) { return &packageseats.Holder{Objects: parts.BusObjects(packageseats.IncomingObjects), Transfers: parts.BusTransfers[packageseats.Opening](packageseats.TransfersState), - Builder: packageseats.Reserved{Ask: stdio.Ask}, Store: seatStore{forge}, Say: say, Finding: finding, + Builder: packageseats.Reserved{Ask: stdio.Ask}, Store: seatStore{forge}, Say: say, Finding: findings.Say, Cap: boundCap, Record: recordAct, Idle: parts.BusArm(packageseats.IdleState)}, nil } diff --git a/modules/gitea/cmd/package-registry/seats_test.go b/modules/gitea/cmd/package-registry/seats_test.go index 6deb1ab47..13d4fbd0f 100644 --- a/modules/gitea/cmd/package-registry/seats_test.go +++ b/modules/gitea/cmd/package-registry/seats_test.go @@ -19,8 +19,10 @@ import ( "testing" "time" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" + keyparts "git.novox.be/novox/mesh-sdk/go/keys" + "git.novox.be/novox/mesh-sdk/go/views" "gitea/internal/forgehttp" + "gitea/internal/packageseats" ) @@ -133,7 +135,7 @@ func TestThePackageSeatsStoreIntoTheForge(t *testing.T) { // and the next write the forge takes clears it (no timer looks at the token). var refused []bool forge.Refused = func(raised bool) { refused = append(refused, raised) } - if k := meshconditionsclient.Key(meshconditionsclient.ScopeSeat, packageseats.Self, PackagesTokenRefused+".write"); k != PackagesTokenRefusedKey { + if k := views.FindingKey("seat", keyparts.EscapeName(packageseats.Self)+".write", PackagesTokenRefused); k != PackagesTokenRefusedKey { t.Errorf("the refusal's key is %s, the watcher watches %s", k, PackagesTokenRefusedKey) } _ = os.WriteFile(writer.TokenFile, []byte("stale\n"), 0o600) diff --git a/modules/gitea/cmd/package-registry/whole_integration_test.go b/modules/gitea/cmd/package-registry/whole_integration_test.go index 5a52ce7a4..ec90b8933 100644 --- a/modules/gitea/cmd/package-registry/whole_integration_test.go +++ b/modules/gitea/cmd/package-registry/whole_integration_test.go @@ -127,7 +127,10 @@ func (p *player) handle(m wire) { p.answer(m.ID, map[string]any{"module": "m", "version": q.Version, "artifacts": []map[string]any{{"artifact": "m:a", "digest": d}}}, nil) case m.Method == "mesh/seat.publish": p.answer(m.ID, map[string]any{}, p.nc.Publish(a.Subject, a.Body)) - case m.Method == "mesh/publish": + case m.Method == "mesh/capabilities": + p.answer(m.ID, map[string]any{"words": []string{"put-sessions", "finding-calls"}}, nil) + case m.Method == "mesh/publish", m.Method == "mesh/subscribe": + // the module's finding writer emits its events and subscribes to findings-asked (design 47 G A-16); none is played p.answer(m.ID, map[string]any{}, nil) case strings.HasPrefix(m.Method, "mesh/objects."): p.objects(m, a.Objects, a.Name, a.Data) @@ -307,7 +310,7 @@ func TestThePackageSeatsWhole(t *testing.T) { return file } cmd := exec.CommandContext(ctx, bin) - cmd.Env = append(os.Environ(), "MESH_GITEA_URL="+srv.URL, "MESH_NPM_OWNER=acme", "MESH_GITEA_TOOLS_USER=mesh-tools", + cmd.Env = append(os.Environ(), "MESH_NODE=novox", "MESH_GITEA_URL="+srv.URL, "MESH_NPM_OWNER=acme", "MESH_GITEA_TOOLS_USER=mesh-tools", "MESH_GITEA_TOKEN_FILE="+kept("token", "reader", "read:package,read:user,write:issue,write:repository for mesh-tools"), "MESH_GITEA_PACKAGES_TOKEN_FILE="+kept("packages-token", "writer", "write:package for mesh-packages")) cmd.Stderr = os.Stderr diff --git a/modules/gitea/go.mod b/modules/gitea/go.mod index d84ba67cf..11e94c655 100644 --- a/modules/gitea/go.mod +++ b/modules/gitea/go.mod @@ -3,7 +3,6 @@ module gitea go 1.26.0 require ( - git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient 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/provisioning/go/clients/meshprovisionerclient v0.0.0-00010101000000-000000000000 diff --git a/modules/gitea/packages-account.ts b/modules/gitea/packages-account.ts index ffbfab7b9..ac0ec296b 100644 --- a/modules/gitea/packages-account.ts +++ b/modules/gitea/packages-account.ts @@ -89,7 +89,7 @@ export async function keepPackagesToken(source: MintedToken, url: string, owner } /** The finding the Go bundle raises in the module's findings state when the forge refuses the write token (cmd/package-registry PackagesTokenRefusedKey). */ -export const PACKAGES_TOKEN_REFUSED = "seat.gitea.packages-token-refused_write"; +export const PACKAGES_TOKEN_REFUSED = "seat.gitea.write.packages-token-refused"; /** One change of the findings state, as its watch hands it. */ export interface FindingChange { readonly key: string; readonly op: "put" | "delete" } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/main.go b/modules/mesh-build-go/cmd/mesh-build-go/main.go index 46872509f..a571e8538 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/main.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/main.go @@ -73,14 +73,19 @@ func main() { if m.Node != "" { go m.Start(ctx) findings, err := newFindingsWriter() - if err == nil { - err = findings.Start(ctx) - } if err != nil { logf("the finding writer: %v", err) os.Exit(1) } manifestFindings = views.WriterFindings{Writer: findings} + // the runtime answers the writer's subscription only once Serve has its channel + go func() { + <-stdio.Served() + if err := findings.Start(ctx); err != nil { + logf("the finding writer: %v", err) + os.Exit(1) + } + }() } go startKeeper(ctx) // Every state it keeps, said on the bus before it serves (ADR 0495 §2; issue 1790). diff --git a/modules/mesh-build-go/go.mod b/modules/mesh-build-go/go.mod index 87a97efea..a3591514d 100644 --- a/modules/mesh-build-go/go.mod +++ b/modules/mesh-build-go/go.mod @@ -101,7 +101,7 @@ require git.novox.be/novox/mesh-catalog/modules/ssh-client/sshkeys v0.0.0-000101 replace git.novox.be/novox/mesh-catalog/modules/ssh-client/sshkeys => ../ssh-client/sshkeys require ( - git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions v0.0.0-00010101000000-000000000000 // indirect + git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/contracts/go/clients/gopackageregistryclient v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/contracts/go/clients/npmpackageregistryclient v0.0.0-00010101000000-000000000000 // indirect diff --git a/modules/mesh-config/cmd/mesh-config/config.go b/modules/mesh-config/cmd/mesh-config/config.go index a745180e4..0b62393dd 100644 --- a/modules/mesh-config/cmd/mesh-config/config.go +++ b/modules/mesh-config/cmd/mesh-config/config.go @@ -86,6 +86,10 @@ type Config struct { Logf func(format string, a ...any) mu sync.Mutex + // Findings is the module's finding writer (design 47 G §689); nil, no finding is said. findMu guards the findings open. + Findings FindingsWriter + findMu sync.Mutex + open map[string]openFinding // withheld is why the newest list of predefined keys was not published, empty when it was (publish). withheld string diff --git a/modules/mesh-config/cmd/mesh-config/findings.go b/modules/mesh-config/cmd/mesh-config/findings.go index 3f9c1482d..42c4d7fd5 100644 --- a/modules/mesh-config/cmd/mesh-config/findings.go +++ b/modules/mesh-config/cmd/mesh-config/findings.go @@ -5,17 +5,19 @@ package main // is gone. The keeper raises and clears the condition of the same key (mesh-conditions' Observers and Homes). import ( + "context" "fmt" "slices" "strings" "time" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" + "git.novox.be/novox/mesh-sdk/go/keys" + "git.novox.be/novox/mesh-sdk/go/views" ) // FindingsState is the state, meshconditions.StateFindings. -const FindingsState = meshconditions.StateFindings +const FindingsState = "findings" // The kinds of finding mesh-config writes. const ( @@ -31,89 +33,116 @@ const ( FindingRetireHeld = "retirement-held" ) -// findingKey is the key of a finding of kind about a module on a node: the condition's key. -func findingKey(kind, module, node, ref string) string { - id := module +// findingID is the id of a finding about a module on a node: the module, the node when there is one, and the ref +// (`..`), each escaped as a key's name is, since the kind is a fixed word (design 47 G A-14). +func findingID(module, node, ref string) string { + id := keys.EscapeName(module) if node != "" { - id += "." + node + id += "." + keys.EscapeName(node) } - return meshconditionsclient.Key(meshconditionsclient.ScopeModule, id, kind+"-"+ref) + return id + "." + keys.EscapeName(ref) } -func finding(kind, module, node, ref, summary, headline, explanation, needs, resolved, said string, at time.Time) meshconditions.FindingsByKeyValue { - id := module - if node != "" { - id += "." + node - } - return meshconditions.FindingsByKeyValue{V: meshconditionsclient.FindingVersion, Key: findingKey(kind, module, node, ref), Kind: kind, Ref: kind + "-" + ref, - Subject: meshconditions.Subject{Scope: meshconditionsclient.ScopeModule, ID: id, Machine: node}, - Severity: meshconditionsclient.Warning, Resolver: meshconditionsclient.ResolverOperator, Summary: summary, Headline: headline, - Explanation: explanation, Needs: needs, Resolved: resolved, +func finding(kind, module, node, ref, summary, headline, explanation, needs, said string, at time.Time) meshconditions.Finding { + id := findingID(module, node, ref) + return meshconditions.Finding{Scope: "module", ID: id, Kind: kind, + Subject: meshconditions.Subject{Scope: "module", ID: id, Machine: node}, + Severity: meshconditions.FindingSeverityWarning, Resolver: "operator", Summary: summary, Headline: headline, + Explanation: explanation, Needs: needs, Evidence: []meshconditions.Evidence{{At: at.UTC(), Said: said}}, Source: "config", Observer: Module, At: at.UTC()} } // renameRefused is the finding of a move mesh-config refused. -func renameRefused(module, to, from, why string, at time.Time) meshconditions.FindingsByKeyValue { +func renameRefused(module, to, from, why string, at time.Time) meshconditions.Finding { return finding(FindingRenameRefused, module, "", to, fmt.Sprintf("the rename of %s's config value %s to %s is refused: %s", module, from, to, why), fmt.Sprintf("%s's value %s is not moved to %s", module, from, to), fmt.Sprintf("The new version of %s names %s as the new name of its config value %s, and mesh-config refused to move the value: %s. Nothing was moved.", module, to, from, why), fmt.Sprintf("put the cause right, then move it with config_rename_again (module %s, to %s); the details name it.", module, to), - fmt.Sprintf("Resolved: %s's value %s is under %s", module, from, to), why, at) + why, at) } // windowHeld is the finding of a rename window held open past HeldFinding by a node. -func windowHeld(r Rename, node string, at time.Time) meshconditions.FindingsByKeyValue { +func windowHeld(r Rename, node string, at time.Time) meshconditions.Finding { return finding(FindingWindowHeld, r.Module, node, r.To, fmt.Sprintf("%s on %s has held the old config name %s open for %d days", r.Module, node, r.From, int(HeldFinding.Hours()/24)), fmt.Sprintf("%s on %s still runs the old config name", r.Module, node), fmt.Sprintf("The rename of %s's config value %s to %s is not complete on %s: its node-engine has not reported a build that reads %s (it may be away).", r.Module, r.From, r.To, node, r.To), fmt.Sprintf("bring %s back, or drop it from the window with config_rename_close.", node), - fmt.Sprintf("Resolved: %s reads %s on %s", r.Module, r.To, node), fmt.Sprintf("the window opened %s and %s has not reported a build reading %s", r.Nodes[node].Since.UTC().Format(time.RFC3339), node, r.To), at) } -// setFinding writes a finding when it differs from the one kept. -func (c *Config) setFinding(f meshconditions.FindingsByKeyValue) { - if err := meshconditionsclient.CheckFinding(f); err != nil { +// FindingsWriter is the module's finding writer (views.Writer), through which mesh-config writes and clears its findings +// (design 47 G §689). +type FindingsWriter interface { + Write(ctx context.Context, finding meshconditions.Finding) error + Clear(ctx context.Context, scope, id, kind, why string) error +} + +// openFinding is a finding written and not yet cleared, with the parts of its id. +type openFinding struct { + f meshconditions.Finding + module, ref string +} + +// setFinding writes a finding when it differs from the one written; the module and ref are the parts of its id. +func (c *Config) setFinding(module, ref string, f meshconditions.Finding) { + if c.Findings == nil { + return + } + if err := views.CheckFinding(f); err != nil { c.logf("[mesh-config] a finding is not valid: %v", err) return } - if e, err := c.Store.Get(FindingsState, f.Key); err == nil && e != nil { - if old, derr := meshconditions.DecodeFindingsByKeyValue(e.Value); derr == nil && old.Summary == f.Summary { - return // what it says has not changed - } + key := views.FindingKey(f.Scope, f.ID, f.Kind) + c.findMu.Lock() + defer c.findMu.Unlock() + if old, ok := c.open[key]; ok && old.f.Summary == f.Summary { + return // what it says has not changed } - if err := c.Store.Put(FindingsState, f.Key, f); err != nil { - c.logf("[mesh-config] the finding %s is not written: %v", f.Key, err) + if err := c.Findings.Write(context.Background(), f); err != nil { + c.logf("[mesh-config] the finding %s is not written: %v", key, err) + return + } + if c.open == nil { + c.open = map[string]openFinding{} + } + c.open[key] = openFinding{f: f, module: module, ref: ref} +} + +// clearFinding clears the findings of a kind and ref about a module, on whatever node but those in keep. +func (c *Config) clearFinding(kind, module, ref string, keep ...string) { + if c.Findings == nil { + return + } + c.findMu.Lock() + defer c.findMu.Unlock() + for k, o := range c.open { + if o.f.Kind != kind || o.ref != ref || o.module != module || + (o.f.Subject.Machine != "" && slices.Contains(keep, o.f.Subject.Machine)) { + continue + } + if err := c.Findings.Clear(context.Background(), o.f.Scope, o.f.ID, o.f.Kind, "the fault is gone"); err != nil { + c.logf("[mesh-config] the finding %s is not cleared: %v", k, err) + continue + } + delete(c.open, k) } } -// clearFinding deletes the findings of a kind and ref about a module, on whatever node but those in keep. -func (c *Config) clearFinding(kind, module, ref string, keep ...string) { - keys, err := c.Store.Keys(FindingsState) - if err != nil { - return - } - want := kind + "-" + ref - for _, k := range keys { - e, err := c.Store.Get(FindingsState, k) - if err != nil || e == nil { - continue - } - f, err := meshconditions.DecodeFindingsByKeyValue(e.Value) - if err != nil || f.Ref != want || !(f.Subject.ID == module || strings.HasPrefix(f.Subject.ID, module+".")) || - (f.Subject.Machine != "" && slices.Contains(keep, f.Subject.Machine)) { - continue - } - if err := c.Store.Delete(FindingsState, k); err != nil { - c.logf("[mesh-config] the finding %s is not cleared: %v", k, err) - } +// FindingsObserved is the findings open now, for the writer's statement (views.WriterConfig.Observe). +func (c *Config) FindingsObserved(context.Context) (views.Observed[meshconditions.Finding], error) { + c.findMu.Lock() + defer c.findMu.Unlock() + var out views.Observed[meshconditions.Finding] + for _, o := range c.open { + out.Findings = append(out.Findings, o.f) } + return out, nil } // retirementHeld is the finding of a retirement that waits seven days for nodes that still run a build reading the value. -func retirementHeld(e Retiring, at time.Time) meshconditions.FindingsByKeyValue { +func retirementHeld(e Retiring, at time.Time) meshconditions.Finding { node := "" if len(e.Nodes) > 0 { node = e.Nodes[0] @@ -123,17 +152,15 @@ func retirementHeld(e Retiring, at time.Time) meshconditions.FindingsByKeyValue fmt.Sprintf("%s's %s cannot be retired yet", e.Module, e.Key), fmt.Sprintf("No manifest reads %s's %s any more, but %s still run a build that does, so the operator is not asked to remove it.", e.Module, e.Key, strings.Join(e.Nodes, ", ")), "bring those nodes up to date, or cancel the retirement with config_retiring.", - fmt.Sprintf("Resolved: no node reads %s's %s", e.Module, e.Key), "retiring since "+e.Since.UTC().Format(time.RFC3339)+", still read on "+strings.Join(e.Nodes, ", "), at) } // retireUnanswered is the finding of a retirement asked twice and not answered. -func retireUnanswered(e Retiring, at time.Time) meshconditions.FindingsByKeyValue { +func retireUnanswered(e Retiring, at time.Time) meshconditions.Finding { return finding(FindingRetireUnanswered, e.Module, "", e.Key, fmt.Sprintf("the operator did not answer the ask to remove %s's %s, asked twice", e.Module, e.Key), fmt.Sprintf("%s's %s waits for an answer", e.Module, e.Key), fmt.Sprintf("No manifest reads %s's %s any more. The operator was asked twice whether to remove it and did not answer, so it stays.", e.Module, e.Key), "answer the ask again with config_retiring ask-again, or cancel it.", - fmt.Sprintf("Resolved: %s's %s is answered or read again", e.Module, e.Key), "asked "+e.Asked.UTC().Format(time.RFC3339)+" and once more", at) } diff --git a/modules/mesh-config/cmd/mesh-config/main.go b/modules/mesh-config/cmd/mesh-config/main.go index 16a839755..e45094a12 100644 --- a/modules/mesh-config/cmd/mesh-config/main.go +++ b/modules/mesh-config/cmd/mesh-config/main.go @@ -18,10 +18,11 @@ import ( "strings" "time" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" + meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/views" ) // Seat is the role this module holds, once for the mesh, and Verb its one verb. @@ -120,13 +121,30 @@ func (runtimeAsks) Warranted(ask string) error { func main() { c := &Config{Store: runtimeStore{}, 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 writer (design 47 G §689, A-16): the renames' and retirements' findings and the views reader's go through it. + w, err := views.NewStdioWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{ + Module: Module, State: stdio.State(FindingsState), Observe: c.FindingsObserved, + Kinds: []string{FindingRenameRefused, FindingWindowHeld, FindingRetireUnanswered, FindingRetireHeld}}) + if err != nil { + logf("%v", err) + os.Exit(1) + } + c.Findings = w moved := make(chan struct{}, 1) theViews = newOwnerViews(stdio.Ask, func() { select { case moved <- struct{}{}: default: } - }, meshconditionsclient.FindingWriter{State: stdio.State(FindingsState)}) + }, views.WriterFindings{Writer: w}) + // the runtime answers the writer's subscription only once Serve has its channel + go func() { + <-stdio.Served() + if err := w.Start(context.Background()); err != nil { + logf("%v", err) + os.Exit(1) + } + }() theViews.Start(context.Background()) go resolving(c, moved) go hearing(c) diff --git a/modules/mesh-config/cmd/mesh-config/renames.go b/modules/mesh-config/cmd/mesh-config/renames.go index a22de1506..f7338a38a 100644 --- a/modules/mesh-config/cmd/mesh-config/renames.go +++ b/modules/mesh-config/cmd/mesh-config/renames.go @@ -155,7 +155,7 @@ func (c *Config) rename(s *Sources, held map[string]Rename, trunk ModuleDeclarat nodes := nodesPlaced(s, module) var said []string refuse := func(why string) []string { - c.setFinding(renameRefused(module, to, from, why, now)) + c.setFinding(module, to, renameRefused(module, to, from, why, now)) if r, ok := held[key]; ok { s.Renames[key] = r } @@ -488,7 +488,7 @@ func (c *Config) sweepRenameFindings(s *Sources, now time.Time) { } c.clearFinding(FindingWindowHeld, r.Module, r.To, held...) for _, node := range held { - c.setFinding(windowHeld(r, node, now)) + c.setFinding(r.Module, r.To, windowHeld(r, node, now)) } } } diff --git a/modules/mesh-config/cmd/mesh-config/renames_test.go b/modules/mesh-config/cmd/mesh-config/renames_test.go index e4b5d3a27..7f7c6557d 100644 --- a/modules/mesh-config/cmd/mesh-config/renames_test.go +++ b/modules/mesh-config/cmd/mesh-config/renames_test.go @@ -1,6 +1,7 @@ package main import ( + "context" "encoding/json" "errors" "fmt" @@ -12,6 +13,7 @@ import ( meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" 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" + "git.novox.be/novox/mesh-sdk/go/views" ) // fakeDelivery is the delivery parts as mesh-config asks them: each node's have, whether a node-engine states what it @@ -78,20 +80,20 @@ func aRenamingMesh(t *testing.T) (*Config, *memStore, *fakeDelivery, *time.Time) } clock := time.Date(2026, 11, 4, 10, 0, 0, 0, time.UTC) d := &fakeDelivery{haves: map[string]string{"g14.player": fp1, "novox.player": fp1}, notSays: map[string]bool{}} - c := &Config{Store: m, Now: func() time.Time { return clock }, Logf: t.Logf, Delivery: d} + c := &Config{Store: m, Now: func() time.Time { return clock }, Logf: t.Logf, Delivery: d, Findings: m} if _, err := c.Reconcile(); err != nil { t.Fatal(err) } return c, m, d, &clock } -func findingsOf(t *testing.T, m *memStore, kind string) []meshconditions.FindingsByKeyValue { +func findingsOf(t *testing.T, m *memStore, kind string) []meshconditions.Finding { t.Helper() - var out []meshconditions.FindingsByKeyValue + var out []meshconditions.Finding for _, k := range m.keysOf(FindingsState) { e, _ := m.Get(FindingsState, k) - f, err := meshconditions.DecodeFindingsByKeyValue(e.Value) - if err != nil { + var f meshconditions.Finding + if err := json.Unmarshal(e.Value, &f); err != nil { t.Fatalf("finding %s: %v", k, err) } if f.Kind == kind { @@ -435,3 +437,12 @@ func TestARetirementDeclinedUnansweredCancelledOrASecret(t *testing.T) { } _ = fmt.Sprint } + +// Write and Clear are the module's finding writer in a test: a finding is kept in its key and deleted as views.Writer does. +func (m *memStore) Write(_ context.Context, f meshconditions.Finding) error { + return m.Put(FindingsState, views.FindingKey(f.Scope, f.ID, f.Kind), f) +} + +func (m *memStore) Clear(_ context.Context, scope, id, kind, _ string) error { + return m.Delete(FindingsState, views.FindingKey(scope, id, kind)) +} diff --git a/modules/mesh-config/cmd/mesh-config/retiring.go b/modules/mesh-config/cmd/mesh-config/retiring.go index dc010dd74..6f6bd2c16 100644 --- a/modules/mesh-config/cmd/mesh-config/retiring.go +++ b/modules/mesh-config/cmd/mesh-config/retiring.go @@ -235,7 +235,7 @@ func (c *Config) advanceRetiring(s Sources, e *Retiring) []string { return []string{fmt.Sprintf("the retirement %s is not kept: %v", key, err)} } if now.Sub(e.Since) >= HeldFinding { - c.setFinding(retirementHeld(*e, now)) + c.setFinding(e.Module, e.Key, retirementHeld(*e, now)) } return []string{fmt.Sprintf("%s's %s is not read by any manifest, and %s still run a build that reads it", e.Module, e.Key, strings.Join(e.Nodes, ", "))} @@ -410,7 +410,7 @@ func (c *Config) decideRetirement(e *Retiring, now time.Time) []string { if w.Outcome != occ.OutcomeChosen { out := keep(RetireUnanswered, "the ask "+string(w.Outcome)+"; nothing is removed") if e.AskedAgain { - c.setFinding(retireUnanswered(*e, now)) + c.setFinding(e.Module, e.Key, retireUnanswered(*e, now)) } return out } 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 a8d253d6e..407535096 100644 --- a/modules/mesh-config/cmd/mesh-config/whole_integration_test.go +++ b/modules/mesh-config/cmd/mesh-config/whole_integration_test.go @@ -726,7 +726,8 @@ func TestConfigWholeRenamesAndRetiresAgainstTheBus(t *testing.T) { keys, _ := (func() ([]string, error) { kv, _ := p.kv(FindingsState); return kv.Keys(p.ctx) })() for _, k := range keys { raw, _ := p.get(FindingsState, k) - if f, err := meshconditions.DecodeFindingsByKeyValue(raw); err == nil && f.Kind == FindingRenameRefused && strings.Contains(f.Summary, "dock") { + var f meshconditions.Finding + if err := json.Unmarshal(raw, &f); err == nil && f.Kind == FindingRenameRefused && strings.Contains(f.Summary, "dock") { return true } } diff --git a/modules/mesh-config/go.mod b/modules/mesh-config/go.mod index fc6a03485..ced63103d 100644 --- a/modules/mesh-config/go.mod +++ b/modules/mesh-config/go.mod @@ -9,7 +9,6 @@ require ( git.novox.be/novox/mesh-contracts/go/seats/meshconfig v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner v0.0.0-00010101000000-000000000000 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/meshconditionsclient 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 diff --git a/modules/mesh-provisioner/cmd/mesh-provisioner/main.go b/modules/mesh-provisioner/cmd/mesh-provisioner/main.go index 5220c92d1..8ffad9c06 100644 --- a/modules/mesh-provisioner/cmd/mesh-provisioner/main.go +++ b/modules/mesh-provisioner/cmd/mesh-provisioner/main.go @@ -246,10 +246,14 @@ func main() { } return e.Value, true, nil }) - if err := findings.Start(context.Background()); err != nil { - logf("%v", err) - os.Exit(1) - } + // the runtime answers the writer's subscription only once Serve has its channel + go func() { + <-stdio.Served() + if err := findings.Start(context.Background()); err != nil { + logf("%v", err) + os.Exit(1) + } + }() v.Start(context.Background()) h := &Holder{Store: Watching(s, ws), Asks: runtimeAsks{}, Now: time.Now, Logf: logf} h.Compose = FromCompositions(h.Store) // the node-engines' published compositions, from the copy (ADR 0593 §8) diff --git a/modules/mesh-vault/cmd/mesh-vault/main.go b/modules/mesh-vault/cmd/mesh-vault/main.go index c6dc697f9..171e66138 100644 --- a/modules/mesh-vault/cmd/mesh-vault/main.go +++ b/modules/mesh-vault/cmd/mesh-vault/main.go @@ -8,8 +8,9 @@ package main import ( - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" + meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" + "context" "encoding/json" "errors" @@ -118,12 +119,25 @@ func watchLeavers(env *Env) bool { return true } if assignmentsView == nil { + // The vault's finding writer (design 47 G §689, A-16): it states the assignments view's findings and observes none of + // its own, and names no secret. + w, err := views.NewStdioWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{ + Module: Seat, State: stdio.State(FindingsState), + Observe: func(context.Context) (views.Observed[meshconditions.Finding], error) { + return views.Observed[meshconditions.Finding]{}, nil + }}) + if err == nil { + err = w.Start(context.Background()) + } + if err != nil { + return false + } assignmentsView = newAssignmentsView(stdio.Ask, func(key string) { if node, ok := leaverKey(key); ok { env.Vault.NodeLeft(node) } }, env.Vault.AssignmentsChanged, - meshconditionsclient.FindingWriter{State: stdio.State(FindingsState)}) + views.WriterFindings{Writer: w}) beginAssignmentsView(assignmentsView) } return true diff --git a/modules/mesh-vault/cmd/mesh-vault/vault_test.go b/modules/mesh-vault/cmd/mesh-vault/vault_test.go index 68def3bd5..d953d15db 100644 --- a/modules/mesh-vault/cmd/mesh-vault/vault_test.go +++ b/modules/mesh-vault/cmd/mesh-vault/vault_test.go @@ -198,7 +198,7 @@ func TestNoBucketHoldsASecret(t *testing.T) { code = []byte(strings.Replace(string(code), "stdio.State(WantedRead).Watch(", "", 1)) code = []byte(strings.Replace(string(code), "stdio.State(WantedRead).Get(", "", 1)) // And its findings (ADR 0615 §6): the finding of a first view unanswered, written and deleted, never a secret. - code = []byte(strings.Replace(string(code), "meshconditionsclient.FindingWriter{State: stdio.State(FindingsState)}", "", 1)) + code = []byte(strings.Replace(string(code), "State: stdio.State(FindingsState)", "", 1)) // And its outcome entries (novox/hq ADR 0570 clauses 3, 6): a value's name and generation, never a value; and // one read of the seat's provisions, which hold none. for _, call := range []string{"Get(", "Put(", "Delete(", "Keys("} { diff --git a/modules/mesh-vault/go.mod b/modules/mesh-vault/go.mod index b09f9aeb8..b9bd08189 100644 --- a/modules/mesh-vault/go.mod +++ b/modules/mesh-vault/go.mod @@ -38,11 +38,10 @@ require ( ) require ( - git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-networking/modules/mesh-wireguard/go v0.0.0-00010101000000-000000000000 ) require ( - git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions v0.0.0-00010101000000-000000000000 // indirect + git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/theprivatenetwork v0.0.0-00010101000000-000000000000 // indirect ) diff --git a/modules/mesh-watcher/cmd/mesh-watcher/main.go b/modules/mesh-watcher/cmd/mesh-watcher/main.go index b4152ba21..d3a34095c 100644 --- a/modules/mesh-watcher/cmd/mesh-watcher/main.go +++ b/modules/mesh-watcher/cmd/mesh-watcher/main.go @@ -84,10 +84,14 @@ func main() { } assignments.Findings = views.WriterFindings{Writer: findings} assignments.Follow(assignerModule, mesh.StateSeatHolders, SeatHoldersTool) - if err := findings.Start(context.Background()); err != nil { - logf("%v", err) - os.Exit(1) - } + // the runtime answers the writer's subscription only once Serve has its channel + go func() { + <-stdio.Served() + if err := findings.Start(context.Background()); err != nil { + logf("%v", err) + os.Exit(1) + } + }() go run(w, l, node, assignments) if err := stdio.Serve("", append(tools(w, l), assignments.Tools("watcher")...)); err != nil { logf("%v", err) diff --git a/modules/minio/cmd/object-store/main.go b/modules/minio/cmd/object-store/main.go index 6b9f7712b..193c790bb 100644 --- a/modules/minio/cmd/object-store/main.go +++ b/modules/minio/cmd/object-store/main.go @@ -44,10 +44,14 @@ func main() { fmt.Fprintf(os.Stderr, "[object-store] %v\n", err) os.Exit(1) } - if err := findings.Start(context.Background()); err != nil { - fmt.Fprintf(os.Stderr, "[object-store] %v\n", err) - os.Exit(1) - } + // the runtime answers the writer's subscription only once Serve has its channel + go func() { + <-stdio.Served() + if err := findings.Start(context.Background()); err != nil { + fmt.Fprintf(os.Stderr, "[object-store] %v\n", err) + os.Exit(1) + } + }() seats.Start(context.Background()) if err := stdio.Serve("", append(tools(s), seats.Tools("object_store")...)); err != nil { fmt.Fprintf(os.Stderr, "[object-store] %v\n", err) diff --git a/modules/nfs-server/cmd/nfs-server/main.go b/modules/nfs-server/cmd/nfs-server/main.go index 17feebc89..3a85efeb4 100644 --- a/modules/nfs-server/cmd/nfs-server/main.go +++ b/modules/nfs-server/cmd/nfs-server/main.go @@ -63,10 +63,14 @@ func main() { seats.Findings = views.WriterFindings{Writer: w} seats.Follow(assignerModule, mesh.StateSeatHolders, SeatHoldersTool) seats.Start(context.Background()) - if err := w.Start(context.Background()); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } + // the runtime answers the writer's subscription only once Serve has its channel + go func() { + <-stdio.Served() + if err := w.Start(context.Background()); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + }() go ShareExports(m) if err := stdio.Serve("", append(Tools(m), seats.Tools("nfs_server")...)); err != nil { fmt.Fprintln(os.Stderr, err)