From 19090b79f078b14a0c3415a5c1c8cf539e53f8df Mon Sep 17 00:00:00 2001 From: Nikketryhard Date: Sat, 14 Feb 2026 23:31:26 -0600 Subject: [PATCH] fix: prevent MALFORMED_FUNCTION_CALL infinite retry loop Root cause: after stripping LS tool definitions, two things remained: 1. toolConfig with mode=VALIDATED (forces function calling even with empty tools array) 2. Model's training/identity context causing it to attempt function calls in text Fix: - Remove empty tools array and toolConfig when no custom tools injected - Strip functionCall/functionResponse parts from conversation history - Append explicit 'no tools available' instruction to system prompt - Remove debug dump code --- src/mitm/modify.rs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/mitm/modify.rs b/src/mitm/modify.rs index c73c301..bebf2cc 100644 --- a/src/mitm/modify.rs +++ b/src/mitm/modify.rs @@ -48,7 +48,16 @@ pub fn modify_request(body: &[u8], tool_ctx: Option<&ToolContext>) -> Option\n{}\n", identity_text.trim()); + let mut new_sys = format!("\n{}\n", identity_text.trim()); + + // When no tools are available, explicitly tell the model not to attempt + // function calls. Without this, the model's training causes it to try + // calling tools from its identity context, resulting in MALFORMED_FUNCTION_CALL. + let has_tools = tool_ctx.as_ref().map_or(false, |ctx| ctx.tools.is_some()); + if !has_tools { + new_sys.push_str("\n\nIMPORTANT: You have NO tools available. Do not attempt to call any functions or tools. Respond with text only."); + } + let stripped = original_len - new_sys.len(); if stripped > 0 { changes.push(format!( @@ -181,11 +190,24 @@ pub fn modify_request(body: &[u8], tool_ctx: Option<&ToolContext>) -> Option