diff --git a/cmd/mesh-host/nodeagent.go b/cmd/mesh-host/nodeagent.go index f5a4c0a1..7e6e8eb8 100644 --- a/cmd/mesh-host/nodeagent.go +++ b/cmd/mesh-host/nodeagent.go @@ -253,6 +253,21 @@ func awaitBackup(ctx context.Context, events <-chan []byte, node, module string, } } +// 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. +func convergeDirectories(statePath string, runs compositions.Composed) []string { + d, err := declarationOf(runs) + if err != nil { + return []string{fmt.Sprintf("the directories of %s are not judged at this start: its composition does not read: %v", runs.Module, err)} + } + known, err := store.Load(statePath) + if err != nil { + return []string{fmt.Sprintf("the directories of %s are not judged at this start: %v", runs.Module, err)} + } + return apply.ConvergeDirectories(d, known) +} + // declarationOf is the declaration the compositions make together: their resources, and the node's own parts when the // node's composition is among them. It is what the node judges and says its health by, and what one module's apply is // made of (with a single composition). @@ -536,6 +551,28 @@ func nodeAgent(ctx context.Context, opts options, signer ed25519.PublicKey, trus Backup: backupNode(session), // What the filling refused before this apply may be named now: the provisioner's client is told to fill // 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) + } + }, + DirectoryDrift: func(runs compositions.Composed) []agent.DriftView { + d, err := declarationOf(runs) + if err != nil { + return nil + } + known, err := store.Load(opts.state) + if err != nil { + return nil + } + var out []agent.DriftView + for _, x := range apply.DirectoryDrifts(d, known) { + out = append(out, agent.DriftView{ID: x.ID, Path: x.Path, Drift: x.Drift}) + } + return out + }, Settled: func(module string) { knockFilling(module+" settled", say) judgeWhatRunsNow(opts.state, signer) diff --git a/internal/agent/agent.go b/internal/agent/agent.go index e4beb02a..cf450d61 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -185,6 +185,13 @@ type Env struct { // them. The node-engine's filling tells the provisioner's client then to fill again what it refused (mesh-host // internal/filling Knock; ace, 2026-10-25 21:17). Nil tells nothing. Settled func(module string) + // TakenAtStart is told, outside the agent's lock, each module taken as running at this start from its have or the + // kept declaration, with no apply, and the composition it runs: the node-engine converges the owner and mode of each + // record-held directory entry of it (H-A-374, H-A-392). Nil tells nothing. + TakenAtStart func(module string, runs compositions.Composed) + // DirectoryDrift reads, for the verb composition's diff, each directory entry of runs the mesh made whose owner or + // mode on disk differ (apply.DirectoryDrifts, H-A-392). Nil reads none. + DirectoryDrift func(runs compositions.Composed) []DriftView // RunsChanged is told, outside the agent's lock, that what runs (Everything) changed other than by an apply or a // removal: once at the start, when the haves and the compositions they name are read, and each time a module is // taken as running from its have or the kept declaration. Like Settled, the filling then tells the provisioner's @@ -1700,6 +1707,7 @@ func (a *Agent) baseline(ctx context.Context, module string, w wantNow, comp com a.running[module] = nodeengine.HaveByNodeModuleValue{Fp: h.FP, Composed: h.Composed, Phase: nodeengineclient.PhaseRunning} a.mu.Unlock() a.runsChanged(module + " was taken from its have") + a.takenAtStart(module, comp.C) // Said again, so the session holds the module and its key carries the health this engine judges, // not the one an older engine left there (xorg's unknown on g14 and shanks, 2026-10-25). a.writeHave(ctx, Have{Module: module, FP: h.FP, Composed: h.Composed, @@ -1729,6 +1737,7 @@ func (a *Agent) baseline(ctx context.Context, module string, w wantNow, comp com a.running[module] = nodeengine.HaveByNodeModuleValue{Fp: w.Want.Fp, Composed: w.Composed, Phase: nodeengineclient.PhaseRunning} a.mu.Unlock() a.runsChanged(module + " was taken from the kept declaration") + a.takenAtStart(module, comp.C) a.writeHave(ctx, Have{Module: module, FP: w.Want.Fp, Composed: w.Composed, Phase: string(nodeengineclient.PhaseRunning)}) return true } @@ -1876,6 +1885,13 @@ func (a *Agent) apply(ctx context.Context, module string, w wantNow, comp compNo a.catchUp(ctx, module, catchUps) } +// takenAtStart tells TakenAtStart of a module taken as running at this start (H-A-392). +func (a *Agent) takenAtStart(module string, runs compositions.Composed) { + if a.env.TakenAtStart != nil { + a.env.TakenAtStart(module, runs) + } +} + // runIsNewer says whether a want's run orders after the run a module was applied from (design 47 H2 §813 (1)): each is // the composition run's pipeline sequence in decimal. A want or an apply without one is not ordered by run, nor is a run // that is no decimal number. diff --git a/internal/agent/agent_test.go b/internal/agent/agent_test.go index 8c06ef1b..71eaf3b6 100644 --- a/internal/agent/agent_test.go +++ b/internal/agent/agent_test.go @@ -133,6 +133,8 @@ type bed struct { said []string // settled is each module Settled was told, and settledRan whether what runs held it then. settled []string + // takenAtStart is each module TakenAtStart was told, with its composition's resource count. + takenAtStart []string settledRan []bool ag *Agent // the agent run() runs env Env @@ -179,6 +181,11 @@ func newBed(t *testing.T) *bed { } return Backup{Done: true} }, + TakenAtStart: func(m string, runs compositions.Composed) { + b.mu.Lock() + defer b.mu.Unlock() + b.takenAtStart = append(b.takenAtStart, fmt.Sprintf("%s:%d", m, len(runs.Resources))) + }, Settled: func(m string) { ran := false if b.ag != nil { @@ -420,6 +427,11 @@ func TestStartingOnWhatTheKeptDeclarationHoldsAppliesNothing(t *testing.T) { if got := b.appliedList(); len(got) != 0 { t.Fatalf("a module that already runs what the want names was applied: %v", got) } + // H-A-392: a module taken as running at the start is told with the composition it runs, for its directories' drift. + if b.mu.Lock(); !slices.Equal(b.takenAtStart, []string{"web:2"}) { + t.Errorf("taken at the start were %v; want web with its two resources", b.takenAtStart) + } + b.mu.Unlock() t.Run("starting on its own have applies nothing", testStartingOnItsOwnHaveAppliesNothing) } @@ -1907,10 +1919,11 @@ func TestTheNodesOwnCompositionIsAppliedWhenItDiffersAndARecordMoveWaitsForAYes( b.haves.Write(context.Background(), Have{Module: "broken", FP: brokenFP, Composed: brokenDig, Phase: string(nodeengineclient.PhaseFailed), Detail: "apply: unit failed to start"}) want("broken", brokenFP, "") - eventually(t, "unread's want was not taken", func() bool { + // Every want taken before the mark, not unread's alone: fresh's taken after it was not marked (seen under -race). + eventually(t, "the wants were not taken", func() bool { b.ag.mu.Lock() defer b.ag.mu.Unlock() - return b.ag.wants["unread"].Present + return b.ag.wants["unread"].Present && b.ag.wants["fresh"].Present && b.ag.wants["broken"].Present }) b.ag.NotComposed(context.Background(), "a stand-in for web's password, again") if h := b.haves.last("unread"); h.Module != "" { @@ -1992,7 +2005,14 @@ func TestTheNodesOwnCompositionIsAppliedWhenItDiffersAndARecordMoveWaitsForAYes( // container it recreates; the revision that runs, asked by its digest, is no move (issue 774). keep(dk, draw) keep(dk, drawMoved) + // H-A-392: and with diff, each directory the mesh made of what runs whose mode or owner on disk drifted. + b.ag.env.DirectoryDrift = func(runs compositions.Composed) []DriftView { + return []DriftView{{ID: runs.Module + ".data", Path: "/srv/db", Drift: "mode 0700, 0711 declared"}} + } view, err := b.ag.Composition(context.Background(), "db", "", true) + if len(view.Drift) != 1 || view.Drift[0].ID != "db.data" { + t.Errorf("the diff of db names the drift %+v", view.Drift) + } if err != nil || view.Have == nil || view.Have.Phase != string(nodeengineclient.PhaseWaitingYes) || view.Composition.Digest != ddigMoved || view.Runs == nil || view.Runs.Composed != ddig || view.Judged.Judgement != string(nodeengineclient.JudgedRecreates) || !view.Judged.WaitsForYes || len(view.Diff) != 1 || view.Diff[0].ID != "db.server" || view.Diff[0].Change != "changed" || diff --git a/internal/agent/composition.go b/internal/agent/composition.go index e9017838..c39bb8bd 100644 --- a/internal/agent/composition.go +++ b/internal/agent/composition.go @@ -49,6 +49,10 @@ type CompositionView struct { Judged JudgedView `json:"judged"` // Diff is every resource that differs from what runs, when asked. Diff []judge.ResourceChange `json:"diff,omitempty"` + // Drift is, when the diff is asked, each directory entry of what runs that the mesh made, as its record holds, whose + // owner or mode on disk differ from it (H-A-374, H-A-392): read-only; the engine sets it right at its next start or + // apply of the module. + Drift []DriftView `json:"drift,omitempty"` // Own is, of the node's own composition (module node), its own fields beside what runs names (issue 774: ace's // own composition dropped the provisioner client's account, and nothing read could show it). Own *OwnView `json:"own,omitempty"` @@ -302,6 +306,9 @@ func (a *Agent) Composition(ctx context.Context, module, digest string, diff boo } if diff { view.Diff = judge.ChangedResources(running, read.C) + if running != nil && a.env.DirectoryDrift != nil { + view.Drift = a.env.DirectoryDrift(*running) + } } return view, nil } @@ -341,6 +348,14 @@ func (a *Agent) revisionsOf(ctx context.Context, key string) ([]compNow, error) return out, nil } +// DriftView is one directory entry the mesh made whose owner or mode on disk differ from it, as the verb composition's +// diff names it (H-A-392): its id, its path and how, in words. +type DriftView struct { + ID string `json:"id"` + Path string `json:"path"` + Drift string `json:"drift"` +} + // 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. diff --git a/internal/apply/converge_dirs.go b/internal/apply/converge_dirs.go new file mode 100644 index 00000000..c210a09f --- /dev/null +++ b/internal/apply/converge_dirs.go @@ -0,0 +1,111 @@ +package apply + +import ( + "errors" + "fmt" + "os" + + "git.novox.be/novox/mesh-sdk/go/declaration" + "github.com/novox/mesh-host/internal/store" +) + +// ConvergeDirectories gives each directory entry of d whose record known holds, found on disk with another owner or mode +// than declared, its declared owner and mode (design 47 H2's E lines, H-A-374, H-A-392): at the engine's start, so a +// drift lasts at most until then, and with no apply of an unchanged composition. One the mesh never made (its record used +// as found, issue 339) is left as found and said. Answered as one line per directory set right or left; a directory +// declared that is missing, that has no record, or that is as declared, says nothing. +func ConvergeDirectories(d *declaration.Declaration, known store.State) []string { + if d == nil { + return nil + } + var lines []string + for _, r := range d.Resources { + dir, ok := r.(*declaration.Directory) + if !ok { + continue + } + rec, held := known.Find(dir.ID) + if !held || rec.Type != string(declaration.TypeDirectory) { + continue + } + drift, err := directoryDrift(dir) + switch { + case errors.Is(err, os.ErrNotExist) || (err == nil && drift == ""): + continue + case err != nil: + lines = append(lines, fmt.Sprintf("%s (%s) cannot be judged at this start: %v", dir.Path, dir.ID, err)) + continue + } + out, err := applyDirectory(dir, rec, true) + switch { + case err != nil: + lines = append(lines, fmt.Sprintf("%s (%s) is %s and cannot be set right at this start: %v", dir.Path, dir.ID, drift, err)) + case out.asFound: + lines = append(lines, fmt.Sprintf("%s (%s) is %s, and is left as found at this start: %s", dir.Path, dir.ID, drift, out.Detail)) + default: + lines = append(lines, fmt.Sprintf("%s (%s) was %s, and is set right at this start", dir.Path, dir.ID, drift)) + } + } + return lines +} + +// DirectoryDrift is one directory entry the mesh made, as its record holds, whose owner or mode on disk differ from +// what is declared (H-A-392): its id, its path and how it differs, in words. +type DirectoryDrift struct { + ID string `json:"id"` + Path string `json:"path"` + Drift string `json:"drift"` +} + +// DirectoryDrifts is each directory entry of d the mesh made, by its record in known (not used as found), whose owner or +// mode on disk differ from its entry's (H-A-392): read-only, what the verb composition's diff names as drift. A directory +// that is missing, has no record, or cannot be read says nothing here. +func DirectoryDrifts(d *declaration.Declaration, known store.State) []DirectoryDrift { + if d == nil { + return nil + } + var out []DirectoryDrift + for _, r := range d.Resources { + dir, ok := r.(*declaration.Directory) + if !ok { + continue + } + if rec, held := known.Find(dir.ID); !held || rec.Type != string(declaration.TypeDirectory) || rec.AsFound { + continue + } + if drift, err := directoryDrift(dir); err == nil && drift != "" { + out = append(out, DirectoryDrift{ID: dir.ID, Path: dir.Path, Drift: drift}) + } + } + return out +} + +// directoryDrift says how a directory on disk differs from its entry's owner and mode, in words; empty when it does not. +func directoryDrift(dir *declaration.Directory) (string, error) { + mode, err := dirModeOf(dir.Mode, 0o755) + if err != nil { + return "", err + } + info, err := os.Lstat(dir.Path) + if err != nil { + return "", err + } + if !info.IsDir() { + return "", fmt.Errorf("%s is not a directory", dir.Path) + } + owned, err := ownedBy(dir.Path, dir.Owner) + if err != nil { + return "", err + } + var drift string + if dirBits(info.Mode()) != dirBits(mode) { + drift = fmt.Sprintf("mode %s, %s declared", modeText(dirBits(info.Mode())), modeText(mode)) + } + if !owned { + if drift != "" { + drift += ", and " + } + drift += fmt.Sprintf("owned by %s, %s declared", ownerText(info), dir.Owner) + } + return drift, nil +} diff --git a/internal/apply/mesh_tree_found_test.go b/internal/apply/mesh_tree_found_test.go index a3603a0a..134403a2 100644 --- a/internal/apply/mesh_tree_found_test.go +++ b/internal/apply/mesh_tree_found_test.go @@ -47,21 +47,49 @@ func TestADirectoryInTheMeshsOwnTreeMadeByTheEngineIsTheMeshs(t *testing.T) { if info, _ := os.Stat(made); info.Mode().Perm() != 0o750 { t.Errorf("it was not given its declared mode: %04o", info.Mode().Perm()) } + // H-A-374, H-A-392: a record-held directory whose mode drifted on disk is set right at the engine's start, with one + // line, though its composition is unchanged; as declared, it says nothing. + if err := os.Chmod(made, 0o700); err != nil { + t.Fatal(err) + } + postgres := declare(t, `{"id":"postgres.data","type":"directory","path":"`+made+`","mode":"0750"}`) + if drifts := DirectoryDrifts(postgres, state); len(drifts) != 1 || drifts[0].Drift != "mode 0700, 0750 declared" { + t.Errorf("the drift read is %+v", drifts) + } + if lines := ConvergeDirectories(postgres, state); len(lines) != 1 || !strings.Contains(lines[0], "was mode 0700, 0750 declared, and is set right") { + t.Errorf("a drifted directory the mesh made was converged as %q", lines) + } + if info, _ := os.Stat(made); info.Mode().Perm() != 0o750 { + t.Errorf("it was not set right at the start: %04o", info.Mode().Perm()) + } + if lines := ConvergeDirectories(postgres, state); len(lines) != 0 { + t.Errorf("a directory as declared was said at the start: %q", lines) + } - // Outside the mesh's tree the rule of issue 339 stands: found, used as found. + // Outside the mesh's tree the rule of issue 339 stands: found, used as found; and left so at a start, said. other := filepath.Join(root, "var-lib", "notes") if err := os.MkdirAll(other, 0o700); err != nil { t.Fatal(err) } said = nil decl = `{"id":"notes.data","type":"directory","path":"` + other + `","mode":"0750"}` - if _, _, err := Apply(context.Background(), archHost(t), declare(t, decl), store.State{}, store.OriginDeclared, - noServices, func(line string) { said = append(said, line) }, nil); err != nil { + _, found, err := Apply(context.Background(), archHost(t), declare(t, decl), store.State{}, store.OriginDeclared, + noServices, func(line string) { said = append(said, line) }, nil) + if err != nil { t.Fatal(err) } if !strings.Contains(strings.Join(said, "\n"), "WARNING notes.data") { t.Errorf("a directory found outside the mesh's tree was not used as found: %q", said) } + if lines := ConvergeDirectories(declare(t, decl), found); len(lines) != 1 || !strings.Contains(lines[0], "left as found") { + t.Errorf("a directory the mesh never made was converged as %q", lines) + } + if drifts := DirectoryDrifts(declare(t, decl), found); len(drifts) != 0 { + t.Errorf("a directory the mesh never made is named as drift: %+v", drifts) + } + if info, _ := os.Stat(other); info.Mode().Perm() != 0o700 { + t.Errorf("a directory the mesh never made was changed at the start: %04o", info.Mode().Perm()) + } } // Review of issue 1873's fix: the tree is judged by where the path leads, not by its text. A root-only link inside the