Skip to content

Add /v4/browsersession API routes#1876

Draft
pirate wants to merge 2 commits intomainfrom
v4-browsersession-routes
Draft

Add /v4/browsersession API routes#1876
pirate wants to merge 2 commits intomainfrom
v4-browsersession-routes

Conversation

@pirate
Copy link
Copy Markdown
Member

@pirate pirate commented Mar 23, 2026

why

what changed

test plan


Summary by cubic

Adds v4 BrowserSession routes and a pluggable session store with a default in‑memory LRU/TTL cache to improve session lifecycle and performance.

  • New Features
    • Routes: POST /v4/browsersession/close, /isAdvancedStealth, /isBrowserbase, /setViewportSize.
    • Session store: SessionStore interface and default InMemorySessionStore (lazy V3 init, LRU eviction, TTL, periodic cleanup) managed via sessionStoreManager.
    • All routes use the shared action handler and fastify-zod-openapi schemas; errors return the standard action error envelope.
    • Integration tests cover context/browser helpers (e.g., connectURL, viewport, pages, cookies) and validate the action error envelope.

Written for commit 9952cf6. Summary will update on new commits. Review in cubic

@pirate pirate marked this pull request as draft March 23, 2026 22:36
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 23, 2026

⚠️ No Changeset found

Latest commit: 9952cf6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

5 issues found across 5 files

Confidence score: 2/5

  • High merge risk: there are concrete, high-confidence runtime failures in new handlers, including stagehand being dereferenced without being provided in packages/server-v4/src/routes/v4/browsersession/setViewportSize.ts and sessionStore.endSession(...) being called on undefined in packages/server-v4/src/routes/v4/browsersession/close.ts.
  • Several new routes appear unreachable because they are not registered in the route plugin/routes.ts (packages/server-v4/src/routes/v4/browsersession/close.ts, packages/server-v4/src/routes/v4/browsersession/setViewportSize.ts, packages/server-v4/src/routes/v4/browsersession/isBrowserbase.ts), so intended API behavior will not be exposed at runtime.
  • Given the 7-9/10 severities with strong confidence, this is likely to cause immediate endpoint regressions rather than minor polish issues.
  • Pay close attention to packages/server-v4/src/routes/v4/browsersession/routes.ts, packages/server-v4/src/routes/v4/browsersession/close.ts, packages/server-v4/src/routes/v4/browsersession/setViewportSize.ts, packages/server-v4/src/routes/v4/browsersession/isBrowserbase.ts - register all new routes and ensure required handler dependencies (stagehand, sessionStore) are actually passed.
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/server-v4/src/routes/v4/browsersession/isBrowserbase.ts">

<violation number="1" location="packages/server-v4/src/routes/v4/browsersession/isBrowserbase.ts:14">
P2: This route is defined but never registered in `browserSessionRoutes`, so the `/v4/browsersession/isBrowserbase` endpoint won’t be reachable. Add it to `packages/server-v4/src/routes/v4/browsersession/routes.ts`.</violation>
</file>

<file name="packages/server-v4/src/routes/v4/browsersession/setViewportSize.ts">

<violation number="1" location="packages/server-v4/src/routes/v4/browsersession/setViewportSize.ts:14">
P1: This route is not imported/registered in the browser session route plugin, so the new endpoint is unreachable.</violation>

<violation number="2" location="packages/server-v4/src/routes/v4/browsersession/setViewportSize.ts:29">
P1: The handler dereferences `stagehand`, but `createBrowserSessionActionHandler` never passes `stagehand`, causing a runtime crash on this endpoint.</violation>
</file>

<file name="packages/server-v4/src/routes/v4/browsersession/close.ts">

<violation number="1" location="packages/server-v4/src/routes/v4/browsersession/close.ts:14">
P0: Dead code: The newly added route files are not imported or registered in the routes.ts file. They are exported but never consumed, making them unreachable at runtime.</violation>

<violation number="2" location="packages/server-v4/src/routes/v4/browsersession/close.ts:30">
P1: `sessionStore` is passed as `undefined` by the shared action handler, so calling `sessionStore.endSession(...)` will crash with a runtime TypeError.</violation>
</file>
Architecture diagram
sequenceDiagram
    participant Client
    participant Router as Fastify Router (v4)
    participant Handler as Shared Action Handler
    participant Store as SessionStoreManager
    participant SH as Stagehand Instance
    participant PW as Playwright / Browser

    Note over Client,PW: NEW: Browser Session Management Flow (v4)

    Client->>Router: POST /v4/browsersession/[method]
    Note right of Client: Includes sessionId & params

    Router->>Handler: Execute Route Handler
    
    Handler->>Store: NEW: Get session (sessionId)
    alt Session Not Found
        Store-->>Handler: null
        Handler-->>Client: 404 Action Error Envelope
    else Session Active
        Store-->>Handler: Stagehand Instance
        
        Handler->>Handler: NEW: Validate params via Zod Schema
        
        alt method == "setViewportSize"
            Handler->>SH: context.awaitActivePage()
            SH-->>Handler: Page Instance
            Handler->>PW: page.setViewportSize(w, h)
            PW-->>Handler: void
        else method == "close"
            Handler->>Store: NEW: endSession(sessionId)
            Store->>SH: close()
            Store->>Store: Remove from LRU Cache
        else method == "isBrowserbase"
            Handler->>SH: Access .isBrowserbase property
        end

        Handler-->>Client: 200 Success (Standard Action Envelope)
    end

    Note over Store: InMemorySessionStore: Handles TTL,<br/>LRU eviction, and lazy V3 init
Loading

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

createBrowserSessionActionHandler,
} from "./shared.js";

const closeRoute: RouteOptions = {
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 23, 2026

Choose a reason for hiding this comment

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

P0: Dead code: The newly added route files are not imported or registered in the routes.ts file. They are exported but never consumed, making them unreachable at runtime.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/server-v4/src/routes/v4/browsersession/close.ts, line 14:

<comment>Dead code: The newly added route files are not imported or registered in the routes.ts file. They are exported but never consumed, making them unreachable at runtime.</comment>

<file context>
@@ -0,0 +1,40 @@
+  createBrowserSessionActionHandler,
+} from "./shared.js";
+
+const closeRoute: RouteOptions = {
+  method: "POST",
+  url: "/browsersession/close",
</file context>
Fix with Cubic

createBrowserSessionActionHandler,
} from "./shared.js";

const setViewportSizeRoute: RouteOptions = {
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 23, 2026

Choose a reason for hiding this comment

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

P1: This route is not imported/registered in the browser session route plugin, so the new endpoint is unreachable.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/server-v4/src/routes/v4/browsersession/setViewportSize.ts, line 14:

<comment>This route is not imported/registered in the browser session route plugin, so the new endpoint is unreachable.</comment>

<file context>
@@ -0,0 +1,48 @@
+  createBrowserSessionActionHandler,
+} from "./shared.js";
+
+const setViewportSizeRoute: RouteOptions = {
+  method: "POST",
+  url: "/browsersession/setViewportSize",
</file context>
Fix with Cubic

handler: createBrowserSessionActionHandler({
method: "setViewportSize",
actionSchema: BrowserSessionSetViewportSizeActionSchema,
execute: async ({ stagehand, params }) => {
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 23, 2026

Choose a reason for hiding this comment

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

P1: The handler dereferences stagehand, but createBrowserSessionActionHandler never passes stagehand, causing a runtime crash on this endpoint.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/server-v4/src/routes/v4/browsersession/setViewportSize.ts, line 29:

<comment>The handler dereferences `stagehand`, but `createBrowserSessionActionHandler` never passes `stagehand`, causing a runtime crash on this endpoint.</comment>

<file context>
@@ -0,0 +1,48 @@
+  handler: createBrowserSessionActionHandler({
+    method: "setViewportSize",
+    actionSchema: BrowserSessionSetViewportSizeActionSchema,
+    execute: async ({ stagehand, params }) => {
+      const page = await stagehand.context.awaitActivePage();
+      await page.setViewportSize(params.width, params.height, {
</file context>
Fix with Cubic

method: "close",
actionSchema: BrowserSessionCloseActionSchema,
execute: async ({ sessionId, sessionStore }) => {
await sessionStore.endSession(sessionId);
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 23, 2026

Choose a reason for hiding this comment

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

P1: sessionStore is passed as undefined by the shared action handler, so calling sessionStore.endSession(...) will crash with a runtime TypeError.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/server-v4/src/routes/v4/browsersession/close.ts, line 30:

<comment>`sessionStore` is passed as `undefined` by the shared action handler, so calling `sessionStore.endSession(...)` will crash with a runtime TypeError.</comment>

<file context>
@@ -0,0 +1,40 @@
+    method: "close",
+    actionSchema: BrowserSessionCloseActionSchema,
+    execute: async ({ sessionId, sessionStore }) => {
+      await sessionStore.endSession(sessionId);
+      return {
+        result: {
</file context>
Fix with Cubic

createBrowserSessionActionHandler,
} from "./shared.js";

const isBrowserbaseRoute: RouteOptions = {
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 23, 2026

Choose a reason for hiding this comment

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

P2: This route is defined but never registered in browserSessionRoutes, so the /v4/browsersession/isBrowserbase endpoint won’t be reachable. Add it to packages/server-v4/src/routes/v4/browsersession/routes.ts.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/server-v4/src/routes/v4/browsersession/isBrowserbase.ts, line 14:

<comment>This route is defined but never registered in `browserSessionRoutes`, so the `/v4/browsersession/isBrowserbase` endpoint won’t be reachable. Add it to `packages/server-v4/src/routes/v4/browsersession/routes.ts`.</comment>

<file context>
@@ -0,0 +1,39 @@
+  createBrowserSessionActionHandler,
+} from "./shared.js";
+
+const isBrowserbaseRoute: RouteOptions = {
+  method: "POST",
+  url: "/browsersession/isBrowserbase",
</file context>
Fix with Cubic

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.

1 participant