fix(stagehand): wait for Stagehand to register a page before using it#3865
Merged
Conversation
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.
barjin
approved these changes
Jul 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
StagehandCrawlerintermittently fails withFailed to resolve V3 Page from Playwright pagewhen 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.attachedToTargetevents on its own connection and maps them by main frame id. Because we create pages through a separateconnectOverCDP()handle,context.newPage()can resolve before Stagehand has processed that event, leaving the page unresolvable for a short window. Stagehand's ownnewPage()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.