package selfcompose import ( "crypto/sha256" "encoding/hex" "encoding/json" "fmt" "slices" "git.novox.be/novox/mesh-sdk/go/catalogue" engcomp "github.com/novox/mesh-host/internal/compositions" ) // The one place that answers "the manifest of the build a module runs here" (design 47 G A-484, joining A-480, A-480a, A-483 and // A-461d). Every step that needs it reads it here and nowhere else: what the module provides (a held module keeps providing // what it runs, frombus.Scope.RunningManifest), what it keeps on the bus and whether that changes form (FormChanges), the // artifact store's holder (layModules, A-461d), and the picture of the catalog the composer resolves from. It answers in this // order: the entry of the running build; else the composition this node runs of that build; else the module's trunk manifest // through the seat mesh-catalog's verb `module`. // The three sources of the one place, in the words it says them in. const ( SourceEntry = "the entry of the running build" SourceComposition = "the composition this node runs of it" SourceTrunk = "the module's trunk manifest in the catalog" ) // runningAnswer is what the one place answers of one module. type runningAnswer struct { // Source is the source that answered; empty when none did, and Why then names the three. Source string // FP is the build the answer is of: the running build for an entry, the composition's build, or the trunk's. FP string // Known says the manifest itself was read (an entry or the trunk); a composition names its build and whether it keeps // data, which is all it holds of the manifest. Known bool Manifest catalogue.Manifest Raw []byte Digest string // Data is, from the composition, whether the running build keeps data on the bus. Data bool // Repository, Commit and Path are where the manifest's entry or the trunk's answer says it is built from. Repository, Commit, Path string // Why names what each of the three said, when none answered. Why string // RunFP is the running build the trunk manifest is asked for and kept against (A-461b). RunFP string // NeedTrunk says the trunk manifest is the one source left and is not held yet: the composer asks it beside the pass. NeedTrunk bool // Said is the line to say, once per module and source (principle 3); empty when it was said before. Said string } // runningFpsLocked are the fingerprints of the build module runs on node, as the node's own record says (c.mu held), most direct // first: the build its running event names; on this node else the hint the caller knows and the build of the composition it runs; // on another node also the build its placement names, the mesh's truth of what runs there (A-461). func (c *Composer) runningFpsLocked(running []engcomp.Composed, node, module, hint string) (fps []string) { add := func(fp string) { if fp != "" && !slices.Contains(fps, fp) { fps = append(fps, fp) } } if r, ok := c.heard[node][module]; ok && !r.stopped { add(r.fingerprint) } if node != c.env.Node { add(c.placedBuild(node, module)) return fps } add(hint) for _, r := range running { if r.Module == module { add(r.FP) } } return fps } // runningLocked answers the manifest of the build module runs on node (c.mu held; running is what this node runs, read before // the lock), from the three sources in order, and says which answered once per module and source. func (c *Composer) runningLocked(running []engcomp.Composed, node, module, hint string) runningAnswer { fps := c.runningFpsLocked(running, node, module, hint) fp := "" if len(fps) > 0 { fp = fps[0] } var a runningAnswer entryWhy, compWhy, trunkWhy := "", "", "" // 1. The entry of the running build. for _, f := range fps { k, ok := c.keptEnt[f] if !ok || k.entry.Module == "" { continue } if digest, raw, err := entryManifest(k.entry); err == nil { if man, err := catalogue.Decode(raw); err == nil { a = runningAnswer{Source: SourceEntry, FP: f, Known: true, Manifest: man, Raw: raw, Digest: digest, Repository: k.entry.Repository, Commit: k.entry.Commit, Path: k.entry.Path} break } } } if a.Source == "" { entryWhy = fmt.Sprintf("no entry of the build %s is held", orNone(fp)) // 2. The composition this node runs of that build, whatever the want's build. if node == c.env.Node { for _, r := range running { if r.Module != module || r.FP == "" { continue } // The composition holds no manifest, only its build and whether that keeps data; its build's entry, if held, was // read as the running build's above. a = runningAnswer{Source: SourceComposition, FP: r.FP, Data: r.Data} break } if a.Source == "" { compWhy = "this node runs no composition of it" } } else { compWhy = "only the node itself runs compositions of its modules" } } // 3. The module's trunk manifest through the seat's verb `module`. if !a.Known { if t, ok := c.trunkBuilds[module]; ok && t.running == fp && len(t.manifest) > 0 { if raw, err := json.Marshal(t.manifest); err == nil { if man, err := catalogue.Decode(raw); err == nil { keep := a // the composition's words stay: it says whether the build keeps data a = runningAnswer{Source: SourceTrunk, FP: t.fp, Known: true, Manifest: man, Raw: raw, Digest: digestOf(raw), Repository: t.repository, Commit: t.commit, Path: t.path, Data: keep.Data} } } } if !a.Known { trunkWhy = c.trunkWhy[module] if trunkWhy == "" { trunkWhy = "the seat mesh-catalog's verb module has not answered" } a.NeedTrunk, a.RunFP = true, fp } } if a.Source == "" || (!a.Known && a.Source == SourceComposition) { a.Why = fmt.Sprintf("the manifest of the build %s runs here is read from none of: %s (%s); %s (%s); %s (%s)", module, SourceEntry, orWords(entryWhy, "its entry is held without a manifest"), SourceComposition, orWords(compWhy, "it names the build "+a.FP+" and no entry of that build is held"), SourceTrunk, trunkWhy) } if a.Source != "" { line := fmt.Sprintf("%s runs a build whose manifest is read from %s", module, a.Source) key := "running-manifest:" + node + "/" + module + "/" + a.Source if c.told == nil { c.told = map[string]bool{} } if !c.told[key] { c.told[key] = true a.Said = line } } return a } // runningManifest is runningLocked with the lock taken here: what runs is read first, since the agent's record may wait. func (c *Composer) runningManifest(node, module, hint string) runningAnswer { var running []engcomp.Composed if c.env.Running != nil { running = c.env.Running() } c.mu.Lock() a := c.runningLocked(running, node, module, hint) c.mu.Unlock() if a.Said != "" { c.env.Say(a.Said) } return a } // countsAsRunning says this node holds module on as it runs it (A-482, c.mu held): it is heard running here, in what the agent // runs, or its have names a build in the phase running or held. A module wanted here with none of these runs nothing here. func (c *Composer) countsAsRunning(module string) bool { if run := c.heard[c.env.Node][module]; run.stopped { return false } return c.runsHere(module) || c.haveRuns[module] } // RunningManifest is what the one place says of module's running build here, in words, for the tool composition: the source that // answered with its build, or the words naming the three it tried. func (c *Composer) RunningManifest(module string) (source, fp, words string) { a := c.runningManifest(c.env.Node, module, "") if a.Source == "" || (!a.Known && a.Why != "") { return a.Source, a.FP, a.Why } return a.Source, a.FP, fmt.Sprintf("the manifest of the build %s runs here is read from %s", module, a.Source) } // runningFor is the manifest of the build a module runs on node, for frombus.Scope.RunningManifest: only a module counted as // running here (A-482) answers, and only a manifest read, never a composition alone. func (c *Composer) runningFor(node string) func(module string) (catalogue.Manifest, bool) { return func(module string) (catalogue.Manifest, bool) { c.mu.Lock() counts := node != c.env.Node || c.countsAsRunning(module) c.mu.Unlock() if !counts { return catalogue.Manifest{}, false } a := c.runningManifest(node, module, "") return a.Manifest, a.Known } } func digestOf(raw []byte) string { sum := sha256.Sum256(raw) return "sha256:" + hex.EncodeToString(sum[:]) } func orNone(s string) string { if s == "" { return "(none named)" } return s } func orWords(s, otherwise string) string { if s == "" { return otherwise } return s }