diff --git a/packages/opencode/src/altimate/review/adapter-capabilities.ts b/packages/opencode/src/altimate/review/adapter-capabilities.ts index 537c6940c7..e9d51b21b5 100644 --- a/packages/opencode/src/altimate/review/adapter-capabilities.ts +++ b/packages/opencode/src/altimate/review/adapter-capabilities.ts @@ -18,6 +18,11 @@ export interface AdapterCapability { * leaves no table during the build. Undefined = not established. */ contractTableReplaceAtomic?: boolean + /** + * A microbatch model's `batch_size` must equal its `partition_by` + * granularity, or the run fails. Undefined = the adapter has no such rule. + */ + microbatchBatchSizeMatchesPartition?: boolean /** Documentation backing the entries above. */ sources: string[] } @@ -61,7 +66,12 @@ export const ADAPTER_CAPABILITIES: Record = { }, bigquery: { integerDivisionTruncates: false, - sources: ["https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#arithmetic_operators"], + microbatchBatchSizeMatchesPartition: true, + sources: [ + "https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#arithmetic_operators", + "https://docs.getdbt.com/docs/build/incremental-microbatch", + "https://github.com/dbt-labs/dbt-adapters/blob/main/dbt-bigquery/src/dbt/include/bigquery/macros/materializations/incremental_strategy/microbatch.sql", + ], }, databricks: { integerDivisionTruncates: false, diff --git a/packages/opencode/src/altimate/review/ai-review.ts b/packages/opencode/src/altimate/review/ai-review.ts index 63ce9e32d5..c31f9563cc 100644 --- a/packages/opencode/src/altimate/review/ai-review.ts +++ b/packages/opencode/src/altimate/review/ai-review.ts @@ -7,7 +7,7 @@ import { MessageID, SessionID } from "@/session/schema" import { Log } from "@/altimate/util/log" import { redactSecrets } from "@/installation" import { Dispatcher } from "../native" -import { type Finding, type ReviewCategory, type Severity, makeFinding } from "./finding" +import { type Finding, type ReviewCategory, type Severity, makeFinding, ruleId } from "./finding" import { readStreamText } from "../llm/stream-text" const log = Log.create({ service: "ai-review" }) @@ -16,6 +16,33 @@ const AI_TIMEOUT_BASE_MS = 60_000 const AI_TIMEOUT_MAX_MS = 300_000 const MAX_DIFF_CHARS = 6_000 // per file, keep the prompt bounded const MAX_FILES = 20 +const CONTEXT_FILE_CHARS = 8_000 +const CONTEXT_TOTAL_CHARS = 40_000 +const BATCH_CONCURRENCY = 2 + +/** + * How much of the PR the lane sends. Defaults keep one bounded call; the + * `ALTIMATE_REVIEW_AI_*` variables widen it for experiments: more files, longer + * files, and a per-call size above which files are split across several calls + * instead of being cut. + */ +export interface AiInputBudget { + maxFiles: number + maxFileChars: number + /** Characters of file sections per call; Infinity keeps a single call. */ + batchChars: number +} +export function aiInputBudget(env: Record = process.env): AiInputBudget { + const num = (v: string | undefined, d: number) => { + const n = Number(v) + return v && Number.isFinite(n) && n > 0 ? n : d + } + return { + maxFiles: num(env.ALTIMATE_REVIEW_AI_MAX_FILES, MAX_FILES), + maxFileChars: num(env.ALTIMATE_REVIEW_AI_MAX_FILE_CHARS, MAX_DIFF_CHARS), + batchChars: num(env.ALTIMATE_REVIEW_AI_BATCH_CHARS, Infinity), + } +} export interface AiReviewFile { path: string @@ -49,18 +76,62 @@ function kindRank(kind: string | undefined): number { return i < 0 ? KIND_ORDER.length : i } -/** Files the lane sends, in priority order, capped at MAX_FILES. */ -export function selectAiFiles(files: AiReviewFile[]): AiReviewFile[] { +/** Files the lane sends, in priority order, capped at `maxFiles`. */ +export function selectAiFiles(files: AiReviewFile[], maxFiles = MAX_FILES): AiReviewFile[] { return files .filter((f) => f.status !== "deleted" && (f.diff || f.sql)) .map((f, i) => ({ f, i })) .sort((a, b) => kindRank(a.f.kind) - kindRank(b.f.kind) || a.i - b.i) .map(({ f }) => f) - .slice(0, MAX_FILES) + .slice(0, maxFiles) +} + +/** One changed file as it appears in the prompt. */ +function fileSection(f: AiReviewFile, maxChars: number): string { + const label = f.kind && KIND_LABEL[f.kind] + const parts = [`\n### ${f.path} (${f.status}${label ? `, ${label}` : ""})`] + if (f.diff) parts.push("Diff:\n```diff\n" + truncate(f.diff, maxChars) + "\n```") + if (f.sql) { + const isModel = !f.kind || f.kind === "model_sql" + parts.push(`${isModel ? "Current SQL" : "Current file"}:\n\`\`\`${FENCE[f.kind ?? ""] ?? "sql"}\n` + truncate(f.sql, maxChars) + "\n```") + } + return parts.join("\n") +} + +/** Split files, in order, into groups whose sections fit `batchChars` (an oversized file goes alone). */ +export function batchAiFiles(files: AiReviewFile[], budget: AiInputBudget): AiReviewFile[][] { + const batches: AiReviewFile[][] = [] + let cur: AiReviewFile[] = [] + let size = 0 + for (const f of files) { + const n = fileSection(f, budget.maxFileChars).length + if (cur.length && size + n > budget.batchChars) { + batches.push(cur) + cur = [] + size = 0 + } + cur.push(f) + size += n + } + if (cur.length) batches.push(cur) + return batches +} + +/** An unchanged file the changed ones depend on (dbt DAG parent), sent as read-only context. */ +export interface AiContextFile { + path: string + /** dbt resource type: `model`, `seed`, `snapshot`, `source`, `macro`. */ + kind: string + name: string + /** Changed files that depend on it. */ + forPaths: string[] + content: string } export interface AiReviewInput { files: AiReviewFile[] + /** Unchanged parents of the changed files, for cross-file checks (columns, seeds, macro signatures). */ + context?: AiContextFile[] /** Deterministic engine findings — grounding the AI must NOT duplicate. */ grounding: Finding[] prTitle?: string @@ -78,6 +149,21 @@ export interface AiLaneStatus { /** `provider/model` the lane ran with. */ model?: string durationMs?: number + /** Tokens spent across all calls that reported usage. */ + tokens?: AiTokens + /** Estimated cost in USD from the model's price table (0 when unpriced). */ + cost?: number + /** Model calls made (one per batch). */ + calls?: number +} +/** Token counts as `Session.getUsage` reports them: `input` excludes cached + * tokens, `inputTotal` includes them. */ +export interface AiTokens { + input: number + inputTotal: number + output: number + reasoning: number + cacheRead: number } export interface AiReviewResult { findings: Finding[] @@ -116,8 +202,23 @@ function truncate(s: string | undefined, n: number): string { return s.length > n ? s.slice(0, n) + "\n… (truncated)" : s } +/** Context sections for the parents of the files in this call, within the context budget. */ +function relatedContext(input: AiReviewInput): string[] { + const paths = new Set(input.files.map((f) => f.path)) + const out: string[] = [] + let used = 0 + for (const c of input.context ?? []) { + if (paths.has(c.path) || !c.forPaths.some((p) => paths.has(p))) continue + const section = `\n### ${c.path} (${c.kind} \`${c.name}\`, used by ${c.forPaths.filter((p) => paths.has(p)).join(", ")})\n\`\`\`${c.kind === "source" || c.path.endsWith(".yml") ? "yaml" : c.path.endsWith(".csv") ? "csv" : "sql"}\n${truncate(c.content, CONTEXT_FILE_CHARS)}\n\`\`\`` + if (used + section.length > CONTEXT_TOTAL_CHARS) break + out.push(section) + used += section.length + } + return out +} + /** Assemble the user message (mechanical formatting — not IP). */ -export function buildUserMessage(input: AiReviewInput): string { +export function buildUserMessage(input: AiReviewInput, maxFileChars = MAX_DIFF_CHARS): string { const parts: string[] = [] if (input.prTitle) parts.push(`PR title: ${truncate(input.prTitle, 200)}`) if (input.prBody) parts.push(`PR description:\n${truncate(input.prBody, 1500)}`) @@ -132,14 +233,13 @@ export function buildUserMessage(input: AiReviewInput): string { } parts.push("\n## CHANGED FILES (review for what the engine missed):") - for (const f of input.files.slice(0, MAX_FILES)) { - const label = f.kind && KIND_LABEL[f.kind] - parts.push(`\n### ${f.path} (${f.status}${label ? `, ${label}` : ""})`) - if (f.diff) parts.push("Diff:\n```diff\n" + truncate(f.diff, MAX_DIFF_CHARS) + "\n```") - if (f.sql) { - const isModel = !f.kind || f.kind === "model_sql" - parts.push(`${isModel ? "Current SQL" : "Current file"}:\n\`\`\`${FENCE[f.kind ?? ""] ?? "sql"}\n` + truncate(f.sql, MAX_DIFF_CHARS) + "\n```") - } + for (const f of input.files) parts.push(fileSection(f, maxFileChars)) + const context = relatedContext(input) + if (context.length) { + parts.push( + "\n## RELATED FILES — unchanged dbt parents of the changed files (context only; comment on the changed files, not these):", + ...context, + ) } parts.push("\nReturn ONLY the JSON array per the output contract.") return parts.join("\n") @@ -158,17 +258,12 @@ export async function runAiReview(input: AiReviewInput): Promise findings, status: { ...status, durationMs: Date.now() - startedAt }, }) - const files = selectAiFiles(input.files) + const budget = aiInputBudget() + const files = selectAiFiles(input.files, budget.maxFiles) if (!files.length) return done([], { state: "skipped", reason: "no changed dbt files to review" }) - const controller = new AbortController() - let timeoutMs = AI_TIMEOUT_BASE_MS - let timeout: ReturnType | undefined let modelLabel: string | undefined try { - const message = buildUserMessage({ ...input, files }) - timeoutMs = aiTimeoutMs(message.length) - timeout = setTimeout(() => controller.abort(), timeoutMs) // Prompt comes from the compiled core, not this file. const promptRes = await Dispatcher.call("altimate_core.review_ai_prompt", {}) const system = ((promptRes.data ?? {}) as Record).prompt as string | undefined @@ -189,6 +284,110 @@ export async function runAiReview(input: AiReviewInput): Promise } modelLabel = `${model.providerID}/${model.id}` + // One call per batch; a PR that fits the budget is a single call, as before. + const batches = batchAiFiles(files, budget) + const results: BatchResult[] = [] + for (let i = 0; i < batches.length; i += BATCH_CONCURRENCY) { + const slice = batches.slice(i, i + BATCH_CONCURRENCY) + results.push(...(await Promise.all(slice.map((b) => reviewBatch(input, b, budget, system, model))))) + } + const findings = results.flatMap((r) => r.findings) + const failed = results.filter((r) => r.state !== "ok") + log.info("ai review complete", { findings: findings.length, batches: batches.length, failed: failed.length }) + // The lane is ok when any call answered; a partial failure is reported, not hidden. + const spent = laneUsage(results) + if (failed.length === results.length) + return done([], { state: failed[0].state, reason: failed[0].reason, model: modelLabel, ...spent }) + const reason = failed.length ? `${failed.length} of ${results.length} calls failed: ${failed[0].reason}` : undefined + return done(findings, { state: "ok", model: modelLabel, ...(reason ? { reason } : {}), ...spent }) + } catch (err) { + log.error("ai review failed", { error: err }) + return done([], { state: "error", reason: safeReason(err), model: modelLabel }) + } +} + +interface BatchResult { + findings: Finding[] + state: AiLaneState + reason?: string + usage?: BatchUsage +} + +export interface BatchUsage { + tokens: AiTokens + cost: number +} + +export function addUsage(a: BatchUsage, b: BatchUsage): BatchUsage { + return { + tokens: { + input: a.tokens.input + b.tokens.input, + inputTotal: a.tokens.inputTotal + b.tokens.inputTotal, + output: a.tokens.output + b.tokens.output, + reasoning: a.tokens.reasoning + b.tokens.reasoning, + cacheRead: a.tokens.cacheRead + b.tokens.cacheRead, + }, + cost: a.cost + b.cost, + } +} + +/** Lane-status accounting fields for a set of batch results: calls made, and + * tokens and cost summed over the calls that reported usage. */ +export function laneUsage(results: Array<{ usage?: BatchUsage }>): Pick { + const reported = results.flatMap((r) => (r.usage ? [r.usage] : [])) + if (!reported.length) return { calls: results.length } + const total = reported.reduce(addUsage) + return { tokens: total.tokens, cost: total.cost, calls: results.length } +} + +/** + * Tokens and cost of one drained stream, with the session's accounting rules. + * Returns undefined when the provider reported nothing usable: accounting + * never fails the lane. + */ +export async function streamUsage( + stream: { totalUsage: PromiseLike; providerMetadata: PromiseLike }, + model: Pick, +): Promise { + try { + const usage = await stream.totalUsage + const metadata = await stream.providerMetadata + if (!usage) return undefined + // Loaded lazily, as in learn/usage.ts, to keep the session module out of + // this module's import graph. + const { Session } = await import("@/session") + const u = Session.getUsage({ model: model as Provider.Model, usage: usage as any, metadata: (metadata ?? undefined) as any }) + return { + tokens: { + input: u.tokens.input, + inputTotal: u.tokens.inputTotal, + output: u.tokens.output, + reasoning: u.tokens.reasoning, + cacheRead: u.tokens.cache.read, + }, + cost: u.cost, + } + } catch (err) { + log.warn("ai review usage unavailable", { error: err }) + return undefined + } +} + +/** One model call over a group of files: send, wait (bounded), parse and clamp in core. */ +async function reviewBatch( + input: AiReviewInput, + files: AiReviewFile[], + budget: AiInputBudget, + system: string, + model: Awaited>, +): Promise { + const message = buildUserMessage({ ...input, files }, budget.maxFileChars) + const controller = new AbortController() + const timeoutMs = aiTimeoutMs(message.length) + const timeout = setTimeout(() => controller.abort(), timeoutMs) + const timedOut = (): BatchResult => ({ findings: [], state: "timeout", reason: `no answer within ${Math.round(timeoutMs / 1000)} s` }) + let usage: BatchUsage | undefined + try { const agent: Agent.Info = { name: "dbt-ai-reviewer", mode: "primary", @@ -206,7 +405,6 @@ export async function runAiReview(input: AiReviewInput): Promise agent: agent.name, model: { providerID: model.providerID, modelID: model.id }, } - const stream = await LLM.stream({ agent, user, @@ -220,9 +418,9 @@ export async function runAiReview(input: AiReviewInput): Promise messages: [{ role: "user", content: message }], }) const text = await readStreamText(stream) - if (controller.signal.aborted) - return done([], { state: "timeout", reason: `no answer within ${Math.round(timeoutMs / 1000)} s`, model: modelLabel }) - if (!text) return done([], { state: "error", reason: "the model returned no text", model: modelLabel }) + if (controller.signal.aborted) return timedOut() + usage = await streamUsage(stream, model) + if (!text) return { findings: [], state: "error", reason: "the model returned no text", usage } // Parse + clamp in core (the prompt-injection-resistant, advisory-only // contract). Returns already-validated, severity-clamped, file-checked items. @@ -234,11 +432,11 @@ export async function runAiReview(input: AiReviewInput): Promise // throwing; that is an error, never an "ok" run with no findings. const parsedRaw = ((parseRes.data ?? {}) as Record).findings if (parseRes.success === false || !Array.isArray(parsedRaw)) - return done([], { + return { + findings: [], state: "error", reason: `could not parse the model's answer: ${safeReason(parseRes.error ?? "no findings list")}`, - model: modelLabel, - }) + } const parsed = parsedRaw as any[] const byFile = new Map(files.map((f) => [f.path, f])) @@ -267,19 +465,17 @@ export async function runAiReview(input: AiReviewInput): Promise confidence: item.confidence === "high" ? "high" : item.confidence === "low" ? "low" : "medium", evidence: { tool: "ai-review", result: { confidence: item.confidence } }, ruleKey: `ai:${item.category}:${String(item.title).slice(0, 60)}`, + rule: ruleId("ai-review", String(item.category)), }), ) } catch (err) { log.warn("skipping malformed ai finding", { error: err }) } } - log.info("ai review complete", { findings: out.length }) - return done(out, { state: "ok", model: modelLabel }) + return { findings: out, state: "ok", usage } } catch (err) { - log.error("ai review failed", { error: err }) - if (controller.signal.aborted) - return done([], { state: "timeout", reason: `no answer within ${Math.round(timeoutMs / 1000)} s`, model: modelLabel }) - return done([], { state: "error", reason: safeReason(err), model: modelLabel }) + if (controller.signal.aborted) return timedOut() + return { findings: [], state: "error", reason: safeReason(err), usage } } finally { clearTimeout(timeout) } diff --git a/packages/opencode/src/altimate/review/config.ts b/packages/opencode/src/altimate/review/config.ts index 55587f9acd..f95f224550 100644 --- a/packages/opencode/src/altimate/review/config.ts +++ b/packages/opencode/src/altimate/review/config.ts @@ -4,7 +4,7 @@ import { promises as fs } from "node:fs" import YAML from "yaml" import { createHash } from "node:crypto" import { Rubric, DEFAULT_RUBRIC } from "./rubric" -import { ReviewMode } from "./verdict" +import { ReviewMode, DEFAULT_POSTING_POLICY } from "./verdict" import { Severity } from "./finding" /** @@ -77,6 +77,27 @@ export const ReviewConfig = z.object({ .default({ enabled: false, warehouse: "" }), /** Rubric overrides, deep-merged onto DEFAULT_RUBRIC. */ rubric: Rubric.partial().default({}), + /** + * Per-rule posting policy, keyed by finding `rule` id (`tool:rule`) or a + * `tool:*` glob: `inline` posts on the diff, `summary` keeps it to the + * summary comment, `off` drops it. Overrides the shipped defaults in + * rule-tiers.ts. Read from the base branch, so a PR cannot relax it. + * Engine rules that decide the verdict can be turned off only by exact id. + */ + rules: z.record(z.string(), z.enum(["inline", "summary", "off"])).default({}), + /** + * How findings reach the PR: `inlineBudget` caps new inline comments per run + * (the rest stay in the summary), `inlineSeverityFloor` keeps lower + * severities out of inline comments, and `silentWhenClean` posts nothing on + * a run with no findings and nothing to report about coverage. + */ + posting: z + .object({ + inlineBudget: z.number().int().min(0).default(DEFAULT_POSTING_POLICY.inlineBudget), + inlineSeverityFloor: Severity.default(DEFAULT_POSTING_POLICY.inlineSeverityFloor), + silentWhenClean: z.boolean().default(DEFAULT_POSTING_POLICY.silentWhenClean), + }) + .default(() => ({ ...DEFAULT_POSTING_POLICY })), /** Extra path suffixes to exclude from review. */ exclude: z.array(z.string()).default([]), /** diff --git a/packages/opencode/src/altimate/review/dbt-patterns.ts b/packages/opencode/src/altimate/review/dbt-patterns.ts index 87018a977e..7ec3794cab 100644 --- a/packages/opencode/src/altimate/review/dbt-patterns.ts +++ b/packages/opencode/src/altimate/review/dbt-patterns.ts @@ -135,7 +135,7 @@ const detectClock: Detector = (c) => { file: c.file.path, model: c.model, confidence: "high", - evidence: { tool: "dbt-patterns", result: { rule: "clock_in_transform", lines: hits.slice(0, 3) } }, + evidence: { tool: "dbt-patterns", result: { rule: "clock_in_transform", line: hits[0], lines: hits.slice(0, 3) } }, ruleKey: "idempotency:clock", }) } @@ -240,7 +240,7 @@ const detectFanout: Detector = (c) => { confidence: "high", evidence: { tool: "dbt-patterns", - result: { rule: "fanout_join", joins: newJoins.map((j) => j.trim()).slice(0, 2) }, + result: { rule: "fanout_join", line: newJoins[0], joins: newJoins.map((j) => j.trim()).slice(0, 2) }, }, ruleKey: "fanout:join-before-agg", }) @@ -293,18 +293,19 @@ function addedLineNumbers(diff: string | undefined): Set | undefined { * only when the diff carries no hunk headers, and then only for added lines * that themselves hold window syntax). */ -function dedupWindows(c: Ctx): Array<{ spec: string; text: string }> { +function dedupWindows(c: Ctx): Array<{ spec: string; text: string; line: string }> { // Comments blanked with newlines kept, so offsets map to head line numbers. const sql = c.newSql.replace(/\/\*[\s\S]*?\*\/|--[^\n]*|\{#[\s\S]*?#\}/g, (m) => m.replace(/[^\n]/g, " ")) const lineAt = (offset: number) => sql.slice(0, offset).split("\n").length + const headLines = c.newSql.split("\n") const addedLines = addedLineNumbers(c.file.diff) // Text fallback (diff without hunk headers): only added lines that are part // of a window spec can touch one; a bare identifier elsewhere never does. const squash = (t: string) => t.replace(/\s+/g, " ").trim() const added = c.added - .map((l) => squash(stripComments(l))) - .filter((l) => l.length >= 3 && /\b(row_number|over|partition\s+by|order\s+by)\b/i.test(l)) - const out: Array<{ spec: string; text: string }> = [] + .map((raw) => ({ raw, l: squash(stripComments(raw)) })) + .filter(({ l }) => l.length >= 3 && /\b(row_number|over|partition\s+by|order\s+by)\b/i.test(l)) + const out: Array<{ spec: string; text: string; line: string }> = [] for (const m of sql.matchAll(/row_number\s*\(\s*\)\s*over\s*\(/gi)) { const open = m.index! + m[0].length - 1 let depth = 0 @@ -318,11 +319,22 @@ function dedupWindows(c: Ctx): Array<{ spec: string; text: string }> { } if (close < 0) continue const text = sql.slice(m.index!, close + 1) - const touched = addedLines - ? [...addedLines].some((n) => n >= lineAt(m.index!) && n <= lineAt(close)) - : added.some((l) => squash(text).includes(l) || l.includes(squash(text))) - if (!touched) continue - out.push({ spec: sql.slice(open + 1, close).replace(/\s+/g, " ").trim(), text: text.replace(/\s+/g, " ") }) + let line: string | undefined + if (addedLines) { + // The added line that opens the window, else the first added line inside it. + const first = lineAt(m.index!) + const last = lineAt(close) + const touched = [...addedLines].filter((n) => n >= first && n <= last).sort((a, b) => a - b) + if (!touched.length) continue + line = headLines[(touched.includes(first) ? first : touched[0]) - 1] ?? "" + } else { + const inside = added.filter(({ l }) => squash(text).includes(l) || l.includes(squash(text))) + if (!inside.length) continue + const end = sql.indexOf("\n", m.index!) + const opener = sql.slice(sql.lastIndexOf("\n", m.index!) + 1, end < 0 ? undefined : end).trim() + line = (added.find(({ l }) => l === squash(opener)) ?? inside[0]).raw + } + out.push({ spec: sql.slice(open + 1, close).replace(/\s+/g, " ").trim(), text: text.replace(/\s+/g, " "), line }) } return out } @@ -345,7 +357,7 @@ const detectDedupTie: Detector = (c) => { file: c.file.path, model: c.model, confidence: "high", - evidence: { tool: "dbt-patterns", result: { rule: "dedup_no_tiebreaker", line: hit.text } }, + evidence: { tool: "dbt-patterns", result: { rule: "dedup_no_tiebreaker", line: hit.line, window: hit.text } }, ruleKey: "dedup:no-tiebreaker", }) } @@ -375,7 +387,7 @@ const detectDedupLoadTimestamp: Detector = (c) => { file: c.file.path, model: c.model, confidence: "high", - evidence: { tool: "dbt-patterns", result: { rule: "dedup_load_timestamp_only", line: w.text } }, + evidence: { tool: "dbt-patterns", result: { rule: "dedup_load_timestamp_only", line: w.line, window: w.text } }, ruleKey: "dedup:load-timestamp-only", }) } @@ -443,6 +455,7 @@ const detectCountDistinct: Detector = (c) => { const addedDistinct = c.added.some(hasCountDistinct) const removedPlain = c.removed.some(hasPlainCount) if (!((removedDistinct && addedPlain) || (addedDistinct && removedPlain))) return null + const line = c.added.find(removedDistinct && addedPlain ? hasPlainCount : hasCountDistinct) return makeFinding({ severity: "warning", category: "sql_correctness", @@ -453,7 +466,7 @@ const detectCountDistinct: Detector = (c) => { file: c.file.path, model: c.model, confidence: "high", - evidence: { tool: "dbt-patterns", result: { rule: "count_distinct_change" } }, + evidence: { tool: "dbt-patterns", result: { rule: "count_distinct_change", line } }, ruleKey: "sql_correctness:count-distinct", }) } @@ -722,6 +735,7 @@ const detectCaseNoElse: Detector = (c) => { (m) => !/\belse\b/i.test(m[0]) && !NULL_CONSUMING_CALL.test(joined.slice(0, m.index)), ) if (!bare) return null + const head = bare[0].split("\n")[0].trim() return pattern( c, "sql_correctness", @@ -729,6 +743,7 @@ const detectCaseNoElse: Detector = (c) => { "case-no-else", "CASE without ELSE returns NULL for unmatched rows", "A `CASE` expression with no `ELSE` silently yields NULL for any unmatched row, which can skew downstream metrics. Add an explicit `ELSE`.", + c.added.find((l) => stripComments(l).includes(head)), ) } @@ -764,7 +779,8 @@ const detectNaturalJoin: Detector = (c) => { // 38. Self-join: same ref()/source() appears 2+ times. const detectSelfJoin: Detector = (c) => { - if (!addedHit(c, /\bjoin\b/i)) return null + const hit = addedHit(c, /\bjoin\b/i) + if (!hit) return null // A self-join reads one relation twice in the same FROM … JOIN chain. The same ref in two // CTEs, or in two UNION branches, is two separate reads, not a self-join. const sql = stripComments(c.newSql).replace(/\{\{\s*(ref|source)\s*\(([^)]*)\)\s*\}\}/gi, (_, kind, args) => @@ -778,6 +794,7 @@ const detectSelfJoin: Detector = (c) => { "self-join", "self-join detected — verify it can't fan out", "The same relation is joined to itself. Self-joins are easy to get wrong (missing grain predicate → fan-out). Confirm the join keys keep it one-to-one.", + hit, ) } @@ -802,7 +819,7 @@ const detectWindowNoPartition: Detector = (c) => { const head = sql.slice(lineStart, open + 1).trim() return changed.some((l) => l.includes(head)) && !/partition\s+by/i.test(sql.slice(open, end)) }) - const hit = unpartitioned ? changed[0] : undefined + const hit = unpartitioned ? c.added.find((l) => stripComments(l).trim() === changed[0]) : undefined if (!hit) return null return pattern( c, @@ -903,6 +920,7 @@ const detectOffsetNoOrder: Detector = (c) => { const detectMultiCountDistinct: Detector = (c) => { const count = (stripComments(c.added.join("\n")).match(/count\s*\(\s*distinct/gi) || []).length if (count < 2) return null + const hit = addedHit(c, /count\s*\(\s*distinct/i) return pattern( c, "warehouse_cost", @@ -910,6 +928,7 @@ const detectMultiCountDistinct: Detector = (c) => { "multi-count-distinct", `${count} COUNT(DISTINCT …) in one query is expensive`, "Multiple `COUNT(DISTINCT …)` each need their own sort/hash; on large tables this is a major cost. Consider APPROX_COUNT_DISTINCT or pre-aggregation where exactness isn't required.", + hit, ) } @@ -998,7 +1017,7 @@ export function detectMacroPatterns( file: file.path, model, confidence: "high", - evidence: { tool: "dbt-patterns", result: { rule: "macro-var-no-default", var: name } }, + evidence: { tool: "dbt-patterns", result: { rule: "macro-var-no-default", var: name, line } }, ruleKey: `materialization:macro-var-no-default`, }), ) diff --git a/packages/opencode/src/altimate/review/diff-filter.ts b/packages/opencode/src/altimate/review/diff-filter.ts index 2e1435200c..ee5673caa5 100644 --- a/packages/opencode/src/altimate/review/diff-filter.ts +++ b/packages/opencode/src/altimate/review/diff-filter.ts @@ -7,6 +7,8 @@ * snapshots, macros, schema.yml contract blocks, seeds. */ +import { posix } from "node:path" + /** Paths that are build output / vendored and carry no review signal. */ const SKIP_DIR_PATTERNS = [ /(^|\/)target\//, @@ -86,20 +88,41 @@ export function relativeToRoot(p: string, root: string): string | undefined { return f.startsWith(r + "/") ? f.slice(r.length + 1) : undefined } -/** The role of a project-relative path from the configured directories; the +/** + * The layout's directories as repo-relative paths. Each `*-paths` entry is + * relative to the project root and may climb out of it (`../../dbt_macros` + * in a monorepo of sub-projects sharing one macro dir); entries that resolve + * outside the repository are dropped. + */ +export function resolvedLayoutDirs(layout: DbtProjectLayout): Array<{ role: DbtRole; dir: string }> { + const root = norm(layout.root).replace(/^\.\/?/, "").replace(/\/+$/, "") + const out: Array<{ role: DbtRole; dir: string }> = [] + for (const [role, list] of Object.entries(layout.dirs) as Array<[DbtRole, string[]]>) { + for (const d of list) { + const dir = posix.normalize(posix.join(root || ".", norm(d))).replace(/\/+$/, "") + // The project root itself (`model-paths: ["."]` with the project at the repo root). + if (dir === ".") { + out.push({ role, dir: "" }) + continue + } + if (!dir || dir === ".." || dir.startsWith("../") || dir.startsWith("/")) continue + out.push({ role, dir }) + } + } + return out +} + +/** The role of a repo-relative path from the configured directories; the * longest matching directory wins so `models/tests/x.sql` is a model when * `models` is a model path. */ -function roleFromLayout(rel: string, dirs: Record): DbtRole | undefined { - const p = rel.toLowerCase() +function roleFromLayout(path: string, layout: DbtProjectLayout): DbtRole | undefined { + const p = norm(path).toLowerCase() let best: { role: DbtRole; len: number } | undefined - for (const [role, list] of Object.entries(dirs) as Array<[DbtRole, string[]]>) { - for (const d of list) { - const dir = norm(d).toLowerCase().replace(/^\.\/?/, "").replace(/\/+$/, "") - // `model-paths: ["."]` normalises to "": it matches every path at length 0, - // so a more specific directory still wins. - const matches = dir === "" || p.startsWith(dir + "/") - if (matches && (!best || dir.length > best.len)) best = { role, len: dir.length } - } + for (const { role, dir } of resolvedLayoutDirs(layout)) { + const d = dir.toLowerCase() + // "" (`model-paths: ["."]` at the repo root) matches every path at length 0, + // so a more specific directory still wins. + if ((d === "" || p.startsWith(d + "/")) && (!best || d.length > best.len)) best = { role, len: d.length } } return best?.role } @@ -123,8 +146,12 @@ export function classifyDbtFile(path: string, layout?: DbtProjectLayout): DbtFil const p = norm(path).toLowerCase() const isYaml = p.endsWith(".yml") || p.endsWith(".yaml") if (/(^|\/)(dbt_project|profiles|packages|dependencies)\.ya?ml$/.test(p)) return "project_config" - const rel = layout ? relativeToRoot(norm(path), layout.root) : undefined - const role = rel !== undefined && layout ? roleFromLayout(rel, layout.dirs) : roleFromConvention(p) + // A layout dir decides wherever it points, including outside the project + // root; other files outside the root fall back to conventional names. + const role = layout + ? (roleFromLayout(path, layout) ?? + (relativeToRoot(norm(path), layout.root) === undefined ? roleFromConvention(p) : undefined)) + : roleFromConvention(p) if (role === "macro") return "macro" // Snapshots split by extension: `.sql` files are snapshot definitions // (tier-forcing, catalog-scoped), YAML property files under `snapshots/` @@ -195,18 +222,45 @@ function globToRegExp(glob: string): RegExp { return new RegExp(`^${re}$`) } +/** A changed file the review did not look at, and why. */ +export interface NotReviewed { + path: string + reason: string +} + +/** Split changed files into the reviewable set (with their dbt kind) and the + * files dropped before review, each with the reason it was dropped. */ +export function partitionChangedFiles( + files: ChangedFile[], + extraExcludeGlobs: string[] = [], + layout?: DbtProjectLayout, +): { reviewable: Array; dropped: NotReviewed[] } { + // Normalize to forward slashes so `/`-based glob semantics hold on Windows paths. + const excluders = extraExcludeGlobs.map((g) => ({ glob: g, re: globToRegExp(g) })) + const reviewable: Array = [] + const dropped: NotReviewed[] = [] + for (const f of files) { + if (!shouldReview(f.path)) { + dropped.push({ path: f.path, reason: "not a dbt file (extension or generated path)" }) + continue + } + const hit = excluders.find(({ re }) => re.test(norm(f.path))) + if (hit) { + dropped.push({ path: f.path, reason: `excluded by glob ${hit.glob}` }) + continue + } + reviewable.push({ ...f, kind: classifyDbtFile(f.path, layout) }) + } + return { reviewable, dropped } +} + /** Filter changed files down to the reviewable set, with their dbt kind. */ export function filterChangedFiles( files: ChangedFile[], extraExcludeGlobs: string[] = [], layout?: DbtProjectLayout, ): Array { - const excluders = extraExcludeGlobs.map(globToRegExp) - // Normalize to forward slashes so `/`-based glob semantics hold on Windows paths. - return files - .filter((f) => shouldReview(f.path)) - .filter((f) => !excluders.some((re) => re.test(norm(f.path)))) - .map((f) => ({ ...f, kind: classifyDbtFile(f.path, layout) })) + return partitionChangedFiles(files, extraExcludeGlobs, layout).reviewable } /** The first folder under the file's role directory (`staging` for @@ -231,6 +285,10 @@ export interface ScopeResult { outOfScope: number /** Non-model renames with identical content that keep the name, role and layer (a pure move). */ movedUnchanged: number + /** Paths behind `outOfScope`. */ + outOfScopePaths: string[] + /** Paths behind `movedUnchanged`. */ + movedUnchangedPaths: string[] } const isModel = (p: string, layout?: DbtProjectLayout) => { @@ -243,7 +301,9 @@ const isModel = (p: string, layout?: DbtProjectLayout) => { * * - Files outside `root` belong to another project or to non-dbt code: they * are counted, never reviewed (a monorepo with several dbt projects must not - * review its siblings). + * review its siblings). The exception is a layout dir that points outside + * the root (`macro-paths: ["../../dbt_macros"]`): files there are part of + * the project and stay in scope. * - Every model move stays in scope, even a same-name 100% rename: a model's * folder can carry config in `dbt_project.yml` (materialization, schema, * tags) and match risk paths, so moving it can change how it builds. @@ -260,17 +320,19 @@ export function scopeToProject( root: string | undefined, layout?: DbtProjectLayout, ): ScopeResult { - let outOfScope = 0 - let movedUnchanged = 0 + const outOfScopePaths: string[] = [] + const movedUnchangedPaths: string[] = [] const inScope: ChangedFile[] = [] const basename = (p: string) => norm(p).split("/").pop() - const isInside = (p: string) => root === undefined || relativeToRoot(p, root) !== undefined + const layoutDirs = layout ? resolvedLayoutDirs(layout).map((d) => d.dir) : [] + const isInside = (p: string) => + root === undefined || relativeToRoot(p, root) !== undefined || layoutDirs.some((d) => relativeToRoot(p, d) !== undefined) for (const f of files) { const inside = isInside(f.path) // A rename moving a file into or out of the project still concerns it. const oldInside = !!f.oldPath && isInside(f.oldPath) if (!inside && !oldInside) { - outOfScope++ + outOfScopePaths.push(f.path) continue } if (f.oldPath && inside !== oldInside) { @@ -287,12 +349,18 @@ export function scopeToProject( classifyDbtFile(f.oldPath, layout) === classifyDbtFile(f.path, layout) && layerOf(f.oldPath, root, layout) === layerOf(f.path, root, layout) ) { - movedUnchanged++ + movedUnchangedPaths.push(f.path) continue } inScope.push(f) } - return { inScope, outOfScope, movedUnchanged } + return { + inScope, + outOfScope: outOfScopePaths.length, + movedUnchanged: movedUnchangedPaths.length, + outOfScopePaths, + movedUnchangedPaths, + } } /** Heuristic: does a yml diff declare dbt `sources:`? Tolerates unified-diff @@ -315,3 +383,51 @@ export function countChangedLines(diff: string | undefined): number { } return n } + +/** A row inside a unified diff hunk. `line` is the head-side line number for + * `+` and context rows; for a `-` row it is the head line that follows the + * deletion. `hunk` numbers the hunks from 0. */ +export interface DiffRow { + line: number + text: string + kind: "+" | "-" | " " + hunk: number +} + +/** Walk every row of a unified diff's hunks: added, removed and context lines. */ +export function* walkDiffRows(diff: string | undefined): Generator { + if (!diff) return + let right = 0 + let hunk = -1 + let inHunk = false + const rows = diff.split("\n") + if (rows[rows.length - 1] === "") rows.pop() // the diff's trailing newline, not a line + for (const row of rows) { + const h = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(row) + if (h) { + right = Number(h[1]) + hunk++ + inHunk = true + continue + } + // File headers (`--- a/…`, `+++ b/…`) only occur outside a hunk. + if (!inHunk || row.startsWith("\\")) continue + // A blank context line may have lost its leading space in transit. + if (row.startsWith("+")) yield { line: right++, text: row.slice(1), kind: "+", hunk } + else if (row.startsWith(" ") || row === "") yield { line: right++, text: row.slice(1), kind: " ", hunk } + else if (row.startsWith("-")) yield { line: right, text: row.slice(1), kind: "-", hunk } + else inHunk = false + } +} + +/** A head-side line inside a diff hunk: an added (`+`) or a context (` `) line. */ +export interface DiffLine { + line: number + text: string + kind: "+" | " " +} + +/** Walk a unified diff's hunks, yielding each added and context line with its head-side line number. */ +export function* walkDiffLines(diff: string | undefined): Generator { + for (const { line, text, kind } of walkDiffRows(diff)) if (kind !== "-") yield { line, text, kind } +} diff --git a/packages/opencode/src/altimate/review/finding.ts b/packages/opencode/src/altimate/review/finding.ts index 44f751f194..a36e2b5bd4 100644 --- a/packages/opencode/src/altimate/review/finding.ts +++ b/packages/opencode/src/altimate/review/finding.ts @@ -89,6 +89,27 @@ export const Finding = z.object({ * verdict unless the rubric opts a named catalog rule in. Stamped by * buildEnvelope from `evidence.tool`. */ origin: z.enum(["engine", "catalog", "ai"]).optional(), + /** Stable rule id, `tool:rule`, normalised by `ruleId`. Keys the per-rule + * posting policy; not part of the fingerprint. */ + rule: z.string().optional(), + /** True when the finding sits on a line the PR did not add. */ + preExisting: z.boolean().optional(), + /** True when the line stands in for code the PR removed (the head-side site + * of a deletion): the finding concerns the PR's own change, so a context + * line does not make it pre-existing. */ + deletionSite: z.boolean().optional(), + /** Where the finding is posted: inline on the diff, or in the summary only. */ + tier: z.enum(["inline", "summary"]).optional(), + /** The source line the finding points at, quoted as evidence. */ + evidenceLine: z.string().max(200).optional(), + /** A replacement for a line range, rendered as a suggestion block. */ + suggestion: z + .object({ + startLine: z.number().int().positive(), + endLine: z.number().int().positive(), + replacement: z.string().max(2000), + }) + .optional(), }) export type Finding = z.infer @@ -145,8 +166,32 @@ export function fingerprint(input: FingerprintInput): string { return "f_" + createHash("sha256").update(canonical).digest("hex").slice(0, 16) } -/** Build a finding, auto-assigning the fingerprint id when not supplied. */ +/** Normalise one half of a rule id: lowercase, `_` to `-`, any other run of + * characters outside `[a-z0-9.-]` to a single `-`, no leading/trailing `-`. */ +export function normRuleToken(s: string): string { + return s + .trim() + .toLowerCase() + .replace(/_/g, "-") + .replace(/[^a-z0-9.-]+/g, "-") + .replace(/^-+|-+$/g, "") +} + +/** The stable `tool:rule` id used to key per-rule posting policy. */ +export function ruleId(tool: string, rule: string): string { + return `${normRuleToken(tool)}:${normRuleToken(rule)}` +} + +/** Build a finding, auto-assigning the fingerprint id when not supplied. The + * `rule` id is derived from the evidence when not supplied; it never feeds the + * fingerprint, so ids stay stable across the change. */ export function makeFinding(input: Omit, "id"> & { id?: string; ruleKey?: string }): Finding { + const result = input.evidence?.result as { rule?: unknown; code?: unknown } | undefined + const rule = + input.rule ?? + (input.evidence?.tool + ? ruleId(input.evidence.tool, String(result?.rule ?? result?.code ?? ruleTitle(input))) + : undefined) const id = input.id ?? fingerprint({ @@ -156,7 +201,7 @@ export function makeFinding(input: Omit, "id"> & { id?: column: input.column, ruleKey: input.ruleKey ?? input.title, }) - return Finding.parse({ ...input, id }) + return Finding.parse({ ...input, id, rule }) } /** Serialize findings as JSONL (one finding per line). */ @@ -240,7 +285,7 @@ export function mergeRepeats(findings: Finding[]): Finding[] { out.push(f) continue } - const key = [f.file, f.category, f.severity, !!f.degraded, ruleTitle(f)].join("|") + const key = [f.file, f.category, f.severity, !!f.degraded, !!f.preExisting, ruleTitle(f)].join("|") const g = groups.get(key) if (g) g.push(f) else groups.set(key, [f]) diff --git a/packages/opencode/src/altimate/review/format.ts b/packages/opencode/src/altimate/review/format.ts index ebe91a0982..ec118df9f4 100644 --- a/packages/opencode/src/altimate/review/format.ts +++ b/packages/opencode/src/altimate/review/format.ts @@ -1,5 +1,5 @@ -import { type Finding, type Severity, ruleTitle } from "./finding" -import { type VerdictEnvelope } from "./verdict" +import { type Finding, type Severity, type Confidence, SEVERITY_ORDER, ruleTitle } from "./finding" +import { type VerdictEnvelope, type PostingPolicy, DEFAULT_POSTING_POLICY } from "./verdict" /** * Render a verdict envelope for humans (PR summary markdown) and for the VCS @@ -45,11 +45,32 @@ export interface RenderOptions { /** Character budget for the comment; findings beyond it are counted, not listed. * `Infinity` renders everything (local CLI output). */ maxChars?: number + /** Finding ids already commented inline on the PR (see postedFindingIds). */ + alreadyPosted?: Set + /** False when the summary is the only output (local CLI, agent tool): every + * finding is then rendered in full instead of as an inline one-liner. */ + inline?: boolean + /** False renders a clean run even under `silentWhenClean` (to replace an + * earlier summary that listed findings). */ + allowSilent?: boolean } -/** Full PR/MR summary comment body (markdown), prefixed with the dedup marker. */ +/** Full PR/MR summary comment body (markdown), prefixed with the dedup marker. + * Returns `""` for a clean run under `silentWhenClean`: nothing to post. */ export function renderSummary(env: VerdictEnvelope, opts: RenderOptions = {}): string { const maxChars = opts.maxChars ?? SUMMARY_BUDGET_CHARS + const notes = coverageNotes(env) + if ( + !env.findings.length && + !notes.length && + !env.summary.degraded && + !env.override && + env.verdict === "APPROVE" && + opts.allowSilent !== false && + postingPolicy(env).silentWhenClean + ) + return "" + const lines: string[] = [REVIEW_MARKER, "", `## ${verdictHeadline(env)}`, ""] if (env.summary.degraded) { @@ -59,8 +80,9 @@ export function renderSummary(env: VerdictEnvelope, opts: RenderOptions = {}): s "", ) } - lines.push(...coverageNotes(env)) + lines.push(...notes) + const tail: string[] = [] // G1 (Round 18) — surface classifier reasons when --explain-tier populated // them, so a customer can see why the review ran at this tier. Also surfaces // when --force-tier bypassed the classifier (tierReasons is auto-populated). @@ -73,26 +95,13 @@ export function renderSummary(env: VerdictEnvelope, opts: RenderOptions = {}): s // Pick an inline-code-span fence longer than any backtick run inside `r` // so a path like `packages/…/foo`bar`.sql` cannot terminate the span // (cubic-review P3). - const shown = env.tierReasons - .slice(0, RENDER_CAP) - .map((r) => { - const runs = r.match(/`+/g) - const maxRun = runs ? Math.max(...runs.map((run) => run.length)) : 0 - const fence = "`".repeat(maxRun + 1) - // If the reason itself starts/ends with a backtick, pad with a space so - // the leading/trailing backtick isn't glued to the fence. - const pad = /^`|`$/.test(r) ? " " : "" - return `${fence}${pad}${r}${pad}${fence}` - }) - .join(", ") + const shown = env.tierReasons.slice(0, RENDER_CAP).map(codeSpan).join(", ") const overflow = env.tierReasons.length > RENDER_CAP ? ` (+${env.tierReasons.length - RENDER_CAP} more in verdict envelope)` : "" - lines.push(`> 🧭 **Tier: ${env.tier}** — ${shown}${overflow}`, "") + tail.push(`> 🧭 **Tier: ${env.tier}** — ${shown}${overflow}`, "") } - - const tail: string[] = [] if (env.override) { tail.push( `> 🔓 **Break-glass override** by @${env.override.by}: ${env.override.reason}`, @@ -107,6 +116,7 @@ export function renderSummary(env: VerdictEnvelope, opts: RenderOptions = {}): s (env.manifestHash ? ` · manifest \`${env.manifestHash.slice(0, 10)}\`` : "") + (env.scope?.baseRef ? ` · base \`${env.scope.baseRef}\`` : "") + (env.ai?.state === "ok" && env.ai.model ? ` · AI \`${env.ai.model}\`` : "") + + (env.ai?.tokens ? ` · tokens ${(env.ai.tokens.inputTotal + env.ai.tokens.output).toLocaleString("en-US")}` : "") + "", ) @@ -115,21 +125,34 @@ export function renderSummary(env: VerdictEnvelope, opts: RenderOptions = {}): s return [...lines, ...tail].join("\n") } + // Findings posted inline get a one-line pointer here; the comment on the + // diff carries the detail. Pre-existing findings sit in a collapsed list. + const deferredIds = new Set(inlineComments(env, opts.alreadyPosted).deferred.map((f) => f.id)) + const posting = postingPolicy(env) + const inline = + opts.inline === false ? [] : env.findings.filter((f) => inlineEligible(f, posting) && !deferredIds.has(f.id)) + const inlineIds = new Set(inline.map((f) => f.id)) + const preExisting = env.findings.filter((f) => f.preExisting && !inlineIds.has(f.id)) + const rest = env.findings.filter((f) => !f.preExisting && !inlineIds.has(f.id)) + if (inline.length) { + lines.push( + `### Inline (${inline.length})`, + "", + ...inline.map((f) => `- \`${f.file}:${f.startLine}\` — ${f.title}`), + "", + ) + } + // Findings fill the budget left after the fixed parts, most severe first. // What does not fit is counted, never silently dropped: the envelope keeps // every finding. const overflowReserve = 220 let used = [...lines, ...tail].join("\n").length + overflowReserve let hidden = 0 - const grouped = groupBySeverity(env.findings) - for (const sev of ["critical", "warning", "suggestion"] as const) { - const items = grouped[sev] - if (!items.length) continue - const heading = [`### ${SEVERITY_EMOJI[sev]} ${capitalize(sev)} (${items.length})`, ""] - const bullets = bulletsFor(items) + const fit = (heading: string[], bullets: Array<{ text: string; count: number }>, close: string[]) => { let headed = false for (const b of bullets) { - const cost = b.text.length + 1 + (headed ? 0 : heading.join("\n").length + 2) + const cost = b.text.length + 1 + (headed ? 0 : heading.join("\n").length + close.join("\n").length + 2) if (used + cost > maxChars) { hidden += b.count continue @@ -141,7 +164,20 @@ export function renderSummary(env: VerdictEnvelope, opts: RenderOptions = {}): s lines.push(b.text) used += cost } - if (headed) lines.push("") + if (headed) lines.push(...close, "") + } + const grouped = groupBySeverity(rest) + for (const sev of ["critical", "warning", "suggestion"] as const) { + const items = grouped[sev] + if (!items.length) continue + fit([`### ${SEVERITY_EMOJI[sev]} ${capitalize(sev)} (${items.length})`, ""], bulletsFor(items), []) + } + if (preExisting.length) { + fit( + [`
Pre-existing (${preExisting.length})`, ""], + bulletsFor(preExisting), + ["", "
"], + ) } if (hidden) { lines.push( @@ -174,7 +210,7 @@ function bulletsFor(items: Finding[]): Array<{ text: string; count: number }> { out.push({ text: `- **${ruleTitle(g[0])}** — ${g.length} findings across ${names.length} model(s): ${shown}${more} \n` + - ` ${oneLine(g[0].body)} \n ${g[0].category}`, + ` ${oneLine(clampBody(g[0].body))} \n ${g[0].category}`, count: g.length, }) continue @@ -182,7 +218,10 @@ function bulletsFor(items: Finding[]): Array<{ text: string; count: number }> { for (const f of g) { const loc = f.file + (f.startLine ? `:${f.startLine}` : "") out.push({ - text: `- **${f.title}** \n ${oneLine(f.body)} \n \`${loc}\`${f.degraded ? " · _unverified_" : ""} · ${f.category}`, + text: + `- **${f.title}** \n` + + (f.evidenceLine ? ` ${evidenceText(f)} \n` : "") + + ` ${oneLine(clampBody(f.body))} \n \`${loc}\`${f.degraded ? " · _unverified_" : ""} · ${f.category}`, count: 1, }) } @@ -219,12 +258,16 @@ export function coverageNotes(env: VerdictEnvelope): string[] { const list = env.engineErrors.map((e) => `${e.method} ×${e.count} (${e.reason})`).join("; ") notes.push(`Some engine checks failed and did not run: ${list}. This verdict is not a clean approval.`) } - if (env.scope?.outOfScopeFiles) + // The ledger lists out-of-scope and moved files by path; the count lines are + // for envelopes that carry only the counts. + const ledger = notReviewedLedger(env) + const listed = new Set(ledger.map((e) => e.path)) + const scopePaths = env.scope?.notReviewed ?? [] + if (env.scope?.outOfScopeFiles && !scopePaths.some((e) => /^outside the dbt project/.test(e.reason) && listed.has(e.path))) notes.push(`${env.scope.outOfScopeFiles} changed file(s) outside the dbt project were not reviewed.`) - if (env.scope?.movedUnchangedFiles) + if (env.scope?.movedUnchangedFiles && !scopePaths.some((e) => /^moved without edits/.test(e.reason) && listed.has(e.path))) notes.push( - `${env.scope.movedUnchangedFiles} file(s) moved without edits; their SQL was not re-reviewed. ` + - "Folder-level config in `dbt_project.yml` (`+schema`, `+materialized`, `+tags`) can still change where and how a moved model builds.", + `${env.scope.movedUnchangedFiles} file(s) moved without edits; their SQL was not re-reviewed. ${MOVED_HINT}`, ) if (env.policy?.changedInPr?.length) { notes.push( @@ -235,18 +278,98 @@ export function coverageNotes(env: VerdictEnvelope): string[] { if (env.ai && env.ai.state !== "ok") { const what = { skipped: "skipped", timeout: "timed out", error: "failed" }[env.ai.state] notes.push(`Advisory AI review ${what}${env.ai.reason ? `: ${env.ai.reason}` : ""}.`) + } else if (env.ai?.reason) { + // Some calls answered and some failed: the files in the failed calls were not reviewed by the AI. + notes.push(`Advisory AI review partly failed: ${env.ai.reason}.`) + } + const out = notes.length ? [...notes.map((n) => `> ℹ️ ${n}`), ""] : [] + return [...out, ...ledgerBlock(ledger)] +} + +/** Paths shown in the "Not reviewed" block at most; the rest are counted. */ +export const LEDGER_MAX_PATHS = 30 + +/** The coverage ledger minus the `unverified` entries, which have their own + * note above it. */ +function notReviewedLedger(env: VerdictEnvelope): Array<{ path: string; reason: string }> { + const unverified = new Set((env.scope?.unverified ?? []).map((u) => `${u.path}\0${u.reason}`)) + return (env.scope?.notReviewed ?? []).filter((e) => !unverified.has(`${e.path}\0${e.reason}`)) +} + +const MOVED_HINT = + "Folder-level config in `dbt_project.yml` (`+schema`, `+materialized`, `+tags`) can still change where and how a moved model builds." + +/** Collapsed "Not reviewed: n file(s)" block, grouped by reason. */ +function ledgerBlock(ledger: Array<{ path: string; reason: string }>): string[] { + if (!ledger.length) return [] + const byReason = new Map() + for (const e of ledger) byReason.set(e.reason, [...(byReason.get(e.reason) ?? []), e.path]) + const lines = [`
Not reviewed: ${ledger.length} file(s)`, ""] + let shown = 0 + for (const [reason, paths] of byReason) { + if (shown >= LEDGER_MAX_PATHS) break + const take = paths.slice(0, LEDGER_MAX_PATHS - shown) + shown += take.length + // Escape Markdown emphasis so a glob like `**/*.md` in a reason renders literally. + lines.push(`${reason.replace(/([*_])/g, "\\$1")} (${paths.length}):`, ...take.map((p) => `- ${codeSpan(p)}`)) + if (/^moved without edits/.test(reason)) lines.push("", MOVED_HINT) + lines.push("") } - return notes.length ? [...notes.map((n) => `> ℹ️ ${n}`), ""] : [] + if (ledger.length > shown) lines.push(`… and ${ledger.length - shown} more`, "") + lines.push("
", "") + return lines } -/** GitHub `pulls.createReview` inline comments — only findings with a line. */ +/** GitHub `pulls.createReview` inline comments — only findings with a line. + * A multi-line range (a suggestion block) sets `start_line`/`start_side`. */ export interface InlineComment { path: string line: number side: "RIGHT" + start_line?: number + start_side?: "RIGHT" body: string } +/** Rendered length cap for a finding body; the envelope keeps the full text. */ +export const BODY_MAX_CHARS = 600 + +/** Cut `body` to at most `max` characters at a sentence boundary (a word + * boundary when no sentence ends late enough) and append "…". A code fence + * left open by the cut is closed. */ +export function clampBody(body: string, max = BODY_MAX_CHARS): string { + if (body.length <= max) return body + const head = body.slice(0, max - 1) + const sentence = Math.max(...[". ", "! ", "? ", ".\n", "\n\n"].map((t) => head.lastIndexOf(t))) + const word = head.search(/\s\S*$/) + const cut = sentence >= max / 2 ? sentence + 1 : word > 0 ? word : head.length + let out = head.slice(0, cut).trimEnd() + "…" + if ((out.match(/^```/gm) ?? []).length % 2) out += "\n```" + return out +} + +/** Inline code span for `s`, fenced longer than any backtick run inside it. */ +function codeSpan(s: string): string { + const runs = s.match(/`+/g) + const fence = "`".repeat((runs ? Math.max(...runs.map((r) => r.length)) : 0) + 1) + // A leading/trailing backtick needs a space so it isn't glued to the fence. + const pad = /^`|`$/.test(s) ? " " : "" + return `${fence}${pad}${s}${pad}${fence}` +} + +/** `` `file:line` — `` ``: the line a finding quotes as evidence. */ +function evidenceText(f: Finding): string { + const loc = f.file + (f.startLine ? `:${f.startLine}` : "") + return `\`${loc}\` — ${codeSpan(oneLine(f.evidenceLine ?? ""))}` +} + +/** A GitHub suggestion block replacing the comment's line range. */ +function suggestionBlock(replacement: string): string { + const runs = replacement.match(/`{3,}/g) + const fence = "`".repeat(Math.max(3, ...(runs ?? []).map((r) => r.length + 1))) + return `${fence}suggestion\n${replacement}\n${fence}` +} + /** Hidden per-finding marker on inline comments, keyed by the finding's stable * fingerprint (rule, file, model, column — not the line), so a rerun can tell * which findings are already on the PR. */ @@ -261,15 +384,62 @@ export function postedFindingIds(bodies: Array): Set< return out } -export function inlineComments(env: VerdictEnvelope, alreadyPosted: Set = new Set()): InlineComment[] { - return env.findings - .filter((f) => typeof f.startLine === "number" && !alreadyPosted.has(f.id)) - .map((f) => ({ +/** The posting policy an envelope was produced under (defaults for envelopes + * from releases that did not record one). */ +export function postingPolicy(env: VerdictEnvelope): PostingPolicy { + return env.policy?.posting ?? DEFAULT_POSTING_POLICY +} + +const CONFIDENCE_ORDER: Record = { high: 3, medium: 2, low: 1, unknown: 0 } + +/** Whether a finding may be posted as an inline comment: an inline-tier rule + * (AI findings default to inline in `ruleTier`), on a line the PR added, at or + * above the severity floor. */ +export function inlineEligible(f: Finding, posting: PostingPolicy): boolean { + if (f.tier !== "inline") return false + if (f.preExisting || typeof f.startLine !== "number") return false + return SEVERITY_ORDER[f.severity] >= SEVERITY_ORDER[posting.inlineSeverityFloor] +} + +/** + * Inline comments for this run: eligible findings not already on the PR, most + * severe and most confident first, capped at the posting budget. Findings over + * the budget are `deferred` and stay in the summary. + */ +export function inlineComments( + env: VerdictEnvelope, + alreadyPosted: Set = new Set(), +): { comments: InlineComment[]; deferred: Finding[] } { + const posting = postingPolicy(env) + const fresh = env.findings + .filter((f) => inlineEligible(f, posting) && !alreadyPosted.has(f.id)) + .sort( + (a, b) => + SEVERITY_ORDER[b.severity] - SEVERITY_ORDER[a.severity] || + CONFIDENCE_ORDER[b.confidence] - CONFIDENCE_ORDER[a.confidence] || + a.file.localeCompare(b.file) || + a.startLine! - b.startLine!, + ) + const comments = fresh.slice(0, posting.inlineBudget).map((f): InlineComment => { + const parts = [`${SEVERITY_EMOJI[f.severity]} **${f.title}**`] + if (f.evidenceLine) parts.push(evidenceText(f)) + parts.push(clampBody(f.body)) + const s = f.suggestion + if (s) parts.push(suggestionBlock(s.replacement)) + parts.push(findingMarker(f.id)) + const body = parts.join("\n\n") + // GitHub applies a suggestion to the comment's line range, so the + // comment spans exactly the replaced lines. + if (!s) return { path: f.file, line: f.startLine!, side: "RIGHT", body } + return { path: f.file, - line: f.startLine!, - side: "RIGHT" as const, - body: `${SEVERITY_EMOJI[f.severity]} **${f.title}**\n\n${f.body}\n\n${findingMarker(f.id)}`, - })) + line: s.endLine, + side: "RIGHT", + ...(s.startLine !== s.endLine ? { start_line: s.startLine, start_side: "RIGHT" as const } : {}), + body, + } + }) + return { comments, deferred: fresh.slice(posting.inlineBudget) } } function groupBySeverity(findings: Finding[]): Record { diff --git a/packages/opencode/src/altimate/review/incremental-semantics.ts b/packages/opencode/src/altimate/review/incremental-semantics.ts index 0df5556423..1ca4c3ae5c 100644 --- a/packages/opencode/src/altimate/review/incremental-semantics.ts +++ b/packages/opencode/src/altimate/review/incremental-semantics.ts @@ -264,7 +264,16 @@ export interface IncrementalInput { columnDelta: () => Promise<{ added: string[]; removed: string[] }> } -function finding(input: { file: ChangedFile; model: string }, rule: string, severity: Finding["severity"], category: Finding["category"], title: string, body: string): Finding { +function finding( + input: { file: ChangedFile; model: string }, + rule: string, + severity: Finding["severity"], + category: Finding["category"], + title: string, + body: string, + /** The raw added line the rule matched, when it matches one. */ + line?: string, +): Finding { return makeFinding({ severity, category, @@ -273,7 +282,7 @@ function finding(input: { file: ChangedFile; model: string }, rule: string, seve file: input.file.path, model: input.model, confidence: "high", - evidence: { tool: "dbt-semantics", result: { rule } }, + evidence: { tool: "dbt-semantics", result: line === undefined ? { rule } : { rule, line } }, ruleKey: `${category}:${rule}`, }) } @@ -286,6 +295,7 @@ export async function incrementalFindings(input: IncrementalInput): Promise input.added.find((l) => re.test(l)) const out: Finding[] = [] const cap = input.dialectKnown ? capabilityFor(input.dialect) : undefined @@ -313,7 +323,10 @@ export async function incrementalFindings(input: IncrementalInput): Promise /\w{2}/.test(l) && (inlineNew.partition_by ?? "").includes(l.trim()))), ) if (cfg.onSchemaChange === "fail" && !isNew) { @@ -339,7 +352,8 @@ export async function incrementalFindings(input: IncrementalInput): Promise> /** Direct downstream models (name + file path) of a model, from the manifest DAG. */ downstreamModels?(model: string): Promise> + /** + * Direct dbt parents of the nodes defined in these files (project-relative + * paths): models, seeds and snapshots by file, sources with their declared + * columns, and the project's own macros. A changed yml maps to the models its + * tests cover. Paths of parents are project-relative too. + */ + graphParents?(paths: string[]): Promise> /** * Consumers of a model that dbt refs don't show: exposures that depend on it, * and whether it has an enforced contract (with the declared columns). @@ -232,6 +252,8 @@ export interface OrchestrateInput { aiDisabledReason?: string /** Explicit `provider/model` for the AI lane. */ aiModel?: string + /** Send the changed files' unchanged dbt parents to the AI lane as context. */ + aiGraphContext?: boolean /** PR metadata passed to the AI reviewer for intent checking. */ prTitle?: string prBody?: string @@ -246,7 +268,7 @@ export interface OrchestrateInput { /** Names `ref()` can resolve to at head (repo resources + installed packages); undefined = unknown. */ resourceNames?: () => Promise | undefined> /** What the review covered: base resolution and files left out of scope. */ - scope?: Omit + scope?: Omit & { notReviewed?: NotReviewed[] } /** Drop the regex catalog's cross-engine portability findings (single known engine). */ suppressPortability?: boolean /** Other adapters the project supports (from its adapter-specific macros); portability advice is kept when any of them lacks the construct. */ @@ -275,42 +297,168 @@ export function isSuppressiblePortability(f: Finding, dialect: string, targets: return !worksOn || [dialect.toLowerCase(), ...targets].every((d) => worksOn.includes(d)) } +/** Head-side line numbers of a unified diff: `commentable` holds the lines + * GitHub accepts for an inline comment (added and context lines inside a + * hunk); `added` holds only the `+` lines the PR introduced. */ +export function scanDiffLines(diff: string | undefined): { commentable: Set; added: Set } { + const commentable = new Set() + const added = new Set() + for (const { line, kind } of walkDiffLines(diff)) { + commentable.add(line) + if (kind === "+") added.add(line) + } + return { commentable, added } +} + /** Line numbers on the head side of a unified diff that GitHub accepts for an * inline comment: added lines and context lines inside a hunk. */ export function commentableLines(diff: string | undefined): Set { - const out = new Set() - if (!diff) return out - let right = 0 - let inHunk = false - const rows = diff.split("\n") - if (rows[rows.length - 1] === "") rows.pop() // the diff's trailing newline, not a line - for (const line of rows) { - const h = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(line) - if (h) { - right = Number(h[1]) - inHunk = true - continue - } - // File headers (`--- a/…`, `+++ b/…`) only occur outside a hunk. - if (!inHunk || line.startsWith("\\")) continue - // A blank context line may have lost its leading space in transit. - if (line.startsWith("+") || line.startsWith(" ") || line === "") out.add(right++) - else if (!line.startsWith("-")) inHunk = false + return scanDiffLines(diff).commentable +} + +/** Head-side line numbers each changed file's diff adds. A newly added file + * counts every line of its diff as added. */ +export function addedLinesByFile(files: ChangedFile[]): Map> { + return new Map( + files.map((f) => { + const { commentable, added } = scanDiffLines(f.diff) + return [f.path, f.status === "added" ? commentable : added] + }), + ) +} + +/** + * Keep a finding's `suggestion` only when GitHub can apply it as written: every + * line of the range was added by the PR, the head file has those lines, and + * the replacement changes them. Otherwise the replacement moves into the body + * as a plain "Suggested fix" block. + */ +export async function validateSuggestions( + findings: Finding[], + getContent: OrchestrateInput["getContent"], + added: Map>, +): Promise { + const heads = new Map>() + const head = (file: string) => { + if (!heads.has(file)) heads.set(file, getContent?.(file, "new").catch(() => undefined) ?? Promise.resolve(undefined)) + return heads.get(file)! } - return out + return Promise.all( + findings.map(async (f) => { + const s = f.suggestion + if (!s) return f + const lines = added.get(f.file) + const range = Array.from({ length: s.endLine - s.startLine + 1 }, (_, i) => s.startLine + i) + const inDiff = s.startLine <= s.endLine && !!lines && range.every((n) => lines.has(n)) + const text = inDiff ? await head(f.file) : undefined + const rows = text?.split("\n") + const current = rows && rows.length >= s.endLine ? rows.slice(s.startLine - 1, s.endLine).join("\n") : undefined + if (current !== undefined && current !== s.replacement) return f + const { suggestion: _drop, ...rest } = f + return { ...rest, body: `${f.body}\n\nSuggested fix:\n\`\`\`sql\n${s.replacement}\n\`\`\`` } as Finding + }), + ) +} + +/** Shortest evidence text matched as a substring of an added line. */ +const EVIDENCE_SUBSTRING_MIN = 12 + +/** + * Give a line-less finding the diff line its rule matched. The rule records + * the matched source text in `evidence.result.line`; this finds it in the + * file's diff: the first added line with the same trimmed text, else the first + * context line with it (which `anchorToDiff` then marks pre-existing), else the + * first added line containing it when it is long enough to be specific. A + * finding whose text isn't in the diff keeps no line and stays in the summary. + */ +export function anchorByEvidence(findings: Finding[], files: ChangedFile[]): Finding[] { + const diffs = new Map(files.map((f) => [f.path, f.diff])) + const walked = new Map() + return findings.map((f) => { + if (typeof f.startLine === "number") return f + const raw = (f.evidence?.result as { line?: unknown } | undefined)?.line + const want = typeof raw === "string" ? raw.trim() : "" + if (!want || !diffs.has(f.file)) return f + if (!walked.has(f.file)) walked.set(f.file, [...walkDiffLines(diffs.get(f.file))]) + const rows = walked.get(f.file)! + const hit = + rows.find((r) => r.kind === "+" && r.text.trim() === want) ?? + rows.find((r) => r.kind === " " && r.text.trim() === want) ?? + (want.length >= EVIDENCE_SUBSTRING_MIN ? rows.find((r) => r.kind === "+" && r.text.includes(want)) : undefined) + return hit ? { ...f, startLine: hit.line, endLine: hit.line } : f + }) +} + +const escapeRe = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + +/** The head line nearest a removed row: the `+` row at the same position in + * its change block, else the nearest `+` row in its hunk, else (a pure + * deletion) the context row after it, else the one before it. */ +function headSiteOfRemoved(rows: DiffRow[], i: number): number | undefined { + const hunk = rows[i].hunk + let lo = i + let hi = i + while (lo > 0 && rows[lo - 1].hunk === hunk && rows[lo - 1].kind !== " ") lo-- + while (hi < rows.length - 1 && rows[hi + 1].hunk === hunk && rows[hi + 1].kind !== " ") hi++ + const block = rows.slice(lo, hi + 1) + const plus = block.filter((r) => r.kind === "+") + if (plus.length) { + const k = block.filter((r) => r.kind === "-").indexOf(rows[i]) + return plus[Math.min(k, plus.length - 1)].line + } + const inHunk = rows.map((r, j) => ({ r, j })).filter(({ r }) => r.hunk === hunk) + const nearest = (kind: DiffRow["kind"]) => + inHunk.filter(({ r }) => r.kind === kind).sort((a, b) => Math.abs(a.j - i) - Math.abs(b.j - i))[0]?.r + const added = nearest("+") + if (added) return added.line + return ( + inHunk.find(({ r, j }) => j > i && r.kind === " ")?.r.line ?? + inHunk.filter(({ r, j }) => j < i && r.kind === " ").at(-1)?.r.line + ) +} + +/** + * Anchor for a finding about removed output columns: the head-side site of + * the base line that defined the first removed column the diff shows. The + * defining line is the last removed line whose select item ends in the column + * (`x.col`, `expr as col`), else the first removed line naming it. The site is + * a `deletionSite`: the finding is about the PR's removal, not pre-existing + * code. No removed line names a column → no anchor (the finding stays in the + * summary). + */ +export function removedColumnAnchor( + diff: string | undefined, + columns: string[], +): Pick { + const rows = [...walkDiffRows(diff)] + const removed = rows.map((r, i) => ({ r, i, code: r.text.replace(/--.*$/, "").trim() })).filter((x) => x.r.kind === "-") + for (const col of columns) { + const id = `["\`\\[]?${escapeRe(col)}["\`\\]]?` + const defines = new RegExp(`^,?\\s*(?:.*\\s+as\\s+|(?:[\\w"\`\\[\\]]+\\.)*)${id}\\s*,?$`, "i") + const names = new RegExp(`(^|[^\\w])${escapeRe(col)}([^\\w]|$)`, "i") + const def = removed.filter((x) => defines.test(x.code)).at(-1) ?? removed.find((x) => names.test(x.code)) + const line = def && headSiteOfRemoved(rows, def.i) + if (line !== undefined) return { startLine: line, endLine: line, deletionSite: true } + } + return {} } /** * Keep a finding's line only when it can be commented on: inside the file's * diff hunks on the head side. Anything else (a line from compiled SQL, an * unchanged region) moves to the summary instead of making GitHub reject the - * whole batch of inline comments. + * whole batch of inline comments. A finding anchored on a context line (one + * the PR did not add) keeps its line and is marked `preExisting`, unless the + * line is a `deletionSite` standing in for code the PR removed. */ export function anchorToDiff(findings: Finding[], files: ChangedFile[]): Finding[] { - const lines = new Map(files.map((f) => [f.path, commentableLines(f.diff)])) + const lines = new Map(files.map((f) => [f.path, { ...scanDiffLines(f.diff), isNew: f.status === "added" }])) return findings.map((f) => { if (typeof f.startLine !== "number") return f - if (lines.get(f.file)?.has(f.startLine)) return f + const file = lines.get(f.file) + if (file?.commentable.has(f.startLine)) { + return file.isNew || file.added.has(f.startLine) || f.deletionSite ? f : { ...f, preExisting: true } + } const { startLine: _s, endLine: _e, ...rest } = f return rest as Finding }) @@ -381,6 +529,7 @@ function lineageBreakageLane(file: ChangedFile & { kind: string }, impact: Impac degraded, evidence: { tool: "impact_analysis", result: impact }, ruleKey: `lineage_breakage:${file.status}`, + rule: "impact-analysis:downstream-consumers", }), ] } @@ -399,6 +548,7 @@ async function semanticChangeLane( dialect: string, impact: ImpactResult, rubric: Rubric, + unverified: Array<{ path: string; reason: string }>, ): Promise { if (!hasOldSide(file)) return [] if (!oldSql || !newSql || oldSql.trim() === newSql.trim()) return [] @@ -411,24 +561,14 @@ async function semanticChangeLane( if (eq.decided && eq.equivalent) return [] if (!eq.decided) { - // Undecidable / no schema → a suggestion to data-diff: the engine not knowing is - // not evidence of a change (1 of 426 such findings matched a real defect on replay). - return [ - makeFinding({ - severity: "suggestion", - category: "semantic_change", - title: `${model}: refactor could not be proven equivalent`, - body: - `The logic of \`${model}\` changed and equivalence could not be decided` + - ` (no schema, or unsupported SQL). Treat as a potential behavior change and verify with a data-diff.`, - file: file.path, - model, - confidence: "unknown", - degraded: true, - evidence: { tool: "altimate_core.equivalence", result: { decided: false } }, - ruleKey: "semantic_change:undecidable", - }), - ] + // Undecidable / no schema: the engine not knowing is not evidence of a change + // (1 of 426 such findings matched a real defect on replay), so it goes to the + // coverage ledger instead of the findings. + unverified.push({ + path: file.path, + reason: "equivalence could not be proven (no schema or unsupported SQL); verify with a data-diff", + }) + return [] } // Decided NOT equivalent. THE FUSION: a proven behavior change to a model with @@ -456,10 +596,43 @@ async function semanticChangeLane( confidence: eq.confidence ?? "medium", evidence: { tool: "altimate_core.equivalence", result: { equivalent: false, differences: diffs } }, ruleKey: "semantic_change:not-equivalent", + rule: "altimate-core.equivalence:not-equivalent", }), ] } +/** + * A check issue as evidence. On compiled SQL its line number doesn't exist in + * the PR's file, so the compiled line's text goes in `line` (the number moves + * to `compiledLine`) for `anchorByEvidence` to find among the added lines; + * only text that is specific and unique in the compiled SQL. + */ +function checkEvidence(issue: { line?: number }, sql: string, linesMatchFile: boolean): object { + if (linesMatchFile || typeof issue.line !== "number") return issue + const rows = sql.split("\n") + const text = rows[issue.line - 1]?.trim() ?? "" + if (text.length < EVIDENCE_SUBSTRING_MIN || rows.filter((r) => r.trim() === text).length !== 1) return issue + return { ...issue, line: text, compiledLine: issue.line } +} + +/** + * Fallback line for a non-deterministic-dedup issue on compiled SQL whose + * compiled text is not in the raw diff (the window came from a macro, or a + * Jinja-built line): the added line that opens a `row_number()` over the + * named time column, else the added line ordering by it (`order by col`, + * a macro's `order_by='col'`). No added line names the column → no line. + */ +function anchorDedupIssue(f: Finding, file: ChangedFile): Finding { + if (typeof anchorByEvidence([f], [file])[0].startLine === "number") return f + const cols = (/\[([^\]]+)\]/.exec(f.body)?.[1] ?? "").split(",").map((c) => c.trim()).filter(Boolean) + const names = (t: string) => cols.some((c) => new RegExp(`(^|[^\\w])${escapeRe(c)}([^\\w]|$)`, "i").test(t)) + const added = [...walkDiffLines(file.diff)].filter((r) => r.kind === "+") + const hit = + added.find((r) => /\brow_number\s*\(/i.test(r.text) && names(r.text)) ?? + added.find((r) => /\border(\s+by|_by)\b/i.test(r.text) && names(r.text)) + return hit ? { ...f, startLine: hit.line, endLine: hit.line } : f +} + async function qualityLane( file: ChangedFile & { kind: string }, runner: ReviewRunner, @@ -510,23 +683,22 @@ async function qualityLane( let sev: Severity = isError ? "warning" : "suggestion" if (isError && (cat === "contract_violation" || cat === "pii_exposure")) sev = "critical" sev = syntax ? "suggestion" : clampSeverity(cat, sev, "high") - findings.push( - makeFinding({ - severity: sev, - category: cat, - title: `${model}: ${issue.rule}`, - body: issue.message, - file: file.path, - model, - startLine: linesMatchFile ? issue.line : undefined, - endLine: linesMatchFile ? issue.line : undefined, - ...(syntax ? { confidence: "unknown" as const } : {}), - evidence: { tool: "altimate_core.check", result: issue }, - // Include the message so distinct instances of the same rule (e.g. two - // different non-portable functions) don't collapse under one fingerprint. - ruleKey: `quality:${issue.rule}:${(issue.message ?? "").slice(0, 60)}`, - }), - ) + const f = makeFinding({ + severity: sev, + category: cat, + title: `${model}: ${issue.rule}`, + body: issue.message, + file: file.path, + model, + startLine: linesMatchFile ? issue.line : undefined, + endLine: linesMatchFile ? issue.line : undefined, + ...(syntax ? { confidence: "unknown" as const } : {}), + evidence: { tool: "altimate_core.check", result: checkEvidence(issue, sql, linesMatchFile) }, + // Include the message so distinct instances of the same rule (e.g. two + // different non-portable functions) don't collapse under one fingerprint. + ruleKey: `quality:${issue.rule}:${(issue.message ?? "").slice(0, 60)}`, + }) + findings.push(!linesMatchFile && rule === "non_deterministic_dedup" ? anchorDedupIssue(f, file) : f) } return findings } @@ -624,6 +796,7 @@ async function columnBreakageLane( confidence: "high", evidence: { tool: "altimate_core.column_lineage", result: { removed, brokenBy: Object.fromEntries([...brokenBy].map(([k, v]) => [k, [...v]])) } }, ruleKey: `lineage_breakage:column-removed`, + rule: "altimate-core.column-lineage:removed-columns", }), ] } @@ -642,8 +815,9 @@ async function outputColumns(runner: ReviewRunner, sql: string, dialect: string) // set is unknown. Filling it from the select list alone would make every // unaliased expression look removed when only one side's lineage failed. if (!cols.length) return new Set() - // `*` in the final SELECT makes `named` null: keep lineage's own answer. - const named = selectListNames(sql) + // `*` in the final SELECT: keep lineage's own answer (it expands stars it can resolve), + // plus the CTE's own list when the star reads a CTE of this query. + const named = finalSelectNames(sql) return new Set([...cols, ...(named ?? [])]) } @@ -690,7 +864,7 @@ function rawSelectList(sql: string): Set { // it becomes its own `__jinja__` item, which marks the list unknown. .replace(/\{%[\s\S]*?%\}/g, " , __jinja__ , ") .replace(/\{\{[\s\S]*?\}\}/g, " __jinja__ ") - const names = selectListNames(plain, { complete: true }) + const names = finalSelectNames(plain, { complete: true }) // A Jinja expression inside an item (e.g. `{{ col }} as x`) leaves its alias // intact; only a Jinja item standing alone is unknown. if (!names || names.includes("__jinja__")) return new Set() @@ -767,7 +941,7 @@ async function consumerContractLane( if (removed.length && !declared) { out.push( makeFinding({ - severity: "suggestion", + severity: "warning", category: "lineage_breakage", title: `${model}: removed column(s) on a final-layer model`, body: @@ -778,6 +952,8 @@ async function consumerContractLane( confidence: "medium", evidence: { tool: "dbt.manifest", result: { removed, finalLayer: true } }, ruleKey: "lineage_breakage:final-layer-column-removed", + rule: "dbt.manifest:removed-final-layer-columns", + ...removedColumnAnchor(ctx.file.diff, removed), }), ) } @@ -803,6 +979,8 @@ async function consumerContractLane( result: { removed, exposures: consumers.exposures.map((e) => e.name), contractEnforced: consumers.contractEnforced }, }, ruleKey: "lineage_breakage:consumer-column-removed", + rule: "dbt.manifest:removed-columns-declared-consumers", + ...removedColumnAnchor(ctx.file.diff, removed), }), ) } @@ -827,6 +1005,7 @@ async function consumerContractLane( confidence: "high", evidence: { tool: "contract-check", result: { missing } }, ruleKey: "contract_violation:contract-missing-column", + rule: "contract-check:missing-columns", }), ) } @@ -872,6 +1051,7 @@ async function consumerListLane( confidence: "high", evidence: { tool: "consumer-column-list", result: { list: list.path, missing, stale } }, ruleKey: `lineage_breakage:consumer-list:${list.path}`, + rule: "consumer-column-list:stale", }), ) } @@ -950,6 +1130,7 @@ async function dataDiffLane( confidence: "high", evidence: { tool: "data.diff", result: diff }, ruleKey: "semantic_change:data-diff", + rule: "data.diff:row-delta", }), ] } @@ -1058,6 +1239,7 @@ async function topologyEquivalenceLane( }, }, ruleKey: `semantic_change:topology-merge:${wName}`, + rule: "altimate-core.equivalence:not-equivalent", }), ) } @@ -1103,6 +1285,7 @@ async function grainMismatchLane(ctx: ModelContext, runner: ReviewRunner): Promi confidence: "medium", evidence: { tool: "altimate_core.grain", result: { grain: actual, declared: pk, extra } }, ruleKey: "sql_correctness:grain-mismatch", + rule: "altimate-core.grain:conflicts-declared-uniqueness", }), ] } @@ -1140,6 +1323,7 @@ async function missingGrainTestLane(ctx: ModelContext, runner: ReviewRunner): Pr confidence: "medium", evidence: { tool: "dbt.manifest", result: { hasGrainTest: false } }, ruleKey: "test_coverage:missing-grain-test", + rule: "dbt.manifest:new-model-no-grain-test", }), ] } @@ -1239,6 +1423,37 @@ function changesMergeGrain(message: string, newSql: string, oldSql: string | und return !added.every((c) => new RegExp(`['"]${c.replace(/\W/g, "")}['"]`).test(partition)) } +/** + * The changed files' unchanged dbt parents, read at head, for the AI lane's + * cross-file checks: an upstream model's columns, a seed's rows, a macro's + * signature. Manifest paths are project-relative; changed paths may carry a + * prefix (review run from the repo root), which is reapplied to the parents. + */ +async function aiGraphContext( + paths: string[], + runner: ReviewRunner, + getContent: OrchestrateInput["getContent"], +): Promise { + if (!runner.graphParents || !getContent || !paths.length) return undefined + try { + const parents = await runner.graphParents(paths) + const out: AiContextFile[] = [] + for (const p of parents) { + if (p.kind === "source") { + if (!p.columns?.length) continue + out.push({ path: p.path, kind: p.kind, name: p.name, forPaths: p.forPaths, content: `columns: ${p.columns.join(", ")}` }) + continue + } + const content = await getContent(p.path, "new").catch(() => undefined) + if (content) out.push({ path: p.path, kind: p.kind, name: p.name, forPaths: p.forPaths, content }) + } + return out + } catch { + // Context is an enrichment: without it the lane reviews the changed files alone. + return undefined + } +} + /** * dbt CONFIG lint — runs the core dbt-config parser (minijinja) over the RAW model * (the `{{ config() }}` block + body) and emits the config/Jinja findings: incremental @@ -1320,6 +1535,7 @@ async function joinKeyUniquenessLane(ctx: ModelContext, runner: ReviewRunner): P confidence: "medium", evidence: { tool: "dbt.manifest", result: { joined: t.model, keys: t.keys, declared } }, ruleKey: `fanout:join-key-untested:${t.model}`, + rule: "dbt.manifest:left-join-key-not-unique", }), ) } @@ -1376,6 +1592,7 @@ async function uniqueKeyMismatchLane(ctx: ModelContext, runner: ReviewRunner, un confidence: "high", evidence: { tool: "dbt.manifest", result: { unique_key: uk, declared } }, ruleKey: "materialization:unique-key-test-mismatch", + rule: "dbt.manifest:unique-key-differs-from-test", }), ] } @@ -1506,6 +1723,7 @@ async function siblingConsistencyLane(ctxs: ModelContext[], runner: ReviewRunner confidence: "medium", evidence: { tool: "altimate_core.source_filters", result: { upstream: u, missing } }, ruleKey: `sql_correctness:sibling-filter:${u}`, + rule: "altimate-core.source-filters:sibling-mismatch", }), ) } @@ -1592,6 +1810,7 @@ async function piiClassifyLane( confidence: highConfidence ? "high" : "medium", evidence: { tool: "altimate_core.classify_pii", result: p }, ruleKey: `pii_exposure:${col}`, + rule: "altimate-core.classify-pii:exposes", }), ) } @@ -1620,6 +1839,7 @@ function piiLane(file: ChangedFile & { kind: string }, columns: string[], baseUn confidence: baseUnknown ? "medium" : "high", evidence: { tool: "schema.detect_pii", result: { columns } }, ruleKey: "pii:exposure", + rule: "schema.detect-pii:exposes", }), ] } @@ -1642,7 +1862,16 @@ interface ModelContext { } export async function runReview(input: OrchestrateInput): Promise { - const reviewable = filterChangedFiles(input.changedFiles, input.rubric.exclusions.excludeGlobs, input.layout) + const { reviewable, dropped } = partitionChangedFiles( + input.changedFiles, + input.rubric.exclusions.excludeGlobs, + input.layout, + ) + // Files some lane received (the coverage ledger lists the rest). + const touched = new Set() + const touch = (files: Array<{ path: string }>) => { + for (const f of files) touched.add(f.path) + } const dialect = input.config.dialect const getContent = input.getContent const getCompiled = input.getCompiled @@ -1651,11 +1880,15 @@ export async function runReview(input: OrchestrateInput): Promise f.kind === "model_sql" || f.kind === "python_model") + // Every model gets at least the PII check; one that fails is recorded as unverified. + touch(modelFiles) const ctxByPath = new Map() const unverified: NonNullable = [] let anyManifest = false + let manifestMissing = false if (input.runner.manifestAvailable) { anyManifest = await input.runner.manifestAvailable().catch(() => false) + manifestMissing = !anyManifest } // PR-level lanes (across files) that threw: reported with the engine errors. const laneErrors: Array<{ method: string; reason: string; count: number }> = [] @@ -1815,6 +2048,7 @@ export async function runReview(input: OrchestrateInput): Promise undefined) .then((mc) => [ ...jinjaFindings(ctx.file, ctx.oldSql, ctx.newSql), - ...modelConfigFindings(ctx.file, ctx.newSql && inlineConfigSplats(ctx.newSql), dialect, mc), + ...modelConfigFindings(ctx.file, ctx.newSql && inlineConfigSplats(ctx.newSql), dialect, mc, !!input.dialectKnown), ]), ) } @@ -1943,6 +2177,7 @@ export async function runReview(input: OrchestrateInput): Promise f.kind === "snapshot" && f.status !== "deleted" && f.status !== "added") + touch(snapshots) const sets = await Promise.all( snapshots.map(async (file) => { try { @@ -1963,6 +2198,7 @@ export async function runReview(input: OrchestrateInput): Promise f.kind === "macro" && f.status !== "deleted") + touch(macros) const sets = await Promise.all( macros.map(async (file) => { try { @@ -1996,6 +2232,7 @@ export async function runReview(input: OrchestrateInput): Promise f.status !== "deleted" && ((f.kind === "test" || f.kind === "snapshot") || f.kind === "project_config"), ) + touch(others) const sets = await Promise.all( others.map(async (file) => { try { @@ -2036,6 +2273,7 @@ export async function runReview(input: OrchestrateInput): Promise f.kind === "schema_yml") + touch(schemaFiles) const sqlByModel = new Map([...ctxByPath.values()].filter((c) => c.newSql).map((c) => [modelNameFromPath(c.file.path).toLowerCase(), c.newSql!])) if (schemaFiles.length) { const schemaFindingSets = await Promise.all( @@ -2065,6 +2303,7 @@ export async function runReview(input: OrchestrateInput): Promise f.kind === "project_config" && /(^|\/)dbt_project\.ya?ml$/.test(f.path))) { if (file.status !== "modified") continue + touch([file]) const [oldContent, newContent] = await Promise.all([getContent?.(file.path, "old"), getContent?.(file.path, "new")]) all.push(detectThresholdLoosening(file, oldContent, newContent)) } @@ -2180,12 +2419,26 @@ export async function runReview(input: OrchestrateInput): Promise { - let out = dedupe(list).filter((f) => !exclusionReason(f, input.rubric)) + let out = dedupe(list) + .filter((f) => !exclusionReason(f, input.rubric)) + .map((f) => { + // The matched source text some lanes record, quoted as evidence. + const line = (f.evidence?.result as { line?: unknown } | undefined)?.line + return typeof line === "string" && line.trim() && !f.evidenceLine + ? { ...f, evidenceLine: line.trim().slice(0, 200) } + : f + }) + // Per-rule posting policy: `off` rules are dropped (and so never ground the + // AI lane); the rest are marked for inline or summary-only posting. + out = out + .map((f) => ({ f, tier: ruleTier(f.rule, input.config.rules) })) + .filter(({ tier }) => tier !== "off") + .map(({ f, tier }) => ({ ...f, tier: tier === "inline" ? ("inline" as const) : ("summary" as const) })) // Cross-engine portability advice from the regex catalog is noise for a // project that runs on one known engine. Core's dialect-aware check (a // function missing on THIS engine) is a real error and is kept. if (input.suppressPortability) out = out.filter((f) => !isSuppressiblePortability(f, dialect, input.portabilityTargets)) - out = anchorToDiff(out, input.changedFiles) + out = anchorToDiff(anchorByEvidence(out, input.changedFiles), input.changedFiles) if (merge) out = mergeRepeats(out) return out.sort((a, b) => SEVERITY_ORDER[b.severity] - SEVERITY_ORDER[a.severity] || a.file.localeCompare(b.file)) } @@ -2220,9 +2473,13 @@ export async function runReview(input: OrchestrateInput): Promise aiFiles.push({ path: f.path, status: f.status, model: modelNameFromPath(f.path), kind: f.kind, diff: f.diff, sql: contents[i] }), ) + // Only the files within the AI lane's file cap are reached; the rest stay in the ledger. + touch(selectAiFiles(aiFiles, aiInputBudget().maxFiles)) + const context = input.aiGraphContext ? await aiGraphContext(aiFiles.map((f) => f.path), input.runner, getContent) : undefined try { const res = await input.aiReview({ files: aiFiles, + context, grounding: cleanFindings(merged), prTitle: input.prTitle, prBody: input.prBody, @@ -2239,16 +2496,37 @@ export async function runReview(input: OrchestrateInput): Promise SEVERITY_ORDER[f.severity] >= minSev) + const findings = cleanFindings(validated).filter((f) => SEVERITY_ORDER[f.severity] >= minSev) // The verdict counts every instance; merging repeats is presentation only. - const verdictFindings = cleanFindings(merged, false).filter((f) => SEVERITY_ORDER[f.severity] >= minSev) + const verdictFindings = cleanFindings(validated, false).filter((f) => SEVERITY_ORDER[f.severity] >= minSev) // Lint-only means no changed model resolved against a manifest. A single // undecidable finding does not make the whole run lint-only; those findings // carry their own `degraded` flag and are counted separately in the summary. const degraded = runDegraded const engineErrors = [...(input.runner.failures?.() ?? []), ...laneErrors] + + // Coverage ledger: every changed file the review did not look at, and why. + // First reason per path wins; `unverified` entries are always kept. + const sortedUnverified = unverified.sort((a, b) => a.path.localeCompare(b.path)) + const ledger: NotReviewed[] = [ + ...(input.scope?.notReviewed ?? []), + ...dropped, + ...reviewable + .filter((f) => f.kind === "other" || f.kind === "analysis") + .map((f) => ({ path: f.path, reason: "not a dbt model/macro/test/yml/seed file" })), + ...reviewable + .filter((f) => !touched.has(f.path)) + .map((f) => ({ path: f.path, reason: `no checks run for ${f.kind} files at the ${tier} tier` })), + ...(manifestMissing && !anyManifest + ? modelFiles.map((f) => ({ path: f.path, reason: "no dbt manifest: lineage, config and graph checks skipped" })) + : []), + ] + const seen = new Set() + const notReviewed = ledger.filter((e) => !seen.has(e.path) && !!seen.add(e.path)) + notReviewed.push(...sortedUnverified) const envelope = buildEnvelope({ engineErrors: engineErrors.length ? engineErrors : undefined, ai: aiStatus, @@ -2264,8 +2542,8 @@ export async function runReview(input: OrchestrateInput): Promise a.path.localeCompare(b.path)) : undefined } + input.scope || notReviewed.length + ? { ...input.scope, unverified: sortedUnverified.length ? sortedUnverified : undefined, notReviewed } : undefined, // Include tierReasons whenever `--explain-tier` / `--force-tier` is set, // when a riskTierPathTokens config error was caught above, OR when the diff --git a/packages/opencode/src/altimate/review/post-github.ts b/packages/opencode/src/altimate/review/post-github.ts index 3b9699e325..8129bbe769 100644 --- a/packages/opencode/src/altimate/review/post-github.ts +++ b/packages/opencode/src/altimate/review/post-github.ts @@ -1,7 +1,15 @@ import { Octokit } from "@octokit/rest" import { promises as fs } from "node:fs" import { type VerdictEnvelope, VCS_EVENT, reviewIncomplete } from "./verdict" -import { renderSummary, inlineComments, postedFindingIds, REVIEW_MARKER, verdictHeadline } from "./format" +import { + renderSummary, + inlineComments, + inlineEligible, + postedFindingIds, + postingPolicy, + REVIEW_MARKER, + verdictHeadline, +} from "./format" /** * Post a verdict envelope to a GitHub pull request: an upserted summary comment @@ -61,6 +69,9 @@ export interface PostResult { reviewSkipped?: boolean /** Earlier blocking altimate reviews dismissed because this run no longer blocks. */ dismissedReviews?: number + /** True when a clean run under `silentWhenClean` posted no review (and no + * summary, unless one from an earlier run needed replacing). Implies `reviewSkipped`. */ + silent?: boolean } /** True when every check ran (no engine error, no failed file, no abstained @@ -105,11 +116,34 @@ export async function postGitHubReview( const octo = client ?? new Octokit({ auth: target.token }) const { owner, repo, prNumber } = target const result: PostResult = { inlineFellBack: false } + const projectRoot = env.scope?.projectRoot || undefined + + // 1. Read what is already on the PR. Rerun dedup: a finding already + // commented inline (same fingerprint, which ignores line numbers) is not + // posted again on the next push. + let posted = new Set() + let priorBotReview = false + let staleBlocking: number[] = [] + try { + const [reviewComments, reviews] = await Promise.all([ + octo.paginate(octo.rest.pulls.listReviewComments, { owner, repo, pull_number: prNumber, per_page: 100 }), + octo.paginate(octo.rest.pulls.listReviews, { owner, repo, pull_number: prNumber, per_page: 100 }), + ]) + posted = postedFindingIds(reviewComments.map((c) => c.body)) + const botReviews = reviews.filter((r) => isBotReview(r, projectRoot)) + priorBotReview = botReviews.length > 0 + staleBlocking = botReviews.filter((r) => r.state === "CHANGES_REQUESTED").map((r) => r.id) + } catch { + /* listing failed: post everything rather than nothing */ + } - // 1. Upsert the summary comment (dedup by marker). Paginate ALL comments — + // 2. Upsert the summary comment (dedup by marker). Paginate ALL comments — // on a busy PR the prior marker comment can be past the first page, and - // missing it would post a duplicate summary on every rerun. - const summary = renderSummary(env) + // missing it would post a duplicate summary on every rerun. A silent clean + // run posts nothing, but still overwrites an earlier summary so it does + // not keep showing findings that are gone. + const summary = renderSummary(env, { alreadyPosted: posted }) + const silent = summary === "" const existing = await octo.paginate(octo.rest.issues.listComments, { owner, repo, @@ -118,42 +152,24 @@ export async function postGitHubReview( }) const prior = existing.find((c) => c.body?.includes(REVIEW_MARKER)) if (prior) { - const r = await octo.rest.issues.updateComment({ owner, repo, comment_id: prior.id, body: summary }) + const body = silent ? renderSummary(env, { alreadyPosted: posted, allowSilent: false }) : summary + const r = await octo.rest.issues.updateComment({ owner, repo, comment_id: prior.id, body }) result.summaryCommentId = r.data.id - } else { + } else if (!silent) { const r = await octo.rest.issues.createComment({ owner, repo, issue_number: prNumber, body: summary }) result.summaryCommentId = r.data.id } - // 2. Post a review. `env.verdict` arrives already mode-gated from + // 3. Post a review. `env.verdict` arrives already mode-gated from // buildEnvelope (comment mode softened REQUEST_CHANGES → COMMENT upstream); // this map only translates verdict → GitHub event. APPROVE always maps to a // COMMENT event — the bot must never submit a formal GitHub approval. const event = VCS_EVENT[env.verdict] const headline = verdictHeadline(env) - const projectRoot = env.scope?.projectRoot || undefined const reviewBody = `${headline}\n\n${botReviewMarker(projectRoot)}` - - // Rerun dedup: a finding already commented inline (same fingerprint, which - // ignores line numbers) is not posted again on the next push. - let posted = new Set() - let priorBotReview = false - let staleBlocking: number[] = [] - try { - const [reviewComments, reviews] = await Promise.all([ - octo.paginate(octo.rest.pulls.listReviewComments, { owner, repo, pull_number: prNumber, per_page: 100 }), - octo.paginate(octo.rest.pulls.listReviews, { owner, repo, pull_number: prNumber, per_page: 100 }), - ]) - posted = postedFindingIds(reviewComments.map((c) => c.body)) - const botReviews = reviews.filter((r) => isBotReview(r, projectRoot)) - priorBotReview = botReviews.length > 0 - staleBlocking = botReviews.filter((r) => r.state === "CHANGES_REQUESTED").map((r) => r.id) - } catch { - /* listing failed: post everything rather than nothing */ - } - const comments = inlineComments(env, posted) - const withLine = env.findings.filter((f) => typeof f.startLine === "number").length - result.inlineSkippedAsPosted = withLine - comments.length + const { comments } = inlineComments(env, posted) + const posting = postingPolicy(env) + result.inlineSkippedAsPosted = env.findings.filter((f) => inlineEligible(f, posting) && posted.has(f.id)).length // Anchor inline comments to the PR head commit. Without commit_id, GitHub's // line-positioning is fragile and can 422 even on valid lines. @@ -194,6 +210,14 @@ export async function postGitHubReview( result.dismissedReviews = dismissed } + // A silent clean run adds no review: the check status records the run. + if (silent) { + result.silent = true + result.reviewSkipped = true + await dismissStale() + return result + } + // Nothing new inline, not blocking, and the PR already has an altimate // review: the updated summary comment says it all; another "Reviewed with // comments" entry per push is noise. A blocking verdict always posts. @@ -228,6 +252,16 @@ export async function postGitHubReview( const isPositioning = status === 422 && /(position|\bline\b|diff|commit)/.test(detail) result.inlineFellBack = isPositioning result.postError = isPositioning ? undefined : `${status ?? ""} ${e?.message ?? e}`.trim() + // The summary listed inline findings as one-line pointers; with the inline + // comments gone, rewrite it with every finding in full so none loses its body. + if (comments.length && result.summaryCommentId !== undefined) { + try { + const body = renderSummary(env, { alreadyPosted: posted, inline: false, allowSilent: false }) + await octo.rest.issues.updateComment({ owner, repo, comment_id: result.summaryCommentId, body }) + } catch { + /* the event-only review below still records the verdict */ + } + } try { const r = await octo.rest.pulls.createReview({ owner, diff --git a/packages/opencode/src/altimate/review/project-rules.ts b/packages/opencode/src/altimate/review/project-rules.ts index 1caa55473f..378505917e 100644 --- a/packages/opencode/src/altimate/review/project-rules.ts +++ b/packages/opencode/src/altimate/review/project-rules.ts @@ -3,9 +3,10 @@ import path from "path" import YAML from "yaml" import { type Finding, type ReviewCategory, type Severity, makeFinding } from "./finding" -import type { ChangedFile } from "./diff-filter" +import { type ChangedFile, walkDiffLines } from "./diff-filter" import { incrementalFilter, inlineConfig } from "./incremental-semantics" import { parenBody, topLevel } from "./rule-catalog" +import { capabilityFor } from "./adapter-capabilities" const nameOf = (p: string) => path.basename(p).replace(/\.(sql|py|ya?ml|csv)$/i, "") @@ -16,6 +17,8 @@ function finding( category: ReviewCategory, title: string, body: string, + /** The raw added line the rule matched, when it matches one. */ + line?: string, /** What the finding is about (a ref target, a column, a macro), so two findings of one rule in one file keep distinct ids. */ subject?: string, ): Finding { @@ -28,7 +31,7 @@ function finding( file: file.path, model, confidence: "high", - evidence: { tool: "dbt-project-rules", result: { rule } }, + evidence: { tool: "dbt-project-rules", result: line === undefined ? { rule } : { rule, line } }, ruleKey: subject ? `${category}:${rule}:${subject}` : `${category}:${rule}`, }) } @@ -36,6 +39,10 @@ function finding( const addedLines = (diff: string | undefined) => (diff ?? "").split("\n").filter((l) => l.startsWith("+") && !l.startsWith("+++")).map((l) => l.slice(1)) +/** The first added line matching `re`, for anchoring a finding to the diff. */ +const addedLine = (file: ChangedFile, re: RegExp) => addedLines(file.diff).find((l) => re.test(l)) +const escapeRe = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + function parseYaml(text: string | undefined): any { if (!text) return undefined try { @@ -71,6 +78,7 @@ export function packagesFindings(file: ChangedFile, newContent: string | undefin "materialization", "package installed from a git branch", `${list} tracks a branch, so \`dbt deps\` installs whatever the branch holds on the day it runs. Pin a release (\`package:\` + \`version:\` range) or a tag/commit sha before merging.`, + addedLines(file.diff).find((l) => l.includes(touched[0].git)), ), ] } @@ -133,6 +141,7 @@ export function dbtProjectFindings(file: ChangedFile, oldContent: string | undef "sql_quality", `duplicate grantee under \`${at}.${priv}\``, `${[...new Set(dups)].map((d) => `\`${d}\``).join(", ")} is listed more than once. Usually a copy-paste where another role was meant; check the list is complete.`, + undefined, `${at}.${priv}`, ), ) @@ -150,6 +159,7 @@ export function dbtProjectFindings(file: ChangedFile, oldContent: string | undef "lineage_breakage", "resources disabled in dbt_project.yml", "This change adds `enabled: false`. Disabled models drop out of the DAG: anything that refs them fails to compile and their tables stop refreshing. Say in the PR why, and whether downstream models are disabled too.", + addedLine(file, /^\s*\+?enabled\s*:\s*false\b/i), ), ) return out @@ -195,6 +205,18 @@ export function jinjaBalance(sql: string): string | undefined { return undefined } +/** The first added head line inside macro `name`'s parameter list in `sql` + * (a multi-line signature changes on a parameter line, not the `{% macro` + * line), with its text; undefined when the PR added none of those lines. */ +function signatureAnchor(file: ChangedFile, sql: string, name: string): { line: number; text: string } | undefined { + const m = new RegExp(`\\{%-?\\s*macro\\s+${escapeRe(name)}\\s*\\([^)]*\\)`).exec(sql) + if (!m) return undefined + const first = sql.slice(0, m.index).split("\n").length + const last = first + m[0].split("\n").length - 1 + const hit = [...walkDiffLines(file.diff)].find((r) => r.kind === "+" && r.line >= first && r.line <= last) + return hit && { line: hit.line, text: hit.text } +} + const macroSignatures = (sql: string | undefined) => { const out = new Map() for (const m of (sql ?? "").matchAll(/\{%-?\s*macro\s+([\w.]+)\s*\(([^)]*)\)/g)) @@ -205,7 +227,7 @@ const macroSignatures = (sql: string | undefined) => { /** The comment text on a line (`-- …`, `/* … *\/`, `{# … #}`), or "". */ const commentOf = (l: string) => /--(.*)$|\/\*(.*?)(\*\/|$)|\{#(.*?)(#\}|$)/.exec(l)?.slice(1).filter(Boolean).join(" ") ?? "" const TEMP_MARKER = - /\b(temp(orary)?\b.{0,40}\b(ci|test|validation|sample|debug|hack)|revert\b.{0,30}\b(to|before|after)\b|do\s+not\s+merge|remove\s+before\s+(merge|release)|ci\s+test\b|for\s+testing\s+only)/i + /\b(temp(orary)?\b.{0,40}\b(ci|test|validation|sample|debug|hack)|revert\b.{0,30}\b(to|before|after)\b|do\s+not\s+merge|remove\s+before\s+(merge|release)|ci\s+test\b|for\s+testing\s+only|take\s+(this|it)\s+out\s+(before|once|after|when)\b|\b(added|enabled|only)\s+while\s+debugging\b|(remove|delete|drop)\s+(this|it)\s+before\s+(merg|releas|deploy|prod))/i const varNames = (sql: string | undefined) => new Set([...(sql ?? "").matchAll(/\bvar\s*\(\s*['"]([^'"]+)['"]/g)].map((m) => m[1])) @@ -234,18 +256,19 @@ export function jinjaFindings(file: ChangedFile, oldContent: string | undefined, if (!old || old.length === 0) continue const kept = old.filter((p) => params.includes(p)) const shifted = kept.filter((p) => params.indexOf(p) !== old.indexOf(p)) - if (shifted.length) - out.push( - finding( - file, - "macro-param-shifted", - "warning", - "sql_correctness", - `\`${name}\` parameters moved position`, - `${shifted.map((p) => `\`${p}\``).join(", ")} ${shifted.length === 1 ? "is" : "are"} no longer at the same position in \`${name}(${params.join(", ")})\`. Callers that pass arguments positionally now bind them to different parameters. Add new parameters at the end, or check every caller uses keywords.`, - name, - ), - ) + if (!shifted.length) continue + const at = signatureAnchor(file, newContent, name) + const f = finding( + file, + "macro-param-shifted", + "warning", + "sql_correctness", + `\`${name}\` parameters moved position`, + `${shifted.map((p) => `\`${p}\``).join(", ")} ${shifted.length === 1 ? "is" : "are"} no longer at the same position in \`${name}(${params.join(", ")})\`. Callers that pass arguments positionally now bind them to different parameters. Add new parameters at the end, or check every caller uses keywords.`, + at?.text ?? addedLine(file, new RegExp(`\\bmacro\\s+${escapeRe(name)}\\s*\\(`)), + name, + ) + out.push(at ? { ...f, startLine: at.line, endLine: at.line } : f) } // A var() key renamed with no fallback: projects that set the old key lose their override silently. @@ -285,10 +308,12 @@ export function jinjaFindings(file: ChangedFile, oldContent: string | undefined, "sql_correctness", "code marked temporary is part of the change", `\`${marker.trim().slice(0, 160)}\` says it is temporary. If this merges, the temporary filter, date or sample ships to production. Revert it, or drop the comment if it is meant to stay.`, + marker, ), ) - if (addedLines(file.diff).some((l) => /modules\.datetime\.datetime\.utcnow\s*\(/.test(l))) + const utcnow = addedLine(file, /modules\.datetime\.datetime\.utcnow\s*\(/) + if (utcnow !== undefined) out.push( finding( file, @@ -297,6 +322,7 @@ export function jinjaFindings(file: ChangedFile, oldContent: string | undefined, "idempotency", "`datetime.utcnow()` returns a naive timestamp", "`utcnow()` is deprecated since Python 3.12 and returns a timestamp with no timezone, which compares wrongly against aware ones. Use `modules.datetime.datetime.now(modules.datetime.timezone.utc)`.", + utcnow, ), ) return out @@ -329,6 +355,8 @@ export function modelConfigFindings( dialect: string, /** The manifest's config for the model: fills what the file leaves to project defaults. */ manifestConfig?: Record, + /** False when `dialect` is a default guess, not read from config or the manifest. */ + dialectKnown = true, ): Finding[] { if (!newSql || file.status === "deleted") return [] const cfg = inlineConfig(newSql) @@ -348,12 +376,18 @@ export function modelConfigFindings( "materialization", `\`unique_key\` has no effect on a ${materialized}`, `\`unique_key\` only drives incremental merges; on a \`${materialized}\` it neither deduplicates nor checks anything. Add a \`unique\` / \`unique_combination_of_columns\` test to enforce it.`, + addedLine(file, /\bunique_key\b/), ), ) // dbt-bigquery requires microbatch `batch_size` to equal the partition granularity. - // A `partition_by` dict with `granularity` is BigQuery's syntax, so it identifies the adapter too. - if (strategy === "microbatch" && (/bigquery/i.test(dialect) || /['"]granularity['"]\s*:/.test(cfg.partition_by ?? ""))) { + // On a known adapter its capability decides; otherwise a `partition_by` dict with + // `granularity` (BigQuery's syntax) identifies the adapter. + const cap = dialectKnown ? capabilityFor(dialect) : undefined + const batchMustMatchPartition = cap + ? !!cap.microbatchBatchSizeMatchesPartition + : /bigquery/i.test(dialect) || /['"]granularity['"]\s*:/.test(cfg.partition_by ?? "") + if (strategy === "microbatch" && batchMustMatchPartition) { const sizes = valueLiterals(cfg.batch_size, newSql).filter((s) => ["hour", "day", "month", "year"].includes(s)) const gran = /['"]granularity['"]\s*:\s*['"](\w+)['"]/i.exec(cfg.partition_by ?? "")?.[1]?.toLowerCase() ?? (cfg.partition_by ? "day" : undefined) const off = gran ? sizes.filter((s) => s !== gran) : [] @@ -366,6 +400,7 @@ export function modelConfigFindings( "materialization", "microbatch `batch_size` differs from the partition granularity", `\`batch_size\` can be ${off.map((s) => `\`${s}\``).join(", ")} but \`partition_by\` granularity is \`${gran}\`. dbt-bigquery rejects a microbatch model whose batch size and partition granularity differ, so those runs fail before executing.`, + addedLine(file, /\bbatch_size\b/) ?? addedLine(file, /\bgranularity\b|\bpartition_by\b/), ), ) } @@ -384,6 +419,7 @@ export function modelConfigFindings( "warehouse_cost", "partition column is not in the merge `unique_key`", `The table is partitioned by ${missing.map((p) => `\`${p}\``).join(", ")}, which \`unique_key\` [${uk.join(", ")}] doesn't include, so the merge can't prune target partitions and scans the whole table. Add the partition column to \`unique_key\` (or \`incremental_predicates\`).`, + addedLine(file, /\bunique_key\b/) ?? addedLine(file, /\bpartition_by\b/), ), ) } @@ -403,7 +439,8 @@ export function modelConfigFindings( // Filtering on a creation timestamp misses updates to rows created earlier. // Rows are expected to change when the model carries an update timestamp or merges on a key. const rowsChange = /\b\w*(updated|modified|changed)(_at|_on|_date|_ts|_time)\b/i.test(newSql) || (uk.length > 0 && strategy !== "append") - if (filter && /\b\w*(created|inserted)(_at|_on|_date|_ts|_time)\b/i.test(filter) && !/\b\w*(updated|modified|changed)\w*\b/i.test(filter) && rowsChange) + const created = filter && /\b\w*(created|inserted)(_at|_on|_date|_ts|_time)\b/i.exec(filter)?.[0] + if (filter && created && !/\b\w*(updated|modified|changed)\w*\b/i.test(filter) && rowsChange) out.push( finding( file, @@ -412,6 +449,9 @@ export function modelConfigFindings( "materialization", "incremental filter reads new rows only, not updated ones", `The \`is_incremental()\` filter keys on a creation timestamp while ${/\b\w*(updated|modified|changed)(_at|_on|_date|_ts|_time)\b/i.test(newSql) ? "the model also carries an update timestamp" : "the model merges on \`unique_key\`, so existing rows are expected to change"}. Rows created before the last run and changed since are never picked up again. Filter on the update timestamp, or explain why rows never change.`, + // The first added line that compares the creation timestamp. `filter` is + // tokenised, so the raw line is matched on the column and a comparison. + addedLines(file.diff).find((l) => new RegExp(`\\b${created}\\b`, "i").test(l) && /[<>]=?|\bbetween\b/i.test(l)), ), ) } @@ -436,13 +476,14 @@ function editDistance(a: string, b: string): number { /** Single-argument `ref('x')` on added lines whose target no resource carries. */ export function missingRefFindings(file: ChangedFile, names: Set): Finding[] { - const refs = new Set() + const refs = new Map() for (const l of addedLines(file.diff)) - for (const m of l.matchAll(/\bref\s*\(\s*['"]([^'"]+)['"]\s*(?:,\s*v(?:ersion)?\s*=\s*[^)]*)?\)/g)) refs.add(m[1]) + for (const m of l.matchAll(/\bref\s*\(\s*['"]([^'"]+)['"]\s*(?:,\s*v(?:ersion)?\s*=\s*[^)]*)?\)/g)) + if (!refs.has(m[1])) refs.set(m[1], l) // Versioned models live in `_v1.sql`, `_v2.sql`; `ref('')` resolves to one of them. const known = new Set(names) for (const n of names) known.add(n.replace(/_v\d+$/, "")) - const missing = [...refs].filter((r) => !known.has(r.toLowerCase()) && !known.has(r.toLowerCase().replace(/_v\d+$/, ""))) + const missing = [...refs.keys()].filter((r) => !known.has(r.toLowerCase()) && !known.has(r.toLowerCase().replace(/_v\d+$/, ""))) return missing.map((r) => { const near = [...names].filter((n) => Math.abs(n.length - r.length) <= 2 && editDistance(n, r.toLowerCase()) <= 2) return finding( @@ -452,6 +493,7 @@ export function missingRefFindings(file: ChangedFile, names: Set): Findi "sql_correctness", `\`ref('${r}')\` points at no model`, `No model, seed or snapshot named \`${r}\` exists in this project or its installed packages, so dbt fails to parse the project.${near.length ? ` Did you mean ${near.slice(0, 3).map((n) => `\`${n}\``).join(", ")}?` : " Add the model in this PR or fix the name."}`, + refs.get(r), r.toLowerCase(), ) }) @@ -623,6 +665,7 @@ export function testsOnNullColumns(file: ChangedFile, ymlContent: string | undef "test_coverage", `\`${m.name}.${c.name}\` is tested but the model sets it to NULL`, `\`${m.name}\` selects \`${c.name}\` as a literal NULL, so its ${failing.map((t) => `\`${t}\``).join(" / ")} test fails on every row. Drop the test until the column is populated, or populate it.`, + addedLine(file, new RegExp(`\\b${escapeRe(col)}\\b`, "i")) ?? addedLine(file, /not_null/), `${m.name}.${c.name}`, ), ) diff --git a/packages/opencode/src/altimate/review/rule-catalog.ts b/packages/opencode/src/altimate/review/rule-catalog.ts index e40ff9eece..b5b5958d0b 100644 --- a/packages/opencode/src/altimate/review/rule-catalog.ts +++ b/packages/opencode/src/altimate/review/rule-catalog.ts @@ -50,7 +50,11 @@ export interface Rule { * engine, unless that engine is missing from `worksOn` (then it is a real * error there and is kept). Omit `worksOn` when support isn't established. */ - portability?: { worksOn?: string[] } + portability?: { + worksOn?: string[] + /** More engines that accept the construct when `check` holds on the new file (comments and strings blanked). */ + alsoWorksOn?: { engines: string[]; check: (sql: string) => boolean } + } /** * Engine-dependent rule: fires as written where this capability is true for * the project's adapter; where it is false, fires with `portabilityBody` @@ -106,10 +110,20 @@ export interface RuleInput { /** True when the rule matches the change. */ export function ruleMatches(rule: Rule, input: RuleInput): boolean { + return matchRule(rule, input) !== null +} + +/** + * Match a rule against the change. Returns null when it doesn't match, else the + * raw added line that carried the signal (the first one that satisfied `added`, + * else the added line inside the matching ON clause, else the first added line + * where the `newSql` pattern shows), when one exists. + */ +export function matchRule(rule: Rule, input: RuleInput): { line?: string } | null { const kind = fileKind(input.file) const wantKind = rule.kind ?? "model_sql" - if (wantKind !== "any" && kind !== wantKind && !(kind === "macro" && rule.macros)) return false - if (rule.pathGlob && !rule.pathGlob.test(input.file.path)) return false + if (wantKind !== "any" && kind !== wantKind && !(kind === "macro" && rule.macros)) return null + if (rule.pathGlob && !rule.pathGlob.test(input.file.path)) return null // Relevance gate: a rule only fires when its signal is in THE DIFF, never on // an unrelated edit to a matching file. added/removed are diff-anchored by @@ -117,24 +131,26 @@ export function ruleMatches(rule: Rule, input: RuleInput): boolean { // changed lines to count as relevant. `absentNewSql` is a whole-file guard. const diffText = stripComments([...input.added, ...input.removed].join("\n")) let relevant = false + let line: string | undefined if (rule.added) { - const hit = input.added.some((l) => { + // Tested on the stripped line; the raw line is what gets recorded. + line = input.added.find((l) => { const s = rule.ignoreStrings ? stripStrings(stripComments(l)) : stripComments(l) return rule.added!.test(s) && !(rule.unless && rule.unless.test(s)) }) - if (!hit) return false + if (line === undefined) return null relevant = true } if (rule.removed) { - if (!input.removed.some((l) => rule.removed!.test(stripComments(l)))) return false + if (!input.removed.some((l) => rule.removed!.test(stripComments(l)))) return null relevant = true } if (rule.newSql) { - if (!rule.newSql.test(stripComments(input.newSql))) return false + if (!rule.newSql.test(stripComments(input.newSql))) return null if (rule.newSql.test(diffText)) relevant = true } if (rule.absentNewSql) { - if (rule.absentNewSql.test(stripComments(input.newSql))) return false + if (rule.absentNewSql.test(stripComments(input.newSql))) return null } if ( rule.check && @@ -143,14 +159,24 @@ export function ruleMatches(rule: Rule, input: RuleInput): boolean { input.added.map(stripComments), ) ) - return false + return null if (rule.onClause) { - const changed = input.added.map((l) => stripStrings(stripComments(l)).trim()).filter((l) => /\w{2}/.test(l)) - const hit = onClauses(input.newSql).some((c) => rule.onClause!.test(c) && changed.some((l) => c.includes(l))) - if (!hit) return false + const changed = input.added + .map((raw) => ({ raw, s: stripStrings(stripComments(raw)).trim() })) + .filter(({ s }) => /\w{2}/.test(s)) + let onLine: string | undefined + for (const c of onClauses(input.newSql)) { + if (!rule.onClause.test(c)) continue + onLine = changed.find(({ s }) => c.includes(s))?.raw + if (onLine !== undefined) break + } + if (onLine === undefined) return null + line ??= onLine relevant = true } - return relevant + if (!relevant) return null + if (line === undefined && rule.newSql) line = input.added.find((l) => rule.newSql!.test(stripComments(l))) + return line === undefined ? {} : { line } } const ON_END = /^(?:join|where|group\s+by|order\s+by|qualify|having|union|limit|window|left|right|inner|full|cross|natural|lateral|select|from)\b|^;|^\{%/i @@ -320,6 +346,13 @@ export function ruleEngine(rule: Rule): string | undefined { return m ? PACK_ENGINE[m[1]] : undefined } +/** Engines a portability rule's construct runs on, for this file. */ +function worksOn(rule: Rule, newSql: string): string[] | undefined { + const p = rule.portability + if (!p?.worksOn || !p.alsoWorksOn?.check(stripStrings(stripComments(newSql)))) return p?.worksOn + return [...p.worksOn, ...p.alsoWorksOn.engines] +} + /** Evaluate the catalog against a changed file. */ export function evaluateCatalog( file: ChangedFile, @@ -337,7 +370,8 @@ export function evaluateCatalog( // applies to its own engine once the project's engine is known. const engine = ruleEngine(rule) if (engine && ctx?.dialectKnown && ctx.dialect.toLowerCase().replace(/^spark$/, "databricks") !== engine) continue - if (!ruleMatches(rule, input)) continue + const match = matchRule(rule, input) + if (!match) continue const body = gatedBody(rule, ctx) if (body === null) continue const f = makeFinding({ @@ -350,10 +384,11 @@ export function evaluateCatalog( confidence: "high", evidence: { tool: "rule-catalog", - result: - rule.portability || body !== rule.body - ? { rule: rule.id, portability: true, worksOn: rule.portability?.worksOn } - : { rule: rule.id }, + result: { + rule: rule.id, + ...(rule.portability || body !== rule.body ? { portability: true, worksOn: worksOn(rule, newSql) } : {}), + ...(match.line !== undefined ? { line: match.line } : {}), + }, }, ruleKey: `${rule.category}:${rule.id}`, }) @@ -439,12 +474,6 @@ function windowOrderRepeatsPartition(sql: string): boolean { return false } -/** A condition with whitespace collapsed and keywords lowercased outside quotes; quoted text stays exact ('A' is not 'a'). */ -const normalizeCondition = (c: string) => - c - .replace(/('[^']*'|"[^"]*")|[^'"]+/g, (seg, quoted) => (quoted ? seg : seg.replace(/\s+/g, " ").toLowerCase())) - .trim() - /** A CASE with two WHEN branches whose conditions are identical: the second is unreachable. */ function duplicateCaseWhen(sql: string): boolean { for (const m of sql.matchAll(/\bcase\b/gi)) { @@ -465,6 +494,31 @@ function duplicateCaseWhen(sql: string): boolean { return false } +/** Condition text for comparison: case and spacing folded outside string literals and quoted + * identifiers, which are kept verbatim ('A' is not 'a'; "Col" is case-sensitive). */ +function normalizeCondition(cond: string): string { + return cond + .split(/('(?:[^']|'')*'|"(?:[^"]|"")*")/) + .map((part, i) => + i % 2 + ? part + : part + .toLowerCase() + .replace(/\s+/g, " ") + .replace(/\s*([^\w\s])\s*/g, "$1"), + ) + .join("") + .trim() +} + +const NAVIGATION_WINDOW_FN = /^(first_value|last_value|nth_value|lead|lag)$/i + +/** Calls written `f(x IGNORE NULLS [ORDER BY …] [LIMIT n])`, with whether an OVER follows. */ +function ignoreNullsInCall(sql: string): { fn: string; over: boolean }[] { + const re = /\b(\w+)\s*\(\s*[^()]*?\bignore\s+nulls(?:\s+(?:order\s+by|limit)\b[^()]*)?\s*\)(\s*over\b)?/gi + return [...sql.matchAll(re)].map((m) => ({ fn: m[1], over: !!m[2] })) +} + /** A LEFT JOIN whose right side is filtered in the WHERE of the same SELECT: the join behaves as INNER. * With `added`, only when an added line references that right side (its columns or its join). */ export function leftJoinNullifiedByWhere(sql: string, added?: string[]): boolean { @@ -562,12 +616,19 @@ const FEASIBLE: Rule[] = [ "sql_correctness", "warning", "IGNORE NULLS inside the function call is BigQuery syntax", - "`f(x IGNORE NULLS)` parses on BigQuery only. Trino, Snowflake and others take it after the call: `last_value(x) IGNORE NULLS OVER (…)`.", + "`f(x IGNORE NULLS)` parses on BigQuery, and on DuckDB for window functions only. Trino, Snowflake and others take it after the call: `last_value(x) IGNORE NULLS OVER (…)`.", { added: /\bignore\s+nulls\b/i, - check: (sql) => /\(\s*[^()]*?\bignore\s+nulls\s*\)/i.test(sql), + check: (sql) => ignoreNullsInCall(sql).length > 0, macros: true, - portability: { worksOn: ["bigquery", "duckdb"] }, + // DuckDB takes the in-call form on navigation window functions only; on an aggregate it is a parse error. + portability: { + worksOn: ["bigquery"], + alsoWorksOn: { + engines: ["duckdb"], + check: (sql) => ignoreNullsInCall(sql).every((m) => m.over && NAVIGATION_WINDOW_FN.test(m.fn)), + }, + }, }, { add: "last_value(x ignore nulls) over (partition by k order by d)" }, { add: "last_value(x) ignore nulls over (partition by k order by d)" }, diff --git a/packages/opencode/src/altimate/review/rule-tiers.ts b/packages/opencode/src/altimate/review/rule-tiers.ts new file mode 100644 index 0000000000..2648addbb3 --- /dev/null +++ b/packages/opencode/src/altimate/review/rule-tiers.ts @@ -0,0 +1,184 @@ +import { normRuleToken } from "./finding" + +/** + * Per-rule posting policy. Every finding carries a stable `rule` id + * (`tool:rule`, see `ruleId` in finding.ts); this table decides whether a rule + * is posted inline on the diff, kept to the summary, or not reported at all. + * + * The defaults come from blind per-rule judging of replayed public dbt pull + * requests: rules whose findings were mostly judged valuable go inline, rules + * that were wrong at least half the time with nothing valuable and no real + * catch in the benchmark are off, and everything else stays in the summary. + * Teams override any entry with `rules:` in `.altimate/review.yml`. + */ + +export type RuleTier = "inline" | "summary" | "off" + +const INLINE = [ + "dbt-project-rules:incremental-filter-created-only", + "dbt-project-rules:temporary-code-marker", + "dbt.manifest:removed-final-layer-columns", + "dbt-project-rules:microbatch-granularity-mismatch", + "altimate-core.check:non-deterministic-dedup", + "dbt-patterns:dedup-no-tiebreaker", + "dbt-semantics:incremental-partition-change", + "dbt-project-rules:ref-target-missing", + "rule-catalog:qualify-unsupported", + "dbt-project-rules:macro-param-shifted", + "rule-catalog:case-duplicate-when", + "dbt-patterns:macro-var-no-default", + "dbt.manifest:removed-columns-declared-consumers", + "rule-catalog:ignore-nulls-inside-call", +] + +const OFF = [ + "dbt-patterns:case-no-else", + "dbt-patterns:comma-join", + "rule-catalog:cast-float-to-int", + "altimate-core.lexical:reserved-word-alias", + "dbt-patterns:cross-join", + "rule-catalog:self-join-no-inequality", + "rule-catalog:not-exists-suggested", + "rule-catalog:string-truncate-left", + "altimate-core.structural-diff:type-narrowing", + "rule-catalog:join-without-schema-prefix", + "rule-catalog:bq-generate-array-large", + "rule-catalog:intersect-perf", + "altimate-core.lexical:op-pg-regex-match", + "altimate-core.grain:conflicts-declared-uniqueness", + "rule-catalog:distinct-with-window", + "rule-catalog:lag-lead-no-default", + "rule-catalog:boolean-as-int", + "altimate-core.source-filters:sibling-mismatch", + "rule-catalog:double-aggregation-alias", + "rule-catalog:empty-string-eq", + "rule-catalog:join-no-on", + "rule-catalog:bq-cross-join-unnest", + "altimate-core.structural-diff:coalesce-removed", + "dbt-patterns:window-no-partition", + "rule-catalog:inequality-join", + "rule-catalog:running-total-no-order", + "dbt-patterns:float-equality", + "altimate-core.structural-diff:group-by-change", + "rule-catalog:bq-array-no-offset", + "altimate-core.structural-diff:join-key-regression", + "rule-catalog:large-in-list", + "rule-catalog:count-distinct-multiple-cols", + "rule-catalog:varchar-fixed-size", + "rule-catalog:boolean-eq-literal", + "dbt-patterns:boolean-precedence", + "rule-catalog:bq-legacy-sql", + "rule-catalog:run-query-parse", +] + +/** Summary rules listed explicitly: the high-volume ones, and rules judged + * mostly wrong that still produced a real catch in the benchmark. */ +const SUMMARY = [ + "dbt.manifest:new-model-no-grain-test", + "rule-catalog:union-select-star", + "rule-catalog:multiple-left-joins", + "dbt-semantics:incremental-history-plan", + "test-adequacy:test-inherits-threshold", + "dbt.manifest:left-join-key-not-unique", + "rule-catalog:join-on-cast", + "dbt-patterns:removed-tests", + "rule-catalog:group-by-positional", + "dbt-project-rules:incremental-filter-not-partition", + "altimate-core.structural-diff:removed-predicate", + "dbt-project-rules:partition-not-in-merge-key", + "dbt-project-rules:models-disabled", + "rule-catalog:greatest-least-null", + "rule-catalog:hardcoded-year-filter", + "dbt-patterns:hardcoded-date", + "altimate-core.dbt-config:incremental-no-guard", + "altimate-core.dbt-config:unique-key-change", + "dbt-patterns:fanout-join", + "dbt-patterns:self-join", + "rule-catalog:join-case-folded-one-side", + "dbt-project-rules:test-on-null-column", + "rule-catalog:distinct-and-group-by", + + // Off by dev judging, but the tier moved when one repo was left out of the + // judging sample; kept at summary until re-judged on held-out repos. + "altimate-core.check:aggregation-before-join", + "altimate-core.check:non-portable-function", + "altimate-core.check:non-portable-type", + "altimate-core.structural-diff:join-type-change", + "dbt-patterns:pii-into-mart", + "rule-catalog:bq-string-comparison-collation", + "rule-catalog:count-star-nullable", + "rule-catalog:count-star-with-left-join", + "rule-catalog:hardcoded-env-name", + "rule-catalog:having-row-filter", + "rule-catalog:implicit-cross-join-comma", + "rule-catalog:null-concat", + "rule-catalog:select-now-column", + "rule-catalog:self-equality", + "rule-catalog:top-n-no-order", + "rule-catalog:trailing-semicolon", + "rule-catalog:union-not-all", + "rule-catalog:unqualified-column-multi-table", + "rule-catalog:where-1-eq-1", +] + +export const DEFAULT_RULE_TIERS: Record = { + ...Object.fromEntries(SUMMARY.map((r) => [r, "summary" as const])), + ...Object.fromEntries(OFF.map((r) => [r, "off" as const])), + ...Object.fromEntries(INLINE.map((r) => [r, "inline" as const])), +} + +/** Engine rules (equivalence, lineage, impact, PII, contracts, data diff) that + * are never off by default: a glob override cannot drop them, only an exact + * id. Other engine rules are not on the floor; a few (structural-diff, grain, + * source-filters detectors judged mostly wrong) default to `off` in the table + * above, so their findings no longer count toward the verdict unless a team + * turns them back on. */ +const ENGINE_FLOOR = [ + "altimate-core.equivalence:", + "altimate-core.column-lineage:", + "impact-analysis:", + "schema.detect-pii:", + "altimate-core.classify-pii:", + "contract-check:", + "consumer-column-list:", + "data.diff:", +] + +/** A rule id or override key in the spelling finding ids use (`ruleId`): + * `dbt_patterns:self_join` matches `dbt-patterns:self-join`. A trailing `*` + * glob is kept. */ +function normRuleKey(key: string): string { + const glob = key.endsWith("*") + const body = glob ? key.slice(0, -1) : key + const i = body.indexOf(":") + const norm = i < 0 ? normRuleToken(body) : `${normRuleToken(body.slice(0, i))}:${normRuleToken(body.slice(i + 1))}` + return glob ? norm + "*" : norm +} + +const isFloor = (rule: string) => ENGINE_FLOOR.some((p) => rule.startsWith(p)) + +/** + * Resolve a rule's tier: exact override, then the most specific glob override + * (`tool:*`, longest prefix wins), then the default table, then `summary`. + * An engine-floor rule can be turned off only by an exact override. + */ +export function ruleTier(rule: string | undefined, rawOverrides: Record = {}): RuleTier { + if (!rule) return "summary" + rule = normRuleKey(rule) + const overrides = Object.fromEntries(Object.entries(rawOverrides).map(([k, v]) => [normRuleKey(k), v])) + const exact = overrides[rule] + if (exact) return exact + const floor = isFloor(rule) + const globs = Object.entries(overrides) + .filter(([k]) => k.endsWith("*") && rule.startsWith(k.slice(0, -1))) + .sort(([a], [b]) => b.length - a.length) + for (const [, tier] of globs) { + if (floor && tier === "off") continue + return tier + } + // AI reviewer findings were judged per finding by the verifier, not per rule: + // they default to inline, and an explicit override still wins above. + if (rule.startsWith("ai-review:")) return "inline" + const tier = DEFAULT_RULE_TIERS[rule] ?? "summary" + return floor && tier === "off" ? "summary" : tier +} diff --git a/packages/opencode/src/altimate/review/run.ts b/packages/opencode/src/altimate/review/run.ts index 8171b3b763..d74006097d 100644 --- a/packages/opencode/src/altimate/review/run.ts +++ b/packages/opencode/src/altimate/review/run.ts @@ -446,6 +446,7 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise aiReview: opts.noAi || config.ai === false ? undefined : runAiReview, aiDisabledReason, aiModel: opts.aiModel, + aiGraphContext: process.env.ALTIMATE_REVIEW_AI_GRAPH === "1", prTitle: opts.prTitle, prBody: opts.prBody, explainTier: opts.explainTier, @@ -456,7 +457,12 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise portabilityTargets: await projectAdapters(opts.cwd, opts.head, projectRootRel), dialectKnown, consumerLists, - policy: { source: policy.source, hash: policy.hash, changedInPr: policy.changedInPr.length ? policy.changedInPr : undefined }, + policy: { + source: policy.source, + hash: policy.hash, + changedInPr: policy.changedInPr.length ? policy.changedInPr : undefined, + posting: config.posting, + }, scope: { baseRef: resolved?.ref ?? opts.base, baseSha: resolved?.sha, @@ -465,6 +471,10 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise projectRoot: projectRootRel, outOfScopeFiles: scoped.outOfScope || undefined, movedUnchangedFiles: scoped.movedUnchanged || undefined, + notReviewed: [ + ...scoped.outOfScopePaths.map((path) => ({ path, reason: `outside the dbt project (${projectRootRel || "."})` })), + ...scoped.movedUnchangedPaths.map((path) => ({ path, reason: "moved without edits; not re-reviewed" })), + ], }, }) } diff --git a/packages/opencode/src/altimate/review/runner.ts b/packages/opencode/src/altimate/review/runner.ts index 0c89c2e063..c3df5ca427 100644 --- a/packages/opencode/src/altimate/review/runner.ts +++ b/packages/opencode/src/altimate/review/runner.ts @@ -418,6 +418,49 @@ export function createDispatcherRunner(opts: DispatcherRunnerOptions): ReviewRun return Array.isArray(pk) && pk.length ? pk.map((c: string) => String(c)) : undefined }, + async graphParents(paths: string[]) { + const raw = loadRawManifest(opts.manifestPath) + if (!raw) return [] + const project = raw.metadata?.project_name + const nodes = Object.values(raw.nodes ?? {}) + // A changed path may carry a prefix the manifest's project-relative paths don't. + const owner = (ofp: string) => paths.find((p) => p === ofp || p.endsWith("/" + ofp)) + const parents = new Map() + const add = (key: string, entry: { path: string; kind: string; name: string; columns?: string[] }, forPath: string) => { + const hit = parents.get(key) ?? { ...entry, forPaths: [] } + if (!hit.forPaths.includes(forPath)) hit.forPaths.push(forPath) + parents.set(key, hit) + } + // Paths are only resolvable for the root project's files; installed packages live elsewhere. + const local = (n: any) => !project || !n?.package_name || n.package_name === project + for (const node of nodes) { + const ofp: string | undefined = node?.original_file_path + if (!ofp || !local(node)) continue + const changed = owner(ofp) + if (!changed) continue + const base = changed.slice(0, changed.length - ofp.length) + for (const uid of asArray(node.depends_on?.nodes)) { + const parent = raw.nodes?.[uid] + if (parent?.original_file_path && ["model", "seed", "snapshot"].includes(parent.resource_type)) { + if (!local(parent)) continue + add(uid, { path: base + parent.original_file_path, kind: parent.resource_type, name: parent.name }, changed) + continue + } + const source = raw.sources?.[uid] + if (source && local(source)) { + const columns = Object.keys(source.columns ?? {}) + add(uid, { path: base + source.original_file_path, kind: "source", name: `${source.source_name}.${source.name}`, columns }, changed) + } + } + for (const uid of asArray(node.depends_on?.macros)) { + const macro = raw.macros?.[uid] + if (macro?.original_file_path && project && macro.package_name === project) + add(uid, { path: base + macro.original_file_path, kind: "macro", name: macro.name }, changed) + } + } + return [...parents.values()].filter((p) => !paths.includes(p.path)) + }, + async downstreamModels(model: string): Promise> { const m = await loadManifest() const node = m.byName.get(model) diff --git a/packages/opencode/src/altimate/review/select-list.ts b/packages/opencode/src/altimate/review/select-list.ts index a167dbebc1..4e414a85a8 100644 --- a/packages/opencode/src/altimate/review/select-list.ts +++ b/packages/opencode/src/altimate/review/select-list.ts @@ -62,17 +62,27 @@ const unquote = (s: string) => s.replace(/^["`\[]|["`\]]$/g, "") /** A top-level set operator (parenthesized groups are already blanked). */ const SET_OP = /\b(union|intersect|except|minus)\b(\s+(all|distinct))?\s*(\bselect\b|\()/i +/** + * Offset just past the `select` keyword of the final top-level query. For a + * set operation (`… union all select …`) that is the chain's first branch, + * which names the output columns. + */ +function finalSelectStart(flat: string): number | undefined { + const selects = [...flat.matchAll(/\bselect\b/gi)] + if (!selects.length) return undefined + const head = SET_OP.test(flat) ? selects[0] : selects[selects.length - 1] + return head.index! + "select".length +} + /** Output names of the final top-level SELECT, or null when unknown (`*`, unparseable). * A set operation (`UNION` …) takes its names from its first branch. Items it * cannot name contribute nothing, unless `complete` is set: then any unnamed * item makes the whole list unknown (null). */ export function selectListNames(sql: string, opts: { complete?: boolean } = {}): string[] | null { const flat = flatten(sql) - const selects = [...flat.matchAll(/\bselect\b/gi)] - if (!selects.length) return null - const head = SET_OP.test(flat) ? selects[0] : selects[selects.length - 1] - const start = head.index! + "select".length - const fromRel = flat.slice(start).search(/\bfrom\b/i) + const start = finalSelectStart(flat) + if (start === undefined) return null + const fromRel = flat.slice(start).search(/\b(from|union|intersect|except|minus)\b/i) const listFlat = flat.slice(start, fromRel < 0 ? undefined : start + fromRel) const listRaw = blankComments(sql).slice(start, fromRel < 0 ? undefined : start + fromRel) // Split on top-level commas (the flattened text has no nested commas). @@ -99,3 +109,37 @@ export function selectListNames(sql: string, opts: { complete?: boolean } = {}): } return names } + +/** + * Output names of the final SELECT, following `select * from ` to that + * CTE's own select list (dbt's `select * from final`). Null when unknown; + * `complete` is passed to `selectListNames`. + */ +export function finalSelectNames(sql: string, opts: { complete?: boolean } = {}): string[] | null { + // String contents blanked too, so a `')'` literal can't close a CTE body early. + const text = blankComments(sql).replace(/'(?:[^']|'')*'/g, (m) => "'" + "_".repeat(m.length - 2) + "'") + let query = sql + for (let hop = 0; hop < 5; hop++) { + const names = selectListNames(query, opts) + if (names) return names + const flat = flatten(query) + const start = finalSelectStart(flat) + if (start === undefined) return null + const tail = flat.slice(start) + const from = /^\s*\*\s+from\s+([A-Za-z_][A-Za-z0-9_]*)\s*(?:(?:where|order|limit|qualify)\b[\s\S]*)?;?\s*$/i.exec(tail) + if (!from) return null + // The CTE's body: the parenthesis after ` as`. + const def = new RegExp(`(?:\\bwith\\s+(?:recursive\\s+)?|,\\s*)${from[1]}\\s+as\\s*\\(`, "i").exec(text) + if (!def) return null + const open = def.index + def[0].length - 1 + let level = 0 + let close = -1 + for (let i = open; i < text.length && close < 0; i++) { + if (text[i] === "(") level++ + else if (text[i] === ")" && --level === 0) close = i + } + if (close < 0) return null + query = text.slice(open + 1, close) + } + return null +} diff --git a/packages/opencode/src/altimate/review/telemetry.ts b/packages/opencode/src/altimate/review/telemetry.ts index 0e6f4c0ad5..1b7d41f8dc 100644 --- a/packages/opencode/src/altimate/review/telemetry.ts +++ b/packages/opencode/src/altimate/review/telemetry.ts @@ -115,6 +115,11 @@ export function emitReviewRun(input: { unverified_models: env.scope?.unverified?.length ?? 0, ai_state: env.ai?.state, ai_duration_ms: env.ai?.durationMs, + ai_tokens_total: env.ai?.tokens ? env.ai.tokens.inputTotal + env.ai.tokens.output : undefined, + ai_tokens_input: env.ai?.tokens?.inputTotal, + ai_tokens_output: env.ai?.tokens?.output, + ai_cost: env.ai?.cost, + ai_calls: env.ai?.calls, base_source: env.scope?.baseSource, out_of_scope_files: env.scope?.outOfScopeFiles ?? 0, engine_errors: env.engineErrors?.reduce((n, e) => n + e.count, 0) ?? 0, diff --git a/packages/opencode/src/altimate/review/verdict.ts b/packages/opencode/src/altimate/review/verdict.ts index 2e8a4daf47..5a9b3f43f7 100644 --- a/packages/opencode/src/altimate/review/verdict.ts +++ b/packages/opencode/src/altimate/review/verdict.ts @@ -1,6 +1,6 @@ import z from "zod" import { createHmac, createHash } from "node:crypto" -import { Finding, type Severity, catalogRuleId, findingOrigin, normalizeRuleId } from "./finding" +import { Finding, Severity, catalogRuleId, findingOrigin, normalizeRuleId } from "./finding" import { Rubric, DEFAULT_RUBRIC, blockingCategories } from "./rubric" /** @@ -43,8 +43,10 @@ export const VCS_EVENT: Record = { /** Whether a finding may influence the verdict: deterministic engine * findings always; catalog (pattern) findings only when the rubric names the - * rule in `blockingRules`; advisory AI findings never. */ + * rule in `blockingRules`; advisory AI findings never. A finding on a line the + * PR did not add (`preExisting`) never does: the PR did not introduce it. */ export function verdictEligible(f: Finding, rubric: Rubric = DEFAULT_RUBRIC): boolean { + if (f.preExisting) return false const origin = findingOrigin(f) if (origin === "engine") return true if (origin === "ai") return false @@ -101,6 +103,22 @@ export const EngineVersions = z.object({ }) export type EngineVersions = z.infer +/** How findings reach the PR: at most `inlineBudget` new inline comments per + * run, none below `inlineSeverityFloor`, and no comment at all on a clean run + * when `silentWhenClean` is set. */ +export const PostingPolicy = z.object({ + inlineBudget: z.number().int().min(0), + inlineSeverityFloor: Severity, + silentWhenClean: z.boolean(), +}) +export type PostingPolicy = z.infer + +export const DEFAULT_POSTING_POLICY: PostingPolicy = { + inlineBudget: 10, + inlineSeverityFloor: "warning", + silentWhenClean: true, +} + /** What the review looked at: the resolved base, and files it deliberately * did not review. Signed with the rest of the envelope so a verdict computed * against the wrong base, or with models left unverified, is auditable. */ @@ -126,6 +144,10 @@ export const ReviewScope = z.object({ unverified: z .array(z.object({ path: z.string(), reason: z.string(), kind: z.enum(["failed", "abstained", "not_compiled"]).optional() })) .optional(), + /** Every changed file the review did not look at (excluded, outside the + * project, moved, not a dbt file, no check at this tier, no manifest), with + * the reason; also repeats the `unverified` entries. */ + notReviewed: z.array(z.object({ path: z.string(), reason: z.string() })).default([]), }) export type ReviewScope = z.infer @@ -174,6 +196,9 @@ export const VerdictEnvelope = z.object({ source: z.enum(["base", "working_tree", "default"]), hash: z.string(), changedInPr: z.array(z.string()).optional(), + /** Posting policy applied to this run (see PostingPolicy). Absent on + * envelopes from older releases; renderers then use the defaults. */ + posting: PostingPolicy.optional(), }) .optional(), /** Advisory AI lane outcome: ran, skipped, timed out or failed, and why. */ @@ -183,6 +208,19 @@ export const VerdictEnvelope = z.object({ reason: z.string().optional(), model: z.string().optional(), durationMs: z.number().int().nonnegative().optional(), + /** Tokens spent by the lane's model calls (`input` excludes cached tokens). */ + tokens: z + .object({ + input: z.number().nonnegative(), + inputTotal: z.number().nonnegative(), + output: z.number().nonnegative(), + reasoning: z.number().nonnegative(), + cacheRead: z.number().nonnegative(), + }) + .optional(), + /** Estimated USD cost from the model's price table. */ + cost: z.number().nonnegative().optional(), + calls: z.number().int().nonnegative().optional(), }) .optional(), /** ISO timestamp; injected by the caller (no clock access in pure code). */ @@ -246,7 +284,7 @@ export interface BuildEnvelopeInput { tierClassified?: RiskTier /** True when mtime signals the manifest predates a change-affecting file. */ staleManifest?: boolean - scope?: ReviewScope + scope?: z.input ai?: VerdictEnvelope["ai"] policy?: VerdictEnvelope["policy"] engineErrors?: VerdictEnvelope["engineErrors"] diff --git a/packages/opencode/src/altimate/telemetry/index.ts b/packages/opencode/src/altimate/telemetry/index.ts index 288b69319a..889a44835d 100644 --- a/packages/opencode/src/altimate/telemetry/index.ts +++ b/packages/opencode/src/altimate/telemetry/index.ts @@ -1605,6 +1605,14 @@ export namespace Telemetry { /** Advisory AI lane outcome: ok | skipped | timeout | error. */ ai_state?: string ai_duration_ms?: number + /** AI lane tokens: input (including cached) plus output. Output includes reasoning. */ + ai_tokens_total?: number + ai_tokens_input?: number + ai_tokens_output?: number + /** Estimated USD cost of the AI lane from the model's price table. */ + ai_cost?: number + /** Model calls the AI lane made. */ + ai_calls?: number /** Where the review base came from (flag, github_event, default_branch, …). */ base_source?: string out_of_scope_files?: number diff --git a/packages/opencode/src/altimate/tools/dbt-pr-review.ts b/packages/opencode/src/altimate/tools/dbt-pr-review.ts index 47d68a6549..82eb0d2640 100644 --- a/packages/opencode/src/altimate/tools/dbt-pr-review.ts +++ b/packages/opencode/src/altimate/tools/dbt-pr-review.ts @@ -77,7 +77,7 @@ export const DbtPrReviewTool = Tool.define("dbt_pr_review", { signature: env.signature, findingCount: env.findings.length, }, - output: renderSummary(env, { maxChars: Infinity }), + output: renderSummary(env, { maxChars: Infinity, inline: false, allowSilent: false }), } }, }) diff --git a/packages/opencode/src/cli/cmd/review.ts b/packages/opencode/src/cli/cmd/review.ts index b1677b7fe1..d1f58a3e65 100644 --- a/packages/opencode/src/cli/cmd/review.ts +++ b/packages/opencode/src/cli/cmd/review.ts @@ -154,7 +154,7 @@ export const ReviewCommand = cmd({ process.stdout.write(JSON.stringify(env, null, 2) + "\n") } else { // Local output is complete; the size budget applies to the posted comment. - process.stdout.write(renderSummary(env, { maxChars: Infinity }) + "\n") + process.stdout.write(renderSummary(env, { maxChars: Infinity, inline: false, allowSilent: false }) + "\n") } if (args.post) { @@ -194,7 +194,11 @@ export const ReviewCommand = cmd({ `Posted review to ${where}` + (r.inlineFellBack ? " (inline comments fell back to summary-only)" : "") + (r.inlineSkippedAsPosted ? ` (${r.inlineSkippedAsPosted} inline already on the PR)` : "") + - (r.reviewSkipped ? " (summary updated; no new review needed)" : "") + + (r.silent + ? " (clean run: nothing to post)" + : r.reviewSkipped + ? " (summary updated; no new review needed)" + : "") + (r.dismissedReviews ? ` (dismissed ${r.dismissedReviews} earlier blocking review(s))` : ""), ) } diff --git a/packages/opencode/test/altimate/review-ai-inputs.test.ts b/packages/opencode/test/altimate/review-ai-inputs.test.ts index 0778d617f9..d3818c5116 100644 --- a/packages/opencode/test/altimate/review-ai-inputs.test.ts +++ b/packages/opencode/test/altimate/review-ai-inputs.test.ts @@ -1,8 +1,27 @@ -import { describe, test, expect } from "bun:test" +import { describe, test, expect, spyOn, mock, afterEach } from "bun:test" import { runReview, type ReviewRunner } from "../../src/altimate/review/orchestrate" import { DEFAULT_REVIEW_CONFIG } from "../../src/altimate/review/config" import { DEFAULT_RUBRIC } from "../../src/altimate/review/rubric" -import { buildUserMessage, selectAiFiles, type AiReviewFile, type AiReviewInput } from "../../src/altimate/review/ai-review" +import { + addUsage, + aiInputBudget, + batchAiFiles, + buildUserMessage, + laneUsage, + selectAiFiles, + streamUsage, + type AiLaneStatus, + type AiReviewFile, + type AiReviewInput, +} from "../../src/altimate/review/ai-review" +import { inlineComments, renderSummary } from "../../src/altimate/review/format" +import { makeFinding } from "../../src/altimate/review/finding" +import { emitReviewRun } from "../../src/altimate/review/telemetry" +import { Telemetry } from "../../src/altimate/telemetry" +import { createDispatcherRunner } from "../../src/altimate/review/runner" +import { writeFileSync } from "node:fs" +import path from "node:path" +import { tmpdir } from "../fixture/fixture" import type { ChangedFile } from "../../src/altimate/review/diff-filter" /** @@ -86,6 +105,75 @@ describe("files sent to the advisory AI lane", () => { expect(JSON.stringify(input)).not.toContain("hunter2") }) + test("an explicit summary tier keeps an AI finding out of the inline comments", async () => { + const run = (rules: Record) => + runReview({ + changedFiles: [mod("models/marts/m.sql", "@@ -0,0 +1 @@\n+select 2 as id\n")], + config: { ...DEFAULT_REVIEW_CONFIG, rules }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + getContent: async (f, side) => (side === "old" ? undefined : FILES[f]), + generatedAt: "2026-11-14T00:00:00Z", + forceTier: "full", + aiReview: async () => ({ + findings: [ + makeFinding({ + severity: "warning", + category: "sql_quality", + title: "m: ai finding", + body: "why", + file: "models/marts/m.sql", + model: "m", + startLine: 1, + rule: "ai-review:sql-quality", + evidence: { tool: "ai-review", result: {} }, + }), + ], + status: { state: "ok" }, + }), + }) + const byDefault = await run({}) + expect(byDefault.findings.find((f) => f.rule === "ai-review:sql-quality")?.tier).toBe("inline") + expect(inlineComments(byDefault).comments).toHaveLength(1) + const summary = await run({ "ai-review:*": "summary" }) + expect(summary.findings.find((f) => f.rule === "ai-review:sql-quality")?.tier).toBe("summary") + expect(inlineComments(summary).comments).toHaveLength(0) + }) + + test("files past the AI file cap are not marked reviewed in the ledger", async () => { + const seeds = Array.from({ length: 21 }, (_, i) => mod(`seeds/s${String(i).padStart(2, "0")}.csv`, "+a,b\n")) + const env = await runReview({ + changedFiles: seeds, + config: { ...DEFAULT_REVIEW_CONFIG }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + getContent: async () => undefined, + generatedAt: "2026-11-14T00:00:00Z", + forceTier: "full", + aiReview: async () => ({ findings: [], status: { state: "ok" } }), + }) + expect(env.scope?.notReviewed?.map((e) => e.path)).toEqual(["seeds/s20.csv"]) + expect(renderSummary(env)).toContain("Not reviewed: 1 file(s)") + }) + + test("a partial AI failure is shown even when the lane state is ok", async () => { + const env = await runReview({ + changedFiles: [mod("models/marts/m.sql")], + config: { ...DEFAULT_REVIEW_CONFIG }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + getContent: async (f, side) => (side === "old" ? undefined : FILES[f]), + generatedAt: "2026-11-14T00:00:00Z", + forceTier: "full", + aiReview: async () => ({ findings: [], status: { state: "ok", reason: "1 of 2 calls failed: timed out" } }), + }) + const md = renderSummary(env) + expect(md).toContain("Advisory AI review partly failed: 1 of 2 calls failed: timed out.") + }) + test("deleted files are not sent", async () => { const input = await aiInputFor([mod("models/marts/m.sql"), { path: "macros/old.sql", status: "deleted", diff: "-x\n" }]) expect(input?.files.some((f) => f.path === "macros/old.sql")).toBe(false) @@ -123,3 +211,194 @@ describe("selection and prompt layout", () => { expect(msg).toContain("### models/a.sql (modified)\nCurrent SQL:\n```sql") }) }) + + +describe("input budget: more of the PR, split across calls instead of cut", () => { + const file = (p: string, n: number): AiReviewFile => ({ path: p, status: "modified", model: p, sql: "x".repeat(n) }) + + test("defaults keep one bounded call; the env widens files, length and splits calls", () => { + expect(aiInputBudget({})).toEqual({ maxFiles: 20, maxFileChars: 6_000, batchChars: Infinity }) + expect( + aiInputBudget({ ALTIMATE_REVIEW_AI_MAX_FILES: "200", ALTIMATE_REVIEW_AI_MAX_FILE_CHARS: "40000", ALTIMATE_REVIEW_AI_BATCH_CHARS: "60000" }), + ).toEqual({ maxFiles: 200, maxFileChars: 40_000, batchChars: 60_000 }) + expect(aiInputBudget({ ALTIMATE_REVIEW_AI_MAX_FILES: "nope" }).maxFiles).toBe(20) + }) + + test("a file longer than the default limit is sent whole under a wider limit", () => { + const f = file("models/big.sql", 10_000) + expect(buildUserMessage({ grounding: [], files: [f] })).toContain("… (truncated)") + expect(buildUserMessage({ grounding: [], files: [f] }, 40_000)).not.toContain("… (truncated)") + }) + + test("files are grouped in order under the per-call size; an oversized file goes alone", () => { + const budget = { maxFiles: 100, maxFileChars: 40_000, batchChars: 25_000 } + const batches = batchAiFiles([file("a", 10_000), file("b", 10_000), file("c", 10_000), file("d", 30_000), file("e", 1_000)], budget) + expect(batches.map((b) => b.map((f) => f.path))).toEqual([["a", "b"], ["c"], ["d"], ["e"]]) + expect(batchAiFiles([file("a", 10), file("b", 10)], { ...budget, batchChars: Infinity })).toHaveLength(1) + }) +}) + +describe("graph context: the changed files' unchanged dbt parents", () => { + test("the related section lists only parents of the files in this call", () => { + const msg = buildUserMessage({ + grounding: [], + files: [{ path: "models/fct.sql", status: "modified", model: "fct", sql: "select 1" }], + context: [ + { path: "models/stg.sql", kind: "model", name: "stg", forPaths: ["models/fct.sql"], content: "select id, closed_at from raw" }, + { path: "seeds/codes.csv", kind: "seed", name: "codes", forPaths: ["models/other.sql"], content: "code\nUS" }, + ], + }) + expect(msg).toContain("## RELATED FILES") + expect(msg).toContain("### models/stg.sql (model `stg`, used by models/fct.sql)") + expect(msg).not.toContain("seeds/codes.csv") + expect(buildUserMessage({ grounding: [], files: [{ path: "models/fct.sql", status: "modified", model: "fct" }] })).not.toContain( + "RELATED FILES", + ) + }) + + test("graphParents: models, seeds, sources (with columns) and project macros; a yml maps through its tests", async () => { + await using tmp = await tmpdir() + const manifestPath = path.join(tmp.path, "manifest.json") + writeFileSync( + manifestPath, + JSON.stringify({ + metadata: { project_name: "shop" }, + nodes: { + "model.shop.fct": { + resource_type: "model", name: "fct", original_file_path: "models/fct.sql", + depends_on: { nodes: ["model.shop.stg", "seed.shop.codes", "source.shop.raw.orders", "model.pkg.calendar", "source.pkg.ext.events"], macros: ["macro.shop.cents", "macro.dbt_utils.star"] }, + }, + "model.shop.stg": { resource_type: "model", name: "stg", original_file_path: "models/stg.sql", depends_on: { nodes: [] } }, + // Installed-package nodes: their paths are relative to the package, not the project. + "model.pkg.calendar": { resource_type: "model", name: "calendar", package_name: "pkg", original_file_path: "models/calendar.sql", depends_on: { nodes: [] } }, + "seed.shop.codes": { resource_type: "seed", name: "codes", original_file_path: "seeds/codes.csv", depends_on: { nodes: [] } }, + "test.shop.unique_stg_id": { + resource_type: "test", name: "unique_stg_id", original_file_path: "models/_schema.yml", depends_on: { nodes: ["model.shop.stg"] }, + }, + }, + sources: { + "source.pkg.ext.events": { source_name: "ext", name: "events", package_name: "pkg", original_file_path: "models/src.yml" }, + "source.shop.raw.orders": { source_name: "raw", name: "orders", original_file_path: "models/_sources.yml", columns: { id: {}, status: {} } }, + }, + macros: { + "macro.shop.cents": { name: "cents", package_name: "shop", original_file_path: "macros/cents.sql" }, + "macro.dbt_utils.star": { name: "star", package_name: "dbt_utils", original_file_path: "macros/sql/star.sql" }, + }, + }), + ) + const runner = createDispatcherRunner({ manifestPath }) + const parents = await runner.graphParents!(["proj/models/fct.sql", "proj/models/_schema.yml"]) + const byName = Object.fromEntries(parents.map((p) => [p.name, p])) + expect(Object.keys(byName).sort()).toEqual(["cents", "codes", "raw.orders", "stg"]) + expect(byName.stg.path).toBe("proj/models/stg.sql") + expect(byName.stg.forPaths.sort()).toEqual(["proj/models/_schema.yml", "proj/models/fct.sql"]) + expect(byName["raw.orders"].columns).toEqual(["id", "status"]) + expect(byName.codes.kind).toBe("seed") + }) + + test("the orchestrator sends parents only when graph context is on", async () => { + let seen: AiReviewInput | undefined + const withGraph = { + ...runner, + async graphParents() { + return [{ path: "models/stg.sql", kind: "model", name: "stg", forPaths: ["models/marts/m.sql"] }] + }, + } as ReviewRunner + const run = (aiGraphContext: boolean) => + runReview({ + changedFiles: [mod("models/marts/m.sql")], + config: { ...DEFAULT_REVIEW_CONFIG }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner: withGraph, + aiGraphContext, + forceTier: "full", + getContent: async (f, side) => (side === "old" ? undefined : f === "models/stg.sql" ? "select id from raw" : FILES[f]), + generatedAt: "2026-11-14T00:00:00Z", + aiReview: async (input) => { + seen = input + return { findings: [], status: { state: "ok" } } + }, + }) + await run(false) + expect(seen?.context).toBeUndefined() + await run(true) + expect(seen?.context?.map((c) => [c.path, c.content])).toEqual([["models/stg.sql", "select id from raw"]]) + }) +}) + +describe("token and cost accounting", () => { + afterEach(() => mock.restore()) + + const model = { api: { npm: "@ai-sdk/openai" }, cost: { input: 1, output: 4, cache: { read: 0.5, write: 0 } } } as any + const fakeStream = (usage: unknown, metadata: unknown = undefined) => ({ + totalUsage: Promise.resolve(usage), + providerMetadata: Promise.resolve(metadata), + }) + + test("a drained stream's usage becomes tokens and cost with the session's rules", async () => { + const u = await streamUsage( + fakeStream({ inputTokens: 1_000, outputTokens: 200, totalTokens: 1_200, reasoningTokens: 50, cachedInputTokens: 400 }), + model, + ) + // OpenAI-style input includes cached tokens: input = 1000 - 400. + expect(u!.tokens).toEqual({ input: 600, inputTotal: 1_000, output: 200, reasoning: 50, cacheRead: 400 }) + // (600·1 + 200·4 + 400·0.5 + 50·4) / 1e6 + expect(u!.cost).toBeCloseTo(0.0018, 10) + }) + + test("missing or failing usage never fails the lane", async () => { + expect(await streamUsage(fakeStream(undefined), model)).toBeUndefined() + const rejected = { totalUsage: Promise.reject(new Error("no usage")), providerMetadata: Promise.resolve(undefined) } + expect(await streamUsage(rejected, model)).toBeUndefined() + }) + + test("batches sum; calls count every batch, even one without usage", () => { + const a = { tokens: { input: 1, inputTotal: 2, output: 3, reasoning: 0, cacheRead: 1 }, cost: 0.1 } + const b = { tokens: { input: 10, inputTotal: 20, output: 30, reasoning: 5, cacheRead: 10 }, cost: 0.2 } + const sum = addUsage(a, b) + expect(sum.tokens).toEqual({ input: 11, inputTotal: 22, output: 33, reasoning: 5, cacheRead: 11 }) + expect(sum.cost).toBeCloseTo(0.3, 10) + const lane = laneUsage([{ usage: a }, { usage: b }, {}]) + expect(lane.calls).toBe(3) + expect(lane.tokens).toEqual({ input: 11, inputTotal: 22, output: 33, reasoning: 5, cacheRead: 11 }) + expect(laneUsage([{}, {}])).toEqual({ calls: 2 }) + }) + + test("the lane's tokens reach the envelope, the footer and the review_run event", async () => { + const status: AiLaneStatus = { + state: "ok", + model: "anthropic/claude", + tokens: { input: 900, inputTotal: 1_500, output: 300, reasoning: 0, cacheRead: 600 }, + cost: 0.0123, + calls: 2, + } + const env = await runReview({ + changedFiles: [mod("models/marts/m.sql")], + config: { ...DEFAULT_REVIEW_CONFIG }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + getContent: async (f, side) => (side === "old" ? undefined : FILES[f]), + generatedAt: "2026-11-14T00:00:00Z", + forceTier: "full", + aiReview: async () => ({ findings: [], status }), + }) + expect(env.ai).toMatchObject({ tokens: status.tokens, cost: 0.0123, calls: 2 }) + expect(renderSummary(env, { allowSilent: false })).toContain("· tokens 1,800") + + const events: Telemetry.Event[] = [] + spyOn(Telemetry, "track").mockImplementation((e: Telemetry.Event) => { + events.push(e) + }) + emitReviewRun({ invocation: "cli", durationMs: 1, sessionID: "", envelope: env }) + expect(events[0]).toMatchObject({ + type: "review_run", + ai_tokens_total: 1_800, + ai_tokens_input: 1_500, + ai_tokens_output: 300, + ai_cost: 0.0123, + ai_calls: 2, + }) + }) +}) diff --git a/packages/opencode/test/altimate/review-ci-honesty.test.ts b/packages/opencode/test/altimate/review-ci-honesty.test.ts index 3901b47612..fe09bad3d0 100644 --- a/packages/opencode/test/altimate/review-ci-honesty.test.ts +++ b/packages/opencode/test/altimate/review-ci-honesty.test.ts @@ -9,7 +9,7 @@ import { makeFinding } from "../../src/altimate/review/finding" import { aiTimeoutMs, safeReason } from "../../src/altimate/review/ai-review" import { coverageNotes, renderSummary } from "../../src/altimate/review/format" import { prContextFromEnv } from "../../src/altimate/review/run" -import { verifyEnvelope } from "../../src/altimate/review/verdict" +import { buildEnvelope, signEnvelope, verifyEnvelope, VerdictEnvelope } from "../../src/altimate/review/verdict" /** * The PR comment must be honest about what was checked: a manifest-backed run @@ -54,13 +54,12 @@ function review(extra: Partial[0]> = {}) { } describe("lint-only banner", () => { - test("an undecidable finding on a manifest-backed run does not flip the run to lint-only", async () => { + test("undecidable equivalence on a manifest-backed run goes to the ledger, not lint-only", async () => { const env = await review() - expect(env.findings.some((f) => f.degraded)).toBe(true) + expect(env.findings.some((f) => f.rule === "altimate-core.equivalence:undecided")).toBe(false) expect(env.summary.degraded).toBe(false) - const md = renderSummary(env) - expect(md).not.toContain("Lint-only run") - expect(md).toContain("could not be decided by the engine") + expect(env.scope?.notReviewed?.some((n) => n.reason.startsWith("equivalence could not be proven"))).toBe(true) + expect(renderSummary(env)).not.toContain("Lint-only run") }) test("no manifest for the changed models is still lint-only", async () => { @@ -237,3 +236,147 @@ describe("unverified models are listed per reason", () => { expect(notes.find((n) => n.includes("models/a.sql"))).not.toContain("models/b.sql") }) }) + +describe("silent clean runs", () => { + const clean = (silentWhenClean?: boolean) => + buildEnvelope({ + findings: [], + tier: "lite", + mode: "comment", + policy: + silentWhenClean === undefined + ? undefined + : { source: "base", hash: "h", posting: { inlineBudget: 10, inlineSeverityFloor: "warning", silentWhenClean } }, + }) + + test("a clean run renders nothing by default and under silentWhenClean", () => { + expect(renderSummary(clean())).toBe("") + expect(renderSummary(clean(true))).toBe("") + }) + + test("without silentWhenClean a clean run keeps the old text", () => { + expect(renderSummary(clean(false))).toContain("No issues found in the changed dbt models.") + }) + + test("a clean run with something to report about coverage is not silent", () => { + const env = buildEnvelope({ + findings: [], + tier: "lite", + mode: "comment", + ai: { state: "timeout", reason: "no answer within 75 s" }, + }) + expect(renderSummary(env)).toContain("Advisory AI review timed out") + }) + + test("the posting policy is part of the signed envelope", () => { + const quiet = signEnvelope(clean(true), "k") + expect(quiet.policy?.posting?.silentWhenClean).toBe(true) + expect(verifyEnvelope(quiet, "k")).toBe(true) + const tampered = { ...quiet, policy: { ...quiet.policy!, posting: { ...quiet.policy!.posting!, inlineBudget: 50 } } } + expect(verifyEnvelope(tampered, "k")).toBe(false) + }) + + test("rendering does not change the envelope", () => { + const env = clean(true) + const before = JSON.stringify(env) + renderSummary(env) + expect(JSON.stringify(env)).toBe(before) + }) +}) + +describe("coverage ledger: what the review did not look at", () => { + const seedRunner = { ...runner, async manifestAvailable() { return true } } as ReviewRunner + + test("excluded, non-dbt, out-of-root and untouched files are listed with their reasons", async () => { + const env = await runReview({ + changedFiles: [ + { path: "models/legacy/old.sql", status: "modified", diff: "+select 1\n" }, + { path: "README.md", status: "modified", diff: "+docs\n" }, + { path: "seeds/countries.csv", status: "modified", diff: "+fr,France\n" }, + { path: "analyses/explore.sql", status: "modified", diff: "+select 1\n" }, + ], + config: DEFAULT_REVIEW_CONFIG, + rubric: { ...DEFAULT_RUBRIC, exclusions: { ...DEFAULT_RUBRIC.exclusions, excludeGlobs: ["models/legacy/**"] } }, + mode: "comment", + runner: seedRunner, + getContent: content, + forceTier: "trivial", + scope: { notReviewed: [{ path: "other_project/models/x.sql", reason: "outside the dbt project (analytics)" }] }, + }) + const reasons = Object.fromEntries(env.scope!.notReviewed.map((e) => [e.path, e.reason])) + expect(reasons).toEqual({ + "other_project/models/x.sql": "outside the dbt project (analytics)", + "models/legacy/old.sql": "excluded by glob models/legacy/**", + "README.md": "not a dbt file (extension or generated path)", + "analyses/explore.sql": "not a dbt model/macro/test/yml/seed file", + "seeds/countries.csv": "no checks run for seed files at the trivial tier", + }) + const md = renderSummary(env) + expect(md).toContain("
Not reviewed: 5 file(s)") + expect(md).toContain("excluded by glob models/legacy/\\*\\* (1):") + expect(md).toContain("- `README.md`") + }) + + test("a run without a manifest says so for each model", async () => { + const env = await review({ + runner: { + ...runner, + async manifestAvailable() { + return false + }, + async impact() { + return { hasManifest: false, severity: "SAFE", directCount: 0, transitiveCount: 0, testCount: 0 } + }, + } as ReviewRunner, + }) + expect(env.scope!.notReviewed).toContainEqual({ + path: "models/marts/m.sql", + reason: "no dbt manifest: lineage, config and graph checks skipped", + }) + }) + + test("no manifest note when the runner cannot tell", async () => { + const env = await review() + expect((env.scope?.notReviewed ?? []).filter((n) => n.reason.includes("manifest"))).toEqual([]) + }) + + test("the ledger replaces the count lines and caps the list", () => { + const paths = Array.from({ length: 40 }, (_, i) => `other/f${i}.sql`) + const env = { + ...signEnvelope(buildEnvelope({ findings: [], tier: "lite", mode: "comment" })), + scope: { + outOfScopeFiles: 40, + notReviewed: paths.map((path) => ({ path, reason: "outside the dbt project (.)" })), + }, + } + const notes = coverageNotes(env).join("\n") + expect(notes).not.toContain("changed file(s) outside the dbt project were not reviewed") + expect(notes).toContain("Not reviewed: 40 file(s)") + expect(notes).toContain("- `other/f29.sql`") + expect(notes).not.toContain("- `other/f30.sql`") + expect(notes).toContain("… and 10 more") + }) + + test("moved files in the ledger keep the folder-level config hint", () => { + const env = { + ...signEnvelope(buildEnvelope({ findings: [], tier: "lite", mode: "comment" })), + scope: { + movedUnchangedFiles: 1, + notReviewed: [{ path: "models/new_home/m.sql", reason: "moved without edits; not re-reviewed" }], + }, + } + const notes = coverageNotes(env).join("\n") + expect(notes).not.toContain("their SQL was not re-reviewed") + expect(notes).toContain("moved without edits; not re-reviewed (1):\n- `models/new_home/m.sql`\n\nFolder-level config") + expect(notes).toContain("`+schema`, `+materialized`") + }) + + test("envelopes from older releases without a ledger still parse and keep the count line", () => { + const env = VerdictEnvelope.parse({ + ...buildEnvelope({ findings: [], tier: "lite", mode: "comment" }), + scope: { outOfScopeFiles: 2 }, + }) + expect(env.scope!.notReviewed).toEqual([]) + expect(coverageNotes(env).join("\n")).toContain("2 changed file(s) outside the dbt project were not reviewed") + }) +}) diff --git a/packages/opencode/test/altimate/review-contracts-dialect.test.ts b/packages/opencode/test/altimate/review-contracts-dialect.test.ts index 0dc20cdeb5..aabe0177fb 100644 --- a/packages/opencode/test/altimate/review-contracts-dialect.test.ts +++ b/packages/opencode/test/altimate/review-contracts-dialect.test.ts @@ -5,7 +5,16 @@ import { tmpdir } from "node:os" import { execFile } from "node:child_process" import { promisify } from "node:util" import { evaluateCatalog } from "../../src/altimate/review/rule-catalog" -import { isCatalogPortability, runReview, ymlModelColumns, type ReviewRunner } from "../../src/altimate/review/orchestrate" +import { + anchorToDiff, + isCatalogPortability, + removedColumnAnchor, + runReview, + ymlModelColumns, + type ReviewRunner, +} from "../../src/altimate/review/orchestrate" +import { inlineComments } from "../../src/altimate/review/format" +import { buildEnvelope, verdictEligible } from "../../src/altimate/review/verdict" import { DEFAULT_REVIEW_CONFIG } from "../../src/altimate/review/config" import { DEFAULT_RUBRIC } from "../../src/altimate/review/rubric" import { extractColumnList, extractColumnLists, listMirrorsModel, loadConsumerLists } from "../../src/altimate/review/consumer-lists" @@ -145,10 +154,10 @@ describe("consumers outside the DAG", () => { expect(plain.findings.some((x) => x.title.includes("declared consumers"))).toBe(false) }) - test("nothing declared: a final-layer model still gets a suggestion, an intermediate model does not", async () => { + test("nothing declared: a final-layer model still gets a warning, an intermediate model does not", async () => { const none = { exposures: [], contractEnforced: false, contractColumns: [] } const mart = (await review(none)).findings.find((x) => x.title.includes("final-layer")) - expect(mart?.severity).toBe("suggestion") + expect(mart?.severity).toBe("warning") expect(mart?.body).toContain("`email`") const inter = await review(none, { changedFiles: [{ ...changed[0], path: "models/intermediate/int_x.sql" }], @@ -160,6 +169,21 @@ describe("consumers outside the DAG", () => { expect(nested.findings.some((x) => x.title.includes("final-layer"))).toBe(false) }) + test("final-layer `select * from final`: a renamed alias in the CTE is a removed column", async () => { + const none = { exposures: [], contractEnforced: false, contractColumns: [] } + const model = (alias: string) => + `with payments as (\n select customer_id, amount from src\n),\nfinal as (\n select customer_id, ${alias}\n from payments\n)\nselect * from final` + const before = model("sum(amount) as customer_lifetime_value") + // Lineage that only traces plain columns, as the engine does without a schema. + const runner = { ...fakeRunner(none), columnLineage: async () => [{ source: '"src"."customer_id"', target: "customer_id" }] } as ReviewRunner + const run = (after: string) => + review(none, { runner, getContent: async (_f, side) => (side === "old" ? before : after), getCompiled: async (_f, side) => (side === "old" ? before : after) }) + const renamed = (await run(model("sum(amount) as lifetime_value"))).findings.find((x) => x.rule === "dbt.manifest:removed-final-layer-columns") + expect(renamed?.body).toContain("`customer_lifetime_value`") + const kept = await run(model("coalesce(sum(amount), 0) as customer_lifetime_value")) + expect(kept.findings.some((x) => x.rule === "dbt.manifest:removed-final-layer-columns")).toBe(false) + }) + const optIn = { config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["lineage_breakage", "contract_violation"], checkContractColumns: true } } test("opt-in: a new column missing from the enforced contract is reported, and never blocks", async () => { @@ -210,6 +234,103 @@ describe("consumers outside the DAG", () => { }) }) +describe("removed-column findings anchor where the column was removed", () => { + const hunk = (start: number, rows: string[]) => `@@ -${start},${rows.length} +${start},${rows.length} @@\n` + rows.join("\n") + "\n" + + test("rename: the added line that replaced the removed one in the same change block", () => { + const diff = hunk(10, [ + " select", + " s.first_name,", + "- c.mclass_period,", + "- c.mclass_measure_year_growth,", + "+ c.period,", + "+ c.measure_year_growth,", + " from s", + ]) + expect(removedColumnAnchor(diff, ["mclass_measure_year_growth"])).toEqual({ startLine: 13, endLine: 13, deletionSite: true }) + expect(removedColumnAnchor(diff, ["mclass_period"]).startLine).toBe(12) + // An alias rename: `expr as old` -> `expr as new`. + const alias = hunk(4, [" select", "- sum(amount) as customer_lifetime_value", "+ sum(amount) as lifetime_value", " from p"]) + expect(removedColumnAnchor(alias, ["customer_lifetime_value"]).startLine).toBe(5) + }) + + test("the select-list line that defined the column wins over a later mention", () => { + const diff = hunk(1, [ + " select", + "- platform", + "+ platform_fk", + " , user_id", + " from t", + "-group by platform, user_id", + "+group by platform_fk, user_id", + ]) + expect(removedColumnAnchor(diff, ["platform"]).startLine).toBe(2) + }) + + test("pure deletion: the next head line in the hunk, marked as a deletion site", () => { + const diff = hunk(5, [" id,", "- email,", " amount", " from src"]) + expect(removedColumnAnchor(diff, ["email"])).toEqual({ startLine: 6, endLine: 6, deletionSite: true }) + // A deletion at the end of a hunk falls back to the line before it. + const tail = hunk(5, [" id,", " amount,", "- email"]) + expect(removedColumnAnchor(tail, ["email"]).startLine).toBe(6) + }) + + test("multi-hunk: the anchor is in the hunk that removed the column; the first removed column found wins", () => { + const diff = + hunk(1, [" with a as (", "- select x from t", "+ select y from t", " )"]) + + hunk(20, [" select", " id,", "- email,", " amount", " from a"]) + expect(removedColumnAnchor(diff, ["email"]).startLine).toBe(22) + expect(removedColumnAnchor(diff, ["not_in_diff", "email", "x"]).startLine).toBe(22) + expect(removedColumnAnchor(diff, ["x"]).startLine).toBe(2) + }) + + test("a column the diff never removes, or a diff without hunks, stays unanchored", () => { + const diff = hunk(1, [" select", "- a,", "+ b,", " from t"]) + expect(removedColumnAnchor(diff, ["c"])).toEqual({}) + expect(removedColumnAnchor(diff, ["ab"])).toEqual({}) + expect(removedColumnAnchor("-select a from t\n+select b from t\n", ["a"])).toEqual({}) + expect(removedColumnAnchor(undefined, ["a"])).toEqual({}) + }) + + test("a deletion site on a context line is the PR's change: not pre-existing, inline-eligible, verdict-eligible", () => { + const diff = hunk(5, [" id,", "- email,", " amount", " from src"]) + const file = { path: "models/marts/fct.sql", status: "modified" as const, diff } + const base = { + id: "f1", + severity: "warning" as const, + category: "lineage_breakage" as const, + title: "fct: removed column(s) on a model with declared consumers", + body: "b", + file: file.path, + confidence: "high" as const, + degraded: false, + tier: "inline" as const, + evidence: { tool: "dbt.manifest", result: { removed: ["email"] } }, + } + const [site] = anchorToDiff([{ ...base, ...removedColumnAnchor(diff, ["email"]) }], [file]) + expect(site.startLine).toBe(6) + expect(site.preExisting).toBeUndefined() + expect(verdictEligible(site)).toBe(true) + expect(inlineComments(buildEnvelope({ findings: [site], tier: "lite", mode: "comment" })).comments.map((c) => c.line)).toEqual([6]) + // The same context line without the marker is pre-existing. + const [plain] = anchorToDiff([{ ...base, startLine: 6, endLine: 6 }], [file]) + expect(plain.preExisting).toBe(true) + }) + + test("end to end: both removed-column rules carry the anchor", async () => { + const diff = hunk(1, ["-select id, email, amount from src", "+select id, amount, region from src"]) + const files = [{ ...changed[0], diff }] + const declared = await review({ exposures: [], contractEnforced: true, contractColumns: ["id", "amount", "region"] }, { changedFiles: files }) + const d = declared.findings.find((x) => x.rule === "dbt.manifest:removed-columns-declared-consumers") + expect(d?.startLine).toBe(1) + expect(d?.preExisting).toBeUndefined() + const none = await review({ exposures: [], contractEnforced: false, contractColumns: [] }, { changedFiles: files }) + const f = none.findings.find((x) => x.rule === "dbt.manifest:removed-final-layer-columns") + expect(f?.startLine).toBe(1) + expect(f?.body).toContain("`email`") + }) +}) + describe("opt-in consumer column lists", () => { test("lists are extracted from the capture group; models matched by name", () => { const py = 'MART_COLUMNS = [\n "id",\n "amount",\n \'email\',\n]\nTABLE = "fct"\n' diff --git a/packages/opencode/test/altimate/review-failures-gate.test.ts b/packages/opencode/test/altimate/review-failures-gate.test.ts index dac4afa702..ebb63b87f0 100644 --- a/packages/opencode/test/altimate/review-failures-gate.test.ts +++ b/packages/opencode/test/altimate/review-failures-gate.test.ts @@ -244,6 +244,7 @@ describe("posting: dedup, dismissal of a stale block, no repeat reviews", () => startLine: line, evidence: { tool: "altimate_core.check", result: {} }, ruleKey: "q:select-star", + tier: "inline", }) test("a clean rerun dismisses the earlier blocking altimate review", async () => { @@ -371,6 +372,28 @@ describe("posting: dedup, dismissal of a stale block, no repeat reviews", () => expect(calls.dismissReview).toHaveLength(0) }) + test("a rejected inline review rewrites the summary with every finding in full", async () => { + const f = { ...engineFinding(3), title: "m: code marked temporary", body: "remove the debug sample before merge" } + const env = buildEnvelope({ findings: [f], tier: "lite", mode: "comment" }) + const { client, calls } = fakeClient({}) + let attempt = 0 + ;(client.rest.pulls as any).createReview = async (a: any) => { + calls.createReview.push(a) + if (attempt++ === 0) + throw Object.assign(new Error("Unprocessable Entity"), { status: 422, response: { data: { errors: ["line must be part of the diff"] } } }) + return { data: { id: 99 } } + } + const r = await postGitHubReview(env, target, client) + expect(r.inlineFellBack).toBe(true) + expect(calls.createReview[1].comments).toBeUndefined() + // First post: a one-line inline pointer. After the fallback: the full body. + expect(calls.createComment[0].body).toContain("### Inline (1)") + expect(calls.createComment[0].body).not.toContain("remove the debug sample before merge") + expect(calls.updateComment).toHaveLength(1) + expect(calls.updateComment[0].body).not.toContain("### Inline") + expect(calls.updateComment[0].body).toContain("remove the debug sample before merge") + }) + test("a still-blocking rerun does not dismiss and posts REQUEST_CHANGES", async () => { const blocking = makeFinding({ severity: "critical", @@ -403,4 +426,12 @@ describe("posting: dedup, dismissal of a stale block, no repeat reviews", () => expect(calls.createReview).toHaveLength(0) expect(calls.createComment.length + calls.updateComment.length).toBe(1) }) + + test("a silent clean run makes no write calls", async () => { + const env = buildEnvelope({ findings: [], tier: "lite", mode: "comment" }) + const { client, calls } = fakeClient({}) + const r = await postGitHubReview(env, target, client) + expect(r.silent).toBe(true) + expect(Object.values(calls).flat()).toHaveLength(0) + }) }) diff --git a/packages/opencode/test/altimate/review-noise.test.ts b/packages/opencode/test/altimate/review-noise.test.ts index 51a02cdb70..25ba605ae6 100644 --- a/packages/opencode/test/altimate/review-noise.test.ts +++ b/packages/opencode/test/altimate/review-noise.test.ts @@ -1,16 +1,22 @@ import { describe, test, expect } from "bun:test" import { + anchorByEvidence, anchorToDiff, commentableLines, + scanDiffLines, + walkDiffLines, isCatalogPortability, isSuppressiblePortability, runReview, + validateSuggestions, type ReviewRunner, } from "../../src/altimate/review/orchestrate" import { DEFAULT_REVIEW_CONFIG } from "../../src/altimate/review/config" import { DEFAULT_RUBRIC } from "../../src/altimate/review/rubric" import { makeFinding, mergeRepeats, type Finding } from "../../src/altimate/review/finding" import { + BODY_MAX_CHARS, + clampBody, findingMarker, inlineComments, postedFindingIds, @@ -19,7 +25,9 @@ import { } from "../../src/altimate/review/format" import { buildEnvelope } from "../../src/altimate/review/verdict" import { evaluateCatalog } from "../../src/altimate/review/rule-catalog" +import { detectModelPatterns } from "../../src/altimate/review/dbt-patterns" import { classifyPostOutcome } from "../../src/altimate/review/telemetry" +import { DEFAULT_RULE_TIERS, ruleTier, type RuleTier } from "../../src/altimate/review/rule-tiers" /** * The PR comment must stay readable: inline comments land on real lines of the @@ -112,20 +120,20 @@ describe("inline anchors", () => { describe("inline dedup across pushes", () => { test("inline bodies carry the finding fingerprint, which ignores the line", () => { - const a = finding({ startLine: 3 }) - const moved = finding({ startLine: 9 }) + const a = finding({ startLine: 3, severity: "warning", tier: "inline" }) + const moved = finding({ startLine: 9, severity: "warning", tier: "inline" }) expect(a.id).toBe(moved.id) const env = buildEnvelope({ findings: [a], tier: "lite", mode: "comment" }) - expect(inlineComments(env)[0].body).toContain(findingMarker(a.id)) + expect(inlineComments(env).comments[0].body).toContain(findingMarker(a.id)) }) test("findings already commented on the PR are not posted again", () => { - const a = finding({ startLine: 3, title: "m: a" }) - const b = finding({ startLine: 4, title: "m: b" }) + const a = finding({ startLine: 3, title: "m: a", severity: "warning", tier: "inline" }) + const b = finding({ startLine: 4, title: "m: b", severity: "warning", tier: "inline" }) const env = buildEnvelope({ findings: [a, b], tier: "lite", mode: "comment" }) const posted = postedFindingIds(["🛑 old\n\n" + findingMarker(a.id), null, "human comment"]) expect([...posted]).toEqual([a.id]) - expect(inlineComments(env, posted).map((c) => c.line)).toEqual([4]) + expect(inlineComments(env, posted).comments.map((c) => c.line)).toEqual([4]) }) test("a skipped repeat review counts as a full post in telemetry", () => { @@ -343,3 +351,566 @@ describe("repeats of one rule within a file", () => { expect(all.id).not.toBe(grain("block_month").id) }) }) + +describe("per-rule tiers", () => { + const sql = [ + "select o.id, o.amount, case when o.amount > 0 then 'pos' end as sign", + "from {{ ref('orders') }} o, {{ ref('customers') }} c", + "where 1=1 and o.id = c.id", + "group by 1, 2", + ].join("\n") + const runner: ReviewRunner = { + async impact() { + return { hasManifest: true, severity: "SAFE", directCount: 0, transitiveCount: 0, testCount: 0 } + }, + async grade() { + return { grade: "B" } + }, + async check() { + return { issues: [], ran: true } + }, + async equivalence() { + return { decided: true, equivalent: false, differences: ["row count differs"], confidence: "high" } as any + }, + async detectPii() { + return { columns: [] } + }, + } + const review = (rules: Record, aiReview?: (input: { grounding: Finding[] }) => Promise) => + runReview({ + changedFiles: [{ path: "models/marts/m.sql", status: "modified", diff: sql.split("\n").map((l) => `+${l}`).join("\n") }], + config: { + ...DEFAULT_REVIEW_CONFIG, + reviewers: aiReview ? ["dbt_patterns", "semantic_change", "ai_review"] : ["dbt_patterns", "semantic_change"], + rules, + }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + forceTier: "full", + aiReview: aiReview as any, + getContent: async (_p, side) => (side === "old" ? "select id from {{ ref('orders') }}" : sql), + getCompiled: async (_p, side) => + side === "old" ? "select id from db.orders" : sql.replace(/\{\{ ref\('(\w+)'\) \}\}/g, "db.$1"), + }) + const rules = (fs: Finding[]) => fs.map((f) => f.rule).sort() + + test("an off rule is dropped from the findings and from the AI's grounding", async () => { + let grounding: Finding[] | undefined + const env = await review({}, async (input) => { + grounding = input.grounding + return [] + }) + expect(DEFAULT_RULE_TIERS["dbt-patterns:comma-join"]).toBe("off") + expect(rules(env.findings)).toEqual([ + "altimate-core.equivalence:not-equivalent", + "rule-catalog:group-by-positional", + "rule-catalog:self-equality", + "rule-catalog:where-1-eq-1", + ]) + expect(grounding).toBeDefined() + expect(rules(grounding!)).toEqual(rules(env.findings)) + expect(env.findings.every((f) => f.tier === "summary")).toBe(true) + }) + + test("the same rules surface when the policy reports everything", async () => { + const env = await review({ "*": "summary" }) + // These five ids are emitted by real detectors and are keys of the default table. + for (const id of [ + "dbt-patterns:comma-join", + "dbt-patterns:case-no-else", + "rule-catalog:where-1-eq-1", + "rule-catalog:self-equality", + "rule-catalog:group-by-positional", + ]) { + expect(rules(env.findings)).toContain(id) + expect(DEFAULT_RULE_TIERS[id]).toBeDefined() + } + }) + + test("a glob turns off catalog rules but not engine equivalence", async () => { + const env = await review({ "rule-catalog:*": "off", "*": "off" }) + expect(rules(env.findings)).toEqual(["altimate-core.equivalence:not-equivalent"]) + }) + + test("an exact override can turn off an engine rule, and can promote a rule inline", async () => { + const env = await review({ "altimate-core.equivalence:not-equivalent": "off", "rule-catalog:group-by-positional": "inline" }) + expect(rules(env.findings)).toEqual(["rule-catalog:group-by-positional", "rule-catalog:self-equality", "rule-catalog:where-1-eq-1"]) + expect(env.findings.find((f) => f.rule === "rule-catalog:group-by-positional")?.tier).toBe("inline") + }) + + test("resolution order: exact, longest glob, default table, summary", () => { + expect(ruleTier(undefined)).toBe("summary") + expect(ruleTier("some-new-tool:unknown-rule")).toBe("summary") + expect(ruleTier("dbt-project-rules:temporary-code-marker")).toBe("inline") + expect(ruleTier("rule-catalog:join-no-on")).toBe("off") + expect(ruleTier("rule-catalog:join-no-on", { "rule-catalog:*": "summary" })).toBe("summary") + expect(ruleTier("rule-catalog:join-no-on", { "rule-catalog:*": "summary", "rule-catalog:join-*": "inline" })).toBe("inline") + expect(ruleTier("rule-catalog:join-no-on", { "rule-catalog:*": "inline", "rule-catalog:join-no-on": "off" })).toBe("off") + expect(ruleTier("impact-analysis:downstream-consumers", { "impact-analysis:*": "off" })).toBe("summary") + expect(ruleTier("impact-analysis:downstream-consumers", { "impact-analysis:*": "inline" })).toBe("inline") + }) + + test("AI findings default to inline, and an explicit override wins", () => { + expect(ruleTier("ai-review:sql-quality")).toBe("inline") + expect(ruleTier("ai-review:sql-quality", { "ai-review:*": "summary" })).toBe("summary") + expect(ruleTier("ai-review:sql-quality", { "ai-review:sql_quality": "off" })).toBe("off") + }) + + test("override keys match in any spelling a finding id normalises from", () => { + expect(ruleTier("dbt-patterns:self-join", { "dbt_patterns:self_join": "off" })).toBe("off") + expect(ruleTier("dbt-patterns:self-join", { "DBT_Patterns:*": "inline" })).toBe("inline") + expect(ruleTier("rule-catalog:join-no-on", { "rule_catalog:join_*": "summary" })).toBe("summary") + }) + + test("default table keys are well-formed ids, and verdict-relevant engine rules are never off", () => { + const keys = Object.keys(DEFAULT_RULE_TIERS) + expect(keys.filter((k) => DEFAULT_RULE_TIERS[k] === "inline")).toHaveLength(14) + for (const k of keys) expect(k.endsWith(":*") || /^[a-z0-9.-]+:[a-z0-9.-]+$/.test(k)).toBe(true) + // Rules kept at summary because they produced a real benchmark catch. + for (const k of [ + "altimate-core.structural-diff:removed-predicate", + "dbt-project-rules:partition-not-in-merge-key", + "dbt-project-rules:models-disabled", + "rule-catalog:greatest-least-null", + "rule-catalog:hardcoded-year-filter", + "dbt-patterns:hardcoded-date", + "altimate-core.dbt-config:incremental-no-guard", + "altimate-core.dbt-config:unique-key-change", + "dbt-patterns:fanout-join", + ]) + expect(ruleTier(k)).toBe("summary") + for (const p of ["altimate-core.equivalence:", "impact-analysis:", "schema.detect-pii:", "contract-check:", "data.diff:"]) + expect(keys.filter((k) => k.startsWith(p) && DEFAULT_RULE_TIERS[k] === "off")).toEqual([]) + }) +}) + +describe("inline posting policy", () => { + const inlineAt = (i: number, over: Partial[0]> = {}) => + finding({ + severity: "warning", + category: "sql_quality", + tier: "inline", + model: `m${i}`, + file: `models/m${i}.sql`, + startLine: 3, + title: `m${i}: inline issue ${i}`, + ...over, + }) + + test("at most inlineBudget new comments; the rest are deferred and stay in the summary", () => { + const env = buildEnvelope({ findings: Array.from({ length: 12 }, (_, i) => inlineAt(i)), tier: "lite", mode: "comment" }) + const { comments, deferred } = inlineComments(env) + expect(comments).toHaveLength(10) + expect(deferred).toHaveLength(2) + const md = renderSummary(env) + expect(md).toContain("### Inline (10)") + for (const f of deferred) expect(md).toContain(`**${f.title}**`) + }) + + test("the budget counts only comments new this run", () => { + const fs = Array.from({ length: 12 }, (_, i) => inlineAt(i)) + const env = buildEnvelope({ findings: fs, tier: "lite", mode: "comment" }) + const posted = new Set(fs.slice(0, 5).map((f) => f.id)) + const { comments, deferred } = inlineComments(env, posted) + expect(comments).toHaveLength(7) + expect(deferred).toHaveLength(0) + }) + + test("findings below the severity floor, summary-tier rules and pre-existing lines are never inline", () => { + const env = buildEnvelope({ + findings: [ + inlineAt(1, { severity: "suggestion" }), + inlineAt(2, { tier: "summary" }), + inlineAt(3, { preExisting: true }), + inlineAt(4, { startLine: undefined }), + ], + tier: "lite", + mode: "comment", + }) + expect(inlineComments(env).comments).toHaveLength(0) + const md = renderSummary(env) + expect(md).not.toContain("### Inline") + expect(md).toContain("
Pre-existing (1)") + expect(md).toContain("m3: inline issue 3") + }) + + test("AI-lane findings follow their tier like any other rule", () => { + const ai = (tier?: "inline" | "summary") => + finding({ + severity: "warning", + category: "sql_quality", + startLine: 3, + title: "m: ai finding", + evidence: { tool: "ai-review", result: {} }, + ...(tier ? { tier } : {}), + }) + expect(inlineComments(buildEnvelope({ findings: [ai("inline")], tier: "lite", mode: "comment" })).comments).toHaveLength(1) + expect(inlineComments(buildEnvelope({ findings: [ai("summary")], tier: "lite", mode: "comment" })).comments).toHaveLength(0) + }) + + test("most severe, then most confident, then file and line first", () => { + const env = buildEnvelope({ + findings: [ + inlineAt(1, { confidence: "low", file: "models/a.sql" }), + inlineAt(2, { file: "models/b.sql", startLine: 9 }), + inlineAt(3, { file: "models/b.sql", startLine: 2 }), + inlineAt(4, { severity: "critical", category: "lineage_breakage", file: "models/z.sql" }), + ], + tier: "lite", + mode: "comment", + }) + expect(inlineComments(env).comments.map((c) => `${c.path}:${c.line}`)).toEqual([ + "models/z.sql:3", + "models/b.sql:2", + "models/b.sql:9", + "models/a.sql:3", + ]) + }) + + test("the envelope's posting policy sets the budget and floor", () => { + const env = buildEnvelope({ + findings: [inlineAt(1), inlineAt(2), inlineAt(3, { severity: "suggestion" })], + tier: "lite", + mode: "comment", + policy: { source: "base", hash: "h", posting: { inlineBudget: 1, inlineSeverityFloor: "suggestion", silentWhenClean: true } }, + }) + const { comments, deferred } = inlineComments(env) + expect(comments).toHaveLength(1) + expect(deferred).toHaveLength(2) + }) + + test("local output renders every finding in full", () => { + const env = buildEnvelope({ findings: [inlineAt(1, { body: "the detail" })], tier: "lite", mode: "comment" }) + expect(renderSummary(env)).not.toContain("the detail") + expect(renderSummary(env, { inline: false })).toContain("the detail") + }) +}) + +describe("output form", () => { + const head = "select\n id\n , new_col\n , other_col\n\nfrom src" + const added = new Map([["models/m.sql", new Set([3, 4])]]) + const getContent = async () => head + const withSuggestion = (startLine: number, endLine: number, replacement: string) => + finding({ + severity: "warning", + category: "sql_quality", + title: "m: ai finding", + startLine, + evidence: { tool: "ai-review", result: {} }, + tier: "inline", + suggestion: { startLine, endLine, replacement }, + }) + + test("a valid suggestion renders a suggestion fence over its line range", async () => { + const [f] = await validateSuggestions([withSuggestion(3, 4, " , new_col\n , other_col_v2")], getContent, added) + expect(f.suggestion).toBeDefined() + const [c] = inlineComments(buildEnvelope({ findings: [f], tier: "lite", mode: "comment" })).comments + expect(c.body).toContain("```suggestion\n , new_col\n , other_col_v2\n```") + expect({ start_line: c.start_line, start_side: c.start_side, line: c.line, side: c.side }).toEqual({ + start_line: 3, + start_side: "RIGHT", + line: 4, + side: "RIGHT", + }) + }) + + test("a one-line suggestion sets only `line`", async () => { + const [f] = await validateSuggestions([withSuggestion(3, 3, " , new_col_v2")], getContent, added) + const [c] = inlineComments(buildEnvelope({ findings: [f], tier: "lite", mode: "comment" })).comments + expect(c.line).toBe(3) + expect(c.start_line).toBeUndefined() + }) + + test("a range outside the added lines, past the file end, or unchanged degrades to a text block", async () => { + const cases = [ + withSuggestion(2, 3, " id\n , x"), // line 2 is context, not added + withSuggestion(3, 3, " , new_col"), // replacement equals the current line + ] + const out = await validateSuggestions(cases, getContent, added) + const past = await validateSuggestions( + [withSuggestion(3, 4, "x")], + async () => "select\n id\n , new_col", + added, + ) + for (const f of [...out, ...past]) { + expect(f.suggestion).toBeUndefined() + expect(f.body).toContain("Suggested fix:\n```sql\n") + const [c] = inlineComments(buildEnvelope({ findings: [f], tier: "lite", mode: "comment" })).comments + expect(c.body).not.toContain("```suggestion") + expect(c.start_line).toBeUndefined() + } + }) + + test("long bodies are clamped when rendered; the envelope keeps the full text", () => { + const body = Array.from({ length: 40 }, (_, i) => `Sentence number ${i} explains the problem.`).join(" ") + const f = finding({ severity: "warning", category: "sql_quality", tier: "inline", startLine: 3, body }) + const env = buildEnvelope({ findings: [f], tier: "lite", mode: "comment" }) + expect(env.findings[0].body).toBe(body) + const [c] = inlineComments(env).comments + expect(c.body).not.toContain(body) + expect(c.body).toContain("explains the problem.…") + const clamped = clampBody(body) + expect(clamped.length).toBeLessThanOrEqual(BODY_MAX_CHARS) + expect(clamped.endsWith("problem.…")).toBe(true) + expect(renderSummary(env, { inline: false })).toContain(clamped) + expect(clampBody("short")).toBe("short") + }) + + test("a clamp inside a code fence closes the fence", () => { + const body = "Use this instead:\n```sql\n" + "select a, b from t\n".repeat(60) + "```" + const out = clampBody(body, 200) + expect((out.match(/^```/gm) ?? []).length % 2).toBe(0) + }) + + test("the matched source line is quoted in inline comments and summary bullets", async () => { + const sql = "select id from a\nwhere id not in (select id from b)" + const env = await runReview({ + changedFiles: [{ path: "models/m.sql", status: "added", diff: `@@ -0,0 +1,2 @@\n+select id from a\n+where id not in (select id from b)\n` }], + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["dbt_patterns"] }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner: { + async impact() { + return { hasManifest: true, severity: "SAFE", directCount: 0, transitiveCount: 0, testCount: 0 } + }, + async grade() { + return { grade: "B" } + }, + async check() { + return { issues: [], ran: true } + }, + async equivalence() { + return { decided: true, equivalent: true } + }, + async detectPii() { + return { columns: [] } + }, + } as ReviewRunner, + getContent: async () => sql, + forceTier: "full", + }) + const f = env.findings.find((x) => x.evidenceLine === "where id not in (select id from b)") + expect(f).toBeDefined() + const md = renderSummary(env, { inline: false }) + expect(md).toContain(`\`${f!.file}${f!.startLine ? `:${f!.startLine}` : ""}\` — \`where id not in (select id from b)\``) + + const g = finding({ severity: "warning", category: "sql_quality", tier: "inline", startLine: 3, evidenceLine: " , new_col" }) + const [c] = inlineComments(buildEnvelope({ findings: [g], tier: "lite", mode: "comment" })).comments + expect(c.body).toContain("`models/m.sql:3` — `, new_col`") + }) +}) + +describe("anchoring rule findings by their matched line", () => { + const runner = { + async impact() { + return { hasManifest: true, severity: "SAFE", directCount: 0, transitiveCount: 0, testCount: 0 } + }, + async grade() { + return { grade: "B" } + }, + async check() { + return { issues: [], ran: true } + }, + async equivalence() { + return { decided: true, equivalent: true } + }, + async detectPii() { + return { columns: [] } + }, + } as ReviewRunner + + // Head lines: 1 select id, 2 from t, 3 qualify (context), 4 qualify (added), 5 where x = 1, 6 qualify (added again) + const QDIFF = [ + "@@ -1,3 +1,6 @@", + " select id", + " from t", + " qualify row_number() over (partition by id order by d) = 1", + "+ qualify row_number() over (partition by k order by d) = 1 -- latest", + "+where x = 1", + "+ qualify row_number() over (partition by k order by d) = 1 -- latest", + ].join("\n") + const file = { path: "models/m.sql", status: "modified" as const, diff: QDIFF } + const withLine = (line: string) => finding({ evidence: { tool: "rule-catalog", result: { rule: "x", line } } }) + + test("the hunk walker yields head-side lines that agree with scanDiffLines", () => { + const rows = [...walkDiffLines(DIFF)] + expect(rows.map((r) => `${r.kind}${r.line}`)).toEqual([" 1", " 2", "+3", "+4", " 5", " 6"]) + expect(rows[2].text).toBe(" , new_col") + const { commentable, added } = scanDiffLines(DIFF) + expect([...commentable]).toEqual(rows.map((r) => r.line)) + expect([...added]).toEqual(rows.filter((r) => r.kind === "+").map((r) => r.line)) + }) + + test("a catalog rule records the raw added line, not the comment-stripped one", () => { + const added = ["select id from t", " qualify row_number() over (partition by k order by d) = 1 -- latest"] + const f = evaluateCatalog(file, added.join("\n"), added, [], DEFAULT_RUBRIC).find( + (x) => (x.evidence?.result as { rule?: string }).rule === "qualify-unsupported", + ) + expect((f!.evidence!.result as { line?: string }).line).toBe(added[1]) + }) + + test("a finding whose text is on an added line gets that line, and it posts inline when its rule is inline-tier", () => { + const [f] = anchorByEvidence([withLine(" qualify row_number() over (partition by k order by d) = 1 -- latest")], [file]) + expect(f.startLine).toBe(4) + expect(f.endLine).toBe(4) + const [g] = anchorToDiff([{ ...f, severity: "warning", tier: "inline" }], [file]) + expect(g.preExisting).toBeUndefined() + const { comments } = inlineComments(buildEnvelope({ findings: [g], tier: "lite", mode: "comment" })) + expect(comments.map((c) => c.line)).toEqual([4]) + }) + + test("text only on a context line anchors there and is marked pre-existing", () => { + const [f] = anchorToDiff(anchorByEvidence([withLine("qualify row_number() over (partition by id order by d) = 1")], [file]), [file]) + expect(f.startLine).toBe(3) + expect(f.preExisting).toBe(true) + }) + + test("text absent from the diff leaves the finding without a line", () => { + const [f] = anchorByEvidence([withLine("select something_else from nowhere")], [file]) + expect(f.startLine).toBeUndefined() + const [g] = anchorByEvidence([withLine("where x = 1")], [{ ...file, path: "models/other.sql" }]) + expect(g.startLine).toBeUndefined() + }) + + test("duplicate added lines anchor to the first; surrounding whitespace is ignored", () => { + const [f] = anchorByEvidence([withLine("\tqualify row_number() over (partition by k order by d) = 1 -- latest ")], [file]) + expect(f.startLine).toBe(4) + }) + + test("a long fragment matches inside an added line; a short one does not", () => { + const [long] = anchorByEvidence([withLine("over (partition by k order by d)")], [file]) + expect(long.startLine).toBe(4) + const [short] = anchorByEvidence([withLine("x = 1 --")], [file]) + expect(short.startLine).toBeUndefined() + }) + + test("a finding that already has a line keeps it", () => { + const [f] = anchorByEvidence([{ ...withLine("where x = 1"), startLine: 1, endLine: 1 }], [file]) + expect(f.startLine).toBe(1) + }) + + test("a dbt-patterns detector's recorded line anchors", () => { + const diff = "@@ -1,2 +1,3 @@\n select id from a\n+where id not in (select id from b)\n from_here\n" + const changed = { path: "models/m.sql", status: "modified" as const, diff } + const found = detectModelPatterns(changed, "select id from a\nwhere id not in (select id from b)\n", DEFAULT_RUBRIC) + const notIn = found.find((f) => (f.evidence?.result as { rule?: string }).rule === "not_in_nullable") + expect(notIn).toBeDefined() + expect(anchorByEvidence([notIn!], [changed])[0].startLine).toBe(2) + }) + + test("the dedup detector records the added line that opens the window", () => { + const sql = "select *\nfrom (\n select *, row_number() over (\n partition by id\n ) as rn\n from t\n)\nwhere rn = 1\n" + const diff = "@@ -0,0 +1,8 @@\n" + sql.trimEnd().split("\n").map((l) => `+${l}`).join("\n") + "\n" + const changed = { path: "models/m.sql", status: "added" as const, diff } + const f = detectModelPatterns(changed, sql, DEFAULT_RUBRIC).find( + (x) => (x.evidence?.result as { rule?: string }).rule === "dedup_no_tiebreaker", + ) + expect((f!.evidence!.result as { line?: string }).line).toBe(" select *, row_number() over (") + expect(anchorByEvidence([f!], [changed])[0].startLine).toBe(3) + }) + + test("end to end: an inline-tier catalog rule becomes an inline comment on its line", async () => { + const sql = "select id\nfrom t\nqualify row_number() over (partition by id order by d desc) = 1\n" + const env = await runReview({ + changedFiles: [ + { + path: "models/m.sql", + status: "modified", + diff: "@@ -1,2 +1,3 @@\n select id\n from t\n+qualify row_number() over (partition by id order by d desc) = 1\n", + }, + ], + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["dbt_patterns"], dialect: "postgres" }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + getContent: async () => sql, + forceTier: "full", + }) + const f = env.findings.find((x) => x.rule === "rule-catalog:qualify-unsupported") + expect(f).toBeDefined() + expect(f!.tier).toBe("inline") + expect(f!.startLine).toBe(3) + expect(f!.preExisting).toBeUndefined() + const { comments } = inlineComments(env) + expect(comments.find((c) => c.body.includes(findingMarker(f!.id)))?.line).toBe(3) + }) + + test("a core check issue on a Jinja model anchors by its compiled line's text", async () => { + const raw = "with o as (\n select * from {{ ref('orders') }}\n)\nselect *,\n row_number() over (partition by order_id order by ordered_at desc) as rn\nfrom o\n" + const compiled = "with o as (\n\n select * from \"db\".\"main\".\"orders\"\n\n)\nselect *,\n row_number() over (partition by order_id order by ordered_at desc) as rn\nfrom o\n" + const diff = "@@ -1,4 +1,6 @@\n with o as (\n select * from {{ ref('orders') }}\n )\n-select * from o\n+select *,\n+ row_number() over (partition by order_id order by ordered_at desc) as rn\n+from o\n" + const review = (line: number) => + runReview({ + changedFiles: [{ path: "models/m.sql", status: "modified", diff }], + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["sql_quality"] }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner: { + ...runner, + async check() { + return { issues: [{ rule: "non_deterministic_dedup", message: "ROW_NUMBER() orders only by time", line, severity: "warning" }], ran: true } + }, + } as ReviewRunner, + getContent: async () => raw, + getCompiled: async () => compiled, + forceTier: "full", + }) + const dedup = (env: Awaited>) => env.findings.find((x) => x.rule === "altimate-core.check:non-deterministic-dedup") + // Compiled line 7 is the window: found on head line 5 of the raw file. + expect(dedup(await review(7))?.startLine).toBe(5) + // Compiled line 3 (the expanded ref) is not in the raw file; line 5 (`)`) is too short to be specific. + for (const line of [3, 5]) { + const f = dedup(await review(line)) + expect(f).toBeDefined() + expect(f!.startLine).toBeUndefined() + } + }) + + describe("non-deterministic-dedup whose compiled text is not in the raw diff", () => { + const MSG = "ROW_NUMBER() orders only by time/date column(s) [_airbyte_extracted_at] with no unique tiebreaker." + const review = (raw: string, diff: string, compiled: string, line: number, message = MSG) => + runReview({ + changedFiles: [{ path: "models/m.sql", status: "added", diff }], + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["sql_quality"] }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner: { + ...runner, + async check() { + return { issues: [{ rule: "non_deterministic_dedup", message, line, severity: "warning" }], ran: true } + }, + } as ReviewRunner, + getContent: async () => raw, + getCompiled: async () => compiled, + forceTier: "full", + }).then((env) => env.findings.find((x) => x.rule === "altimate-core.check:non-deterministic-dedup")!) + const added = (raw: string) => { + const rows = raw.trimEnd().split("\n") + return `@@ -0,0 +1,${rows.length} @@\n` + rows.map((l) => `+${l}`).join("\n") + "\n" + } + const compiled = + "with source as (\n select * from raw.t\n)\n, most_recent_source as (\n select *\n , row_number() over ( partition by id order by _airbyte_extracted_at desc) as row_num\n from source\n)\nselect * from most_recent_source where row_num = 1\n" + + test("a dedup macro call: the added line that orders by the named column", async () => { + const raw = + "with source as (\n select * from {{ source('raw', 't') }}\n)\n\n-- dedupe the raw table\n{{ deduplicate_raw_table(order_by='_airbyte_extracted_at' , partition_columns = 'id') }}\n\nselect * from most_recent_source\n" + const f = await review(raw, added(raw), compiled, 6) + expect(f.startLine).toBe(6) + }) + + test("a Jinja-built window: the added row_number line naming the column wins over a later order by", async () => { + const raw = + "select *,\n row_number() over (partition by {{ key }} order by _airbyte_extracted_at desc) as rn\nfrom t\norder by _airbyte_extracted_at\n" + const f = await review(raw, added(raw), compiled, 6) + expect(f.startLine).toBe(2) + }) + + test("no added line names the column: no line", async () => { + const raw = "{{ deduplicate_raw_table(partition_columns = 'id') }}\nselect * from most_recent_source\n" + expect((await review(raw, added(raw), compiled, 6)).startLine).toBeUndefined() + const named = "{{ deduplicate_raw_table(order_by='_airbyte_extracted_at') }}\n" + expect((await review(named, added(named), compiled, 6, "ROW_NUMBER() orders only by time")).startLine).toBeUndefined() + }) + }) +}) diff --git a/packages/opencode/test/altimate/review-project-rules.test.ts b/packages/opencode/test/altimate/review-project-rules.test.ts index cef8d1a85d..71f5fa4ab8 100644 --- a/packages/opencode/test/altimate/review-project-rules.test.ts +++ b/packages/opencode/test/altimate/review-project-rules.test.ts @@ -10,7 +10,8 @@ import { packagesFindings, testsOnNullColumns, } from "../../src/altimate/review/project-rules" -import { CATALOG, ruleMatches, leftJoinNullifiedByWhere } from "../../src/altimate/review/rule-catalog" +import { CATALOG, ruleMatches, leftJoinNullifiedByWhere, evaluateCatalog } from "../../src/altimate/review/rule-catalog" +import { isSuppressiblePortability } from "../../src/altimate/review/orchestrate" import { detectMacroPatterns } from "../../src/altimate/review/dbt-patterns" import { DEFAULT_RUBRIC } from "../../src/altimate/review/rubric" import type { ChangedFile } from "../../src/altimate/review/diff-filter" @@ -82,6 +83,43 @@ describe("Jinja structure", () => { expect(titles(jinjaFindings(file("macros/ghost.sql", inserted), old, inserted))).toContain("ghost: `ghost` parameters moved position") expect(jinjaFindings(file("macros/ghost.sql", appended), old, appended).some((f) => /moved/.test(f.title))).toBe(false) }) + describe("macro-param-shifted anchors on the changed signature", () => { + const shifted = (diff: string, old: string, head: string) => + jinjaFindings({ path: "macros/m.sql", status: "modified", diff }, old, head).find((f) => /moved/.test(f.title)) + + test("multi-line signature: the inserted parameter line, not the unchanged `{% macro` line", () => { + const old = "{% macro stab(\n model,\n max_change_percent,\n days_back\n) %}\nx\n{% endmacro %}" + const head = "{% macro stab(\n model,\n max_change_percent,\n max_change_absolute,\n days_back\n) %}\nx\n{% endmacro %}" + const diff = "@@ -2,3 +2,4 @@\n model,\n max_change_percent,\n+ max_change_absolute,\n days_back\n" + const f = shifted(diff, old, head)! + expect(f.startLine).toBe(4) + expect((f.evidence!.result as { line?: string }).line).toBe(" max_change_absolute,") + }) + + test("single-line signature: the changed `{% macro` line", () => { + const old = "{% macro ghost(a, b) %}{% endmacro %}" + const head = "-- header\n{% macro ghost(a, x, b) %}{% endmacro %}" + const diff = "@@ -1 +1,2 @@\n+-- header\n-{% macro ghost(a, b) %}{% endmacro %}\n+{% macro ghost(a, x, b) %}{% endmacro %}\n" + expect(shifted(diff, old, head)!.startLine).toBe(2) + }) + + test("two macros in one file: each finding anchors inside its own signature", () => { + const old = "{% macro one(a, b) %}{% endmacro %}\n{% macro two(\n c,\n d\n) %}{% endmacro %}" + const head = "{% macro one(a, b) %}{% endmacro %}\n{% macro two(\n e,\n c,\n d\n) %}{% endmacro %}" + const diff = "@@ -2,2 +2,3 @@\n {% macro two(\n+ e,\n c,\n" + const f = shifted(diff, old, head)! + expect(f.title).toContain("`two`") + expect(f.startLine).toBe(3) + }) + + test("no added line inside the signature: no line set here (left to the evidence text)", () => { + const old = "{% macro ghost(a, b) %}{% endmacro %}" + const head = "{% macro ghost(b, a) %}{% endmacro %}" + const f = shifted("@@ -5 +5 @@\n-x\n+y\n", old, head)! + expect(f.startLine).toBeUndefined() + }) + }) + test("a var() key renamed without the old key as a fallback", () => { const old = "{%- set concat = var('concat_string', '||') -%}" const renamed = "{%- set concat = var('datavault4dbt.concat_string', '||') -%}" @@ -104,6 +142,21 @@ describe("Jinja structure", () => { const ok = "where mod(abs(xxhash64(address)), 1000) = 0 -- one in a thousand, as a template example" expect(jinjaFindings(file("models/m.sql", ok), "select 1", ok).some((f) => /temporary/.test(f.title))).toBe(false) }) + test("temporary-code phrasing: take this out, while debugging, remove it before release", () => { + const temp = (sql: string) => jinjaFindings(file("models/m.sql", sql), "select 1", sql).some((f) => /temporary/.test(f.title)) + expect(temp(" where order_id < 50 -- sample while debugging locally, take this out before release")).toBe(true) + expect(temp(" limit 100 -- take it out once the backfill finishes")).toBe(true) + expect(temp(" and region = 'eu' {# remove this before deploying #}")).toBe(true) + expect(temp(" where amount > 0 -- zero-amount rows are voided payments from the card processor")).toBe(false) + expect(temp(" where released_at < current_date -- orders placed before release are pre-orders")).toBe(false) + expect(temp(" -- take this out before release")).toBe(true) + // Only comments count: the phrase in code or a string literal is not a marker. + expect(temp(" select 'take this out' as label")).toBe(false) + // Without removal intent the phrases describe code meant to stay. + expect(temp(" limit 10 -- handy while debugging: take this out of the CTE to see raw rows")).toBe(false) + expect(temp(" and is_test = false -- surfaced while debugging the refund mismatch")).toBe(false) + expect(temp(" limit 10 -- added while debugging")).toBe(true) + }) }) describe("model config combinations", () => { @@ -117,6 +170,16 @@ describe("model config combinations", () => { expect(run(sql, "unknown")).toContain("m: microbatch `batch_size` differs from the partition granularity") expect(run(sql.replace("'year' if flags.FULL_REFRESH else 'day'", "'day'"))).not.toContain("m: microbatch `batch_size` differs from the partition granularity") }) + test("microbatch granularity mismatch is a BigQuery rule: silent on other known adapters", () => { + const sql = `{{ config(materialized='incremental', incremental_strategy='microbatch', event_time='paid_at',\n batch_size='hour',\n partition_by={'field': 'paid_at', 'data_type': 'timestamp', 'granularity': 'day'}) }}\nselect 1` + const title = "m: microbatch `batch_size` differs from the partition granularity" + const at = (dialect: string, known = true) => titles(modelConfigFindings(file("models/m.sql", sql), sql, dialect, undefined, known)) + expect(at("bigquery")).toContain(title) + for (const d of ["duckdb", "postgres", "snowflake", "databricks"]) expect(at(d)).not.toContain(title) + // A guessed dialect: the BigQuery `granularity` dict still identifies the adapter. + expect(at("snowflake", false)).toContain(title) + expect(titles(modelConfigFindings(file("models/m.sql", sql), sql.replace("'hour'", "'day'"), "bigquery"))).not.toContain(title) + }) test("partition column missing from the merge key, and an incremental filter that doesn't prune", () => { const sql = `{{ config(materialized='incremental', incremental_strategy='merge', partition_by=['block_month'], unique_key=['tx_hash', 'evt_index']) }}\nselect * from t\n{% if is_incremental() %}\nwhere block_time >= now() - interval '1' day\n{% endif %}` const t = run(sql, "trino") @@ -130,6 +193,11 @@ describe("model config combinations", () => { expect(run(sql, "trino")).toContain("m: incremental filter reads new rows only, not updated ones") expect(run(sql.replace(/enrollment_created_on/g, "enrollment_updated_on"), "trino")).not.toContain("m: incremental filter reads new rows only, not updated ones") }) + test("the creation-time filter finding quotes the added filter line", () => { + const sql = `{{ config(materialized='incremental', unique_key='enrollment_key') }}\nselect * from e\n{% if is_incremental() %}\n WHERE Enrollment_Created_On > (select max(enrollment_created_on) from {{ this }})\n{% endif %}` + const f = modelConfigFindings(file("models/m.sql", sql), sql, "trino").find((x) => x.rule === "dbt-project-rules:incremental-filter-created-only") + expect((f?.evidence?.result as { line?: string } | undefined)?.line).toBe(" WHERE Enrollment_Created_On > (select max(enrollment_created_on) from {{ this }})") + }) }) describe("ref() targets", () => { @@ -238,6 +306,14 @@ describe("SQL shapes from the catalog", () => { expect(fires("case-duplicate-when", "case\n when grade = 'A' then 1\n when grade = 'a' then 2\nend")).toBe(false) expect(fires("case-duplicate-when", "case\n when Grade = 'A' then 1\n WHEN grade = 'A' then 2\nend")).toBe(true) }) + test("duplicate WHEN ignores spacing and keyword case, but not literal case", () => { + const tier = (third: string) => + `CASE\n WHEN s.lifetime_spend >= 1000 THEN 'platinum'\n WHEN s.lifetime_spend >= 500 THEN 'gold'\n ${third}\n ELSE 'standard'\nEND` + expect(fires("case-duplicate-when", tier("WHEN s.lifetime_spend>=1000 THEN 'vip'"))).toBe(true) + expect(fires("case-duplicate-when", tier("when S.Lifetime_Spend >= 1000 then 'vip'"))).toBe(true) + expect(fires("case-duplicate-when", tier("WHEN s.lifetime_spend>=100 THEN 'silver'"))).toBe(false) + expect(fires("case-duplicate-when", "case\n when status = 'A' then 1\n when status = 'a' then 2\nend")).toBe(false) + }) test("window ORDER BY repeating the partition key", () => { expect(fires("window-order-repeats-partition", "row_number() over (partition by tx_hash, evt_index order by tx_hash) as rn")).toBe(true) expect(fires("window-order-repeats-partition", "row_number() over (partition by tx_hash order by block_time desc) as rn")).toBe(false) @@ -252,8 +328,29 @@ describe("SQL shapes from the catalog", () => { const sql = "select\n last_value(\n content_title\n ignore nulls\n ) over (partition by k order by d) as t\nfrom x" expect(fires("ignore-nulls-inside-call", sql, [" ignore nulls"])).toBe(true) expect(fires("ignore-nulls-inside-call", "select last_value(t) ignore nulls over (partition by k order by d) from x")).toBe(false) - // DuckDB accepts IGNORE NULLS inside the call too, so it is not flagged as non-portable there. - expect(rule("ignore-nulls-inside-call").portability?.worksOn).toContain("duckdb") + // DuckDB accepts IGNORE NULLS inside a navigation window call, so that form is portable there. + const duck = rule("ignore-nulls-inside-call").portability?.alsoWorksOn + expect(duck?.engines).toContain("duckdb") + expect(duck?.check(sql)).toBe(true) + }) + test("IGNORE NULLS inside an aggregate call followed by ORDER BY", () => { + expect(fires("ignore-nulls-inside-call", "select ARRAY_AGG(status IGNORE NULLS ORDER BY order_date) as s from o")).toBe(true) + expect(fires("ignore-nulls-inside-call", "select ARRAY_AGG(status IGNORE NULLS LIMIT 10) as s from o")).toBe(true) + expect(fires("ignore-nulls-inside-call", "select ARRAY_AGG(status ORDER BY order_date) FILTER (WHERE status IS NOT NULL) as s from o")).toBe(false) + }) + test("IGNORE NULLS inside the call: kept on Postgres/Snowflake; dropped on DuckDB for window functions only", () => { + const ignoreNulls = (sql: string) => + evaluateCatalog({ path: "models/m.sql", status: "modified" }, sql, sql.split("\n"), [], DEFAULT_RUBRIC).find( + (f) => (f.evidence?.result as { rule?: string }).rule === "ignore-nulls-inside-call", + )! + const win = ignoreNulls("select last_value(location_id ignore nulls) over (partition by c order by d) as l from o") + const agg = ignoreNulls("select ARRAY_AGG(status IGNORE NULLS ORDER BY order_date) as s from o") + expect(isSuppressiblePortability(win, "duckdb")).toBe(true) + expect(isSuppressiblePortability(win, "bigquery")).toBe(true) + expect(isSuppressiblePortability(win, "postgres")).toBe(false) + expect(isSuppressiblePortability(win, "snowflake")).toBe(false) + expect(isSuppressiblePortability(agg, "duckdb")).toBe(false) + expect(isSuppressiblePortability(agg, "bigquery")).toBe(true) }) }) diff --git a/packages/opencode/test/altimate/review-scope-diff.test.ts b/packages/opencode/test/altimate/review-scope-diff.test.ts index 160ff58174..37110d845d 100644 --- a/packages/opencode/test/altimate/review-scope-diff.test.ts +++ b/packages/opencode/test/altimate/review-scope-diff.test.ts @@ -7,7 +7,9 @@ import { promisify } from "node:util" import { collectChangedFiles, resolveReviewBase } from "../../src/altimate/review/git" import { classifyDbtFile, scopeToProject, type ChangedFile } from "../../src/altimate/review/diff-filter" import { dbtProjectLayout } from "../../src/altimate/review/compiled" -import { runReview, type ReviewRunner } from "../../src/altimate/review/orchestrate" +import { runReview, anchorToDiff, addedLinesByFile, type ReviewRunner } from "../../src/altimate/review/orchestrate" +import { makeFinding, mergeRepeats, type Finding } from "../../src/altimate/review/finding" +import { computeIdealVerdict } from "../../src/altimate/review/verdict" import { DEFAULT_REVIEW_CONFIG } from "../../src/altimate/review/config" import { DEFAULT_RUBRIC } from "../../src/altimate/review/rubric" import { reviewPullRequest } from "../../src/altimate/review/run" @@ -246,6 +248,18 @@ describe("scopeToProject: the dbt project being reviewed, nothing else", () => { expect(r.movedUnchanged).toBe(0) }) + test("the dropped files are listed by path for the coverage ledger", () => { + const macroMove: ChangedFile = { + path: "projects/a/macros/utils/sub/m.sql", + oldPath: "projects/a/macros/utils/m.sql", + status: "renamed", + similarity: 100, + } + const r = scopeToProject([...files, macroMove], "projects/a") + expect(r.outOfScopePaths).toEqual(["projects/b/models/y.sql", ".github/workflows/ci.yml"]) + expect(r.movedUnchangedPaths).toEqual(["projects/a/macros/utils/sub/m.sql"]) + }) + test("project at the repo root keeps every file", () => { expect(scopeToProject(files, "").outOfScope).toBe(0) }) @@ -326,6 +340,75 @@ describe("classifyDbtFile: the project's own directories decide", () => { }) }) +describe("layout dirs outside the project root (shared macros in a monorepo)", () => { + async function solanaLayout(macroPaths: string) { + const root = await newRepo() + await write(root, "dbt_subprojects/solana/dbt_project.yml", `name: solana\nmacro-paths: ${macroPaths}\n`) + return dbtProjectLayout(path.join(root, "dbt_subprojects/solana"), "dbt_subprojects/solana") + } + + test("a macro dir above the root classifies as macro, stays in scope and reaches the AI lane", async () => { + const layout = await solanaLayout(`["macros", "../../dbt_macros"]`) + expect(classifyDbtFile("dbt_macros/shared/x.sql", layout)).toBe("macro") + expect(classifyDbtFile("dbt_subprojects/solana/macros/y.sql", layout)).toBe("macro") + const files: ChangedFile[] = [ + { path: "dbt_macros/shared/x.sql", status: "modified", diff: "+{% macro x() %}1{% endmacro %}\n" }, + { path: "dbt_subprojects/solana/models/m.sql", status: "modified", diff: "+select 1 as id\n" }, + { path: "dbt_subprojects/ethereum/models/e.sql", status: "modified", diff: "+select 1 as id\n" }, + ] + const r = scopeToProject(files, layout!.root, layout) + expect(r.inScope.map((f) => f.path)).toEqual(["dbt_macros/shared/x.sql", "dbt_subprojects/solana/models/m.sql"]) + expect(r.outOfScope).toBe(1) + // Without the layout the shared macro dir is out of scope, as before. + expect(scopeToProject(files, layout!.root).outOfScope).toBe(2) + + let aiPaths: Array<{ path: string; kind?: string }> = [] + await runReview({ + changedFiles: r.inScope, + config: { ...DEFAULT_REVIEW_CONFIG }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + layout, + runner: { + async impact() { + return { hasManifest: true, severity: "SAFE", directCount: 0, transitiveCount: 0, testCount: 0 } + }, + async grade() { + return { grade: "B" } + }, + async check() { + return { issues: [], ran: true } + }, + async equivalence() { + return { decided: false } + }, + async detectPii() { + return { columns: [] } + }, + } as unknown as ReviewRunner, + getContent: async (f, side) => (side === "old" ? undefined : f.endsWith("x.sql") ? "{% macro x() %}1{% endmacro %}\n" : "select 1 as id\n"), + getCompiled: async () => undefined, + aiReview: async (input) => { + aiPaths = input.files.map((f) => ({ path: f.path, kind: f.kind })) + return { findings: [], status: { state: "ok" } } + }, + }) + expect(aiPaths).toContainEqual({ path: "dbt_macros/shared/x.sql", kind: "macro" }) + }) + + test("a layout dir that escapes the repository is ignored", async () => { + const layout = await solanaLayout(`["macros", "../../../outside"]`) + const files: ChangedFile[] = [ + { path: "outside/x.sql", status: "modified" }, + { path: "../outside/x.sql", status: "modified" }, + ] + const r = scopeToProject(files, layout!.root, layout) + expect(r.inScope).toEqual([]) + expect(r.outOfScope).toBe(2) + expect(classifyDbtFile("outside/x.sql", layout)).toBe("other") + }) +}) + describe("raw Jinja never reaches the engine lanes", () => { function runnerRecording(seen: string[]): ReviewRunner { return { @@ -514,3 +597,67 @@ describe("a shallow CI checkout without the target branch", () => { expect(resolved.sha).toBe(branchPoint) }, 60_000) }) + +describe("changed-line awareness: findings on lines the PR did not add", () => { + // Head lines: 10 and 11 are context, 12 is added, 13 is context. + const diff = [ + "diff --git a/models/m.sql b/models/m.sql", + "--- a/models/m.sql", + "+++ b/models/m.sql", + "@@ -10,3 +10,4 @@", + " select a,", + " b,", + "+ c,", + " d", + "", + ].join("\n") + const file: ChangedFile = { path: "models/m.sql", status: "modified", diff } + const at = (line: number, title = `m: risky join on line ${line}`): Finding => + makeFinding({ + category: "sql_quality", + severity: "warning", + confidence: "high", + file: "models/m.sql", + startLine: line, + title, + body: "body", + evidence: { tool: "dbt.manifest", result: { rule: "risky-join" } }, + }) + + test("a finding on a context line is marked pre-existing and keeps its line", () => { + const [ctx, added] = anchorToDiff([at(11), at(12)], [file]) + expect(ctx.preExisting).toBe(true) + expect(ctx.startLine).toBe(11) + expect(added.preExisting).toBeUndefined() + expect(added.startLine).toBe(12) + }) + + test("a line outside the hunks still loses its anchor and is not marked", () => { + const [far] = anchorToDiff([at(40)], [file]) + expect(far.startLine).toBeUndefined() + expect(far.preExisting).toBeUndefined() + }) + + test("every line of a newly added file counts as added", () => { + const fresh: ChangedFile = { path: "models/m.sql", status: "added", diff: "@@ -0,0 +1,2 @@\n+select 1\n+from x\n" } + expect([...addedLinesByFile([fresh]).get("models/m.sql")!]).toEqual([1, 2]) + expect([...addedLinesByFile([file]).get("models/m.sql")!]).toEqual([12]) + const [f] = anchorToDiff([at(1)], [fresh]) + expect(f.preExisting).toBeUndefined() + }) + + test("pre-existing warnings do not count toward the verdict", () => { + const pre = anchorToDiff([at(10, "m: a"), at(11, "m: b"), at(13, "m: c")], [file]) + expect(pre.every((f) => f.preExisting)).toBe(true) + // Three engine warnings meet the default risk-pattern threshold; pre-existing ones must not. + expect(computeIdealVerdict(pre, DEFAULT_RUBRIC)).toBe("COMMENT") + const fresh = pre.map(({ preExisting: _p, ...f }) => f as Finding) + expect(computeIdealVerdict(fresh, DEFAULT_RUBRIC)).toBe("REQUEST_CHANGES") + }) + + test("mergeRepeats keeps pre-existing and new instances of one rule apart", () => { + const merged = mergeRepeats(anchorToDiff([at(11, "m: risky join on `a`"), at(12, "m: risky join on `c`")], [file])) + expect(merged).toHaveLength(2) + expect(merged.filter((f) => f.preExisting)).toHaveLength(1) + }) +}) diff --git a/packages/opencode/test/altimate/review-select-list.test.ts b/packages/opencode/test/altimate/review-select-list.test.ts index a61aaf2d25..99756b35be 100644 --- a/packages/opencode/test/altimate/review-select-list.test.ts +++ b/packages/opencode/test/altimate/review-select-list.test.ts @@ -1,5 +1,5 @@ import { describe, test, expect } from "bun:test" -import { selectListNames } from "../../src/altimate/review/select-list" +import { finalSelectNames, selectListNames } from "../../src/altimate/review/select-list" describe("final SELECT output names", () => { test("aliases, bare and qualified columns, literals and functions", () => { @@ -13,6 +13,9 @@ describe("final SELECT output names", () => { test("a set operation takes its names from the first branch", () => { expect(selectListNames("select id, a as kind from t union all select id, b as other from u")).toEqual(["id", "kind"]) expect(selectListNames("with c as (select x from t) select x as y from c except select z from d")).toEqual(["y"]) + expect(selectListNames("select id, amount as total from a union all select id, fee as charge from b")).toEqual(["id", "total"]) + expect(selectListNames("with c as (select 1 as z) select 1 as one union select 2")).toEqual(["one"]) + expect(finalSelectNames("with f as (select id from t) select * from f union all select * from g")).toBeNull() }) test("ambiguous items contribute nothing; operators are not aliases", () => { expect(selectListNames("select a + b, c - 1 as d, case when e then 1 end from t")).toEqual(["d"]) @@ -27,3 +30,19 @@ describe("final SELECT output names", () => { expect(selectListNames("select id, 'it''s -- fine' as note /* c, d */ , z from t")).toEqual(["id", "note", "z"]) }) }) + +describe("final SELECT through a CTE", () => { + test("`select * from ` takes that CTE's list, across hops", () => { + const sql = "with src as (select * from raw), final as (select id, sum(x) as total from src group by 1)\nselect * from final" + expect(finalSelectNames(sql)).toEqual(["id", "total"]) + expect(finalSelectNames("with a as (select k, v as w from t), b as (select * from a)\nselect * from b where k > 0")).toEqual(["k", "w"]) + expect(finalSelectNames("select id from t")).toEqual(["id"]) + expect(finalSelectNames("with f as (select id, ')' as p from t)\nselect * from f")).toEqual(["id", "p"]) + }) + test("a star over a table, a join, or a CTE with its own star stays unknown", () => { + expect(finalSelectNames("select * from analytics.customers")).toBeNull() + expect(finalSelectNames("with f as (select id from t) select * from f join g on f.id = g.id")).toBeNull() + expect(finalSelectNames("with f as (select t.*, 1 as one from t) select * from f")).toBeNull() + expect(finalSelectNames("with f as (select * from raw) select * from f")).toBeNull() + }) +}) diff --git a/packages/opencode/test/altimate/review-verdict-trust.test.ts b/packages/opencode/test/altimate/review-verdict-trust.test.ts index 3ccd36eca4..2d7e211da9 100644 --- a/packages/opencode/test/altimate/review-verdict-trust.test.ts +++ b/packages/opencode/test/altimate/review-verdict-trust.test.ts @@ -4,7 +4,8 @@ import path from "node:path" import { tmpdir } from "node:os" import { execFile } from "node:child_process" import { promisify } from "node:util" -import { makeFinding, findingOrigin, type Finding } from "../../src/altimate/review/finding" +import { makeFinding, findingOrigin, fingerprint, ruleId, type Finding } from "../../src/altimate/review/finding" +import { runReview, type ReviewRunner } from "../../src/altimate/review/orchestrate" import { DEFAULT_RUBRIC, Rubric } from "../../src/altimate/review/rubric" import { buildEnvelope, computeIdealVerdict, signEnvelope, verdictEligible, verifyEnvelope } from "../../src/altimate/review/verdict" import { DEFAULT_REVIEW_CONFIG, policyChanges, policyHash } from "../../src/altimate/review/config" @@ -220,3 +221,75 @@ describe("policy comes from the base branch", () => { ) }, 60_000) }) + +describe("every finding carries a stable rule id", () => { + const RULE_ID = /^[a-z0-9.-]+:[a-z0-9.-]+$/ + + test("ruleId normalises both halves", () => { + expect(ruleId("altimate_core.equivalence", "Not Equivalent")).toBe("altimate-core.equivalence:not-equivalent") + expect(ruleId("ai-review", "sql_correctness")).toBe("ai-review:sql-correctness") + expect(ruleId(" rule-catalog ", "--a/b__c--")).toBe("rule-catalog:a-b--c") + }) + + test("a review touching several lanes stamps a well-formed rule on each finding, ids unchanged", async () => { + const sql = "select id, email, amount\nfrom {{ ref('orders') }} o, {{ ref('customers') }} c\nwhere o.id = c.id\n" + const compiled = "select id, email, amount\nfrom db.orders o, db.customers c\nwhere o.id = c.id\n" + const runner: ReviewRunner = { + async impact(model) { + const n = model === "gone" ? 3 : 0 + return { hasManifest: true, severity: n ? "BREAKING" : "SAFE", directCount: n, transitiveCount: n, testCount: 0 } + }, + async grade() { + return { grade: "B" } + }, + async check() { + return { issues: [{ rule: "select_star", message: "avoid select *", severity: "warning" }], ran: true } + }, + async equivalence() { + return { decided: true, equivalent: false, differences: ["row count differs"], confidence: "high" } as any + }, + async detectPii(sql) { + return { columns: sql.includes("email") ? ["email"] : [] } + }, + } + const env = await runReview({ + changedFiles: [ + { path: "models/marts/m.sql", status: "modified", diff: sql.split("\n").map((l) => `+${l}`).join("\n") }, + { path: "models/staging/gone.sql", status: "deleted", diff: "" }, + ], + // Report every rule, including ones the default posting policy turns off. + config: { ...DEFAULT_REVIEW_CONFIG, dialect: "snowflake", rules: { "*": "summary" } }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + forceTier: "full", + runner, + getContent: async (p, side) => + p.endsWith("gone.sql") ? "select 1 as id\n" : side === "old" ? "select id, amount from {{ ref('orders') }}\n" : sql, + getCompiled: async (p, side) => + p.endsWith("gone.sql") ? undefined : side === "old" ? "select id, amount from db.orders\n" : compiled, + }) + const tools = new Set(env.findings.map((x) => x.evidence?.tool)) + // Engine (impact, equivalence, check, PII) and pattern lanes all fired. + for (const t of ["impact_analysis", "altimate_core.equivalence", "altimate_core.check", "schema.detect_pii"]) { + expect(tools.has(t)).toBe(true) + } + expect(env.findings.some((x) => findingOrigin(x) === "catalog")).toBe(true) + for (const x of env.findings) expect(x.rule ?? "").toMatch(RULE_ID) + + const byTool = (t: string) => env.findings.find((x) => x.evidence?.tool === t)! + expect(byTool("impact_analysis").rule).toBe("impact-analysis:downstream-consumers") + expect(byTool("altimate_core.equivalence").rule).toBe("altimate-core.equivalence:not-equivalent") + expect(byTool("altimate_core.check").rule).toBe("altimate-core.check:select-star") + expect(byTool("schema.detect_pii").rule).toBe("schema.detect-pii:exposes") + // The fingerprint ignores `rule`: this id was produced before the field existed. + expect(byTool("impact_analysis").id).toBe( + fingerprint({ category: "lineage_breakage", file: "models/staging/gone.sql", model: "gone", ruleKey: "lineage_breakage:deleted" }), + ) + expect(byTool("impact_analysis").id).toBe("f_c13e1671eb1b007a") + }) + + test("findings without evidence carry no rule", () => { + const x = makeFinding({ severity: "suggestion", category: "sql_quality", title: "t", body: "b", file: "a.sql" }) + expect(x.rule).toBeUndefined() + }) +}) diff --git a/packages/opencode/test/altimate/review.test.ts b/packages/opencode/test/altimate/review.test.ts index 6495fb83c4..e7d345bd09 100644 --- a/packages/opencode/test/altimate/review.test.ts +++ b/packages/opencode/test/altimate/review.test.ts @@ -36,6 +36,10 @@ import { DEFAULT_REVIEW_CONFIG, } from "../../src/altimate/review" +/** Detection tests below exercise rules the default posting policy turns off; + * report every rule so they still see the lane's output. */ +const SHOW_ALL_RULES = { "*": "summary" } as const + // --------------------------------------------------------------------------- // finding.ts // --------------------------------------------------------------------------- @@ -1126,7 +1130,7 @@ describe("orchestrate", () => { } const envCore = await runReview({ changedFiles: files, - config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["sql_quality", "dbt_patterns"] }, + config: { ...DEFAULT_REVIEW_CONFIG, rules: SHOW_ALL_RULES, reviewers: ["sql_quality", "dbt_patterns"] }, rubric: DEFAULT_RUBRIC, mode: "comment", runner: withCore, @@ -1145,7 +1149,7 @@ describe("orchestrate", () => { // Control: core lint NOT available (ran:false) → regex twin is kept (fallback). const noCore = await runReview({ changedFiles: files, - config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["sql_quality", "dbt_patterns"] }, + config: { ...DEFAULT_REVIEW_CONFIG, rules: SHOW_ALL_RULES, reviewers: ["sql_quality", "dbt_patterns"] }, rubric: DEFAULT_RUBRIC, mode: "comment", runner: { ...fakeRunner({}), async check() { return { issues: [], ran: false } } }, @@ -1208,7 +1212,7 @@ describe("orchestrate", () => { } const env = await runReview({ changedFiles: [{ path: "models/marts/m.sql", status: "modified", diff: "+group by" }], - config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["semantic_change"], ai: false }, + config: { ...DEFAULT_REVIEW_CONFIG, rules: SHOW_ALL_RULES, reviewers: ["semantic_change"], ai: false }, rubric: DEFAULT_RUBRIC, mode: "comment", runner, @@ -1261,7 +1265,7 @@ describe("orchestrate", () => { diff: "+on orders.order_id = customers.customer_id\n-on orders.customer_id = customers.customer_id\n", }, ], - config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["semantic_change"], ai: false }, + config: { ...DEFAULT_REVIEW_CONFIG, rules: SHOW_ALL_RULES, reviewers: ["semantic_change"], ai: false }, rubric: DEFAULT_RUBRIC, mode: "gate", runner, @@ -1360,7 +1364,7 @@ describe("orchestrate", () => { } const env = await runReview({ changedFiles: files, - config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["sql_quality", "dbt_patterns"] }, + config: { ...DEFAULT_REVIEW_CONFIG, rules: SHOW_ALL_RULES, reviewers: ["sql_quality", "dbt_patterns"] }, rubric: DEFAULT_RUBRIC, mode: "comment", runner, @@ -1626,7 +1630,7 @@ describe("orchestrate", () => { { path: "models/staging/stg_a.sql", status: "modified", diff: "+x\n" }, { path: "models/staging/stg_b.sql", status: "modified", diff: "+y\n" }, ], - config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["semantic_change"], ai: false }, + config: { ...DEFAULT_REVIEW_CONFIG, rules: SHOW_ALL_RULES, reviewers: ["semantic_change"], ai: false }, rubric: DEFAULT_RUBRIC, mode: "comment", runner, @@ -1664,7 +1668,7 @@ describe("orchestrate", () => { } const env = await runReview({ changedFiles: [{ path: "models/intermediate/int_roles.sql", status: "modified", diff: "+x\n" }], - config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["semantic_change"], ai: false }, + config: { ...DEFAULT_REVIEW_CONFIG, rules: SHOW_ALL_RULES, reviewers: ["semantic_change"], ai: false }, rubric: DEFAULT_RUBRIC, mode: "gate", runner, @@ -1943,7 +1947,7 @@ describe("orchestrate", () => { expect(env.findings.find((f) => f.category === "semantic_change")).toBeUndefined() }) - test("undecidable equivalence → unknown-confidence warning, never critical", async () => { + test("undecidable equivalence → coverage ledger entry, no finding", async () => { const files: ChangedFile[] = [{ path: "models/marts/fct_revenue.sql", status: "modified", diff: "+x\n" }] const runner: ReviewRunner = { ...fakeRunner({}), @@ -1959,10 +1963,11 @@ describe("orchestrate", () => { runner, getContent: content("select 2", "select 1"), }) - const sem = env.findings.find((f) => f.category === "semantic_change") - expect(sem).toBeDefined() - expect(sem!.confidence).toBe("unknown") - expect(sem!.severity).not.toBe("critical") + expect(env.findings.find((f) => f.category === "semantic_change")).toBeUndefined() + expect(env.scope?.notReviewed).toContainEqual({ + path: "models/marts/fct_revenue.sql", + reason: "equivalence could not be proven (no schema or unsupported SQL); verify with a data-diff", + }) }) test("PII exposure → critical pii_exposure finding", async () => { @@ -2295,7 +2300,7 @@ describe("orchestrate", () => { } const env = await runReview({ changedFiles: files, - config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["dbt_patterns"] }, + config: { ...DEFAULT_REVIEW_CONFIG, rules: SHOW_ALL_RULES, reviewers: ["dbt_patterns"] }, rubric: DEFAULT_RUBRIC, mode: "gate", runner, @@ -2331,7 +2336,7 @@ describe("orchestrate", () => { } const env = await runReview({ changedFiles: files, - config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["dbt_patterns"] }, + config: { ...DEFAULT_REVIEW_CONFIG, rules: SHOW_ALL_RULES, reviewers: ["dbt_patterns"] }, rubric: DEFAULT_RUBRIC, mode: "gate", runner, @@ -2402,7 +2407,7 @@ describe("orchestrate", () => { } const env = await runReview({ changedFiles: [{ path: "models/marts/fct_orders.sql", status: "modified", diff: "+group by g, h\n" }], - config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["semantic_change"], ai: false }, + config: { ...DEFAULT_REVIEW_CONFIG, rules: SHOW_ALL_RULES, reviewers: ["semantic_change"], ai: false }, rubric: DEFAULT_RUBRIC, mode: "gate", runner, @@ -2435,7 +2440,7 @@ describe("orchestrate", () => { diff: "+on orders.order_id = customers.customer_id\n", }, ], - config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["semantic_change"], ai: false }, + config: { ...DEFAULT_REVIEW_CONFIG, rules: SHOW_ALL_RULES, reviewers: ["semantic_change"], ai: false }, rubric: DEFAULT_RUBRIC, mode: "gate", runner, @@ -2534,7 +2539,8 @@ describe("orchestrate", () => { "`both-ends`", // both leading + trailing backticks — symmetric padding ], }) - const summary = renderSummary(env) + // A clean run is silent by default; render it to check the tier line. + const summary = renderSummary(env, { allowSilent: false }) // Sanity: the tier line exists expect(summary).toContain("Tier: full") // A path with `single` inside must be wrapped by a fence longer than the @@ -2562,6 +2568,7 @@ describe("orchestrate", () => { file: "models/x.sql", startLine: 5, ruleKey: "r", + tier: "inline", }), ], tier: "lite", @@ -2571,7 +2578,7 @@ describe("orchestrate", () => { const summary = renderSummary(env) expect(summary).toContain("altimate-code-review") expect(summary).toContain("Reviewed with comments") - const inline = inlineComments(env) + const inline = inlineComments(env).comments expect(inline.length).toBe(1) expect(inline[0]).toMatchObject({ path: "models/x.sql", line: 5, side: "RIGHT" }) })