From 907bd88c8f7e23cc57360e34e1fc5a63695d0120 Mon Sep 17 00:00:00 2001 From: Kaloyan Nikolov Date: Wed, 25 Feb 2026 23:56:29 +0100 Subject: [PATCH] fix: federation only on first iteration, local-only for tool result processing - Critical fix: peers don't have tool results from previous iterations - Running federation on tool result iterations causes inconsistent context - Now federation is ONLY used on iteration 1 (initial planning) - Iterations 2+ are local-only (tool result processing) - This prevents the infinite ls loop and wrong file hallucinations - All 41 tests passing --- src/api/chat_handlers.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/api/chat_handlers.py b/src/api/chat_handlers.py index ed8b881..31183eb 100644 --- a/src/api/chat_handlers.py +++ b/src/api/chat_handlers.py @@ -562,15 +562,32 @@ async def handle_chat_completion( iteration += 1 logger.info(f"--- Tool Execution Iteration {iteration} ---") - # Generate response (unified interface - handles federation automatically) - logger.debug(f"Generating response...") - response_text, tokens_generated, tps = await _generate_with_consensus( - prompt=prompt, - max_tokens=request.max_tokens or 1024, - temperature=request.temperature or 0.7, - swarm_manager=swarm_manager, - federated_swarm=federated_swarm - ) + # Generate response + # IMPORTANT: Only use federation on FIRST iteration (initial planning) + # Subsequent iterations process tool results which only head node has + if iteration == 1 and use_federation: + # First iteration: use federation for consensus on initial plan + logger.info(f"🌐 Using federation for initial generation...") + response_text, tokens_generated, tps = await _generate_with_consensus( + prompt=prompt, + max_tokens=request.max_tokens or 1024, + temperature=request.temperature or 0.7, + swarm_manager=swarm_manager, + federated_swarm=federated_swarm + ) + else: + # Subsequent iterations: LOCAL ONLY + # Peers don't have tool results from previous iterations + # Using federation here would cause inconsistent context + if iteration > 1: + logger.debug(f"Using local generation (iteration {iteration}, tool context local only)") + response_text, tokens_generated, tps = await _generate_with_consensus( + prompt=prompt, + max_tokens=request.max_tokens or 1024, + temperature=request.temperature or 0.7, + swarm_manager=swarm_manager, + federated_swarm=None # Force local-only + ) logger.info(f"Generated response ({len(response_text)} chars, {tokens_generated} tokens)") logger.debug(f"Response: {response_text[:200]}...") @@ -701,7 +718,7 @@ async def handle_chat_completion( max_tokens=request.max_tokens or 1024, temperature=request.temperature or 0.7, swarm_manager=swarm_manager, - federated_swarm=federated_swarm + federated_swarm=None # Tool result processing is local-only ) logger.info(f"✅ Generated with tool results ({len(response_text)} chars, {tokens_generated} tokens)")