Skip to content

[BUG] @mastra/agent-browser: Browser fails to initialize with thread scope (default) #15283

@chaoticsmol

Description

@chaoticsmol

Bug Description

When using @mastra/agent-browser with the default scope: 'thread' configuration, the browser fails to initialize. The launch() method completes successfully but getPage() throws "Browser not launched. Call launch first."

Environment

  • @mastra/agent-browser: v0.1.0
  • agent-browser: v0.19.0
  • Node.js: $(node --version 2>/dev/null || echo "unknown")
  • Platform: macOS (Darwin)
  • Chrome installed: Yes

Steps to Reproduce

import { AgentBrowser } from '@mastra/agent-browser';

const browser = new AgentBrowser({ headless: false }); // scope defaults to 'thread'
await browser.launch();
const page = await browser.getPage(); // throws: "Browser not launched. Call launch first."

Root Cause Analysis

In @mastra/agent-browser, the doLaunch() method (lines 561-580) has this logic:

async doLaunch() {
  const scope = this.threadManager.getScope();
  if (scope === "thread") {
    this.sharedManager = new BrowserManager();
    this.threadManager.setSharedManager(this.sharedManager);
    return; // Returns early WITHOUT launching the browser!
  }
  // shared scope continues to launch...
  await this.sharedManager.launch(launchOptions);
}

For thread scope, the browser is supposed to launch lazily when a thread session is created via createSession(). However, in AgentBrowserThreadManager.createSession():

if (this.scope === "thread") {
  // ...
  if (threadId !== DEFAULT_THREAD_ID && !existingSession) {
    await this.getManagerForThread(threadId);  // Only creates if threadId !== DEFAULT_THREAD_ID
  }
}

The lazy launch only triggers when threadId !== DEFAULT_THREAD_ID. When using the default thread ID, no browser is ever launched.

Workaround

Use scope: 'shared' instead:

const browser = new AgentBrowser({ 
  headless: false,
  scope: 'shared'
});

Expected Behavior

Browser should initialize properly with the default thread scope configuration, or the documentation should clearly state that thread scope requires explicit thread ID management.

Impact

  • All users relying on default configuration are affected
  • Breaking change for anyone migrating to this package
  • No clear error message indicating the configuration issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions