diff --git a/docs/docs/usage/dbt-pr-review.md b/docs/docs/usage/dbt-pr-review.md index 0952e234fd..7fb829c3b5 100644 --- a/docs/docs/usage/dbt-pr-review.md +++ b/docs/docs/usage/dbt-pr-review.md @@ -260,6 +260,8 @@ mode: comment # comment | gate severityThreshold: suggestion manifestPath: target/manifest.json dialect: snowflake +portability: false # cross-engine advice; default off once the engine is known + # (set true for packages that support several adapters) reviewers: [] # empty = risk-tier defaults; or pin lanes dataDiff: # OFF by default โ€” see "Data-diff in CI" below enabled: false @@ -279,6 +281,22 @@ rubric: skipNonProdModels: true ``` +### What the PR comment shows + +- **One summary comment**, updated in place on every push, capped at about + 5,000 characters. Critical findings come first; anything that does not fit is + counted, and the full list is always in the verdict envelope + (`altimate review --json`). Local CLI output is never truncated. +- **Repeated rules are grouped:** the same rule on three or more models is one + bullet listing the models. +- **Inline comments only on lines of the PR's diff.** Findings computed on + compiled SQL, or on unchanged lines, stay in the summary. +- **No re-posting on reruns:** each inline comment carries the finding's + fingerprint, so a push does not repeat findings already on the PR, and a + non-blocking rerun with nothing new only updates the summary. +- **PII:** flagged only for PII columns the change newly exposes, not for + columns the base already surfaced. + ### Risk-tier path tokens Named categories under `riskTierPathTokens` promote any diff touching a matching diff --git a/packages/opencode/src/altimate/review/config.ts b/packages/opencode/src/altimate/review/config.ts index 793121c510..9d370d8062 100644 --- a/packages/opencode/src/altimate/review/config.ts +++ b/packages/opencode/src/altimate/review/config.ts @@ -26,6 +26,14 @@ export const ReviewConfig = z.object({ dialect: z.string().default(""), /** Enable the advisory LLM reviewer lane (needs a configured model). */ ai: z.boolean().default(true), + /** + * Report cross-engine portability advice ("isn't portable across + * warehouses"). Off by default once the engine is known (from the manifest's + * adapter or `dialect`), because a project that runs on one engine gains + * nothing from it. Turn on for packages that support several adapters. + * Functions missing on the project's own engine are always reported. + */ + portability: z.boolean().optional(), /** * Data-diff: actually run base-vs-head against the warehouse (core DataParity) * and report row/value deltas. OPT-IN โ€” it costs warehouse compute and needs a diff --git a/packages/opencode/src/altimate/review/format.ts b/packages/opencode/src/altimate/review/format.ts index a110068e8b..b298b619dc 100644 --- a/packages/opencode/src/altimate/review/format.ts +++ b/packages/opencode/src/altimate/review/format.ts @@ -36,8 +36,20 @@ export function verdictHeadline(env: VerdictEnvelope): string { return `${VERDICT_LABEL[env.verdict]} โ€” ${counts} (${tierLabel})` } +/** Default size budget for the posted PR comment (characters). */ +export const SUMMARY_BUDGET_CHARS = 5_000 +/** The same rule on this many models or more collapses into one bullet. */ +export const GROUP_MIN = 3 + +export interface RenderOptions { + /** Character budget for the comment; findings beyond it are counted, not listed. + * `Infinity` renders everything (local CLI output). */ + maxChars?: number +} + /** Full PR/MR summary comment body (markdown), prefixed with the dedup marker. */ -export function renderSummary(env: VerdictEnvelope): string { +export function renderSummary(env: VerdictEnvelope, opts: RenderOptions = {}): string { + const maxChars = opts.maxChars ?? SUMMARY_BUDGET_CHARS const lines: string[] = [REVIEW_MARKER, "", `## ${verdictHeadline(env)}`, ""] if (env.summary.degraded) { @@ -80,33 +92,15 @@ export function renderSummary(env: VerdictEnvelope): string { lines.push(`> ๐Ÿงญ **Tier: ${env.tier}** โ€” ${shown}${overflow}`, "") } - if (!env.findings.length) { - lines.push("No issues found in the changed dbt models. ๐ŸŽ‰", "") - } else { - const grouped = groupBySeverity(env.findings) - for (const sev of ["critical", "warning", "suggestion"] as const) { - const items = grouped[sev] - if (!items.length) continue - lines.push(`### ${SEVERITY_EMOJI[sev]} ${capitalize(sev)} (${items.length})`, "") - for (const f of items) { - const loc = f.file + (f.startLine ? `:${f.startLine}` : "") - lines.push( - `- **${f.title}** \n ${oneLine(f.body)} \n \`${loc}\`${f.degraded ? " ยท _unverified_" : ""} ยท ${f.category}`, - ) - } - lines.push("") - } - } - + const tail: string[] = [] if (env.override) { - lines.push( + tail.push( `> ๐Ÿ”“ **Break-glass override** by @${env.override.by}: ${env.override.reason}`, `> (would have been \`${env.override.priorVerdict}\`)`, "", ) } - - lines.push( + tail.push( "---", `altimate dbt-pr-review ยท verdict \`${env.verdict}\`` + (env.signature ? ` ยท signed \`${env.signature.slice(0, 18)}โ€ฆ\`` : "") + @@ -115,7 +109,90 @@ export function renderSummary(env: VerdictEnvelope): string { (env.ai?.state === "ok" && env.ai.model ? ` ยท AI \`${env.ai.model}\`` : "") + "", ) - return lines.join("\n") + + if (!env.findings.length) { + lines.push("No issues found in the changed dbt models. ๐ŸŽ‰", "") + return [...lines, ...tail].join("\n") + } + + // 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) + let headed = false + for (const b of bullets) { + const cost = b.text.length + 1 + (headed ? 0 : heading.join("\n").length + 2) + if (used + cost > maxChars) { + hidden += b.count + continue + } + if (!headed) { + lines.push(...heading) + headed = true + } + lines.push(b.text) + used += cost + } + if (headed) lines.push("") + } + if (hidden) { + lines.push( + `> โ€ฆ ${hidden} more finding(s) not shown to keep this comment short โ€” the full list is in the verdict envelope ` + + "(`altimate review --json`, or the action's `--output` artifact).", + "", + ) + } + return [...lines, ...tail].join("\n") +} + +/** A finding's title without its leading `model: `, with backticked names + * blanked, so the same rule on different models/columns shares a key. */ +function ruleTitle(f: Finding): string { + const t = f.model && f.title.startsWith(`${f.model}: `) ? f.title.slice(f.model.length + 2) : f.title + return t.replace(/`[^`]*`/g, "`โ€ฆ`") +} + +/** Render findings of one severity as bullets; a rule repeated on GROUP_MIN or + * more models collapses into a single bullet listing the models. */ +function bulletsFor(items: Finding[]): Array<{ text: string; count: number }> { + const groups = new Map() + for (const f of items) { + const key = `${f.category}|${ruleTitle(f)}` + const g = groups.get(key) + if (g) g.push(f) + else groups.set(key, [f]) + } + const out: Array<{ text: string; count: number }> = [] + for (const g of groups.values()) { + if (g.length >= GROUP_MIN) { + const names = [...new Set(g.map((f) => f.model ?? f.file))] + const shown = names.slice(0, 8).map((n) => `\`${n}\``).join(", ") + const more = names.length > 8 ? ` (+${names.length - 8} more)` : "" + 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}`, + count: g.length, + }) + continue + } + 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}`, + count: 1, + }) + } + } + return out } /** @@ -139,6 +216,11 @@ export function coverageNotes(env: VerdictEnvelope): string[] { } if (env.scope?.outOfScopeFiles) notes.push(`${env.scope.outOfScopeFiles} changed file(s) outside the dbt project were not reviewed.`) + if (env.scope?.movedUnchangedFiles) + 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.", + ) 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}` : ""}.`) @@ -154,14 +236,28 @@ export interface InlineComment { body: string } -export function inlineComments(env: VerdictEnvelope): InlineComment[] { +/** 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. */ +export function findingMarker(id: string): string { + return `` +} + +/** Finding ids already posted, read from existing inline comment bodies. */ +export function postedFindingIds(bodies: Array): Set { + const out = new Set() + for (const b of bodies) for (const m of (b ?? "").matchAll(//g)) out.add(m[1]) + return out +} + +export function inlineComments(env: VerdictEnvelope, alreadyPosted: Set = new Set()): InlineComment[] { return env.findings - .filter((f) => typeof f.startLine === "number") + .filter((f) => typeof f.startLine === "number" && !alreadyPosted.has(f.id)) .map((f) => ({ path: f.file, line: f.startLine!, side: "RIGHT" as const, - body: `${SEVERITY_EMOJI[f.severity]} **${f.title}**\n\n${f.body}`, + body: `${SEVERITY_EMOJI[f.severity]} **${f.title}**\n\n${f.body}\n\n${findingMarker(f.id)}`, })) } diff --git a/packages/opencode/src/altimate/review/orchestrate.ts b/packages/opencode/src/altimate/review/orchestrate.ts index 48e979e1ff..515ed2e4d2 100644 --- a/packages/opencode/src/altimate/review/orchestrate.ts +++ b/packages/opencode/src/altimate/review/orchestrate.ts @@ -206,6 +206,65 @@ export interface OrchestrateInput { layout?: DbtProjectLayout /** What the review covered: base resolution and files left out of scope. */ scope?: Omit + /** Drop the regex catalog's cross-engine portability findings (single known engine). */ + suppressPortability?: boolean +} + +/** A catalog finding tagged as cross-engine portability advice. */ +export function isCatalogPortability(f: Finding): boolean { + return f.evidence?.tool === "rule-catalog" && (f.evidence.result as { portability?: boolean })?.portability === true +} + +/** + * Portability advice that is noise for a project on `dialect`: dropped unless + * the rule names the engines that support the construct and `dialect` isn't + * one of them โ€” then it is an error on this project's own engine. + */ +export function isSuppressiblePortability(f: Finding, dialect: string): boolean { + if (!isCatalogPortability(f)) return false + const worksOn = (f.evidence?.result as { worksOn?: string[] })?.worksOn + return !worksOn || worksOn.includes(dialect.toLowerCase()) +} + +/** 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 out +} + +/** + * 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. + */ +export function anchorToDiff(findings: Finding[], files: ChangedFile[]): Finding[] { + const lines = new Map(files.map((f) => [f.path, commentableLines(f.diff)])) + return findings.map((f) => { + if (typeof f.startLine !== "number") return f + if (lines.get(f.file)?.has(f.startLine)) return f + const { startLine: _s, endLine: _e, ...rest } = f + return rest as Finding + }) } /** True when SQL text contains Jinja (`{{ }}`, `{% %}` or `{# #}`). */ @@ -395,6 +454,8 @@ async function qualityLane( sql: string | undefined, dialect: string, oldSql?: string, + /** True when `sql` is the raw file; compiled-SQL line numbers do not exist in the PR's file. */ + linesMatchFile = false, ): Promise { if (!sql || file.status === "deleted") return [] const model = modelNameFromPath(file.path) @@ -432,8 +493,8 @@ async function qualityLane( body: issue.message, file: file.path, model, - startLine: issue.line, - endLine: issue.line, + startLine: linesMatchFile ? issue.line : undefined, + endLine: linesMatchFile ? issue.line : undefined, 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. @@ -1117,7 +1178,8 @@ export async function runReview(input: OrchestrateInput): Promise 0 ? !anyManifest : reviewable.length === 0 + // Nothing to resolve (yml-only, pure moves) is not a lint-only run. + const runDegraded = modelFiles.length > 0 && !anyManifest // High-risk path tokens are user-configured (billing/pci/patient/etc.) โ€” // the reviewer core carries no default list. `undefined` when no @@ -1186,7 +1248,9 @@ export async function runReview(input: OrchestrateInput): Promise[] = [] // Engine lanes consume COMPILED SQL (rendered by dbt) when available. if (lanes.has("sql_quality") || lanes.has("warehouse_cost")) - tasks.push(qualityLane(ctx.file, input.runner, ctx.engineNewSql, dialect, ctx.engineOldSql)) + tasks.push( + qualityLane(ctx.file, input.runner, ctx.engineNewSql, dialect, ctx.engineOldSql, ctx.engineNewSql === ctx.newSql), + ) if (lanes.has("semantic_change")) { tasks.push(dataDiffLane(ctx, input.runner, dialect, input.config.dataDiff)) tasks.push(grainMismatchLane(ctx, input.runner)) @@ -1228,9 +1292,17 @@ export async function runReview(input: OrchestrateInput): Promise !diffScopedPii.classifiedColumns.has(col.toLowerCase())) : currentCtx.pii + // Only columns this PR newly exposes: a PII column the base already + // surfaced is not this change's concern (the docs promise "newly expose"). + if (fallbackPii.length && hasOldSide(currentCtx.file) && currentCtx.engineOldSql) { + const basePii = new Set( + (await input.runner.detectPii(currentCtx.engineOldSql, dialect)).columns.map((c) => c.toLowerCase()), + ) + fallbackPii = fallbackPii.filter((col) => !basePii.has(col.toLowerCase())) + } findings.push(...piiLane(currentCtx.file, fallbackPii)) } return findings @@ -1448,6 +1520,11 @@ export async function runReview(input: OrchestrateInput): Promise !exclusionReason(f, input.rubric)) + // 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) findings = findings.filter((f) => !isSuppressiblePortability(f, dialect)) + findings = anchorToDiff(findings, input.changedFiles) const minSev = SEVERITY_ORDER[input.config.severityThreshold] findings = findings.filter((f) => SEVERITY_ORDER[f.severity] >= minSev) // Sort by severity desc, then file. diff --git a/packages/opencode/src/altimate/review/post-github.ts b/packages/opencode/src/altimate/review/post-github.ts index 9ef43286f5..9a34c83199 100644 --- a/packages/opencode/src/altimate/review/post-github.ts +++ b/packages/opencode/src/altimate/review/post-github.ts @@ -1,7 +1,7 @@ import { Octokit } from "@octokit/rest" import { promises as fs } from "node:fs" import { type VerdictEnvelope, VCS_EVENT } from "./verdict" -import { renderSummary, inlineComments, REVIEW_MARKER, verdictHeadline } from "./format" +import { renderSummary, inlineComments, postedFindingIds, REVIEW_MARKER, verdictHeadline } from "./format" /** * Post a verdict envelope to a GitHub pull request: an upserted summary comment @@ -54,8 +54,16 @@ export interface PostResult { inlineFellBack: boolean /** Set when the review event could not be posted for a non-positioning reason. */ postError?: string + /** Inline comments not re-posted because the same finding is already on the PR. */ + inlineSkippedAsPosted?: number + /** True when no review was submitted: nothing new to say and a previous + * non-blocking altimate review already exists (the summary comment was updated). */ + reviewSkipped?: boolean } +/** Headline of a review this bot posted (see verdictHeadline). */ +const BOT_REVIEW_RE = /(Approved|Reviewed with comments|Changes requested)\b.*\((trivial|lite|full) tier/ + export async function postGitHubReview(env: VerdictEnvelope, target: GitHubTarget): Promise { const octo = new Octokit({ auth: target.token }) const { owner, repo, prNumber } = target @@ -85,9 +93,34 @@ export async function postGitHubReview(env: VerdictEnvelope, target: GitHubTarge // 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 comments = inlineComments(env) const reviewBody = verdictHeadline(env) + // 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 + 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)) + priorBotReview = reviews.some((r) => BOT_REVIEW_RE.test(r.body ?? "")) + } 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 + + // 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. + if (!comments.length && event === "COMMENT" && priorBotReview) { + result.reviewSkipped = true + return result + } + // Anchor inline comments to the PR head commit. Without commit_id, GitHub's // line-positioning is fragile and can 422 even on valid lines. let commitId: string | undefined diff --git a/packages/opencode/src/altimate/review/rule-catalog.ts b/packages/opencode/src/altimate/review/rule-catalog.ts index 0d8313c458..98360039a7 100644 --- a/packages/opencode/src/altimate/review/rule-catalog.ts +++ b/packages/opencode/src/altimate/review/rule-catalog.ts @@ -44,6 +44,12 @@ export interface Rule { unless?: RegExp kind?: RuleKind pathGlob?: RegExp + /** + * Cross-engine portability advice: dropped for a project on one known + * 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[] } example: RuleExample counter?: RuleExample } @@ -122,7 +128,10 @@ export function evaluateCatalog( file: file.path, model, confidence: "high", - evidence: { tool: "rule-catalog", result: { rule: rule.id } }, + evidence: { + tool: "rule-catalog", + result: rule.portability ? { rule: rule.id, portability: true, worksOn: rule.portability.worksOn } : { rule: rule.id }, + }, ruleKey: `${rule.category}:${rule.id}`, }) if (!exclusionReason(f, rubric)) matched.push({ rule, finding: f }) @@ -167,7 +176,7 @@ const r = ( severity: Severity, title: string, body: string, - match: Pick, + match: Pick, example: RuleExample, counter?: RuleExample, ): Rule => ({ id, category, severity, title, body, ...match, example, counter }) @@ -585,7 +594,7 @@ const AGG: Rule[] = [ "suggestion", "COUNT(DISTINCT a, b) portability", "Multi-column COUNT(DISTINCT) isn't supported on all warehouses; concat or use a subquery.", - { added: /count\s*\(\s*distinct\s+[^),]+,/i }, + { added: /count\s*\(\s*distinct\s+[^),]+,/i, portability: { worksOn: ["mysql", "snowflake", "databricks"] } }, { add: "count(distinct a, b)" }, { add: "count(distinct a)" }, ), @@ -1703,7 +1712,7 @@ const STYLE: Rule[] = [ "suggestion", "PIVOT/UNPIVOT is dialect-specific", "`PIVOT`/`UNPIVOT` syntax varies by warehouse and is hard to test; consider conditional aggregation.", - { added: /\b(pivot|unpivot)\s*\(/i }, + { added: /\b(pivot|unpivot)\s*\(/i, portability: {} }, { add: "from t pivot(sum(x) for k in ('a'))" }, { add: "sum(case when k='a' then x end)" }, ), @@ -1723,7 +1732,7 @@ const STYLE: Rule[] = [ "suggestion", "GROUP BY on a SELECT alias", "Grouping by a select-list alias isn't portable (errors on some warehouses); repeat the expression.", - { added: /\bgroup\s+by\s+\w+_(amt|amount|total|cnt|count)\b/i }, + { added: /\bgroup\s+by\s+\w+_(amt|amount|total|cnt|count)\b/i, portability: { worksOn: ["snowflake", "bigquery", "postgres", "redshift", "databricks", "duckdb", "mysql", "clickhouse"] } }, { add: "group by total_amount" }, { add: "group by customer_id" }, ), @@ -1733,7 +1742,7 @@ const STYLE: Rule[] = [ "suggestion", "string interval literal portability", "`interval '1 day'` literal syntax differs across warehouses; prefer dateadd/date_add functions.", - { added: /interval\s+'\d+\s*\w+'/i }, + { added: /interval\s+'\d+\s*\w+'/i, portability: {} }, { add: "d + interval '1 day'" }, { add: "dateadd(day, 1, d)" }, ), @@ -1743,7 +1752,7 @@ const STYLE: Rule[] = [ "suggestion", "integer arithmetic on a date", "`date + 7` (bare int) isn't portable and is ambiguous (days? seconds?); use dateadd/interval.", - { added: /\b\w*(date|_at|_dt)\b\s*[+\-]\s*\d+\b/i }, + { added: /\b\w*(date|_at|_dt)\b\s*[+\-]\s*\d+\b/i, portability: { worksOn: ["postgres", "redshift", "snowflake", "duckdb", "oracle"] } }, { add: "select order_date + 7" }, { add: "select dateadd(day, 7, order_date)" }, ), @@ -1866,7 +1875,7 @@ const MISC: Rule[] = [ "suggestion", "DISTINCT ON is Postgres-specific", "`DISTINCT ON (โ€ฆ)` isn't portable; use `qualify row_number()` for first-row-per-group.", - { added: /distinct\s+on\s*\(/i }, + { added: /distinct\s+on\s*\(/i, portability: { worksOn: ["postgres", "duckdb"] } }, { add: "select distinct on (id) * from t" }, { add: "qualify row_number() over (partition by id order by d) = 1" }, ), @@ -1876,7 +1885,7 @@ const MISC: Rule[] = [ "suggestion", "FETCH FIRST portability", "`FETCH FIRST n ROWS` isn't supported everywhere; `LIMIT` (with ORDER BY) is more portable.", - { added: /fetch\s+first\s+\d+/i }, + { added: /fetch\s+first\s+\d+/i, portability: { worksOn: ["postgres", "snowflake", "oracle", "trino"] } }, { add: "fetch first 10 rows only" }, { add: "limit 10" }, ), @@ -2299,7 +2308,7 @@ const MISC2: Rule[] = [ "suggestion", "non-portable type name (TEXT)", "`cast(x as text)` isn't supported on all warehouses; use `string`/`varchar`.", - { added: /\bas\s+text\b/i }, + { added: /\bas\s+text\b/i, portability: { worksOn: ["postgres", "redshift", "duckdb", "snowflake"] } }, { add: "cast(x as text)" }, { add: "cast(x as string)" }, ), diff --git a/packages/opencode/src/altimate/review/run.ts b/packages/opencode/src/altimate/review/run.ts index 5bd581f204..21cdc1ba09 100644 --- a/packages/opencode/src/altimate/review/run.ts +++ b/packages/opencode/src/altimate/review/run.ts @@ -318,7 +318,9 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise // Resolve the SQL dialect: explicit config wins; otherwise auto-detect from // the dbt manifest's `adapter_type` (so a BigQuery/Redshift project isn't // analyzed as the snowflake default โ€” wrong-dialect portability suppression). - if (!config.dialect) config.dialect = (await detectDialect(manifestAbs)) ?? "snowflake" + const detectedDialect = config.dialect ? undefined : await detectDialect(manifestAbs) + const dialectKnown = !!config.dialect || !!detectedDialect + if (!config.dialect) config.dialect = detectedDialect ?? "snowflake" // Prefer dbt's catalog.json (real warehouse columns from `dbt docs generate`) // for the schema context โ€” complete columns are what make column-lineage @@ -389,6 +391,7 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise forceTier: opts.forceTier, staleManifest, layout, + suppressPortability: config.portability === undefined ? dialectKnown : !config.portability, scope: { baseRef: resolved?.ref ?? opts.base, baseSha: resolved?.sha, diff --git a/packages/opencode/src/altimate/review/telemetry.ts b/packages/opencode/src/altimate/review/telemetry.ts index 34016e8a50..3fbe4af1bb 100644 --- a/packages/opencode/src/altimate/review/telemetry.ts +++ b/packages/opencode/src/altimate/review/telemetry.ts @@ -71,7 +71,8 @@ export function classifyReviewFailure(err: unknown): "config_error" | "git_error * caller reports `summary_failed` for that. */ export function classifyPostOutcome(result: PostResult): "full" | "partial" { - if (result.inlineFellBack || result.postError || result.reviewId === undefined) return "partial" + if (result.inlineFellBack || result.postError) return "partial" + if (result.reviewId === undefined && !result.reviewSkipped) return "partial" return "full" } diff --git a/packages/opencode/src/altimate/tools/dbt-pr-review.ts b/packages/opencode/src/altimate/tools/dbt-pr-review.ts index ab3ceab013..47d68a6549 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), + output: renderSummary(env, { maxChars: Infinity }), } }, }) diff --git a/packages/opencode/src/cli/cmd/review.ts b/packages/opencode/src/cli/cmd/review.ts index e35556aa0b..cef6942c20 100644 --- a/packages/opencode/src/cli/cmd/review.ts +++ b/packages/opencode/src/cli/cmd/review.ts @@ -153,7 +153,8 @@ export const ReviewCommand = cmd({ if (args.json) { process.stdout.write(JSON.stringify(env, null, 2) + "\n") } else { - process.stdout.write(renderSummary(env) + "\n") + // Local output is complete; the size budget applies to the posted comment. + process.stdout.write(renderSummary(env, { maxChars: Infinity }) + "\n") } if (args.post) { @@ -191,7 +192,9 @@ export const ReviewCommand = cmd({ } else { UI.println( `Posted review to ${where}` + - (r.inlineFellBack ? " (inline comments fell back to summary-only)" : ""), + (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)" : ""), ) } } diff --git a/packages/opencode/test/altimate/review-noise.test.ts b/packages/opencode/test/altimate/review-noise.test.ts new file mode 100644 index 0000000000..dfd0208133 --- /dev/null +++ b/packages/opencode/test/altimate/review-noise.test.ts @@ -0,0 +1,271 @@ +import { describe, test, expect } from "bun:test" +import { + anchorToDiff, + commentableLines, + isCatalogPortability, + isSuppressiblePortability, + 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 { makeFinding, type Finding } from "../../src/altimate/review/finding" +import { + findingMarker, + inlineComments, + postedFindingIds, + renderSummary, + SUMMARY_BUDGET_CHARS, +} from "../../src/altimate/review/format" +import { buildEnvelope } from "../../src/altimate/review/verdict" +import { evaluateCatalog } from "../../src/altimate/review/rule-catalog" +import { classifyPostOutcome } from "../../src/altimate/review/telemetry" + +/** + * The PR comment must stay readable: inline comments land on real lines of the + * PR's file and are not re-posted on every push, a rule repeated across many + * models is one bullet, the comment has a size budget, and cross-engine + * portability advice is dropped for a single-engine project. + */ + +function finding(over: Partial[0]> = {}): Finding { + return makeFinding({ + severity: "suggestion", + category: "test_coverage", + title: "m: new model has no uniqueness/grain test", + body: "Add a unique or unique_combination_of_columns test.", + file: "models/m.sql", + model: "m", + confidence: "high", + evidence: { tool: "test-coverage", result: {} }, + ...over, + }) +} + +const DIFF = [ + "diff --git a/models/m.sql b/models/m.sql", + "--- a/models/m.sql", + "+++ b/models/m.sql", + "@@ -1,4 +1,5 @@", + " select", + " id", + "- , old_col", + "+ , new_col", + "+ , other_col", + "", + " from src", +].join("\n") + +describe("inline anchors", () => { + test("commentable lines are the head-side added and context lines inside hunks", () => { + // 1 select, 2 id, 3 new_col, 4 other_col, 5 blank context, 6 from src + expect([...commentableLines(DIFF)].sort((a, b) => a - b)).toEqual([1, 2, 3, 4, 5, 6]) + expect(commentableLines(undefined).size).toBe(0) + }) + + test("an added SQL line starting with `++` still counts; headers outside hunks do not", () => { + const d = "--- a/x.sql\n+++ b/x.sql\n@@ -0,0 +1,2 @@\n+select 1\n++ 1 as weird\n" + expect([...commentableLines(d)].sort()).toEqual([1, 2]) + }) + + test("a line outside the diff moves to the summary instead of failing the batch", () => { + const inDiff = finding({ startLine: 3, endLine: 3, title: "m: a" }) + const outside = finding({ startLine: 40, endLine: 40, title: "m: b" }) + const [a, b] = anchorToDiff([inDiff, outside], [{ path: "models/m.sql", status: "modified", diff: DIFF }]) + expect(a.startLine).toBe(3) + expect(b.startLine).toBeUndefined() + expect(b.endLine).toBeUndefined() + }) + + test("engine lint on compiled SQL never anchors to the raw file", async () => { + const runner = { + async impact() { + return { hasManifest: true, severity: "SAFE", directCount: 0, transitiveCount: 0, testCount: 0 } + }, + async grade() { + return { grade: "B" } + }, + async check() { + return { issues: [{ rule: "select-star", message: "avoid select *", severity: "warning", line: 2 }], ran: true } + }, + async equivalence() { + return { decided: true, equivalent: true } + }, + async detectPii() { + return { columns: [] } + }, + } as ReviewRunner + const env = await runReview({ + changedFiles: [{ path: "models/m.sql", status: "added", diff: "@@ -0,0 +1,3 @@\n+{{ config() }}\n+select *\n+from {{ ref('a') }}\n" }], + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["sql_quality"] }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + getContent: async () => "{{ config() }}\nselect *\nfrom {{ ref('a') }}\n", + getCompiled: async () => "\n\n\nselect *\nfrom db.a\n", + }) + const f = env.findings.find((x) => x.evidence?.tool === "altimate_core.check") + expect(f).toBeDefined() + expect(f!.startLine).toBeUndefined() + }) +}) + +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 }) + expect(a.id).toBe(moved.id) + const env = buildEnvelope({ findings: [a], tier: "lite", mode: "comment" }) + expect(inlineComments(env)[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 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]) + }) + + test("a skipped repeat review counts as a full post in telemetry", () => { + expect(classifyPostOutcome({ inlineFellBack: false, reviewSkipped: true })).toBe("full") + expect(classifyPostOutcome({ inlineFellBack: false })).toBe("partial") + }) +}) + +describe("summary grouping and budget", () => { + test("the same rule on three or more models is one bullet listing the models", () => { + const fs = ["a", "b", "c", "d"].map((m) => + finding({ model: m, file: `models/${m}.sql`, title: `${m}: new model has no uniqueness/grain test` }), + ) + const md = renderSummary(buildEnvelope({ findings: fs, tier: "lite", mode: "comment" })) + expect(md.match(/no uniqueness\/grain test/g)?.length).toBe(1) + expect(md).toContain("4 findings across 4 model(s): `a`, `b`, `c`, `d`") + }) + + test("two occurrences stay as separate bullets", () => { + const fs = ["a", "b"].map((m) => finding({ model: m, file: `models/${m}.sql`, title: `${m}: new model has no uniqueness/grain test` })) + const md = renderSummary(buildEnvelope({ findings: fs, tier: "lite", mode: "comment" })) + expect(md.match(/no uniqueness\/grain test/g)?.length).toBe(2) + }) + + test("backticked column names do not split a group", () => { + const fs = ["a", "b", "c"].map((m) => + finding({ model: m, file: `models/${m}.sql`, title: `${m}: grain column \`${m}_id\` lacks not_null`, category: "test_coverage" }), + ) + const md = renderSummary(buildEnvelope({ findings: fs, tier: "lite", mode: "comment" })) + expect(md).toContain("3 findings across 3 model(s)") + }) + + test("the posted comment stays within budget, most severe first, and counts what it hides", () => { + const many: Finding[] = [] + for (let i = 0; i < 200; i++) + many.push( + finding({ + severity: i < 3 ? "critical" : "suggestion", + category: i < 3 ? "lineage_breakage" : "sql_quality", + model: `m${i}`, + file: `models/m${i}.sql`, + title: `m${i}: distinct issue number ${i}`, + body: "x".repeat(120), + }), + ) + const env = buildEnvelope({ findings: many, tier: "full", mode: "comment" }) + const md = renderSummary(env) + expect(md.length).toBeLessThanOrEqual(SUMMARY_BUDGET_CHARS) + expect(md).toContain("m0: distinct issue number 0") + expect(md).toMatch(/โ€ฆ \d+ more finding\(s\) not shown/) + // Local output is complete. + expect(renderSummary(env, { maxChars: Infinity })).toContain("m199: distinct issue number 199") + }) +}) + +describe("portability noise", () => { + const portability = finding({ + severity: "suggestion", + category: "sql_quality", + title: "m: DISTINCT ON isn't portable", + body: "`DISTINCT ON (โ€ฆ)` isn't portable; use `qualify row_number()` for first-row-per-group.", + evidence: { tool: "rule-catalog", result: { rule: "distinct-on", portability: true, worksOn: ["postgres", "duckdb"] } }, + }) + const coreMissing = finding({ + category: "sql_quality", + title: "m: non_portable_function", + body: "GETDATE() is a non-standard / dialect-specific function.", + evidence: { tool: "altimate_core.check", result: { rule: "non_portable_function" } }, + }) + + test("only explicitly tagged rules are portability advice, not rules that mention standard SQL", async () => { + const legacy = evaluateCatalog( + { path: "models/m.sql", status: "added" }, + "select * from [proj:ds.t]", + ["select * from [proj:ds.t]"], + [], + DEFAULT_RUBRIC, + ).find((f) => (f.evidence?.result as { rule: string }).rule === "bq-legacy-sql") + expect(legacy).toBeDefined() + expect(isCatalogPortability(legacy!)).toBe(false) + }) + + test("a construct that fails on the project's own engine is kept", () => { + expect(isSuppressiblePortability(portability, "postgres")).toBe(true) + expect(isSuppressiblePortability(portability, "snowflake")).toBe(false) + const untagged = { ...portability, evidence: { tool: "rule-catalog", result: { rule: "pivot-portability", portability: true } } } + expect(isSuppressiblePortability(untagged, "snowflake")).toBe(true) + }) + + test("catalog portability advice is recognised; core's dialect-aware check is not", () => { + expect(isCatalogPortability(portability)).toBe(true) + expect(isCatalogPortability(coreMissing)).toBe(false) + }) + + test("a single known engine drops portability advice it supports, keeps what fails on it", async () => { + 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 + const sql = "select distinct on (customer_id) customer_id, order_id from orders" + const base = { + changedFiles: [{ path: "models/m.sql", status: "added" as const, diff: `+${sql}\n` }], + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["dbt_patterns"], dialect: "snowflake" }, + rubric: DEFAULT_RUBRIC, + mode: "comment" as const, + runner, + getContent: async () => sql, + } + const kept = await runReview(base) + expect(kept.findings.filter(isCatalogPortability).length).toBeGreaterThan(0) + // Postgres supports DISTINCT ON: the advice is noise there and is dropped. + const pg = { ...base, config: { ...base.config, dialect: "postgres" } } + expect((await runReview({ ...pg, suppressPortability: true })).findings.filter(isCatalogPortability).length).toBe(0) + // Snowflake doesn't: on a Snowflake project it is a real error and stays. + expect((await runReview({ ...base, suppressPortability: true })).findings.filter(isCatalogPortability).length).toBeGreaterThan(0) + }) +}) + +describe("moved files", () => { + test("moves are named in the comment with the folder-config caveat, and are not a lint-only run", async () => { + const env = buildEnvelope({ + findings: [], + tier: "trivial", + mode: "comment", + scope: { movedUnchangedFiles: 3 } as any, + }) + expect(renderSummary(env)).toContain("3 file(s) moved without edits") + expect(renderSummary(env)).toContain("`+schema`") + }) +}) diff --git a/packages/opencode/test/altimate/review.test.ts b/packages/opencode/test/altimate/review.test.ts index 113a2202fd..2ec9445a26 100644 --- a/packages/opencode/test/altimate/review.test.ts +++ b/packages/opencode/test/altimate/review.test.ts @@ -1967,8 +1967,8 @@ describe("orchestrate", () => { const files: ChangedFile[] = [{ path: "models/marts/dim_customers.sql", status: "modified", diff: "+email\n" }] const runner: ReviewRunner = { ...fakeRunner({}), - async detectPii() { - return { columns: ["email", "ssn"] } + async detectPii(sql: string) { + return { columns: ["email", "ssn"].filter((c) => sql.includes(c)) } }, } const env = await runReview({ @@ -1977,7 +1977,7 @@ describe("orchestrate", () => { rubric: DEFAULT_RUBRIC, mode: "gate", runner, - getContent: content("select email, ssn from x"), + getContent: content("select email, ssn from x", "select id from x"), }) const pii = env.findings.find((f) => f.category === "pii_exposure") expect(pii).toBeDefined() @@ -2024,7 +2024,7 @@ describe("orchestrate", () => { expect(env.verdict).toBe("REQUEST_CHANGES") }) - test("broad PII detector remains fallback for existing output columns", async () => { + test("a PII column the base already exposed is not re-flagged when the PR only touches it", async () => { const files: ChangedFile[] = [{ path: "models/marts/dim_customers.sql", status: "modified", diff: "+ lower(email) as email\n" }] const runner: ReviewRunner = { ...fakeRunner({}), @@ -2056,18 +2056,18 @@ describe("orchestrate", () => { runner, getContent: content("select customer_id, lower(email) as email from customers", "select customer_id, email from customers"), }) + // `email` was already in the base's output: this PR does not newly expose it. const pii = env.findings.filter((f) => f.category === "pii_exposure") - expect(pii).toHaveLength(1) - expect(pii[0].evidence?.tool).toBe("schema.detect_pii") - expect(env.verdict).toBe("REQUEST_CHANGES") + expect(pii).toHaveLength(0) + expect(env.verdict).not.toBe("REQUEST_CHANGES") }) test("PII fallback survives core lineage failure instead of aborting review", async () => { const files: ChangedFile[] = [{ path: "models/marts/dim_customers.sql", status: "modified", diff: "+ email\n" }] const runner: ReviewRunner = { ...fakeRunner({}), - async detectPii() { - return { columns: ["email"] } + async detectPii(sql: string) { + return { columns: sql.includes("email") ? ["email"] : [] } }, async columnLineage() { throw new Error("lineage parser crashed") @@ -2098,8 +2098,8 @@ describe("orchestrate", () => { const files: ChangedFile[] = [{ path: "models/marts/dim_customers.sql", status: "modified", diff: "+ email\n" }] const runner: ReviewRunner = { ...fakeRunner({}), - async detectPii() { - return { columns: ["email"] } + async detectPii(sql: string) { + return { columns: sql.includes("email") ? ["email"] : [] } }, async columnLineage(sql) { return sql.includes("email") @@ -2205,8 +2205,8 @@ describe("orchestrate", () => { const files: ChangedFile[] = [{ path: "models/marts/dim_customers.sql", status: "modified", diff: "+ ssn\n" }] const runner: ReviewRunner = { ...fakeRunner({}), - async detectPii() { - return { columns: ["ssn"] } + async detectPii(sql: string) { + return { columns: sql.includes("ssn") ? ["ssn"] : [] } }, async columnLineage(sql) { return sql.includes("ssn")