The pattern OpenAI’s Codex team uses to keep an agent-generated codebase from drifting into chaos. Two halves: golden principles (opinionated, mechanical rules encoded in the repo) and garbage collection (recurring background agents that scan for deviations and open targeted refactor PRs).

Source: Harness Engineering at OpenAI.

The problem this solves

Codex (or any coding agent) replicates patterns it sees in the repo. Even uneven or suboptimal patterns. Over many PRs and many agent runs, the codebase drifts:

  • Two ways to do the same thing accumulate side by side.
  • Hand-rolled helpers proliferate when a shared utility already exists.
  • Boundary validation is done in inconsistent places.
  • Naming conventions split into multiple competing styles.
  • Stale documentation and code disagree.

The OpenAI team’s first response was the obvious one: every Friday, humans cleaned up “AI slop” for 20% of the week.

Key

Manual slop cleanup does not scale. The throughput that makes coding agents valuable is the same throughput that creates entropy faster than humans can fix it.

So the cleanup itself had to be automated.

The two halves

1. Golden principles (encoded rules)

Opinionated, mechanical rules that define what “good” means in this repo. Not aspirational (“we should write clean code”) but mechanical and checkable. The OpenAI examples:

  • Prefer shared utility packages over hand-rolled helpers. Keep invariants centralised so when one changes, every caller sees the change.
  • No YOLO data probing. Validate at boundaries or use typed SDKs. The agent must not build on guessed shapes.
  • Parse data shapes at the boundary (Parse, don’t validate).

These are written down in the repo (in OpenAI’s case, as docs/QUALITY_SCORE.md and similar), making them legible (see Agent Legibility) and enforceable.

2. Recurring GC agents

Background Codex tasks that run on a schedule. They:

  1. Scan the codebase for deviations from the golden principles.
  2. Update quality grades per domain in QUALITY_SCORE.md.
  3. Open targeted refactor PRs with the deviations fixed.

The PRs are deliberately small and focused. Most can be reviewed in under a minute and auto-merge once they pass CI. Human review is only required when the GC agent flags something it cannot fix mechanically.

Why this works

“Technical debt is like a high-interest loan. It’s almost always better to pay it down continuously in small increments than to let it compound and tackle it in painful bursts.”

Three mechanics make this stick:

  1. Capture human taste once, enforce continuously. A golden principle is a one-time write that pays back on every line of code thereafter. The cost amortises across the entire codebase.
  2. Catch drift early. Bad patterns get found within a day, not weeks. They have not propagated yet.
  3. Repurpose the throughput that creates the problem to solve it. The same agent throughput that produces drift can produce drift-fixes. The fix-rate matches the entropy-rate by construction.

Compare and contrast

vs. traditional human refactor sprints

TraitHuman sprintGC agent
CadenceQuarterly or “when it gets bad”Continuous, daily
Scope per PRLarge, mixedSmall, targeted
Review costHigh (humans must read)Low (mostly auto-merged)
Team moralePainfulInvisible
Capture of “what good looks like”In senior engineers’ headsIn repo as golden principles

vs. doc-gardening (the docs sibling)

OpenAI runs a parallel pattern for documentation. A “doc-gardening” agent scans for stale or obsolete docs that no longer reflect the real code behaviour and opens fix-up PRs. Same mechanic, different target.

This pairs well with Agent Legibility: GC fixes drifted code; doc-gardening fixes drifted documentation. Together they keep the legibility envelope clean.

What goes into a golden principle

A useful golden principle has four properties:

  1. Mechanical: a lint or structural test can detect violations. “Be consistent” is not mechanical; “no file over 500 lines” is.
  2. Local: a violation can be fixed without understanding the whole system. “Use Zod at the boundary” is local; “rearchitect this domain” is not.
  3. Cheap to fix: most violations should auto-merge after the fix. Anything that needs human judgment to resolve should not be a golden principle.
  4. Stable: the rule should not change weekly. Rules that flip create churn and confuse the agent.

Examples that fit:

  • File size limits.
  • Naming conventions for schemas, types, modules.
  • Layer dependency rules (see Layered Domain Architecture for AI Agents).
  • Structured logging formats.
  • Required test coverage on critical paths.
  • “Use this shared helper, not a hand-roll.”

Examples that don’t fit:

  • “Write clean code” (not mechanical).
  • “Improve this domain’s design” (not local, not cheap).
  • “Use the latest framework version” (unstable, also not always desirable).

Custom lints with remediation messages

A specific technique that pairs naturally with GC: when a custom lint fires, write the error message for the agent. Tell it what to do instead, not just that it was wrong.

“Because the lints are custom, we write the error messages to inject remediation instructions into agent context.”

Standard lint message: "file too large".
Agent-targeted lint message: "file exceeds 500 lines. Split into separate modules along domain boundaries: (a) types, (b) repo, (c) service. See AGENTS.md > File Size for the rationale."

The remediation message becomes part of the agent’s loop. When the agent fixes its own lint failure, it has the instructions in context. See Agent-Computer Interface (ACI) for the broader principle.

When this becomes critical

Two thresholds:

  1. Throughput threshold. Once agent throughput exceeds human review capacity, manual GC is impossible. OpenAI hit this in weeks, not months. For most teams the threshold is lower than expected.
  2. Codebase size threshold. Past a certain LOC count, drift becomes invisible to any single human. GC agents see the whole codebase consistently; humans cannot.

If your team is below both thresholds, golden principles + GC is overkill. If you are above either, it is a prerequisite.

Risks and limits

  • Over-mechanisation. A golden principle that encodes a bad rule will produce drift in the wrong direction at scale. Bad rules become bad code on every line.
  • GC agents can be wrong. They open PRs that look right but break edge cases. Tests must be strong enough to catch this; the Layered Domain Architecture for AI Agents is partly there to make these PRs verifiable.
  • The “easy auto-merge” feedback loop can normalise low-attention review. Teams should keep at least one critical reviewer in the loop for any GC PR that touches public surface area or security-sensitive code.

Application to regulated industries

For payments and banking specifically, GC agents are a strong fit because:

  • Audit demands the rules be written down anyway. Golden principles double as compliance documentation.
  • Drift is dangerous. A payment system that quietly accumulates two ways to handle currency rounding is not just untidy; it is a financial-correctness risk.
  • Continuous small PRs are easier to review and approve under change-management discipline than large quarterly refactors.

The merge-philosophy lesson from Harness Engineering at OpenAI (minimal merge gates) does not transfer. But the GC lesson does, possibly with stricter human review on each GC PR.

Open Questions

  • What is the minimum set of golden principles to start with? Three? Ten? Different for different stacks?
  • How do you sunset a golden principle gracefully? Once it is in CI, removing it is a coordinated change.
  • For monorepos with multiple domains, do you have one set of principles or per-domain sets?
  • How aggressive should auto-merge be on GC PRs? Always-on, conditional on file paths, or always-human-touched?