agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]: edit_file hangs indefinitely when old_string is empty

Open wangxuefen88 opened this issue 4 months ago • 3 comments

Calling edit_file with an empty old_string never returns.

Reproduction:

filesystem.edit(runtimeContext, "/notes.md", "", "replacement", false);

The loop in FilesystemUtils.countOccurrences() calls indexOf("", idx). Because the empty string has length 0, idx never advances and the loop runs forever.

Expected behavior: Return a normal validation error such as: "old_string must not be empty"

Suggested fix: Validate oldString before countOccurrences() and add a unit test with a timeout assertion.

This should be a small backward-compatible fix with no API change.

wangxuefen88 avatar Jul 28 '26 05:07 wangxuefen88

🤖 Bot Assessment — Bug Confirmed ✅

Root cause verified in code. FilesystemUtils.countOccurrences() (line 90-96) enters an infinite loop when oldString is empty:

while ((idx = text.indexOf(sub, idx)) >= 0) {
    count++;
    idx += sub.length(); // sub.length() == 0 → idx never advances
}

String.indexOf("", idx) always returns idx (≥ 0), and idx += 0 means the loop never terminates.

Suggested fix aligns with the issue description — add an early validation in performStringReplacement() or the calling edit() method:

if (oldString == null || oldString.isEmpty()) {
    return new Object[]{"Error: old_string must not be empty"};
}

A unit test with a timeout assertion would prevent regression. This is a small, backward-compatible fix.

oss-maintainer avatar Jul 28 '26 05:07 oss-maintainer

Thanks for confirming the issue. I'd like to work on this fix.

I will add the validation in FilesystemUtils.performStringReplacement() so it covers both local and remote filesystem implementations, together with a focused regression test. No API changes will be introduced.

wangxuefen88 avatar Jul 28 '26 05:07 wangxuefen88

我也发现了,一个空文件,如果想要追加内容,直接阻塞了

tangdong520 avatar Jul 31 '26 01:07 tangdong520