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
33 changes: 29 additions & 4 deletions docs/docs/usage/dbt-pr-review.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,30 @@ jobs:
| `target-base/compiled/…` | Compile the base ref into `target-base/` (below) | Equivalence proofs and column-breakage for modified models. |
| `target/catalog.json` | `dbt docs generate` | Complete warehouse columns for column-lineage breakage. |

Compiling the base (optional, for equivalence and column-breakage):
The action can build these for you with `dbt_prepare` (off by default):

```yaml
- uses: AltimateAI/altimate-code/github/review@main

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: This snippet pins the action to @main, but the rest of the page pins @v0.8.5. Pin to the same release tag so users get a reviewed, reproducible action version.

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 215:

<comment>This snippet pins the action to `@main`, but the rest of the page pins `@v0.8.5`. Pin to the same release tag so users get a reviewed, reproducible action version.</comment>

<file context>
@@ -209,12 +209,30 @@ jobs:
+The action can build these for you with `dbt_prepare` (off by default):
+
+```yaml
+      - uses: AltimateAI/altimate-code/github/review@main
+        with:
+          dbt_prepare: compile        # none | compile | full
</file context>
Suggested change
- uses: AltimateAI/altimate-code/github/review@main
- uses: AltimateAI/altimate-code/github/review@v0.8.5

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not changing yet: @main is intentional until a release that includes dbt_prepare ships, and we'll re-pin it then.

with:
dbt_prepare: compile # none | compile | full
dbt_command: dbt # or "uv run dbt", "poetry run dbt"
dbt_profiles_dir: . # relative to project_dir
```

- **`compile`:** runs `dbt compile` on the head, then compiles the PR's merge-base
into `target-base/` in a temporary worktree. This enables equivalence and
column-breakage proofs. It usually works offline.
- **`full`:** also runs `dbt docs generate` for `catalog.json` (complete column
types). That needs warehouse credentials in the profile.
- A base compile that fails is a warning: the review still runs, and the
affected models are listed as not verified.

To build them yourself, compile the **merge-base**, not the base branch's tip:

```yaml
- name: Compile the base into target-base/
run: |
# The review diffs against the merge-base, not the target branch tip.
git worktree add ../base "$(git merge-base "origin/${{ github.base_ref }}" HEAD)"
git worktree add ../base "$(git merge-base origin/${{ github.base_ref }} HEAD)"
(cd ../base && dbt deps && dbt compile --target-path "$GITHUB_WORKSPACE/target-base")
```

Expand Down Expand Up @@ -246,7 +263,7 @@ ways, or neither (it self-disables, leaving a deterministic-only review):
Always pass keys as repo **secrets**. Warehouse credentials are consumed by the
`dbt compile` step (via your `profiles.yml`), **not** by the review step. A
complete, copy-paste workflow lives at
[`github/review/examples/altimate-ingestion.yml`](https://github.com/AltimateAI/altimate-code/blob/main/github/review/examples/altimate-ingestion.yml).
[`github/review/examples/dbt-pr-review.yml`](https://github.com/AltimateAI/altimate-code/blob/main/github/review/examples/dbt-pr-review.yml).

Re-pushing commits updates the same summary comment in place; fixed findings are
dropped on the next run. `--post` targets **GitHub** PRs (it reads `GITHUB_TOKEN` /
Expand Down Expand Up @@ -500,6 +517,14 @@ The deterministic rules group into four families:
- **dbt unit tests.** A unit test whose `given` inputs are all `rows: []` is
reported: it either never tests the model's logic, or expects rows the model has nothing to
build from.
- **Data tests that can't do their job.** These are checked on tests added or
changed in the PR:
- a test that compares a model with itself (`dbt_utils.equality` or
`equal_rowcount` against its own `ref`, or a `relationships` test pointing
at its own column) passes by construction;
- new tests that inherit `severity: warn` or an `error_if` threshold from
`dbt_project.yml` report failures without failing the build;
- a `dbt_project.yml` change that loosens those thresholds for a whole folder.
- **Incremental and snapshot semantics.** Config is read from the manifest
(project, yml and inline, as dbt merges them) with the head's inline
`{{ config() }}` on top. Formatting-only edits never count as a change.
Expand Down
62 changes: 62 additions & 0 deletions github/review/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ inputs:
description: "Post the verdict to the PR (summary comment + inline review)."
required: false
default: "true"
dbt_prepare:
description: "Build the dbt artifacts the full review uses before reviewing. `none` (default): you build them in earlier steps. `compile`: `dbt compile` on the head, and on the PR's merge-base into `target-base/` (equivalence and lineage proofs). `full`: also `dbt docs generate` for `catalog.json` (complete column types). `compile` usually works offline; `full` needs warehouse credentials in the profile."
required: false
default: "none"
dbt_command:
description: "How to invoke dbt for `dbt_prepare`, e.g. `dbt`, `uv run dbt`, `poetry run dbt`."
required: false
default: "dbt"
dbt_profiles_dir:
description: "`--profiles-dir` for `dbt_prepare`, relative to `project_dir`. Empty = dbt's default lookup."
required: false
signing_key:
description: "Optional HMAC key to sign the verdict envelope (ALTIMATE_REVIEW_SIGNING_KEY)."
required: false
Expand Down Expand Up @@ -167,6 +178,57 @@ runs:
echo "Advisory lane: no model/credentials provided — running deterministic-only."
fi

# Optional: build the head and base artifacts the full-tier review needs.
# Inputs are passed as ENV, never interpolated into the script.
- name: Prepare dbt artifacts
if: inputs.dbt_prepare != 'none'
shell: bash
env:
IN_PREPARE: ${{ inputs.dbt_prepare }}
IN_DBT: ${{ inputs.dbt_command }}
IN_PROFILES: ${{ inputs.dbt_profiles_dir }}
IN_PROJECT_DIR: ${{ inputs.project_dir }}
IN_BASE: ${{ inputs.base }}
EVENT_BASE: ${{ github.event.pull_request.base.ref }}
run: |
set -euo pipefail
case "$IN_PREPARE" in compile|full) ;; *) echo "::error::dbt_prepare must be none, compile or full (got '$IN_PREPARE')"; exit 1 ;; esac
read -ra DBT <<< "$IN_DBT"
PROJECT="$(cd "${IN_PROJECT_DIR:-.}" && pwd)"
REL="$(git -C "$PROJECT" rev-parse --show-prefix)"
PROFILES=()
[[ -n "${IN_PROFILES:-}" ]] && PROFILES=(--profiles-dir "$(cd "$PROJECT" && cd "$IN_PROFILES" && pwd)")
build() { # $1 = project dir, $2 = target path
( cd "$1"
[[ -f packages.yml || -f dependencies.yml ]] && "${DBT[@]}" deps ${PROFILES[@]+"${PROFILES[@]}"}

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: "${DBT[@]}" expands an array that can be empty

read -ra DBT <<< "$IN_DBT" produces an empty array when dbt_command is empty/whitespace. Under set -u on bash < 4.4 (e.g. macOS 3.2) "${DBT[@]}" is an unbound-variable error; on newer bash the command word vanishes and deps/compile fails obscurely. Validate it ([[ ${#DBT[@]} -gt 0 ]] || { echo "::error::dbt_command is empty"; exit 1; }) and use the file's own ${DBT[@]+"${DBT[@]}"} idiom for consistency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not changing: dbt_command defaults to dbt, and an empty value is a configuration error that fails loudly.

"${DBT[@]}" compile --target-path "$2" ${PROFILES[@]+"${PROFILES[@]}"} )
}
echo "Compiling the head…"
build "$PROJECT" "$PROJECT/target"

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.

[MINOR] dbt_prepare compiles the checkout, ignoring the head input; head-side failures abort the job

build "$PROJECT" "$PROJECT/target"
  1. The merge-base is computed against literal HEAD and the head compile uses the checkout, but the review step passes --head "$IN_HEAD" when set. With head: A while B is checked out, A's diff is reviewed using B's compiled SQL and equivalence can be wrongly reported. Prepare against the selected head or reject a mismatch.
  2. Under set -e, a failed head deps/compile/docs generate fails the step so the review never runs (even in mode: comment), while the input description and docs (docs/docs/usage/dbt-pr-review.md:224) say compile "usually works offline". Many adapters need a connection to compile. Either degrade to a warning like the base side, or document that it fails the job and drop the offline claim.
  3. Minor: manifest_path is not honoured (head always goes to $PROJECT/target), and a runtime-generated profiles.yml in the project dir is absent from the base worktree, so the base compile quietly fails.

if [[ "$IN_PREPARE" == "full" ]]; then
echo "Generating catalog.json…"
( cd "$PROJECT" && "${DBT[@]}" docs generate --target-path "$PROJECT/target" ${PROFILES[@]+"${PROFILES[@]}"} ) ||
echo "::warning::dbt docs generate failed; continuing without catalog.json."
fi
# The base side: the PR's merge-base, compiled into target-base/.
BASE="${IN_BASE:-$EVENT_BASE}"
BASE="${BASE#origin/}"
if [[ -z "$BASE" ]]; then echo "::warning::No base branch known; skipping the base compile."; exit 0; fi
git fetch --no-tags origin "+refs/heads/$BASE:refs/remotes/origin/$BASE" >/dev/null 2>&1 || true
MB="$(git merge-base "origin/$BASE" HEAD 2>/dev/null || git merge-base "$BASE" HEAD 2>/dev/null || true)"
if [[ -z "$MB" && "$(git rev-parse --is-shallow-repository)" == "true" ]]; then
git fetch --no-tags --deepen=1000 origin "+refs/heads/$BASE:refs/remotes/origin/$BASE" "$(git rev-parse HEAD)" >/dev/null 2>&1 || true
MB="$(git merge-base "origin/$BASE" HEAD 2>/dev/null || true)"
fi
if [[ -z "$MB" ]]; then echo "::warning::Could not find the merge-base with $BASE; skipping the base compile."; exit 0; fi
WT="$RUNNER_TEMP/altimate-review-base"
trap 'git worktree remove --force "$WT" >/dev/null 2>&1 || true' EXIT
git worktree add --detach "$WT" "$MB" >/dev/null || { echo "::warning::Could not check out the base; skipping the base compile."; exit 0; }
# A stale target-base/ from an earlier run must not stand in for a failed compile.
rm -rf "$PROJECT/target-base"

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 stale target-base/ cleanup runs only after the guards that can skip the base compile

When BASE is empty, the merge-base can't be found, or git worktree add fails, the script exits 0 before this line, so a target-base/ left by an earlier run survives. makeCompiledResolver reads target-base/compiled whenever it exists (compiled.ts:97), so on a cached or self-hosted workspace the head is compared against the previous run's base, and because the old side still resolves, those models are not even listed as unverified. Move the cleanup above the BASE/MB/worktree guards, and make it non-fatal (rm -rf ... || { echo "::warning::..."; exit 0; }) so an rm failure cannot abort the step and skip the review.

echo "Compiling the base ($BASE @ ${MB:0:12})…"
build "$WT/$REL" "$PROJECT/target-base" || echo "::warning::The base compile failed; equivalence will be marked unverified."
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved

- name: Run dbt PR review
shell: bash
# Inputs are passed as ENV (data), never interpolated into the command —
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Example: drop-in dbt PR review for a dbt repo (e.g. altimate-ingestion).
# Example: drop-in dbt PR review for a dbt repo.
# Copy to the target repo as `.github/workflows/dbt-pr-review.yml` and adjust the
# dbt adapter + profiles/secrets to match the warehouse.
#
Expand Down
72 changes: 62 additions & 10 deletions packages/opencode/src/altimate/review/dbt-patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,70 @@ const detectNotIn: Detector = (c) => {
})
}

/** Head-side line numbers of the added lines; undefined when the diff has no hunk headers. */
function addedLineNumbers(diff: string | undefined): Set<number> | 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.

SUGGESTION: This hunk walker duplicates the exported commentableLines

commentableLines (orchestrate.ts:268) already walks @@ hunks with the same regex and the same right counter to compute head-side line numbers; this copy already differs (it does not drop the diff's trailing-newline row) and the two will drift. dbt-patterns cannot import orchestrate without a cycle (the file already keeps a local modelNameFromPath for exactly that reason), so extract the walker into a shared module (e.g. diff-filter.ts) and take the added-line set from it, or pass the numbers in through Ctx.

if (!diff || !/^@@ /m.test(diff)) return undefined
const out = new Set<number>()
let right = 0
let inHunk = false
for (const line of diff.split("\n")) {
const h = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(line)
if (h) {
right = Number(h[1])
inHunk = true
continue
}
if (!inHunk || line.startsWith("\\")) continue
if (line.startsWith("+")) out.add(right++)
else if (line.startsWith(" ") || line === "") right++
else if (!line.startsWith("-")) inHunk = false
}
return out
}

/**
* `row_number() over (…)` windows in the model, read from the whole file so a
* window split across lines is seen whole. A window counts as part of the
* change when one of the PR's added lines falls inside its line span (by text
* only when the diff carries no hunk headers).
*/
function dedupWindows(c: Ctx): Array<{ spec: string; text: string }> {
// Comments blanked with newlines kept, so offsets map to head line numbers.
const sql = c.newSql.replace(/\/\*[\s\S]*?\*\/|--[^\n]*|\{#[\s\S]*?#\}/g, (m) => m.replace(/[^\n]/g, " "))

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: This comment blanking is not quote-aware; blankComments() already does this

A -- inside a string literal blanks the rest of the line: select '--' as sep, row_number() over (partition by id) as rn loses the window entirely (a missed dedup finding), and a /* in a literal can blank a multi-line span. blankComments() in select-list.ts is the existing length-preserving, quote-aware helper (it skips '...', "..." and backticked identifiers), so reuse it here and keep only the {# ... #} alternative.

const lineAt = (offset: number) => sql.slice(0, offset).split("\n").length
const addedLines = addedLineNumbers(c.file.diff)
const added = c.added.map((l) => stripComments(l).trim()).filter((l) => l.length >= 3)

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 length >= 3 filter drops valid added SQL lines such as id or x. A multiline window changed only by adding such a two-character expression is then ignored, so the no-ORDER BY detector misses the change; filter only empty normalized lines.

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/dbt-patterns.ts, line 235:

<comment>The `length >= 3` filter drops valid added SQL lines such as `id` or `x`. A multiline window changed only by adding such a two-character expression is then ignored, so the no-`ORDER BY` detector misses the change; filter only empty normalized lines.</comment>

<file context>
@@ -225,15 +225,42 @@ const detectNotIn: Detector = (c) => {
+ */
+function dedupWindows(c: Ctx): Array<{ spec: string; text: string }> {
+  const sql = stripComments(c.newSql)
+  const added = c.added.map((l) => stripComments(l).trim()).filter((l) => l.length >= 3)
+  const out: Array<{ spec: string; text: string }> = []
+  for (const m of sql.matchAll(/row_number\s*\(\s*\)\s*over\s*\(/gi)) {
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Follow-up: low priority, tracked. When the diff has hunk headers (the PR API always sends them), matching now uses line numbers and no longer depends on the length >= 3 text filter.

const out: Array<{ spec: string; text: string }> = []
for (const m of sql.matchAll(/row_number\s*\(\s*\)\s*over\s*\(/gi)) {
const open = m.index! + m[0].length - 1
let depth = 0
let close = -1
for (let i = open; i < sql.length; i++) {
if (sql[i] === "(") depth++
else if (sql[i] === ")" && --depth === 0) {

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 parenthesis scan counts parentheses inside SQL string literals. A value like status = ')' closes the window early, truncating spec before a real ORDER BY; mask quoted literals or use a quote-aware scanner while finding the matching window close.

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/dbt-patterns.ts, line 243:

<comment>The parenthesis scan counts parentheses inside SQL string literals. A value like `status = ')'` closes the window early, truncating `spec` before a real `ORDER BY`; mask quoted literals or use a quote-aware scanner while finding the matching window close.</comment>

<file context>
@@ -225,15 +225,42 @@ const detectNotIn: Detector = (c) => {
+    let close = -1
+    for (let i = open; i < sql.length; i++) {
+      if (sql[i] === "(") depth++
+      else if (sql[i] === ")" && --depth === 0) {
+        close = i
+        break
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Follow-up: low priority, tracked. We'll run stripLiterals before the paren scan.

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: Paren scan runs on comment-stripped but not literal-stripped SQL

sql comes from stripComments, but string literals are not blanked (the file already has stripLiterals). A ( or ) inside a literal in the OVER spec — e.g. partition by coalesce(customer_id, ')') or regexp_replace(code, '\\(', '') — miscounts depth, truncating the window before its ORDER BY (false "no ORDER BY") or over-extending it into the rest of the file (dropping/dirtying the window). Apply stripLiterals before the depth scan.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Follow-up: low priority, tracked. Same as r4626962367.

close = i
break
}
}
if (close < 0) continue
const text = sql.slice(m.index!, close + 1)
const touched = addedLines
? [...addedLines].some((n) => n >= lineAt(m.index!) && n <= lineAt(close))
: added.some((l) => text.includes(l) || l.includes(text.replace(/\s+/g, " ")))
if (!touched) continue
out.push({ spec: sql.slice(open + 1, close).replace(/\s+/g, " ").trim(), text: text.replace(/\s+/g, " ") })
}
return out
}

// 9. Dedup via ROW_NUMBER()/QUALIFY whose ORDER BY has no unique tiebreaker.
const detectDedupTie: Detector = (c) => {
const hit = c.added.find((l) => /row_number\s*\(\s*\)\s*over\s*\(/i.test(stripComments(l)))
if (!hit) return null
// Only flag dedup with NO ORDER BY — that is unambiguously non-deterministic.
// A present ORDER BY (even single-column) is the developer's deterministic
// choice and the dbt-recommended pattern; warning "it might not be unique"
// on every dedup is noise (false positives on the common, correct case).
if (/order\s+by/i.test(stripComments(hit))) return null
const hit = dedupWindows(c).find((w) => !/order\s+by/i.test(w.spec))
if (!hit) return null
return makeFinding({
severity: "warning",
category: "dedup",
Expand All @@ -244,7 +299,7 @@ const detectDedupTie: Detector = (c) => {
file: c.file.path,
model: c.model,
confidence: "high",
evidence: { tool: "dbt-patterns", result: { rule: "dedup_no_tiebreaker", line: hit.trim() } },
evidence: { tool: "dbt-patterns", result: { rule: "dedup_no_tiebreaker", line: hit.text } },
ruleKey: "dedup:no-tiebreaker",
})
}
Expand All @@ -257,11 +312,8 @@ const detectDedupTie: Detector = (c) => {
const LOAD_TS_RE =
/^(_?(ingest(ion|ed)?|load(ed)?|etl|extract(ed)?|batch)(_at|_ts|_time|_timestamp|_date|_dt)|_?(ingestion|load|etl|batch)_timestamp|_loaded_at)$/i
const detectDedupLoadTimestamp: Detector = (c) => {
for (const line of c.added) {
const s = stripComments(line)
const m = /row_number\s*\(\s*\)\s*over\s*\(([^)]*)\)/i.exec(s)
if (!m) continue
const order = /order\s+by\s+(.+)$/i.exec(m[1])?.[1]
for (const w of dedupWindows(c)) {
const order = /order\s+by\s+(.+)$/i.exec(w.spec)?.[1]
if (!order) continue
const keys = order.split(",").map((k) => k.trim().replace(/\s+(asc|desc)\b.*$/i, "").replace(/\s+nulls\s+(first|last)$/i, ""))
if (keys.length !== 1) continue
Expand All @@ -277,7 +329,7 @@ const detectDedupLoadTimestamp: Detector = (c) => {
file: c.file.path,
model: c.model,
confidence: "high",
evidence: { tool: "dbt-patterns", result: { rule: "dedup_load_timestamp_only", line: line.trim() } },
evidence: { tool: "dbt-patterns", result: { rule: "dedup_load_timestamp_only", line: w.text } },
ruleKey: "dedup:load-timestamp-only",
})
}
Expand Down
60 changes: 48 additions & 12 deletions packages/opencode/src/altimate/review/orchestrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { detectModelPatterns, detectSchemaYmlPatterns, splitDiff } from "./dbt-p
import { listMirrorsModel, type ConsumerColumnList } from "./consumer-lists"
import { detectUnitTestFixtures } from "./unit-tests"
import { selectListNames } from "./select-list"
import { detectTestAdequacy, detectThresholdLoosening } from "./test-adequacy"
import { incrementalFindings, snapshotFindings, snapshotYmlFindings } from "./incremental-semantics"
import { type AiLaneStatus, type AiReviewInput, type AiReviewResult, safeReason } from "./ai-review"

Expand Down Expand Up @@ -661,19 +662,44 @@ async function columnDelta(
dialect: string,
): Promise<{ added: string[]; removed: string[]; current: Set<string> }> {
const none = { added: [], removed: [], current: new Set<string>() }
const { file, engineNewSql, engineOldSql } = ctx
if (!engineNewSql || file.status === "deleted") return none
const current = await outputColumns(runner, engineNewSql, dialect)
if (!current.size) return none
if (!hasOldSide(file)) return { added: [...current], removed: [], current }
if (!engineOldSql) return none
const before = await outputColumns(runner, engineOldSql, dialect)
if (!before.size) return none
return {
added: [...current].filter((c) => !before.has(c)),
removed: [...before].filter((c) => !current.has(c)),
current,
const { file, engineNewSql, engineOldSql, newSql, oldSql } = ctx
if (file.status === "deleted") return none
const withOld = hasOldSide(file)
const delta = (current: Set<string>, before?: Set<string>) =>
!current.size || (withOld && !before?.size)
? none
: {
added: [...current].filter((c) => !before?.has(c)),
removed: before ? [...before].filter((c) => !current.has(c)) : [],
current,
}
// Compiled SQL on both sides: column lineage plus the final SELECT's names.
if (engineNewSql && (!withOld || engineOldSql)) {

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: When only the base compile is missing, columnDelta now falls through to the raw-SQL path and can emit removed-column warnings. Before this change that case returned no deltas. Confirm this is intended for models marked unverified, or keep returning no deltas when the head is compiled and the base is not.

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 664:

<comment>When only the base compile is missing, `columnDelta` now falls through to the raw-SQL path and can emit removed-column warnings. Before this change that case returned no deltas. Confirm this is intended for models marked unverified, or keep returning no deltas when the head is compiled and the base is not.</comment>

<file context>
@@ -648,19 +649,41 @@ async function columnDelta(
+          current,
+        }
+  // Compiled SQL on both sides: column lineage plus the final SELECT's names.
+  if (engineNewSql && (!withOld || engineOldSql)) {
+    const current = await outputColumns(runner, engineNewSql, dialect)
+    const before = withOld ? await outputColumns(runner, engineOldSql!, dialect) : undefined
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in e77d121. A {% if %} or {% for %} tag in the raw select list now makes the list unknown, so conditional columns are no longer reported as added or removed. We kept the raw fallback when only one side is compiled: both sides are then read from raw SQL, so the comparison stays like for like.

const current = await outputColumns(runner, engineNewSql, dialect)
const before = withOld ? await outputColumns(runner, engineOldSql!, dialect) : undefined
return delta(current, before)
}
// No compiled SQL (CI that only runs `dbt parse`): the raw model's own SELECT
// list, when it names its columns. Columns produced inside a macro stay unknown.
if (!newSql) return none
const current = rawSelectList(newSql)

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.

[MINOR] Parse-only select list treated as complete when items are dropped

const current = rawSelectList(newSql)

selectListNames is deliberately conservative (items it cannot name contribute nothing). Previously that only supplemented lineage; here it is the whole answer. select id, amount::numeric(18,2) yields ["id"], so going from amount to a cast is seen as a removed column, and with exposures/contracts this produces a high-confidence "removed columns" warning. The branch also now runs when the head is compiled but the base is not.

Fix: return unknown (empty set) when any non-empty select item produced no name. Related: stripping {% if %} / {% for %} tags keeps their bodies, so {% if var('x', false) %}, region {% endif %} is counted as an unconditional added column and can trigger the on_schema_change='fail' warning; treat projection-affecting control blocks as unknown.

const before = withOld && oldSql ? rawSelectList(oldSql) : undefined
return delta(current, before)
}

/** Output names of a raw (Jinja) model's final SELECT; empty when a Jinja
* expression or tag is in the list. */
function rawSelectList(sql: string): Set<string> {
const plain = sql
.replace(/\{#[\s\S]*?#\}/g, " ")
// A tag in the list (`{% if %}` / `{% for %}`) makes its columns conditional:
// it becomes its own `__jinja__` item, which marks the list unknown.
.replace(/\{%[\s\S]*?%\}/g, " , __jinja__ , ")
.replace(/\{\{[\s\S]*?\}\}/g, " __jinja__ ")
const names = selectListNames(plain)
if (!names || names.includes("__jinja__")) return new Set()
// A Jinja expression inside an item (e.g. `{{ col }} as x`) leaves its alias intact.
return new Set(names)
}

const ticks = (xs: string[]) => xs.map((x) => `\`${x}\``).join(", ")
Expand Down Expand Up @@ -1725,6 +1751,9 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
// Fetches run in parallel across schema files (a schema-heavy PR could touch
// dozens of yml files; serial `git show` per file adds up).
const schemaFiles = reviewable.filter((f) => f.kind === "schema_yml")
// The head's dbt_project.yml (test thresholds inherited by new tests), read once.
const projectYmlPath = `${input.layout?.root ? `${input.layout.root.replace(/\/$/, "")}/` : ""}dbt_project.yml`
const projectYml = schemaFiles.length ? await getContent?.(projectYmlPath, "new").catch(() => undefined) : undefined
if (schemaFiles.length) {
const schemaFindingSets = await Promise.all(
schemaFiles.map(async (file) => {
Expand All @@ -1738,6 +1767,7 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
...detectSchemaYmlPatterns(file, input.rubric, { oldContent, newContent }),
...detectUnitTestFixtures(file, oldContent, newContent),
...snapshotYmlFindings(file, oldContent, newContent),
...detectTestAdequacy({ file, oldContent, newContent, projectYml, layout: input.layout }),
]
} catch (err) {
// One malformed schema file degrades that file only.
Expand All @@ -1748,6 +1778,12 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
)
for (const findings of schemaFindingSets) all.push(findings)
}
// A dbt_project.yml change that loosens test thresholds for a whole path.
for (const file of reviewable.filter((f) => f.kind === "project_config" && /(^|\/)dbt_project\.ya?ml$/.test(f.path))) {
if (file.status !== "modified") continue
const [oldContent, newContent] = await Promise.all([getContent?.(file.path, "old"), getContent?.(file.path, "new")])

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: Re-fetch of dbt_project.yml is redundant and unguarded

When the PR modifies dbt_project.yml, this re-reads the head content that projectYml already holds (fetched with .catch at line 1671), and unlike that read it has no .catch. A rejecting getContent (valid custom/unit-test resolver) aborts the entire runReview instead of degrading this lane. Reuse projectYml when file.path === projectYmlPath, and guard the old-side read.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Follow-up: low priority, tracked. We'll reuse projectYml and add a .catch.

all.push(detectThresholdLoosening(file, oldContent, newContent))
}

// Architectural dedup: the regex `dbt-patterns`/`rule-catalog` layer is a
// diff-aware, Jinja-aware, offline fallback. For the STRUCTURAL SQL checks
Expand Down
Loading
Loading