From aef8632d95d7436cc6c5069320bdf2d4b1f11554 Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 21 Nov 2026 11:30:35 +0100 Subject: [PATCH] Design 47 H1: the indexer's writer owns the kinds its code raises, held by a test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The daemon's writer owned every kind while the tool runner's writer shares the state `findings`; under G §580 each bundle states only its own kinds. The daemon's writer is now made with FindingKinds, the ten kinds its code raises, and a unit test reads the daemon's code for every kind it gives the writer and fails on one the list does not declare. Builds: design 47 H H-A-240, H-A-242 (2), H-A-233, G §580 --- .../cmd/mesh-delivery-indexer/main.go | 4 +- .../internal/index/finding_kinds_test.go | 151 ++++++++++++++++++ .../internal/index/findings.go | 6 + 3 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 modules/mesh-delivery-indexer/internal/index/finding_kinds_test.go diff --git a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/main.go b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/main.go index 76a24231..7045cdec 100644 --- a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/main.go +++ b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/main.go @@ -289,7 +289,9 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro // The indexer's findings are the seat mesh-conditions' generated finding, written through mesh-sdk's held finding writer // (design 47 G §689, A-29): it holds what is written before the state is bound, and starts once the state is. writer, err := views.NewHeldWriter(views.WriterConfig[mcseat.Finding, mcseat.FindingCleared, mcseat.FindingsStated, mcseat.FindingsAskedEvent]{ - Module: cred.Module, State: calls.State(index.FindingsState), Node: cred.Node, Asked: mcseat.SubjectFindingsAsked, JS: js, Conn: conn, Logf: logger.Printf}) + Module: cred.Module, State: calls.State(index.FindingsState), Node: cred.Node, Asked: mcseat.SubjectFindingsAsked, JS: js, Conn: conn, Logf: logger.Printf, + // its own kinds only (G §580, H-A-242): the tool runner's writer states its kinds over the same state + Kinds: index.FindingKinds}) if err != nil { return fmt.Errorf("the indexer's finding writer cannot be made: %w", err) } diff --git a/modules/mesh-delivery-indexer/internal/index/finding_kinds_test.go b/modules/mesh-delivery-indexer/internal/index/finding_kinds_test.go new file mode 100644 index 00000000..31310d7f --- /dev/null +++ b/modules/mesh-delivery-indexer/internal/index/finding_kinds_test.go @@ -0,0 +1,151 @@ +package index + +import ( + "go/ast" + "go/parser" + "go/token" + "os" + "path/filepath" + "slices" + "strconv" + "strings" + "testing" + + mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" + "git.novox.be/novox/mesh-sdk/go/views" +) + +// Design 47 H H-A-240, H-A-242, G §580: every finding kind the daemon's code raises or clears is one its writer owns +// (FindingKinds, and a views reader's views.ViewKinds), so the writer never refuses a kind of the indexer's own. The test +// reads the daemon's code (this package and cmd/mesh-delivery-indexer) for every place a kind is given to the writer: the +// kind argument of observed, finding, clearFinding, clearHeld and a writer's Clear, and the Kind of a Finding literal. A +// kind there is a string, a constant of the package or a constant named in imported below; any other form fails the test, +// naming its place, but for the pass-through parameter `kind`, a held finding's `.Kind` and the kind cut from a key a views +// reader names, which carry a kind given elsewhere. +func TestFindingKindsHoldEveryKindTheCodeRaises(t *testing.T) { + imported := map[string]string{"mcc.KindCallUnanswered": mcc.KindCallUnanswered, "views.ViewWaitsKind": views.ViewWaitsKind} + // kindArg is the place of the kind among a raising call's arguments. + kindArg := map[string]int{"observed": 2, "finding": 2, "clearFinding": 3, "clearHeld": 4, "Clear": 3} + fset := token.NewFileSet() + var files []*ast.File + consts := map[string]string{} + for _, dir := range []string{".", filepath.Join("..", "..", "cmd", "mesh-delivery-indexer")} { + entries, err := os.ReadDir(dir) + if err != nil { + t.Fatal(err) + } + for _, e := range entries { + if !strings.HasSuffix(e.Name(), ".go") || strings.HasSuffix(e.Name(), "_test.go") { + continue + } + f, err := parser.ParseFile(fset, filepath.Join(dir, e.Name()), nil, 0) + if err != nil { + t.Fatal(err) + } + files = append(files, f) + if dir != "." { + continue + } + for _, d := range f.Decls { + g, ok := d.(*ast.GenDecl) + if !ok || g.Tok != token.CONST { + continue + } + for _, s := range g.Specs { + v := s.(*ast.ValueSpec) + for i, n := range v.Names { + if i < len(v.Values) { + if l, ok := v.Values[i].(*ast.BasicLit); ok && l.Kind == token.STRING { + consts[n.Name], _ = strconv.Unquote(l.Value) + } + } + } + } + } + } + } + declared := append(slices.Clone(FindingKinds), views.ViewKinds...) + raised := map[string]string{} // kind -> a place it is raised + kindOf := func(e ast.Expr) (string, bool) { + switch x := e.(type) { + case *ast.BasicLit: + s, err := strconv.Unquote(x.Value) + return s, err == nil + case *ast.Ident: + if x.Name == "kind" { + return "", true // a pass-through parameter: its callers give the kind + } + s, ok := consts[x.Name] + if !ok { + s, ok = imported["index."+x.Name] + } + return s, ok + case *ast.SliceExpr: + return "", true // the kind of a key a views reader names: its own kinds (views.ViewKinds) + case *ast.SelectorExpr: + if x.Sel.Name == "Kind" { + return "", true // a held finding's kind, given when it was raised + } + if p, ok := x.X.(*ast.Ident); ok { + if p.Name == "index" { + s, ok := consts[x.Sel.Name] + return s, ok + } + s, ok := imported[p.Name+"."+x.Sel.Name] + return s, ok + } + } + return "", false + } + note := func(e ast.Expr) { + k, ok := kindOf(e) + if !ok { + t.Errorf("%s: a finding kind the test cannot read; give it as a constant (or name it in imported)", fset.Position(e.Pos())) + return + } + if k != "" { + raised[k] = fset.Position(e.Pos()).String() + } + } + for _, f := range files { + ast.Inspect(f, func(n ast.Node) bool { + switch x := n.(type) { + case *ast.CallExpr: + var name string + switch fn := x.Fun.(type) { + case *ast.Ident: + name = fn.Name + case *ast.SelectorExpr: + name = fn.Sel.Name + } + if i, ok := kindArg[name]; ok && len(x.Args) > i { + if name == "Clear" && len(x.Args) != 5 { + return true // not a finding writer's Clear(ctx, scope, id, kind, why) + } + note(x.Args[i]) + } + case *ast.CompositeLit: + if s, ok := x.Type.(*ast.SelectorExpr); ok && s.Sel.Name == "Finding" { + for _, el := range x.Elts { + if kv, ok := el.(*ast.KeyValueExpr); ok { + if id, ok := kv.Key.(*ast.Ident); ok && id.Name == "Kind" { + note(kv.Value) + } + } + } + } + } + return true + }) + } + for _, k := range []string{PinMissingKind, OutOfStepKind, PackagesNotPublishedKind} { + if _, ok := raised[k]; !ok { + t.Errorf("the test no longer finds the kind %s the code raises: it reads the code wrongly", k) + } + } + for k, at := range raised { + if !slices.Contains(declared, k) { + t.Errorf("%s: the code raises the finding kind %q, which the writer's list FindingKinds does not declare", at, k) + } + } +} diff --git a/modules/mesh-delivery-indexer/internal/index/findings.go b/modules/mesh-delivery-indexer/internal/index/findings.go index 761480f7..25df6af3 100644 --- a/modules/mesh-delivery-indexer/internal/index/findings.go +++ b/modules/mesh-delivery-indexer/internal/index/findings.go @@ -12,6 +12,12 @@ const ( FindingsState = "findings" ) +// FindingKinds are the finding kinds the indexer's daemon raises, the kinds its writer owns (design 47 G §580, H H-A-240, +// H-A-242): the tool runner's writer over the same state `findings` owns its own, and a views reader's (views.ViewKinds) are +// always the writer's besides. TestFindingKindsHoldEveryKindTheCodeRaises holds the code to this list. +var FindingKinds = []string{PinMissingKind, OutOfStepKind, PackagesNotPublishedKind, HistoryFillingKind, SeedWaitsKind, + SeedLeftOutKind, SeatDefinitionUnparsedKind, SeatDefinedTwiceKind, SeatOutsideItsDomainKind, mcc.KindCallUnanswered} + // FindingWriter is the indexer's finding writer, mesh-sdk's held writer of the seat mesh-conditions' generated finding (design // 47 G A-29, A-38): Write raises a finding, Clear clears one, Held answers the findings it holds, so the indexer reads what // stands from the writer.