# Loop Engineering

> A CLI and starter kits that scaffold scheduled coding-agent loops (triage, PR babysitting, CI and dependency sweeps) into a repo and score how ready they are to run

- Category: AI Coding / AI SDLC Automation
- Language: TypeScript
- Licence: MIT
- Repository: https://github.com/cobusgreyling/loop-engineering
- Website: https://cobusgreyling.github.io/loop-engineering/
- Canonical page: https://ai-tldr.dev/tools/loop-engineering/
- Updated: 2026-07-20

## In short

Loop Engineering is an open-source pattern library and command-line toolkit by Cobus Greyling for running AI coding agents in loops around a codebase. It sits in the AI SDLC Automation category of the AI Coding tools and is open-source, written in TypeScript and licensed MIT. To get started: scaffold a pattern.

## Overview

Loop Engineering is an open-source pattern library and command-line toolkit by Cobus Greyling for running AI coding agents in loops around a codebase. Instead of typing the next prompt yourself, you set up a system that discovers work on a schedule, hands it to an agent, verifies the result and keeps state in files in the repo. The README is explicit about scope: it is for operating agents around a codebase (keeping issues, CI and dependencies healthy, landing pull requests), not a button that rewrites a module.

The installable part is the unified CLI, `npx @cobusgreyling/loop`. `loop init` copies a starter for a chosen pattern and coding tool into your repository and creates `STATE.md`, `LOOP.md`, `loop-budget.md` and `loop-run-log.md`; `loop doctor` runs a readiness audit plus a drift check between `STATE.md` and `LOOP.md` and prints the top three actions. Scaffolding supports Claude Code (the default), Grok, Codex and opencode, and the docs give manual setups for Cursor, Windsurf, OpenClaw, Hermes and GitHub Actions.

Each pattern comes with a cadence and a staged rollout: week one is report-only (L1), then assisted fixes (L2), then unattended runs (L3) only after the verifier has been right for a week. The patterns are Daily Triage, Thin loop, PR Babysitter, CI Sweeper, Dependency Sweeper, Changelog Drafter, Post-Merge Cleanup and Issue Triage. Around them sit smaller tools for the risky parts: a token cost estimator, a circuit breaker that escalates repeated failures to a human, a path denylist and auto-merge allowlist gate, per-attempt git worktrees, a patch-capturing sandbox and an MCP server. The project is MIT-licensed and written in TypeScript; release v1.6.0 shipped on 2026-07-20.

## What it does

- One front door, `npx @cobusgreyling/loop`, with `init`, `doctor`, `status`, `audit`, `cost`, `sync`, `badge`, `context`, `worktree`, `gate`, `mcp` and `sandbox` subcommands
- Starters for eight loop patterns, from Daily Triage and Issue Triage to PR Babysitter, CI Sweeper and Dependency Sweeper, each with a recommended cadence and week-one level
- Loop Ready score from 0 to 100 with suggested next steps; recent runs weigh more than files on disk
- Token cost estimates per pattern, level and cadence before you schedule anything
- Safety tooling: a circuit breaker (exit 2 = escalate), a `gate.yaml` denylist and auto-merge allowlist, isolated worktrees per fix attempt and a sandbox that captures changes as a `.patch`
- Scaffolds for Claude Code, Grok, Codex and opencode, plus a GitHub Actions composite action and an MCP server for on-demand pattern and state lookup

## Getting started

Everything runs through npx in the root of an existing git repository; no clone is needed. Week one is report-only: let the loop write to `STATE.md` and read what it says before you allow fixes.

1. Scaffold a pattern

   Pick a pattern and the coding tool you use. `--tool` defaults to `claude`; `grok`, `codex` and `opencode` are also scaffolded. This copies the starter kit, creates the state files and prints your Loop Ready score.

```bash
npx @cobusgreyling/loop init . --pattern daily-triage --tool claude
```

2. Run the health check

   `doctor` combines the readiness audit, the STATE/LOOP drift check and a file check. It exits 0 when healthy, 1 on warnings and 2 when blocked (score under 40, critical drift, or not scaffolded). Add `--json` for machine-readable output.

```bash
npx @cobusgreyling/loop doctor .
```

3. Estimate the token cost

   Check what a pattern will spend at a given level and cadence before you schedule it. High-frequency loops such as CI Sweeper at 5 minutes can burn tokens fast.

```bash
npx @cobusgreyling/loop cost --pattern daily-triage --level L1 --cadence 1d
```

4. Start the loop, report-only

   In Claude Code, schedule the triage skill with `/loop`. The quickstart's first command reads state, updates priorities and does not touch code.

```text
/loop 1d Run $loop-triage. Read STATE.md. Merge findings into High Priority and Watch List. Update Last run. Do not edit code.
```

5. Set the merge gate before any auto-merge

   Copy the `gate.yaml` template to the repo root, then check paths against it. Exit 0 means allowed, 2 means escalate to a human.

```yaml
# gate.yaml
version: 1
denylist:
  - "src/auth/**"
  - "**/*.env"
autoMergeAllowlist:
  - "docs/**"
  - "**/*.md"

npx @cobusgreyling/loop gate check --action auto-merge --paths <f1,f2,...>
```

6. Check progress

   `status` shows a run-log dashboard; re-run `audit --suggest` after each improvement and only move from L1 to L2 to L3 once the verifier has proved itself.

```bash
npx @cobusgreyling/loop status .
npx @cobusgreyling/loop audit . --suggest
```

## When to use it

- Setting up a daily, report-only triage loop with Claude Code, Codex, Grok or opencode that keeps a repository's issues, CI and dependencies in a single `STATE.md`
- Babysitting open pull requests or sweeping failing CI with isolated worktrees and a circuit breaker that hands repeated failures back to a human
- Putting a mechanical denylist and auto-merge allowlist in front of any agent that is allowed to merge
- Estimating what a scheduled agent loop will cost in tokens, and scoring whether a repo is ready to run it unattended

## Version history

- 2026-07-20 v1.6.0: Added `loop-init --with-foundry` to scaffold a harness-foundry stack alongside the loop, and shipped the `loop-gate` path denylist and auto-merge allowlist. `loop-context` gained stagnation detection and `loop-worktree` a `--wait` queue.
