Keep a views reader's copy equal to its owner's: no false gaps, a lost last change caught up (issue 1966)
mesh/delivery merged: source.main moved to novox/mesh-sdk@c2b4178a8009
mesh/check being carried to 04b323868fc7 on c2b4178a8009 | from 4b9bac9c73a9

The owner's watch of a state keeping one value per key is never handed a
revision that a later write of the same key replaced first, so every such
skip read as "changes missed" and the reader asked its first view again,
over and over in a burst. Each event now names the revision said before it
(prev), and a reader takes it when its picture holds that one.

A change whose event never reached a reader was only noticed at a later
change, and a lost last one never. The owner now says its mark as an event
each time it writes it; a reader short of it asks its first view at once
and holds what it read, never the event's body.

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
This commit is contained in:
2026-11-09 21:15:55 +01:00
parent 0177432196
commit 04b323868f
5 changed files with 186 additions and 22 deletions
@@ -5,6 +5,7 @@ package views_test
import (
"context"
"encoding/json"
"fmt"
"testing"
"time"
@@ -77,6 +78,43 @@ func TestAReaderHearsAnOwnersChangesAsOrdinaryEvents(t *testing.T) {
if got := <-other; got != "mesh.mod.o.event.something-else" {
t.Fatalf("other got %s", got)
}
// Across bursts that write the same keys over and over, the reader's copy stays its owner's, by the events alone: a
// revision written over before the owner's watch was handed it is no gap, and the owner's mark after each burst asks
// nothing of a reader that holds it (issue 1966).
all, err := pic.WatchAll(ctx)
if err != nil {
t.Fatal(err)
}
var last uint64
for round := range 300 {
for range 3 {
if last, err = kv.Put(ctx, fmt.Sprintf("k%d", round%5), fmt.Appendf(nil, `{"round":%d}`, round)); err != nil {
t.Fatal(err)
}
}
}
f := r.Followers()[0]
wait, stop := context.WithTimeout(ctx, 20*time.Second)
defer stop()
for f.Status().Revision < last {
select {
case <-all.Updates():
case <-wait.Done():
t.Fatalf("the reader stayed behind its owner at %d: %+v", last, f.Status())
}
}
for i := range 5 {
e, err := kv.Get(ctx, fmt.Sprintf("k%d", i))
if err != nil {
t.Fatal(err)
}
if v, _ := f.Get(e.Key()); string(v) != string(e.Value()) {
t.Errorf("%s: the reader holds %s, its owner %s", e.Key(), v, e.Value())
}
}
if s := f.Status(); s.Asks != 1 {
t.Errorf("the reader asked its first view %d times across the bursts (%s): a revision written over was taken as a gap", s.Asks, s.Why)
}
}
// A module has one durable consumer on the shared events stream, so the loops that hear events in one module must share