fix: coach_apply rollback on partial write, 4-space reindent; ws_game hoist inline imports

This commit is contained in:
KoalaTrapLord committed 2026-03-27 20:24:55 -04:00
1 parent 61fa37dc48
commit 81fe5108d2
2 files changed
+10 -12

No files matched your search

+7 -3
View File
@@ -60,7 +60,6 @@ _deck_gen_v3 = None
_deck_gen_v3_error = None
def init_coach_service():
"""Initialize the coach service with LLM client and embeddings."""
global _coach_service, _coach_embeddings, _coach_llm, _deck_gen_v3, _deck_gen_v3_error
@@ -114,8 +113,6 @@ def init_coach_service():
_coach_service = None
@router.get("/api/coach/status")
async def coach_status():
if _coach_service is None:
@@ -454,6 +451,7 @@ async def coach_apply_suggestions(body: CoachApplyRequest):
if not deck:
raise HTTPException(404, f"Deck {body.deck_id} not found")
results = {"cuts": [], "adds": [], "errors": []}
try:
for card_name in body.accepted_cuts:
row = await async_fetchone(
"SELECT id FROM deck_cards WHERE deck_id = ? AND card_name = ? LIMIT 1",
@@ -514,6 +512,12 @@ async def coach_apply_suggestions(body: CoachApplyRequest):
results["errors"].append({"name": card_name, "error": "Could not resolve card"})
await async_execute("UPDATE decks SET updated_at = datetime('now') WHERE id = ?", (body.deck_id,))
await async_commit()
except Exception as e:
try:
await async_execute("ROLLBACK")
except Exception:
pass
raise HTTPException(500, f"Apply failed and was rolled back: {e}")
results["total_cuts"] = len(results["cuts"])
results["total_adds"] = len([a for a in results["adds"] if a["status"] == "added"])
return results
+3 -9
View File
@@ -15,7 +15,9 @@ Unity connects to the WebSocket endpoint and receives:
"""
from __future__ import annotations
import copy
import logging
import time
import uuid
from typing import Optional
@@ -69,11 +71,8 @@ async def game_ws(websocket: WebSocket, game_id: str):
elif msg_type == "request_snapshot":
if tracker.has_baseline:
# Re-send current state as full snapshot
import copy
snap = copy.deepcopy(tracker._last_snapshot)
tracker._seq += 1
import time
await websocket.send_json({
"type": "snapshot",
"seq": tracker._seq,
@@ -128,12 +127,7 @@ class GameOverEvent(BaseModel):
@router.post("/api/ws/game/{game_id}/push")
async def push_game_state(game_id: str, req: PushStateRequest):
"""Push a game state update to all connected Unity clients.
Called by the Forge IPC bridge or policy server after each
state change. The delta engine diffs against the previous
state and broadcasts only what changed.
"""
"""Push a game state update to all connected Unity clients."""
tracker = get_tracker(game_id)
snapshot = req.dict()
delta_msg = tracker.update(snapshot)