Spaces:
Running
Running
Download patch_catch_up_fix_v3.py from bep40/ml-intern: direct link, hf CLI and curl.
- Browser
- Download file 27.5 kB
-
https://huggingface.co/spaces/bep40/ml-intern/resolve/main/patch_catch_up_fix_v3.py
- Command line
-
hf download hf://spaces/bep40/ml-intern/patch_catch_up_fix_v3.py
-
curl -L -o patch_catch_up_fix_v3.py https://huggingface.co/spaces/bep40/ml-intern/resolve/main/patch_catch_up_fix_v3.py
27.5 kB
| #!/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 ===") | |