Skip to content

Commit 1dd7d43

Browse files
add error log for BB session timeout (#1298)
# why - if a Browserbase session times out, we are currently logging a generic CDP transport closed message # what changed - we can grab the status of the session from the BB API and yield a more informative log # test plan - unit tests
1 parent 6cb41bf commit 1dd7d43

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

.changeset/lemon-eggs-happen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
log Browserbase session status when websocket is closed due to session timeout

packages/core/lib/v3/v3.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ export class V3 {
149149
return this.browserbaseDebugUrl;
150150
}
151151
private _onCdpClosed = (why: string) => {
152+
if (this.state.kind === "BROWSERBASE") {
153+
void this._logBrowserbaseSessionStatus();
154+
}
155+
152156
// Single place to react to the transport closing
153157
this._immediateShutdown(`CDP transport closed: ${why}`).catch(() => {});
154158
};
@@ -1459,6 +1463,33 @@ export class V3 {
14591463
throw new StagehandInvalidArgumentError("Unsupported page object.");
14601464
}
14611465

1466+
private async _logBrowserbaseSessionStatus(): Promise<void> {
1467+
if (this.state.kind !== "BROWSERBASE") {
1468+
return;
1469+
}
1470+
1471+
try {
1472+
const snapshot = (await this.state.bb.sessions.retrieve(
1473+
this.state.sessionId,
1474+
)) as { id?: string; status?: string };
1475+
if (!snapshot?.status) return;
1476+
1477+
const sessionId = snapshot.id ?? this.state.sessionId;
1478+
const message =
1479+
snapshot.status === "TIMED_OUT"
1480+
? `Browserbase session timed out (sessionId: ${sessionId})`
1481+
: `Browserbase session status: ${snapshot.status}`;
1482+
1483+
this.logger({
1484+
category: "v3",
1485+
message,
1486+
level: 0,
1487+
});
1488+
} catch {
1489+
// Ignore failures; nothing to log
1490+
}
1491+
}
1492+
14621493
/**
14631494
* Create a v3 agent instance (AISDK tool-based) with execute().
14641495
* Mirrors the v2 Stagehand.agent() tool mode (no CUA provider here).

0 commit comments

Comments
 (0)