Skip to content

fix(opencode): Improve "Read" tool output to aid subsequent uses of the "Edit" tool - #12030

Closed
albertov wants to merge 1 commit into
anomalyco:devfrom
albertov:dev
Closed

albertov wants to merge 1 commit into
anomalyco:devfrom
albertov:dev

Conversation

@albertov

@albertov albertov commented Feb 3, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR helps models (tested with Haiku 4.5) use the "Edit" tool more effectively with regards to leading whitespace after reading context given by the "Read" tool. Fixes #10656

How did you verify your code works?

I tasked agents with writing Haskell (white-space sensitive) and they improved considerably their success rate (to consistently perfect edits) when using the Edit tool as to not introduce leading-whitespace related errors

@github-actions

github-actions Bot commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

Hey! Your PR title Improve "Read" tool output to aid subsequent uses of the "Edit" tool doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@github-actions

github-actions Bot commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

@albertov albertov changed the title Improve "Read" tool output to aid subsequent uses of the "Edit" tool fix(opencode): Improve "Read" tool output to aid subsequent uses of the "Edit" tool Feb 3, 2026
@github-actions

github-actions Bot commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@albertov

albertov commented Feb 4, 2026

Copy link
Copy Markdown
Contributor Author

Below is the conclusion at which Haiku 4.5 at copilot-github analysed codebase to figure out.

Why are agents having so much trouble using the edit tool from opencode when dealing with whitespace and indentation?


The Root Problem

The edit tool has a critical design flaw in how it instructs agents to handle the line number prefix from the Read tool output.

The Documentation Issue

In packages/opencode/src/tool/edit.txt, the tool states:

When editing text from Read tool output, ensure you preserve the exact indentation 
(tabs/spaces) as it appears AFTER the line number prefix. The line number prefix 
format is: spaces + line number + tab. Everything after that tab is the actual 
file content to match. Never include any part of the line number prefix in the 
oldString or newString.

The problem: This instruction is extremely difficult for agents to follow because:

  1. Visual ambiguity: When you see 45 function foo(), it's nearly impossible to distinguish where the "line number prefix" (spaces + line number + tab) ends and the actual indentation begins, especially since tabs and spaces look identical visually.

  2. Fragile parsing: Agents must correctly parse the line number prefix format (spaces + number + tab), but different tools/systems may format the line output differently, making this brittle.

  3. Easy mistakes: An agent might accidentally:

    • Include leading spaces from the prefix in the oldString
    • Strip legitimate indentation from the beginning of the code line
    • Miscount spaces vs tabs

The Implementation Shows Why

Looking at the replace() function in packages/opencode/src/tool/edit.ts, there are 9 different "replacer" strategies used to try to fix agent mistakes:

for (const replacer of [
    SimpleReplacer,
    LineTrimmedReplacer,
    BlockAnchorReplacer,
    WhitespaceNormalizedReplacer,      // ← Handles whitespace issues
    IndentationFlexibleReplacer,       // ← Handles indentation issues
    EscapeNormalizedReplacer,
    TrimmedBoundaryReplacer,
    ContextAwareReplacer,
    MultiOccurrenceReplacer,
  ])

Even with these fallback strategies:

  • WhitespaceNormalizedReplacer collapses all whitespace to single spaces (/\s+/g, " ")
  • IndentationFlexibleReplacer strips common leading indentation

These exist precisely because agents struggle with whitespace/indentation. The tool was designed with these fallbacks as a band-aid, acknowledging the fundamental problem.

Why Agents Struggle

  1. Invisible characters: The line number prefix uses a TAB character (invisible, variable width)
  2. Context loss: The Read tool output format strips structural context
  3. Ambiguous instructions: "Everything after that tab" is ambiguous when tabs aren't rendered visually
  4. No validation feedback: An agent won't know they got it wrong until the edit fails

Solution

The edit tool documentation should be updated to provide explicit visual guidance, such as:

  • Show agents exactly what to copy (with a clear marker)
  • Provide examples with visual separators
  • Or, better yet: Have the Read tool strip the line number prefix before returning output to agents, so they never have to parse it manually

@albertov
albertov force-pushed the dev branch 4 times, most recently from bd87b2b to 360acdb Compare February 8, 2026 14:08
@Nico-Guyon

Nico-Guyon commented Feb 8, 2026 •

Copy link
Copy Markdown

I can confirm this PR fix the indentation problem I was having with python code / opencode 1.1.53 and the following models: devstrall 2 small, GLM 4.7 flash, Qwen3-coder-next

These indentation errors slow down a lot the agents and waste context space.
At some point some models like q3cn write a bash or python script in order to "manually" fix the indentation.

@albertov
albertov force-pushed the dev branch 3 times, most recently from be0b0ab to 16c1a73 Compare February 11, 2026 08:42
@superchangme

Copy link
Copy Markdown

I can confirm this PR fix the indentation problem I was having with python code / opencode 1.1.53 and the following models: devstrall 2 small, GLM 4.7 flash, Qwen3-coder-next

These indentation errors slow down a lot the agents and waste context space. At some point some models like q3cn write a bash or python script in order to "manually" fix the indentation.

Dear Nico, i met same problem, how did you resolve it? replace node_modules file?

@albertov

Copy link
Copy Markdown
Contributor Author

Latest changes in dev fix the issue so closing this

@albertov albertov closed this Feb 12, 2026
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.

There is a very high probability of encountering indentation bugs when using the Edit tool in OpenCode.

3 participants