Add context to browser handles - #2582
Open
miguelg719 wants to merge 1 commit into
Open
Conversation
|
Contributor
There was a problem hiding this comment.
cubic analysis
4 issues found across 7 files
Confidence score: 2/5
- In
packages/sdk-go/browser.go,Createstill sets context onStagehandand does not populatebrowser.contextvia the attach flow, so the documented post-attach access path can fail in Go clients — wireCreate/initialization throughattachBrowserContextsobrowser.contextis reliably set. - In
packages/sdk-python/src/stagehand/browser.py, attach/detach primitives exist but are not connected to Stagehand runtime initialization, which can leavebrowser.contextunavailable when users follow the new API pattern — hook attachment into the initialization lifecycle and validate with an integration test. - In
packages/sdk-ts/src/browser/index.ts, attachment is not integrated into the Stagehand lifecycle, sobrowser.contextcan remain unset in the canonical TypeScript path, creating cross-SDK behavior drift and user-facing regressions — invoke attach during Stagehand startup and assertbrowser.contextafter attach. - In
packages/sdk-go/browser_test.go, bothBrowser.Context()andStagehand.Context()remain public/in use, so callers can keep depending on the old path and mask migration gaps — converge tests and public usage onbrowser.context(or explicitly deprecate the old accessor).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/sdk-python/src/stagehand/browser.py">
<violation number="1" location="packages/sdk-python/src/stagehand/browser.py:176">
P1: According to linked Linear issue STG-2763, `browser.context` should be the API access pattern after attachment, but this change adds attach/detach primitives without wiring them into runtime Stagehand initialization/teardown. As shipped, `browser.context` stays unavailable in real usage because the browser handle is never populated.</violation>
</file>
<file name="packages/sdk-go/browser_test.go">
<violation number="1" location="packages/sdk-go/browser_test.go:23">
P2: According to linked Linear issue STG-2763, the API should expose one access pattern (browser.context) rather than both. This change adds Browser.Context() but leaves Stagehand.Context() public and still in use, so both patterns remain exposed after this PR. Consider removing or deprecating Stagehand.Context() and migrating remaining callers (stagehand.go:355 and the live tests) to the browser handle so the access pattern is singular.</violation>
</file>
<file name="packages/sdk-go/browser.go">
<violation number="1" location="packages/sdk-go/browser.go:79">
P1: According to linked Linear issue STG-2763, `browser.context` should become available after attach, but this Go path is never populated. `Create` still writes the context to `Stagehand` and never calls `attachBrowserContext`, so `Browser.Context()` will keep returning `ErrNotInitialized` in normal use.</violation>
</file>
<file name="packages/sdk-ts/src/browser/index.ts">
<violation number="1" location="packages/sdk-ts/src/browser/index.ts:71">
P1: According to linked Linear issue STG-2763, `browser.context` should become the canonical attached context path, but this implementation never wires attachment into the Stagehand lifecycle. `browser.context` will always throw in real usage unless `attachStagehandBrowserContext` is called manually; consider attaching after successful `Stagehand.create` init and detaching during close.</violation>
</file>
Linked issue analysis
Linked issue: STG-2763: Change stagehand.context to browser.context
| Status | Acceptance criteria | Notes |
|---|---|---|
| ✅ | Expose browser.context on browser handles (SDK surface) | Each SDK adds a context property to the browser handle/interface: TypeScript StagehandBrowser includes context and accessor; Python StagehandBrowser exposes .context; Go Browser.Context() method implemented. |
| ✅ | Block access until Stagehand claims and attaches the browser; error on early access | All SDKs throw or return an error when context is accessed before attach; the code enforces claimed check before attaching. |
| ✅ | Provide internal attach/detach primitives (TypeScript, Python, Go) and prevent double-attach | Each language adds attach/detach functions and checks for claimed state and existing context to prevent replacement. |
| ✅ | Add lifecycle tests for claim/attach/detach and error paths across SDKs | Tests were added/modified to cover unavailable-before-attach, attach success, reject second attach, and detachment behavior in each SDK test suite. |
Architecture diagram
sequenceDiagram
participant User
participant SH as Stagehand (SDK)
participant BH as Browser Handle (TS/Py/Go)
participant BC as BrowserContext
Note over User,BC: Initialization Phase
User->>BH: GET browser.context
alt NEW: Context not yet attached
BH-->>User: Throw Error (Runtime/ErrNotInitialized)
end
User->>SH: Stagehand.create({ browser: handle })
rect rgb(240, 240, 240)
Note right of SH: NEW: Attachment Lifecycle
SH->>BH: claimBrowser()
SH->>BC: Create Context
SH->>BH: NEW: attachBrowserContext(context)
BH->>BH: Store context reference
end
SH-->>User: Stagehand Instance
Note over User,BC: Usage Phase
User->>BH: GET browser.context
BH-->>User: Return context
opt NEW: Attempt duplicate attachment
SH->>BH: attachBrowserContext(new_context)
BH-->>SH: Throw Error ("already has a context")
end
Note over User,BC: Cleanup Phase
User->>SH: Close / De-init
SH->>BH: NEW: detachBrowserContext()
BH->>BH: context = nil / undefined
SH->>BH: releaseBrowserClaim() (Existing)
SH-->>User: Done
User->>BH: GET browser.context
BH-->>User: Throw Error (Context unavailable)
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
miguelg719
force-pushed
the
miguelgonzalez/stg-2763-change-stagehandcontext-to-browsercontext
branch
from
August 4, 2026 19:27
c0c302e to
67b5cf2
Compare
miguelg719
force-pushed
the
miguelgonzalez/stg-2763-change-stagehandcontext-to-browsercontext
branch
from
August 4, 2026 20:23
67b5cf2 to
f893bcf
Compare
miguelg719
force-pushed
the
miguelgonzalez/stg-2763-change-stagehandcontext-to-browsercontext
branch
from
August 4, 2026 20:25
f893bcf to
2ccb22b
Compare
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.
why
Browser context belongs to the factory-created browser handle and should only become available after Stagehand attaches.
what changed
Adds guarded context access plus internal attach/detach primitives across TypeScript, Python, and Go. Includes focused lifecycle tests.
No changeset: this targets the unreleased v4 API.
test plan
Summary by cubic
Adds
browser.contextto Stagehand browser handles and makes it available only after Stagehand claims and attaches the browser. Aligns the v4 API with STG-2763 by preferringbrowser.contextoverstagehand.context.browser.context(BrowserContext) on handles in TypeScript, Python, and Go; available post-claim + attach; errors on early access or re-attach; cleared on detach.context.DomainPolicyInputandActionInput.Written for commit 2ccb22b. Summary will update on new commits.