E5: answer the node-engine seat's composition-diff
The node-engine serves seat verbs and no tools bundle, so composed values read by resource are a read verb beside composition: per resource, the entries that differ between what runs and a revision or digest of its composition key, environment entries by name, a secret's value masked. Builds: design 47 H2 §813 (3); H-A-349 DQ-12b
This commit is contained in:
@@ -1949,6 +1949,29 @@ func TestTheNodesOwnCompositionIsAppliedWhenItDiffersAndARecordMoveWaitsForAYes(
|
||||
if _, err := b.ag.Composition(context.Background(), "db", "sha256:"+strings.Repeat("0", 64), false); err == nil {
|
||||
t.Error("a digest no revision has was answered")
|
||||
}
|
||||
// Design 47 H2 §813 (3): composition-diff answers, by resource, the entries that differ between what runs and the
|
||||
// revision asked, by its digest or its revision; a secret's value is masked, an environment entry named by its name.
|
||||
if d, err := b.ag.CompositionDiff(context.Background(), "db", 0, ddigMoved); err != nil || d.Running != ddig || d.Other == nil ||
|
||||
d.Other.Digest != ddigMoved || len(d.Resources) != 1 || d.Resources[0].Resource != "db.server" || len(d.Resources[0].Entries) != 1 ||
|
||||
d.Resources[0].Entries[0] != (nodeengine.CompositionDiffEntry{Name: "image", Running: "db@sha256:" + strings.Repeat("1", 64),
|
||||
Other: "db@sha256:" + strings.Repeat("7", 64)}) {
|
||||
t.Errorf("composition-diff of db's waiting revision reads %+v (%v)", d, err)
|
||||
}
|
||||
if d, err := b.ag.CompositionDiff(context.Background(), "db", int64(view.Revisions[0].Revision), ""); err != nil ||
|
||||
d.Other == nil || d.Other.Digest != ddig || len(d.Resources) != 0 {
|
||||
t.Errorf("composition-diff of db's running revision, by its revision, reads %+v (%v)", d, err)
|
||||
}
|
||||
if _, err := b.ag.CompositionDiff(context.Background(), "db", 0, ""); err == nil {
|
||||
t.Error("composition-diff with neither a revision nor a digest was answered")
|
||||
}
|
||||
if got := DiffResources([]json.RawMessage{json.RawMessage(`{"id":"web.key","type":"file","sealed":"a","content":"x"}`),
|
||||
json.RawMessage(`{"id":"web.run","type":"process","env":{"MODE":"a","GONE":"1"}}`)},
|
||||
[]json.RawMessage{json.RawMessage(`{"id":"web.key","type":"file","sealed":"b","content":"y"}`),
|
||||
json.RawMessage(`{"id":"web.run","type":"process","env":{"MODE":"b"}}`)}); len(got) != 2 ||
|
||||
!slices.Equal(got[0].Entries, []nodeengine.CompositionDiffEntry{{Name: "content", Masked: true}, {Name: "sealed", Masked: true}}) ||
|
||||
!slices.Equal(got[1].Entries, []nodeengine.CompositionDiffEntry{{Name: "env.GONE", Running: "1"}, {Name: "env.MODE", Running: "a", Other: "b"}}) {
|
||||
t.Errorf("the entries that differ, a secret masked, read %+v", got)
|
||||
}
|
||||
// Issue 774 (ace, 2026-11-04 01:29): the node's own revision shows its own fields, the provisioner client's account
|
||||
// and the adoption among them, and what the composer's last pass held.
|
||||
ownKey, ownRaw, ownDig := encodedAs(t, compositions.Composed{Node: node, Own: compositions.NodeOwn{Provisioner: "mesh-provisioner-client",
|
||||
|
||||
@@ -2,9 +2,14 @@ package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient"
|
||||
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
||||
engcomp "github.com/novox/mesh-host/internal/compositions"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
@@ -335,3 +340,160 @@ func (a *Agent) revisionsOf(ctx context.Context, key string) ([]compNow, error)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// CompositionDiff answers the node-engine seat's read verb composition-diff (design 47 H2 §813 (3), H-A-349 DQ-12b): one
|
||||
// module's composition that runs beside another revision of its composition key, given by its revision or its digest,
|
||||
// as the entries that differ per resource; environment entries by name, a secret's value masked. It changes nothing.
|
||||
func (a *Agent) CompositionDiff(ctx context.Context, module string, revision int64, digest string) (nodeengine.CompositionDiffAnswer, error) {
|
||||
answer := nodeengine.CompositionDiffAnswer{Node: a.env.Node, Module: module, Resources: []nodeengine.CompositionDiffResource{}}
|
||||
switch {
|
||||
case module == "":
|
||||
return answer, errors.New("name the module, or node for the node's own composition")
|
||||
case (revision == 0) == (digest == ""):
|
||||
return answer, errors.New("give the revision or the digest of the composition to compare with, one of the two")
|
||||
}
|
||||
a.mu.Lock()
|
||||
fp := ""
|
||||
if w := a.wants[module]; w.Present {
|
||||
fp = w.Want.Fp
|
||||
}
|
||||
running, known := a.runningComposition(module)
|
||||
a.mu.Unlock()
|
||||
if fp == "" && known {
|
||||
fp = running.FP
|
||||
}
|
||||
if module != NodeModule && fp == "" {
|
||||
return answer, fmt.Errorf("%s has no want and nothing runs of it on %s, so no composition of it is kept here", module, a.env.Node)
|
||||
}
|
||||
key, err := a.compositionKey(module, fp)
|
||||
if err != nil {
|
||||
return answer, err
|
||||
}
|
||||
revs, err := a.revisionsOf(ctx, key)
|
||||
if err != nil {
|
||||
return answer, fmt.Errorf("the revisions of %s cannot be read: %w", key, err)
|
||||
}
|
||||
var other *compNow
|
||||
for i := range revs {
|
||||
if (revision != 0 && revs[i].Rev == uint64(revision)) || (digest != "" && revs[i].Err == nil && revs[i].Dig == digest) {
|
||||
other = &revs[i]
|
||||
}
|
||||
}
|
||||
if other == nil {
|
||||
return answer, fmt.Errorf("no revision of %s the bucket keeps is the one asked; composition lists those it keeps", key)
|
||||
}
|
||||
answer.Other = &nodeengine.Revision{Key: key, Revision: int64(other.Rev), Digest: other.Dig, Resources: int64(len(other.C.Resources)),
|
||||
Latest: other.Rev == revs[len(revs)-1].Rev}
|
||||
if other.Err != nil {
|
||||
answer.Other.Refused = other.Err.Error()
|
||||
return answer, nil
|
||||
}
|
||||
var from []json.RawMessage
|
||||
if known {
|
||||
from = running.Resources
|
||||
if answer.Running, err = engcomp.DigestOf(running); err != nil {
|
||||
return answer, fmt.Errorf("the composition that runs of %s has no digest: %w", module, err)
|
||||
}
|
||||
}
|
||||
answer.Resources = DiffResources(from, other.C.Resources)
|
||||
return answer, nil
|
||||
}
|
||||
|
||||
// DiffResources is, per resource by its id, the entries that differ from running to other (design 47 H2 §813 (3)): each
|
||||
// top-level field by its name, the environment's entries each by `env.<name>`, and a value the declaration keeps secret
|
||||
// (a file's sealed, vault-sealed or secrets, and the content of a file that has any of them) masked. Resources in id
|
||||
// order, entries in name order; none that is the same in both.
|
||||
func DiffResources(running, other []json.RawMessage) []nodeengine.CompositionDiffResource {
|
||||
was, now := entriesByID(running), entriesByID(other)
|
||||
ids := map[string]bool{}
|
||||
for id := range was {
|
||||
ids[id] = true
|
||||
}
|
||||
for id := range now {
|
||||
ids[id] = true
|
||||
}
|
||||
out := []nodeengine.CompositionDiffResource{}
|
||||
for _, id := range slices.Sorted(maps.Keys(ids)) {
|
||||
w, n := was[id], now[id]
|
||||
names := map[string]bool{}
|
||||
for k := range w.values {
|
||||
names[k] = true
|
||||
}
|
||||
for k := range n.values {
|
||||
names[k] = true
|
||||
}
|
||||
var entries []nodeengine.CompositionDiffEntry
|
||||
for _, name := range slices.Sorted(maps.Keys(names)) {
|
||||
rv, rok := w.values[name]
|
||||
ov, ook := n.values[name]
|
||||
if rok == ook && rv == ov {
|
||||
continue
|
||||
}
|
||||
e := nodeengine.CompositionDiffEntry{Name: name}
|
||||
if w.secret[name] || n.secret[name] {
|
||||
e.Masked = true
|
||||
} else {
|
||||
e.Running, e.Other = rv, ov
|
||||
}
|
||||
entries = append(entries, e)
|
||||
}
|
||||
if len(entries) > 0 {
|
||||
out = append(out, nodeengine.CompositionDiffResource{Resource: id, Entries: entries})
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// resourceEntries are one resource's entries by name, as text, and which of them hold a secret.
|
||||
type resourceEntries struct {
|
||||
values map[string]string
|
||||
secret map[string]bool
|
||||
}
|
||||
|
||||
// secretFields are the fields of a resource the declaration keeps secret (declaration.File.Secret, its Secrets).
|
||||
var secretFields = []string{"sealed", "vault-sealed", "secrets"}
|
||||
|
||||
func entriesByID(resources []json.RawMessage) map[string]resourceEntries {
|
||||
out := map[string]resourceEntries{}
|
||||
for _, raw := range resources {
|
||||
var fields map[string]json.RawMessage
|
||||
if json.Unmarshal(raw, &fields) != nil {
|
||||
continue
|
||||
}
|
||||
var id string
|
||||
_ = json.Unmarshal(fields["id"], &id)
|
||||
r := resourceEntries{values: map[string]string{}, secret: map[string]bool{}}
|
||||
hasSecret := false
|
||||
for _, f := range secretFields {
|
||||
if _, ok := fields[f]; ok {
|
||||
hasSecret = true
|
||||
}
|
||||
}
|
||||
for name, value := range fields {
|
||||
if name == "env" {
|
||||
var env map[string]string
|
||||
if json.Unmarshal(value, &env) == nil {
|
||||
for k, v := range env {
|
||||
r.values["env."+k] = v
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
r.values[name] = entryText(value)
|
||||
if slices.Contains(secretFields, name) || (hasSecret && name == "content") {
|
||||
r.secret[name] = true
|
||||
}
|
||||
}
|
||||
out[id] = r
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// entryText is a field's value as the answer says it: a string as itself, anything else as its JSON.
|
||||
func entryText(value json.RawMessage) string {
|
||||
var s string
|
||||
if json.Unmarshal(value, &s) == nil {
|
||||
return s
|
||||
}
|
||||
return string(value)
|
||||
}
|
||||
|
||||
@@ -44,6 +44,9 @@ const (
|
||||
ActSetuidSearch = nodeengine.VerbSetuidSearch
|
||||
// ActComposition reads a module's composition beside what runs of it; read-only, no warrant (issue 774).
|
||||
ActComposition = nodeengine.VerbComposition
|
||||
// ActCompositionDiff reads a module's running composition beside another revision of it, by resource; read-only (design
|
||||
// 47 H2 §813 (3)).
|
||||
ActCompositionDiff = nodeengine.VerbCompositionDiff
|
||||
// ActCompose composes the node now, the recovery of a failed composition (issue 774).
|
||||
ActCompose = nodeengine.VerbCompose
|
||||
// ActPackages reads the packages this node's compositions declare, and with write writes its key again (novox/hq
|
||||
@@ -163,6 +166,9 @@ type HandActs struct {
|
||||
// Composition reads one module's composition beside what runs of it, read-only (the verb composition, issue 774):
|
||||
// the latest of its want's build or the one whose digest is given, and with diff the resources that differ.
|
||||
Composition func(module, digest string, diff bool) (any, error)
|
||||
// CompositionDiff answers the verb composition-diff: the entries that differ per resource between the module's running
|
||||
// composition and the revision or digest given (design 47 H2 §813 (3)).
|
||||
CompositionDiff func(module string, revision int64, digest string) (any, error)
|
||||
// Compose makes a pass of the node's composition now and says how it went; with module, its apply is also tried
|
||||
// again (the verb compose, issue 774).
|
||||
Compose func(module string) (any, error)
|
||||
@@ -248,6 +254,20 @@ func (h *HandActs) Call(c EngineCall) (any, error) {
|
||||
diff = b
|
||||
}
|
||||
return h.Composition(arg("module"), arg("digest"), diff)
|
||||
case ActCompositionDiff:
|
||||
if h.CompositionDiff == nil {
|
||||
return nil, errors.New("this engine reads no composition yet: its node agent has not started")
|
||||
}
|
||||
var revision int64
|
||||
switch r := c.Args["revision"].(type) {
|
||||
case float64:
|
||||
revision = int64(r)
|
||||
case int64:
|
||||
revision = r
|
||||
case int:
|
||||
revision = int64(r)
|
||||
}
|
||||
return h.CompositionDiff(arg("module"), revision, arg("digest"))
|
||||
case ActCompose:
|
||||
if h.Compose == nil {
|
||||
return nil, errors.New("this engine composes nothing on a call yet: its node agent has not started")
|
||||
|
||||
Reference in New Issue
Block a user