diff --git a/.opencode/skills/query-optimize/SKILL.md b/.opencode/skills/query-optimize/SKILL.md index df3ebdac3c..6d1dd6d6d2 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 44f657bfc2..307df090e5 100644 --- a/packages/opencode/src/altimate/tools/altimate-core-rewrite.ts +++ b/packages/opencode/src/altimate/tools/altimate-core-rewrite.ts @@ -4,26 +4,51 @@ 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 + // 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 }, + 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 +62,142 @@ 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)) + // 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 ?? [] + 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/test/altimate/altimate-core-rewrite-verify.test.ts b/packages/opencode/test/altimate/altimate-core-rewrite-verify.test.ts new file mode 100644 index 0000000000..bb13a1f6ee --- /dev/null +++ b/packages/opencode/test/altimate/altimate-core-rewrite-verify.test.ts @@ -0,0 +1,288 @@ +/** + * 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" + +// 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 { 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 +// 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("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, + 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) + }) + + 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", () => { + 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") + }) + + 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) ---- +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("Rewrite") // "Rewrite (verified): …" or "Rewrite: no rewrites" + }) +})