Kode-cli/AGENT_LOOP_FIX.md
CrazyBoyM 926df2cfaf feat: Ultra-redesign completion system with @mention integration
- Complete architectural overhaul of useUnifiedCompletion hook
- Unified state management: 8 separate states → single CompletionState interface
- Simplified core logic: getWordAtCursor 194 lines → 42 lines (78% reduction)
- Fixed infinite React update loops with ref-based input tracking
- Smart triggering mechanism replacing aggressive auto-completion
- Integrated @agent and @file mention system with system reminders
- Added comprehensive agent loading and mention processing
- Enhanced Tab/Arrow/Enter key handling with clean event management
- Maintained 100% functional compatibility across all completion types

Key improvements:
• File path completion (relative, absolute, ~expansion, @references)
• Slash command completion (/help, /model, etc.)
• Agent completion (@agent-xxx with intelligent descriptions)
• System command completion (PATH scanning with fallback)
• Terminal-style Tab cycling, Enter confirmation, Escape cancellation
• Preview mode with boundary calculation
• History navigation compatibility
• Empty directory handling with user feedback

Architecture: Event-driven @mention detection → system reminder injection → LLM tool usage
Performance: Eliminated 7-layer nested conditionals, reduced state synchronization issues
Reliability: Fixed maximum update depth exceeded warnings, stable state management
2025-08-21 01:21:12 +08:00

2.0 KiB

Agent Loop Fix Summary

Root Cause Analysis

The agent loop was breaking because we introduced complex async @mention processing that interrupted the clean message flow. The original working codebase uses a simple, synchronous approach.

Key Problems Identified

  1. Complex Async Processing: The modified code added multiple async imports and checks during message processing
  2. Input Modification: Removing @mentions and injecting system reminders changed the message structure
  3. Flow Interruption: The async operations and input modifications broke the continuous agent execution loop

Solution Applied

Restored the original simple message processing:

  • Use resolveFileReferences to embed file content directly (as the original does)
  • Remove complex agent mention detection
  • Keep the message flow synchronous and clean

Original vs Modified

Original (Working):

// Simple and direct
if (input.includes('@')) {
  processedInput = await resolveFileReferences(processedInput)
}

Modified (Broken):

// Complex with multiple async operations
if (input.includes('@')) {
  // Multiple async imports
  // Agent detection logic
  // System reminder generation
  // Input modification
  // ... 70+ lines of complex logic
}

Why This Fixes the Issue

  1. Preserves Message Integrity: Messages flow through without modification
  2. No Async Interruptions: Simple, predictable execution
  3. Maintains Agent Context: The agent loop can continue without context loss

Testing

The agent loop should now work properly:

  • Messages will process continuously
  • Agents can execute multiple rounds
  • @file mentions will embed content (as designed)
  • No unexpected interruptions

Lesson Learned

Keep the message processing pipeline simple and synchronous. Complex async operations and input modifications during message processing break the agent execution loop. Any special handling should be done at the tool execution level, not during message preparation.