Multi-Agent AI•9 min read

Getting Autonomous AI Swarms to Review GitHub PRs Without Hallucinating

Lessons from building GH-Swarm: Finite-state orchestration, deterministic verification gates, and preventing infinite LLM feedback loops.

#Multi-Agent AI#GitHub Actions#Architecture#TypeScript#LLMs
Getting Autonomous AI Swarms to Review GitHub PRs Without Hallucinating technical architecture schematic
Architectural Schematic • Harvinder Singh

Key Architectural Takeaways

  • ✓Never allow LLMs unconstrained bidirectional delegation without cycle-detection counters.
  • ✓Verification must be deterministic: use tsc, eslint, and test suites as infallible ground truth.
  • ✓Split agent responsibilities into specialized single-purpose roles (Reviewer, Coder, Verifier).
  • ✓Always generate patch diffs, never ask an LLM to rewrite entire source files.

When we built GH-Swarm, our dream was autonomous repository maintenance: a swarm of agents that could ingest an open issue, review pull requests, identify security vulnerabilities, and propose validated patches. During early prototyping, we encountered the fundamental trap of autonomous multi-agent systems: recursive reasoning loops.

The Infinite Loop Problem in Agent Swarms

If you give two LLM agents mutual peer feedback permissions, Agent A will propose an optimization, Agent B will suggest a stylistic tweak, and Agent A will modify it while introducing a subtle regression. Without rigid state controls, agents will happily burn through tokens debating code aesthetics without ever terminating.

⚠️ Architecture WarningUncontrolled agent communication is an anti-pattern. Autonomous swarms require a strictly directed acyclic graph (DAG) or a centralized Finite-State Machine with immutable transition limits.

Finite-State Machine (FSM) Orchestration

In GH-Swarm, no agent speaks directly to another agent. Instead, all agents interact exclusively with a central state machine via typed message schemas. Every task is bounded by a maximum iteration counter and transition guards:

swarm-state-machine.ts
type SwarmState = 'TRIAGE' | 'ANALYSIS' | 'PATCH_GEN' | 'VERIFY' | 'DONE' | 'FAILED';

interface SwarmContext {
  issueId: number;
  attempts: number;
  maxAttempts: 3;
  compilerDiagnostics: string[];
}

function transition(state: SwarmState, event: SwarmEvent, ctx: SwarmContext): SwarmState {
  if (ctx.attempts >= ctx.maxAttempts) {
    return 'FAILED'; // Inflexible circuit breaker prevents runaway spend
  }

  switch (state) {
    case 'TRIAGE': return 'ANALYSIS';
    case 'ANALYSIS': return 'PATCH_GEN';
    case 'PATCH_GEN': return 'VERIFY';
    case 'VERIFY': return event.allPassed ? 'DONE' : 'PATCH_GEN';
    default: return 'FAILED';
  }
}

Deterministic Verification Gates

An LLM must never be allowed to approve its own work or another LLMs work using subjective prose. In GH-Swarm, an agent only succeeds if deterministic host compilers verify the code: tsc --noEmit, eslint, and the repository test suite. The LLM is never trusted; the compiler is.

Unified Patch Diffs over Full-File Rewrites

Asking an LLM to regenerate a 600-line file to change 3 lines is a recipe for silent bugs, dropped comments, and hallucinated import paths. GH-Swarm enforces unified patch formats. The agent emits a standard git diff chunk, which is tested against the repository using git apply --check before any commit is drafted.

Production Results & What We Learned

After deploying GH-Swarm across 12 internal micro-repositories, automated PR review turnaround dropped from 4.2 hours to 85 seconds. Crucially, the FSM circuit breaker eliminated infinite LLM feedback loops entirely, reducing token spend by 74% while ensuring zero unvalidated code reached the staging branch.

Engineering Consultation & Advisory

Need similar mission-critical architecture for your venture?

Harvinder partners with founders and engineering leaders as Fractional CTO, conducts deep-dive architecture audits, and engineers production-grade distributed systems.