Skip to content

Conversation

ashishrp-aws
Copy link
Contributor

Problem

The fsReplace tool was failing with TextNotFoundError because the LLM receives HTML-escaped prompts and returns oldStr values with similar escaping. This fix adds unescaping for both HTML entities and backslash-escaped angle brackets before matching against file content.

Solution

  • Add unescapeHtml() function to textFormatting.ts to handle both HTML entity unescaping (< → <, > → >, etc.) and backslash-escaped angle brackets (< → <, > → >)
  • Update fsReplace.ts to unescape oldStr and newStr before normalization
  • Import unescapeHtml from textFormatting module for reusability

License

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

…sReplace

The fsReplace tool was failing with TextNotFoundError because the LLM receives
HTML-escaped prompts and returns oldStr values with similar escaping. This fix
adds unescaping for both HTML entities and backslash-escaped angle brackets
before matching against file content.

Changes:
- Add unescapeHtml() function to textFormatting.ts to handle both HTML entity
  unescaping (&lt; → <, &gt; → >, etc.) and backslash-escaped angle brackets
  (\< → <, \> → >)
- Update fsReplace.ts to unescape oldStr and newStr before normalization
- Import unescapeHtml from textFormatting module for reusability
@ashishrp-aws ashishrp-aws requested a review from a team as a code owner October 9, 2025 20:30
@codecov-commenter
Copy link

codecov-commenter commented Oct 9, 2025

Codecov Report

❌ Patch coverage is 96.77419% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 62.32%. Comparing base (0e8e36d) to head (eff9712).

Files with missing lines Patch % Lines
.../src/language-server/agenticChat/textFormatting.ts 95.83% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2415      +/-   ##
==========================================
+ Coverage   62.30%   62.32%   +0.01%     
==========================================
  Files         266      266              
  Lines       59198    59227      +29     
  Branches     3793     3794       +1     
==========================================
+ Hits        36886    36914      +28     
  Misses      22237    22237              
- Partials       75       76       +1     
Flag Coverage Δ
unittests 62.32% <96.77%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

* 1. HTML escaping done by escape-html: " → &quot;, & → &amp;, ' → &#39;, < → &lt;, > → &gt;
* 2. Backslash escaping of angle brackets that may appear in the LLM response: \< → <, \> → >
*/
export function unescapeHtml(text: string): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use "unescape-html": "^1.1.0" also

declare module 'unescape-html' {
    function unescapeHTML(str: string): string
    export = unescapeHTML
}

Check this PR: https://github.com/aws/language-servers/pull/2360/files#diff-fbaaeff7c4b028ae726c46e4eea2adec92e5fd886d26d5eee21fba8dc67ecfe8L71


// Unescape HTML entities in oldStr since the prompt was HTML-escaped before being sent to LLM
const unescapedOldStr = unescapeHtml(diff.oldStr)
const unescapedNewStr = unescapeHtml(diff.newStr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure this will not introduce any regression? This is a risky change, what is prompting us to make this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fsReplace failures because LLm is reading sanitized prompt. When users are sending code in prompt usingSend to prompt right click. we are sanitizing it with escapeHtml. When there are special characters in said code is making LLM retrun code with incorrect old str

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.

4 participants