From cb5eddeba833c4140d1194bf941065f870219319 Mon Sep 17 00:00:00 2001 From: jochen Date: Sun, 22 Nov 2026 16:12:10 +0100 Subject: [PATCH 1/3] H-A-480 a: the composer's mark is a compare-and-set on the have's revision MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Under load, a rank wait settle wrote between the composer's read of a have and its mark was overwritten by the mark and never written again. The mark and its clear now write over the revision they read (a lost race reads again and keeps the other cause), a module waiting for its rank is never marked, and a mark on a module that never ran is cleared at the next pass that composes it. Builds: design 47 H2 H-A-480 (and its addendum), H §771, H-A-59 --- cmd/mesh-host/nodeagent.go | 26 ++++- internal/agent/agent.go | 180 ++++++++++++++++++++++++++++------- internal/agent/agent_test.go | 98 ++++++++++++++++++- internal/link/have.go | 106 +++++++++++++++++---- internal/link/have_test.go | 37 +++++++ 5 files changed, 388 insertions(+), 59 deletions(-) diff --git a/cmd/mesh-host/nodeagent.go b/cmd/mesh-host/nodeagent.go index fde0506a..c3b8e793 100644 --- a/cmd/mesh-host/nodeagent.go +++ b/cmd/mesh-host/nodeagent.go @@ -104,12 +104,23 @@ func (h sessionHaves) ReadErr(ctx context.Context, module string) (agent.Have, b } return agent.Have{Module: module, Composed: got.Composed, Sealed: got.SealedAt}, true, nil } - got, ok, err := h.session.ReadHaveErr(ctx, module) + got, _, ok, err := h.ReadAt(ctx, module) + return got, ok, err +} + +// ReadAt is ReadErr with the revision of the module's key (agent.HavesAt). +func (h sessionHaves) ReadAt(ctx context.Context, module string) (agent.Have, uint64, bool, error) { + got, revision, ok, err := h.session.ReadHaveAt(ctx, module) if !ok || err != nil { - return agent.Have{}, false, err + return agent.Have{}, revision, false, err } return agent.Have{Module: module, FP: got.Fp, Composed: got.Composed, Phase: string(got.Phase), Detail: got.Detail, - Sealed: got.SealedAt, Wait: got.Wait, Run: got.Run, Pipeline: got.Pipeline}, true, nil + Sealed: got.SealedAt, Wait: got.Wait, Run: got.Run, Pipeline: got.Pipeline}, revision, true, nil +} + +// WriteAt writes a module's have only while its key is at the revision read (agent.HavesAt, H-A-480). +func (h sessionHaves) WriteAt(ctx context.Context, x agent.Have, revision uint64) (bool, error) { + return h.session.NoteModuleAt(ctx, moduleFact(x), revision) } func (h sessionHaves) Write(ctx context.Context, x agent.Have) { @@ -120,13 +131,18 @@ func (h sessionHaves) Write(ctx context.Context, x agent.Have) { } return } + h.session.NoteModule(ctx, moduleFact(x)) +} + +// moduleFact is a module's have as the session writes it. +func moduleFact(x agent.Have) link.ModuleFact { phase := x.Phase if phase == string(nodeengineclient.PhaseRunning) { phase = "" } - h.session.NoteModule(ctx, link.ModuleFact{Module: x.Module, FP: x.FP, Composed: x.Composed, Phase: phase, + return link.ModuleFact{Module: x.Module, FP: x.FP, Composed: x.Composed, Phase: phase, Failed: phase == string(nodeengineclient.PhaseFailed), Detail: shorten(x.Detail), Judgement: x.Judgement, Waits: x.Waits, - BuildAlone: x.BuildAlone, Kept: x.Kept, Sealed: x.Sealed, PullWait: x.PullWait, Wait: x.Wait, Run: x.Run, Pipeline: x.Pipeline}) + BuildAlone: x.BuildAlone, Kept: x.Kept, Sealed: x.Sealed, PullWait: x.PullWait, Wait: x.Wait, Run: x.Run, Pipeline: x.Pipeline} } func (h sessionHaves) Removed(ctx context.Context, module string) { diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 159b5c69..41fc650e 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -581,6 +581,42 @@ type HavesReadErr interface { ReadErr(ctx context.Context, module string) (Have, bool, error) } +// HavesAt is what a Haves may answer besides Write: a module's have with the revision of its key (zero when the bucket +// holds none), and a write of it that lands only while the key is still at that revision, false when it moved (design 47 +// H2 H-A-480: the composer's mark is a compare-and-set on the have's revision). +type HavesAt interface { + ReadAt(ctx context.Context, module string) (Have, uint64, bool, error) + WriteAt(ctx context.Context, h Have, revision uint64) (bool, error) +} + +// readHaveAt is readHave with the revision of the have's key, for a write over it (writeHaveAt). A Haves that keeps no +// revisions answers zero, and its writes land whatever moved. +func (a *Agent) readHaveAt(ctx context.Context, module string) (h Have, revision uint64, found, unread bool) { + at, tells := a.env.Haves.(HavesAt) + if !tells { + h, found, unread = a.readHave(ctx, module) + return h, 0, found, unread + } + for try := 0; ; try++ { + h, revision, found, err := at.ReadAt(ctx, module) + if err == nil { + return h, revision, found, false + } + if try == 2 || ctx.Err() != nil { + return Have{}, 0, false, true + } + select { + case <-ctx.Done(): + return Have{}, 0, false, true + case <-time.After(time.Duration(1< Date: Sun, 22 Nov 2026 16:12:10 +0100 Subject: [PATCH 2/3] H-A-480 b: the composer's watch ends with the agent's stop The composer's loop and its hearers outlived the node agent's run and logged after it ended; the node agent now waits for them (Composer.Wait) once its run stops. Builds: design 47 H2 H-A-480 --- cmd/mesh-host/nodeagent.go | 1 + .../selfcompose/compose_while_passing_test.go | 22 +++++++++++++++ internal/selfcompose/composedstate_test.go | 1 + internal/selfcompose/selfcompose.go | 28 +++++++++++++++---- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/cmd/mesh-host/nodeagent.go b/cmd/mesh-host/nodeagent.go index c3b8e793..5c10a5b0 100644 --- a/cmd/mesh-host/nodeagent.go +++ b/cmd/mesh-host/nodeagent.go @@ -796,6 +796,7 @@ func nodeAgent(ctx context.Context, opts options, signer ed25519.PublicKey, trus err = ag.Run(run) stopRun() cancel() + composer.Wait() // its watch ends with the agent's stop, and says nothing after it (H-A-480) current.set(nil) composerNow.Store(nil) ownFindingsNow.Store(nil) diff --git a/internal/selfcompose/compose_while_passing_test.go b/internal/selfcompose/compose_while_passing_test.go index d755ae2d..d78c4cbd 100644 --- a/internal/selfcompose/compose_while_passing_test.go +++ b/internal/selfcompose/compose_while_passing_test.go @@ -79,3 +79,25 @@ func composeAnsweredWhileAPassRuns(t *testing.T) { t.Fatal("the queued pass was not made when the running one ended") } } + +// H-A-480: the composer's watch ends with the agent's stop: once its ctx ended, Wait returns only after its loop ended, +// so nothing it says comes after the run (the integration tests at -count=15 logged after their test completed). +func theWatchEndsWithItsRun(t *testing.T) { + b := newFakeBus() + ctx, cancel := context.WithCancel(context.Background()) + c := New(b.env()) + out, err := c.Watch(ctx, "n1.>") + if err != nil { + t.Fatal(err) + } + cancel() + c.Wait() + select { + case _, ok := <-out: + for ok { + _, ok = <-out + } + default: + t.Fatal("the composer's watch went on after Wait returned") + } +} diff --git a/internal/selfcompose/composedstate_test.go b/internal/selfcompose/composedstate_test.go index 5de5c96f..104216c0 100644 --- a/internal/selfcompose/composedstate_test.go +++ b/internal/selfcompose/composedstate_test.go @@ -91,6 +91,7 @@ func (f *fakeComposed) keep(t *testing.T, half catalogHalf, module, fp string) { // by a want, and deleted otherwise. func TestTheNodePublishesItsCompositions(t *testing.T) { t.Run("the verb compose is answered while a pass runs", composeAnsweredWhileAPassRuns) + t.Run("the watch ends with its run", theWatchEndsWithItsRun) b := newFakeBus() fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64) ofp := meshcatalogclient.SourcePrefix2 + strings.Repeat("c", 64) diff --git a/internal/selfcompose/selfcompose.go b/internal/selfcompose/selfcompose.go index cf265bfd..4c7d33ba 100644 --- a/internal/selfcompose/selfcompose.go +++ b/internal/selfcompose/selfcompose.go @@ -197,6 +197,8 @@ type Composer struct { AlsoEntriesAskAgain func(ctx context.Context) env Env + // watchers counts the goroutines Watch started that end with its ctx (its loop and its hearers), for Wait (H-A-480). + watchers sync.WaitGroup mu sync.Mutex wants map[string]meshwants.WantsByWantNodeModuleValue // by module, the node's own under agent.NodeModule assignments map[string]meshmoduleassigner.Assignments @@ -582,12 +584,16 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er }(i, ch) } if c.env.Hear != nil { - go c.hearCatalogRevisions(ctx, func() { - select { - case merged <- tagged{in: -1, ok: true}: - case <-ctx.Done(): - } - }) + c.watchers.Add(1) + go func() { + defer c.watchers.Done() + c.hearCatalogRevisions(ctx, func() { + select { + case merged <- tagged{in: -1, ok: true}: + case <-ctx.Done(): + } + }) + }() heard, stop, err := c.env.Hear(VaultSecretChanged) if err != nil { return nil, fmt.Errorf("the node composes again when a secret changes, and cannot hear the vault say so: %w", err) @@ -604,7 +610,9 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er heard <-chan []byte stop func() }{{heard, stop}, {changed, stopChanged}} { + c.watchers.Add(1) go func() { + defer c.watchers.Done() defer h.stop() for { select { @@ -625,7 +633,9 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er } } if c.env.SeatListChanged != nil { + c.watchers.Add(1) go func() { + defer c.watchers.Done() for { select { case <-ctx.Done(): @@ -645,7 +655,9 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er } out := make(chan agent.Update, 64) c.watching.Store(true) + c.watchers.Add(1) go func() { + defer c.watchers.Done() defer close(out) defer c.watching.Store(false) caught := make([]bool, len(ins)) @@ -761,6 +773,10 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er return out, nil } +// Wait waits until the loop and the hearers Watch started have ended, each once its ctx ended: the node agent's stop waits for it, +// so the composer says nothing once its run is over (design 47 H2 H-A-480). +func (c *Composer) Wait() { c.watchers.Wait() } + // hearCatalogRevisions hears the seat mesh-catalog's event catalog-revision: each one asks again the entries whose ask failed (H-A-110), clears the backoffs of the trunk builds the // catalog gave none of and wakes a pass (A-487a). A wake-up only, the backoff bounds the asks without it: a subscription refused // is said once with its subject and error (principle 3), the composer goes on composing, and the subscription is tried again on From 43839ae66ac9a296fdcc4047ff2b4b524b12c5d1 Mon Sep 17 00:00:00 2001 From: jochen Date: Sun, 22 Nov 2026 16:19:14 +0100 Subject: [PATCH 3/3] H-A-480 c: test the rank wait and the clear's guard; say a mark lost to every race Review round 1: the line that never marks a module waiting for its rank and the clear's "only while the have still says the mark" had no failing test; each now has one. A mark whose every write lost its race is now said, not dropped silently (P-failure-loud). Builds: design 47 H2 H-A-480 (review round 1 finding) --- internal/agent/agent.go | 10 +++++- internal/agent/agent_test.go | 68 ++++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 41fc650e..cf1c7f67 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -762,7 +762,7 @@ func (a *Agent) NotComposedEach(ctx context.Context, why string, held map[string // The hold says what it waits on, as every hold but a failed backup does (design 47 G A-453, section 541). wait = a.composeWait(strings.TrimSuffix(heldReason(why), ":"), why) } - written := false + written, lost := false, 0 for try := 0; try < markTries && !written; try++ { if ctx.Err() != nil { return @@ -789,6 +789,7 @@ func (a *Agent) NotComposedEach(ctx context.Context, why string, held map[string break } // The have moved since it was read: read it again, and judge the mark against what it says now. + lost++ h, rev, found, unread := a.readHaveAt(ctx, module) if unread { break @@ -799,6 +800,13 @@ func (a *Agent) NotComposedEach(ctx context.Context, why string, held map[string } } if !written { + if lost == markTries { + // Every write lost its race to another: not marked, and said (P-failure-loud); the next refusal marks it. + a.mu.Lock() + a.sayOnce("unmarked-moved:"+module, fmt.Sprintf("%s is not marked: its have moved under each of %d writes, and the node cannot compose it: %s", + module, markTries, why)) + a.mu.Unlock() + } continue } mark.why = why diff --git a/internal/agent/agent_test.go b/internal/agent/agent_test.go index 01195b73..bf493f76 100644 --- a/internal/agent/agent_test.go +++ b/internal/agent/agent_test.go @@ -2347,15 +2347,22 @@ func testAComposersMarkKeepsWhatWasWrittenSinceItsRead(t *testing.T) { failed := Have{Module: "second", Phase: string(nodeengineclient.PhaseFailed), Detail: "the apply failed: no space left"} for _, row := range []struct { name string - // between writes the module's have between the composer's read and its write. - between func(ctx context.Context, b *bed) - check func(h Have) bool + // before writes the module's have before the composer reads it; between writes it between its read and its write. + before, between func(ctx context.Context, b *bed) + check func(h Have) bool }{ - {"a wait for its rank", func(ctx context.Context, b *bed) { b.ag.placingHave(ctx, "second", []string{"first"}) }, + {"a module that runs and waits for its rank", func(ctx context.Context, b *bed) { + b.ag.running["second"] = nodeengine.HaveByNodeModuleValue{Fp: fpOf("a"), Composed: "sha256:" + strings.Repeat("1", 64), Phase: nodeengineclient.PhaseRunning} + b.ag.placingHave(ctx, "second", []string{"first"}) + }, nil, func(h Have) bool { return h.Phase == string(nodeengineclient.PhaseHeld) && h.Wait != nil && h.Wait.Step == nodeengine.WaitStepPlacing && h.Wait.On == "first" }}, - {"another cause", func(ctx context.Context, b *bed) { b.haves.Write(ctx, failed) }, + {"a wait for its rank", nil, func(ctx context.Context, b *bed) { b.ag.placingHave(ctx, "second", []string{"first"}) }, + func(h Have) bool { + return h.Phase == string(nodeengineclient.PhaseHeld) && h.Wait != nil && h.Wait.Step == nodeengine.WaitStepPlacing && h.Wait.On == "first" + }}, + {"another cause", nil, func(ctx context.Context, b *bed) { b.haves.Write(ctx, failed) }, func(h Have) bool { return h.Phase == failed.Phase && h.Detail == failed.Detail }}, } { t.Run(row.name, func(t *testing.T) { @@ -2363,7 +2370,12 @@ func testAComposersMarkKeepsWhatWasWrittenSinceItsRead(t *testing.T) { b.ag = New(b.env) ctx := context.Background() b.ag.wants["second"] = wantNow{Want: meshwants.WantsByWantNodeModuleValue{Fp: fpOf("b"), Pipeline: "main.7", Rank: 2}, Present: true} - b.haves.afterRead = map[string]func(){"second": func() { row.between(ctx, b) }} + if row.before != nil { + row.before(ctx, b) + } + if row.between != nil { + b.haves.afterRead = map[string]func(){"second": func() { row.between(ctx, b) }} + } b.ag.NotComposedEach(ctx, "", map[string]string{"second": why}) if h := b.haves.last("second"); !row.check(h) { t.Fatalf("what was written between the composer's read and its write was overwritten: %+v", h) @@ -2385,4 +2397,48 @@ func testAComposersMarkKeepsWhatWasWrittenSinceItsRead(t *testing.T) { t.Fatalf("the mark of a module that never ran stays once it composes: %+v", h) } }) + t.Run("a cause written between the mark and its clear is kept", func(t *testing.T) { + b := newBed(t) + b.ag = New(b.env) + ctx := context.Background() + b.ag.wants["third"] = wantNow{Want: meshwants.WantsByWantNodeModuleValue{Fp: fpOf("c")}, Present: true} + b.haves.Write(ctx, Have{Module: "third", FP: fpOf("c"), Phase: string(nodeengineclient.PhaseRunning)}) + b.ag.NotComposedEach(ctx, "", map[string]string{"third": why}) + cause := Have{Module: "third", FP: fpOf("c"), Phase: string(nodeengineclient.PhaseFailed), Detail: "the apply failed: no space left"} + b.haves.Write(ctx, cause) + b.ag.NotComposedEach(ctx, "", nil) + if h := b.haves.last("third"); h.Phase != cause.Phase || h.Detail != cause.Detail { + t.Fatalf("the clear of a mark wrote over a cause written since: %+v", h) + } + }) + t.Run("a mark that loses every race is said", func(t *testing.T) { + b := newBed(t) + b.ag = New(b.env) + ctx := context.Background() + b.ag.wants["fourth"] = wantNow{Want: meshwants.WantsByWantNodeModuleValue{Fp: fpOf("d")}, Present: true} + running := Have{Module: "fourth", FP: fpOf("d"), Phase: string(nodeengineclient.PhaseRunning)} + b.haves.Write(ctx, running) + // Every read is followed by a write of what runs: the key moves under each mark. + var again func() + again = func() { + b.haves.Write(ctx, running) + b.haves.mu.Lock() + b.haves.afterRead["fourth"] = again + b.haves.mu.Unlock() + } + b.haves.afterRead = map[string]func(){"fourth": again} + b.ag.NotComposedEach(ctx, "", map[string]string{"fourth": why}) + b.haves.mu.Lock() + b.haves.afterRead = nil + b.haves.mu.Unlock() + said := false + b.mu.Lock() + for _, l := range b.said { + said = said || strings.Contains(l, "fourth is not marked") && strings.Contains(l, "moved") + } + b.mu.Unlock() + if !said { + t.Fatalf("a mark dropped after every write lost its race was not said: %v", b.said) + } + }) }