Skip to content

Fix critical and high-priority bugs in chunking and regex search - #60

Merged
runonthespot merged 2 commits into
mainfrom
fix/critical-chunk-and-regex-bugs
Sep 21, 2025
Merged

Fix critical and high-priority bugs in chunking and regex search#60
runonthespot merged 2 commits into
mainfrom
fix/critical-chunk-and-regex-bugs

Conversation

@runonthespot

Copy link
Copy Markdown
Contributor

Summary

This PR fixes 3 critical and high-priority runtime bugs that could cause panics and incorrect search results:

Critical Fixes

  • 🐛 Fix divide-by-zero panic in stride_large_chunk when estimate_tokens() returns 0
  • 🛡️ Add robust error handling for empty chunks and zero token estimates

High-Priority Fixes

  • 📏 Fix off-by-one error in strided chunk line_end calculation that caused incorrect span boundaries
  • 🔍 Fix byte offset issues in regex search with mixed line endings (\r\n, \n, \r) that caused misaligned search results on Windows and mixed-format files

Implementation Details

  • Enhanced line ending handling: New split_lines_with_endings() function properly handles all line ending formats
  • True streaming search: Improved search_file_streaming() with BufReader for memory efficiency
  • Robust edge case handling: Support for old Mac files with embedded \r characters
  • Comprehensive test coverage: Added regression tests for all fixed scenarios

Test Results

  • ✅ All 25 tests passing (10 ck-chunk + 15 ck-engine)
  • ✅ Clean code quality with no clippy warnings
  • ✅ Full workspace compilation successful

Test plan

  • All existing tests continue to pass
  • New regression tests cover the fixed scenarios:
    • Empty chunk handling without panics
    • Zero token estimate graceful handling
    • Strided chunk line span accuracy
    • Mixed line ending byte offset precision
  • Code quality checks (clippy) pass
  • Full workspace builds successfully

🤖 Generated with Claude Code

runonthespot and others added 2 commits September 21, 2025 22:38
Critical Fixes:
- Fix divide-by-zero panic in stride_large_chunk when estimate_tokens returns 0
- Add early return for empty chunks and fallback to default chars_per_token ratio

High-Priority Fixes:
- Fix off-by-one error in strided chunk line_end calculation
- Fix byte offset issues in regex search with mixed line endings (\r\n, \n, \r)
- Add accurate line ending length calculation for proper byte positioning

Tests Added:
- Regression tests for empty chunk striding and zero token estimates
- Tests for strided chunk line span calculations
- Tests for regex search with Windows (\r\n) and mixed line endings
- Unit tests for line ending length calculation helper function

All tests pass and code quality checks (clippy) are clean.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Consolidate duplicate if blocks for '\n' and '\r' line endings
- Use more elegant ends_with(['\n', '\r']) pattern matching
- Maintain all functionality while improving code quality

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@runonthespot
runonthespot merged commit 5a04040 into main Sep 21, 2025
9 of 10 checks passed
@runonthespot runonthespot mentioned this pull request Sep 21, 2025
4 tasks
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