The Problem with 'Blind' AI Loops

Most current AI coding agents operate as 'blind loops'—they take a prompt, generate a massive amount of code, and produce 40,000-line pull requests (PRs) that are impossible to review. This approach is risky for production systems, expensive in terms of token usage, and often results in unstable code. The goal should not be to generate as much code as possible, but to build a 'software factory' that improves the codebase incrementally while remaining readable and maintainable.

Implementing Control Theory for Codebases

To move from blind generation to reliable engineering, apply the principles of control theory to your agent loops. A control loop consists of four components:

  • Sensor: Measures the current state of the codebase (e.g., using ast-grep or ESLint to find unmigrated procedures).
  • Set Point: Defines the desired end state (e.g., all procedures migrated to a specific library like Effect).
  • Controller: Calculates the 'error' (the difference between current and desired state) and determines the next incremental change.
  • Actuator: An agent equipped with specific 'golden patterns' (hand-written idiomatic examples) that applies the change.

This pattern allows for deterministic, small-step migrations that are easy to verify. By using tools like ast-grep, you can scan codebases language-agnostically and sort violations deterministically, ensuring the agent works on the highest-priority tasks first.

Operationalizing the Loop

To make these loops production-ready, treat the agent's output as a standard engineering workflow:

  • CI Integration: Use existing CI/CD tools (GitHub Actions, CircleCI) to run the loop. This provides access to secrets and existing infrastructure without needing new clusters.
  • Flow Control: Prevent 'PR stacking' by checking for open PRs before initiating a new loop iteration. If a previous PR from the same loop is still open, the loop shuts down, ensuring humans aren't overwhelmed by duplicate or conflicting work.
  • Human-in-the-Loop: Track a feedback.md file in version control. If a human needs to re-steer the agent, they can add a comment (e.g., /iterate) on the PR. The workflow then loads the PR context, diffs, and the feedback file back into the agent's context to refine the output.
  • Scaling: Once the loop is stable, increase velocity by having the controller pick multiple procedures to migrate in parallel, or by running the workflow multiple times to distribute the work across the team.