fix(agent/new): use randomUUID for new-session lock key to prevent session collision#230
Open
Cyriellewu wants to merge 1 commit into
Open
fix(agent/new): use randomUUID for new-session lock key to prevent session collision#230Cyriellewu wants to merge 1 commit into
Cyriellewu wants to merge 1 commit into
Conversation
POST /api/agent/new derived its in-flight lock key from Date.now(), which has only millisecond resolution. startRpcSession() de-duplicates concurrent callers by that key, so two new-session requests arriving in the same millisecond received the same AgentSessionWrapper and sessionId -- silently merging two independent chats into one pi session (cross-conversation contamination / isolation failure). Use randomUUID() (already used the same way in lib/rpc-manager.ts) so each request gets a unique key. The in-flight lock now only ever coalesces retries of the same logical request, never two distinct ones. Fixes agegr#227 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POST /api/agent/newderived its de-duplication lock key fromDate.now()alone, which has only millisecond resolution.startRpcSession()(lib/rpc-manager.ts) coalesces concurrent callers by that key via an in-flight lock, so two new-session requests arriving in the same millisecond received the sameAgentSessionWrapperand the samesessionId— silently merging two independent chats into one pi session.Fixes #227
Root cause
app/api/agent/new/route.ts:Session creation takes tens to hundreds of ms, so the in-flight lock stays live well beyond 1 ms. Any second
POST /api/agent/newcomputing the sametempKeygets the first request's in-flight promise, and therefore the same real session — a cross-conversation isolation failure the client cannot detect or recover from.Fix
Use
randomUUID()for the key (already imported and used the same way throughoutlib/rpc-manager.ts):A distinct key per request means the in-flight lock only ever coalesces retries of the same logical request, never two different ones.
__new__/tempKeyhas exactly one call site in the repo, and nothing depends on the key being time-ordered or parseable, so there is no downstream impact.Testing
The repo has no test harness, so this was verified by inspection and the deterministic reproduction from the issue (two near-simultaneous
POST /api/agent/newnow return distinctsessionIds). The change is a drop-in of an existing, already-used pattern.