{"record":{"id":"cbec60e0e0d27ba2","repo":"Significant-Gravitas/AutoGPT","slug":"failed-to-fetch-session-status-response-status","errorCode":null,"errorMessage":"Failed to fetch session (status: ${response.status})","messagePattern":"Failed to fetch session \\(status: (.+?)\\)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"autogpt_platform/frontend/src/app/(platform)/copilot/helpers/exportChatAsMarkdown.ts","lineNumber":101,"sourceCode":"\nexport async function fetchAndExportChat(\n  id: string,\n  title: string | null | undefined,\n  fetchSession: typeof import(\"@/app/api/__generated__/endpoints/chat/chat\").getV2GetSession,\n): Promise<void> {\n  const allMessages: SessionChatMessage[] = [];\n  let beforeSequence: number | undefined = undefined;\n  let truncated = false;\n\n  for (let page = 0; page < EXPORT_MAX_PAGES; page++) {\n    const opts: { limit: number; before_sequence?: number } = {\n      limit: EXPORT_PAGE_SIZE,\n    };\n    if (beforeSequence !== undefined) opts.before_sequence = beforeSequence;\n\n    const response = await fetchSession(id, opts);\n    if (response.status !== 200) {\n      throw new Error(`Failed to fetch session (status: ${response.status})`);\n    }\n\n    const pageMessages = (response.data.messages ??\n      []) as unknown as SessionChatMessage[];\n    allMessages.unshift(...pageMessages);\n\n    const hasMore = !!response.data.has_more_messages;\n    const oldestSeq = response.data.oldest_sequence;\n    if (!hasMore || oldestSeq == null) break;\n    if (page === EXPORT_MAX_PAGES - 1) {\n      truncated = true;\n      break;\n    }\n    beforeSequence = oldestSeq;\n  }\n\n  if (truncated) {\n    throw new Error(","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/Significant-Gravitas/AutoGPT/blob/9c8bb5550f446ba5d3046b78896578742495b3cf/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/exportChatAsMarkdown.ts#L83-L119","documentation":"Thrown while paginating a copilot chat session's messages for markdown export (exportChatAsMarkdown.ts). The code loops up to EXPORT_MAX_PAGES calling fetchSession(id, {limit, before_sequence}) and throws if any page returns a status other than 200. Because the generated fetchSession client resolves (rather than throws) on non-2xx, this explicit status check is the only thing that surfaces backend failures during export.","triggerScenarios":"GET /copilot/sessions/{id} returning 404 (session deleted or owned by another user), 401/403 (expired token — note fetchSession here is called without getCopilotAuthHeaders context if auth lapsed), 422 (bad before_sequence after messages were pruned mid-export), or 5xx from the backend while the user clicks Export.","commonSituations":"Exporting a chat that was just deleted in another tab; session expired between opening the chat and clicking export; backend restart/upgrade mid-pagination so sequence numbers shift; pagination cursor (oldest_sequence) pointing at trimmed messages in very long chats.","solutions":["Re-authenticate (sign out/in) and retry the export if the status was 401/403.","Check the network response body for the failing GET session request — FastAPI's detail field says whether it's 404 gone or 422 bad cursor.","Retry after the backend is healthy if it was a transient 5xx; the export is read-only and idempotent.","If it recurs on one specific chat, that session's message history may be inconsistent — fetch it in the copilot UI to confirm, and report the session ID.","For code hardening, include response.status in the thrown message (already done) and surface response.data?.detail too."],"exampleFix":"// before\nif (response.status !== 200) {\n  throw new Error(`Failed to fetch session (status: ${response.status})`);\n}\n\n// after (retry once on 5xx, surface backend detail)\nif (response.status >= 500 && page === 0) { /* single retry */ }\nif (response.status !== 200) {\n  const detail = (response.data as any)?.detail;\n  throw new Error(\n    `Failed to fetch session (status: ${response.status}${detail ? `: ${detail}` : \"\"})`,\n  );\n}","handlingStrategy":"retry","validationCode":"function canExport(sessionId: string): boolean {\n  return typeof sessionId === \"string\" && sessionId.length > 0;\n}","typeGuard":"function isSessionFetchFailure(err: unknown): err is Error {\n  return err instanceof Error && err.message.startsWith(\"Failed to fetch session\");\n}","tryCatchPattern":"try {\n  await exportSession(id);\n} catch (error) {\n  if (isSessionFetchFailure(error)) {\n    // parse embedded status; 401 -> re-auth, 5xx -> retry once, else report\n    toast({ title: \"Export failed\", description: error.message, variant: \"destructive\" });\n  }\n}","preventionTips":["Check session still exists (refetch list) before offering Export on stale UI.","Treat generated fetchSession responses as resolve-not-throw: always check .status explicitly, as this code does.","Keep exports short-lived — don't hold a partially-paginated result across auth refreshes."],"tags":["copilot","export","pagination","http-status","frontend"],"backgroundTag":null,"analyzedSha":"9c8bb5550f446ba5d3046b78896578742495b3cf","analyzedAt":"2026-08-14T17:17:21.957Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}