Skip to content

Fix critical bugs in string truncation and context window lookup - #1193

Closed
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/context-window-string-truncation-bugs
Closed

Fix critical bugs in string truncation and context window lookup#1193
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/context-window-string-truncation-bugs

Conversation

@pavankumar-vh

Copy link
Copy Markdown

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_WINDOW which:

  • Avoids unnecessary lookup when model is undefined
  • More clearly expresses intent
  • Falls back to default window correctly

2. Fix critical bug in truncateStringWithMessage

Issue: When maxLength is 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:

  • END mode: Math.max(0, maxLength - suffix.length)
  • START mode: Math.max(0, maxLength - prefix.length)
  • MIDDLE mode: Math.max(0, Math.floor((maxLength - middle.length) / 2))

This prevents potential runtime errors when truncating strings with very small maxLength values.

3. Add comprehensive tests for truncateStringWithMessage

Added 9 test cases covering:

  • All truncation modes (END, START, MIDDLE)
  • Edge cases with negative/zero available length
  • Custom message handling
  • Empty string handling
  • Default behavior verification

Files Changed

  • agents/base-chat.ts - Improved context window lookup logic
  • common/src/util/string.ts - Added safety guards to prevent negative slice indices
  • common/src/util/__tests__/string.test.ts - Added comprehensive test coverage

Scope

This change only touches agents/ and common/ which are approved contribution areas per the Contributing Guide.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant