All files / src/diff coalesce.ts

97.95% Statements 96/98
95.34% Branches 41/43
100% Functions 14/14
100% Lines 87/87

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347                        19x     19x                                         18x 18x 18x 18x       18x 1x   17x     18x                               30x 30x                                               26x     26x 7x       32x   19x   19x   19x 30x 30x 30x     30x 18x     12x 12x         19x   19x                     4x 2x 2x                             19x                                                       5556x 350x                                       22x 22x 22x   22x 22x   22x 350x 350x   350x 350x 3x 3x       22x                                 30x 30x   30x 574x 574x 574x 572x   574x 570x         296x 296x     30x 30x 30x 30x 30x   30x                                       27x 18x     9x 9x 9x   9x     30x 30x 30x     30x 30x     30x 8x 8x       22x 22x 5x       22x 22x   22x     9x                                                 21x   27x    
/**
 * Hunk coalescing and splitting - manages hunk sizes for LLM analysis.
 *
 * - splitLargeHunks: Breaks large hunks into smaller chunks at logical breakpoints
 * - coalesceHunks: Merges nearby small hunks into fewer, larger chunks
 *
 * Pipeline: parsePatch() → splitLargeHunks() → coalesceHunks() → expandDiffContext()
 */
 
import type { DiffHunk } from './parser.js';
 
/** Default maximum gap in lines between hunks to merge */
export const DEFAULT_MAX_GAP_LINES = 30;
 
/** Default maximum chunk size in characters */
export const DEFAULT_MAX_CHUNK_SIZE = 8000;
 
/**
 * Options for coalescing hunks.
 */
export interface CoalesceOptions {
  /** Max lines gap between hunks to merge (default: 30) */
  maxGapLines?: number;
  /** Target max size per chunk in characters (default: 8000) */
  maxChunkSize?: number;
}
 
/**
 * Merge two adjacent hunks into one.
 *
 * The merged hunk spans from the start of the first hunk to the end of the second,
 * with content combined using '...' as a visual separator. When both hunks have
 * different headers (indicating different function/class scopes), both are preserved.
 */
function mergeHunks(a: DiffHunk, b: DiffHunk): DiffHunk {
  // Calculate the new range that spans both hunks
  const newStart = Math.min(a.newStart, b.newStart);
  const newEnd = Math.max(a.newStart + a.newCount, b.newStart + b.newCount);
  const oldStart = Math.min(a.oldStart, b.oldStart);
  const oldEnd = Math.max(a.oldStart + a.oldCount, b.oldStart + b.oldCount);
 
  // Combine headers when both exist and are different
  let header: string | undefined;
  if (a.header && b.header && a.header !== b.header) {
    header = `${a.header} → ${b.header}`;
  } else {
    header = a.header ?? b.header;
  }
 
  return {
    oldStart,
    oldCount: oldEnd - oldStart,
    newStart,
    newCount: newEnd - newStart,
    header,
    content: a.content + '\n...\n' + b.content,
    lines: [...a.lines, ...b.lines],
  };
}
 
/**
 * Calculate the gap in lines between two hunks.
 * Returns the number of lines between the end of hunk A and the start of hunk B.
 */
function calculateGap(a: DiffHunk, b: DiffHunk): number {
  const aEnd = a.newStart + a.newCount;
  return b.newStart - aEnd;
}
 
/**
 * Coalesce hunks that are close together into larger chunks.
 *
 * This reduces the number of LLM API calls by merging nearby hunks,
 * while respecting size limits to keep chunks manageable.
 *
 * @param hunks - Array of hunks to coalesce
 * @param options - Coalescing options (maxGapLines, maxChunkSize)
 * @returns Array of coalesced hunks (may be smaller than input)
 *
 * Algorithm:
 * 1. Sort hunks by start line
 * 2. For each hunk, check if it can be merged with the previous:
 *    - Gap between hunks <= maxGapLines
 *    - Combined size <= maxChunkSize
 * 3. If both conditions are met, merge; otherwise start a new chunk
 */
export function coalesceHunks(
  hunks: DiffHunk[],
  options: CoalesceOptions = {}
): DiffHunk[] {
  const { maxGapLines = DEFAULT_MAX_GAP_LINES, maxChunkSize = DEFAULT_MAX_CHUNK_SIZE } = options;
 
  // Nothing to coalesce with 0 or 1 hunks
  if (hunks.length <= 1) {
    return hunks;
  }
 
  // Sort hunks by start line to ensure we process them in order
  const sorted = [...hunks].sort((a, b) => a.newStart - b.newStart);
 
  const result: DiffHunk[] = [];
  // sorted[0] is guaranteed to exist since we checked hunks.length > 1 above
  let current = sorted[0] as DiffHunk;
 
  for (let i = 1; i < sorted.length; i++) {
    const next = sorted[i] as DiffHunk;
    const gap = calculateGap(current, next);
    const combinedSize = current.content.length + next.content.length;
 
    // Merge if: close enough AND combined size under limit
    if (gap <= maxGapLines && combinedSize <= maxChunkSize) {
      current = mergeHunks(current, next);
    } else {
      // Can't merge - save current and start a new chunk
      result.push(current);
      current = next;
    }
  }
 
  // Don't forget the last chunk
  result.push(current);
 
  return result;
}
 
/**
 * Check if coalescing would reduce the number of hunks.
 * Useful for deciding whether to show coalescing stats.
 */
export function wouldCoalesceReduce(
  hunks: DiffHunk[],
  options: CoalesceOptions = {}
): boolean {
  if (hunks.length <= 1) return false;
  const coalesced = coalesceHunks(hunks, options);
  return coalesced.length < hunks.length;
}
 
/**
 * Options for splitting large hunks.
 */
export interface SplitOptions {
  /** Target max size per chunk in characters (default: 8000) */
  maxChunkSize?: number;
}
 
/**
 * Patterns that indicate logical breakpoints for splitting.
 * Prioritized in order: blank lines are best, then function/class definitions.
 */
const LOGICAL_BREAKPOINT_PATTERNS = [
  // Blank lines (highest priority - natural paragraph breaks)
  /^[ ]?$/,
  // Function/method definitions (various languages)
  /^[ ]?(export\s+)?(async\s+)?function\s+\w+/,
  /^[ ]?(export\s+)?(const|let|var)\s+\w+\s*=\s*(async\s+)?\(/,
  /^[ ]?(export\s+)?(const|let|var)\s+\w+\s*=\s*(async\s+)?function/,
  /^[ ]?(public|private|protected)?\s*(static\s+)?(async\s+)?\w+\s*\([^)]*\)\s*[:{]/,
  /^[ ]?def\s+\w+/,
  /^[ ]?fn\s+\w+/,
  /^[ ]?func\s+\w+/,
  // Class/struct/interface definitions
  /^[ ]?(export\s+)?(abstract\s+)?class\s+\w+/,
  /^[ ]?(export\s+)?interface\s+\w+/,
  /^[ ]?(export\s+)?type\s+\w+\s*=/,
  /^[ ]?struct\s+\w+/,
  /^[ ]?impl\s+/,
  // Block comments (often precede logical sections)
  /^[ ]?\/\*\*/,
  /^[ ]?\/\//,
  /^[ ]?#\s/,
];
 
/**
 * Check if a line is a good logical breakpoint for splitting.
 * Returns a priority score (lower is better) or -1 if not a breakpoint.
 */
function getBreakpointPriority(line: string): number {
  const index = LOGICAL_BREAKPOINT_PATTERNS.findIndex((pattern) => pattern.test(line));
  return index;
}
 
/**
 * Find the best split point in a range of lines.
 * Prefers logical breakpoints; falls back to midpoint if none found.
 *
 * @param lines - Array of lines to search
 * @param startIdx - Start index in the lines array
 * @param endIdx - End index (exclusive) in the lines array
 * @param targetIdx - Ideal split point (used for fallback)
 * @returns Index of the best split point
 */
function findBestSplitPoint(
  lines: string[],
  startIdx: number,
  endIdx: number,
  targetIdx: number
): number {
  // Search window: look within 20% of chunk size from target
  const windowSize = Math.max(10, Math.floor((endIdx - startIdx) * 0.2));
  const searchStart = Math.max(startIdx + 1, targetIdx - windowSize);
  const searchEnd = Math.min(endIdx - 1, targetIdx + windowSize);
 
  let bestIdx = targetIdx;
  let bestPriority = Infinity;
 
  for (let i = searchStart; i <= searchEnd; i++) {
    const line = lines[i];
    Iif (line === undefined) continue;
 
    const priority = getBreakpointPriority(line);
    if (priority >= 0 && priority < bestPriority) {
      bestPriority = priority;
      bestIdx = i;
    }
  }
 
  return bestIdx;
}
 
/**
 * Create a sub-hunk from a portion of lines.
 *
 * @param originalHunk - The original hunk being split
 * @param lines - The lines for this sub-hunk
 * @param lineOffset - How many lines into the original hunk this sub-hunk starts
 */
function createSubHunk(
  originalHunk: DiffHunk,
  lines: string[],
  lineOffset: number
): DiffHunk {
  // Calculate how many "new" lines we've passed to get the new start position
  // We need to count actual new-file lines, not just array indices
  let newLinesBeforeOffset = 0;
  let oldLinesBeforeOffset = 0;
 
  for (let i = 0; i < lineOffset && i < originalHunk.lines.length; i++) {
    const line = originalHunk.lines[i];
    Iif (line === undefined) continue;
    if (!line.startsWith('-')) {
      newLinesBeforeOffset++;
    }
    if (!line.startsWith('+')) {
      oldLinesBeforeOffset++;
    }
  }
 
  // Count lines in this sub-hunk (lines without '-' are in new file, without '+' are in old file)
  const newCount = lines.filter((line) => !line.startsWith('-')).length;
  const oldCount = lines.filter((line) => !line.startsWith('+')).length;
 
  // Build the @@ header for this sub-hunk
  const newStart = originalHunk.newStart + newLinesBeforeOffset;
  const oldStart = originalHunk.oldStart + oldLinesBeforeOffset;
  const header = originalHunk.header;
  const headerSuffix = header ? ` ${header}` : '';
  const hunkHeader = `@@ -${oldStart},${oldCount} +${newStart},${newCount} @@${headerSuffix}`;
 
  return {
    oldStart,
    oldCount,
    newStart,
    newCount,
    header,
    content: [hunkHeader, ...lines].join('\n'),
    lines,
  };
}
 
/**
 * Split a single large hunk into smaller chunks.
 *
 * @param hunk - The hunk to split
 * @param maxChunkSize - Maximum size in characters per chunk
 * @returns Array of smaller hunks (may be single element if no split needed)
 */
function splitHunk(hunk: DiffHunk, maxChunkSize: number): DiffHunk[] {
  // If hunk is small enough, return as-is
  if (hunk.content.length <= maxChunkSize) {
    return [hunk];
  }
 
  const result: DiffHunk[] = [];
  const lines = hunk.lines;
  let currentStart = 0;
 
  while (currentStart < lines.length) {
    // Estimate how many lines fit in maxChunkSize
    // Use average line length as a rough guide
    const avgLineLength = hunk.content.length / Math.max(1, lines.length);
    const estimatedLines = Math.floor(maxChunkSize / avgLineLength);
    const targetEnd = Math.min(currentStart + estimatedLines, lines.length);
 
    // Calculate remaining content size
    const remainingLines = lines.slice(currentStart);
    const remainingSize = remainingLines.join('\n').length;
 
    // If remaining content fits in maxChunkSize, take it all
    if (remainingSize <= maxChunkSize) {
      result.push(createSubHunk(hunk, remainingLines, currentStart));
      break;
    }
 
    // Find best split point, ensuring we advance by at least one line
    let splitIdx = findBestSplitPoint(lines, currentStart, lines.length, targetEnd);
    if (splitIdx <= currentStart) {
      splitIdx = currentStart + 1;
    }
 
    // Extract lines for this chunk
    const chunkLines = lines.slice(currentStart, splitIdx);
    result.push(createSubHunk(hunk, chunkLines, currentStart));
 
    currentStart = splitIdx;
  }
 
  return result;
}
 
/**
 * Split large hunks into smaller chunks for LLM analysis.
 *
 * Large files (1000+ lines) that become single hunks in file-based analysis
 * can generate prompts exceeding practical limits. This function splits
 * such hunks at logical breakpoints (blank lines, function definitions)
 * to keep chunk sizes manageable.
 *
 * @param hunks - Array of hunks to potentially split
 * @param options - Split options (maxChunkSize)
 * @returns Array of hunks (may be larger than input if splits occurred)
 *
 * @example
 * // Pipeline usage:
 * const diff = parseFileDiff(filename, patch, status);
 * const splitHunks = splitLargeHunks(diff.hunks, { maxChunkSize: 8000 });
 * const coalescedHunks = coalesceHunks(splitHunks, { maxGapLines: 30 });
 */
export function splitLargeHunks(
  hunks: DiffHunk[],
  options: SplitOptions = {}
): DiffHunk[] {
  const { maxChunkSize = DEFAULT_MAX_CHUNK_SIZE } = options;
 
  return hunks.flatMap((hunk) => splitHunk(hunk, maxChunkSize));
}