File size: 31,918 Bytes
858487e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#!/usr/bin/env python3
"""
Patch v2: Comprehensive catch-up + shareable URL fix for bep40/ml-intern.

Addresses three problems:
  1. "Catch me up" throws errors frequently
  2. Context is lost after catch-up (context preserved in session but not restored)
  3. Per-session shareable URLs

Runs during Docker build β€” never fails the build.
"""

import ast
import os
import re
import sys

# ─── Backend paths (exist in the Python/stage-2 build) ───────────────────────

BACKEND_SESSION_MANAGER = "/app/backend/session_manager.py"
BACKEND_SESSION_LOGGER = "/app/backend/session_logger.py"
BACKEND_ROUTES_AGENT = "/app/backend/routes/agent.py"

MARKER = "# === PATCH: catch-up-v2 ==="


def _read(path):
    with open(path, "r", encoding="utf-8") as f:
        return f.read()


def _write(path, content):
    with open(path, "w", encoding="utf-8") as f:
        f.write(content)


def _safe_replace(content, old, new):
    if old in content:
        return content.replace(old, new, 1), True
    return content, False


# ─── Backend session_manager.py ─────────────────────────────────────────────

print("=== Patching session_manager.py ===")

if os.path.exists(BACKEND_SESSION_MANAGER):
    content = _read(BACKEND_SESSION_MANAGER)
    changed = False

    # 1. seed_from_summary: fall back to raw messages on summary failure
    if 'logger.error("Summary call failed during seed: %s", e)' in content and "Fall back to raw messages" not in content:
        old = (
            '        except Exception as e:\n'
            '            logger.error("Summary call failed during seed: %s", e)\n'
            '            raise\n'
        )
        new = (
            '        except Exception as e:\n'
            '            logger.error("Summary call failed during seed, falling back to raw messages: %s", e)\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'
        )
        if old in content:
            content, ok = _safe_replace(content, old, new)
            if ok:
                print("  + seed_from_summary falls back to raw messages")
                changed = True
        else:
            # Try regex match for the pattern
            pattern = re.compile(
                r'            logger\.error\("Summary call failed during seed: %s", e\)\n            raise\n'
            )
            match = pattern.search(content)
            if match:
                content = pattern.sub(
                    '            logger.error("Summary call failed during seed, falling back to raw messages: %s", e)\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',
                    content, count=1
                )
                print("  + seed_from_summary fallback (regex match)")
                changed = True

    # 2. seed_from_summary: skip event rows
    if "skip event rows" not in content.lower():
        old_parse = (
            '        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'
        )
        new_parse = (
            '        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'
        )
        if old_parse in content:
            content, ok = _safe_replace(content, old_parse, new_parse)
            if ok:
                print("  + seed_from_summary skips event rows")
                changed = True

    # 3. Add record_catchup_done method
    if "def record_catchup_done" not in content:
        insert_after = "        return len(parsed)\n\n    @staticmethod"
        if insert_after in content:
            new_method = (
                "        return len(parsed)\n\n"
                "    async def record_catchup_done(self, session_id: str) -> None:\n"
                "        \"\"\"Record that a session was successfully catch-up restored.\"\"\"\n"
                "        store = self._store()\n"
                "        if not getattr(store, 'enabled', False):\n"
                "            return\n"
                "        try:\n"
                "            await store.update_session_fields(\n"
                "                session_id,\n"
                "                catchup_done=True,\n"
                "            )\n"
                "        except Exception as e:\n"
                "            logger.debug('record_catchup_done failed: %s', e)\n\n"
                "    @staticmethod"
            )
            content, ok = _safe_replace(content, insert_after, new_method)
            if ok:
                print("  + Added record_catchup_done method")
                changed = True

    # 4. Record catchup_done after seed
    old_seed_done = (
        '        session.context_manager.items.append(seed)\n'
        '        self._touch(agent_session)\n'
        '        await self.persist_session_snapshot(agent_session, runtime_state="idle")\n'
        '        return len(parsed)\n'
    )
    new_seed_done = (
        '        session.context_manager.items.append(seed)\n'
        '        self._touch(agent_session)\n'
        '        await self.persist_session_snapshot(agent_session, runtime_state="idle")\n'
        '        # === PATCH: catch-up-v2 ===\n'
        '        try:\n'
        '            await self.record_catchup_done(session_id)\n'
        '        except Exception as e:\n'
        '            logger.debug("record_catchup_done failed: %s", e)\n'
        '        return len(parsed)\n'
    )
    if old_seed_done in content:
        content, ok = _safe_replace(content, old_seed_done, new_seed_done)
        if ok:
            print("  + seed_from_summary records catchup_done")
            changed = True

    if changed:
        if MARKER not in content:
            content = content + "\n" + MARKER + "\n"
        try:
            ast.parse(content)
            _write(BACKEND_SESSION_MANAGER, content)
            print("OK: session_manager.py patched")
        except SyntaxError as e:
            print(f"FAIL: session_manager.py syntax: {e}")
else:
    print("SKIP: session_manager.py not found")


# ─── Backend session_logger.py ───────────────────────────────────────────────

print("\n=== Patching session_logger.py ===")

if os.path.exists(BACKEND_SESSION_LOGGER):
    content = _read(BACKEND_SESSION_LOGGER)
    changed = False

    # load_session_history: filter event rows at source
    sort_line = '    rows.sort(key=lambda r: (r.get("seq", 0), r.get("timestamp", "")))'
    if sort_line in content and 'valid_roles' not in content.split(sort_line, 1)[1][:300]:
        idx = content.find(sort_line)
        end_idx = content.find("\n", idx) + 1
        insert_code = (
            '    # === PATCH: catch-up-v2 ===\n'
            '    # Filter out event/system rows so summarization never sees\n'
            '    # internal tracing data that cant be parsed as Message objects.\n'
            '    valid_roles = {"user", "assistant", "tool"}\n'
            '    rows = [r for r in rows if r.get("role") in valid_roles]\n'
        )
        content = content[:end_idx] + insert_code + content[end_idx:]
        print("  + load_session_history filters event rows")
        changed = True

    if changed:
        try:
            ast.parse(content)
            _write(BACKEND_SESSION_LOGGER, content)
            print("OK: session_logger.py patched")
        except SyntaxError as e:
            print(f"FAIL: session_logger.py syntax: {e}")
else:
    print("SKIP: session_logger.py not found")


# ─── Backend routes/agent.py ─────────────────────────────────────────────────

print("\n=== Patching routes/agent.py ===")

if os.path.exists(BACKEND_ROUTES_AGENT):
    content = _read(BACKEND_ROUTES_AGENT)
    changed = False

    # Ensure session-history endpoint filters events
    old_history = (
        '    from backend.session_logger import load_session_history\n'
        '    return load_session_history(session_id)'
    )
    new_history = (
        '    from backend.session_logger import load_session_history\n'
        '    rows = load_session_history(session_id)\n'
        '    valid_roles = {"user", "assistant", "tool"}\n'
        '    return [r for r in rows if r.get("role") in valid_roles]'
    )
    if old_history in content:
        content, ok = _safe_replace(content, old_history, new_history)
        if ok:
            print("  + session-history endpoint filters event rows")
            changed = True

    # Add catchup-done endpoint
    if "/session/{session_id}/catchup-done" not in content:
        catchup_endpoint = '''

@router.post("/session/{session_id}/catchup-done")
async def mark_catchup_done(
    session_id: str, user: dict = Depends(get_current_user)
) -> dict:
    """Mark that a session was successfully catch-up restored.

    The frontend calls this after a successful catch-up so the backend
    can track which sessions were restored from summary vs. fresh.
    """
    await _check_session_access(session_id, user, preload_sandbox=False)
    try:
        await session_manager.record_catchup_done(session_id)
    except Exception as e:
        logger.warning("record_catchup_done failed for %s: %s", session_id, e)
    return {"status": "ok", "session_id": session_id}
'''
        content = content.rstrip() + "\n" + catchup_endpoint
        print("  + Added /session/{id}/catchup-done endpoint")
        changed = True

    if changed:
        try:
            ast.parse(content)
            _write(BACKEND_ROUTES_AGENT, content)
            print("OK: routes/agent.py patched")
        except SyntaxError as e:
            print(f"FAIL: routes/agent.py syntax: {e}")
else:
    print("SKIP: routes/agent.py not found")


# ─── Frontend patches ────────────────────────────────────────────────────────

# These files are only present in the frontend-builder stage.

FRONTEND_DIR = "/source/frontend/src"
FE_MARKER = "// === PATCH: catch-up-v2 ==="
URL_MARKER = "// === PATCH: shareable-url-v2 ==="


def patch_frontend_file(relpath, patch_fn, label):
    path = os.path.join(FRONTEND_DIR, relpath)
    if not os.path.exists(path):
        print(f"SKIP: {path} not found")
        return
    with open(path, "r", encoding="utf-8") as f:
        content = f.read()
    content, changed = patch_fn(content, path)
    if not changed:
        print(f"SKIP: {path} β€” no changes needed ({label})")
        return
    with open(path, "w", encoding="utf-8") as f:
        f.write(content)
    print(f"OK: {path} patched ({label})")


# ─── useAgentChat.ts ─────────────────────────────────────────────────────────

print("\n=== Patching useAgentChat.ts ===")


def patch_useAgentChat(content, _path):
    changed = False

    if FE_MARKER in content:
        print("  SKIP: already patched")
        return content, False

    # 1. Add catch-up guard + URL sync helpers after chatActionsRef
    ref_def = "  const chatActionsRef = useRef<"
    if ref_def in content:
        idx = content.find(ref_def)
        end_idx = content.find("});", idx)
        if end_idx > 0:
            insert_after = content.find("\n", end_idx) + 1
            helper = '''
  // === PATCH: catch-up-v2 (frontend) ===
  // After catch-up (renameSession), the backend only has the summary message
  // but the frontend has the full conversation in localStorage. These guards
  // prevent the backend's sparse data from overwriting the frontend's rich
  // conversation. The ml-intern-catchup-done flag is set after renameSession
  // and cleared after the first message is sent.
  const _catchupDone = (() => {
    try {
      return localStorage.getItem(`ml-intern-catchup-done:${sessionId}`) === 'true';
    } catch {
      return false;
    }
  })();
  const _clearCatchupDone = useCallback(() => {
    try {
      localStorage.removeItem(`ml-intern-catchup-done:${sessionId}`);
    } catch {}
  }, [sessionId]);
'''
            content = content[:insert_after] + helper + content[insert_after:]
            print("  + Added catchup guard helper")
            changed = True

    # 2. Guard onRecoverMessages
    old_recover = (
        "          const setMsgs = chatActionsRef.current.setMessages;\n"
        "          if (setMsgs && uiMsgs.length >= currentMessageCount) {\n"
        "            setMsgs(uiMsgs);\n"
        "            saveMessages(sessionId, uiMsgs);\n"
        "          }"
    )
    new_recover = (
        "          const setMsgs = chatActionsRef.current.setMessages;\n"
        "          // === PATCH: catch-up-v2 (frontend) ===\n"
        "          const _feCount = chatActionsRef.current.messages.length;\n"
        "          if (setMsgs && (!_catchupDone || uiMsgs.length >= _feCount) && uiMsgs.length >= currentMessageCount) {\n"
        "            setMsgs(uiMsgs);\n"
        "            saveMessages(sessionId, uiMsgs);\n"
        "          }"
    )
    content, ok = _safe_replace(content, old_recover, new_recover)
    if ok:
        print("  + Guarded onRecoverMessages")
        changed = True

    # 3. Guard refreshMessages
    old_refresh = (
        "      const setMsgs = chatActionsRef.current.setMessages;\n"
        "      if (setMsgs && uiMsgs.length > 0) {\n"
        "        setMsgs(uiMsgs);\n"
        "        saveMessages(sessionId, uiMsgs);\n"
        "      }\n"
        "      return true;"
    )
    new_refresh = (
        "      const setMsgs = chatActionsRef.current.setMessages;\n"
        "      // === PATCH: catch-up-v2 (frontend) ===\n"
        "      const _feCountR = chatActionsRef.current.messages.length;\n"
        "      if (setMsgs && (!_catchupDone || uiMsgs.length > _feCountR) && uiMsgs.length > 0) {\n"
        "        setMsgs(uiMsgs);\n"
        "        saveMessages(sessionId, uiMsgs);\n"
        "      }\n"
        "      return true;"
    )
    content, ok = _safe_replace(content, old_refresh, new_refresh)
    if ok:
        print("  + Guarded refreshMessages")
        changed = True

    # 4. Guard all chat.setMessages(uiMsgs) in hydration paths (10-space indent)
    old_hydrate = (
        "          if (uiMsgs.length > 0) {\n"
        "            chat.setMessages(uiMsgs);\n"
        "            saveMessages(sessionId, uiMsgs);\n"
        "          }"
    )
    new_hydrate = (
        "          // === PATCH: catch-up-v2 (frontend) ===\n"
        "          const _feCountH = chatActionsRef.current.messages.length;\n"
        "          if (uiMsgs.length > 0 && (!_catchupDone || uiMsgs.length > _feCountH)) {\n"
        "            chat.setMessages(uiMsgs);\n"
        "            saveMessages(sessionId, uiMsgs);\n"
        "          }"
    )
    count = 0
    while old_hydrate in content:
        content, ok = _safe_replace(content, old_hydrate, new_hydrate)
        if ok:
            count += 1
            changed = True
        else:
            break
    if count > 0:
        print(f"  + Guarded {count} hydration setMessages paths")

    # 5. Wrap sendMessage to clear catchup flag + update URL
    old_send = "    sendMessage: chat.sendMessage,\n    stop,\n    status: chat.status,\n"
    if old_send in content:
        new_send = (
            "    sendMessage,\n"
            "    stop,\n"
            "    status: chat.status,\n"
        )
        # Actually, we need to insert the wrapper BEFORE the return and change sendMessage in it
        # Find the return block
        return_block = "  return {\n    messages: chat.messages,\n    sendMessage: chat.sendMessage,\n    stop,\n    status: chat.status,\n    undoLastTurn,\n"
        if return_block in content:
            wrapper_block = (
                "  const _sendMessageWrapped = useCallback((msg) => {\n"
                "    // === PATCH: catch-up-v2 (frontend) ===\n"
                "    _clearCatchupDone();\n"
                "    // === PATCH: shareable-url-v2 ===\n"
                "    const params = new URLSearchParams(window.location.search);\n"
                "    if (params.get('chat') !== sessionId) {\n"
                "      params.set('chat', sessionId);\n"
                "      const qs = params.toString();\n"
                "      window.history.replaceState(null, '', qs ? `${window.location.pathname}?${qs}` : window.location.pathname);\n"
                "    }\n"
                "    return chat.sendMessage(msg);\n"
                "  }, [chat, _clearCatchupDone, sessionId]);\n"
                "\n"
                "  return {\n    messages: chat.messages,\n    sendMessage: _sendMessageWrapped,\n    stop,\n    status: chat.status,\n    undoLastTurn,\n"
            )
            content, ok = _safe_replace(content, return_block, wrapper_block)
            if ok:
                print("  + Wrapped sendMessage (clear flag + URL sync)")
                changed = True
        else:
            # Try the simpler pattern
            simple_return = "  return {\n    messages: chat.messages,\n    sendMessage: chat.sendMessage,\n"
            if simple_return in content:
                wrapper_block = (
                    "  const _sendMessageWrapped = useCallback((msg) => {\n"
                    "    // === PATCH: catch-up-v2 (frontend) ===\n"
                    "    _clearCatchupDone();\n"
                    "    // === PATCH: shareable-url-v2 ===\n"
                    "    const params = new URLSearchParams(window.location.search);\n"
                    "    if (params.get('chat') !== sessionId) {\n"
                    "      params.set('chat', sessionId);\n"
                    "      const qs = params.toString();\n"
                    "      window.history.replaceState(null, '', qs ? `${window.location.pathname}?${qs}` : window.location.pathname);\n"
                    "    }\n"
                    "    return chat.sendMessage(msg);\n"
                    "  }, [chat, _clearCatchupDone, sessionId]);\n"
                    "\n"
                    "  return {\n    messages: chat.messages,\n    sendMessage: _sendMessageWrapped,\n"
                )
                content, ok = _safe_replace(content, simple_return, wrapper_block)
                if ok:
                    print("  + Wrapped sendMessage (simple pattern)")
                    changed = True

    if changed and "import { logger } from '@/utils/logger';" in content:
        content = content.replace(
            "import { logger } from '@/utils/logger';",
            "import { logger } from '@/utils/logger';\n" + FE_MARKER + " applied",
            1
        )

    return content, changed


# ─── ExpiredBanner.tsx ──────────────────────────────────────────────────────

print("\n=== Patching ExpiredBanner.tsx ===")


def patch_expired_banner(content, _path):
    changed = False

    if FE_MARKER in content:
        print("  SKIP: already patched")
        return content, False

    old_success = (
        "      useAgentStore.getState().clearSessionState(sessionId);\n"
        "      renameSession(sessionId, newId);\n"
        "      if (data.model) updateSessionModel(newId, data.model);"
    )
    new_success = (
        "      useAgentStore.getState().clearSessionState(sessionId);\n"
        "      renameSession(sessionId, newId);\n"
        "      if (data.model) updateSessionModel(newId, data.model);\n"
        "      // === PATCH: catch-up-v2 (frontend) ===\n"
        "      // Mark session as catch-up done so hydration paths don't\n"
        "      // overwrite the full localStorage conversation with the\n"
        "      // backend's sparse summary-only data.\n"
        "      try {\n"
        "        localStorage.setItem(`ml-intern-catchup-done:${newId}`, 'true');\n"
        "      } catch {}\n"
        "      // Also notify the backend\n"
        "      try { await apiFetch(`/api/session/${newId}/catchup-done`, { method: 'POST' }); } catch {}"
    )
    content, ok = _safe_replace(content, old_success, new_success)
    if ok:
        print("  + Set catchup-done flag after successful catch-up")
        changed = True
    else:
        print("  INFO: catch-up success pattern not found")

    if changed and "import { logger } from '@/utils/logger';" in content:
        content = content.replace(
            "import { logger } from '@/utils/logger';",
            "import { logger } from '@/utils/logger';\n" + FE_MARKER + " applied",
            1
        )

    return content, changed


# ─── AppLayout.tsx ───────────────────────────────────────────────────────────

print("\n=== Patching AppLayout.tsx ===")


def patch_app_layout(content, _path):
    changed = False

    if URL_MARKER in content:
        print("  SKIP: already patched")
        return content, False

    # 1. Add ShareIcon import
    if "import LightModeOutlinedIcon from '@mui/icons-material/LightModeOutlined';" in content:
        content = content.replace(
            "import LightModeOutlinedIcon from '@mui/icons-material/LightModeOutlined';",
            "import LightModeOutlinedIcon from '@mui/icons-material/LightModeOutlined';\n"
            "import ShareIcon from '@mui/icons-material/Share';\n"
            "// === PATCH: shareable-url-v2 ===",
            1
        )
        print("  + Added ShareIcon import")
        changed = True

    # 2. Add URL-based session selection on mount
    old_health = "  // -- LLM health check on mount -----------------------------------------"
    new_code = """  // === PATCH: shareable-url-v2 ===
  // On mount, read ?chat={sessionId} from URL and auto-select that session
  useEffect(() => {
    const params = new URLSearchParams(window.location.search);
    const chatParam = params.get('chat');
    if (chatParam) {
      const { sessions: currentSessions, switchSession } = useSessionStore.getState();
      if (currentSessions.find((s) => s.id === chatParam)) {
        switchSession(chatParam);
      }
    }
  }, []); // eslint-disable-line react-hooks/exhaustive-deps

  // Update URL whenever the active session changes
  useEffect(() => {
    if (activeSessionId) {
      const params = new URLSearchParams(window.location.search);
      if (params.get('chat') !== activeSessionId) {
        params.set('chat', activeSessionId);
        const qs = params.toString();
        window.history.replaceState(null, '', qs ? `${window.location.pathname}?${qs}` : window.location.pathname);
      }
    }
  }, [activeSessionId]);

  // -- LLM health check on mount -----------------------------------------"""
    content, ok = _safe_replace(content, old_health, new_code)
    if ok:
        print("  + URL-based session selection + URL sync on active change")
        changed = True

    # 3. Add Share button to the header
    old_avatar = (
        "          {user?.picture ? (\n"
        "            <Avatar\n"
        "              src={user.picture}\n"
        "              alt={user.username || 'User'}\n"
        "              sx={{ width: 28, height: 28, ml: 0.5 }}\n"
        "            />\n"
        "          ) : user?.username ? ("
    )
    new_avatar = (
        "          {activeSessionId && (\n"
        "            <Tooltip title=\"Share this chat\">\n"
        "              <IconButton\n"
        "                size=\"small\"\n"
        "                onClick={async () => {\n"
        "                  const url = window.location.href;\n"
        "                  if (navigator.clipboard?.writeText) {\n"
        "                    await navigator.clipboard.writeText(url);\n"
        "                  } else {\n"
        "                    prompt('Copy this link:', url);\n"
        "                  }\n"
        "                }}\n"
        "                sx={{ color: 'text.secondary', ml: 0.5 }}\n"
        "              >\n"
        "                <ShareIcon fontSize=\"small\" />\n"
        "              </IconButton>\n"
        "            </Tooltip>\n"
        "          )}\n"
        "          {user?.picture ? (\n"
        "            <Avatar\n"
        "              src={user.picture}\n"
        "              alt={user.username || 'User'}\n"
        "              sx={{ width: 28, height: 28, ml: 0.5 }}\n"
        "            />\n"
        "          ) : user?.username ? ("
    )
    content, ok = _safe_replace(content, old_avatar, new_avatar)
    if ok:
        print("  + Added Share button to header")
        changed = True

    return content, changed


# ─── SessionSidebar.tsx ──────────────────────────────────────────────────────

print("\n=== Patching SessionSidebar.tsx ===")


def patch_session_sidebar(content, _path):
    changed = False

    if URL_MARKER in content:
        print("  SKIP: already patched")
        return content, False

    # 1. URL update after handleNewSession
    old_create = (
        "      createSession(data.session_id, data.model);\n"
        "      void useUsageStore.getState().fetchUsage(data.session_id);\n"
        "      setPlan([]);\n"
        "      clearPanel();\n"
        "      onClose?.();"
    )
    new_create = (
        "      createSession(data.session_id, data.model);\n"
        "      void useUsageStore.getState().fetchUsage(data.session_id);\n"
        "      setPlan([]);\n"
        "      clearPanel();\n"
        "      // === PATCH: shareable-url-v2 ===\n"
        "      const _params = new URLSearchParams(window.location.search);\n"
        "      _params.set('chat', data.session_id);\n"
        "      const _qs = _params.toString();\n"
        "      window.history.replaceState(null, '', _qs ? `${window.location.pathname}?${_qs}` : window.location.pathname);\n"
        "      onClose?.();"
    )
    content, ok = _safe_replace(content, old_create, new_create)
    if ok:
        print("  + URL update on new session")
        changed = True

    # 2. URL update on second new session (delete-last case)
    old_create2 = (
        "          createSession(data.session_id, data.model);\n"
        "          void useUsageStore.getState().fetchUsage(data.session_id);\n"
        "          setPlan([]);\n"
        "          clearPanel();"
    )
    new_create2 = (
        "          createSession(data.session_id, data.model);\n"
        "          void useUsageStore.getState().fetchUsage(data.session_id);\n"
        "          setPlan([]);\n"
        "          clearPanel();\n"
        "          // === PATCH: shareable-url-v2 ===\n"
        "          const _params2 = new URLSearchParams(window.location.search);\n"
        "          _params2.set('chat', data.session_id);\n"
        "          const _qs2 = _params2.toString();\n"
        "          window.history.replaceState(null, '', _qs2 ? `${window.location.pathname}?${_qs2}` : window.location.pathname);"
    )
    content, ok = _safe_replace(content, old_create2, new_create2)
    if ok:
        print("  + URL update on new session (delete-last case)")
        changed = True

    # 3. URL update on session select
    old_select = (
        "    const shouldActivateSession = sessionId !== activeSessionId;\n"
        "    switchSession(sessionId);"
    )
    new_select = (
        "    const shouldActivateSession = sessionId !== activeSessionId;\n"
        "    switchSession(sessionId);\n"
        "    // === PATCH: shareable-url-v2 ===\n"
        "    const _p = new URLSearchParams(window.location.search);\n"
        "    if (_p.get('chat') !== sessionId) {\n"
        "      _p.set('chat', sessionId);\n"
        "      const _q = _p.toString();\n"
        "      window.history.replaceState(null, '', _q ? `${window.location.pathname}?${_q}` : window.location.pathname);\n"
        "    }"
    )
    content, ok = _safe_replace(content, old_select, new_select)
    if ok:
        print("  + URL update on session select")
        changed = True

    return content, changed


# ─── WelcomeScreen.tsx ───────────────────────────────────────────────────────

print("\n=== Patching WelcomeScreen.tsx ===")

WS_PATH = os.path.join(FRONTEND_DIR, "components/WelcomeScreen/WelcomeScreen.tsx")
if os.path.exists(WS_PATH):
    with open(WS_PATH, "r", encoding="utf-8") as f:
        ws_content = f.read()
    if URL_MARKER not in ws_content:
        old_ws = (
            "      createSession(data.session_id, data.model);\n"
            "      void useUsageStore.getState().fetchUsage(data.session_id);\n"
            "      setPlan([]);\n"
            "      clearPanel();\n"
            "    } catch"
        )
        new_ws = (
            "      createSession(data.session_id, data.model);\n"
            "      void useUsageStore.getState().fetchUsage(data.session_id);\n"
            "      setPlan([]);\n"
            "      clearPanel();\n"
            "      // === PATCH: shareable-url-v2 ===\n"
            "      const _params = new URLSearchParams(window.location.search);\n"
            "      _params.set('chat', data.session_id);\n"
            "      const _qs = _params.toString();\n"
            "      window.history.replaceState(null, '', _qs ? `${window.location.pathname}?${_qs}` : window.location.pathname);\n"
            "    } catch"
        )
        if old_ws in ws_content:
            ws_content = ws_content.replace(old_ws, new_ws, 1)
            with open(WS_PATH, "w", encoding="utf-8") as f:
                f.write(ws_content)
            print("  + URL update on welcome screen session creation")
        else:
            print("  INFO: welcome screen pattern not found")
    else:
        print("  SKIP: already patched")
else:
    print("SKIP: WelcomeScreen.tsx not found")


# ─── Execute frontend patches ─────────────────────────────────────────────────

patch_frontend_file("hooks/useAgentChat.ts", patch_useAgentChat, "catch-up guard + URL sync")
patch_frontend_file("components/Chat/ExpiredBanner.tsx", patch_expired_banner, "catchup-done flag")
patch_frontend_file("components/Layout/AppLayout.tsx", patch_app_layout, "URL session selection + share button")
patch_frontend_file("components/SessionSidebar/SessionSidebar.tsx", patch_session_sidebar, "URL update on session actions")

print("\n=== All v2 patches applied ===")