File size: 8,432 Bytes
8feb239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bf6eb9
8feb239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bf6eb9
8feb239
 
2bf6eb9
8feb239
 
 
 
 
 
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/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 ===")