# Loop Engineering from First Principles — Kyle Mistele, HumanLayer

## Executive summary

The talk argues that relying solely on large-scale prompts for AI code generation is insufficient for complex, mission-critical systems. The solution lies in adopting 'control loops'—a pattern borrowed from control theory (Sensor $\rightarrow$ Controller $\rightarrow$ Actuator)—to ensure that code changes are small, verifiable, and incremental. This approach minimizes risk by preventing the creation of massive, unreviewable pull requests (PRs) and allows for robust automation within existing CI/CD pipelines.

## Key takeaways

- The Problem with Large PRs: Current AI workflows often result in 'blind Ralph loops'—massive, unreadable pull requests (e.g., 40,000 lines) that are too risky for large teams and critical systems. The goal must be to design loops that make code easier to read and verify.
- The Control Loop Framework: A robust system uses a Sensor (to measure the current state/error), a Controller (to determine the optimal incremental change), and an Actuator (to apply that change). This pattern is ideal for systems where changes must be measured, applied incrementally, and verified.
- Implementing Low-Friction Loops: Loops should be integrated into CI/CD platforms (GitHub Actions, CircleCI) using deterministic workflows. To maintain human oversight without friction, a 'feedback file' tracked in version control and triggered by comments (e.g., `/iterate`) is recommended to guide the agent.

## Technical details

- Control Loop Components: The process involves: 1) **Sensor:** Finding unmigrated code (e.g., using `as grep` for language-agnostic structural searches). 2) **Controller:** Determining the smallest, safest change to apply (e.g., picking the smallest unmigrated procedure). 3) **Actuator:** An agent combined with a skill that applies the change and commits/creates a PR.
- Golden Patterns: To guide the Actuator Agent, developers should manually define 'golden patterns'—idiomatic, handwritten examples used as pattern replicators within the agent's skill to ensure adherence to best practices.
- CI/CD Integration and Flow Control: Loops should be run via CI workflows (GitHub Actions, GitLab, CircleCI). To prevent work duplication or conflicts when humans are away, the workflow must check if a PR with the loop's label is already open; if so, it shuts down to ensure only one active PR per loop.

## Practical implications

- Reduces the risk associated with large-scale AI code generation by enforcing small, verifiable, and incremental changes.
- Provides a structured methodology (Sensor $\rightarrow$ Controller $\rightarrow$ Actuator) for automating complex codebase migrations (e.g., migrating RPC APIs to 'effect').
- Enables continuous, low-friction automation of technical debt reduction while maintaining human review checkpoints via specialized CI triggers.

## Topics

Control Theory, Agentic Workflows, Software Architecture, CI/CD Automation, Code Migration, Build Engineering, Kyle Mistele, HumanLayer

Source: https://www.youtube.com/watch?v=xIt_mTQp6mY
