fix(agent): attachments no longer crash linked-session turns; ask_user closes its tool batch
Two agent-loop bugs found in the Sep 24 loop evaluation, folded into the 1.5.7 line: 1. A linked-session turn whose user message carried an image/video/document attachment crashed 100% of the time: derive_llm_history projects multimodal content as an OpenAI parts list, and orchestrate() read that list as the goal and called .strip() on it (AttributeError: 'list' object has no attribute 'strip'). New log.text_of_content() extracts the text part; the goal read uses it, so single-loop and swarm paths both survive attachments. 2. When ask_user paused the turn mid tool-batch, the remaining calls of that step never executed but also never received tool results, leaving an assistant tool_calls message with dangling ids - an invalid request sequence that strict OpenAI-compatible servers 400 on the next turn. The pause path now synthesizes skipped tool/call + tool/result events and tool-role messages for the un-executed ids before ending the turn. The new multimodal-goal tests now stub the planner LLM: the attachment appendix puts newlines in the extracted goal, so the message is never trivial-path and the unstubbed planner made a real network call - the source of the tests' flakiness (and the swarm-path test now attaches the document it always claimed to). Backend suite: 538 passed.
This commit is contained in:
@@ -624,6 +624,25 @@ def max_turn_number(session_id: int) -> int:
|
||||
STEERING_INJECT_HEADER = "[STEERING — user message sent while you work]"
|
||||
|
||||
|
||||
def text_of_content(content: Any) -> str:
|
||||
"""The text of a projected content value (string or multimodal parts list).
|
||||
|
||||
`project_user_content` returns an OpenAI parts list when a message has
|
||||
usable attachments. Routing/planning code that only needs the words (the
|
||||
orchestrator goal, swarm synthesis) must go through this helper — calling
|
||||
string methods on the parts list directly is the multimodal-goal crash
|
||||
(`'list' object has no attribute 'strip'`).
|
||||
"""
|
||||
if isinstance(content, list):
|
||||
texts = [
|
||||
str(p.get("text") or "")
|
||||
for p in content
|
||||
if isinstance(p, dict) and p.get("type") == "text"
|
||||
]
|
||||
return "\n".join(t for t in texts if t).strip()
|
||||
return content if isinstance(content, str) else ""
|
||||
|
||||
|
||||
def steering_user_content(d: dict[str, Any]) -> Any:
|
||||
"""Projected steering content with the [STEERING] header.
|
||||
|
||||
|
||||
@@ -318,7 +318,7 @@ async def run_turn(
|
||||
if reasoning_text:
|
||||
tool_msg_data["reasoning"] = reasoning_text
|
||||
log_append(session_log.ASSISTANT_MESSAGE, tool_msg_data)
|
||||
for tc in tool_calls:
|
||||
for tc_idx, tc in enumerate(tool_calls):
|
||||
name = tc["function"]["name"]
|
||||
raw_args = tc["function"].get("arguments") or "{}"
|
||||
try:
|
||||
@@ -425,6 +425,66 @@ async def run_turn(
|
||||
# ask_user ends the turn: the agent's question waits for the
|
||||
# user's answer, so the loop must not burn steps polling.
|
||||
if isinstance(result, dict) and result.get("awaiting_user_input"):
|
||||
# Close the batch first: any REMAINING calls of this step
|
||||
# never executed, and an assistant tool_calls message with
|
||||
# missing results is an invalid request sequence (strict
|
||||
# OpenAI-compatible servers 400 the next turn). Synthesize
|
||||
# skipped receipts for the un-executed call ids.
|
||||
for pending in tool_calls[tc_idx + 1:]:
|
||||
skipped = {
|
||||
"ok": False,
|
||||
"skipped": True,
|
||||
"error": (
|
||||
"Not executed — this turn paused for the user's "
|
||||
"answer to an ask_user question in the same step. "
|
||||
"Re-issue the call after they answer if still needed."
|
||||
),
|
||||
}
|
||||
pname = pending["function"]["name"]
|
||||
praw_args = pending["function"].get("arguments") or "{}"
|
||||
try:
|
||||
pargs = json.loads(praw_args) if praw_args.strip() else None
|
||||
except json.JSONDecodeError:
|
||||
pargs = None
|
||||
log_append(
|
||||
session_log.TOOL_CALL,
|
||||
{
|
||||
"turn": turn_no,
|
||||
"step": iteration,
|
||||
"call_id": pending["id"],
|
||||
"tool_name": pname,
|
||||
"arguments": praw_args,
|
||||
"agent_name": agent_name,
|
||||
},
|
||||
)
|
||||
log_append(
|
||||
session_log.TOOL_RESULT,
|
||||
{
|
||||
"turn": turn_no,
|
||||
"step": iteration,
|
||||
"call_id": pending["id"],
|
||||
"tool_name": pname,
|
||||
"result": skipped,
|
||||
"agent_name": agent_name,
|
||||
},
|
||||
)
|
||||
messages.append(
|
||||
{
|
||||
"role": "tool",
|
||||
"tool_call_id": pending["id"],
|
||||
"content": json.dumps(skipped, ensure_ascii=False),
|
||||
}
|
||||
)
|
||||
await emit(
|
||||
{
|
||||
"role": "tool",
|
||||
"agent_name": agent_name,
|
||||
"tool_name": pname,
|
||||
"tool_args": pargs if isinstance(pargs, dict) else None,
|
||||
"tool_result": skipped,
|
||||
"content": "",
|
||||
}
|
||||
)
|
||||
log_append(session_log.STEP_END, {"turn": turn_no, "step": iteration})
|
||||
turn_status = "awaiting_input"
|
||||
raise _AwaitingInput()
|
||||
|
||||
@@ -313,9 +313,15 @@ async def orchestrate(
|
||||
max_user_turns=MAX_HISTORY_USER_TURNS,
|
||||
max_chars=_history_char_budget(),
|
||||
)
|
||||
goal = next(
|
||||
(m["content"] for m in reversed(derived) if m.get("role") == "user"),
|
||||
"",
|
||||
# Multimodal user content (image/video/document attachments) projects as
|
||||
# an OpenAI parts LIST, not a string — extract the text part. Calling
|
||||
# string methods on the raw value crashed every linked-session turn that
|
||||
# carried an attachment ('list' object has no attribute 'strip').
|
||||
goal = session_log.text_of_content(
|
||||
next(
|
||||
(m["content"] for m in reversed(derived) if m.get("role") == "user"),
|
||||
"",
|
||||
)
|
||||
)
|
||||
|
||||
if ctx.project_id is None:
|
||||
|
||||
Reference in New Issue
Block a user