Skip to content

Practical patterns, starters & CLI tools for loop engineering with AI coding agents. Design systems that prompt and orchestrate agents (inspired by Addy Osmani and Boris Cherny). Includes loop-audit, loop-init, loop-cost.

3.8 KB
Raw Blame
import { readFile, copyFile } from 'node:fs/promises';
import { createHash } from 'node:crypto';
import path from 'node:path';
import { runInSandbox, SandboxOptions, SandboxResult } from '@cobusgreyling/loop-sandbox';

export interface SwarmOptions extends SandboxOptions {
  count: number;
}

export interface ConsensusResult {
  reached: boolean;
  majorityHash: string | null;
  majorityCount: number;
  totalPatches: number;
  consensusPatchFile: string | null;
  divergentPatches: string[];
}

export async function runSwarm(root: string, command: string, args: string[], options: SwarmOptions): Promise<ConsensusResult> {
  const { count, ...sandboxOptions } = options;

  console.log(`\n🐝 Launching loop-swarm with ${count} sequential agents (serialized to prevent worktree races)...`);

  const results: SandboxResult[] = [];
  for (let i = 0; i < count; i++) {
    console.log(`\n▶️ Agent ${i + 1}/${count}`);
    results.push(await runInSandbox(root, command, args, sandboxOptions));
  }

  console.log(`\n🧠 Analyzing swarm results...`);

  // Filter out runs that failed (non-zero exitCode)
  const successfulRuns = results.filter(r => r.exitCode === 0);
  const failedRuns = count - successfulRuns.length;
  
  if (failedRuns > 0) {
    console.log(`⚠️ ${failedRuns} agent(s) failed with non-zero exit codes. They are excluded from consensus voting.`);
  }

  // Count identical patches among successful runs
  const hashCounts: Record<string, { count: number; files: string[] }> = {};
  
  let noChangeCount = 0;

  for (const r of successfulRuns) {
    if (!r.hasChanges || !r.patchFile) {
      noChangeCount++;
      continue;
    }
    const buffer = await readFile(r.patchFile);
    const hash = createHash('sha256').update(buffer).digest('hex');
    
    if (!hashCounts[hash]) {
      hashCounts[hash] = { count: 0, files: [] };
    }
    hashCounts[hash].count++;
    hashCounts[hash].files.push(r.patchFile);
  }

  const threshold = Math.floor(count / 2) + 1; // Strict majority of total launched agents

  if (noChangeCount >= threshold) {
    console.log(`✅ Consensus reached! ${noChangeCount}/${count} agents produced NO changes (success).`);
    return {
      reached: true,
      majorityHash: null,
      majorityCount: noChangeCount,
      totalPatches: successfulRuns.length - noChangeCount,
      consensusPatchFile: null,
      divergentPatches: []
    };
  }

  // Find majority among patches
  let maxCount = 0;
  let majorityHash: string | null = null;
  for (const [hash, data] of Object.entries(hashCounts)) {
    if (data.count > maxCount) {
      maxCount = data.count;
      majorityHash = hash;
    }
  }

  const divergentPatches = Object.values(hashCounts).flatMap(d => d.files);

  if (maxCount >= threshold && majorityHash) {
    console.log(`✅ Consensus reached! ${maxCount}/${count} agents produced the exact same patch.`);
    
    const representativePatch = hashCounts[majorityHash].files[0];
    const consensusPatchFile = path.join(path.dirname(representativePatch), 'consensus.patch');
    
    await copyFile(representativePatch, consensusPatchFile);
    console.log(`🎉 Consensus patch saved to: ${consensusPatchFile}`);
    
    return {
      reached: true,
      majorityHash,
      majorityCount: maxCount,
      totalPatches: successfulRuns.length - noChangeCount,
      consensusPatchFile,
      divergentPatches: divergentPatches.filter(f => !hashCounts[majorityHash!].files.includes(f))
    };
  }

  const maxAgreement = Math.max(maxCount, noChangeCount);
  console.log(`❌ Swarm failed to reach consensus. (Highest agreement: ${maxAgreement}/${count})`);
  
  return {
    reached: false,
    majorityHash: null,
    majorityCount: maxAgreement,
    totalPatches: successfulRuns.length - noChangeCount,
    consensusPatchFile: null,
    divergentPatches
  };
}