fix(s08): avoid consecutive user messages when draining background notifications

The previous implementation always appended a new user message for background
results regardless of the trailing message's role. After a tool round-trip
(or right after the user query), the messages list could end up with two
adjacent user messages, which is messy for prompt caching, token accounting
and stricter API gateways.

Now we merge the background-results block into the trailing user message
when its role is already user, and only append a fresh user message
otherwise. tool_result blocks still immediately follow their tool_use, so
the protocol contract is preserved.
This commit is contained in:
huanghuang358
2026-04-19 12:53:46 +08:00
parent 4b95969a03
commit 757e84482f
+18 -2
View File
@@ -187,13 +187,29 @@ TOOLS = [
def agent_loop(messages: list):
while True:
# Drain background notifications and inject as system message before LLM call
# Drain background notifications and inject before the next LLM call.
# Merge into the trailing user message when possible to avoid emitting
# two consecutive user messages (which is messy for caching/debugging).
notifs = BG.drain_notifications()
if notifs and messages:
notif_text = "\n".join(
f"[bg:{n['task_id']}] {n['status']}: {n['result']}" for n in notifs
)
messages.append({"role": "user", "content": f"<background-results>\n{notif_text}\n</background-results>"})
bg_block = {
"type": "text",
"text": f"<background-results>\n{notif_text}\n</background-results>",
}
last = messages[-1]
if last["role"] == "user":
if isinstance(last["content"], str):
last["content"] = [
{"type": "text", "text": last["content"]},
bg_block,
]
else:
last["content"] = list(last["content"]) + [bg_block]
else:
messages.append({"role": "user", "content": [bg_block]})
response = client.messages.create(
model=MODEL, system=SYSTEM, messages=messages,
tools=TOOLS, max_tokens=8000,