web: show a running turn only after replay catches up

The room marked the SSE stream live when the response headers arrived, before it read the durable backlog. A replayed turn_start therefore showed "Rhema is working..." until its turn_end arrived at the end of the replay, even for a turn that had finished.

The feed now closes its replay with a live-only ready frame, and the client marks the stream live on that frame. A backlog over the 2000-row cap sends lag instead, so a skipped turn_end cannot leave the room running either.
This commit is contained in:
vhaudiquet committed 2026-12-03 13:49:32 +01:00
1 parent a40a7a6b36
commit 62fa380ec0
3 files changed
+28 -2

No files matched your search

+6
View File
@@ -528,6 +528,12 @@ Every stream is resumable by sequence number. A reconnect asks for
`since=<last seq>` and gets the gap, then live frames. The hub's
record-under-one-lock trick is the shape to copy: no gap, no overlap.
The replay ends with a live-only `ready` frame. A backlog larger than
the 2000-row cap sends `lag` instead, and the client resumes from its
last processed sequence. The room shows `Rhema is working...` only after
`ready`, so replaying a turn that already ended does not claim it is
still running.
## 8. Auth and trust
### Accounts
+17
View File
@@ -186,6 +186,10 @@ pub async fn events(
})?;
rows.filter_map(Result::ok).collect()
};
let caught_up = backlog
.last()
.map(|envelope| envelope.seq >= max_seq as u64)
.unwrap_or(max_seq <= since);
let mut last = backlog
.last()
.map(|envelope| envelope.seq)
@@ -195,6 +199,19 @@ pub async fn events(
let data = serde_json::to_string(&envelope).unwrap_or_else(|_| "{}".into());
yield Ok(SseEvent::default().id(envelope.seq.to_string()).data(data));
}
// The client uses this frame as the boundary between replay and
// the live feed. Until it arrives, a replayed `turn_start` is a
// turn that already ended, not one that is running now.
if caught_up {
let data = json!({ "seq": last, "kind": "ready", "data": {} }).to_string();
yield Ok(SseEvent::default().event("ready").data(data));
} else {
// The backlog hit its row cap. The client resumes from what
// it processed instead of skipping the rest silently.
let data = json!({ "seq": last, "kind": "lag", "data": {} }).to_string();
yield Ok(SseEvent::default().event("lag").data(data));
return;
}
let mut access = tokio::time::interval(Duration::from_secs(2));
loop {
tokio::select! {
+5 -2
View File
@@ -379,6 +379,11 @@ function processChunk(chunk: string) {
handleLag()
return
}
if (envelope.kind === 'ready') {
retryIndex = 0
streamState.value = 'live'
return
}
handleEnvelope(envelope)
scheduleStick()
} catch {
@@ -412,8 +417,6 @@ async function connectEvents() {
}
throw new Error('The live feed is unavailable.')
}
streamState.value = 'live'
retryIndex = 0
const reader = response.body.getReader()
const decoder = new TextDecoder()
let buffer = ''