Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/series.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

246 lines
11 KiB
Go

package index
import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"slices"
"strings"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"git.novox.be/novox/mesh-sdk/go/keys"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/packagename"
)
// The packages state per series (DLV-I5; milestone § 2 item 1; design 51 *The versions*, ADR 0606 §1, ADR 0609 §1): beside
// the per-module digests (packages.go, ADR 0593 §2), one key per package and series, `<package>.<series>`, holding the newest
// trunk version of that series the builder numbered and the newest a registry seat published. It is filled from the
// builder's `version.numbered` and the package registry seats' `published`, never from a timer; `indexer_requirements`,
// the fingerprint input and `indexer_package_users` read it. A tagged version (a group's) is never a series' newest.
//
// A series key always holds a dot (`mesh-sdk/go.0.1`), which EscapeName never leaves in a per-module key: a reader tells the
// two forms apart by IsSeriesKey.
// SeriesPackage is the value of a series key, and SeriesVersion one version of a series: the seat mesh-catalog's
// types (the newest published version's; the numbered one's is the same fields, numberedOf), which every reader of the
// state decodes.
type (
SeriesPackage = meshcatalog.PackagesByPackageSeriesValue
SeriesVersion = meshcatalog.PackagesByPackageSeriesValueNewest
)
// numberedOf is a series version as the series' numbered one: the same fields in the contract's other type.
func numberedOf(v SeriesVersion) *meshcatalog.PackagesByPackageSeriesValueNumbered {
n := &meshcatalog.PackagesByPackageSeriesValueNumbered{Commit: v.Commit, Digest: v.Digest, Fingerprint: v.Fingerprint, Version: v.Version}
if c := v.Contents; c != nil {
p := publishedOfContents(*c)
nc := &meshcatalog.PackagesByPackageSeriesValueNumberedContents{At: p.At, Commit: p.Commit, Dir: p.Dir, Module: p.Module, Repository: p.Repository, Requires: p.Requires, V: p.V}
for _, d := range p.Packages {
nc.Packages = append(nc.Packages, meshcatalog.PackagesByPackageSeriesValueNumberedContentsPackagesItem{Package: d.Package, Digest: d.Digest})
}
n.Contents = nc
}
return n
}
// SeriesKey is the key of a package's series in the packages state (SeriesPackageKey).
func SeriesKey(pkg, series string) string { return SeriesPackageKey(pkg, series) }
// IsSeriesKey says whether a key of the packages state is a series key, not a per-module one (IsSeriesPackageKey).
func IsSeriesKey(key string) bool { return IsSeriesPackageKey(key) }
// SeriesOf is the series of a trunk version (`0.1.15` is of `0.1`); false for a version that is no `<major>.<minor>.<patch>`
// or that carries a tag (a pre-release).
func SeriesOf(version string) (string, bool) {
p, ok := semverParts("v" + strings.TrimPrefix(version, "v"))
if !ok || p.pre != "" {
return "", false
}
return fmt.Sprintf("%d.%d", p.core[0], p.core[1]), true
}
// HeardVersion is what the indexer takes of a numbered or a published: the package's name in its ecosystem, the seat, the version and its
// tag, the commit, digest and fingerprint, and the Go module path a published names.
type HeardVersion struct {
Package, Seat, Version, Tag, Commit, Digest, Fingerprint, GoModule string
// Published is true for a seat's published, false for the builder's numbered.
Published bool
}
// HearVersion keeps a trunk version in its series' key: as the series' newest numbered, and, once published, as its newest.
// A tagged version, a version that does not read and an older one than held are passed over (false). Nil Packages keeps
// nothing.
func (ix *Indexer) HearVersion(ctx context.Context, h HeardVersion) (bool, error) {
series, ok := SeriesOf(h.Version)
if ix.Packages == nil || h.Package == "" || h.Tag != "" || !ok {
return false, nil
}
v := &SeriesVersion{Version: strings.TrimPrefix(h.Version, "v"), Commit: h.Commit, Digest: h.Digest, Fingerprint: h.Fingerprint}
numbered := numberedOf(*v)
if h.Published && h.GoModule != "" && h.Commit != "" && ix.Files != nil {
// The digests of the published version's commit, read before the write (DLV-I6): a user accepting the series is
// fingerprinted against what this version holds, not against the latest merge.
repo, _, _ := trunk.PackageRepository(h.GoModule)
got, err := ix.packagesAt(ctx, repo, h.Commit, []string{h.GoModule}, nil)
switch p, ok := got[h.GoModule]; {
case err != nil:
ix.logf("%s %s: its contents at %s are not read, so its users keep the commit form until its next publish: %v", h.Package, h.Version, h.Commit, err)
case !ok:
ix.logf("%s %s: the forge's listing at %s holds no Go module %s, so its users keep the commit form", h.Package, h.Version, h.Commit, h.GoModule)
default:
// When the contents were read: a series version's Contents is a published module, which names its time
// (DecodeSeriesPackage refuses one with none).
p.At = ptr(ix.now().UTC())
v.Contents = contentsOfPublished(p)
}
}
newer := func(held string, _ bool) bool { return CompareVersions("v"+held, "v"+v.Version) < 0 }
kept := false
err := write(ctx, ix.Packages, SeriesKey(h.Package, series), func(value []byte, found bool) ([]byte, bool, error) {
s := SeriesPackage{V: keys.Version, Name: h.Package, Series: series}
if found {
if err := json.Unmarshal(value, &s); err != nil {
ix.logf("packages.%s holds what is not a series, written anew: %v", SeriesKey(h.Package, series), err)
s = SeriesPackage{V: keys.Version, Name: h.Package, Series: series}
}
}
kept = false
if s.Name == "" {
s.Name, kept = h.Package, true
}
if s.Numbered == nil || newer(s.Numbered.Version, true) {
s.Numbered, kept = numbered, true
}
if h.Published && (s.Newest == nil || newer(s.Newest.Version, true)) {
s.Newest, kept = v, true
}
if h.Seat != "" && s.Seat != h.Seat {
s.Seat, kept = h.Seat, true
}
if h.GoModule != "" && s.GoModule != h.GoModule {
s.GoModule, kept = h.GoModule, true
}
if !kept {
return nil, false, nil
}
s.At = ptr(ix.now())
next, err := json.Marshal(s)
return next, false, err
})
return kept && err == nil, err
}
// SeriesAll is every series key of the packages state, by key; empty with no state or one that does not answer.
func (ix *Indexer) SeriesAll(ctx context.Context) map[string]SeriesPackage {
out := map[string]SeriesPackage{}
if ix.Packages == nil {
return out
}
keys, err := ix.Packages.Keys(ctx)
if err != nil {
ix.logf("the packages state cannot be listed, so no series is read: %v", err)
return out
}
for _, key := range keys {
if !IsSeriesKey(key) {
continue
}
value, _, found, err := ix.Packages.Get(ctx, key)
if err != nil || !found {
continue
}
s, err := DecodeSeriesPackage(value)
if err != nil {
ix.logf("packages.%s holds what is not a series, left out: %v", key, err)
continue
}
out[key] = s
}
return out
}
// acceptedSeries is the packages state as a module of repository whose manifest is m is fingerprinted against (DLV-I6;
// ADR 0606 §4, ADR 0609 §1; design 32 *A package, its series, and the series its users accept*): each mesh Go module of
// another repository whose package's series the module accepts (Manifest.AcceptedSeries) and a seat published, by that
// series' newest version, its digests read at that version's commit, and pinned to the version itself (Go's form,
// `v0.1.15`). A series with no published version yet, or whose contents the forge did not list, leaves the module's
// latest published commit (the commit form, milestone § 2 item 4). A module of the same repository takes its sibling by
// path, never by a series. With nothing to pin, published is answered as it is.
func acceptedSeries(series map[string]SeriesPackage, m catalogue.Manifest, repository string, published map[string]meshcatalog.PackagesByModuleValue) (map[string]meshcatalog.PackagesByModuleValue, sdkstep.PackageVersions) {
var out map[string]meshcatalog.PackagesByModuleValue
var pins sdkstep.PackageVersions
for _, key := range slices.Sorted(maps.Keys(series)) {
s := series[key]
if s.GoModule == "" || s.Newest == nil || s.Newest.Contents == nil || m.AcceptedSeries(s.Name) != s.Series {
continue
}
if repo, _, ok := trunk.PackageRepository(s.GoModule); !ok || trunk.SameRepository(repo, repository) {
continue
}
if out == nil {
out, pins = maps.Clone(published), sdkstep.PackageVersions{}
}
p := publishedOfContents(*s.Newest.Contents)
version := "v" + s.Newest.Version
p.Commit = version
out[s.GoModule], pins[s.GoModule] = p, version
}
if out == nil {
return published, nil
}
return out, pins
}
// KeepPackageNames writes, at a trunk merge, each package artifact's name in its own ecosystem into its series key
// (design 32): a Go package's module path from the go.mod under its paths at the merge commit, a TypeScript package's
// package.json name, each read through the forge. The key keeps what it held and gains the name and the module and
// artifact it is built from, so the builder numbers it under that name before its first version. A name the forge does
// not answer is said and left for the next merge; a manifest that does not decode is the judge's to refuse.
func (ix *Indexer) KeepPackageNames(ctx context.Context, f forgemsg.Forge) error {
if ix.Packages == nil || ix.ReadFile == nil || f.Kind != forgemsg.ForgeMerged || f.Merge == "" {
return nil
}
var errs []error
for _, dir := range slices.Sorted(maps.Keys(f.Manifests)) {
m, err := catalogue.Decode([]byte(f.Manifests[dir]))
if err != nil || m.Build == nil {
continue
}
for _, a := range m.Build.Artifacts {
if a.Kind != catalogue.ArtifactPackage {
continue
}
name, err := packagename.Of(ctx, packagename.ReadFile(ix.ReadFile), f.Repository, f.Merge, dir, a)
if err != nil {
ix.logf("packages: the name of %s/%s at %.12s is not read, so it is numbered under no name until the next merge: %v", m.Module, a.Name, f.Merge, err)
continue
}
series, built := a.SeriesOf(), m.Module+"/"+a.Name
errs = append(errs, write(ctx, ix.Packages, SeriesKey(name, series), func(value []byte, found bool) ([]byte, bool, error) {
s := SeriesPackage{V: keys.Version, Series: series}
if found && json.Unmarshal(value, &s) != nil {
s = SeriesPackage{V: keys.Version, Series: series}
}
if s.Name == name && s.Package == built && (a.Language != "go" || s.GoModule == name) {
return nil, false, nil
}
s.Name, s.Package = name, built
if a.Language == "go" {
s.GoModule = name
}
s.At = ptr(ix.now())
next, err := json.Marshal(s)
return next, false, err
}))
}
}
return errors.Join(errs...)
}