From 30230f277fa9295c2624dcb0ed15883ee4393b92 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Mon, 8 Jun 2026 08:28:50 -0700 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20verified-optimize=20tool=20?= =?UTF-8?q?=E2=80=94=20surface=20only=20provably-equivalent=20query=20rewr?= =?UTF-8?q?ites?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- .../tools/altimate-verified-optimize.ts | 159 +++++++++++++ packages/opencode/src/tool/registry.ts | 2 + .../altimate-verified-optimize.test.ts | 219 ++++++++++++++++++ 3 files changed, 380 insertions(+) create mode 100644 packages/opencode/src/altimate/tools/altimate-verified-optimize.ts create mode 100644 packages/opencode/test/altimate/altimate-verified-optimize.test.ts diff --git a/packages/opencode/src/altimate/tools/altimate-verified-optimize.ts b/packages/opencode/src/altimate/tools/altimate-verified-optimize.ts new file mode 100644 index 000000000..8e6e4457f --- /dev/null +++ b/packages/opencode/src/altimate/tools/altimate-verified-optimize.ts @@ -0,0 +1,159 @@ +import z from "zod" +import { Tool } from "../../tool/tool" +import { Dispatcher } from "../native" + +/** + * Verified query optimization. + * + * Composes two altimate-core capabilities into one trust-preserving primitive: + * 1. `altimate_core.rewrite` — propose optimization rewrites of a query. + * 2. `altimate_core.equivalence` — prove each rewrite returns the same results. + * + * A rewrite is reported as VERIFIED only when equivalence is proven (`equivalent + * === true`). Anything the engine cannot prove equivalent — including rewrites it + * is merely unsure about, or any rewrite when no schema is supplied — is returned + * but explicitly labeled UNVERIFIED ("review before applying"). The gate is + * deliberately conservative: it never marks a rewrite verified unless the + * equivalence check affirmatively says so, so a "verified" optimization is safe + * to apply without changing results. + * + * This is the core mechanic behind one-click "optimize this query" UX: surface + * the savings, but only promise safety where it is actually provable. + */ +export const AltimateVerifiedOptimizeTool = Tool.define("altimate_verified_optimize", { + description: + "Suggest query optimizations that are PROVEN to preserve results. Runs altimate-core rewrite, then verifies each proposed rewrite is semantically equivalent to the original; only rewrites that pass the equivalence check are marked verified-safe to apply. Rewrites that cannot be proven equivalent are still returned but clearly labeled 'review before applying'. Provide schema_context or schema_path so table/column references can be resolved — without a schema, equivalence cannot be verified and all rewrites are returned unverified.", + parameters: z.object({ + sql: z.string().describe("SQL query to optimize"), + schema_path: z.string().optional().describe("Path to YAML/JSON schema file"), + schema_context: z.record(z.string(), z.any()).optional().describe("Inline schema definition"), + dialect: z.string().optional().describe("SQL dialect for the rewrite engine"), + }), + async execute(args) { + const hasSchema = !!(args.schema_path || (args.schema_context && Object.keys(args.schema_context).length > 0)) + const norm = (s: string) => s.replace(/\s+/g, " ").trim().toLowerCase() + try { + const rw = await Dispatcher.call("altimate_core.rewrite", { + sql: args.sql, + schema_path: args.schema_path ?? "", + schema_context: args.schema_context, + }) + const data = (rw.data ?? {}) as Record + const rwError = rw.error ?? data.error + if (rwError) { + return { + title: "Verified Optimize: ERROR", + metadata: { success: false, verified_count: 0, unverified_count: 0, error: rwError }, + output: `Failed to generate rewrites: ${rwError}`, + } + } + + // Collect candidate rewrites: the whole-query rewrite plus any per-suggestion + // rewrites. Drop blanks and no-ops (rewrite identical to the original). + const suggestions: any[] = data.suggestions ?? data.rewrites ?? [] + const raw: Array<{ sql: string; rule?: string; explanation?: string }> = [] + if (typeof data.rewritten_sql === "string" && data.rewritten_sql.trim()) { + raw.push({ sql: data.rewritten_sql }) + } + for (const s of suggestions) { + if (typeof s?.rewritten_sql === "string" && s.rewritten_sql.trim()) { + raw.push({ sql: s.rewritten_sql, rule: s.rule ?? s.type, explanation: s.explanation ?? s.description }) + } + } + const seen = new Set([norm(args.sql)]) + const candidates = raw.filter((c) => { + const n = norm(c.sql) + if (seen.has(n)) return false + seen.add(n) + return true + }) + + if (!candidates.length) { + return { + title: "Verified Optimize: no rewrites", + metadata: { success: true, verified_count: 0, unverified_count: 0, has_schema: hasSchema }, + output: "No optimizations suggested for this query.", + } + } + + // Verify each candidate against the original via the equivalence engine. + const verified: Array<{ sql: string; rule?: string; confidence?: number }> = [] + const unverified: Array<{ sql: string; rule?: string; reason: string }> = [] + for (const c of candidates) { + if (!hasSchema) { + unverified.push({ sql: c.sql, rule: c.rule, reason: "no schema supplied — equivalence cannot be verified" }) + continue + } + const eq = await Dispatcher.call("altimate_core.equivalence", { + sql1: args.sql, + sql2: c.sql, + schema_path: args.schema_path ?? "", + schema_context: args.schema_context, + }) + const ed = (eq.data ?? {}) as Record + if (ed.equivalent === true) { + verified.push({ sql: c.sql, rule: c.rule, confidence: ed.confidence }) + } else { + const diffs: any[] = ed.differences ?? [] + const reason = + (eq.error ?? ed.error) || + (diffs.length + ? `not proven equivalent: ${diffs.map((d) => d.description).filter(Boolean).slice(0, 2).join("; ")}` + : "could not be proven equivalent") + unverified.push({ sql: c.sql, rule: c.rule, reason }) + } + } + + return { + title: `Verified Optimize: ${verified.length} verified, ${unverified.length} unverified`, + metadata: { + success: true, + verified_count: verified.length, + unverified_count: unverified.length, + has_schema: hasSchema, + }, + output: formatVerifiedOptimize(verified, unverified, hasSchema), + } + } catch (e) { + const msg = e instanceof Error ? e.message : String(e) + return { + title: "Verified Optimize: ERROR", + metadata: { success: false, verified_count: 0, unverified_count: 0, error: msg }, + output: `Failed: ${msg}`, + } + } + }, +}) + +function formatVerifiedOptimize( + verified: Array<{ sql: string; rule?: string; confidence?: number }>, + unverified: Array<{ sql: string; rule?: string; reason: string }>, + hasSchema: boolean, +): string { + const lines: string[] = [] + if (verified.length) { + lines.push("✓ VERIFIED-EQUIVALENT optimizations (safe to apply — proven to return the same results):") + for (const v of verified) { + lines.push(` • ${v.rule ?? "rewrite"}${v.confidence != null ? ` (confidence ${v.confidence})` : ""}`) + lines.push(` ${v.sql}`) + } + lines.push("") + } + if (unverified.length) { + lines.push("⚠ UNVERIFIED optimizations (review before applying — equivalence NOT proven):") + for (const u of unverified) { + lines.push(` • ${u.rule ?? "rewrite"} — ${u.reason}`) + lines.push(` ${u.sql}`) + } + lines.push("") + } + if (!verified.length && !unverified.length) { + return "No optimizations suggested for this query." + } + if (!hasSchema) { + lines.push("Note: no schema was supplied, so no rewrite could be verified. Provide schema_context or schema_path to verify equivalence.") + } else if (!verified.length) { + lines.push("Note: optimizations were found but none could be proven equivalent. Apply only after manual review.") + } + return lines.join("\n").trimEnd() +} diff --git a/packages/opencode/src/tool/registry.ts b/packages/opencode/src/tool/registry.ts index 0a20269b0..893620684 100644 --- a/packages/opencode/src/tool/registry.ts +++ b/packages/opencode/src/tool/registry.ts @@ -107,6 +107,7 @@ import { AltimateCoreFingerprintTool } from "../altimate/tools/altimate-core-fin import { AltimateCoreIntrospectionSqlTool } from "../altimate/tools/altimate-core-introspection-sql" import { AltimateCoreParseDbtTool } from "../altimate/tools/altimate-core-parse-dbt" import { AltimateCoreRewriteTool } from "../altimate/tools/altimate-core-rewrite" +import { AltimateVerifiedOptimizeTool } from "../altimate/tools/altimate-verified-optimize" import { ToolLookupTool } from "../altimate/tools/tool-lookup" import { ProjectScanTool } from "../altimate/tools/project-scan" import { DatamateManagerTool } from "../altimate/tools/datamate" @@ -270,6 +271,7 @@ export namespace ToolRegistry { SchemaTagsListTool, SqlRewriteTool, AltimateCoreRewriteTool, + AltimateVerifiedOptimizeTool, SchemaDiffTool, AltimateCoreValidateTool, AltimateCoreCheckTool, diff --git a/packages/opencode/test/altimate/altimate-verified-optimize.test.ts b/packages/opencode/test/altimate/altimate-verified-optimize.test.ts new file mode 100644 index 000000000..d26c10d14 --- /dev/null +++ b/packages/opencode/test/altimate/altimate-verified-optimize.test.ts @@ -0,0 +1,219 @@ +/** + * Tests for the verified-optimize tool: rewrite -> prove-equivalent -> gate. + * + * The load-bearing property is the GATE: a rewrite may be reported VERIFIED only + * when the equivalence engine affirmatively returns `equivalent === true`. Any + * other verdict (false, undefined, truthy-but-not-true, error, or no schema) MUST + * land in the unverified bucket — otherwise the tool could promise "safe to apply" + * on a rewrite that changes results. The adversarial block below pins that. + */ +import { describe, expect, test, beforeAll, afterAll, beforeEach } from "bun:test" +import * as Dispatcher from "../../src/altimate/native/dispatcher" +// Import the native index so its setRegistrationHook() runs NOW (before beforeAll). +// Otherwise the lazy hook is armed only when the tool first imports ../native +// mid-test, then fires on the next call and overwrites our mocks with the real +// handlers. Arming it up front lets beforeAll's __trigger_hook__ fire+disarm it. +import "../../src/altimate/native" + +beforeAll(async () => { + process.env.ALTIMATE_TELEMETRY_DISABLED = "true" + try { + await Dispatcher.call("__trigger_hook__" as any, {} as any) + } catch {} +}) +afterAll(() => { + delete process.env.ALTIMATE_TELEMETRY_DISABLED +}) + +function stubCtx(): any { + return { + sessionID: "test", + messageID: "test", + agent: "test", + abort: new AbortController().signal, + messages: [], + metadata: () => {}, + } +} + +const SCHEMA = { customers: { customer_id: "INTEGER", first_name: "VARCHAR" } } +const ORIGINAL = "SELECT customer_id FROM (SELECT * FROM customers) s WHERE customer_id = 1" +const REWRITE = "SELECT customer_id FROM customers WHERE customer_id = 1" + +async function runTool(args: any) { + const { AltimateVerifiedOptimizeTool } = await import("../../src/altimate/tools/altimate-verified-optimize") + const tool = await AltimateVerifiedOptimizeTool.init() + return tool.execute(args, stubCtx()) +} + +// Register a rewrite handler that returns one candidate rewrite, and an +// equivalence handler with the given verdict. +function mockRewriteAndEquivalence(rewrittenSql: string | undefined, equivalence: any) { + Dispatcher.register("altimate_core.rewrite" as any, async () => ({ + success: true, + data: rewrittenSql + ? { original_sql: ORIGINAL, rewritten_sql: rewrittenSql, suggestions: [{ rule: "FLATTEN_SUBQUERY", rewritten_sql: rewrittenSql }] } + : { original_sql: ORIGINAL, suggestions: [] }, + })) + Dispatcher.register("altimate_core.equivalence" as any, async () => ({ success: true, data: equivalence })) +} + +describe("verified-optimize — gate logic (mocked engine)", () => { + beforeEach(() => Dispatcher.reset()) + + test("proven-equivalent rewrite is VERIFIED", async () => { + mockRewriteAndEquivalence(REWRITE, { equivalent: true, confidence: 1 }) + const r = await runTool({ sql: ORIGINAL, schema_context: SCHEMA }) + expect(r.metadata.verified_count).toBe(1) + expect(r.metadata.unverified_count).toBe(0) + expect(String(r.output)).toContain("VERIFIED-EQUIVALENT") + expect(String(r.output)).toContain(REWRITE) + }) + + test("not-equivalent rewrite is UNVERIFIED with a reason", async () => { + mockRewriteAndEquivalence(REWRITE, { + equivalent: false, + confidence: 0.5, + differences: [{ description: "filters differ" }], + }) + const r = await runTool({ sql: ORIGINAL, schema_context: SCHEMA }) + expect(r.metadata.verified_count).toBe(0) + expect(r.metadata.unverified_count).toBe(1) + expect(String(r.output)).toContain("UNVERIFIED") + expect(String(r.output)).toContain("filters differ") + }) + + test("no schema -> rewrite returned but UNVERIFIED (equivalence not attempted)", async () => { + let equivalenceCalled = false + Dispatcher.register("altimate_core.rewrite" as any, async () => ({ + success: true, + data: { rewritten_sql: REWRITE, suggestions: [] }, + })) + Dispatcher.register("altimate_core.equivalence" as any, async () => { + equivalenceCalled = true + return { success: true, data: { equivalent: true } } + }) + const r = await runTool({ sql: ORIGINAL }) // no schema + expect(r.metadata.has_schema).toBe(false) + expect(r.metadata.verified_count).toBe(0) + expect(r.metadata.unverified_count).toBe(1) + expect(equivalenceCalled).toBe(false) // never claim verified without a schema + expect(String(r.output)).toContain("no schema") + }) + + test("no-op rewrite (identical to original) is filtered out", async () => { + mockRewriteAndEquivalence(ORIGINAL, { equivalent: true }) + const r = await runTool({ sql: ORIGINAL, schema_context: SCHEMA }) + expect(r.metadata.verified_count).toBe(0) + expect(r.metadata.unverified_count).toBe(0) + expect(String(r.output)).toContain("No optimizations") + }) + + test("no suggestions -> no optimizations", async () => { + mockRewriteAndEquivalence(undefined, { equivalent: true }) + const r = await runTool({ sql: ORIGINAL, schema_context: SCHEMA }) + expect(r.metadata.verified_count).toBe(0) + expect(String(r.output)).toContain("No optimizations") + }) + + test("rewrite-engine error -> error result, never a false verified", async () => { + Dispatcher.register("altimate_core.rewrite" as any, async () => ({ success: false, error: "engine down" })) + const r = await runTool({ sql: ORIGINAL, schema_context: SCHEMA }) + expect(r.metadata.success).toBe(false) + expect(r.metadata.verified_count).toBe(0) + expect(String(r.output)).toContain("engine down") + }) + + test("duplicate candidates are de-duplicated", async () => { + Dispatcher.register("altimate_core.rewrite" as any, async () => ({ + success: true, + data: { + rewritten_sql: REWRITE, + suggestions: [{ rewritten_sql: REWRITE }, { rewritten_sql: " " + REWRITE + " " }], + }, + })) + Dispatcher.register("altimate_core.equivalence" as any, async () => ({ success: true, data: { equivalent: true } })) + const r = await runTool({ sql: ORIGINAL, schema_context: SCHEMA }) + expect(r.metadata.verified_count).toBe(1) // not 3 + }) + + test("mixed: one verified + one unverified", async () => { + const r2 = "SELECT customer_id FROM customers WHERE customer_id = 2" + Dispatcher.register("altimate_core.rewrite" as any, async () => ({ + success: true, + data: { suggestions: [{ rewritten_sql: REWRITE }, { rewritten_sql: r2 }] }, + })) + Dispatcher.register("altimate_core.equivalence" as any, async (p: any) => ({ + success: true, + data: { equivalent: p.sql2 === REWRITE }, // only the first is equivalent + })) + const r = await runTool({ sql: ORIGINAL, schema_context: SCHEMA }) + expect(r.metadata.verified_count).toBe(1) + expect(r.metadata.unverified_count).toBe(1) + }) +}) + +describe("verified-optimize — ADVERSARIAL: gate only trusts strict equivalent===true", () => { + beforeEach(() => Dispatcher.reset()) + // Anything other than the boolean true must NOT be reported as verified. + for (const [label, verdict] of [ + ["string 'true'", "true"], + ["number 1", 1], + ["undefined", undefined], + ["null", null], + ["object truthy", {}], + ] as [string, any][]) { + test(`equivalent=${label} -> UNVERIFIED (never trusted)`, async () => { + mockRewriteAndEquivalence(REWRITE, { equivalent: verdict }) + const r = await runTool({ sql: ORIGINAL, schema_context: SCHEMA }) + expect(r.metadata.verified_count).toBe(0) + expect(r.metadata.unverified_count).toBe(1) + }) + } + + test("equivalence handler throws -> tool fails safe (no verified), does not crash", async () => { + Dispatcher.register("altimate_core.rewrite" as any, async () => ({ + success: true, + data: { rewritten_sql: REWRITE, suggestions: [] }, + })) + Dispatcher.register("altimate_core.equivalence" as any, async () => { + throw new Error("equiv boom") + }) + const r = await runTool({ sql: ORIGINAL, schema_context: SCHEMA }) + expect(r.metadata.verified_count).toBe(0) + expect(typeof r.output).toBe("string") + }) +}) + +// --- real engine integration (skips if altimate-core native binary absent) ---- +let coreAvailable = false +try { + require.resolve("@altimateai/altimate-core") + coreAvailable = true +} catch {} +const describeIf = coreAvailable ? describe : describe.skip + +describeIf("verified-optimize — real altimate-core integration", () => { + beforeAll(async () => { + process.env.ALTIMATE_TELEMETRY_DISABLED = "true" + const core = await import("../../src/altimate/native/altimate-core") + const sql = await import("../../src/altimate/native/sql/register") + core.registerAll() + sql.registerAllSql() + }) + + test("runs end-to-end and returns a well-formed, internally-consistent result", async () => { + const r = await runTool({ + sql: "SELECT customer_id, first_name FROM customers WHERE customer_id = 1", + schema_context: SCHEMA, + }) + expect(typeof r.output).toBe("string") + expect(r.metadata.has_schema).toBe(true) + expect(typeof r.metadata.verified_count).toBe("number") + expect(typeof r.metadata.unverified_count).toBe("number") + // Counts must be non-negative and the title must reflect them. + expect(r.metadata.verified_count).toBeGreaterThanOrEqual(0) + expect(r.metadata.unverified_count).toBeGreaterThanOrEqual(0) + expect(r.title).toContain("Verified Optimize") // robust to the no-rewrites path + }) +}) From 017a40d29d8ab567bbdbd5140efe5c032a0abd84 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Mon, 8 Jun 2026 13:09:57 -0700 Subject: [PATCH 2/3] refactor: fold verified optimization into altimate_core_rewrite (verify_equivalence) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- .opencode/skills/query-optimize/SKILL.md | 18 +- .../altimate/tools/altimate-core-rewrite.ts | 166 +++++++++++++++++- .../tools/altimate-verified-optimize.ts | 159 ----------------- packages/opencode/src/tool/registry.ts | 2 - ...s => altimate-core-rewrite-verify.test.ts} | 54 +++++- 5 files changed, 217 insertions(+), 182 deletions(-) delete mode 100644 packages/opencode/src/altimate/tools/altimate-verified-optimize.ts rename packages/opencode/test/altimate/{altimate-verified-optimize.test.ts => altimate-core-rewrite-verify.test.ts} (78%) diff --git a/.opencode/skills/query-optimize/SKILL.md b/.opencode/skills/query-optimize/SKILL.md index df3ebdac3..6d1dd6d6d 100644 --- a/.opencode/skills/query-optimize/SKILL.md +++ b/.opencode/skills/query-optimize/SKILL.md @@ -7,7 +7,7 @@ description: Analyze and optimize SQL queries for better performance ## Requirements **Agent:** any (read-only analysis) -**Tools used:** sql_optimize, sql_analyze, sql_explain, altimate_core_equivalence, read, glob, schema_inspect, warehouse_list +**Tools used:** altimate_core_rewrite (with `verify_equivalence: true`), sql_analyze, sql_explain, read, glob, schema_inspect, warehouse_list Analyze SQL queries for performance issues and suggest concrete optimizations including rewritten SQL. @@ -20,9 +20,9 @@ Analyze SQL queries for performance issues and suggest concrete optimizations in 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. -3. **Run the optimizer**: - - Call `sql_optimize` with the SQL, dialect, and schema context if available - - 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) +3. **Run the verified optimizer**: + - 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) + - 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. 4. **Run detailed analysis**: - 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 - Look for: full table scans, sort operations on large datasets, inefficient join strategies, missing partition pruning - Include key findings in the report under "Execution Plan Insights" -6. **Verify rewrites preserve correctness**: - - If `sql_optimize` produced a rewritten query, call `altimate_core_equivalence` to verify the original and optimized queries produce the same result set - - If not equivalent, flag the difference and present both versions for the user to decide - - This prevents "optimization" that silently changes query semantics +6. **Equivalence verification is built into step 3** (`verify_equivalence: true`): + - Present the **verified-equivalent** rewrites as safe to apply. + - Present **unverified** rewrites separately with their reason ("review before applying") — do not recommend applying these without manual review. + - If no schema was available, all rewrites come back unverified; say so and recommend supplying a schema (or a warehouse connection) to enable verification. 7. **Present findings** in a structured format: @@ -83,4 +83,4 @@ The user invokes this skill with SQL or a file path: - `/query-optimize models/staging/stg_orders.sql` -- Optimize SQL from a file - `/query-optimize` -- Optimize the most recently discussed SQL in the conversation -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). +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). diff --git a/packages/opencode/src/altimate/tools/altimate-core-rewrite.ts b/packages/opencode/src/altimate/tools/altimate-core-rewrite.ts index 44f657bfc..0665eabbf 100644 --- a/packages/opencode/src/altimate/tools/altimate-core-rewrite.ts +++ b/packages/opencode/src/altimate/tools/altimate-core-rewrite.ts @@ -4,26 +4,47 @@ import { Dispatcher } from "../native" export const AltimateCoreRewriteTool = Tool.define("altimate_core_rewrite", { description: - "Suggest query optimization rewrites. Analyzes SQL and proposes concrete rewrites for better performance. Provide schema_context or schema_path for accurate table/column resolution.", + "Suggest query optimization rewrites. Analyzes SQL and proposes concrete rewrites for better performance. Provide schema_context or schema_path for accurate table/column resolution. " + + "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.", parameters: z.object({ sql: z.string().describe("SQL query to optimize"), schema_path: z.string().optional().describe("Path to YAML/JSON schema file"), schema_context: z.record(z.string(), z.any()).optional().describe("Inline schema definition"), + verify_equivalence: z + .boolean() + .optional() + .describe( + "If true, verify each rewrite is semantically equivalent to the original before recommending it (requires a schema).", + ), }), - async execute(args, ctx) { + async execute(args) { try { - const result = await Dispatcher.call("altimate_core.rewrite", { + const result = (await Dispatcher.call("altimate_core.rewrite", { sql: args.sql, schema_path: args.schema_path ?? "", schema_context: args.schema_context, - }) - const data = (result.data ?? {}) as Record + })) as { success?: boolean; error?: string; data?: Record } | null + const data = (result?.data ?? {}) as Record + const error = result?.error ?? data.error + if (error) { + return { + title: "Rewrite: ERROR", + metadata: { success: false, rewrite_count: 0, verified_count: 0, unverified_count: 0, error }, + output: `Failed to generate rewrites: ${error}`, + } + } + const suggestions = data.suggestions ?? data.rewrites ?? [] const rewriteCount = suggestions.length || (data.rewritten_sql && data.rewritten_sql !== args.sql ? 1 : 0) - const error = result.error ?? data.error + + // Verified mode: gate each rewrite on proven semantic equivalence to the original. + if (args.verify_equivalence) { + return await verifyRewrites(args, data) + } + return { title: `Rewrite: ${rewriteCount} suggestion(s)`, - metadata: { success: result.success, rewrite_count: rewriteCount, ...(error && { error }) }, + metadata: { success: result?.success, rewrite_count: rewriteCount }, output: formatRewrite(data), } } catch (e) { @@ -37,6 +58,137 @@ export const AltimateCoreRewriteTool = Tool.define("altimate_core_rewrite", { }, }) +/** + * Verify each candidate rewrite against the original via altimate-core equivalence. + * A rewrite is VERIFIED only when equivalence affirmatively returns `equivalent === true` + * (strict boolean — never a truthy coercion). Everything else — not-equivalent, + * can't-decide, a thrown equivalence check, or no schema — is returned UNVERIFIED + * ("review before applying"). Conservative by design: a verified rewrite is safe to + * apply without changing results. + */ +async function verifyRewrites( + args: { sql: string; schema_path?: string; schema_context?: Record }, + data: Record, +) { + const hasSchema = !!(args.schema_path || (args.schema_context && Object.keys(args.schema_context).length > 0)) + const norm = (s: string) => s.replace(/\s+/g, " ").trim().toLowerCase() + + // Collect candidate rewrites (whole-query + per-suggestion), drop blanks/no-ops/dupes. + const suggestions: any[] = data.suggestions ?? data.rewrites ?? [] + const raw: Array<{ sql: string; rule?: string }> = [] + if (typeof data.rewritten_sql === "string" && data.rewritten_sql.trim()) raw.push({ sql: data.rewritten_sql }) + for (const s of suggestions) { + if (typeof s?.rewritten_sql === "string" && s.rewritten_sql.trim()) { + raw.push({ sql: s.rewritten_sql, rule: s.rule ?? s.type }) + } + } + const seen = new Set([norm(args.sql)]) + const candidates = raw.filter((c) => { + const n = norm(c.sql) + if (seen.has(n)) return false + seen.add(n) + return true + }) + + if (!candidates.length) { + return { + title: "Rewrite: no rewrites", + metadata: { success: true, rewrite_count: 0, verified_count: 0, unverified_count: 0, has_schema: hasSchema }, + output: "No optimizations suggested for this query.", + } + } + + const verified: Array<{ sql: string; rule?: string; confidence?: number }> = [] + const unverified: Array<{ sql: string; rule?: string; reason: string }> = [] + for (const c of candidates) { + if (!hasSchema) { + unverified.push({ sql: c.sql, rule: c.rule, reason: "no schema supplied — equivalence cannot be verified" }) + continue + } + try { + const eq = (await Dispatcher.call("altimate_core.equivalence", { + sql1: args.sql, + sql2: c.sql, + schema_path: args.schema_path ?? "", + schema_context: args.schema_context, + })) as { error?: string; data?: Record } | null + const ed = (eq?.data ?? {}) as Record + if (ed.equivalent === true) { + verified.push({ sql: c.sql, rule: c.rule, confidence: ed.confidence }) + } else { + // Derive a specific reason. The gate stays strict (only `=== true` verifies); + // this just explains WHY a rewrite is unverified so callers can act on it. + const diffs = Array.isArray(ed.differences) ? ed.differences : [] + let reason: string + if (eq?.error ?? ed.error) { + reason = String(eq?.error ?? ed.error) + } else if (!("equivalent" in ed)) { + reason = "missing 'equivalent' field in equivalence response" + } else if (typeof ed.equivalent !== "boolean") { + reason = `non-boolean 'equivalent' value (${typeof ed.equivalent})` + } else if (diffs.length) { + reason = `not proven equivalent: ${diffs + .map((d: any) => d?.description) + .filter(Boolean) + .slice(0, 2) + .join("; ")}` + } else { + reason = "could not be proven equivalent" + } + unverified.push({ sql: c.sql, rule: c.rule, reason }) + } + } catch (e) { + const msg = e instanceof Error ? e.message : String(e) + unverified.push({ sql: c.sql, rule: c.rule, reason: `equivalence check failed: ${msg}` }) + } + } + + return { + title: `Rewrite (verified): ${verified.length} verified, ${unverified.length} unverified`, + metadata: { + success: true, + rewrite_count: candidates.length, + verified_count: verified.length, + unverified_count: unverified.length, + has_schema: hasSchema, + }, + output: formatVerified(verified, unverified, hasSchema), + } +} + +function formatVerified( + verified: Array<{ sql: string; rule?: string; confidence?: number }>, + unverified: Array<{ sql: string; rule?: string; reason: string }>, + hasSchema: boolean, +): string { + const lines: string[] = [] + if (verified.length) { + lines.push("✓ VERIFIED-EQUIVALENT optimizations (safe to apply — proven to return the same results):") + for (const v of verified) { + lines.push(` • ${v.rule ?? "rewrite"}${v.confidence != null ? ` (confidence ${v.confidence})` : ""}`) + lines.push(` ${v.sql}`) + } + lines.push("") + } + if (unverified.length) { + lines.push("⚠ UNVERIFIED optimizations (review before applying — equivalence NOT proven):") + for (const u of unverified) { + lines.push(` • ${u.rule ?? "rewrite"} — ${u.reason}`) + lines.push(` ${u.sql}`) + } + lines.push("") + } + if (!verified.length && !unverified.length) return "No optimizations suggested for this query." + if (!hasSchema) { + lines.push( + "Note: no schema was supplied, so no rewrite could be verified. Provide schema_context or schema_path to verify equivalence.", + ) + } else if (!verified.length) { + lines.push("Note: optimizations were found but none could be proven equivalent. Apply only after manual review.") + } + return lines.join("\n").trimEnd() +} + function formatRewrite(data: Record): string { if (data.error) return `Error: ${data.error}` const suggestions = data.suggestions ?? data.rewrites ?? [] diff --git a/packages/opencode/src/altimate/tools/altimate-verified-optimize.ts b/packages/opencode/src/altimate/tools/altimate-verified-optimize.ts deleted file mode 100644 index 8e6e4457f..000000000 --- a/packages/opencode/src/altimate/tools/altimate-verified-optimize.ts +++ /dev/null @@ -1,159 +0,0 @@ -import z from "zod" -import { Tool } from "../../tool/tool" -import { Dispatcher } from "../native" - -/** - * Verified query optimization. - * - * Composes two altimate-core capabilities into one trust-preserving primitive: - * 1. `altimate_core.rewrite` — propose optimization rewrites of a query. - * 2. `altimate_core.equivalence` — prove each rewrite returns the same results. - * - * A rewrite is reported as VERIFIED only when equivalence is proven (`equivalent - * === true`). Anything the engine cannot prove equivalent — including rewrites it - * is merely unsure about, or any rewrite when no schema is supplied — is returned - * but explicitly labeled UNVERIFIED ("review before applying"). The gate is - * deliberately conservative: it never marks a rewrite verified unless the - * equivalence check affirmatively says so, so a "verified" optimization is safe - * to apply without changing results. - * - * This is the core mechanic behind one-click "optimize this query" UX: surface - * the savings, but only promise safety where it is actually provable. - */ -export const AltimateVerifiedOptimizeTool = Tool.define("altimate_verified_optimize", { - description: - "Suggest query optimizations that are PROVEN to preserve results. Runs altimate-core rewrite, then verifies each proposed rewrite is semantically equivalent to the original; only rewrites that pass the equivalence check are marked verified-safe to apply. Rewrites that cannot be proven equivalent are still returned but clearly labeled 'review before applying'. Provide schema_context or schema_path so table/column references can be resolved — without a schema, equivalence cannot be verified and all rewrites are returned unverified.", - parameters: z.object({ - sql: z.string().describe("SQL query to optimize"), - schema_path: z.string().optional().describe("Path to YAML/JSON schema file"), - schema_context: z.record(z.string(), z.any()).optional().describe("Inline schema definition"), - dialect: z.string().optional().describe("SQL dialect for the rewrite engine"), - }), - async execute(args) { - const hasSchema = !!(args.schema_path || (args.schema_context && Object.keys(args.schema_context).length > 0)) - const norm = (s: string) => s.replace(/\s+/g, " ").trim().toLowerCase() - try { - const rw = await Dispatcher.call("altimate_core.rewrite", { - sql: args.sql, - schema_path: args.schema_path ?? "", - schema_context: args.schema_context, - }) - const data = (rw.data ?? {}) as Record - const rwError = rw.error ?? data.error - if (rwError) { - return { - title: "Verified Optimize: ERROR", - metadata: { success: false, verified_count: 0, unverified_count: 0, error: rwError }, - output: `Failed to generate rewrites: ${rwError}`, - } - } - - // Collect candidate rewrites: the whole-query rewrite plus any per-suggestion - // rewrites. Drop blanks and no-ops (rewrite identical to the original). - const suggestions: any[] = data.suggestions ?? data.rewrites ?? [] - const raw: Array<{ sql: string; rule?: string; explanation?: string }> = [] - if (typeof data.rewritten_sql === "string" && data.rewritten_sql.trim()) { - raw.push({ sql: data.rewritten_sql }) - } - for (const s of suggestions) { - if (typeof s?.rewritten_sql === "string" && s.rewritten_sql.trim()) { - raw.push({ sql: s.rewritten_sql, rule: s.rule ?? s.type, explanation: s.explanation ?? s.description }) - } - } - const seen = new Set([norm(args.sql)]) - const candidates = raw.filter((c) => { - const n = norm(c.sql) - if (seen.has(n)) return false - seen.add(n) - return true - }) - - if (!candidates.length) { - return { - title: "Verified Optimize: no rewrites", - metadata: { success: true, verified_count: 0, unverified_count: 0, has_schema: hasSchema }, - output: "No optimizations suggested for this query.", - } - } - - // Verify each candidate against the original via the equivalence engine. - const verified: Array<{ sql: string; rule?: string; confidence?: number }> = [] - const unverified: Array<{ sql: string; rule?: string; reason: string }> = [] - for (const c of candidates) { - if (!hasSchema) { - unverified.push({ sql: c.sql, rule: c.rule, reason: "no schema supplied — equivalence cannot be verified" }) - continue - } - const eq = await Dispatcher.call("altimate_core.equivalence", { - sql1: args.sql, - sql2: c.sql, - schema_path: args.schema_path ?? "", - schema_context: args.schema_context, - }) - const ed = (eq.data ?? {}) as Record - if (ed.equivalent === true) { - verified.push({ sql: c.sql, rule: c.rule, confidence: ed.confidence }) - } else { - const diffs: any[] = ed.differences ?? [] - const reason = - (eq.error ?? ed.error) || - (diffs.length - ? `not proven equivalent: ${diffs.map((d) => d.description).filter(Boolean).slice(0, 2).join("; ")}` - : "could not be proven equivalent") - unverified.push({ sql: c.sql, rule: c.rule, reason }) - } - } - - return { - title: `Verified Optimize: ${verified.length} verified, ${unverified.length} unverified`, - metadata: { - success: true, - verified_count: verified.length, - unverified_count: unverified.length, - has_schema: hasSchema, - }, - output: formatVerifiedOptimize(verified, unverified, hasSchema), - } - } catch (e) { - const msg = e instanceof Error ? e.message : String(e) - return { - title: "Verified Optimize: ERROR", - metadata: { success: false, verified_count: 0, unverified_count: 0, error: msg }, - output: `Failed: ${msg}`, - } - } - }, -}) - -function formatVerifiedOptimize( - verified: Array<{ sql: string; rule?: string; confidence?: number }>, - unverified: Array<{ sql: string; rule?: string; reason: string }>, - hasSchema: boolean, -): string { - const lines: string[] = [] - if (verified.length) { - lines.push("✓ VERIFIED-EQUIVALENT optimizations (safe to apply — proven to return the same results):") - for (const v of verified) { - lines.push(` • ${v.rule ?? "rewrite"}${v.confidence != null ? ` (confidence ${v.confidence})` : ""}`) - lines.push(` ${v.sql}`) - } - lines.push("") - } - if (unverified.length) { - lines.push("⚠ UNVERIFIED optimizations (review before applying — equivalence NOT proven):") - for (const u of unverified) { - lines.push(` • ${u.rule ?? "rewrite"} — ${u.reason}`) - lines.push(` ${u.sql}`) - } - lines.push("") - } - if (!verified.length && !unverified.length) { - return "No optimizations suggested for this query." - } - if (!hasSchema) { - lines.push("Note: no schema was supplied, so no rewrite could be verified. Provide schema_context or schema_path to verify equivalence.") - } else if (!verified.length) { - lines.push("Note: optimizations were found but none could be proven equivalent. Apply only after manual review.") - } - return lines.join("\n").trimEnd() -} diff --git a/packages/opencode/src/tool/registry.ts b/packages/opencode/src/tool/registry.ts index 893620684..0a20269b0 100644 --- a/packages/opencode/src/tool/registry.ts +++ b/packages/opencode/src/tool/registry.ts @@ -107,7 +107,6 @@ import { AltimateCoreFingerprintTool } from "../altimate/tools/altimate-core-fin import { AltimateCoreIntrospectionSqlTool } from "../altimate/tools/altimate-core-introspection-sql" import { AltimateCoreParseDbtTool } from "../altimate/tools/altimate-core-parse-dbt" import { AltimateCoreRewriteTool } from "../altimate/tools/altimate-core-rewrite" -import { AltimateVerifiedOptimizeTool } from "../altimate/tools/altimate-verified-optimize" import { ToolLookupTool } from "../altimate/tools/tool-lookup" import { ProjectScanTool } from "../altimate/tools/project-scan" import { DatamateManagerTool } from "../altimate/tools/datamate" @@ -271,7 +270,6 @@ export namespace ToolRegistry { SchemaTagsListTool, SqlRewriteTool, AltimateCoreRewriteTool, - AltimateVerifiedOptimizeTool, SchemaDiffTool, AltimateCoreValidateTool, AltimateCoreCheckTool, diff --git a/packages/opencode/test/altimate/altimate-verified-optimize.test.ts b/packages/opencode/test/altimate/altimate-core-rewrite-verify.test.ts similarity index 78% rename from packages/opencode/test/altimate/altimate-verified-optimize.test.ts rename to packages/opencode/test/altimate/altimate-core-rewrite-verify.test.ts index d26c10d14..1ad20e8d5 100644 --- a/packages/opencode/test/altimate/altimate-verified-optimize.test.ts +++ b/packages/opencode/test/altimate/altimate-core-rewrite-verify.test.ts @@ -40,10 +40,12 @@ const SCHEMA = { customers: { customer_id: "INTEGER", first_name: "VARCHAR" } } const ORIGINAL = "SELECT customer_id FROM (SELECT * FROM customers) s WHERE customer_id = 1" const REWRITE = "SELECT customer_id FROM customers WHERE customer_id = 1" +// Exercise altimate_core_rewrite in VERIFY mode (verify_equivalence: true) — the +// verified-optimization path folded into the rewrite tool. async function runTool(args: any) { - const { AltimateVerifiedOptimizeTool } = await import("../../src/altimate/tools/altimate-verified-optimize") - const tool = await AltimateVerifiedOptimizeTool.init() - return tool.execute(args, stubCtx()) + const { AltimateCoreRewriteTool } = await import("../../src/altimate/tools/altimate-core-rewrite") + const tool = await AltimateCoreRewriteTool.init() + return tool.execute({ ...args, verify_equivalence: true }, stubCtx()) } // Register a rewrite handler that returns one candidate rewrite, and an @@ -52,7 +54,11 @@ function mockRewriteAndEquivalence(rewrittenSql: string | undefined, equivalence Dispatcher.register("altimate_core.rewrite" as any, async () => ({ success: true, data: rewrittenSql - ? { original_sql: ORIGINAL, rewritten_sql: rewrittenSql, suggestions: [{ rule: "FLATTEN_SUBQUERY", rewritten_sql: rewrittenSql }] } + ? { + original_sql: ORIGINAL, + rewritten_sql: rewrittenSql, + suggestions: [{ rule: "FLATTEN_SUBQUERY", rewritten_sql: rewrittenSql }], + } : { original_sql: ORIGINAL, suggestions: [] }, })) Dispatcher.register("altimate_core.equivalence" as any, async () => ({ success: true, data: equivalence })) @@ -151,6 +157,28 @@ describe("verified-optimize — gate logic (mocked engine)", () => { expect(r.metadata.verified_count).toBe(1) expect(r.metadata.unverified_count).toBe(1) }) + + test("partial success: one succeeds, one throws, remaining checked -> returns all results", async () => { + const r2 = "SELECT customer_id FROM customers WHERE customer_id = 2" + const r3 = "SELECT customer_id FROM customers WHERE customer_id = 3" + Dispatcher.register("altimate_core.rewrite" as any, async () => ({ + success: true, + data: { suggestions: [{ rewritten_sql: REWRITE }, { rewritten_sql: r2 }, { rewritten_sql: r3 }] }, + })) + let callCount = 0 + Dispatcher.register("altimate_core.equivalence" as any, async () => { + callCount++ + if (callCount === 1) return { success: true, data: { equivalent: true } } + if (callCount === 2) throw new Error("equiv boom") + return { success: true, data: { equivalent: false } } + }) + const r = await runTool({ sql: ORIGINAL, schema_context: SCHEMA }) + expect(r.metadata.verified_count).toBe(1) + expect(r.metadata.unverified_count).toBe(2) + expect(r.metadata.success).toBe(true) + expect(String(r.output)).toContain(REWRITE) + expect(String(r.output)).toContain("equiv boom") + }) }) describe("verified-optimize — ADVERSARIAL: gate only trusts strict equivalent===true", () => { @@ -183,6 +211,22 @@ describe("verified-optimize — ADVERSARIAL: gate only trusts strict equivalent= expect(r.metadata.verified_count).toBe(0) expect(typeof r.output).toBe("string") }) + + test("missing 'equivalent' field -> UNVERIFIED with clear error", async () => { + mockRewriteAndEquivalence(REWRITE, { differences: [] }) + const r = await runTool({ sql: ORIGINAL, schema_context: SCHEMA }) + expect(r.metadata.verified_count).toBe(0) + expect(r.metadata.unverified_count).toBe(1) + expect(String(r.output)).toContain("missing 'equivalent' field") + }) + + test("non-boolean 'equivalent' value -> UNVERIFIED with type error", async () => { + mockRewriteAndEquivalence(REWRITE, { equivalent: "yes" }) + const r = await runTool({ sql: ORIGINAL, schema_context: SCHEMA }) + expect(r.metadata.verified_count).toBe(0) + expect(r.metadata.unverified_count).toBe(1) + expect(String(r.output)).toContain("non-boolean 'equivalent' value") + }) }) // --- real engine integration (skips if altimate-core native binary absent) ---- @@ -214,6 +258,6 @@ describeIf("verified-optimize — real altimate-core integration", () => { // Counts must be non-negative and the title must reflect them. expect(r.metadata.verified_count).toBeGreaterThanOrEqual(0) expect(r.metadata.unverified_count).toBeGreaterThanOrEqual(0) - expect(r.title).toContain("Verified Optimize") // robust to the no-rewrites path + expect(r.title).toContain("Rewrite") // "Rewrite (verified): …" or "Rewrite: no rewrites" }) }) From b4bea7ffc9aa430eefa98544326a9985ff36a579 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Tue, 9 Jun 2026 17:56:21 -0700 Subject: [PATCH 3/3] fix: address review on verify_equivalence mode (coderabbit + multi-persona) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- .../altimate/tools/altimate-core-rewrite.ts | 15 ++++++++--- .../altimate-core-rewrite-verify.test.ts | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/altimate/tools/altimate-core-rewrite.ts b/packages/opencode/src/altimate/tools/altimate-core-rewrite.ts index 0665eabbf..307df090e 100644 --- a/packages/opencode/src/altimate/tools/altimate-core-rewrite.ts +++ b/packages/opencode/src/altimate/tools/altimate-core-rewrite.ts @@ -25,8 +25,12 @@ export const AltimateCoreRewriteTool = Tool.define("altimate_core_rewrite", { schema_context: args.schema_context, })) as { success?: boolean; error?: string; data?: Record } | null const data = (result?.data ?? {}) as Record - const error = result?.error ?? data.error - if (error) { + // Treat an explicit `success === false` as a failure even when the error + // string is empty/absent — otherwise an `{ success: false, error: "" }` + // payload would fall through to the success path and misreport results. + const failed = result?.success === false || !!(result?.error ?? data.error) + if (failed) { + const error = result?.error ?? data.error ?? "rewrite failed" return { title: "Rewrite: ERROR", metadata: { success: false, rewrite_count: 0, verified_count: 0, unverified_count: 0, error }, @@ -71,7 +75,12 @@ async function verifyRewrites( data: Record, ) { const hasSchema = !!(args.schema_path || (args.schema_context && Object.keys(args.schema_context).length > 0)) - const norm = (s: string) => s.replace(/\s+/g, " ").trim().toLowerCase() + // Normalize whitespace only for dedup / no-op detection — do NOT case-fold: + // lowercasing would collapse rewrites that differ only by a case-sensitive + // string literal or quoted identifier (distinct queries) into one, dropping a + // valid candidate before it's verified. Equivalence itself is checked by the + // engine, not by this string compare. + const norm = (s: string) => s.replace(/\s+/g, " ").trim() // Collect candidate rewrites (whole-query + per-suggestion), drop blanks/no-ops/dupes. const suggestions: any[] = data.suggestions ?? data.rewrites ?? [] diff --git a/packages/opencode/test/altimate/altimate-core-rewrite-verify.test.ts b/packages/opencode/test/altimate/altimate-core-rewrite-verify.test.ts index 1ad20e8d5..bb13a1f6e 100644 --- a/packages/opencode/test/altimate/altimate-core-rewrite-verify.test.ts +++ b/packages/opencode/test/altimate/altimate-core-rewrite-verify.test.ts @@ -130,6 +130,31 @@ describe("verified-optimize — gate logic (mocked engine)", () => { expect(String(r.output)).toContain("engine down") }) + test("success:false with EMPTY error -> still treated as failure (no fall-through)", async () => { + // Regression: a `{ success: false, error: "" }` payload must not fall through + // to the success path and misreport rewrites. + Dispatcher.register("altimate_core.rewrite" as any, async () => ({ success: false, error: "" })) + const r = await runTool({ sql: ORIGINAL, schema_context: SCHEMA }) + expect(r.metadata.success).toBe(false) + expect(r.metadata.verified_count).toBe(0) + expect(String(r.output)).toContain("Failed to generate rewrites") + }) + + test("dedup preserves case-sensitive literals (does NOT case-fold)", async () => { + // Two rewrites differing only by a string-literal case are DISTINCT and must + // both be verified — not collapsed into one by case-folding. + const rwA = "SELECT id FROM t WHERE name = 'A'" + const rwB = "SELECT id FROM t WHERE name = 'a'" + Dispatcher.register("altimate_core.rewrite" as any, async () => ({ + success: true, + data: { suggestions: [{ rewritten_sql: rwA }, { rewritten_sql: rwB }] }, + })) + Dispatcher.register("altimate_core.equivalence" as any, async () => ({ success: true, data: { equivalent: true } })) + const r = await runTool({ sql: "SELECT id FROM t", schema_context: SCHEMA }) + // Both candidates survive dedup and get verified (2, not 1). + expect(r.metadata.verified_count).toBe(2) + }) + test("duplicate candidates are de-duplicated", async () => { Dispatcher.register("altimate_core.rewrite" as any, async () => ({ success: true,