File size: 8,719 Bytes
6e62f72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/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)