* 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, )
| 264 | * Returns the FileAttributionState to store, or null if tracking failed. |
| 265 | */ |
| 266 | function computeFileModificationState( |
| 267 | existingFileStates: Map<string, FileAttributionState>, |
| 268 | filePath: string, |
| 269 | oldContent: string, |
| 270 | newContent: string, |
| 271 | mtime: number, |
| 272 | ): FileAttributionState | null { |
| 273 | const normalizedPath = normalizeFilePath(filePath) |
| 274 | |
| 275 | try { |
| 276 | // Calculate NCode's character contribution |
| 277 | let assistantContribution: number |
| 278 | |
| 279 | if (oldContent === '' || newContent === '') { |
| 280 | // New file or full deletion - contribution is the content length |
| 281 | assistantContribution = |
| 282 | oldContent === '' ? newContent.length : oldContent.length |
| 283 | } else { |
| 284 | // Find actual changed region via common prefix/suffix matching. |
| 285 | // This correctly handles same-length replacements (e.g., "Esc" → "esc") |
| 286 | // where Math.abs(newLen - oldLen) would be 0. |
| 287 | const minLen = Math.min(oldContent.length, newContent.length) |
| 288 | let prefixEnd = 0 |
| 289 | while ( |
| 290 | prefixEnd < minLen && |
| 291 | oldContent[prefixEnd] === newContent[prefixEnd] |
| 292 | ) { |
| 293 | prefixEnd++ |
| 294 | } |
| 295 | let suffixLen = 0 |
| 296 | while ( |
| 297 | suffixLen < minLen - prefixEnd && |
| 298 | oldContent[oldContent.length - 1 - suffixLen] === |
| 299 | newContent[newContent.length - 1 - suffixLen] |
| 300 | ) { |
| 301 | suffixLen++ |
| 302 | } |
| 303 | const oldChangedLen = oldContent.length - prefixEnd - suffixLen |
| 304 | const newChangedLen = newContent.length - prefixEnd - suffixLen |
| 305 | assistantContribution = Math.max(oldChangedLen, newChangedLen) |
| 306 | } |
| 307 | |
| 308 | // Get current file state if it exists |
| 309 | const existingState = existingFileStates.get(normalizedPath) |
| 310 | const existingContribution = existingState?.assistantContribution ?? 0 |
| 311 | |
| 312 | return { |
| 313 | contentHash: computeContentHash(newContent), |
| 314 | assistantContribution: existingContribution + assistantContribution, |
| 315 | mtime, |
| 316 | } |
| 317 | } catch (error) { |
| 318 | logError(error as Error) |
| 319 | return null |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
no test coverage detected