Skip to content

< CollectionsRalph · 2 of 4 ·114 · Level 05, Agents

Work a Ralph Loop Through Its Plan File

medium · implement · Agents

A Ralph loop keeps its plan in a file: a markdown checklist like fix_plan.md. Implement ralph_plan(plan, agent, run_tests, max_iterations=10, max_failures=3), which works through the checklist one item per iteration and returns (plan, log): the updated text, and one (item, outcome) per iteration.

An item is a line - [ ] text (unchecked) or - [x] text (checked, x or X), with any indentation before the dash. Its text is what follows the box, stripped. No other line is an item.

  1. Pick the first unchecked item in the file, at any depth, that has not been given up on. With none left, stop.
  2. Work. agent(item, notes) returns a dict with a "note", and maybe "added" (new item texts) and "done" (items it says it finished). Append the note to notes, one list for the whole run.
  3. Test. run_tests() returns (passed, output). Passed: check the item (its [ ] becomes [x]) and log "done". Failed: leave it, append output to notes, and log "failed".
  4. Give up on an item at its max_failures-th failure: log "gave up" instead of "failed", leave it unchecked, and skip that line for the rest of the run (an item with the same text elsewhere is still picked).
  5. Added items go in as - [ ] text lines right below the picked item, at its indentation, in order, whether or not the tests passed. They are picked like any other.
  6. Stop after max_iterations iterations.

The catch. The file is a person's plan, so the loop changes the one box it means to and nothing else: other lines, indentation, \r\n endings and a missing final newline come back byte for byte. And the agent's "done" is only a claim: each iteration checks one item, the one picked, and only when the tests pass.