← Back to PRs

#18232: fix: webchat rapid messages create orphan sessions

by MisterGuy420 open 2026-02-16 16:45 View on GitHub →
app: web-ui gateway size: XS trusted-contributor
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