Conversation Main Loop Decision Mechanism & Concurrency Control
🔄 1. Session Main Loop Deep Dive
In the run_conversation() function in agent/conversation_loop.py, the agent maintains a state machine. It does not simply execute a single LLM request but enters an adaptive tool-calling loop. The specific steps include:
- State Initialization: Load session context and long-term memory from the database, assembling the initial
messagesarray. - Interrupt Pre-check: At the start of each loop iteration, check
agent._interrupt_requested. The user sending a new message or a cancel signal from the gateway can trigger an interrupt. - LLM Negotiation: Invoke
client.chat.completions.createwithapi_messagesand the tool list.
🛡️ 2. Concurrency Throttling & Grace Call Mechanism
Key design to handle token budget exhaustion and runaway multi-agent concurrency:
🚦 Graceful Exit (grace_call)
When iteration_budget.remaining reaches zero, to prevent the agent from abruptly halting, the system activates _budget_grace_call. This grants the model one final read-only interaction, clearing the tools definition and forcing the LLM to generate only a concluding summary reply to the user.
🛑 Sub-Agent Truncation (_cap_delegate_task_calls)
When the LLM dispatches too many parallel delegate_task calls, high local resource consumption occurs. The system truncates concurrent sub-tasks to the MAX_CONCURRENT_CHILDREN limit and feeds overflow tasks back to the LLM as warning-format feedback, preventing runaway execution.