fix: question and answer vanish from the chat when a background sub-agent finishes before the answer (#31557)

When a background sub-agent finished while the model was still writing the answer that started it, the sub-agent's report was placed as a reply to the previous answer instead. Once the answer ended, the chat switched to that other branch of the conversation, which hid the latest question and answer, and the model replied to the report without seeing that question and answer. The report now follows the answer that started the sub-agent (or the newest completed reply after it), so the conversation stays on one branch.

Fixes #31507
This commit is contained in:
Classic298 2026-09-30 17:43:13 +02:00 • committed by GitHub
parent f98ca224c5
commit d7a74450b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -615,10 +615,16 @@ async def delegate(
updated_chat = copy.deepcopy(parent.chat or {})
updated_history = updated_chat.setdefault('history', {})
updated_messages = updated_history.setdefault('messages', {})
parent_message = updated_messages.get(parent_message_id)
done_assistants = [
message
for message in updated_messages.values()
if message.get('role') == 'assistant' and message.get('done') is not False
for message_id, message in updated_messages.items()
if message.get('role') == 'assistant'
and message.get('done') is not False
and (
parent_message is None
or any(entry is parent_message for entry in get_message_list(updated_messages, message_id))
)
]
result_parent_id = (
max(done_assistants, key=lambda message: message.get('timestamp', 0)).get('id')