Files
mesh-catalog/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go
T
jschoubben cc1140ba36 mesh-delivery-gate: apply each event and call whole, one at a time, so no record loses its delivery time
The runtime hands each event and each tool call to a goroutine of its own (mesh-sdk stdio), and the gate
applied them to the same in-memory records with no lock: Records.Outcome writes the ledger and only then
memory, so a pipeline-wanted heard once a running's outcome was in the ledger ended the record before
memory held that running, and timesAtEnd found no running: the record ended with no delivery time and
no class (main.493's failure of TestAGateWhole...). The race detector names it: 9 reports in 30 runs on
main, the running's Records.Outcome (record.go:759) against pipeline-wanted's timesAtEnd (movers.go:67).

The fix, the thorough one for the kind (events and calls applied concurrently to one in-memory state):
- take applies every event under Holder.applying; the ledger write and the memory write of an entry
  happen under it, so whatever is heard after the write lands waits until memory holds it too.
- Holder.applied runs every tool call under the same lock and takes its answer (marshalled) under it,
  so a view of a kept record is never written out while the next event writes the record.
- The bus test's bundle log fails the test on a data race the bundle reports under -race, a guard for
  every bus test of the gate.
The quick fix, a lock around Records.End and Records.Outcome alone, was not taken: it leaves an event's
several writes (pipeline-ended, take-in) interleaved with another's and the calls reading half-written
records.

Test: TestWhatIsHeardWhileAnEventIsWrittenWaitsUntilItIsApplied hears pipeline-wanted, and calls show,
as the running's write lands; each of the three parts removed fails a row.

Fixes: issue 721 note 11
Builds: design 47 H2 §810
2026-11-23 15:28:11 +01:00

92 lines
3.4 KiB
Go

package main
import (
"sync"
"time"
)
// The holder of the seat mesh-delivery (novox/hq ADR 0239, to-be 47): from milestone G's delivery it keeps no merge states
// and asks no Approve per merge (design 47 G §172, §431, §592, §593, §613). What it keeps is the delivery records
// (record.go), the source revision per watched branch (merges.go) and what it owes the forge. It merges nothing.
// Seat is the seat the gate holds.
const Seat = "mesh-delivery"
// Holder is the seat's holder.
type Holder struct {
// Settings reads the operator's values (settings.go); nil keeps every default.
Settings func() Settings
Now func() time.Time
Logf func(format string, a ...any)
// Forge is the seat git's reads (sourcestart.go).
Forge ForgeReads
// Catalog reads the seat mesh-catalog's registrations (sourcestart.go); startMu keeps one start of the sources at a time.
Catalog Registered
startMu sync.Mutex
Source SourceStore
// Owed says what the gate owes the forge as the seat's event owed (owed.go).
Owed OwedSayer
// Records keeps the delivery records on the gate's ledger (record.go, ADR 0656 §7); nil keeps none.
Records *Records
// Consumer applies the step events to the records (consumer.go); nil applies none.
Consumer *StepConsumer
// SaySeat says an event of the seat mesh-delivery (recordevents.go); nil says none.
SaySeat SeatSay
// Findings is the gate's one held writer of its own findings (findings.go); nil writes none.
Findings FindingWriter
// Heads is the state heads of the watchdog (heads.go); nil watches none.
Heads HeadStore
mu sync.Mutex
started time.Time
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{}
wanted lookWanted
}
// owedTries is how many owed effects were tried since this owner started, and when the last was.
type owedTries struct {
done, failed, givenUp int
last time.Time
}
// The word this owner signs its acts with.
const byOwner = "mesh-delivery"
func (h *Holder) init() {
if h.Now == nil {
h.Now = func() time.Time { return time.Now().UTC() }
}
if h.Logf == nil {
h.Logf = func(string, ...any) {}
}
h.poke = make(chan struct{}, 1)
h.started = h.Now()
}
// Refuse keeps a refusal from outside the lock.
func (h *Holder) Refuse(err error) {
h.mu.Lock()
defer h.mu.Unlock()
h.refuse(err)
}
// refuse keeps a refusal, said, for the status; under the lock.
func (h *Holder) refuse(err error) {
h.Logf("[mesh-delivery] %v", err)
h.refusals = append(h.refusals, h.Now().Format(time.RFC3339)+" "+err.Error())
if len(h.refusals) > 50 {
h.refusals = h.refusals[len(h.refusals)-50:]
}
}