Skip to content

fix(edit): show best matching region on mismatch instead of raw file head#86168

Closed
imwyvern wants to merge 1 commit into
openclaw:mainfrom
imwyvern:fix/edit-fuzzy-match-hint
Closed

fix(edit): show best matching region on mismatch instead of raw file head#86168
imwyvern wants to merge 1 commit into
openclaw:mainfrom
imwyvern:fix/edit-fuzzy-match-hint

Conversation

@imwyvern

Copy link
Copy Markdown
Contributor

Problem

When edit oldText doesn't match the file contents exactly, the current error hint shows the first 800 characters of the file regardless of where the closest match might be. For large files this is rarely helpful — the model sees the file header and has to guess where its oldText diverged.

Solution

Replace the raw file-head dump with a best-matching region finder that:

  1. Slides a line-based window (same height as oldText) across the file
  2. Scores each window by counting shared leading lines with oldText
  3. Returns the highest-scoring region with line numbers and ±2 lines of context

When no region scores above zero (totally unrelated content), the behavior falls back to the existing full-file-head hint.

Before:

Could not find the exact text in config.ts.
Current file contents:
import foo from 'bar';
// ... first 800 chars of a 2000-line file

After:

Could not find the exact text in config.ts.
Best matching region (lines 142+):
140 |   const timeout = 30_000;
141 |   const retries = 3;
142 |   const endpoint = 'https://api.example.com';
143 |   const headers = { Authorization: token };
144 |   const body = JSON.stringify(payload);
145 |   // END config block

Changes

  • src/agents/pi-tools.host-edit.ts — Add findBestMatchRegion() + countSharedLines(); update appendMismatchHint() to prefer region-based hints
  • src/agents/pi-tools.read.host-edit-recovery.test.ts — Add test for best-match region behavior

Tests

All 18 existing + new host-edit-recovery tests pass.

Supersedes #56857 and #42265 (combined approach, fresh implementation on current main).

…head

When edit oldText doesn't match exactly, the error hint now locates and
displays the most similar region in the file (with line numbers) instead
of dumping the first 800 characters. This helps the model quickly spot
which lines diverged from its expectation.

Falls back to the current full-file-head behavior when no region scores
above zero shared lines.

Combines the intent of openclaw#56857 and openclaw#42265.
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 24, 2026
@clawsweeper

clawsweeper Bot commented May 24, 2026

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Close as superseded: #88531 is the current open replacement for the same closest-match edit hint, while this branch targets the removed src/agents/pi-tools.* path, is conflicting, lacks real behavior proof, and still has known correctness gaps.

Canonical path: Live GitHub search for the central closest-edit-match problem found this PR and the newer open replacement #88531; older related attempts such as #42265 are closed.

So I’m closing this here and keeping the remaining discussion on #88531 and #42265.

Review details

Best possible solution:

Close this stale branch and continue the closest-match edit hint work in #88531, which targets the current shared edit implementation.

Do we have a high-confidence way to reproduce the issue?

Yes. Source inspection of current main shows exact-match failures still append a bounded Current file contents: hint from src/agents/sessions/tools/edit.ts:165, while the closest-match behavior is only proposed in PR branches.

Is this the best way to solve the issue?

No. This stale branch is not the best way now because it edits removed pi-tools files and has correctness gaps; the better path is the newer shared edit-diff.ts implementation in #88531.

Security review:

Security review cleared: The diff only changes edit-tool error hint logic and a focused test, with no dependency, workflow, secret, package, or code-execution surface change.

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • @vincentkoc: Current main blame and history show the sessions-tool edit path was added/moved in commit fc6d448, which now owns the code surface that a replacement PR must touch. (role: recent area contributor; confidence: high; commits: fc6d448138fc; files: src/agents/sessions/tools/edit-diff.ts, src/agents/sessions/tools/edit.ts, src/agents/sessions/tools/edit.test.ts)
  • @steipete: History on the old pi-tools edit path shows several central edits, including splitting host-edit wrappers and adding edits[] payload support before this PR targeted that path. (role: feature-history contributor; confidence: high; commits: d380ed710df1, 693d17c4a2ae, ce1cd26fbcb0; files: src/agents/pi-tools.host-edit.ts, src/agents/pi-tools.read.host-edit-recovery.test.ts)
  • @mbelinky: The main edit recovery hardening commit modified the same old host-edit recovery path and tests that this PR attempted to extend. (role: edit recovery contributor; confidence: high; commits: 922f4e66ea1a; files: src/agents/pi-tools.host-edit.ts, src/agents/pi-tools.read.host-edit-recovery.test.ts, src/agents/pi-tools.read.ts)
  • @stainlu: Recent history shows host path-resolution work in the same old edit recovery files, making this a plausible adjacent review route for edit-target behavior. (role: recent adjacent contributor; confidence: medium; commits: ecfaf64526a7; files: src/agents/pi-tools.host-edit.ts, src/agents/pi-tools.read.host-edit-recovery.test.ts)

Codex review notes: model internal, reasoning high; reviewed against 44e6caff5401.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0c4d4f5ccc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if (snippet.length > maxLen) {
snippet = `${snippet.slice(0, maxLen)}\n... (truncated)`;
}
return { snippet, startLine: ctxBefore + 1 };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Report actual match line in mismatch hint

The line label in the new hint is off by up to two lines because startLine is derived from the context window (ctxBefore + 1) instead of the matched window start. When a match is found away from the file top, the message Best matching region (lines N+) points above the true match and can misdirect follow-up edits to the wrong section. Keep the context lines in the snippet, but report the match anchor (bestStart + 1).

Useful? React with 👍 / 👎.

Comment on lines +169 to +173

let bestScore = 0;
let bestStart = 0;

for (let i = 0; i <= contentLines.length - windowSize; i++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Score regions when oldText has more lines than file

The sliding-window loop never executes when oldText is longer than the current file (contentLines.length - windowSize < 0), so bestScore stays zero and the code falls back to dumping the file head even when the file shares a meaningful leading prefix with oldText. That drops the new “best matching region” behavior in a common mismatch case (extra lines in oldText), making recovery guidance much less useful.

Useful? React with 👍 / 👎.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels May 24, 2026
@clawsweeper

clawsweeper Bot commented May 24, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper PR egg

🎁 Pass real behavior proof to wake the egg and unlock a hatchable treat.

Where did the egg go?
  • The egg game starts only after the PR passes the real-behavior proof check.
  • Before that, no creature or rarity is rolled. The treat waits for real proof.
  • This is still just collectible flavor: proof affects review readiness, not creature quality.

@omarshahine

Copy link
Copy Markdown
Contributor

Thanks for the PR. Before this can move forward, please add live proof from the affected surface, not just unit tests, mocked tests, or source inspection.

A useful proof update should include:

  • the exact build/SHA tested
  • the real environment used
  • the command, UI flow, channel flow, provider request, or other live path exercised
  • before/after symptom evidence where applicable
  • the observed result after the patch
  • any remaining proof gaps

Please redact secrets, tokens, phone numbers, and private message content from logs or screenshots.

@clawsweeper clawsweeper Bot added the P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. label Jun 15, 2026
@openclaw-clownfish

Copy link
Copy Markdown
Contributor

Thanks @imwyvern for continuing the closest-match edit hint work here. I am closing this as superseded by #88531 because both PRs track the same edit oldText mismatch recovery path, and #88531 is the current open replacement on the shared edit implementation with supplied real behavior proof.

This branch still targets the older src/agents/pi-tools.host-edit.ts surface, is currently conflicting, lacks passing real behavior proof, and has unresolved review items, so Clownfish cannot safely land it as the active path. We will preserve credit and attribution from this source PR in the canonical thread: #86168.

I am keeping the canonical path open at #88531 so validation and follow-up stay in one place. If this branch has a distinct reproduction path that is not covered there, please reply and we can reopen or split it back out.

@openclaw-clownfish openclaw-clownfish Bot added the clownfish Tracked by Clownfish automation label Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling clownfish Tracked by Clownfish automation P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants