bep40 commited on
Commit
8feb239
Β·
verified Β·
1 Parent(s): 2440424

Upload patch_catch_up_fix_backend.py

Browse files
Files changed (1) hide show
  1. patch_catch_up_fix_backend.py +218 -0
patch_catch_up_fix_backend.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Patch: Fix catch-up backend robustness.
4
+
5
+ Issues fixed:
6
+ 1. /api/session-history/{session_id} returns ALL rows including 'event' type
7
+ rows (role='event'). These cause Message.model_validate to silently drop them.
8
+ Fix: Filter out non-message roles (event, system) from history endpoint.
9
+
10
+ 2. seed_from_summary raises if summarization fails (API error, rate limit, credits),
11
+ causing restore-summary to return 500 and the frontend to show "Couldn't catch up."
12
+ Fix: Fall back to raw messages if summarization fails.
13
+
14
+ 3. Event rows with role='event' fail Message.model_validate.
15
+ Fix: Skip event rows explicitly in seed_from_summary.
16
+ """
17
+
18
+ import ast
19
+ import os
20
+ import sys
21
+
22
+ PATCHED_MARKER = "# === PATCH: catch-up fix (backend) ==="
23
+
24
+
25
+ def _safe_replace(content, old, new, count=1):
26
+ if old in content:
27
+ content = content.replace(old, new, count)
28
+ return content, True
29
+ return content, False
30
+
31
+
32
+ def patch_file(path, patch_fn):
33
+ if not os.path.exists(path):
34
+ print(f"SKIP: {path} not found")
35
+ return False
36
+ try:
37
+ with open(path, "r") as f:
38
+ content = f.read()
39
+ content, changed = patch_fn(content)
40
+ if not changed:
41
+ print(f"SKIP: {path} β€” no changes needed")
42
+ return False
43
+ try:
44
+ ast.parse(content)
45
+ except SyntaxError as e:
46
+ print(f"FAIL: {path} syntax: {e}")
47
+ return False
48
+ with open(path, "w") as f:
49
+ f.write(content)
50
+ print(f"OK: {path} patched (catch-up fix)")
51
+ return True
52
+ except Exception as e:
53
+ print(f"WARN: {path} patch error: {e}")
54
+ return False
55
+
56
+
57
+ # ─── Patch routes/agent.py: filter event rows from session-history ─────────
58
+ print("=== Patching routes/agent.py (session-history filter) ===")
59
+
60
+ ROUTES_AGENT = "/app/backend/routes/agent.py"
61
+
62
+ def _patch_routes(content):
63
+ changed = False
64
+
65
+ if PATCHED_MARKER in content:
66
+ print(" SKIP: already patched")
67
+ return content, False
68
+
69
+ # Patch the get_session_history endpoint to filter out event rows
70
+ old_endpoint = (
71
+ '@router.get("/session-history/{session_id}")\n'
72
+ 'async def get_session_history(\n'
73
+ ' session_id: str, user: dict = Depends(get_current_user)\n'
74
+ ') -> list:\n'
75
+ ' """Get full message history for a session from the permanent dataset."""\n'
76
+ ' from backend.session_logger import load_session_history\n'
77
+ ' return load_session_history(session_id)'
78
+ )
79
+
80
+ new_endpoint = (
81
+ '@router.get("/session-history/{session_id}")\n'
82
+ 'async def get_session_history(\n'
83
+ ' session_id: str, user: dict = Depends(get_current_user)\n'
84
+ ') -> list:\n'
85
+ ' """Get full message history for a session from the permanent dataset.\n'
86
+ '\n'
87
+ ' Returns only valid conversation messages (user/assistant/tool roles).\n'
88
+ ' Event rows (role=event) are filtered out because they are internal\n'
89
+ ' tracing data that cannot be parsed as Message objects and would be\n'
90
+ ' silently dropped during catch-up, causing message loss.\n'
91
+ ' """\n'
92
+ ' from backend.session_logger import load_session_history\n'
93
+ ' rows = load_session_history(session_id)\n'
94
+ ' # Filter out event rows and any rows without valid roles\n'
95
+ ' valid_roles = {"user", "assistant", "tool"}\n'
96
+ ' return [r for r in rows if r.get("role") in valid_roles]'
97
+ )
98
+
99
+ content, ok = _safe_replace(content, old_endpoint, new_endpoint)
100
+ if ok:
101
+ print(" + Filtered event rows from session-history endpoint")
102
+ changed = True
103
+ else:
104
+ print(" SKIP: target block not found")
105
+
106
+ # Add marker
107
+ content = content.rstrip() + "\n\n" + PATCHED_MARKER + "\n"
108
+ changed = True
109
+
110
+ return content, changed
111
+
112
+ patch_file(ROUTES_AGENT, _patch_routes)
113
+
114
+ # ─── Patch session_manager.py: robust seed_from_summary ─────────────────────
115
+ print("\n=== Patching session_manager.py (seed_from_summary robustness) ===")
116
+
117
+ SESSION_MANAGER = "/app/backend/session_manager.py"
118
+
119
+ def _patch_seed_from_summary(content):
120
+ changed = False
121
+
122
+ # Check if already patched
123
+ if PATCHED_MARKER in content:
124
+ print(" SKIP: already patched")
125
+ return content, False
126
+
127
+ # Patch 1: Skip event rows explicitly in seed_from_summary
128
+ old_parse = (
129
+ ' # Parse into Message objects, tolerating malformed entries.\n'
130
+ ' parsed: list[Message] = []\n'
131
+ ' for raw in messages:\n'
132
+ ' if raw.get("role") == "system":\n'
133
+ ' continue # the new session has its own system prompt\n'
134
+ ' try:\n'
135
+ ' parsed.append(Message.model_validate(raw))\n'
136
+ ' except Exception as e:\n'
137
+ ' logger.warning("Dropping malformed message during seed: %s", e)'
138
+ )
139
+
140
+ new_parse = (
141
+ ' # Parse into Message objects, tolerating malformed entries.\n'
142
+ ' # === PATCH: skip event rows (not just system) ===\n'
143
+ ' parsed: list[Message] = []\n'
144
+ ' for raw in messages:\n'
145
+ ' role = raw.get("role", "")\n'
146
+ ' if role in ("system", "event"):\n'
147
+ ' continue # not conversation messages\n'
148
+ ' try:\n'
149
+ ' parsed.append(Message.model_validate(raw))\n'
150
+ ' except Exception as e:\n'
151
+ ' logger.warning("Dropping malformed message during seed: %s", e)'
152
+ )
153
+
154
+ content, ok = _safe_replace(content, old_parse, new_parse)
155
+ if ok:
156
+ print(" + Skip event rows explicitly in seed_from_summary")
157
+ changed = True
158
+
159
+ # Patch 2: Fall back to raw messages if summarization fails
160
+ old_try = (
161
+ ' try:\n'
162
+ ' summary, _ = await summarize_messages(\n'
163
+ ' parsed,\n'
164
+ ' model_name=session.config.model_name,\n'
165
+ ' hf_token=session.hf_token,\n'
166
+ ' max_tokens=4000,\n'
167
+ ' prompt=_RESTORE_PROMPT,\n'
168
+ ' tool_specs=tool_specs,\n'
169
+ ' session=session,\n'
170
+ ' kind="restore",\n'
171
+ ' )\n'
172
+ ' except Exception as e:\n'
173
+ ' logger.error("Summary call failed during seed: %s", e)\n'
174
+ ' raise\n'
175
+ '\n'
176
+ ' seed = Message('
177
+ )
178
+
179
+ new_try = (
180
+ ' # === PATCH: fall back to raw messages if summarization fails ===\n'
181
+ ' summary = None\n'
182
+ ' try:\n'
183
+ ' summary, _ = await summarize_messages(\n'
184
+ ' parsed,\n'
185
+ ' model_name=session.config.model_name,\n'
186
+ ' hf_token=session.hf_token,\n'
187
+ ' max_tokens=4000,\n'
188
+ ' prompt=_RESTORE_PROMPT,\n'
189
+ ' tool_specs=tool_specs,\n'
190
+ ' session=session,\n'
191
+ ' kind="restore",\n'
192
+ ' )\n'
193
+ ' except Exception as e:\n'
194
+ ' logger.error("Summary call failed during seed, falling back to raw messages: %s", e)\n'
195
+ ' # Fall back: inject raw messages directly into the new session context\n'
196
+ ' # This preserves the full conversation instead of losing it\n'
197
+ ' for msg in parsed:\n'
198
+ ' session.context_manager.items.append(msg)\n'
199
+ ' self._touch(agent_session)\n'
200
+ ' await self.persist_session_snapshot(agent_session, runtime_state="idle")\n'
201
+ ' return len(parsed)\n'
202
+ '\n'
203
+ ' seed = Message('
204
+ )
205
+
206
+ content, ok = _safe_replace(content, old_try, new_try)
207
+ if ok:
208
+ print(" + Fall back to raw messages if summarization fails")
209
+ changed = True
210
+
211
+ if changed:
212
+ content = content + "\n" + PATCHED_MARKER + "\n"
213
+
214
+ return content, changed
215
+
216
+ patch_file(SESSION_MANAGER, _patch_seed_from_summary)
217
+
218
+ print("\n=== Backend catch-up fixes applied ===")