Fix critical bugs in string truncation and context window lookup - #1194
Fix critical bugs in string truncation and context window lookup#1194pavankumar-vh wants to merge 1 commit into
Conversation
Bug Fixes: 1. Fix context window lookup in base-chat.ts: Handle missing/undefined model correctly - Change: → - Prevents unnecessary lookup and improves clarity 2. Fix critical bug in truncateStringWithMessage: Prevent negative slice indices - Added Math.max(0, ...) guards to prevent negative slice lengths - Fixes potential runtime errors when maxLength < message length - Applies to all truncation modes (START, END, MIDDLE) 3. Add comprehensive tests for truncateStringWithMessage - Added 9 test cases covering edge cases - Tests for negative/zero available length scenarios - Tests for all truncation modes (START, END, MIDDLE) - Tests for custom messages and empty strings All changes are in approved contribution areas (agents/, common/) and improve code safety.
|
Nice catch on The Also note the test file has two Overall: keep the string.ts fix, drop or relabel the base-chat.ts change, and remove the duplicate test. |
Overview
Fix two critical bugs that could cause runtime errors and improve code safety.
Bug Fixes
1. Fix context window lookup in base-chat.ts
Issue: The code used
CONTEXT_WINDOWS[model ?? '']which converts undefined model to empty string and unnecessarily performs a lookup.Fix: Changed to
(model && CONTEXT_WINDOWS[model]) ?? DEFAULT_CONTEXT_WINDOWwhich:2. Fix critical bug in truncateStringWithMessage
Issue: When
maxLengthis smaller than the message length, the function calculates negative slice indices, causing unexpected behavior.Fix: Added
Math.max(0, ...)guards to prevent negative slice lengths:Math.max(0, maxLength - suffix.length)Math.max(0, maxLength - prefix.length)Math.max(0, Math.floor((maxLength - middle.length) / 2))This prevents potential runtime errors when truncating strings with very small maxLength values.
Files Changed
agents/base-chat.ts- Improved context window lookup logiccommon/src/util/string.ts- Added safety guards to prevent negative slice indicesScope
This change only touches
agents/andcommon/which are approved contribution areas per the Contributing Guide.