Finding 2 (review r1): the wait's held-said and ending are written by compare-and-set, and wait-held is owed only once the write took

Builds: design 47 H2b §832 3., §831
This commit is contained in:
2026-11-27 03:51:32 +01:00
parent e06c204af3
commit e061c319f1
2 changed files with 59 additions and 4 deletions
@@ -1,6 +1,7 @@
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
@@ -408,6 +409,7 @@ func (p *passer) outsideAnswered(w *outside.Wait, node, module, fp string) bool
// outsideApply keeps what a judging came to: the wait's record, its events, its finding and its ask.
func (p *passer) outsideApply(ctx context.Context, key, hex, node, module, fp string, prev *outside.Wait, d outside.Decision, now time.Time, running bool) {
o := p.outside
read := p.w.metaOf(buckets.Waits(), key).revision
switch {
case prev != nil && prev.Ending != "":
return // its end is owed, and its say finishes it (outsideReview)
@@ -422,9 +424,13 @@ func (p *passer) outsideApply(ctx context.Context, key, hex, node, module, fp st
}
w := *d.Wait
if d.Held {
// The record is written with `held-said` raised first, and the event said under the id it names (§832 3.).
// The record is written with `held-said` raised first, by compare-and-set on the revision the judging read, and the event
// is owed under the id it names only once that write took (§832 3.); a lost race is judged again at the next pass.
w.HeldSaid++
p.keepWait(ctx, key, w)
if !p.keepWaitAt(ctx, key, w, read) {
p.logf("the wait of %s on %s at %s is not written, so its wait-held is not owed yet", module, node, fp)
return
}
p.wantsOwe(waitOrder(key, w.ClockFrom), meshwants.EventWaitHeld, p.outsideBody(w), waitEventID(node, module, hex, "held."+strconv.Itoa(w.HeldSaid), w.ClockFrom), nil)
p.outsideFinding(w, true)
}
@@ -473,7 +479,37 @@ func containsString(l []string, s string) bool {
// keepWait writes the wait's record.
func (p *passer) keepWait(ctx context.Context, key string, w outside.Wait) {
p.put(ctx, buckets.Waits(), key, func() ([]byte, error) { return outside.Encode(w) })
p.keepWaitAt(ctx, key, w, p.w.metaOf(buckets.Waits(), key).revision)
}
// keepWaitAt writes the wait's record by compare-and-set on revision, the one read (0: the key is new), and says whether it took: a
// record that moved past it (a hand act, another write) is not overwritten, and its newer value stands (design 47 H2b §832 3.).
func (p *passer) keepWaitAt(ctx context.Context, key string, w outside.Wait, revision uint64) bool {
value, err := outside.Encode(w)
if err != nil {
p.logf("%s %s is not written: %v", buckets.Waits(), key, err)
return false
}
raw := p.w.value(buckets.Waits(), key)
if raw != nil && bytes.Equal(raw, value) {
return true // it stands as it is to be
}
kv := p.kvs[buckets.Waits()]
if raw == nil || revision == 0 {
revision, err = kv.Create(ctx, key, value)
} else {
revision, err = kv.Update(ctx, key, value, revision)
}
if err != nil {
if errors.Is(err, jetstream.ErrKeyExists) || errors.Is(err, jetstream.ErrKeyRevisionMismatch) || isWrongSequence(err) {
p.logf("the wait %s is not written: its record moved past the revision this pass read; the next pass judges it again", key)
} else if !errors.Is(err, context.Canceled) {
p.logf("%s %s could not be written: %v", buckets.Waits(), key, err)
}
return false
}
p.w.putEntry(buckets.Waits(), key, value, revision, p.clock())
return true
}
func (p *passer) outsideBody(w outside.Wait) meshwants.Wait {
@@ -494,7 +530,7 @@ func (p *passer) outsideEnd(ctx context.Context, key string, w outside.Wait, how
}
if w.Ending == "" {
w.Ending = how
if !p.written(ctx, buckets.Waits(), key, func() ([]byte, error) { return outside.Encode(w) }) {
if !p.keepWaitAt(ctx, key, w, p.w.metaOf(buckets.Waits(), key).revision) {
p.logf("the end of %s on %s (%s) is not written, so it is not said", w.Module, w.Node, how)
return
}
@@ -642,6 +642,25 @@ func TestTheWaitOnAnOutsideCause(t *testing.T) {
t.Errorf("id %q holds a dot unescaped", id)
}
})
t.Run("held-said and ending are written by compare-and-set: a record that moved past the read is not overwritten and owes no event (§832 3., review r1 finding 2)", func(t *testing.T) {
r := newOutsideRig(t)
r.cond("machine.hub.unreachable", "ace")
r.review(false, nil)
kv := r.p.kvs[buckets.Waits()].(*memKV)
for key := range kv.values {
kv.revs[key] += 100 // a rival write: the world still holds the revision it read
}
before := len(r.ids)
r.conds = nil
r.cond("machine.hub.network", "ace")
r.now = r.now.Add(time.Minute)
r.p.outside.Heard("mesh.seat.mesh-conditions.event.condition-raised", []byte(`{"key":"machine.hub.network","subject":{"id":"hub","scope":"machine","machine":"hub","also":["ace"]}}`))
<-r.woke
r.review(false, nil)
if len(r.ids) != before || r.wait().HeldSaid != 1 {
t.Errorf("a held write that lost the race owed %v and left held-said %d", r.ids[before:], r.wait().HeldSaid)
}
})
t.Run("its bounds are read at the start from the files the manifest declares (A-112)", func(t *testing.T) {
dir := t.TempDir()
write := func(name, content string) string {