Skip to content

fix(tools): count StrReplaceFile replacements against running content - #2554

Open
ayaangazali wants to merge 1 commit into
MoonshotAI:mainfrom
ayaangazali:fix-strreplace-count
Open

fix(tools): count StrReplaceFile replacements against running content#2554
ayaangazali wants to merge 1 commit into
MoonshotAI:mainfrom
ayaangazali:fix-strreplace-count

Conversation

@ayaangazali

@ayaangazali ayaangazali commented Jul 23, 2026

Copy link
Copy Markdown

Related Issue

No open issue for this one, I hit it while reading StrReplaceFile. It is a small self-contained correctness fix (well under the 100 LOC discussion threshold), happy to close if you would rather have an issue first.

Description

The success message for StrReplaceFile counted replacements by summing original_content.count(edit.old) for each edit. But edits apply sequentially, so a later edit runs against the output of the earlier ones, not the original file. When edits interact, the reported total is wrong.

Example: file foo bar foo, two replace_all edits foo -> "foo bar" then bar -> "baz":

  • foo -> "foo bar" replaces 2, giving foo bar bar foo bar
  • bar -> "baz" then replaces 3, giving foo baz baz foo baz
  • actual total 5, but the message reported 3 (2 + 1 counted against the original)

The file edit itself was always correct; only the reported count was off. Fix counts each replacement as it actually happens by having _apply_edit return the count alongside the new content, which also lets the redundant second pass over the edits go away.

Single-edit cases are unchanged (a replace_all edit still reports its occurrence count, a single edit reports 1).

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked the related issue, if any.
  • I have added tests that prove my fix is effective or that my feature works. (two tests in test_str_replace_file.py: a single-edit count check and the interacting-edits case above, which reported 3 before)
  • I have run make gen-changelog to update the changelog. (hand-edited CHANGELOG.md in the same style, I do not have the Kimi API setup the skill needs)
  • I have run make gen-docs to update the user documentation. (no user docs describe this message, nothing to regenerate)

quick note: freshman here, found this reading the edit tool and figured a wrong count could quietly mislead. kept the change tiny and consulted claude code on the counting semantics to be sure str.count lines up with str.replace. if you would rather not touch it, no worries at all, still learning what is worth a PR :)


Open in Devin Review

The success message summed occurrences of each edit's old string in the
original file, but edits apply sequentially so a later edit operates on
the output of earlier ones. When edits interact the reported total was
wrong (e.g. two replace_all edits on "foo bar foo" reported 3 instead
of 5). Count each replacement as it actually happens by returning the
count from _apply_edit, which also removes the now-redundant second
pass over the edits.
Copilot AI review requested due to automatic review settings July 23, 2026 23:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

@ayaangazali

Copy link
Copy Markdown
Author

Status update on this one.

What it fixes: StrReplaceFile counts replacements against the original file content while the edits actually apply sequentially, so when a later edit matches text an earlier edit produced, the reported total is wrong. The file is written correctly, only the number in the result message is off.

Worth noting #2526 was opened after this PR by someone who hit the same thing independently, so it is not purely theoretical. Their repro (hello -> goodbye -> farewell reporting 1 instead of 2) is a cleaner example than mine.

Branch is current with main (main has not moved since 4a550ef, nothing to rebase onto) and still merges clean. make check-kimi-cli and make test-kimi-cli passed locally.

Same note as my other PRs: the checks here show as action_required rather than passing or failing, which I think is the first time contributor approval gate, so they have not run yet.

Glad to change the approach if you would rather solve it a different way.

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.

2 participants