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
15 changes: 11 additions & 4 deletions docs/docs/usage/dbt-pr-review.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,10 @@ mode: comment # comment | gate
severityThreshold: suggestion
manifestPath: target/manifest.json
dialect: snowflake
portability: false # cross-engine advice; default off once the engine is known
# (set true for packages that support several adapters)
portability: false # cross-engine advice; default off once the engine is known.
# A project whose macros have variants for several adapters
# (`postgres__x`, `snowflake__x`) still gets advice on constructs
# one of those adapters lacks. Set true to see all of it.
reviewers: [] # empty = risk-tier defaults; or pin lanes
dataDiff: # OFF by default — see "Data-diff in CI" below
enabled: false
Expand Down Expand Up @@ -357,8 +359,10 @@ consumerColumnLists:
5,000 characters. Critical findings come first; anything that does not fit is
counted, and the full list is always in the verdict envelope
(`altimate review --json`). Local CLI output is never truncated.
- **Repeated rules are grouped:** the same rule on three or more models is one
bullet listing the models.
- **Repeated rules are grouped:** the same rule several times in one file (one
finding per column of a grain test, say) is one finding listing each
instance; critical findings stay separate. The same rule on two or more
models is one summary bullet listing the models.
- **Inline comments only on lines of the PR's diff.** Findings computed on
compiled SQL, or on unchanged lines, stay in the summary.
- **No re-posting on reruns:** each inline comment carries the finding's
Expand Down Expand Up @@ -579,6 +583,9 @@ across 12 dialects all plan; anything still unrecognized stays *undecidable*, ne
3. **AI reviewer lane (layer 3).** When a model is configured, the LLM reviewer
runs **after** the deterministic lanes, grounded in their findings, and adds
advisory contextual comments (intent, naming, business logic, test coverage).
It sees every changed dbt file, not only models: macros, singular data tests,

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 AI lane does not see every changed dbt file: deleted files are filtered out, and analysis/other classified dbt files are never added to its input. Say “eligible non-deleted files in these supported roles” or expand the implementation to cover all changed dbt kinds.

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

<comment>The AI lane does not see every changed dbt file: deleted files are filtered out, and `analysis`/other classified dbt files are never added to its input. Say “eligible non-deleted files in these supported roles” or expand the implementation to cover all changed dbt kinds.</comment>

<file context>
@@ -579,6 +583,9 @@ across 12 dialects all plan; anything still unrecognized stays *undecidable*, ne
 3. **AI reviewer lane (layer 3).** When a model is configured, the LLM reviewer
    runs **after** the deterministic lanes, grounded in their findings, and adds
    advisory contextual comments (intent, naming, business logic, test coverage).
+   It sees every changed dbt file, not only models: macros, singular data tests,
+   snapshots, schema/source yml, seeds (diff only) and project config, each
+   labeled with its role, with models first under the 20-file cap.
</file context>
Suggested change
It sees every changed dbt file, not only models: macros, singular data tests,
It sees eligible non-deleted dbt files, not only models: macros, singular data tests,

snapshots, schema/source yml, seeds (diff only) and project config, each
labeled with its role, with models first under the 20-file cap.
Findings are clamped to ≤ warning and never block. Skipped (clean degrade) if
no model is available.
4. **Rubric → verdict.** Findings map to a verdict by a versioned rubric (data,
Expand Down
48 changes: 42 additions & 6 deletions packages/opencode/src/altimate/review/ai-review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,45 @@ const MAX_FILES = 20
export interface AiReviewFile {
path: string
status: string
/** Model name for a model; the file's base name for any other dbt file. */
model: string
/** dbt role of the file (`model_sql`, `macro`, `schema_yml`, `test`, …); omitted means a model. */
kind?: string
/** Unified diff (added/removed lines) for this file. */
diff?: string
/** dbt-compiled (preferred) or raw SQL for context. */
/** Current content: dbt-compiled (preferred) or raw SQL for a model, the file itself otherwise. */
sql?: string
}

/** Order under the file cap: models first, then the files whose defects reach the most models. */
const KIND_ORDER = ["model_sql", "python_model", "macro", "test", "snapshot", "schema_yml", "source_yml", "seed", "project_config"]
const KIND_LABEL: Record<string, string> = {
macro: "macro (Jinja; changes every model that calls it)",
test: "singular data test",
snapshot: "snapshot",
schema_yml: "schema yml (tests, contracts, descriptions)",
source_yml: "sources yml",

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 new source_yml label is unreachable because classifyDbtFile never produces that kind; source declaration YAML is labeled as schema_yml instead. Add source-YAML classification before the generic schema branch, or remove this unused role so the AI receives an accurate file label.

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/ai-review.ts, line 39:

<comment>The new `source_yml` label is unreachable because `classifyDbtFile` never produces that kind; source declaration YAML is labeled as `schema_yml` instead. Add source-YAML classification before the generic schema branch, or remove this unused role so the AI receives an accurate file label.</comment>

<file context>
@@ -19,13 +19,45 @@ const MAX_FILES = 20
+  test: "singular data test",
+  snapshot: "snapshot",
+  schema_yml: "schema yml (tests, contracts, descriptions)",
+  source_yml: "sources yml",
+  seed: "seed CSV",
+  project_config: "project config",
</file context>

seed: "seed CSV",
project_config: "project config",
python_model: "Python model",
}
const FENCE: Record<string, string> = { schema_yml: "yaml", source_yml: "yaml", project_config: "yaml", seed: "csv", python_model: "python" }

function kindRank(kind: string | undefined): number {
const i = KIND_ORDER.indexOf(kind ?? "model_sql")
return i < 0 ? KIND_ORDER.length : i
}

/** Files the lane sends, in priority order, capped at MAX_FILES. */
export function selectAiFiles(files: AiReviewFile[]): AiReviewFile[] {
return files
.filter((f) => f.status !== "deleted" && (f.diff || f.sql))
.map((f, i) => ({ f, i }))
.sort((a, b) => kindRank(a.f.kind) - kindRank(b.f.kind) || a.i - b.i)
.map(({ f }) => f)
.slice(0, MAX_FILES)
}

export interface AiReviewInput {
files: AiReviewFile[]
/** Deterministic engine findings — grounding the AI must NOT duplicate. */
Expand Down Expand Up @@ -81,7 +113,7 @@ function truncate(s: string | undefined, n: number): string {
}

/** Assemble the user message (mechanical formatting — not IP). */
function buildUserMessage(input: AiReviewInput): string {
export function buildUserMessage(input: AiReviewInput): string {
const parts: string[] = []
if (input.prTitle) parts.push(`PR title: ${truncate(input.prTitle, 200)}`)
if (input.prBody) parts.push(`PR description:\n${truncate(input.prBody, 1500)}`)
Expand All @@ -97,9 +129,13 @@ function buildUserMessage(input: AiReviewInput): string {

parts.push("\n## CHANGED FILES (review for what the engine missed):")
for (const f of input.files.slice(0, MAX_FILES)) {
parts.push(`\n### ${f.path} (${f.status})`)
const label = f.kind && KIND_LABEL[f.kind]
parts.push(`\n### ${f.path} (${f.status}${label ? `, ${label}` : ""})`)
if (f.diff) parts.push("Diff:\n```diff\n" + truncate(f.diff, MAX_DIFF_CHARS) + "\n```")
if (f.sql) parts.push("Current SQL:\n```sql\n" + truncate(f.sql, MAX_DIFF_CHARS) + "\n```")
if (f.sql) {
const isModel = !f.kind || f.kind === "model_sql"
parts.push(`${isModel ? "Current SQL" : "Current file"}:\n\`\`\`${FENCE[f.kind ?? ""] ?? "sql"}\n` + truncate(f.sql, MAX_DIFF_CHARS) + "\n```")
}
}
parts.push("\nReturn ONLY the JSON array per the output contract.")
return parts.join("\n")
Expand All @@ -118,8 +154,8 @@ export async function runAiReview(input: AiReviewInput): Promise<AiReviewResult>
findings,
status: { ...status, durationMs: Date.now() - startedAt },
})
const files = input.files.filter((f) => f.status !== "deleted" && (f.diff || f.sql))
if (!files.length) return done([], { state: "skipped", reason: "no changed model SQL to review" })
const files = selectAiFiles(input.files)
if (!files.length) return done([], { state: "skipped", reason: "no changed dbt files to review" })

const message = buildUserMessage({ ...input, files })
const controller = new AbortController()
Expand Down
161 changes: 136 additions & 25 deletions packages/opencode/src/altimate/review/dbt-patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,10 @@ const detectClock: Detector = (c) => {
})
}

// 4. SELECT * added (warehouse cost + fragility on columnar warehouses).
const detectSelectStar: Detector = (c) => {
const hit = c.added.find((l) => /^\s*select\s+\*/i.test(stripComments(l)) && !/select\s+\*\s+from\s+\{\{/i.test(l))
if (!hit) return null
return makeFinding({
severity: "suggestion",
category: "warehouse_cost",
title: `${c.model}: SELECT * scans all columns`,
body:
"`SELECT *` reads every column on a columnar warehouse (Snowflake/BigQuery) and makes the model fragile to " +
"upstream column adds. Select only the columns you need.",
file: c.file.path,
model: c.model,
confidence: "high",
evidence: { tool: "dbt-patterns", result: { rule: "select_star", line: hit.trim() } },
ruleKey: "warehouse_cost:select-star",
})
}
// 4. SELECT * — covered by the catalog's "SELECT * in final projection" rule, which
// fires only when the model's final SELECT reads straight from another relation.
// A `select *` from a CTE or an import CTE is the dbt style-guide idiom, and
// columnar engines prune the unused columns, so it is not flagged per line.

// 5. LEFT/RIGHT JOIN silently collapsed to INNER by a WHERE/AND on the outer table.

Expand Down Expand Up @@ -188,7 +174,8 @@ const detectFanout: Detector = (c) => {
const newJoins = c.added.filter((l) => /\bjoin\b/i.test(stripComments(l)) && !/\bcross\s+join\b/i.test(l))
if (newJoins.length < 1) return null
return makeFinding({
severity: "warning",
// A "may": the join's cardinality is unknown here (1 partial match in 254 on replay).
severity: "suggestion",
category: "fanout",
title: `${c.model}: new join before an aggregate may fan out and inflate metrics`,
body:
Expand Down Expand Up @@ -308,11 +295,37 @@ const detectDedupLoadTimestamp: Detector = (c) => {
return null
}

const NOT_COLUMN = new Set([
"year", "month", "day", "week", "quarter", "hour", "minute", "second", "epoch", "dow", "doy", "from", "as", "date", "timestamp",
"timestamp_ntz", "timestamp_tz", "datetime", "time", "boolean", "bool", "int", "integer", "bigint", "smallint", "numeric", "decimal",
"float", "double", "real", "varchar", "char", "string", "text", "current_date", "current_timestamp", "now", "null", "true", "false",
"interval", "with", "zone", "at", "precision",
])

/** True when a partition-defeating function in the line wraps a bare column (not a literal, var or clock). */
function wrapsColumn(line: string): boolean {
const s = line.replace(/'(?:[^']|'')*'/g, "''").replace(/\{\{[\s\S]*?\}\}/g, " ")
for (const m of s.matchAll(/\b(extract|date_trunc|date|year|month|cast|trunc)\s*\(/gi)) {
let depth = 1
let i = (m.index ?? 0) + m[0].length
const start = i
while (i < s.length && depth > 0) {
if (s[i] === "(") depth++
else if (s[i] === ")") depth--
i++
}
const args = s.slice(start, i - 1)
const idents = [...args.matchAll(/(?<![\w.'])([a-z_][\w]*(?:\.[a-z_][\w]*)*)(?!\s*\()/gi)].map((x) => x[1].toLowerCase())

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: Qualified columns written directly after a Jinja relation are no longer detected, so common date({{ ref('events') }}.event_date) filters evade the partition-pruning rule. Allow the identifier matcher to start after a qualification dot.

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

<comment>Qualified columns written directly after a Jinja relation are no longer detected, so common `date({{ ref('events') }}.event_date)` filters evade the partition-pruning rule. Allow the identifier matcher to start after a qualification dot.</comment>

<file context>
@@ -308,11 +295,37 @@ const detectDedupLoadTimestamp: Detector = (c) => {
+      i++
+    }
+    const args = s.slice(start, i - 1)
+    const idents = [...args.matchAll(/(?<![\w.'])([a-z_][\w]*(?:\.[a-z_][\w]*)*)(?!\s*\()/gi)].map((x) => x[1].toLowerCase())
+    if (idents.some((x) => !NOT_COLUMN.has(x.split(".").pop()!))) return true
+  }
</file context>
Suggested change
const idents = [...args.matchAll(/(?<![\w.'])([a-z_][\w]*(?:\.[a-z_][\w]*)*)(?!\s*\()/gi)].map((x) => x[1].toLowerCase())
const idents = [...args.matchAll(/(?<![\w'])([a-z_][\w]*(?:\.[a-z_][\w]*)*)(?!\s*\()/gi)].map((x) => x[1].toLowerCase())

if (idents.some((x) => !NOT_COLUMN.has(x.split(".").pop()!))) return true
}
return false
}

// 11. Function/cast wrapped around a column in an added WHERE — defeats partition pruning.
const detectPartitionFunction: Detector = (c) => {
const hit = c.added.find((l) => {
const s = stripComments(l)
return /^\s*(where|and)\b/i.test(s) && /\b(extract|date_trunc|date|year|month|cast|trunc)\s*\(/i.test(s)
return /^\s*(where|and)\b/i.test(s) && wrapsColumn(s)
})
if (!hit) return null
return makeFinding({
Expand Down Expand Up @@ -677,7 +690,26 @@ const detectSelfJoin: Detector = (c) => {

// 39. Window function without PARTITION BY (whole-table window — often unintended).
const detectWindowNoPartition: Detector = (c) => {
const hit = c.added.find((l) => /\bover\s*\(/i.test(stripComments(l)) && !/partition\s+by/i.test(l))
// Read each OVER clause whole: `over (` on one line and `partition by` on the next is partitioned.
const changed = c.added.map((l) => stripComments(l).trim()).filter((l) => /\bover\s*\(/i.test(l))
const whole = stripComments(c.newSql ?? "")
const sql = /\bover\s*\(/i.test(whole) ? whole : changed.join("\n")
const unpartitioned = [...sql.matchAll(/\bover\s*\(/gi)].some((m) => {
const open = m.index! + m[0].length - 1
let depth = 0
let end = sql.length
for (let i = open; i < sql.length; i++) {
if (sql[i] === "(") depth++
else if (sql[i] === ")" && --depth === 0) {
end = i
break
}
}
const lineStart = sql.lastIndexOf("\n", m.index!) + 1
const head = sql.slice(lineStart, open + 1).trim()
return changed.some((l) => l.includes(head)) && !/partition\s+by/i.test(sql.slice(open, end))

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: This prefix-based association can report an unchanged whole-table window when the PR adds another window with the same row_number() over ( prefix. Associate the finding with the actual added window span rather than matching duplicate prefixes.

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

<comment>This prefix-based association can report an unchanged whole-table window when the PR adds another window with the same `row_number() over (` prefix. Associate the finding with the actual added window span rather than matching duplicate prefixes.</comment>

<file context>
@@ -677,7 +690,26 @@ const detectSelfJoin: Detector = (c) => {
+    }
+    const lineStart = sql.lastIndexOf("\n", m.index!) + 1
+    const head = sql.slice(lineStart, open + 1).trim()
+    return changed.some((l) => l.includes(head)) && !/partition\s+by/i.test(sql.slice(open, end))
+  })
+  const hit = unpartitioned ? changed[0] : undefined
</file context>

})
const hit = unpartitioned ? changed[0] : undefined
if (!hit) return null
return pattern(
c,
Expand Down Expand Up @@ -789,7 +821,6 @@ const detectMultiCountDistinct: Detector = (c) => {

const MODEL_DETECTORS: Detector[] = [
detectClock,
detectSelectStar,
detectCrossJoin,
detectFanout,
detectNotIn,
Expand Down Expand Up @@ -824,6 +855,64 @@ const MODEL_DETECTORS: Detector[] = [
detectMultiCountDistinct,
]

/**
* A changed macro: the catalog rules marked `macros`, and `var('x')` read with no
* default when `x` isn't set in dbt_project.yml (in a package, every project that
* doesn't set it fails to compile).
*/
export const ADAPTERS = ["bigquery", "snowflake", "databricks", "spark", "redshift", "postgres", "trino", "athena", "duckdb", "exasol", "synapse", "sqlserver", "fabric", "oracle", "clickhouse", "presto"]

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: Add mysql to ADAPTERS; otherwise MySQL-specific macros produce portability noise and project adapter discovery misses mysql__... variants.

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

<comment>Add `mysql` to `ADAPTERS`; otherwise MySQL-specific macros produce portability noise and project adapter discovery misses `mysql__...` variants.</comment>

<file context>
@@ -824,6 +855,64 @@ const MODEL_DETECTORS: Detector[] = [
+ * default when `x` isn't set in dbt_project.yml (in a package, every project that
+ * doesn't set it fails to compile).
+ */
+export const ADAPTERS = ["bigquery", "snowflake", "databricks", "spark", "redshift", "postgres", "trino", "athena", "duckdb", "exasol", "synapse", "sqlserver", "fabric", "oracle", "clickhouse", "presto"]
+
+/** The adapter a macro file is specific to, from its directory or its macros' `<adapter>__` prefix. */
</file context>
Suggested change
export const ADAPTERS = ["bigquery", "snowflake", "databricks", "spark", "redshift", "postgres", "trino", "athena", "duckdb", "exasol", "synapse", "sqlserver", "fabric", "oracle", "clickhouse", "presto"]
export const ADAPTERS = ["bigquery", "snowflake", "databricks", "spark", "redshift", "postgres", "trino", "athena", "mysql", "duckdb", "exasol", "synapse", "sqlserver", "fabric", "oracle", "clickhouse", "presto"]


/** The adapter a macro file is specific to, from its directory or its macros' `<adapter>__` prefix. */
function macroAdapter(p: string, sql: string): string | undefined {
const seg = p.toLowerCase().split("/").find((s) => ADAPTERS.includes(s))
if (seg) return seg
const names = [...sql.matchAll(/\{%-?\s*macro\s+(\w+?)__\w+/g)].map((m) => m[1].toLowerCase())
return names.length && names.every((n) => n === names[0]) && ADAPTERS.includes(names[0]) ? names[0] : undefined
}

export function detectMacroPatterns(
file: ChangedFile,
newSql: string,
rubric: Rubric,
projectVars: Set<string>,
catalogCtx?: CatalogContext,
): Finding[] {
if (fileKind(file) !== "macro" || file.status === "deleted") return []
const { added, removed } = splitDiff(file.diff)
// An adapter's own variant (`macros/…/bigquery/x.sql`, `{% macro bigquery__x %}`) runs only on that
// adapter, so portability advice applies only where that adapter lacks the construct.
const adapter = macroAdapter(file.path, newSql)
const out = evaluateCatalog(file, newSql, added, removed, rubric, catalogCtx).filter((f) => {
if (!adapter || f.evidence?.tool !== "rule-catalog" || (f.evidence.result as { portability?: boolean })?.portability !== true) return true
const worksOn = (f.evidence.result as { worksOn?: string[] }).worksOn
return !!worksOn && !worksOn.includes(adapter)
})
const model = modelNameFromPath(file.path)
const seen = new Set<string>()
for (const line of added) {
for (const m of stripComments(line).matchAll(/\bvar\(\s*['"]([\w.]+)['"]\s*\)/g)) {
const name = m[1]
const esc = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
if (seen.has(name) || projectVars.has(name) || new RegExp(`var\\(\\s*['"]${esc}['"]\\s*,`).test(newSql)) continue

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: A default on another var('window', ...) call suppresses the warning for var('window'), but dbt does not make that per-call default global. Check each invocation independently, or only suppress when the project defines the variable.

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

<comment>A default on another `var('window', ...)` call suppresses the warning for `var('window')`, but dbt does not make that per-call default global. Check each invocation independently, or only suppress when the project defines the variable.</comment>

<file context>
@@ -824,6 +855,64 @@ const MODEL_DETECTORS: Detector[] = [
+    for (const m of stripComments(line).matchAll(/\bvar\(\s*['"]([\w.]+)['"]\s*\)/g)) {
+      const name = m[1]
+      const esc = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
+      if (seen.has(name) || projectVars.has(name) || new RegExp(`var\\(\\s*['"]${esc}['"]\\s*,`).test(newSql)) continue
+      seen.add(name)
+      out.push(
</file context>

seen.add(name)
out.push(
makeFinding({
severity: "suggestion",
category: "materialization",
title: `${model}: var('${name}') read with no default in a macro`,
body: `\`var('${name}')\` has no default and isn't set in dbt_project.yml, so any project that calls this macro without setting it fails to compile. Pass a default: \`var('${name}', <default>)\`.`,
file: file.path,
model,
confidence: "high",
evidence: { tool: "dbt-patterns", result: { rule: "macro-var-no-default", var: name } },
ruleKey: `materialization:macro-var-no-default`,

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: Distinct missing variables in one macro are deduplicated into a single finding because their ruleKey omits the variable name. Include name in the key so every compile-breaking variable is reported.

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

<comment>Distinct missing variables in one macro are deduplicated into a single finding because their `ruleKey` omits the variable name. Include `name` in the key so every compile-breaking variable is reported.</comment>

<file context>
@@ -824,6 +855,64 @@ const MODEL_DETECTORS: Detector[] = [
+          model,
+          confidence: "high",
+          evidence: { tool: "dbt-patterns", result: { rule: "macro-var-no-default", var: name } },
+          ruleKey: `materialization:macro-var-no-default`,
+        }),
+      )
</file context>
Suggested change
ruleKey: `materialization:macro-var-no-default`,
ruleKey: `materialization:macro-var-no-default:${name}`,

}),
)
}
}
return out.filter((f) => !exclusionReason(f, rubric))
}

/** Run the dbt anti-pattern detectors over a changed MODEL file. */
export function detectModelPatterns(
file: ChangedFile,
Expand Down Expand Up @@ -1430,9 +1519,30 @@ export function detectSchemaYmlPatterns(
if (canCommit) {
const oldSet = extractTestOccurrences(oldDoc ?? {})
const newSet = extractTestOccurrences(isDeletedFile ? {} : newDoc)
// A test that moved with a column rename (old column gone, a new column on
// the same model carries the same test) is not a removal.
const cols = (set: Set<string>) => {
const m = new Map<string, Set<string>>()
for (const k of set) {
const [mod, col] = k.split("\x00")
if (!m.has(mod)) m.set(mod, new Set())
m.get(mod)!.add(col)
}
return m
}
const oldCols = cols(oldSet)
const newCols = cols(newSet)
const movedTo = (model: string, column: string, testField: string) =>

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: movedTo assumes any matching test on a newly tested column represents a column rename, so it hides genuine removals when a guardrail is intentionally reassigned. Only suppress the finding when a rename is established; otherwise retain the old-column removal.

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

<comment>`movedTo` assumes any matching test on a newly tested column represents a column rename, so it hides genuine removals when a guardrail is intentionally reassigned. Only suppress the finding when a rename is established; otherwise retain the old-column removal.</comment>

<file context>
@@ -1430,9 +1519,30 @@ export function detectSchemaYmlPatterns(
+      }
+      const oldCols = cols(oldSet)
+      const newCols = cols(newSet)
+      const movedTo = (model: string, column: string, testField: string) =>
+        !!column &&
+        !newCols.get(model)?.has(column) &&
</file context>

!!column &&
!newCols.get(model)?.has(column) &&
[...newSet].some((k) => {
const [mod, col, tf] = k.split("\x00")
return mod === model && tf === testField && !!col && !oldCols.get(model)?.has(col)
})
for (const key of oldSet) {
if (newSet.has(key)) continue
const [model, column, testField] = key.split("\x00")
if (movedTo(model, column, testField)) continue
// `testField` may carry a `\x01<to>\x02<field>` discriminator for
// `relationships` tests so multiple relationships on the same column
// don't collapse to one removal (MINOR #4). Strip it for display;
Expand Down Expand Up @@ -1626,14 +1736,15 @@ export function detectSchemaYmlPatterns(
: `Add \`not_null\` to \`${g.column}\`'s \`data_tests:\` on \`${g.model}\` (contract is not enforced, so a \`constraints:\` entry would be inert — use a data_test).`
findings.push(
makeFinding({
severity: clampSeverity("test_coverage", "warning", "high"),
severity: "suggestion",
category: "test_coverage",
title: `${filename}: grain column \`${g.column}\` in unique_combination_of_columns lacks \`not_null\` on \`${g.model}\``,
body:
`The \`unique_combination_of_columns\` test on \`${g.model}\` names \`${g.column}\` as a grain key, but no \`not_null\` ` +
`coverage is declared for it (either as a \`constraints:\` entry on a contracted model, or as a \`data_tests: [not_null]\` ` +
`on a view). A NULL grain-key value silently passes the uniqueness test, so a fan-out or duplicate bug can ship without ` +
`any test catching it. ${recommendation}`,
`on a view). The uniqueness test still catches duplicates (rows with NULL keys group together), but nothing stops NULL ` +
`grain keys themselves, and as an incremental \`unique_key\` a NULL never matches on merge, so those rows are re-inserted every run. ` +
`${recommendation}`,
file: file.path,
model: g.model,
column: g.column,
Expand Down
43 changes: 43 additions & 0 deletions packages/opencode/src/altimate/review/finding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,46 @@ export function dedupe(findings: Finding[]): Finding[] {
}
return [...byId.values()]
}

/** A finding's title without its leading `<model or file>: `, with backticked
* names blanked, so the same rule on different models/columns shares a key. */
export function ruleTitle(f: Pick<Finding, "title">): string {
return f.title
.replace(/^[\w.\-/]+: /, "")

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: ruleTitle does not normalize model or file labels containing spaces or non-ASCII characters, so equivalent findings fail to merge or group. Strip the exact model/file prefix instead of restricting the prefix to \w, dots, slashes, and hyphens.

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/finding.ts, line 211:

<comment>`ruleTitle` does not normalize model or file labels containing spaces or non-ASCII characters, so equivalent findings fail to merge or group. Strip the exact model/file prefix instead of restricting the prefix to `\w`, dots, slashes, and hyphens.</comment>

<file context>
@@ -203,3 +203,46 @@ export function dedupe(findings: Finding[]): Finding[] {
+ *  names blanked, so the same rule on different models/columns shares a key. */
+export function ruleTitle(f: Pick<Finding, "title">): string {
+  return f.title
+    .replace(/^[\w.\-/]+: /, "")
+    .replace(/ \(\+\d+ more\)$/, "")
+    .replace(/`[^`]*`/g, "`…`")
</file context>

.replace(/ \(\+\d+ more\)$/, "")
.replace(/`[^`]*`/g, "`…`")
}

/**
* Merge repeats of one rule within one file (one finding per column of a grain
* test, one per join onto the same kind of key) into a single finding that
* lists every instance. Critical findings stay separate: each one can block.
*/
export function mergeRepeats(findings: Finding[]): Finding[] {
const groups = new Map<string, Finding[]>()
const out: Finding[] = []
for (const f of findings) {
if (f.severity === "critical") {
out.push(f)
continue
}
const key = [f.file, f.category, f.severity, !!f.degraded, ruleTitle(f)].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.

P1: mergeRepeats collapses distinct same-rule lint issues even though qualityLane deliberately fingerprints each message separately. The merged finding keeps only the first line, body, and evidence, hiding the other defect from the envelope and inline review.

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/finding.ts, line 229:

<comment>`mergeRepeats` collapses distinct same-rule lint issues even though `qualityLane` deliberately fingerprints each message separately. The merged finding keeps only the first line, body, and evidence, hiding the other defect from the envelope and inline review.</comment>

<file context>
@@ -203,3 +203,46 @@ export function dedupe(findings: Finding[]): Finding[] {
+      out.push(f)
+      continue
+    }
+    const key = [f.file, f.category, f.severity, !!f.degraded, ruleTitle(f)].join("|")
+    const g = groups.get(key)
+    if (g) g.push(f)
</file context>

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 delimiter-joined grouping key is ambiguous when a file or title contains |, allowing unrelated findings to merge. Serialize the tuple with JSON.stringify or use a collision-safe key.

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/finding.ts, line 229:

<comment>This delimiter-joined grouping key is ambiguous when a file or title contains `|`, allowing unrelated findings to merge. Serialize the tuple with `JSON.stringify` or use a collision-safe key.</comment>

<file context>
@@ -203,3 +203,46 @@ export function dedupe(findings: Finding[]): Finding[] {
+      out.push(f)
+      continue
+    }
+    const key = [f.file, f.category, f.severity, !!f.degraded, ruleTitle(f)].join("|")
+    const g = groups.get(key)
+    if (g) g.push(f)
</file context>
Suggested change
const key = [f.file, f.category, f.severity, !!f.degraded, ruleTitle(f)].join("|")
const key = JSON.stringify([f.file, f.category, f.severity, !!f.degraded, ruleTitle(f)])

const g = groups.get(key)
if (g) g.push(f)
else groups.set(key, [f])
}
for (const g of groups.values()) {
if (g.length === 1) {
out.push(g[0])
continue
}
g.sort((a, b) => tieKey(a).localeCompare(tieKey(b)))
const [first, ...rest] = g
out.push({
...first,
title: `${first.title} (+${rest.length} more)`,
body: `${first.body}\n\nSame rule, also here:\n${rest.map((f) => `- ${f.title.replace(/^[\w.\-/]+: /, "")}`).join("\n")}`,
})
}
return out
}
Loading
Loading