Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/versions_edges.go
T
jschoubben 71a35a6376 MV1: move contracts and sdk under modules/, importers follow
The delivery repository's contracts and SDK live under modules/ as
mesh-delivery-contracts and mesh-delivery-sdk, their Go module paths
renamed to match the folders (the TypeScript names already did), and
every importer in mesh-delivery, mesh-catalog, mesh-host, mesh-tools
and mesh-sdk follows in the same group: a design as it should be, one
path per module, checked by the group's check building every importer.

Builds: design 47 G §427, §88

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 05:09:56 +01:00

382 lines
13 KiB
Go

package index
import (
"cmp"
"context"
"encoding/json"
"errors"
"fmt"
"slices"
"strings"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
"git.novox.be/novox/mesh-sdk/go/keys"
)
// What outdated compares against is kept as heard, in the indexer's state heard (design 47 G A-68): toolchain.<language> the newest
// toolchain-agreed per language, delivered.<module> the newest version-delivered per module.
const (
ToolchainPrefix = "toolchain."
DeliveredPrefix = "delivered."
// ActedPrefix opens acted.<acting part>.<dependent>, the newest wait-ended of an acting part's wait for a dependent, ran or released
// (A-78 a), each key part escaped as a name (A-80, §73).
ActedPrefix = "acted."
)
// Acted is what an acted key holds: the build the dependent was at when the acting part acted for it (the wait's version, §374 kind 5).
type Acted struct {
V int64 `json:"v"`
Part string `json:"part"`
Step string `json:"step"`
Build string `json:"build"`
How string `json:"how"`
At time.Time `json:"at"`
}
// HearWaitEnded keeps, for kind 5 (§374, §375, A-78 a), the newest wait-ended of an acting step that ended ran or released: the
// acting part, the step and the build it acted for. A wait that ended superseded, or of a step that is no acting part's (put,
// delivery, kept-build, backup), is not an act and keeps nothing.
func (ix *Indexer) HearWaitEnded(ctx context.Context, w meshwants.WaitEndedEvent) error {
if w.Part == "" || w.Module == "" || (w.How != "ran" && w.How != "released") {
return nil
}
switch w.Step {
case meshwants.WaitStepPut, meshwants.WaitStepDelivery, meshwants.WaitStepKeptBuild, meshwants.WaitStepBackup, "":
return nil
}
if ix.Heard == nil {
return errors.New("the state heard is not kept here")
}
a := Acted{V: 1, Part: w.Part, Step: w.Step, Build: w.Version, How: w.How, At: w.Since}
return ix.keepNewest(ctx, ActedKey(w.Part, w.Module), a, func(kept []byte) bool {
var k Acted
return json.Unmarshal(kept, &k) != nil || !a.At.Before(k.At)
})
}
// ActedKey is the key of the state heard of an acting part's act for a dependent.
func ActedKey(part, dependent string) string {
return ActedPrefix + keys.EscapeName(part) + "." + keys.EscapeName(dependent)
}
// actedFor is the act kept for the acting part (an artifact reference, or its module) and the dependent module.
func (ix *Indexer) actedFor(ctx context.Context, part, dependent string) (Acted, bool) {
for _, p := range []string{part, strings.SplitN(part, ":", 2)[0]} {
raw, _, found, err := ix.Heard.Get(ctx, ActedKey(p, dependent))
var a Acted
if err == nil && found && json.Unmarshal(raw, &a) == nil {
return a, true
}
}
return Acted{}, false
}
// keepNewest writes body at key of the state heard unless it holds a value whose version is higher; version of the body
// is read by newer, which says whether the heard one is not older than the kept.
func (ix *Indexer) keepNewest(ctx context.Context, key string, body any, newer func(kept []byte) bool) error {
if ix.Heard == nil {
return errors.New("the own state is not kept here")
}
data, err := json.Marshal(body)
if err != nil {
return err
}
return write(ctx, ix.Heard, key, func(kept []byte, found bool) ([]byte, bool, error) {
if found && !newer(kept) {
return nil, false, nil
}
return data, false, nil
})
}
// HearToolchainAgreed keeps the newest toolchain-agreed of a language (A-68, §375): the one heard last.
func (ix *Indexer) HearToolchainAgreed(ctx context.Context, t meshbuilds.ToolchainAgreed) error {
if t.Language == "" {
return errors.New("a toolchain-agreed names no language")
}
return ix.keepNewest(ctx, ToolchainPrefix+keys.EscapeName(t.Language), t, func([]byte) bool { return true })
}
// HearVersionDelivered takes one version-delivered (design 47 G §374, §376, A-68): the entries of kinds 2, 4 and 6 of the
// delivered release, which read "not yet delivered", are written once with each dependency's newest delivered version and
// part digest; then the delivered version is kept as the module's newest.
func (ix *Indexer) HearVersionDelivered(ctx context.Context, d meshwants.VersionDeliveredEvent) error {
if ix.Versions == nil || ix.Heard == nil {
return errors.New("the state versions is not kept here")
}
if _, ok := versionParts(d.Version); !ok {
return fmt.Errorf("the version-delivered of %s names the version %q, which is no MAJOR.MINOR.PATCH", d.Module, d.Version)
}
key := meshcatalog.VersionsByModuleVersionKey(d.Module, d.Version)
err := write(ctx, ix.Versions, key, func(raw []byte, found bool) ([]byte, bool, error) {
if !found {
return nil, false, nil // a version the catalog does not hold (a build of another branch) has no entries to write
}
v, err := meshcatalog.DecodeVersionsByModuleVersionValue(raw)
if err != nil {
return nil, false, err
}
changed := false
for i, e := range v.Edges {
if e.Version != notYetDelivered {
continue
}
dep, ok := ix.deliveredOf(ctx, strings.SplitN(e.Part, ":", 2)[0])
if !ok {
continue // the dependency is not delivered yet: the entry reads so until this release's own delivery finds it
}
v.Edges[i].Version, v.Edges[i].Digest = dep.Version, dep.Parts[e.Part]
changed = true
}
if !changed {
return nil, false, nil
}
data, err := json.Marshal(v)
return data, false, err
})
if err != nil {
return err
}
return ix.keepNewest(ctx, DeliveredPrefix+keys.EscapeName(d.Module), d, func(kept []byte) bool {
var k meshwants.VersionDeliveredEvent
return json.Unmarshal(kept, &k) != nil || compareVersions(d.Version, k.Version) >= 0
})
}
// deliveredOf is the newest version-delivered kept of a module.
func (ix *Indexer) deliveredOf(ctx context.Context, module string) (meshwants.VersionDeliveredEvent, bool) {
var d meshwants.VersionDeliveredEvent
raw, _, found, err := ix.Heard.Get(ctx, DeliveredPrefix+keys.EscapeName(module))
if err != nil || !found || json.Unmarshal(raw, &d) != nil {
return d, false
}
return d, true
}
// Outdated answers the verb outdated (design 47 G §375) for every edge kind: every entry of each module's newest released
// version that is behind. Kind 1: the recorded digest differs from the part's digest in the dependency's newest released
// version. Kind 3: the recorded toolchain version differs from the version of the newest toolchain-agreed of the dependent
// artifact's language (A-80, toolchain.<language>). Kinds 2 and 4: from the dependency's newest version-delivered. Kind 5: the
// acting part has not acted for the dependent's current build (acted.<acting part>.<dependent> against the newest trunk
// entry's fingerprint, A-78 a). Kind 6: the delivered version's catalog entry no longer serves the verb or word (A-78 b). A
// stale edge (kinds 1, 3) has the act rebuild, an ordering edge is shown and starts nothing. A dependency's newer version that
// left the digest as it was makes nothing behind.
func (ix *Indexer) Outdated(ctx context.Context, in meshcatalog.OutdatedInput) (meshcatalog.OutdatedAnswer, error) {
out := meshcatalog.OutdatedAnswer{Behind: []meshcatalog.Outdated{}}
if ix.Versions == nil || ix.Heard == nil {
return out, errors.New("the state versions is not kept here")
}
all, err := ix.Versions.Keys(ctx)
if err != nil {
return out, err
}
modules := map[string]bool{}
for _, k := range all {
if m, _, ok := meshcatalog.ParseVersionsByModuleVersionKey(k); ok {
modules[m] = true
}
}
newest := map[string]meshcatalog.VersionEntry{}
for m := range modules {
vs, err := ModuleVersions(ctx, ix.Versions, m)
if err != nil {
return out, err
}
for _, v := range vs {
if !v.Late {
newest[m] = v
break
}
}
}
trunk := map[string]meshcatalog.CatalogEntry{} // the newest trunk revision's entries, read where a kind needs them
if ix.Revisions != nil {
if rev, found, err := newestTrunk(ctx, ix.Revisions); err == nil && found {
if _, entries, err := ReadRevision(ctx, ix.Revisions, fmt.Sprintf("trunk.%d", rev.Revision)); err == nil {
for _, e := range entries {
trunk[e.Module] = e
}
}
}
}
for _, m := range sortedKeys(newest) {
v := newest[m]
for _, e := range v.Edges {
dep := strings.SplitN(e.Part, ":", 2)[0]
if in.Dependency != "" && in.Dependency != e.Part {
continue
}
o := meshcatalog.Outdated{Module: m, Version: v.Version, Kind: e.Kind,
Dependency: meshcatalog.OutdatedDependency{Artifact: e.Part, Branch: ix.firstBranch(ctx, dep)}}
switch e.Kind {
case 1:
n, ok := newest[dep]
if !ok {
continue
}
var digest string
for _, a := range n.Artifacts {
if a.Artifact == e.Part {
digest = a.Digest
}
}
if digest == "" || digest == e.Digest {
continue
}
o.Recorded, o.Newest = e.Version+" "+e.Digest, n.Version+" "+digest
case 3:
tc, ok := ix.agreedToolchain(ctx, languageOf(trunk[m], e))
if !ok || tc.Version == "" || tc.Version == e.Version {
continue
}
o.Recorded, o.Newest = e.Version+" "+e.Digest, tc.Version
case 2, 4:
d, ok := ix.deliveredOf(ctx, dep)
if !ok || e.Version == notYetDelivered {
continue
}
digest := d.Parts[e.Part]
if digest == "" || digest == e.Digest {
continue
}
o.Recorded, o.Newest = e.Version+" "+e.Digest, d.Version+" "+digest
case 5:
cur := trunk[m].Fp
if cur == "" {
continue
}
acted, ok := ix.actedFor(ctx, e.Part, m)
if ok && acted.Build == cur {
continue
}
o.Recorded, o.Newest = "acted for "+cmp.Or(acted.Build, "no build"), "the current build "+cur
case 6:
d, ok := ix.deliveredOf(ctx, dep)
if !ok || e.Version == notYetDelivered || d.Version == e.Version {
continue
}
if serves, known := ix.deliveredServes(ctx, dep, d.Version, e.Word); !known || serves {
continue
}
o.Recorded, o.Newest = e.Version+" "+e.Word, d.Version+" no longer serves "+e.Word
default:
continue
}
if e.Stale {
o.Act = "rebuild"
}
out.Behind = append(out.Behind, o)
}
}
behind, err := ix.outdatedBranchBuilds(ctx, in)
if err != nil {
return out, err
}
out.Behind = append(out.Behind, behind...)
return out, nil
}
// agreedToolchain is the newest toolchain-agreed kept for a language (A-80).
func (ix *Indexer) agreedToolchain(ctx context.Context, language string) (meshbuilds.ToolchainAgreed, bool) {
var t meshbuilds.ToolchainAgreed
if language == "" {
return t, false
}
raw, _, found, err := ix.Heard.Get(ctx, ToolchainPrefix+keys.EscapeName(language))
if err != nil || !found || json.Unmarshal(raw, &t) != nil {
return t, false
}
return t, true
}
// languageOf is the language of the artifact a kind-3 edge of the entry builds: the target of the catalog graph's edge from the
// toolchain part, else the one language the entry's artifacts share.
func languageOf(entry meshcatalog.CatalogEntry, e meshcatalog.VersionEdge) string {
lang := map[string]string{}
for _, a := range entry.Artifacts {
lang[a.Ref] = a.Language
}
for _, g := range entry.Edges {
if g.Kind == 3 && (g.Part == e.Part || g.From == e.Part) && lang[g.To] != "" {
return lang[g.To]
}
}
one := ""
for _, l := range lang {
if l == "" || (one != "" && one != l) {
return ""
}
one = l
}
return one
}
// deliveredServes says whether the catalog entry of a delivered version of module serves word, its verb (seat.verb, as the entry's
// serves lists it, the form `seat:` allowed) or a word of its manifest (A-78 b); known is false where that version's entry cannot be read.
func (ix *Indexer) deliveredServes(ctx context.Context, module, version, word string) (serves, known bool) {
if ix.Revisions == nil {
return false, false
}
vs, err := ModuleVersions(ctx, ix.Versions, module)
if err != nil {
return false, false
}
var source int64
for _, v := range vs {
if v.Version == version {
source = v.Source
}
}
if source == 0 {
return false, false
}
names, err := ix.Revisions.Names(ctx)
if err != nil {
return false, false
}
var numbers []int64
for _, n := range names {
if r, ok := TrunkRevisionOf(n); ok {
numbers = append(numbers, r)
}
}
slices.SortFunc(numbers, func(a, b int64) int { return cmp.Compare(b, a) })
for _, n := range numbers { // the newest revision taken from the source revision the version was built at
rev, entries, err := ReadRevision(ctx, ix.Revisions, fmt.Sprintf("trunk.%d", n))
if err != nil || rev.Source != source {
continue
}
for _, e := range entries {
if e.Module != module {
continue
}
w := strings.TrimPrefix(word, "seat:")
_, inManifest := e.Manifest[word]
return slices.Contains(e.Serves, w) || slices.Contains(e.Provides, w) || slices.Contains(e.Streams, w) || inManifest, true
}
}
return false, false
}
// firstBranch is the first watched branch of a module's registration, which its released versions are of.
func (ix *Indexer) firstBranch(ctx context.Context, module string) string {
if ix.Registry != nil {
if r, found, err := ix.Registry.Registration(ctx, module); err == nil && found && len(r.Branches) > 0 {
return r.Branches[0]
}
}
return ""
}
func sortedKeys[V any](m map[string]V) []string {
out := make([]string, 0, len(m))
for k := range m {
out = append(out, k)
}
slices.Sort(out)
return out
}