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
This commit is contained in:
2026-02-25 23:56:29 +01:00
parent af728505e8
commit 907bd88c8f
+27 -10
View File
@@ -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)")