@@ -113,21 +113,26 @@ function checkSharedTimeLimit() {
113113
114114/**
115115 * Get the current loop index for a specific invocation.
116- * The loop index represents how many times ALL test files have been run through.
117- * This is the batch count from the loop-runner.
116+ * When using external loop-runner (Jest), returns the batch number directly.
117+ * When using internal looping (Vitest), tracks and returns the invocation count.
118+ *
118119 * @param {string } invocationKey - Unique key for this test invocation
119- * @returns {number } The current batch number (loop index )
120+ * @returns {number } The loop index for timing markers (1-based )
120121 */
121122function getInvocationLoopIndex ( invocationKey ) {
122- // Track local loop count for stopping logic (increments on each call)
123+ // When using external loop-runner, use the batch number directly
124+ // This is reliable because Jest resets module state between batches
125+ const currentBatch = process . env . CODEFLASH_PERF_CURRENT_BATCH ;
126+ if ( currentBatch !== undefined ) {
127+ return parseInt ( currentBatch , 10 ) ;
128+ }
129+
130+ // For internal looping (Vitest), track the count locally
123131 if ( ! sharedPerfState . invocationLoopCounts [ invocationKey ] ) {
124132 sharedPerfState . invocationLoopCounts [ invocationKey ] = 0 ;
125133 }
126134 ++ sharedPerfState . invocationLoopCounts [ invocationKey ] ;
127-
128- // Return the batch number as the loop index for timing markers
129- // This represents how many times all test files have been run through
130- return parseInt ( process . env . CODEFLASH_PERF_CURRENT_BATCH || '1' , 10 ) ;
135+ return sharedPerfState . invocationLoopCounts [ invocationKey ] ;
131136}
132137
133138/**
@@ -693,11 +698,9 @@ function capturePerf(funcName, lineId, fn, ...args) {
693698 // If not set, we're in Vitest mode and need to do all loops internally
694699 const hasExternalLoopRunner = process . env . CODEFLASH_PERF_CURRENT_BATCH !== undefined ;
695700
696- // Batched looping: run BATCH_SIZE loops per capturePerf call when using loop-runner
701+ // When using external loop-runner (Jest), execute only once per call - the loop-runner handles batching
697702 // For Vitest (no loop-runner), do all loops internally in a single call
698- const batchSize = shouldLoop
699- ? ( hasExternalLoopRunner ? getPerfBatchSize ( ) : getPerfLoopCount ( ) )
700- : 1 ;
703+ const batchSize = hasExternalLoopRunner ? 1 : ( shouldLoop ? getPerfLoopCount ( ) : 1 ) ;
701704
702705 // Initialize runtime tracking for this invocation if needed
703706 if ( ! sharedPerfState . invocationRuntimes [ invocationKey ] ) {
@@ -719,7 +722,7 @@ function capturePerf(funcName, lineId, fn, ...args) {
719722 break ;
720723 }
721724
722- // Get the loop index (batch number) for timing markers
725+ // Get the loop index for timing markers
723726 const loopIndex = getInvocationLoopIndex ( invocationKey ) ;
724727
725728 // Check if we've exceeded max loops for this invocation
0 commit comments