fix: very large system prompts can freeze the server (#35560)

A system prompt with many repeated tool approval policy or tool list markers and no closing marker took time growing with the square of its size to clean up, tens of seconds or more for half a megabyte. The tool approval cleanup runs on every chat request and blocks the server while it runs, so one oversized request stalled every user on that server process. Both cleanups now ignore the text after the last closing marker, which can never be removed anyway, so the same input takes under a millisecond and the cleaned prompt is the same as before.
This commit is contained in:
Classic298 authored and GitHub committed 2026-12-03 03:20:46 +04:00
1 parent 0d3f6b6ced
commit e160769e2f
2 files changed
+9 -2

No files matched your search

+5 -1
View File
@@ -90,7 +90,11 @@ _TOOL_APPROVAL_PROMPT_RE = re.compile(r'\n?<tool_approval_policy>\n.*?</tool_app
def strip_tool_approval_prompt(prompt):
"""Remove request-specific approval instructions before rebuilding or reusing a prompt."""
return _TOOL_APPROVAL_PROMPT_RE.sub('', prompt) if prompt else prompt
if not prompt:
return prompt
# Text after the last closing tag can never match, so skipping it avoids quadratic rescans.
head, tag, tail = prompt.rpartition('</tool_approval_policy>')
return _TOOL_APPROVAL_PROMPT_RE.sub('', head + tag) + tail
def set_tool_call_metadata(item, tools):
+4 -1
View File
@@ -239,7 +239,10 @@ def resolve_tool_call(tool_call: dict, tools: dict[str, dict]) -> str | None:
def strip_deferred_tools_manifest(system_prompt: str | None) -> str | None:
"""Remove the manifest from a system prompt that is reused by a request which builds its own."""
return _MANIFEST_RE.sub('', system_prompt) if system_prompt else system_prompt
if not system_prompt:
return system_prompt
head, tag, tail = system_prompt.rpartition('</available_tools>')
return _MANIFEST_RE.sub('', head + tag) + tail
# Letters, digits and combining marks, so vowel signs in scripts like Devanagari stay attached to their word.