Closed-Loop Repair

How Kōdo's compiler enables autonomous error repair by AI agents

Closed-Loop Repair

Kōdo’s compiler is designed for autonomous error repair. When an AI agent writes code that doesn’t compile, the compiler returns structured, machine-parseable errors with byte-accurate fix patches that can be applied automatically — no regex parsing, no guessing, no prose interpretation.

This is the core value proposition of Kōdo: the compiler is a repair partner, not just a validator.

The Repair Cycle

Every Kōdo compilation follows this closed-loop pattern:

    ┌──────────────────┐
    │  1. Agent writes  │
    │     .ko source    │
    └────────┬─────────┘


    ┌──────────────────┐       ┌──────────────────┐
    │  2. kodoc check  │──────▶│  3. kodoc fix    │
    │  --json-errors   │ erros │  (aplica patches)│
    └────────┬─────────┘       └────────┬─────────┘
             ▲        ▲                 │
             │        └─────────────────┘

             │ zero erros

    ┌──────────────────┐
    │  4. kodoc build  │
    │  binary + cert   │
    └────────┬─────────┘


       binary nativo
    + .ko.cert.json
    + confidence scores

The four stages:

StageCommandWhat happens
1. Write(agent generates code)Agent writes .ko source with contracts, annotations, and intent blocks
2. Checkkodoc check --json-errorsCompiler returns structured JSON errors with unique codes, byte offsets, and FixPatch objects
3. Fixkodoc fixAll machine-applicable patches are applied automatically — zero guessing
4. Buildkodoc buildNative binary + compilation certificate with contracts verified and confidence scores persisted

Walkthrough: The Full Cycle

Let’s walk through a complete closed-loop repair cycle, from broken code to verified binary.

Stage 1 — Agent writes code (with a bug)

An AI agent generates the following Kōdo module. There’s a deliberate type error: assigning an Int to a String variable.

module closed_loop_demo {
    meta {
        purpose: "Demonstrate the closed-loop error repair cycle",
        version: "0.1.0",
        author: "Demo"
    }

    @authored_by(agent: "claude")
    @confidence(0.85)
    fn safe_divide(a: Int, b: Int) -> Int
        requires { b != 0 }
        ensures { result * b <= a }
    {
        return a / b
    }

    fn main() {
        let x: String = 42           // ← bug: Int assigned to String
        let result: Int = safe_divide(10, 0)  // ← will fail contract at runtime
        print_int(result)
    }
}

Stage 2 — Compiler returns structured errors

kodoc check broken.ko --json-errors

The compiler returns machine-parseable JSON, not prose:

{
  "errors": [
    {
      "code": "E0200",
      "severity": "error",
      "message": "type mismatch: expected `String`, found `Int`",
      "span": {
        "file": "broken.ko",
        "start": 380,
        "end": 398,
        "line": 18,
        "column": 9
      },
      "suggestion": "ensure the expression produces a value of type `String`",
      "fixability": "auto",
      "fix_patch": {
        "description": "change type to `String`",
        "start_offset": 380,
        "end_offset": 398,
        "replacement": "String"
      },
      "see_also": "docs/error_index.md#E0200"
    }
  ],
  "warnings": [],
  "status": "failed",
  "count": 1
}

Every field is designed for agents:

FieldPurposeWhy it matters
codeUnique error code (E0200)Agent can look up exact fix strategies per code
spanByte offsets + line/columnAgent can locate the error precisely in the source
fix_patchMachine-applicable replacementAgent applies the patch with no guessing — just splice bytes
fixabilityauto, assisted, or manualAgent knows instantly if it can fix this alone
see_alsoLink to documentationAgent can learn more about the error pattern

Stage 3 — Auto-fix

The agent can preview patches before applying them:

kodoc fix broken.ko --dry-run
{
  "applied": false,
  "patches": [
    {
      "description": "change type to `String`",
      "file": "broken.ko",
      "start_offset": 380,
      "end_offset": 398,
      "replacement": "String"
    }
  ]
}

Then apply all auto-fixable patches:

kodoc fix broken.ko

Or the agent can apply patches programmatically — the byte offsets are exact, so it’s a simple string splice operation. No regex, no heuristics.

Stage 4 — Re-check and build

After fixing let x: String = 42 to let x: Int = 42 and safe_divide(10, 0) to safe_divide(10, 2):

kodoc check fixed.ko
Check passed for module `closed_loop_demo`
  contracts: 0 statically verified, 2 runtime checks

Zero errors. Now build:

kodoc build fixed.ko -o demo
./demo
Successfully compiled `closed_loop_demo` → demo
5

Beyond the Build: Verification Artifacts

The closed-loop doesn’t stop at a working binary. Kōdo generates artifacts that enable automated trust decisions.

Compilation Certificate

Every build produces a .ko.cert.json alongside the binary:

kodoc describe ./demo
Kodo Module Metadata
========================================
  compiler_version: 0.5.1
  module: closed_loop_demo
  purpose: Demonstrate the closed-loop error repair cycle
  version: 0.1.0
  functions:
    - safe_divide (authored_by: "claude", confidence: 0.85)
        requires: [requires clause 1]
        ensures:  [ensures clause 1]
    - main
  validators:
    - validate_safe_divide

The certificate records: who wrote each function, what contracts are in place, and whether they were verified.

Confidence Report

kodoc confidence-report fixed.ko
Confidence Report for module `closed_loop_demo`
============================================================
Overall confidence: 0.85
Module average:     0.85
Threshold:          0.80

Function                         Declared   Computed  Flags
----------------------------------------------------------------------
safe_divide                          0.85       0.85
main                                 1.00       0.85  no @authored_by

Notice: main has a declared confidence of 1.00, but its computed confidence drops to 0.85 because it calls safe_divide which has 0.85. Confidence propagates transitively through the call graph — a function is only as trustworthy as its least-confident dependency.

Error Explanation

Agents can also query the compiler for detailed explanations of any error code:

kodoc explain E0200
Error E0200: Type Mismatch

An expression produced a type that does not match what was expected.
Kōdo has no implicit conversions — all types must match exactly.
For example, you cannot assign a String to an Int variable or
return a Bool from a function declared to return Int.

Example of incorrect code:
    let x: Int = "hello"    // String cannot be assigned to Int

Corrected code:
    let x: Int = 42         // Int assigned to Int — correct

Structured Error Output

Every compiler error includes the following fields:

FieldTypePurpose
codeStringUnique error code (E0001–E0699)
severityString"error" or "warning"
messageStringHuman-readable description
span.fileStringSource file path
span.start / span.endIntByte offsets in source
span.line / span.columnIntLine and column numbers
suggestionStringNatural language fix suggestion
fix_patch.replacementStringExact text to splice in
fix_patch.start_offset / end_offsetIntByte range to replace
fixabilityString"auto", "assisted", or "manual"
see_alsoStringLink to error documentation

Fix Categories

CategoryMeaningAgent action
autoCan be applied without human interventionApply fix_patch directly
assistedNeeds context from the agentAgent uses suggestion + own context to fix
manualRequires human decisionFlag for human review

Target: >80% of errors should be auto-fixable. The compiler actively invests in generating precise patches, not just descriptions.

Agent Integration Pattern

The recommended integration pattern for AI agents using Kōdo:

┌─────────────────────────────────────────────────────────┐
│                   Agent Workflow                         │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  1. Generate .ko source code                            │
│     ↓                                                   │
│  2. kodoc check --json-errors                           │
│     ↓                                                   │
│  3. Parse JSON → categorize by fixability               │
│     ├── auto    → kodoc fix (apply patches)             │
│     ├── assisted → apply fix using agent context        │
│     └── manual  → flag for human review                 │
│     ↓                                                   │
│  4. Re-run kodoc check (verify zero errors)             │
│     ↓                                                   │
│  5. kodoc build → native binary + certificate           │
│     ↓                                                   │
│  6. kodoc confidence-report --json                      │
│     ↓                                                   │
│  7. Check: all functions > threshold?                   │
│     ├── yes → deploy                                    │
│     └── no  → flag low-confidence functions             │
│                                                         │
└─────────────────────────────────────────────────────────┘

This cycle typically completes in 1–2 iterations for well-formed code. The structured error format eliminates the regex-parsing and prose-interpretation overhead that plagues agents working with traditional compilers.

MCP Server Integration

For agents that support the Model Context Protocol, the closed-loop can be driven entirely through MCP tool calls — see MCP Server for details:

  • kodo.check — type-check with structured JSON errors
  • kodo.fix — auto-apply patches
  • kodo.build — compile to native binary
  • kodo.describe — inspect compilation certificate
  • kodo.explain — get error documentation
  • kodo.confidence_report — check trust scores

Error Code Ranges

RangePhaseExamples
E0001–E0099LexerInvalid tokens, unterminated strings
E0100–E0199ParserSyntax errors, missing braces
E0200–E0299TypesType mismatches, undefined variables
E0300–E0399ContractsContract violations, Z3 refutations
E0400–E0499ResolverIntent resolution failures
E0500–E0599MIROptimization issues
E0600–E0699CodegenCode generation failures

For the complete error catalog, see the Error Index.

Try It Yourself

The demo files from this walkthrough are available in the repository:

Run the full cycle:

# Stage 2: Check (see structured errors)
kodoc check examples/closed_loop_demo/step1_broken.ko --json-errors

# Stage 3: Preview fixes
kodoc fix examples/closed_loop_demo/step1_broken.ko --dry-run

# Stage 4: Build the fixed version
kodoc build examples/closed_loop_demo/step2_fixed.ko -o demo
./demo

# Bonus: Inspect the result
kodoc describe ./demo
kodoc confidence-report examples/closed_loop_demo/step2_fixed.ko
kodoc explain E0200

Next Steps