E4: apply a want again only from a newer run

One writer per composition key: the holder of mesh-wants writes run, the
composition run's pipeline sequence, on the want; the node-engine keeps
the run each module was applied from, applies again only a want whose run
is newer, and names the run in its journal.

Builds: design 47 H2 §813 (1); H-A-355
This commit is contained in:
2026-11-21 23:22:30 +01:00
parent 53d5c8b20f
commit 84fc34398a
2 changed files with 64 additions and 2 deletions
+34 -1
View File
@@ -23,6 +23,7 @@ import (
"github.com/novox/mesh-host/internal/compositions"
"slices"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
@@ -249,6 +250,9 @@ type Agent struct {
// applied is the composition each module was last applied from: a revision a want named from the key's history is
// not the key's latest value, and what it holds is still held out of the others' applies.
applied map[string]compNow
// appliedRun is, of each module applied from a want that carried run, that run: the composition run's pipeline sequence
// in decimal (design 47 H2 §813 (1), H-A-355). A want whose run is not newer is not applied again.
appliedRun map[string]string
// had is, for each module whose have names a composition the bucket keeps (its latest value or a revision), that
// composition, read once at the start (loadHad): what runs of a module the agent has not taken yet, ahead of the kept
// declaration (issue 924).
@@ -341,7 +345,7 @@ func New(env Env) *Agent {
if env.Say == nil {
env.Say = func(string) {}
}
return &Agent{env: env, wants: map[string]wantNow{}, comps: map[string]compNow{}, running: map[string]nodeengine.HaveByNodeModuleValue{}, applied: map[string]compNow{}, had: map[string]compNow{}, keptData: map[string]bool{},
return &Agent{env: env, wants: map[string]wantNow{}, comps: map[string]compNow{}, running: map[string]nodeengine.HaveByNodeModuleValue{}, applied: map[string]compNow{}, appliedRun: map[string]string{}, had: map[string]compNow{}, keptData: map[string]bool{},
attempt: map[string]attempt{}, checked: map[string]bool{}, prior: map[string]bool{}, said: map[string]string{}, waited: map[string]string{}, notComposed: map[string]notComposedMark{},
pulls: map[string]*pullWait{}, settledAt: map[string]time.Time{}, settledSeq: map[string]int{},
retry: make(chan []string, 8), reconcile: make(chan []string, 64), failedAgain: make(chan string, 8), tries: map[string]int{}, again: make(chan againAsk, 64),
@@ -1544,6 +1548,13 @@ func (a *Agent) settleOne(ctx context.Context, module string) {
a.mu.Unlock()
return // failed already for exactly these keys: retried by the next change of either
}
if was := a.appliedRun[module]; !runIsNewer(w.Want.Run, was) {
// Design 47 H2 §813 (1), H-A-355: one writer per composition key, and the engine applies again only a newer write.
a.sayOnce("run:"+module+":"+w.Want.Run, fmt.Sprintf("the want of %s carries run %s, and the engine applied it from run %s, "+
"which is not older: nothing is applied", module, w.Want.Run, was))
a.mu.Unlock()
return
}
if module == NodeModule {
// Not while a module's composition cannot be read or is refused: what it carries may be what left the node's own,
// and the node's own apply would remove it, held out by nothing (issue 1945, the review's gap 2).
@@ -1829,7 +1840,14 @@ func (a *Agent) apply(ctx context.Context, module string, w wantNow, comp compNo
delete(a.notComposed, module) // the apply says what it runs now
a.running[module] = nodeengine.HaveByNodeModuleValue{Fp: fp, Composed: dig, Phase: nodeengineclient.PhaseRunning}
a.applied[module] = comp
run := w.Want.Run
if run != "" {
a.appliedRun[module] = run
}
a.mu.Unlock()
if run != "" {
a.env.Say(fmt.Sprintf("applied %s from run %s", what, run))
}
a.keepApplied(module, comp)
if a.env.Settled != nil {
a.env.Settled(module)
@@ -1838,6 +1856,21 @@ func (a *Agent) apply(ctx context.Context, module string, w wantNow, comp compNo
a.catchUp(ctx, module, catchUps)
}
// 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.
func runIsNewer(want, applied string) bool {
if want == "" || applied == "" {
return true
}
w, err1 := strconv.ParseUint(want, 10, 64)
p, err2 := strconv.ParseUint(applied, 10, 64)
if err1 != nil || err2 != nil {
return true
}
return w > p
}
// ranFailed marks what runs of a module as failed once an apply of it failed (novox/hq issue 1966, 2026-11-08): a failed
// apply may have replaced some of its resources (distribution's store container) before it stopped, so what ran is no
// longer whole, and a want of it again (a put back) is applied rather than taken as running. Called with a.mu held.
+30 -1
View File
@@ -337,7 +337,7 @@ func TestAWantAndItsCompositionApplyTheModuleAndTheHaveNamesBoth(t *testing.T) {
if err := json.Unmarshal(wrawn, &later); err != nil {
t.Fatal(err)
}
later["form"], later["a-later-field"] = 3, "x"
later["form"], later["a-later-field"], later["run"] = 3, "x", "7"
wrawn, _ = json.Marshal(later)
b.wants.put(wkn, wrawn)
b.idle()
@@ -347,6 +347,35 @@ func TestAWantAndItsCompositionApplyTheModuleAndTheHaveNamesBoth(t *testing.T) {
if h := b.haves.last("web"); h.Phase != string(nodeengineclient.PhaseRunning) || h.Wait != nil || h.FP != fpn {
t.Fatalf("the have is %s %q wait %+v, want running at %s with no wait (§750)", h.Phase, h.Detail, h.Wait, fpn)
}
// Design 47 H2 §813 (1), H-A-355: the engine applies a composition again only when its run is newer than the one it
// applied, naming the run in its journal; a want of an older run is said and not applied.
for _, row := range []struct {
fp, run string
applied int
}{{"6", "5", 0}, {"6", "7", 0}, {"6", "8", 1}} {
before := len(b.appliedList())
fpr := fpOf(row.fp)
ckr, crawr, digr := composition(t, "web", fpr, false, archive("a", d2))
b.comps.put(ckr, crawr)
wkr, _ := wantFor(t, "web", fpr, digr, "")
rawr, err := bucketstest.Want(meshwants.WantsByWantNodeModuleValue{Fp: fpr, Run: row.run})
if err != nil {
t.Fatal(err)
}
b.wants.put(wkr, rawr)
b.idle()
if got := len(b.appliedList()) - before; got != row.applied {
t.Errorf("a want of run %s after run 7: applied %d times, want %d", row.run, got, row.applied)
}
}
b.mu.Lock()
said := strings.Join(b.said, "\n")
b.mu.Unlock()
for _, want := range []string{"from run 7", "from run 8", "carries run 5, and the engine applied it from run 7"} {
if !strings.Contains(said, want) {
t.Errorf("the journal does not say %q:\n%s", want, said)
}
}
}
// R6-A AC2: given a kept declaration and wants and compositions equal to it, when the agent starts, nothing is applied