{"record":{"id":"06ad12d931feddd5","repo":"garrytan/gstack","slug":"cannot-use-back-inside-a-frame-run-frame-main-f","errorCode":null,"errorMessage":"Cannot use back inside a frame. Run 'frame main' first.","messagePattern":"Cannot use back inside a frame\\. Run 'frame main' first\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"browse/src/write-commands.ts","lineNumber":158,"sourceCode":"  const target = session.getActiveFrameOrPage();\n  const inFrame = session.getFrame() !== null;\n\n  switch (command) {\n    case 'goto': {\n      if (inFrame) throw new Error('Cannot use goto inside a frame. Run \\'frame main\\' first.');\n      const url = args[0];\n      if (!url) throw new Error('Usage: browse goto <url>');\n      // Clear loadedHtml BEFORE navigation — a timeout after the main-frame commit\n      // must not leave stale content that could resurrect on a later context recreation.\n      session.clearLoadedHtml();\n      const normalizedUrl = await validateNavigationUrl(url);\n      const response = await page.goto(normalizedUrl, { waitUntil: 'domcontentloaded', timeout: 15000 });\n      const status = response?.status() || 'unknown';\n      return `Navigated to ${normalizedUrl} (${status})`;\n    }\n\n    case 'back': {\n      if (inFrame) throw new Error('Cannot use back inside a frame. Run \\'frame main\\' first.');\n      session.clearLoadedHtml();\n      await page.goBack({ waitUntil: 'domcontentloaded', timeout: 15000 });\n      return `Back → ${page.url()}`;\n    }\n\n    case 'forward': {\n      if (inFrame) throw new Error('Cannot use forward inside a frame. Run \\'frame main\\' first.');\n      session.clearLoadedHtml();\n      await page.goForward({ waitUntil: 'domcontentloaded', timeout: 15000 });\n      return `Forward → ${page.url()}`;\n    }\n\n    case 'reload': {\n      if (inFrame) throw new Error('Cannot use reload inside a frame. Run \\'frame main\\' first.');\n      session.clearLoadedHtml();\n      await page.reload({ waitUntil: 'domcontentloaded', timeout: 15000 });\n      return `Reloaded ${page.url()}`;\n    }","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/garrytan/gstack/blob/94993f74012782fd94416dd44b8314f6363a13a4/browse/src/write-commands.ts#L140-L176","documentation":"Frame-context guard in the back case at write-commands.ts:159. page.goBack() operates on the top-level page; with a subframe selected (session.getFrame() !== null) it is refused. Same invariant as goto — navigation is a page-level operation.","triggerScenarios":"Running 'back' while a frame context is active (after 'frame <selector>' and before 'frame main').","commonSituations":"Automated flow enters an iframe, interacts, then tries to go back without resetting the frame.","solutions":["Run 'frame main' before 'back'","Order navigation commands (back/forward/goto/reload) before any frame entry","Guard automation on session.getFrame() === null"],"exampleFix":"// before\nawait handleWriteCommand('back', [], session, bm)  // in frame → throws\n// after\nawait handleWriteCommand('frame', ['main'], session, bm)\nawait handleWriteCommand('back', [], session, bm)","handlingStrategy":"validation","validationCode":"function canNavigate(session: { getFrame(): unknown }): boolean {\n  return session.getFrame() === null\n}","typeGuard":"function isMainFrameSession(s: { getFrame(): unknown }): boolean {\n  return s.getFrame() === null\n}","tryCatchPattern":null,"preventionTips":["Run 'frame main' before 'back'","Keep navigation commands outside frame-scoped blocks","Assert getFrame() === null in automation"],"tags":["frames","navigation","write-commands","session-state"],"backgroundTag":null,"analyzedSha":"94993f74012782fd94416dd44b8314f6363a13a4","analyzedAt":"2026-08-12T04:06:23.140Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}