Skip to content

Commit 90e2eae

Browse files
anandgupta42claude
andcommitted
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]>
1 parent 2762415 commit 90e2eae

6 files changed

Lines changed: 80 additions & 17 deletions

File tree

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

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import * as core from "@altimateai/altimate-core"
1212
import { EngineCoerce } from "./engine-coerce"
1313
import { register } from "./dispatcher"
14-
import { schemaOrEmpty, resolveSchema } from "./schema-resolver"
14+
import { schemaOrEmpty, resolveSchema, normalizeSchemaContext } from "./schema-resolver"
1515
import type { AltimateCoreResult } from "./types"
1616

1717
// ---------------------------------------------------------------------------
@@ -169,7 +169,11 @@ export function registerAll(): void {
169169
register("altimate_core.check", async (params) => {
170170
try {
171171
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
172-
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))
173177
// Diff-scoped lint: when a base SQL is supplied, core returns only the
174178
// findings the change INTRODUCED (pre-existing issues in the file are
175179
// dropped) — the structural comparison stays in the AST engine.
@@ -178,21 +182,39 @@ export function registerAll(): void {
178182
? core.lintDiff(
179183
params.sql,
180184
params.base_sql,
181-
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 ? normalizeSchemaContext(params.schema_context) : undefined,
182188
)
183189
: core.lint(params.sql, schema)
184190
// Diff-scope safety like lint: threats present in the base SQL are
185-
// pre-existing, not introduced by this change — subtract them by
186-
// (rule, matched_pattern) identity when a base is supplied.
187-
let safety = core.scanSql(params.sql)
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))
188197
if (params.base_sql) {
189198
try {
190-
const baseKeys = new Set(
191-
core.scanSql(params.base_sql).threats.map((t: any) => `${t.rule}|${t.matched_pattern}`),
192-
)
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+
})
193213
safety = {
194214
...safety,
195-
threats: safety.threats.filter((t: any) => !baseKeys.has(`${t.rule}|${t.matched_pattern}`)),
215+
threats: remaining,
216+
safe: remaining.length === 0 ? true : safety.safe,
217+
risk_score: remaining.length === 0 ? 0 : safety.risk_score,
196218
}
197219
} catch {
198220
// Unscannable base — keep the full head scan (fail open to MORE findings).
@@ -204,13 +226,26 @@ export function registerAll(): void {
204226
let pii: Record<string, unknown>
205227
try {
206228
pii = toData(core.checkQueryPii(params.sql, schema))
229+
if (params.base_sql && Array.isArray(pii.pii_columns) && (pii.pii_columns as any[]).length) {
230+
try {
231+
// Pre-existing exposures (same table.column already exposed by the
232+
// base) are not introduced by this change.
233+
const baseExposed = new Set(
234+
core.checkQueryPii(params.base_sql, schema).pii_columns.map((c: any) => `${c.table}|${c.column}`),
235+
)
236+
const remaining = (pii.pii_columns as any[]).filter((c: any) => !baseExposed.has(`${c.table}|${c.column}`))
237+
pii = { ...pii, pii_columns: remaining, accesses_pii: remaining.length > 0 ? pii.accesses_pii : false }
238+
} catch {
239+
// Unscannable base — keep the full head exposure list.
240+
}
241+
}
207242
} catch (e) {
208243
// Mark as an abstention — an empty object would render "No PII
209244
// detected", a false-clean verdict.
210245
pii = { parse_error: String(e) }
211246
}
212247
const data: Record<string, unknown> = {
213-
validation: toData(validation),
248+
validation,
214249
lint: toData(lintResult),
215250
safety: toData(safety),
216251
pii,

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

Lines changed: 1 addition & 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
}

‎packages/opencode/src/altimate/native/schema/pii-detector.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,5 @@ async function detectPiiLive(params: PiiDetectParams): Promise<PiiDetectResult>
220220
}
221221
}
222222
}
223+
224+
export * as PiiDetector from "./pii-detector"

‎packages/opencode/src/altimate/tools/altimate-core-policy.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ export const AltimateCorePolicyTool = Tool.define("altimate_core_policy", {
2525
const error = result.error ?? data.error
2626
// altimate_change start — sql quality findings for telemetry
2727
const violations = Array.isArray(data.violations) ? data.violations : []
28-
const findings: Telemetry.Finding[] = violations.map((v: any) => ({
29-
category: v.rule ?? "policy_violation",
30-
}))
28+
const warnings = Array.isArray(data.warnings) ? data.warnings : []
29+
const findings: Telemetry.Finding[] = [
30+
...violations.map((v: any) => ({ category: v.rule ?? "policy_violation" })),
31+
...warnings.map((w: any) => ({ category: w.rule ?? "policy_warning" })),
32+
]
3133
// altimate_change end
3234
// Engine PolicyResult: { allowed, violations, warnings, … } — `pass`
3335
// never existed, so every clean query used to render VIOLATIONS FOUND.

‎packages/opencode/src/cli/cmd/check.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ async function runSemantic(sql: string, file: string, schemaPath?: string): Prom
276276
}
277277
if (result.data.valid === false) {
278278
// SemanticResult.validation_errors carries the reason the query is
279-
// unplannable — surface it instead of a bare generic message.
279+
// unplannable — surface it instead of a bare generic message. With a
280+
// schema supplied this is an abstention that must fail closed (error);
281+
// schema-less runs abstain routinely, so only warn there.
280282
const detail = (result.data.validation_errors as unknown[] | undefined)
281283
?.map((e) => (typeof e === "string" ? e : ((e as Record<string, unknown>)?.message ?? String(e))))
282284
.filter(Boolean)
@@ -285,7 +287,7 @@ async function runSemantic(sql: string, file: string, schemaPath?: string): Prom
285287
{
286288
file,
287289
rule: "semantic",
288-
severity: "warning",
290+
severity: schemaPath ? ("error" as const) : ("warning" as const),
289291
message: (result.error ?? detail) || "Semantic check found issues",
290292
},
291293
]

‎packages/opencode/test/altimate/altimate-core-e2e.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,28 @@ describeIf("consumer contract sync (round 2)", () => {
13121312
})
13131313
const scopedThreats = ((scoped.data as any).safety?.threats ?? []).map((t: any) => t.rule)
13141314
expect(scopedThreats).not.toContain("tautology_attack")
1315+
// With every threat pre-existing, the verdict must not stay stale-unsafe.
1316+
expect((scoped.data as any).safety.safe).toBe(true)
1317+
expect((scoped.data as any).safety.risk_score).toBe(0)
1318+
})
1319+
1320+
test("composite check diff-scopes PII but NOT validation against base_sql", async () => {
1321+
const { Dispatcher } = await import("../../src/altimate/native")
1322+
const base = "SELECT zzz, email FROM customers"
1323+
const head = "SELECT zzz, yyy, email FROM customers"
1324+
const scoped = await Dispatcher.call("altimate_core.check", { sql: head, base_sql: base, schema_context: SCHEMA })
1325+
const d = scoped.data as any
1326+
// Validation is deliberately NOT diff-scoped: the engine reports only
1327+
// the FIRST error, so base subtraction could hide new breakage (yyy).
1328+
expect(d.validation.valid).toBe(false)
1329+
expect((d.validation.errors ?? []).length).toBeGreaterThan(0)
1330+
// email was already exposed by the base — not introduced by this change.
1331+
const exposed = (d.pii?.pii_columns ?? []).map((c: any) => c.column)
1332+
expect(exposed).not.toContain("email")
1333+
// Full run (no base) still reports the exposure.
1334+
const full = await Dispatcher.call("altimate_core.check", { sql: head, schema_context: SCHEMA })
1335+
const fullExposed = ((full.data as any).pii?.pii_columns ?? []).map((c: any) => c.column)
1336+
expect(fullExposed).toContain("email")
13151337
})
13161338

13171339
test("check tool PII section reports abstention for unparseable SQL, not a clean verdict", async () => {

0 commit comments

Comments
 (0)