Skip to content

fix: VariableTruncator._truncate_array under-reports truncated flag#37543

Open
ifer47 wants to merge 1 commit into
langgenius:mainfrom
ifer47:fix/variable-truncator-truncated-flag
Open

fix: VariableTruncator._truncate_array under-reports truncated flag#37543
ifer47 wants to merge 1 commit into
langgenius:mainfrom
ifer47:fix/variable-truncator-truncated-flag

Conversation

@ifer47

@ifer47 ifer47 commented Jun 16, 2026

Copy link
Copy Markdown

Summary

  • Fix _truncate_array size-limit break not setting truncated = True before exiting the loop, causing dropped tail elements to go unreported
  • Fix _truncate_array overwriting the truncated flag on each iteration instead of accumulating it (truncated = truncated or part_result.truncated), so a later non-truncated element could erase an earlier truncation signal
  • Add two targeted unit tests reproducing the exact scenarios from the issue

Context

Closes #37192

The _truncate_object and truncate_variable_mapping sibling methods already accumulate the flag correctly. This PR brings _truncate_array in line with them.

Bug 1 — size-limit break: When used_size > target_size, the loop breaks but leaves truncated = False, so dropped array elements are not reported:

t = VariableTruncator(array_element_limit=20, max_size_bytes=1000)
r = t._truncate_array([10, 20, 30, 40, 50], 12)
# Before fix: r.truncated == False (wrong — element 50 was dropped)
# After fix:  r.truncated == True

Bug 2 — flag overwrite: truncated = part_result.truncated replaces the flag on each iteration instead of OR-accumulating:

t2 = VariableTruncator(array_element_limit=20, max_size_bytes=1000, string_length_limit=10)
r2 = t2._truncate_array(["x" * 60, "a"], 60)
# Before fix: r2.truncated == False (wrong — "x"*60 was truncated)
# After fix:  r2.truncated == True

Test plan

  • New test test_array_size_limit_break_sets_truncated_flag verifies bug 1
  • New test test_array_truncated_flag_accumulates_across_elements verifies bug 2
  • Existing TestArrayTruncation tests continue to pass (no behavior change for non-buggy paths)

🤖 Generated with Claude Code Best

The `_truncate_array` method had two bugs causing the `truncated` flag
to be incorrectly reported as `False` when data was actually truncated:

1. When the size-limit `break` was hit, the loop exited without setting
   `truncated = True`, so dropped tail elements went unreported.

2. On each iteration, `truncated = part_result.truncated` overwrote the
   flag instead of accumulating it (`truncated = truncated or ...`), so a
   later non-truncated element could erase an earlier truncation signal.

Both fixes align `_truncate_array` with its sibling methods
`_truncate_object` and `truncate_variable_mapping`, which already
accumulate the flag correctly.

Closes langgenius#37192

Co-Authored-By: zhipu/glm-5 <zai-org@claude-code-best.win>
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VariableTruncator._truncate_array under-reports the truncated flag after dropping/shortening array elements

1 participant