A12 start-says-again: a start says wait-ended for an ending record, wait-held for every standing wait, a removal from its mark, then wanted for each want written with the key's write time as its at

Builds: design 47 H2b §832 4.
This commit is contained in:
2026-11-27 03:25:20 +01:00
parent 3089814382
commit f9232d5e60
7 changed files with 342 additions and 9 deletions
@@ -365,8 +365,10 @@ func (p *passer) capabilityDigests() []meshwants.AssignmentRevisionCapabilitiesI
}
// sayWanted says `wanted` for a want written or removed now, from the revision the pass keeps (A-61, A-67); with none kept, it is
// said in the log that it was not said. fp is the want's, and for a removal the fingerprint last wanted there.
func (p *passer) sayWanted(node, module, fp, pipeline string, removed bool, wantRevision uint64) {
// said in the log that it was not said. fp is the want's, and for a removal the fingerprint last wanted there. at is the want key's
// write time when the event is said again after a restart (design 47 H2b H-A-859), zero for a say made with the write; taken runs
// once the bus has taken it (a removal's mark goes then, §832 4.).
func (p *passer) sayWanted(node, module, fp, pipeline string, removed bool, wantRevision uint64, at time.Time, taken func()) {
if p.publishWants == nil {
return
}
@@ -381,11 +383,14 @@ func (p *passer) sayWanted(node, module, fp, pipeline string, removed bool, want
p.logf("the want of %s on %s is written, but no assignment graph revision is kept, so wanted is not said", module, node)
return
}
at := p.clock().UTC()
if at.IsZero() {
at = p.clock()
}
at = at.UTC()
body := meshwants.WantedEvent{V: keys.Version, Node: node, Module: module, Fp: fp, Pipeline: pipeline, Removed: removed, Revision: kept.Revision, Source: source, At: &at, WantRevision: int64(wantRevision)}
event := strings.TrimPrefix(meshwants.SubjectWanted(keys.EscapeName(node), keys.EscapeName(module)), "mesh.seat."+meshwants.Seat+".event.")
id := fmt.Sprintf("wanted.%s.%s.%s.%t.%d", keys.EscapeName(node), keys.EscapeName(module), fp, removed, wantRevision)
p.wantsOwe("want."+keys.EscapeName(node)+"."+keys.EscapeName(module), event, body, id, nil)
p.wantsOwe("want."+keys.EscapeName(node)+"."+keys.EscapeName(module), event, body, id, taken)
}
// namedBuild answers what a want of module at fp names of its build (design 47 G §379, §526, A-96): a kept build's or another watched
@@ -125,6 +125,105 @@ func TestTheWantsAreSaidWantedFromTheKeptAssignmentGraphRevision(t *testing.T) {
t.Errorf("the removal said %+v under %s", last, events[3].id)
}
})
// Design 47 H2b §832 4. (H-A-843, H-A-859; 1984/AC-832e, AC-832f, AC-832r, AC-832w): a start says again what the kept state implies
// under the same ids; a removal is marked before it is written and said, and the mark is said from at a start; a `wanted` said
// again carries the want key's write time as its `at`.
t.Run("a start says again the wants written and the removals marked, under the same ids", func(t *testing.T) {
first := time.Date(2026, 11, 27, 5, 0, 0, 0, time.UTC)
build := func(kv *memKV, ends *memEnds, now time.Time) (*passer, *[]said) {
q := newPasser(kvSet{buckets.Wants(): kv}, now)
q.now = func() time.Time { return now }
q.logf = t.Logf
var out []said
q.publishWants = func(event string, body any, id string) error {
raw, _ := json.Marshal(body)
out = append(out, said{event, id, raw})
return nil
}
q.graph = &graphKeeper{objects: memObjects{}, halves: func() map[string]mcc.Half { return map[string]mcc.Half{trunkBranch: half} }}
q.ends = newPipelineEnds(ends, func() {}, t.Logf)
q.keepGraph(ctx)
out = nil
return q, &out
}
// the process before the restart: the want written, said, then a removal the bus refuses
kv, ends := &memKV{values: map[string][]byte{}}, newMemEnds()
q, out := build(kv, ends, first)
q.putWant(ctx, "ace", "web", mwc.Want{Fp: fpTest, Self: true})
q.putWant(ctx, "ace", "keep", mwc.Want{Fp: fpTest, Self: true})
keepID := (*out)[1].id
q.publishWants = func(string, any, string) error { return errors.New("the bus refuses") }
q.putWant(ctx, "ace", "web", mwc.Want{Removed: true})
if raw, _, _ := ends.get(removalKey("ace", "web")); raw == nil {
t.Fatal("AC-832f: the removal has no mark")
}
// AC-832r: assigner_written answers the mark
written := writtenTools(memStore{writtenState: &memKV{values: map[string][]byte{removalKey("ace", "web"): ends.vals[removalKey("ace", "web")]}}})
if got, err := written[0].Run(map[string]any{}); err != nil || got.(map[string]any)[removalKey("ace", "web")] == nil {
t.Errorf("AC-832r: assigner_written answered %v (%v)", got, err)
}
// the restart: the kept state is the want keys and the mark
restart := func(now time.Time) (*passer, *[]said) {
r, out := build(kv, ends, now)
for k, v := range kv.values {
r.w.putEntry(buckets.Wants(), k, v, kv.revision(k), first)
}
return r, out
}
r, out2 := restart(first.Add(time.Hour))
r.startSays(ctx)
var removal, again *said
for i, e := range *out2 {
var b meshwants.WantedEvent
_ = json.Unmarshal(e.body, &b)
switch {
case b.Module == "web" && b.Removed:
removal = &(*out2)[i]
case b.Module == "keep":
again = &(*out2)[i]
}
}
if removal == nil || (*out2)[len(*out2)-1].event == "" || again == nil {
t.Fatalf("AC-832e: the start said %v", *out2)
}
var b meshwants.WantedEvent
_ = json.Unmarshal(again.body, &b)
if again.id != keepID || b.At == nil || !b.At.Equal(first) || b.WantRevision == 0 {
t.Errorf("AC-832w: the want said again is %s %+v, want the key's write time %v as its at", again.id, b, first)
}
if raw, _, _ := ends.get(removalKey("ace", "web")); raw != nil {
t.Errorf("AC-832f: the mark stands once the removal was taken")
}
webKey, _ := mwc.WantKey("ace", "web")
if w, _ := mwc.DecodeWant(kv.values[webKey]); !w.Removed {
t.Errorf("AC-832f: the want's key holds %+v, want the removing want", w)
}
// the same ids again at the next start; the removal's mark is gone, so only the want written is said
r2, out3 := restart(first.Add(2 * time.Hour))
r2.startSays(ctx)
ids := map[string]bool{}
for _, e := range *out3 {
ids[e.id] = true
}
if !ids[again.id] || len(*out3) != 1 {
t.Errorf("AC-832e: the second start said %v, want the want written under %s", *out3, again.id)
}
// a crash between the mark and the removing want: the key still holds the revision the mark names, so the start writes it first
kv2, ends2 := &memKV{values: map[string][]byte{}}, newMemEnds()
c, _ := build(kv2, ends2, first)
c.putWant(ctx, "ace", "web", mwc.Want{Fp: fpTest, Self: true})
cKey, _ := mwc.WantKey("ace", "web")
mark, _ := json.Marshal(removalMark{Node: "ace", Module: "web", Fp: fpTest, WantRevision: kv2.revision(cKey)})
ends2.vals[removalKey("ace", "web")], ends2.revs[removalKey("ace", "web")] = mark, 1
d, out4 := build(kv2, ends2, first.Add(time.Hour))
for k, v := range kv2.values {
d.w.putEntry(buckets.Wants(), k, v, kv2.revision(k), first)
}
d.startSays(ctx)
if w, _ := mwc.DecodeWant(kv2.values[cKey]); !w.Removed || len(*out4) != 1 || !strings.HasSuffix((*out4)[0].id, ".true."+strconv.FormatUint(kv2.revision(cKey)-1, 10)) {
t.Errorf("AC-832f: a mark whose want still stands left %+v and said %v", w, *out4)
}
})
t.Run("a want's run", func(t *testing.T) {
wkey, _ := mwc.WantKey("g14", "db")
stored := func() mwc.Want { w, _ := mwc.DecodeWant(kv.values[wkey]); return w }
@@ -419,6 +419,30 @@ func TestTheWaitOnAnOutsideCause(t *testing.T) {
t.Errorf("AC-831d: after the restart the record is %+v, ask %v, ended %d", w, r.p.w.value(buckets.Asks(), askKey) != nil, ended(r))
}
})
// Design 47 H2b §832 4. (1984/AC-832e): a start says `wait-held` again for every standing wait under its held-said, and no
// `wait-held` for a settle or a record with `ending`.
t.Run("a start says wait-held again for a standing wait under the id first said (§832 4.)", func(t *testing.T) {
r := newOutsideRig(t)
r.cond("machine.hub.unreachable", "ace")
r.review(false, nil)
if len(r.ids) != 1 {
t.Fatalf("ids %v", r.ids)
}
r.p.owed.list = nil // the memory of the process is gone
r.p.startWaitsSaid = false
r.p.startSays(context.Background())
if len(r.ids) != 2 || r.ids[1] != r.ids[0] || r.events[1] != meshwants.EventWaitHeld {
t.Errorf("AC-832e: the start said %v %v, want wait-held again under %s", r.events, r.ids, r.ids[0])
}
r.p.startWaitsSaid = false
w := r.wait()
w.Ending = outside.HowHealthy
r.p.keepWait(context.Background(), outside.Key("ace", "nats", strings.TrimPrefix(fpTest, "src2:")), w)
r.p.startSays(context.Background())
if r.events[len(r.events)-1] != meshwants.EventWaitEnded || len(r.ids) != 3 {
t.Errorf("AC-832e: a record with ending said %v, want only its end", r.events)
}
})
t.Run("a newer want ends the wait replaced, and a restart judges it again", func(t *testing.T) {
r := newOutsideRig(t)
r.cond("machine.hub.unreachable", "ace")
@@ -80,6 +80,8 @@ type passer struct {
// nodeFailed is, by node and module, the words of the condition last raised for a failure that is the node's own (ADR 0557
// §1, issue 1496): raised once per words, cleared once when it ends.
nodeFailed map[string]string
// startSaid says the start's saying again was made (startsays.go).
startSaid, startWaitsSaid bool
// callsCleared says the first pass cleared the conditions of refused controller calls a previous process raised
// (clearStaleCalls): the assigner makes no such call now (issue 774).
callsCleared bool
@@ -174,7 +176,9 @@ func (p *passer) pass(ctx context.Context) {
watch.in(phaseAssignmentsSeat)
p.assignmentsSeat(ctx)
watch.in(phaseKeptGraph)
if held := p.keepGraph(ctx); held == "" {
held := p.keepGraph(ctx)
p.startSays(ctx)
if held == "" {
watch.in(phaseWants)
p.wants(ctx)
watch.in(phasePassSaid)
@@ -379,12 +383,21 @@ func (p *passer) putWant(ctx context.Context, node, module string, want mwc.Want
}
// The want-revision `wanted` says is the revision of the want's key it says; for a removal, of the write it removes (§832 3.).
removes := p.w.metaOf(buckets.Wants(), key).revision
// A removal is written before it is said (§832 4., H-A-843): its mark, the fingerprint and the want-revision of the write it
// removes, is kept in the state `written` before the removing want is written, and goes once the bus has taken the event.
var taken func()
if want.Removed && removes != 0 && p.ends != nil {
if !p.markRemoval(node, module, fp, removes) {
return
}
taken = func() { p.dropRemovalMark(node, module) }
}
if p.wantWritten(ctx, node, module, key, want) {
revision := removes
if !want.Removed {
revision = p.w.metaOf(buckets.Wants(), key).revision
}
p.sayWanted(node, module, fp, want.Pipeline, want.Removed, revision)
p.sayWanted(node, module, fp, want.Pipeline, want.Removed, revision, time.Time{}, taken)
}
}
@@ -411,7 +411,7 @@ func (e *pipelineEnds) branchOf(module, placed string) string {
// writtenTools is the tool assigner_written: every key of the state written, with its end.
func writtenTools(st store) []stdio.Tool {
return []stdio.Tool{{Name: writtenTool, Description: "the pipeline ends the assigner's wants follow: per module and watched branch, the newest version released whose pipeline passed, with its pipeline and rank; per pipeline whose wants are written tier by tier, its tier progress (tier, since, movers, and clock-from and paused-from: the time tier-bound counts from, and when a pause of the count by an ask of the operator began; tier-late-at, the time any tier was first found past tier-bound, and tier-late-said, the time the bus took pipeline-wanted tier-late: a key with the first and not the second holds a tier-late still to be said); with why, why each want is what it is (as assigner_why).",
return []stdio.Tool{{Name: writtenTool, Description: "the pipeline ends the assigner's wants follow: per module and watched branch, the newest version released whose pipeline passed, with its pipeline and rank; per pipeline whose wants are written tier by tier, its tier progress (tier, since, movers, and clock-from and paused-from: the time tier-bound counts from, and when a pause of the count by an ask of the operator began; tier-late-at, the time any tier was first found past tier-bound, and tier-late-said, the time the bus took pipeline-wanted tier-late: a key with the first and not the second holds a tier-late still to be said); per removal written and not yet said (removal.<node>.<module>), its mark: node, module, the fingerprint and the want-revision of the write it removes; with why, why each want is what it is (as assigner_why).",
Input: schema(map[string]string{"why": "true: why each want is what it is", "module": "with why: a module's name", "node": "with why: a node's name"}, nil, "why"),
Run: func(args map[string]any) (any, error) {
if args["why"] == true || args["why"] == "true" {
@@ -438,6 +438,13 @@ func writtenTools(st store) []stdio.Tool {
}
continue
}
if strings.HasPrefix(k, removalPrefix) { // a removal written and not said, its mark (startsays.go)
var m removalMark
if json.Unmarshal(raw, &m) == nil {
out[k] = m
}
continue
}
var e End
if json.Unmarshal(raw, &e) == nil {
out[k] = e
@@ -468,7 +475,7 @@ func (e *pipelineEnds) registered(reg mpcat.Registration, gone bool) {
prefix := keys.EscapeName(reg.Module) + "."
for _, k := range kept {
rest, ok := strings.CutPrefix(k, prefix)
if !ok || isTierKey(k) || strings.HasPrefix(k, whyPrefix) {
if !ok || isTierKey(k) || strings.HasPrefix(k, whyPrefix) || strings.HasPrefix(k, removalPrefix) {
continue
}
branch, err := keys.UnescapeName(rest)
@@ -0,0 +1,185 @@
package main
import (
"context"
"encoding/json"
"slices"
"sort"
"strconv"
"strings"
mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient"
meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
"git.novox.be/novox/mesh-sdk/go/keys"
"mesh-delivery-assigner/internal/buckets"
"mesh-delivery-assigner/internal/outside"
)
// A start says again what its kept state implies (design 47 H2b §832 4., H-A-843, H-A-859): what was owed in memory is gone after a
// restart, so the first pass that has an assignment graph revision kept says again, under the same ids and in each order's order,
// `wait-ended` for a record with `ending` (§831), `wait-held` for every standing wait under its `held-said`, and per node and module
// the removal its mark holds, then `wanted` for the want written. `pipeline-wanted` `tier-late` and `running` are said again by the
// tier progress's own pass (§824), which owes them whenever its keys say so.
// removalPrefix is the prefix of a removal's mark in the state `written`: `removal.<node>.<module>`.
const removalPrefix = "removal."
// removalMark is a removal written before it is said: the fingerprint and the want-revision of the write it removes. A mark holds
// the newest removal of its node and module, the one a consumer needs.
type removalMark struct {
Node string `json:"node"`
Module string `json:"module"`
Fp string `json:"fp"`
WantRevision uint64 `json:"want-revision"`
}
func removalKey(node, module string) string {
return removalPrefix + keys.EscapeName(node) + "." + keys.EscapeName(module)
}
// markRemoval keeps the mark of the removal of the want of module on node, which stands at revision, before the removing want is
// written; false (said in the journal) when it could not be kept, so the removal is not made.
func (p *passer) markRemoval(node, module, fp string, revision uint64) bool {
raw, err := json.Marshal(removalMark{Node: node, Module: module, Fp: fp, WantRevision: revision})
if err != nil {
return false
}
key := removalKey(node, module)
for range 2 {
held, rev, err := p.ends.st.get(key)
if err != nil {
p.logf("the mark of the removal of %s on %s cannot be read, so the removal waits: %v", module, node, err)
return false
}
if held != nil && string(held) == string(raw) {
return true
}
switch err = p.ends.st.swap(key, raw, rev); err {
case nil:
return true
case errSwapped:
continue
}
p.logf("the mark of the removal of %s on %s is not written, so the removal waits: %v", module, node, err)
return false
}
return false
}
// dropRemovalMark deletes the mark of a removal the bus has taken.
func (p *passer) dropRemovalMark(node, module string) {
if err := p.ends.st.drop(removalKey(node, module)); err != nil {
p.logf("the mark of the removal of %s on %s is not deleted: %v", module, node, err)
}
}
// startSays is the start pass's saying again: the waits' events at the first pass, and the wants' at the first pass that keeps an
// assignment graph revision, each once.
func (p *passer) startSays(ctx context.Context) {
if !p.startWaitsSaid {
p.startWaitsSaid = true
p.sayWaitsAgain(ctx)
}
if p.startSaid {
return
}
if _, _, ok := p.graphKept(); !ok {
return
}
p.startSaid = true
p.sayWantsAgain(ctx)
}
// sayWaitsAgain says `wait-ended` again for a record with `ending` (the rest of the end is done once the bus takes it), and
// `wait-held` for every standing wait under its `held-said`.
func (p *passer) sayWaitsAgain(ctx context.Context) {
if p.kvs[buckets.Waits()] == nil {
return
}
kept := p.w.read(buckets.Waits())
for _, key := range slices.Sorted(mapKeys(kept)) {
w, err := outside.Decode(kept[key])
if err != nil {
continue
}
hex := key[strings.LastIndex(key, ".")+1:]
switch {
case w.Ending != "":
p.outsideEnd(ctx, key, w, w.Ending, p.clock())
case w.Standing():
p.wantsOwe(waitOrder(key, w.ClockFrom), meshwants.EventWaitHeld, p.outsideBody(w), waitEventID(w.Node, w.Module, hex, "held."+strconv.Itoa(w.HeldSaid), w.ClockFrom), nil)
}
}
}
func mapKeys[M ~map[string]V, V any](m M) func(func(string) bool) {
return func(yield func(string) bool) {
for k := range m {
if !yield(k) {
return
}
}
}
}
// sayWantsAgain says, per node and module, the removal its mark holds (the removing want written first when the want's key still
// holds the revision the mark names), then `wanted` for the want written, carrying the key's write time as its `at`.
func (p *passer) sayWantsAgain(ctx context.Context) {
marks := map[string]removalMark{}
if p.ends != nil {
ks, err := p.ends.st.keys()
if err != nil {
p.logf("the removal marks cannot be listed: %v", err)
}
sort.Strings(ks)
for _, k := range ks {
if !strings.HasPrefix(k, removalPrefix) {
continue
}
raw, _, err := p.ends.st.get(k)
var m removalMark
if err != nil || raw == nil || json.Unmarshal(raw, &m) != nil {
continue
}
if key, err := mwc.WantKey(m.Node, m.Module); err == nil {
marks[key] = m
}
}
}
wants := p.w.read(buckets.Wants())
seen := map[string]bool{}
keysOf := slices.Sorted(mapKeys(wants))
for k := range marks {
if _, ok := wants[k]; !ok {
keysOf = append(keysOf, k)
}
}
sort.Strings(keysOf)
for _, key := range keysOf {
if seen[key] {
continue
}
seen[key] = true
node, module, err := mwc.ParseWantKey(key)
if err != nil {
continue
}
if m, ok := marks[key]; ok {
if meta := p.w.metaOf(buckets.Wants(), key); wants[key] != nil && meta.revision == m.WantRevision {
p.wantWritten(ctx, node, module, key, mwc.Want{Removed: true, Words: "the removal is written from its mark, at a start (design 47 H2b §832 4.)"})
}
p.sayWanted(node, module, m.Fp, "", true, m.WantRevision, p.w.metaOf(buckets.Wants(), key).at, func() { p.dropRemovalMark(node, module) })
}
raw := p.w.value(buckets.Wants(), key)
if raw == nil {
continue
}
want, err := mwc.DecodeWant(raw)
if err != nil || want.Removed {
continue
}
meta := p.w.metaOf(buckets.Wants(), key)
p.sayWanted(node, module, want.Fp, want.Pipeline, false, meta.revision, meta.at, nil)
}
}
@@ -401,7 +401,7 @@ func serveTools() error {
tools = append(tools, engineReadsTools(runtimeStore{})...)
tools = append(tools, writtenTools(runtimeStore{})...)
tools = append(tools, progressDropTools(runtimeStore{}, stdio.Ask)...) // a tier progress whose tier never runs (§824 7.)
tools = append(tools, waitDropTools(runtimeStore{}, stdio.Ask)...) // a decided wait record (§830 2.)
tools = append(tools, waitDropTools(runtimeStore{}, stdio.Ask)...) // a decided wait record (§830 2.)
tools = append(tools, whyTools(runtimeStore{})...)
tools = append(tools, askAgainTools(runtimeStore{}, stdio.Ask, time.Now)...)
tools = append(tools, wantDropTools(runtimeStore{}, stdio.Ask)...)