fix: use a unique key for the new-session RPC lock (#227)#228
Open
shani-singh1 wants to merge 1 commit into
Open
fix: use a unique key for the new-session RPC lock (#227)#228shani-singh1 wants to merge 1 commit into
shani-singh1 wants to merge 1 commit into
Conversation
startRpcSession coalesces concurrent callers that pass the same key onto one shared session (in-flight lock in lib/rpc-manager.ts). The new-session route derived that key from Date.now(), which has millisecond resolution, so two POST /api/agent/new requests in the same millisecond received the same AgentSessionWrapper and sessionId. Two independent chats were then merged into a single pi session. Use randomUUID() so the key is unique per request; the lock now only coalesces retries of the same logical request, never two distinct ones. Fixes agegr#227
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
Fixes #227.
POST /api/agent/newderived its de-duplication lock key fromDate.now(), which is not unique under concurrency. Two new-session requests that arrive in the same millisecond receive the sameAgentSessionWrapperandsessionId, silently merging two independent chats into one pi session.Root cause
app/api/agent/new/route.ts:startRpcSessioncoalesces concurrent callers that share a key via an in-flight lock (lib/rpc-manager.ts):Session creation takes tens–hundreds of ms, so the lock stays in-flight far longer than 1 ms. Any second request that computes the same millisecond-resolution
tempKeygets the first request's in-flight promise — and therefore the same real session. The existing comment already calls this a "one-time key";Date.now()just doesn't guarantee that.Fix
Use
randomUUID()(already imported and used throughoutlib/rpc-manager.ts) so each request gets a genuinely unique key. The lock then only ever coalesces retries of the same logical request, never two different new-session requests.One line of behavior change; no API or type changes.
Testing
tsc --noEmitpasseseslintpassesPOST /api/agent/newcalls for the samecwdnow always return distinctsessionIds (before, same-millisecond calls returned an identicalsessionId).