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"""Patch agent_loop.py: guarantee _call_llm_streaming / _call_llm_non_streaming always
|
| 2 |
+
return an LLMResult, and make OpenRouter errors explicit instead of None.
|
| 3 |
+
|
| 4 |
+
ROOT CAUSE of "'NoneType' object has no attribute 'usage'"
|
| 5 |
+
--------------------------------------------------------------
|
| 6 |
+
Both call sites in run_agent do:
|
| 7 |
+
|
| 8 |
+
llm_result = await _call_llm_streaming(...)
|
| 9 |
+
llm_observed_cost_usd = llm_result.usage.get("cost_usd")
|
| 10 |
+
|
| 11 |
+
Upstream defines those helpers as:
|
| 12 |
+
|
| 13 |
+
for _llm_attempt in range(_MAX_LLM_RETRIES): # _MAX_LLM_RETRIES == 3
|
| 14 |
+
try:
|
| 15 |
+
...
|
| 16 |
+
except Exception as e:
|
| 17 |
+
if <retryable> and _llm_attempt < _MAX_LLM_RETRIES - 1:
|
| 18 |
+
continue
|
| 19 |
+
raise
|
| 20 |
+
|
| 21 |
+
The `for` loop IS the last statement of the function. Every attempt either
|
| 22 |
+
returns or raises EXCEPT the case where a retry branch `continue`s on the final
|
| 23 |
+
iteration -- but the guard prevents that. The real hole: the effort-heal and
|
| 24 |
+
invalid-thinking-signature branches `continue` UNCONDITIONALLY with no attempt
|
| 25 |
+
budget check, so a provider that rejects the same config every time burns all 3
|
| 26 |
+
attempts via `continue`, the loop ends, and the function falls off the end
|
| 27 |
+
returning None. run_agent then dereferences None.
|
| 28 |
+
|
| 29 |
+
This is an upstream bug, not caused by the new model patches -- but adding
|
| 30 |
+
extra one-shot heals (tools-unsupported, upstream-429) gives it more ways to be
|
| 31 |
+
hit, so we close it properly.
|
| 32 |
+
|
| 33 |
+
FIX
|
| 34 |
+
---
|
| 35 |
+
1. Append an explicit terminal LLMResult after each `for` loop so the functions
|
| 36 |
+
can never return None.
|
| 37 |
+
2. Make the terminal result distinguishable (empty content, finish_reason=None)
|
| 38 |
+
so run_agent's existing code path degrades gracefully instead of crashing.
|
| 39 |
+
3. Defensive: use `getattr(llm_result, "usage", None) or {}` at the two
|
| 40 |
+
dereference sites so a missing usage can never raise.
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
import ast
|
| 44 |
+
|
| 45 |
+
FILE = "/app/agent/core/agent_loop.py"
|
| 46 |
+
|
| 47 |
+
with open(FILE) as f:
|
| 48 |
+
c = f.read()
|
| 49 |
+
|
| 50 |
+
if "_PATCH_TERMINAL_GUARD" in c:
|
| 51 |
+
print("OK: agent_loop.py already patched (idempotent)")
|
| 52 |
+
raise SystemExit(0)
|
| 53 |
+
|
| 54 |
+
tree = ast.parse(c)
|
| 55 |
+
targets = {
|
| 56 |
+
n.name: n
|
| 57 |
+
for n in tree.body
|
| 58 |
+
if isinstance(n, ast.AsyncFunctionDef)
|
| 59 |
+
and n.name in ("_call_llm_streaming", "_call_llm_non_streaming")
|
| 60 |
+
}
|
| 61 |
+
missing = {"_call_llm_streaming", "_call_llm_non_streaming"} - set(targets)
|
| 62 |
+
if missing:
|
| 63 |
+
print(f"FAIL: could not find {missing} in agent_loop.py")
|
| 64 |
+
raise SystemExit(1)
|
| 65 |
+
|
| 66 |
+
terminal = ''' # === PATCH_TERMINAL_GUARD ===
|
| 67 |
+
# Reached only when every retry attempt `continue`d without returning.
|
| 68 |
+
# Upstream has no `return` after the retry `for` loop, so the function used
|
| 69 |
+
# to fall off the end and hand back None, which run_agent then dereferenced
|
| 70 |
+
# (`llm_result.usage.get(...)`) -> AttributeError. Return an empty result so
|
| 71 |
+
# the turn degrades to a no-op instead of crashing.
|
| 72 |
+
logger.error(
|
| 73 |
+
"%s exhausted %d attempts without a result; returning empty result",
|
| 74 |
+
_model_name_for_error(llm_params),
|
| 75 |
+
_MAX_LLM_RETRIES,
|
| 76 |
+
)
|
| 77 |
+
return LLMResult(
|
| 78 |
+
content=None,
|
| 79 |
+
tool_calls_acc={},
|
| 80 |
+
token_count=0,
|
| 81 |
+
finish_reason=None,
|
| 82 |
+
)
|
| 83 |
+
'''
|
| 84 |
+
|
| 85 |
+
# Insert the terminal guard at the very end of each target function.
|
| 86 |
+
fn_spans = sorted(targets.values(), key=lambda n: n.lineno, reverse=True)
|
| 87 |
+
for fn in fn_spans:
|
| 88 |
+
end = fn.end_lineno
|
| 89 |
+
lines = c.splitlines(keepends=True)
|
| 90 |
+
body = "".join(lines[fn.body[0].lineno - 1: fn.body[-1].end_lineno])
|
| 91 |
+
if "_PATCH_TERMINAL_GUARD" in body:
|
| 92 |
+
print(f"OK: {fn.name} already guarded")
|
| 93 |
+
continue
|
| 94 |
+
code = terminal.replace("_model_name_for_error(llm_params)", "_model_name_for_error(llm_params)")
|
| 95 |
+
lines.insert(end, code)
|
| 96 |
+
c = "".join(lines)
|
| 97 |
+
print(f"OK: added terminal guard to {fn.name}")
|
| 98 |
+
|
| 99 |
+
ast.parse(c)
|
| 100 |
+
print("OK: agent_loop.py syntax valid after terminal guards")
|
| 101 |
+
|
| 102 |
+
# ---- Defensive dereference in run_agent --------------------------------
|
| 103 |
+
old = 'llm_observed_cost_usd = llm_result.usage.get("cost_usd")'
|
| 104 |
+
new = ('llm_observed_cost_usd = (getattr(llm_result, "usage", None) or {}).get(\n'
|
| 105 |
+
' "cost_usd"\n'
|
| 106 |
+
' )')
|
| 107 |
+
if old in c:
|
| 108 |
+
c = c.replace(old, new)
|
| 109 |
+
print("OK: hardened llm_result.usage dereference in run_agent")
|
| 110 |
+
else:
|
| 111 |
+
print("WARN: usage dereference pattern not found")
|
| 112 |
+
|
| 113 |
+
old2 = """ if session.stream:
|
| 114 |
+
llm_result = await _call_llm_streaming(
|
| 115 |
+
session, messages, tools, llm_params
|
| 116 |
+
)
|
| 117 |
+
else:
|
| 118 |
+
llm_result = await _call_llm_non_streaming(
|
| 119 |
+
session, messages, tools, llm_params
|
| 120 |
+
)
|
| 121 |
+
"""
|
| 122 |
+
new2 = """ if session.stream:
|
| 123 |
+
llm_result = await _call_llm_streaming(
|
| 124 |
+
session, messages, tools, llm_params
|
| 125 |
+
)
|
| 126 |
+
else:
|
| 127 |
+
llm_result = await _call_llm_non_streaming(
|
| 128 |
+
session, messages, tools, llm_params
|
| 129 |
+
)
|
| 130 |
+
if llm_result is None:
|
| 131 |
+
# Defensive: the helpers are now patched to always return an
|
| 132 |
+
# LLMResult, but never dereference None if that regresses.
|
| 133 |
+
logger.error(
|
| 134 |
+
"LLM helper returned None for %s; treating as empty turn",
|
| 135 |
+
_model_name_for_error(llm_params),
|
| 136 |
+
)
|
| 137 |
+
llm_result = LLMResult(
|
| 138 |
+
content=None,
|
| 139 |
+
tool_calls_acc={},
|
| 140 |
+
token_count=0,
|
| 141 |
+
finish_reason=None,
|
| 142 |
+
)
|
| 143 |
+
"""
|
| 144 |
+
if old2 in c:
|
| 145 |
+
c = c.replace(old2, new2)
|
| 146 |
+
print("OK: added None guard before run_agent dereferences llm_result")
|
| 147 |
+
else:
|
| 148 |
+
print("WARN: llm_result call-site block not found")
|
| 149 |
+
|
| 150 |
+
ast.parse(c)
|
| 151 |
+
with open(FILE, "w") as f:
|
| 152 |
+
f.write(c)
|
| 153 |
+
print("OK: agent_loop.py patched (terminal guards + defensive dereference)")
|
| 154 |
+
print("DONE: None-return fix applied")
|