Abstract:
When software is authored at machine speed, codebase entropy accelerates by orders of magnitude. Autonomous coding agents generate functional code quickly, but they frequently leave behind orphaned functions, redundant utility wrappers, stale test mocks, and drifting documentation. Without continuous automated cleanup, repositories experience decay, inflating context sizes and confounding future agent iterations. This article details the architecture of Autonomous Garbage Collection (GC) Loops: background agent routines that continuously prune dead code, deduplicate tests, and synchronize documentation with ground truth.
In human-led software engineering, code is written slowly; developers clean up as they go, guided by architectural reviews. In an agent-first software engineering lifecycle:
THE CODEBASE ENTROPY SPIRAL
┌─────────────────────────┐
│ Rapid Agent Patch │
└───────────┬─────────────┘
│
▼ Generates redundant helpers & leaves orphaned functions
┌─────────────────────────┐
│ Codebase Bloat (Entropy)│
└───────────┬─────────────┘
│
▼ Inflates context window & creates ambiguous search matches
┌─────────────────────────┐
│ Slower / Confused Agent │
└───────────┬─────────────┘
│
▼ Creates even more workaround code & hallucinated patterns
┌─────────────────────────┐
│ Total Repository Decay │
└─────────────────────────┘
formatDateTimeHelper() rather than finding formatTimestamp() in src/utils/date.ts, resulting in 8 slightly different date formatters across the repo.In modern harness engineering, code hygiene is externalized from the primary feature-development loop into dedicated Garbage Collection (GC) Subagents:
DUAL-LOOP AGENT ARCHITECTURE
[Primary Feature Loop] [Background GC Loop]
┌───────────────────────┐ ┌───────────────────────┐
│ Feature Coding Agent │ │ Garbage Collector │
│ • Implements PRs │ │ • Runs asynchronously│
│ • Fixes bugs │ │ • Operates on branch │
│ • Adds features │ │ • Zero-human blocker │
└───────────┬───────────┘ └───────────┬───────────┘
│ Merges PR │ Creates PR
▼ ▼
┌────────────────────────────────────────────────────────────────────────────┐
│ MAIN REPOSITORY CODEBASE │
└────────────────────────────────────────────────────────────────────────────┘
▲ │ Scans
│ Clean context & fast tests │
└────────────────────────────────────────────────────┘
The feature agent focuses strictly on delivering verified business logic. The background GC agent runs periodically (e.g., nightly or post-merge) to restore codebase purity.
The GC harness runs static dependency-graph analyzers (such as Knip, ts-prune, or Vulture) to find unreferenced files, unused exports, and dead dependencies:
# Automated Sweeper Routine
knip --production --reporter json > dead_code_report.json
The GC agent consumes dead_code_report.json, verifies that the unreferenced exports are not dynamically imported, generates a pruning patch, runs unit test sensors, and submits an automated cleanup PR:
"chore(gc): prune 14 unreferenced utility functions and 2 unused npm packages".
Iterative agent sessions often introduce duplicate test cases that test the exact same code paths under different descriptions. * Mutation Testing: The GC agent runs mutation tests (e.g., Stryker) to identify tests that never fail when mutations are introduced. * Flaky Test Quarantine: Tests with non-deterministic outcomes are automatically isolated into quarantine files to prevent blocking the primary feature loop.
The GC agent runs vector and AST similarity searches across the repository to locate duplicate implementations of standard logic (e.g., string sanitization, HTTP retries): * Extracts the canonical implementation into a central shared primitive. * Refactors all redundant callsites to consume the shared primitive. * Verifies all test sensors pass before committing.
A common source of agent failure is outdated documentation. The GC loop enforces synchronization:
* Compiles TypeScript interfaces / Pydantic models into OpenAPI specs and GraphQL schemas.
* Runs markdown linters to ensure all code examples in docs/ and README.md match actual method signatures.
Continuous codebase pruning delivers concrete engineering and financial returns:
| Metric | Without GC Loop | With Autonomous GC Loop | Benefit |
|---|---|---|---|
| Repo Token Size | +35% growth / quarter | Controlled (zero bloat) | Saves 20–35% in LLM API tokens across all future queries. |
| CI Test Runtime | Degrades over time (15m+) | Kept under 3 minutes | Fast test sensors keep feature agents operating at peak velocity. |
| Agent Hallucinations | High (confused by duplicate methods) | Minimal (single canonical API per domain) | Increases first-try task resolution rates by up to 25%. |
We haven't launched yet. But leave your email to get notified about exciting things that we will launch.