Skip to content

Conversation

@seanmcguire12
Copy link
Member

why

  • currently, we add the init scripts to the main world
  • this is problematic, because page.evaluate() uses the v3-world
  • this means that you can't easily evaluate the script you add with addInitScript()

what changed

  • makes sure that init scripts are added to the v3-world

test plan

  • added a test which calls page.evaluate() on a script added via context.addInitScript()

@changeset-bot
Copy link

changeset-bot bot commented Nov 25, 2025

🦋 Changeset detected

Latest commit: 6d78851

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

This PR includes changesets to release 2 packages
Name Type
@browserbasehq/stagehand Patch
@browserbasehq/stagehand-evals 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

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Nov 25, 2025

Greptile Overview

Greptile Summary

Fixed context.addInitScript() to inject scripts into the v3-world isolated execution context instead of the main world, ensuring that page.evaluate() can access functions and globals added via init scripts.

Key changes:

  • Modified installInitScriptOnSession() in packages/core/lib/v3/understudy/page.ts:135-138 to include worldName: "v3-world" parameter
  • Added comprehensive test case verifying the fix works as expected
  • Change is consistent with how other v3 components (Frame, selectorResolver) use isolated worlds throughout the codebase

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The fix is a simple, targeted two-line change that correctly aligns init scripts with the isolated world pattern used throughout the v3 codebase. The change is well-tested with a new test case, follows existing patterns, and the implementation is correct
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
packages/core/lib/v3/understudy/page.ts 5/5 Added worldName: "v3-world" to installInitScriptOnSession() to ensure init scripts execute in the same isolated world as page.evaluate(), making injected globals accessible
packages/core/lib/v3/tests/context-addInitScript.spec.ts 5/5 Added test verifying that functions injected via context.addInitScript() are callable from page.evaluate(), validating the isolated world fix
.changeset/seven-mice-draw.md 5/5 Changeset correctly describes the fix as a patch-level change with accurate user-facing description

Sequence Diagram

sequenceDiagram
    participant User
    participant Context
    participant Page
    participant CDPSession
    participant Browser
    participant IsolatedWorld as v3-world (Isolated)

    User->>Context: addInitScript(fn)
    Context->>Page: registerInitScript(source)
    Page->>Page: Check if script already exists
    Page->>Page: Add to initScripts array
    Page->>CDPSession: Page.addScriptToEvaluateOnNewDocument
    Note over CDPSession,Browser: worldName: "v3-world" (NEW)
    CDPSession->>Browser: Install script in v3-world
    Browser->>IsolatedWorld: Script registered for future docs
    
    User->>Page: goto(url)
    Browser->>IsolatedWorld: Execute init script in v3-world
    Note over IsolatedWorld: window.sayHelloFromStagehand = ...
    
    User->>Page: evaluate(fn)
    Page->>Page: createIsolatedWorldForCurrentMain()
    Note over Page,IsolatedWorld: Creates/reuses v3-world context
    Page->>IsolatedWorld: Runtime.evaluate in v3-world
    IsolatedWorld-->>Page: Access to init script globals
    Page-->>User: Result
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

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