diff --git a/cmd/mesh-host/engineseat.go b/cmd/mesh-host/engineseat.go index 81914329..b806028b 100644 --- a/cmd/mesh-host/engineseat.go +++ b/cmd/mesh-host/engineseat.go @@ -319,6 +319,20 @@ func compositionFrom(current *agentNow, withheld func(ctx context.Context, modul } } +// compositionDiffFrom answers the verb composition-diff from the node agent that runs now (design 47 H2 §813 (3)); refused in +// words while none does. +func compositionDiffFrom(current *agentNow) func(module string, revision int64, digest string) (any, error) { + return func(module string, revision int64, digest string) (any, error) { + ag := current.get() + if ag == nil { + return nil, errors.New("this engine's node agent has not started, so it holds no composition yet") + } + ctx, cancel := context.WithTimeout(context.Background(), compositionReadFor) + defer cancel() + return ag.CompositionDiff(ctx, module, revision, digest) + } +} + // withheldPartsOf is, of the composer that runs now, each part its last pass kept for module because its read did not answer. func withheldPartsOf(composer *atomic.Pointer[selfcompose.Composer]) func(module string) map[string]string { return func(module string) map[string]string { diff --git a/cmd/mesh-host/main.go b/cmd/mesh-host/main.go index 0651e1e5..2ce4db11 100644 --- a/cmd/mesh-host/main.go +++ b/cmd/mesh-host/main.go @@ -1028,6 +1028,7 @@ func runLink(ctx context.Context, opts options) error { go reconcileActs(ctx, queue.Acts) current := &agentNow{} queue.Acts.Composition = compositionFrom(current, queue.WithheldOf, withheldPartsOf(&composerNow)) + queue.Acts.CompositionDiff = compositionDiffFrom(current) queue.Acts.Compose = composeFrom(current, &composerNow) queue.Acts.ApplyOrder = applyOrderFrom(current) queue.Acts.Packages = packagesFrom(mine.Node, queue, &composerNow) diff --git a/internal/agent/agent_test.go b/internal/agent/agent_test.go index 3f12b797..77990ac5 100644 --- a/internal/agent/agent_test.go +++ b/internal/agent/agent_test.go @@ -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", diff --git a/internal/agent/composition.go b/internal/agent/composition.go index b80a2891..e9017838 100644 --- a/internal/agent/composition.go +++ b/internal/agent/composition.go @@ -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.`, 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) +} diff --git a/internal/link/handacts.go b/internal/link/handacts.go index d902eb3f..cbea5eb1 100644 --- a/internal/link/handacts.go +++ b/internal/link/handacts.go @@ -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")