{"record":{"id":"e609d0c4ebcb7803","repo":"HKUDS/Vibe-Trading","slug":"goal-created-but-could-not-be-reloaded","errorCode":null,"errorMessage":"Goal created but could not be reloaded","messagePattern":"Goal created but could not be reloaded","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"agent/src/api/sessions_routes.py","lineNumber":477,"sourceCode":"        goal_store = _get_goal_store()\n        try:\n            goal = goal_store.replace_goal(\n                session_id=session_id,\n                objective=req.objective,\n                criteria=criteria,\n                ui_summary=req.ui_summary,\n                source=\"api\",\n                protocol=req.protocol,\n                risk_tier=risk_tier,\n                token_budget=req.token_budget,\n                turn_budget=req.turn_budget,\n                time_budget_seconds=req.time_budget_seconds,\n            )\n        except ValueError as exc:\n            raise HTTPException(status_code=400, detail=str(exc)) from exc\n        snapshot = goal_store.get_goal_snapshot(goal.goal_id)\n        if snapshot is None:\n            raise HTTPException(status_code=500, detail=\"Goal created but could not be reloaded\")\n        svc.event_bus.emit(session_id, \"goal.created\", {\"goal\": snapshot[\"goal\"]})\n        return snapshot\n\n    @app.get(\n        \"/sessions/{session_id}/goal\",\n        response_model=GoalSnapshotResponse,\n        dependencies=[Depends(require_auth)],\n    )\n    async def get_session_goal(session_id: str):\n        \"\"\"Return the current finance research goal snapshot for a session.\"\"\"\n        _host_validate_path_param(session_id, \"session_id\")\n        _get_existing_session_or_404(session_id)\n        snapshot = _get_goal_store().get_current_snapshot(session_id)\n        if snapshot is None:\n            raise HTTPException(status_code=404, detail=\"No current goal\")\n        return snapshot\n\n    @app.patch(","sourceCodeStart":459,"sourceCodeEnd":495,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/api/sessions_routes.py#L459-L495","documentation":"After replace_goal returns a goal, the route re-reads it with goal_store.get_goal_snapshot(goal.goal_id); if that returns None (store inconsistency, eviction, or a persistence failure between write and read) the route fails with 500 'Goal created but could not be reloaded'. The write happened, but the read-back did not — so state may be inconsistent.","triggerScenarios":"A race where the just-created goal is evicted/deleted/never persisted before the immediate re-read; snapshot construction failing; storage backend (DB/file) losing the row; concurrent tests resetting the store mid-request.","commonSituations":"Flaky test suites sharing a goal store that gets cleared between operations; in-memory store swapped concurrently; DB transaction visibility issues (uncommitted write); a bug in a custom goal-store backend.","solutions":["Retry the operation: GET /sessions/{id}/goal to see if the goal actually exists despite the 500","If it persists, inspect the goal store implementation/persistence config (get_goal_snapshot vs replace_goal asymmetry)","Isolate concurrent store resets in tests (one store per test) to rule out interference","Report upstream with the goal_id if the store is the library's own — the write/read mismatch indicates a bug"],"exampleFix":"# before\nsnapshot = api.create_session_goal(sid, ...)  # 500, goal may exist\n\n# after\ntry:\n    snapshot = api.create_session_goal(sid, ...)\nexcept HTTPError as e:\n    if e.response.status_code == 500:\n        snapshot = api.get_session_goal(sid)  # confirm whether it was actually created\n    else:\n        raise","handlingStrategy":"fallback","validationCode":"# nothing client-side can prevent an internal read-back failure; pre-flight store health if available\nr = requests.get(f\"{BASE}/sessions\", params={\"limit\": 1})\nif r.status_code >= 500:\n    raise RuntimeError(\"Server unhealthy; goal create may fail with 500\")","typeGuard":null,"tryCatchPattern":"try:\n    snap = api.create_session_goal(sid, payload)\nexcept HTTPError as e:\n    if e.response.status_code == 500:\n        snap = None\n        try:\n            snap = api.get_session_goal(sid)  # goal may exist despite the 500\n        except NotFoundError:\n            pass\n        if snap is None:\n            raise\n    else:\n        raise","preventionTips":["After a 500 on create, verify with GET before assuming failure","Use isolated goal stores per test to avoid concurrent resets","Monitor 500 rates on goal routes to catch store regressions early"],"tags":["goals","http-500","internal","store-consistency"],"backgroundTag":"write-then-read-inconsistency","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}