Fix/issue 367 kimi k2 streaming#477
Open
ayorindeadunse wants to merge 4 commits intoCodebuffAI:mainfrom
Open
Conversation
…runtime errors - Add safety check for lineInfo.lineStartCols existence before accessing it - Prevents 'undefined is not an object' error when OpenTUI TextBufferView doesn't populate lineStartCols - Fixes issue where CLI would crash on startup with TypeError
- Fixed incorrect property name reference at line 804 - All references use correct lineStartCols (not lineStarts) - Resolves TypeScript compilation errors
…reaming responses
- Add attemptJSONParse() function with 4 fallback recovery strategies for truncated JSON
1. Try parsing as-is
2. Try trimming trailing whitespace
3. Try closing incomplete JSON structures ({}, ]}, }]}, [])
4. Return null if all attempts fail
- Integrate defensive parsing into handleLine() function:
- Replace simple JSON.parse() with attemptJSONParse() with null checks
- Add error-like pattern detection to handle error responses with malformed schema
- Enhanced logging showing raw response preview and recovery attempts
- Detailed debug logs for truncated/malformed chunks
- Improve handleStreamChunk() robustness:
- Add defensive check for null/undefined/non-array choices
- Better logging with diagnostic information for empty choices
This fixes issue CodebuffAI#367 where kimi-k2 and similar models that send truncated JSON at newline boundaries would cause silent parsing failures and agent crashes.
jahooma
reviewed
Mar 21, 2026
jahooma
reviewed
Mar 21, 2026
jahooma
reviewed
Mar 21, 2026
jahooma
reviewed
Mar 21, 2026
…#367) - Add attemptJSONParse() function with recovery strategies for truncated JSON - Attempts to parse as-is, then trims whitespace, then closes incomplete structures - Handles truncated newline boundaries gracefully - Remove redundant error-like fallback path (handleResponse already returns {state} for errors) - Improve logging for schema validation failures - Fixes issue where kimi-k2 model breaks due to JSON chunk truncation
74d22f3 to
c2b99f5
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.
Description
Fixes #367 - The "kimi-k2" Model breaks off after use at the beginning
Problem
The kimi-k2 model was breaking off during streaming responses due to truncated JSON chunks. When JSON is sent at newline boundaries, it can arrive incomplete, causing parsing failures and stream termination.
Solution
This PR adds defensive JSON parsing and error recovery mechanisms to gracefully handle truncated or malformed JSON responses from OpenRouter:
attemptJSONParse()function - Implements multi-stage JSON recovery:Enhanced error handling - Improved logging and validation:
Import cleanup - Reorganized imports for consistency and removed unused types
Testing
Related
Fixes #367