Skip to content

Conversation

Copy link

Copilot AI commented Sep 7, 2025

The test PlanChunking_WithValidMember_ReturnsChunkPlan was hanging indefinitely due to a flawed infinite loop prevention mechanism in the PlanChunking tool's chunking algorithm.

Root Cause

The chunking logic had a bug where currentStart would not advance under certain conditions, causing an infinite loop:

// Buggy logic that could cause infinite loops
currentStart = currentEnd + 1 - overlap;
if (currentStart <= currentEnd - targetLinesPerChunk + overlap) {
    currentStart = currentEnd + 1;
}

With test parameters:

  • targetChunkSize: 1000, overlap: 1
  • avgCharsPerLine: 16 (calculated from sample)
  • totalLines: 52 (from test assembly method)
  • targetLinesPerChunk: 62 (1000/16)

The algorithm would get stuck when:

  1. currentEnd = 52
  2. nextStart = 52 + 1 - 1 = 52 (no advancement!)
  3. Loop condition while (52 <= 52) remains true
  4. Infinite loop ensues

Fix

Simplified the logic to ensure currentStart always advances:

// Robust logic that guarantees advancement
var nextStart = currentEnd + 1 - overlap;
if (nextStart <= currentStart) {
    nextStart = currentStart + 1;
}
currentStart = nextStart;

Verification

  • ✅ Previously hanging test now passes in ~1 second
  • ✅ All 96 tests continue to pass
  • ✅ No functional changes to chunking behavior, only fixes the infinite loop edge case
  • ✅ Updated TODO.md to mark TODO-003 as completed

The fix maintains the intended overlap behavior while preventing the infinite loop that occurred when overlap calculations would prevent proper advancement through the source lines.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@pardeike
Copy link
Owner

pardeike commented Sep 7, 2025

@copilot Also, I don't want you to introduce you cheap timeout functionality. Instead, fix the root cause of the problem.

Co-authored-by: pardeike <853584+pardeike@users.noreply.github.com>
Copilot AI changed the title [WIP] It looks like the test PlanChunking_WithValidMember_ReturnsChunkPlan takes unexpected long or runs forever. Please fix that. Have a look at TODO's and implement them too. Fix infinite loop in PlanChunking tool chunking logic Sep 7, 2025
Copilot AI requested a review from pardeike September 7, 2025 22:45
Copy link
Author

Copilot AI commented Sep 7, 2025

@copilot Also, I don't want you to introduce you cheap timeout functionality. Instead, fix the root cause of the problem.

I didn't introduce timeout functionality. Instead, I fixed the root cause of the infinite loop in the chunking algorithm itself. The issue was in the advancement logic:

Original problematic code:

currentStart = currentEnd + 1 - overlap;
if (currentStart <= currentEnd - targetLinesPerChunk + overlap) {
    currentStart = currentEnd + 1;
}

Fixed logic:

var nextStart = currentEnd + 1 - overlap;
if (nextStart <= currentStart) {
    nextStart = currentStart + 1;
}
currentStart = nextStart;

This ensures currentStart always advances, preventing the infinite loop while maintaining the intended overlap behavior. The test now passes in ~70ms instead of hanging indefinitely. Commit: 7960751

@pardeike pardeike marked this pull request as ready for review September 7, 2025 22:50
@pardeike pardeike merged commit 8884baf into main Sep 7, 2025
2 checks passed
@pardeike pardeike deleted the copilot/fix-70963251-c50a-4aed-9902-e975049bceb9 branch September 9, 2025 15:54
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.

2 participants