Design 47 H1: the indexer's writer owns the kinds its code raises, held by a test

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
This commit is contained in:
2026-11-21 11:50:27 +01:00
parent dedcc3ead7
commit aef8632d95
3 changed files with 160 additions and 1 deletions
@@ -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)
}
@@ -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)
}
}
}
@@ -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.