diff --git a/internal/selfcompose/selfcompose.go b/internal/selfcompose/selfcompose.go index 1ad36a7f..64feb327 100644 --- a/internal/selfcompose/selfcompose.go +++ b/internal/selfcompose/selfcompose.go @@ -460,6 +460,7 @@ func (c *Composer) ComposeNow(ctx context.Context) (Pass, error) { return Pass{}, errors.New("the node's composer runs no loop now: it composes nothing until its agent starts again") } c.mu.Lock() + c.dropForgottenLocked("") // a compose by hand asks again what a failed fetch made it forget (H-A-146) last := c.last c.mu.Unlock() if !last.At.IsZero() && time.Since(last.At) < askedWithin { @@ -857,6 +858,7 @@ func (c *Composer) takeWantLocked(u agent.Update) string { } delete(c.notTaken, module) delete(c.keptWants, module) + c.dropForgottenLocked(module) // its next want asks again what a failed fetch made it forget (H-A-146) w, err := meshwants.DecodeWantsByWantNodeModuleValue(u.Value) if u.Deleted || err != nil || w.Removed || w.Fp == "" { delete(c.wants, module) @@ -1691,11 +1693,15 @@ func (r reader) Archive(ctx context.Context, url, digest string) ([]byte, error) defer cancel() body, err := r.c.env.Fetch(fetching, url) if r.c.note(err) != nil { + // A resolution whose fetch failed is forgotten, and versions asked again (H-A-146). + r.c.forgetFetched(digest, fmt.Sprintf("the fetch of %s failed: %v", url, err)) return nil, err } sum := sha256.Sum256(body) if got := "sha256:" + hex.EncodeToString(sum[:]); got != digest { - return nil, fmt.Errorf("%s was recorded as %s and what arrived is %s", url, digest, got) + err := fmt.Errorf("%s was recorded as %s and what arrived is %s", url, digest, got) + r.c.forgetFetched(digest, "its fetch failed: "+err.Error()) + return nil, err } r.c.keepRead(func(k *Composer) { k.archives[digest] = body }) return body, nil diff --git a/internal/selfcompose/selfcompose_test.go b/internal/selfcompose/selfcompose_test.go index 0a924f67..d06377de 100644 --- a/internal/selfcompose/selfcompose_test.go +++ b/internal/selfcompose/selfcompose_test.go @@ -212,6 +212,7 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) { t.Run("it reads the catalog half by the fingerprints its wants name", composeFromEntries) t.Run("a running module without an entry stands as the newest build", runningModuleWithoutEntryStandsAsTheNewestBuild) t.Run("a want naming its version is resolved through the registry seats", aWantNamingItsVersionIsResolvedThroughTheRegistrySeats) + t.Run("a resolution whose fetch failed is forgotten", aResolutionWhoseFetchFailedIsForgotten) b := newFakeBus() fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64) digest := meshcatalogclient.ManifestDigest([]byte(lemurs)) diff --git a/internal/selfcompose/versions.go b/internal/selfcompose/versions.go index f3bf6060..8baa6fa0 100644 --- a/internal/selfcompose/versions.go +++ b/internal/selfcompose/versions.go @@ -3,6 +3,8 @@ package selfcompose import ( "encoding/json" "fmt" + "maps" + "slices" "sort" "strings" "time" @@ -33,10 +35,13 @@ const versionsVerb = "versions" 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 + parts map[string]string + failed string + at time.Time + forgotten bool } // registryVersions is the answer of either seat's verb `versions`: the two seats answer the same shape. @@ -125,6 +130,46 @@ func (c *Composer) resolve(module string, e meshcatalog.CatalogEntry, w meshwant 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 " + 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(resolveAgain, c.askRehand) + } +} + +// 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) { diff --git a/internal/selfcompose/versions_test.go b/internal/selfcompose/versions_test.go index 65aab52a..2f15afa7 100644 --- a/internal/selfcompose/versions_test.go +++ b/internal/selfcompose/versions_test.go @@ -2,11 +2,16 @@ package selfcompose import ( "context" + "crypto/sha256" + "encoding/hex" "encoding/json" "errors" "strings" "sync" "testing" + "time" + + "github.com/novox/mesh-host/internal/agent" "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" @@ -130,3 +135,79 @@ func TestAModulesClaimsAreReadFromTheEntryOfItsWantsBuild(t *testing.T) { t.Errorf("a module with no want has no manifest read: claims %v, known %v", claims, known) } } + +// Design 47 H, the lead's answer H-A-146 (A-430a: the bytes are fetched by the digest the versions answer gives): a resolution +// whose fetch failed is forgotten and said so in the journal; the seats' verb versions is asked again at the module's next want, at +// the next compose by hand, and after a bound while the module is held, never before; a resolution whose fetch succeeded is kept. +func aResolutionWhoseFetchFailedIsForgotten(t *testing.T) { + fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64) + body := []byte("the files") + sum := sha256.Sum256(body) + good := "sha256:" + hex.EncodeToString(sum[:]) + wrong := "sha256:" + strings.Repeat("e", 64) // the store's answer of the manifest's digest where the blob's was due + entry := meshcatalog.CatalogEntry{Module: "mail", Fp: fp, Manifest: map[string]any{"module": "mail", "version": "1"}, + Artifacts: []meshcatalog.Artifact{{Ref: "mail:files", Kind: "archive"}}} + want := meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: "1.2.3", Self: true} + for name, row := range map[string]struct { + answer string // the digest versions answers + fetch error // the store's answer to the fetch + then string // what follows the fetch: "" (a pass within the bound), want, compose, bound + asked bool // versions is asked again + forgets bool // the journal says it forgot + }{ + "a fetch that succeeded keeps its resolution": {answer: good, then: "bound"}, + "a 404, the next pass within the bound": {answer: wrong, fetch: errors.New("404 Not Found"), forgets: true}, + "a 404, then the module's next want": {answer: wrong, fetch: errors.New("404 Not Found"), then: "want", asked: true, forgets: true}, + "a 404, then a compose by hand": {answer: wrong, fetch: errors.New("404 Not Found"), then: "compose", asked: true, forgets: true}, + "a 404, then the bound while it is held": {answer: wrong, fetch: errors.New("404 Not Found"), then: "bound", asked: true, forgets: true}, + "bytes that are not of the digest": {answer: wrong, then: "want", asked: true, forgets: true}, + } { + now := time.Date(2026, 11, 20, 16, 0, 0, 0, time.UTC) + var said []string + env := Env{Node: "desk", Say: func(line string) { said = append(said, line) }, Now: func() time.Time { return now }} + calls := 0 + env.Registry = EntryCall(func(context.Context, string, string, any) (json.RawMessage, error) { + calls++ + return json.RawMessage(`{"versions":[{"digest":"` + row.answer + `","version":"1.2.3"}]}`), nil + }) + env.Fetch = func(context.Context, string) ([]byte, error) { return body, row.fetch } + env.Entry = EntryCall(func(context.Context, string, string, any) (json.RawMessage, error) { + return nil, errors.New("not asked") + }) + c := New(env) + c.wants["mail"] = want + c.keptEnt[fp] = keptEntry{entry: entry} + parts, _ := c.partsOf("mail", entry, want) + _, err := reader{c: c}.Archive(context.Background(), referenceOf("mail", "files", parts["mail:files"]), parts["mail:files"]) + if (err != nil) != row.forgets { + t.Errorf("%s: the fetch: %v", name, err) + } + switch row.then { + case "want": + raw, _ := json.Marshal(want) + c.takeWant(agent.Update{Key: meshwants.WantsByWantNodeModuleKey("desk", "mail"), Value: raw}) + case "compose": + c.watching.Store(true) + ended, cancel := context.WithCancel(context.Background()) + cancel() + c.ComposeNow(ended) + case "bound": + now = now.Add(resolveAgain) + } + before := calls + again, missing := c.partsOf("mail", entry, want) + if asked := calls > before; asked != row.asked { + t.Errorf("%s: versions asked again %v, want %v", name, asked, row.asked) + } + if row.forgets && !row.asked && (again != nil || !strings.Contains(missing, "forgotten")) { + t.Errorf("%s: parts %v, missing %q: want the module held, saying the resolution was forgotten", name, again, missing) + } + if !row.forgets && again["mail:files"] != good { + t.Errorf("%s: parts %v: want the resolution kept", name, again) + } + line := "resolution of mail 1.2.3 forgotten: " + if got := strings.Contains(strings.Join(said, "\n"), line); got != row.forgets { + t.Errorf("%s: said %q; want %q said: %v", name, said, line, row.forgets) + } + } +}