Skip to content

prompt_diff

diapason.learning.spec_search.plan.prompt_diff

Prompt diff utilities for the plan phase.

Handles the PATCH_SYSTEM_PROMPT → REPLACE_SYSTEM_PROMPT downgrade logic. When the teacher proposes a PATCH edit, the planner checks if the diff would change more than 50% of lines. If so, it downgrades to a full REPLACE so the user sees the complete new prompt in the review queue.

See spec §6.3.

apply_unified_diff(original, diff)

Apply a unified diff to the original string.

Returns the patched string, or None if the diff cannot be applied. This is a simplified implementation that handles basic unified diffs.

changed_line_ratio(original, modified)

Compute the fraction of lines that differ between two strings.

Uses a simple line-by-line comparison. Returns 0.0 if both are empty, 1.0 if one is empty and the other is not.

maybe_downgrade_to_replace(edit, *, prompt_reader)

Downgrade PATCH_SYSTEM_PROMPT to REPLACE if the diff is large.

Parameters

edit : The edit to check. prompt_reader : A callable that takes a target string (e.g. "agents.simple.system_prompt") and returns the current prompt content.

Returns

Edit The edit unchanged when it is not a PATCH operation or the diff is small enough; otherwise, a new REPLACE edit when the diff changes more than 50% of the lines or cannot be applied.