node-engine: at its first pass after an update, a process taken as running gets its unit written again with MESH_NODE (1984/AC16, AC17; H-A-711, H-A-713, H-A-714)

A module taken as running its want's composition at the start (the agent's TakenAtStart, now the named
takenAtStartIn the node agent hands it) has the unit of each process whose composed unit differs from its
applied record's written again, by one function (apply.ProcessWrote, which the apply builds its record with
too), and the record takes it, so a second pass finds nothing. Nothing is applied: no backup, no other
resource, the have unchanged. A held module, or one whose want names a new fingerprint, is never taken as
running and is not touched. A process whose record names another bundle, or none, is left as it is and said
(H-A-714); a failed units reload is said.

What restarts: services only. Run-once steps and scheduled processes' units are written, not run or started.
Brother's two processes (brother-device, brother-watch; neither run-once nor scheduled) are services: each
restarts once, if brother runs its want's composition.

Builds: design 47 G A-342, H-A-711, H-A-713, H-A-714
This commit is contained in:
2026-11-25 15:34:13 +01:00
parent c312e6a30e
commit 3c2e60dc1f
7 changed files with 253 additions and 9 deletions
+44 -5
View File
@@ -276,6 +276,49 @@ func awaitBackup(ctx context.Context, events <-chan []byte, node, module string,
}
}
// takenAtStartIn is what the agent does with a module taken as running at this start, with no apply: each record-held
// directory entry of it given its owner and mode where they drifted on disk (H-A-374, H-A-392), and the unit of each of
// its processes written again where it lacks its node's name (H-A-713), one line each.
func takenAtStartIn(opts options, say func(string)) func(module string, runs compositions.Composed) {
return func(module string, runs compositions.Composed) {
for _, line := range convergeDirectories(opts.state, runs) {
say(line)
}
for _, line := range convergeUnits(context.Background(), opts, runs) {
say(line)
}
}
}
// convergeUnits writes again the unit of each process of a module taken as running at this start whose unit as composed
// differs from its applied record's, and restarts a service of them (apply.ConvergeProcessUnits, H-A-713): the module's
// have is unchanged and nothing else of it is written, backed up or run. One line per process, and one when the
// composition or the state does not read.
func convergeUnits(ctx context.Context, opts options, runs compositions.Composed) []string {
d, err := declarationOf(runs)
if err != nil {
return []string{fmt.Sprintf("the units of %s are not judged at this start: its composition does not read: %v", runs.Module, err)}
}
applying.Lock()
defer applying.Unlock()
unlock, err := store.Lock(opts.state, nil)
if err != nil {
return []string{fmt.Sprintf("the units of %s are not judged at this start: %v", runs.Module, err)}
}
defer unlock()
known, err := store.Load(opts.state)
if err != nil {
return []string{fmt.Sprintf("the units of %s are not judged at this start: %v", runs.Module, err)}
}
now, lines := apply.ConvergeProcessUnits(ctx, d, known, opts.run())
if len(lines) > 0 {
if err := store.Save(opts.state, now); err != nil {
lines = append(lines, fmt.Sprintf("the record of the units of %s could not be saved: %v", runs.Module, err))
}
}
return lines
}
// convergeDirectories gives each directory entry of runs that this node's record holds, drifted on disk, its declared
// owner and mode (apply.ConvergeDirectories, H-A-374, H-A-392): one line per directory, and one when the record or the
// composition does not read.
@@ -586,11 +629,7 @@ func nodeAgent(ctx context.Context, opts options, signer ed25519.PublicKey, trus
// again (ace, 2026-10-25 21:17).
// A module taken as running at this start has each record-held directory entry of it given its owner and mode
// where they drifted on disk, one line each (H-A-374, H-A-392).
TakenAtStart: func(module string, runs compositions.Composed) {
for _, line := range convergeDirectories(opts.state, runs) {
say(line)
}
},
TakenAtStart: takenAtStartIn(opts, say),
DirectoryDrift: func(runs compositions.Composed) []nodeengine.CompositionAnswerDriftItem {
d, err := declarationOf(runs)
if err != nil {
+116
View File
@@ -1,10 +1,21 @@
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"encoding/json"
"errors"
"git.novox.be/novox/mesh-sdk/go/declaration"
"github.com/novox/mesh-host/internal/apply"
engcomp "github.com/novox/mesh-host/internal/compositions"
"github.com/novox/mesh-host/internal/system"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/novox/mesh-host/internal/liveness"
@@ -33,6 +44,110 @@ func testACompositionIsRecordedOnlyWhenEveryResourceIs(t *testing.T) {
}
}
// 1984/AC16 (H-A-713): a module taken as running at the start, whose processes a real apply recorded with units written
// without MESH_NODE, has those units written again with it: the service restarted, the run-once step written and not run,
// no other command (no backup is the agent's, never reached here), and nothing at the second pass. The record the
// comparison reads comes from a real apply (apply.Apply with no node, as the engine before the fix).
func testProcessUnitsWithoutTheNodesNameAreWrittenAgainAtTheStart(t *testing.T) {
var raw bytes.Buffer
zipped := gzip.NewWriter(&raw)
w := tar.NewWriter(zipped)
_ = w.WriteHeader(&tar.Header{Name: "main.js", Mode: 0o644, Size: 1, Typeflag: tar.TypeReg})
_, _ = w.Write([]byte("1"))
_ = w.Close()
_ = zipped.Close()
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) { _, _ = rw.Write(raw.Bytes()) }))
t.Cleanup(server.Close)
one := func(id, name, extra string) json.RawMessage {
return json.RawMessage(`{"id":"` + id + `","type":"process","name":"` + name + `","source":"` + server.URL + `/a.tgz","digest":"` +
declaration.DigestOf(raw.Bytes()) + `","run":["node","main.js"],"env":{"A":"1"}` + extra + `}`)
}
service, step := one("m.service", "m-service", ""), one("m.step", "m-step", `,"run-once":true`)
units := t.TempDir()
t.Cleanup(apply.ForTests(units, t.TempDir()))
var commands []string
run := func(_ context.Context, name string, args ...string) (string, error) {
commands = append(commands, name+" "+strings.Join(args, " "))
return "", nil
}
sys, err := system.For("arch")
if err != nil {
t.Fatal(err)
}
d, err := declaration.Parse([]byte(`{"declaration":1,"resources":[` + string(service) + `,` + string(step) + `]}`))
if err != nil {
t.Fatal(err)
}
_, applied, err := apply.Apply(context.Background(), sys, d, store.State{}, store.OriginDeclared, run, nil, nil)
if err != nil {
t.Fatal(err)
}
state := filepath.Join(t.TempDir(), "state.json")
if err := store.Save(state, applied); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(filepath.Join(units, "m-service.service")); strings.Contains(string(b), "MESH_NODE") {
t.Fatalf("the first apply, with no node, wrote the name:\n%s", b)
}
commands = nil
opts := options{state: state, out: &strings.Builder{}, runner: run}
c := engcomp.Composed{Node: "ace", Module: "m", Resources: []json.RawMessage{service, step}}
var said []string
takenAt := takenAtStartIn(opts, func(l string) { said = append(said, l) })
takenAt(c.Module, c)
if len(said) != 2 {
t.Fatalf("the first pass said %v; want one line per process", said)
}
for _, name := range []string{"m-service", "m-step"} {
if b, _ := os.ReadFile(filepath.Join(units, name+".service")); !strings.Contains(string(b), `Environment="MESH_NODE=ace"`) {
t.Errorf("%s was not written again with the node's name:\n%s", name, b)
}
}
if got := strings.Join(commands, "\n"); !strings.Contains(got, "systemctl restart m-service.service") ||
strings.Contains(got, "m-step") || strings.Contains(got, "pacman") {
t.Errorf("the first pass ran:\n%s; want the service restarted, the step not run, nothing else", got)
}
commands = nil
said = nil
takenAt(c.Module, c)
if len(said) != 0 || len(commands) != 0 {
t.Errorf("the second pass said %v and ran %v; want nothing", said, commands)
}
// 1984/AC16 (H-A-714): a process whose record names another bundle is left as it is, with a line naming both; and a
// units reload that fails is said, not skipped.
other := one("m.other", "m-other", `,"env":{"B":"2"}`)
var st store.State
st.Record(store.Applied{ID: "m.other", Type: "process", Target: "m-other", Wrote: "sha256:" + strings.Repeat("1", 64) + " unit"})
st.Record(store.Applied{ID: "m.service", Type: "process", Target: "m-service", Wrote: apply.ProcessWrote(mustProcess(t, service), "")})
if err := store.Save(state, st); err != nil {
t.Fatal(err)
}
say2 := []string{}
commands = nil
opts.runner = func(_ context.Context, name string, args ...string) (string, error) {
commands = append(commands, name+" "+strings.Join(args, " "))
return "", errors.New("no bus to the manager")
}
takenAtStartIn(opts, func(l string) { say2 = append(say2, l) })("m", engcomp.Composed{Node: "ace", Module: "m", Resources: []json.RawMessage{other, service}})
joined := strings.Join(say2, "\n")
if !strings.Contains(joined, "m-other") || !strings.Contains(joined, "sha256:"+strings.Repeat("1", 64)) || !strings.Contains(joined, declaration.DigestOf(raw.Bytes())) ||
!strings.Contains(joined, "units could not be reloaded") || !strings.Contains(joined, "m-service") {
t.Errorf("the lines were:\n%s", joined)
}
if b, _ := os.ReadFile(filepath.Join(units, "m-other.service")); strings.Contains(string(b), "MESH_NODE") {
t.Error("a process whose record names another bundle was written")
}
}
func mustProcess(t *testing.T, raw json.RawMessage) *declaration.Process {
t.Helper()
r, err := declaration.ParseResource(raw)
if err != nil {
t.Fatal(err)
}
return r.(*declaration.Process)
}
// The modules applied again are those with a missing resource, each once; one down but there is not among them.
func testTheModulesAppliedAgainAreThoseWithAMissingResource(t *testing.T) {
t.Run("1957: the machine shows a resource not active", testTheMachineShowsAResourceNotActive)
@@ -97,6 +212,7 @@ func testAFilledFileDoesNotMakeItsCompositionUnrecorded(t *testing.T) {
// TestRecorded runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
func TestRecorded(t *testing.T) {
t.Run("ACompositionIsRecordedOnlyWhenEveryResourceIs", testACompositionIsRecordedOnlyWhenEveryResourceIs)
t.Run("ProcessUnitsWithoutTheNodesNameAreWrittenAgainAtTheStart", testProcessUnitsWithoutTheNodesNameAreWrittenAgainAtTheStart)
t.Run("TheModulesAppliedAgainAreThoseWithAMissingResource", testTheModulesAppliedAgainAreThoseWithAMissingResource)
t.Run("OnlyANewlyMissingResourceAppliesItsModuleAgain", testOnlyANewlyMissingResourceAppliesItsModuleAgain)
t.Run("AFilledFileDoesNotMakeItsCompositionUnrecorded", testAFilledFileDoesNotMakeItsCompositionUnrecorded)