diff --git a/specs/rhema-together.md b/specs/rhema-together.md index 6f012f8..ff1ab15 100644 --- a/specs/rhema-together.md +++ b/specs/rhema-together.md @@ -528,6 +528,12 @@ Every stream is resumable by sequence number. A reconnect asks for `since=` 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 diff --git a/src/api/messages.rs b/src/api/messages.rs index 660b07d..33ef042 100644 --- a/src/api/messages.rs +++ b/src/api/messages.rs @@ -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! { diff --git a/web/app/pages/room/[id].vue b/web/app/pages/room/[id].vue index f35e757..0c8d1a6 100644 --- a/web/app/pages/room/[id].vue +++ b/web/app/pages/room/[id].vue @@ -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 = ''