{"record":{"id":"8ce0d6d7416b50a9","repo":"agentscope-ai/agentscope","slug":"session-session-id-r-not-found-8ce0d6","errorCode":null,"errorMessage":"Session {session_id!r} not found.","messagePattern":"Session (.+?) not found\\.","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"src/agentscope/app/storage/_sql/_storage.py","lineNumber":1132,"sourceCode":"                    SessionRow.user_id == user_id,\n                )\n                .values(team_id=team_id, updated_at=now),\n            )\n            await sess.commit()\n\n    async def update_session_state(\n        self,\n        user_id: str,\n        agent_id: str,\n        session_id: str,\n        state: AgentState,\n    ) -> None:\n        \"\"\"Read-modify-write on the payload; raises if absent.\"\"\"\n        _ = user_id, agent_id  # scoping enforced by caller\n        async with self._session() as sess:\n            row = await sess.get(SessionRow, session_id)\n            if row is None:\n                raise KeyError(f\"Session {session_id!r} not found.\")\n            record = _to_record(row, SessionRecord)\n            record.state = state\n            record.updated_at = _utcnow()\n            new_row = _from_record(SessionRow, record)\n            row.payload = new_row.payload\n            row.updated_at = new_row.updated_at\n            await sess.commit()\n\n    async def list_sessions(\n        self,\n        user_id: str,\n        agent_id: str,\n    ) -> list[SessionRecord]:\n        \"\"\"Sessions for a (user, agent) pair — newest first.\"\"\"\n        from sqlalchemy import select\n\n        async with self._session() as sess:\n            rows = (","sourceCodeStart":1114,"sourceCodeEnd":1150,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/app/storage/_sql/_storage.py#L1114-L1150","documentation":"Raised by SQLStorage.update_session_state when the session row identified by session_id does not exist in the database. The method performs a read-modify-write on the session payload and cannot proceed on a missing row.","triggerScenarios":"Calling update_session_state(session_id, ...) for a session that was never created, was deleted, or whose id is misspelled; also after expiry-based cleanup removed the row.","commonSituations":"Using a stale session id after session expiry or deletion; resuming a conversation whose id came from old config; racing with a concurrent delete.","solutions":["Verify the session exists first with get_session(user_id, session_id) and create it if missing","Check for typos or truncated ids in the session_id passed in","If sessions expire, recreate the session and re-apply state instead of updating a dead one"],"exampleFix":"// before\nawait storage.update_session_state(sid, state)\n// after\nif await storage.get_session(uid, sid) is None:\n    sid = await storage.create_session(uid, aid, SessionRecord(...))\nawait storage.update_session_state(sid, state)","handlingStrategy":"try-catch","validationCode":"if await storage.get_session(user_id, session_id) is None:\\n    session_id = await storage.create_session(...)","typeGuard":null,"tryCatchPattern":"try:\\n    await storage.update_session_state(sid, state)\\nexcept KeyError:\\n    sid = await storage.create_session(...); await storage.update_session_state(sid, state)","preventionTips":["Treat session ids as ephemeral; persist them alongside checkpoints","Handle KeyError by recreating the session"],"tags":["session","not-found","keyerror","sql-storage"],"backgroundTag":"record-not-found","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}