Work a Ralph Loop Through Its Plan File
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.
- Pick the first unchecked item in the file, at any depth, that has not been given up on. With none left, stop.
- 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 tonotes, one list for the whole run. - Test.
run_tests()returns(passed, output). Passed: check the item (its[ ]becomes[x]) and log"done". Failed: leave it, appendoutputtonotes, and log"failed". - 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). - Added items go in as
- [ ] textlines right below the picked item, at its indentation, in order, whether or not the tests passed. They are picked like any other. - Stop after
max_iterationsiterations.
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.