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 @@ -281,8 +281,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 @@ -364,8 +366,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 @@ -591,6 +595,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,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Follow-up: low priority, tracked (wording will note deleted and analysis files are excluded).

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
10 changes: 4 additions & 6 deletions packages/opencode/src/altimate/native/altimate-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ import { EngineCoerce } from "./engine-coerce"
import { register } from "./dispatcher"
import { schemaOrEmpty, SchemaResolver, prepareSql } from "./schema-resolver"
import type { AltimateCoreResult } from "./types"
// altimate_change start — dependency-free so the review runner can use it without loading the binding
import { isExistenceError } from "./existence-error"
// altimate_change end

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/** Spread a rich TypeScript object into a plain Record for the data field. */
// altimate_change start — the engine's existence findings, which mean nothing without a schema
const EXISTENCE_CODES = new Set(["E001", "E002"])
const EXISTENCE_KINDS = new Set(["TableNotFound", "ColumnNotFound"])
export function isExistenceError(err: unknown): boolean {
const e = err as { code?: unknown; kind?: { type?: unknown } } | null
return EXISTENCE_CODES.has(String(e?.code)) || EXISTENCE_KINDS.has(String(e?.kind?.type))
}
export { isExistenceError }
// altimate_change end

// altimate_change start — fail closed when the engine skips plan-based analysis
Expand Down
10 changes: 10 additions & 0 deletions packages/opencode/src/altimate/native/existence-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* The engine's existence findings, which mean nothing without a schema.
* Dependency-free so callers can use it without loading the native core binding.
*/
const EXISTENCE_CODES = new Set(["E001", "E002"])
const EXISTENCE_KINDS = new Set(["TableNotFound", "ColumnNotFound"])
export function isExistenceError(err: unknown): boolean {
const e = err as { code?: unknown; kind?: { type?: unknown } } | null
return EXISTENCE_CODES.has(String(e?.code)) || EXISTENCE_KINDS.has(String(e?.kind?.type))
}
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 @@ -20,13 +20,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>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Follow-up: low priority, tracked.

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 @@ -85,7 +117,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 @@ -101,9 +133,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 @@ -122,8 +158,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 controller = new AbortController()
let timeoutMs = AI_TIMEOUT_BASE_MS
Expand Down
Loading
Loading