{"version":3,"file":"edit-hashline.d.ts","sourceRoot":"","sources":["../../../src/core/tools/edit-hashline.ts"],"names":[],"mappings":"AAgBA;;;;;;;;GAQG;AAEH,MAAM,WAAW,oBAAoB;IACpC,KAAK,EAAE,OAAO,CAAC;IACf,kEAAkE;IAClE,SAAS,EAAE,MAAM,CAAC;IAClB,4DAA4D;IAC5D,OAAO,EAAE,MAAM,CAAC;IAChB;;;;OAIG;IACH,KAAK,EAAE,MAAM,CAAC;IACd,2DAA2D;IAC3D,SAAS,EAAE,OAAO,CAAC;IACnB;;;OAGG;IACH,oBAAoB,EAAE,MAAM,CAAC;CAC7B;AAWD,gFAAgF;AAChF,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE/C;AAED,kDAAkD;AAClD,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAUrE;AAED,+DAA+D;AAC/D,eAAO,MAAM,kBAAkB,MAAM,CAAC;AACtC,mEAAmE;AACnE,eAAO,MAAM,gBAAgB,OAAO,CAAC;AAmBrC;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,oBAAoB,CAyEvF;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAK7G","sourcesContent":["import { createHash } from \"crypto\";\n\n// Local copy of edit-diff's fuzzy normalization, duplicated to avoid an\n// import cycle (edit-diff.ts imports this module for anchor recovery).\nfunction normalizeForFuzzyMatch(text: string): string {\n\treturn text\n\t\t.normalize(\"NFKC\")\n\t\t.split(\"\\n\")\n\t\t.map((line) => line.trimEnd())\n\t\t.join(\"\\n\")\n\t\t.replace(/[\\u2018\\u2019\\u201A\\u201B]/g, \"'\")\n\t\t.replace(/[\\u201C\\u201D\\u201E\\u201F]/g, '\"')\n\t\t.replace(/[\\u2010\\u2011\\u2012\\u2013\\u2014\\u2015\\u2212]/g, \"-\")\n\t\t.replace(/[\\u00A0\\u2002-\\u200A\\u202F\\u205F\\u3000]/g, \" \");\n}\n\n/**\n * Hashline-style anchor recovery for the edit tool.\n *\n * When exact and whitespace-fuzzy matching both fail (the file drifted since\n * the model read it: lines edited, shifted, or partially rewritten), we anchor\n * each line of oldText by a short content hash and look for the window of the\n * file with the highest anchor hit rate. The replacement is only applied when\n * the best window is unambiguous (clear margin over the runner-up).\n */\n\nexport interface AnchorRecoveryResult {\n\tfound: boolean;\n\t/** Start line (0-based) of the recovered window in the content */\n\tstartLine: number;\n\t/** End line (0-based, exclusive) of the recovered window */\n\tendLine: number;\n\t/**\n\t * Similarity of the window with oldText (0-1): matched non-empty lines divided by\n\t * the larger of the oldText and window non-empty line counts, so it bounds both the\n\t * share of oldText found and the share of the window the model has seen.\n\t */\n\tscore: number;\n\t/** True when two windows score too close to pick safely */\n\tambiguous: boolean;\n\t/**\n\t * Non-empty lines of the window that match no oldText line: content the model\n\t * never saw and that the replacement overwrites. Callers must report it.\n\t */\n\tunmatchedWindowLines: number;\n}\n\nconst NOT_FOUND: AnchorRecoveryResult = {\n\tfound: false,\n\tstartLine: -1,\n\tendLine: -1,\n\tscore: 0,\n\tambiguous: false,\n\tunmatchedWindowLines: 0,\n};\n\n/** Short content hash of one normalized line (trailing whitespace stripped). */\nexport function lineAnchor(line: string): string {\n\treturn createHash(\"sha1\").update(normalizeForFuzzyMatch(line).trimEnd()).digest(\"hex\").slice(0, 8);\n}\n\n/** Map of hash -> line numbers for a document. */\nexport function computeAnchors(content: string): Map<string, number[]> {\n\tconst map = new Map<string, number[]>();\n\tconst lines = content.split(\"\\n\");\n\tfor (let i = 0; i < lines.length; i++) {\n\t\tconst h = lineAnchor(lines[i]);\n\t\tconst list = map.get(h);\n\t\tif (list) list.push(i);\n\t\telse map.set(h, [i]);\n\t}\n\treturn map;\n}\n\n/** Minimum anchor hit rate for a recovery to be considered. */\nexport const RECOVERY_THRESHOLD = 0.6;\n/** Minimum relative margin between best and second-best window. */\nexport const AMBIGUITY_MARGIN = 0.15;\n\nconst isBlank = (line: string): boolean => line.trim().length === 0;\n\n/** Count non-empty lines shared by both hash lists, each line used at most once. */\nfunction countMatchedLines(oldHashes: string[], windowHashes: string[]): number {\n\tconst remaining = new Map<string, number>();\n\tfor (const h of windowHashes) remaining.set(h, (remaining.get(h) ?? 0) + 1);\n\tlet matched = 0;\n\tfor (const h of oldHashes) {\n\t\tconst count = remaining.get(h) ?? 0;\n\t\tif (count > 0) {\n\t\t\tmatched++;\n\t\t\tremaining.set(h, count - 1);\n\t\t}\n\t}\n\treturn matched;\n}\n\n/**\n * Recover the position of oldText in content by line anchors.\n * oldText must span at least 2 non-empty lines; single-line recovery is left\n * to the exact/fuzzy matchers (single short lines are too ambiguous).\n *\n * Safety rules: the first and last non-empty lines of oldText must both match the\n * boundaries of the window (so the replacement can neither slide past the region\n * the model saw nor swallow neighbouring lines), leading/trailing blank lines of\n * oldText must be blank in the file too, and the window may not grow or shrink by\n * more than the tolerated drift.\n */\nexport function recoverByAnchors(content: string, oldText: string): AnchorRecoveryResult {\n\tconst allOldLines = oldText.split(\"\\n\");\n\tlet leadingBlank = 0;\n\twhile (leadingBlank < allOldLines.length && isBlank(allOldLines[leadingBlank])) leadingBlank++;\n\tlet trailingBlank = 0;\n\twhile (\n\t\ttrailingBlank < allOldLines.length - leadingBlank &&\n\t\tisBlank(allOldLines[allOldLines.length - 1 - trailingBlank])\n\t) {\n\t\ttrailingBlank++;\n\t}\n\tconst coreOldLines = allOldLines.slice(leadingBlank, allOldLines.length - trailingBlank);\n\tconst oldHashes = coreOldLines.filter((l) => !isBlank(l)).map(lineAnchor);\n\tif (oldHashes.length < 2) return NOT_FOUND;\n\n\tconst contentLines = content.split(\"\\n\");\n\tconst contentHashes = contentLines.map(lineAnchor);\n\tconst firstHash = oldHashes[0];\n\tconst lastHash = oldHashes[oldHashes.length - 1];\n\tconst coreSpan = coreOldLines.length;\n\tconst tolerance = Math.ceil(coreSpan * (1 - RECOVERY_THRESHOLD));\n\tconst minSpan = Math.max(2, coreSpan - tolerance);\n\tconst maxSpan = coreSpan + tolerance;\n\n\tconst candidates: Array<{ start: number; end: number; score: number; unmatched: number }> = [];\n\tfor (let start = 0; start < contentLines.length; start++) {\n\t\tif (contentHashes[start] !== firstHash || isBlank(contentLines[start])) continue;\n\t\tconst lastEnd = Math.min(start + maxSpan - 1, contentLines.length - 1);\n\t\tfor (let end = start + minSpan - 1; end <= lastEnd; end++) {\n\t\t\tif (contentHashes[end] !== lastHash || isBlank(contentLines[end])) continue;\n\t\t\tconst windowStart = start - leadingBlank;\n\t\t\tconst windowEnd = end + 1 + trailingBlank;\n\t\t\tif (windowStart < 0 || windowEnd > contentLines.length) continue;\n\t\t\tlet blankBoundaries = true;\n\t\t\tfor (let i = windowStart; i < start; i++) blankBoundaries &&= isBlank(contentLines[i]);\n\t\t\tfor (let i = end + 1; i < windowEnd; i++) blankBoundaries &&= isBlank(contentLines[i]);\n\t\t\tif (!blankBoundaries) continue;\n\n\t\t\tconst windowHashes: string[] = [];\n\t\t\tfor (let i = start; i <= end; i++) {\n\t\t\t\tif (!isBlank(contentLines[i])) windowHashes.push(contentHashes[i]);\n\t\t\t}\n\t\t\tconst matched = countMatchedLines(oldHashes, windowHashes);\n\t\t\tcandidates.push({\n\t\t\t\tstart: windowStart,\n\t\t\t\tend: windowEnd,\n\t\t\t\tscore: matched / Math.max(oldHashes.length, windowHashes.length),\n\t\t\t\tunmatched: windowHashes.length - matched,\n\t\t\t});\n\t\t}\n\t}\n\n\tif (candidates.length === 0) return NOT_FOUND;\n\n\tcandidates.sort((a, b) => b.score - a.score);\n\tconst best = candidates[0];\n\tif (best.score < RECOVERY_THRESHOLD) return NOT_FOUND;\n\n\t// Every candidate is a distinct window, so any runner-up within the margin is a\n\t// genuine alternative location (or extent) and the choice would be a guess.\n\tconst second = candidates.length > 1 ? candidates[1].score : 0;\n\tif (best.score - second < AMBIGUITY_MARGIN) {\n\t\treturn { ...NOT_FOUND, ambiguous: true, score: best.score };\n\t}\n\n\treturn {\n\t\tfound: true,\n\t\tstartLine: best.start,\n\t\tendLine: best.end,\n\t\tscore: best.score,\n\t\tambiguous: false,\n\t\tunmatchedWindowLines: best.unmatched,\n\t};\n}\n\n/**\n * Replace the recovered window [startLine, endLine) of content with newText.\n * Returns the new content. Callers must have validated recovery.found first.\n */\nexport function applyRecoveredWindow(content: string, recovery: AnchorRecoveryResult, newText: string): string {\n\tconst lines = content.split(\"\\n\");\n\tconst replacement = newText.split(\"\\n\");\n\tlines.splice(recovery.startLine, recovery.endLine - recovery.startLine, ...replacement);\n\treturn lines.join(\"\\n\");\n}\n"]}