oficina / slides Public

Presenting: one place writes a position to a stream

Review fix round 2. The seq filter ran before the held stream's
catch-up, so a late older event that ended a hold was written after the
catch-up's newer row, and the newer event that followed was dropped.

Every position a stream writes (connect, catch-up, hub event) now goes
through deliver, which compares with lastSeq and moves it only on a
write. The hold checks run first, so an event is compared with the row
the catch-up just wrote. A held-stream test forces A's late event to
end the hold after B commits.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 454a084fe65d173eb6eaccef5c60e2701ec24fe5 parent 8e89cec
2 files changed, +110 −34
  • internal/slides/presentation.go +38 −34
  • internal/slides/presenting_position_test.go +72 −0
diff --git a/internal/slides/presentation.go b/internal/slides/presentation.go
index 1aec305..f0791fe 100644
--- a/internal/slides/presentation.go
+++ b/internal/slides/presentation.go
@@ -1251,22 +1251,33 @@ func (a *app) streamPresentation(w http.ResponseWriter, r *http.Request, p Prese
fl.Flush()
return true
}
- // lastSeq is the newest position sequence this stream has sent. Two
- // presses commit in order but publish from their own goroutines,
- // so the older one's event can reach the hub second; an event at or
- // below lastSeq is that, or a repeat, and is not sent. Sequence 0
- // is a session begun before positions existed, or the ended event
- // endPresentationsOf publishes, and always goes through.
+ // deliver is the one place a position is written to this stream:
+ // the connect event, the catch-up after a hold, and every event the
+ // hub sends all go through it.
+ //
+ // lastSeq is the newest position sequence it has written. Two
+ // presses commit in order but publish from their own goroutines, so
+ // the older one's event can reach the hub second, and a catch-up
+ // can write the row before an event published earlier arrives. An
+ // event at or below lastSeq is one of those, or a repeat of what
+ // the page has, and is not written. lastSeq moves only when an
+ // event is written, so nothing newer than the page has is lost.
+ // Sequence 0 is a session begun before positions existed, or the
+ // ended event endPresentationsOf publishes, and is always written.
var lastSeq int64
- stale := func(e present.Event) bool {
- if e.Seq == 0 {
+ deliver := func(e present.Event) bool {
+ if e.Seq != 0 && e.Seq <= lastSeq {
+ return true
+ }
+ body, err := json.Marshal(e)
+ if err != nil {
return false
}
- if e.Seq <= lastSeq {
- return true
+ if !send("event: slide\ndata: " + string(body) + "\n\n") {
+ return false
}
- lastSeq = e.Seq
- return false
+ lastSeq = max(lastSeq, e.Seq)
+ return true
}
// catchUp is what a held stream owes when the presenter's standing
// returns: the talk as it is now, read afresh.
@@ -1275,15 +1286,12 @@ func (a *app) streamPresentation(w http.ResponseWriter, r *http.Request, p Prese
if err := a.db.G.WithContext(r.Context()).Take(&now, p.ID).Error; err != nil {
return false
}
- // Sent whatever lastSeq says: an event dropped while the stream
- // was held may have moved lastSeq without reaching the page,
- // and the row is the newest there is.
- e := eventFor(now)
- lastSeq = max(lastSeq, e.Seq)
- body, err := json.Marshal(e)
- return err == nil && send("event: slide\ndata: "+string(body)+"\n\n")
+ return deliver(eventFor(now))
}
- write := func(text string) bool {
+ // open runs the checks before any write: false ends the stream. A
+ // stream that was held and stands again is caught up first, so
+ // whatever the caller writes next is compared with the row.
+ open := func() bool {
if alive != nil && !alive() {
return false
}
@@ -1291,23 +1299,19 @@ func (a *app) streamPresentation(w http.ResponseWriter, r *http.Request, p Prese
if !stands() {
return false
}
- if held {
- return !strings.HasPrefix(text, ":") || send(text)
- }
- if wasHeld && !catchUp() {
- return false
- }
- return send(text)
+ return held || !wasHeld || catchUp()
+ }
+ // ping is the heartbeat, which a held stream still sends.
+ ping := func() bool {
+ return open() && send(": ping\n\n")
}
+ // event is one of the hub's events. A held stream drops it; the
+ // catch-up writes the row when the hold ends.
event := func(e present.Event) bool {
- if stale(e) {
- return true
- }
- body, err := json.Marshal(e)
- if err != nil {
+ if !open() {
return false
}
- return write("event: slide\ndata: " + string(body) + "\n\n")
+ return held || deliver(e)
}
if !event(eventFor(current)) {
@@ -1346,7 +1350,7 @@ func (a *app) streamPresentation(w http.ResponseWriter, r *http.Request, p Prese
}
heartbeat.Reset(streamHeartbeat)
case <-heartbeat.C:
- if !write(": ping\n\n") {
+ if !ping() {
return
}
heartbeat.Reset(streamHeartbeat)
diff --git a/internal/slides/presenting_position_test.go b/internal/slides/presenting_position_test.go
index 8a0680e..2c6c915 100644
--- a/internal/slides/presenting_position_test.go
+++ b/internal/slides/presenting_position_test.go
@@ -167,3 +167,75 @@ func readSlideEvent(t *testing.T, r *bufio.Reader) present.Event {
}
}
}
+
+// TestAHeldStreamNeverGoesBackToAnOlderPress is the same interleaving
+// meeting a held projector (team mode, the presenter's standing
+// unknown): A commits (seq 4), B commits (seq 5), and the hold ends on
+// the arrival of A's late event. The catch-up writes the row, B, and A
+// must not be written after it; nor may B's own event, arriving last,
+// be lost. The page ends at B.
+//
+// The hold re-asks at the roster's own 5 s, not shortHold's 100 ms, so
+// it is A's event, not the retry timer, that ends the hold.
+func TestAHeldStreamNeverGoesBackToAnOlderPress(t *testing.T) {
+ f := newTeamFixture(t)
+ colleague := f.member(teamSubject('b'), "member")
+ plan := f.deck(colleague, "Team plan", rungTeam)
+ f.member(teamSubject('a'), "member")
+ session := f.startTalk(t, plan)
+ var p Presentation
+ if err := f.d.G.Where("sqid = ?", session).Take(&p).Error; err != nil {
+ t.Fatal(err)
+ }
+ f.dir.SetDown(true)
+ f.clock.Step(59 * time.Second)
+ feed := lines(f.openStream(t, "/p/"+session+"/stream", false))
+ f.clock.Step(2 * time.Second)
+ time.Sleep(1500 * time.Millisecond) // past the stream's lease timer: held
+
+ // Both committed, in order: the row holds B.
+ a2, b := p, p
+ a2.SlideID, a2.Seq = "after-a", 4
+ b.SlideID, b.Seq = "after-b", 5
+ if err := f.d.G.Model(&Presentation{}).Where("id = ?", p.ID).
+ Updates(map[string]any{"slide_id": b.SlideID, "seq": b.Seq}).Error; err != nil {
+ t.Fatal(err)
+ }
+ // Standing returns; A's event arrives first, then B's, then an end
+ // to fence the stream.
+ f.dir.SetDown(false)
+ f.refreshed()
+ f.a.hub.Publish(session, eventFor(a2))
+ f.a.hub.Publish(session, eventFor(b))
+ f.a.hub.Publish(session, present.Event{Ended: true})
+
+ var last present.Event
+ var written []int64
+ deadline := time.After(4 * time.Second)
+ for {
+ select {
+ case l, ok := <-feed:
+ if !ok {
+ t.Fatalf("the stream ended before its fence; wrote seqs %v", written)
+ }
+ data, isData := strings.CutPrefix(strings.TrimRight(l, "\n"), "data: ")
+ if !isData {
+ continue
+ }
+ var e present.Event
+ if err := json.Unmarshal([]byte(data), &e); err != nil {
+ t.Fatalf("the stream's event %q: %v", data, err)
+ }
+ if e.Ended {
+ if last.Seq != 5 || last.SlideID != "after-b" {
+ t.Errorf("the held stream left its page at %+v (wrote seqs %v), want B's position (after-b, seq 5)", last, written)
+ }
+ return
+ }
+ written = append(written, e.Seq)
+ last = e
+ case <-deadline:
+ t.Fatalf("no fence within 4s; wrote seqs %v", written)
+ }
+ }
+}