#18232: fix: webchat rapid messages create orphan sessions
app: web-ui
gateway
size: XS
trusted-contributor
Cluster:
Network Error Handling Improvements
When webchat messages are sent rapidly (before the first reply returns), each message was creating a separate session instead of being routed to the existing webchat session. This caused complete loss of conversation context.
## Root Cause
The issue was in `src/gateway/server-methods/chat.ts`. When a webchat message arrived and there was no existing session entry with a `sessionId`, the code was using the `clientRunId` (the idempotency key) as the sessionId. Since each message has a different idempotency key, this caused each message to create a new "orphan" session.
## Fix
1. Generate a proper stable `sessionId` using `crypto.randomUUID()` instead of using the `clientRunId`
2. Persist this sessionId to the session store so subsequent messages in the same session reuse the same sessionId
This ensures that all rapid messages in a webchat session are routed to the same session, preserving conversation context.
## Testing
- Build passes
- Existing chat tests pass
Fixes #18203
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR attempts to fix webchat orphan sessions caused by rapid messages by generating a stable `sessionId` via `randomUUID()` instead of using `clientRunId`. While the approach is correct in principle, there are two issues:
- **Missing `storePath` destructuring (line 575)**: The `loadSessionEntry` call does not extract `storePath`, so the session persistence block on lines 636-648 references an undefined variable. This means the fix never actually persists the generated `sessionId`, completely defeating the purpose of the change.
- **Race condition in `updateSessionStore` mutator**: Even once `storePath` is fixed, two concurrent messages that both see `entry?.sessionId` as undefined will each generate different UUIDs. The mutator does not check whether a `sessionId` was already written by a prior concurrent message, so the second write overwrites the first, creating inconsistent session state.
<h3>Confidence Score: 1/5</h3>
- This PR has a compile-breaking bug that prevents the fix from working and should not be merged as-is.
- The core fix (session persistence) references an undeclared variable `storePath`, which would cause a TypeScript compile error. If it somehow bypassed the type checker, it would throw a ReferenceError at runtime. Even after fixing this, a race condition exists where concurrent rapid messages can overwrite each other's sessionIds.
- `src/gateway/server-methods/chat.ts` — the `storePath` destructuring must be fixed on line 575, and the race condition in the `updateSessionStore` mutator needs to be addressed.
<sub>Last reviewed commit: 7a4ff70</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#14966: fix(webchat): preserve user message visibility after chat.send
by BenediktSchackenberg · 2026-02-12
78.6%
#19343: Refactor chat state management: reset chat messages and queue on se...
by saurav470 · 2026-02-17
78.6%
#17527: fix(gateway): allow WebChat to attach to main session regardless of...
by Glucksberg · 2026-02-15
78.4%
#3182: fix(gateway): use canonical session key in chat.send
by chrisherold · 2026-01-28
78.2%
#14309: fix(ui): resolve chat event session key mismatch
by justonlyforyou · 2026-02-11
78.1%
#18694: fix(ui): reset session key to main after /new in webchat
by Phineas1500 · 2026-02-17
77.7%
#21828: fix: acquire session write lock in delivery mirror and gateway chat...
by inkolin · 2026-02-20
76.9%
#15564: fix: webchat messages disappear during concurrent session activity
by Automatedcapitalist · 2026-02-13
76.9%
#13104: fix: persist user command message in chat transcript
by mcaxtr · 2026-02-10
76.5%
#4567: Webchat: canonicalize main session key for /new (fix #4446)
by selfboot · 2026-01-30
76.4%