{"record":{"id":"7135f984f2767c15","repo":"jackwener/OpenCLI","slug":"chatgpt-navigated-away-from-the-target-conversatio","errorCode":null,"errorMessage":"ChatGPT navigated away from the target conversation (${options.conversationUrl}); current URL is ${currentUrl}","messagePattern":"ChatGPT navigated away from the target conversation \\((.+?)\\); current URL is (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/chatgpt/utils.js","lineNumber":2215,"sourceCode":"        const key = responsePairKey(user, assistant);\n        if ((currentPairCounts.get(key) || 0) <= (baselinePairCounts.get(key) || 0)) continue;\n        return String(assistant.Text || '').trim();\n    }\n    return '';\n}\n\nexport async function waitForChatGPTResponse(page, baselineCount, prompt, timeoutSeconds, options = {}) {\n    const startTime = Date.now();\n    let lastText = '';\n    let stableCount = 0;\n    const baselinePairCounts = normalizeBaselinePairCounts(options);\n\n    while (Date.now() - startTime < timeoutSeconds * 1000) {\n        await page.sleep(3);\n        if (options.conversationUrl) {\n            const currentUrl = await currentChatGPTUrl(page);\n            if (currentUrl && !isSameChatGPTConversation(currentUrl, options.conversationUrl)) {\n                throw new CommandExecutionError(\n                    `ChatGPT navigated away from the target conversation (${options.conversationUrl}); current URL is ${currentUrl}`,\n                );\n            }\n        }\n        if (await isGenerating(page)) {\n            stableCount = 0;\n            continue;\n        }\n\n        const messages = await getVisibleMessages(page, { textOnly: true });\n        const candidate = findLatestNewAssistantResponse(messages, prompt, baselinePairCounts);\n        if (!candidate || candidate === String(prompt || '').trim()) continue;\n\n        if (candidate === lastText) {\n            stableCount += 1;\n            if (stableCount >= 2) return candidate;\n        } else {\n            lastText = candidate;","sourceCodeStart":2197,"sourceCodeEnd":2233,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/chatgpt/utils.js#L2197-L2233","documentation":"While waiting for an ask/response to stabilize, the library periodically compares the current ChatGPT URL to the conversation URL it is monitoring. If the browser navigated to a different conversation, the polling would read the wrong text, so it throws immediately.","triggerScenarios":"During the ask wait loop with options.conversationUrl set, currentChatGPTUrl(page) returns a URL that fails isSameChatGPTConversation — a navigation to another conversation, the home page, or a login redirect occurred mid-wait.","commonSituations":"Session expired and ChatGPT redirected to login; user or another script navigated the shared tab; clicking a suggestion opened a new conversation; SPA redirect after an error.","solutions":["Re-run the ask and avoid navigating the tab while waiting","Re-authenticate if a login redirect happened, then retry","Pin the automation to a dedicated tab/profile so nothing else navigates it","If a fork to a new conversation is expected, use the new URL via isSameChatGPTConversation-aware options or disable the URL guard"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"const url = await currentChatGPTUrl(page);\nif (options.conversationUrl && !isSameChatGPTConversation(url, options.conversationUrl)) {\n  throw new Error('already off the target conversation before waiting');\n}","typeGuard":"function onTargetConversation(currentUrl, targetUrl) { return !!currentUrl && isSameChatGPTConversation(currentUrl, targetUrl); }","tryCatchPattern":"try {\n  await waitForChatGPTAskResult(page, { conversationUrl });\n} catch (e) {\n  if (String(e.message).startsWith('ChatGPT navigated away')) {\n    // re-authenticate if redirected to login, then restart the ask on a fresh navigation\n  } else throw e;\n}","preventionTips":["Never navigate the shared tab while an ask wait is in flight","Run each ask in a dedicated page/tab","Handle login redirects by re-authenticating before retrying"],"tags":["chatgpt","navigation","url","stability-check"],"backgroundTag":"unexpected-page-navigation","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}