{"record":{"id":"1189f565fa6bc100","repo":"jackwener/OpenCLI","slug":"target-tab-resolvedtarget-is-not-part-of-the-cu","errorCode":null,"errorMessage":"Target tab ${resolvedTarget} is not part of the current browser session.","messagePattern":"Target tab (.+?) is not part of the current browser session\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cli.ts","lineNumber":1238,"sourceCode":"    }));\n\n  addBrowserTabOption(browserTab.command('close')\n    .argument('[targetId]', 'Target tab/page identity returned by \"browser open\", \"browser tab new\", or \"browser tab list\"')\n    .description('Close a tab by target ID'))\n    .action(browserAction(async (page, targetId?: string, opts?: { tab?: string } | Command) => {\n      const resolvedTarget = resolveBrowserTabTarget(targetId, opts);\n      if (!page.closeTab) {\n        throw new Error('This browser session does not support closing tabs');\n      }\n      if (!resolvedTarget) {\n        throw new Error('Target tab required. Pass it as an argument or --tab <targetId>.');\n      }\n      const validatedTarget = await resolveBrowserTargetInSession(page, resolvedTarget, {\n        scope: getPageScope(page),\n        source: 'explicit',\n      });\n      if (!validatedTarget) {\n        throw new Error(`Target tab ${resolvedTarget} is not part of the current browser session.`);\n      }\n      await page.closeTab(validatedTarget);\n      const scope = getPageScope(page);\n      if (loadBrowserTargetState(scope)?.defaultPage === validatedTarget) {\n        saveBrowserTargetState(undefined, scope);\n      }\n      console.log(JSON.stringify({ closed: validatedTarget }, null, 2));\n    }));\n\n  // ── Navigation ──\n\n  addBrowserTabOption(browser.command('open').argument('<url>').description('Open URL in the browser session'))\n    .action(browserAction(async (page, url) => {\n      // Start session-level capture before navigation (catches initial requests)\n      const hasSessionCapture = await page.startNetworkCapture?.() ?? false;\n      await page.goto(url);\n      await page.wait(2);\n      // Fallback: inject JS interceptor when session capture is unavailable","sourceCodeStart":1220,"sourceCodeEnd":1256,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/src/cli.ts#L1220-L1256","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run `browser state` (or `browser tabs`) again and use a current tab ref from that output.","Verify you are not mixing refs across browser sessions or scope values; start a fresh session if unsure.","Guard the close operation: resolve the target first and skip if it is absent, or wrap the close in try/catch.","Clear stale persisted browser target state (`saveBrowserTargetState(undefined, scope)` path) so old default/page refs are not reused."],"exampleFix":"// before\nawait run(['browser', 'tab', 'close', '3']); // stale ref -> throws\n// after\nconst state = await run(['browser', 'state']);\nif (state.tabs.some(t => t.ref === '3')) {\n  await run(['browser', 'tab', 'close', '3']);\n}","handlingStrategy":"validation","validationCode":"const state = await run(['browser', 'state']);\nif (!state.tabs?.some(t => String(t.ref) === targetRef)) {\n  throw new Error(`Tab ${targetRef} not in current session; pick a ref from browser state`);\n}\nawait run(['browser', 'tab', 'close', targetRef]);","typeGuard":"function isTabInSession(state, ref) {\n  return Array.isArray(state?.tabs) && state.tabs.some(t => String(t.ref) === String(ref));\n}","tryCatchPattern":"try {\n  await run(['browser', 'tab', 'close', ref]);\n} catch (err) {\n  if (String(err.message).includes('is not part of the current browser session')) {\n    const state = await run(['browser', 'state']);\n    const fresh = state.tabs.find(t => t.url === wantedUrl);\n    if (fresh) await run(['browser', 'tab', 'close', fresh.ref]);\n  } else throw err;\n}","preventionTips":["Always fetch fresh refs from `browser state` immediately before closing.","Do not reuse tab refs across sessions or after prior close commands.","Handle self-closing pages (window.close) that invalidate refs between commands.","Clear stale persisted target state between runs."],"tags":["browser","cli","session-state"],"backgroundTag":"stale-target-reference","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}