Files
mesh-host/internal/selfcompose/versions.go
T
jschoubben 845de02631 Read a module's version through the registry seats' list
H2's registry seat definitions fold the version read into list, filtered by
module, so the validator refused the engine's manifest for invoking
versions. The engine reads a version as a tag in list: oci-artifact-registry
by the module, every page while it says next, the blob a manifest keeps;
container-image-registry, whose list takes no module, by the repository's
name. The manifest invokes list, and the node grant the tests run under
follows it.

Builds: design 47 H2 §779, §813 (7); H-A-350 DMC-10, H-A-415 (2)
2026-11-22 01:12:33 +01:00

309 lines
12 KiB
Go

package selfcompose
import (
"encoding/json"
"fmt"
"maps"
"slices"
"sort"
"strings"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
"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-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry"
"git.novox.be/novox/mesh-sdk/go/buscalls"
"git.novox.be/novox/mesh-sdk/go/catalogue"
)
// **A want names its version; the node-engine resolves the digests** (design 47 G A-430, A-430a, the lead's answer A-430b).
// A want of a released build names the module's version and no parts: each artifact of the build's entry is looked up at that
// version through the verb `list` of the registry seat of its kind (oci-artifact-registry for a bundle or an archive,
// container-image-registry for an image), under the name the builder numbered it by: `<module>/<artifact>`, or the entry's
// package name for a package. An upstream copy is not numbered (it is mirrored by its pinned digest): its digest is the one its
// manifest's `from` pins. A want that names parts (a build no version is known of) is read by its parts as before. A lookup that
// fails is said in the journal, naming the module, the version and the seat, and the module is held whole: never composed partly.
// DefaultResolveAgain is how long a failed lookup stands before it is asked again when the setting `resolve-again-minutes` gives
// nothing (Env.ResolveAgain 0); a pass is asked then (H-A-146 "after a bound"; principle 11: a bound is a setting with a default).
const DefaultResolveAgain = time.Minute
// resolveAgain is how long a failed lookup stands before it is asked again: the setting, or DefaultResolveAgain.
func (c *Composer) resolveAgain() time.Duration {
if c.env.ResolveAgain > 0 {
return c.env.ResolveAgain
}
return DefaultResolveAgain
}
// listVerb is the verb a lookup asks: the registry seats read a version through `list`, filtered by module on
// oci-artifact-registry (design 47 H2 §813 (7), DMC-10, H-A-415 (2)).
const listVerb = "list"
// listPagesMost bounds the pages one lookup reads of a seat's list.
const listPagesMost = 64
// resolvedKey names one resolution: a module's build at a version.
type resolvedKey struct{ module, fp, version string }
// versionParts is a module's build at a version as resolved: its digests by artifact reference, or why not, and when that was found.
// forgotten marks a resolution whose fetch failed (H-A-146): it stands as a failed one, and is asked again at the module's next want,
// at the next compose by hand, or after resolveAgain.
type versionParts struct {
parts map[string]string
failed string
at time.Time
forgotten bool
}
// registryList is the answer of either seat's verb `list` (form 2): each repository with the manifests it holds, each
// manifest's digest, the blob it keeps (a bundle's or an archive's) and the tags naming it; capped with next when the rest
// is on another page.
type registryList struct {
Repositories []struct {
Name string `json:"name"`
Manifests []struct {
Digest string `json:"digest"`
Artifact string `json:"artifact,omitempty"`
Tags []string `json:"tags,omitempty"`
} `json:"manifests"`
} `json:"repositories"`
Capped bool `json:"capped,omitempty"`
Total int64 `json:"total,omitempty"`
Next string `json:"next,omitempty"`
}
// seatOfKind is the registry seat an artifact of kind is numbered in, and whether one is (design 51 *The seats*).
func seatOfKind(kind string) (string, bool) {
switch kind {
case catalogue.ArtifactImage:
return containerimageregistry.Seat, true
case catalogue.ArtifactBundle, catalogue.ArtifactArchive:
return ociartifactregistry.Seat, true
}
return "", false
}
// registryName is the name an artifact of module is numbered under: `<module>/<artifact>`, or a package's name in its
// ecosystem as the entry gives it.
func registryName(module string, a meshcatalog.Artifact) string {
_, artifact, _ := strings.Cut(a.Ref, ":")
if a.Kind == catalogue.ArtifactPackage && a.Package != nil && a.Package.Name != "" {
return a.Package.Name
}
return module + "/" + artifact
}
// partsOf is the digest of each artifact of e for want w, by artifact reference, and the artifacts it names no digest for ("" when
// each has one). A want names its version alone (design 47 H §750); its digests are resolved through the registry seats.
func (c *Composer) partsOf(module string, e meshcatalog.CatalogEntry, w meshwants.WantsByWantNodeModuleValue) (map[string]string, string) {
var parts map[string]string
if w.Version != "" && len(e.Artifacts) > 0 {
var why string
if parts, why = c.resolve(module, e, w); why != "" {
return nil, why
}
}
return parts, partsMissing(e, parts)
}
// partsMissing names the artifacts of e whose digest parts does not give; "" when each has one.
func partsMissing(e meshcatalog.CatalogEntry, parts map[string]string) string {
var out []string
for _, a := range e.Artifacts {
if parts[a.Ref] == "" {
out = append(out, a.Ref)
}
}
sort.Strings(out)
return strings.Join(out, ", ")
}
// resolve is the digests of module's build at w's version, from what is resolved already or asked of the seats now. why names
// what failed; a failure stands resolveAgain, then is asked again.
func (c *Composer) resolve(module string, e meshcatalog.CatalogEntry, w meshwants.WantsByWantNodeModuleValue) (map[string]string, string) {
key := resolvedKey{module, w.Fp, w.Version}
now := c.now()
c.mu.Lock()
r, ok := c.resolved[key]
c.mu.Unlock()
if ok && (r.failed == "" || now.Sub(r.at) < c.resolveAgain()) {
return r.parts, r.failed
}
parts, failed := c.lookUp(module, e, w)
c.mu.Lock()
if c.resolved == nil {
c.resolved = map[resolvedKey]versionParts{}
}
// Only the resolutions of the builds a want names now are kept.
for k := range c.resolved {
if cur, wanted := c.wants[k.module]; k.module != module && (!wanted || cur.Fp != k.fp || cur.Version != k.version) {
delete(c.resolved, k)
}
}
c.resolved[key] = versionParts{parts: parts, failed: failed, at: now}
c.mu.Unlock()
if failed != "" {
time.AfterFunc(c.resolveAgain(), c.askRehand)
return nil, failed
}
return parts, ""
}
// forgetFetched forgets every resolution that names digest, as its fetch failed for why (design 47 H, the lead's answer H-A-146; A-430a:
// the bytes are fetched by the digest the versions answer gives): the store's answer may have been wrong, and a resolution kept for
// the process's life would never see it fixed. Each is said in the journal; the module is held until the seat's verb versions is
// asked again, at its next want, at the next compose by hand, or after resolveAgain. A resolution whose fetch succeeded is kept.
func (c *Composer) forgetFetched(digest, why string) {
if digest == "" {
return
}
now := c.now()
var lines []string
c.mu.Lock()
for k, r := range c.resolved {
if r.failed != "" || !slices.Contains(slices.Collect(maps.Values(r.parts)), digest) {
continue
}
line := fmt.Sprintf("resolution of %s %s forgotten: %s", k.module, k.version, why)
c.resolved[k] = versionParts{failed: line + "; the seats' verb versions is asked again at its next want, at the next compose or in " + c.resolveAgain().String(),
at: now, forgotten: true}
lines = append(lines, line)
}
c.mu.Unlock()
sort.Strings(lines)
for _, line := range lines {
c.env.Say(line)
}
if len(lines) > 0 {
time.AfterFunc(c.resolveAgain(), c.askRehand)
}
}
// ForgetFetched is forgetFetched for the apply's fetch of a bundle the store answered and that failed (H-A-150, apply.FetchFailed).
func (c *Composer) ForgetFetched(digest, why string) { c.forgetFetched(digest, why) }
// dropForgottenLocked ends the standing of every forgotten resolution (H-A-146), of module alone or, for "", of all: the next
// pass asks the seats' verb versions again. Under mu.
func (c *Composer) dropForgottenLocked(module string) {
for k, r := range c.resolved {
if r.forgotten && (module == "" || k.module == module) {
delete(c.resolved, k)
}
}
}
// lookUp asks each artifact's digest at w's version of the seat of its kind. Every failure is said, naming the module, the version
// and the seat; any failure resolves nothing of the build.
func (c *Composer) lookUp(module string, e meshcatalog.CatalogEntry, w meshwants.WantsByWantNodeModuleValue) (map[string]string, string) {
parts := map[string]string{}
var failed []string
fail := func(seat, ref, why string) {
line := fmt.Sprintf("%s at version %s: the seat %s's verb versions resolves no digest of %s: %s, so the node holds %s and composes nothing of it",
module, w.Version, seat, ref, why, module)
c.sayOnceEach(line)
failed = append(failed, fmt.Sprintf("%s (seat %s: %s)", ref, seat, why))
}
var manifest *catalogue.Manifest
for _, a := range e.Artifacts {
_, artifact, _ := strings.Cut(a.Ref, ":")
if a.Kind == catalogue.ArtifactUpstream {
if manifest == nil {
_, raw, err := entryManifest(e)
m, derr := catalogue.Decode(raw)
if err != nil || derr != nil {
fail("(none)", a.Ref, "the entry's manifest cannot be read for the digest its upstream copy pins")
continue
}
manifest = &m
}
if d := pinnedDigest(*manifest, artifact); d != "" {
parts[a.Ref] = d
} else {
fail("(none)", a.Ref, "the manifest pins no digest of the upstream copy")
}
continue
}
seat, numbered := seatOfKind(a.Kind)
if !numbered {
fail("(none)", a.Ref, "no registry seat the node-engine reads numbers an artifact of kind "+a.Kind)
continue
}
digest, why := c.versionDigest(seat, module, registryName(module, a), w)
if why != "" {
fail(seat, a.Ref, why)
continue
}
parts[a.Ref] = digest
}
if len(failed) > 0 {
sort.Strings(failed)
return nil, "no digest is resolved at version " + w.Version + " for " + strings.Join(failed, ", ")
}
return parts, ""
}
// versionDigest is the digest the seat's verb list answers for the repository name at w's version, a tag naming it: a
// version names one build (A-430a). oci-artifact-registry is asked for the module's repositories (`list {module}`) and
// answers the blob a manifest keeps, the digest a bundle or an archive is fetched by; container-image-registry, whose list
// takes no module, is asked for the repository by its name and answers the image manifest's digest. Every page is read
// while the answer says next (design 47 H2 §779, §813 (7), H-A-415 (2)).
func (c *Composer) versionDigest(seat, module, name string, w meshwants.WantsByWantNodeModuleValue) (string, string) {
if c.env.Registry == nil {
return "", "this node-engine reads no registry seat"
}
in := map[string]string{"name": name}
if seat == ociartifactregistry.Seat {
in = map[string]string{"module": module}
}
for page := 0; page < listPagesMost; page++ {
raw, err := c.env.Registry("seat:"+seat+"."+listVerb, in)
if err != nil {
return "", fmt.Sprintf("the call for %s failed: %v", name, err)
}
answer, said, ok := buscalls.Unwrap(raw)
if !ok {
return "", fmt.Sprintf("the seat refused %s: %s", name, said)
}
var l registryList
if err := json.Unmarshal(answer, &l); err != nil {
return "", fmt.Sprintf("its answer for %s cannot be read: %v", name, err)
}
for _, r := range l.Repositories {
if r.Name != name {
continue
}
for _, m := range r.Manifests {
if !slices.Contains(m.Tags, w.Version) || m.Digest == "" {
continue
}
if seat == ociartifactregistry.Seat && m.Artifact != "" {
return m.Artifact, ""
}
return m.Digest, ""
}
}
if l.Next == "" {
return "", fmt.Sprintf("it holds no version %s of %s", w.Version, name)
}
in["next"] = l.Next
}
return "", fmt.Sprintf("its list of %s did not end within %d pages", module, listPagesMost)
}
// pinnedDigest is the digest the manifest's upstream artifact pins in its from (`<image>@sha256:<hex>`); "" when none.
func pinnedDigest(m catalogue.Manifest, artifact string) string {
if m.Build == nil {
return ""
}
for _, a := range m.Build.Artifacts {
if a.Name == artifact {
if _, d, ok := strings.Cut(a.From, "@"); ok && strings.HasPrefix(d, "sha256:") {
return d
}
}
}
return ""
}