Skip to content

fix(stagehand): wait for Stagehand to register a page before using it#3865

Merged
B4nan merged 2 commits into
masterfrom
claude/fix-stagehand-page-registration
Jul 16, 2026
Merged

fix(stagehand): wait for Stagehand to register a page before using it#3865
B4nan merged 2 commits into
masterfrom
claude/fix-stagehand-page-registration

Conversation

@B4nan

@B4nan B4nan commented Jul 16, 2026

Copy link
Copy Markdown
Member

StagehandCrawler intermittently fails with Failed to resolve V3 Page from Playwright page when a page's AI methods (extract()/act()/observe()) run right after the page is created — most visibly under concurrency, where it looks like a "poisoned" browser instance whose every request keeps failing.

Stagehand learns about pages from CDP Target.attachedToTarget events on its own connection and maps them by main frame id. Because we create pages through a separate connectOverCDP() handle, context.newPage() can resolve before Stagehand has processed that event, leaving the page unresolvable for a short window. Stagehand's own newPage() polls for exactly this reason.

The fix makes _newPage() wait until Stagehand has registered the page (resolving it by main frame id) before returning it, with a 10s timeout. If registration never happens — e.g. Stagehand couldn't install its helper script into the page's CDP session — the page is closed and an error is thrown rather than handed back in an unusable state.

Stagehand launches the browser and we attach a second Playwright handle to it
over CDP, so pages we create with `context.newPage()` are unknown to Stagehand
until it processes its own `Target.attachedToTarget` event. Its AI methods
resolve pages by main frame id, so calling `extract()` inside that window fails
with 'Failed to resolve V3 Page from Playwright page'. Stagehand's own
`newPage()` polls until the target is registered; we bypassed it.

The window was normally hidden by navigation happening before the request
handler runs, which is why this only surfaced as a flaky e2e test.

Wait for the page to be resolvable before handing it out, mirroring what
Stagehand does. This also surfaces a clear error when Stagehand skips
registration for good (it does that when it cannot install its helper script
into the page's CDP session) instead of failing later inside the handler.
@B4nan B4nan added the adhoc Ad-hoc unplanned task added during the sprint. label Jul 16, 2026
@github-actions github-actions Bot added this to the 145th sprint - Tooling team milestone Jul 16, 2026
@github-actions github-actions Bot added t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics. labels Jul 16, 2026
@B4nan
B4nan requested a review from barjin July 16, 2026 08:24
Comment thread packages/stagehand-crawler/src/internals/stagehand-controller.ts Outdated
@B4nan
B4nan merged commit 238214f into master Jul 16, 2026
8 checks passed
@B4nan
B4nan deleted the claude/fix-stagehand-page-registration branch July 16, 2026 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants