fix: remove synthetic error message injection, fix session resume after repeated failures
Two changes to the error handler in the agent loop: 1. Remove the 'if not pending_handled' block that injected fake [System error during processing: ...] messages into conversation history. These polluted history, burned tokens on retries, and could violate role alternation by injecting as role=user. The tool_calls error-result path (role=tool) is preserved. 2. Append the error final_response as an assistant message when hitting the iteration limit, so session resume doesn't produce consecutive user messages.
This commit is contained in:
parent
10d719ac1b
commit
779619f742
1 changed files with 9 additions and 11 deletions
20
run_agent.py
20
run_agent.py
|
|
@ -6807,20 +6807,18 @@ class AIAgent:
|
||||||
pending_handled = True
|
pending_handled = True
|
||||||
break
|
break
|
||||||
|
|
||||||
if not pending_handled:
|
# Non-tool errors don't need a synthetic message injected.
|
||||||
# Error happened before tool processing (e.g. response parsing).
|
# The error is already printed to the user (line above), and
|
||||||
# Choose role to avoid consecutive same-role messages.
|
# the retry loop continues. Injecting a fake user/assistant
|
||||||
last_role = messages[-1].get("role") if messages else None
|
# message pollutes history, burns tokens, and risks violating
|
||||||
err_role = "assistant" if last_role == "user" else "user"
|
# role-alternation invariants.
|
||||||
sys_err_msg = {
|
|
||||||
"role": err_role,
|
|
||||||
"content": f"[System error during processing: {error_msg}]",
|
|
||||||
}
|
|
||||||
messages.append(sys_err_msg)
|
|
||||||
|
|
||||||
# If we're near the limit, break to avoid infinite loops
|
# If we're near the limit, break to avoid infinite loops
|
||||||
if api_call_count >= self.max_iterations - 1:
|
if api_call_count >= self.max_iterations - 1:
|
||||||
final_response = f"I apologize, but I encountered repeated errors: {error_msg}"
|
final_response = f"I apologize, but I encountered repeated errors: {error_msg}"
|
||||||
|
# Append as assistant so the history stays valid for
|
||||||
|
# session resume (avoids consecutive user messages).
|
||||||
|
messages.append({"role": "assistant", "content": final_response})
|
||||||
break
|
break
|
||||||
|
|
||||||
if final_response is None and (
|
if final_response is None and (
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue