{"record":{"id":"5c8bdccb16640c31","repo":"datawhalechina/hello-agents","slug":"agent-not-initialized","errorCode":null,"errorMessage":"Agent not initialized","messagePattern":"Agent not initialized","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"Co-creation-projects/tino-chen-HelloClaw/src/api/session.py","lineNumber":117,"sourceCode":"            updated_at=s[\"updated_at\"]\n        )\n        for s in sessions\n    ])\n\n\n@router.post(\"/create\", response_model=SessionCreateResponse)\nasync def create_session(request: SessionCreateRequest = None):\n    \"\"\"创建新会话\n\n    可选参数：\n    - summarize_old: 是否在创建新会话前总结旧会话\n    - old_session_id: 要总结的旧会话 ID（如果不指定，则总结最近一个会话）\n\n    返回新会话的 ID\n    \"\"\"\n    agent = get_agent()\n    if not agent:\n        raise HTTPException(status_code=500, detail=\"Agent not initialized\")\n\n    request = request or SessionCreateRequest()\n    summary_file = None\n\n    # 如果需要总结旧会话\n    if request.summarize_old:\n        old_session_id = request.old_session_id\n\n        # 如果没有指定旧会话，找最近的一个\n        if not old_session_id:\n            sessions = agent.list_sessions()\n            if sessions:\n                old_session_id = sessions[0][\"id\"]\n\n        # 总结旧会话\n        if old_session_id:\n            summary_file = await _summarize_session(agent, old_session_id)\n","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/tino-chen-HelloClaw/src/api/session.py#L99-L135","documentation":"Raised by POST /api/session/create when the module-level get_agent() returns None — the global HelloClawAgent singleton has not been initialized in this server process. Session creation requires a live agent (it may summarize the previous session via the LLM), so there is no degraded path; the route answers HTTP 500 immediately.","triggerScenarios":"POST /api/session/create hit before app startup finished initializing the agent; agent init failed at boot (bad config.json llm credentials) and the server kept running; tests constructing the router without the app lifespan; calling the router function directly in a script.","commonSituations":"Race at startup: frontend fires the create-session call as soon as the port opens; config.json missing/invalid so initialization silently left the global None; running under a reloader that swapped the process holding the global.","solutions":["Check server startup logs for agent initialization errors and fix them (usually config.json llm fields)","Wait for a readiness/health signal before the first session call, or retry once after a short delay","Ensure you go through the app's startup (lifespan) path instead of importing the router standalone"],"exampleFix":"// before\nawait fetch('/api/session/create', {method:'POST', body:'{}'})\n// after\nawait waitForHealth(); // GET /health returns ok only after agent init\nawait fetch('/api/session/create', {method:'POST', body:'{}'})","handlingStrategy":"retry","validationCode":"const health = await (await fetch('/health')).json();\nif (!health.ready) await waitUntilReady();","typeGuard":null,"tryCatchPattern":"for (let i = 0; i < 3; i++) {\n  try { return await createSession(); }\n  catch (e) { if (e.status !== 500 || i === 2) throw e; await sleep(500 * (i + 1)); }\n}","preventionTips":["Gate first API call on a readiness signal, not port-open","Fix boot-time init failures (config.json) before blaming the endpoint"],"tags":["fastapi","session","initialization","http-500"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}