Repository navigation
fix(harness): truncate text on rune boundary to keep UTF-8 valid - #16511
Conversation
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.
📝 WalkthroughWalkthroughFixed 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. ChangesRune-Safe Text Truncation
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/harness/core/middlewares/reduction/reduction.go (1)
164-175: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winCorrect rune-boundary fix.
Guards correctly handle both byte-length and rune-count early exits before slicing on runes, matching the existing
TruncateToolResultpattern intool_result.go.Separately, this is now the third near-identical "truncate to N runes" implementation in the codebase (here,
summarization.go, andTruncateToolResultintool_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
📒 Files selected for processing (4)
internal/harness/core/middlewares/reduction/reduction.gointernal/harness/core/middlewares/reduction/truncate_text_test.gointernal/harness/core/middlewares/summarization/summarization.gointernal/harness/core/middlewares/summarization/truncate_text_test.go
Summary
truncateTextin thereductionandsummarizationmiddlewares truncates withs[:maxLen], which slices by byte. WhenmaxLenlands 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.TruncateToolResultin the samereductionpackage already avoids this by slicing on a rune boundary and even notes it in a comment. This PR makes the twotruncateTexthelpers 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.