* Compute the character contribution for a file modification. * Returns the FileAttributionState to store, or null if tracking failed.
( existingFileStates: Map<string, FileAttributionState>, filePath: string, oldContent: string, newContent: string, mtime: number, )
| 323 | * Returns the FileAttributionState to store, or null if tracking failed. |
| 324 | */ |
| 325 | function computeFileModificationState( |
| 326 | existingFileStates: Map<string, FileAttributionState>, |
| 327 | filePath: string, |
| 328 | oldContent: string, |
| 329 | newContent: string, |
| 330 | mtime: number, |
| 331 | ): FileAttributionState | null { |
| 332 | const normalizedPath = normalizeFilePath(filePath) |
| 333 | |
| 334 | try { |
| 335 | // Calculate Claude's character contribution |
| 336 | let claudeContribution: number |
| 337 | |
| 338 | if (oldContent === '' || newContent === '') { |
| 339 | // New file or full deletion - contribution is the content length |
| 340 | claudeContribution = |
| 341 | oldContent === '' ? newContent.length : oldContent.length |
| 342 | } else { |
| 343 | // Find actual changed region via common prefix/suffix matching. |
| 344 | // This correctly handles same-length replacements (e.g., "Esc" → "esc") |
| 345 | // where Math.abs(newLen - oldLen) would be 0. |
| 346 | const minLen = Math.min(oldContent.length, newContent.length) |
| 347 | let prefixEnd = 0 |
| 348 | while ( |
| 349 | prefixEnd < minLen && |
| 350 | oldContent[prefixEnd] === newContent[prefixEnd] |
| 351 | ) { |
| 352 | prefixEnd++ |
| 353 | } |
| 354 | let suffixLen = 0 |
| 355 | while ( |
| 356 | suffixLen < minLen - prefixEnd && |
| 357 | oldContent[oldContent.length - 1 - suffixLen] === |
| 358 | newContent[newContent.length - 1 - suffixLen] |
| 359 | ) { |
| 360 | suffixLen++ |
| 361 | } |
| 362 | const oldChangedLen = oldContent.length - prefixEnd - suffixLen |
| 363 | const newChangedLen = newContent.length - prefixEnd - suffixLen |
| 364 | claudeContribution = Math.max(oldChangedLen, newChangedLen) |
| 365 | } |
| 366 | |
| 367 | // Get current file state if it exists |
| 368 | const existingState = existingFileStates.get(normalizedPath) |
| 369 | const existingContribution = existingState?.claudeContribution ?? 0 |
| 370 | |
| 371 | return { |
| 372 | contentHash: computeContentHash(newContent), |
| 373 | claudeContribution: existingContribution + claudeContribution, |
| 374 | mtime, |
| 375 | } |
| 376 | } catch (error) { |
| 377 | logError(error as Error) |
| 378 | return null |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /** |
no test coverage detected