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.
…CodebuffAI#367) - Add attemptJSONParse() function with recovery strategies for truncated/malformed JSON - Handle incomplete JSON structures by closing them (}, ]}, }]}, etc.) - Improve error handling and logging for malformed chunks - Gracefully degrade when parsing fails instead of crashing - Fixes issue where kimi-k2 model breaks off due to JSON truncation at newline boundaries
jahooma
reviewed
Mar 21, 2026
| let obj = attemptJSONParse(raw) | ||
| if (obj === null) { | ||
| // All parsing attempts failed - log with detailed context for debugging | ||
| logger.debug( |
Contributor
There was a problem hiding this comment.
Should we keep the logger.warn level rather than downgrading to debug?
jahooma
reviewed
Mar 21, 2026
| function isPrintableCharacterKey(key: KeyEvent): boolean { | ||
| const name = key.name | ||
|
|
Contributor
There was a problem hiding this comment.
Note: it would be better not to include many trivial formatting changes
Contributor
Author
There was a problem hiding this comment.
I'll work on this. Thanks!
jahooma
reviewed
Mar 21, 2026
| ) | ||
|
|
||
| const cursorRow = lineInfo | ||
| const cursorRow = lineInfo && lineInfo.lineStartCols |
Contributor
There was a problem hiding this comment.
Are you sure that lineInfo doesn't include lineStartCols sometimes?
Contributor
Author
There was a problem hiding this comment.
I thought so, but typescript flagged an error. I'll check this again.
jahooma
reviewed
Mar 21, 2026
| 'OpenRouter response matches error pattern but schema validation failed', | ||
| ) | ||
| // Continue processing as error response | ||
| return handleResponse({ |
Contributor
There was a problem hiding this comment.
If it's error like, doesn't handleResponse already return { state }, e.g. see:
if ('error' in data || !data.usage) {
// Stream not finished
return { state }
}
So this doesn't seem to do anything
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