The Three-Loop AI Engineering Harness: Eliminating Hallucinations in Production
Moving beyond loose prompts into structured harness engineering: isolating Project, Feature, and Task loops with AST verification, contract tests, and pre-commit security gates.
Over the past two years, engineering organizations have rushed to incorporate AI developer assistants and autonomous agents into their daily development workflows. Yet, team leads frequently encounter a common obstacle: while LLMs are phenomenal at generating greenfield code snippets, they struggle with sustained accuracy in mature multi-repository architectures.
The Failure of the Single-Prompt Loop
Most agent failures do not stem from reasoning deficiencies, but rather from uncontrolled scope and context pollution. When an agent is asked to fulfill a high-level user request in a single execution loop, it attempts to plan, code, refactor, and self-test all within a single shrinking context window. This inevitably leads to:
- Context Degradation: Earlier invariant instructions and architectural constraints get dropped from the active context.
- Silent Regressions: Existing unit tests are skipped or silently modified to force a green build.
- Leaked Discussion State: Workspace commit histories become polluted with raw prompt chatter.
The Three Loops: Project, Feature, and Task
The solution is to decouple AI operations into three distinct, deterministic harness loops:
1. The Project Loop (Repo Intelligence & Governance)
Maintains persistent AST catalogs, dependency graphs, security policies, and commit invariant rules outside the conversation context window.
2. The Feature Loop (Contract Verification & Spec Alignment)
Freezes the API schema and test specifications before any code generation begins. The agent is strictly evaluated against predefined mocks.
3. The Task Loop (Atomic Code Edits & Zero-Token Hooks)
Micro-executions that edit specific files, verified immediately by deterministic local pre-commit hooks at zero token cost.
Deterministic Quality Gates
By enforcing zero-token Git hooks (e.g., verifying commit author signatures, blocking accidental secret commits, and requiring conventional commit messages), enterprise teams ensure that agents operate with the same engineering rigor expected of human staff engineers.
Implementation: Zero-Token Quality Gate Hook
To enforce harness rules without spending token budget on repetitive lint and security prompts, we execute a deterministic local pre-commit gate:
| #!/bin/bash | |
| # Pre-commit zero-token quality gate enforcing conventional commits and clean author state | |
| set -euo pipefail | |
| # 1. Author identity verification | |
| EXPECTED_EMAIL="email@gmail.com" | |
| AUTHOR_EMAIL=$(git config user.email) | |
| if [ "$AUTHOR_EMAIL" != "$EXPECTED_EMAIL" ]; then | |
| echo "❌ [HARNESS GATE] Invalid commit author email: $AUTHOR_EMAIL" | |
| echo "Expected: $EXPECTED_EMAIL" | |
| exit 1 | |
| fi | |
| # 2. Secret scan | |
| if grep -rE "(AKIA[0-9A-Z]{16}|ghp_[a-zA-Z0-9]{36}|AIza[0-9A-Za-z\\-_]{35})" src/ 2>/dev/null; then | |
| echo "❌ [HARNESS GATE] Hardcoded secret or API key detected in commit diff!" | |
| exit 1 | |
| fi | |
| echo "✅ [HARNESS GATE] Zero-token deterministic verification passed." | |
| exit 0 |
