{"record":{"id":"44cebfbe6da1ab24","repo":"garrytan/gstack","slug":"state-not-found-statepath","errorCode":null,"errorMessage":"State not found: ${statePath}","messagePattern":"State not found: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"browse/src/meta-commands.ts","lineNumber":954,"sourceCode":"      const stateDir = path.join(config.stateDir, 'browse-states');\n      mkdirSecure(stateDir);\n      const statePath = path.join(stateDir, `${name}.json`);\n\n      if (action === 'save') {\n        const state = await bm.saveState();\n        // V1: cookies + URLs only (not localStorage — breaks on load-before-navigate)\n        const saveData = {\n          version: 1,\n          savedAt: new Date().toISOString(),\n          cookies: state.cookies,\n          pages: state.pages.map(p => ({ url: p.url, isActive: p.isActive })),\n        };\n        writeSecureFile(statePath, JSON.stringify(saveData, null, 2));\n        return `State saved: ${statePath} (${state.cookies.length} cookies, ${state.pages.length} pages)\\n⚠️  Cookies stored in plaintext. Delete when no longer needed.`;\n      }\n\n      if (action === 'load') {\n        if (!fs.existsSync(statePath)) throw new Error(`State not found: ${statePath}`);\n        const data = JSON.parse(fs.readFileSync(statePath, 'utf-8'));\n        if (!Array.isArray(data.cookies) || !Array.isArray(data.pages)) {\n          throw new Error('Invalid state file: expected cookies and pages arrays');\n        }\n        // Validate and filter cookies — reject malformed or internal-network cookies\n        const validatedCookies = data.cookies.filter((c: any) => {\n          if (typeof c !== 'object' || !c) return false;\n          if (typeof c.name !== 'string' || typeof c.value !== 'string') return false;\n          if (typeof c.domain !== 'string' || !c.domain) return false;\n          const d = c.domain.startsWith('.') ? c.domain.slice(1) : c.domain;\n          if (d === 'localhost' || d.endsWith('.internal') || d === '169.254.169.254') return false;\n          return true;\n        });\n        if (validatedCookies.length < data.cookies.length) {\n          console.warn(`[browse] Filtered ${data.cookies.length - validatedCookies.length} invalid cookies from state file`);\n        }\n        // Warn on state files older than 7 days\n        if (data.savedAt) {","sourceCodeStart":936,"sourceCodeEnd":972,"githubUrl":"https://github.com/garrytan/gstack/blob/94993f74012782fd94416dd44b8314f6363a13a4/browse/src/meta-commands.ts#L936-L972","documentation":"On `state load`, the command checks `fs.existsSync(statePath)` (line 954) and throws if the file is absent. The path is built as `<stateDir>/browse-states/<name>.json`, so the error names the resolved absolute path to make it obvious what was looked for.","triggerScenarios":"`browse state load <name>` when no prior `state save <name>` wrote the file, or the state directory was cleared/moved.","commonSituations":"Loading before saving in a fresh environment, a typo in the name, or `stateDir` (from `resolveConfig()`) pointing somewhere unexpected after a config/env change.","solutions":["Run `browse state save <name>` first to create the file.","Verify the name spelling matches the save call (case-sensitive).","Check `resolveConfig().stateDir` to confirm the browse-states directory is where you expect.","List `<stateDir>/browse-states/*.json` to see which saved states actually exist."],"exampleFix":"// before\nbrowse state load nope\n// after\nbrowse state save nope && browse state load nope","handlingStrategy":"try-catch","validationCode":"const statePath = path.join(config.stateDir, 'browse-states', `${name}.json`);\nif (!fs.existsSync(statePath)) {\n  throw new Error(`No saved state '${name}'. Run: state save ${name}`);\n}","typeGuard":null,"tryCatchPattern":"try { await browse.state('load', name); }\ncatch (err) {\n  if (/State not found/.test(err.message)) {\n    await browse.state('save', name); // create then retry\n    return browse.state('load', name);\n  }\n  throw err;\n}","preventionTips":["Check existence before loading, or save-then-load in a single flow.","Confirm `stateDir` is stable across sessions (don't rely on a temp dir)."],"tags":["state","filesystem","not-found"],"backgroundTag":null,"analyzedSha":"94993f74012782fd94416dd44b8314f6363a13a4","analyzedAt":"2026-08-12T04:06:23.140Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}