Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
14 changes: 14 additions & 0 deletions packages/opencode/src/altimate/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,20 @@ export namespace Telemetry {
total_cost: number
}
// altimate_change end
// altimate_change start — pre-execution SQL validation telemetry
| {
type: "sql_pre_validation"
timestamp: number
session_id: string
/** skipped = no cache or stale, passed = valid SQL, blocked = invalid SQL caught, error = validation itself failed */
outcome: "skipped" | "passed" | "blocked" | "error"
/** why: no_cache, stale_cache, empty_cache, valid, non_structural, structural_error, validation_exception */
reason: string
schema_columns: number
duration_ms: number
error_message?: string
}
// altimate_change end

/** SHA256 hash a masked error message for anonymous grouping. */
export function hashError(maskedMessage: string): string {
Expand Down
140 changes: 140 additions & 0 deletions packages/opencode/src/altimate/tools/sql-execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { Telemetry } from "../telemetry"
// altimate_change start — progressive disclosure suggestions
import { PostConnectSuggestions } from "./post-connect-suggestions"
// altimate_change end
// altimate_change start — pre-execution SQL validation via cached schema
import { getCache } from "../native/schema/cache"
// altimate_change end

export const SqlExecuteTool = Tool.define("sql_execute", {
description: "Execute SQL against a connected data warehouse. Returns results as a formatted table.",
Expand All @@ -34,6 +37,14 @@ export const SqlExecuteTool = Tool.define("sql_execute", {
}
// altimate_change end

// altimate_change start — shadow-mode pre-execution SQL validation
// Runs validation against cached schema and emits sql_pre_validation telemetry,
// but does NOT block execution. Used to measure catch rate before deciding
// whether to enable blocking in a future release. Fire-and-forget so it
// doesn't add latency to the sql_execute hot path.
preValidateSql(args.query, args.warehouse).catch(() => {})
// altimate_change end

try {
const result = await Dispatcher.call("sql.execute", {
sql: args.query,
Expand Down Expand Up @@ -91,6 +102,135 @@ export const SqlExecuteTool = Tool.define("sql_execute", {
},
})

// altimate_change start — pre-execution SQL validation via cached schema
const CACHE_TTL_MS = 24 * 60 * 60 * 1000 // 24 hours

interface PreValidationResult {
blocked: boolean
error?: string
}

async function preValidateSql(sql: string, warehouse?: string): Promise<PreValidationResult> {
const startTime = Date.now()
try {
const cache = await getCache()
const status = cache.cacheStatus()

// Find the target warehouse in cache
const warehouseName = warehouse || status.warehouses[0]?.name
if (!warehouseName) {
trackPreValidation("skipped", "no_cache", 0, Date.now() - startTime)
return { blocked: false }
}

const warehouseStatus = status.warehouses.find((w) => w.name === warehouseName)
if (!warehouseStatus?.last_indexed) {
trackPreValidation("skipped", "no_cache", 0, Date.now() - startTime)
return { blocked: false }
}

// Check cache freshness
const cacheAge = Date.now() - new Date(warehouseStatus.last_indexed).getTime()
if (cacheAge > CACHE_TTL_MS) {
trackPreValidation("skipped", "stale_cache", 0, Date.now() - startTime)
return { blocked: false }
}

// Build schema context from cached columns
const columns = cache.listColumns(warehouseName, 10_000)
if (columns.length === 0) {
trackPreValidation("skipped", "empty_cache", 0, Date.now() - startTime)
return { blocked: false }
}

const schemaContext: Record<string, any> = {}
for (const col of columns) {
const tableName = col.schema_name ? `${col.schema_name}.${col.table}` : col.table
if (!schemaContext[tableName]) {
schemaContext[tableName] = []
}
schemaContext[tableName].push({
name: col.name,
type: col.data_type || "VARCHAR",
nullable: col.nullable,
})
}

// Validate SQL against cached schema
const validationResult = await Dispatcher.call("altimate_core.validate", {
sql,
schema_path: "",
schema_context: schemaContext,
})

const data = (validationResult.data ?? {}) as Record<string, any>
const errors = Array.isArray(data.errors) ? data.errors : []
const isValid = data.valid !== false && errors.length === 0

if (isValid) {
trackPreValidation("passed", "valid", columns.length, Date.now() - startTime)
return { blocked: false }
}

// Only block on high-confidence structural errors
const structuralErrors = errors.filter((e: any) => {
const msg = (e.message ?? "").toLowerCase()
return msg.includes("column") || msg.includes("table") || msg.includes("not found") || msg.includes("does not exist")
})

if (structuralErrors.length === 0) {
// Non-structural errors (ambiguous cases) — let them through
trackPreValidation("passed", "non_structural", columns.length, Date.now() - startTime)
return { blocked: false }
}

// Build helpful error with available columns
const errorMsgs = structuralErrors.map((e: any) => e.message).join("\n")
const referencedTables = Object.keys(schemaContext).slice(0, 10)
const availableColumns = referencedTables
.map((t) => `${t}: ${schemaContext[t].map((c: any) => c.name).join(", ")}`)
.join("\n")

const errorOutput = [
`Pre-execution validation failed (validated against cached schema):`,
``,
errorMsgs,
``,
`Available tables and columns:`,
availableColumns,
``,
`Fix the query and retry. If the schema cache is outdated, run schema_index to refresh it.`,
].join("\n")

trackPreValidation("blocked", "structural_error", columns.length, Date.now() - startTime, errorMsgs)
return { blocked: true, error: errorOutput }
} catch {
// Validation failure should never block execution
trackPreValidation("error", "validation_exception", 0, Date.now() - startTime)
return { blocked: false }
}
}

function trackPreValidation(
outcome: "skipped" | "passed" | "blocked" | "error",
reason: string,
schema_columns: number,
duration_ms: number,
error_message?: string,
) {
Telemetry.track({
type: "sql_pre_validation",
timestamp: Date.now(),
session_id: Telemetry.getContext().sessionId,
outcome,
reason,
schema_columns,
duration_ms,
...(error_message && { error_message: error_message.slice(0, 500) }),
})
}
// altimate_change end

function formatResult(result: SqlExecuteResult): string {
if (result.row_count === 0) return "(0 rows)"

Expand Down
Loading