BitFun icon indicating copy to clipboard operation
BitFun copied to clipboard

[BUG] Edit tool: token amplification and hang when editing large files with stale read-state cache

Open 1688mengdie opened this issue 4 months ago • 1 comments

Summary

Edit tool causes massive token amplification (957K input for a 1185-line file) and hangs (1m23s instead of <5s) when:

  • Editing large files (1000+ lines)
  • Read-state cache is stale (e.g., consecutive Edits to same file in one round)
  • old_string is moderately long

Root Cause

Combination of two upstream commits:

1. f8a32ce6 — kev1n77 — "fix(edit): add whitespace-normalization candidates and fix CRLF blind spot"

Added convert_tabs_to_spaces() and convert_leading_spaces_to_tabs() to edit_string_candidates() in src/crates/execution/tool-execution/src/fs/edit_file.rs. For each tab_width in [2,4], generates up to 4 additional candidate (old,new) pairs via tab↔space conversion. Each candidate calls find_actual_string(content, candidate) which does full-file character-by-character scan with quote normalization.

On a 1185-line file, this multiplies the scan cost significantly.

2. e5024fdd — limityan — "refactor(core): move file tool result facts into execution"

Moved read-state validation from assembly layer to portable execution layer (src/crates/execution/agent-runtime/src/file_read_state.rs). Also restructured validate_input in FileEditTool to include the dry-run (read_current_file_content + apply_edit_to_content). The dry-run happens in validate_input before the tool is even executed, meaning the expensive candidate generation runs on the validation path.

Combined Effect

In the original flow (before these commits), when read-state cache is stale:

  1. validate_input fails fast with "Use Read to load..."
  2. AI receives error → calls Read → retries Edit
  3. Total context: ~200K tokens, <5s

With both commits combined:

  1. validate_input still fails fast (read-state check is first)
  2. But when AI retries with Read+Edit, the file content gets packed into context multiple times due to changed read-state tracking in portable layer
  3. Each full-file scan during dry-run triggers whitespace-normalization candidates, amplifying token usage
  4. Result: 957K input tokens, 1m23s wall time, only 6K output

Evidence

Tool: Edit
Duration: 1m23s (normal: <5s)
Input tokens: 957K (file loaded ~3-4x into context)
Output tokens: 6K (actual edit operation is tiny)
File size: 1185 lines
StateStore Edit (same round): 94ms — normal

Same scenario on pre-refactor version: fast-fail → Read → re-Edit → <5s.

Suggested Fix

  1. Short-term: Move the dry-run (read_current_file_content + apply_edit_to_content) out of validate_input and into call_impl, after the read-state guardrail passes. The dry-run should not block the fast-fail path.

  2. Medium-term: Limit whitespace-normalization candidate generation in edit_string_candidates() for large files, or only run find_actual_string when exact match fails first.

  3. Alternative: Restore original fast-fail behavior — skip the dry-run in validate_input entirely when read-state is known to be stale.

Affected Files

  • src/crates/execution/tool-execution/src/fs/edit_file.rs — whitespace candidates (f8a32ce6)
  • src/crates/execution/agent-runtime/src/file_read_state.rs — read-state logic (e5024fdd)
  • src/crates/assembly/core/src/agentic/tools/implementations/file_edit_tool.rs — validate_input dry-run (e5024fdd)
  • src/crates/assembly/core/src/agentic/tools/file_read_state_runtime.rs — delegation to portable layer (e5024fdd)

1688mengdie avatar Jul 20 '26 23:07 1688mengdie

Fixed in #1692

1688mengdie avatar Jul 22 '26 20:07 1688mengdie