{"record":{"id":"797869c88d903ea0","repo":"jackwener/OpenCLI","slug":"failed-to-detect-geogebra-applet-err-message","errorCode":null,"errorMessage":"Failed to detect GeoGebra applet: ${err?.message || err}","messagePattern":"Failed to detect GeoGebra applet: (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/geogebra/utils.js","lineNumber":85,"sourceCode":"\n/**\n * Navigate to GeoGebra Geometry (if not already there) and wait for\n * the ggbApplet API to become available.\n */\nexport async function ensureApplet(page) {\n  let currentUrl = '';\n  try {\n    currentUrl = await page.getCurrentUrl();\n  } catch {\n    currentUrl = '';\n  }\n  // If already on the geometry page, check if applet is ready without re-navigating\n  if (currentUrl?.includes('geogebra.org/geometry')) {\n    try {\n      const ready = unwrapBridgeEnvelope(await page.evaluate(`typeof ggbApplet !== 'undefined' && typeof ggbApplet.evalCommand === 'function'`));\n      if (ready) return;\n    } catch (err) {\n      throw new CommandExecutionError(`Failed to detect GeoGebra applet: ${err?.message || err}`);\n    }\n  }\n  // Navigate to GeoGebra Geometry\n  try {\n    await page.goto(GEOGEBRA_URL);\n  } catch (err) {\n    throw new CommandExecutionError(`Failed to load GeoGebra Geometry: ${err?.message || err}`);\n  }\n\n  let ready;\n  try {\n    ready = unwrapBridgeEnvelope(await page.evaluate(`\n      (async () => {\n        const deadline = Date.now() + ${APPLET_WAIT_MS};\n        while (Date.now() < deadline) {\n          if (typeof ggbApplet !== 'undefined' && typeof ggbApplet.evalCommand === 'function') {\n            return true;\n          }","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/geogebra/utils.js#L67-L103","documentation":"ensureApplet() first checks whether an active GeoGebra page already has a usable ggbApplet. If the page.evaluate probe itself throws (page crashed, closed, navigation in progress, script blocked), it rethrows as CommandExecutionError 'Failed to detect GeoGebra applet: ...' wrapping the underlying browser error message.","triggerScenarios":"Calling any geometry tool while the page context is destroyed: tab closed, browser crashed, a navigation interrupted the evaluate, CSP or an extension blocked the evalCommand existence check, or the 'geogebra.org/geometry' URL matches but the document is about:blank/broken.","commonSituations":"Long-lived MCP/browser sessions where the tab was closed by the user, headless browser OOM crashes, flaky network leaving a partially loaded page, or corporate proxies blocking geogebra.org scripts.","solutions":["Check the wrapped err message (err?.message || err) for the root cause: 'Target closed' means re-open/reconnect the browser/page.","Re-navigate: close the stale page, open a fresh page at the GeoGebra Geometry URL, then retry the tool.","Verify network access to geogebra.org and that no extension/CSP blocks inline script evaluation.","Add a guard that catches page-destroyed states and recreates the page before calling tools."],"exampleFix":"// before\nawait drawPolygon(page, '1,2;3,4;5,0'); // throws if page was closed\n// after\nif (page.isClosed()) { page = await browser.newPage(); }\nawait ensureApplet(page);\nawait drawPolygon(page, '1,2;3,4;5,0');","handlingStrategy":"try-catch","validationCode":"if (!page || page.isClosed()) { page = await browser.newPage(); }","typeGuard":"null","tryCatchPattern":"try { await ensureApplet(page); } catch (e) { if (String(e.message).includes('Failed to detect')) { page = await browser.newPage(); await ensureApplet(page); } else throw e; }","preventionTips":["Check page.isClosed() before each tool call in long sessions.","Recreate the page after any browser crash or target-closed event.","Avoid navigating the shared tab while geometry tools run.","Test with extensions disabled if detection fails intermittently."],"tags":["browser","automation","page-evaluation","geogebra"],"backgroundTag":"browser-page-evaluation-failed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}