diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/bus_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/bus_test.go index 65e5fce31..ec23fe56c 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/bus_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/bus_test.go @@ -134,6 +134,24 @@ type runtime struct { operatorCancels []string // owedSaid are the subjects of the seat events owed the gate said, in order (W11.17). owedSaid []string + // logs is what the bundle said in its journal. + logs *testLog +} + +// journalHas is whether the bundle said a line in its journal holding every one of words. +func (r *runtime) journalHas(words ...string) bool { + r.logs.mu.Lock() + defer r.logs.mu.Unlock() + for _, l := range r.logs.said { + all := true + for _, w := range words { + all = all && strings.Contains(l, w) + } + if all { + return true + } + } + return false } type askedOfMesh struct { @@ -154,19 +172,28 @@ func (b *testBus) start(t *testing.T) *runtime { if err != nil { t.Fatal(err) } - cmd.Stderr = testLog{t} + logs := &testLog{t: t} + cmd.Stderr = logs if err := cmd.Start(); err != nil { t.Fatal(err) } - r := &runtime{t: t, b: b, cmd: cmd, in: in, wait: map[string]chan json.RawMessage{}, notify: make(chan struct{}, 64)} + r := &runtime{t: t, b: b, cmd: cmd, in: in, wait: map[string]chan json.RawMessage{}, notify: make(chan struct{}, 64), logs: logs} go r.read(out) t.Cleanup(func() { _ = in.Close(); _ = cmd.Wait() }) return r } -type testLog struct{ t *testing.T } +// testLog is the bundle's journal: said in the test's log, and kept for journalHas. +type testLog struct { + t *testing.T + mu sync.Mutex + said []string +} -func (w testLog) Write(p []byte) (int, error) { +func (w *testLog) Write(p []byte) (int, error) { + w.mu.Lock() + w.said = append(w.said, string(p)) + w.mu.Unlock() defer func() { _ = recover() }() // the bundle may speak after the test ended w.t.Logf("bundle: %s", strings.TrimRight(string(p), "\n")) // Built with -race, the bundle is too: a race it reports fails the test (issue 721 note 11: two events applied at once diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go index f61195ae0..799f85f7b 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/consumer.go @@ -247,9 +247,11 @@ func ownerRefusal(f stepFamily, e stdio.Envelope) string { return "" } -// apply writes an outcome into every record that holds it, by its merge commits or by its entry: an open record, or one +// apply writes an outcome into every record that holds it, by its merge commits or by its entry: an open record, one // that ended without the node the outcome comes from (a node away that wakes adds its outcomes, and is no longer shown -// away). It ends no record: the assigner's pipeline-wanted does (design 47 H §738, H-A-89 (4)). +// away), or one that ended before a running of its movers was heard (H-A-585: the running is applied to it, and its end +// written again from it, Records.RanAfterEnd). It ends no record: the assigner's pipeline-wanted does (design 47 H §738, +// H-A-89 (4)). func (c *StepConsumer) apply(o StepOutcome, event string) error { records, err := c.Records.Kept() if err != nil { @@ -258,12 +260,14 @@ func (c *StepConsumer) apply(o StepOutcome, event string) error { for _, r := range records { _, fromAway := r.Away[o.Node] fromAway = fromAway && o.Node != "" - if (r.Ended != nil && !fromAway) || !holds(r, o) { + ranAfter := r.Ended != nil && o.Step == StepRunning && o.Outcome == OutcomeRunning + if (r.Ended != nil && !fromAway && !ranAfter) || !holds(r, o) { continue } ro := RecordOutcome{Module: o.Module, Fingerprint: o.Fingerprint, Node: o.Node, Step: o.Step, Outcome: o.Outcome, Why: o.Why, At: o.At, Event: event} - if _, err := c.Records.Outcome(r, ro); err != nil { + applied, err := c.Records.Outcome(r, ro) + if err != nil { return err } if fromAway { @@ -271,6 +275,14 @@ func (c *StepConsumer) apply(o StepOutcome, event string) error { return err } } + if ranAfter { + // The running as the record keeps it, applied now or by an event offered again after its end could not be + // written. + kept := r.Outcomes[OutcomeKey(o.Module, o.Fingerprint, o.Node, o.Step)] + if err := c.Records.RanAfterEnd(r, kept, applied); err != nil { + return err + } + } } return nil } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecall_bus_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecall_bus_test.go index c21674dd2..6d05ed956 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecall_bus_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecall_bus_test.go @@ -179,6 +179,21 @@ func TestAGateWholeTakesAMergeCallAndWatchesTheHeadsOverARealBus(t *testing.T) { b.publish(t, "mesh.seat.mesh-wants.event.pipeline-wanted."+escaped+".2", map[string]any{"v": 1, "branch": branch, "source": 2, "pipeline": recordMilestone(branch, 2), "outcome": "running"}) rt.until("record-too-large cleared at the record's end", eventWithin, func() bool { return !findingHeld(ctx, b, RecordTooLarge, "") }) + // H-A-585: the record ended on pipeline-wanted before any of its movers' running was applied, with no delivery time; a + // running of one of them heard after takes it, its ended and delivery-time written again, and says so in the journal. + late := taken[1].(map[string]any) + b.publish(t, "mesh.seat.node-engine.event.running.test."+late["module"].(string), map[string]any{"v": 1, "module": late["module"], + "node": "test", "fingerprint": late["fingerprint"], "at": time.Now().UTC().Format(time.RFC3339)}) + var ended2 Record + rt.until("the ended record's delivery time written from a running heard after its end", eventWithin, func() bool { + b.mu.Lock() + defer b.mu.Unlock() + ended2 = Record{} + return json.Unmarshal(b.ledger.states[instance2], &ended2) == nil && ended2.Ended != nil && ended2.DeliveryTime != "" + }) + rt.until("the journal line naming the record, the mover on test and both times", eventWithin, func() bool { + return rt.journalHas(instance2, late["module"].(string)+" on test", "after it ended") + }) // The seed (§768): every watched branch's first source revision at its head; its head-moved then holds nothing. seeded := strings.Repeat("5", 40) diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordevents.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordevents.go index 5d5bc3640..f97e61290 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordevents.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordevents.go @@ -172,6 +172,53 @@ func (rs *Records) end(r *Record, why, by string) error { return nil } +// RanAfterEnd writes a record's end again from a running of one of its movers kept after the record ended (H-A-585): when +// that running is later than the record's ended, ended takes its time and, for a record that ended with every mover running, +// delivery-time runs to it, with the class its movers' entries kept when they opened (H2 §810, H-A-394 (3)). record-ended is +// not said a second time; the journal says the record, the module, the machine and both times. Applied says the running was +// applied now; one applied before and not later than the end (its end written from it already) changes and says nothing. +func (rs *Records) RanAfterEnd(r *Record, o RecordOutcome, applied bool) error { + if r.Ended == nil { + return nil + } + was := r.Ended.At + if !applied && !o.At.After(was) { + return nil + } + if o.At.After(was) { + e := *r.Ended + e.At = o.At + body := map[string]any{"ended": e} + next := *r + next.Ended = &e + var took time.Duration + class := r.Class + if e.Why == WhyAllRun { + var kept string + if took, kept = timesAtEnd(r); took > 0 { + class = firstOf(class, kept) + body["delivery-time"], body["class"] = took.String(), class + next.DeliveryTime, next.Class = took.String(), class + } + } + seq, err := rs.write(r, r.Instance, OpEnd, body, next.head()) + if err != nil { + return err + } + r.Ended, r.Sequence = &e, seq + if took > 0 { + r.DeliveryTime, r.Class = took.String(), class + } + rs.keep(r) + } + if rs.Logf != nil { + rs.Logf("[mesh-delivery] the record %s took the running of %s on %s at %s, after it ended at %s; it ends at %s, its delivery time %q", + r.Instance, o.Module, o.Node, o.At.UTC().Format(time.RFC3339Nano), was.UTC().Format(time.RFC3339Nano), + r.Ended.At.UTC().Format(time.RFC3339Nano), r.DeliveryTime) + } + return nil +} + func (rs *Records) sayEnded(r *Record) error { if r.Ended == nil { return nil diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordpipeline_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordpipeline_test.go index 89d4e6b71..959af1390 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordpipeline_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/recordpipeline_test.go @@ -122,39 +122,50 @@ func (l *hookedLedger) Apply(instance, operation string, body any) (uint64, erro return seq, err } -// 721/AC1 (issue 721 note 11; design 47 H2 §810): the runtime hands each event and each call to a goroutine of its own, so -// one can be heard once a running's outcome is in the ledger and before the gate's memory holds it. Heard then, it waits -// until the running is applied whole: a pipeline-wanted ends the record with its delivery time and class (applied at once it -// ended the record with neither, the delivery time lost), and the call show answers the record with the running, its answer -// taken before the next event writes the record. +// heardBed is what the gate said while a row of TestWhatIsHeardWhileAnEventIsWrittenWaitsUntilItIsApplied ran. +type heardBed struct { + ended int // the record-ended said + journal []string // the lines the records said in the journal +} + +// Issue 721 note 11 (design 47 H2 §810): the runtime hands each event and each call to a goroutine of its own, so one can be +// heard once a running's outcome is in the ledger and before the gate's memory holds it. Heard then, it waits until the running +// is applied whole: a pipeline-wanted ends the record with its delivery time and class (applied at once it ended the record +// with neither, the delivery time lost), and the call show answers the record with the running, its answer taken before the +// next event writes the record. A pipeline-wanted heard before the running (H-A-585) ends the record, and the running heard +// after is applied to it: ended and delivery-time are written again from it, the class kept, record-ended said once and the +// journal told, also when that end could first not be written and the running is offered again. func TestWhatIsHeardWhileAnEventIsWrittenWaitsUntilItIsApplied(t *testing.T) { running := func(node string) stdio.Envelope { return stdio.Envelope{Key: "node-engine.running." + node + ".app", Node: node, Body: json.RawMessage( `{"v":1,"at":"2026-11-20T03:05:00Z","module":"app","node":"` + node + `","fingerprint":"f","healthy":true}`)} } + pipelineWanted := func(h *Holder) (any, error) { + return nil, take(h, stdio.Envelope{Key: "mesh-wants.pipeline-wanted.main.1", Body: json.RawMessage( + `{"v":1,"pipeline":"main.1","branch":"main","source":1,"outcome":"running"}`)}) + } for _, tc := range []struct { - name string - heard func(h *Holder) (any, error) - check func(t *testing.T, h *Holder, answer any) + name string + // before hears it before the running, a minute earlier; else it is heard as the running's write lands. + before bool + heard func(h *Holder) (any, error) + check func(t *testing.T, h *Holder, answer any, said *heardBed) }{ - {"pipeline-wanted", func(h *Holder) (any, error) { - return nil, take(h, stdio.Envelope{Key: "mesh-wants.pipeline-wanted.main.1", Body: json.RawMessage( - `{"v":1,"pipeline":"main.1","branch":"main","source":1,"outcome":"running"}`)}) - }, func(t *testing.T, h *Holder, _ any) { + {"pipeline-wanted", false, pipelineWanted, func(t *testing.T, h *Holder, _ any, said *heardBed) { r, err := h.Records.ByKey("main", 1) - if err != nil || r == nil || r.Ended == nil || r.DeliveryTime != "5m0s" || r.Class != catalogue.ClassCore { - t.Fatalf("the record ended %+v keeping the delivery time %q and the class %q (%v); want 5m0s and core", r.Ended, - r.DeliveryTime, r.Class, err) + if err != nil || r == nil || r.Ended == nil || r.DeliveryTime != "5m0s" || r.Class != catalogue.ClassCore || said.ended != 1 { + t.Fatalf("the record ended %+v keeping the delivery time %q and the class %q, said %d times (%v); want 5m0s and core, once", + r.Ended, r.DeliveryTime, r.Class, said.ended, err) } }}, - {"the call show", func(h *Holder) (any, error) { + {"the call show", false, func(h *Holder) (any, error) { for _, tool := range tools(h, &listening{}) { if tool.Name == Seat+".show" { return tool.Run(map[string]any{"id": "main.1"}) } } return nil, fmt.Errorf("the gate serves no %s.show", Seat) - }, func(t *testing.T, h *Holder, answer any) { + }, func(t *testing.T, h *Holder, answer any, _ *heardBed) { if err := take(h, running("g14")); err != nil { t.Fatal(err) } @@ -173,12 +184,46 @@ func TestWhatIsHeardWhileAnEventIsWrittenWaitsUntilItIsApplied(t *testing.T) { t.Fatal(err) } }}, + {"pipeline-wanted before the running (H-A-585)", true, pipelineWanted, func(t *testing.T, h *Holder, _ any, said *heardBed) { + r, err := h.Records.ByKey("main", 1) + ran := time.Date(2026, 11, 20, 3, 6, 0, 0, time.UTC) + if err != nil || r == nil || r.Ended == nil || !r.Ended.At.Equal(ran) || r.Ended.Why != WhyAllRun || r.DeliveryTime != "6m0s" || + r.Class != catalogue.ClassCore || said.ended != 1 { + t.Fatalf("the record ended %+v keeping the delivery time %q and the class %q, said %d times (%v); want ended at the "+ + "running, 6m0s and core, said once", r.Ended, r.DeliveryTime, r.Class, said.ended, err) + } + if o := r.Outcomes[OutcomeKey("app", "f", "ace", StepRunning)]; o.Outcome != OutcomeRunning { + t.Fatalf("the running after the end is %+v", o) + } + var lines []string + for _, l := range said.journal { + if strings.Contains(l, "after it ended") { + lines = append(lines, l) + } + } + if len(lines) != 1 || !strings.Contains(lines[0], r.Instance) || !strings.Contains(lines[0], "app on ace") || + !strings.Contains(lines[0], "03:05:00Z") || !strings.Contains(lines[0], "03:06:00Z") { + t.Fatalf("the journal says %q: want one line naming the record, the module, the machine and both times", said.journal) + } + // The ledger holds what memory does. + back, err := h.Records.Read(r.Instance) + if err != nil || back.Ended == nil || !back.Ended.At.Equal(ran) || back.DeliveryTime != "6m0s" || back.Class != catalogue.ClassCore { + t.Fatalf("the ledger keeps %+v, %q, %q (%v)", back.Ended, back.DeliveryTime, back.Class, err) + } + }}, } { t.Run(tc.name, func(t *testing.T) { now := time.Date(2026, 11, 20, 3, 0, 0, 0, time.UTC) + said := &heardBed{} h := &Holder{Now: func() time.Time { return now }} ledger := &hookedLedger{ledgerFake: newLedgerFake(t)} - h.Records = &Records{Ledger: ledger, Now: h.Now, Class: func(string) (string, error) { return catalogue.ClassCore, nil }} + h.Records = &Records{Ledger: ledger, Now: h.Now, Class: func(string) (string, error) { return catalogue.ClassCore, nil }, + Say: func(subject string, _ any, _ string) error { + if strings.Contains(subject, "record-ended") { + said.ended++ + } + return nil + }, Logf: func(format string, a ...any) { said.journal = append(said.journal, fmt.Sprintf(format, a...)) }} h.Consumer = &StepConsumer{Records: h.Records, Families: stepFamilies, Now: h.Now, Taken: h.TakenIn, Follow: h.Follow} if _, _, err := h.Records.Open(opening("main", 1, map[string]string{"novox/app@5": strings.Repeat("a", 40)})); err != nil { t.Fatal(err) @@ -196,20 +241,33 @@ func TestWhatIsHeardWhileAnEventIsWrittenWaitsUntilItIsApplied(t *testing.T) { err error } heard := make(chan heardOut, 1) - ledger.after = func(_, operation string, body any) { - if o, ok := body.(RecordOutcome); !ok || operation != OpOutcome || o.Step != StepRunning { - return + if tc.before { + answer, err := tc.heard(h) + heard <- heardOut{answer, err} + now = now.Add(time.Minute) + // The end written again from the running is refused at first: the running is offered again (the take below), + // and the end is written then. + ledger.failOn = OpEnd + if err := take(h, running("ace")); err == nil { + t.Fatal("a running whose end could not be written again is taken") } - ledger.after = nil - // Heard as the running's write lands: taken there and then when nothing holds the records, as the runtime's - // goroutines allow, else on a goroutine of its own, as the runtime hands it. - hear := func() { answer, err := tc.heard(h); heard <- heardOut{answer, err} } - if h.applying.TryLock() { - h.applying.Unlock() - hear() - return + ledger.failOn = "" + } else { + ledger.after = func(_, operation string, body any) { + if o, ok := body.(RecordOutcome); !ok || operation != OpOutcome || o.Step != StepRunning { + return + } + ledger.after = nil + // Heard as the running's write lands: taken there and then when nothing holds the records, as the + // runtime's goroutines allow, else on a goroutine of its own, as the runtime hands it. + hear := func() { answer, err := tc.heard(h); heard <- heardOut{answer, err} } + if h.applying.TryLock() { + h.applying.Unlock() + hear() + return + } + go hear() } - go hear() } if err := take(h, running("ace")); err != nil { t.Fatal(err) @@ -218,7 +276,7 @@ func TestWhatIsHeardWhileAnEventIsWrittenWaitsUntilItIsApplied(t *testing.T) { if out.err != nil { t.Fatal(out.err) } - tc.check(t, h, out.answer) + tc.check(t, h, out.answer, said) }) } }