Skip to content

Commit d4b1282

Browse files
anandgupta42claude
andcommitted
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]>
1 parent 494407e commit d4b1282

6 files changed

Lines changed: 76 additions & 31 deletions

File tree

‎packages/opencode/src/altimate/review/runner.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,16 @@ export function createDispatcherRunner(opts: DispatcherRunnerOptions): ReviewRun
205205
// top-level keys as a fallback for older core builds.
206206
const rawIssues = asArray(data.lint?.findings)
207207
.concat(asArray(data.validation?.errors).map((e: any) => ({ ...e, rule: "validate", severity: "error" })))
208-
.concat(asArray(data.safety?.threats))
208+
.concat(
209+
// ThreatFinding severity is low|medium|high|critical and its location
210+
// is a byte tuple — normalize both so downstream lanes (which only
211+
// recognize error/warning and location.line) classify them correctly.
212+
asArray(data.safety?.threats).map((t: any) => ({
213+
...t,
214+
severity: t.severity === "critical" || t.severity === "high" ? "error" : t.severity === "medium" ? "warning" : "info",
215+
location: undefined,
216+
})),
217+
)
209218
.concat(asArray(data.issues))
210219
.concat(asArray(data.violations))
211220
.concat(asArray(data.findings))

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ export const AltimateCoreSemanticsTool = Tool.define("altimate_core_semantics",
4545
title: hasError ? "Semantics: ERROR" : `Semantics: ${issueCount === 0 ? "VALID" : `${issueCount} issues`}`,
4646
metadata: {
4747
// Semantic issues are findings, not failures — but a failed engine
48-
// call must not report success.
49-
success: result.success,
48+
// call or an abstention (validation_errors) must not report success:
49+
// telemetry classifies soft failures on metadata.success === false.
50+
success: result.success && !hasError,
5051
valid: data.valid,
5152
issue_count: issueCount,
5253
has_schema: hasSchema,

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ import {
2222
// On Dispatcher failure, emit an error-severity finding so CI doesn't false-pass.
2323
// ---------------------------------------------------------------------------
2424

25+
/** Render an engine [byteOffset, byteLength] tuple as an inclusive byte range. */
26+
function byteRange(loc: unknown): string {
27+
if (!Array.isArray(loc) || loc.length < 2) return ""
28+
const [start, len] = loc as [number, number]
29+
return ` (bytes ${start}-${start + Math.max(len - 1, 0)})`
30+
}
31+
2532
function dispatcherErrorFinding(check: string, file: string, e: unknown): Finding {
2633
return {
2734
file,
@@ -110,26 +117,24 @@ async function runSafety(sql: string, file: string): Promise<Finding[]> {
110117
result.data.issues ??
111118
result.data.findings ??
112119
[]) as Array<Record<string, unknown>>
113-
if (issues.length > 0) {
114-
return issues.map((f) => {
115-
// ThreatFinding.location is [byteOffset, byteLength] — label as bytes
116-
// (offsets diverge from char indexes on multibyte SQL) and render the
117-
// INCLUSIVE end. ThreatFinding carries no line/column/code fields.
118-
const loc = f.location as [number, number] | undefined
119-
const at = Array.isArray(loc) ? ` (bytes ${loc[0]}-${loc[0] + Math.max(loc[1] - 1, 0)})` : ""
120-
return {
121-
file,
122-
rule: (f.rule ?? f.category ?? "safety") as string,
123-
severity: normalizeSeverity(f.severity as string),
124-
message: `${(f.message ?? f.description ?? "") as string}${at}`,
125-
suggestion: (f.suggestion ?? f.detail) as string | undefined,
126-
}
127-
})
128-
}
120+
const mapped = issues.map((f) => ({
121+
file,
122+
rule: (f.rule ?? f.category ?? "safety") as string,
123+
severity: normalizeSeverity(f.severity as string),
124+
// ThreatFinding.location is [byteOffset, byteLength] — labeled as bytes
125+
// (offsets diverge from char indexes on multibyte SQL), inclusive end.
126+
// ThreatFinding carries no line/column/code fields.
127+
message: `${(f.message ?? f.description ?? "") as string}${byteRange(f.location)}`,
128+
suggestion: (f.suggestion ?? f.detail) as string | undefined,
129+
}))
129130
if (!result.success) {
130131
// Fail closed like every other check — a crashed safety engine must not
131-
// pass --fail-on error.
132-
return [dispatcherErrorFinding("safety", file, result.error ?? "altimate_core.safety failed")]
132+
// pass --fail-on error, even when partial threats were returned and none
133+
// of them is error-severity.
134+
return [...mapped, dispatcherErrorFinding("safety", file, result.error ?? "altimate_core.safety failed")]
135+
}
136+
if (mapped.length > 0) {
137+
return mapped
133138
}
134139
if (result.data.safe === false) {
135140
return [
@@ -328,12 +333,10 @@ async function runGrade(
328333
}) as Array<Record<string, unknown>>),
329334
...(((safety?.threats as Array<Record<string, unknown>> | undefined) ?? []).map((t) => {
330335
// ThreatFinding: location is [byteOffset, byteLength]; detail is the fix hint
331-
const loc = t.location as [number, number] | undefined
332-
const at = Array.isArray(loc) ? ` (bytes ${loc[0]}-${loc[0] + Math.max(loc[1] - 1, 0)})` : ""
333336
return {
334337
...t,
335338
rule: (t.rule as string) ?? "safety",
336-
message: `${(t.message ?? "") as string}${at}`,
339+
message: `${(t.message ?? "") as string}${byteRange(t.location)}`,
337340
location: undefined,
338341
suggestion: t.suggestion ?? t.detail,
339342
}

‎packages/opencode/test/altimate/pii-detector-e2e.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import { afterAll, beforeAll, describe, expect, test } from "bun:test"
22

3-
import { detectPii, piiColumnsFromReport } from "../../src/altimate/native/schema/pii-detector"
43
import * as Registry from "../../src/altimate/native/connections/registry"
54

65
// Skip (don't crash) when the NAPI binary is absent — same guard as
7-
// altimate-core-e2e.test.ts; this file imports the engine at module scope.
6+
// altimate-core-e2e.test.ts. pii-detector imports the engine at module scope,
7+
// so it must be loaded AFTER (and only if) the guard passes.
88
let coreAvailable = false
99
try {
1010
require.resolve("@altimateai/altimate-core")
1111
coreAvailable = true
1212
} catch {}
1313
const describeIf = coreAvailable ? describe : describe.skip
1414

15+
const { detectPii, piiColumnsFromReport } = coreAvailable
16+
? await import("../../src/altimate/native/schema/pii-detector")
17+
: ({} as typeof import("../../src/altimate/native/schema/pii-detector"))
18+
1519
const RUN = coreAvailable && process.env.ALTIMATE_RUN_WAREHOUSE_E2E === "1"
1620
const e2eTest = RUN ? test : test.skip
1721

‎packages/opencode/test/altimate/tool-error-propagation.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,12 @@ describe("altimate_core_semantics error propagation", () => {
147147
stubCtx(),
148148
)
149149

150-
// Handler completed (success=true), but validation_errors are surfaced in metadata.error
151-
expect(result.metadata.success).toBe(true)
150+
// The handler completed, but validation_errors mean the semantic check
151+
// ABSTAINED — a soft failure: telemetry classifies on
152+
// metadata.success === false, so an ERROR-titled abstention must not
153+
// report success.
154+
expect(result.metadata.success).toBe(false)
155+
expect(result.title).toBe("Semantics: ERROR")
152156
expect(result.metadata.error).toContain("Failed to resolve table")
153157
expect(telemetryWouldExtract(result.metadata)).not.toBe("unknown error")
154158
})

‎packages/opencode/test/cli/check-e2e.test.ts‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,32 @@ describe("check command E2E", () => {
589589

590590
const r = await runHandler(baseArgs({ files: [file], checks: "safety" }))
591591
const j = parseJson(r.stdout)
592-
expect(j.results.safety.findings).toHaveLength(1)
592+
// success:false envelope adds a fail-closed error finding alongside the threat.
593+
expect(j.results.safety.findings).toHaveLength(2)
593594
expect(j.results.safety.findings[0].rule).toBe("sql-injection")
595+
expect(j.results.safety.findings[1].rule).toBe("safety-error")
596+
expect(j.results.safety.findings[1].severity).toBe("error")
597+
})
598+
599+
test("safety envelope failure with only sub-error threats still fails closed", async () => {
600+
// success:false + partial warning-severity threats must not let
601+
// --fail-on=error pass — an error-severity envelope finding is appended.
602+
const file = await writeSql(tmpDir.dir, "partial.sql", "SELECT 1;")
603+
setDispatcherResponse("altimate_core.safety", () => ({
604+
success: false,
605+
error: "scanner crashed midway",
606+
data: {
607+
safe: false,
608+
threats: [{ rule: "multi_statement", severity: "medium", message: "Multiple statements" }],
609+
},
610+
}))
611+
installDispatcherMocks()
612+
613+
const r = await runHandler(baseArgs({ files: [file], checks: "safety", "fail-on": "error", failOn: "error" }))
614+
const j = parseJson(r.stdout)
615+
expect(j.results.safety.findings).toHaveLength(2)
616+
expect(j.results.safety.findings.some((f: any) => f.severity === "error")).toBe(true)
617+
expect(j.summary.pass).toBe(false)
594618
})
595619

596620
test("safety check surfaces engine ThreatFinding shape (threats/rule/message/detail)", async () => {
@@ -615,9 +639,9 @@ describe("check command E2E", () => {
615639
severity: "high",
616640
message: "Unbalanced quote suggests injection breakout",
617641
detail: "Quote count is odd within a single statement",
618-
// Real engine semantics: [byteOffset, byteLength] — "OR 1=1 " at 37.
642+
// Real engine semantics: [byteOffset, byteLength] — the 'x' literal.
619643
location: [33, 3],
620-
matched_pattern: "' OR 1=1 --",
644+
matched_pattern: "'",
621645
},
622646
],
623647
},

0 commit comments

Comments
 (0)