diff --git a/internal/compose/composed.go b/internal/compose/composed.go index 6866fa9a..f8f6d567 100644 --- a/internal/compose/composed.go +++ b/internal/compose/composed.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "slices" "sort" "strings" @@ -210,6 +211,13 @@ func compositions(in Input, node string, fps map[string]string, mode Mode, appli if err := json.Unmarshal(body, &envelope); err != nil { return nil, nil, nil, nil, err } + // What the tool runner's process runs as, whose slice of each held module the composed process keeps (H-A-306). + var runsRuntime map[string]string + for i := range reading.Nodes { + if reading.Nodes[i].Name == node { + runsRuntime = reading.Nodes[i].RunsRuntime + } + } var kept []json.RawMessage keptPaths := map[string]bool{} foldedPaths := map[string]bool{} @@ -290,6 +298,27 @@ func compositions(in Input, node string, fps map[string]string, mode Mode, appli c.Filled = append(c.Filled, ownFilled...) out[own] = c } + if len(held) > 0 && runsRuntime != nil { + // A held module's slice of the runtime process is what runs of it, verbatim, its fingerprint included: a pass that + // holds it for a part withheld composes the runtime as it runs, and writes no new revision for it (design 47 H, + // H-A-303, H-A-306). Taken after Split, which gives each module's words the fingerprint of its want (§519). + for key, c := range out { + for i, raw := range c.Resources { + var head struct { + ID string `json:"id"` + Module string `json:"module"` + Type string `json:"type"` + } + if json.Unmarshal(raw, &head) != nil || runtimeOwner(head.ID, head.Module, head.Type) == "" { + continue + } + if c.Resources[i], err = keepHeldSlices(raw, runsRuntime, held); err != nil { + return nil, nil, nil, nil, err + } + } + out[key] = c + } + } for key, c := range out { if held[c.Module] { delete(out, key) // its stub's: the node keeps what it last applied of it (Node.Held) @@ -409,6 +438,120 @@ func ToolsRunning(running []engcomp.Composed) map[string][]catalogue.Bundle { return out } +// RuntimeRunning is the environment of the tool runner's process as the node runs it (`.runtime`, +// resolutionRuntimeProcess), or nil when running holds none (Node.RunsRuntime). +func RuntimeRunning(running []engcomp.Composed) map[string]string { + for _, c := range running { + for _, raw := range c.Resources { + var r struct { + ID string `json:"id"` + Module string `json:"module"` + Type string `json:"type"` + Env map[string]string `json:"env"` + } + if json.Unmarshal(raw, &r) == nil && runtimeOwner(r.ID, r.Module, r.Type) != "" && r.Env != nil { + return r.Env + } + } + } + return nil +} + +// runtimeOwner is the module of the tool runner's process a resource is (`.runtime`), or "" when it is none. +func runtimeOwner(id, module, kind string) string { + if kind != "process" { + return "" + } + if module == "" { + module, _ = declaration.ModuleGuess(id) + } + if module == "" || id != module+"."+resolution.RuntimeProcessID() { + return "" + } + return module +} + +// keepHeldSlices is the runtime process raw with each held module's slice taken verbatim from what runs (runs, the +// process's environment as it runs): its entries in catalogue.RuntimeToolModules and its words in +// catalogue.RuntimeToolEnv, its fingerprint among them (H-A-306). A held module the running process serves nothing of +// and gives no words keeps what the composition made of it (runningBundles: a drop already applied is undone). raw is +// answered unchanged when nothing differs. +func keepHeldSlices(raw json.RawMessage, runs map[string]string, held map[string]bool) (json.RawMessage, error) { + var fields map[string]json.RawMessage + if err := json.Unmarshal(raw, &fields); err != nil { + return nil, err + } + var env map[string]string + if json.Unmarshal(fields["env"], &env) != nil || env == nil { + return raw, nil + } + ranServed := map[string][]string{} + for _, entry := range servedEntries(runs[catalogue.RuntimeToolModules]) { + module, _, _ := strings.Cut(entry, "=") + ranServed[module] = append(ranServed[module], entry) + } + var ranWords, words map[string]json.RawMessage + _ = json.Unmarshal([]byte(runs[catalogue.RuntimeToolEnv]), &ranWords) + _ = json.Unmarshal([]byte(env[catalogue.RuntimeToolEnv]), &words) + keep := map[string]bool{} + for module, isHeld := range held { + if _, says := ranWords[module]; isHeld && (len(ranServed[module]) > 0 || says) { + keep[module] = true + } + } + if len(keep) == 0 { + return raw, nil + } + var served []string + for _, entry := range servedEntries(env[catalogue.RuntimeToolModules]) { + if module, _, _ := strings.Cut(entry, "="); !keep[module] { + served = append(served, entry) + } + } + for module := range keep { + served = append(served, ranServed[module]...) + if w, says := ranWords[module]; says { + if words == nil { + words = map[string]json.RawMessage{} + } + words[module] = w + } else { + delete(words, module) + } + } + sort.Strings(served) + kept := make(map[string]string, len(env)) + for k, v := range env { + kept[k] = v + } + kept[catalogue.RuntimeToolModules] = strings.Join(served, ",") + delete(kept, catalogue.RuntimeToolEnv) + if len(words) > 0 { + body, err := json.Marshal(words) + if err != nil { + return nil, err + } + kept[catalogue.RuntimeToolEnv] = string(body) + } + if maps.Equal(kept, env) { + return raw, nil + } + body, err := json.Marshal(kept) + if err != nil { + return nil, err + } + fields["env"] = body + return json.Marshal(fields) +} + +// servedEntries is the entries of a runtime's catalogue.RuntimeToolModules, `=` each; none for "". +func servedEntries(s string) []string { + if s == "" { + return nil + } + return strings.Split(s, ",") +} + // runningBundles is the bundles of a module composed as what runs of it (Node.RunsTools): each that runs, with what the // runtime loads of it and its words; one the runtime loads nothing of takes its loads and words from the module's build // (man: its built bundles, else its build's artifacts, catalogue.BundleLoads), so a drop already applied is undone. diff --git a/internal/compose/composed_test.go b/internal/compose/composed_test.go index 71eaf9b0..d28fb74d 100644 --- a/internal/compose/composed_test.go +++ b/internal/compose/composed_test.go @@ -6,6 +6,7 @@ import ( engcomp "github.com/novox/mesh-host/internal/compositions" "slices" "sort" + "strconv" "strings" "testing" @@ -133,39 +134,62 @@ func TestCompositionsAreTheDeclarationSplitByModule(t *testing.T) { t.Fatal(err) } served := "lamp=" + resolution.BundlePath("lamp", "tools") + "/lamp-tools" - runtimeRuns := func(dropped bool) engcomp.Composed { + runtimeRuns := func(dropped bool, ran string) engcomp.Composed { env := `"MESH_TOOL_MODULES":"` + served + `","MESH_TOOL_ENV":"{\"lamp\":{\"LAMP_CONF\":\"/var/lib/lamp/conf\"}}"` if dropped { env = `"MESH_TOOL_MODULES":""` // the drop already applied (shanks, 2026-10-29) } + if ran != "" { + env = ran + } return engcomp.Composed{Node: "anchor", Module: "node-tools", Resources: []json.RawMessage{json.RawMessage( `{"id":"node-tools.runtime","module":"node-tools","type":"process","name":"node-tools","env":{` + env + `}}`)}} } lampRuns := `{"id":"lamp.bundle-tools","type":"archive","source":"` + catalogue.ArtifactStoreScheme + `lamp/tools/blobs/` + dg('b') + `","digest":"` + dg('b') + `","path":"` + resolution.BundlePath("lamp", "tools") + `"}` + // H-A-303, H-A-306: what runs of lamp in the runtime, its fingerprint and the paths it owns among its words, at a load + // its new build no longer names. + ranServed := "lamp=" + resolution.BundlePath("lamp", "tools") + "/lamp-tools-old" + ranWords := `{"lamp":{"LAMP_CONF":"/var/lib/lamp/conf","MESH_FINGERPRINT":"` + fpFor('0') + `","MESH_OWNED_PATHS":"/var/lib/lamp"}}` + ranEnv := `"MESH_TOOL_MODULES":"` + ranServed + `","MESH_TOOL_ENV":` + strconv.Quote(ranWords) + held := func(n *Node) engcomp.Composed { + n.Held = map[string]bool{"lamp": true} + lr := engcomp.Composed{Node: "anchor", Module: "lamp", Resources: []json.RawMessage{json.RawMessage( + strings.Replace(lampRuns, `"type"`, `"module":"lamp","type"`, 1))}} + n.Kept = map[string]catalogue.Manifest{"lamp": Carried(newLamp, "lamp", &lr)} + return lr + } for _, row := range []struct { name string set func(n *Node) engcomp.Composed // and what runs of lamp fps map[string]string dropped bool + ran string // the runtime's env as it runs, when not served's + // What the runtime is composed to serve of lamp, and its words, when not served and its manifest's words. + wantServed, wantWords string }{ {"folded at a build of the old form", func(n *Node) engcomp.Composed { n.InOwn = map[string]bool{"lamp": true} n.RunsOwn = &engcomp.Composed{Node: "anchor", Resources: []json.RawMessage{json.RawMessage(lampRuns)}} return *n.RunsOwn - }, map[string]string{}, false}, + }, map[string]string{}, false, "", "", ""}, {"folded, the runtime already dropped it", func(n *Node) engcomp.Composed { n.InOwn = map[string]bool{"lamp": true} n.RunsOwn = &engcomp.Composed{Node: "anchor", Resources: []json.RawMessage{json.RawMessage(lampRuns)}} return *n.RunsOwn - }, map[string]string{}, true}, - {"held, its new build not delivered", func(n *Node) engcomp.Composed { - n.Held = map[string]bool{"lamp": true} - lr := engcomp.Composed{Node: "anchor", Module: "lamp", Resources: []json.RawMessage{json.RawMessage( + }, map[string]string{}, true, "", "", ""}, + // H-A-306: a held module's slice is what runs of it, verbatim: no fingerprint of its want where none runs. + {"held, its new build not delivered", held, map[string]string{"lamp": fpFor('1')}, false, "", + served, `{"lamp":{"LAMP_CONF":"/var/lib/lamp/conf"}}`}, + // H-A-303, H-A-306: held for a part withheld, its load, its fingerprint and its files words are what runs. + {"held for a part withheld, its slice as it runs", held, map[string]string{"lamp": fpFor('1')}, false, ranEnv, + ranServed, ranWords}, + // H-A-306 (3): a module whose parts answered and changed is composed from its build, not from what runs. + {"read, its build changed", func(n *Node) engcomp.Composed { + n.Kept = map[string]catalogue.Manifest{"lamp": newLamp} + return engcomp.Composed{Node: "anchor", Module: "lamp", Resources: []json.RawMessage{json.RawMessage( strings.Replace(lampRuns, `"type"`, `"module":"lamp","type"`, 1))}} - n.Kept = map[string]catalogue.Manifest{"lamp": Carried(newLamp, "lamp", &lr)} - return lr - }, map[string]string{"lamp": fpFor('1')}, false}, + }, map[string]string{"lamp": fpFor('1')}, false, ranEnv, "", ""}, } { tools := aMesh(t) tools.Catalogue["node-tools"], tools.Catalogue["artifacts"], tools.Catalogue["lamp"] = runtime, store, lamp @@ -173,32 +197,47 @@ func TestCompositionsAreTheDeclarationSplitByModule(t *testing.T) { anchor.Assigned = append(anchor.Assigned, "artifacts", "node-tools", "lamp") tools.BusMinted = []string{"anchor." + "node-tools"} anchor.SealingKey = keyOf(t, "anchor") - anchor.RunsTools = ToolsRunning([]engcomp.Composed{runtimeRuns(row.dropped), row.set(anchor)}) + ran := []engcomp.Composed{runtimeRuns(row.dropped, row.ran), row.set(anchor)} + anchor.RunsTools, anchor.RunsRuntime = ToolsRunning(ran), RuntimeRunning(ran) row.fps[overlay.Name], row.fps["artifacts"], row.fps["node-tools"] = fpFor('a'), fpFor('5'), fpFor('e') - out, _, _, err := CompositionsIn(tools, "anchor", row.fps, Foreseeing) - if err != nil { - t.Fatalf("%s: %v", row.name, err) - } + var digests []string var env map[string]any - for _, c := range out { - for _, raw := range c.Resources { - var r struct { - ID string `json:"id"` - Env map[string]any `json:"env"` - } - if json.Unmarshal(raw, &r) == nil && r.ID == "node-tools.runtime" { - env = r.Env + for range 2 { // two passes over the same input compose the same runtime (H-A-303) + out, _, _, err := CompositionsIn(tools, "anchor", row.fps, Foreseeing) + if err != nil { + t.Fatalf("%s: %v", row.name, err) + } + for _, c := range out { + for _, raw := range c.Resources { + var r struct { + ID string `json:"id"` + Env map[string]any `json:"env"` + } + if json.Unmarshal(raw, &r) == nil && r.ID == "node-tools.runtime" { + env = r.Env + dig, err := engcomp.DigestOf(c) + if err != nil { + t.Fatal(err) + } + digests = append(digests, dig) + } } } } + if len(digests) != 2 || digests[0] != digests[1] { + t.Errorf("%s: two passes composed the runtime at %v", row.name, digests) + } // §519: the words each bundle's module is given hold the fingerprint of the want it applies, where there is one. - words := `{"lamp":{"LAMP_CONF":"/var/lib/lamp/conf"}}` + wantServed, words := served, `{"lamp":{"LAMP_CONF":"/var/lib/lamp/conf"}}` if fp := row.fps["lamp"]; fp != "" { words = `{"lamp":{"LAMP_CONF":"/var/lib/lamp/conf","MESH_FINGERPRINT":"` + fp + `"}}` } - if env[catalogue.RuntimeToolModules] != served || env[catalogue.RuntimeToolEnv] != words { - t.Errorf("%s: the runtime serves %v with %v, want what runs of lamp, %s", row.name, - env[catalogue.RuntimeToolModules], env[catalogue.RuntimeToolEnv], served) + if row.wantServed != "" { + wantServed, words = row.wantServed, row.wantWords + } + if env[catalogue.RuntimeToolModules] != wantServed || env[catalogue.RuntimeToolEnv] != words { + t.Errorf("%s: the runtime serves %v with %v, want %s with %s", row.name, + env[catalogue.RuntimeToolModules], env[catalogue.RuntimeToolEnv], wantServed, words) } } } diff --git a/internal/compose/frombus/frombus.go b/internal/compose/frombus/frombus.go index b7240a0a..316fcad2 100644 --- a/internal/compose/frombus/frombus.go +++ b/internal/compose/frombus/frombus.go @@ -475,6 +475,8 @@ func readBase(ctx context.Context, r Reader, mesh Mesh, s Scope) (base, error) { if node == s.Node { // What its runtime serves as it runs: what a module composed as what runs of it contributes there (issue 1126). n.RunsTools = compose.ToolsRunning(s.Running) + // And the runtime's process as it runs, whose slice of each held module is kept verbatim (H-A-306). + n.RunsRuntime = compose.RuntimeRunning(s.Running) for _, module := range s.InOwn { if n.InOwn == nil { n.InOwn = map[string]bool{} diff --git a/internal/compose/input.go b/internal/compose/input.go index d4dd4a07..d185e399 100644 --- a/internal/compose/input.go +++ b/internal/compose/input.go @@ -273,6 +273,11 @@ type Node struct { // bundles, and a held module's new build's bundles are not delivered (issue 1126, 2026-10-29: shanks's runtime lost // the folded systemd-resolved, novox's the folded nats). RunsTools map[string][]catalogue.Bundle + // RunsRuntime is the environment of the tool runner's process as it runs (RuntimeRunning of what the node runs); nil + // when none runs. A held module's slice of the runtime process (its entries in catalogue.RuntimeToolModules, its words + // in catalogue.RuntimeToolEnv, its fingerprint among them) is copied from it verbatim, so a pass that holds a module + // for a part withheld composes the runtime as it runs (design 47 H, H-A-303, H-A-306). + RunsRuntime map[string]string } // ComposedHow is how a node-engine composes its node from an Input (Input.ComposedHow). diff --git a/internal/link/movesecret_test.go b/internal/link/movesecret_test.go index ca6385ce..f5121f04 100644 --- a/internal/link/movesecret_test.go +++ b/internal/link/movesecret_test.go @@ -161,7 +161,18 @@ func TestNatsAMoveIsAskedOfTheNodeAndTheVaultIsAskedInItsName(t *testing.T) { if err := conn.Flush(); err != nil { t.Fatal(err) } - if got, unknown := q.WithheldOf(t.Context(), "shell"); unknown != WithheldUnknown || got != nil { - t.Errorf("with the grants part down, withheld = %+v (%q)", got, unknown) + // H-A-303 (3), H-A-306: the part withheld and the holder asked are said in the read, and once in the journal. + var journal []string + sayWas := q.Say + q.Say = func(s string) { journal = append(journal, s) } + defer func() { q.Say = sayWas }() + for range 2 { + if got, unknown := q.WithheldOf(t.Context(), "shell"); !strings.HasPrefix(unknown, WithheldUnknown) || + !strings.Contains(unknown, "the seat mesh-grants's verb withheld") || got != nil { + t.Errorf("with the grants part down, withheld = %+v (%q)", got, unknown) + } + } + if len(journal) != 1 || !strings.Contains(journal[0], "the seat mesh-grants's verb withheld") { + t.Errorf("the grants part withheld twice is said %q, want once naming the seat asked", journal) } } diff --git a/internal/link/session.go b/internal/link/session.go index 8e687287..99c55e35 100644 --- a/internal/link/session.go +++ b/internal/link/session.go @@ -30,6 +30,10 @@ type Session struct { // Own is the module that is this node-engine (§527, §634): its discovery, finding writer and vault login names read it. // Nil names none, as a test's session does. Own *OwnName + // withheldSaid says the grants part's withheld not answering was said in the journal, until it answers (WithheldOf); + // guarded by withheldMu. + withheldMu sync.Mutex + withheldSaid bool // haveSay guards haveSaidFn and haveGoneFn (SetHaveSaid). haveSay sync.Mutex haveSaidFn func(module string, h nodeengine.HaveByNodeModuleValue) diff --git a/internal/link/withheld.go b/internal/link/withheld.go index f0d005b7..70427a96 100644 --- a/internal/link/withheld.go +++ b/internal/link/withheld.go @@ -11,9 +11,13 @@ import ( // GrantsSeat is the seat of the grants part, whose verb withheld names the marked subjects withheld from a node's runtime. const GrantsSeat = "mesh-grants" -// WithheldUnknown is what a composition says when the grants part does not answer withheld (design 46 approval mark §6). +// WithheldUnknown is what a composition says when the grants part does not answer withheld (design 46 approval mark §6), +// followed by the holder it asked and why (design 47 H, H-A-303 (3), H-A-306). const WithheldUnknown = "withheld unknown: the grants part does not answer" +// withheldAsked names the holder the composition read asks what is withheld: the seat and its verb. +const withheldAsked = "the seat " + GrantsSeat + "'s verb withheld" + // WithheldSubject is one marked subject withheld from this node's runtime, and why. type WithheldSubject struct { Subject string `json:"subject"` @@ -21,12 +25,14 @@ type WithheldSubject struct { } // WithheldOf is what the grants part withholds of module on this node, asked through its verb withheld: the subjects, or, -// when the verb does not answer or answers what is not its answer, WithheldUnknown as unknown. +// when the verb does not answer (not answered, cut by its bound, no responders) or answers what is not its answer, +// WithheldUnknown as unknown, naming the holder asked and why. That the part is withheld is said once in the journal +// (Say), until it answers again (design 47 H, H-A-303 (3), H-A-306). func (q *Session) WithheldOf(ctx context.Context, module string) (subjects []WithheldSubject, unknown string) { raw, err := q.askCalled(ctx, GrantsSeat, "withheld", map[string]string{"node": q.Membership.Node}, VaultWithin, "the grants part's withheld") if err != nil { - return nil, WithheldUnknown + return nil, q.withheldUnknown("asked of " + withheldAsked + ", which did not answer: " + err.Error()) } var answer struct { Node string `json:"node"` @@ -36,8 +42,11 @@ func (q *Session) WithheldOf(ctx context.Context, module string) (subjects []Wit } `json:"modules"` } if json.Unmarshal(raw, &answer) != nil || answer.Node != q.Membership.Node { - return nil, WithheldUnknown + return nil, q.withheldUnknown("asked of " + withheldAsked + ", which answered what is not its answer for " + q.Membership.Node) } + q.withheldMu.Lock() + q.withheldSaid = false + q.withheldMu.Unlock() for _, m := range answer.Modules { if m.Module == module { subjects = append(subjects, m.Subjects...) @@ -45,3 +54,16 @@ func (q *Session) WithheldOf(ctx context.Context, module string) (subjects []Wit } return subjects, "" } + +// withheldUnknown is WithheldUnknown with why, said once in the journal until the grants part answers again. +func (q *Session) withheldUnknown(why string) string { + unknown := WithheldUnknown + " (" + why + ")" + q.withheldMu.Lock() + said := q.withheldSaid + q.withheldSaid = true + q.withheldMu.Unlock() + if !said && q.Say != nil { + q.Say("the grants part is withheld from this node's composition read, which says " + unknown + " until it answers") + } + return unknown +}