{"record":{"id":"f7f1bd1483677208","repo":"Significant-Gravitas/AutoGPT","slug":"session-session-id-not-found","errorCode":null,"errorMessage":"Session {session_id} not found.","messagePattern":"Session (.+?) not found\\.","errorType":"exception","errorClass":"NotFoundError","httpStatus":404,"severity":"error","filePath":"autogpt_platform/backend/backend/api/features/chat/routes.py","lineNumber":154,"sourceCode":"logger = logging.getLogger(__name__)\n\nconfig = ChatConfig()\ncredentials_manager = IntegrationCredentialsManager()\n\n\nasync def _validate_and_get_session(\n    session_id: str,\n    user_id: str | None,\n) -> ChatSessionInfo:\n    \"\"\"Validate session exists and belongs to user.\n\n    Returns metadata-only — callers needing the message history must use\n    ``get_chat_session`` directly. Bypassing the message-loading path\n    avoids a multi-KB cache deserialisation per ownership check.\n    \"\"\"\n    session = await get_chat_session_metadata(session_id, user_id)\n    if not session:\n        raise NotFoundError(f\"Session {session_id} not found.\")\n    return session\n\n\n# Minimum age before the orphan-reset paths (``get_session`` and\n# ``cancel_session_task``) will touch a ``chatStatus='running'`` session\n# that has no live Redis stream.  Lower bound has to clear the\n# ``acquire_turn_slot``→``dispatch_turn.create_session`` window (a few\n# ms in practice).  30s is a generous safety margin — anything still\n# at ``running`` after that without a Redis stream is genuinely an\n# orphan, not an in-flight admit racing this read.\n_ORPHAN_RUNNING_RESET_THRESHOLD_SECONDS = 30\n\n\nasync def _try_release_orphan_running(session_id: str, user_id: str) -> bool:\n    \"\"\"Force-release a session if it's stuck in ``chatStatus='running'``\n    older than ``_ORPHAN_RUNNING_RESET_THRESHOLD_SECONDS`` (= the\n    ``acquire_turn_slot``→``create_session`` race window).  Returns\n    True iff a release happened — callers map that into their response","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/Significant-Gravitas/AutoGPT/blob/9c8bb5550f446ba5d3046b78896578742495b3cf/autogpt_platform/backend/backend/api/features/chat/routes.py#L136-L172","documentation":"A NotFoundError (surfaced as 404) from the chat session ownership validator: get_chat_session_metadata(session_id, user_id) returned None, meaning no session with that id exists OR it exists but belongs to a different user/organization. The helper deliberately fetches metadata only (no message history) for cheap ownership checks; None covers both 'missing' and 'not yours' without leaking which.","triggerScenarios":"Any chat route that calls _validate_and_get_session — e.g. GET/DELETE/PATCH on /chat/sessions/{session_id} — with a session id that was deleted, never existed, belongs to another user, or was created under a different organization context.","commonSituations":"Stale session id kept in frontend state after the user deleted the session in another tab; copy-pasting or reusing ids across environments (dev id against prod API); logged-out user with a cached id; session pruned by retention cleanup; wrong user context after an account switch.","solutions":["Refresh the client's session list (the id is likely deleted or belongs to another account) and retry with a current id.","Verify environment consistency: the session id must come from the same backend/environment the request targets.","If it should exist, check in the DB (ChatSession table) whether the row exists and compare its user_id/organization with the authenticated user; a mismatch means an auth-context bug, not a missing session."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"const sessions = await listSessions();\nif (!sessions.some(s => s.session_id === sessionId)) redirect('/chat');","typeGuard":null,"tryCatchPattern":"try {\n  await api.getSession(sessionId);\n} catch (e) {\n  if (e.status === 404) { dropLocalSession(sessionId); navigate('/chat'); return; }\n  throw e;\n}","preventionTips":["Treat 404 as 'gone or not yours'; remove the id from client state.","Never persist session ids across logins or environment switches.","Reconcile cached ids against the session list on app load."],"tags":["backend","chat","session","http-404","authorization"],"backgroundTag":null,"analyzedSha":"9c8bb5550f446ba5d3046b78896578742495b3cf","analyzedAt":"2026-08-14T17:17:21.957Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}