#!/usr/bin/env python3 """ Frontend-only half of patch_fix_approval_buttons.py. Runs in the node:20-alpine frontend-builder stage where /source/frontend/src/hooks/useAgentChat.ts exists. The backend half (session.py / session_manager.py / routes/agent.py) runs in the python:3.12 stage via patch_fix_approval_buttons.py. Idempotent — safe to run in either order. """ import os MARKER_TS = "// === PATCH: fix-approval-buttons ===" FSRC = "/source/frontend/src/hooks/useAgentChat.ts" def _read(path): with open(path, 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_all(content, old, new): if old in content: return content.replace(old, new), True return content, False if not os.path.exists(FSRC): print(f"SKIP: {FSRC} not found (not in this build stage)") raise SystemExit(0) content = _read(FSRC) if MARKER_TS in content: print("SKIP: useAgentChat.ts already patched") raise SystemExit(0) changed = False # 4a. Helper: does this message list contain an approval-requested part? old_waiting = "function waitingApprovalStatus(items: PendingApprovalItem[] | undefined) {" helper = ( "// === PATCH: fix-approval-buttons ===\n" "/** True if any tool part is currently awaiting approval. */\n" "function _hasPendingApproval(msgs: UIMessage[]): boolean {\n" " return msgs.some((m) =>\n" " m.parts.some(\n" " (p) => p.type === 'dynamic-tool' && p.state === 'approval-requested',\n" " ),\n" " );\n" "}\n" "// === PATCH: fix-approval-buttons ===\n" "\n" "function waitingApprovalStatus(items: PendingApprovalItem[] | undefined) {" ) content, ok = _replace_all(content, old_waiting, helper) if ok: print(" + added _hasPendingApproval helper") changed = True # 4b. Replace `_catchupDone ? false :` in EVERY hydration guard with # `_catchupDone ? _hasPendingApproval(uiMsgs) :` so approval rounds # always hydrate, including the approval_required SSE branch. variants = [ ( " const _shouldOverwrite = _catchupDone\n" " ? false\n" " : (uiMsgs.length > _currentNonSystem.length || _currentNonSystem.length === 0);\n", " const _shouldOverwrite = _catchupDone\n" " ? _hasPendingApproval(uiMsgs)\n" " : (uiMsgs.length > _currentNonSystem.length || _currentNonSystem.length === 0);\n", ), ( " const _shouldOverwrite2 = _catchupDone\n" " ? false\n" " : (uiMsgs.length > _currentNonSystem2.length || _currentNonSystem2.length === 0);\n", " const _shouldOverwrite2 = _catchupDone\n" " ? _hasPendingApproval(uiMsgs)\n" " : (uiMsgs.length > _currentNonSystem2.length || _currentNonSystem2.length === 0);\n", ), ( " const _shouldSetB = _catchupDone\n" " ? false\n" " : (uiMsgs.length > _currentMsgsB.length || _currentMsgsB.length === 0);\n", " const _shouldSetB = _catchupDone\n" " ? _hasPendingApproval(uiMsgs)\n" " : (uiMsgs.length > _currentMsgsB.length || _currentMsgsB.length === 0);\n", ), ( " const _shouldSetC = _catchupDone\n" " ? false\n" " : (uiMsgs.length > _currentMsgsC.length || _currentMsgsC.length === 0);\n", " const _shouldSetC = _catchupDone\n" " ? _hasPendingApproval(uiMsgs)\n" " : (uiMsgs.length > _currentMsgsC.length || _currentMsgsC.length === 0);\n", ), ( " const _shouldSet = _catchupDone\n" " ? false\n" " : (uiMsgs.length > _currentMsgs.length || _currentMsgs.length === 0);\n", " const _shouldSet = _catchupDone\n" " ? _hasPendingApproval(uiMsgs)\n" " : (uiMsgs.length > _currentMsgs.length || _currentMsgs.length === 0);\n", ), ] for old_g, new_g in variants: content, ok = _replace_all(content, old_g, new_g) if ok: print(" + hydration guard -> _hasPendingApproval") changed = True # 4c. Any remaining generic `_catchupDone ? false` (loose form). old_loose = "_catchupDone\n ? false\n" while old_loose in content: content = content.replace( old_loose, "_catchupDone\n ? _hasPendingApproval(uiMsgs)\n", 1, ) changed = True print(" + loose _catchupDone ? false -> _hasPendingApproval") # 4d. event-stream consume guard (_c3a) -- add approval bypass. old3 = ( " if (uiMsgs.length > 0 && (!_catchupDone || uiMsgs.length > _c3a.length)) {\n" ) new3 = ( " if (uiMsgs.length > 0 && (!_catchupDone || uiMsgs.length > _c3a.length || _hasPendingApproval(uiMsgs))) {\n" ) content, ok = _replace_all(content, old3, new3) if ok: print(" + event-stream consume guard honors pending approvals") changed = True else: print(" INFO: _c3a consume guard not found (may differ in fork)") if changed: _write(FSRC, content) print("OK: useAgentChat.ts patched (frontend approval bypass)") else: print("SKIP: useAgentChat.ts - no changes needed")