G13 indexes-kept: the gate keeps its ledger indexes by a watcher, builds an index behind again at its start before the pass of the ends, and raises index-behind and record-end-unwritten

Builds: design 47 H2 H-A-690 (2) (3) (4), H-A-634 (2) (hq 95cf2052, 999e6525); H-A-706 (DMC-13) (hq 543e6963)
This commit is contained in:
2026-11-25 17:29:28 +01:00
parent aa1a59c6bc
commit 5de68de0a0
7 changed files with 406 additions and 1 deletions
@@ -29,7 +29,7 @@ type FindingWriter interface {
// generated contract of the seat that defines it (A-17). It is started once the runtime serves (A-40).
func NewFindingWriter() (*views.HeldWriter[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent], error) {
return views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{
Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{HeadUntaken, RecordTooLarge}})
Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{HeadUntaken, RecordTooLarge, IndexBehind, RecordEndUnwritten}})
}
// recordTooLarge raises record-too-large for a delivery record's entry the gate refused over EntryLimit, its subject the
@@ -69,3 +69,75 @@ func (h *Holder) recordTooLarge(record, instance string, bytes int, raised bool)
h.Logf("[mesh-delivery] record-too-large of %s (%s) is not said: %v", instance, record, err)
}
}
// The findings of the gate's ledger indexes and of an end not written (design 47 H2 H-A-690 (3), (4)).
const (
// IndexBehind has the ledger index for its id: a call that keeps it failed, or `lag` still names it behind after its whole
// reindex; cleared at the next `lag` that names it not behind.
IndexBehind = "index-behind"
// RecordEndUnwritten has the record's instance for its id: the end of a mover or an act, or its emptying at a reopening,
// was not written; cleared when the pass for the record writes it.
RecordEndUnwritten = "record-end-unwritten"
)
// indexBehind raises index-behind for a ledger index with why, or clears it (Records.IndexBehind).
func (h *Holder) indexBehind(index, why string, behind bool) {
if h.Findings == nil {
return
}
var err error
if behind {
now := h.Now().UTC()
err = h.Findings.Write(context.Background(), meshconditions.Finding{Scope: FindingScope, ID: index, Kind: IndexBehind,
Observer: Module, Severity: meshconditions.FindingSeverityWarning, Resolver: "operator", Since: now, At: now,
Summary: fmt.Sprintf("the index %s of the delivery records is behind its history: %s", index, why),
Headline: "A delivery record index is behind", Subject: meshconditions.Subject{Scope: FindingScope, ID: index},
Explanation: "The gate keeps the index by a call of the tool runner's repair reindex at each change; it clears at the " +
"next lag that names the index not behind (the gate's next start, or after ledger_reindex by hand)."})
} else if h.heldFinding(IndexBehind, index) {
err = h.Findings.Clear(context.Background(), FindingScope, index, IndexBehind, "lag names the index not behind")
}
if err != nil && h.Logf != nil {
h.Logf("[mesh-delivery] index-behind of %s is not said: %v", index, err)
}
}
// endUnwritten raises record-end-unwritten for a record, naming the instance and the error, or clears it
// (Records.EndUnwritten).
func (h *Holder) endUnwritten(record, instance string, cause error, raised bool) {
if h.Findings == nil {
return
}
var err error
if raised {
now := h.Now().UTC()
what := record
if instance != "" {
what = instance
}
err = h.Findings.Write(context.Background(), meshconditions.Finding{Scope: FindingScope, ID: record, Kind: RecordEndUnwritten,
Observer: Module, Severity: meshconditions.FindingSeverityWarning, Resolver: "operator", Since: now, At: now,
Summary: fmt.Sprintf("the end of %s of the record %s is not written: %v", what, record, cause),
Headline: "A delivery record's end is not written to all its entries", Subject: meshconditions.Subject{Scope: FindingScope, ID: record},
Explanation: "A mover or an act of the record does not hold the record's end, so its key does not take the lifetime of the " +
"end. The gate writes it at the record's next event, or at its next start, and the finding clears when it is written."})
} else if h.heldFinding(RecordEndUnwritten, record) {
err = h.Findings.Clear(context.Background(), FindingScope, record, RecordEndUnwritten, "the end is written to every entry")
}
if err != nil && h.Logf != nil {
h.Logf("[mesh-delivery] record-end-unwritten of %s is not said: %v", record, err)
}
}
// heldFinding is whether the writer holds the finding of the kind under the id.
func (h *Holder) heldFinding(kind, id string) bool {
if h.Findings == nil {
return false
}
for _, f := range h.Findings.Held() {
if f.Scope == FindingScope && f.Kind == kind && f.ID == id {
return true
}
}
return false
}
@@ -66,6 +66,8 @@ func main() {
h.Records = &Records{Ledger: sdkLedger{}, Now: h.Now, Say: stdio.SeatPublish, Logf: logf, TooLarge: h.recordTooLarge,
Class: seatCatalog{ask: stdio.Ask}.Class}
h.Records.Changed = h.recordChanged
h.Records.IndexBehind, h.Records.EndUnwritten = h.indexBehind, h.endUnwritten
h.Records.EndPending = func(record string) bool { return h.heldFinding(RecordEndUnwritten, record) }
var daysErr error
if h.Records.KeepingDays, h.Records.EndedDays, daysErr = ownmanifest.RecordsDays(); daysErr != nil {
logf("%v", daysErr)
@@ -105,6 +107,16 @@ func main() {
// tick, no forge poll, no reconcile loop. Each step is retried, each failure said, never given up on quietly.
func run(h *Holder, l *listening) {
<-stdio.Served() // Serve first: the state is reached through it, and an event is heard from as soon as it serves
// The indexes first: built again where behind, each record's movers and acts given its end, then watched (H-A-690 (2)).
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
err := h.Records.KeepIndexes()
if err == nil {
break
}
l.set("not yet: " + err.Error())
logf("[mesh-delivery] the record indexes are not kept yet (%v); asking again in %s", err, wait)
time.Sleep(wait)
}
go h.Work()
for _, pattern := range followed {
pattern := pattern
@@ -271,6 +271,11 @@ type RecordLedger interface {
Expires(instance string) (*time.Time, error)
// Remove removes a record with its operations and its snapshot through the tool runner's ledger removal.
Remove(instance string) error
// Reindex builds a declared index again from the history: of the one instance only, or whole when instance is empty
// (buscalls.LedgerReindex, design 47 H2 H-A-690 (1)); the entries it wrote.
Reindex(index, instance string) (int, error)
// Lag is every instance and index behind its history (buscalls.LedgerLag).
Lag() (buscalls.Lagged, error)
// Query is the instances a declared index names under key (`>` for the rest).
Query(index, key string) ([]string, error)
// Watch hands handle the current state of each instance `instances` names (an instance, `*` for one name, `>` for the
@@ -326,6 +331,32 @@ func (sdkLedger) Remove(instance string) error {
return nil
}
// Reindex is the tool runner's repair `reindex` of the gate's own ledger.
func (sdkLedger) Reindex(index, instance string) (int, error) {
out, err := ledgerCall(buscalls.LedgerReindex, buscalls.ReindexRequest{Ledger: RecordsLedger, Index: index, Instance: instance})
if err != nil {
return 0, err
}
var got buscalls.Repaired
if err := json.Unmarshal(out, &got); err != nil {
return 0, fmt.Errorf("the ledger's answer to the reindex of %s is not readable: %w", index, err)
}
return got.Written, nil
}
// Lag is the tool runner's `lag` of the gate's own ledger.
func (sdkLedger) Lag() (buscalls.Lagged, error) {
var got buscalls.Lagged
out, err := ledgerCall(buscalls.LedgerLag, buscalls.LedgerRequest{Ledger: RecordsLedger})
if err != nil {
return got, err
}
if err := json.Unmarshal(out, &got); err != nil {
return got, fmt.Errorf("the ledger's answer to lag is not readable: %w", err)
}
return got, nil
}
func (sdkLedger) Query(index, key string) ([]string, error) {
out, err := ledgerCall(buscalls.LedgerQuery, buscalls.QueryRequest{Ledger: RecordsLedger, Index: index, Key: key})
if err != nil {
@@ -396,6 +427,12 @@ type Records struct {
// KeepingDays and EndedDays are the ledger's `max-age-days` and `ended-days` its manifest gives, said by the read tools
// (design 47 H2 H-A-634 (7)); zero says none.
KeepingDays, EndedDays int
// IndexBehind raises (behind) or clears the finding index-behind for a ledger index, with why (design 47 H2 H-A-690 (3));
// nil says none. EndUnwritten raises (raised) or clears record-end-unwritten for a record, naming the instance and the
// error (H-A-690 (4)); EndPending says whether the record holds one, so its next event finishes it; nil: none held.
IndexBehind func(index, why string, behind bool)
EndUnwritten func(record, instance string, err error, raised bool)
EndPending func(record string) bool
// held are the records kept, by instance: the current states of a watch of the ledger's `record/>` at the first read,
// then as the gate writes them (the gate is their one writer); a key the watch hands gone is dropped, and remembered in
@@ -680,6 +717,15 @@ func (rs *Records) ByKey(branch string, source int64) (*Record, error) {
// the gate keeps as it writes it when it holds it, so a write through it is seen by every reader, else read from the
// ledger; nil when none is kept.
func (rs *Records) ByMilestone(milestone string) (*Record, error) {
r, err := rs.byMilestone(milestone)
if r != nil && err == nil && rs.EndPending != nil && rs.EndPending(r.Instance) {
// The next event of a record whose end was not written in full finishes it, by the pass for it alone (H-A-690 (4)).
_, _ = rs.followRecord(r)
}
return r, err
}
func (rs *Records) byMilestone(milestone string) (*Record, error) {
inst, err := RecordInstance(milestone)
if err != nil {
return nil, err
@@ -36,6 +36,13 @@ type ledgerFake struct {
failOn string
// fill is what Fill answers; nil, the ledger says no fill.
fill *buscalls.Fill
// lagged is what Lag answers; a whole Reindex of an index takes it out unless stuck names it. calls is every reindex and every
// `end` applied, in order; reindexErr fails every Reindex; endErr fails the `end` of an instance.
lagged buscalls.Lagged
stuck string
calls []string
reindexErr error
endErr string
// watchers are the watches the gate started, each told of every change to an instance its pattern names.
watchers []ledgerWatch
}
@@ -112,10 +119,36 @@ func newLedgerFake(t *testing.T) *ledgerFake {
return f
}
// Reindex records the call; a whole one makes the index not behind, unless it is stuck.
func (f *ledgerFake) Reindex(index, instance string) (int, error) {
f.calls = append(f.calls, "reindex "+index+" "+instance)
if f.reindexErr != nil {
return 0, f.reindexErr
}
if instance == "" && index != f.stuck {
var keep []buscalls.IndexLag
for _, l := range f.lagged.Indexes {
if l.Index != index {
keep = append(keep, l)
}
}
f.lagged.Indexes = keep
}
return 3, nil
}
func (f *ledgerFake) Lag() (buscalls.Lagged, error) { return f.lagged, nil }
func (f *ledgerFake) Apply(instance, operation string, body any) (uint64, error) {
if f.failOn == operation {
return 0, fmt.Errorf("the ledger is away")
}
if operation == OpEnd {
f.calls = append(f.calls, "end "+instance)
if f.endErr == instance {
return 0, fmt.Errorf("the ledger did not take the end of %s", instance)
}
}
ty, ok := f.types[strings.SplitN(instance, "/", 2)[0]]
if !ok {
return 0, fmt.Errorf("no type for %s", instance)
@@ -1319,3 +1352,121 @@ func opening(branch string, source int64, heads map[string]string) RecordOpening
}
func ptrTime(t time.Time) *time.Time { return &t }
// H-A-690 (2) to (4): the gate keeps its ledger's indexes by a watcher, builds an index behind again whole at its start before the
// pass of the ends, says what fails as index-behind and record-end-unwritten, and finishes an end at the record's next event.
func TestTheGateKeepsItsLedgerIndexesByAWatcher(t *testing.T) {
at := time.Date(2026, 11, 25, 12, 0, 0, 0, time.UTC)
heads := map[string]string{"novox/app@111111111111": strings.Repeat("1", 40)}
setup := func(t *testing.T) (*Records, *Holder, *ledgerFake, *Record, string) {
l := newLedgerFake(t)
h := &Holder{Findings: &findingsFake{held: map[string]meshconditions.Finding{}}, Now: func() time.Time { return at }}
rs := &Records{Ledger: l, Now: func() time.Time { return at }, IndexBehind: h.indexBehind, EndUnwritten: h.endUnwritten,
EndPending: func(record string) bool { return h.heldFinding(RecordEndUnwritten, record) }}
r, _, err := rs.Open(opening("main", 47, heads))
if err != nil {
t.Fatal(err)
}
if err := rs.SetMover(r, "mesh-x", "src2:f1", "main.47", "0.1.0"); err != nil {
t.Fatal(err)
}
inst, _ := MoverInstance(r.Milestone, "mesh-x")
return rs, h, l, r, inst
}
count := func(l *ledgerFake, call string) (n int) {
for _, c := range l.calls {
if c == call {
n++
}
}
return n
}
t.Run("a change the watch hands makes one reindex naming it, and the current states none", func(t *testing.T) {
rs, _, l, r, mover := setup(t)
if err := rs.KeepIndexes(); err != nil {
t.Fatal(err)
}
if len(l.calls) != 0 {
t.Fatalf("the start called %v", l.calls)
}
if err := rs.SetMover(r, "mesh-x", "src2:f1", "main.48", "0.1.1"); err != nil {
t.Fatal(err)
}
l.change(mover)
if got := count(l, "reindex movers "+mover); got != 1 || len(l.calls) != 1 {
t.Fatalf("calls %v", l.calls)
}
l.states[mover] = json.RawMessage("null")
l.change(mover) // a key gone by its lifetime needs no call
if len(l.calls) != 1 {
t.Fatalf("a key gone called %v", l.calls)
}
})
t.Run("an index behind is built again whole before the pass of the ends, and still behind it is raised", func(t *testing.T) {
rs, h, l, r, mover := setup(t)
if err := rs.End(r, "done", "x"); err != nil {
t.Fatal(err)
}
if _, err := l.Apply(mover, OpEnd, map[string]any{"ended": nil}); err != nil { // the mover lost its end
t.Fatal(err)
}
l.calls = nil
l.lagged = buscalls.Lagged{Indexes: []buscalls.IndexLag{{Index: MoversIndex, Behind: 5}}}
if err := rs.KeepIndexes(); err != nil {
t.Fatal(err)
}
if len(l.calls) < 2 || l.calls[0] != "reindex movers " || l.calls[1] != "end "+mover {
t.Fatalf("the start called %v", l.calls)
}
if h.heldFinding(IndexBehind, MoversIndex) {
t.Fatal("an index built again is still raised")
}
l.lagged = buscalls.Lagged{Indexes: []buscalls.IndexLag{{Index: MoversIndex, Behind: 5}}}
l.stuck = MoversIndex
if err := rs.KeepIndexes(); err != nil {
t.Fatal(err)
}
if !h.heldFinding(IndexBehind, MoversIndex) {
t.Fatal("an index still behind after its whole reindex is not raised")
}
l.stuck, l.lagged = "", buscalls.Lagged{}
if err := rs.KeepIndexes(); err != nil || h.heldFinding(IndexBehind, MoversIndex) {
t.Fatalf("lag naming it not behind did not clear it: %v", err)
}
})
t.Run("a failed call raises index-behind and is made again at the instance's next change", func(t *testing.T) {
rs, h, l, _, mover := setup(t)
if err := rs.KeepIndexes(); err != nil {
t.Fatal(err)
}
l.reindexErr = errors.New("the tool runner is away")
l.change(mover)
if !h.heldFinding(IndexBehind, MoversIndex) {
t.Fatal("a failed reindex is not raised")
}
l.reindexErr = nil
l.change(mover)
if got := count(l, "reindex movers "+mover); got != 2 {
t.Fatalf("calls %v", l.calls)
}
})
t.Run("a failed end raises record-end-unwritten, and the record's next event writes it and clears it", func(t *testing.T) {
rs, h, l, r, mover := setup(t)
l.endErr = mover
if err := rs.End(r, "done", "x"); err != nil {
t.Fatal(err) // the record's end stands
}
if !h.heldFinding(RecordEndUnwritten, r.Instance) || r.Ended == nil {
t.Fatal("a failed end of an entry is not raised")
}
l.endErr = ""
if got, err := rs.ByMilestone(r.Milestone); err != nil || got == nil {
t.Fatalf("%v %v", got, err)
}
var st struct{ Ended *RecordEnded }
_ = json.Unmarshal(l.states[mover], &st)
if st.Ended == nil || h.heldFinding(RecordEndUnwritten, r.Instance) {
t.Fatalf("the next event did not write the end (%v) or clear the finding", st.Ended)
}
})
}
@@ -195,6 +195,17 @@ func sameEnded(a, b *RecordEnded) bool {
// they hold another, and takes it from those of an open record, by their `end` (H-A-634 (2), (7)): the instances it wrote. It
// stops at the first write that fails, naming it.
func (rs *Records) followRecord(r *Record) ([]string, error) {
written, err := rs.followEnds(r)
if rs.EndUnwritten != nil {
rs.EndUnwritten(r.Instance, "", err, err != nil)
}
if err != nil {
rs.log("the record %s: its movers and acts do not all hold its end: %v", r.Instance, err)
}
return written, err
}
func (rs *Records) followEnds(r *Record) ([]string, error) {
var written []string
for _, index := range []string{MoversIndex, ActsIndex} {
insts, err := rs.Ledger.Query(index, moversKey(r.Milestone))
@@ -243,6 +243,9 @@ func (rs *Records) endEntries(r *Record, e *RecordEnded) {
if rs.Logf != nil {
rs.Logf("[mesh-delivery] the record %s: the end of its entry %s is not written: %v", r.Instance, instance, err)
}
if rs.EndUnwritten != nil {
rs.EndUnwritten(r.Instance, instance, err, true)
}
}
modules := make([]string, 0, len(r.movers))
for module := range r.movers {
@@ -0,0 +1,110 @@
package main
// The gate keeps the ledger's indexes `movers` and `acts` by a watcher of its own process (design 47 H2 H-A-690 (2) to (4); ADR
// 0597 §4): the tool runner stays the one writer of index keys, the gate asks it, by the repair `reindex`, for each instance
// that changed; at its start it reads `lag`, builds again whole each index behind, and gives every record's movers and acts its
// end before it follows the changes.
import (
"encoding/json"
"fmt"
"sync/atomic"
"git.novox.be/novox/mesh-sdk/go/buscalls"
)
// KeepIndexes is the gate's start of its indexes: `lag`, a whole `reindex` of each index it names behind (said with the count
// written), the finding index-behind for an index still behind after it and its clearing for one not, the pass of the ends over
// every record, then a watch of the ledger's `mover/>` and `act/>` that makes one `reindex` naming each instance a change
// hands after the current states. What fails is said and made again at the next change or the next start, never given up.
func (rs *Records) KeepIndexes() error {
rs.reindexBehind()
list, err := rs.Kept()
if err != nil {
return err
}
for _, r := range list {
if _, err := rs.followRecord(r); err != nil {
continue // said, and the finding raised, by the pass
}
}
var caught atomic.Bool
watch := func(index string) func(string, json.RawMessage) error {
return func(instance string, state json.RawMessage) error {
// The current states are not changes; a key gone by its lifetime (or removed) has no index key left to write.
if !caught.Load() || len(state) == 0 || string(state) == "null" {
return nil
}
rs.reindexInstance(index, instance)
return nil
}
}
for _, w := range []struct{ prefix, index string }{{TypeMover + "/>", MoversIndex}, {TypeAct + "/>", ActsIndex}} {
if err := rs.Ledger.Watch(w.prefix, watch(w.index)); err != nil {
return fmt.Errorf("the ledger's %s cannot be watched: %w", w.prefix, err)
}
}
caught.Store(true)
return nil
}
// reindexInstance asks the tool runner to write one instance's entries of an index again; a call that fails is said and
// raises index-behind, and is made again at that instance's next change (H-A-690 (2), (3)).
func (rs *Records) reindexInstance(index, instance string) {
if _, err := rs.Ledger.Reindex(index, instance); err != nil {
rs.log("the index %s is not kept for %s: %v", index, instance, err)
if rs.IndexBehind != nil {
rs.IndexBehind(index, fmt.Sprintf("the reindex of %s failed: %v", instance, err), true)
}
}
}
// reindexBehind reads `lag` and builds again whole each index it names behind; an index still behind after it is raised as
// index-behind, one `lag` does not name is cleared (H-A-690 (2), (3)).
func (rs *Records) reindexBehind() {
lag, err := rs.Ledger.Lag()
if err != nil {
rs.log("the ledger's lag cannot be read, so no index is built again at this start: %v", err)
return
}
for _, index := range []string{MoversIndex, ActsIndex} {
if !behind(lag, index) {
if rs.IndexBehind != nil {
rs.IndexBehind(index, "", false)
}
continue
}
n, err := rs.Ledger.Reindex(index, "")
if err != nil {
rs.log("the index %s is behind and its reindex failed: %v", index, err)
if rs.IndexBehind != nil {
rs.IndexBehind(index, fmt.Sprintf("the whole reindex failed: %v", err), true)
}
continue
}
rs.log("the index %s was behind its history and is built again: %d entries written", index, n)
again, err := rs.Ledger.Lag()
switch {
case err != nil:
rs.log("the ledger's lag cannot be read after the reindex of %s: %v", index, err)
case behind(again, index):
if rs.IndexBehind != nil {
rs.IndexBehind(index, fmt.Sprintf("still behind its history after a whole reindex (%d entries written)", n), true)
}
default:
if rs.IndexBehind != nil {
rs.IndexBehind(index, "", false)
}
}
}
}
// behind is whether lag names the index behind its history.
func behind(l buscalls.Lagged, index string) bool {
for _, i := range l.Indexes {
if i.Index == index && i.Behind > 0 {
return true
}
}
return false
}