ProgramDiff

Helpers & Utilities · AiDotNet.Evolution.Programs

Parses SEARCH/REPLACE edit blocks from a model response and applies them to a program.

For Beginners

Large language models edit code by sending small “find this, replace it with that” instructions rather than rewriting whole files. This class reads those instructions out of the model’s answer and carries them out. Crucially, when an instruction cannot be carried out — usually because the text it wants to find is not in the file — you are told which one failed and why, so you can ask the model to try again with that feedback instead of silently evolving nothing.

How It Works

The block format is the one the reference OpenEvolve implementation emits: a line containing <<<<<<< SEARCH, the text to find, a line of equals signs, the replacement text, and a line containing >>>>>>> REPLACE. Upstream that format is recognized with a single regular expression that hard-codes bare line feeds, so a response with Windows line endings, an indented marker, or a trailing space after a marker matches nothing and the whole iteration is reported as “no valid diffs found”. This parser is line based and tolerant of all three, while keeping the marker text configurable through ProgramDiffOptions.

The second upstream weakness is silence on failure: apply_diff looks for the first exact line-window match and, when there is none, moves to the next block without recording anything, so an iteration can produce a child identical to its parent and still consume evaluator budget. Here every rejected block becomes a typed ProgramDiffFailure naming the block and the reason, an empty replacement really deletes its lines instead of leaving a blank one behind, an unchanged result is reported as a failure by default, and CreateUnifiedDiff renders exactly what an application changed.

Methods

Method Summary
ProgramDiffApplyResult Apply(string source, IReadOnlyList<ProgramDiffBlock> blocks, ProgramEvolutionOptions options) Applies parsed edit blocks to a program.
ProgramDiffApplyResult ApplyResponse(string source, string response, ProgramEvolutionOptions options) Parses a model response and applies every block it contains in one step.
ProgramDiffParseResult Parse(string response, ProgramDiffOptions options) Parses every SEARCH/REPLACE block in a model response.
ProgramDiffTargetSplit SplitByTarget(IReadOnlyList<ProgramDiffBlock> blocks, string source, string changesDescription, ProgramEvolutionOptions options) Routes edit blocks to the program or to the changes description by where their SEARCH text occurs.
string CreateUnifiedDiff(string original, string modified, int contextLines) Renders the difference between two program texts as a unified diff.
string FormatSummary(IReadOnlyList<ProgramDiffBlock> blocks, int maxLineLength, int maxLines) Renders a human-readable summary of a set of edit blocks.