11import z from "zod"
22import { Tool } from "../../tool/tool"
33import { Dispatcher } from "../native"
4+ import type { Telemetry } from "../telemetry"
45
56export const AltimateCoreValidateTool = Tool . define ( "altimate_core_validate" , {
67 description :
@@ -11,25 +12,48 @@ export const AltimateCoreValidateTool = Tool.define("altimate_core_validate", {
1112 schema_context : z . record ( z . string ( ) , z . any ( ) ) . optional ( ) . describe ( "Inline schema definition" ) ,
1213 } ) ,
1314 async execute ( args , ctx ) {
15+ const hasSchema = ! ! ( args . schema_path || ( args . schema_context && Object . keys ( args . schema_context ) . length > 0 ) )
1416 try {
1517 const result = await Dispatcher . call ( "altimate_core.validate" , {
1618 sql : args . sql ,
1719 schema_path : args . schema_path ?? "" ,
1820 schema_context : args . schema_context ,
1921 } )
2022 const data = result . data as Record < string , any >
23+ // altimate_change start — sql quality findings for telemetry
24+ const findings : Telemetry . Finding [ ] = ( data . errors ?? [ ] ) . map ( ( err : any ) => ( {
25+ category : classifyValidationError ( err . message ?? "" ) ,
26+ severity : "error" ,
27+ } ) )
28+ // altimate_change end
2129 return {
2230 title : `Validate: ${ data . valid ? "VALID" : "INVALID" } ` ,
23- metadata : { success : result . success , valid : data . valid } ,
31+ metadata : {
32+ success : result . success ,
33+ valid : data . valid ,
34+ has_schema : hasSchema ,
35+ dialect : "snowflake" ,
36+ ...( findings . length > 0 && { findings } ) ,
37+ } ,
2438 output : formatValidate ( data ) ,
2539 }
2640 } catch ( e ) {
2741 const msg = e instanceof Error ? e . message : String ( e )
28- return { title : "Validate: ERROR" , metadata : { success : false , valid : false } , output : `Failed: ${ msg } ` }
42+ return { title : "Validate: ERROR" , metadata : { success : false , valid : false , has_schema : hasSchema , dialect : "snowflake" } , output : `Failed: ${ msg } ` }
2943 }
3044 } ,
3145} )
3246
47+ function classifyValidationError ( message : string ) : string {
48+ const lower = message . toLowerCase ( )
49+ // Column check before table — "column not found in table" would match both
50+ if ( lower . includes ( "column" ) && lower . includes ( "not found" ) ) return "missing_column"
51+ if ( lower . includes ( "table" ) && lower . includes ( "not found" ) ) return "missing_table"
52+ if ( lower . includes ( "syntax" ) ) return "syntax_error"
53+ if ( lower . includes ( "type" ) ) return "type_mismatch"
54+ return "validation_error"
55+ }
56+
3357function formatValidate ( data : Record < string , any > ) : string {
3458 if ( data . error ) return `Error: ${ data . error } `
3559 if ( data . valid ) return "SQL is valid."
0 commit comments