MCPcopy Create free account
hub / github.com/Noumena-Network/code / computeFileModificationState

Function computeFileModificationState

src/utils/commitAttribution.ts:266–321  ·  view source on GitHub ↗

* 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,
)

Source from the content-addressed store, hash-verified

264 * Returns the FileAttributionState to store, or null if tracking failed.
265 */
266function 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/**

Callers 2

trackFileModificationFunction · 0.85
trackBulkFileChangesFunction · 0.85

Calls 5

normalizeFilePathFunction · 0.85
computeContentHashFunction · 0.85
maxMethod · 0.80
logErrorFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected