ml-intern / patch_catch_up_context_restore.py
bep40's picture
Add patch: restore full session context from HF dataset (fix catch-me-up banner + context loss)
6e62f72 verified
Raw History Blame Contribute Delete
8.72 kB
#!/usr/bin/env python3
"""
Patch: Restore full session context from the permanent HF dataset.
Fixes two related bugs in bep40/ml-intern (ML Intern V2 Space):
1. "Catch me up" banner appears constantly after a Space restart.
The backend only loads sessions from get_session_store() (Mongo/Noop).
On HF Spaces there is no MONGODB_URI, so NoopSessionStore.load_session()
always returns None -> every previously-open session is treated as dead
-> the frontend shows "Where were we? / Catch me up" for ALL old
sessions, even though the full conversation is safely stored in the
bep40/ml-intern-sessions dataset.
2. After clicking "Catch me up", the model forgets prior context.
seed_from_summary() creates a brand-new session and seeds it with only a
lossy LLM summary (or raw messages that then get pruned). The model
never sees the full prior conversation, so it genuinely does not
remember.
Fix (both in backend/session_manager.py):
- ensure_session_loaded(): when the persistence store has no session,
fall back to the permanent HF dataset (backend.session_logger.
load_session_history) and re-materialize the session with the FULL
conversation, so old sessions come back alive with complete context.
- seed_from_summary(): if the dataset has the full history for the old
session, seed the new session with the full conversation instead of a
lossy summary (falling back to the summary path when unavailable).
Runs during Docker build; never fails the build.
"""
import ast
import os
PATCHED_MARKER = "# === PATCH: dataset-context-restore ==="
def _read(path: str) -> str:
with open(path, encoding="utf-8") as f:
return f.read()
def _write(path: str, content: str) -> None:
with open(path, "w", encoding="utf-8") as f:
f.write(content)
def _replace(content: str, old: str, new: str) -> tuple[str, bool]:
if old in content:
return content.replace(old, new, 1), True
return content, False
SESSION_MANAGER = "/app/backend/session_manager.py"
if os.path.exists(SESSION_MANAGER):
content = _read(SESSION_MANAGER)
changed = False
if PATCHED_MARKER in content:
print("SKIP: session_manager.py already patched")
else:
# ---- 1) Dataset fallback in ensure_session_loaded -----------------
old_load = (
" store = self._store()\n"
" loaded = await store.load_session(session_id)\n"
" if not loaded:\n"
" return None\n"
)
new_load = (
" store = self._store()\n"
" loaded = await store.load_session(session_id)\n"
" if not loaded:\n"
" # === PATCH: dataset-context-restore ===\n"
" # The persistence store has no record for this session\n"
" # (typical on HF Spaces: no MONGODB_URI -> NoopSessionStore).\n"
" # Before declaring the session dead, try the permanent HF\n"
" # dataset so old sessions survive a Space restart with\n"
" # their FULL context intact.\n"
" try:\n"
" from backend.session_logger import load_session_history\n"
" ds_rows = load_session_history(session_id)\n"
" ds_messages = [\n"
" r for r in ds_rows\n"
" if r.get(\"role\") in (\"user\", \"assistant\", \"tool\")\n"
" ]\n"
" if ds_messages:\n"
" loaded = {\n"
" \"metadata\": {\n"
" \"user_id\": user_id,\n"
" \"model\": next(\n"
" (r.get(\"model_name\") for r in ds_rows if r.get(\"model_name\")),\n"
" self.config.model_name,\n"
" ),\n"
" },\n"
" \"messages\": ds_messages,\n"
" }\n"
" logger.info(\n"
" \"Restored session %s from HF dataset (%d messages)\",\n"
" session_id, len(ds_messages),\n"
" )\n"
" except Exception as e:\n"
" logger.debug(\"Dataset restore fallback failed for %s: %s\", session_id, e)\n"
" if not loaded:\n"
" return None\n"
)
content, ok = _replace(content, old_load, new_load)
if ok:
print("+ ensure_session_loaded falls back to HF dataset")
changed = True
else:
print("INFO: store.load fallback anchor not found")
# ---- 2) seed_from_summary: prefer full dataset history ------------
old_seed = (
" seed = Message(\n"
" role=\"user\",\n"
" content=(\n"
" \"[SYSTEM: Your prior memory of this conversation \u2014 written \"\n"
" \"in your own voice right before restart. Continue from here.]\\n\\n\"\n"
" + (summary or \"(no summary returned)\")\n"
" ),\n"
" )\n"
)
new_seed = (
" # === PATCH: dataset-context-restore ===\n"
" # If the permanent dataset has the full history for this\n"
" # session, seed the new session with the ACTUAL conversation\n"
" # instead of a lossy summary so the model really remembers.\n"
" seeded_from_dataset = 0\n"
" try:\n"
" from backend.session_logger import load_session_history\n"
" ds_rows = load_session_history(session_id)\n"
" full_msgs = [\n"
" r for r in ds_rows\n"
" if r.get(\"role\") in (\"user\", \"assistant\", \"tool\")\n"
" ]\n"
" if full_msgs:\n"
" for raw in full_msgs:\n"
" try:\n"
" session.context_manager.items.append(Message.model_validate(raw))\n"
" seeded_from_dataset += 1\n"
" except Exception as e:\n"
" logger.warning(\"Dropping dataset msg during seed: %s\", e)\n"
" except Exception as e:\n"
" logger.debug(\"Dataset seed fallback failed: %s\", e)\n"
"\n"
" if seeded_from_dataset:\n"
" self._touch(agent_session)\n"
" await self.persist_session_snapshot(agent_session, runtime_state=\"idle\")\n"
" return seeded_from_dataset\n"
"\n"
" seed = Message(\n"
" role=\"user\",\n"
" content=(\n"
" \"[SYSTEM: Your prior memory of this conversation \u2014 written \"\n"
" \"in your own voice right before restart. Continue from here.]\\n\\n\"\n"
" + (summary or \"(no summary returned)\")\n"
" ),\n"
" )\n"
)
content, ok = _replace(content, old_seed, new_seed)
if ok:
print("+ seed_from_summary restores full dataset history")
changed = True
else:
print("INFO: seed block anchor not found")
if changed:
try:
ast.parse(content)
_write(SESSION_MANAGER, content)
print("OK: session_manager.py patched (dataset context restore)")
except SyntaxError as e:
print("FAIL: session_manager.py syntax error:", e)
else:
print("SKIP: no changes applied")
# ---- Verify importability of patched modules -----------------------------
try:
import ast as _ast
for f in ["/app/backend/session_manager.py", "/app/backend/session_logger.py", "/app/backend/routes/agent.py"]:
if os.path.exists(f):
_ast.parse(open(f, encoding="utf-8").read())
print("SYNTAX OK:", f)
except Exception as e:
print("VERIFY ERR:", e)