From 0c9314f957cacf00d6e17392492afe3437b50a78 Mon Sep 17 00:00:00 2001 From: RaghwendraSingh Date: Thu, 12 Nov 2026 16:17:32 +0530 Subject: [PATCH] fix(review): review the PR's own changes: real base, merge-base diff, project scope, renames, `model-paths`, no raw Jinja - Resolve the base from `--base`, then the CI PR/MR target branch (`pull_request.base.ref`, `GITHUB_BASE_REF`, `CI_MERGE_REQUEST_TARGET_BRANCH_NAME`), then the remote default branch, then main/master. An unfetched CI target branch is an error instead of a silent `origin/main` guess. - Always diff from the merge-base, so working-tree and `--head` runs agree and commits the base gained after branching never appear as changes. - Scope changed files to the dbt project at or above `--cwd`; files outside it are counted, not reviewed. - Pair renames (both paths in the per-file diff), give renamed models their old side, and skip pure directory moves that keep the model name. - Classify files from `dbt_project.yml` `*-paths` (custom `model-paths`); without a layout the outermost conventional directory wins (`models/tests/x.sql` is a model). - Never pass raw Jinja to engine lanes: a Jinja model without compiled SQL is recorded as unverified instead of warned on its template syntax. - Record base ref/SHA/source, project root, out-of-scope and moved counts, and unverified models in an optional signed `scope` field on the envelope. - Regression tests on synthetic git topologies (`review-scope-diff.test.ts`). Co-Authored-By: Claude Opus 5.5 --- docs/docs/usage/dbt-pr-review.md | 4 +- github/review/action.yml | 2 +- .../opencode/src/altimate/review/compiled.ts | 34 ++ .../src/altimate/review/dbt-patterns.ts | 6 +- .../src/altimate/review/diff-filter.ts | 147 +++++++- packages/opencode/src/altimate/review/git.ts | 141 +++++++- .../src/altimate/review/orchestrate.ts | 53 ++- .../opencode/src/altimate/review/risk-tier.ts | 4 +- .../src/altimate/review/rule-catalog.ts | 4 +- packages/opencode/src/altimate/review/run.ts | 139 +++++-- .../opencode/src/altimate/review/verdict.ts | 26 ++ .../src/altimate/tools/dbt-pr-review.ts | 2 +- packages/opencode/src/cli/cmd/review.ts | 2 +- .../test/altimate/review-scope-diff.test.ts | 339 ++++++++++++++++++ .../opencode/test/altimate/review.test.ts | 3 + 15 files changed, 830 insertions(+), 76 deletions(-) create mode 100644 packages/opencode/test/altimate/review-scope-diff.test.ts diff --git a/docs/docs/usage/dbt-pr-review.md b/docs/docs/usage/dbt-pr-review.md index 2c2a19afe5..cd1d9fc297 100644 --- a/docs/docs/usage/dbt-pr-review.md +++ b/docs/docs/usage/dbt-pr-review.md @@ -96,7 +96,7 @@ below it fill gaps and add context without ever overriding a proof. ## Quick start (local) -Run the reviewer on your working-tree changes against `origin/main`: +Run the reviewer on your working-tree changes against the branch you will merge into (the remote default branch unless `--base` says otherwise): ```bash altimate review # human-readable summary @@ -108,7 +108,7 @@ Options: | Flag | Description | |------|-------------| -| `--base ` | Base git ref. Defaults to the merge-base with `origin/main`. | +| `--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`). | | `--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`. | diff --git a/github/review/action.yml b/github/review/action.yml index 48c369b4e6..8820ed8892 100644 --- a/github/review/action.yml +++ b/github/review/action.yml @@ -18,7 +18,7 @@ inputs: required: false default: "suggestion" base: - description: "Base git ref to diff against. Defaults to the merge-base with origin/main." + description: "Base git ref to diff against. Defaults to the pull request's target branch (from the event), reduced to its merge-base with the head." required: false head: description: "Head git ref. Defaults to the checked-out PR head." diff --git a/packages/opencode/src/altimate/review/compiled.ts b/packages/opencode/src/altimate/review/compiled.ts index b804a0f33e..65d505e98c 100644 --- a/packages/opencode/src/altimate/review/compiled.ts +++ b/packages/opencode/src/altimate/review/compiled.ts @@ -2,6 +2,7 @@ import { promises as fs } from "node:fs" import path from "node:path" import YAML from "yaml" import { safeReadInside } from "./git" +import { DEFAULT_DBT_DIRS, type DbtProjectLayout } from "./diff-filter" /** * Resolve dbt's COMPILED SQL for static analysis. @@ -36,6 +37,39 @@ export async function dbtProjectName(cwd: string): Promise { return undefined } +const LAYOUT_KEYS: Record = { + model: "model-paths", + snapshot: "snapshot-paths", + seed: "seed-paths", + macro: "macro-paths", + test: "test-paths", + analysis: "analysis-paths", +} + +/** + * Read the project's directory layout (`model-paths`, `snapshot-paths`, …) + * from dbt_project.yml, falling back to dbt's defaults per key. `root` is the + * project directory relative to the repository root. Undefined when there is + * no readable dbt_project.yml in `projectDir`. + */ +export async function dbtProjectLayout(projectDir: string, root: string): Promise { + for (const f of ["dbt_project.yml", "dbt_project.yaml"]) { + let doc: any + try { + doc = YAML.parse(await fs.readFile(path.join(projectDir, f), "utf8")) + } catch { + continue + } + const dirs = { ...DEFAULT_DBT_DIRS } + for (const [role, key] of Object.entries(LAYOUT_KEYS) as Array<[keyof typeof dirs, string]>) { + const v = doc?.[key] + if (Array.isArray(v) && v.length && v.every((x) => typeof x === "string")) dirs[role] = v + } + return { root: root.replace(/\\/g, "/"), dirs } + } + return undefined +} + export interface CompiledResolverOptions { /** dbt project root (the dir containing `dbt_project.yml`). */ cwd: string diff --git a/packages/opencode/src/altimate/review/dbt-patterns.ts b/packages/opencode/src/altimate/review/dbt-patterns.ts index 1ed240b083..b9b99c1f8b 100644 --- a/packages/opencode/src/altimate/review/dbt-patterns.ts +++ b/packages/opencode/src/altimate/review/dbt-patterns.ts @@ -2,7 +2,7 @@ import path from "node:path" import YAML from "yaml" import { Log } from "@/altimate/util/log" import { type Finding, type Severity, type ReviewCategory, makeFinding } from "./finding" -import { type ChangedFile, classifyDbtFile } from "./diff-filter" +import { type ChangedFile, fileKind } from "./diff-filter" import { type Rubric, clampSeverity, exclusionReason } from "./rubric" import { evaluateCatalog } from "./rule-catalog" @@ -766,7 +766,7 @@ const MODEL_DETECTORS: Detector[] = [ /** Run the dbt anti-pattern detectors over a changed MODEL file. */ export function detectModelPatterns(file: ChangedFile, newSql: string | undefined, rubric: Rubric): Finding[] { - const kind = classifyDbtFile(file.path) + const kind = fileKind(file) if (!isModelSql(kind) || file.status === "deleted" || !newSql) return [] const { added, removed } = splitDiff(file.diff) const ctx: Ctx = { @@ -1281,7 +1281,7 @@ export function detectSchemaYmlPatterns( rubric: Rubric, opts: SchemaYmlDetectContent = {}, ): Finding[] { - const kind = classifyDbtFile(file.path) + const kind = fileKind(file) if (kind !== "schema_yml") return [] const removals: Array<{ diff --git a/packages/opencode/src/altimate/review/diff-filter.ts b/packages/opencode/src/altimate/review/diff-filter.ts index 4079fb6409..78661d6ef4 100644 --- a/packages/opencode/src/altimate/review/diff-filter.ts +++ b/packages/opencode/src/altimate/review/diff-filter.ts @@ -55,23 +55,85 @@ export type DbtFileKind = | "project_config" // dbt_project.yml / profiles.yml / packages.yml | "other" -/** Classify a changed file by its role in a dbt project. */ -export function classifyDbtFile(path: string): DbtFileKind { - const p = path.replace(/\\/g, "/").toLowerCase() +type DbtRole = "model" | "snapshot" | "seed" | "macro" | "test" | "analysis" + +/** + * Where a dbt project keeps each kind of file, from `dbt_project.yml` + * (`model-paths`, `snapshot-paths`, …). `root` is the project directory + * relative to the repository root ("" when the project is at the root). + */ +export interface DbtProjectLayout { + root: string + dirs: Record +} + +export const DEFAULT_DBT_DIRS: Record = { + model: ["models"], + snapshot: ["snapshots"], + seed: ["seeds"], + macro: ["macros"], + test: ["tests"], + analysis: ["analyses"], +} + +const norm = (p: string) => p.replace(/\\/g, "/") + +/** `p` relative to `root`, or undefined when it lies outside. Both POSIX, repo-relative. */ +export function relativeToRoot(p: string, root: string): string | undefined { + const r = norm(root).replace(/^\.\/?/, "").replace(/\/+$/, "") + const f = norm(p) + if (!r) return f + return f.startsWith(r + "/") ? f.slice(r.length + 1) : undefined +} + +/** The role of a project-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() + 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(/\/+$/, "") + if (dir && p.startsWith(dir + "/") && (!best || dir.length > best.len)) best = { role, len: dir.length } + } + } + return best?.role +} + +/** Without a layout, the OUTERMOST conventional directory decides the role, + * so `models/tests/x.sql` is a model and `tests/models/x.sql` a test. */ +function roleFromConvention(p: string): DbtRole | undefined { + const segments = p.toLowerCase().split("/").slice(0, -1) + for (const seg of segments) { + for (const [role, list] of Object.entries(DEFAULT_DBT_DIRS) as Array<[DbtRole, string[]]>) { + if (list.includes(seg)) return role + } + } + return undefined +} + +/** Classify a changed file by its role in a dbt project. With a layout, the + * project's own `*-paths` decide (custom `model-paths` are honoured); + * without one, conventional directory names do. */ +export function classifyDbtFile(path: string, layout?: DbtProjectLayout): DbtFileKind { + 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" - if (/(^|\/)macros\//.test(p)) return "macro" + const rel = layout ? relativeToRoot(norm(path), layout.root) : undefined + const role = rel !== undefined && layout ? roleFromLayout(rel, layout.dirs) : 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/` // are schema files (declared tests + column metadata) and should route to // `schema_yml` so the test-removal detector runs on them. - if (/(^|\/)snapshots\//.test(p) && p.endsWith(".sql")) return "snapshot" - if (/(^|\/)seeds\//.test(p) && p.endsWith(".csv")) return "seed" - if (/(^|\/)tests\//.test(p) && p.endsWith(".sql")) return "test" - if (/(^|\/)analyses\//.test(p)) return "analysis" - if (/(^|\/)models\//.test(p) && p.endsWith(".py")) return "python_model" - if (/(^|\/)models\//.test(p) && p.endsWith(".sql")) return "model_sql" - if (isYaml && /(^|\/)(models|snapshots|seeds|tests)\//.test(p)) return "schema_yml" + if (role === "snapshot" && p.endsWith(".sql")) return "snapshot" + if (role === "seed" && p.endsWith(".csv")) return "seed" + if (role === "test" && p.endsWith(".sql")) return "test" + if (role === "analysis") return "analysis" + if (role === "model" && p.endsWith(".py")) return "python_model" + if (role === "model" && p.endsWith(".sql")) return "model_sql" + if (isYaml && (role === "model" || role === "snapshot" || role === "seed" || role === "test")) return "schema_yml" if (isYaml && /(^|\/)(_?schema|_?models|_?sources|sources|properties)\.ya?ml$/.test(p)) return "schema_yml" return "other" } @@ -84,6 +146,21 @@ export interface ChangedFile { diff?: string /** Previous path for renames. */ oldPath?: string + /** Rename similarity (0–100) from git; 100 means the content is unchanged. */ + similarity?: number + /** dbt role, set by filterChangedFiles from the project layout. */ + kind?: DbtFileKind +} + +/** The file's dbt role: the layout-aware kind when filterChangedFiles set it. */ +export function fileKind(file: ChangedFile): DbtFileKind { + return file.kind ?? classifyDbtFile(file.path) +} + +/** True when a file has a previous version to compare against: a modification, + * or a rename (whose old side lives at `oldPath`). */ +export function hasOldSide(file: ChangedFile): boolean { + return file.status === "modified" || (file.status === "renamed" && !!file.oldPath) } // Compile a glob (`**`, `*`, `?`) to an anchored RegExp with path semantics: @@ -119,14 +196,58 @@ function globToRegExp(glob: string): RegExp { 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. - const norm = (p: string) => p.replace(/\\/g, "/") return files .filter((f) => shouldReview(f.path)) .filter((f) => !excluders.some((re) => re.test(norm(f.path)))) - .map((f) => ({ ...f, kind: classifyDbtFile(f.path) })) + .map((f) => ({ ...f, kind: classifyDbtFile(f.path, layout) })) +} + +export interface ScopeResult { + /** Files inside the dbt project, with unchanged moves removed. */ + inScope: ChangedFile[] + /** Changed files outside the dbt project root (counted, not reviewed). */ + outOfScope: number + /** Renames with identical content that keep the model name (a pure move). */ + movedUnchanged: number +} + +/** + * Restrict the PR's changed files to the dbt project being reviewed. + * + * - 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). + * - A rename at 100% similarity that keeps the file name is a pure move: dbt + * resolves `ref()` by name, so nothing about the model changed and + * re-reviewing it would only repeat pre-existing findings. A rename that + * changes the file name stays in scope — it renames the model and can break + * downstream refs. + */ +export function scopeToProject(files: ChangedFile[], root: string | undefined): ScopeResult { + let outOfScope = 0 + let movedUnchanged = 0 + const inScope: ChangedFile[] = [] + const base = (p: string) => norm(p).split("/").pop() + for (const f of files) { + const isInside = (p: string) => root === undefined || relativeToRoot(p, root) !== undefined + 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++ + continue + } + if (f.status === "renamed" && f.similarity === 100 && f.oldPath && base(f.oldPath) === base(f.path) && inside && oldInside) { + movedUnchanged++ + continue + } + inScope.push(f) + } + return { inScope, outOfScope, movedUnchanged } } /** Heuristic: does a yml diff declare dbt `sources:`? Tolerates unified-diff diff --git a/packages/opencode/src/altimate/review/git.ts b/packages/opencode/src/altimate/review/git.ts index 1e73fb7327..c6087b50bf 100644 --- a/packages/opencode/src/altimate/review/git.ts +++ b/packages/opencode/src/altimate/review/git.ts @@ -24,6 +24,12 @@ function parseStatus(code: string): ChangedFile["status"] { return "modified" } +/** Rename similarity from a `--name-status` code such as `R087`; undefined otherwise. */ +function parseSimilarity(code: string): number | undefined { + const m = /^R(\d{1,3})$/.exec(code) + return m ? Number(m[1]) : undefined +} + export interface CollectOptions { base: string /** Omit to diff against the working tree. */ @@ -31,14 +37,19 @@ export interface CollectOptions { cwd: string } -/** Collect changed files between base and head (or working tree). */ +/** Collect changed files between base and head (or working tree). + * + * `base` should be the merge-base SHA (see `resolveReviewBase`), so the + * two-dot working-tree diff and the three-dot `base...head` diff mean the same + * thing: only what the PR changed, never commits the target branch gained + * after the PR branched. */ export async function collectChangedFiles(opts: CollectOptions): Promise { const range = opts.head ? [`${opts.base}...${opts.head}`] : [opts.base] const nameStatus = await git(["diff", "--name-status", "-M", ...range], opts.cwd) // Parse the name-status lines first, then fetch per-file hunk diffs // concurrently — a large PR must not spawn N serial git processes. - const entries: Array<{ status: ChangedFile["status"]; newPath: string; oldPath?: string }> = [] + const entries: Array<{ status: ChangedFile["status"]; newPath: string; oldPath?: string; similarity?: number }> = [] for (const line of nameStatus.split("\n")) { if (!line.trim()) continue const parts = line.split("\t") @@ -46,18 +57,22 @@ export async function collectChangedFiles(opts: CollectOptions): Promise { + entries.map(async ({ status, newPath, oldPath, similarity }) => { + // A rename's hunk diff must name BOTH paths: limiting the pathspec to the + // new path hides the old one from git, so it cannot pair the rename and + // reports every line as added. + const pathspec = oldPath ? [oldPath, newPath] : [newPath] let diff = "" try { - diff = await git(["diff", "-M", ...range, "--", newPath], opts.cwd) + diff = await git(["diff", "-M", ...range, "--", ...pathspec], opts.cwd) } catch { diff = "" } - return { path: newPath, status, diff, oldPath } satisfies ChangedFile + return { path: newPath, status, diff, oldPath, similarity } satisfies ChangedFile }), ) } @@ -139,18 +154,116 @@ export async function gitRepoRoot(cwd: string): Promise { } } -/** Resolve a sensible default base ref (merge-base with origin/main/master). */ -export async function defaultBaseRef(cwd: string): Promise { - for (const candidate of ["origin/main", "origin/master", "main", "master"]) { +/** Where the review's base came from, recorded on the verdict envelope. */ +export type BaseSource = + | "flag" // --base + | "github_event" // pull_request.base.ref in GITHUB_EVENT_PATH + | "github_env" // GITHUB_BASE_REF + | "gitlab_env" // CI_MERGE_REQUEST_TARGET_BRANCH_NAME + | "remote_default" // refs/remotes/origin/HEAD + | "default_branch" // origin/main, origin/master, main, master + | "parent" // ~1, nothing else resolved + +export interface ResolvedBase { + /** The ref the base was resolved from (e.g. `origin/deployment`). */ + ref: string + /** Merge-base of `ref` and the head: the commit the diff and the old side use. */ + sha: string + source: BaseSource +} + +export interface ResolveBaseOptions { + cwd: string + /** Explicit `--base`. */ + base?: string + /** Head ref; omit for the working tree (merge-base uses HEAD). */ + head?: string + /** Defaults to process.env; injected by tests. */ + env?: NodeJS.ProcessEnv +} + +async function mergeBase(a: string, b: string, cwd: string): Promise { + try { + const out = (await git(["merge-base", a, b], cwd)).trim() + return out || undefined + } catch { + return undefined + } +} + +/** 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) { try { - const mb = (await git(["merge-base", "HEAD", candidate], cwd)).trim() - if (mb) return mb + const event = JSON.parse(await fs.readFile(env.GITHUB_EVENT_PATH, "utf8")) + const ref = event?.pull_request?.base?.ref + if (typeof ref === "string" && ref) return { branch: ref, source: "github_event" } } catch { - // try next + // Unreadable event file: fall through to the plain env vars. } } - // Fall back to the previous commit. - return "HEAD~1" + if (env.GITHUB_BASE_REF) return { branch: env.GITHUB_BASE_REF, source: "github_env" } + if (env.CI_MERGE_REQUEST_TARGET_BRANCH_NAME) + return { branch: env.CI_MERGE_REQUEST_TARGET_BRANCH_NAME, source: "gitlab_env" } + return undefined +} + +/** + * Resolve the review base: the PR's real target branch, reduced to the + * merge-base with the head so the diff contains only the PR's own changes. + * + * Order: explicit `--base`; the CI provider's PR/MR target branch; the + * remote's default branch; main/master; the head's parent. When CI names a + * target branch that cannot be resolved locally (usually a shallow checkout), + * this throws instead of silently diffing against another branch — that guess + * is what made PRs into a non-default branch review hundreds of unrelated files. + */ +export async function resolveReviewBase(opts: ResolveBaseOptions): Promise { + const head = opts.head ?? "HEAD" + const env = opts.env ?? process.env + + if (opts.base) { + const sha = await mergeBase(opts.base, head, opts.cwd) + if (!sha) throw new Error(`--base ${opts.base}: no merge-base with ${head}. Fetch the base branch (fetch-depth: 0).`) + return { ref: opts.base, sha, source: "flag" } + } + + const target = await ciTargetBranch(env) + if (target) { + 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 } + } + 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.`, + ) + } + + try { + const remoteHead = (await git(["symbolic-ref", "--quiet", "refs/remotes/origin/HEAD"], opts.cwd)).trim() + const ref = remoteHead.replace(/^refs\/remotes\//, "") + const sha = ref ? await mergeBase(ref, head, opts.cwd) : undefined + if (sha) return { ref, sha, source: "remote_default" } + } catch { + // origin/HEAD not set (common in CI clones): try the conventional names. + } + + for (const ref of ["origin/main", "origin/master", "main", "master"]) { + const sha = await mergeBase(ref, head, opts.cwd) + if (sha) return { ref, sha, source: "default_branch" } + } + + // The head's parent, so a custom --head is never compared against an + // unrelated checkout commit. + const parent = `${head}~1` + const sha = (await git(["rev-parse", parent], opts.cwd).catch(() => parent)).trim() + return { ref: parent, sha, source: "parent" } +} + +/** Resolve a default base SHA. Kept for callers that only need the commit. */ +export async function defaultBaseRef(cwd: string, head?: string): Promise { + return (await resolveReviewBase({ cwd, head })).sha } /** Compute a short hash of the manifest file for the verdict envelope. */ diff --git a/packages/opencode/src/altimate/review/orchestrate.ts b/packages/opencode/src/altimate/review/orchestrate.ts index d0d89143a4..2c81fcc721 100644 --- a/packages/opencode/src/altimate/review/orchestrate.ts +++ b/packages/opencode/src/altimate/review/orchestrate.ts @@ -8,11 +8,11 @@ import { dedupe, SEVERITY_ORDER, } from "./finding" -import { type ChangedFile, filterChangedFiles } from "./diff-filter" +import { type ChangedFile, type DbtProjectLayout, filterChangedFiles, hasOldSide } from "./diff-filter" import { classifyPR, compilePathTokenResolver, TIER_LANES } from "./risk-tier" import { type Rubric, exclusionReason, clampSeverity } from "./rubric" import { type ReviewConfig } from "./config" -import { type ReviewMode, type VerdictEnvelope, buildEnvelope, signEnvelope } from "./verdict" +import { type ReviewMode, type ReviewScope, type VerdictEnvelope, buildEnvelope, signEnvelope } from "./verdict" import { detectModelPatterns, detectSchemaYmlPatterns, splitDiff } from "./dbt-patterns" import { type AiReviewInput } from "./ai-review" @@ -198,6 +198,15 @@ export interface OrchestrateInput { forceTier?: "trivial" | "lite" | "full" /** Manifest was stale relative to change-affecting files (see run.ts::detectStaleManifest). */ staleManifest?: boolean + /** The dbt project's directories from dbt_project.yml (custom model-paths). */ + layout?: DbtProjectLayout + /** What the review covered: base resolution and files left out of scope. */ + scope?: Omit +} + +/** True when SQL text contains Jinja (`{{ }}`, `{% %}` or `{# #}`). */ +export function hasJinja(sql: string | undefined): boolean { + return !!sql && /\{\{|\{%|\{#/.test(sql) } /** Derive the dbt model name from a model file path. */ @@ -317,7 +326,7 @@ async function semanticChangeLane( impact: ImpactResult, rubric: Rubric, ): Promise { - if (file.status !== "modified") return [] + if (!hasOldSide(file)) return [] if (!oldSql || !newSql || oldSql.trim() === newSql.trim()) return [] const model = modelNameFromPath(file.path) const eq = await runner.equivalence(oldSql, newSql, dialect) @@ -478,7 +487,7 @@ async function columnBreakageLane( ): Promise { if (!runner.columnLineage || !runner.downstreamModels || !getCompiled) return [] const { file, engineOldSql, engineNewSql } = ctx - if (!engineOldSql || !engineNewSql || file.status !== "modified") return [] + if (!engineOldSql || !engineNewSql || !hasOldSide(file)) return [] const model = modelNameFromPath(file.path) // 1. Output columns removed by this change (column-lineage targets, old − new). @@ -542,7 +551,7 @@ async function dataDiffLane( if (!cfg.enabled) return [] if (!runner.dataDiff || !runner.columnLineage) return [] const { file, engineOldSql, engineNewSql } = ctx - if (!engineOldSql || !engineNewSql || file.status !== "modified") return [] + if (!engineOldSql || !engineNewSql || !hasOldSide(file)) return [] const model = modelNameFromPath(file.path) const cols = (await runner.columnLineage(engineNewSql, dialect)).map((e) => e.target.toLowerCase()) const keys = [...new Set(cols.filter((c) => c === "id" || /_id$/.test(c)))].slice(0, 1) @@ -774,7 +783,7 @@ async function dbtConfigLane(ctx: ModelContext, runner: ReviewRunner): Promise { - const reviewable = filterChangedFiles(input.changedFiles, input.rubric.exclusions.excludeGlobs) + const reviewable = filterChangedFiles(input.changedFiles, input.rubric.exclusions.excludeGlobs, input.layout) const dialect = input.config.dialect const getContent = input.getContent const getCompiled = input.getCompiled @@ -1066,6 +1075,7 @@ 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 }> = [] let anyManifest = false if (input.runner.manifestAvailable) { anyManifest = await input.runner.manifestAvailable().catch(() => false) @@ -1075,16 +1085,25 @@ export async function runReview(input: OrchestrateInput): Promise a.path.localeCompare(b.path)) : undefined } + : undefined, // Include tierReasons whenever `--explain-tier` / `--force-tier` is set, // when a riskTierPathTokens config error was caught above, OR when the // classifier lands on `full` tier — a naturally-full run is a customer- diff --git a/packages/opencode/src/altimate/review/risk-tier.ts b/packages/opencode/src/altimate/review/risk-tier.ts index ddad1f9706..d85e8eff9b 100644 --- a/packages/opencode/src/altimate/review/risk-tier.ts +++ b/packages/opencode/src/altimate/review/risk-tier.ts @@ -1,7 +1,7 @@ import { type ChangedFile, type DbtFileKind, - classifyDbtFile, + fileKind, countChangedLines, looksLikeSourceYml, touchesContract, @@ -290,7 +290,7 @@ function dbtRiskYmlKeyMatches(kind: DbtFileKind, scanned: string): string[] { /** Classify a single changed file from its diff + optional manifest signals. */ export function classifyFile(file: ChangedFile, opts: ClassifyOptions = {}): FileChangeClass { - const kind = classifyDbtFile(file.path) + const kind = fileKind(file) const diff = file.diff const changed = changedLines(diff) // Block-scalar-aware scan: walks the whole diff (including context lines) diff --git a/packages/opencode/src/altimate/review/rule-catalog.ts b/packages/opencode/src/altimate/review/rule-catalog.ts index eaf4389c4a..0d8313c458 100644 --- a/packages/opencode/src/altimate/review/rule-catalog.ts +++ b/packages/opencode/src/altimate/review/rule-catalog.ts @@ -1,5 +1,5 @@ import { type Finding, type Severity, type ReviewCategory, makeFinding } from "./finding" -import { type ChangedFile, classifyDbtFile } from "./diff-filter" +import { type ChangedFile, fileKind } from "./diff-filter" import { type Rubric, clampSeverity, exclusionReason } from "./rubric" import path from "node:path" @@ -68,7 +68,7 @@ export interface RuleInput { /** True when the rule matches the change. */ export function ruleMatches(rule: Rule, input: RuleInput): boolean { - const kind = classifyDbtFile(input.file.path) + const kind = fileKind(input.file) const wantKind = rule.kind ?? "model_sql" if (wantKind !== "any" && kind !== wantKind) return false if (rule.pathGlob && !rule.pathGlob.test(input.file.path)) return false diff --git a/packages/opencode/src/altimate/review/run.ts b/packages/opencode/src/altimate/review/run.ts index 9e8db4cb7b..b315c550dc 100644 --- a/packages/opencode/src/altimate/review/run.ts +++ b/packages/opencode/src/altimate/review/run.ts @@ -3,8 +3,16 @@ import { readFile, realpath, stat, access } from "node:fs/promises" import { Installation } from "../../installation" import { loadReviewConfig, resolveRubric } from "./config" import type { Severity } from "./finding" -import { collectChangedFiles, makeContentResolver, defaultBaseRef, gitRepoRoot, manifestHash } from "./git" -import { makeCompiledResolver, dbtProjectName } from "./compiled" +import { + collectChangedFiles, + makeContentResolver, + resolveReviewBase, + type ResolvedBase, + gitRepoRoot, + manifestHash, +} from "./git" +import { makeCompiledResolver, dbtProjectName, dbtProjectLayout } from "./compiled" +import { scopeToProject } from "./diff-filter" import { buildCatalogSchemaContext } from "./schema-context" import { createDispatcherRunner } from "./runner" import { runReview } from "./orchestrate" @@ -21,7 +29,7 @@ import type { ChangedFile } from "./diff-filter" export interface ReviewPullRequestOptions { cwd: string - /** Base ref; defaults to merge-base with origin/main. */ + /** Base ref; defaults to the PR target branch from CI (else the remote default branch), reduced to the merge-base. */ base?: string /** Head ref; omit to diff against the working tree. */ head?: string @@ -213,26 +221,12 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise if (opts.severityThreshold) config.severityThreshold = opts.severityThreshold const rubric = resolveRubric(config) - // Only resolve a base ref if we actually need git (to collect changed files - // or to read old/new content). A caller that supplies BOTH `changedFiles` and - // `getContent` (e.g. a non-git CI integration) must not be forced through a - // git lookup that can fail when there's no usable history. - const needGit = !opts.changedFiles || !opts.getContent - const base = opts.base ?? (needGit ? await defaultBaseRef(opts.cwd) : "") - const changedFiles = opts.changedFiles ?? (await collectChangedFiles({ base, head: opts.head, cwd: opts.cwd })) // Resolve the repo top-level once; used to root working-tree FS reads, the // stale-manifest existence check, and the compiled-SQL resolver's path // mapping. Falls back to opts.cwd when we couldn't resolve it (non-git or // bare-repo contexts) — safe for the repo-root-invoked case but not helpful // in the subdir-invocation case that this addresses. const gitRoot = (await gitRepoRoot(opts.cwd)) ?? opts.cwd - // Map renamed files → their old path so getContent resolves the "old" side - // from where the file lived at `base`. - const renames = new Map( - changedFiles.filter((f) => f.status === "renamed" && f.oldPath).map((f) => [f.path, f.oldPath as string]), - ) - const getContent = - opts.getContent ?? makeContentResolver({ base, head: opts.head, cwd: opts.cwd, renames, gitRoot }) // Resolve the manifest against the PROJECT being reviewed (cwd), not the // binary's process.cwd() — otherwise a relative path silently misses when the @@ -241,11 +235,10 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise ? config.manifestPath : path.join(opts.cwd, config.manifestPath) // The "effective dbt project root" for downstream lookups (dbtProjectName, - // compiled-SQL resolver). Starts as opts.cwd — G3 auto-discovery updates it - // when it finds a dbt_project.yml in an ancestor directory, so the compiled - // resolver and project-name lookups target the discovered project rather - // than the CLI's working directory (which may be a subdir). - let dbtRoot = opts.cwd + // compiled-SQL resolver, scope). The nearest dbt_project.yml at or above cwd + // (inside the repo); G3 auto-discovery below may refine it. + const project = await findDbtProject(opts.cwd, gitRoot, manifestAbs) + let dbtRoot = project ?? opts.cwd // G3 (Round 18) — auto-discovery when the caller didn't pass `--manifest` and // the config-relative resolve above doesn't point at an existing file. Walk // upward for the dbt project root and use its `target/manifest.json`. Explicit @@ -270,6 +263,44 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise } } } + // The dbt project's location inside the repo ("" = repo root). Undefined + // when no dbt_project.yml was found or it lies outside the repo: then + // nothing is scoped out and conventional directory names classify files. + const projectRootRel = await repoRelative(gitRoot, dbtRoot, project !== undefined || dbtRoot !== opts.cwd) + const layout = projectRootRel !== undefined ? await dbtProjectLayout(dbtRoot, projectRootRel) : undefined + + // Only resolve a base ref if we actually need git (to collect changed files + // or to read old/new content). A caller that supplies BOTH `changedFiles` and + // `getContent` (e.g. a non-git CI integration) must not be forced through a + // git lookup that can fail when there's no usable history. + // + // The base is always the MERGE-BASE of the PR's target branch and the head: + // the diff, and every "old" file version, come from the commit the PR + // branched from, so commits the target gained since never leak in as changes. + const needGit = !opts.changedFiles || !opts.getContent + const resolved: ResolvedBase | undefined = needGit + ? await resolveReviewBase({ cwd: opts.cwd, base: opts.base, head: opts.head }) + : undefined + if (resolved && resolved.source !== "flag") { + process.stderr.write(`ℹ️ review base: ${resolved.ref} (${resolved.source}), merge-base ${resolved.sha.slice(0, 12)}\n`) + } + const base = resolved?.sha ?? opts.base ?? "" + const collected = opts.changedFiles ?? (await collectChangedFiles({ base, head: opts.head, cwd: opts.cwd })) + const scoped = scopeToProject(collected, projectRootRel) + const changedFiles = scoped.inScope + if (scoped.outOfScope > 0) { + process.stderr.write( + `ℹ️ ${scoped.outOfScope} changed file(s) outside the dbt project (${projectRootRel || "."}) were not reviewed.\n`, + ) + } + // Map renamed files → their old path so getContent resolves the "old" side + // from where the file lived at `base`. + const renames = new Map( + changedFiles.filter((f) => f.status === "renamed" && f.oldPath).map((f) => [f.path, f.oldPath as string]), + ) + const getContent = + opts.getContent ?? makeContentResolver({ base, head: opts.head, cwd: opts.cwd, renames, gitRoot }) + // Freshness check: detect (and warn — non-fatal) when the manifest predates // change-affecting files. Runs for both the pinned-head CI shape AND the // working-tree local shape — the local scenario "dbt compile once, edit for @@ -353,5 +384,69 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise explainTier: opts.explainTier, forceTier: opts.forceTier, staleManifest, + layout, + scope: { + baseRef: resolved?.ref ?? opts.base, + baseSha: resolved?.sha, + baseSource: resolved?.source, + projectRoot: projectRootRel, + outOfScopeFiles: scoped.outOfScope || undefined, + movedUnchangedFiles: scoped.movedUnchanged || undefined, + }, }) } + +/** + * The dbt project being reviewed: the nearest directory at or above `cwd` + * (not above the repo root) holding dbt_project.yml; else the project that + * owns the configured manifest (`/target/manifest.json`). + */ +async function findDbtProject(cwd: string, gitRoot: string, manifestAbs: string): Promise { + const hasProjectFile = async (dir: string) => { + for (const fn of ["dbt_project.yml", "dbt_project.yaml"]) { + try { + await access(path.join(dir, fn)) + return true + } catch { + /* try next */ + } + } + return false + } + let stop = path.resolve(gitRoot) + try { + stop = await realpath(stop) + } catch { + /* keep unresolved */ + } + let dir = path.resolve(cwd) + try { + dir = await realpath(dir) + } catch { + /* keep unresolved */ + } + for (;;) { + if (await hasProjectFile(dir)) return dir + if (dir === stop || path.dirname(dir) === dir) break + dir = path.dirname(dir) + } + const owner = path.dirname(path.dirname(manifestAbs)) + return (await hasProjectFile(owner)) ? owner : undefined +} + +/** `dir` relative to the repo root in POSIX form, or undefined when unknown + * or outside the repo. */ +async function repoRelative(gitRoot: string, dir: string, known: boolean): Promise { + if (!known) return undefined + let a = gitRoot + let b = dir + try { + a = await realpath(gitRoot) + b = await realpath(dir) + } catch { + /* compare unresolved */ + } + const rel = path.relative(a, b).replace(/\\/g, "/") + if (rel.startsWith("..") || path.isAbsolute(rel)) return undefined + return rel +} diff --git a/packages/opencode/src/altimate/review/verdict.ts b/packages/opencode/src/altimate/review/verdict.ts index fe7441bb81..4a2fa372a7 100644 --- a/packages/opencode/src/altimate/review/verdict.ts +++ b/packages/opencode/src/altimate/review/verdict.ts @@ -89,6 +89,28 @@ export const EngineVersions = z.object({ }) export type EngineVersions = z.infer +/** 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. */ +export const ReviewScope = z.object({ + /** Ref the base was resolved from (e.g. `origin/deployment`). */ + baseRef: z.string().optional(), + /** Merge-base commit the diff and the old file versions were taken from. */ + baseSha: z.string().optional(), + baseSource: z + .enum(["flag", "github_event", "github_env", "gitlab_env", "remote_default", "default_branch", "parent"]) + .optional(), + /** dbt project directory relative to the repository root ("" = root). */ + projectRoot: z.string().optional(), + /** Changed files outside the dbt project: counted, not reviewed. */ + outOfScopeFiles: z.number().int().nonnegative().optional(), + /** Renames with unchanged content and name (pure moves): not re-reviewed. */ + movedUnchangedFiles: z.number().int().nonnegative().optional(), + /** Models the engine lanes could not verify, with the reason. */ + unverified: z.array(z.object({ path: z.string(), reason: z.string() })).optional(), +}) +export type ReviewScope = z.infer + export const VerdictEnvelope = z.object({ version: z.literal("1"), verdict: Verdict, @@ -120,6 +142,8 @@ export const VerdictEnvelope = z.object({ * against out-of-date metadata — a stderr warning alone is easy for CI to * swallow. */ staleManifest: z.boolean().optional(), + /** Base resolution and review coverage (see ReviewScope). */ + scope: ReviewScope.optional(), /** ISO timestamp; injected by the caller (no clock access in pure code). */ generatedAt: z.string().optional(), /** Optional break-glass override record. */ @@ -178,6 +202,7 @@ export interface BuildEnvelopeInput { tierClassified?: RiskTier /** True when mtime signals the manifest predates a change-affecting file. */ staleManifest?: boolean + scope?: ReviewScope } function summarize(findings: Finding[], degraded: boolean): VerdictEnvelope["summary"] { @@ -206,6 +231,7 @@ export function buildEnvelope(input: BuildEnvelopeInput): VerdictEnvelope { engine: EngineVersions.parse(input.engine ?? {}), manifestHash: input.manifestHash, staleManifest: input.staleManifest ? true : undefined, + scope: input.scope, generatedAt: input.generatedAt, }) } diff --git a/packages/opencode/src/altimate/tools/dbt-pr-review.ts b/packages/opencode/src/altimate/tools/dbt-pr-review.ts index 16ce290242..ab3ceab013 100644 --- a/packages/opencode/src/altimate/tools/dbt-pr-review.ts +++ b/packages/opencode/src/altimate/tools/dbt-pr-review.ts @@ -27,7 +27,7 @@ export const DbtPrReviewTool = Tool.define("dbt_pr_review", { '- dbt_pr_review({ manifest_path: "target/manifest.json", mode: "gate" })', ].join("\n"), parameters: z.object({ - base: z.string().optional().describe("Base git ref. Defaults to merge-base with origin/main."), + base: z.string().optional().describe("Base git ref. Defaults to the PR target branch from CI, else the remote default branch, reduced to the merge-base."), head: z.string().optional().describe("Head git ref. Omit to review the working tree."), manifest_path: z .string() diff --git a/packages/opencode/src/cli/cmd/review.ts b/packages/opencode/src/cli/cmd/review.ts index 88d6395b41..7897396ac5 100644 --- a/packages/opencode/src/cli/cmd/review.ts +++ b/packages/opencode/src/cli/cmd/review.ts @@ -31,7 +31,7 @@ export const ReviewCommand = cmd({ // (exit 0, no review runs). With `boolean-negation: false`, `--no-ai` binds // to the declared `noAi` boolean flag as authored. B1 fix, Round 18. .parserConfiguration({ "boolean-negation": false }) - .option("base", { type: "string", describe: "base git ref (default: merge-base with origin/main)" }) + .option("base", { type: "string", describe: "base git ref (default: the PR target branch from CI, else the remote default branch; always reduced to the merge-base)" }) .option("head", { type: "string", describe: "head git ref (default: working tree)" }) .option("manifest", { type: "string", describe: "path to dbt manifest.json (auto-discovered under target/ when omitted)" }) .option("mode", { diff --git a/packages/opencode/test/altimate/review-scope-diff.test.ts b/packages/opencode/test/altimate/review-scope-diff.test.ts new file mode 100644 index 0000000000..2c11c4bbc4 --- /dev/null +++ b/packages/opencode/test/altimate/review-scope-diff.test.ts @@ -0,0 +1,339 @@ +import { describe, test, expect, afterEach } from "bun:test" +import { promises as fs } from "node:fs" +import path from "node:path" +import { tmpdir } from "node:os" +import { execFile } from "node:child_process" +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 { DEFAULT_REVIEW_CONFIG } from "../../src/altimate/review/config" +import { DEFAULT_RUBRIC } from "../../src/altimate/review/rubric" +import { reviewPullRequest } from "../../src/altimate/review/run" + +/** + * The review must look at exactly the PR's changes, in the dbt + * project it was pointed at. Each test builds a real git topology in a temp + * dir (synthetic, no customer data) for one input shape that is common in + * public dbt repos: a PR into a non-default branch, a base that moved after + * branching, a monorepo with several dbt projects, renames, custom + * `model-paths`, and a `dbt parse`-only CI with no compiled SQL. + */ + +const exec = promisify(execFile) +const g = (cwd: string, ...args: string[]) => exec("git", args, { cwd }).then((r) => r.stdout.trim()) + +async function write(root: string, rel: string, body: string) { + await fs.mkdir(path.dirname(path.join(root, rel)), { recursive: true }) + await fs.writeFile(path.join(root, rel), body) +} + +async function commitAll(root: string, msg: string) { + await g(root, "add", "-A") + await g(root, "commit", "-qm", msg) + return g(root, "rev-parse", "HEAD") +} + +async function newRepo(): Promise { + const root = await fs.realpath(await fs.mkdtemp(path.join(tmpdir(), "review-scope-"))) + await g(root, "init", "-q", "-b", "main") + await g(root, "config", "user.email", "t@test") + await g(root, "config", "user.name", "t") + await g(root, "config", "commit.gpgsign", "false") + return root +} + +const noEnv: NodeJS.ProcessEnv = {} + +describe("resolveReviewBase: the PR's real target branch, as a merge-base", () => { + test("PR into a drifted non-default branch diffs against that branch, not main", async () => { + const root = await newRepo() + await write(root, "models/a.sql", "select 1 as id\n") + await commitAll(root, "seed") + await g(root, "branch", "release") + // main drifts ahead with 3 commits the PR never touched. + for (let i = 0; i < 3; i++) { + await write(root, `models/drift_${i}.sql`, `select ${i} as id\n`) + await commitAll(root, `drift ${i}`) + } + await g(root, "checkout", "-q", "-b", "feature", "release") + await write(root, "models/a.sql", "select 1 as id, 2 as x\n") + await commitAll(root, "pr change") + + const eventPath = path.join(root, "..", `event-${path.basename(root)}.json`) + await fs.writeFile(eventPath, JSON.stringify({ pull_request: { base: { ref: "release" } } })) + const resolved = await resolveReviewBase({ cwd: root, env: { GITHUB_EVENT_PATH: eventPath } }) + expect(resolved.source).toBe("github_event") + expect(resolved.ref).toBe("release") + expect(resolved.sha).toBe(await g(root, "rev-parse", "release")) + + const files = await collectChangedFiles({ base: resolved.sha, head: "HEAD", cwd: root }) + expect(files.map((f) => f.path)).toEqual(["models/a.sql"]) + }) + + test("GITHUB_BASE_REF and GitLab's MR target branch are honoured", async () => { + const root = await newRepo() + await write(root, "a.sql", "select 1\n") + await commitAll(root, "seed") + await g(root, "branch", "develop") + await g(root, "checkout", "-q", "-b", "feature") + await write(root, "a.sql", "select 2\n") + await commitAll(root, "pr") + expect((await resolveReviewBase({ cwd: root, env: { GITHUB_BASE_REF: "develop" } })).source).toBe("github_env") + const gl = await resolveReviewBase({ cwd: root, env: { CI_MERGE_REQUEST_TARGET_BRANCH_NAME: "develop" } }) + expect(gl.source).toBe("gitlab_env") + expect(gl.ref).toBe("develop") + }) + + test("a CI target branch that is not fetched fails loudly instead of guessing main", async () => { + const root = await newRepo() + await write(root, "a.sql", "select 1\n") + await commitAll(root, "seed") + await expect(resolveReviewBase({ cwd: root, env: { GITHUB_BASE_REF: "deployment" } })).rejects.toThrow( + /deployment.*not available locally/, + ) + }) + + test("explicit --base wins and is reduced to the merge-base", async () => { + const root = await newRepo() + await write(root, "a.sql", "select 1\n") + const forkPoint = await commitAll(root, "seed") + await g(root, "checkout", "-q", "-b", "feature") + await write(root, "b.sql", "select 2\n") + await commitAll(root, "pr") + await g(root, "checkout", "-q", "main") + await write(root, "c.sql", "select 3\n") + await commitAll(root, "main moves on") + await g(root, "checkout", "-q", "feature") + const r = await resolveReviewBase({ cwd: root, base: "main", env: noEnv }) + expect(r.source).toBe("flag") + expect(r.sha).toBe(forkPoint) + }) +}) + +describe("collectChangedFiles: only the PR's own changes", () => { + test("commits the base gains after branching do not leak in (working-tree and head diffs)", async () => { + const root = await newRepo() + await write(root, "models/a.sql", "select 1\n") + await commitAll(root, "seed") + await g(root, "checkout", "-q", "-b", "feature") + await write(root, "models/pr.sql", "select 2\n") + await commitAll(root, "pr") + await g(root, "checkout", "-q", "main") + await write(root, "models/later.sql", "select 3\n") + await commitAll(root, "main moves on") + await g(root, "checkout", "-q", "feature") + + const { sha } = await resolveReviewBase({ cwd: root, base: "main", env: noEnv }) + const withHead = await collectChangedFiles({ base: sha, head: "HEAD", cwd: root }) + const workingTree = await collectChangedFiles({ base: sha, cwd: root }) + expect(withHead.map((f) => f.path)).toEqual(["models/pr.sql"]) + expect(workingTree.map((f) => f.path)).toEqual(["models/pr.sql"]) + }) + + test("a moved-and-edited model is paired as a rename with only the edited lines in its diff", async () => { + const root = await newRepo() + const body = Array.from({ length: 20 }, (_, i) => ` , col_${i}`).join("\n") + await write(root, "models/staging/stg_orders.sql", `select\n id\n${body}\nfrom src\n`) + await commitAll(root, "seed") + const base = await g(root, "rev-parse", "HEAD") + await fs.mkdir(path.join(root, "models/core"), { recursive: true }) + await g(root, "mv", "models/staging/stg_orders.sql", "models/core/stg_orders.sql") + await write(root, "models/core/stg_orders.sql", `select\n id\n${body}\n , col_new\nfrom src\n`) + await commitAll(root, "move and edit") + + const [f] = await collectChangedFiles({ base, head: "HEAD", cwd: root }) + expect(f.status).toBe("renamed") + expect(f.oldPath).toBe("models/staging/stg_orders.sql") + expect(f.similarity).toBeLessThan(100) + const added = f.diff!.split("\n").filter((l) => l.startsWith("+") && !l.startsWith("+++")) + expect(added).toEqual(["+ , col_new"]) + }) + + test("a pure move is reported at 100% similarity", async () => { + const root = await newRepo() + await write(root, "models/a/x.sql", "select 1 as id\nfrom src\n") + await commitAll(root, "seed") + const base = await g(root, "rev-parse", "HEAD") + await fs.mkdir(path.join(root, "models/b"), { recursive: true }) + await g(root, "mv", "models/a/x.sql", "models/b/x.sql") + await commitAll(root, "move") + const [f] = await collectChangedFiles({ base, head: "HEAD", cwd: root }) + expect(f.status).toBe("renamed") + expect(f.similarity).toBe(100) + }) +}) + +describe("scopeToProject: the dbt project being reviewed, nothing else", () => { + const files: ChangedFile[] = [ + { path: "projects/a/models/x.sql", status: "modified" }, + { path: "projects/b/models/y.sql", status: "modified" }, + { path: ".github/workflows/ci.yml", status: "modified" }, + { path: "projects/a/models/moved/z.sql", oldPath: "projects/a/models/z.sql", status: "renamed", similarity: 100 }, + { path: "projects/a/models/w2.sql", oldPath: "projects/a/models/w.sql", status: "renamed", similarity: 100 }, + ] + + test("monorepo: sibling projects and non-dbt files are counted, not reviewed", () => { + const r = scopeToProject(files, "projects/a") + expect(r.inScope.map((f) => f.path)).toEqual(["projects/a/models/x.sql", "projects/a/models/w2.sql"]) + expect(r.outOfScope).toBe(2) + // A pure directory move keeps the model name → dropped; a rename that + // changes the file (model) name can break refs → kept. + expect(r.movedUnchanged).toBe(1) + }) + + test("project at the repo root keeps every file", () => { + expect(scopeToProject(files, "").outOfScope).toBe(0) + }) + + test("unknown project root scopes nothing out", () => { + expect(scopeToProject(files, undefined).inScope.length).toBe(4) + }) +}) + +describe("classifyDbtFile: the project's own directories decide", () => { + test("custom model-paths are models, and the layout root is stripped", async () => { + const root = await newRepo() + await write(root, "projects/a/dbt_project.yml", "name: a\nmodel-paths: [models, cases]\nsnapshot-paths: [snaps]\n") + const layout = await dbtProjectLayout(path.join(root, "projects/a"), "projects/a") + expect(layout?.dirs.model).toEqual(["models", "cases"]) + expect(layout?.dirs.seed).toEqual(["seeds"]) + expect(classifyDbtFile("projects/a/cases/i01/i01_revenue.sql", layout)).toBe("model_sql") + expect(classifyDbtFile("projects/a/snaps/orders.sql", layout)).toBe("snapshot") + expect(classifyDbtFile("projects/a/models/tests/helper.sql", layout)).toBe("model_sql") + // Without the layout a custom model path is unknown. + expect(classifyDbtFile("projects/a/cases/i01/i01_revenue.sql")).toBe("other") + }) + + test("without a layout the outermost conventional directory wins", () => { + expect(classifyDbtFile("models/tests/helper.sql")).toBe("model_sql") + expect(classifyDbtFile("tests/models/assert_x.sql")).toBe("test") + expect(classifyDbtFile("models/marts/_marts.yml")).toBe("schema_yml") + }) +}) + +describe("raw Jinja never reaches the engine lanes", () => { + function runnerRecording(seen: string[]): ReviewRunner { + return { + async impact() { + return { hasManifest: true, severity: "SAFE", directCount: 0, transitiveCount: 0, testCount: 0 } + }, + async grade() { + return { grade: "B" } + }, + async check(sql: string) { + seen.push(sql) + // What core reports for Jinja text today. + return /\{\{/.test(sql) + ? { issues: [{ rule: "encoding_bypass", message: "suspicious template", severity: "warning" }], ran: true } + : { issues: [], ran: true } + }, + async equivalence() { + return { decided: false } + }, + async detectPii(sql: string) { + seen.push(sql) + return { columns: [] } + }, + } as ReviewRunner + } + + test("two new Jinja models with no compiled SQL: no engine warnings, marked unverified", async () => { + const seen: string[] = [] + const sql = "{{ config(materialized='table') }}\nselect id from {{ ref('stg_orders') }}\n" + const env = await runReview({ + changedFiles: [ + { path: "models/marts/fct_a.sql", status: "added", diff: "+" + sql }, + { path: "models/marts/fct_b.sql", status: "added", diff: "+" + sql }, + ], + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["sql_quality"] }, + rubric: DEFAULT_RUBRIC, + mode: "gate", + runner: runnerRecording(seen), + getContent: async () => sql, + getCompiled: async () => undefined, + generatedAt: "2026-11-12T00:00:00Z", + }) + expect(seen.some((s) => s.includes("{{"))).toBe(false) + expect(env.findings.some((f) => f.evidence?.tool === "altimate_core.check")).toBe(false) + expect(env.verdict).not.toBe("REQUEST_CHANGES") + expect(env.scope?.unverified?.map((u) => u.path)).toEqual(["models/marts/fct_a.sql", "models/marts/fct_b.sql"]) + }) + + test("a plain-SQL model without Jinja still runs through the engine", async () => { + const seen: string[] = [] + await runReview({ + changedFiles: [{ path: "models/marts/fct_a.sql", status: "added", diff: "+select 1 as id\n" }], + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["sql_quality"] }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner: runnerRecording(seen), + getContent: async () => "select 1 as id\n", + getCompiled: async () => undefined, + }) + expect(seen).toContain("select 1 as id\n") + }) + + test("a renamed-and-edited model gets its old side, so before/after lanes run", async () => { + let compared: [string, string] | undefined + const runner: ReviewRunner = { + ...runnerRecording([]), + async equivalence(o: string, n: string) { + compared = [o, n] + return { decided: true, equivalent: true } + }, + } as ReviewRunner + await runReview({ + changedFiles: [ + { path: "models/core/m.sql", oldPath: "models/staging/m.sql", status: "renamed", similarity: 90, diff: "" }, + ], + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["semantic_change"] }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + getContent: async (_f, side) => (side === "old" ? "select 1 as id" : "select 1 as id, 2 as x"), + getCompiled: async (f, side) => (side === "old" && f === "models/staging/m.sql" ? "select 1 as id" : "select 1 as id, 2 as x"), + }) + expect(compared).toEqual(["select 1 as id", "select 1 as id, 2 as x"]) + }) +}) + +describe("reviewPullRequest end to end: monorepo, PR into a non-default branch", () => { + const saved = { ...process.env } + afterEach(() => { + for (const k of ["GITHUB_EVENT_PATH", "GITHUB_BASE_REF", "CI_MERGE_REQUEST_TARGET_BRANCH_NAME"]) { + if (saved[k] === undefined) delete process.env[k] + else process.env[k] = saved[k] + } + }) + + test("reviews only the PR's files inside --cwd's project and records the base on the envelope", async () => { + const root = await newRepo() + await write(root, "projects/a/dbt_project.yml", "name: a\nmodel-paths: [models, cases]\n") + await write(root, "projects/b/dbt_project.yml", "name: b\n") + await write(root, "projects/a/models/base_a.sql", "select 1 as id\n") + await write(root, "projects/b/models/base_b.sql", "select 1 as id\n") + await commitAll(root, "seed") + await g(root, "branch", "release") + await write(root, "projects/a/models/drift.sql", "select 9 as id\n") + await commitAll(root, "main drifts") + await g(root, "checkout", "-q", "-b", "feature", "release") + await write(root, "projects/a/cases/c1/c1_model.sql", "select 2 as id\n") + await write(root, "projects/b/models/sibling.sql", "select 3 as id\n") + await write(root, "README.md", "docs\n") + await commitAll(root, "pr") + + delete process.env.GITHUB_EVENT_PATH + delete process.env.CI_MERGE_REQUEST_TARGET_BRANCH_NAME + process.env.GITHUB_BASE_REF = "release" + const env = await reviewPullRequest({ cwd: path.join(root, "projects/a"), head: "HEAD", noAi: true }) + expect(env.scope?.baseSource).toBe("github_env") + expect(env.scope?.baseSha).toBe(await g(root, "rev-parse", "release")) + expect(env.scope?.projectRoot).toBe("projects/a") + // sibling project file + README are out of scope; drift on main never appears. + expect(env.scope?.outOfScopeFiles).toBe(2) + expect(env.findings.every((f) => f.file.startsWith("projects/a/"))).toBe(true) + expect(env.findings.some((f) => f.file.includes("drift"))).toBe(false) + }, 60_000) +}) diff --git a/packages/opencode/test/altimate/review.test.ts b/packages/opencode/test/altimate/review.test.ts index 24bde20a7a..113a2202fd 100644 --- a/packages/opencode/test/altimate/review.test.ts +++ b/packages/opencode/test/altimate/review.test.ts @@ -1129,6 +1129,8 @@ describe("orchestrate", () => { mode: "comment", runner: withCore, getContent: content(sql), + // Core lint runs on dbt-compiled SQL; raw Jinja never reaches the engine. + getCompiled: async () => "select * from prod.a a, prod.b b", generatedAt: "2026-05-29T00:00:00Z", }) // The regex comma-join twin is dropped; the core AST finding remains. @@ -1361,6 +1363,7 @@ describe("orchestrate", () => { mode: "comment", runner, getContent: content(sql), + getCompiled: async () => "select getdate() as loaded_at from prod.a", generatedAt: "2026-05-29T00:00:00Z", }) // Core L033 is the single portability source.