Skip to content

feat: extension watchdog for lost SDK clients - #3325

Merged
miguelg719 merged 18 commits into
feat/browser-disconnectfrom
feat/client-watchdog
Oct 29, 2026
Merged

miguelg719 merged 18 commits into
feat/browser-disconnectfrom
feat/client-watchdog

Conversation

@miguelg719

Copy link
Copy Markdown
Collaborator

why

if the sdk process dies without disconnect() / close() (crash, SIGKILL, dropped connection), nothing tells the browser: a local chrome stagehand launched keeps running, and the extension stays initialized, so reconnecting fails with "A Stagehand instance is already initialized". builds on #3305 & adopts the mechanism from service-watchdog

what changed

  • on connect the sdk creates a hidden target on its own cdp connection; chrome destroys it when that connection ends. the extension watches Target.targetDestroyed over its own connection, with no heartbeat or timers
  • on loss: a local browser stagehand launched without keepAlive gets Browser.close; anything else keeps running, and the extension releases the instance & closes its own connection, so browserbase applies its keepAlive policy and a new client can attach
  • disconnect() detaches (disarms the watchdog) before closing; if the detach isn't acknowledged, the socket & handle stay open, since closing would read as a loss
  • registration checks the marker really is hidden (type: "other"); with an older extension or a chrome without hidden targets the sdk warns once & continues without the watchdog
  • one client per browser: a second client gets "A Stagehand client is already connected"
  • ts & python; docs & changeset

test plan

  • extension unit tests: loss/terminate/detach ordering, close of the extension's connection on loss, registration races
  • sdk unit tests in both sdks: loss policy per provider/origin/keepAlive, unacknowledged detach keeps the handle usable
  • real chrome (ts & python): SIGKILL/SIGINT matrix (owned keepAlive=false is shut down, keepAlive=true & externally launched chrome survive and re-attach), disconnect → reconnect → create
  • live browserbase: hidden target works through the proxy; after SIGKILL of the host, a keepAlive: false session is COMPLETED within 5s, and a keepAlive: true session stays RUNNING & accepts connect() + Stagehand.create()

seanmcguire12 and others added 2 commits October 26, 2026 17:46
Adapts the service watchdog onto browser.disconnect()/close():
- loss without shutdown also closes the worker's own connection (B)
- registration verifies the marker is hidden and falls back with a warning
  on old extensions or Chrome builds without hidden targets
- disconnect() detaches before closing; an unacknowledged detach keeps the
  socket and handle open
- no reconnect-to-close; close() follows the #3305 rules

Co-authored-by: Sean McGuire <[email protected]>
@miguelg719
miguelg719 requested a review from a team as a code owner October 27, 2026 00:56
@changeset-bot

changeset-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3dcf927

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 23 packages
Name Type
@browserbasehq/stagehand Minor
@browserbasehq/stagehand-python Minor
@browserbasehq/stagehand-extension Minor
browse Patch
@browserbasehq/stagehand-examples Patch
@browserbasehq/stagehand-integrations-claude-cua-sdk Patch
@browserbasehq/stagehand-integrations Patch
@browserbasehq/eve Patch
@browserbasehq/stagehand-integrations-gemini-cua-sdk Patch
@browserbasehq/stagehand-integrations-example-pi-facade Patch
@browserbasehq/stagehand-integrations-claude-agent-sdk Patch
@browserbasehq/stagehand-integrations-example-claude-code-facade Patch
@browserbasehq/stagehand-integrations-codex-sdk Patch
@browserbasehq/stagehand-integrations-example-codex-facade Patch
@browserbasehq/stagehand-integrations-cursor-sdk Patch
@browserbasehq/stagehand-integrations-example-cursor-facade Patch
@browserbasehq/stagehand-integrations-deepagents-sdk Patch
@browserbasehq/stagehand-integrations-eve-sdk Patch
@browserbasehq/stagehand-integrations-fx-sdk Patch
@browserbasehq/stagehand-integrations-mastra-sdk Patch
@browserbasehq/stagehand-integrations-example-mastra-facade Patch
@browserbasehq/stagehand-integrations-pi-sdk Patch
@browserbasehq/stagehand-integrations-example-vercel-ai-facade Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 19 files

Architecture diagram
sequenceDiagram
    participant SDK as SDK Client (TS/Python)
    participant EXT as Stagehand Extension
    participant SW as Service Worker Runtime
    participant CL as ClientLifecycle
    participant RPC as RPCClient
    participant CDP as CDP Connection
    participant BROWSER as Browser/Chrome
    participant BB as Browserbase API

    Note over SDK,BROWSER: NEW: Client Connection Watchdog Flow

    SDK->>SDK: Register client lifecycle policy
    SDK->>CDP: Target.createTarget (hidden marker)
    CDP-->>SDK: targetId
    
    alt Extension supports lifecycle
        SDK->>EXT: Attach request (targetId, cdpUrl, closeBrowserOnDisconnect)
        EXT->>CL: handle(attach)
        CL->>CDP: Establish browser connection
        CL->>CDP: Subscribe Target.targetDestroyed
        CL-->>SDK: Attachment acknowledged
        SDK->>SDK: Store clientTargetId
    else Older extension or no hidden targets
        SDK->>CDP: Target.closeTarget
        SDK->>SDK: Warn once, continue without watchdog
    end

    Note over SDK,BROWSER: Intentional Disconnect

    SDK->>CDP: detachClient()
    CDP->>EXT: Detach request (targetId)
    EXT->>CL: handle(detach)
    CL->>CL: Remove attachment, disarm watchdog
    CL->>RPC: releaseClient()
    CL->>EXT: Release Stagehand instance
    CL->>CDP: Close browser connection
    CDP-->>SDK: Detach acknowledged
    SDK->>CDP: close()

    Note over SDK,BROWSER: Unexpected Client Loss (crash/SIGKILL)

    CDP-->>CL: Target.targetDestroyed
    CL->>CL: Verify attachment matches
    alt closeBrowserOnDisconnect = true (local keepAlive=false)
        CL->>CDP: Browser.close
        CDP-->>BROWSER: Terminate browser
    else closeBrowserOnDisconnect = false
        CL->>RPC: releaseClient()
        CL->>EXT: Release Stagehand instance
        CL->>CDP: Close connection
        alt Browserbase session
            CDP-->>BB: Session closed
            BB->>BB: Apply keepAlive policy
        end
    end

    Note over SDK,BROWSER: Reconnection after loss

    SDK->>SDK: New client starts
    SDK->>CDP: Create new hidden target
    SDK->>EXT: Attach request (new targetId)
    CL->>CL: Check existing attachment
    alt Previous attachment still valid
        CL-->>SDK: Error: "A Stagehand client is already connected"
    else Previous attachment destroyed
        CL->>CL: Reconcile and clean up previous
        CL->>CL: Accept new attachment
        CL-->>SDK: New attachment acknowledged
    end

    opt Detach not acknowledged
        SDK->>CDP: detachClient() fails
        SDK->>SDK: Keep socket and handle open
        SDK->>SDK: Allow retry
    end
Loading

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread packages/extension/clientLifecycle.ts Resolved
Comment thread packages/extension/clients/rpcClient.ts Resolved
Comment thread packages/sdk-ts/src/cdpClient.ts Resolved
Comment thread packages/extension/clientLifecycle.ts Resolved
@miguelg719
miguelg719 added this pull request to stack #3331 October 27, 2026 18:30
Comment thread .changeset/watchful-hosts-leave.md Resolved
Comment thread packages/extension/clientLifecycle.ts Resolved
Comment thread packages/extension/clientLifecycle.ts Resolved
Comment thread packages/sdk-ts/src/cdpClient.ts Resolved
…ate detach, require Chrome 137, keep-alive docs

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 9 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread packages/extension/runtime.ts Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 7 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread packages/extension/clientLifecycle.ts Resolved
Comment thread packages/sdk-ts/src/cdpClient.ts Resolved
@seanmcguire12

Copy link
Copy Markdown
Member

also can we update /extension/manifest.json to say "minimum_chrome_version": "137" instead of "minimum_chrome_version": "116"

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 8 files (changes from recent commits).

Confidence score: 3/5

  • In clientLifecycle.ts, a reconnect can be rejected if it calls initialize() before the previous initialization and disposal have drained. Queue or defer reconnect initialization until that work finishes.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/extension/clientLifecycle.ts">

<violation number="1" location="packages/extension/clientLifecycle.ts:67">
P2: A reconnect can call `initialize()` before the previous initialization and disposal drain; `StagehandRuntime.initialize()` checks `initializationInProgress` before enqueueing and rejects instead of queuing. Make the new initialization wait until the prior initialization settles, or make `initialize()` join the lifecycle queue before checking that flag.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread packages/extension/clientLifecycle.ts Resolved
Comment thread packages/sdk-ts/src/cdpClient.ts Resolved
@miguelg719
miguelg719 merged commit d59afde into main Oct 29, 2026
63 checks passed
@miguelg719
miguelg719 deleted the feat/client-watchdog branch October 29, 2026 01:15
miguelg719 pushed a commit that referenced this pull request Nov 4, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @browserbasehq/[email protected]

### Minor Changes

- [#3305](#3305)
[`aeb1ba0`](aeb1ba0)
Thanks [@miguelg719](https://github.com/miguelg719)! - add
`browser.disconnect()`, which closes an attached Stagehand instance and
the connection to a browser without closing the browser or releasing its
Browserbase session (Browserbase still ends sessions without `keepAlive`
on disconnect). `browser.close()` after `disconnect()` now throws
(connect again to close the browser), a launched local browser receives
`Browser.close` before it is terminated, browser handles expose `cdpUrl`
(`cdp_url` in Python) for local browsers, and `connected` reports
whether the handle still has a live connection

- [#3325](#3325)
[`d59afde`](d59afde)
Thanks [@miguelg719](https://github.com/miguelg719)! - Add an extension
watchdog for lost SDK clients

### Patch Changes

- [#3339](#3339)
[`44d99df`](44d99df)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Adopt
cross-origin iframes that were already loaded when Stagehand attached,
or that are nested inside another cross-origin iframe, so snapshots and
actions can reach them.

- [#3330](#3330)
[`dc8eb40`](dc8eb40)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - bump zod to
4.6.6

- [#3340](#3340)
[`edb76b0`](edb76b0)
Thanks [@akeimach](https://github.com/akeimach)! - Fix act, observe and
extract with Claude Opus 5.5 by bumping `@ai-sdk/anthropic` and `ai` in
the extension.

- [#3387](#3387)
[`066e875`](066e875)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fixed
domain policy bypasses involving URLs with credentials, background
requests, and speculative page loads

- [#3383](#3383)
[`1e50db6`](1e50db6)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip
variable substitution for drag-and-drop arguments in act().

- [#3388](#3388)
[`18b96f2`](18b96f2)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fix issue
where calling act() prevents cross-origin iframes from loading
## @browserbasehq/[email protected]

### Patch Changes

- [#3330](#3330)
[`dc8eb40`](dc8eb40)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - bump zod to
4.6.6

- [#3350](#3350)
[`b7a4b89`](b7a4b89)
Thanks [@miguelg719](https://github.com/miguelg719)! - Save run
screenshots only as PNG or JPEG files, under ./stagehand-artifacts by
default

- Updated dependencies
[[`44d99df`](44d99df),
[`dc8eb40`](dc8eb40),
[`edb76b0`](edb76b0),
[`066e875`](066e875),
[`1e50db6`](1e50db6),
[`aeb1ba0`](aeb1ba0),
[`18b96f2`](18b96f2),
[`d59afde`](d59afde)]:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Minor Changes

- [#3325](#3325)
[`d59afde`](d59afde)
Thanks [@miguelg719](https://github.com/miguelg719)! - Add an extension
watchdog for lost SDK clients

### Patch Changes

- [#3339](#3339)
[`44d99df`](44d99df)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Adopt
cross-origin iframes that were already loaded when Stagehand attached,
or that are nested inside another cross-origin iframe, so snapshots and
actions can reach them.

- [#3330](#3330)
[`dc8eb40`](dc8eb40)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - bump zod to
4.6.6

- [#3340](#3340)
[`edb76b0`](edb76b0)
Thanks [@akeimach](https://github.com/akeimach)! - Fix act, observe and
extract with Claude Opus 5.5 by bumping `@ai-sdk/anthropic` and `ai` in
the extension.

- [#3387](#3387)
[`066e875`](066e875)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fixed
domain policy bypasses involving URLs with credentials, background
requests, and speculative page loads

- [#3383](#3383)
[`1e50db6`](1e50db6)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip
variable substitution for drag-and-drop arguments in act().

- [#3388](#3388)
[`18b96f2`](18b96f2)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fix issue
where calling act() prevents cross-origin iframes from loading
## @browserbasehq/[email protected]

### Minor Changes

- [#3305](#3305)
[`aeb1ba0`](aeb1ba0)
Thanks [@miguelg719](https://github.com/miguelg719)! - add
`browser.disconnect()`, which closes an attached Stagehand instance and
the connection to a browser without closing the browser or releasing its
Browserbase session (Browserbase still ends sessions without `keepAlive`
on disconnect). `browser.close()` after `disconnect()` now throws
(connect again to close the browser), a launched local browser receives
`Browser.close` before it is terminated, browser handles expose `cdpUrl`
(`cdp_url` in Python) for local browsers, and `connected` reports
whether the handle still has a live connection

- [#3325](#3325)
[`d59afde`](d59afde)
Thanks [@miguelg719](https://github.com/miguelg719)! - Add an extension
watchdog for lost SDK clients

### Patch Changes

- [#3339](#3339)
[`44d99df`](44d99df)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Adopt
cross-origin iframes that were already loaded when Stagehand attached,
or that are nested inside another cross-origin iframe, so snapshots and
actions can reach them.

- [#3330](#3330)
[`dc8eb40`](dc8eb40)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - bump zod to
4.6.6

- [#3340](#3340)
[`edb76b0`](edb76b0)
Thanks [@akeimach](https://github.com/akeimach)! - Fix act, observe and
extract with Claude Opus 5.5 by bumping `@ai-sdk/anthropic` and `ai` in
the extension.

- [#3387](#3387)
[`066e875`](066e875)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fixed
domain policy bypasses involving URLs with credentials, background
requests, and speculative page loads

- [#3383](#3383)
[`1e50db6`](1e50db6)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip
variable substitution for drag-and-drop arguments in act().

- [#3388](#3388)
[`18b96f2`](18b96f2)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fix issue
where calling act() prevents cross-origin iframes from loading
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`44d99df`](44d99df),
[`dc8eb40`](dc8eb40),
[`edb76b0`](edb76b0),
[`066e875`](066e875),
[`1e50db6`](1e50db6),
[`aeb1ba0`](aeb1ba0),
[`18b96f2`](18b96f2),
[`d59afde`](d59afde)]:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
##
@browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`44d99df`](44d99df),
[`dc8eb40`](dc8eb40),
[`edb76b0`](edb76b0),
[`066e875`](066e875),
[`1e50db6`](1e50db6),
[`aeb1ba0`](aeb1ba0),
[`18b96f2`](18b96f2),
[`d59afde`](d59afde)]:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`44d99df`](44d99df),
[`dc8eb40`](dc8eb40),
[`edb76b0`](edb76b0),
[`066e875`](066e875),
[`1e50db6`](1e50db6),
[`aeb1ba0`](aeb1ba0),
[`18b96f2`](18b96f2),
[`d59afde`](d59afde)]:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`44d99df`](44d99df),
[`dc8eb40`](dc8eb40),
[`edb76b0`](edb76b0),
[`066e875`](066e875),
[`1e50db6`](1e50db6),
[`aeb1ba0`](aeb1ba0),
[`18b96f2`](18b96f2),
[`d59afde`](d59afde)]:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`44d99df`](44d99df),
[`dc8eb40`](dc8eb40),
[`edb76b0`](edb76b0),
[`066e875`](066e875),
[`1e50db6`](1e50db6),
[`aeb1ba0`](aeb1ba0),
[`18b96f2`](18b96f2),
[`d59afde`](d59afde)]:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- [#3330](#3330)
[`dc8eb40`](dc8eb40)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - bump zod to
4.6.6

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@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.

2 participants