Docs Universal Loop

Universal Self-Improvement Loop

A config-driven audit engine and self-improvement loop that works in any project, any stack, any agent client. v3.0.0

Overview

This page proves that the self-improvement loop works in any project, any stack, any agent client. The loop detects code quality issues automatically, diagnoses them against known patterns, proposes fixes, and executes them — all within a single, config-driven pipeline that adapts to the project's language, framework, and agent environment.

Architecture

Two components power the loop: universal-audit.sh — a config-driven audit engine that scans code against project-specific rules — and the self-improvement skill — an orchestrator that interprets audit results and drives the fix pipeline.

init-agents → scaffolding → audit detects → skill diagnoses
       ↓
agent proposes → human approves → fix → verify → commit

Execution Flow

  1. init-agents — bootstraps the project with audit config, scripts, and skill directory
  2. scaffolding — creates PATTERNS.md, ANTI-PATTERNS.md, ADR skeleton
  3. audit detectsuniversal-audit.sh scans codebase and emits JSON report
  4. skill diagnoses — the self-improvement skill matches findings to patterns
  5. agent proposes — agent presents a fix plan with rationale
  6. human approves — user reviews and approves or rejects the proposal
  7. fix → verify → commit — approved fix is applied, re-audited, and committed

Key Files

The init-agents command creates 7 artifacts that form the loop's infrastructure:

FilePurpose
.audit-config.jsonConfiguration for the audit engine: excludes, severity levels, language-specific rules
scripts/audit-project.shThe audit script that runs checks and outputs structured JSON
skills/self-improvement/Skill directory containing the orchestrator and pattern definitions
PATTERNS.mdCatalog of recognized improvement patterns with severity and fix strategies
ANTI-PATTERNS.mdCatalog of anti-patterns to detect and avoid
ADRs/Architecture Decision Records directory for documenting audit-driven decisions
scripts/generate-adr.shAutomated ADR generator for documenting fixed issues

Stack Awareness

The loop auto-detects the project's language and configures excludes and check presets accordingly. Supported stacks and their auto-detection markers:

The auto-detected configuration is written to .audit-config.json:

{
  "version": "3.0.0",
  "stack": "node",
  "excludes": ["node_modules", "dist", ".next"],
  "checks": {
    "patterns": true,
    "anti-patterns": true,
    "adr-gap": true,
    "skill-gap": true
  },
  "severity": {
    "P0": "blocking",
    "P1": "high",
    "P2": "medium",
    "P3": "low"
  }
}

Agent Client Support

The loop uses a detect_target mechanism to adapt its output format and invocation style to the agent client in use:

ClientDetectionFormat
OpenCodeOPENCODE_HOME env varStandard skill invocation via skill() tool
Claude CodeCLAUDE_CODE env varCLAUDE.md + .claude/skills/ convention
Cursorcursor in TERM_PROGRAM.cursorrules injection
DevinDEVIN env varDevin-compatible instruction format
Gemini CLIGEMINI_CLI env varGemini-compatible prompt template

Cross-Platform

On Unix systems, the init command creates symlinks for the skill and script directories. On Windows, where symlinks may require elevated privileges, the loop falls back to copying the files instead. The init-agents.sh script detects the platform automatically and selects the appropriate strategy — no manual flags needed.

Quick Start

Initialize the loop in any project with a single command, then run the audit:

bash init-agents.sh
bash scripts/audit-project.sh --json
# Review the output, then run the self-improvement loop:
# "run self-improvement loop"