fix(gateway): respect utf16 budget in _split_text_chunks no-newline fallback - #42523
fix(gateway): respect utf16 budget in _split_text_chunks no-newline fallback#42523Frowtek wants to merge 1 commit into
Conversation
…allback The no-newline fallback hard-split at `limit`, which is measured in len_fn units (UTF-16 code units for Telegram), but used it as a codepoint slice index. Non-BMP text (emoji = 2 UTF-16 units each) then produced chunks up to 2x the budget, exceeding the platform cap so the final fallback message was rejected and lost. Split at the already-computed _cp_budget instead. For the default len_fn=len, _cp_budget == limit, so plain-text behavior is unchanged.
|
Positive verification: The root-cause fix is correct — |
|
Thanks for isolating the fallback UTF-16 boundary defect. Current main still has the exact unsafe fallback at Problems
Suggested changes
Automated hermes-sweeper review. |
What does this PR do?
GatewayStreamConsumer._split_text_chunkssplits the fallback finalmessage into platform-sized chunks. Its no-newline fallback hard-split at
limit, butlimitis measured inlen_fnunits (UTF-16 code units forTelegram) while it was used as a codepoint slice index. Non-BMP text
(emoji = 2 UTF-16 units each) produced chunks up to 2x the budget,
exceeding the platform cap — so the final fallback message was rejected
and lost.
The function already computes
_cp_budget(the largest codepoint offsetwithin the unit budget) and uses it to bound the newline search; the fix
also uses it for the hard split. For the default
len_fn=len,_cp_budget == limit, so plain-text behavior is unchanged.Related Issue
N/A
Type of Change
Changes Made
gateway/stream_consumer.py— in_split_text_chunks, hard-split at_cp_budgetinstead oflimitso chunks respect thelen_fnbudget.tests/gateway/test_stream_consumer.py— add regression tests.How to Test
Run:
Added tests (all pass):
utf16_len(chunk) <= limitlen_fnbehavior unchangedBefore the fix, an emoji-only chunk reached
utf16_len = 8000for a4000-unit limit; after, all chunks are
<= limit.Checklist