Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
56194d7
fix: [AI-5975] propagate actual error messages to telemetry instead o…
suryaiyer95 Mar 24, 2026
1dfb107
fix: [AI-5975] early-return "No schema provided" error for `validate`…
suryaiyer95 Mar 24, 2026
f5f5382
fix: [AI-5975] add `.filter(Boolean)` to all error extractors to prev…
suryaiyer95 Mar 24, 2026
244df89
merge: resolve conflict in `sql-analyze.ts` — take main's spread patt…
suryaiyer95 Mar 24, 2026
1e4c4e4
fix: [AI-5975] treat "not equivalent" as valid result in `altimate_co…
suryaiyer95 Mar 24, 2026
fde7db3
fix: [AI-5975] address PR review feedback
suryaiyer95 Mar 24, 2026
c14852a
fix: [AI-5975] use error-driven title in semantics, generic error lab…
suryaiyer95 Mar 24, 2026
40f8e92
fix: [AI-5975] ensure `metadata.error` is never `undefined` on succes…
suryaiyer95 Mar 24, 2026
f123470
fix: [AI-5975] dispatcher wrappers: `success` means "handler complete…
suryaiyer95 Mar 24, 2026
efef758
fix: [AI-5975] address code review findings for tool wrappers
suryaiyer95 Mar 24, 2026
03b4dd9
fix: [AI-5975] revert validate tool to use dispatcher's `result.succe…
suryaiyer95 Mar 24, 2026
f0be8f2
fix: [AI-5975] make error propagation tests self-contained with mocks
suryaiyer95 Mar 24, 2026
ddf53be
fix: [AI-5975] propagate tool error messages to telemetry across all …
suryaiyer95 Mar 24, 2026
9d08243
fix: [AI-5975] add missing `dialect` fields to test args for type-check
suryaiyer95 Mar 24, 2026
3623461
fix: [AI-5975] remove spurious `dialect` from grade test (no such param)
suryaiyer95 Mar 24, 2026
8f05cf8
Merge remote-tracking branch 'origin/main' into fix/ai-5975-tool-erro…
suryaiyer95 Mar 24, 2026
7a85ad8
fix: [AI-5975] update test expectation to match error title format af…
suryaiyer95 Mar 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions packages/opencode/src/altimate/native/altimate-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ function toData(obj: unknown): Record<string, unknown> {
return JSON.parse(JSON.stringify(obj)) as Record<string, unknown>
}

/** Wrap a handler body into the standard AltimateCoreResult envelope. */
/**
* Wrap a handler body into the standard AltimateCoreResult envelope.
*
* Contract: ok(true, data) means "the operation completed." Semantic results
* (e.g., SQL is invalid, queries are not equivalent) live in the data fields,
* NOT in the success flag. success=false only when the handler throws (fail()).
* This prevents semantic findings from being misreported as tool crashes.
*/
function ok(
success: boolean,
data: Record<string, unknown>,
Expand Down Expand Up @@ -92,7 +99,7 @@ register("altimate_core.validate", async (params) => {
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
const raw = await core.validate(params.sql, schema)
const data = toData(raw)
return ok(data.valid !== false, data)
return ok(true, data)
} catch (e) {
return fail(e)
}
Expand All @@ -104,7 +111,7 @@ register("altimate_core.lint", async (params) => {
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
const raw = core.lint(params.sql, schema)
const data = toData(raw)
return ok(data.clean !== false, data)
return ok(true, data)
} catch (e) {
return fail(e)
}
Expand All @@ -115,7 +122,7 @@ register("altimate_core.safety", async (params) => {
try {
const raw = core.scanSql(params.sql)
const data = toData(raw)
return ok(data.safe !== false, data)
return ok(true, data)
} catch (e) {
return fail(e)
}
Expand Down Expand Up @@ -147,7 +154,7 @@ register("altimate_core.transpile", async (params) => {
}
}

return ok(data.success !== false, data)
return ok(true, data)
} catch (e) {
return fail(e)
}
Expand All @@ -159,7 +166,7 @@ register("altimate_core.explain", async (params) => {
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
const raw = await core.explain(params.sql, schema)
const data = toData(raw)
return ok(data.valid !== false, data)
return ok(true, data)
} catch (e) {
return fail(e)
}
Expand Down Expand Up @@ -193,7 +200,7 @@ register("altimate_core.fix", async (params) => {
params.max_iterations ?? undefined,
)
const data = toData(raw)
return ok(data.fixed !== false, data)
return ok(true, data)
} catch (e) {
return fail(e)
}
Expand All @@ -205,7 +212,7 @@ register("altimate_core.policy", async (params) => {
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
const raw = await core.checkPolicy(params.sql, schema, params.policy_json)
const data = toData(raw)
return ok(data.allowed !== false, data)
return ok(true, data)
} catch (e) {
return fail(e)
}
Expand All @@ -217,7 +224,7 @@ register("altimate_core.semantics", async (params) => {
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
const raw = await core.checkSemantics(params.sql, schema)
const data = toData(raw)
return ok(data.valid !== false, data)
return ok(true, data)
} catch (e) {
return fail(e)
}
Expand All @@ -240,7 +247,7 @@ register("altimate_core.equivalence", async (params) => {
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
const raw = await core.checkEquivalence(params.sql1, params.sql2, schema)
const data = toData(raw)
return ok(data.equivalent !== false, data)
return ok(true, data)
} catch (e) {
return fail(e)
}
Expand All @@ -256,7 +263,7 @@ register("altimate_core.migration", async (params) => {
)
const raw = core.analyzeMigration(params.new_ddl, schema)
const data = toData(raw)
return ok(data.safe !== false, data)
return ok(true, data)
} catch (e) {
return fail(e)
}
Expand Down Expand Up @@ -291,7 +298,7 @@ register("altimate_core.correct", async (params) => {
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
const raw = await core.correct(params.sql, schema)
const data = toData(raw)
return ok(data.status !== "unfixable", data)
return ok(true, data)
} catch (e) {
return fail(e)
}
Expand Down Expand Up @@ -337,7 +344,7 @@ register("altimate_core.resolve_term", async (params) => {
const raw = core.resolveTerm(params.term, schema)
// Rust returns an array of matches — wrap for consistent object shape
const matches = Array.isArray(raw) ? JSON.parse(JSON.stringify(raw)) : []
return ok(matches.length > 0, { matches })
return ok(true, { matches })
} catch (e) {
return fail(e)
}
Expand Down Expand Up @@ -374,7 +381,7 @@ register("altimate_core.format", async (params) => {
try {
const raw = core.formatSql(params.sql, params.dialect || undefined)
const data = toData(raw)
return ok(data.success !== false, data)
return ok(true, data)
} catch (e) {
return fail(e)
}
Expand Down
10 changes: 10 additions & 0 deletions packages/opencode/src/altimate/native/sql/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,23 @@ register("sql.fix", async (params) => {
fixed_sql: f.fixed_sql ?? f.rewritten_sql,
}))

const unfixableMessages = Array.isArray(result.unfixable_errors)
? result.unfixable_errors
.map((e: any) => e.error?.message ?? e.message ?? e.reason ?? String(e))
.filter((msg: any) => typeof msg === "string" ? msg.trim().length > 0 : Boolean(msg))
: []
const unfixableError = !result.fixed && unfixableMessages.length > 0
? unfixableMessages.join("; ")
: undefined

return {
success: result.fixed ?? true,
original_sql: result.original_sql ?? params.sql,
fixed_sql: result.fixed_sql ?? params.sql,
error_message: params.error_message ?? "",
suggestions,
suggestion_count: suggestions.length,
...(unfixableError && { error: unfixableError }),
}
} catch (e) {
return {
Expand Down
2 changes: 2 additions & 0 deletions packages/opencode/src/altimate/native/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface SqlExecuteResult {
export interface SqlAnalyzeParams {
sql: string
dialect?: string
schema_path?: string
schema_context?: Record<string, any>
}

Expand Down Expand Up @@ -385,6 +386,7 @@ export interface SqlFixResult {
error_message: string
suggestions: SqlFixSuggestion[]
suggestion_count: number
error?: string
}

// --- SQL Autocomplete ---
Expand Down
9 changes: 5 additions & 4 deletions packages/opencode/src/altimate/tools/altimate-core-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Dispatcher } from "../native"

export const AltimateCoreCheckTool = Tool.define("altimate_core_check", {
description:
"Run full analysis pipeline: validate + lint + safety scan + PII check using the Rust-based altimate-core engine. Single call for comprehensive SQL analysis.",
"Run full analysis pipeline: validate + lint + safety scan + PII check. Single call for comprehensive SQL analysis. Provide schema_context or schema_path for accurate table/column resolution.",
parameters: z.object({
sql: z.string().describe("SQL query to analyze"),
schema_path: z.string().optional().describe("Path to YAML/JSON schema file"),
Expand All @@ -17,15 +17,16 @@ export const AltimateCoreCheckTool = Tool.define("altimate_core_check", {
schema_path: args.schema_path ?? "",
schema_context: args.schema_context,
})
const data = result.data as Record<string, any>
const data = (result.data ?? {}) as Record<string, any>
const error = result.error ?? data.error
return {
title: `Check: ${formatCheckTitle(data)}`,
metadata: { success: result.success },
metadata: { success: result.success, ...(error && { error }) },
output: formatCheck(data),
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return { title: "Check: ERROR", metadata: { success: false }, output: `Failed: ${msg}` }
return { title: "Check: ERROR", metadata: { success: false, error: msg }, output: `Failed: ${msg}` }
}
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Dispatcher } from "../native"

export const AltimateCoreClassifyPiiTool = Tool.define("altimate_core_classify_pii", {
description:
"Classify PII columns in a schema using the Rust-based altimate-core engine. Identifies columns likely containing personal identifiable information by name patterns and data types.",
"Classify PII columns in a schema. Identifies columns likely containing personal identifiable information by name patterns and data types. Provide schema_context or schema_path for accurate table/column resolution.",
parameters: z.object({
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"),
Expand All @@ -15,17 +15,18 @@ export const AltimateCoreClassifyPiiTool = Tool.define("altimate_core_classify_p
schema_path: args.schema_path ?? "",
schema_context: args.schema_context,
})
const data = result.data as Record<string, any>
const data = (result.data ?? {}) as Record<string, any>
const piiColumns = data.columns ?? data.findings ?? []
const findingCount = piiColumns.length
const error = result.error ?? data.error
return {
title: `PII Classification: ${findingCount} finding(s)`,
metadata: { success: result.success, finding_count: findingCount },
metadata: { success: result.success, finding_count: findingCount, ...(error && { error }) },
output: formatClassifyPii(data),
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return { title: "PII Classification: ERROR", metadata: { success: false, finding_count: 0 }, output: `Failed: ${msg}` }
return { title: "PII Classification: ERROR", metadata: { success: false, finding_count: 0, error: msg }, output: `Failed: ${msg}` }
}
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Dispatcher } from "../native"

export const AltimateCoreColumnLineageTool = Tool.define("altimate_core_column_lineage", {
description:
"Trace schema-aware column lineage using the Rust-based altimate-core engine. Maps how columns flow through a query from source tables to output. Requires altimate_core.init() with API key.",
"Trace schema-aware column lineage. Maps how columns flow through a query from source tables to output. Requires altimate_core.init() with API key. Provide schema_context or schema_path for accurate table/column resolution.",
parameters: z.object({
sql: z.string().describe("SQL query to trace lineage for"),
dialect: z.string().optional().describe("SQL dialect (e.g. snowflake, bigquery)"),
Expand All @@ -19,16 +19,17 @@ export const AltimateCoreColumnLineageTool = Tool.define("altimate_core_column_l
schema_path: args.schema_path ?? "",
schema_context: args.schema_context,
})
const data = result.data as Record<string, any>
const data = (result.data ?? {}) as Record<string, any>
const edgeCount = data.column_lineage?.length ?? 0
const error = result.error ?? data.error
return {
title: `Column Lineage: ${edgeCount} edge(s)`,
metadata: { success: result.success, edge_count: edgeCount },
metadata: { success: result.success, edge_count: edgeCount, ...(error && { error }) },
output: formatColumnLineage(data),
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return { title: "Column Lineage: ERROR", metadata: { success: false, edge_count: 0 }, output: `Failed: ${msg}` }
return { title: "Column Lineage: ERROR", metadata: { success: false, edge_count: 0, error: msg }, output: `Failed: ${msg}` }
}
},
})
Expand Down
9 changes: 5 additions & 4 deletions packages/opencode/src/altimate/tools/altimate-core-compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Dispatcher } from "../native"

export const AltimateCoreCompareTool = Tool.define("altimate_core_compare", {
description:
"Structurally compare two SQL queries using the Rust-based altimate-core engine. Identifies differences in table references, join conditions, filters, projections, and aggregations.",
"Structurally compare two SQL queries. Identifies differences in table references, join conditions, filters, projections, and aggregations.",
parameters: z.object({
left_sql: z.string().describe("First SQL query"),
right_sql: z.string().describe("Second SQL query"),
Expand All @@ -17,16 +17,17 @@ export const AltimateCoreCompareTool = Tool.define("altimate_core_compare", {
right_sql: args.right_sql,
dialect: args.dialect ?? "",
})
const data = result.data as Record<string, any>
const data = (result.data ?? {}) as Record<string, any>
const diffCount = data.differences?.length ?? 0
const error = result.error ?? data.error
return {
title: `Compare: ${diffCount === 0 ? "IDENTICAL" : `${diffCount} difference(s)`}`,
metadata: { success: result.success, difference_count: diffCount },
metadata: { success: result.success, difference_count: diffCount, ...(error && { error }) },
output: formatCompare(data),
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return { title: "Compare: ERROR", metadata: { success: false, difference_count: 0 }, output: `Failed: ${msg}` }
return { title: "Compare: ERROR", metadata: { success: false, difference_count: 0, error: msg }, output: `Failed: ${msg}` }
}
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Dispatcher } from "../native"

export const AltimateCoreCompleteTool = Tool.define("altimate_core_complete", {
description:
"Get cursor-aware SQL completion suggestions using the Rust-based altimate-core engine. Returns table names, column names, functions, and keywords relevant to the cursor position.",
"Get cursor-aware SQL completion suggestions. Returns table names, column names, functions, and keywords relevant to the cursor position. Provide schema_context or schema_path for accurate table/column resolution.",
parameters: z.object({
sql: z.string().describe("Partial SQL query"),
cursor_pos: z.number().describe("Cursor position (0-indexed character offset)"),
Expand All @@ -19,16 +19,17 @@ export const AltimateCoreCompleteTool = Tool.define("altimate_core_complete", {
schema_path: args.schema_path ?? "",
schema_context: args.schema_context,
})
const data = result.data as Record<string, any>
const data = (result.data ?? {}) as Record<string, any>
const count = data.items?.length ?? data.suggestions?.length ?? 0
const error = result.error ?? (data as any).error
return {
title: `Complete: ${count} suggestion(s)`,
metadata: { success: result.success, suggestion_count: count },
metadata: { success: result.success, suggestion_count: count, ...(error && { error }) },
output: formatComplete(data),
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return { title: "Complete: ERROR", metadata: { success: false, suggestion_count: 0 }, output: `Failed: ${msg}` }
return { title: "Complete: ERROR", metadata: { success: false, suggestion_count: 0, error: msg }, output: `Failed: ${msg}` }
}
},
})
Expand Down
21 changes: 17 additions & 4 deletions packages/opencode/src/altimate/tools/altimate-core-correct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Dispatcher } from "../native"

export const AltimateCoreCorrectTool = Tool.define("altimate_core_correct", {
description:
"Iteratively correct SQL using a propose-verify-refine loop via the Rust-based altimate-core engine. More thorough than fix — applies multiple correction rounds to produce valid SQL.",
"Iteratively correct SQL using a propose-verify-refine loop. More thorough than fix — applies multiple correction rounds to produce valid SQL. Provide schema_context or schema_path for accurate table/column resolution.",
parameters: z.object({
sql: z.string().describe("SQL query to correct"),
schema_path: z.string().optional().describe("Path to YAML/JSON schema file"),
Expand All @@ -17,19 +17,32 @@ export const AltimateCoreCorrectTool = Tool.define("altimate_core_correct", {
schema_path: args.schema_path ?? "",
schema_context: args.schema_context,
})
const data = result.data as Record<string, any>
const data = (result.data ?? {}) as Record<string, any>
const error = result.error ?? data.error ?? extractCorrectErrors(data)
return {
title: `Correct: ${data.success ? "CORRECTED" : "COULD NOT CORRECT"}`,
metadata: { success: result.success, iterations: data.iterations },
metadata: { success: result.success, iterations: data.iterations, ...(error && { error }) },
output: formatCorrect(data),
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return { title: "Correct: ERROR", metadata: { success: false, iterations: 0 }, output: `Failed: ${msg}` }
return { title: "Correct: ERROR", metadata: { success: false, iterations: 0, error: msg }, output: `Failed: ${msg}` }
}
},
})

function extractCorrectErrors(data: Record<string, any>): string | undefined {
if (data.final_validation?.errors?.length > 0) {
const msgs = data.final_validation.errors.map((e: any) => e.message ?? String(e)).filter(Boolean)
if (msgs.length > 0) return msgs.join("; ")
}
if (Array.isArray(data.errors) && data.errors.length > 0) {
const msgs = data.errors.map((e: any) => e.message ?? String(e)).filter(Boolean)
if (msgs.length > 0) return msgs.join("; ")
}
return undefined
}

function formatCorrect(data: Record<string, any>): string {
if (data.error) return `Error: ${data.error}`
const lines: string[] = []
Expand Down
Loading
Loading