fix(agent): prevent GLM stop-to-length heuristic false positives (#14572)#15463
Closed
aj-nt wants to merge 2 commits into
Closed
fix(agent): prevent GLM stop-to-length heuristic false positives (#14572)#15463aj-nt wants to merge 2 commits into
aj-nt wants to merge 2 commits into
Conversation
Collaborator
|
Likely duplicate of #14574 — both fix #14572 (emoji sign-off false positives in GLM stop-to-length heuristic). This PR is more comprehensive (adds 500-char min gate + config opt-out) but targets the same root cause in |
added 2 commits
April 24, 2026 23:56
…sResearch#14572) Three-pronged fix for the Ollama/GLM stop-to-length heuristic that was triggering continuation loops on any response not ending with ASCII/CJK punctuation — including emoji sign-offs (💛, ✨, 🙌), Markdown links, and conversational text lacking terminal punctuation. 1. Expand _has_natural_response_ending() with emoji recognition: - Strip trailing Unicode combining marks (variation selectors, ZWJ) before checking the base character. - Detect Unicode categories So (Other_Symbol) and Sk (Modifier_Symbol). - Check Extended_Pictographic heuristic range U+1F000–U+1FAFF. - Hardcode common sign-off codepoints outside those ranges. 2. Add 500-char minimum-length gate in _should_treat_stop_as_truncated(): - Responses under 500 visible chars are almost certainly complete — they couldn't have hit a meaningful token limit. - Retains the original 20-char/no-whitespace short-junk detection. - Eliminates the vast majority of false positives from conversational replies and emoji sign-offs. 3. Config opt-out via agent.glm_truncation_heuristic (default true): - Set to false to disable the heuristic entirely. - Read with getattr(self, _glm_truncation_heuristic_enabled, True) for backwards compatibility. 57 new tests in test_glm_stop_heuristic.py. Updated existing integration test mock content to >=500 chars so it still triggers the heuristic.
…search#14572)\n\n- Move import unicodedata to module top-level (was inline in method body)\n- Add Sm (Math_Symbol) category to natural-ending recognition for arrows\n and math symbols that commonly end structured responses\n- Remove dead hardcoded string - every character in it is category So,\n already caught by the category check. The comment claimed they were\n outside those ranges but they weren't.\n- Rename test_emoji_sign_off_with_100_chars to\n test_short_response_with_emoji_does_not_trigger to accurately describe\n what it tests (the 500-char gate, not emoji recognition).\n- Add 8 parametrized tests for Sm/So characters including arrows, math\n symbols, and codepoints from the removed hardcoded list.
b2e9bfc to
31595e2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #14572 — the Ollama/GLM stop-to-length heuristic was triggering 100% false positives on responses ending with emoji sign-offs (💛, ✨, 🙌), Markdown links, or conversational text lacking terminal punctuation. Each false positive wasted up to 3 continuation API calls per turn.
Root cause:
_has_natural_response_ending()only recognized ASCII and CJK punctuation as natural endings. Any response ending with an emoji, symbol, or bare word was classified as truncated.Three-pronged fix
1. Expand
_has_natural_response_ending()— emoji and symbol recognitionunicodedata.category()So (Other_Symbol), Sk (Modifier_Symbol), and Sm (Math_Symbol) as natural endings"✓✔✗✘♠♣♥♦♪♫☀☁☂★☆"— every character was already caught by the So category check (the comment incorrectly claimed they were "outside those ranges")2. 500-char minimum-length gate in
_should_treat_stop_as_truncated()3. Config opt-out —
agent.glm_truncation_heuristiccli-config.yaml.example, defaults totrue(heuristic enabled)falseto disable the heuristic entirelygetattr(self, "_glm_truncation_heuristic_enabled", True)for backwards compatibilityRefactoring pass (commit 2)
import unicodedatafrom inline in method body to module top-leveltest_emoji_sign_off_with_100_chars→test_short_response_with_emoji_does_not_trigger(it tests the 500-char gate, not emoji recognition)Test plan