A want with parts is applied only when every fetched archive, process and container digest is among the parts and every part is a fetched digest; otherwise the module keeps running what it has and its have is held with detail parts-differ naming the digests. A want with no parts is applied as before. Builds: design 47 G §524, §379 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/declaration"
|
|
)
|
|
|
|
// PartsDiffer is the start of the detail of a have held because a want's parts and the digests its composition fetches
|
|
// disagree (design 47 G section 524).
|
|
const PartsDiffer = "parts-differ:"
|
|
|
|
// partsDifference checks a want's `parts` against the digests of the archive, process and container resources of the
|
|
// composition, which are the digests the apply fetches and checks (design 47 G section 524). A want with no `parts` (an
|
|
// assigner of before G, or a node whose record does not state `want-parts`) is applied as today: it returns "". With
|
|
// `parts`, the want is applied only when the digest of every resource fetched is among the values of `parts` and every
|
|
// value of `parts` is the digest of a resource fetched; otherwise it returns the detail naming each digest.
|
|
func partsDifference(parts map[string]string, resources []json.RawMessage) string {
|
|
if len(parts) == 0 {
|
|
return ""
|
|
}
|
|
fetched := map[string]bool{}
|
|
for _, raw := range resources {
|
|
r, err := declaration.ParseResource(raw)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch res := r.(type) {
|
|
case *declaration.Archive:
|
|
if res.Source != "" {
|
|
fetched[res.Digest] = true
|
|
}
|
|
case *declaration.Process:
|
|
if res.Source != "" {
|
|
fetched[res.Digest] = true
|
|
}
|
|
case *declaration.Container:
|
|
if strings.HasPrefix(res.Image, "sha256:") {
|
|
fetched[res.Image] = true
|
|
} else if _, digest, ok := strings.Cut(res.Image, "@"); ok {
|
|
fetched[digest] = true
|
|
}
|
|
}
|
|
}
|
|
named := map[string]bool{}
|
|
for _, d := range parts {
|
|
named[d] = true
|
|
}
|
|
var outside, missing []string
|
|
for d := range fetched {
|
|
if !named[d] {
|
|
outside = append(outside, d)
|
|
}
|
|
}
|
|
for d := range named {
|
|
if !fetched[d] {
|
|
missing = append(missing, d)
|
|
}
|
|
}
|
|
if len(outside) == 0 && len(missing) == 0 {
|
|
return ""
|
|
}
|
|
sort.Strings(outside)
|
|
sort.Strings(missing)
|
|
var said []string
|
|
if len(outside) > 0 {
|
|
said = append(said, "fetched and not in the want's parts: "+strings.Join(outside, ", "))
|
|
}
|
|
if len(missing) > 0 {
|
|
said = append(said, "in the want's parts and not fetched: "+strings.Join(missing, ", "))
|
|
}
|
|
return fmt.Sprintf("%s %s", PartsDiffer, strings.Join(said, "; "))
|
|
}
|