-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathaltimate-core-check.ts
More file actions
110 lines (102 loc) · 3.81 KB
/
altimate-core-check.ts
File metadata and controls
110 lines (102 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import z from "zod"
import { Tool } from "../../tool/tool"
import { Dispatcher } from "../native"
import type { Telemetry } from "../telemetry"
export const AltimateCoreCheckTool = Tool.define("altimate_core_check", {
description:
"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"),
schema_context: z.record(z.string(), z.any()).optional().describe("Inline schema definition"),
}),
async execute(args, ctx) {
const hasSchema = !!(args.schema_path || (args.schema_context && Object.keys(args.schema_context).length > 0))
try {
const result = await Dispatcher.call("altimate_core.check", {
sql: args.sql,
schema_path: args.schema_path ?? "",
schema_context: args.schema_context,
})
const data = (result.data ?? {}) as Record<string, any>
const error = result.error ?? data.error
// altimate_change start — sql quality findings for telemetry
const findings: Telemetry.Finding[] = []
for (const err of data.validation?.errors ?? []) {
findings.push({ category: "validation_error" })
}
for (const f of data.lint?.findings ?? []) {
findings.push({ category: f.rule ?? "lint" })
}
for (const t of data.safety?.threats ?? []) {
findings.push({ category: t.type ?? "safety_threat" })
}
for (const p of data.pii?.findings ?? []) {
findings.push({ category: "pii_detected" })
}
// altimate_change end
return {
title: `Check: ${formatCheckTitle(data)}`,
metadata: {
success: result.success,
has_schema: hasSchema,
...(error && { error }),
...(findings.length > 0 && { findings }),
},
output: formatCheck(data),
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return {
title: "Check: ERROR",
metadata: { success: false, has_schema: hasSchema, error: msg },
output: `Failed: ${msg}`,
}
}
},
})
export function formatCheckTitle(data: Record<string, any>): string {
const parts: string[] = []
if (!data.validation?.valid) parts.push("validation errors")
if (!data.lint?.clean) parts.push(`${data.lint?.findings?.length ?? 0} lint findings`)
if (!data.safety?.safe) parts.push("safety threats")
if (data.pii?.findings?.length) parts.push("PII detected")
return parts.length ? parts.join(", ") : "PASS"
}
export function formatCheck(data: Record<string, any>): string {
const lines: string[] = []
lines.push("=== Validation ===")
if (data.validation?.valid) {
lines.push("Valid SQL.")
} else {
const validationMessages = (data.validation?.errors ?? [])
.map((e: any) => (typeof e === "string" ? e : e?.message))
.filter(Boolean)
lines.push(`Invalid: ${validationMessages.join("; ") || "unknown"}`)
}
lines.push("\n=== Lint ===")
if (data.lint?.clean) {
lines.push("No lint findings.")
} else {
for (const f of data.lint?.findings ?? []) {
lines.push(` [${f.severity}] ${f.rule}: ${f.message}`)
}
}
lines.push("\n=== Safety ===")
if (data.safety?.safe) {
lines.push("Safe — no threats.")
} else {
for (const t of data.safety?.threats ?? []) {
lines.push(` [${t.severity}] ${t.type}: ${t.description}`)
}
}
lines.push("\n=== PII ===")
if (!data.pii?.findings?.length) {
lines.push("No PII detected.")
} else {
for (const p of data.pii?.findings ?? []) {
lines.push(` ${p.column}: ${p.category} (${p.confidence} confidence)`)
}
}
return lines.join("\n")
}