Spaces:
Sleeping
Sleeping
Fix silent auto-approve failure: anchor correctly, add runtime opt-out, drop dead mid-task block
Browse files- patch_agent_backend.py +56 -72
patch_agent_backend.py
CHANGED
|
@@ -1,78 +1,44 @@
|
|
| 1 |
-
"""Patch:
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
-
import os
|
| 7 |
import ast
|
| 8 |
|
| 9 |
-
AGENT_LOOP = "/app/agent/core/agent_loop.py"
|
| 10 |
-
AGENT_FILE = "/app/backend/routes/agent.py"
|
| 11 |
SESSION_FILE = "/app/agent/core/session.py"
|
| 12 |
|
| 13 |
-
# ===== PATCH 1:
|
| 14 |
-
|
| 15 |
-
c = f.read()
|
| 16 |
-
|
| 17 |
-
# Add helper function before class Handlers
|
| 18 |
-
if "_unfinished_plan" not in c:
|
| 19 |
-
fn = (
|
| 20 |
-
"\n\ndef _unfinished_plan(s):\n"
|
| 21 |
-
" p = getattr(s, 'current_plan', None) or []\n"
|
| 22 |
-
" return [it for it in p if it.get('status') in ('pending', 'in_progress')]\n\n"
|
| 23 |
-
)
|
| 24 |
-
c = c.replace("class Handlers:", fn + "class Handlers:")
|
| 25 |
-
print("OK: Added _unfinished_plan()")
|
| 26 |
-
|
| 27 |
-
# Add detection: if model produced no tool calls but content still exists,
|
| 28 |
-
# and plan items are still pending/in_progress, send session_update event
|
| 29 |
-
if "ac_plan" not in c:
|
| 30 |
-
check = (
|
| 31 |
-
"\n # Auto-continue: detect model stopped mid-task\n"
|
| 32 |
-
" if not llm_result.tool_calls_acc and llm_result.content:\n"
|
| 33 |
-
" unfinished = _unfinished_plan(session)\n"
|
| 34 |
-
" if unfinished:\n"
|
| 35 |
-
" await session.send_event(Event(event_type='session_update', data={\n"
|
| 36 |
-
" 'ac_plan': unfinished,\n"
|
| 37 |
-
" }))\n"
|
| 38 |
-
)
|
| 39 |
-
c = c.replace(" # -- End of turn --", check + " # -- End of turn --")
|
| 40 |
-
print("OK: Added mid-task detection")
|
| 41 |
-
|
| 42 |
-
try:
|
| 43 |
-
ast.parse(c)
|
| 44 |
-
with open(AGENT_LOOP, "w") as f:
|
| 45 |
-
f.write(c)
|
| 46 |
-
print("OK: agent_loop.py patched")
|
| 47 |
-
except SyntaxError as e:
|
| 48 |
-
print(f"FAIL: agent_loop.py syntax: {e}")
|
| 49 |
-
|
| 50 |
-
# ===== PATCH 2: Enable auto_approval by default for ALL sessions =====
|
| 51 |
-
# So free models don't block on user approval.
|
| 52 |
-
#
|
| 53 |
-
# HISTORY OF THIS PATCH (it previously never applied at all):
|
| 54 |
-
# v1 inserted `self.auto_approval = {"enabled": True, ...}` right after the
|
| 55 |
-
# first indented line following the FIRST "def __init__" in the file.
|
| 56 |
-
# Session.__init__ has a multi-line signature ending in `):`, so the insert
|
| 57 |
-
# landed INSIDE the parameter list -> ast.parse raised SyntaxError -> the
|
| 58 |
-
# script caught it and skipped the write. Auto-approve was silently off.
|
| 59 |
-
# It also used the wrong API: nothing in the codebase reads a
|
| 60 |
-
# `self.auto_approval` dict (see agent/core/agent_loop.py::
|
| 61 |
-
# _session_auto_approval_enabled, agent/core/yolo_budget.py,
|
| 62 |
-
# backend/session_manager.py), which all read `auto_approval_enabled`.
|
| 63 |
-
#
|
| 64 |
-
# v2 anchored on `self.auto_approval_enabled: bool = False`, which fixed the
|
| 65 |
-
# SyntaxError but introduced an ORDERING BUG: it called
|
| 66 |
-
# set_auto_approval_policy() at that point, but the very next upstream line
|
| 67 |
-
# re-assigns `self.auto_approval_cost_cap_usd = None`, silently undoing half
|
| 68 |
-
# of the call. Worse, set_auto_approval_policy() ran BEFORE
|
| 69 |
-
# auto_approval_cost_cap_usd / auto_approval_estimated_spend_usd were
|
| 70 |
-
# initialised.
|
| 71 |
-
#
|
| 72 |
-
# v3 (this version) anchors on the LAST line of the auto-approval block in
|
| 73 |
-
# __init__ -- `self.auto_approval_estimated_spend_usd: float = 0.0` -- so the
|
| 74 |
-
# policy call happens after every related attribute exists and nothing
|
| 75 |
-
# overwrites it afterwards.
|
| 76 |
MARKER = "# === PATCH: auto-approve by default ==="
|
| 77 |
|
| 78 |
with open(SESSION_FILE) as f:
|
|
@@ -89,15 +55,33 @@ else:
|
|
| 89 |
" # Flip the real flag that _session_auto_approval_enabled() reads,\n"
|
| 90 |
" # using the public setter. Placed AFTER the attribute initialisers\n"
|
| 91 |
" # above so nothing overwrites it.\n"
|
| 92 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
)
|
| 94 |
|
| 95 |
-
if
|
| 96 |
-
print("FAIL:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
raise SystemExit(1)
|
| 98 |
|
| 99 |
c = c.replace(anchor, anchor + auto_patch, 1)
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
with open(SESSION_FILE, "w") as f:
|
| 102 |
f.write(c)
|
| 103 |
print("OK: session.py patched (auto_approval_enabled now defaults to True)")
|
|
|
|
| 1 |
+
"""Patch: enable auto-approve by default for free/OpenRouter models.
|
| 2 |
|
| 3 |
+
HISTORY OF THIS PATCH — it silently never applied until v3:
|
| 4 |
+
|
| 5 |
+
v1 injected `self.auto_approval = {"enabled": True, ...}` right after the
|
| 6 |
+
first indented line following the FIRST "def __init__" in the file.
|
| 7 |
+
Session.__init__ has a multi-line signature ending in `):`, so the insert
|
| 8 |
+
landed INSIDE the parameter list -> ast.parse raised SyntaxError -> the
|
| 9 |
+
script caught it and skipped the write. Auto-approve was silently off.
|
| 10 |
+
It also used the wrong API: nothing reads a `self.auto_approval` dict
|
| 11 |
+
(agent_loop.py::_session_auto_approval_enabled, yolo_budget.py and
|
| 12 |
+
backend/session_manager.py all read the flat `auto_approval_enabled`).
|
| 13 |
+
|
| 14 |
+
v2 anchored on `self.auto_approval_enabled: bool = False`, which fixed the
|
| 15 |
+
SyntaxError but introduced an ORDERING BUG: it called
|
| 16 |
+
set_auto_approval_policy() there, yet the next upstream line re-assigns
|
| 17 |
+
`self.auto_approval_cost_cap_usd = None`, silently undoing half the call
|
| 18 |
+
— and the call ran BEFORE the other two attributes were initialised.
|
| 19 |
+
|
| 20 |
+
v3 (this version) anchors on the LAST line of the auto-approval block in
|
| 21 |
+
__init__ -- `self.auto_approval_estimated_spend_usd: float = 0.0` -- so the
|
| 22 |
+
policy call happens after every related attribute exists and nothing
|
| 23 |
+
overwrites it afterwards. Adds an ML_INTERN_AUTO_APPROVE=0 runtime opt-out.
|
| 24 |
+
|
| 25 |
+
v3 also added a "mid-task detection" block anchored on `# -- End of turn --`.
|
| 26 |
+
That anchor does not exist in current upstream, so `str.replace()` was a no-op
|
| 27 |
+
while the script still printed "OK: Added mid-task detection" — a green build
|
| 28 |
+
that changed nothing. Current upstream already implements this natively
|
| 29 |
+
(`_unfinished_plan_items` + `_NO_TOOL_INCOMPLETE_PLAN_RETRY_LIMIT` at
|
| 30 |
+
agent_loop.py:1513-1558), which auto-retries a text-only response when the plan
|
| 31 |
+
still has unfinished items. Re-adding it would double the retry budget against
|
| 32 |
+
the same condition, so it is deliberately NOT patched here; the dead code is
|
| 33 |
+
removed rather than left to keep lying about applying.
|
| 34 |
"""
|
| 35 |
|
|
|
|
| 36 |
import ast
|
| 37 |
|
|
|
|
|
|
|
| 38 |
SESSION_FILE = "/app/agent/core/session.py"
|
| 39 |
|
| 40 |
+
# ===== PATCH 1: enable auto-approve by default =====
|
| 41 |
+
# So free models don't block on a manual approval prompt.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
MARKER = "# === PATCH: auto-approve by default ==="
|
| 43 |
|
| 44 |
with open(SESSION_FILE) as f:
|
|
|
|
| 55 |
" # Flip the real flag that _session_auto_approval_enabled() reads,\n"
|
| 56 |
" # using the public setter. Placed AFTER the attribute initialisers\n"
|
| 57 |
" # above so nothing overwrites it.\n"
|
| 58 |
+
" # Runtime opt-out: set ML_INTERN_AUTO_APPROVE=0 (no rebuild needed).\n"
|
| 59 |
+
" if os.environ.get(\n"
|
| 60 |
+
' "ML_INTERN_AUTO_APPROVE", "1"\n'
|
| 61 |
+
' ).strip().lower() not in ("0", "false", "no", "off"):\n'
|
| 62 |
+
" self.set_auto_approval_policy(enabled=True, cost_cap_usd=None)\n"
|
| 63 |
)
|
| 64 |
|
| 65 |
+
if "import os" not in c.split("class Session:")[0]:
|
| 66 |
+
print("FAIL: session.py has no top-level 'import os'; cannot patch safely")
|
| 67 |
+
raise SystemExit(1)
|
| 68 |
+
|
| 69 |
+
if c.count(anchor) != 1:
|
| 70 |
+
print(
|
| 71 |
+
"FAIL: expected exactly 1 auto-approval anchor in session.py, "
|
| 72 |
+
f"found {c.count(anchor)}. Upstream layout changed — refusing to guess."
|
| 73 |
+
)
|
| 74 |
raise SystemExit(1)
|
| 75 |
|
| 76 |
c = c.replace(anchor, anchor + auto_patch, 1)
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
ast.parse(c) # fail loudly rather than silently skipping the write
|
| 80 |
+
except SyntaxError as e:
|
| 81 |
+
print(f"FAIL: patched session.py has a syntax error at line {e.lineno}: {e.msg}")
|
| 82 |
+
print(" File left untouched.")
|
| 83 |
+
raise SystemExit(1)
|
| 84 |
+
|
| 85 |
with open(SESSION_FILE, "w") as f:
|
| 86 |
f.write(c)
|
| 87 |
print("OK: session.py patched (auto_approval_enabled now defaults to True)")
|