diff --git a/_sidebar.md b/_sidebar.md index 17acc05..306dd6c 100644 --- a/_sidebar.md +++ b/_sidebar.md @@ -127,6 +127,7 @@ * **Docs** * [Ai agent context injection pipelines](docs/ai-agent-context-injection-pipelines.md) * [Ai agent context pruning](docs/ai-agent-context-pruning.md) + * [Ai agent debugging loops and self correction](docs/ai-agent-debugging-loops-and-self-correction.md) * [Ai agent dynamic context pruning](docs/ai-agent-dynamic-context-pruning.md) * [Ai agent memory architectures](docs/ai-agent-memory-architectures.md) * [Ai agent multi model consensus](docs/ai-agent-multi-model-consensus.md) diff --git a/docs/ai-agent-debugging-loops-and-self-correction.md b/docs/ai-agent-debugging-loops-and-self-correction.md new file mode 100644 index 0000000..fbe9dec --- /dev/null +++ b/docs/ai-agent-debugging-loops-and-self-correction.md @@ -0,0 +1,129 @@ +--- +technology: AI Agent Orchestration +domain: Documentation +level: Senior/Architect +version: Latest +tags: [ai agents, orchestration, debugging, self-correction, vibe coding] +ai_role: Autonomous Architect +last_updated: 2026-05-18 +--- + +> ๐Ÿ“ฆ [best-practise](../README.md) / ๐Ÿ“„ [docs](./) +# ๐Ÿ”„ AI Agent Debugging Loops & Self-Correction Mechanisms + +In the autonomous zero-approval workflows of 2026, one of the greatest risks is the "Infinite Debugging Loop" โ€“ an scenario where an AI Agent repeatedly tries the same failing fix, draining API tokens and blocking CI/CD pipelines. To achieve robust AI Agent Orchestration, deterministic self-correction and loop-breaking mechanisms must be integrated directly into the agent's state machine. + +This guide defines the architectural standards for implementing fault-tolerant debugging and predictive self-correction loops. + +## ๐Ÿงฑ The Loop-Breaking Architecture + +Agents must track their attempt history statefully and implement a hard "Pivot Protocol" when the same error occurs sequentially. + +### โŒ Bad Practice + +Allowing an agent to repeatedly execute tests and read logs without maintaining an execution history or enforcing a failure threshold. + +```typescript +// โŒ Anti-pattern: Stateless retry loop +async function agentFixLoop(taskId: string) { + let success = false; + while (!success) { + const errorLog = await executeTests(); + if (!errorLog) { + success = true; + break; + } + // Agent tries to fix it based ONLY on the last error, ignoring history + const patch = await generateLlmPatch(errorLog); + await applyPatch(patch); + } +} +``` + +### โš ๏ธ Problem + +The approach above causes the agent to thrash. If the LLM generates a fix that causes the same error (or reverts to a previous state), the agent has no memory of this failure mode and will repeat the action infinitely, causing high latency and cost overruns. + +### โœ… Best Practice + +Implement a stateful failure counter and a deterministic "Pivot Strategy" using an orchestration boundary. When a threshold is reached, the agent MUST change its approach (e.g., reset state, search knowledge base, or request human intervention). + +```typescript +// โœ… Best-pattern: Stateful Self-Correction with Pivot Thresholds +interface AgentExecutionState { + attempts: number; + previousErrors: string[]; + strategiesTried: string[]; +} + +async function deterministicFixLoop(task: TaskContext) { + let state: AgentExecutionState = { attempts: 0, previousErrors: [], strategiesTried: [] }; + const MAX_RETRIES = 3; + + while (state.attempts < MAX_RETRIES) { + const result = await executePipeline(); + + if (result.success) return true; + + state.attempts++; + state.previousErrors.push(result.errorHash); + + if (hasRepeatedError(state.previousErrors)) { + // Force a pivot: Stop guessing, read documentation or rollback + const newStrategy = await triggerPivotProtocol(state); + state.strategiesTried.push(newStrategy.name); + await applyStrategy(newStrategy); + } else { + // Standard fix attempt + await generateAndApplyPatch(result.errorLog, state.strategiesTried); + } + } + + throw new Error("Systematic failure: Loop breaker activated after 3 attempts."); +} +``` + +### ๐Ÿš€ Solution + +By encapsulating the debugging process within a stateful object (`AgentExecutionState`), the orchestrator mathematically guarantees the loop will terminate. The `triggerPivotProtocol` ensures that after a repeated failure, the agent shifts cognitive contextsโ€”for instance, switching from "code editing" to "documentation lookup (`knowledgebase_lookup`)" or "deep log analysis". This enforces systemic stability and deterministic execution. + +--- + +## ๐Ÿ—บ๏ธ Self-Correction Data Flow + +The following sequence demonstrates how an agent transitions from local execution into an escalated pivot state. + +```mermaid +stateDiagram-v2 + [*] --> ExecuteTask + ExecuteTask --> VerificationGate + + VerificationGate --> Commit: Success (Zero-Approval) + VerificationGate --> AnalyzeFailure: Failure + + AnalyzeFailure --> CheckThreshold + + CheckThreshold --> StandardFix: Attempts < 3 & Unique Error + StandardFix --> ExecuteTask + + CheckThreshold --> PivotProtocol: Attempts >= 3 OR Repeated Error + + state PivotProtocol { + [*] --> RollbackState + RollbackState --> ReadDocs + ReadDocs --> GenerateNewHypothesis + GenerateNewHypothesis --> [*] + } + + PivotProtocol --> ExecuteTask: New Strategy Found + PivotProtocol --> Halt: No Strategies Left (Human Fallback) + + Commit --> [*] + Halt --> [*] +``` + +> [!IMPORTANT] +> The Failure Counter is absolute. If an agent tries to fix the same function 3 times with similar logic and fails, it MUST pivot. + +> [!NOTE] +> During a pivot, the agent should always search for `knowledgebase_lookup` tools to find alternative solutions rather than relying purely on pre-trained context. \ No newline at end of file