I8: the take-in fed by the forge bridge's messages goes with B1's forgemsg: the FORGE consumer and its handlers, the group end, the pending state with its keys, claims, dry take-in and hand tools (indexer_pending, indexer_take_in_pending, indexer_drop_pending, indexer_take_in_merge, kept again for the held take-in in I5), the catch-up by comparison (kickBehind, TakeInBehind, sayUntaken, takeInHeads, kickCatchUp, CatchUp, merge-untaken), the source numbering in memory (nextSource), the source verb's follow and Src stamping on a source answer, the bridge's merged message call, the pull request candidates written from pull and open messages, the checker's previews and verdict hearing. A repository's trees at a commit are the internal whole (wholeAt), which ApplyMessage takes in; the dry indexer stays for a run on request's candidate. The manifest drops the consumes of forge.**, the checker's verdict and version-delivered, the reads of the checker's previews and the invokes of open-pulls, bridge_merged_message and mesh-delivery.source. RoomAgain says room when a take-in is held on a full store (heldFull, I5 holds it). H-A-82: the fill of delivered versions into the edges goes: HearVersionDelivered, the delivered.<module> key, the first fill's kinds 2, 4 and 6 from the placement and the stream running, and outdated's kinds 2, 4 and 6; their entries read not yet delivered. The tests of the removed paths go (index_test's candidate groups, source_test, branch_take_in_test, previews_test, candidate_manifests_test, the FORGE handle tests); the versions tests now hold kinds 2, 4 and 6 as declarations. The integration tests still drive FORGE and are rewritten in I10. Builds: design 47 H §743, §744, §764 (the indexer); H-A-48, H-A-82 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
261 lines
8.9 KiB
Go
261 lines
8.9 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.
|
|
const (
|
|
ToolchainPrefix = "toolchain."
|
|
// 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 })
|
|
}
|
|
|
|
// 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, 4 and 6 are declarations, compared with nothing (design 47 H H-A-82). 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 {
|
|
_, entries, found, err := readNewestOf(ctx, ix.Revisions, TrunkBranch)
|
|
if err != nil {
|
|
return out, fmt.Errorf("the trunk's entries, which the kinds 3 and 5 are told by, cannot be read: %w", err)
|
|
}
|
|
if found {
|
|
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 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
|
|
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
|
|
}
|
|
|
|
// 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
|
|
}
|