The Agentic Loop is the core operational engine of cli_engineer. It facilitates an autonomous, iterative cycle of planning, execution, and review to accomplish complex software engineering tasks. This mechanism allows the agent to break down a high-level goal, act on it, assess its own work, and self-correct until the objective is met.
The entire process is managed by the AgenticLoop struct defined in src/agentic_loop.rs.
The loop follows a distinct, repeating sequence of phases for each iteration. The state and knowledge from one iteration are passed to the next via the IterationContext.
The flow can be summarized as:
- Interpret: The initial user request is parsed into a structured
Task. - Plan: A detailed, step-by-step
Planis created to achieve theTask's goal. - Execute: The agent carries out each
Stepin thePlan. - Review: The results of the execution are critically evaluated for correctness and quality.
- Repeat or Finish: If the review determines the task is complete, the loop terminates. Otherwise, it begins a new iteration, using the review's feedback to create a revised plan.
This cycle continues until the task is successfully completed or the configured max_iterations limit is reached.
- Source:
src/interpreter.rs - The process begins when the
Interpreterreceives the raw user input. It analyzes the prompt to define a clearTaskwith a high-leveldescriptionand a specificgoal.
- Source:
src/planner.rs - The
Planneris responsible for creating a structuredPlan. It uses the LLM to break down theTask's goal into a sequence of actionableSteps. - Crucially, the
Planneris given the currentIterationContext. This allows it to make informed decisions based on what has already been accomplished, what files exist, and what issues were found in the previous iteration. For example, if a file already exists, the planner will generate aCodeModificationstep instead of aCodeGenerationstep.
- Source:
src/executor.rs - The
Executortakes thePlanand processes eachStepsequentially. - For each step, it constructs a specific prompt for the LLM based on the step's category (e.g.,
CodeGeneration,FileOperation,Documentation). - The
Executoris responsible for interacting with theArtifactManagerto create or modify files based on the LLM's output. The outcome of this phase is a collection ofStepResultobjects.
- Source:
src/reviewer.rs - The
Revieweracts as the quality assurance gate. It examines theStepResults from the execution phase. - Using the LLM, it assesses the work against the original plan, identifying
Issues and evaluating theoverall_quality. - The output is a
ReviewResult, which contains a list of issues, suggestions, and aready_to_deployflag. This flag is the primary signal for terminating the loop successfully.
- Source:
src/iteration_context.rs
The IterationContext is the "memory" of the agent, carrying state between iterations. It is the key to the agent's ability to learn and self-correct.
pub struct IterationContext {
pub iteration: usize,
pub existing_files: HashMap<String, FileInfo>,
pub last_review: Option<ReviewResult>,
pub pending_issues: Vec<Issue>,
pub progress_summary: String,
}Its primary responsibilities are:
existing_files: Tracks all files that have been created or modified. This prevents the agent from re-creating files and helps thePlannerdecide between generation and modification.pending_issues&last_review: This is the feedback mechanism. The issues identified by theReviewerin one iteration are fed directly into thePlannerin the next. This prompts the agent to generate steps that specifically address and fix the problems it found in its own work.
By passing this context object through each loop, the agent builds a progressively more accurate understanding of the project's state and what needs to be done next.