Files
mesh-delivery/modules/mesh-delivery-indexer/judge/oldsections.go
T

106 lines
4.1 KiB
Go

package judge
import (
"fmt"
"slices"
"strings"
"git.novox.be/novox/mesh-sdk/go/catalogue"
)
// The old sections end when no module is left in them (issue 1966 D4; novox/hq ADR 0595 §11): the indexer counts, per old
// section, the accepted modules using it, the foundation left out, and the judge refuses a manifest of another module
// that uses a section whose count is 0. The foundation (the bus module, the grants part and the indexer itself) is
// created before the bus module can create anything, so it keeps the old sections for good and is always accepted.
// OldSections are the sections of a manifest that keep bus state in the old forms.
var OldSections = []string{"state", "logs", "streams", "objects"}
// Foundation are the modules that keep using the old sections for good: exactly the nine parts the bus module's own
// composition, assignments, build or delivery needs before it can answer (novox/hq ADR 0619 §1, §3): the bus module, the
// grants part, the indexer, the assigner, the tool runner, the builder, the checker, mesh-config and the vault. Their new
// state goes on their old sections too, so a foundation manifest asking bus state in the new form is refused.
var Foundation = []string{"nats", "mesh-grants", "mesh-delivery-indexer", "mesh-delivery-assigner", "node-tools",
"mesh-delivery-builder", "mesh-delivery-checker", "mesh-config", "mesh-vault"}
// ViewOwners are the two owners whose state every reader reads through their first views and read tools, never by a
// `reads` entry (novox/hq ADR 0615 §1): CountOwnerReads counts what is left of those entries.
var ViewOwners = []string{"mesh-delivery-indexer", "mesh-delivery-assigner"}
// CountOwnerReads counts the `reads` entries on each view owner's state, by owner and by entry, across manifests: what
// the live row of the move reads 0 of (novox/hq ADR 0615 How it is checked). Every owner is in the answer, 0 where none.
func CountOwnerReads(manifests []catalogue.Manifest) map[string]int {
counts := map[string]int{}
for _, o := range ViewOwners {
counts[o] = 0
}
for _, m := range manifests {
for _, r := range m.Reads {
for _, o := range ViewOwners {
if strings.HasPrefix(r, o+".") {
counts[o]++
counts[r]++
}
}
}
}
return counts
}
// OldSectionCounts is, per old section, how many accepted modules outside the foundation use it. Nil is no count: the
// judge then refuses no use of a section.
type OldSectionCounts map[string]int
// SectionsUsed are the old sections a manifest uses, in OldSections' order.
func SectionsUsed(m catalogue.Manifest) []string {
var used []string
for _, s := range OldSections {
if (s == "state" && len(m.State) > 0) || (s == "logs" && len(m.Logs) > 0) ||
(s == "streams" && len(m.Streams) > 0) || (s == "objects" && len(m.Objects) > 0) {
used = append(used, s)
}
}
return used
}
// CountOldSections counts, per old section, the manifests using it, the foundation's left out; every section is in the
// answer, with 0 where none uses it.
func CountOldSections(manifests []catalogue.Manifest) OldSectionCounts {
counts := OldSectionCounts{}
for _, s := range OldSections {
counts[s] = 0
}
for _, m := range manifests {
if slices.Contains(Foundation, m.Module) {
continue
}
for _, s := range SectionsUsed(m) {
counts[s]++
}
}
return counts
}
// OldSectionProblems are the old sections a manifest uses that no accepted module outside the foundation uses any more.
func OldSectionProblems(m catalogue.Manifest, counts OldSectionCounts) []string {
if slices.Contains(Foundation, m.Module) {
if len(m.BusState) > 0 {
return []string{fmt.Sprintf("%s is a part of the foundation, which keeps its bus state in the old sections, new "+
"entries included: it asks none in `requires` by template (novox/hq ADR 0619 §1)", m.Module)}
}
return nil
}
if counts == nil {
return nil
}
var problems []string
for _, s := range SectionsUsed(m) {
if counts[s] == 0 {
problems = append(problems, fmt.Sprintf(
"%s uses the old section %s, which no other module uses any more (0 modules count); ask for it in "+
"`requires` by template (novox/hq ADR 0595 §11)", m.Module, s))
}
}
return problems
}