fix: two background sub-agent reports that finish close together split the chat into two branches (#34542)

* fix: two background sub-agent reports that finish close together split the chat into two branches

When a model started two background sub-agents and the second one finished while the reply to the first report was still being written, the second report was attached to the answer that started them. The two reports ended up as separate branches and the chat switched to the second one, hiding the first report and the reply to it. The second report now goes after the reply that is still being written and is answered once that reply ends, so the conversation stays on one branch.

Fixes #34540

* Only treat an unfinished reply as the place for a report while it is still being written
This commit is contained in:
Classic298 authored and GitHub committed 2026-11-16 23:27:31 +09:00
1 parent e5aa83b847
commit 75bb64be19
1 file changed
+6 -5
+6 -5
View File
@@ -661,23 +661,24 @@ async def delegate(
updated_history = updated_chat.setdefault('history', {})
updated_messages = updated_history.setdefault('messages', {})
parent_message = updated_messages.get(parent_message_id)
done_assistants = [
is_active = await has_active_tasks(request.app.state.redis, parent_chat_id)
assistants = [
message
for message_id, message in updated_messages.items()
if message.get('role') == 'assistant'
and message.get('done') is not False
and (is_active or 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')
if done_assistants
max(assistants, key=lambda message: message.get('timestamp', 0)).get('id')
if assistants
else parent_message_id
)
pending_message['parentId'] = result_parent_id
if await has_active_tasks(request.app.state.redis, parent_chat_id):
if is_active:
pending_message['meta']['status'] = 'pending'
updated_messages[pending_message_id] = pending_message
if result_parent_id and result_parent_id in updated_messages: