Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/verdicts.go
T
jschoubben 0fffea89b1 B1 (indexer): the indexer's own name is its credential's module, no constant
index.Module is gone: the indexer and its registry carry the credential's module (findings' observer and ids, its own
entry and graph key, the history subject, the views emitter and reader), the tools take the served-as module.

Builds: design 47 G A-296, §428

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 23:37:54 +01:00

809 lines
34 KiB
Go

package index
import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"slices"
"strings"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"git.novox.be/novox/mesh-sdk/go/process"
"mesh-delivery-indexer/judge"
)
// The indexer judges every manifest it holds, once, with its own judge (novox/hq ADR 0578 §1): it writes each verdict in
// its state verdicts (VerdictsState), one key per manifest digest, only when the verdict differs from the one
// held; a module's value in the modules state carries a refusal (Verdict, Problems), so a reader of modules takes it
// without a second read; a new refusal is said as the event manifest-refused; a manifest with a field the judge does not know waits instead (ownBuildIsNewest). It judges at its start, so a judge that
// changed (a new indexer) judges every manifest again, and after every merge it takes in, since a rule between manifests
// can turn on any of them. Nothing polls.
// VerdictsState is the indexer's state of verdicts, its local name, as the tools bundle reads it.
const VerdictsState = "verdicts"
// Judged is what one pass of the judge did: the manifests judged, the verdicts written, and the modules refused.
type Judged struct {
Judged, Written int
Refused map[string][]string
// Outgrown are the modules accepted before whose problems are now warnings (ADR 0579 §1).
Outgrown []string
// Waiting are the modules whose manifest carries a field this judge does not know and waits.
Waiting []string
}
// JudgeAll judges every manifest the modules state names, as one catalogue with the predefined list the indexer keeps,
// and writes what differs. A manifest the manifests state does not hold is left unjudged and said; a write that fails is
// said and the pass goes on, so one key never costs the others their verdict.
//
// **A stricter judge never refuses what it accepted before** (novox/hq ADR 0579 §1): a manifest whose verdict under its
// digest is accepted, or which the indexer held before any verdict (taken as accepted, ADR 0578 §3) and is not in fresh,
// stays accepted, the new problems written as the verdict's Warnings and carried in its module's value, which the
// watchman raises as manifest-outgrown. A manifest in fresh (the digests a merge just took in) with no verdict yet, and
// one already refused, is judged in full.
func (ix *Indexer) JudgeAll(ctx context.Context, fresh map[string]bool) (Judged, error) {
return ix.judgeOver(ctx, fresh, "", "")
}
// judgeOver is JudgeAll; with a module and a manifest digest it judges that digest alone, in the catalogue the modules
// state holds with the module's manifest replaced by it (a module the state does not hold added), and writes its verdict
// and nothing else: no event, no count of the old sections, and no module's value but one that names the digest judged
// (carryVerdict). A digest with no verdict yet is judged in full, as a fresh one. One digest at a time, so the known
// packages dropped for a digest the trunk does not hold are dropped for that digest alone (review round 1, J7's note).
func (ix *Indexer) judgeOver(ctx context.Context, fresh map[string]bool, overModule, overDigest string) (Judged, error) {
var over map[string]string
if overModule != "" {
over = map[string]string{overModule: overDigest}
}
// A merge, the start pass and indexer_judge may all judge at once: one pass at a time, so none writes a verdict from
// modules another is rewriting.
ix.judgeMu.Lock()
defer ix.judgeMu.Unlock()
if !ix.underSeats {
// The seat list stands still for this pass: a take-in's dry run may put its revision's seats over it (seatsof.go).
defer judge.HoldSeats()()
}
return ix.judgeOverHeld(ctx, fresh, over)
}
// judgeOverHeld is judgeOver for a caller that holds the judge pass (judgeMu) and keeps the seat list as it wants it
// (judge.HoldSeats, or inside judge.WithSeats): it takes no lock, so a pass that put its seats over the list judges inside
// it without asking the shared lock the exclusive one forbids.
func (ix *Indexer) judgeOverHeld(ctx context.Context, fresh map[string]bool, over map[string]string) (Judged, error) {
var out Judged
if ix.Verdicts == nil || ix.Modules == nil || ix.Manifests == nil {
return out, nil
}
if over == nil {
ix.SayUnsaid(ctx) // a manifest-accepted a pass before failed to publish is said again first (DLV-I9b)
// What a take-in wrote and no pass judged yet is fresh too, whichever pass judges it (unjudged.go, issue 1966).
fresh = ix.withUnjudged(ctx, fresh)
}
held, err := ix.heldModules(ctx)
if err != nil {
return out, err
}
heldDigests, judgedDigests := map[string]bool{}, map[string]bool{}
for _, m := range held {
heldDigests[m.Manifest] = m.Manifest != ""
}
trunkOf := map[string]string{} // a judged change's module to the trunk's manifest digest it changes (DLV-I15)
// The known packages a manifest's `packages` may name (DLV-I14). The digest judged the trunk does not hold (a pull
// request's candidate, one the trunk no longer names) is not judged on them: the checker judges a change's names,
// knowing its delivery group, whose new packages this state does not know yet (the lead's ruling, issue 1966). The
// trunk's own manifests are, and a merge's are again in full (namesAtMerge).
known := ix.knownPackages(ctx)
// A digest judged over the trunk that is the trunk's own held one is said as the full pass says it (manifest-accepted,
// manifest-refused): indexer_judge with it, or the start pass's judgeStale, is that manifest's only judgement (review
// round 1 of issue 1966; ADR 0668 §4). A candidate's verdict is said nowhere.
onTrunk := map[string]bool{}
if over != nil {
fresh = map[string]bool{}
for module, digest := range over {
m := held[module]
onTrunk[module] = m.Manifest == digest
if m.Manifest != "" && m.Manifest != digest {
trunkOf[module] = m.Manifest
}
if m.Manifest != digest {
known = nil
}
m.Manifest = digest
held[module] = m
fresh[digest] = true
}
}
raws := map[string][]byte{}
digests := map[string]string{}
for _, name := range slices.Sorted(maps.Keys(held)) {
digest := held[name].Manifest
if digest == "" {
continue
}
value, _, found, err := ix.Manifests.Get(ctx, digest)
if err != nil {
return out, fmt.Errorf("reading manifests.%s of %s: %w", digest, name, err)
}
if !found {
ix.logf("judge: the manifest of %s (digest %s) is not in the manifests state, so it is not judged", name, digest)
continue
}
kept, err := meshcatalogclient.DecodeManifest(value)
if err != nil {
ix.logf("judge: manifests.%s of %s cannot be read, so it is not judged: %v", digest, name, err)
continue
}
raws[name] = []byte(kept.Manifest)
digests[name] = digest
}
predefined := ix.predefinedList(ctx)
// The old sections end when no module is left in them (issue 1966 D4): the count is the accepted set the last pass held.
var counts judge.OldSectionCounts
if ix.OldSections != nil {
counts = ix.heldOldSections(ctx)
}
refused, waits, unnamed := judge.ShelfWaiting(raws, predefined, 0, counts, known)
// A change's renamed-from is judged against the trunk's manifest of the same module (DLV-I15, design 32).
for module, digest := range trunkOf {
if _, waiting := waits[module]; waiting || raws[module] == nil {
continue
}
value, _, found, err := ix.Manifests.Get(ctx, digest)
kept, derr := meshcatalogclient.DecodeManifest(value)
if err != nil || !found || derr != nil {
ix.logf("judge: the trunk's manifest %s of %s cannot be read, so its renames are not judged against it", digest, module)
continue
}
head, herr := catalogue.Decode(raws[module])
trunk, terr := catalogue.Decode([]byte(kept.Manifest))
if herr != nil || terr != nil {
continue // the manifest's own problems say why
}
if p := judge.RenameProblems(module, head, trunk, predefined); len(p) > 0 {
refused[module] = append(refused[module], p...)
}
}
for _, p := range unnamed {
ix.logf("judge: a problem between the manifests that no one module's removal ends, so it refuses none: %s", p)
}
// A package's name belongs to one module (design 47 G §491, §676): the trunk's manifests only, since a candidate's
// revision is validated by the validator built from its own heads (§16).
if over == nil {
if refused == nil {
refused = map[string][]string{}
}
for module, p := range judge.PackageNameProblems(ix.packageEntries(ctx, held, raws)) {
if _, waiting := waits[module]; !waiting {
refused[module] = append(refused[module], p...)
}
}
}
out.Refused = refused
// A manifest with a field this judge does not know waits for a judge that may know it (clause 3 of the record on a
// release's Approve; ADR 0578 §1): it is refused only when this program is the newest build of the indexer the trunk
// holds, so no judge that could know the field is still to come.
newest := ix.ownBuildIsNewest(held)
id := ix.judgeID()
var accepted []catalogue.Manifest
for _, name := range slices.Sorted(maps.Keys(digests)) {
if _, judged := over[name]; over != nil && !judged {
continue
}
out.Judged++
v := meshcatalog.VerdictsByDigestValue{Module: name, State: meshcatalogclient.VerdictAccepted, Judge: id, At: ptr(ix.now())}
if fields := waits[name]; len(fields) > 0 {
accepted := ix.acceptedBefore(ctx, digests[name], fresh)
if !newest && !accepted {
v.State, v.Unknown = meshcatalogclient.VerdictManifestWaits, fields
out.Waiting = append(out.Waiting, name)
} else {
_, problems := judge.Manifest(raws[name], ix.predefinedList(ctx))
if accepted {
v.Warnings = problems
out.Outgrown = append(out.Outgrown, name)
} else {
v.State, v.Problems = meshcatalogclient.VerdictRefused, problems
}
}
} else if problems := refused[name]; len(problems) > 0 {
if ix.acceptedBefore(ctx, digests[name], fresh) && !namesAtMerge(name, raws[name], fresh[digests[name]] && over == nil, known) {
v.Warnings = problems
out.Outgrown = append(out.Outgrown, name)
} else {
v.State, v.Problems = meshcatalogclient.VerdictRefused, problems
}
}
if !meshcatalogclient.VerdictRefuses(v) {
if m, err := catalogue.Decode(raws[name]); err == nil {
m.Module = name
accepted = append(accepted, m)
}
}
wrote, revision, err := ix.writeVerdict(ctx, digests[name], v)
if errors.Is(err, process.ErrNotYet) {
return out, fmt.Errorf("the state %s is not on the bus yet: %w", VerdictsState, err)
}
if err != nil {
ix.logf("judge: the verdict on %s (digest %s) is not written: %v", name, digests[name], err)
continue
}
judgedDigests[digests[name]] = true
if wrote && over != nil && !onTrunk[name] {
out.Written++
ix.logf("judge: %s judged again by %s: %s", digests[name], id, v.State)
} else if wrote {
out.Written++
if meshcatalogclient.VerdictWaits(v) {
ix.logf("judge: %s", meshcatalogclient.VerdictWhy(v))
}
if meshcatalogclient.VerdictRefuses(v) {
ix.logf("judge: %s", meshcatalogclient.VerdictWhy(v))
if ix.SayRefused != nil {
r := refusedOf(digests[name], v)
r.Revision = int64(revision)
ix.SayRefused(ctx, r)
}
} else if !meshcatalogclient.VerdictWaits(v) {
// Every manifest newly accepted is said whole to the bus module, its entries empty when it asks for none
// (issue 1966 D3, ADR 0595 §4, design 25 § 12.2); a manifest set aside to wait gets none (DLV-I9).
a := acceptedOf(digests[name], v, revision, raws[name])
_ = ix.sayAccepted(ctx, a, acceptedEventID(a))
}
} else if over == nil && fresh[digests[name]] && !meshcatalogclient.VerdictHeld(v) {
// A manifest a merge just made its module's trunk manifest is newly accepted there, even when a pull request's
// judgement wrote its verdict before the merge and said it nowhere (a candidate's verdict is not said): it is
// said now from the verdict held, with its revision, so the bus module and the grants part hear it (design 25
// bus-state §1; issue 1966: claude-licence-manager's new builds waited for a grant nothing wrote).
ix.sayHeld(ctx, digests[name], raws[name])
}
// The reader capabilities the manifest's own words, forms and seat names need, named with the verdict (ADR 0614 §4).
needs, _ := catalogue.ManifestNeeds(raws[name])
if err := ix.carryVerdict(ctx, name, digests[name], v, needs); err != nil {
ix.logf("judge: modules.%s does not carry its verdict: %v", name, err)
}
}
// The verdicts carried are kept with the view (KeepView), so a restart reads them back.
if err := ix.KeepView(ctx); err != nil {
ix.logf("judge: the verdicts carried are not kept in a catalog graph revision: %v", err)
}
if over != nil {
return out, nil
}
ix.endUnjudged(ctx, judgedDigests, heldDigests)
if err := ix.keepOldSections(ctx, judge.CountOldSections(accepted), judge.CountOwnerReads(accepted)); err != nil {
ix.logf("judge: the count of the old sections is not kept: %v", err)
}
return out, nil
}
// sayHeld says manifest-accepted for the trunk's manifest of digest from the accepted verdict the verdicts state holds on
// it, with that verdict's revision; a verdict that cannot be read, or that refuses or waits, says nothing.
func (ix *Indexer) sayHeld(ctx context.Context, digest string, raw []byte) {
key, err := meshcatalogclient.VerdictsKey(digest)
if err != nil {
return
}
value, revision, found, err := ix.Verdicts.Get(ctx, key)
if err != nil || !found {
ix.logf("judge: the verdict on %s cannot be read, so its manifest-accepted is not said: %v", digest, err)
return
}
v, err := meshcatalogclient.DecodeVerdict(value)
if err != nil || meshcatalogclient.VerdictHeld(v) {
return
}
a := acceptedOf(digest, v, revision, raw)
_ = ix.sayAccepted(ctx, a, acceptedEventID(a))
}
// JudgeAtStart is JudgeAll of every manifest held, none fresh, as the indexer starts (a judge that changed judges every
// one again); one that fails is judged again at the next message or source revision heard (kickJudge). Then every other
// digest of the verdicts state that another judge refused or held waiting (one of a candidate, or one back on the trunk
// later) is judged again by this judge (judgeStale), so a refusal an older judge made never stands for good.
func (ix *Indexer) JudgeAtStart(ctx context.Context) (Judged, error) {
j, err := ix.JudgeAll(ctx, nil)
ix.judgePending.Store(err != nil)
if err == nil {
ix.judgeStale(ctx)
}
return j, err
}
// judgeID is the judge a verdict of this program names.
func (ix *Indexer) judgeID() string {
if ix.JudgeID != "" {
return ix.JudgeID
}
return judge.Identity()
}
// staleVerdict says whether the digest's verdict wants this judge's judgement: there is none, or another judge refused
// it or held it waiting. An accepted verdict stays accepted under any later judge (ADR 0579 §1), so judging it again
// changes nothing.
func (ix *Indexer) staleVerdict(ctx context.Context, digest string) bool {
value, _, found, err := ix.Verdicts.Get(ctx, digest)
if err != nil {
return false // unread: left, said by the next pass that reads it
}
if !found {
return true
}
v, err := meshcatalogclient.DecodeVerdict(value)
return err != nil || (meshcatalogclient.VerdictHeld(v) && v.Judge != ix.judgeID())
}
// judgeDigests judges each digest of over (module to digest) whose verdict is stale (staleVerdict), one at a time in the
// catalogue the modules state holds; a pass that fails is said and the others go on.
func (ix *Indexer) judgeDigests(ctx context.Context, over map[string]string, why string) {
if ix.Verdicts == nil || ix.Manifests == nil || ix.Modules == nil {
return
}
for _, module := range slices.Sorted(maps.Keys(over)) {
if !ix.staleVerdict(ctx, over[module]) {
continue
}
if _, err := ix.judgeOver(ctx, nil, module, over[module]); err != nil {
ix.logf("judge: the manifest %s of %s (%s) is not judged: %v", over[module], module, why, err)
}
}
}
// judgeDigestsWith is judgeDigests with the seats put over the catalogue's list for the whole pass (judge.WithSeats): one
// locked pass that takes the seats, and never a second lock under it (the exclusive one of WithSeats and the shared one of
// judgeOver would wait for each other for good).
func (ix *Indexer) judgeDigestsWith(ctx context.Context, seats []catalogue.Seat, over map[string]string, why string) {
if ix.Verdicts == nil || ix.Manifests == nil || ix.Modules == nil {
return
}
ix.judgeMu.Lock()
defer ix.judgeMu.Unlock()
judge.WithSeats(seats, func() {
for _, module := range slices.Sorted(maps.Keys(over)) {
if !ix.staleVerdict(ctx, over[module]) {
continue
}
if _, err := ix.judgeOverHeld(ctx, nil, map[string]string{module: over[module]}); err != nil {
ix.logf("judge: the manifest %s of %s (%s) is not judged: %v", over[module], module, why, err)
}
}
})
}
// judgeStale judges again every digest of the verdicts state another judge refused or held waiting, each as a manifest
// of the module its verdict names (judgeDigests). Never on a timer: at the start pass.
func (ix *Indexer) judgeStale(ctx context.Context) {
if ix.Verdicts == nil {
return
}
keys, err := ix.Verdicts.Keys(ctx)
if err != nil {
ix.logf("judge: the verdicts other judges made are not judged again: listing the verdicts: %v", err)
return
}
byModule := map[string][]string{}
for _, digest := range keys {
value, _, found, err := ix.Verdicts.Get(ctx, digest)
if err != nil || !found {
continue
}
if v, err := meshcatalogclient.DecodeVerdict(value); err == nil && meshcatalogclient.VerdictHeld(v) && v.Judge != ix.judgeID() {
byModule[v.Module] = append(byModule[v.Module], digest)
}
}
for _, module := range slices.Sorted(maps.Keys(byModule)) {
for _, digest := range byModule[module] {
ix.judgeDigests(ctx, map[string]string{module: digest}, "made by another judge")
}
}
}
// namesAtMerge says whether a manifest a merge takes in (atMerge) names a package known does not: it is refused for it even
// when its digest was accepted before, since that verdict was its candidate's, judged without the names (judgeOver), so
// no stricter judge turns an acceptance into a refusal here (ADR 0579 §1).
func namesAtMerge(module string, raw []byte, atMerge bool, known judge.KnownPackages) bool {
if !atMerge || known == nil {
return false
}
m, err := catalogue.Decode(raw)
return err == nil && len(judge.UnknownPackageNames(module, m, known)) > 0
}
// knownPackages are the packages a manifest's `packages` may name (DLV-I14, design 32): every name the packages state's
// series keys hold, as KeepPackageNames reads it at each merge, and the Go module path of every per-module record the
// state holds from the trunk: while no build module builds Go package artifacts, a Go module the trunk holds a record of
// has no series and is known by its module path, never by a package inside it (design 32; issue 1966). Nil, which judges
// no name, where the indexer keeps no packages state or it cannot be listed (said): a state the judge cannot read refuses no manifest.
func (ix *Indexer) knownPackages(ctx context.Context) judge.KnownPackages {
if ix.Packages == nil {
return nil
}
keys, err := ix.Packages.Keys(ctx)
if err != nil {
ix.logf("judge: the packages state cannot be listed, so the names of packages are not judged: %v", err)
return nil
}
known := judge.KnownPackages{}
for _, key := range keys {
value, _, found, err := ix.Packages.Get(ctx, key)
if err != nil {
ix.logf("judge: packages.%s cannot be read, so the names of packages are not judged: %v", key, err)
return nil
}
if !IsSeriesKey(key) {
if r, err := DecodePublishedPackages(value); found && err == nil && r.Module != "" {
known[r.Module] = true
}
continue
}
if s, err := DecodeSeriesPackage(value); found && err == nil {
known[s.Name] = true
}
}
return known
}
// kickJudge judges again a start pass that failed, when the indexer hears something: never on a timer.
func (ix *Indexer) kickJudge(ctx context.Context) {
if !ix.judgePending.Load() {
return
}
if j, err := ix.JudgeAtStart(ctx); err != nil {
ix.logf("judge: the manifests are still not judged: %v", err)
} else {
ix.logf("judge: judged %d manifests again after a failed start: %d verdicts written", j.Judged, j.Written)
}
}
// acceptedBefore says whether the manifest of digest was accepted before this judgement: its verdict says accepted, or
// it has none and is not fresh (held before any verdict, so taken in under the judges that ran until then).
func (ix *Indexer) acceptedBefore(ctx context.Context, digest string, fresh map[string]bool) bool {
value, _, found, err := ix.Verdicts.Get(ctx, digest)
if err != nil {
return false // unread: judged in full, said by the write that follows
}
if !found {
return !fresh[digest]
}
prev, err := meshcatalogclient.DecodeVerdict(value)
return err == nil && prev.State == meshcatalogclient.VerdictAccepted
}
// writeVerdict writes v under the digest's key unless the verdict held there says the same (ManifestVerdict.Same); wrote
// says it was written, and revision is the verdicts state's revision of that write, which the event saying it carries
// (novox/hq ADR 0622 §2). It is read back after the write: when another write came between, the revision read holds
// another verdict and revision is 0, so a reader takes the later write's own event and never pairs v with its revision.
func (ix *Indexer) writeVerdict(ctx context.Context, digest string, v meshcatalog.VerdictsByDigestValue) (wrote bool, revision uint64, err error) {
key, err := meshcatalogclient.VerdictsKey(digest)
if err != nil {
return false, 0, err
}
err = write(ctx, ix.Verdicts, key, func(value []byte, found bool) ([]byte, bool, error) {
wrote = false
if found {
if cur, err := meshcatalogclient.DecodeVerdict(value); err == nil && meshcatalogclient.SameVerdict(cur, v) {
return nil, false, nil
}
}
wrote = true
next, err := meshcatalogclient.EncodeVerdict(v)
return next, false, err
})
if err != nil || !wrote {
return wrote, 0, err
}
if value, rev, found, err := ix.Verdicts.Get(ctx, key); err == nil && found {
if cur, err := meshcatalogclient.DecodeVerdict(value); err == nil && meshcatalogclient.SameVerdict(cur, v) {
revision = rev
}
}
return wrote, revision, nil
}
// OwnBuildState is the indexer's state of the build it runs (the bucket mesh-delivery-indexer_own-build, one key, OwnBuildKey):
// written once, at the process's start by FixOwnBuild, so indexer_pending says whether this process knows its build and, when it
// does not, since when and why.
const (
OwnBuildStateName = "own-build"
OwnBuildKey = "state"
)
// OwnBuild is the value of the own-build state.
type OwnBuild struct {
V int `json:"v"`
Known bool `json:"known"`
FP string `json:"fp,omitempty"`
Since time.Time `json:"since"`
Why string `json:"why,omitempty"`
}
// FixOwnBuild fixes, once and at the process's start, which build of the indexer this program is: asked of the node-engine's
// seat (OwnBuild; never the trunk's value of the indexer, which names a build that may be merged and not delivered), before
// this process hears a merge. It is never asked again: while the node-engine replaces the indexer the seat already names the
// successor, and an old process that learnt it later would take revisions in with its old judge. Where the start's ask is not
// answered the build stays unknown for the life of this process: every merge waits, loudly (a take-in.pending that says so, the
// own-build state, indexer_pending), and the module is started again to learn it.
func (ix *Indexer) FixOwnBuild(ctx context.Context) {
ix.ownMu.Lock()
if ix.ownFixed || ix.OwnBuild == nil {
ix.ownMu.Unlock()
return
}
ix.ownFixed = true
ix.ownMu.Unlock()
fp, err := ix.OwnBuild(ctx)
if err == nil && fp == "" {
err = errors.New("the node-engine names no build of the indexer")
}
st := OwnBuild{V: 1, Since: ix.now().UTC()}
if err != nil {
st.Why = err.Error()
ix.logf("judge: the build this program runs is not known (%v), so every merge waits and a manifest with a field it does not know waits and is not refused; it is learnt only at the start, so the indexer is started again", err)
} else {
st.Known, st.FP = true, fp
ix.logf("judge: this program runs the indexer build %s, fixed here at its start", fp)
}
ix.ownMu.Lock()
ix.ownFP, ix.ownSince, ix.ownWhy = fp, st.Since, st.Why
ix.ownMu.Unlock()
if ix.OwnBuildState == nil {
return
}
data, _ := json.Marshal(st)
if _, rev, found, gerr := ix.OwnBuildState.Get(ctx, OwnBuildKey); gerr == nil && found {
err = ix.OwnBuildState.Update(ctx, OwnBuildKey, data, rev)
} else if gerr == nil {
err = ix.OwnBuildState.Create(ctx, OwnBuildKey, data)
} else {
err = gerr
}
if err != nil {
ix.logf("the own-build state cannot be written: %v", err)
}
}
// ownBuildIsNewest says whether the build of the indexer this program runs (ownFP, from the node-engine) is the newest the
// trunk holds: the indexer's own modules value names it. A newer build merged and not yet delivered ends it at the trunk's
// merge, as one that pins a changed mesh-sdk always is; false when either is unknown.
func (ix *Indexer) ownBuildIsNewest(held map[string]meshcatalog.ModulesByModuleValue) bool {
fp := held[ix.Module].Fp2
if fp == "" {
return false
}
ix.ownMu.Lock()
defer ix.ownMu.Unlock()
return ix.ownFP != "" && ix.ownFP == fp
}
// carryVerdict puts a refusal into the module's value, and takes one out once the manifest is accepted, while the value
// still names the digest judged, and the reader capabilities the manifest needs beside it (needs, ADR 0614 §4); an
// accepted module's value carries no verdict, so judging writes no value of a module the
// judge accepts (a reader takes a value without a verdict as accepted, meshcatalogclient.TakeVerdict).
func (ix *Indexer) carryVerdict(ctx context.Context, name, digest string, v meshcatalog.VerdictsByDigestValue, needs []string) error {
key, err := meshcatalogclient.ModulesKey(name)
if err != nil {
return err
}
return write(ctx, ix.Modules, key, func(value []byte, found bool) ([]byte, bool, error) {
if !found {
return nil, false, nil
}
cur, err := meshcatalogclient.DecodeModule(value)
if err != nil || cur.Manifest != digest {
return nil, false, nil // gone, unreadable, or moved to another manifest since: the next pass judges it
}
// A wait is written as a refusal naming the fields, so a reader pinned before the verdict waits holds it as refused.
state, problems := "", v.Problems
switch {
case meshcatalogclient.VerdictWaits(v):
state, problems = meshcatalogclient.VerdictRefused, []string{meshcatalogclient.WaitsOn(v)}
case meshcatalogclient.VerdictRefuses(v):
state = meshcatalogclient.VerdictRefused
}
// The judge reads the manifest alone; the forms its templates' text writes came with its merge (ADR 0632 §2) and
// stand, cur being of the same manifest.
needs := withTemplateForms(needs, cur.Needs)
if cur.Verdict == state && slices.Equal(cur.Problems, problems) && slices.Equal(cur.Unknown, v.Unknown) && slices.Equal(cur.Warnings, v.Warnings) &&
slices.Equal(cur.Needs, needs) {
return nil, false, nil
}
cur.Verdict, cur.Problems, cur.Unknown, cur.Warnings, cur.Needs = state, problems, v.Unknown, v.Warnings, needs
if state == "" {
cur.Problems = nil
}
next, err := meshcatalogclient.EncodeModule(cur)
return next, false, err
})
}
// predefinedList is the catalogue's predefined list the indexer keeps (writePredefined), or nil when it keeps none or
// it cannot be read: the judge then takes each fallback as its value declares it.
func (ix *Indexer) predefinedList(ctx context.Context) catalogue.PredefinedList {
if ix.Predefined == nil {
return nil
}
value, _, found, err := ix.Predefined.Get(ctx, PredefinedListKey)
if err != nil || !found {
return nil
}
var kept struct {
List json.RawMessage `json:"list"`
}
if json.Unmarshal(value, &kept) != nil {
return nil
}
list, err := catalogue.ParsePredefined(kept.List)
if err != nil {
ix.logf("judge: the predefined list kept cannot be read, so fallbacks are judged as their values declare them: %v", err)
return nil
}
return list
}
// The tools that read and steer the verdicts (the instruction "tools are how a module is spoken to"): indexer_verdicts
// lists the manifests that wait or are refused, and indexer_judge judges one module again now.
const (
VerdictsTool = "indexer_verdicts"
JudgeAgainTool = "indexer_judge"
judgeAgainPrefix = "judge."
)
// VerdictRow is one verdict as indexer_verdicts lists it: the manifest's digest, the module, its state, the fields it waits
// on or the problems that refuse it, and the judge that made it.
type VerdictRow struct {
Module string `json:"module"`
Digest string `json:"digest"`
State string `json:"state"`
Words string `json:"words,omitempty"`
Unknown []string `json:"unknown,omitempty"`
Problems []string `json:"problems,omitempty"`
Warnings []string `json:"warnings,omitempty"`
Judge string `json:"judge"`
At time.Time `json:"at"`
}
// ListVerdicts reads the verdicts under keys with get and lists those of module (every module when empty) that wait or are
// refused, or every one with all, sorted by module and digest. A value that does not read is said in unread by its key.
func ListVerdicts(keys []string, get func(key string) ([]byte, error), module string, all bool) (rows []VerdictRow, unread map[string]string) {
unread = map[string]string{}
for _, key := range keys {
value, err := get(key)
if err != nil || value == nil {
if err != nil {
unread[key] = err.Error()
}
continue
}
v, err := meshcatalogclient.DecodeVerdict(value)
if err != nil {
unread[key] = err.Error()
continue
}
if (module != "" && v.Module != module) || (!all && !meshcatalogclient.VerdictHeld(v)) {
continue
}
rows = append(rows, VerdictRow{Module: v.Module, Digest: key, State: v.State, Words: meshcatalogclient.VerdictWhy(v), Unknown: v.Unknown,
Problems: v.Problems, Warnings: v.Warnings, Judge: v.Judge, At: timeOf(v.At)})
}
slices.SortFunc(rows, func(a, b VerdictRow) int {
if c := strings.Compare(a.Module, b.Module); c != 0 {
return c
}
return strings.Compare(a.Digest, b.Digest)
})
return rows, unread
}
// JudgeAgain judges the manifests again now, as at the start, and answers how the module's stands: the tool
// indexer_judge. A module the modules state does not hold, or whose manifest is not in the manifests state, is
// answered so. With a digest it judges that manifest alone, in the catalogue the trunk holds (judgeOver), whoever made
// its verdict: a candidate's, or one the trunk names again, refused by an older judge.
func (ix *Indexer) JudgeAgain(ctx context.Context, module, digest string) TakenIn {
out := TakenIn{Module: module}
held, err := ix.heldModules(ctx)
if err != nil {
out.Answer = "the modules state cannot be read: " + err.Error()
return out
}
if digest != "" {
// One digest (a candidate's, or one the trunk no longer names), judged by this judge in the catalogue the trunk holds.
value, _, found, err := ix.Manifests.Get(ctx, digest)
kept, derr := meshcatalogclient.DecodeManifest(value)
switch {
case err != nil || !found:
out.Answer = "the indexer keeps no manifest of digest " + digest
return out
case derr != nil:
out.Answer = "the manifest of digest " + digest + " cannot be read: " + derr.Error()
return out
case module != "" && kept.Module != module:
out.Answer = "the manifest of digest " + digest + " is " + kept.Module + "'s, not " + module + "'s"
return out
}
out.Module = kept.Module
if _, err := ix.judgeOver(ctx, nil, kept.Module, digest); err != nil {
out.Answer = "the manifest could not be judged: " + err.Error()
return out
}
return ix.answerVerdict(ctx, out, digest)
}
m, ok := held[module]
if !ok || m.Manifest == "" {
out.Answer = "the indexer holds no manifest of " + module
return out
}
if _, err := ix.JudgeAll(ctx, nil); err != nil {
out.Answer = "the manifests could not be judged: " + err.Error()
return out
}
return ix.answerVerdict(ctx, out, m.Manifest)
}
// answerVerdict is out answering how the verdict under digest stands.
func (ix *Indexer) answerVerdict(ctx context.Context, out TakenIn, digest string) TakenIn {
module := out.Module
value, _, found, err := ix.Verdicts.Get(ctx, digest)
if err != nil || !found {
out.Answer = "judged, but the verdict on " + module + " cannot be read"
return out
}
v, err := meshcatalogclient.DecodeVerdict(value)
if err != nil {
out.Answer = "judged, but the verdict on " + module + " cannot be read: " + err.Error()
return out
}
out.Stored = true
out.Answer = "judged again by " + v.Judge + ": " + v.State
if meshcatalogclient.VerdictHeld(v) {
out.Answer += " (" + meshcatalogclient.VerdictWhy(v) + ")"
}
return out
}
// NewJudgeAgain is the value indexer_judge writes for a module, and one digest of its manifest when given: its key in
// the takeins state and the value.
func NewJudgeAgain(module, digest, why, by string, at time.Time) (string, []byte, error) {
if digest != "" {
if _, err := meshcatalogclient.ManifestsKey(digest); err != nil {
return "", nil, err
}
}
key, data, err := NewTakeIn(module, why, by, at)
if err != nil {
return "", nil, err
}
var a TakeInAsk
if err := json.Unmarshal(data, &a); err != nil {
return "", nil, err
}
a.Judge, a.Digest = true, digest
data, err = json.Marshal(a)
return judgeAgainPrefix + key, data, err
}
// withTemplateForms is needs with the reference forms of had added: a module's templates write forms only (catalogue
// ModuleNeeds), and a merge carried them with the manifest the judge reads again alone.
func withTemplateForms(needs, had []string) []string {
out := slices.Clone(needs)
for _, n := range had {
if strings.HasPrefix(n, catalogue.ReadForm) && !slices.Contains(out, n) {
out = append(out, n)
}
}
slices.Sort(out)
return out
}
// timeOf is the time a pointer holds, the zero time for none.
func timeOf(t *time.Time) time.Time {
if t == nil {
return time.Time{}
}
return *t
}