Skip to content

Commit da952c1

Browse files
anandgupta42claude
andauthored
chore: upgrade @altimateai/altimate-core to 0.7.0 and sync consumer contracts (#1090)
* chore: upgrade `@altimateai/altimate-core` to 0.7.0 and sync consumer contracts Engine upgrade 0.5.1 → 0.7.0 (through 0.6.0). Both releases are correctness releases whose output differs where the old output was wrong: more lineage edges (derived tables, CTE chains, CTAS/INSERT…SELECT), more PII exposures, stricter migration verdicts, a new `unbalanced_quote` safety rule, and validate/transpile/equivalence fixes. One type-surface addition: `PiiColumnAccess.query_targets`. Contract sync (consumers still read legacy pre-native-core shapes): - `altimate-core-migration` tool: read `findings`/`safe`/`overall_risk` (engine `MigrationResult`) instead of nonexistent `risks` — previously ANY migration, including `DROP COLUMN`, rendered "Migration: SAFE"; never render SAFE when the engine call errored; count only non-"safe" findings as risks. - `altimate_core.migration` handler: coerce empty dialect `"" → undefined` (`|| undefined`, same as the equivalence handler) so `Schema.fromDdl` does not throw on the default empty-string dialect. - `check --checks safety`: read engine `threats[]` (`rule`/`message`/`detail`) so real threats (e.g. `unbalanced_quote`) render with rule and message instead of a generic warning. - `normalizeSeverity`: map engine severities `high → error`, `medium → warning` — previously both degraded to `info`, so `--fail-on`/`--severity` silently passed high-risk injections. - `check --checks pii`: map engine `PiiColumnAccess` (`table`/`column`/ `classification`/`query_targets`/`suggested_masking`); stop assigning the column NAME to the numeric column-position field; report the exposing alias; stringify `{ Custom: string }` classifications (also in the query-pii tool renderer, which printed `[object Object]`). - `altimate-core-track-lineage` tool: collect edges from `queries[].edges` (engine `LineageResult`) instead of flat `edges` — previously always "0 edges"; render `impact_map`; format `{table, column}` refs; render ERROR instead of "0 edges" when the engine call fails. - `altimate-core-query-pii` tool: surface new 0.7.0 `query_targets` field as "Exposed via: …". - `altimate-core-check` tool: safety renderer and telemetry read `rule`/`message` with legacy fallback. Test updates for 0.7.0 behavior: - Dialect-forwarding fixture: `payload:f` now parses in the default dialect, so it no longer discriminates; switched to Snowflake time-travel `AT(OFFSET => -60)` which still does. - Grade comparison made apples-to-apples (same table, projection-only diff). - New real-engine tests: `query_targets` contract + rendering, migration destructive/safe/empty-dialect/error + tool-title regression, track-lineage edge surfacing + error rendering, check CLI safety `ThreatFinding` (incl. `high → error` normalization) and PII `PiiColumnAccess` shapes (incl. `{ Custom }` classification). - Strengthened previously vacuous migration e2e assertions. Verification: typecheck clean; test/altimate + test/cli fully green (4750 pass / 0 fail); full-suite failures (MCP/TUI-sound/subprocess) reproduced identically on 0.5.1 or shown run-to-run flaky with no engine coupling — pre-existing. Marker check: no upstream-shared files modified. Codex reviewed twice (found the legacy-shape consumers; verified all fixes non-tautological). Closes #1089 Co-Authored-By: Claude Fable 5 <[email protected]> * fix: never emit `null` suggestion from `suggested_masking` in PII check `f.suggestion ?? f.suggested_masking` leaked `null` into the `suggestion` field when the engine emits `suggested_masking: null`. Coerce to `undefined` and lock with a test. (Kilo review follow-up on #1090.) Co-Authored-By: Claude Fable 5 <[email protected]> * fix: sync remaining engine-output consumers flagged by consensus review Round-2 contract sync for PR #1090, fixing every Critical/Major finding from the 4-model consensus review (all verified against the live 0.7.0 binary; all latent since the Python-engine elimination, not 0.7.0 breakage): Dead gates (silent false-passes): - `check --checks validate`: gated on envelope `success`, which the handler sets even for invalid SQL — every file passed. Now gates on `data.valid`, maps `ValidationError` (`location.line/column`, `suggestions`) and fails closed on engine failure. - `check --checks semantic` + `altimate_core_semantics` tool: engine returns `valid:true` WITH `findings` (cartesian product) — `valid` means "plannable", not "clean". Both consumers now read `findings` and never gate on `valid`; tool formatter/title/telemetry follow the findings list. - `check --checks grade`: read `grade`/`score`/`recommendations`, none of which `evaluate()` returns — no grade or finding ever surfaced. Now reads `overall_grade`/`scores.overall`/`lint.findings` and fails closed on the failure envelope. The check-e2e mock enshrined the fictional shape — fixed. - `schema.detect_pii` (pii-detector): read `piiData.findings`; engine `PiiReport` is `{ columns, pii_count, … }` — schema PII scanning returned zero findings for every scan. Shared `piiColumnsFromReport` now filters `classification !== "None"` on both the cache and live paths. Wrong shapes / crashes: - `altimate-core-compare` tool: read `differences` (engine: `identical`/ `diff_count`/`diffs`, `DiffEntry.change_type`) — different queries rendered "Compare: IDENTICAL". Error-gated title added. - `altimate-core-policy` tool: titled on `pass` (engine: `allowed`) — clean SQL always rendered "VIOLATIONS FOUND"; `metadata.success` now reflects the envelope and error output no longer contradicts the ERROR title. - Empty-dialect coercion centralized: new `dialectHint()` in `native/engine-coerce.ts` applied to all 7 dialect-forwarding handlers (columnLineage, formatSql, extractMetadata, compareQueries, importDdl + the two already fixed) — the compare/column-lineage/extract-metadata/ import-ddl tools crashed with `unknown dialect ''` whenever `dialect` was omitted (the common invocation path). - `{ Custom: string }` PII classifications rendered `[object Object]` in classify-pii, the composite check renderer, and the review runner's signed verdict — shared `classificationToString()` used everywhere; classify-pii also no longer counts `classification: "None"` rows and error-gates its title/output; query-pii treats engine `parse_error` as an abstention (previously rendered CLEAN for unparseable SQL). - Composite `altimate_core.check` now computes query PII (fail-safe) — the tool's "=== PII ===" section previously always printed "No PII detected" because the handler never populated it. - `ThreatFinding.location` is `[byteOffset, byteLength]` — surfaced as a `(chars a-b)` range in `check --checks safety` (was dropped entirely). Tests: real-engine "consumer contract sync (round 2)" block (semantics valid-gate, compare, policy pass/violation, classify-pii None-filter, no-dialect tool invocations, query-pii/classify-pii/policy error gating, composite-check PII, grade contract); CLI-shape tests for validate/semantic/ grade incl. fail-closed cases; pii-detector unit tests against the live engine + a DuckDB-gated e2e; legacy formatter tests updated to real shapes. Also filed upstream: stale `SafetyRule` union in the engine's index.d.ts (altimate-core-internal#764). Codex-verified twice: all shape corrections match the installed 0.7.0 engine; its five review findings (parse_error abstention, grade fail-closed, byte-length location, contradictory failure outputs, untracked files) are addressed in this commit. Co-Authored-By: Claude Fable 5 <[email protected]> * fix: address cubic round-2 review — runner PII extraction + consumer polish - `review/runner.ts`: the composite check's `data.pii` is now the engine `PiiQueryResult` object — extract columns from `pii_columns` (legacy array shape kept as fallback) so check-derived PII columns reach the signed review verdict. - Composite check renderer: surface `query_targets` ("exposed via: …") and honor the engine's `parse_error` abstention ("PII check skipped: …") instead of rendering "No PII detected" for unparseable SQL. - `pii-detector`: band numeric engine confidence (0..1) to `high`/`medium`/`low` via shared `bandConfidence` — `PiiFinding.confidence` is a string field. - `check --checks validate`: normalize string-shaped `suggestions` entries as well as `Suggestion` objects. - Safety location label: `bytes a-b` (engine offsets are byte-based and diverge from char indexes on multibyte SQL). - Semantics tool metadata reports `result.success` instead of hardcoding true (consistent with the policy/compare tools). - `classify-pii` tool reuses `piiColumnsFromReport` instead of a second None-filter implementation. - Tests: classify-pii error case made deterministic (malformed schema file — previously guarded by an `if`, so it could pass vacuously); composite-check alias + parse-error abstention assertions added; bytes label updated. Co-Authored-By: Claude Fable 5 <[email protected]> * fix: cubic round-3 polish — no eager NAPI load from tool imports - Move `piiColumnsFromReport` into `native/engine-coerce.ts` (re-exported from `pii-detector` for compatibility) so `altimate-core-classify-pii` no longer pulls the native NAPI binding eagerly at tool-registry load time. - `bandConfidence`: missing confidence maps to "medium" (unknown), restoring the previous default instead of degrading to "low". - classify-pii error test: temp dir created outside `try` but cleaned in `finally` regardless of setup failure. Co-Authored-By: Claude Fable 5 <[email protected]> * fix: address round-4 review comments (Codex connector, CodeRabbit, Kilo) Abstention and fail-closed hardening: - `check --checks pii`: engine `parse_error` abstentions now produce an error finding ("PII analysis skipped: …") instead of an empty pass — `--fail-on` no longer PASSes files whose PII analysis never ran. - Composite `altimate_core.check`: a thrown `checkQueryPii` now marks the PII section as an abstention (`parse_error`) instead of leaving `{}`, which rendered a false-clean "No PII detected"; `formatCheckTitle` reports "PII check skipped" instead of PASS for abstained-but-clean runs. - `schema.detect_pii`: per-column classify failures are counted and flip `success` to false (fail closed) instead of being silently swallowed; `piiColumnsFromReport` now throws on malformed reports (missing/non-array `columns`) rather than yielding zero findings. - `altimate_core_query_pii` tool: abstentions set `metadata.success: false` so telemetry's soft-failure classification records them. Correctness: - `check --checks grade`: per-file grades (`results.grade.grades`) — the shared `gradeValue`/`gradeScore` raced across concurrent batch promises, keeping whichever file finished last; flat `grade`/`score` retained for single-file runs only. Module shape / dedup: - `engine-coerce.ts` gets the AGENTS.md-prescribed `EngineCoerce` self-reexport; all consumers import the namespace projection. - `review/runner.ts` drops its local `bandConfidence` for the shared one; shared version now bands missing/non-numeric confidence as "medium" (matching the runner's previous behavior). Tests: CLI PII-abstention and multi-file grade regression tests; "PII check skipped" title test; strict malformed-PiiReport assertions; classify-pii e2e asserts a positive `pii_count` lower bound; DuckDB e2e closes its connector and restores the telemetry env var on teardown. Co-Authored-By: Claude Fable 5 <[email protected]> * fix: round-5 review follow-through — failed scans and warnings must surface - `schema_detect_pii` tool: honor `success: false` from `detectPii` — a scan that failed for any column now renders "PII Scan: ERROR" (with any partial findings attached) instead of a clean "no findings" verdict. The previous round made the detector fail closed but left its only user-facing consumer branching on `finding_count` alone. (cursor High / cubic P1 / Codex P2) - `check --checks grade`: flat `grade`/`score` gated on the invocation file count, not surviving-grade count — with several files where all but one grade call failed, the flat fields would have misattributed the survivor. (CodeRabbit Major) - `altimate_core_policy` tool: `allowed: true` with non-empty `warnings` now renders the warnings with the pass instead of hiding them behind the early "passes all policy checks" return. (Codex P2) - `review/runner.ts`: PII column extraction includes `query_targets` output aliases (e.g. `SELECT email AS contact` exposes `contact`), not just source columns. (Codex P2) - Tests: fail-closed detect-pii tool cases (zero and partial findings), policy warnings-on-pass case. Co-Authored-By: Claude Fable 5 <[email protected]> * fix: round-6 review polish — alias preference + grade nested findings - `review/runner.ts`: when a PII column has `query_targets`, report ONLY the output aliases — the source column name is not present in the model output, so including it flagged a column that isn't there (`SELECT email AS contact` now reports `contact`, not `email` + `contact`). - `check --checks grade`: findings now come from all nested EvalResult sections (lint.findings + validation.errors + safety.threats) — a failing grade with clean lint no longer yields an empty, passing finding list. - Regression test for the nested-findings case. Co-Authored-By: Claude Fable 5 <[email protected]> * fix: preserve locations/suggestions in nested grade findings `runGrade`'s nested `validation.errors`/`safety.threats` went through the shared flat mapper, dropping the line numbers and fixes that `runValidate`/ `runSafety` already surface. Normalize each shape first: `ValidationError` hoists `location.{line,column}` and the first `suggestions[]` entry; `ThreatFinding` renders its `[byteOffset, byteLength]` range into the message and maps `detail` to the suggestion. Regression assertions added. Co-Authored-By: Claude Fable 5 <[email protected]> * fix: address post-approval review follow-ups All items from the approving human review, except where noted in the PR reply: Introduced by this PR: - `classificationToString` default fallback unified to "UNKNOWN" across all call sites (was "PII"/"UNKNOWN"/"" depending on caller — an unrecognized classification no longer renders as a positive PII assertion). - `pii-detector-e2e.test.ts` gets the same NAPI-availability guard as `altimate-core-e2e.test.ts` (skip, not crash, when the binary is absent). - The `unbalanced_quote` check-e2e fixture SQL is now engine-faithful (a dangling quote, which the live 0.7.0 runtime genuinely flags with that rule — only the stale `SafetyRule` union omits it; upstream issue #764). Adjacent pre-existing: - `check --checks policy`: violations map `remediation` into the suggestion. - `sql.format` / `sql.column_lineage` handlers: last two raw dialect forwards coerced via `EngineCoerce.dialectHint`. - Review runner: composite `validation.errors` and `safety.threats` now flow into review issues alongside lint findings. - `runSafety` dispatcher-failure fallback fails closed (error severity via `dispatcherErrorFinding`, matching every other check). - Runner grade reads `overall_grade` before the legacy `grade`. Nits: - `runSemantic`'s valid:false fallback surfaces `validation_errors` detail. - Dead `line`/`column`/`code` reads dropped from the threats mapper. - Byte ranges render the INCLUSIVE end (`[37,7]` → `bytes 37-43`) in both places; tests updated. - `normalizeSeverity` documents the intentional low→info mapping. - PII findings carry a machine-readable `columnName` field. Co-Authored-By: Claude Fable 5 <[email protected]> * fix: round-7 review polish — threat normalization in review lane + fail-closed partials - Review runner: composite `safety.threats` are normalized before joining review issues — engine severities (`critical`/`high` → error, `medium` → warning, `low` → info) and the byte-tuple `location` dropped, so the quality lane classifies threats as errors instead of suggestions. - `check --checks safety`: a `success:false` envelope now appends the fail-closed error finding even when partial sub-error threats were returned — `--fail-on error` can no longer pass a crashed scanner. - Semantics tool: abstentions (`validation_errors`) set `metadata.success: false` so telemetry records the soft failure (consistent with query-pii); contract test updated accordingly. - Shared `byteRange()` helper replaces the duplicated inclusive-range formula in `runSafety`/`runGrade`. - `pii-detector-e2e.test.ts` defers the pii-detector import until after the NAPI-availability guard (static import defeated the guard). - Fixture cleanup: stale `matched_pattern`/comment from the old OR 1=1 payload removed; new fail-closed partial-threats test. Co-Authored-By: Claude Fable 5 <[email protected]> * fix: round-8 review polish — lint-only validation gating, diff-scoped safety, CLI policy warnings - Review runner: validation errors from the composite check are surfaced ONLY when a real schema exists — in lint-only mode the throwaway `_altimate_lint_` schema marks every real table unknown and would flood the review (restores the documented intent of that mode). - Composite `altimate_core.check`: safety threats are diff-scoped like lint when `base_sql` is supplied — threats already present in the base (matched by rule + matched_pattern) are pre-existing, not introduced by the change. Real-engine regression test added. - `check --checks policy`: advisory `warnings[]` on an allowed result now surface as info findings (they do not fail the check). Test added. Co-Authored-By: Claude Fable 5 <[email protected]> * fix: round-9 review polish — sound diff-scoping + composite lintDiff schema bug - Safety diff-scoping is now a MULTISET subtraction (one base occurrence consumes one head occurrence of the same rule + matched_pattern) — a PR that adds a second identical injection still reports it; `safe` and `risk_score` are recomputed when every threat is pre-existing so the verdict doesn't stay stale-unsafe. - PII exposures are diff-scoped the same way: (table, column) pairs already exposed by the base are not reported as introduced. - Validation is deliberately NOT diff-scoped: the engine validates fail-fast (only the first error is reported), so base subtraction can hide genuinely new breakage behind a pre-existing error — proven by test; re-reporting a pre-existing error is the safe mode, documented inline. - Pre-existing bug found while testing: the composite check with `base_sql` + a flat agent schema_context crashed with "missing field tables" — lintDiff takes SchemaDefinition JSON; now normalized via `normalizeSchemaContext` (exported from schema-resolver). This made every diff-scoped composite call with a flat schema fail closed since the lintDiff wiring landed. - Semantic CLI abstention severity: error when a schema was provided (the analysis should have run), warning schema-less (abstains routinely). - Policy tool telemetry includes advisory warnings (`policy_warning`). - `pii-detector` gets the AGENTS.md namespace self-reexport. Co-Authored-By: Claude Fable 5 <[email protected]> * fix: round-10 review polish — alias-aware PII scoping + scoped scores - PII diff-scoping identity now includes the sorted `query_targets` — adding or renaming a SELECT-list alias for an already-exposed column is a NEW output exposure and surfaces; identical exposures stay filtered. Regression test covers rename vs identical. - `risk_score` is recomputed (documented severity-based approximation) when only SOME threats are pre-existing, instead of keeping the full head scan's score; `risk_level` resets to "None" when every PII exposure was pre-existing. - `schema-resolver` gets the AGENTS.md `SchemaResolver` self-reexport; the new `normalizeSchemaContext` consumer uses the namespace projection. Co-Authored-By: Claude Fable 5 <[email protected]> --------- Co-authored-by: Claude Fable 5 <[email protected]>
1 parent e0a1232 commit da952c1

26 files changed

Lines changed: 1641 additions & 210 deletions

‎bun.lock‎

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/opencode/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"@ai-sdk/togetherai": "2.0.41",
7979
"@ai-sdk/vercel": "2.0.39",
8080
"@ai-sdk/xai": "3.0.82",
81-
"@altimateai/altimate-core": "0.5.1",
81+
"@altimateai/altimate-core": "0.7.0",
8282
"@altimateai/drivers": "workspace:*",
8383
"@aws-sdk/credential-providers": "3.1057.0",
8484
"@clack/prompts": "1.0.0-alpha.1",

‎packages/opencode/src/altimate/native/altimate-core.ts‎

Lines changed: 99 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
*/
1010

1111
import * as core from "@altimateai/altimate-core"
12+
import { EngineCoerce } from "./engine-coerce"
1213
import { register } from "./dispatcher"
13-
import { schemaOrEmpty, resolveSchema } from "./schema-resolver"
14+
import { schemaOrEmpty, resolveSchema, SchemaResolver } from "./schema-resolver"
1415
import type { AltimateCoreResult } from "./types"
1516

1617
// ---------------------------------------------------------------------------
@@ -168,7 +169,11 @@ export function registerAll(): void {
168169
register("altimate_core.check", async (params) => {
169170
try {
170171
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
171-
const validation = await core.validate(params.sql, schema)
172+
// NOTE: validation is deliberately NOT diff-scoped against base_sql.
173+
// The engine validates fail-fast (only the FIRST error is reported), so
174+
// subtracting base errors can hide genuinely new breakage behind a
175+
// pre-existing one. Re-reporting a pre-existing error is the safe mode.
176+
const validation: Record<string, unknown> = toData(await core.validate(params.sql, schema))
172177
// Diff-scoped lint: when a base SQL is supplied, core returns only the
173178
// findings the change INTRODUCED (pre-existing issues in the file are
174179
// dropped) — the structural comparison stays in the AST engine.
@@ -177,14 +182,91 @@ export function registerAll(): void {
177182
? core.lintDiff(
178183
params.sql,
179184
params.base_sql,
180-
params.schema_context ? JSON.stringify(params.schema_context) : undefined,
185+
// lintDiff takes SchemaDefinition JSON — normalize flat agent
186+
// schemas too, or the whole composite throws "missing field tables".
187+
params.schema_context ? SchemaResolver.normalizeSchemaContext(params.schema_context) : undefined,
181188
)
182189
: core.lint(params.sql, schema)
183-
const safety = core.scanSql(params.sql)
190+
// Diff-scope safety like lint: threats present in the base SQL are
191+
// pre-existing, not introduced by this change. Subtract as a MULTISET on
192+
// (rule, matched_pattern) — one base occurrence consumes one head
193+
// occurrence, so a PR that ADDS a second identical injection still
194+
// reports it. Recompute safe/risk_score from the surviving threats so a
195+
// fully pre-existing threat set doesn't leave a stale unsafe verdict.
196+
let safety: Record<string, unknown> = toData(core.scanSql(params.sql))
197+
if (params.base_sql) {
198+
try {
199+
const baseCounts = new Map<string, number>()
200+
for (const t of core.scanSql(params.base_sql).threats) {
201+
const k = `${t.rule}|${t.matched_pattern}`
202+
baseCounts.set(k, (baseCounts.get(k) ?? 0) + 1)
203+
}
204+
const remaining = (safety.threats as any[]).filter((t: any) => {
205+
const k = `${t.rule}|${t.matched_pattern}`
206+
const left = baseCounts.get(k) ?? 0
207+
if (left > 0) {
208+
baseCounts.set(k, left - 1)
209+
return false
210+
}
211+
return true
212+
})
213+
// The engine's risk_score covers the FULL head scan — once threats
214+
// are filtered it no longer matches. Recompute a documented
215+
// approximation from the surviving severities (matches the engine's
216+
// observed single-threat scores closely enough for gating).
217+
const severityScore: Record<string, number> = { critical: 0.98, high: 0.95, medium: 0.6, low: 0.3 }
218+
const rescored =
219+
remaining.length === (safety.threats as any[]).length
220+
? (safety.risk_score as number)
221+
: remaining.reduce((m: number, t: any) => Math.max(m, severityScore[t.severity] ?? 0.5), 0)
222+
safety = {
223+
...safety,
224+
threats: remaining,
225+
safe: remaining.length === 0 ? true : safety.safe,
226+
risk_score: rescored,
227+
}
228+
} catch {
229+
// Unscannable base — keep the full head scan (fail open to MORE findings).
230+
}
231+
}
232+
// PII exposure for the composite check — the tool has always rendered a
233+
// PII section; previously nothing populated it. Additive: a PII failure
234+
// must not fail the whole composite.
235+
let pii: Record<string, unknown>
236+
try {
237+
pii = toData(core.checkQueryPii(params.sql, schema))
238+
if (params.base_sql && Array.isArray(pii.pii_columns) && (pii.pii_columns as any[]).length) {
239+
try {
240+
// Pre-existing exposures are not introduced by this change. The
241+
// identity INCLUDES the sorted output aliases — adding or renaming
242+
// a SELECT-list alias for an already-exposed column is a NEW
243+
// output exposure and must still surface.
244+
const exposureKey = (c: any) =>
245+
`${c.table}|${c.column}|${[...(c.query_targets ?? [])].sort().join(",")}`
246+
const baseExposed = new Set(core.checkQueryPii(params.base_sql, schema).pii_columns.map(exposureKey))
247+
const remaining = (pii.pii_columns as any[]).filter((c: any) => !baseExposed.has(exposureKey(c)))
248+
pii = {
249+
...pii,
250+
pii_columns: remaining,
251+
accesses_pii: remaining.length > 0 ? pii.accesses_pii : false,
252+
// risk_level covered the full head report; with no surviving
253+
// exposures it must not keep claiming risk.
254+
risk_level: remaining.length === 0 ? "None" : pii.risk_level,
255+
}
256+
} catch {
257+
// Unscannable base — keep the full head exposure list.
258+
}
259+
}
260+
} catch (e) {
261+
// Mark as an abstention — an empty object would render "No PII
262+
// detected", a false-clean verdict.
263+
pii = { parse_error: String(e) }
264+
}
184265
const data: Record<string, unknown> = {
185-
validation: toData(validation),
266+
validation,
186267
lint: toData(lintResult),
187268
safety: toData(safety),
269+
pii,
188270
}
189271
return ok(true, data)
190272
} catch (e) {
@@ -323,10 +405,10 @@ export function registerAll(): void {
323405
// Pass the optional dialect hint so dialect-specific compiled warehouse SQL
324406
// (e.g. Snowflake semi-structured `col:field`) parses and the pair is
325407
// decidable instead of abstaining on a syntax error. Supported since
326-
// [email protected]. Use `|| undefined` (not `??`) so the default empty
327-
// string from ReviewConfig.dialect coerces to "no hint": the engine throws
328-
// on an unknown dialect "", and "" must mean auto-detect, not a real dialect.
329-
const raw = await core.checkEquivalence(params.sql1, params.sql2, schema, params.dialect || undefined)
408+
// [email protected]. dialectHint coerces "" (the ReviewConfig default)
409+
// to undefined: the engine throws on an unknown dialect "", and "" must
410+
// mean auto-detect, not a real dialect.
411+
const raw = await core.checkEquivalence(params.sql1, params.sql2, schema, EngineCoerce.dialectHint(params.dialect))
330412
const data = toData(raw)
331413
return ok(true, data)
332414
} catch (e) {
@@ -337,8 +419,9 @@ export function registerAll(): void {
337419
// 12. altimate_core.migration
338420
register("altimate_core.migration", async (params) => {
339421
try {
340-
// Build schema from old_ddl, analyze new_ddl against it
341-
const schema = core.Schema.fromDdl(params.old_ddl, params.dialect ?? undefined)
422+
// Build schema from old_ddl, analyze new_ddl against it. dialectHint
423+
// coerces "" to auto-detect (the engine throws on an unknown dialect "").
424+
const schema = core.Schema.fromDdl(params.old_ddl, EngineCoerce.dialectHint(params.dialect))
342425
const raw = core.analyzeMigration(params.new_ddl, schema)
343426
const data = toData(raw)
344427
return ok(true, data)
@@ -432,7 +515,7 @@ export function registerAll(): void {
432515
register("altimate_core.column_lineage", async (params) => {
433516
try {
434517
const schema = resolveSchema(params.schema_path, params.schema_context)
435-
const raw = core.columnLineage(params.sql, params.dialect ?? undefined, schema ?? undefined)
518+
const raw = core.columnLineage(params.sql, EngineCoerce.dialectHint(params.dialect), schema ?? undefined)
436519
return ok(true, toData(raw))
437520
} catch (e) {
438521
return fail(e)
@@ -453,7 +536,7 @@ export function registerAll(): void {
453536
// 22. altimate_core.format
454537
register("altimate_core.format", async (params) => {
455538
try {
456-
const raw = core.formatSql(params.sql, params.dialect ?? undefined)
539+
const raw = core.formatSql(params.sql, EngineCoerce.dialectHint(params.dialect))
457540
const data = toData(raw)
458541
return ok(true, data)
459542
} catch (e) {
@@ -464,7 +547,7 @@ export function registerAll(): void {
464547
// 23. altimate_core.metadata
465548
register("altimate_core.metadata", async (params) => {
466549
try {
467-
const raw = core.extractMetadata(params.sql, params.dialect ?? undefined)
550+
const raw = core.extractMetadata(params.sql, EngineCoerce.dialectHint(params.dialect))
468551
return ok(true, toData(raw))
469552
} catch (e) {
470553
return fail(e)
@@ -474,7 +557,7 @@ export function registerAll(): void {
474557
// 24. altimate_core.compare
475558
register("altimate_core.compare", async (params) => {
476559
try {
477-
const raw = core.compareQueries(params.left_sql, params.right_sql, params.dialect ?? undefined)
560+
const raw = core.compareQueries(params.left_sql, params.right_sql, EngineCoerce.dialectHint(params.dialect))
478561
return ok(true, toData(raw))
479562
} catch (e) {
480563
return fail(e)
@@ -528,7 +611,7 @@ export function registerAll(): void {
528611
// 29. altimate_core.import_ddl — returns Schema, must serialize
529612
register("altimate_core.import_ddl", async (params) => {
530613
try {
531-
const schema = core.importDdl(params.ddl, params.dialect ?? undefined)
614+
const schema = core.importDdl(params.ddl, EngineCoerce.dialectHint(params.dialect))
532615
const jsonObj = schema.toJson()
533616
return ok(true, { success: true, schema: toData(jsonObj) })
534617
} catch (e) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Shared coercions for altimate-core engine values.
3+
*
4+
* The engine's napi surface has recurring foot-guns for consumers:
5+
* - `PiiClassification` is a string OR `{ Custom: string }` — naive string
6+
* interpolation renders `[object Object]`.
7+
* - dialect parameters throw on the empty string (`unknown dialect ''`);
8+
* `""` must mean "auto-detect" and be passed as undefined.
9+
* - confidence is numeric (0..1) while several consumers declare string bands.
10+
*
11+
* This module must stay free of `@altimateai/altimate-core` imports so tool
12+
* modules can use it without eagerly loading the native NAPI binding.
13+
*/
14+
15+
/** PiiClassification is 'Email' | … | { Custom: string } | 'None'. */
16+
export function classificationToString(c: unknown, fallback = "UNKNOWN"): string {
17+
if (typeof c === "string") return c
18+
if (c && typeof c === "object" && typeof (c as { Custom?: unknown }).Custom === "string") {
19+
return (c as { Custom: string }).Custom
20+
}
21+
return fallback
22+
}
23+
24+
/** Coerce an optional dialect to an engine-safe hint: "" and null mean auto-detect. */
25+
export function dialectHint(dialect: string | undefined | null): string | undefined {
26+
return dialect || undefined
27+
}
28+
29+
/**
30+
* Map the engine's numeric confidence (0..1) to a string band.
31+
* Missing or non-numeric confidence is unknown, not low — band it "medium".
32+
*/
33+
export function bandConfidence(c: unknown): "high" | "medium" | "low" {
34+
if (typeof c === "string") {
35+
const s = c.toLowerCase()
36+
if (s === "high" || s === "medium" || s === "low") return s
37+
}
38+
const n = typeof c === "number" && Number.isFinite(c) ? c : 0.5
39+
if (n >= 0.8) return "high"
40+
if (n >= 0.5) return "medium"
41+
return "low"
42+
}
43+
44+
/**
45+
* Extract the real PII rows from an engine PiiReport.
46+
*
47+
* The engine returns `{ columns, pii_count, risk_level, total_columns }` with
48+
* a row for EVERY column — classification "None" means not PII.
49+
*
50+
* Fails closed: a report without an array `columns` field is malformed (the
51+
* engine always emits one) and throws instead of silently yielding zero
52+
* findings — silent-empty output is exactly the bug class this fixes.
53+
*/
54+
export function piiColumnsFromReport(piiData: unknown): Array<Record<string, any>> {
55+
const columns = (piiData as Record<string, any> | null | undefined)?.columns
56+
if (!Array.isArray(columns)) {
57+
throw new TypeError("malformed PiiReport: missing columns array")
58+
}
59+
return columns.filter((c) => c && c.classification !== "None")
60+
}
61+
62+
export * as EngineCoerce from "./engine-coerce"

‎packages/opencode/src/altimate/native/schema-resolver.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function flatToSchemaDefinition(flat: Record<string, any>): Record<string, any>
8484
* Normalize a schema_context into SchemaDefinition JSON format.
8585
* Accepts both flat and SchemaDefinition formats.
8686
*/
87-
function normalizeSchemaContext(ctx: Record<string, any>): string {
87+
export function normalizeSchemaContext(ctx: Record<string, any>): string {
8888
if (isSchemaDefinitionFormat(ctx)) {
8989
return JSON.stringify(ctx)
9090
}
@@ -120,3 +120,5 @@ export function schemaOrEmpty(
120120
if (s !== null) return s
121121
return Schema.fromDdl("CREATE TABLE _empty_ (id INT);")
122122
}
123+
124+
export * as SchemaResolver from "./schema-resolver"

0 commit comments

Comments
 (0)