Repository navigation
fix(gateway): eliminate SSE history double-read race — derive sanitized and raw views from single transcript snapshot - #61855
Conversation
Greptile SummaryThis PR eliminates a theoretical race window in the SSE history endpoint by reading the session transcript exactly once (
Confidence Score: 4/5Safe to merge — the core race fix is correct and the one flagged inconsistency has no functional impact today. The change is focused and the logic is straightforward. The only identified issue is a P2 style inconsistency ( src/gateway/sessions-history-http.ts line 290 — minor field name inconsistency in sentHistory accumulation.
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 32b155050c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Pull request overview
Updates the gateway’s session history HTTP/SSE handler to avoid reading the session transcript twice, ensuring both sanitized and raw-derived state are computed from a single transcript snapshot (preventing potential seq/content divergence across reads).
Changes:
- Read the session transcript once into a
rawSnapshotand derive sanitized history from it. - Initialize
rawTranscriptSeqfrom the samerawSnapshot(instead of performing a second transcript read).
…ed and raw views from single transcript snapshot
|
Landed via temp rebase onto
Thanks @100yenadmin! |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bac32d33b6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| maxChars: effectiveMaxChars, | ||
| limit, | ||
| cursor, | ||
| initialRawMessages: rawSnapshot, |
There was a problem hiding this comment.
Seed SSE sequence after registering transcript listener
Passing initialRawMessages: rawSnapshot seeds rawTranscriptSeq from a pre-subscription snapshot, so any message appended between that read and onSessionTranscriptUpdate(...) registration is missed while still advancing the real transcript; the next inline update then reuses a stale sequence and emits shifted messageSeq/__openclaw.seq values for SSE clients. Fresh evidence in this commit is src/gateway/session-history-state.test.ts, which now asserts no second transcript read occurs, locking in pre-subscription seeding behavior.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Thanks for the careful read — I traced through the actual code on upstream/main and I'm confident this is not a real race condition (~98% confidence).
Why the window doesn't exist in Node.js
The sequence in handleSessionHistoryHttpRequest (sessions-history-http.ts, lines 166–219) is:
rawSnapshot = readSessionMessages(...)— synchronous disk readsanitizeChatHistoryMessages,paginateSessionMessages,resolveSessionTranscriptCandidates,new SessionHistorySseState({initialRawMessages: rawSnapshot}),sseState.snapshot(),setSseHeaders,res.write,sseWrite,setInterval— all synchronous, noawaitunsubscribe = onSessionTranscriptUpdate(...)— listener registered
onSessionTranscriptUpdate / emitSessionTranscriptUpdate (transcript-events.ts) are plain synchronous in-process functions — they iterate a Set with a for...of loop, no async boundary.
Because Node.js is single-threaded with a cooperative event loop, no I/O callback or emit can fire during synchronous execution. The event loop cannot yield between steps 1 and 3. A message appended by any other coroutine would queue as a microtask or I/O event and only run after the current synchronous frame completes — which is after the listener is already registered.
Re: seq seeding
rawTranscriptSeq is seeded from rawSnapshot.at(-1).__openclaw.seq ?? rawSnapshot.length (session-history-state.ts line 113). The first inline appendInlineMessage call increments this before stamping the next message. If a message somehow landed in the window (which can't happen, as above), the refresh() fallback path (triggered when update.message === undefined) re-reads disk and resets rawTranscriptSeq from the fresh file — so even hypothetically, seq would self-correct on the next non-inline event.
Verdict: Not a real race. No fix needed. The design is sound and the test (session-history-state.test.ts line 51, asserting readSpy is never called) locks in the intent correctly. No new PR required.
What this fixes (plain English)
The SSE (Server-Sent Events) session history endpoint was reading the session transcript twice — once for the sanitized view and once for the raw view. If a write happened between the two reads, the sanitized and raw views could be inconsistent, causing seq ordering errors in real-time history events.
Technical details
Root cause: Two separate transcript reads in the SSE history handler created a race window. A concurrent write between reads could produce mismatched sanitized/raw snapshots, leading to incorrect seq values in downstream events.
Fix: Read the session transcript once and derive both sanitized and raw views from the same snapshot. Also fixes
rawTranscriptSeqinitialization to use the single-read raw snapshot.Files changed:
src/gateway/sessions-history-http.ts— single-read pattern for transcript, derive both views from snapshotRelated
Test plan