针对中长文本的一段write功能出现严重反复。详见transcript
Bug Report
Description
codewhale 的 File 编辑工具(action=edit / action=patch)在执行"替换大段代码"时反复失败,失败模式缺乏可操作的诊断信息,导致模型在同一个文件上进行了 15+ 次失败尝试、3 次 git checkout 全量回滚,最终只能绕过工具、改用外部 Python 脚本(字符串/正则锚点)才完成写入。
Steps to reproduce
- 打开约 700 行、含大量中文注释、CRLF 行尾的 C 文件(复现时使用
_CODE_APP/_APP/app_foc.c)。 - 调用
File工具action="edit",search为精确复制的 40+ 行中文代码块(含#if 0、嵌套大括号),replace为新实现。 - 调用
File工具action="patch",使用带行号的 unified diff 替换 ~120 行核心段(如@@ -327,139 +327,78 @@)。 - 观察工具返回。
Expected behavior
edit应正确匹配并替换目标代码块,返回 diff;当search/replace参数异常时应给出能定位问题的诊断信息。patch应支持在行号偏移(前面已有其他编辑)后仍能定位上下文,或在失败时提示行号已过期、建议重新生成 patch。
Actual behavior
同一任务中依次出现 4 种失败模式:
| # | 报错 | 次数 | 细节 |
|---|---|---|---|
| 1 | Error: Invalid input for tool 'File': search and replace are identical, no change intended |
3 次(本任务内;整段会话共 10 次) | 长文本场景下模型把 replace 生成得与 search 完全相同。工具仅返回该错误,无任何差异提示或补救建议,模型随后反复犯同一错误,直到系统级告警 Tool calls have failed for 2 consecutive steps 才被迫换策略 |
| 2 | could not find matching context near line N(patch 上下文匹配失败) |
2 次 | 前序编辑使行号漂移(如 218 → 228),模型未重读文件核对行号即按旧行号重试;patch 无模糊定位能力 |
| 3 | Search string not found |
1 次 | 文件已被部分编辑破坏后,search 字符串与磁盘实际内容不一致,报错未提示差异位置 |
| 4 | 部分替换成功但破坏文件结构(仅替换 #if 0 行、遗留 #endif) |
1 次 | 工具返回成功,但产物是结构损坏的代码,模型只能全量回滚 |
后果链:成功的小改动因结构损坏被 git checkout 丢弃 → 重做 → 再次失败 → 循环 3 轮(turn 409/434/481 三次回滚)。期间还发生一次 write 目标路径错误(新代码写入 app_foc_new.c 而非目标文件)。最终在 turn 483-486 手工编写独立 Python 脚本 apply_all.py,用 data.replace() + re.subn() 字符串锚点原子替换才成功(Change 3: 1 replacements),全程未使用 File 工具完成目标替换。
Impact
- 频率:高。凡是对 100+ 行、含中文注释的 C 文件做大段替换,本任务 15+ 次 File 工具尝试中无一成功。
- 阻塞程度:严重。同一替换操作被反复重试,消耗大量 token 与时间;期间两次误删/损坏代码(
app_foc_new.c误写、扭矩节被按错误行号删除),靠git checkout兜底;每次回滚都会丢失已成功的小改动,放大重做成本。
Environment
- OS: Windows(工作区
E:\,PowerShell 执行环境) - codewhale version: 待补充(可用
codewhale --version查询;transcript 导出于 2026-07-31,session c8d4fc92) - Install method: 待补充
codewhale doctorsummary: 待补充- Model/provider: deepseek-v4-pro / DeepSeek
- Terminal app: 待补充
- Shell: PowerShell
Logs, screenshots, or recordings
- 完整失败序列:
E:\PROJECTS\20260709-TI-BOOTLOADER\写文件失败transcript-turn360-末尾.txt(5641 行,turn 360-497,UTF-8,含全部工具调用输入/输出与报错原文)。 - 关键调用索引(在该文件内搜索以下报错串可定位):
- turn 382、404、457:
search and replace are identical - turn 374、402:
could not find matching context - turn 406:
#if 0部分替换致结构损坏(工具返回 success) - turn 460:系统告警
Tool calls have failed for 2 consecutive steps - turn 483-486:
apply_all.py成功写入(对照组:字符串锚点方案一次成功)
- turn 382、404、457:
以上内容由deepseek 4 flash自动填写。该问题出自一次对业务代码的修订,恕不能提供完整源码。
Thanks @DracheTek for the report.
This issue is staying open for maintainer triage. CodeWhale gets better because people bring us real edge cases from real machines, providers, regions, and workflows.
If you can add a reproduction, logs, version output, screenshots, or the provider/model involved, that makes it much easier for us to verify and harvest the fix. Maintainers may comment /lgtmi to mark recurring issue reporters as approved so this intake note is skipped next time.
多谢反馈,我还以为是我自己的问题。好像所有含中文的文件都会静默失败。我先前排查问题排查到了一半,以为可能只是我的个例就暂时放下了。现在我立刻回去看看
Thank you for the feedback. I thought it was just my own issue. It seems that all files containing Chinese characters fail silently. I was troubleshooting halfway earlier and thought it might just be an isolated case on my end, so I put it aside for the time being. Now I'll go back and look into it right away.