Skip to content
216 changes: 206 additions & 10 deletions packages/opencode/src/altimate/review/ai-review.ts

Large diffs are not rendered by default.

25 changes: 22 additions & 3 deletions packages/opencode/src/altimate/review/ai-verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export interface AiVerifyEvidence {
prBody?: string
/** Head-side line numbers each changed file adds (see `addedLinesByFile`). */
addedLines: Map<string, Set<number>>
/** Every file the PR changes, repo-relative, including tests and CI outside the dbt project. */
prFiles?: string[]
}

export type Decision = "keep" | "discard" | "summary"
Expand Down Expand Up @@ -379,12 +381,27 @@ export function hunkAround(diff: string | undefined, line?: number): string {
: text || "(the line is outside the diff)"
}

/** Most PR file paths listed in a verifier message. */
const PR_FILES_MAX = 40

/** The user message for one batch: the PR title and each candidate with its hunk. Not the whole file, not the grounding. */
export function buildVerifyMessage(
prTitle: string | undefined,
candidates: Array<{ id: string; finding: Finding; diff?: string }>,
prFiles: string[] = [],
): string {
const parts = [`PR: ${(prTitle ?? "(untitled)").slice(0, 200)}`, "", "Candidates:"]
const parts = [`PR: ${(prTitle ?? "(untitled)").slice(0, 200)}`]
// A test-coverage claim is checked against the PR's own test files, which may sit outside the dbt project.
if (prFiles.length && candidates.some((c) => c.finding.category === "test_coverage")) {
const shown = prFiles.slice(0, PR_FILES_MAX)

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 verifier cannot discover changed files after the first 40 paths, so a test added at path 41+ can be missed by the test-gap check; add a bounded file-list tool or otherwise expose all paths to the verifier.

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-verify.ts, line 395:

<comment>The verifier cannot discover changed files after the first 40 paths, so a test added at path 41+ can be missed by the test-gap check; add a bounded file-list tool or otherwise expose all paths to the verifier.</comment>

<file context>
@@ -378,12 +380,27 @@ export function hunkAround(diff: string | undefined, line?: number): string {
+  const parts = [`PR: ${(prTitle ?? "(untitled)").slice(0, 200)}`]
+  // A test-coverage claim is checked against the PR's own test files, which may sit outside the dbt project.
+  if (prFiles.length && candidates.some((c) => c.finding.category === "test_coverage")) {
+    const shown = prFiles.slice(0, PR_FILES_MAX)
+    parts.push(
+      "",
</file context>

parts.push(
"",
"Files this PR changes (read_file can open any of them):",
...shown.map((p) => `- ${p}`),
...(prFiles.length > shown.length ? [`- … and ${prFiles.length - shown.length} more`] : []),
)
}
parts.push("", "Candidates:")
for (const { id, finding: f, diff } of candidates) {
parts.push(
"",
Expand Down Expand Up @@ -764,6 +781,7 @@ export function verifierTools(
...ev.addedLines.keys(),
...graph.parents.map((p) => p.path),
...graph.downstream.map((d) => d.path),
...(ev.prFiles ?? []),

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 verifier's read allowlist now includes every PR file, including paths the user excluded with exclude or rubric excludeGlobs and out-of-project files. prFiles comes from the unfiltered collected list in run.ts, so those contents can reach the model provider. Build prFiles from the reviewable set after exclusions, or filter the allowlist with the same exclusion rules.

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-verify.ts, line 783:

<comment>The verifier's read allowlist now includes every PR file, including paths the user excluded with `exclude` or rubric `excludeGlobs` and out-of-project files. `prFiles` comes from the unfiltered `collected` list in run.ts, so those contents can reach the model provider. Build `prFiles` from the reviewable set after exclusions, or filter the allowlist with the same exclusion rules.</comment>

<file context>
@@ -763,6 +780,7 @@ export function verifierTools(
       ...ev.addedLines.keys(),
       ...graph.parents.map((p) => p.path),
       ...graph.downstream.map((d) => d.path),
+      ...(ev.prFiles ?? []),
     ]),
   ]
</file context>

]),
]
/** A requested path inside the review scope, as the repo-relative path to read; a project-relative path resolves too. */
Expand All @@ -779,11 +797,11 @@ export function verifierTools(
graph.parents.find((p) => p.name === name && (p.kind === "model" || p.kind === "snapshot"))?.path ??
graph.downstream.find((d) => d.name === name)?.path
const side = z.enum(["old", "new"]).optional().describe("old=base, new=head (default)")
const scope = NA("outside review scope (changed files, their dbt parents and direct downstream models only)")
const scope = NA("outside review scope (files the PR changes, their dbt parents and direct downstream models only)")

return {
read_file: {
description: "Up to 200 numbered lines of a changed file, its dbt parents or direct children.",
description: "Up to 200 numbered lines of any file the PR changes (tests and CI included), a dbt parent or direct child.",
input: z.object({
path: z.string(),
side,
Expand Down Expand Up @@ -1158,6 +1176,7 @@ export async function runAiVerify(
finding: input.findings[i],
diff: fileByPath.get(input.findings[i].file)?.diff,
})),
ev.prFiles,
)
return { idx, ids, message }
})
Expand Down
4 changes: 3 additions & 1 deletion packages/opencode/src/altimate/review/dbt-patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,9 @@ export function redundantJoinPredicates(sql: string): string[] {
} 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 })
// A join with no ON (`cross join params as p`): step over its alias so the chain continues.
const bare = /^\s+(?:as\s+)?(?!(?:natural|inner|left|right|full|cross|outer|join|lateral|on|using|where|group|order|qualify|having|union|limit|window|select|from)\b)\w+\b/i.exec(s.slice(i))

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 join ... using (...) has no ON clause, but this change does not step over it, so the join chain still breaks there and a redundant predicate on the next join is missed. Let the bare-join match also consume a USING (...) list.

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

<comment>A `join ... using (...)` has no ON clause, but this change does not step over it, so the join chain still breaks there and a redundant predicate on the next join is missed. Let the bare-join match also consume a `USING (...)` list.</comment>

<file context>
@@ -929,7 +929,9 @@ export function redundantJoinPredicates(sql: string): string[] {
     if (!tail) {
-      joins.push({ preds: [], start: m.index!, end: i, inner })
+      // A join with no ON (`cross join params as p`): step over its alias so the chain continues.
+      const bare = /^\s+(?:as\s+)?(?!(?:natural|inner|left|right|full|cross|outer|join|lateral|on|using|where|group|order|qualify|having|union|limit|window|select|from)\b)\w+\b/i.exec(s.slice(i))
+      joins.push({ preds: [], start: m.index!, end: i + (bare?.[0].length ?? 0), inner })
       continue
</file context>
Suggested change
const bare = /^\s+(?:as\s+)?(?!(?:natural|inner|left|right|full|cross|outer|join|lateral|on|using|where|group|order|qualify|having|union|limit|window|select|from)\b)\w+\b/i.exec(s.slice(i))
const bare = /^\s+(?:as\s+)?(?!(?:natural|inner|left|right|full|cross|outer|join|lateral|on|using|where|group|order|qualify|having|union|limit|window|select|from)\b)\w+\b|^\s+using\s*\([^)]*\)/i.exec(s.slice(i))

joins.push({ preds: [], start: m.index!, end: i + (bare?.[0].length ?? 0), inner })
continue
}
const alias = tail[1] && tail[1].toLowerCase() !== "on" ? tail[1].toLowerCase() : rel?.[1].split(".").pop()?.toLowerCase()
Expand Down
8 changes: 8 additions & 0 deletions packages/opencode/src/altimate/review/diff-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ export function partitionChangedFiles(
return { reviewable, dropped }
}

/** True when a path is build output / vendored or matches one of the user's exclude globs:
* files the review must not read, even outside the reviewable set. */
export function isExcludedPath(path: string, extraExcludeGlobs: string[] = []): boolean {
const p = norm(path)
if (SKIP_DIR_PATTERNS.some((re) => re.test(p)) || SKIP_FILE_PATTERNS.some((re) => re.test(p))) return true
return extraExcludeGlobs.some((g) => globToRegExp(g).test(p))
}

/** Filter changed files down to the reviewable set, with their dbt kind. */
export function filterChangedFiles(
files: ChangedFile[],
Expand Down
26 changes: 24 additions & 2 deletions packages/opencode/src/altimate/review/orchestrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
type NotReviewed,
partitionChangedFiles,
hasOldSide,
isExcludedPath,
walkDiffLines,
walkDiffRows,
} from "./diff-filter"
Expand Down Expand Up @@ -285,6 +286,8 @@ export interface OrchestrateInput {
aiVerify?: boolean
/** Fixed-string search over the dbt project at head, for the verifier. */
grepProject?: AiVerifyEvidence["grepProject"]
/** Every file the PR changes (repo-relative), including files outside the dbt project; the verifier may read them. */
prFiles?: string[]
/** PR metadata passed to the AI reviewer for intent checking. */
prTitle?: string
prBody?: string
Expand All @@ -298,6 +301,8 @@ export interface OrchestrateInput {
layout?: DbtProjectLayout
/** Names `ref()` can resolve to at head (repo resources + installed packages); undefined = unknown. */
resourceNames?: () => Promise<Set<string> | undefined>
/** `{% docs %}` names installed packages define; undefined when that can't be known. */
packageDocNames?: () => Promise<Set<string> | undefined>
/** What the review covered: base resolution and files left out of scope. */
scope?: Omit<ReviewScope, "unverified" | "notReviewed"> & { notReviewed?: NotReviewed[] }
/** Drop the regex catalog's cross-engine portability findings (single known engine). */
Expand Down Expand Up @@ -2352,20 +2357,31 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
// dozens of yml files; serial `git show` per file adds up).
// A `{% docs name %}` block in any of the project's .md files at head (cached per name).
const docLookups = new Map<string, Promise<boolean>>()
// Docs blocks of installed packages (cached); undefined when they can't be known.
let pkgDocs: Promise<Set<string> | undefined> | undefined
const packageDocs = () => (pkgDocs ??= input.packageDocNames?.().catch(() => undefined) ?? Promise.resolve(undefined))
const docExists = (name: string) => {
if (!docLookups.has(name)) {
const block = new RegExp(`\\{%-?\\s*docs\\s+${name}\\s*-?%\\}`)
docLookups.set(
name,
(input.grepProject?.(`docs ${name}`, "*.md") ?? Promise.resolve([]))
// An unavailable search (undefined) is no evidence the block is missing.
.then((hits) => hits === undefined || hits.some((h) => block.test(h.text)))
.then(async (hits) => hits === undefined || hits.some((h) => block.test(h.text)) || !!(await packageDocs())?.has(name))
// A failed search is no evidence the block is missing.
.catch(() => true),
)
}
return docLookups.get(name)!
}
// The project defines docs blocks of its own (any `{% docs x %}` in its .md files), and its
// installed packages' blocks are known: a package may define the name otherwise.
let docsAny: Promise<boolean> | undefined
const projectHasDocs = () =>
(docsAny ??= (input.grepProject?.("docs", "*.md") ?? Promise.resolve([]))

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: the generic docs pattern can exhaust the search cap before reaching a docs block

grepFixedAt stops after 50 total hits (git.ts:194-210; -m 50 per file), and docs shows up in ordinary markdown prose, so those hits can be used up before the file holding {% docs ... %} is scanned. projectHasDocs then returns false and the new doc-block-missing finding stays silent — the recall gap this change targets. A block-specific fixed string (e.g. separate searches for {% docs and {%- docs) would keep unrelated prose from diluting the result.

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 generic docs search can exhaust grepFixedAt’s 50-hit limit on Markdown prose before reaching a docs block, causing projectHasDocs() to suppress missing-doc() findings. Use a targeted, complete docs-block search instead.

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

<comment>The generic `docs` search can exhaust `grepFixedAt`’s 50-hit limit on Markdown prose before reaching a docs block, causing `projectHasDocs()` to suppress missing-`doc()` findings. Use a targeted, complete docs-block search instead.</comment>

<file context>
@@ -2279,6 +2281,13 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
+  // The project defines docs blocks of its own (any `{% docs x %}` in its .md files).
+  let docsAny: Promise<boolean> | undefined
+  const projectHasDocs = () =>
+    (docsAny ??= (input.grepProject?.("docs", "*.md") ?? Promise.resolve([]))
+      .then((hits) => hits.some((h) => /\{%-?\s*docs\s+\w+\s*-?%\}/.test(h.text)))
+      // A failed search is no evidence: stay silent.
</file context>

.then(async (hits) => !!hits?.some((h) => /\{%-?\s*docs\s+\w+\s*-?%\}/.test(h.text)) && (await packageDocs()) !== undefined)
// A failed or unavailable search is no evidence: stay silent.
.catch(() => false))
const schemaFiles = reviewable.filter((f) => f.kind === "schema_yml")
touch(schemaFiles)
const sqlByModel = new Map([...ctxByPath.values()].filter((c) => c.newSql).map((c) => [modelNameFromPath(c.file.path).toLowerCase(), c.newSql!]))
Expand All @@ -2384,7 +2400,7 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
...detectUnitTestFixtures(file, oldContent, newContent),
...snapshotYmlFindings(file, oldContent, newContent),
...detectTestAdequacy({ file, oldContent, newContent, projectYml, layout: input.layout }),
...(lanes.has("dbt_patterns") && input.grepProject ? await docRefFindings(file, oldContent, newContent, docExists) : []),
...(lanes.has("dbt_patterns") && input.grepProject ? await docRefFindings(file, oldContent, newContent, docExists, projectHasDocs) : []),
]
} catch (err) {
// One malformed schema file degrades that file only.
Expand Down Expand Up @@ -2571,6 +2587,10 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
touch(aiFiles)
if (input.aiContextCard && anyManifest) await attachContextCards(aiFiles, input.runner)
const context = input.aiGraphContext ? await aiGraphContext(aiFiles.map((f) => f.path), input.runner, getContent) : undefined
// Files the PR changes that the AI may list and the verifier may read; never the user's exclusions.
const prFiles = (input.prFiles ?? input.changedFiles.map((f) => f.path)).filter(
(p) => !isExcludedPath(p, input.rubric.exclusions.excludeGlobs),
)
// The verifier reads engine SQL first (the same SQL the engine lanes saw), then compiled output.
const evidence: AiVerifyEvidence | undefined = input.aiVerify
? {
Expand All @@ -2582,6 +2602,7 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
prTitle: input.prTitle,
prBody: input.prBody,
grepProject: input.grepProject,
prFiles,
readFile: async (p, side, start, end) => numberedSlice(await getContent?.(p, side), start, end),
compiledSql: async (p, side) => {
const ctx = ctxByPath.get(p)
Expand All @@ -2598,6 +2619,7 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
prTitle: input.prTitle,
prBody: input.prBody,
model: input.aiModel,
prFiles,
...(evidence ? { evidence } : {}),
})
const aiFindings = Array.isArray(res) ? res : res.findings
Expand Down
20 changes: 14 additions & 6 deletions packages/opencode/src/altimate/review/project-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,11 @@ export function yamlParseFindings(file: ChangedFile, oldContent: string | undefi
if (!/\.ya?ml$/i.test(file.path) || file.status === "deleted" || !newContent) return []
const err = yamlError(newContent)
if (!err || (oldContent !== undefined && yamlError(oldContent))) return []
const text = err.line ? newContent.split("\n")[err.line - 1] : undefined
// Some errors carry no line, or one past the edit (an unterminated quote fails where the scan
// ends): anchor on the added line at or before it, else the first added line.
const added = [...walkDiffRows(file.diff)].filter((r) => r.kind === "+")
const at = (err.line ? added.find((r) => r.line === err.line) ?? [...added].reverse().find((r) => r.line < err.line!) : undefined) ?? added[0]
const text = at?.text ?? (err.line ? newContent.split("\n")[err.line - 1] : undefined)
const f = finding(
file,
"yaml-parse-error",
Expand All @@ -402,7 +406,7 @@ export function yamlParseFindings(file: ChangedFile, oldContent: string | undefi
: `${err.message}${err.line ? ` (line ${err.line})` : ""}. The file parsed before this change; tools that read it now fail.`,
text,
)
return [err.line ? { ...f, startLine: err.line, endLine: err.line } : f]
return [at ? { ...f, startLine: at.line, endLine: at.line } : f]
}

/** New models named like the public models a `.quickstart/quickstart.yml` lists, but missing from
Expand Down Expand Up @@ -477,14 +481,17 @@ export function packageLockFindings(file: ChangedFile, isPackage: boolean): Find

const DOC_REF = /\bdoc\s*\(\s*['"](\w+)['"]\s*\)/g

/** `doc('x')` names this change adds to a yml file that no docs block in the project defines:
* dbt fails to parse the project. Only when another `doc()` in the file resolves in the project,
* so docs kept in an installed package aren't mistaken for missing ones. */
/** `doc('x')` names this change adds to a yml file that no docs block in the project or its
* installed packages defines: dbt fails to parse the project. Only when another `doc()` in the
* file resolves, or, when the file has no other `doc()`, the project keeps its own docs blocks
* (`projectHasDocs` also requires the packages' blocks to be known); so docs kept in an
* uninstalled package aren't mistaken for missing ones. */
export async function docRefFindings(
file: ChangedFile,
oldContent: string | undefined,
newContent: string | undefined,
docExists: (name: string) => Promise<boolean>,
projectHasDocs?: () => Promise<boolean>,
): Promise<Finding[]> {
if (!newContent || file.status === "deleted") return []
const before = new Set([...(oldContent ?? "").matchAll(DOC_REF)].map((m) => m[1]))
Expand All @@ -497,6 +504,7 @@ export async function docRefFindings(
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
if (!others.length && projectHasDocs) resolves = await projectHasDocs()

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: projectHasDocs() cannot tell a project docs block from one that resolves in an installed package

docExists only greps the dbt project's own paths (grepProject pathspecs come from the dbt_project.yml model/macro/seed/snapshot/test dirs, run.ts:431-438), so a doc('name') defined by an installed package already counts as "missing". This new fallback then treats any docs block anywhere in the project as proof that the name resolves, so for a file whose only doc() is the new one (typically a newly added schema yml) a package-provided doc is reported as "dbt fails to parse" — exactly the package case the guard above was added to avoid. The finding body also says "the other doc() references in this file resolve there", which is false on this branch (there are none). Consider also searching installed-package .md files before concluding the name is missing, or keep the previous "another doc() in this file resolves" requirement for names that could come from a package.

Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
if (!resolves) return []
return missing.map((name) =>
finding(
Expand All @@ -505,7 +513,7 @@ export async function docRefFindings(
"warning",
"sql_correctness",
`\`doc('${name}')\` has no docs block`,
`No \`{% docs ${name} %}\` block exists in the project's \`.md\` files, while the other \`doc()\` references in this file resolve there. dbt fails to parse the project on a \`doc()\` it can't resolve. Add the docs block, or fix the name.`,
`No \`{% docs ${name} %}\` block exists in the project's \`.md\` files or its installed packages, while ${others.length ? "the other \`doc()\` references in this file resolve" : "the project defines its own docs blocks"}. dbt fails to parse the project on a \`doc()\` it can't resolve. Add the docs block, or fix the name.`,
lines.get(name),
),
)
Expand Down
5 changes: 4 additions & 1 deletion packages/opencode/src/altimate/review/rubric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ export function blockingCategories(rubric: Rubric): Set<string> {
* be dropped, or null to keep it. Centralizing this here (vs prompt text) is
* what keeps the false-positive rate down deterministically.
*/
/** Findings that break `dbt parse` for the whole project, so a dev/sandbox/scratch folder doesn't excuse them. */

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: the exclusionReason JSDoc now documents this constant

The /** Apply the rubric's exclusion predicates ... */ block above now attaches to PROJECT_BREAKING_RULES, so exclusionReason loses its doc and editors/TS show the wrong description for it. Move the constant above the JSDoc, or give the constant its own comment.

const PROJECT_BREAKING_RULES = new Set(["dbt-project-rules:yaml-parse-error", "dbt-project-rules:doc-block-missing"])

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: Move PROJECT_BREAKING_RULES above the exclusionReason JSDoc or give the constant its own comment so the predicate documentation remains attached to the function.

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/rubric.ts, line 79:

<comment>Move `PROJECT_BREAKING_RULES` above the `exclusionReason` JSDoc or give the constant its own comment so the predicate documentation remains attached to the function.</comment>

<file context>
@@ -75,12 +75,15 @@ export function blockingCategories(rubric: Rubric): Set<string> {
  * what keeps the false-positive rate down deterministically.
  */
+/** Findings that break `dbt parse` for the whole project, so a dev/sandbox/scratch folder doesn't excuse them. */
+const PROJECT_BREAKING_RULES = new Set(["dbt-project-rules:yaml-parse-error", "dbt-project-rules:doc-block-missing"])
+
 export function exclusionReason(finding: Finding, rubric: Rubric): string | null {
</file context>


export function exclusionReason(finding: Finding, rubric: Rubric): string | null {
const ex = rubric.exclusions
const isStaging = /(^|\/)stg_|(^|\/)staging\//.test(finding.file) || finding.model?.startsWith("stg_")
const isDev = /(^|\/)(dev|sandbox|scratch)\//.test(finding.file)

if (ex.skipNonProdModels && isDev) return "non-prod model (dev/sandbox/scratch)"
if (ex.skipNonProdModels && isDev && !PROJECT_BREAKING_RULES.has(finding.rule ?? "")) return "non-prod model (dev/sandbox/scratch)"

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: ref-target-missing is still dropped in dev/sandbox/scratch folders even though it breaks dbt parse

This exemption only keeps yaml-parse-error and doc-block-missing, but missingRefFindings emits dbt-project-rules:ref-target-missing, whose own body says "so dbt fails to parse the project" (project-rules.ts:828). skipNonProdModels defaults to true, so a ref('typo') added under models/dev/, sandbox/ or scratch/ is still filtered out here — the exact case the new constant is meant to stop. Add dbt-project-rules:ref-target-missing to the set (and jinja-unbalanced, which breaks compilation of every dependent model, if that is in scope for this fix).


if (
ex.allowSelectStarInStaging &&
Expand Down
3 changes: 2 additions & 1 deletion packages/opencode/src/altimate/review/rule-catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,9 @@ const CORRECTNESS: Rule[] = [
{
// 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).
// Single- or double-quoted; an escape (`\n`) at the seam is not a word.
added:
/\{[{%](?:(?![}%]\}).)*?(?:'[^'\n]*\s[^'\n]*[A-Za-z0-9]'\s*~\s*[A-Za-z_][\w.]*|[\w)]\s*~\s*'[A-Za-z0-9][^'\n]*\s[^'\n]*')/,
/\{[{%](?:(?![}%]\}).)*?(?:'[^'\n]*\s[^'\n]*(?<!\\)[A-Za-z0-9]'\s*~\s*[A-Za-z_][\w.]*|[\w)]\s*~\s*'[A-Za-z0-9][^'\n]*\s[^'\n]*'|"[^"\n]*\s[^"\n]*(?<!\\)[A-Za-z0-9]"\s*~\s*[A-Za-z_][\w.]*|[\w)]\s*~\s*"[A-Za-z0-9][^"\n]*\s[^"\n]*")/,
// Messages are not SQL.
unless: /\b(?:log|print|raise_compiler_error|warn)\s*\(/,
macros: true,
Expand Down
37 changes: 37 additions & 0 deletions packages/opencode/src/altimate/review/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise

return runReview({
resourceNames: () => projectResourceNames(opts.cwd, opts.head, projectRootRel, dbtRoot),
packageDocNames: () => packageDocNames(opts.cwd, opts.head, projectRootRel, dbtRoot),
changedFiles,
config,
rubric,
Expand Down Expand Up @@ -460,6 +461,7 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise
const specs = projectGrepSpecs(projectRootRel, dirs, glob)
return specs ? grepFixedAt(gitRoot, opts.head, pattern, specs) : Promise.resolve(undefined)
},
prFiles: collected.map((f) => f.path),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: prFiles includes deleted files, so a PR that removes a test file still shows that test under "PR's other changed paths" and the test-gap check may treat it as coverage. Filter out status === "deleted" before mapping to paths.

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/run.ts, line 440:

<comment>`prFiles` includes deleted files, so a PR that removes a test file still shows that test under "PR's other changed paths" and the test-gap check may treat it as coverage. Filter out `status === "deleted"` before mapping to paths.</comment>

<file context>
@@ -437,6 +437,7 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise
       const specs = glob ? [`${root}${glob}`] : dirs.map((d) => path.posix.normalize(root + d))
       return grepFixedAt(gitRoot, opts.head, pattern, specs.filter((d) => !d.startsWith("..")))
     },
+    prFiles: collected.map((f) => f.path),
     prTitle: opts.prTitle,
     prBody: opts.prBody,
</file context>
Suggested change
prFiles: collected.map((f) => f.path),
prFiles: collected.filter((f) => f.status !== "deleted").map((f) => f.path),

prTitle: opts.prTitle,
prBody: opts.prBody,
explainTier: opts.explainTier,
Expand Down Expand Up @@ -599,6 +601,41 @@ export async function projectAdapters(
return [...seen].sort()
}

/**
* `{% docs name %}` blocks defined in the `.md` files of installed packages (`dbt_packages/`). Empty when the
* project declares no packages; undefined when that can't be known (no project root, git can't
* list the tree, or packages are declared but not installed).
*/
export async function packageDocNames(
cwd: string,
head: string | undefined,
projectRootRel: string | undefined,
dbtRoot: string,
): Promise<Set<string> | undefined> {
if (projectRootRel === undefined) return undefined
const files = await listFilesAt(cwd, head)
if (!files) return undefined
const prefix = projectRootRel ? projectRootRel + "/" : ""
const names = new Set<string>()
if (!files.includes(prefix + "packages.yml") && !files.includes(prefix + "dependencies.yml")) return names

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: a working-tree review treats an uncommitted packages.yml as "no packages"

listFilesAt(cwd, head) falls back to git ls-tree HEAD when head is undefined, so a newly added/uncommitted packages.yml is not in files and this returns an empty set. Callers treat an empty set as authoritative (projectHasDocs then assumes the packages' blocks are known), while dbt_packages below is read from the working tree, so a doc() supplied by an installed package is reported missing. Mirror projectResourceNames, which merges listWorkingTreeFiles when head === undefined (run.ts:655), or return undefined here instead of an empty set.

const walk = async (dir: string, depth: number): Promise<boolean> => {
let entries
try {
entries = await readdir(dir, { withFileTypes: true })
} catch {
return false
}
for (const e of entries) {
const p = path.join(dir, e.name)
if (e.isDirectory() && depth < 12 && e.name !== ".git") await walk(p, depth + 1)
else if (e.isFile() && e.name.endsWith(".md"))
for (const m of (await readFile(p, "utf8").catch(() => "")).matchAll(/\{%-?\s*docs\s+(\w+)\s*-?%\}/g)) names.add(m[1])
}
return true
}
return (await walk(path.join(dbtRoot, "dbt_packages"), 0)) ? names : undefined
}

/**
* Names `ref()` can resolve to: model/seed/snapshot files in the repo at head, snapshots
* defined in YAML, and
Expand Down
2 changes: 2 additions & 0 deletions packages/opencode/src/altimate/review/verdict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ export const VerdictEnvelope = z.object({
hunksSent: z.number().int().nonnegative(),
hunksTotal: z.number().int().nonnegative(),
card: z.boolean(),
/** Index of the call that carried the file (focused batching only). */
batch: z.number().int().nonnegative().optional(),
}),
)
.optional(),
Expand Down
Loading
Loading