Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions docs/docs/usage/dbt-pr-review.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Options:

| Flag | Description |
|------|-------------|
| `--base <ref>` | 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 <ref>` | 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 <ref>` | Head git ref. Omit to review the working tree. |
| `--manifest <path>` | 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`. |
Expand Down Expand Up @@ -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. |
Expand Down Expand Up @@ -296,6 +296,12 @@ 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, the earlier block stays.

### What the PR comment shows

Expand All @@ -311,7 +317,14 @@ rubric:
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.
columns the base already surfaced. A model the PII check could not analyse is
listed as not fully verified.
- **Failures are never a clean pass:** if an engine check throws, the comment
names it and the verdict is at most COMMENT, never APPROVE (envelope

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The docs overstate the engine-error downgrade: a gate-mode run with an engine error and another blocking finding can still return REQUEST_CHANGES. Limit this statement to otherwise empty reviews, which are downgraded from APPROVE to COMMENT.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At docs/docs/usage/dbt-pr-review.md, line 317:

<comment>The docs overstate the engine-error downgrade: a gate-mode run with an engine error and another blocking finding can still return `REQUEST_CHANGES`. Limit this statement to otherwise empty reviews, which are downgraded from APPROVE to COMMENT.</comment>

<file context>
@@ -311,7 +311,14 @@ rubric:
+  columns the base already surfaced. A model the PII check could not analyse is
+  listed as not fully verified.
+- **Failures are never a clean pass:** if an engine check throws, the comment
+  names it and the verdict is at most COMMENT, never APPROVE (envelope
+  `engineErrors`). A failure on one file marks that file unverified and the rest
+  of the PR is still reviewed.
</file context>
Suggested change
names it and the verdict is at most COMMENT, never APPROVE (envelope
names it; an otherwise empty review is COMMENT rather than APPROVE (envelope

`engineErrors`). A failure on one file marks that file unverified and the rest
of the PR is still reviewed.
- **The gate clears itself:** when a rerun no longer blocks, the earlier blocking
altimate review is dismissed (needs `pull-requests: write`).

### Risk-tier path tokens

Expand Down
20 changes: 13 additions & 7 deletions packages/opencode/src/altimate/review/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,19 @@ 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) {
const head = unverified.slice(0, 5).map((u) => `\`${u.path}\``).join(", ")
const more = unverified.length > 5 ? ` and ${unverified.length - 5} more` : ""
notes.push(
`${unverified.length} model(s) not verified by the engine — ${unverified[0].reason}: ${head}${more}. ` +
"Run `dbt compile` (and compile the base into `target-base/`) for equivalence and lineage checks.",
)
const byReason = new Map<string, string[]>()
for (const u of unverified) byReason.set(u.reason, [...(byReason.get(u.reason) ?? []), u.path])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: Grouping by the full reason text fragments per-file error messages into one note each, so a widespread failure produces many notes. Group by a normalized reason category, or cap the number of distinct notes, so the comment stays compact.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/opencode/src/altimate/review/format.ts, line 210:

<comment>Grouping by the full reason text fragments per-file error messages into one note each, so a widespread failure produces many notes. Group by a normalized reason category, or cap the number of distinct notes, so the comment stays compact.</comment>

<file context>
@@ -206,13 +206,19 @@ export function coverageNotes(env: VerdictEnvelope): string[] {
-        "Run `dbt compile` (and compile the base into `target-base/`) for equivalence and lineage checks.",
-    )
+  const byReason = new Map<string, string[]>()
+  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(", ")
</file context>

Comment on lines +209 to +210

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[🟠 MEDIUM] Using the array spread operator within a loop to accumulate items creates a new array on each iteration. This can lead to O(N²) time complexity and unnecessary memory allocations when there are many items. It is more efficient to mutate the existing array by pushing new elements.

Suggested change:

Suggested change
const byReason = new Map<string, string[]>()
for (const u of unverified) byReason.set(u.reason, [...(byReason.get(u.reason) ?? []), u.path])
const byReason = new Map<string, string[]>()
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("; ")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: e.reason is not sanitized here, and PR-level lane errors can contain newlines. That breaks the > ℹ️ blockquote in the posted comment. Collapse whitespace before interpolating the reason.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/opencode/src/altimate/review/format.ts, line 220:

<comment>`e.reason` is not sanitized here, and PR-level lane errors can contain newlines. That breaks the `> ℹ️` blockquote in the posted comment. Collapse whitespace before interpolating the reason.</comment>

<file context>
@@ -206,13 +206,19 @@ export function coverageNotes(env: VerdictEnvelope): string[] {
+    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.`)
   }
</file context>

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.`)
Expand Down
38 changes: 35 additions & 3 deletions packages/opencode/src/altimate/review/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,33 @@ async function mergeBase(a: string, b: string, cwd: string): Promise<string | un
}
}

/**
* A default CI checkout is shallow (`actions/checkout` fetches one commit) and
* lacks the target branch. Fetch the branch, then deepen the history until a
* merge-base exists, so the review works without `fetch-depth: 0`. Returns
* true once `found` resolves; false when it never does (no remote, no access).
*/
async function fetchTargetHistory(branch: string, cwd: string, found: () => Promise<unknown>): Promise<boolean> {
if (branch.startsWith("-")) return false
try {
await git(["fetch", "--no-tags", "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 git(["rev-parse", "--is-shallow-repository"], cwd)).trim() !== "true") return false
const headSha = (await git(["rev-parse", "HEAD"], cwd)).trim()
await git(["fetch", "--no-tags", `--deepen=${depth}`, "origin", `+refs/heads/${branch}:refs/remotes/origin/${branch}`, headSha], cwd)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The deepen fetch adds headSha as a raw SHA request, which requires the remote to allow reachable SHA wants and can make otherwise-valid CI remotes reject the fetch. Remove this extra argument; the target-branch refspec is sufficient for deepening the shallow checkout.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/opencode/src/altimate/review/git.ts, line 212:

<comment>The deepen fetch adds `headSha` as a raw SHA request, which requires the remote to allow reachable SHA wants and can make otherwise-valid CI remotes reject the fetch. Remove this extra argument; the target-branch refspec is sufficient for deepening the shallow checkout.</comment>

<file context>
@@ -191,6 +191,33 @@ async function mergeBase(a: string, b: string, cwd: string): Promise<string | un
+    try {
+      if ((await git(["rev-parse", "--is-shallow-repository"], cwd)).trim() !== "true") return false
+      const headSha = (await git(["rev-parse", "HEAD"], cwd)).trim()
+      await git(["fetch", "--no-tags", `--deepen=${depth}`, "origin", `+refs/heads/${branch}:refs/remotes/origin/${branch}`, headSha], cwd)
+    } catch {
+      return false
</file context>
Suggested change
await git(["fetch", "--no-tags", `--deepen=${depth}`, "origin", `+refs/heads/${branch}:refs/remotes/origin/${branch}`, headSha], cwd)
await git(["fetch", "--no-tags", `--deepen=${depth}`, "origin", `+refs/heads/${branch}:refs/remotes/origin/${branch}`], cwd)

} catch {
return false
}
if (await found()) return true
}
return false
}
Comment on lines +194 to +219

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[🟠 MEDIUM] By making fetchTargetHistory return a boolean, the caller is forced to execute await find() an extra time when the fetch succeeds. Since find() invokes an external git merge-base process, this results in redundant and potentially expensive operations. It is recommended to use a generic type so the function can return the found result directly, avoiding double evaluation.

Suggested change:

Suggested change
/**
* A default CI checkout is shallow (`actions/checkout` fetches one commit) and
* lacks the target branch. Fetch the branch, then deepen the history until a
* merge-base exists, so the review works without `fetch-depth: 0`. Returns
* true once `found` resolves; false when it never does (no remote, no access).
*/
async function fetchTargetHistory(branch: string, cwd: string, found: () => Promise<unknown>): Promise<boolean> {
if (branch.startsWith("-")) return false
try {
await git(["fetch", "--no-tags", "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 git(["rev-parse", "--is-shallow-repository"], cwd)).trim() !== "true") return false
const headSha = (await git(["rev-parse", "HEAD"], 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
}
/**
* A default CI checkout is shallow (`actions/checkout` fetches one commit) and
* lacks the target branch. Fetch the branch, then deepen the history until a
* merge-base exists, so the review works without `fetch-depth: 0`. Returns
* the resolved value once `found` succeeds; undefined when it never does (no remote, no access).
*/
async function fetchTargetHistory<T>(branch: string, cwd: string, found: () => Promise<T | undefined>): Promise<T | undefined> {
if (branch.startsWith("-")) return undefined
try {
await git(["fetch", "--no-tags", "origin", `+refs/heads/${branch}:refs/remotes/origin/${branch}`], cwd)
} catch {
return undefined
}
let result = await found()
if (result) return result
for (const depth of [50, 200, 1000]) {
try {
if ((await git(["rev-parse", "--is-shallow-repository"], cwd)).trim() !== "true") return undefined
const headSha = (await git(["rev-parse", "HEAD"], cwd)).trim()
await git(["fetch", "--no-tags", `--deepen=${depth}`, "origin", `+refs/heads/${branch}:refs/remotes/origin/${branch}`, headSha], cwd)
} catch {
return undefined
}
result = await found()
if (result) return result
}
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) {
Expand Down Expand Up @@ -230,10 +257,15 @@ export async function resolveReviewBase(opts: ResolveBaseOptions): Promise<Resol

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 }
const find = async () => {
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, opts.cwd, find)) ? await find() : undefined)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The result of find() inside fetchTargetHistory is thrown away and find() runs again here. Have fetchTargetHistory return the resolved base (or undefined) so the caller uses it directly, which also removes the boolean-plus-callback pattern.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/opencode/src/altimate/review/git.ts, line 267:

<comment>The result of `find()` inside `fetchTargetHistory` is thrown away and `find()` runs again here. Have `fetchTargetHistory` return the resolved base (or undefined) so the caller uses it directly, which also removes the boolean-plus-callback pattern.</comment>

<file context>
@@ -230,10 +257,15 @@ export async function resolveReviewBase(opts: ResolveBaseOptions): Promise<Resol
+      }
+      return undefined
     }
+    const found = (await find()) ?? ((await fetchTargetHistory(target.branch, opts.cwd, find)) ? await find() : undefined)
+    if (found) return found
     throw new Error(
</file context>

if (found) return found
Comment on lines +267 to +268

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[🔵 LOW] Following the suggested refactoring of fetchTargetHistory, you can significantly simplify this caller logic. This effectively removes the complex ternary expression and avoids the redundant invocation of find().

Suggested change:

Suggested change
const found = (await find()) ?? ((await fetchTargetHistory(target.branch, opts.cwd, find)) ? await find() : undefined)
if (found) return found
const found = (await find()) ?? (await fetchTargetHistory(target.branch, opts.cwd, find))
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.`,
Expand Down
105 changes: 73 additions & 32 deletions packages/opencode/src/altimate/review/orchestrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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
Expand All @@ -118,7 +120,10 @@ export interface ReviewRunner {
grade(sql: string, dialect: string): Promise<GradeResult>
check(sql: string, dialect: string, baseSql?: string): Promise<CheckResult>
equivalence(oldSql: string, newSql: string, dialect?: string): Promise<EquivalenceResult>
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.
Expand Down Expand Up @@ -1147,35 +1152,55 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
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<Finding[]>) =>
lane.catch((err) => {
laneErrors.push({ method, reason: (err instanceof Error ? err.message : String(err)).slice(0, 200), count: 1 })
return [] as Finding[]
})
const failedFile = (file: { path: string }, err: unknown) =>
unverified.push({
path: file.path,
reason: `review failed for this file: ${(err instanceof Error ? err.message : String(err)).replace(/\s+/g, " ").slice(0, 160)}`,
})
Comment on lines +1156 to +1166

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[🔵 LOW] Low Severity: Error object stringification obscuring details & Duplication of unverified files

  1. Object Stringification: Using String(err) on plain object errors (e.g., { code: 500, message: "failed" }) will evaluate to "[object Object]", making debugging difficult. We should serialize plain objects using JSON.stringify().
  2. Duplication in unverified: A single file might be pushed to unverified multiple times (e.g., once for PII abstained, and again here if a lane crashes). The downstream consumer or UI may not expect duplicate paths. It's recommended to deduplicate or aggregate reasons for the same path.

You can create a helper to format errors and ensure duplicate entries for the same file path are merged or ignored.

Suggested change:

Suggested change
const laneErrors: Array<{ method: string; reason: string; count: number }> = []
const prLevel = (method: string, lane: Promise<Finding[]>) =>
lane.catch((err) => {
laneErrors.push({ method, reason: (err instanceof Error ? err.message : String(err)).slice(0, 200), count: 1 })
return [] as Finding[]
})
const failedFile = (file: { path: string }, err: unknown) =>
unverified.push({
path: file.path,
reason: `review failed for this file: ${(err instanceof Error ? err.message : String(err)).replace(/\s+/g, " ").slice(0, 160)}`,
})
const formatErr = (err: unknown) => err instanceof Error ? err.message : (typeof err === "object" && err !== null ? JSON.stringify(err) : String(err))
const laneErrors: Array<{ method: string; reason: string; count: number }> = []
// Consider taking a function `() => Promise<Finding[]>` and using try/catch if lanes can throw synchronously
const prLevel = (method: string, lane: Promise<Finding[]>) =>
lane.catch((err) => {
laneErrors.push({ method, reason: formatErr(err).slice(0, 200), count: 1 })
return [] as Finding[]
})
const failedFile = (file: { path: string }, err: unknown) => {
const reason = `review failed for this file: ${formatErr(err).replace(/\s+/g, " ").slice(0, 160)}`
const existing = unverified.find((u) => u.path === file.path)
if (existing) {
if (!existing.reason.includes(reason)) existing.reason += `; ${reason}`
} else {
unverified.push({ path: file.path, reason })
}
}

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)" })
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 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" })

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: An abstained PII result is reported but then treated as "no PII" for tiering. pii is assigned piiRes.columns, which is empty, so touchesPiiOf (line 1230) returns false and the FULL-tier "PII column touched" hard floor cannot fire. On trivial/lite tiers pii_exposure is not in TIER_LANES, so piiLane never runs for this file either, leaving only the coarse regex twins. Abstention means unknown, not clean — consider promoting the file (or keeping the broad fallback) when abstained is true.

const pii = piiRes.columns
const complex = engineNewSql && input.runner.isComplex ? await input.runner.isComplex(engineNewSql) : false

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: The per-file try is too coarse — a failure in a supplementary call discards the whole file.

ctxByPath.set is the last statement of the block (line 1190), so a throw from input.runner.impact(...) (1184), detectPii(...) (1186) or isComplex(...) (1189) leaves the file out of ctxByPath entirely. isComplex in particular is only a tiering hint, yet its failure means the file gets no quality / semantic-change / dbt-patterns lanes, no lineage lane, and no blast-radius / PII / complexity signal for tiering (blastRadiusOf / touchesPiiOf / isComplexOf at 1227-1231) — zero review even though its SQL was read successfully.

Consider per-call fallbacks (impact → zeroed result, pii: [], complex: false) and always calling ctxByPath.set once the content has been fetched, so an enrichment failure degrades the file instead of skipping it.

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)
}
}),
)

Expand Down Expand Up @@ -1327,20 +1352,32 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
if (lanes.has("semantic_change") || lanes.has("dbt_patterns")) {
tasks.push(structuralChangeLane(ctx, input.runner))
}
all.push(...(await Promise.all(tasks)))
// A lane that throws drops only its own findings for this file, recorded
// as unverified — never the whole review.
let laneFailed = false
const settled = await Promise.all(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Per-file isolation is incomplete: the new tasks catch does not cover the synchronous lanes (lineageBreakageLane, detectModelPatterns) or the schema.yml detector. A throw from any of them still rejects runReview and aborts the whole review. Wrap each per-file lane call in the same try/catch that tasks uses, so the failure goes to failedFile.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/opencode/src/altimate/review/orchestrate.ts, line 1349:

<comment>Per-file isolation is incomplete: the new `tasks` catch does not cover the synchronous lanes (`lineageBreakageLane`, `detectModelPatterns`) or the schema.yml detector. A throw from any of them still rejects `runReview` and aborts the whole review. Wrap each per-file lane call in the same try/catch that `tasks` uses, so the failure goes to `failedFile`.</comment>

<file context>
@@ -1318,20 +1343,32 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
+    // A lane that throws drops only its own findings for this file, recorded
+    // as unverified — never the whole review.
+    let laneFailed = false
+    const settled = await Promise.all(
+      tasks.map((t) =>
+        t.catch((err) => {
</file context>

tasks.map((t) =>
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
Expand Down Expand Up @@ -1393,7 +1430,9 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
[...ctxByPath.values()].map(async (ctx) => {
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)
Comment on lines +1433 to 1436

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[🔴 HIGH] High Severity: Risk of synchronous exceptions bypassing .catch()

The fault-tolerance logic heavily relies on chaining .catch() directly to the promise returned by input.runner.check(...). However, check is an interface method. If the underlying implementation is not a strict async function and throws a synchronous exception (e.g., during argument validation before returning a Promise), the exception will bypass the .catch() and crash the Promise.all block.

To safely handle both synchronous exceptions and asynchronous rejections, it's recommended to wrap the await within a try/catch block.

Suggested change:

Suggested change
const cr = await input.runner
.check(ctx.engineNewSql, dialect, ctx.engineOldSql)
.catch(() => ({ ran: false }) as CheckResult)
if (cr.ran) coreRanFiles.add(ctx.file.path)
let cr: CheckResult = { ran: false }
try {
cr = await input.runner.check(ctx.engineNewSql, dialect, ctx.engineOldSql)
} catch {
// ignore failures
}
if (cr.ran) coreRanFiles.add(ctx.file.path)

}),
)
Expand Down Expand Up @@ -1536,7 +1575,9 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
// undecidable finding does not make the whole run lint-only; those findings
// carry their own `degraded` flag and are counted separately in the summary.
const degraded = runDegraded
const engineErrors = [...(input.runner.failures?.() ?? []), ...laneErrors]
const envelope = buildEnvelope({
engineErrors: engineErrors.length ? engineErrors : undefined,
ai: aiStatus,
policy: input.policy,
findings,
Expand Down
Loading
Loading