diff --git a/internal/selfcompose/compose_while_passing_test.go b/internal/selfcompose/compose_while_passing_test.go index d78c4cbd..96f91949 100644 --- a/internal/selfcompose/compose_while_passing_test.go +++ b/internal/selfcompose/compose_while_passing_test.go @@ -19,7 +19,7 @@ import ( // Issue 1965 (shanks 2026-11-05 22:27: the verb compose gave no answer in 30 s while a pass waited on the bus): an ask // that comes while a pass runs is answered at once, says one more pass is queued and what the running one waits on, and -// the queued pass is made when the running one ends. +// the queued pass is made when the running one ends. A pass made by hand meanwhile is made after the running one too. func composeAnsweredWhileAPassRuns(t *testing.T) { b := newFakeBus() ctx, cancel := context.WithCancel(context.Background()) @@ -30,7 +30,10 @@ func composeAnsweredWhileAPassRuns(t *testing.T) { reached := make(chan struct{}, 8) env.Running = func() []compositions.Composed { passes.Add(1) - reached <- struct{}{} + select { + case reached <- struct{}{}: + default: // a pass reads what runs more than once: past eight reads unheard, a read is told to nobody and never waits + } if passes.Load() == 1 { <-hold // the first pass waits on the bus } @@ -72,12 +75,28 @@ func composeAnsweredWhileAPassRuns(t *testing.T) { case <-time.After(2 * time.Second): t.Fatal("the verb compose gave no answer while a pass ran") } + // Issue 721 note 11: a pass made by hand while the loop's runs waits for it to end, and never begins beside it. + byHand := make(chan struct{}) + go func() { + defer close(byHand) + c.compose(ctx) + }() + select { + case <-reached: + t.Error("a pass made by hand began while the loop's pass ran") + case <-time.After(50 * time.Millisecond): + } close(hold) select { case <-reached: // the queued pass case <-time.After(5 * time.Second): t.Fatal("the queued pass was not made when the running one ended") } + select { + case <-byHand: + case <-time.After(5 * time.Second): + t.Fatal("the pass made by hand 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, diff --git a/internal/selfcompose/selfcompose.go b/internal/selfcompose/selfcompose.go index e0e7f896..f3e419ec 100644 --- a/internal/selfcompose/selfcompose.go +++ b/internal/selfcompose/selfcompose.go @@ -309,6 +309,10 @@ type Composer struct { asked chan chan Pass // passing is set while a pass is made. passing atomic.Bool + // passMu is held for the whole of a pass (compose, composeEarly): a second pass over this composer's state waits for + // the one running to end, never runs beside it (issue 721 note 11: a pass beside the loop's handed again what the + // other had taken back). + passMu sync.Mutex // early is true while the early pass of a start runs (design 47 G §683, A-117). early atomic.Bool // assignmentsRead is set once the watch of the seat's assignments has caught up: from then the placements this composer holds @@ -1089,8 +1093,16 @@ func (c *Composer) takeValue(u agent.Update) { } // compose composes the node once and answers the updates to hand: each composition whose digest differs from the one -// handed, and a deletion for each handed one no want names any more. +// handed, and a deletion for each handed one no want names any more. It holds passMu for the whole pass, so a pass asked +// while another runs is made after it. func (c *Composer) compose(ctx context.Context) []agent.Update { + c.passMu.Lock() + defer c.passMu.Unlock() + return c.composePass(ctx) +} + +// composePass is compose's pass, made with passMu held. +func (c *Composer) composePass(ctx context.Context) []agent.Update { early := c.early.Load() pass := Pass{} defer func() { @@ -2645,8 +2657,11 @@ func (c *Composer) standing() bool { // composeEarly is the pass of a start before the assignments, haves and resolved values are read: it composes only the modules whose // kept want and kept entry it holds, a kept want standing for their placement, and leaves every other module as it runs; the full -// pass follows once the inputs are read (design 47 G §683, A-95, A-117). +// pass follows once the inputs are read (design 47 G §683, A-95, A-117). The placement it stands in for the assignments, and +// its taking back, are within the pass: passMu is held for all of it. func (c *Composer) composeEarly(ctx context.Context) []agent.Update { + c.passMu.Lock() + defer c.passMu.Unlock() node := c.env.Node c.mu.Lock() _, hadRecord := c.records[node] @@ -2684,5 +2699,5 @@ func (c *Composer) composeEarly(ctx context.Context) []agent.Update { } }() c.env.Say("the node composes the modules it kept a want and an entry of, before its other inputs are read") - return c.compose(ctx) + return c.composePass(ctx) } diff --git a/internal/selfcompose/selfcompose_test.go b/internal/selfcompose/selfcompose_test.go index 1928c99b..c3a54c67 100644 --- a/internal/selfcompose/selfcompose_test.go +++ b/internal/selfcompose/selfcompose_test.go @@ -393,17 +393,21 @@ func testTheNodeComposesItsWantsFromTheBus(t *testing.T) { // Issue 1966 (novox, 2026-11-09): every pass names each wanted module it composed nothing of, or composed from the indexer's // manifest for want of a done build, with why, and a want it did not take; said once; the agent's word that it waits // hands a composition the node believes handed again. + // The passes of this step are made by hand on a copy of what c holds, which runs no loop (issue 721 note 11): beside c's + // loop, a pass it makes when an entry is answered composed tool too, and handed it on out (pipelines main.266, main.459, main.479). + sc := c.scratch() + sc.env.Running = nil // composed counts c's passes alone tfp := toolFp - c.holdEntry(t, "tool", tfp, toolBody) + sc.holdEntry(t, "tool", tfp, toolBody) for module, w := range map[string]meshwants.WantsByWantNodeModuleValue{"tool": {Fp: tfp, Self: true}, "ghost": {Fp: ghostFp, Self: true}, "nofp": {Self: true}} { raw, _ := bucketstest.Want(w) - c.takeWant(agent.Update{Key: "want.n1." + module, Value: raw}) + sc.takeWant(agent.Update{Key: "want.n1." + module, Value: raw}) } - c.mu.Lock() - placed := c.assignments["n1"] + sc.mu.Lock() + placed := sc.assignments["n1"] placed.Modules = append(slices.Clone(placed.Modules), meshmoduleassigner.PlacedModule{Module: "tool"}) - c.assignments["n1"] = placed - c.mu.Unlock() + sc.assignments["n1"] = placed + sc.mu.Unlock() said := func(sub string) (n int) { b.mu.Lock() defer b.mu.Unlock() @@ -414,27 +418,18 @@ func testTheNodeComposesItsWantsFromTheBus(t *testing.T) { } return n } - c.compose(ctx) + sc.compose(ctx) saidOfNofp := said("the node composes nothing of nofp") - why := map[string]string{"tool": c.LastHeld()["tool"], "ghost": c.LastHeld()["ghost"], "nofp": c.WhyUnmade("nofp"), "lemurs": c.WhyUnmade("lemurs")} + why := map[string]string{"tool": sc.LastHeld()["tool"], "ghost": sc.LastHeld()["ghost"], "nofp": sc.WhyUnmade("nofp"), "lemurs": sc.WhyUnmade("lemurs")} if !strings.Contains(why["tool"], "tool at "+tfp) || !strings.Contains(why["ghost"], "entry-asked: the entry of the build "+ghostFp) || !strings.Contains(why["nofp"], "want not taken: its want names no fingerprint") || why["lemurs"] != "" { t.Errorf("the pass says it composed nothing of %q", why) } readGhost() - c.compose(ctx) + sc.compose(ctx) if n := said("the node composes nothing of nofp"); n != saidOfNofp || n == 0 { t.Errorf("the journal says why nofp is not composed %d times after the second pass, %d after the first: want the same, not none", n, saidOfNofp) } - // The state of this step is taken away again, and the pass that takes back what it handed is made here. - for _, module := range []string{"tool", "ghost", "nofp"} { - c.takeWant(agent.Update{Key: "want.n1." + module, Deleted: true}) - } - c.mu.Lock() - placed.Modules = placed.Modules[:len(placed.Modules)-1] - c.assignments["n1"] = placed - c.mu.Unlock() - c.compose(ctx) // The agent waits for what the node believes it handed: handed again; for what it never handed, nothing is. c.Waiting("ghost", fp) select {