"""Patch: agent_loop.py task detection + auto-approve for free models.""" import os import re import ast AGENT_LOOP = "/app/agent/core/agent_loop.py" AGENT_FILE = "/app/backend/routes/agent.py" SESSION_FILE = "/app/agent/core/session.py" # ===== PATCH 1: agent_loop.py - detect model stopped mid-task ===== with open(AGENT_LOOP) as f: c = f.read() # Add helper function before class Handlers if "_unfinished_plan" not in c: fn = ( "\n\ndef _unfinished_plan(s):\n" " p = getattr(s, 'current_plan', None) or []\n" " return [it for it in p if it.get('status') in ('pending', 'in_progress')]\n\n" ) c = c.replace("class Handlers:", fn + "class Handlers:") print("OK: Added _unfinished_plan()") # Add detection: if model produced no tool calls but content still exists, # and plan items are still pending/in_progress, send session_update event if "ac_plan" not in c: check = ( "\n # Auto-continue: detect model stopped mid-task\n" " if not llm_result.tool_calls_acc and llm_result.content:\n" " unfinished = _unfinished_plan(session)\n" " if unfinished:\n" " await session.send_event(Event(event_type='session_update', data={\n" " 'ac_plan': unfinished,\n" " }))\n" ) c = c.replace(" # -- End of turn --", check + " # -- End of turn --") print("OK: Added mid-task detection") try: ast.parse(c) with open(AGENT_LOOP, "w") as f: f.write(c) print("OK: agent_loop.py patched") except SyntaxError as e: print(f"FAIL: agent_loop.py syntax: {e}") # ===== PATCH 2: Enable auto_approval for ALL sessions by default ===== # So free models don't block on user approval. with open(SESSION_FILE) as f: c = f.read() if "AUTO_APPROVE" not in c and "self.auto_approval" not in c: auto_patch = ( "\n # === PATCH: Auto-approve for free models ===\n" " self.auto_approval = {\n" ' "enabled": True,\n' ' "cost_cap_usd": None,\n' ' "estimated_spend_usd": 0.0,\n' ' "remaining_usd": None,\n' " }\n" ) # Insert as the FIRST STATEMENT of __init__ body: find the closing # " ):" that terminates the def __init__(...) parameter list and # insert right after it. (Inserting into the parameter list corrupts # the file with invalid syntax.) init_match = re.search(r"def __init__\(.*?\):\n", c, re.DOTALL) if init_match: insert_at = init_match.end() c = c[:insert_at] + auto_patch + c[insert_at:] print("OK: Added auto_approve default") else: print("WARN: could not locate __init__ signature in session.py") try: ast.parse(c) with open(SESSION_FILE, "w") as f: f.write(c) print("OK: session.py patched & syntactically valid") except SyntaxError as e: print(f"FAIL: session.py syntax: {e}") # Don't block on this patch else: print("OK: session.py already has auto_approval") print("DONE: Backend patches applied")