fix: bound broad tool workflows

This commit is contained in:
Mikei386
2026-08-24 13:29:41 +02:00
parent 5db73d0a92
commit 308b8a4d1f
11 changed files with 186 additions and 29 deletions
@@ -33,6 +33,12 @@ replacement = """ tool_call_iterations = 0
tool_call_executions = 0
max_tool_call_executions = 40
max_executions_per_tool = 12
# Inlet filters only see the first request, not later internal
# tool continuations. Bound the evidence injected into those
# continuations so one broad inventory cannot consume context.
max_single_tool_result_chars = 12000
max_total_tool_result_chars = 64000
tool_result_chars_used = 0
tool_execution_counts = {}
tool_signature_counts = {}
max_tool_call_iterations = getattr(
@@ -96,6 +102,64 @@ if source.count(needle) != 1:
)
source = source.replace(needle, replacement)
needle = """ results.append(
{
'tool_call_id': tool_call_id,
'content': str(tool_result) if tool_result else '',
**({'files': tool_result_files} if tool_result_files else {}),
**({'embeds': tool_result_embeds} if tool_result_embeds else {}),
}
)
"""
replacement = """ # Citations and the visible terminal event above use the
# complete result. Only the model continuation is compacted.
tool_result_text = str(tool_result) if tool_result else ''
remaining_tool_chars = max(
0, max_total_tool_result_chars - tool_result_chars_used
)
allowed_tool_chars = min(
max_single_tool_result_chars, remaining_tool_chars
)
if len(tool_result_text) > allowed_tool_chars:
original_tool_chars = len(tool_result_text)
if allowed_tool_chars >= 800:
marker = (
'\\n\\n[Tool result compacted by Open WebUI: '
f'{original_tool_chars} characters total; middle omitted. '
'Refine the next tool call instead of requesting the same '
'broad inventory again.]\\n\\n'
)
tail_chars = min(2000, allowed_tool_chars // 4)
head_chars = max(
0, allowed_tool_chars - len(marker) - tail_chars
)
tool_result_text = (
tool_result_text[:head_chars]
+ marker
+ tool_result_text[-tail_chars:]
)
else:
tool_result_text = (
'[Tool-result context budget exhausted. Use a more targeted '
'tool call or answer from existing evidence.]'
)[:allowed_tool_chars]
tool_result_chars_used += len(tool_result_text)
results.append(
{
'tool_call_id': tool_call_id,
'content': tool_result_text,
**({'files': tool_result_files} if tool_result_files else {}),
**({'embeds': tool_result_embeds} if tool_result_embeds else {}),
}
)
"""
if source.count(needle) != 1:
raise SystemExit(
f"expected exactly one Open WebUI tool-result anchor, found {source.count(needle)}"
)
source = source.replace(needle, replacement)
needle = """ res = await generate_chat_completion(
request,
new_form_data,