jackwener/OpenCLI · error

This browser session does not support closing tabs

Error message

This browser session does not support closing tabs

What it means

The `browser tab close` subcommand requires the page's optional closeTab capability; when the current session's page does not implement it, the CLI throws before attempting the close. Like 5266, this is a capability guard for sessions that cannot close tabs.

Source

Thrown at src/cli.ts:1228

    .argument('[targetId]', 'Target tab/page identity returned by "browser open", "browser tab new", or "browser tab list"')
    .description('Select a tab by target ID and make it the default browser tab'))
    .action(browserAction(async (page, targetId?: string, opts?: { tab?: string } | Command) => {
      const resolvedTarget = resolveBrowserTabTarget(targetId, opts);
      if (!resolvedTarget) {
        throw new Error('Target tab required. Pass it as an argument or --tab <targetId>.');
      }
      await page.selectTab(resolvedTarget);
      saveBrowserTargetState(resolvedTarget, getPageScope(page));
      console.log(JSON.stringify({ selected: resolvedTarget }, null, 2));
    }));

  addBrowserTabOption(browserTab.command('close')
    .argument('[targetId]', 'Target tab/page identity returned by "browser open", "browser tab new", or "browser tab list"')
    .description('Close a tab by target ID'))
    .action(browserAction(async (page, targetId?: string, opts?: { tab?: string } | Command) => {
      const resolvedTarget = resolveBrowserTabTarget(targetId, opts);
      if (!page.closeTab) {
        throw new Error('This browser session does not support closing tabs');
      }
      if (!resolvedTarget) {
        throw new Error('Target tab required. Pass it as an argument or --tab <targetId>.');
      }
      const validatedTarget = await resolveBrowserTargetInSession(page, resolvedTarget, {
        scope: getPageScope(page),
        source: 'explicit',
      });
      if (!validatedTarget) {
        throw new Error(`Target tab ${resolvedTarget} is not part of the current browser session.`);
      }
      await page.closeTab(validatedTarget);
      const scope = getPageScope(page);
      if (loadBrowserTargetState(scope)?.defaultPage === validatedTarget) {
        saveBrowserTargetState(undefined, scope);
      }
      console.log(JSON.stringify({ closed: validatedTarget }, null, 2));
    }));

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Use a session type that owns the browser and supports closing tabs
  2. Verify the browser context permits closing targets (some managed contexts forbid it)
  3. Update the adapter/driver to implement closeTab
  4. Close the tab manually in the browser if the session cannot

Example fix

// before
opencli browser attached-session tab close 1A2B3C4D5E6F  // throws
// after
opencli browser launched-session tab close 1A2B3C4D5E6F
Defensive patterns

Strategy: try-catch

Validate before calling

if (typeof (page as { closeTab?: unknown }).closeTab !== 'function') {
  throw new Error('This session does not support closing tabs; use a launched browser session.');
}

Type guard

function supportsCloseTab(page: IPage): page is IPage & { closeTab: (targetId: string) => Promise<void> } {
  return typeof (page as { closeTab?: unknown }).closeTab === 'function';
}

Try / catch

try {
  await run(['opencli', 'browser', session, 'tab', 'close', targetId]);
} catch (e) {
  if (String((e as Error).message).includes('does not support closing tabs')) {
    console.error(`Session '${session}' cannot close tabs; close them in the browser or use a launched session.`);
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: Running `opencli browser <session> tab close [targetId]` against a session whose page lacks closeTab (page.closeTab is undefined).

Common situations: Attached/externally managed browser contexts where the CLI may not close pages; restricted remote sessions; older adapter versions without closeTab.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/d7320868dc9d13c1. Report an issue: GitHub.