{"record":{"id":"0f6d16dafc8daf9c","repo":"agentscope-ai/agentscope","slug":"session-session-id-r-not-found-for-agent-agent","errorCode":null,"errorMessage":"Session {session_id!r} not found for agent {agent_id!r}.","messagePattern":"Session (.+?) not found for agent (.+?)\\.","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"src/agentscope/app/_service/_chat.py","lineNumber":696,"sourceCode":"                # the agent is not visible to the caller.\n                # -------------------------------------------------------------\n                try:\n                    agent_record = await self._access.resolve_agent(\n                        user_id,\n                        agent_id,\n                    )\n                except HTTPException as exc:\n                    raise HTTPException(\n                        status_code=404,\n                        detail=f\"Agent {agent_id!r} not found.\",\n                    ) from exc\n                session_record = await self._storage.get_session(\n                    user_id,\n                    agent_id,\n                    session_id,\n                )\n                if session_record is None:\n                    raise HTTPException(\n                        status_code=404,\n                        detail=(\n                            f\"Session {session_id!r} not found for \"\n                            f\"agent {agent_id!r}.\"\n                        ),\n                    )\n                worker_name = agent_record.data.name\n\n                # -------------------------------------------------------------\n                # 1b. Resolve the team identity ONCE, before anything that\n                # can fail: a worker whose assembly dies still has to reach\n                # its leader. Downstream consumers reuse this.\n                # -------------------------------------------------------------\n                if session_record.team_id is not None:\n                    team = await self._storage.get_team(\n                        user_id,\n                        session_record.team_id,\n                    )","sourceCodeStart":678,"sourceCodeEnd":714,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/app/_service/_chat.py#L678-L714","documentation":"Raised by ChatService._run_impl when storage.get_session returns None for the (user_id, agent_id, session_id) triple during a run. Unlike the agent check, this 404 names the session explicitly, telling the caller the session no longer exists for that agent. Runs must target an existing session; continuation of a vanished session is rejected.","triggerScenarios":"Calling run with a session_id that was deleted, expired, or belongs to a different agent or user. Typical after session cleanup, switching agents mid-conversation, or resuming from stale persisted state.","commonSituations":"Frontend keeps a session_id in localStorage after server-side session GC; user switches agent in the UI but keeps the old session id; test fixtures reuse session ids across runs; multi-instance deployments where session data wasn't shared.","solutions":["Create a new session (fresh run without session_id) when this 404 is returned, and persist the new id","Verify the session belongs to the same agent_id being run","Shorten the gap between session creation and use, or extend session TTL if expiry is the cause","Clear cached session ids on agent switch in the client"],"exampleFix":"// before\nawait client.run(agent_id, session_id=\"sess-old\", ...);  // 404\n// after\ntry:\n    run = await client.run(agent_id, session_id=\"sess-old\", ...);\nexcept NotFoundError:\n    run = await client.run(agent_id, ...);  // new session\n    save_session(run.session_id);","handlingStrategy":"fallback","validationCode":"session = await storage.get_session(user_id, agent_id, session_id)\nif session is None:\n    session_id = None  # force new session\nawait client.run(agent_id=agent_id, session_id=session_id, ...)","typeGuard":null,"tryCatchPattern":"try:\n    run = await client.run(agent_id, session_id=session_id, ...)\nexcept NotFoundError:\n    run = await client.run(agent_id, ...)  # start fresh session\n    persist(run.session_id)","preventionTips":["Clear cached session ids when switching agents","On any session-404, transparently create a new session rather than surfacing the error"],"tags":["session","not-found-404","chat-run"],"backgroundTag":"session-not-found","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}