mesh-delivery-gate: apply each event and call whole, one at a time, so no record loses its delivery time (issue 721) #749

Merged
mesh-tools merged 2 commits from fix/gate-records-race into main 2026-11-23 14:54:31 +00:00
4 changed files with 158 additions and 7 deletions
@@ -169,6 +169,11 @@ type testLog struct{ t *testing.T }
func (w testLog) Write(p []byte) (int, error) {
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
// to one record lost its delivery time).
if strings.Contains(string(p), "WARNING: DATA RACE") {
w.t.Error("the bundle reports a data race, above")
}
return len(p), nil
}
@@ -42,6 +42,12 @@ type Holder struct {
refusals []string
// tried counts the owed effects tried since this owner started, by how each ended (novox/hq issue 407).
tried owedTries
// applying keeps one event or tool call at a time on the delivery records the gate holds in memory (issue 721 note 11):
// the runtime hands each event and each call to a goroutine of its own, and each is applied whole under this lock (take,
// applied), so none reads a record another is halfway through writing. A record's entry is written to the ledger and then
// to memory under it, so an event heard once that write lands (a pipeline-wanted after the running it ends on) is applied
// only after memory holds it too. Taken before h.mu, never under it.
applying sync.Mutex
// reactMu keeps one React at a time; poke asks for a look (react.go); wanted is when the next is wanted, and its timer.
reactMu sync.Mutex
poke chan struct{}
@@ -7,6 +7,7 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
@@ -125,7 +126,33 @@ func handle(h *Holder, e stdio.Envelope) error {
return taken
}
// applied is a call applied whole, one at a time with the events (Holder.applying), its answer taken under the same lock: a
// view of a record the gate keeps is written out before the next event writes that record. A verb that changes a delivery
// asks for a look at what it makes possible (react.go).
func (h *Holder) applied(run func(map[string]any) (any, error)) func(map[string]any) (any, error) {
return func(a map[string]any) (any, error) {
out, err := func() (any, error) {
h.applying.Lock()
defer h.applying.Unlock()
out, err := run(a)
if err != nil || out == nil {
return out, err
}
raw, err := json.Marshal(out)
if err != nil {
return nil, err
}
return json.RawMessage(raw), nil
}()
h.Poke()
return out, err
}
}
// take applies one event whole, one at a time (Holder.applying).
func take(h *Holder, e stdio.Envelope) error {
h.applying.Lock()
defer h.applying.Unlock()
if h.Consumer != nil && h.Consumer.Takes(e.Key) {
return h.Consumer.Hear(e)
}
@@ -265,14 +292,9 @@ func tools(h *Holder, l *listening) []stdio.Tool {
list = append(list, headTools(h, meshhandactsclient.Recorder{Ask: stdio.Ask})...)
// The prune of heads by hand, now or kept from it (design 47 H2 §812, H-A-380 (3)).
list = append(list, headsPruneTools(meshhandactsclient.Recorder{Ask: stdio.Ask})...)
// A verb that changes a delivery asks for a look at what it makes possible (react.go).
// Each call is applied whole, one at a time with the events, and asks for a look after (applied).
for i := range list {
run := list[i].Run
list[i].Run = func(a map[string]any) (any, error) {
out, err := run(a)
h.Poke()
return out, err
}
list[i].Run = h.applied(list[i].Run)
}
return list
}
@@ -2,11 +2,14 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"strings"
"testing"
"time"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/catalogue"
)
// pipelineBed is a gate with one record open, of main at source revision 1, and its consumer.
@@ -105,6 +108,121 @@ func TestARecordFollowsItsPipelineToTheMachines(t *testing.T) {
}
}
// hookedLedger is the ledger fake, told after each operation it took.
type hookedLedger struct {
*ledgerFake
after func(instance, operation string, body any)
}
func (l *hookedLedger) Apply(instance, operation string, body any) (uint64, error) {
seq, err := l.ledgerFake.Apply(instance, operation, body)
if err == nil && l.after != nil {
l.after(instance, operation, body)
}
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.
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}`)}
}
for _, tc := range []struct {
name string
heard func(h *Holder) (any, error)
check func(t *testing.T, h *Holder, answer any)
}{
{"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) {
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)
}
}},
{"the call show", 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) {
if err := take(h, running("g14")); err != nil {
t.Fatal(err)
}
raw, _ := json.Marshal(answer)
if !strings.Contains(string(raw), OutcomeKey("app", "f", "ace", StepRunning)) || strings.Contains(string(raw), "g14") {
t.Fatalf("show answered %.600s: want the running on ace, and nothing the next event wrote", raw)
}
// Every call runs under the lock the events take.
if _, err := h.applied(func(map[string]any) (any, error) {
if h.applying.TryLock() {
h.applying.Unlock()
return nil, errors.New("a call runs while an event can write the records")
}
return nil, nil
})(nil); err != nil {
t.Fatal(err)
}
}},
} {
t.Run(tc.name, func(t *testing.T) {
now := time.Date(2026, 11, 20, 3, 0, 0, 0, time.UTC)
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.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)
}
if _, err := h.Records.Kept(); err != nil {
t.Fatal(err)
}
if err := take(h, stdio.Envelope{Key: "mesh-pipeline-runner.pipeline-started.main.1", Body: json.RawMessage(
`{"v":1,"branch":"main","source":1,"pipeline":"main.1","call":"0000000000000001","movers":[{"module":"app","commit":"c","fingerprint":"f","repository":"novox/app","tier":1}]}`)}); err != nil {
t.Fatal(err)
}
now = now.Add(5 * time.Minute)
type heardOut struct {
answer any
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
}
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()
}
if err := take(h, running("ace")); err != nil {
t.Fatal(err)
}
out := <-heard
if out.err != nil {
t.Fatal(out.err)
}
tc.check(t, h, out.answer)
})
}
}
// GATE-5/AC (design 47 H §738, §751; H-A-75, H-A-89 (3), (4)): a pipeline-ended not passed ends the record failed naming
// the pipeline's why, and pipeline-wanted tier-late ends it failed naming the movers and machines not running.
func TestARecordEndsFailedOnAPipelineNotPassedOrATierLate(t *testing.T) {