fix(agent): ensure calls receive isolated browsers handle - #249
Conversation
WalkthroughThe change gives bare calls unique task-specific session handles, prevents orphan-session adoption, and preserves browser reuse when callers echo a handle. Session messages now expose handles for stdio and other transports. Tests cover isolation, reuse, token scoping, concurrency, and response formatting. ChangesSession Isolation and Continuity
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant Caller
participant AgentTool
participant AgentClient
participant Browser
Caller->>AgentTool: Submit call with or without echoed handle
AgentTool->>AgentClient: Request browser session
AgentClient->>Browser: Create or resume exact session
AgentClient-->>AgentTool: Return task-specific handle
AgentTool-->>Caller: Return handle and continuation instructions
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
src/lib/agent-client.ts (2)
662-672: 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy liftResume echoed handles without requiring creation options.
The lookup key includes
proxy,profile,createProfile, andattachSessionId. A caller can open a profile session, then follow the returned instruction and send onlysessionId. The second call has no profile suffix, sosessions.get(key)misses and opens a new browser.Use a token-scoped handle lookup for echoed handles. Preserve the initial session configuration or reject conflicting configuration. Add a test that resumes a profile session with only
sessionId.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/lib/agent-client.ts` around lines 662 - 672, Update the session lookup around getSessionKey and sessions.get so echoed sessionId handles first resolve through a token-scoped handle index, without requiring proxy, profile, createProfile, or attachSessionId options. Preserve the original session configuration when resuming, and reject requests whose supplied configuration conflicts with that existing session. Add coverage for opening a profile session and resuming it using only sessionId.
657-672: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick winPass the minted handle to cleanup. When
echoedSessionIdis absent,getOrCreateSessionstores the session under a key containing its generatedhandle.destroySessionandcloseSessionrebuild the key withundefined, so they do not find that session. PassagentSession.handleto these cleanup calls to prevent failed or closed bare calls from leaving live WebSockets insessions.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/lib/agent-client.ts` around lines 657 - 672, Update the cleanup calls in the agent session lifecycle to pass agentSession.handle to destroySession and closeSession instead of leaving the handle undefined. Ensure cleanup rebuilds the same session key created by getOrCreateSession, including for bare calls where handle is generated.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@src/lib/agent-client.ts`:
- Around line 662-672: Update the session lookup around getSessionKey and
sessions.get so echoed sessionId handles first resolve through a token-scoped
handle index, without requiring proxy, profile, createProfile, or
attachSessionId options. Preserve the original session configuration when
resuming, and reject requests whose supplied configuration conflicts with that
existing session. Add coverage for opening a profile session and resuming it
using only sessionId.
- Around line 657-672: Update the cleanup calls in the agent session lifecycle
to pass agentSession.handle to destroySession and closeSession instead of
leaving the handle undefined. Ensure cleanup rebuilds the same session key
created by getOrCreateSession, including for bare calls where handle is
generated.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 80283f2f-c49e-436f-bbb6-07b2dafb7544
📒 Files selected for processing (4)
src/lib/agent-client.tssrc/tools/agent.tstest/lib/agent-client.spec.tstest/tools/agent.spec.ts
🤖 I have created a release *beep* *boop* --- ## [1.20.2](v1.20.1...v1.20.2) (2026-08-11) ### Bug Fixes * **agent:** ensure calls receive isolated browsers handle ([#249](#249)) ([f3fb119](f3fb119)) * fill error gaps with more useful error outcomes ([#238](#238)) ([2a043e6](2a043e6)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: browserless-actions-bot[bot] <186328842+browserless-actions-bot[bot]@users.noreply.github.com>
Summary by CodeRabbit