Skip to content

fix(agent/new): use randomUUID for new-session lock key to prevent session collision#230

Open
Cyriellewu wants to merge 1 commit into
agegr:mainfrom
Cyriellewu:fix/new-session-lock-key-collision
Open

fix(agent/new): use randomUUID for new-session lock key to prevent session collision#230
Cyriellewu wants to merge 1 commit into
agegr:mainfrom
Cyriellewu:fix/new-session-lock-key-collision

Conversation

@Cyriellewu

Copy link
Copy Markdown

Summary

POST /api/agent/new derived its de-duplication lock key from Date.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 same AgentSessionWrapper and the same sessionId — silently merging two independent chats into one pi session.

Fixes #227

Root cause

app/api/agent/new/route.ts:

const tempKey = `__new__`;

Session creation takes tens to hundreds of ms, so the in-flight lock stays live well beyond 1 ms. Any second POST /api/agent/new computing the same tempKey gets 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 throughout lib/rpc-manager.ts):

const tempKey = `__new__`;

A distinct key per request means the in-flight lock only ever coalesces retries of the same logical request, never two different ones. __new__/tempKey has 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/new now return distinct sessionIds). The change is a drop-in of an existing, already-used pattern.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: concurrent "New Session" requests in the same millisecond collide onto one shared agent session

1 participant