diff --git a/docs/docs/usage/dbt-pr-review.md b/docs/docs/usage/dbt-pr-review.md index c1fc58c71a..077bf0b55e 100644 --- a/docs/docs/usage/dbt-pr-review.md +++ b/docs/docs/usage/dbt-pr-review.md @@ -108,7 +108,7 @@ Options: | Flag | Description | |------|-------------| -| `--base ` | Base git ref. Defaults to the pull request's target branch in CI (GitHub `pull_request.base.ref` or `GITHUB_BASE_REF`, GitLab `CI_MERGE_REQUEST_TARGET_BRANCH_NAME`), else the remote default branch. Always reduced to the merge-base with the head, so commits the base gained after the PR branched are never reviewed. In CI, a target branch that is not fetched is an error (use `fetch-depth: 0`). | +| `--base ` | Base git ref. Defaults to the pull request's target branch in CI (GitHub `pull_request.base.ref` or `GITHUB_BASE_REF`, GitLab `CI_MERGE_REQUEST_TARGET_BRANCH_NAME`), else the remote default branch. Always reduced to the merge-base with the head, so commits the base gained after the PR branched are never reviewed. In CI, a shallow checkout is handled: the target branch is fetched and the history deepened until the merge-base is found. If that fails (no `origin`, no access), the review stops with an error rather than diffing against another branch. | | `--head ` | Head git ref. Omit to review the working tree. | | `--manifest ` | Path to the compiled `manifest.json`. When omitted, the reviewer walks up from the current directory to find the nearest `dbt_project.yml` and uses its adjacent `target/manifest.json`; the discovered path is logged to stderr. | | `--mode comment\|gate` | `comment` never blocks; `gate` exits non-zero on `REQUEST_CHANGES`. | @@ -203,7 +203,7 @@ jobs: | Input | How to produce it | Unlocks | |---|---|---| -| Full git history of the PR's target branch | `actions/checkout` with `fetch-depth: 0` | Correct base: the diff starts at the merge-base with the PR's target branch. A target branch that is not fetched is an error, never a silent fallback to `main`. | +| The PR's target branch and the merge-base | Fetched automatically from `origin` when the checkout is shallow; `fetch-depth: 0` avoids the extra fetches | Correct base: the diff starts at the merge-base with the PR's target branch. When it can't be fetched, the review stops with an error, never a silent fallback to `main`. | | `target/manifest.json` | `dbt compile` (or `dbt parse` for lint + lineage only) | Blast radius, lineage breakage, tiering. Without it the run is lint-only. | | `target/compiled/…` | `dbt compile` | Engine checks on the head (lint, grade, PII, grain). | | `target-base/compiled/…` | Compile the base ref into `target-base/` (below) | Equivalence proofs and column-breakage for modified models. | @@ -303,6 +303,13 @@ rubric: rules) or `ai`. Only `engine` findings count toward REQUEST_CHANGES. A pattern rule you trust can be allowed to block by naming its id in `rubric.blockingRules`. The advisory AI lane never blocks. +- **Upgrading a `gate` setup.** Before this policy, any critical finding in a + `blockOn` category could block, including pattern rules such as the regex + PII check. Now pattern-rule findings comment but don't block. To keep a + pattern rule blocking, add its id to `rubric.blockingRules`. A blocking + review is dismissed only by a later run that completed: when an engine call + or a file failed, or a check (such as PII) could not analyse a model, the + earlier block stays. ### What the PR comment shows @@ -320,7 +327,17 @@ rubric: - **PII:** flagged only for PII columns the change newly exposes, not for columns the base already surfaced. That comparison needs the base version's SQL; when it can't be compiled, PII columns are reported as non-blocking - warnings that may predate the PR. + warnings that may predate the PR. A model the PII check could not analyse is + listed as not fully verified. +- **Failures are never a clean pass:** if an engine check fails, a file's + review fails, or a check could not analyse a model, the comment names it and + the verdict is at most COMMENT, never APPROVE (envelope `engineErrors` and + `scope.unverified`). A failure on one file marks that file unverified and the + rest of the PR is still reviewed. +- **The gate clears itself:** when a completed rerun on the PR's current head + no longer blocks, the earlier blocking altimate review for the same dbt + project is dismissed after the new review is posted (needs + `pull-requests: write`). ### Risk-tier path tokens diff --git a/packages/opencode/src/altimate/review/format.ts b/packages/opencode/src/altimate/review/format.ts index 1b8f6be575..48522a4a25 100644 --- a/packages/opencode/src/altimate/review/format.ts +++ b/packages/opencode/src/altimate/review/format.ts @@ -208,16 +208,23 @@ export function coverageNotes(env: VerdictEnvelope): string[] { const undecided = env.findings.filter((f) => f.degraded).length if (undecided) notes.push(`${undecided} finding(s) could not be decided by the engine and are marked _unverified_.`) const unverified = env.scope?.unverified ?? [] - if (unverified.length) { - // One line per reason: models can lack the head compile or the base compile. - const byReason = new Map() - for (const u of unverified) byReason.set(u.reason, [...(byReason.get(u.reason) ?? []), u.path]) - for (const [reason, paths] of byReason) { - const head = paths.slice(0, 5).map((p) => `\`${p}\``).join(", ") - const more = paths.length > 5 ? ` and ${paths.length - 5} more` : "" - notes.push(`${paths.length} model(s) not verified by the engine — ${reason}: ${head}${more}.`) - } - notes.push("Run `dbt compile` (and compile the base into `target-base/`) for equivalence and lineage checks.") + const byReason = new Map() + for (const u of unverified) { + const paths = byReason.get(u.reason) + if (paths) paths.push(u.path) + else byReason.set(u.reason, [u.path]) + } + for (const [reason, paths] of byReason) { + const head = paths.slice(0, 5).map((p) => `\`${p}\``).join(", ") + const more = paths.length > 5 ? ` and ${paths.length - 5} more` : "" + const hint = /compiled SQL/.test(reason) + ? " Run `dbt compile` (and compile the base into `target-base/`) for equivalence and lineage checks." + : "" + notes.push(`${paths.length} model(s) not fully verified — ${reason}: ${head}${more}.${hint}`) + } + if (env.engineErrors?.length) { + 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) notes.push(`${env.scope.outOfScopeFiles} changed file(s) outside the dbt project were not reviewed.`) diff --git a/packages/opencode/src/altimate/review/git.ts b/packages/opencode/src/altimate/review/git.ts index d7026f82c5..a21152f9d2 100644 --- a/packages/opencode/src/altimate/review/git.ts +++ b/packages/opencode/src/altimate/review/git.ts @@ -202,6 +202,44 @@ async function mergeBase(a: string, b: string, cwd: string): Promise Promise, +): Promise { + if (branch.startsWith("-")) return false + const shallow = async () => (await git(["rev-parse", "--is-shallow-repository"], cwd)).trim() === "true" + try { + // In a shallow checkout, fetch only recent target history; the loop below + // deepens it. A full fetch would pull the target branch's whole history. + const depth = (await shallow()) ? ["--depth=50"] : [] + await git(["fetch", "--no-tags", ...depth, "origin", `+refs/heads/${branch}:refs/remotes/origin/${branch}`], cwd) + } catch { + return false + } + if (await found()) return true + for (const depth of [50, 200, 1000]) { + try { + if (!(await shallow())) return false + // Deepen the history of the head being reviewed, which may differ from + // the checkout (`--head `). + const headSha = (await git(["rev-parse", "--verify", `${head}^{commit}`], cwd)).trim() + await git(["fetch", "--no-tags", `--deepen=${depth}`, "origin", `+refs/heads/${branch}:refs/remotes/origin/${branch}`, headSha], cwd) + } catch { + return false + } + if (await found()) return true + } + return false +} + /** The PR's target branch from the CI provider, when the run is for a PR/MR. */ async function ciTargetBranch(env: NodeJS.ProcessEnv): Promise<{ branch: string; source: BaseSource } | undefined> { if (env.GITHUB_EVENT_PATH) { @@ -241,10 +279,15 @@ export async function resolveReviewBase(opts: ResolveBaseOptions): Promise { + for (const ref of [`origin/${target.branch}`, target.branch]) { + const sha = await mergeBase(ref, head, opts.cwd) + if (sha) return { ref, sha, source: target.source } + } + return undefined } + const found = (await find()) ?? ((await fetchTargetHistory(target.branch, head, opts.cwd, find)) ? await find() : undefined) + if (found) return found throw new Error( `PR target branch '${target.branch}' (${target.source}) is not available locally. ` + `Fetch it (actions/checkout with fetch-depth: 0, or git fetch origin ${target.branch}) or pass --base.`, @@ -293,6 +336,15 @@ export async function showAtRef(cwd: string, ref: string, rel: string): Promise< } } +/** The commit SHA a ref names, or undefined when it does not resolve. */ +export async function resolveCommit(ref: string, cwd: string): Promise { + try { + return (await git(["rev-parse", "--verify", "--quiet", `${ref}^{commit}`], cwd)).trim() || undefined + } catch { + return undefined + } +} + /** Compute a short hash of the manifest file for the verdict envelope. */ export async function manifestHash(manifestPath: string, cwd: string): Promise { try { diff --git a/packages/opencode/src/altimate/review/orchestrate.ts b/packages/opencode/src/altimate/review/orchestrate.ts index 9edfc1b22f..764fb45c98 100644 --- a/packages/opencode/src/altimate/review/orchestrate.ts +++ b/packages/opencode/src/altimate/review/orchestrate.ts @@ -12,7 +12,15 @@ import { type ChangedFile, type DbtProjectLayout, filterChangedFiles, hasOldSide import { classifyPR, compilePathTokenResolver, TIER_LANES } from "./risk-tier" import { type Rubric, exclusionReason, clampSeverity } from "./rubric" import { type ReviewConfig } from "./config" -import { type ReviewMode, type ReviewScope, type VerdictEnvelope, buildEnvelope, signEnvelope } from "./verdict" +import { + type ReviewMode, + type ReviewScope, + type VerdictEnvelope, + FAILED_FILE_REASON, + buildEnvelope, + errorReason, + signEnvelope, +} from "./verdict" import { detectModelPatterns, detectSchemaYmlPatterns, splitDiff } from "./dbt-patterns" import { type AiLaneStatus, type AiReviewInput, type AiReviewResult, safeReason } from "./ai-review" @@ -101,6 +109,8 @@ export interface GradeResult { export interface CheckResult { issues?: Array<{ rule: string; message: string; line?: number; severity?: string; category?: string }> piiColumns?: string[] + /** Core's PII pass could not parse the SQL: `piiColumns` is unknown, not empty. */ + piiAbstained?: boolean /** * True when the native core lint actually ran (parsed the SQL via the AST), * even if it found zero issues. Distinguishes "clean SQL" from "core @@ -118,7 +128,10 @@ export interface ReviewRunner { grade(sql: string, dialect: string): Promise check(sql: string, dialect: string, baseSql?: string): Promise equivalence(oldSql: string, newSql: string, dialect?: string): Promise - detectPii(sql: string, dialect: string): Promise<{ columns: string[] }> + /** `abstained`: the PII pass could not analyse the SQL, so `columns` is unknown. */ + detectPii(sql: string, dialect: string): Promise<{ columns: string[]; abstained?: boolean }> + /** Engine calls that threw during this review (method, first reason, count). */ + failures?(): Array<{ method: string; reason: string; count: number }> /** * Lexical scan (reserved-word aliases + dialect operators) — the curated lists * and detection live in the compiled core; this passes raw added diff lines. @@ -1151,40 +1164,66 @@ export async function runReview(input: OrchestrateInput): Promise f.kind === "model_sql" || f.kind === "python_model") const ctxByPath = new Map() - const unverified: Array<{ path: string; reason: string }> = [] + const unverified: NonNullable = [] let anyManifest = false if (input.runner.manifestAvailable) { anyManifest = await input.runner.manifestAvailable().catch(() => false) } + // PR-level lanes (across files) that threw: reported with the engine errors. + const laneErrors: Array<{ method: string; reason: string; count: number }> = [] + const prLevel = (method: string, lane: Promise) => + lane.catch((err) => { + laneErrors.push({ method, reason: errorReason(err, 200), count: 1 }) + return [] as Finding[] + }) + const failedFile = (file: { path: string }, err: unknown) => + unverified.push({ + path: file.path, + reason: `${FAILED_FILE_REASON}: ${errorReason(err, 160)}`, + kind: "failed", + }) await Promise.all( modelFiles.map(async (file) => { - const model = modelNameFromPath(file.path) - // For renames the previous content lives at oldPath, not the new path. - const oldRef = file.oldPath ?? file.path - const withOld = hasOldSide(file) - const [newSql, oldSql, compiledNew, compiledOld] = await Promise.all([ - file.status !== "deleted" ? getContent?.(file.path, "new") : Promise.resolve(undefined), - withOld ? getContent?.(oldRef, "old") : Promise.resolve(undefined), - file.status !== "deleted" ? getCompiled?.(file.path, "new") : Promise.resolve(undefined), - withOld ? getCompiled?.(oldRef, "old") : Promise.resolve(undefined), - ]) - // Engine lanes need dbt-compiled SQL. Raw SQL is used only when it holds - // no Jinja: the AST engine reads `{{ config() }}` as an injection attempt - // and a mixed raw-vs-compiled pair can never be proven equivalent, so a - // Jinja model without compiled output is marked unverified instead of - // producing warnings about its template syntax. The dbt-patterns lane - // always uses raw (it needs the Jinja). - const engineNewSql = compiledNew ?? (file.kind === "model_sql" && hasJinja(newSql) ? undefined : newSql) - const engineOldSql = compiledOld ?? (file.kind === "model_sql" && hasJinja(oldSql) ? undefined : oldSql) - if (file.status !== "deleted" && newSql && !engineNewSql) - unverified.push({ path: file.path, reason: "no compiled SQL for the head (run dbt compile)" }) - else if (withOld && oldSql && !engineOldSql) - unverified.push({ path: file.path, reason: "no compiled SQL for the base (compile the base into target-base)" }) - const impact = await input.runner.impact(model) - if (impact.hasManifest) anyManifest = true - const pii = engineNewSql ? (await input.runner.detectPii(engineNewSql, dialect)).columns : [] - const complex = engineNewSql && input.runner.isComplex ? await input.runner.isComplex(engineNewSql) : false - ctxByPath.set(file.path, { file, impact, pii, newSql, oldSql, engineNewSql, engineOldSql, complex }) + try { + const model = modelNameFromPath(file.path) + // For renames the previous content lives at oldPath, not the new path. + const oldRef = file.oldPath ?? file.path + const withOld = hasOldSide(file) + const [newSql, oldSql, compiledNew, compiledOld] = await Promise.all([ + file.status !== "deleted" ? getContent?.(file.path, "new") : Promise.resolve(undefined), + withOld ? getContent?.(oldRef, "old") : Promise.resolve(undefined), + file.status !== "deleted" ? getCompiled?.(file.path, "new") : Promise.resolve(undefined), + withOld ? getCompiled?.(oldRef, "old") : Promise.resolve(undefined), + ]) + // Engine lanes need dbt-compiled SQL. Raw SQL is used only when it holds + // no Jinja: the AST engine reads `{{ config() }}` as an injection attempt + // and a mixed raw-vs-compiled pair can never be proven equivalent, so a + // Jinja model without compiled output is marked unverified instead of + // producing warnings about its template syntax. The dbt-patterns lane + // always uses raw (it needs the Jinja). + const engineNewSql = compiledNew ?? (file.kind === "model_sql" && hasJinja(newSql) ? undefined : newSql) + const engineOldSql = compiledOld ?? (file.kind === "model_sql" && hasJinja(oldSql) ? undefined : oldSql) + if (file.status !== "deleted" && newSql && !engineNewSql) + unverified.push({ path: file.path, reason: "no compiled SQL for the head (run dbt compile)", kind: "not_compiled" }) + else if (withOld && oldSql && !engineOldSql) + unverified.push({ + path: file.path, + reason: "no compiled SQL for the base (compile the base into target-base)", + kind: "not_compiled", + }) + const impact = await input.runner.impact(model) + if (impact.hasManifest) anyManifest = true + const piiRes = engineNewSql ? await input.runner.detectPii(engineNewSql, dialect) : { columns: [] } + if (piiRes.abstained) + unverified.push({ path: file.path, reason: "the PII check could not analyse this model", kind: "abstained" }) + const pii = piiRes.columns + const complex = engineNewSql && input.runner.isComplex ? await input.runner.isComplex(engineNewSql) : false + ctxByPath.set(file.path, { file, impact, pii, newSql, oldSql, engineNewSql, engineOldSql, complex }) + } catch (err) { + // One file's failure (unreadable content, an engine crash) degrades that + // file only; it must not abort the whole review. + failedFile(file, err) + } }), ) @@ -1278,7 +1317,8 @@ export async function runReview(input: OrchestrateInput): Promise lineageBreakageLane(ctx.file, ctx.impact, input.rubric))) tasks.push(columnBreakageLane(ctx, input.runner, getCompiled, dialect)) } if (lanes.has("test_coverage")) tasks.push(missingGrainTestLane(ctx, input.runner)) @@ -1309,10 +1349,20 @@ export async function runReview(input: OrchestrateInput): Promise c.toLowerCase()), - ) - fallbackPii = fallbackPii.filter((col) => !basePii.has(col.toLowerCase())) + const baseRes = await input.runner.detectPii(currentCtx.engineOldSql, dialect) + if (baseRes.abstained) { + // An unanalysable base says nothing about what it exposed; reporting + // every head column as newly exposed would be a false positive. + unverified.push({ + path: currentCtx.file.path, + reason: "the PII check could not analyse the base model", + kind: "abstained", + }) + fallbackPii = [] + } else { + const basePii = new Set(baseRes.columns.map((c) => c.toLowerCase())) + fallbackPii = fallbackPii.filter((col) => !basePii.has(col.toLowerCase())) + } } const baseUnknown = hasOldSide(currentCtx.file) && !currentCtx.engineOldSql findings.push(...piiLane(currentCtx.file, fallbackPii, baseUnknown)) @@ -1322,7 +1372,7 @@ export async function runReview(input: OrchestrateInput): Promise detectModelPatterns(ctx.file, ctx.newSql, input.rubric))) tasks.push(lexicalLane(ctx.file, input.runner)) } // dbt config/Jinja lint (materialization / incremental / contract) — gate on its @@ -1337,20 +1387,32 @@ export async function runReview(input: OrchestrateInput): Promise + t.catch((err) => { + if (!laneFailed) failedFile(ctx.file, err) + laneFailed = true + return [] as Finding[] + }), + ), + ) + all.push(...settled) } // PR-level: cross-model sibling filter-consistency (divergent filters on a // shared upstream across the changed models). if (lanes.has("semantic_change")) { - all.push(await siblingConsistencyLane([...ctxByPath.values()], input.runner)) + all.push(await prLevel("sibling_consistency", siblingConsistencyLane([...ctxByPath.values()], input.runner))) } // PR-level: topology-aware equivalence for merge refactors (N models deleted + // 1 added that supersedes them). Per-model equivalence can't see these because // the new model has no 1:1 base. Advisory only (capped at warning). if (lanes.has("semantic_change")) { - all.push(await topologyEquivalenceLane(modelFiles, input.runner, getCompiled, dialect)) + all.push(await prLevel("topology_equivalence", topologyEquivalenceLane(modelFiles, input.runner, getCompiled, dialect))) } // schema.yml-level detectors (test removal) — run on changed YAML files @@ -1378,12 +1440,18 @@ export async function runReview(input: OrchestrateInput): Promise { - const oldRef = file.oldPath ?? file.path - const [oldContent, newContent] = await Promise.all([ - file.status !== "added" ? getContent?.(oldRef, "old") : Promise.resolve(undefined), - file.status !== "deleted" ? getContent?.(file.path, "new") : Promise.resolve(undefined), - ]) - return detectSchemaYmlPatterns(file, input.rubric, { oldContent, newContent }) + try { + const oldRef = file.oldPath ?? file.path + const [oldContent, newContent] = await Promise.all([ + file.status !== "added" ? getContent?.(oldRef, "old") : Promise.resolve(undefined), + file.status !== "deleted" ? getContent?.(file.path, "new") : Promise.resolve(undefined), + ]) + return detectSchemaYmlPatterns(file, input.rubric, { oldContent, newContent }) + } catch (err) { + // One malformed schema file degrades that file only. + failedFile(file, err) + return [] as Finding[] + } }), ) for (const findings of schemaFindingSets) all.push(findings) @@ -1403,7 +1471,9 @@ export async function runReview(input: OrchestrateInput): Promise { if (!ctx.engineNewSql || ctx.file.status === "deleted") return // Cache hit: qualityLane already ran this exact check. - const cr = await input.runner.check(ctx.engineNewSql, dialect, ctx.engineOldSql) + const cr = await input.runner + .check(ctx.engineNewSql, dialect, ctx.engineOldSql) + .catch(() => ({ ran: false }) as CheckResult) if (cr.ran) coreRanFiles.add(ctx.file.path) }), ) @@ -1546,7 +1616,9 @@ export async function runReview(input: OrchestrateInput): Promise