Skip to content

Commit d3edf87

Browse files
mhallidaclaude
andcommitted
fix: sql_diff now correctly detects differences between queries
The handler was returning {diff, equivalent} but the tool expected {has_changes, unified_diff, additions, deletions, change_count, similarity}. Since has_changes was always undefined (falsy), the tool always reported "The two SQL queries are identical." Now returns proper SqlDiffResult fields with token-aware comparison that handles single-line SQL correctly. Fixes TICKET-002 in #422. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 0fe2ba1 commit d3edf87

1 file changed

Lines changed: 45 additions & 8 deletions

File tree

‎packages/opencode/src/altimate/native/sql/register.ts‎

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,29 +326,66 @@ register("sql.diff", async (params) => {
326326
: null
327327
const compare = compareRaw ? JSON.parse(JSON.stringify(compareRaw)) : null
328328

329-
// Simple line-based diff
329+
// Token-aware diff: normalize whitespace and compare tokens
330+
const normalizeForCompare = (s: string) => s.replace(/\s+/g, " ").trim()
331+
const tokensA = normalizeForCompare(sqlA)
332+
const tokensB = normalizeForCompare(sqlB)
333+
334+
// Line-based diff for display
330335
const linesA = sqlA.split("\n")
331336
const linesB = sqlB.split("\n")
332337
const diffLines: string[] = []
338+
let additions = 0
339+
let deletions = 0
333340
const maxLen = Math.max(linesA.length, linesB.length)
334341
for (let i = 0; i < maxLen; i++) {
335342
const a = linesA[i] ?? ""
336343
const b = linesB[i] ?? ""
337344
if (a !== b) {
338-
if (a) diffLines.push(`- ${a}`)
339-
if (b) diffLines.push(`+ ${b}`)
345+
if (a) { diffLines.push(`- ${a}`); deletions++ }
346+
if (b) { diffLines.push(`+ ${b}`); additions++ }
347+
} else if (a) {
348+
diffLines.push(` ${a}`)
340349
}
341350
}
342351

352+
// If both queries are on a single line, do a token-level comparison
353+
if (diffLines.length === 0 && tokensA !== tokensB) {
354+
diffLines.push(`- ${sqlA}`)
355+
diffLines.push(`+ ${sqlB}`)
356+
additions = 1
357+
deletions = 1
358+
}
359+
360+
const hasChanges = tokensA !== tokensB
361+
const changeCount = additions + deletions
362+
363+
// Compute similarity ratio (Dice coefficient on character bigrams)
364+
const bigrams = (s: string): Set<string> => {
365+
const set = new Set<string>()
366+
for (let i = 0; i < s.length - 1; i++) set.add(s.slice(i, i + 2))
367+
return set
368+
}
369+
const bA = bigrams(tokensA)
370+
const bB = bigrams(tokensB)
371+
let intersection = 0
372+
for (const b of bA) { if (bB.has(b)) intersection++ }
373+
const similarity = bA.size + bB.size > 0
374+
? (2 * intersection) / (bA.size + bB.size)
375+
: 1.0
376+
343377
return {
344378
success: true,
345-
diff: diffLines.join("\n"),
346-
equivalent: compare?.equivalent ?? false,
347-
equivalence_confidence: compare?.confidence ?? 0,
348-
differences: compare?.differences ?? [],
379+
has_changes: hasChanges,
380+
unified_diff: diffLines.join("\n"),
381+
additions,
382+
deletions,
383+
change_count: changeCount,
384+
similarity,
385+
changes: compare?.differences ?? [],
349386
}
350387
} catch (e) {
351-
return { success: false, diff: "", equivalent: false, equivalence_confidence: 0, differences: [], error: String(e) }
388+
return { success: false, has_changes: false, unified_diff: "", additions: 0, deletions: 0, change_count: 0, similarity: 0, changes: [], error: String(e) }
352389
}
353390
})
354391

0 commit comments

Comments
 (0)