Skip to content

[FEATURE] Foreman edit-fidelity: actionable feedback when str_replace recovery exhausts with no unique anchor #944

Description

@Defilan

Feature Description

When str_replace fails with zero matches AND anchorContext cannot find a unique anchor line, the model currently gets the bare error str_replace: old_string found 0 times in <path>, want 1 with no actionable information. Add a best-effort feedback fallback for this corner so the model always receives some real file content to re-anchor on.

Problem Statement

The recovery ladder (exact -> whitespace-normalized -> fuzzy unique-window -> anchorContext truth hint) has one dead end: on large files where the drifted old_string has no line that appears verbatim-trimmed exactly once, anchorContext returns nothing and the error carries zero guidance.

Observed live: a coder run drifted old_string against a ~2000-line file three times in a row, received the bare count error each time (no anchor existed for its paraphrased lines), and was terminated by EditFreeStreak. The fuzzy layer correctly refused to guess (the drift was beyond threshold), but the model had no path back to truth.

As a coder-harness operator, I want str_replace failures to always include some real file context so that a local model can re-copy actual bytes instead of re-hallucinating old_string until the stuck-loop detector kills the run.

Proposed Solution

When the recovery ladder is exhausted and anchorContext finds no unique anchor, fall back to a best-effort locator instead of the bare count error, for example:

  • Score every file line against the most distinctive old_string line with the existing boundedLevenshtein helper and return the surrounding real lines of the single lowest-distance hit (even if not unique-verbatim), clearly labeled as approximate.
  • Alternatively (or additionally), tell the model the file's line count and suggest a read_file of a specific line range near the closest match.

Safety posture unchanged: this is feedback only, never an auto-apply. The unique-window rule for automatic edits stays as is.

Alternatives Considered

  • Read-before-edit enforcement (tool-level: reject str_replace unless the file was read this task). Deferred from the original edit-fidelity work; heavier UX change for all models.
  • Raising fuzzy thresholds so more drift auto-applies. Rejected: threshold 6 is already the documented safety ceiling; loosening it trades wrong-target edits for convergence.

Additional Context

Follow-up from the edit-fidelity work (#942 / PR #943), which added the fuzzy recovery and write_file steering; this covers the remaining no-anchor dead end observed during its live fleet validation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions