Spaces:
Sleeping
Sleeping
Upload patch_none_guard.py
Browse files- patch_none_guard.py +105 -49
patch_none_guard.py
CHANGED
|
@@ -37,19 +37,30 @@ FIX
|
|
| 37 |
1. Insert an explicit terminal LLMResult immediately AFTER the retry `for` loop in
|
| 38 |
both helpers, so control can never reach the consumer with no result.
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
2. Defensive `getattr(llm_result, "usage", None) or {}` at the dereference site.
|
| 47 |
3. Defensive None check right after the helper call.
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
"""
|
| 54 |
|
| 55 |
import ast
|
|
@@ -64,43 +75,63 @@ if "_PATCH_TERMINAL_GUARD" in c:
|
|
| 64 |
raise SystemExit(0)
|
| 65 |
|
| 66 |
|
| 67 |
-
def guard_block(fn_name: str) -> str:
|
| 68 |
-
"""Terminal guard for one helper, inserted right after its retry `for` loop.
|
| 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 |
tree = ast.parse(c)
|
|
@@ -139,13 +170,34 @@ for fn in targets:
|
|
| 139 |
f"BUILD_ERROR: retry loop in {fn.name} is not bounded by _MAX_LLM_RETRIES"
|
| 140 |
)
|
| 141 |
raise SystemExit(1)
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
# Insert bottom-up so earlier line numbers stay valid.
|
| 145 |
lines = c.splitlines(keepends=True)
|
| 146 |
for at, fn_name, block in sorted(inserts, key=lambda t: t[0], reverse=True):
|
| 147 |
lines.insert(at, block)
|
| 148 |
-
|
|
|
|
| 149 |
|
| 150 |
c = "".join(lines)
|
| 151 |
ast.parse(c)
|
|
@@ -186,7 +238,11 @@ else:
|
|
| 186 |
|
| 187 |
# ---- Defensive usage dereference ----------------------------------------
|
| 188 |
old_usage = 'llm_observed_cost_usd = llm_result.usage.get("cost_usd")'
|
| 189 |
-
new_usage =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
if old_usage in c:
|
| 191 |
c = c.replace(old_usage, new_usage, 1)
|
| 192 |
print("OK: hardened llm_result.usage dereference in run_agent")
|
|
|
|
| 37 |
1. Insert an explicit terminal LLMResult immediately AFTER the retry `for` loop in
|
| 38 |
both helpers, so control can never reach the consumer with no result.
|
| 39 |
|
| 40 |
+
Two details matter, and both were wrong in earlier revisions of this patch:
|
| 41 |
+
|
| 42 |
+
a) The anchor is the AST node (the top-level `For` whose target is
|
| 43 |
+
`_llm_attempt`) rather than a line number or a text snippet. Appending at
|
| 44 |
+
end-of-function, as an earlier revision did, is dead code in
|
| 45 |
+
_call_llm_non_streaming: that position is already past the final `return`.
|
| 46 |
+
|
| 47 |
+
b) In _call_llm_non_streaming the success path leaves the loop with `break`,
|
| 48 |
+
NOT `return`, and it lands on exactly the line where the guard goes. An
|
| 49 |
+
unconditional guard there fires after every SUCCESSFUL call, logging a
|
| 50 |
+
bogus error and discarding the real response. So the non-streaming guard
|
| 51 |
+
must be conditional on `response is None`, which is only true when every
|
| 52 |
+
attempt `continue`d.
|
| 53 |
+
|
| 54 |
+
_call_llm_streaming has no such `break` (all exits are `return`/`raise`),
|
| 55 |
+
so its guard is unconditional.
|
| 56 |
|
| 57 |
2. Defensive `getattr(llm_result, "usage", None) or {}` at the dereference site.
|
| 58 |
3. Defensive None check right after the helper call.
|
| 59 |
|
| 60 |
+
Step (1) fails the build loudly: if the retry loop is missing or renamed, the
|
| 61 |
+
root cause is still live and we must not pretend the patch applied. Steps 2 and 3
|
| 62 |
+
are best-effort text replacements and only warn, so this patch can never be the
|
| 63 |
+
reason the Space fails to build for an unrelated reason.
|
| 64 |
"""
|
| 65 |
|
| 66 |
import ast
|
|
|
|
| 75 |
raise SystemExit(0)
|
| 76 |
|
| 77 |
|
| 78 |
+
def guard_block(fn_name: str, condition: str | None) -> str:
|
| 79 |
+
"""Terminal guard for one helper, inserted right after its retry `for` loop.
|
| 80 |
+
|
| 81 |
+
`condition` is emitted above the guard body, indented one level deeper.
|
| 82 |
+
Pass None for an unconditional guard (only valid when the retry loop cannot
|
| 83 |
+
exit normally other than via return/raise).
|
| 84 |
+
"""
|
| 85 |
+
body = [
|
| 86 |
+
'logger.error(',
|
| 87 |
+
' "%s: exhausted %d attempts without a result",',
|
| 88 |
+
' llm_params.get("model", "unknown"),',
|
| 89 |
+
" _MAX_LLM_RETRIES,",
|
| 90 |
+
")",
|
| 91 |
+
"await session.send_event(",
|
| 92 |
+
" Event(",
|
| 93 |
+
' event_type="tool_log",',
|
| 94 |
+
" data={",
|
| 95 |
+
' "tool": "system",',
|
| 96 |
+
" \"log\": (",
|
| 97 |
+
' "The model provider failed on every retry "',
|
| 98 |
+
' "(no response). Ending this turn - please "',
|
| 99 |
+
' "resend your message."',
|
| 100 |
+
" ),",
|
| 101 |
+
" },",
|
| 102 |
+
" )",
|
| 103 |
+
")",
|
| 104 |
+
"return LLMResult(",
|
| 105 |
+
" content=None,",
|
| 106 |
+
" tool_calls_acc={},",
|
| 107 |
+
" token_count=0,",
|
| 108 |
+
" finish_reason=None,",
|
| 109 |
+
")",
|
| 110 |
+
]
|
| 111 |
+
|
| 112 |
+
comment = [
|
| 113 |
+
" # === PATCH_TERMINAL_GUARD ===",
|
| 114 |
+
f" # {fn_name}: reached only when every retry attempt `continue`d",
|
| 115 |
+
" # without returning or raising, and without breaking out with a",
|
| 116 |
+
" # result. Upstream has nothing between the retry `for` loop and its",
|
| 117 |
+
" # consumer, so an all-continue loop used to run off the end and hand",
|
| 118 |
+
" # back None (streaming) -> AttributeError. In non-streaming, control",
|
| 119 |
+
" # reached `response.choices[0]` with `response` still None ->",
|
| 120 |
+
" # AttributeError on 'choices'. End the turn with an empty result",
|
| 121 |
+
" # instead of crashing the request.",
|
| 122 |
+
]
|
| 123 |
+
|
| 124 |
+
if condition:
|
| 125 |
+
comment.append(
|
| 126 |
+
f" # Guarded by `{condition}` because this helper leaves the retry"
|
| 127 |
+
)
|
| 128 |
+
comment.append(
|
| 129 |
+
" # loop with `break` on success, which lands on this same line."
|
| 130 |
+
)
|
| 131 |
+
body = [" " + b for b in body]
|
| 132 |
+
body.insert(0, f" {condition}")
|
| 133 |
+
|
| 134 |
+
return "\n" + "\n".join(comment + body) + "\n"
|
| 135 |
|
| 136 |
|
| 137 |
tree = ast.parse(c)
|
|
|
|
| 170 |
f"BUILD_ERROR: retry loop in {fn.name} is not bounded by _MAX_LLM_RETRIES"
|
| 171 |
)
|
| 172 |
raise SystemExit(1)
|
| 173 |
+
|
| 174 |
+
# Decide whether the guard must be conditional. If the loop body can leave
|
| 175 |
+
# the loop without returning or raising (i.e. `break`), the fall-through
|
| 176 |
+
# position is also the success position, so guard on the missing result.
|
| 177 |
+
condition = None
|
| 178 |
+
if fn.name == "_call_llm_non_streaming":
|
| 179 |
+
if "response = None" not in loop_src and not any(
|
| 180 |
+
isinstance(n, ast.Assign)
|
| 181 |
+
and any(
|
| 182 |
+
isinstance(t, ast.Name) and t.id == "response" for t in n.targets
|
| 183 |
+
)
|
| 184 |
+
for n in ast.walk(loop)
|
| 185 |
+
):
|
| 186 |
+
print(
|
| 187 |
+
f"BUILD_ERROR: {fn.name} no longer initialises/assigns `response`; "
|
| 188 |
+
"cannot safely place the guard"
|
| 189 |
+
)
|
| 190 |
+
raise SystemExit(1)
|
| 191 |
+
condition = "if response is None:"
|
| 192 |
+
|
| 193 |
+
inserts.append((loop.end_lineno, fn.name, guard_block(fn.name, condition)))
|
| 194 |
|
| 195 |
# Insert bottom-up so earlier line numbers stay valid.
|
| 196 |
lines = c.splitlines(keepends=True)
|
| 197 |
for at, fn_name, block in sorted(inserts, key=lambda t: t[0], reverse=True):
|
| 198 |
lines.insert(at, block)
|
| 199 |
+
cond = "conditional" if "if response is None:" in block else "unconditional"
|
| 200 |
+
print(f"OK: {fn_name}: {cond} guard inserted after retry loop (line {at})")
|
| 201 |
|
| 202 |
c = "".join(lines)
|
| 203 |
ast.parse(c)
|
|
|
|
| 238 |
|
| 239 |
# ---- Defensive usage dereference ----------------------------------------
|
| 240 |
old_usage = 'llm_observed_cost_usd = llm_result.usage.get("cost_usd")'
|
| 241 |
+
new_usage = (
|
| 242 |
+
'llm_observed_cost_usd = (getattr(llm_result, "usage", None) or {}).get(\n'
|
| 243 |
+
' "cost_usd"\n'
|
| 244 |
+
' )'
|
| 245 |
+
)
|
| 246 |
if old_usage in c:
|
| 247 |
c = c.replace(old_usage, new_usage, 1)
|
| 248 |
print("OK: hardened llm_result.usage dereference in run_agent")
|