{"record":{"id":"57aefd0f1d22467a","repo":"datawhalechina/hello-agents","slug":"diet-run-id","errorCode":null,"errorMessage":"diet_run_id 不存在","messagePattern":"diet_run_id 不存在","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"Co-creation-projects/Shawnxyxy-HealthRecordAgent/backend/api/routes/diet.py","lineNumber":91,"sourceCode":"    \"\"\"\n    饮食推荐：阶段 2 为 **Nutritionist → Coach → Habit** 三 Agent，固定 JSON schema + Pydantic 校验；\n    每阶段最多 2 次尝试，失败则降级并写入 `errors` / `degraded`。\n    仍落库 `diet_runs`，并读取 Reflect 记忆。\n    \"\"\"\n    svc = DietRecommendService()\n    ctx = body.context.model_dump()\n    result = await svc.run(body.user_id, ctx)\n    return result\n\n\n@router.post(\"/diet/reflect\")\nasync def diet_reflect(body: DietReflectRequest):\n    \"\"\"\n    Reflect：用户反馈是否执行及原因，写入 diet_reflect；下次 recommend 自动读取。\n    \"\"\"\n    row = get_diet_run(body.diet_run_id)\n    if not row:\n        raise HTTPException(status_code=404, detail=\"diet_run_id 不存在\")\n    if row.get(\"user_id\") != body.user_id:\n        raise HTTPException(status_code=403, detail=\"该 run 不属于此 user_id\")\n\n    rc = body.reason_code\n    if body.followed and rc is None:\n        rc = \"executed_ok\"\n\n    rid = insert_diet_reflect(\n        user_id=body.user_id,\n        diet_run_id=body.diet_run_id,\n        followed=body.followed,\n        reason_code=rc,\n        reason_detail=body.reason_detail,\n    )\n    asyncio.create_task(asyncio.to_thread(index_reflect_event, rid))\n    return {\n        \"ok\": True,\n        \"reflect_id\": rid,","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/Shawnxyxy-HealthRecordAgent/backend/api/routes/diet.py#L73-L109","documentation":"HTTP 404 raised by POST /diet/reflect when get_diet_run(body.diet_run_id) returns no row — the given diet_run_id does not exist in the diet_runs store. Reflect is meant to record user feedback against an existing recommendation run, so the run must have been created earlier via /diet/recommend.","triggerScenarios":"Calling POST /diet/reflect with a diet_run_id that was never created, was typo'd/truncated (note the model requires 8-64 chars), or belongs to a different (e.g. reset) database.","commonSituations":"Using an expired/in-memory DB that lost runs after a backend restart; passing the reflect endpoint the run's output id field with the wrong name; stale frontend state referencing an old run.","solutions":["Create a run first via POST /diet/recommend and use the returned diet_run_id verbatim.","Verify existence with GET /diet/runs/{run_id} before reflecting.","If runs vanish after restart, switch from in-memory storage to the persistent DB so run ids survive."],"exampleFix":"# before\nrequests.post(f\"{base}/diet/reflect\", json={\"user_id\": \"u1\", \"diet_run_id\": \"does-not-exist\", \"followed\": True})  # 404\n\n# after\nrun = requests.post(f\"{base}/diet/recommend\", json={...}).json()\nrid = run[\"diet_run_id\"]  # use the exact key returned by /diet/recommend\nrequests.post(f\"{base}/diet/reflect\", json={\"user_id\": \"u1\", \"diet_run_id\": rid, \"followed\": True})","handlingStrategy":"validation","validationCode":"resp = requests.get(f\"{base}/diet/runs/{diet_run_id}\")\nif resp.status_code == 404:\n    raise LookupError(f\"diet_run_id {diet_run_id} not found; call /diet/recommend first\")","typeGuard":null,"tryCatchPattern":"try:\n    r = requests.post(f\"{base}/diet/reflect\", json=payload)\n    r.raise_for_status()\nexcept requests.HTTPError as e:\n    if r.status_code == 404:\n        # run id stale/unknown -> recreate the run, do not retry\n        ...\n    else:\n        raise","preventionTips":["Persist the diet_run_id returned by /diet/recommend immediately and reuse it verbatim.","Ensure backend storage is durable if run ids must survive restarts.","Check run existence with GET /diet/runs/{id} before reflecting in interactive flows."],"tags":["fastapi","http-404","diet-api","validation"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}