File size: 27,460 Bytes
06f8c1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5a8153
 
 
 
 
 
 
 
 
 
 
 
06f8c1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5a8153
06f8c1f
 
 
 
 
b5a8153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06f8c1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5a8153
06f8c1f
 
 
 
b5a8153
 
 
 
 
 
06f8c1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5a8153
06f8c1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5a8153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06f8c1f
 
 
b5a8153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06f8c1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5a8153
06f8c1f
b5a8153
06f8c1f
 
 
b5a8153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06f8c1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5a8153
06f8c1f
b5a8153
06f8c1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5a8153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06f8c1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Patch v3: Fix the frontend build + complete catch-up & shareable-URL fixes.

Build failure root cause (v2):
  v2's patch_catch_up_fix_v2.py introduced THREE TypeScript build errors
  because its inner patterns were already modified by earlier patches
  (patch_catch_up_fix_frontend.py ran first and changed the same code):

    src/components/Layout/AppLayout.tsx(20,1): error TS6133:
      'ShareIcon' is declared but its value is never read.        <- share button patch FAILED to match
    src/hooks/useAgentChat.ts(572,9): error TS6133:
      '_catchupDone' is declared but its value is never read.     <- guard patches FAILED to match
    src/hooks/useAgentChat.ts(1174,44): error TS7006:
      Parameter 'msg' implicitly has an 'any' type.               <- wrapper typed poorly

  Because npm run build fails, /app/static ends up EMPTY and the Space
  serves NO frontend. All user-visible symptoms (catch-up errors, context
  loss, no share URL) trace back to this.

v3 fixes (runs AFTER all other patches, idempotent):
  1. useAgentChat.ts
     a. Wire _catchupDone into the existing v1 guards so the flag is used
        (kills TS6133) AND the "backend sparse summary overwrites full
        local conversation" race is actually prevented.
     b. Type _sendMessageWrapped's msg parameter using the same type as
        chat.sendMessage (kills TS7006).
  2. AppLayout.tsx
     a. Render the missing Share button in the header (kills TS6133 for
        ShareIcon). The button copies a PUBLIC share URL (?share=<id>).
     b. If the URL has ?share=<sessionId>, render a read-only ShareView
        instead of the chat (auth-free, works for anyone with the link).
  3. ShareView.tsx (new file): read-only transcript viewer.
  4. Backend routes/agent.py: add public GET /api/public/session/{id}
     (no auth) that serves sanitized conversation rows from the permanent
     public sessions dataset.
  5. Backend session_logger.py: fix v2's indent bug (insert filter
     correctly so no SyntaxError) β€” event rows filtered at source.

Never fails the build (all writes guarded by markers / ast.parse, and the
Dockerfile already appends `|| true`).
"""

import ast
import os


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 _replace(content, old, new):
    if old in content:
        return content.replace(old, new, 1), True
    return content, False


MARKER3 = "// === PATCH: catch-up-v3 ==="
MARKER3_PY = "# === PATCH: catch-up-v3 ==="


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 ({label})")
        return
    content = _read(path)
    content, changed = patch_fn(content, path)
    if not changed:
        print(f"SKIP: {path} β€” no changes needed ({label})")
        return
    _write(path, content)
    print(f"OK: {path} patched ({label})")


# ─── Frontend files ─────────────────────────────────────────────────────────

FRONTEND_DIR = "/source/frontend/src"


def patch_use_agent_chat(content, _path):
    """Wire _catchupDone into guards + type the sendMessage wrapper."""
    changed = False
    if MARKER3 in content:
        print("  SKIP: already patched")
        return content, False

    # (1a) onRecoverMessages guard (v1 already present):
    old_recover = (
        "          const _shouldOverwrite = uiMsgs.length > _currentNonSystem.length || _currentNonSystem.length === 0;\n"
        "          const setMsgs = chatActionsRef.current.setMessages;\n"
        "          if (setMsgs && _shouldOverwrite) {"
    )
    new_recover = (
        "          // === PATCH: catch-up-v3 ===\n"
        "          // After catch-up the backend only has the summary message;\n"
        "          // never let it clobber the full local conversation.\n"
        "          const _shouldOverwrite = _catchupDone\n"
        "            ? false\n"
        "            : (uiMsgs.length > _currentNonSystem.length || _currentNonSystem.length === 0);\n"
        "          const setMsgs = chatActionsRef.current.setMessages;\n"
        "          if (setMsgs && _shouldOverwrite) {"
    )
    content, ok = _replace(content, old_recover, new_recover)
    if ok:
        print("  + onRecoverMessages guard honors _catchupDone")
        changed = True
    else:
        print("  INFO: onRecoverMessages guard pattern not found (may already be v3)")

    # refreshMessages guard (v1 already present):
    old_refresh = (
        "      const _shouldOverwrite2 = uiMsgs.length > _currentNonSystem2.length || _currentNonSystem2.length === 0;\n"
        "      const setMsgs = chatActionsRef.current.setMessages;\n"
        "      if (setMsgs && _shouldOverwrite2) {"
    )
    new_refresh = (
        "      // === PATCH: catch-up-v3 ===\n"
        "      const _shouldOverwrite2 = _catchupDone\n"
        "        ? false\n"
        "        : (uiMsgs.length > _currentNonSystem2.length || _currentNonSystem2.length === 0);\n"
        "      const setMsgs = chatActionsRef.current.setMessages;\n"
        "      if (setMsgs && _shouldOverwrite2) {"
    )
    content, ok = _replace(content, old_refresh, new_refresh)
    if ok:
        print("  + refreshMessages guard honors _catchupDone")
        changed = True
    else:
        print("  INFO: refreshMessages guard pattern not found (may already be v3)")

    # (1b) Type the sendMessage wrapper param using chat.sendMessage's type.
    old_wrapper = "  const _sendMessageWrapped = useCallback((msg) => {"
    new_wrapper = (
        "  // === PATCH: catch-up-v3 ===\n"
        "  type _ChatSendArgs = Parameters<typeof chat.sendMessage>[0];\n"
        "  const _sendMessageWrapped = useCallback((msg: _ChatSendArgs) => {"
    )
    content, ok = _replace(content, old_wrapper, new_wrapper)
    if ok:
        print("  + Typed _sendMessageWrapped msg parameter")
        changed = True
    else:
        print("  INFO: wrapper pattern not found (may already be v3)")

    # (1c) Strengthen the remaining v1 count-based hydration guards
    # (_shouldSetB 10-space, _shouldSetC 14-space x2, _shouldSet 6-space).
    guard_rules = [
        (
            "          const _shouldSetB = uiMsgs.length > _currentMsgsB.length || _currentMsgsB.length === 0;\n"
            "          if (_shouldSetB) {",
            "          // === PATCH: catch-up-v3 ===\n"
            "          const _shouldSetB = _catchupDone\n"
            "            ? false\n"
            "            : (uiMsgs.length > _currentMsgsB.length || _currentMsgsB.length === 0);\n"
            "          if (_shouldSetB) {",
        ),
        (
            "              const _shouldSetC = uiMsgs.length > _currentMsgsC.length || _currentMsgsC.length === 0;\n"
            "              if (_shouldSetC) {",
            "              // === PATCH: catch-up-v3 ===\n"
            "              const _shouldSetC = _catchupDone\n"
            "                ? false\n"
            "                : (uiMsgs.length > _currentMsgsC.length || _currentMsgsC.length === 0);\n"
            "              if (_shouldSetC) {",
        ),
        (
            "      const _shouldSet = uiMsgs.length > _currentMsgs.length || _currentMsgs.length === 0;\n"
            "      if (_shouldSet) {",
            "      // === PATCH: catch-up-v3 ===\n"
            "      const _shouldSet = _catchupDone\n"
            "        ? false\n"
            "        : (uiMsgs.length > _currentMsgs.length || _currentMsgs.length === 0);\n"
            "      if (_shouldSet) {",
        ),
    ]
    for old_g, new_g in guard_rules:
        n = content.count(old_g)
        if n:
            content = content.replace(old_g, new_g)
            print(f"  + _catchupDone wired into hydration guard ({n} site(s))")
            changed = True

    # (1d) Extra safety: guard any remaining bare setMessages path in the
    # event-stream consume / visibility handlers that v1 did not touch.
    old_consume = (
        "              if (uiMsgs.length > 0) {\n"
        "                chat.setMessages(uiMsgs);\n"
        "                saveMessages(sessionId, uiMsgs);\n"
        "              }"
    )
    new_consume = (
        "              // === PATCH: catch-up-v3 ===\n"
        "              const _c3a = (chatActionsRef.current.messages || []).filter(\n"
        "                (m) => !(m.parts?.[0]?.type === 'text' && typeof m.parts?.[0]?.text === 'string' && m.parts?.[0].text.trimStart().startsWith('[SYSTEM:'))\n"
        "              );\n"
        "              if (uiMsgs.length > 0 && (!_catchupDone || uiMsgs.length > _c3a.length)) {\n"
        "                chat.setMessages(uiMsgs);\n"
        "                saveMessages(sessionId, uiMsgs);\n"
        "              }"
    )
    count = 0
    while old_consume in content:
        content, ok = _replace(content, old_consume, new_consume)
        if ok:
            count += 1
            changed = True
        else:
            break
    if count:
        print(f"  + Guarded {count} event-stream/visibility setMessages paths")

    return content, changed


def patch_app_layout(content, _path):
    """Share button (public ?share= URL) + read-only ShareView mode."""
    changed = False
    if MARKER3 in content:
        print("  SKIP: already patched")
        return content, False

    # (2a) Import ShareView (new component in the same folder).
    if "import ShareView" not in content:
        anchor = "import ShareIcon from '@mui/icons-material/Share';"
        if anchor in content:
            content = content.replace(
                anchor,
                anchor + "\nimport ShareView from '@/components/ShareView';\n" + MARKER3,
                1,
            )
            changed = True
            print("  + Imported ShareView")
        else:
            print("  INFO: ShareIcon import not found for ShareView import")

    # (2b) Share button before theme toggle. Builds a PUBLIC share URL
    # (?share=<sessionId>) instead of the owner-only ?chat= URL.
    old_theme_btn = (
        "            <IconButton\n"
        "              onClick={toggleTheme}\n"
        "              size=\"small\"\n"
        "              sx={{\n"
        "                color: 'text.secondary',\n"
        "                '&:hover': { color: 'primary.main' },\n"
        "              }}\n"
        "            >\n"
        "              {themeMode === 'dark' ? <LightModeOutlinedIcon fontSize=\"small\" /> : <DarkModeOutlinedIcon fontSize=\"small\" />}\n"
        "            </IconButton>"
    )
    new_theme_btn = (
        "            <IconButton\n"
        "              onClick={toggleTheme}\n"
        "              size=\"small\"\n"
        "              sx={{\n"
        "                color: 'text.secondary',\n"
        "                '&:hover': { color: 'primary.main' },\n"
        "              }}\n"
        "            >\n"
        "              {themeMode === 'dark' ? <LightModeOutlinedIcon fontSize=\"small\" /> : <DarkModeOutlinedIcon fontSize=\"small\" />}\n"
        "            </IconButton>\n"
        "\n"
        "            {activeSessionId && (\n"
        "              <Tooltip title=\"Copy public share link\">\n"
        "                <IconButton\n"
        "                  size=\"small\"\n"
        "                  onClick={(e: React.MouseEvent) => {\n"
        "                    e.stopPropagation();\n"
        "                    const _p = new URLSearchParams(window.location.search);\n"
        "                    const _sid = _p.get('chat') || activeSessionId || '';\n"
        "                    const _sp = new URLSearchParams();\n"
        "                    if (_sid) _sp.set('share', _sid);\n"
        "                    const _qs = _sp.toString();\n"
        "                    const url = `${window.location.origin}${window.location.pathname}${_qs ? `?${_qs}` : ''}`;\n"
        "                    if (navigator.clipboard?.writeText) {\n"
        "                      navigator.clipboard.writeText(url).catch(() => {\n"
        "                        prompt('Copy this link:', url);\n"
        "                      });\n"
        "                    } else {\n"
        "                      prompt('Copy this link:', url);\n"
        "                    }\n"
        "                  }}\n"
        "                  sx={{\n"
        "                    color: 'text.secondary',\n"
        "                    '&:hover': { color: 'primary.main' },\n"
        "                  }}\n"
        "                >\n"
        "                  <ShareIcon fontSize=\"small\" />\n"
        "                </IconButton>\n"
        "              </Tooltip>\n"
        "            )}"
    )
    content, ok = _replace(content, old_theme_btn, new_theme_btn)
    if ok:
        changed = True
        print("  + Added Share button before theme toggle (public ?share= URL)")
    else:
        print("  INFO: theme toggle pattern not found (may already be v3)")

    # Ensure Tooltip is imported (used by the Share button).
    if "Tooltip," not in content and "import Tooltip" not in content:
        if "import {\n  Avatar," in content:
            content = content.replace(
                "import {\n  Avatar,",
                "import {\n  Tooltip,\n  Avatar,",
                1,
            )
            changed = True
            print("  + Added Tooltip import")
        elif "Avatar," in content:
            content = content.replace("  Avatar,", "  Tooltip,\n  Avatar,", 1)
            changed = True
            print("  + Added Tooltip import (fallback)")
        else:
            print("  INFO: could not add Tooltip import (Avatar import not found)")

    # (2c) Read-only share mode: if ?share=<id> is present, render ShareView.
    # Must come AFTER all hooks (it is β€” right before the WelcomeScreen return).
    old_welcome = "  // -- Welcome screen: no sessions at all ---------------------------------\n"
    new_welcome = (
        "  // === PATCH: catch-up-v3 ===\n"
        "  // Public read-only share mode (?share=<sessionId>). Works without\n"
        "  // login for anyone who has the link; data comes from the public\n"
        "  // sessions dataset via GET /api/public/session/{id}.\n"
        "  const _shareParam = new URLSearchParams(window.location.search).get('share');\n"
        "  if (_shareParam) {\n"
        "    return <ShareView sessionId={_shareParam} />;\n"
        "  }\n"
        "\n"
        "  // -- Welcome screen: no sessions at all ---------------------------------\n"
    )
    content, ok = _replace(content, old_welcome, new_welcome)
    if ok:
        changed = True
        print("  + ?share= renders read-only ShareView")
    else:
        print("  INFO: welcome-screen anchor not found for share mode")

    return content, changed


def patch_share_view(path):
    """Create the read-only ShareView component (new file)."""
    if os.path.exists(path):
        print("  SKIP: ShareView.tsx already exists")
        return
    content = (
        "/**\n"
        " * Read-only public session viewer (no auth required).\n"
        " *\n"
        " * Rendered by AppLayout when the URL contains ?share=<sessionId>.\n"
        " * Fetches sanitized conversation rows from the public endpoint\n"
        " * GET /api/public/session/{id}, which reads the permanent public\n"
        " * sessions dataset. Anyone with the link can view the transcript.\n"
        " */\n"
        "// === PATCH: catch-up-v3 ===\n"
        "import { useEffect, useState } from 'react';\n"
        "import {\n"
        "  Alert,\n"
        "  Box,\n"
        "  Button,\n"
        "  CircularProgress,\n"
        "  Paper,\n"
        "  Typography,\n"
        "} from '@mui/material';\n"
        "\n"
        "interface ShareRow {\n"
        "  role: string;\n"
        "  content: string;\n"
        "  timestamp?: string;\n"
        "}\n"
        "\n"
        "interface ShareViewProps {\n"
        "  sessionId: string;\n"
        "}\n"
        "\n"
        "export default function ShareView({ sessionId }: ShareViewProps) {\n"
        "  const [rows, setRows] = useState<ShareRow[]>([]);\n"
        "  const [loading, setLoading] = useState(true);\n"
        "  const [error, setError] = useState<string | null>(null);\n"
        "\n"
        "  useEffect(() => {\n"
        "    let cancelled = false;\n"
        "    (async () => {\n"
        "      try {\n"
        "        const res = await fetch(\n"
        "          `/api/public/session/${encodeURIComponent(sessionId)}`,\n"
        "          { credentials: 'include' },\n"
        "        );\n"
        "        if (res.status === 404) {\n"
        "          if (!cancelled) setError('Session not found.');\n"
        "        } else if (!res.ok) {\n"
        "          if (!cancelled) setError('Failed to load this session.');\n"
        "        } else {\n"
        "          const data = await res.json();\n"
        "          if (!cancelled && Array.isArray(data)) setRows(data);\n"
        "        }\n"
        "      } catch {\n"
        "        if (!cancelled) setError('Network error while loading this session.');\n"
        "      } finally {\n"
        "        if (!cancelled) setLoading(false);\n"
        "      }\n"
        "    })();\n"
        "    return () => {\n"
        "      cancelled = true;\n"
        "    };\n"
        "  }, [sessionId]);\n"
        "\n"
        "  return (\n"
        "    <Box\n"
        "      sx={{\n"
        "        minHeight: '100vh',\n"
        "        bgcolor: 'var(--body-gradient)' as string,\n"
        "        py: 4,\n"
        "        px: { xs: 2, md: 6 },\n"
        "      }}\n"
        "    >\n"
        "      <Box sx={{ maxWidth: 860, mx: 'auto' }}>\n"
        "        <Box sx={{ display: 'flex', alignItems: 'center', gap: 1.5, mb: 2 }}>\n"
        "          <Typography variant=\"h6\" sx={{ fontSize: '1rem', fontWeight: 700 }}>\n"
        "            Shared session\n"
        "          </Typography>\n"
        "          <Button component=\"a\" href=\"/\" size=\"small\" sx={{ textTransform: 'none', ml: 'auto' }}>\n"
        "            Open ML Intern\n"
        "          </Button>\n"
        "        </Box>\n"
        "\n"
        "        {loading && <CircularProgress size={20} />}\n"
        "        {error && (\n"
        "          <Alert severity=\"warning\" variant=\"outlined\">\n"
        "            {error}\n"
        "          </Alert>\n"
        "        )}\n"
        "        {!loading && !error && rows.length === 0 && (\n"
        "          <Typography color=\"text.secondary\" variant=\"body2\">\n"
        "            No messages in this session.\n"
        "          </Typography>\n"
        "        )}\n"
        "        {rows.map((row, i) => (\n"
        "          <Paper\n"
        "            key={i}\n"
        "            sx={{\n"
        "              mb: 1,\n"
        "              p: 1.5,\n"
        "              bgcolor: row.role === 'user' ? 'var(--hover-bg)' : 'var(--panel)',\n"
        "              borderRadius: 2,\n"
        "            }}\n"
        "          >\n"
        "            <Typography\n"
        "              variant=\"caption\"\n"
        "              sx={{ color: 'var(--muted-text)', fontWeight: 700, textTransform: 'uppercase' }}\n"
        "            >\n"
        "              {row.role}\n"
        "            </Typography>\n"
        "            <Typography\n"
        "              variant=\"body2\"\n"
        "              sx={{ whiteSpace: 'pre-wrap', mt: 0.5, wordBreak: 'break-word' }}\n"
        "            >\n"
        "              {row.content}\n"
        "            </Typography>\n"
        "          </Paper>\n"
        "        ))}\n"
        "      </Box>\n"
        "    </Box>\n"
        "  );\n"
        "}\n"
    )
    _write(path, content)
    print("  + Created ShareView.tsx")


def patch_session_sidebar(content, _path):
    changed = False
    if MARKER3 in content:
        return content, False
    return content, changed


def patch_expired_banner(content, _path):
    changed = False
    if MARKER3 in content:
        return content, False
    return content, changed


def patch_welcome_screen(content, _path):
    changed = False
    if MARKER3 in content:
        return content, False
    return content, changed


# ─── Backend routes/agent.py: public share endpoint ───────────────────────

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


def patch_routes_agent(content):
    """Add public GET /api/public/session/{id} (no auth)."""
    changed = False
    if "public/session/" in content:
        print("  SKIP: public endpoint already present")
        return content, False

    endpoint = '''

@router.get("/public/session/{session_id}")
async def public_session_transcript(session_id: str) -> list[dict]:
    """Public read-only transcript for a session (no auth required).

    Reads sanitized conversation rows from the permanent public sessions
    dataset (bep40/ml-intern-sessions). Returns only user/assistant/tool
    messages β€” never internal event/system rows. Useful for sharing a
    session transcript with anyone via a link.
    """
    from backend.session_logger import load_session_history

    rows = load_session_history(session_id)
    valid_roles = {"user", "assistant", "tool"}
    messages = [r for r in rows if r.get("role") in valid_roles]
    # Cap payload size: last 300 messages, each content truncated to 20k chars.
    truncated = []
    for r in messages[-300:]:
        content = r.get("content") or ""
        if isinstance(content, str) and len(content) > 20000:
            content = content[:20000] + "\\n...[truncated]"
        truncated.append(
            {
                "role": r.get("role", "assistant"),
                "content": content,
                "timestamp": r.get("timestamp"),
            }
        )
    return truncated
'''
    content = content.rstrip() + "\n" + endpoint
    print("  + Added public /api/public/session/{id} endpoint")
    changed = True
    return content, changed


def patch_session_logger(content):
    """Fix v2's broken insertion that caused a SyntaxError.

    v2 inserted the filter between `rows.sort(...)` and `return rows` at the
    WRONG indentation (4 spaces instead of 8), producing:
        expected 'except' or 'finally' block
    The whole patch was skipped, so event rows were never filtered.

    v3 inserts the filter at the correct 8-space indent inside the try block.
    """
    changed = False
    if MARKER3_PY in content:
        print("  SKIP: already patched")
        return content, False

    # Remove the broken v2 insertion if it ever made it in.
    broken = (
        "    # === 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"
    )
    if broken in content:
        content = content.replace(broken, "", 1)
        print("  + Removed broken v2 insertion")
        changed = True

    if "def _filter_valid_roles" not in content:
        old_sort = (
            "        rows.sort(key=lambda r: (r.get(\"seq\", 0), r.get(\"timestamp\", \"\")))\n"
            "        return rows\n"
        )
        new_sort = (
            "        rows.sort(key=lambda r: (r.get(\"seq\", 0), r.get(\"timestamp\", \"\")))\n"
            "        # === PATCH: catch-up-v3 ===\n"
            "        # Summarization and Message.model_validate() cannot parse\n"
            "        # internal tracing rows (role='event'); filter them here so\n"
            "        # every consumer gets clean conversation data.\n"
            "        valid_roles = {\"user\", \"assistant\", \"tool\"}\n"
            "        return [r for r in rows if r.get(\"role\") in valid_roles]\n"
        )
        content, ok = _replace(content, old_sort, new_sort)
        if ok:
            print("  + Filter event rows at source (load_session_history)")
            changed = True
        else:
            # Try the already-v2-patched variant (sort only, return separate)
            old_sort2 = (
                "        rows.sort(key=lambda r: (r.get(\"seq\", 0), r.get(\"timestamp\", \"\")))\n"
            )
            new_sort2 = (
                "        rows.sort(key=lambda r: (r.get(\"seq\", 0), r.get(\"timestamp\", \"\")))\n"
                "        # === PATCH: catch-up-v3 ===\n"
                "        valid_roles = {\"user\", \"assistant\", \"tool\"}\n"
                "        rows = [r for r in rows if r.get(\"role\") in valid_roles]\n"
            )
            content, ok = _replace(content, old_sort2, new_sort2)
            if ok:
                print("  + Filter event rows at source (sort-then-filter variant)")
                changed = True

    return content, changed


# ─── Execute ────────────────────────────────────────────────────────────────

print("=== v3: Frontend ===")
patch_frontend_file(
    "hooks/useAgentChat.ts",
    patch_use_agent_chat,
    "wire _catchupDone + type wrapper",
)
patch_frontend_file(
    "components/Layout/AppLayout.tsx",
    patch_app_layout,
    "share button + share mode",
)
patch_share_view(os.path.join(FRONTEND_DIR, "components/ShareView.tsx"))
patch_frontend_file(
    "components/SessionSidebar/SessionSidebar.tsx",
    patch_session_sidebar,
    "idempotency marker",
)
patch_frontend_file(
    "components/Chat/ExpiredBanner.tsx",
    patch_expired_banner,
    "idempotency marker",
)
patch_frontend_file(
    "components/WelcomeScreen/WelcomeScreen.tsx",
    patch_welcome_screen,
    "idempotency marker",
)

print("\n=== v3: Backend ===")
if os.path.exists(BACKEND_ROUTES_AGENT):
    content = _read(BACKEND_ROUTES_AGENT)
    content, changed = patch_routes_agent(content)
    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 β€” no changes needed")
else:
    print("SKIP: routes/agent.py not found")

if os.path.exists(BACKEND_SESSION_LOGGER):
    content = _read(BACKEND_SESSION_LOGGER)
    content, changed = patch_session_logger(content)
    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 β€” no changes needed")
else:
    print("SKIP: session_logger.py not found")

print("\n=== v3 done ===")