ml-intern / patch_catch_up_fix_backend.py
bep40's picture
Upload patch_catch_up_fix_backend.py
2bf6eb9 verified
Raw History Blame Contribute Delete
8.43 kB
#!/usr/bin/env python3
"""
Patch: Fix catch-up backend robustness.
Issues fixed:
1. /api/session-history/{session_id} returns ALL rows including 'event' type
rows (role='event'). These cause Message.model_validate to silently drop them.
Fix: Filter out non-message roles (event, system) from history endpoint.
2. seed_from_summary raises if summarization fails (API error, rate limit, credits),
causing restore-summary to return 500 and the frontend to show "Couldn't catch up."
Fix: Fall back to raw messages if summarization fails.
3. Event rows with role='event' fail Message.model_validate.
Fix: Skip event rows explicitly in seed_from_summary.
"""
import ast
import os
import sys
PATCHED_MARKER = "# === PATCH: catch-up fix (backend) ==="
def _safe_replace(content, old, new, count=1):
if old in content:
content = content.replace(old, new, count)
return content, True
return content, False
def patch_file(path, patch_fn):
if not os.path.exists(path):
print(f"SKIP: {path} not found")
return False
try:
with open(path, "r") as f:
content = f.read()
content, changed = patch_fn(content)
if not changed:
print(f"SKIP: {path} β€” no changes needed")
return False
try:
ast.parse(content)
except SyntaxError as e:
print(f"FAIL: {path} syntax: {e}")
return False
with open(path, "w") as f:
f.write(content)
print(f"OK: {path} patched (catch-up fix)")
return True
except Exception as e:
print(f"WARN: {path} patch error: {e}")
return False
# ─── Patch routes/agent.py: filter event rows from session-history ─────────
print("=== Patching routes/agent.py (session-history filter) ===")
ROUTES_AGENT = "/app/backend/routes/agent.py"
def _patch_routes(content):
changed = False
if PATCHED_MARKER in content:
print(" SKIP: already patched")
return content, False
# Patch the get_session_history endpoint to filter out event rows
old_endpoint = (
'@router.get("/session-history/{session_id}")\n'
'async def get_session_history(\n'
' session_id: str, user: dict = Depends(get_current_user)\n'
') -> list:\n'
' """Get full message history for a session from the permanent dataset."""\n'
' from backend.session_logger import load_session_history\n'
' return load_session_history(session_id)'
)
new_endpoint = (
'@router.get("/session-history/{session_id}")\n'
'async def get_session_history(\n'
' session_id: str, user: dict = Depends(get_current_user)\n'
') -> list:\n'
' """Get full message history for a session from the permanent dataset.\n'
'\n'
' Returns only valid conversation messages (user/assistant/tool roles).\n'
" Event rows (role='event') are filtered out because they are internal\n"
' tracing data that cannot be parsed as Message objects and would be\n'
' silently dropped during catch-up, causing message loss.\n'
' """\n'
' from backend.session_logger import load_session_history\n'
' rows = load_session_history(session_id)\n'
' # Filter out event rows and any rows without valid roles\n'
' valid_roles = {"user", "assistant", "tool"}\n'
' return [r for r in rows if r.get("role") in valid_roles]'
)
content, ok = _safe_replace(content, old_endpoint, new_endpoint)
if ok:
print(" + Filtered event rows from session-history endpoint")
changed = True
else:
print(" SKIP: target block not found")
# Add marker
content = content.rstrip() + "\n\n" + PATCHED_MARKER + "\n"
changed = True
return content, changed
patch_file(ROUTES_AGENT, _patch_routes)
# ─── Patch session_manager.py: robust seed_from_summary ─────────────────────
print("\n=== Patching session_manager.py (seed_from_summary robustness) ===")
SESSION_MANAGER = "/app/backend/session_manager.py"
def _patch_seed_from_summary(content):
changed = False
# Check if already patched
if PATCHED_MARKER in content:
print(" SKIP: already patched")
return content, False
# Patch 1: Skip event rows explicitly in seed_from_summary
old_parse = (
' # Parse into Message objects, tolerating malformed entries.\n'
' parsed: list[Message] = []\n'
' for raw in messages:\n'
' if raw.get("role") == "system":\n'
' continue # the new session has its own system prompt\n'
' try:\n'
' parsed.append(Message.model_validate(raw))\n'
' except Exception as e:\n'
' logger.warning("Dropping malformed message during seed: %s", e)'
)
new_parse = (
' # Parse into Message objects, tolerating malformed entries.\n'
' # === PATCH: skip event rows (not just system) ===\n'
' parsed: list[Message] = []\n'
' for raw in messages:\n'
' role = raw.get("role", "")\n'
' if role in ("system", "event"):\n'
' continue # not conversation messages\n'
' try:\n'
' parsed.append(Message.model_validate(raw))\n'
' except Exception as e:\n'
' logger.warning("Dropping malformed message during seed: %s", e)'
)
content, ok = _safe_replace(content, old_parse, new_parse)
if ok:
print(" + Skip event rows explicitly in seed_from_summary")
changed = True
# Patch 2: Fall back to raw messages if summarization fails
old_try = (
' try:\n'
' summary, _ = await summarize_messages(\n'
' parsed,\n'
' model_name=session.config.model_name,\n'
' hf_token=session.hf_token,\n'
' max_tokens=4000,\n'
' prompt=_RESTORE_PROMPT,\n'
' tool_specs=tool_specs,\n'
' session=session,\n'
' kind="restore",\n'
' )\n'
' except Exception as e:\n'
' logger.error("Summary call failed during seed: %s", e)\n'
' raise\n'
'\n'
' seed = Message('
)
new_try = (
' # === PATCH: fall back to raw messages if summarization fails ===\n'
' summary = None\n'
' try:\n'
' summary, _ = await summarize_messages(\n'
' parsed,\n'
' model_name=session.config.model_name,\n'
' hf_token=session.hf_token,\n'
' max_tokens=4000,\n'
' prompt=_RESTORE_PROMPT,\n'
' tool_specs=tool_specs,\n'
' session=session,\n'
' kind="restore",\n'
' )\n'
' except Exception as e:\n'
' logger.error("Summary call failed during seed, falling back to raw messages: %s", e)\n'
' # Fall back: inject raw messages directly into the new session context\n'
' # This preserves the full conversation instead of losing it\n'
' for msg in parsed:\n'
' session.context_manager.items.append(msg)\n'
' self._touch(agent_session)\n'
' await self.persist_session_snapshot(agent_session, runtime_state="idle")\n'
' return len(parsed)\n'
'\n'
' seed = Message('
)
content, ok = _safe_replace(content, old_try, new_try)
if ok:
print(" + Fall back to raw messages if summarization fails")
changed = True
# Add marker
if changed:
content = content + "\n" + PATCHED_MARKER + "\n"
changed = True
return content, changed
patch_file(SESSION_MANAGER, _patch_seed_from_summary)
print("\n=== Backend catch-up fixes applied ===")