Files
mesh-delivery/modules/mesh-delivery-checker/rules/seats.go
T
jschoubben 043e3fb979 Read seat definitions once: the indexer's reading becomes a module the checker requires (issue 1921)
The checking program's seat list parsed the definition format a second time beside the
indexer's judge. The indexer's reading moves whole into its own sibling module seatfiles
(a domain path, as takein), with ResolveRaw keeping every field as written for a request's
seats; the judge and the take-in read through it, and the checker's rules only find the
definitions and say why one is left out (the lead's ruling on D3, ADR 0634 §1).

Claude-Session: https://claude.ai/code/session_01NcbgfFSnn24M3GcqttoU7i
2026-11-06 22:39:31 +01:00

109 lines
3.8 KiB
Go

package rules
import (
"encoding/json"
"fmt"
"io/fs"
"maps"
"path"
"slices"
"strings"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-indexer/seatfiles"
)
// The seat list a check makes from its heads (novox/hq ADR 0636 §4, ADR 0644 §1; the lead's ruling Q2 on milestone
// 47/B0): the checking program built from the heads reads the seat definitions they carry and answers them resolved, so
// the running checker hands the manifest validator and the composer bytes it does not read itself. A definition is read
// by the indexer's one reading of the format (package seatfiles); this file finds the definitions and says why one is
// left out.
// SeatsVersion is the version of SeatsAnswer this program writes.
const SeatsVersion = 1
// SeatsAnswer is the checking program's `-seats` answer: every seat definition the heads carry, resolved (the verbs and
// events of the parts it includes put first, as the indexer keeps it), as the requests' field `seats` carries them; and
// each definition left out, with why.
type SeatsAnswer struct {
V int `json:"v"`
Seats []json.RawMessage `json:"seats"`
Left []LeftOut `json:"left,omitempty"`
}
// LeftOut is a seat definition the list does not hold: its file and why.
type LeftOut struct {
Path string `json:"path"`
Why string `json:"why"`
}
// SeatsOf reads the seat definitions in heads, a directory holding each head at its repository's path: a definition is a
// contracts module's `seats/<seat>/seat.json`, with the parts it includes from the `parts/<part>.json` beside that
// `seats` directory (design 26, *A seat's definition*). A file that is not a definition, a part it names that is not
// there, and a seat defined in more than one place are left out, said; heads with no definition give an empty list.
func SeatsOf(heads fs.FS) (SeatsAnswer, error) {
out := SeatsAnswer{V: SeatsVersion, Seats: []json.RawMessage{}}
var files []string
err := fs.WalkDir(heads, ".", func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() && (d.Name() == ".git" || d.Name() == "testdata") {
return fs.SkipDir
}
if !d.IsDir() && d.Name() == "seat.json" && path.Base(path.Dir(path.Dir(p))) == "seats" {
files = append(files, p)
}
return nil
})
if err != nil {
return SeatsAnswer{}, fmt.Errorf("rules: the heads cannot be read: %w", err)
}
type found struct {
path string
raw json.RawMessage
}
bySeat := map[string][]found{}
for _, p := range files {
seat, raw, err := resolvedDefinition(heads, p)
if err != nil {
out.Left = append(out.Left, LeftOut{Path: p, Why: err.Error()})
continue
}
bySeat[seat] = append(bySeat[seat], found{p, raw})
}
for _, seat := range slices.Sorted(maps.Keys(bySeat)) {
fs := bySeat[seat]
if len(fs) > 1 {
var paths []string
for _, f := range fs {
paths = append(paths, f.path)
}
for _, f := range fs {
out.Left = append(out.Left, LeftOut{Path: f.path, Why: "the seat " + seat + " is defined in more than one place: " + strings.Join(paths, ", ")})
}
continue
}
out.Seats = append(out.Seats, fs[0].raw)
}
return out, nil
}
// resolvedDefinition is the definition at p resolved with the parts it includes from the `parts` directory beside its
// `seats` directory, as the indexer's one reading of the format resolves it (seatfiles.ResolveRaw), every field kept as
// written.
func resolvedDefinition(heads fs.FS, p string) (string, json.RawMessage, error) {
raw, err := fs.ReadFile(heads, p)
if err != nil {
return "", nil, err
}
partsDir := path.Join(path.Dir(path.Dir(path.Dir(p))), "parts")
d, out, err := seatfiles.ResolveRaw(raw, func(name string) ([]byte, error) {
praw, err := fs.ReadFile(heads, path.Join(partsDir, name+".json"))
if err != nil {
return nil, fmt.Errorf("it is not at %s", partsDir)
}
return praw, nil
})
return d.Seat, out, err
}