{"record":{"id":"f505361e6af54e49","repo":"jackwener/OpenCLI","slug":"target-tab-candidate-is-not-part-of-the-current","errorCode":null,"errorMessage":"Target tab ${candidate} is not part of the current browser session. The Browser Bridge session may have restarted; re-run \"opencli browser tab list\" and choose a current target.","messagePattern":"Target tab (.+?) is not part of the current browser session\\. The Browser Bridge session may have restarted; re-run \"opencli browser tab list\" and choose a current target\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cli.ts","lineNumber":591,"sourceCode":"      return undefined;\n    }\n    throw new Error(\n      `Target tab ${candidate} could not be validated in the current browser session. ` +\n      'The Browser Bridge session may have restarted; re-run \"opencli browser tab list\" and choose a current target.',\n      { cause: err },\n    );\n  }\n\n  if (Array.isArray(tabs) && hasBrowserTabTarget(tabs, candidate)) {\n    return candidate;\n  }\n\n  if (opts.source === 'saved') {\n    saveBrowserTargetState(undefined, opts.scope);\n    return undefined;\n  }\n\n  throw new Error(\n    `Target tab ${candidate} is not part of the current browser session. ` +\n    'The Browser Bridge session may have restarted; re-run \"opencli browser tab list\" and choose a current target.',\n  );\n}\n\nfunction getBrowserScope(session: string, contextId?: string): string {\n  return contextId ? `${contextId}:${session}` : session;\n}\n\nasync function resolveStoredBrowserTarget(page: import('./types.js').IPage, scope: string): Promise<string | undefined> {\n  const defaultPage = loadBrowserTargetState(scope)?.defaultPage?.trim();\n  if (!defaultPage) return undefined;\n  return resolveBrowserTargetInSession(page, defaultPage, { scope, source: 'saved' });\n}\n\n/** Create a browser page for browser commands. Uses a named browser session for continuity. */\nasync function getBrowserPage(\n  session: string,","sourceCodeStart":573,"sourceCodeEnd":609,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/src/cli.ts#L573-L609","documentation":"The tab candidate was validated against the live session but is not among the current tabs — i.e. page.tabs() succeeded and the requested target ID/index is gone. The library treats this as a stale-target condition, throws this descriptive error, and (for saved targets) clears the stored target state first.","triggerScenarios":"resolveBrowserTargetInSession is called with a candidate not present in page.tabs(): referencing a tab that was closed, a saved target from a previous browser session, or an index/ID that shifted after tabs were opened or reordered.","commonSituations":"Scripts caching tab IDs across runs; a browser restart invalidating all saved targets; the tab closed by the page itself (window.close) or by another automation run.","solutions":["Run `opencli browser tab list` and choose a currently listed target.","Clear the saved browser target state so future runs re-resolve fresh (it is auto-cleared when source is 'saved').","Make scripts resolve tabs by URL/pattern at run time instead of persisting IDs.","Reopen the needed tab if it was closed."],"exampleFix":"// before\nawait runBrowser(savedTargetId);\n// after\nconst tabs = await page.tabs();\nconst target = tabs.find(t => t.url.startsWith('https://app.example.com'));\nif (!target) throw new Error('target tab not found; re-list tabs');","handlingStrategy":"fallback","validationCode":"async function targetExists(page, candidate) {\n  const tabs = await page.tabs();\n  return tabs.some(t => String(t.id) === String(candidate));\n}","typeGuard":"function isCurrentTab(candidate: string, tabs: Tab[]): boolean {\n  return tabs.some(t => String(t.id) === String(candidate));\n}","tryCatchPattern":"try {\n  await resolveBrowserTargetInSession(page, candidate, opts);\n} catch (err) {\n  if (err instanceof Error && err.message.includes('is not part of the current browser session')) {\n    const tabs = await page.tabs();\n    return resolveBrowserTargetInSession(page, tabs[0]?.id, opts);\n  }\n  throw err;\n}","preventionTips":["Resolve tabs by URL or title pattern at run time instead of saved IDs.","Clear saved target state (or let source:'saved' auto-clear it) after browser restarts.","Re-run `opencli browser tab list` whenever tabs were opened/closed.","Avoid closing tabs from parallel automation while a target is in use."],"tags":["browser","stale-state","tab-targeting","session"],"backgroundTag":"stale-browser-target","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}