Skip to content

fix(core): preserve line terminators in truncateString - #29673

Open
diegogodinezr wants to merge 3 commits into
google-gemini:mainfrom
diegogodinezr:fix/textutils-truncate-line-terminators
Open

diegogodinezr wants to merge 3 commits into
google-gemini:mainfrom
diegogodinezr:fix/textutils-truncate-line-terminators

Conversation

@diegogodinezr

@diegogodinezr diegogodinezr commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes truncateString in @google/gemini-cli-core so that line terminators (\n, \r, \r\n, \u2028, \u2029) and complex Unicode grapheme clusters are preserved in the truncated output and counted toward the maxLength budget.

Details

Previously, graphemeRegex in truncateString (/(?:[\uD800-\uDBFF][\uDC00-\uDFFF]|.)\p{M}*/gu) omitted the s (dotAll) flag and used a manual regular expression that skipped line terminators and could split multi-code-point grapheme clusters such as \r\n (CRLF) or ZWJ emoji sequences.

This change replaces the manual regex with Intl.Segmenter (granularity: 'grapheme') so that line terminators, CRLF sequences, and complex Unicode grapheme clusters are treated as atomic segments and counted toward maxLength, and adds unit test coverage for multi-line strings and ZWJ emoji clusters.

Related Issues

Fixes #29562

How to Validate

  1. Run the textUtils unit tests in @google/gemini-cli-core:
    npm test -w @google/gemini-cli-core -- src/utils/textUtils.test.ts
  2. Verify that truncateString preserves line terminators and counts grapheme clusters toward maxLength:
    • truncateString('aa\nbb\ncc', 4, '') returns 'aa\nb'
    • truncateString('a\nb\nc', 3, '') returns 'a\nb'
    • truncateString('x\r\ny', 2, '') returns 'x'
    • truncateString('x\r\ny', 3, '') returns 'x\r\n'

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

Add the dotAll (s) flag to graphemeRegex in truncateString so that '.' matches line terminators (\n, \r, \u2028, \u2029), preserving multi-line formatting and counting line terminators toward the maxLength budget.

Fixes google-gemini#29562
@diegogodinezr
diegogodinezr requested a review from a team as a code owner October 7, 2026 22:52
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where line terminators were being stripped and ignored during string truncation. By enabling the dotAll flag in the regex used for grapheme parsing, the function now correctly treats line breaks as characters, ensuring they are preserved in the output and accounted for within the specified length budget.

Highlights

  • Regex Update: Updated the grapheme cluster regular expression in truncateString to include the 's' (dotAll) flag, ensuring line terminators are correctly matched and counted.
  • Test Coverage: Added comprehensive unit tests to verify that multi-line strings and various line terminators are preserved during string truncation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@github-actions github-actions Bot added the size/s A small PR label Oct 7, 2026
@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

📊 PR Size: size/S

  • Lines changed: 33
  • Additions: +22
  • Deletions: -11
  • Files changed: 2

@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the truncateString utility in textUtils.ts by adding the s (dotAll) flag to the grapheme regular expression, ensuring that line terminators are preserved and counted toward the maximum length budget. It also adds corresponding unit tests in textUtils.test.ts. The review feedback recommends treating \r\n (CRLF) as a single, indivisible grapheme cluster to prevent splitting that could corrupt terminal rendering in CLI environments, and suggests updating the test assertions to reflect this behavior.

Comment thread packages/core/src/utils/textUtils.ts Outdated
Comment on lines +150 to +151
expect(truncateString('x\r\ny', 2, '')).toBe('x\r');
expect(truncateString('x\r\ny', 3, '')).toBe('x\r\n');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Update the test assertions to reflect that \r\n (CRLF) is treated as a single indivisible grapheme cluster, preventing a dangling \r from being returned when truncating.

Suggested change
expect(truncateString('x\r\ny', 2, '')).toBe('x\r');
expect(truncateString('x\r\ny', 3, '')).toBe('x\r\n');
expect(truncateString('x\r\ny', 2, '')).toBe('x');
expect(truncateString('x\r\ny', 3, '')).toBe('x\r\n');
References
  1. When truncating strings that may contain multi-byte Unicode characters, use methods that operate on grapheme clusters instead of UTF-16 code units to prevent character splitting.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the truncateString utility by adding the s (dotAll) flag to its grapheme regular expression, ensuring line terminators are preserved and counted toward the length budget, and adds corresponding unit tests. However, the review feedback highlights that this custom regex approach still fails to handle complex Unicode grapheme clusters correctly, such as splitting CRLF (\r\n) into separate characters (which is also codified in the new tests). Since the project targets Node.js >= 20, it is recommended to replace the custom regex with the built-in Intl.Segmenter API for robust Unicode support.

Comment thread packages/core/src/utils/textUtils.ts Outdated
Comment thread packages/core/src/utils/textUtils.test.ts Outdated
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the truncateString utility in packages/core/src/utils/textUtils.ts to use the native Intl.Segmenter API with grapheme granularity instead of a custom regular expression. This improves the handling of complex grapheme clusters, such as zero-width joiner (ZWJ) emojis and line terminators. Additionally, corresponding unit tests have been added to packages/core/src/utils/textUtils.test.ts to verify these behaviors. There are no review comments, and I have no feedback to provide.

@gemini-cli gemini-cli Bot added the area/core Issues related to User Interface, OS Support, Core Functionality label Oct 7, 2026

This branch has not been deployed

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

Labels

area/core Issues related to User Interface, OS Support, Core Functionality size/s A small PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

truncateString() drops every line terminator and does not charge them against maxLength

1 participant