Reverse Ralph Loop

James Phoenix
James Phoenix

Use the Ralph Loop pattern to reverse-engineer existing software from public resources into clean-room specifications, then regenerate a functionally equivalent implementation.

Origin: Geoffrey Huntley | Related: The RALPH Loop | Date: 2025


Core Concept

The Reverse Ralph Loop inverts the standard Ralph Loop. Instead of building new software from a spec, it starts with an existing product and extracts clean-room specifications from publicly available resources. Those specs then feed into a regular Ralph Loop to produce a new implementation with zero copied code.

┌─────────────────────────────────────────────────────────────┐
│  Standard Ralph:    Spec → Code                              │
│  Reverse Ralph:     Existing Product → Spec → New Code       │
└─────────────────────────────────────────────────────────────┘

This is clean-room development powered by LLMs. The generated implementation is functionally equivalent but legally and technically independent of the original.


The Process

Phase 1: Source Collection

Gather publicly available materials about the target product:

Source Type Examples
Source code Open-source repos, decompiled binaries, objdumps
Documentation Product guides, API docs, man pages
Marketing material Feature lists, comparison pages, blog posts
Runtime artifacts Logs, TCP dumps, intercepted API calls, CLI output

The key constraint: only use publicly accessible information.

Phase 2: Specification Extraction

Run the Ralph Loop over collected materials to generate clean-room specs. The agent reads source material, documents how features work, and produces implementation-independent specifications.

┌─────────────────────────────────────────────────────────────┐
│  1. Read source material (code, docs, marketing)             │
│  2. Document ONE feature's behavior                          │
│  3. Write implementation-independent specification           │
│  4. Update scratchpad with remaining features                │
│  5. SPAWN NEW AGENT → Repeat                                 │
└─────────────────────────────────────────────────────────────┘

The specs describe what the software does, not how the original implements it.

Phase 3: Implementation

Feed the extracted specifications into a standard Ralph Loop. Each iteration picks the highest-priority spec and implements it from scratch, with tests, in the target language/framework.

Phase 4: Verification

Compare the new implementation against the original’s observable behavior. Run the original’s test suites (if public), intercept API responses for comparison, or validate against documentation.


Coordination: How Does the Agent Know What Order to Work In?

This is the most common question. The answer: the agent picks, and it usually picks well.

The Reverse Ralph Loop operates as an agentic greedy algorithm. At each iteration, the agent looks at what’s been documented, what remains, and selects the most important next step. Think of navigating from point A to point B: take a step, look around, decide the next step, repeat.

When it gets stuck or loops on one feature, you intervene:

Problem Fix
Looping on one feature Add guardrails: “move on after documenting, don’t implement yet”
Wrong dependency order Seed the scratchpad with a suggested ordering
Missed discovery Feed additional sources (logs, TCP dumps, runtime traces)
Kinematics vs physics problem* Structure phases explicitly with separate passes

*From the Discord thread: discovering that a game action is physics-driven after you’ve already modeled it as kinematic. The fix is separating exploration/discovery from implementation. Do a full exploration pass before committing to implementation.

Phase Separation for Complex Targets

For non-trivial reverse engineering, run distinct passes:

  1. Exploration pass – Survey the entire codebase/product surface. Document what exists.
  2. Discovery pass – Understand dependencies, architectures, hidden behaviors.
  3. Specification pass – Write clean-room specs from discoveries.
  4. Implementation pass – Standard Ralph Loop against the specs.

Each pass is its own Ralph Loop. The agent exhausts one phase when the scratchpad has no remaining items for that phase.


Demonstrated Use Cases

HashiCorp Nomad Clone (Geoffrey Huntley)

Huntley used the Reverse Ralph Loop to clone HashiCorp Nomad, which was under the BSL license:

Udemy Bestseller

Learn Prompt Engineering

My O'Reilly book adapted for hands-on learning. Build production-ready prompts with practical exercises.

4.5/5 rating
306,000+ learners
View Course
  1. Extracted public source code
  2. Ran the Ralph Loop over product documentation to fill in missing enterprise features
  3. Generated a new, clean implementation matching original behavior

Z80 Spectrum Technique (Geoffrey Huntley)

Converted C code to assembly to specifications, then generated a working Z/80 Spectrum tape. The same technique was applied to reveal the source code architecture of Atlassian’s Rovo AI agent by running it against the Atlassian Command Line Interface.

Daniel Joyce used the same technique to port ls to Rust via an objdump (ls-rs on GitHub).

ExpressJS Double Clean-Room (Ryan Allen)

Reverse-ralphed ExpressJS with Opus running 5 parallel Ralph Loops. Achieved 93% test coverage on first pass. “Double clean-room” because both the specs and the implementation were generated independently.

Legacy REST API (Ryan Allen)

Reverse-engineered a legacy REST API by intercepting calls in-flight. Fed the captured request/response pairs as source material for specification extraction.

Tutorial Book Generation (orange_runner)

An adaptation of Reverse Ralph for learning material:

  1. Build outline and table of contents for a topic
  2. Ralph Loop researches and writes each chapter from internet sources
  3. Backpressure via compiled code examples and formula verification
  4. Result: ~25k lines of markdown on a navigation software topic with no existing study material

Seeding Quality

The quality of output improves significantly when you seed the first iterations with structure:

  • Outline document – Define what sections/modules the specs should cover
  • Scratchpad – List what to explore in future iterations
  • Style guide – Specify the level of detail, format, and what matters (e.g., O’Reilly tech book guidelines)
  • Expert personas – Use sub-agents with different specializations to evaluate materials, then resolve contradictions

Backpressure Mechanisms

Without verification, reverse-ralphed specs drift from reality. Apply backpressure:

Mechanism Purpose
Compiled code examples Verify specs produce working code
Formula verification Cross-check calculations from multiple sources
Test suite comparison Run original tests against new implementation
Multi-expert review Different agent personas evaluate from different angles
Runtime comparison Compare behavior of original vs new implementation

Licensing Implications

The Reverse Ralph Loop enables clean-room reimplementation of products under restrictive licenses (BSL, proprietary). The generated code has no lineage to the original codebase. This mirrors traditional clean-room reverse engineering practices used in the hardware industry for decades, now accelerated by LLMs.

This is not legal advice. The technique produces independent implementations, but consult legal counsel for specific cases.


The Prompt Pattern

A minimal reverse-ralph prompt (adapted from Clayton Farr’s ralph-playbook PR):

Read outline, read scratchpad, read specs.
Your goal is to document how this code works so it can be
implemented from scratch using the specifications.
Select ONE thing to explore and document the findings.
Use sub-agents to perform additional research as needed.
Update documents with findings.
Update scratchpad.md with things to work on in future iterations.
Write the changes log into log.md.
IMPORTANT: work on just ONE task.

When the docs grow large, organize them into sub-modules. The key is constraining each iteration to a single task.


Key Insight

The Reverse Ralph Loop is clean-room reverse engineering at LLM speed. The agent reads public materials, writes specs, and builds from those specs. No code is copied. The result is a functionally equivalent, legally independent implementation.


Related


External References

Topics
Clean Room DevelopmentLlm UsageRalph LoopReverse EngineeringSoftware Specification

More Insights

Cover Image for Autonomous Loops Need a Scoring Function

Autonomous Loops Need a Scoring Function

Without a benchmark, a RALPH loop is a chaos engine. With one, it becomes automated hill climbing. The difference is not the loop. It is the objective.

James Phoenix
James Phoenix
Cover Image for Skill Graphs: Networked Knowledge Beats Monolithic Skill Files

Skill Graphs: Networked Knowledge Beats Monolithic Skill Files

A single skill file captures one capability. A skill graph captures an entire domain. The difference is whether your agent can follow instructions or reason through a field.

James Phoenix
James Phoenix