From 3fe4b128b5b584f41c635f967a8cbe4ece5c75cf Mon Sep 17 00:00:00 2001 From: Leiber Lyu Date: Sun, 31 May 2026 14:29:41 +1000 Subject: [PATCH] Fix a bug where tool_result messages must appear before text in user messages --- s13_background_tasks/code.py | 5 ++--- s14_cron_scheduler/code.py | 5 ++--- s15_agent_teams/code.py | 5 ++--- s16_team_protocols/code.py | 5 ++--- s20_comprehensive/code.py | 3 +-- 5 files changed, 9 insertions(+), 14 deletions(-) diff --git a/s13_background_tasks/code.py b/s13_background_tasks/code.py index e3f056b..49a03ad 100644 --- a/s13_background_tasks/code.py +++ b/s13_background_tasks/code.py @@ -444,15 +444,14 @@ def agent_loop(messages: list, context: dict): "tool_use_id": block.id, "content": output}) - # Inject background notifications + tool results in one user message - user_content = [] + # Inject tool results + background notifications in one user message + user_content = list(results) bg_notifications = collect_background_results() if bg_notifications: for notif in bg_notifications: user_content.append({"type": "text", "text": notif}) print(f" \033[32m[inject] {len(bg_notifications)} background " f"notification(s)\033[0m") - user_content.extend(results) messages.append({"role": "user", "content": user_content}) context = update_context(context, messages) system = get_system_prompt(context) diff --git a/s14_cron_scheduler/code.py b/s14_cron_scheduler/code.py index f18e7b1..7fd3632 100644 --- a/s14_cron_scheduler/code.py +++ b/s14_cron_scheduler/code.py @@ -726,13 +726,12 @@ def agent_loop(messages: list, context: dict) -> dict: "tool_use_id": block.id, "content": output}) - # Merge background notifications + tool results into one user message - user_content = [] + # Merge background tool results + notifications into one user message + user_content = list(results) bg_notifications = collect_background_results() if bg_notifications: for notif in bg_notifications: user_content.append({"type": "text", "text": notif}) - user_content.extend(results) messages.append({"role": "user", "content": user_content}) context = update_context(context, messages) system = get_system_prompt(context) diff --git a/s15_agent_teams/code.py b/s15_agent_teams/code.py index 2173dcd..53324b5 100644 --- a/s15_agent_teams/code.py +++ b/s15_agent_teams/code.py @@ -887,13 +887,12 @@ def agent_loop(messages: list, context: dict): "tool_use_id": block.id, "content": output}) - # Merge background notifications + tool results into one user message - user_content = [] + # Merge background tool results + notifications into one user message + user_content = list(results) bg_notifications = collect_background_results() if bg_notifications: for notif in bg_notifications: user_content.append({"type": "text", "text": notif}) - user_content.extend(results) messages.append({"role": "user", "content": user_content}) context = update_context(context, messages) system = get_system_prompt(context) diff --git a/s16_team_protocols/code.py b/s16_team_protocols/code.py index d0fbd24..c1ec663 100644 --- a/s16_team_protocols/code.py +++ b/s16_team_protocols/code.py @@ -839,13 +839,12 @@ def agent_loop(messages: list, context: dict): "tool_use_id": block.id, "content": output}) - # Merge background notifications + tool results into one user message - user_content = [] + # Merge background tool results + notifications into one user message + user_content = list(results) bg_notifications = collect_background_results() if bg_notifications: for notif in bg_notifications: user_content.append({"type": "text", "text": notif}) - user_content.extend(results) messages.append({"role": "user", "content": user_content}) context = update_context(context, messages) system = get_system_prompt(context) diff --git a/s20_comprehensive/code.py b/s20_comprehensive/code.py index e3c0618..a1f9ba5 100644 --- a/s20_comprehensive/code.py +++ b/s20_comprehensive/code.py @@ -1872,10 +1872,9 @@ def prepare_context(messages: list) -> list: def build_user_content(results: list[dict]) -> list[dict]: # Tool results and completed background notifications are both returned to # the model as user-side content, matching the tool_result feedback loop. - content = [] + content = list(results) for note in collect_background_results(): content.append({"type": "text", "text": note}) - content.extend(results) return content