Skip to content

fix(harness): truncate text on rune boundary to keep UTF-8 valid - #16511

Merged
yingfeng merged 1 commit into
infiniflow:mainfrom
feizhuzheng:fix-truncate-utf8
Jul 1, 2026
Merged

yingfeng merged 1 commit into
infiniflow:mainfrom
feizhuzheng:fix-truncate-utf8

Conversation

@feizhuzheng

Copy link
Copy Markdown
Contributor

Summary

truncateText in the reduction and summarization middlewares truncates with s[:maxLen], which slices by byte. When maxLen lands inside a multi-byte character (common with CJK or other non-ASCII content flowing through the agent), the string is cut mid-rune and the tail byte(s) become invalid UTF-8. That broken text then goes into the reduced context / summary prompt.

TruncateToolResult in the same reduction package already avoids this by slicing on a rune boundary and even notes it in a comment. This PR makes the two truncateText helpers do the same, so they stay consistent with the existing helper.

Both functions keep their existing output shape (summarization still appends ...). Added a small unit test in each package covering ASCII truncation and a CJK string, asserting the result stays valid UTF-8.

truncateText in the reduction and summarization middlewares sliced the
string with s[:maxLen], which indexes bytes. When maxLen falls inside a
multi-byte character (e.g. CJK text), the result is cut mid-rune and is
no longer valid UTF-8 - the same thing TruncateToolResult already guards
against in this package. Slice on a rune boundary instead.
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. labels Jul 1, 2026
@coderabbitai

coderabbitai Bot commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Fixed truncateText functions in both the reduction and summarization middlewares to truncate strings on UTF-8 rune boundaries instead of byte indices, preventing multi-byte character corruption. Added unit tests for both packages verifying short-string, ASCII, and multi-byte truncation behavior.

Changes

Rune-Safe Text Truncation

Layer / File(s) Summary
Reduction middleware truncation fix
internal/harness/core/middlewares/reduction/reduction.go, internal/harness/core/middlewares/reduction/truncate_text_test.go
truncateText converts strings to runes and slices on rune boundaries instead of bytes; new test validates short-string passthrough, ASCII truncation, and UTF-8 safety for multi-byte input.
Summarization middleware truncation fix
internal/harness/core/middlewares/summarization/summarization.go, internal/harness/core/middlewares/summarization/truncate_text_test.go
truncateText applies the same rune-based slicing with ellipsis appended on truncation; new test validates equivalent scenarios plus UTF-8 validity after ellipsis removal.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

A rabbit hops through bytes gone wrong,
Splitting kanji all day long 🐰
Now runes align in tidy rows,
No garbled glyph, no broken prose.
Truncate true, from head to tail—
Tests confirm: no more UTF-8 fail! ✂️📝

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: fixing text truncation to respect UTF-8 rune boundaries.
Description check ✅ Passed The description includes the required Summary section and explains the problem, fix, consistency with another helper, and tests.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

🧹 Nitpick comments (1)
internal/harness/core/middlewares/reduction/reduction.go (1)

164-175: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Correct rune-boundary fix.

Guards correctly handle both byte-length and rune-count early exits before slicing on runes, matching the existing TruncateToolResult pattern in tool_result.go.

Separately, this is now the third near-identical "truncate to N runes" implementation in the codebase (here, summarization.go, and TruncateToolResult in tool_result.go). Consider extracting a shared helper (e.g., in a small shared util package) to avoid triple maintenance of the same rune-boundary logic going forward.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/harness/core/middlewares/reduction/reduction.go` around lines 164 -
175, The truncation helper needs to use the correct rune-boundary guard before
slicing. Update truncateText to follow the same early-exit pattern as
TruncateToolResult in tool_result.go: return the original string when maxLen is
at least the byte length or when maxLen exceeds the rune count, and only then
slice the rune slice. Also consider extracting this repeated truncate-to-N-runes
logic into a shared helper used by truncateText, summarization.go, and
TruncateToolResult to avoid duplicated maintenance.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@internal/harness/core/middlewares/reduction/reduction.go`:
- Around line 164-175: The truncation helper needs to use the correct
rune-boundary guard before slicing. Update truncateText to follow the same
early-exit pattern as TruncateToolResult in tool_result.go: return the original
string when maxLen is at least the byte length or when maxLen exceeds the rune
count, and only then slice the rune slice. Also consider extracting this
repeated truncate-to-N-runes logic into a shared helper used by truncateText,
summarization.go, and TruncateToolResult to avoid duplicated maintenance.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 460cf0da-60fe-4c0f-8d29-93c43b9e457f

📥 Commits

Reviewing files that changed from the base of the PR and between 45fc7fe and 522b0ca.

📒 Files selected for processing (4)
  • internal/harness/core/middlewares/reduction/reduction.go
  • internal/harness/core/middlewares/reduction/truncate_text_test.go
  • internal/harness/core/middlewares/summarization/summarization.go
  • internal/harness/core/middlewares/summarization/truncate_text_test.go

@yingfeng
yingfeng marked this pull request as draft July 1, 2026 07:54
@yingfeng
yingfeng marked this pull request as ready for review July 1, 2026 07:54
@yingfeng yingfeng added the ci Continue Integration label Jul 1, 2026
@yingfeng
yingfeng marked this pull request as draft July 1, 2026 07:54
@yingfeng
yingfeng marked this pull request as ready for review July 1, 2026 07:55
@yingfeng
yingfeng merged commit 97a4c64 into infiniflow:main Jul 1, 2026
13 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants