Skip to content

Idea: replace edit tool input string with formatted code to gaslight the model to think it generated formatted code and have less edit failures #2529

Closed

Description

@remorses

Currently when the LLM edits a file opencode will format the result and pass the formatted code in the tool output

This is nice because the code is always formatted after every tool call, the problem is that the LLM still thinks the edit it generated was the one applied. So subsequent edits will easily fail because the model will pass a non formatted search string for edits.

This could be done by modifying the messages in the next request to format the tool inputs. This way the LLM will think it generated formatted code the first time and not get confused. This will probably help with following edits too because the LLM will follow the same formatting style.

This could decrease a bit tokens caching but should not be a big problem given only the last tool is modified so you only lose a bit of cached tokens, the ones for the edit tool input.

Possible implementation

The simplest method would be to insert random string comments at start and end of the snippet, then use them later to extract the formatted string from the formatted file and save the formatted file discarding the random string separators.

Another way is to use the AST of TreeSitter and compute an Id for the token on the start and end positions of the tool input. Then find the same token with the same id in the formatted code, find the position and return that portion of the string. For computing the id you can concatenate each parent node type and content up until the token on the start and end positions.

Activity

  1. github-actions commented on Sep 10, 2025

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    Feel free to ignore if none of these address your specific case.

  2. rekram1-node commented on Sep 10, 2025

    @rekram1-node
    Contributor

    interesting

  3. changed the title [-]Idea: replace edit tool input string with formatted code to gaslight the model to think it generated formatted code and have less edit edit failures[/-] [+]Idea: replace edit tool input string with formatted code to gaslight the model to think it generated formatted code and have less edit failures[/+] on Sep 10, 2025
  4. remorses commented on Sep 10, 2025

    @remorses
    ContributorAuthor

    This can be done with ai sdk prepareStep. would you like a PR?

  5. rekram1-node commented on Sep 10, 2025

    @rekram1-node
    Contributor

    it's up to Dax if he wants the implementation or not (not me) but you can try your hand at a PR

  6. remorses commented on Sep 10, 2025

    @remorses
    ContributorAuthor

    Working on a PR right now for this @thdxr

  7. remorses commented on Sep 10, 2025

    @remorses
    ContributorAuthor

    Edit: we are so back. I found a way to implement this using a diff algorithm

    Thinking more about this issue I don't thing it's a good idea

    Problems

    • Using the magic comments trick will not work when the LLM replacement string starts or end at a position where the comment would be invalid
    • Using the treesitter approach could theoretically work but it will take time and custom code for each language.

    For the time being #2535 is a better solution to this problem

  8. rekram1-node commented on Dec 28, 2025

    @rekram1-node
    Contributor

    [automated] Closing due to 90+ days of inactivity. Feel free to reopen if you still need this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions