jackwener/OpenCLI · error

Target tab ${resolvedTarget} is not part of the current brow

Error message

Target tab ${resolvedTarget} is not part of the current browser session.

What it means

Thrown by the `browser tab close` command when the target tab reference resolves to a page that is not part of the current browser session. `resolveBrowserTargetInSession` returns null when the requested tab (numeric ref or identifier) cannot be matched against pages belonging to the current session scope, so closing is refused to avoid closing a wrong or foreign page.

Source

Thrown at src/cli.ts:1238

    }));

  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));
    }));

  // ── Navigation ──

  addBrowserTabOption(browser.command('open').argument('<url>').description('Open URL in the browser session'))
    .action(browserAction(async (page, url) => {
      // Start session-level capture before navigation (catches initial requests)
      const hasSessionCapture = await page.startNetworkCapture?.() ?? false;
      await page.goto(url);
      await page.wait(2);
      // Fallback: inject JS interceptor when session capture is unavailable

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Run `browser state` (or `browser tabs`) again and use a current tab ref from that output.
  2. Verify you are not mixing refs across browser sessions or scope values; start a fresh session if unsure.
  3. Guard the close operation: resolve the target first and skip if it is absent, or wrap the close in try/catch.
  4. Clear stale persisted browser target state (`saveBrowserTargetState(undefined, scope)` path) so old default/page refs are not reused.

Example fix

// before
await run(['browser', 'tab', 'close', '3']); // stale ref -> throws
// after
const state = await run(['browser', 'state']);
if (state.tabs.some(t => t.ref === '3')) {
  await run(['browser', 'tab', 'close', '3']);
}
Defensive patterns

Strategy: validation

Validate before calling

const state = await run(['browser', 'state']);
if (!state.tabs?.some(t => String(t.ref) === targetRef)) {
  throw new Error(`Tab ${targetRef} not in current session; pick a ref from browser state`);
}
await run(['browser', 'tab', 'close', targetRef]);

Type guard

function isTabInSession(state, ref) {
  return Array.isArray(state?.tabs) && state.tabs.some(t => String(t.ref) === String(ref));
}

Try / catch

try {
  await run(['browser', 'tab', 'close', ref]);
} catch (err) {
  if (String(err.message).includes('is not part of the current browser session')) {
    const state = await run(['browser', 'state']);
    const fresh = state.tabs.find(t => t.url === wantedUrl);
    if (fresh) await run(['browser', 'tab', 'close', fresh.ref]);
  } else throw err;
}

Prevention

When it happens

Trigger: Running `browser tab close <target>` where the ref/selector does not match any tab in the current session; the target tab was already closed; the ref came from a stale or other-session `browser state` output; or browser target state persisted from a previous run is reused.

Common situations: Scripting tab cleanup after a tab was already closed by the page itself (window.close) or a previous command; copying tab refs between concurrent browser sessions; stale saved target state in the scope's persisted browser state.

Related errors


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