fix(browser): auto-connect provisioned sessions - #119
Conversation
There was a problem hiding this comment.
3 issues found across 3 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/bcode-browser/src/browser-execute.ts">
<violation number="1" location="packages/bcode-browser/src/browser-execute.ts:176">
P1: Cloud snippets following the required `browser-execute` skill now connect twice: the tool boundary auto-connects, then the skill's first-call `session.connect()` opens and replaces the WebSocket because `Session.connect()` is not idempotent. Updating the skill to skip `connect()` when the boundary has already connected (for example, `if (!session.isConnected()) await session.connect()`) would keep the documented flow to one socket.</violation>
<violation number="2" location="packages/bcode-browser/src/browser-execute.ts:255">
P2: Auto-connect via `BU_CDP_URL` fails when deployment supplies `BU_CDP_WS=""`. Select the non-empty endpoint explicitly so an empty alias falls back to `BU_CDP_URL`, matching this gate's behavior.</violation>
<violation number="3" location="packages/bcode-browser/src/browser-execute.ts:255">
P2: A tool call that reconnects after `session.close()` can have its new CDP requests rejected as `CDP socket closed` if the prior socket's delayed close event arrives afterward. Scope the close handler's pending-request cleanup to the socket it created (or otherwise wait for the old socket to close) before automatic reconnects.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Re-trigger cubic
| }) | ||
|
|
||
| yield* Effect.tryPromise({ | ||
| try: () => ensureCloudConnected(session), |
There was a problem hiding this comment.
P1: Cloud snippets following the required browser-execute skill now connect twice: the tool boundary auto-connects, then the skill's first-call session.connect() opens and replaces the WebSocket because Session.connect() is not idempotent. Updating the skill to skip connect() when the boundary has already connected (for example, if (!session.isConnected()) await session.connect()) would keep the documented flow to one socket.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/bcode-browser/src/browser-execute.ts, line 176:
<comment>Cloud snippets following the required `browser-execute` skill now connect twice: the tool boundary auto-connects, then the skill's first-call `session.connect()` opens and replaces the WebSocket because `Session.connect()` is not idempotent. Updating the skill to skip `connect()` when the boundary has already connected (for example, `if (!session.isConnected()) await session.connect()`) would keep the documented flow to one socket.</comment>
<file context>
@@ -167,6 +172,11 @@ export const make = Effect.fn("BrowserExecute.make")(function* (dataDir: string)
})
+ yield* Effect.tryPromise({
+ try: () => ensureCloudConnected(session),
+ catch: (err) => (err instanceof Error ? err : new Error(String(err))),
+ })
</file context>
| const existing = cloudConnections.get(session) | ||
| if (existing) return existing | ||
|
|
||
| const connecting = session.connect() |
There was a problem hiding this comment.
P2: Auto-connect via BU_CDP_URL fails when deployment supplies BU_CDP_WS="". Select the non-empty endpoint explicitly so an empty alias falls back to BU_CDP_URL, matching this gate's behavior.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/bcode-browser/src/browser-execute.ts, line 255:
<comment>Auto-connect via `BU_CDP_URL` fails when deployment supplies `BU_CDP_WS=""`. Select the non-empty endpoint explicitly so an empty alias falls back to `BU_CDP_URL`, matching this gate's behavior.</comment>
<file context>
@@ -235,4 +245,20 @@ export const make = Effect.fn("BrowserExecute.make")(function* (dataDir: string)
+ const existing = cloudConnections.get(session)
+ if (existing) return existing
+
+ const connecting = session.connect()
+ cloudConnections.set(session, connecting)
+ try {
</file context>
| const connecting = session.connect() | |
| const connecting = session.connect({ wsUrl: process.env.BU_CDP_WS || process.env.BU_CDP_URL }) |
| const existing = cloudConnections.get(session) | ||
| if (existing) return existing | ||
|
|
||
| const connecting = session.connect() |
There was a problem hiding this comment.
P2: A tool call that reconnects after session.close() can have its new CDP requests rejected as CDP socket closed if the prior socket's delayed close event arrives afterward. Scope the close handler's pending-request cleanup to the socket it created (or otherwise wait for the old socket to close) before automatic reconnects.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/bcode-browser/src/browser-execute.ts, line 255:
<comment>A tool call that reconnects after `session.close()` can have its new CDP requests rejected as `CDP socket closed` if the prior socket's delayed close event arrives afterward. Scope the close handler's pending-request cleanup to the socket it created (or otherwise wait for the old socket to close) before automatic reconnects.</comment>
<file context>
@@ -235,4 +245,20 @@ export const make = Effect.fn("BrowserExecute.make")(function* (dataDir: string)
+ const existing = cloudConnections.get(session)
+ if (existing) return existing
+
+ const connecting = session.connect()
+ cloudConnections.set(session, connecting)
+ try {
</file context>
Summary
Sessionbeforebrowser_executeruns a snippet whenBU_CDP_WSorBU_CDP_URLis configuredWhy
V4 provisions the browser and sets
BU_CDP_WS, but a fresh worker still begins with a disconnected in-processSession. In a recent ordered production cohort, all 28 exactNot connected. Call session.connect(...) first.snippets reached CDP before executing a connection; 26/28 later recovered with successful browser calls. Initialization should be deterministic instead of model-dependent.Semantics and tradeoffs
BU_CDP_WS/BU_CDP_URL, local BrowserCode behavior is unchangedTests
BU_CDP_WSandBU_CDP_URLtrigger the behaviorValidation:
cd packages/bcode-browser && bun typecheckcd packages/bcode-browser && bun test— 23 passed, 8 live/smoke tests skipped, 0 failedbun run typecheck— 16 packages passedbrowser-execute.tsSummary by cubic
Auto-connects provisioned cloud sessions in
browser_executewhenBU_CDP_WSorBU_CDP_URLis set, eliminating "Not connected" errors. Deduplicates concurrent first connections, keeps local sessions explicit-connect, and returns the original connect error on failure.Sessionbefore a snippet runs when a provisioned endpoint is present; reuse one socket across calls.Sessionto avoid race conditions.Written for commit f28d0aa. Summary will update on new commits.