Repository navigation
feat(review): 11 deterministic rules for macro, test and yml defects - #1770
RaghwendraSingh wants to merge 12 commits into
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
There was a problem hiding this comment.
8 issues found across 7 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. 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. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/opencode/src/altimate/review/dbt-patterns.ts">
<violation number="1" location="packages/opencode/src/altimate/review/dbt-patterns.ts:899">
P2: `predKey` removes whitespace and lowercases SQL string literals, so distinct predicates such as `p.platform = 'a b'` and `p.platform = 'ab'` compare equal and produce a false redundant-join comment. Preserve quoted literal contents while normalizing only SQL syntax and identifiers.</violation>
<violation number="2" location="packages/opencode/src/altimate/review/dbt-patterns.ts:930">
P3: The join parser accepts only unquoted word aliases, so `AS "b"` makes this rule skip the entire join and miss repeated predicates. Parse quoted aliases before treating a join as having no analyzable `ON` clause.</violation>
<violation number="3" location="packages/opencode/src/altimate/review/dbt-patterns.ts:962">
P2: `redundantJoinPredicates` keeps an inner join's predicates in `seen` across a RIGHT or FULL join. Those joins NULL-extend earlier relations, so a later join repeating the predicate is not redundant. Reset `seen` after a right or full join.</violation>
<violation number="4" location="packages/opencode/src/altimate/review/dbt-patterns.ts:972">
P2: `topLevelAnd` treats `AND` and parentheses inside quoted SQL text as syntax, so different literals containing `and` can yield the same malformed fragment and trigger a false redundant-predicate finding. Tokenize quoted literals before splitting the ON clause.</violation>
</file>
<file name="packages/opencode/src/altimate/review/orchestrate.ts">
<violation number="1" location="packages/opencode/src/altimate/review/orchestrate.ts:2124">
P2: `dispatchVariantFindings` searches the repo and reads every hit file for each `adapter.dispatch` name in the macro, even names whose calls add no keyword. Those results are discarded. Limit the loop to the names `addedDispatchKwargs(file, newSql)` returns.</violation>
<violation number="2" location="packages/opencode/src/altimate/review/orchestrate.ts:2206">
P3: For added YAML files, `oldContent` is `undefined`, so `yamlParseFindings` skips the "parsed before" gate. A new file with a syntax error then gets a finding that says it parsed before this change. Either skip added files here or change the message for files with no base.</violation>
</file>
<file name="packages/opencode/src/altimate/review/rule-catalog.ts">
<violation number="1" location="packages/opencode/src/altimate/review/rule-catalog.ts:772">
P2: This line-based regex misses multiline Jinja SQL seams, which are common when SQL fragments are formatted across lines. Match the whole new macro text, not only individual added lines.</violation>
</file>
<file name="packages/opencode/src/altimate/review/project-rules.ts">
<violation number="1" location="packages/opencode/src/altimate/review/project-rules.ts:740">
P2: This condition treats every omitted inline strategy as `merge`, even when `manifestConfig.incremental_strategy` is `insert_overwrite` or `microbatch`. Use the effective manifest strategy before applying the merge-only warning, otherwise project-level strategy configuration produces a false finding.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
| const { alias, preds } = joins[k] | ||
| for (const p of preds) | ||
| if (chained && seen.has(p.key) && /=/.test(p.key) && !(alias && new RegExp(`\\b${alias}\\.`, "i").test(p.raw))) out.push(p.raw) | ||
| if (joins[k].inner) for (const p of preds) seen.add(p.key) |
There was a problem hiding this comment.
P2: redundantJoinPredicates keeps an inner join's predicates in seen across a RIGHT or FULL join. Those joins NULL-extend earlier relations, so a later join repeating the predicate is not redundant. Reset seen after a right or full join.
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 962:
<comment>`redundantJoinPredicates` keeps an inner join's predicates in `seen` across a RIGHT or FULL join. Those joins NULL-extend earlier relations, so a later join repeating the predicate is not redundant. Reset `seen` after a right or full join.</comment>
<file context>
@@ -891,6 +891,117 @@ const detectMultiCountDistinct: Detector = (c) => {
+ const { alias, preds } = joins[k]
+ for (const p of preds)
+ if (chained && seen.has(p.key) && /=/.test(p.key) && !(alias && new RegExp(`\\b${alias}\\.`, "i").test(p.raw))) out.push(p.raw)
+ if (joins[k].inner) for (const p of preds) seen.add(p.key)
+ }
+ return out
</file context>
| if (joins[k].inner) for (const p of preds) seen.add(p.key) | |
| if (joins[k].inner) for (const p of preds) seen.add(p.key) | |
| else if (/\b(right|full)\s+(?:outer\s+)?$/i.test(s.slice(Math.max(0, joins[k].start - 20), joins[k].start))) seen = new Set() |
| const dispatchVariantFindings = async (file: ChangedFile, newSql: string): Promise<Finding[]> => { | ||
| if (!/\badapter\.dispatch\b/.test(newSql)) return [] | ||
| const texts = new Map<string, string>([[file.path, newSql]]) | ||
| for (const name of new Set([...newSql.matchAll(/\badapter\.dispatch\s*\(\s*['"](?:\w+\.)?(\w+)['"]/g)].map((m) => m[1]))) { |
There was a problem hiding this comment.
P2: dispatchVariantFindings searches the repo and reads every hit file for each adapter.dispatch name in the macro, even names whose calls add no keyword. Those results are discarded. Limit the loop to the names addedDispatchKwargs(file, newSql) returns.
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 2124:
<comment>`dispatchVariantFindings` searches the repo and reads every hit file for each `adapter.dispatch` name in the macro, even names whose calls add no keyword. Those results are discarded. Limit the loop to the names `addedDispatchKwargs(file, newSql)` returns.</comment>
<file context>
@@ -2101,6 +2117,18 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
+ const dispatchVariantFindings = async (file: ChangedFile, newSql: string): Promise<Finding[]> => {
+ if (!/\badapter\.dispatch\b/.test(newSql)) return []
+ const texts = new Map<string, string>([[file.path, newSql]])
+ for (const name of new Set([...newSql.matchAll(/\badapter\.dispatch\s*\(\s*['"](?:\w+\.)?(\w+)['"]/g)].map((m) => m[1]))) {
+ const hits = (await input.grepProject?.(`__${name}(`).catch(() => [])) ?? []
+ for (const path of new Set(hits.map((h) => h.path)))
</file context>
| "A string of SQL is joined with `~` to a macro call or variable with no space at the seam, so the rendered SQL runs the two together (`ticket_id` + `, source_relation` works, `ticket_id` + `source_relation` does not). Put a space inside the string next to `~`.", | ||
| { | ||
| // A literal holding SQL (it has whitespace) that ends, or starts, with a word character right at the `~`. | ||
| added: /'[^'\n]*\s[^'\n]*[A-Za-z0-9]'\s*~\s*[A-Za-z_][\w.]*|[\w)]\s*~\s*'[A-Za-z0-9][^'\n]*\s[^'\n]*'/, |
There was a problem hiding this comment.
P2: This line-based regex misses multiline Jinja SQL seams, which are common when SQL fragments are formatted across lines. Match the whole new macro text, not only individual added 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/rule-catalog.ts, line 772:
<comment>This line-based regex misses multiline Jinja SQL seams, which are common when SQL fragments are formatted across lines. Match the whole new macro text, not only individual added lines.</comment>
<file context>
@@ -755,6 +761,31 @@ const CORRECTNESS: Rule[] = [
+ "A string of SQL is joined with `~` to a macro call or variable with no space at the seam, so the rendered SQL runs the two together (`ticket_id` + `, source_relation` works, `ticket_id` + `source_relation` does not). Put a space inside the string next to `~`.",
+ {
+ // A literal holding SQL (it has whitespace) that ends, or starts, with a word character right at the `~`.
+ added: /'[^'\n]*\s[^'\n]*[A-Za-z0-9]'\s*~\s*[A-Za-z_][\w.]*|[\w)]\s*~\s*'[A-Za-z0-9][^'\n]*\s[^'\n]*'/,
+ macros: true,
+ },
</file context>
| const bigquery = dialectKnown ? /bigquery/i.test(dialect) : /['"]field['"]\s*:/.test(cfg.partition_by ?? "") | ||
| const predicates = cfg.incremental_predicates ?? manifestConfig?.incremental_predicates | ||
| const hasPredicates = Array.isArray(predicates) ? predicates.length > 0 : !!predicates && !/^\s*(none|\[\s*\])\s*$/i.test(String(predicates)) | ||
| if (bigquery && parts.length && uk.length && (strategy === "merge" || !strategy) && !hasPredicates) |
There was a problem hiding this comment.
P2: This condition treats every omitted inline strategy as merge, even when manifestConfig.incremental_strategy is insert_overwrite or microbatch. Use the effective manifest strategy before applying the merge-only warning, otherwise project-level strategy configuration produces a false finding.
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/project-rules.ts, line 740:
<comment>This condition treats every omitted inline strategy as `merge`, even when `manifestConfig.incremental_strategy` is `insert_overwrite` or `microbatch`. Use the effective manifest strategy before applying the merge-only warning, otherwise project-level strategy configuration produces a false finding.</comment>
<file context>
@@ -411,6 +732,23 @@ export function modelConfigFindings(
+ const bigquery = dialectKnown ? /bigquery/i.test(dialect) : /['"]field['"]\s*:/.test(cfg.partition_by ?? "")
+ const predicates = cfg.incremental_predicates ?? manifestConfig?.incremental_predicates
+ const hasPredicates = Array.isArray(predicates) ? predicates.length > 0 : !!predicates && !/^\s*(none|\[\s*\])\s*$/i.test(String(predicates))
+ if (bigquery && parts.length && uk.length && (strategy === "merge" || !strategy) && !hasPredicates)
+ out.push(
+ finding(
</file context>
| if (bigquery && parts.length && uk.length && (strategy === "merge" || !strategy) && !hasPredicates) | |
| if (bigquery && parts.length && uk.length && (strategy === "merge" || (!strategy && String(manifestConfig?.incremental_strategy ?? "merge").toLowerCase() === "merge")) && !hasPredicates) |
| const parts: string[] = [] | ||
| let depth = 0 | ||
| let last = 0 | ||
| for (const m of clause.matchAll(/[()]|\band\b/gi)) { |
There was a problem hiding this comment.
P2: topLevelAnd treats AND and parentheses inside quoted SQL text as syntax, so different literals containing and can yield the same malformed fragment and trigger a false redundant-predicate finding. Tokenize quoted literals before splitting the ON clause.
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 972:
<comment>`topLevelAnd` treats `AND` and parentheses inside quoted SQL text as syntax, so different literals containing `and` can yield the same malformed fragment and trigger a false redundant-predicate finding. Tokenize quoted literals before splitting the ON clause.</comment>
<file context>
@@ -891,6 +891,117 @@ const detectMultiCountDistinct: Detector = (c) => {
+ const parts: string[] = []
+ let depth = 0
+ let last = 0
+ for (const m of clause.matchAll(/[()]|\band\b/gi)) {
+ if (m[0] === "(") depth++
+ else if (m[0] === ")") depth--
</file context>
|
|
||
| /** Normalised equality/comparison predicate: no whitespace, lowercase, `a=b` with sides sorted. */ | ||
| function predKey(p: string): string { | ||
| const t = p.replace(/\s+/g, "").toLowerCase() |
There was a problem hiding this comment.
P2: predKey removes whitespace and lowercases SQL string literals, so distinct predicates such as p.platform = 'a b' and p.platform = 'ab' compare equal and produce a false redundant-join comment. Preserve quoted literal contents while normalizing only SQL syntax and identifiers.
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 899:
<comment>`predKey` removes whitespace and lowercases SQL string literals, so distinct predicates such as `p.platform = 'a b'` and `p.platform = 'ab'` compare equal and produce a false redundant-join comment. Preserve quoted literal contents while normalizing only SQL syntax and identifiers.</comment>
<file context>
@@ -891,6 +891,117 @@ const detectMultiCountDistinct: Detector = (c) => {
+
+/** Normalised equality/comparison predicate: no whitespace, lowercase, `a=b` with sides sorted. */
+function predKey(p: string): string {
+ const t = p.replace(/\s+/g, "").toLowerCase()
+ const eq = /^([^=<>!]+)=([^=<>!]+)$/.exec(t)
+ return eq ? [eq[1], eq[2]].sort().join("=") : t
</file context>
| file.status === "added" ? Promise.resolve(undefined) : read(file.oldPath ?? file.path, "old"), | ||
| ]) | ||
| if (file.kind === "other") checkedOther(file) | ||
| return yamlParseFindings(file, oldContent, newContent) |
There was a problem hiding this comment.
P3: For added YAML files, oldContent is undefined, so yamlParseFindings skips the "parsed before" gate. A new file with a syntax error then gets a finding that says it parsed before this change. Either skip added files here or change the message for files with no base.
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 2206:
<comment>For added YAML files, `oldContent` is `undefined`, so `yamlParseFindings` skips the "parsed before" gate. A new file with a syntax error then gets a finding that says it parsed before this change. Either skip added files here or change the message for files with no base.</comment>
<file context>
@@ -2157,6 +2186,56 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
+ file.status === "added" ? Promise.resolve(undefined) : read(file.oldPath ?? file.path, "old"),
+ ])
+ if (file.kind === "other") checkedOther(file)
+ return yamlParseFindings(file, oldContent, newContent)
+ }),
+ )
</file context>
| i++ | ||
| rel = null | ||
| } else if (rel) i += rel[0].length | ||
| const tail = /^\s+(?:as\s+)?(\w+)\s+on\b/i.exec(s.slice(i)) ?? /^\s+on\b/i.exec(s.slice(i)) |
There was a problem hiding this comment.
P3: The join parser accepts only unquoted word aliases, so AS "b" makes this rule skip the entire join and miss repeated predicates. Parse quoted aliases before treating a join as having no analyzable ON clause.
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 930:
<comment>The join parser accepts only unquoted word aliases, so `AS "b"` makes this rule skip the entire join and miss repeated predicates. Parse quoted aliases before treating a join as having no analyzable `ON` clause.</comment>
<file context>
@@ -891,6 +891,117 @@ const detectMultiCountDistinct: Detector = (c) => {
+ i++
+ rel = null
+ } else if (rel) i += rel[0].length
+ const tail = /^\s+(?:as\s+)?(\w+)\s+on\b/i.exec(s.slice(i)) ?? /^\s+on\b/i.exec(s.slice(i))
+ if (!tail) {
+ joins.push({ preds: [], start: m.index!, end: i, inner })
</file context>
| "A string of SQL is joined with `~` to a macro call or variable with no space at the seam, so the rendered SQL runs the two together (`ticket_id` + `, source_relation` works, `ticket_id` + `source_relation` does not). Put a space inside the string next to `~`.", | ||
| { | ||
| // A literal holding SQL (it has whitespace) that ends, or starts, with a word character right at the `~`. | ||
| added: /'[^'\n]*\s[^'\n]*[A-Za-z0-9]'\s*~\s*[A-Za-z_][\w.]*|[\w)]\s*~\s*'[A-Za-z0-9][^'\n]*\s[^'\n]*'/, |
There was a problem hiding this comment.
WARNING: ~ is also the Postgres/Redshift/DuckDB regex-match operator, so this fires on valid SQL
The second alternative ([\w)]\s*~\s*'…') matches a plain comparison such as where email ~ 'a b', and the first matches 'a b' ~ name, because added is tested with string literals kept and the rule runs on model SQL too. That reports "SQL text concatenated in Jinja without a space" for SQL that contains no Jinja ~ at all. Gate the rule on the line containing {{ or {% (or add an unless for a comparison context), so the regex operator form is not reported.
| for (const ch of body + ",") { | ||
| if ("([{".includes(ch)) depth++ | ||
| else if (")]}".includes(ch)) depth-- | ||
| if (ch === "," && depth === 0) { |
There was a problem hiding this comment.
WARNING: a comma inside a quoted default splits the parameter list into phantom parameters
cur is depth-tracked only for ([{ and )]}, not for quotes, so {% macro m(c, precision="10,2") %} parses as [c, precision(default), {name:"2", hasDefault:false}]. When that fragment is on an added signature line the rule emits new parameter 2ofm has no default; a ] inside a default drives depth negative and swallows every later parameter instead. Skip quoted spans while collecting cur (or tokenise with quote awareness) so string defaults cannot create or hide parameters.
| const args = parenBody(sql, argsOpen) | ||
| const first = lineAt(argsOpen) | ||
| const last = first + args.split("\n").length - 1 | ||
| const kws = [...topLevel(args).matchAll(/(?:^|,)\s*(\w+)\s*=(?!=)/g)].map((k) => k[1]) |
There was a problem hiding this comment.
WARNING: keyword arguments are also read out of string arguments
topLevel(args) blanks nested parentheses but keeps string contents, so the (?:^|,)\s*(\w+)\s*=(?!=) scan treats = inside a quoted value as a keyword. For adapter.dispatch('ghost', 'pkg')(col=col, filter="live = true, backfill = false") it collects backfill, and any variant without a backfill parameter gets a false "takes no keyword argument" warning anchored to that string line. Mask literals before matching (as rule-catalog's stripStrings does) and keep the raw line only for the evidence text.
| const out: { macro: string; params: string[]; acceptsKwargs: boolean }[] = [] | ||
| for (const [macro, sig] of signatures(text)) { | ||
| if (!macro.endsWith(`__${name}`) || macro === name) continue | ||
| const at = new RegExp(`\\{%-?\\s*macro\\s+${escapeRe(macro)}\\s*\\(`).exec(text)!.index |
There was a problem hiding this comment.
WARNING: the non-null assertion throws for {% test %} signatures and loses every finding for the file
signatures(text) matches both macro and test, but this re-lookup regex accepts only macro. A generic test whose name ends in __<dispatched name> (tests legitimately live in macros/ and tests/, both searched by grepProject) makes exec() return null, so !.index throws. The throw propagates through dispatchVariantFindings into the macro lane's catch, which calls failedFile() and drops all findings for that macro file, not just this check. Guard the result (const at = …?.index; if (at === undefined) continue) or pass the kind through from signatures().
| const bigquery = dialectKnown ? /bigquery/i.test(dialect) : /['"]field['"]\s*:/.test(cfg.partition_by ?? "") | ||
| const predicates = cfg.incremental_predicates ?? manifestConfig?.incremental_predicates | ||
| const hasPredicates = Array.isArray(predicates) ? predicates.length > 0 : !!predicates && !/^\s*(none|\[\s*\])\s*$/i.test(String(predicates)) | ||
| if (bigquery && parts.length && uk.length && (strategy === "merge" || !strategy) && !hasPredicates) |
There was a problem hiding this comment.
WARNING: incremental_strategy is read only from the inline config, so an insert_overwrite model is reported as an unpruned merge
strategy comes from unquote(cfg.incremental_strategy), while materialized and predicates fall back to the manifest. A BigQuery model with inline config(unique_key=…, partition_by=…) but +incremental_strategy: insert_overwrite in dbt_project.yml or the schema config has strategy === "", so (strategy === "merge" || !strategy) assumes merge and suggests incremental_predicates for a strategy that never merges. Fall back to manifestConfig?.incremental_strategy (and treat a known non-merge strategy as n/a), as modelSemantics already does.
| const parts: string[] = [] | ||
| let depth = 0 | ||
| let last = 0 | ||
| for (const m of clause.matchAll(/[()]|\band\b/gi)) { |
There was a problem hiding this comment.
SUGGESTION: splitting on and inside literals creates a false "redundant" match
topLevelAnd sees the raw clause (literals are deliberately kept), so p.segment = 'smb and enterprise' and p.segment = 'smb and midmarket' both split into the fragment p.segment = 'smb, compare equal, and are reported as redundant even though the joins filter different values. Split on and in a literal-masked copy and keep the raw predicate for the message.
| if (!missing.length) return [] | ||
| const others = [...new Set([...newContent.matchAll(DOC_REF)].map((m) => m[1]))].filter((n) => !missing.includes(n)).slice(0, 5) | ||
| let resolves = false | ||
| for (const n of others) if ((resolves = await docExists(n))) break |
There was a problem hiding this comment.
SUGGESTION: one locally-resolving doc() is not enough to prove the new one is local
The guard only requires any other doc() in the file to resolve in the repo. A package file that mixes a local doc with one shipped by an installed package (for example doc('hubspot__contact_id')) passes the guard, and the new name is then reported as "dbt fails to parse the project" although the block exists in that package — the exact case the docstring says it avoids. Require that the missing name's prefix (or every other doc() in the file) resolves locally before reporting.
| const ymls = reviewable.filter((f) => f.status !== "deleted" && /\.ya?ml$/i.test(f.path)) | ||
| const sets = await Promise.all( | ||
| ymls.map(async (file) => { | ||
| const [newContent, oldContent] = await Promise.all([ |
There was a problem hiding this comment.
SUGGESTION: every changed yml now costs two extra git shows before the schema lane re-reads the same file
getContent (makeContentResolver) has no cache, and ylms includes all schema_yml files, so each one is read old+new here and again in the schema block (around line 2282), on top of the contract lane's reads. The Promise.all over ylms also launches 2 × yml count subprocesses at once. On a schema-heavy PR this is a large share of the review's process time; memoising getContent per (path, side) or reusing these contents in the schema block would keep it bounded.
|
|
||
| /** Each `{% macro name(…) %}` / `{% test name(…) %}` with its parameters (name and whether it has a default) | ||
| * and the head line range of its signature. Defaults may hold calls and lists. */ | ||
| function signatures(sql: string | undefined) { |
There was a problem hiding this comment.
SUGGESTION: signatures() duplicates macroSignatures() with different parsing — consolidate
macroSignatures() (line 217) still feeds macro-param-shifted and signatureAnchor(), but it splits on every comma and truncates at the first ), so defaults like cols=['x', 'y'] or ts=dbt.current_timestamp() produce phantom or falsely moved parameters that the new parser handles correctly. Now that signatures() exists, make it the single parser for both checks (returning sig.first/sig.last for the anchor) and delete the older helper so the two cannot drift.
| return out | ||
| } | ||
|
|
||
| const DISPATCH_VARIANT = new RegExp(`^(default|${ADAPTERS.join("|")})__\\w`, "i") |
There was a problem hiding this comment.
SUGGESTION: the dispatch-variant skip list is limited to ADAPTERS
DISPATCH_VARIANT exempts default__ and the 16 adapters in ADAPTERS, so a new required parameter on a variant such as mysql__sat, sqlite__…, vertica__… or greenplum__… is still reported with "every call written before this change omits it" — false for a variant whose dispatcher passes every argument. Match the generic <prefix>__<same base name> shape instead of an adapter allow-list.
Code Review SummaryStatus: 11 Issues Found | Recommendation: Address before merge Overview
Fix these issues in Kilo Cloud Issue Details (click to expand)WARNING
SUGGESTION
Note: the fix commits resolve the Files Reviewed (7 files)
Previous Review Summary (commit a6823f9)Current summary above is authoritative. Previous snapshots are kept for context only. Previous review (commit a6823f9)Status: 11 Issues Found | Recommendation: Address before merge Overview
Fix these issues in Kilo Cloud Issue Details (click to expand)WARNING
SUGGESTION
Note: the new Files Reviewed (7 files)
Reviewed by deepseek-v4.1-flash-1017 · Input: 166.9K · Output: 58.8K · Cached: 4.6M Review guidance: REVIEW.md from base branch |
hkaltai
left a comment
There was a problem hiding this comment.
Verdict: request changes. Panel: Claude, Codex, lead. All three reviewers independently ran bun test test/altimate/review (711 pass); the findings below were reproduced.
Counts: CRITICAL 0, MAJOR 1, MINOR 6, NIT 6.
MINOR (not anchorable to a diff line), project-rules.ts:677: const strategy = unquote(cfg.incremental_strategy).toLowerCase() ignores manifest/project config, unlike materialized and incremental_predicates. A model whose strategy is insert_overwrite set in dbt_project.yml gets "incremental merge has no incremental_predicates". Fix: fall back to manifestConfig?.incremental_strategy.
The MAJOR is a crash: an unescaped, unaliased join relation (join unnest(a.arr) on ...) reaches new RegExp in redundantJoinPredicates and rejects the whole runReview. The rest are false positives or false negatives in summary-tier rules.
Nits
orchestrate.ts:2267:docs ${name}fixed-string search misses{% docs name %}(extra spaces/tab) and can emitdoc-block-missingfor an existing block. Grep forname, then filter with the existing whitespace-tolerant regex.project-rules.ts:304(macro-param-no-default): renaming a parameter (foo(a)->foo(relation)) fires "every call omits it"; positional callers still bind. Skip same-count renames or reword.project-rules.ts:456(isPackageProject): a project with noprofile:is treated as a package; dbt Cloud projects can omit it and may legitimately commitpackage-lock.yml. Require theintegration_tests/signal as well.project-rules.ts:379(yamlError): theyamllibrary reports two<<: *a/<<: *bkeys asDUPLICATE_KEY, which dbt's PyYAML accepts. Ignore duplicate<<.dbt-patterns.ts:957: a join inside a parenthesised relation starts beforeprev.end, so the slice is""and matchesJOIN_TYPE_GAP; requireprev.end <= start. Also the user message and// 46.comment say "an earlier join" but the logic is INNER only.orchestrate.ts:2205/2527: everyotherYAML file is marked reviewed after only a parse check, shrinking "not reviewed" without content coverage. Alsorule-catalog.tsmacro-group-by-ordinalmissesgroup by 1, customer_id;signaturesvsmacroSignaturesandcheckedOther/otherCheckedare confusingly named.
| if (!chained) seen = new Set() | ||
| const { alias, preds } = joins[k] | ||
| for (const p of preds) | ||
| if (chained && seen.has(p.key) && /=/.test(p.key) && !(alias && new RegExp(`\\b${alias}\\.`, "i").test(p.raw))) out.push(p.raw) |
There was a problem hiding this comment.
[MAJOR] Unescaped join relation text used in new RegExp crashes the whole review
!(alias && new RegExp(`\\b${alias}\\.`, "i").test(p.raw))When a join has no alias, alias falls back to rel[1].split(".").pop() (line 935), which is raw text. For ... join b on a.id = b.id join unnest(a.arr) on a.id = b.id (also Snowflake join table(flatten(a.arr)) on ...) redundantJoinPredicates throws SyntaxError: Invalid regular expression: unmatched parentheses. Reproduced directly. The model detectors run without a try/catch, so runReview rejects and the PR gets no verdict at all. Rare (needs a repeated predicate too), but the failure is total on valid SQL.
Fix: use escapeRe(alias) (or only accept the fallback when it matches /^\w+$/), and add a test with an unaliased unnest(...) join.
| const JOIN_CLAUSE_END = /^(?:join|where|group\s+by|order\s+by|qualify|having|union|limit|window|left|right|inner|full|cross|natural|lateral|select|from)\b|^;/i | ||
|
|
||
| /** Normalised equality/comparison predicate: no whitespace, lowercase, `a=b` with sides sorted. */ | ||
| function predKey(p: string): string { |
There was a problem hiding this comment.
[MINOR] predKey lowercases and strips whitespace inside string literals, so different predicates compare equal
const t = p.replace(/\s+/g, "").toLowerCase()from a inner join b on a.flag = 'X' inner join c on b.id = c.id and a.flag = 'x' flags a.flag = 'x' as redundant (reproduced), but with 'X' filtering first it is not: removing it as suggested changes results. The doc comment says "Literals are compared as written", which this contradicts.
Fix: normalise only outside quoted literals, keep literal contents exact.
| if (!chained) seen = new Set() | ||
| const { alias, preds } = joins[k] | ||
| for (const p of preds) | ||
| if (chained && seen.has(p.key) && /=/.test(p.key) && !(alias && new RegExp(`\\b${alias}\\.`, "i").test(p.raw))) out.push(p.raw) |
There was a problem hiding this comment.
[MINOR] Predicates stay in seen across RIGHT/FULL joins
if (joins[k].inner) for (const p of preds) seen.add(p.key)from a inner join b on a.id = b.id right join c on b.id = c.id inner join d on c.id = d.id and a.id = b.id flags a.id = b.id (reproduced). The RIGHT join re-introduces rows where it is false, so removing it changes results ([1] vs [1, 2] in a SQLite check).
Fix: clear seen when a RIGHT/FULL join is crossed.
| "A string of SQL is joined with `~` to a macro call or variable with no space at the seam, so the rendered SQL runs the two together (`ticket_id` + `, source_relation` works, `ticket_id` + `source_relation` does not). Put a space inside the string next to `~`.", | ||
| { | ||
| // A literal holding SQL (it has whitespace) that ends, or starts, with a word character right at the `~`. | ||
| added: /'[^'\n]*\s[^'\n]*[A-Za-z0-9]'\s*~\s*[A-Za-z_][\w.]*|[\w)]\s*~\s*'[A-Za-z0-9][^'\n]*\s[^'\n]*'/, |
There was a problem hiding this comment.
[MINOR] jinja-concat-missing-space: catastrophic backtracking and ~ false positives
added: /'[^'\n]*\s[^'\n]*[A-Za-z0-9]'\s*~\s*[A-Za-z_][\w.]*|[\w)]\s*~\s*'[A-Za-z0-9][^'\n]*\s[^'\n]*'/,(1) The overlapping [^'\n]*\s[^'\n]* is super-linear on a long line with no closing quote: reported ~1.8 s for a 64 KB padded literal and several seconds at 100 KB+, synchronously. (2) The rule has no kind, so it also runs on model SQL, where the second branch matches the Postgres/Redshift regex operator (where memo ~ 'refund request') and log('Row count is' ~ n).
Fix: find the ~ first and inspect adjacent tokens linearly, or use a lookahead '(?=[^'\n]*\s)[^'\n]*[A-Za-z0-9]'; restrict to Jinja delimiters or kind: "macro".
| const at = new RegExp(`\\{%-?\\s*macro\\s+${escapeRe(macro)}\\s*\\(`).exec(text)!.index | ||
| const end = text.slice(at).search(/\{%-?\s*endmacro\b/) | ||
| const body = text.slice(at, end < 0 ? undefined : at + end) | ||
| out.push({ macro, params: sig.params.map((p) => p.name), acceptsKwargs: /\bkwargs\b/.test(body) || /\bvarargs\b/.test(body) }) |
There was a problem hiding this comment.
[MINOR] acceptsKwargs is true for varargs or the word kwargs in a comment
acceptsKwargs: /\bkwargs\b/.test(body) || /\bvarargs\b/.test(body)In Jinja, varargs captures extra positional args only; foo(x=1, extra=2) against default__foo(x) using varargs still raises takes no keyword argument. A {# no kwargs here #} comment also suppresses the finding. Any x__name macro is also treated as a variant (DISPATCH_VARIANT at line 251 could limit this to adapter prefixes).
Fix: only count real kwargs usage outside comments/strings.
| export function testWindowFindings(file: ChangedFile): Finding[] { | ||
| if (file.status !== "modified" || !/\.sql$/i.test(file.path)) return [] | ||
| const rows = [...walkDiffRows(file.diff)] | ||
| const removed = rows.filter((r) => r.kind === "-").map((r) => windows(r.text)).filter((w) => w.secs.length) |
There was a problem hiding this comment.
[MINOR] test-window-shrunk ignores comparison direction
const removed = rows.filter((r) => r.kind === "-").map((r) => windows(r.text))ts > current_timestamp + interval '2 day' -> '1 day', or ts < dateadd('day', -30, ...) -> -7 make the test stricter, yet fire as a shortened lookback.
Fix: only fire for a window on the right of >/>=/between against the clock.
A macro builds its SELECT list from arguments and loops, so the column at a given position depends on the caller. `group-by-positional` needs two or more positions and so never saw `GROUP BY 2` in a high-water-mark query. - `rule-catalog:macro-group-by-ordinal`: a single `GROUP BY <n>` on an added line of a changed macro (new `macro` rule kind, so models' `group by 1` style is untouched). Anchors to the matched line; summary tier. - Tests: fires and records the line; twin with the column name is silent; two positions and model SQL are left to the existing rule. Dev evidence: 4 replayed reviewer comments asked for a column name instead of a single `GROUP BY` position in macro high-water-mark queries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…efault
Jinja binds an omitted macro parameter to Undefined, not `none`, so when a
parameter is added without a default every call written before the change
gets Undefined: an `is none` fallback downstream is skipped and the value
renders empty. `dbt-patterns:macro-var-no-default` covers `var()` reads only,
which is why it did not see this.
- `dbt-project-rules:macro-param-no-default`: a parameter present in the head
signature of a `{% macro %}` / `{% test %}` that existed at base, absent
from the base signature, with no default, on an added signature line.
`model` / `column_name` of a generic test are skipped. Anchored to the
parameter line; summary tier.
- Signature parsing keeps defaults that hold calls and lists intact.
- Tests: fires on the added line of a test block; `=none` twin is silent;
new macros and unchanged parameters are silent.
Dev evidence: a replayed reviewer comment on a generic test whose new
parameter lacked `=none` while its sibling test had it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sn't accept
When a dispatcher starts forwarding a new keyword, every `<adapter>__` variant
must take it; a variant that doesn't fails on that adapter with "takes no
keyword argument".
- `dbt-project-rules:dispatch-kwarg-not-in-variant`: for each keyword on an
added line inside an `adapter.dispatch('name', …)(…)` argument list, find
the repo's `<prefix>__name` variants (same file plus `grepProject` at head)
and report those whose signature lacks the keyword and whose body doesn't
read `kwargs`. Anchored to the added keyword line; summary tier.
- Uses the existing `grepProject`/`getContent` plumbing; no new inputs.
- Tests: fires on the keyword line naming only the variant that lacks it;
twin where every variant takes it is silent; `kwargs` catch-all and
unchanged lines are silent.
Dev evidence: a replayed reviewer comment on a dispatcher that began
forwarding two new keywords to its adapter variants.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…shorter A data test that checks "rows in the last day" and is edited to check the last 30 minutes silently stops testing everything outside the new window. - `dbt-project-rules:test-window-shrunk`: in a modified singular test, pair each added line with a removed line of the same shape (window literals masked) and report when an `interval N unit` or negative `dateadd` count got shorter. Anchored to the added line; summary tier. - Equivalent rewrites (1 day -> 24 hour), longer windows and filters that replace the literal with a var are silent (tests cover each). Dev evidence: two replayed reviewer comments on singular tests whose lookback shrank to the scheduler interval. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…any changed yml `.quickstart/quickstart.yml` has no dbt role, so the review listed it as not reviewed and never looked at it. Two structural defects live there and in other yml files. - `dbt-project-rules:yaml-parse-error`: a changed `.yml`/`.yaml` (any role) that parsed at base and no longer parses at head. A key indented under the previous entry is reported as "YAML key defined twice". Anchored to the parser's error line; summary tier. File-level by nature. - `dbt-project-rules:quickstart-public-model-missing`: a new model named with the prefix most `public_models` entries share (>=80%, >=3 entries) and not listed there. Goes on the quickstart file when the PR changes it, else on the new model; summary tier. - "Other" files these checks read are marked reviewed in the coverage ledger. - Tests: duplicate-key error line; correctly indented twin and an already broken base are silent; end-to-end run on a quickstart file; listed model twin is silent; staging/intermediate names are not public candidates. Dev evidence: two replayed reviewer comments on quickstart files (a mis-indented variable key; a new model left out of `public_models`). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dbt fails to parse a project when a yml description calls `doc('x')` and no
`{% docs x %}` block exists, which breaks every build of the PR.
- `dbt-project-rules:doc-block-missing`: single-argument `doc('x')` names on
added lines of a schema yml, not referenced at base, with no
`{% docs x %}` block in the project's `.md` files at head (`grepProject`,
cached per name; a failed search counts as found). Fires only when another
`doc()` in the same file resolves locally, so docs kept in an installed
package are not reported. Anchored to the added line; summary tier.
- Uses the existing `grepProject` plumbing; no new inputs.
- Tests: missing name fires with its line; a defined name is silent; a file
whose docs all live elsewhere is silent; end-to-end search of `*.md`.
Deferred: "any `dbt parse` error on head that is absent at base" needs both
sides' parse results, which the review CLI does not receive.
Dev evidence: a replayed reviewer comment on a yml `doc()` whose block was
never written, which failed the PR's CI build.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Projects that install a package resolve their own dependencies and never read the package's lock file; committing it only pins the package's own development installs and drifts from its `packages.yml` ranges. In an application project the lock file belongs in git, so the rule needs the package signal. - `dbt-project-rules:package-lock-in-package`: `package-lock.yml` with status added, next to a `dbt_project.yml` whose repo has an `integration_tests/dbt_project.yml` or whose project names no `profile:`. File-level by nature; anchored to the file's first line; summary tier. - The file is marked reviewed in the coverage ledger when checked. - Tests: package with integration tests and package without a profile fire; application project twin and an already-committed (modified) lock file are silent. Deferred: `require-dbt-version` upper bound excluding the current dbt minor needs the running dbt version, which the review does not receive. Dev evidence: a replayed reviewer comment asking not to commit the lock file in a package repository. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s and Jinja SQL seams Three line-level defects reviewers raised on model SQL that no rule saw. - `dbt-project-rules:merge-no-incremental-predicates`: on BigQuery (known dialect, or a `partition_by` dict when unknown), an incremental model that merges (explicit or default strategy) on a `unique_key` into a partitioned table with no `incremental_predicates` in the file or the manifest config. Only when the PR touches the config or the incremental filter (existing gate). Anchored to the config/filter line; summary tier. - `dbt-patterns:redundant-join-predicate`: an equality in a JOIN's ON clause that an earlier join of the same join chain already states and that does not reference the newly joined relation. Chains break at any clause other than a join, so separate queries/CTEs never pair. Anchored to the added line; summary tier. - `rule-catalog:jinja-concat-missing-space`: a string literal holding SQL (it contains whitespace) joined with `~` to a call or variable with a word character at the seam. Identifier building (`'stg_' ~ name`) and quote wrapping are silent. Models and macros; summary tier. - Tests: each rule fires on its defect shape and its twin (`incremental_ predicates` set; predicate on the joined relation; space inside the literal) is silent, plus the cross-CTE and other-engine cases. Dev evidence: replayed reviewer comments asking for `incremental_predicates` on a partitioned merge, removing join predicates "included in join above", and adding a space around a macro call in a concatenated `datediff` string. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ion-test projects Two gaps found replaying the dev target PRs (not the noise sample): - `dbt-project-rules:quickstart-public-model-missing` returned nothing when the quickstart file had an unrelated duplicate key elsewhere: the strict YAML parse threw. Read it leniently, as PyYAML (which dbt uses) does. - `dbt-project-rules:package-lock-in-package` looked for `integration_tests/dbt_project.yml` only; packages often nest it as `integration_tests/<name>/dbt_project.yml`. Also accept a nested project found through `grepProject`. - Tests: a quickstart file with a duplicate key still reports the missing model (listed-model twin silent); a nested integration project marks the repo as a package. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…te redundant Found on dev target files: `dbt-patterns:redundant-join-predicate` reported `x.platform = ''` repeated across two LEFT JOINs. - After a LEFT JOIN the same predicate is not redundant: the left join keeps the rows it rejects, so a later join's copy still filters. Only predicates seen in an earlier inner (or plain) join of the chain count. - Literals are compared as written; they were blanked, so `= 'web'` and `= 'app'` looked identical. - Tests: LEFT JOIN twin and different-literal twin are silent; equal literals after an inner join still fire. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Per-rule judging on dev target PRs: 5 of 6 non-target findings of `dbt-project-rules:macro-param-no-default` were on `<adapter>__x` variants (`default__`, `snowflake__`, ...). A variant is called only by its dispatcher, which the same change updates and which passes every argument, so "existing callers omit it" does not hold there. - Skip macros named `<adapter or default>__…`. - Test: a new parameter on an adapter variant is silent; the same on the dispatcher-level macro still fires. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…`{% test %}` dispatch crash, Jinja seam gate
- `project-rules.ts`: `modelConfigFindings` falls back to the manifest's
`incremental_strategy` when the file doesn't set one, so a BigQuery
`insert_overwrite` set in `dbt_project.yml` is no longer reported as an
unpruned merge by `merge-no-incremental-predicates`.
- `project-rules.ts`: `dispatchVariants` skips `{% test %}` blocks and guards the
macro re-lookup, which threw on a `{% test x__name %}` and dropped every
finding for the file.
- `rule-catalog.ts`: `jinja-concat-missing-space` only matches a seam inside an
open `{{`/`{%` on the line, so a Postgres/Redshift/DuckDB `col ~ 'a b'` regex
match no longer fires, and log/print/compiler-error messages are exempt.
- Tests for each.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8aba994 to
f3c5e86
Compare
a6823f9 to
936b24a
Compare
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
| */ | ||
| export function redundantJoinPredicates(sql: string): string[] { | ||
| // Literal text stays (`= 'a'` and `= 'b'` differ); `and`/parens inside a literal are rare enough here. | ||
| const s = stripComments(sql).replace(/\{\{[\s\S]*?\}\}/g, "__jinja__").replace(/\{%[\s\S]*?%\}/g, " ") |
There was a problem hiding this comment.
SUGGESTION: literal text now feeds the structural join scan
stripLiterals was dropped here so = 'a' and = 'b' no longer compare equal, but the same s is also used as SQL structure: matchAll(/\bjoin\b/gi) (two lines below) and JOIN_CLAUSE_END now match keywords inside string literals. A model that adds a line such as {% set q = 'select * from a join b on a.id = b.id and y = 1' %} gets a phantom join parsed out of the string: when it precedes the real chain, chained is false at the next real join (its end lands mid-literal), so seen is reset and redundant predicates are missed; when it follows one, the phantom ON clause repeats the earlier predicate key and detectRedundantJoinPredicate finds the predicate text in the literal's own added line and reports "Remove it" against it. Mask literals in the copy used for the structural scan (e.g. replace each with a same-length placeholder) and keep the literal-preserving text only for predKey/the message.
| // A literal holding SQL (it has whitespace) that ends, or starts, with a word character right at the `~`, | ||
| // inside an open `{{`/`{%` on the line (a bare `col ~ 'a b'` is a Postgres regex match, not Jinja). | ||
| added: | ||
| /\{[{%](?:(?![}%]\}).)*?(?:'[^'\n]*\s[^'\n]*[A-Za-z0-9]'\s*~\s*[A-Za-z_][\w.]*|[\w)]\s*~\s*'[A-Za-z0-9][^'\n]*\s[^'\n]*')/, |
There was a problem hiding this comment.
SUGGESTION: the rewritten pattern still backtracks super-linearly on an unterminated quote
'[^'\n]*\s[^'\n]*[A-Za-z0-9]' and '[A-Za-z0-9][^'\n]*\s[^'\n]*' each join two unbounded [^'\n]* with only \s between them, and both stars can consume whitespace, so the split point is ambiguous. matchRule tests this against every added line synchronously, so a long added line with an open {{ and a ' that never closes makes the engine try every (star split, whitespace position) pair before failing, which is quadratic in line length on untrusted PR input. Find the ~ first and inspect the tokens next to it, or fold the whitespace test into a lookahead ('(?=[^'\n]*\s)[^'\n]*[A-Za-z0-9]') so only one unbounded star remains.
| // Lenient like PyYAML: a duplicate key elsewhere in the file must not hide the list. | ||
| let doc: any | ||
| try { | ||
| doc = YAML.parse(content ?? "", { uniqueKeys: false }) |
There was a problem hiding this comment.
SUGGESTION: this duplicates parseYaml
parseYaml (line 45) already wraps YAML.parse in the same try/catch and returns undefined on failure, and an undefined doc produces the same empty listed list below, so the inline copy adds a second YAML parse policy in this module that can drift from the helper. Give parseYaml an options argument (or a lenient flag) and call it here.
Issue for this PR
Part of #1792
Type of change
What does this PR do?
Stacked on #1769. Adds 11 deterministic rules for macro, test and yml defects that the replay showed every reviewer missing. They are written from defects in the tuning repos and checked on repos never used for tuning. All start at summary tier.
Results (rules only, no AI)
New rules
GROUP BY <n>with a single position inside macro SQL.adapter.dispatchkeywords an adapter variant doesn't accept.public_modelsmissing a new model.doc()reference with no docs block.package-lock.ymladded to a package repository.How did you verify your code works?
bun test test/altimate/review: 711 pass.bunx tsc --noEmit: clean.review-dbt-patterns/review-project-rulestests.Cost side effects
None. The 11 rules are deterministic and local: no model calls, warehouse queries or cloud resources.
Screenshots / recordings
Not a UI change.
Checklist
🤖 Generated with Claude Code