Skip to content

Commit 1748c37

Browse files
anandgupta42claude
andauthored
feat: verified query optimization via altimate_core_rewrite verify_equivalence mode (#918)
* feat: verified-optimize tool — surface only provably-equivalent query rewrites Composes altimate_core.rewrite + altimate_core.equivalence into one gated primitive: a rewrite is reported VERIFIED only when equivalence affirmatively returns equivalent===true; everything else (incl. no-schema) is returned but labeled 'review before applying'. The core mechanic for one-click verified query optimization. 15 tests (gate logic + adversarial strict-true + real-engine). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * refactor: fold verified optimization into altimate_core_rewrite (verify_equivalence) Addresses review feedback: a standalone `altimate_verified_optimize` tool overlapped with `sql_optimize` and `altimate_core_rewrite`, risking agent tool-selection confusion (three near-identical "optimize a query" tools). Instead, verification is now a MODE of the existing rewrite tool: - `altimate_core_rewrite` gains `verify_equivalence?: boolean` (default false → existing behavior unchanged). When true, each proposed rewrite is checked against the original via `altimate_core.equivalence`; only rewrites that return strict `equivalent === true` are labeled verified-safe, the rest "review before applying." Conservative gate (no truthy coercion), per-candidate try/catch, array-guarded differences, null-guarded responses, and specific unverified reasons (missing-field / non-boolean / not-equivalent). - Remove the standalone `altimate_verified_optimize` tool + its registration. - `query-optimize` skill now calls `altimate_core_rewrite` with `verify_equivalence: true`, folding its old manual rewrite→equivalence steps (3 + 6) into one verified call. Tests (18) moved to `altimate-core-rewrite-verify.test.ts`, exercising the verify mode: gate logic, adversarial strict-true (rejects "true"/1/{}/undefined/null + missing-field/non-boolean reasons), partial-failure resilience, and a real-engine integration test. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix: address review on verify_equivalence mode (coderabbit + multi-persona) Two real findings on altimate-core-rewrite.ts (the current verify-mode file; most other review comments targeted the now-deleted standalone tool): - MAJOR (coderabbit): handle `success === false` explicitly. An `{ success: false, error: "" }` rewrite payload has a falsy error string and fell through to the success path, misreporting results. Now fails on an explicit `success === false` regardless of error string. - MINOR (coderabbit + multi-persona): dedup/no-op normalization lowercased the full SQL, which could collapse rewrites differing only by a case-sensitive string literal / quoted identifier into one — dropping a valid candidate before verification. Normalize whitespace only; no case-folding. Already-addressed findings (reviewers were looking at the deleted standalone tool): per-candidate try/catch (equivalence throw fails closed per candidate, not the whole tool) and strict boolean validation of `equivalent` (=== true + a typeof-based "non-boolean" reason) are both present in the current code. Tests (20): + success:false-with-empty-error regression, + case-sensitive-literal dedup preservation. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 88b7f6f commit 1748c37

3 files changed

Lines changed: 465 additions & 16 deletions

File tree

‎.opencode/skills/query-optimize/SKILL.md‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: Analyze and optimize SQL queries for better performance
77

88
## Requirements
99
**Agent:** any (read-only analysis)
10-
**Tools used:** sql_optimize, sql_analyze, sql_explain, altimate_core_equivalence, read, glob, schema_inspect, warehouse_list
10+
**Tools used:** altimate_core_rewrite (with `verify_equivalence: true`), sql_analyze, sql_explain, read, glob, schema_inspect, warehouse_list
1111

1212
Analyze SQL queries for performance issues and suggest concrete optimizations including rewritten SQL.
1313

@@ -20,9 +20,9 @@ Analyze SQL queries for performance issues and suggest concrete optimizations in
2020

2121
2. **Determine the dialect** -- Default to `snowflake`. If the user specifies a dialect (postgres, bigquery, duckdb, etc.), use that instead. Check the project for warehouse connections using `warehouse_list` if unsure.
2222

23-
3. **Run the optimizer**:
24-
- Call `sql_optimize` with the SQL, dialect, and schema context if available
25-
- If the user has a warehouse connection, first call `schema_inspect` on the relevant tables to build schema context for better optimization (e.g., SELECT * expansion)
23+
3. **Run the verified optimizer**:
24+
- If the user has a warehouse connection, first call `schema_inspect` on the relevant tables to build schema context (needed both for better rewrites — e.g. SELECT * expansion — and to verify equivalence)
25+
- Call `altimate_core_rewrite` with the SQL, schema context, and **`verify_equivalence: true`**. This proposes rewrites AND proves each one returns the same results as the original in a single step. The result is partitioned into **verified-equivalent** rewrites (safe to apply) and **unverified** rewrites (review before applying), so you never recommend a rewrite that silently changes semantics.
2626

2727
4. **Run detailed analysis**:
2828
- Call `sql_analyze` with the same SQL and dialect to get the full anti-pattern breakdown with recommendations
@@ -32,10 +32,10 @@ Analyze SQL queries for performance issues and suggest concrete optimizations in
3232
- Look for: full table scans, sort operations on large datasets, inefficient join strategies, missing partition pruning
3333
- Include key findings in the report under "Execution Plan Insights"
3434

35-
6. **Verify rewrites preserve correctness**:
36-
- If `sql_optimize` produced a rewritten query, call `altimate_core_equivalence` to verify the original and optimized queries produce the same result set
37-
- If not equivalent, flag the difference and present both versions for the user to decide
38-
- This prevents "optimization" that silently changes query semantics
35+
6. **Equivalence verification is built into step 3** (`verify_equivalence: true`):
36+
- Present the **verified-equivalent** rewrites as safe to apply.
37+
- Present **unverified** rewrites separately with their reason ("review before applying") — do not recommend applying these without manual review.
38+
- If no schema was available, all rewrites come back unverified; say so and recommend supplying a schema (or a warehouse connection) to enable verification.
3939

4040
7. **Present findings** in a structured format:
4141

@@ -83,4 +83,4 @@ The user invokes this skill with SQL or a file path:
8383
- `/query-optimize models/staging/stg_orders.sql` -- Optimize SQL from a file
8484
- `/query-optimize` -- Optimize the most recently discussed SQL in the conversation
8585

86-
Use the tools: `sql_optimize`, `sql_analyze`, `sql_explain` (execution plans), `altimate_core_equivalence` (rewrite verification), `read` (for file-based SQL), `glob` (to find SQL files), `schema_inspect` (for schema context), `warehouse_list` (to check connections).
86+
Use the tools: `altimate_core_rewrite` with `verify_equivalence: true` (proposes rewrites AND proves they preserve results in one step), `sql_analyze`, `sql_explain` (execution plans), `read` (for file-based SQL), `glob` (to find SQL files), `schema_inspect` (for schema context), `warehouse_list` (to check connections).

‎packages/opencode/src/altimate/tools/altimate-core-rewrite.ts‎

Lines changed: 168 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,51 @@ import { Dispatcher } from "../native"
44

55
export const AltimateCoreRewriteTool = Tool.define("altimate_core_rewrite", {
66
description:
7-
"Suggest query optimization rewrites. Analyzes SQL and proposes concrete rewrites for better performance. Provide schema_context or schema_path for accurate table/column resolution.",
7+
"Suggest query optimization rewrites. Analyzes SQL and proposes concrete rewrites for better performance. Provide schema_context or schema_path for accurate table/column resolution. " +
8+
"Set verify_equivalence=true to additionally PROVE each rewrite returns the same results as the original (via semantic equivalence) before recommending it — rewrites that pass are labeled verified-safe to apply, the rest 'review before applying'. Use verify_equivalence=true whenever the rewrite may be applied automatically or correctness matters; it requires a schema.",
89
parameters: z.object({
910
sql: z.string().describe("SQL query to optimize"),
1011
schema_path: z.string().optional().describe("Path to YAML/JSON schema file"),
1112
schema_context: z.record(z.string(), z.any()).optional().describe("Inline schema definition"),
13+
verify_equivalence: z
14+
.boolean()
15+
.optional()
16+
.describe(
17+
"If true, verify each rewrite is semantically equivalent to the original before recommending it (requires a schema).",
18+
),
1219
}),
13-
async execute(args, ctx) {
20+
async execute(args) {
1421
try {
15-
const result = await Dispatcher.call("altimate_core.rewrite", {
22+
const result = (await Dispatcher.call("altimate_core.rewrite", {
1623
sql: args.sql,
1724
schema_path: args.schema_path ?? "",
1825
schema_context: args.schema_context,
19-
})
20-
const data = (result.data ?? {}) as Record<string, any>
26+
})) as { success?: boolean; error?: string; data?: Record<string, any> } | null
27+
const data = (result?.data ?? {}) as Record<string, any>
28+
// Treat an explicit `success === false` as a failure even when the error
29+
// string is empty/absent — otherwise an `{ success: false, error: "" }`
30+
// payload would fall through to the success path and misreport results.
31+
const failed = result?.success === false || !!(result?.error ?? data.error)
32+
if (failed) {
33+
const error = result?.error ?? data.error ?? "rewrite failed"
34+
return {
35+
title: "Rewrite: ERROR",
36+
metadata: { success: false, rewrite_count: 0, verified_count: 0, unverified_count: 0, error },
37+
output: `Failed to generate rewrites: ${error}`,
38+
}
39+
}
40+
2141
const suggestions = data.suggestions ?? data.rewrites ?? []
2242
const rewriteCount = suggestions.length || (data.rewritten_sql && data.rewritten_sql !== args.sql ? 1 : 0)
23-
const error = result.error ?? data.error
43+
44+
// Verified mode: gate each rewrite on proven semantic equivalence to the original.
45+
if (args.verify_equivalence) {
46+
return await verifyRewrites(args, data)
47+
}
48+
2449
return {
2550
title: `Rewrite: ${rewriteCount} suggestion(s)`,
26-
metadata: { success: result.success, rewrite_count: rewriteCount, ...(error && { error }) },
51+
metadata: { success: result?.success, rewrite_count: rewriteCount },
2752
output: formatRewrite(data),
2853
}
2954
} catch (e) {
@@ -37,6 +62,142 @@ export const AltimateCoreRewriteTool = Tool.define("altimate_core_rewrite", {
3762
},
3863
})
3964

65+
/**
66+
* Verify each candidate rewrite against the original via altimate-core equivalence.
67+
* A rewrite is VERIFIED only when equivalence affirmatively returns `equivalent === true`
68+
* (strict boolean — never a truthy coercion). Everything else — not-equivalent,
69+
* can't-decide, a thrown equivalence check, or no schema — is returned UNVERIFIED
70+
* ("review before applying"). Conservative by design: a verified rewrite is safe to
71+
* apply without changing results.
72+
*/
73+
async function verifyRewrites(
74+
args: { sql: string; schema_path?: string; schema_context?: Record<string, any> },
75+
data: Record<string, any>,
76+
) {
77+
const hasSchema = !!(args.schema_path || (args.schema_context && Object.keys(args.schema_context).length > 0))
78+
// Normalize whitespace only for dedup / no-op detection — do NOT case-fold:
79+
// lowercasing would collapse rewrites that differ only by a case-sensitive
80+
// string literal or quoted identifier (distinct queries) into one, dropping a
81+
// valid candidate before it's verified. Equivalence itself is checked by the
82+
// engine, not by this string compare.
83+
const norm = (s: string) => s.replace(/\s+/g, " ").trim()
84+
85+
// Collect candidate rewrites (whole-query + per-suggestion), drop blanks/no-ops/dupes.
86+
const suggestions: any[] = data.suggestions ?? data.rewrites ?? []
87+
const raw: Array<{ sql: string; rule?: string }> = []
88+
if (typeof data.rewritten_sql === "string" && data.rewritten_sql.trim()) raw.push({ sql: data.rewritten_sql })
89+
for (const s of suggestions) {
90+
if (typeof s?.rewritten_sql === "string" && s.rewritten_sql.trim()) {
91+
raw.push({ sql: s.rewritten_sql, rule: s.rule ?? s.type })
92+
}
93+
}
94+
const seen = new Set<string>([norm(args.sql)])
95+
const candidates = raw.filter((c) => {
96+
const n = norm(c.sql)
97+
if (seen.has(n)) return false
98+
seen.add(n)
99+
return true
100+
})
101+
102+
if (!candidates.length) {
103+
return {
104+
title: "Rewrite: no rewrites",
105+
metadata: { success: true, rewrite_count: 0, verified_count: 0, unverified_count: 0, has_schema: hasSchema },
106+
output: "No optimizations suggested for this query.",
107+
}
108+
}
109+
110+
const verified: Array<{ sql: string; rule?: string; confidence?: number }> = []
111+
const unverified: Array<{ sql: string; rule?: string; reason: string }> = []
112+
for (const c of candidates) {
113+
if (!hasSchema) {
114+
unverified.push({ sql: c.sql, rule: c.rule, reason: "no schema supplied — equivalence cannot be verified" })
115+
continue
116+
}
117+
try {
118+
const eq = (await Dispatcher.call("altimate_core.equivalence", {
119+
sql1: args.sql,
120+
sql2: c.sql,
121+
schema_path: args.schema_path ?? "",
122+
schema_context: args.schema_context,
123+
})) as { error?: string; data?: Record<string, any> } | null
124+
const ed = (eq?.data ?? {}) as Record<string, any>
125+
if (ed.equivalent === true) {
126+
verified.push({ sql: c.sql, rule: c.rule, confidence: ed.confidence })
127+
} else {
128+
// Derive a specific reason. The gate stays strict (only `=== true` verifies);
129+
// this just explains WHY a rewrite is unverified so callers can act on it.
130+
const diffs = Array.isArray(ed.differences) ? ed.differences : []
131+
let reason: string
132+
if (eq?.error ?? ed.error) {
133+
reason = String(eq?.error ?? ed.error)
134+
} else if (!("equivalent" in ed)) {
135+
reason = "missing 'equivalent' field in equivalence response"
136+
} else if (typeof ed.equivalent !== "boolean") {
137+
reason = `non-boolean 'equivalent' value (${typeof ed.equivalent})`
138+
} else if (diffs.length) {
139+
reason = `not proven equivalent: ${diffs
140+
.map((d: any) => d?.description)
141+
.filter(Boolean)
142+
.slice(0, 2)
143+
.join("; ")}`
144+
} else {
145+
reason = "could not be proven equivalent"
146+
}
147+
unverified.push({ sql: c.sql, rule: c.rule, reason })
148+
}
149+
} catch (e) {
150+
const msg = e instanceof Error ? e.message : String(e)
151+
unverified.push({ sql: c.sql, rule: c.rule, reason: `equivalence check failed: ${msg}` })
152+
}
153+
}
154+
155+
return {
156+
title: `Rewrite (verified): ${verified.length} verified, ${unverified.length} unverified`,
157+
metadata: {
158+
success: true,
159+
rewrite_count: candidates.length,
160+
verified_count: verified.length,
161+
unverified_count: unverified.length,
162+
has_schema: hasSchema,
163+
},
164+
output: formatVerified(verified, unverified, hasSchema),
165+
}
166+
}
167+
168+
function formatVerified(
169+
verified: Array<{ sql: string; rule?: string; confidence?: number }>,
170+
unverified: Array<{ sql: string; rule?: string; reason: string }>,
171+
hasSchema: boolean,
172+
): string {
173+
const lines: string[] = []
174+
if (verified.length) {
175+
lines.push("✓ VERIFIED-EQUIVALENT optimizations (safe to apply — proven to return the same results):")
176+
for (const v of verified) {
177+
lines.push(` • ${v.rule ?? "rewrite"}${v.confidence != null ? ` (confidence ${v.confidence})` : ""}`)
178+
lines.push(` ${v.sql}`)
179+
}
180+
lines.push("")
181+
}
182+
if (unverified.length) {
183+
lines.push("⚠ UNVERIFIED optimizations (review before applying — equivalence NOT proven):")
184+
for (const u of unverified) {
185+
lines.push(` • ${u.rule ?? "rewrite"} — ${u.reason}`)
186+
lines.push(` ${u.sql}`)
187+
}
188+
lines.push("")
189+
}
190+
if (!verified.length && !unverified.length) return "No optimizations suggested for this query."
191+
if (!hasSchema) {
192+
lines.push(
193+
"Note: no schema was supplied, so no rewrite could be verified. Provide schema_context or schema_path to verify equivalence.",
194+
)
195+
} else if (!verified.length) {
196+
lines.push("Note: optimizations were found but none could be proven equivalent. Apply only after manual review.")
197+
}
198+
return lines.join("\n").trimEnd()
199+
}
200+
40201
function formatRewrite(data: Record<string, any>): string {
41202
if (data.error) return `Error: ${data.error}`
42203
const suggestions = data.suggestions ?? data.rewrites ?? []

0 commit comments

Comments
 (0)