perf: skip the tool approval drain lookup for fresh chat messages (#29142)

Every chat completion request re-loads the target conversation's entire
message history from the database inside drain_approved_tool_calls() before
discovering there is nothing to drain: a fresh message always points at a
newly minted assistant message with no stored output, so the full-history
read (one SELECT of every chat_message row plus building the message map,
uncached, on top of the identical read process_chat_payload already did) is
pure overhead on every message.

Queued tool approvals can only ever be acted on by a resume or continue
request, and exactly those requests carry assistant_message_id in their
payload. The drain now returns early when the field is absent, removing one
O(conversation length) query per chat message while resume, continue, reject
and pause flows behave exactly as before, independent of the approval mode.
This commit is contained in:
Classic298
2026-08-30 23:57:11 -05:00
committed by GitHub
parent 949876f9c0
commit 9f680bb80b
+4 -2
View File
@@ -3221,10 +3221,12 @@ async def execute_tool_call_for_output(request, form_data, user, metadata, event
async def drain_approved_tool_calls(request, form_data, user, model, metadata) -> bool:
chat_id = metadata.get('chat_id')
message_id = metadata.get('message_id') or metadata.get('assistant_message_id')
if not is_saved_chat_id(chat_id) or not message_id:
assistant_message_id = metadata.get('assistant_message_id')
# Only a resume/continue payload re-enters an existing message; other paths mint a fresh id with nothing to drain.
if not is_saved_chat_id(chat_id) or not assistant_message_id:
return False
message_id = metadata.get('message_id') or assistant_message_id
message = await Chats.get_message_by_id_and_message_id(chat_id, message_id)
output = message.get('output') if message else None
if not isinstance(output, list):