fix(hashline): accept legacy hashes for indented anchors

Keep persisted LINE#ID anchors working after strict whitespace hashing by falling back to the legacy hash for validation-only lookups.

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-03-14 13:42:17 +09:00
parent f03de4f8a8
commit bbd2e86499
4 changed files with 43 additions and 11 deletions
+9 -8
View File
@@ -1,4 +1,4 @@
import { computeLineHash } from "./hash-computation"
import { computeLegacyLineHash, computeLineHash } from "./hash-computation"
import { HASHLINE_REF_PATTERN } from "./constants"
export interface LineRef {
@@ -15,6 +15,10 @@ const MISMATCH_CONTEXT = 2
const LINE_REF_EXTRACT_PATTERN = /([0-9]+#[ZPMQVRWSNKTXJBYH]{2})/
function isCompatibleLineHash(line: number, content: string, hash: string): boolean {
return computeLineHash(line, content) === hash || computeLegacyLineHash(line, content) === hash
}
export function normalizeLineRef(ref: string): string {
const originalTrimmed = ref.trim()
let trimmed = originalTrimmed
@@ -71,9 +75,7 @@ export function validateLineRef(lines: string[], ref: string): void {
}
const content = lines[line - 1]
const currentHash = computeLineHash(line, content)
if (currentHash !== hash) {
if (!isCompatibleLineHash(line, content, hash)) {
throw new HashlineMismatchError([{ line, expected: hash }], lines)
}
}
@@ -140,8 +142,8 @@ function suggestLineForHash(ref: string, lines: string[]): string | null {
if (!hashMatch) return null
const hash = hashMatch[1]
for (let i = 0; i < lines.length; i++) {
if (computeLineHash(i + 1, lines[i]) === hash) {
return `Did you mean "${i + 1}#${hash}"?`
if (isCompatibleLineHash(i + 1, lines[i], hash)) {
return `Did you mean "${i + 1}#${computeLineHash(i + 1, lines[i])}"?`
}
}
return null
@@ -169,8 +171,7 @@ export function validateLineRefs(lines: string[], refs: string[]): void {
}
const content = lines[line - 1]
const currentHash = computeLineHash(line, content)
if (currentHash !== hash) {
if (!isCompatibleLineHash(line, content, hash)) {
mismatches.push({ line, expected: hash })
}
}