Skip to content

fix(tui): actionable File edit diagnostics and stale-line-number tolerance (#5003) - #5008

Merged
Hmbown merged 5 commits into
Hmbown:mainfrom
SparkofSpike:fix/file-edit-reliability-5003
Aug 1, 2026
Merged

fix(tui): actionable File edit diagnostics and stale-line-number tolerance (#5003)#5008
Hmbown merged 5 commits into
Hmbown:mainfrom
SparkofSpike:fix/file-edit-reliability-5003

Conversation

@SparkofSpike

@SparkofSpike SparkofSpike commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #5003. The report described a model repeatedly failing to apply large
(100+ line) replacements to a C file with Chinese comments and CRLF line
endings: 15+ failed File tool attempts, 3 full git checkout rollbacks,
and an eventual workaround via an external Python script. All four failure
modes are addressed so the tools either succeed or fail with diagnostics the
model can act on.

Changes

  • edit_file: search == replace now sizes the payload and offers recovery
    the error reports character/line counts, names the root cause (replace
    copied verbatim from search), and recommends apply_patch for large
    multi-line rewrites, so the model self-corrects instead of retrying the
    same no-op call.
  • edit_file: Search string not found previews the search text — the
    error shows the first 3 lines of the search payload (80 chars per line), so
    the model can compare what it searched for against the file's contents.
  • edit_file: C/C++ preprocessor conditional balance check — a replace
    must preserve the net #if/#ifdef/#ifndef vs #endif balance of the
    search span, catching the truncated #if 0 edit that previously applied
    with success and orphaned an #endif. Balanced local edits (e.g. a one-line
    #if 0#if 1 toggle) are unaffected.
  • apply_patch: stale-line-number relocation — when positional fuzz fails
    and the hunk carries at least MIN_ANCHOR_LINES (4) expected lines, the
    hunk is relocated to a unique whole-file context match; ambiguous context
    (multiple matches) is refused with the candidate lines listed instead of
    guessing.
  • apply_patch: clearer failure hints and relocation reportingNoMatch
    now mentions that line numbers may be stale after earlier edits and
    suggests re-reading the file; relocated hunks are reported in
    PatchResult/FileSummary and the summary message.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Tests

Testing

  • cargo fmt — clean (no diffs)
  • cargo clippy -p codewhale-tui --bin codewhale-tui --locked — no new warnings from this change (5 pre-existing warnings live in crates/tui/src/tui/views/fleet_setup.rs, untouched)
  • cargo test -p codewhale-tui --bin codewhale-tui --locked tools:: — 1374 passed, 0 failed (includes 6 new tests: relocation, ambiguous context, short-anchor no-relocation, and an end-to-end CRLF + Chinese-comment scenario)
  • Manual verification on the rebuilt binary — all six issue scenarios pass:
    search==replace diagnostics, Chinese+CRLF replacement, #if truncation
    rejection, search preview, stale-line relocation, ambiguous-context refusal

Note: the full --workspace gate was not run in this session; the focused
codewhale-tui tool gates above were executed and passed.

Checklist

Related Issues

Closes #5003

…rance (Hmbown#5003)

edit_file:
- search==replace now sizes the payload and offers recovery guidance so
  the model self-corrects instead of retrying the same no-op call.
- "Search string not found" previews the first lines of the search text
  so the model can compare it against the file's actual contents.
- refuse payloads that would change the C/C++ preprocessor conditional
  balance (#if/#ifdef/#ifndef vs #endif), catching the #if 0 orphan-#endif
  corruption that previously applied with success.

apply_patch:
- when positional fuzz fails and the hunk carries enough anchor lines
  (>= MIN_ANCHOR_LINES), relocate to a unique whole-file context match,
  covering stale line numbers after earlier edits; ambiguous context is
  refused with the candidate lines listed instead of guessing.
- NoMatch hints mention stale line numbers; relocation is reported in
  PatchResult/FileSummary and the summary message.

Verified: cargo fmt clean; clippy adds no warnings; 1374 tools:: tests
pass, including new relocation/ambiguity unit+integration tests and an
end-to-end CRLF + Chinese-comment scenario.
@SparkofSpike
SparkofSpike force-pushed the fix/file-edit-reliability-5003 branch from db909e5 to 7698265 Compare July 31, 2026 16:12
event-listener 5.4.1 has an unsound StackSlot Send/Sync advisory. Bumping
it in this PR would change Cargo.lock's hash, which misses the rust-cache
seeded by main and breaks the offline runtime-contract-budget gate
(assert-json-diff download failure). Keep Cargo.lock byte-identical to
main and track the dependency bump as a separate PR instead.
@SparkofSpike
SparkofSpike force-pushed the fix/file-edit-reliability-5003 branch from 7698265 to 3554d9c Compare July 31, 2026 16:24
@SparkofSpike

Copy link
Copy Markdown
Contributor Author

PTAL @Hmbown

the Lint check only fails at check-runtime-contract-budget with failed to download assert-json-diff v2.0.2 under --offline. This is a fork-PR limitation, not a code issue: fork PRs run with a read-only token (GITHUB_TOKEN Permissions: Contents: read), so Swatinem/rust-cache restores nothing (No cache found), while the checker forces Cargo offline. The identical cache key on main restores fine and the same step passes on main (run 30632266911). Other fork PRs (e.g. #5006) hit the same failure. The branch's Cargo.lock is byte-identical to main, so the merged result should be green on main CI.

Could you take a look when you have a moment? Happy to adjust if anything comes up.


@SparkofSpike
SparkofSpike marked this pull request as ready for review July 31, 2026 16:52
@SparkofSpike
SparkofSpike requested a review from Hmbown as a code owner July 31, 2026 16:52
Hmbown and others added 3 commits July 31, 2026 21:38
@Hmbown
Hmbown merged commit d683aca into Hmbown:main Aug 1, 2026
18 checks passed
@SparkofSpike
SparkofSpike deleted the fix/file-edit-reliability-5003 branch August 1, 2026 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

针对中长文本的一段write功能出现严重反复。详见transcript

2 participants