diff --git a/packages/opencode/src/altimate/review/ai-context-card.ts b/packages/opencode/src/altimate/review/ai-context-card.ts new file mode 100644 index 0000000000..283684f5b3 --- /dev/null +++ b/packages/opencode/src/altimate/review/ai-context-card.ts @@ -0,0 +1,139 @@ +// altimate_change - new file: dbt manifest context card for the AI lane +// +// Opt-in (`ALTIMATE_REVIEW_AI_CONTEXT_CARD=1`, needs a manifest): each file +// sent to the AI reviewer carries a short block of manifest facts the diff does +// not show — materialization and keys, parents and children, declared columns +// and their tests, sibling models; for a macro, its callers and adapter variants. +import type { AiReviewFile } from "./ai-review" + +/** Manifest facts about one model, snapshot or seed. */ +export interface ModelCard { + resourceType: string + materialized?: string + uniqueKey?: string + incrementalStrategy?: string + partitionBy?: string + clusterBy?: string + contractEnforced: boolean + /** Direct parents: model/seed/snapshot names and `source.table` names. */ + parents: string[] + /** Direct downstream models. */ + children: string[] + exposures: string[] + /** Columns declared in yml (at most 25), with the tests attached to each. */ + columns: Array<{ name: string; dataType?: string; tests: string[] }> + /** Other models in the same directory sharing at least one direct parent (at most 8). */ + siblings: string[] +} + +/** Manifest facts about one macro. */ +export interface MacroCard { + /** The macro the card describes (resolved by file path in the project, else by name). */ + name: string + /** Models whose `depends_on.macros` include it (at most 10, models changed in the PR first). */ + callers: string[] + /** The dispatcher `` and other `__` macros, as `name (path)`. */ + variants: string[] +} + +export type ContextCard = { name: string; model: ModelCard } | { name: string; macro: MacroCard } + +/** Characters of context card per file and per model call. */ +export const CARD_MAX_CHARS = 600 +export const CARD_BATCH_MAX_CHARS = 6_000 + +/** List caps tried in order until the card fits. */ +const LIST_CAPS = [25, 10, 6, 3, 1, 0] + +function list(items: string[], cap: number): string { + const shown = items.slice(0, cap).join(", ") + const more = items.length - Math.min(cap, items.length) + return more > 0 ? `${shown}${shown ? " " : ""}+${more} more` : shown +} + +function renderOne(card: ContextCard, cap: number): string[] { + if ("macro" in card) { + const m = card.macro + const lines = [`macro \`${m.name}\``] + if (m.callers.length) lines.push(`callers: ${list(m.callers, cap)}`) + if (m.variants.length) lines.push(`variants: ${list(m.variants, cap)}`) + return lines + } + const m = card.model + const attrs = [ + m.materialized && `materialized: ${m.materialized}`, + m.incrementalStrategy && `strategy: ${m.incrementalStrategy}`, + m.uniqueKey && `unique_key: ${m.uniqueKey}`, + m.partitionBy && `partition_by: ${m.partitionBy}`, + m.clusterBy && `cluster_by: ${m.clusterBy}`, + m.contractEnforced && "contract: enforced", + ].filter(Boolean) + const lines = [`${m.resourceType} \`${card.name}\`${attrs.length ? `: ${attrs.join("; ")}` : ""}`] + if (m.parents.length) lines.push(`parents: ${list(m.parents, cap)}`) + if (m.children.length) lines.push(`children: ${list(m.children, cap)}`) + if (m.exposures.length) lines.push(`exposures: ${list(m.exposures, cap)}`) + if (m.columns.length) { + const cols = m.columns.map((c) => `${c.name}${c.dataType ? ` ${c.dataType}` : ""}${c.tests.length ? ` [${c.tests.join(", ")}]` : ""}`) + lines.push(`columns: ${list(cols, cap)}`) + } + if (m.siblings.length) lines.push(`siblings: ${list(m.siblings, cap)}`) + return lines +} + +/** The card text for one file (one or more cards), at most `maxChars`; lists shrink to `+N more` first. */ +export function renderContextCard(card: ContextCard | ContextCard[], maxChars = CARD_MAX_CHARS): string { + const cards = Array.isArray(card) ? card : [card] + if (!cards.length) return "" + let text = "" + for (const cap of LIST_CAPS) { + text = cards.flatMap((c) => renderOne(c, cap)).join("\n") + if (text.length <= maxChars) return text + } + return text.slice(0, maxChars - 1) + "…" +} + +const MODEL_KINDS = new Set(["model_sql", "python_model", "snapshot", "seed"]) +const DECLARING_KINDS = new Set(["schema_yml", "source_yml", "test"]) + +interface CardSource { + modelCard?(name: string, opts?: { path?: string }): Promise + patchedModels?(path: string): Promise + macroCard?(name: string, opts?: { path?: string; changedModels?: string[] }): Promise + graphParents?(paths: string[]): Promise> +} + +/** + * Attach a rendered card to each file the manifest knows: models, snapshots and + * seeds get their own card, macros theirs, a yml or singular test the cards of the + * models it declares (`patch_path`) or tests (at most 3). Files without facts get none. Never throws. + */ +export async function attachContextCards(files: AiReviewFile[], runner: CardSource): Promise { + const changedModels = files.filter((f) => MODEL_KINDS.has(f.kind ?? "model_sql")).map((f) => f.model) + const cardsFor = async (f: AiReviewFile): Promise => { + const kind = f.kind ?? "model_sql" + if (MODEL_KINDS.has(kind)) { + const model = await runner.modelCard?.(f.model, { path: f.path }) + return model ? [{ name: f.model, model }] : [] + } + if (kind === "macro") { + const macro = await runner.macroCard?.(f.model, { path: f.path, changedModels }) + return macro ? [{ name: macro.name, macro }] : [] + } + if (!DECLARING_KINDS.has(kind)) return [] + const [declared, parents] = await Promise.all([ + runner.patchedModels?.(f.path) ?? Promise.resolve([]), + runner.graphParents?.([f.path]) ?? Promise.resolve([]), + ]) + const tested = (parents ?? []).filter((p) => p.kind === "model").map((p) => p.name) + const names = [...new Set([...(declared ?? []), ...tested])].slice(0, 3) + const cards = await Promise.all(names.map(async (name) => ({ name, model: await runner.modelCard?.(name) }))) + return cards.flatMap((c) => (c.model ? [{ name: c.name, model: c.model }] : [])) + } + await Promise.all( + files.map(async (f) => { + const cards = await cardsFor(f).catch(() => []) + const text = renderContextCard(cards) + if (text) f.card = text + }), + ) +} diff --git a/packages/opencode/src/altimate/review/ai-review.ts b/packages/opencode/src/altimate/review/ai-review.ts index 3913109938..2e8419cbe5 100644 --- a/packages/opencode/src/altimate/review/ai-review.ts +++ b/packages/opencode/src/altimate/review/ai-review.ts @@ -11,6 +11,8 @@ import { type Finding, type ReviewCategory, type Severity, makeFinding, ruleId } import { readStreamText } from "../llm/stream-text" import { type AiVerifyEvidence, type VerifierStatus, aiVerifyEnabled, runAiVerify } from "./ai-verify" import { aiReplayPath, runAiReplay } from "./ai-replay" +import { walkDiffLines } from "./diff-filter" +import { CARD_BATCH_MAX_CHARS } from "./ai-context-card" const log = Log.create({ service: "ai-review" }) @@ -21,6 +23,17 @@ const MAX_FILES = 20 const CONTEXT_FILE_CHARS = 8_000 const CONTEXT_TOTAL_CHARS = 40_000 const BATCH_CONCURRENCY = 2 +/** Per-kind file quotas under `ALTIMATE_REVIEW_AI_COVERAGE=1`. */ +const COVERAGE_QUOTAS = { model: 12, macro: 8, test: 4, yml: 6 } +/** Head-side lines of context kept around each changed line in a file excerpt. */ +const EXCERPT_CONTEXT_LINES = 20 +/** Characters of a file's diff section under coverage (hunks beyond it are listed as omitted). */ +const COVERAGE_DIFF_CHARS = 16_000 +/** Current-file head kept under coverage when the diff section already carries every hunk of a long diff. */ +const COVERAGE_FULL_DIFF_FILE_CHARS = 2_000 +/** Default per-call size under coverage (~30k tokens): 30 quota files at up to ~22k chars each would + * otherwise make one ~660k-char call that overruns common context windows. */ +const COVERAGE_BATCH_CHARS = 120_000 /** * How much of the PR the lane sends. Defaults keep one bounded call; the @@ -31,21 +44,48 @@ const BATCH_CONCURRENCY = 2 export interface AiInputBudget { maxFiles: number maxFileChars: number - /** Characters of file sections per call; Infinity keeps a single call. */ + /** Characters of file sections per call; Infinity keeps a single call (the default without coverage). */ batchChars: number + /** `ALTIMATE_REVIEW_AI_COVERAGE=1`: per-kind file quotas (replacing `maxFiles`) and + * hunk-centred excerpts instead of head truncation. Undefined keeps today's input. */ + coverage?: AiCoverageRender & { quotas: Record } } export function aiInputBudget(env: Record = process.env): AiInputBudget { const num = (v: string | undefined, d: number) => { const n = Number(v) return v && Number.isFinite(n) && n > 0 ? n : d } + // A quota of 0 disables that bucket. + const quota = (v: string | undefined, d: number) => { + const n = Number(v) + return v && Number.isInteger(n) && n >= 0 ? n : d + } + const coverageOn = env.ALTIMATE_REVIEW_AI_COVERAGE === "1" return { maxFiles: num(env.ALTIMATE_REVIEW_AI_MAX_FILES, MAX_FILES), maxFileChars: num(env.ALTIMATE_REVIEW_AI_MAX_FILE_CHARS, MAX_DIFF_CHARS), - batchChars: num(env.ALTIMATE_REVIEW_AI_BATCH_CHARS, Infinity), + batchChars: num(env.ALTIMATE_REVIEW_AI_BATCH_CHARS, coverageOn ? COVERAGE_BATCH_CHARS : Infinity), + coverage: + coverageOn + ? { + diffChars: num(env.ALTIMATE_REVIEW_AI_COVERAGE_DIFF_CHARS, COVERAGE_DIFF_CHARS), + quotas: { + model: quota(env.ALTIMATE_REVIEW_AI_QUOTA_MODEL, COVERAGE_QUOTAS.model), + macro: quota(env.ALTIMATE_REVIEW_AI_QUOTA_MACRO, COVERAGE_QUOTAS.macro), + test: quota(env.ALTIMATE_REVIEW_AI_QUOTA_TEST, COVERAGE_QUOTAS.test), + yml: quota(env.ALTIMATE_REVIEW_AI_QUOTA_YML, COVERAGE_QUOTAS.yml), + }, + } + : undefined, } } +/** `ALTIMATE_REVIEW_AI_DRYRUN=1`: plan the lane's input and record it on the + * status without calling a model (state `skipped`), for free coverage counts. */ +export function aiDryRunEnabled(env: Record = process.env): boolean { + return env.ALTIMATE_REVIEW_AI_DRYRUN === "1" +} + export interface AiReviewFile { path: string status: string @@ -57,6 +97,8 @@ export interface AiReviewFile { diff?: string /** Current content: dbt-compiled (preferred) or raw SQL for a model, the file itself otherwise. */ sql?: string + /** Rendered dbt manifest context card (`ALTIMATE_REVIEW_AI_CONTEXT_CARD=1`). */ + card?: string } /** Order under the file cap: models first, then the files whose defects reach the most models. */ @@ -78,26 +120,316 @@ function kindRank(kind: string | undefined): number { return i < 0 ? KIND_ORDER.length : i } +function sendable(f: AiReviewFile): boolean { + return f.status !== "deleted" && !!(f.diff || f.sql) +} + /** Files the lane sends, in priority order, capped at `maxFiles`. */ export function selectAiFiles(files: AiReviewFile[], maxFiles = MAX_FILES): AiReviewFile[] { return files - .filter((f) => f.status !== "deleted" && (f.diff || f.sql)) + .filter(sendable) .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, maxFiles) } -/** One changed file as it appears in the prompt. */ -function fileSection(f: AiReviewFile, maxChars: number): string { +/** Quota buckets for `ALTIMATE_REVIEW_AI_COVERAGE`. */ +export type AiFileBucket = keyof typeof COVERAGE_QUOTAS +const BUCKET_OF: Record = { + model_sql: "model", + python_model: "model", + snapshot: "model", + macro: "macro", + test: "test", + schema_yml: "yml", + source_yml: "yml", + project_config: "yml", + seed: "yml", +} +/** Buckets in the order their kinds first appear in KIND_ORDER (model, macro, test, yml). */ +const BUCKET_ORDER = [...new Set(KIND_ORDER.map((k) => BUCKET_OF[k]))] + +/** Added and removed lines of a unified diff (file headers before the first hunk excluded). */ +export function changedLineCount(diff: string | undefined): number { + if (!diff) return 0 + const lines = diff.split("\n") + const first = lines.findIndex((l) => l.startsWith("@@")) + return lines.slice(Math.max(first, 0)).filter((l) => l.startsWith("+") || l.startsWith("-")).length +} + +/** + * Files the lane sends under per-kind quotas, so models cannot crowd out the + * macros, tests and yml whose defects no model diff shows. Each bucket keeps its + * most-changed files (ties by input order); quota a bucket leaves unused goes to + * the other buckets in KIND_ORDER order, up to the sum of the quotas. `unsent` + * lists the sendable files left out, in input order. + */ +export function selectAiFilesByQuota( + files: AiReviewFile[], + quotas: Record = COVERAGE_QUOTAS, +): { sent: AiReviewFile[]; unsent: AiReviewFile[] } { + const eligible = files.filter(sendable) + const byBucket = new Map(BUCKET_ORDER.map((b) => [b, []])) + eligible + .map((f, i) => ({ f, i, n: changedLineCount(f.diff) })) + .sort((a, b) => b.n - a.n || a.i - b.i) + .forEach(({ f }) => byBucket.get(BUCKET_OF[f.kind ?? "model_sql"] ?? "model")!.push(f)) + const take = new Map(BUCKET_ORDER.map((b) => [b, Math.min(quotas[b], byBucket.get(b)!.length)])) + let spare = BUCKET_ORDER.reduce((n, b) => n + quotas[b], 0) - [...take.values()].reduce((n, t) => n + t, 0) + // Unused quota flows to other kinds, but never into a bucket disabled with quota 0. + for (const b of BUCKET_ORDER) { + if (quotas[b] <= 0) continue + const extra = Math.min(spare, byBucket.get(b)!.length - take.get(b)!) + take.set(b, take.get(b)! + extra) + spare -= extra + } + const sent = BUCKET_ORDER.flatMap((b) => byBucket.get(b)!.slice(0, take.get(b)!)) + const chosen = new Set(sent) + return { sent, unsent: eligible.filter((f) => !chosen.has(f)) } +} + +interface DiffHunk { + text: string + /** Head-side line range from the `@@` header; absent for a diff without headers. */ + start?: number + end?: number +} + +/** A unified diff split into the text before its first hunk and its hunks. */ +function splitHunks(diff: string): { preamble: string; hunks: DiffHunk[] } { + const lines = diff.replace(/\n$/, "").split("\n") + const first = lines.findIndex((l) => l.startsWith("@@")) + if (first < 0) return { preamble: "", hunks: lines.length && diff ? [{ text: lines.join("\n") }] : [] } + const hunks: DiffHunk[] = [] + let cur: string[] = [] + const flush = () => { + if (!cur.length) return + const m = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/.exec(cur[0]) + const start = m ? Number(m[1]) : undefined + const count = m ? (m[2] === undefined ? 1 : Number(m[2])) : 0 + hunks.push({ text: cur.join("\n"), start, end: start === undefined ? undefined : start + Math.max(count, 1) - 1 }) + cur = [] + } + for (const l of lines.slice(first)) { + if (l.startsWith("@@")) flush() + cur.push(l) + } + flush() + return { preamble: lines.slice(0, first).join("\n"), hunks } +} + +function lineRange(a: number, b: number): string { + return a === b ? `line ${a}` : `lines ${a}–${b}` +} + +/** What a file section carries of the file's diff and content. */ +interface SectionStats { + truncated: boolean + hunksSent: number + hunksTotal: number +} + +/** + * Whole hunks, in order, up to `maxChars`; a hunk is never cut unless it is the + * first and alone exceeds the budget (its head is kept, marked). Omitted hunks + * are listed by their head-side line ranges. + */ +export function excerptDiff(diff: string, maxChars: number): { text: string } & SectionStats { + const { preamble, hunks } = splitHunks(diff) + const out = preamble ? [preamble] : [] + let used = preamble.length + let sent = 0 + let cut = false + for (const h of hunks) { + if (used + h.text.length + 1 <= maxChars) { + out.push(h.text) + used += h.text.length + 1 + sent++ + continue + } + if (sent === 0) { + const head = h.text.slice(0, Math.max(0, maxChars - used)) + const nl = head.lastIndexOf("\n") + out.push((nl > 0 ? head.slice(0, nl) : head) + "\n… (hunk truncated)") + sent = 1 + cut = true + } + break + } + const omitted = hunks.slice(sent) + if (omitted.length) { + const ranges = omitted.map((h) => (h.start === undefined ? "?" : lineRange(h.start, h.end!).replace(/^lines? /, ""))) + out.push(`… (${omitted.length} more hunk${omitted.length === 1 ? "" : "s"} omitted, at lines ${ranges.join(", ")})`) + } + return { text: out.join("\n"), truncated: cut || omitted.length > 0, hunksSent: sent, hunksTotal: hunks.length } +} + +const COMPILED_UNLOCATED_MARKER = "\n… (compiled file: changed lines not located, head shown; windows would be approximate)" +const JINJA = /\{\{|\{%/ + +/** + * Where the diff's added lines sit in `lines` when the content is not the raw + * file (e.g. dbt-compiled SQL, whose line numbers drift once Jinja expands). + * Returns undefined when the raw line numbers already match (or nothing can be + * checked), the found line numbers when at least half of the eligible added + * lines (8+ non-space chars, no Jinja) are found by text, and "unlocated" otherwise. + * With no eligible added line (Jinja-only or very short changes), the context + * lines decide: raw line numbers are trusted only when those match. + */ +function locateAddedLines(lines: string[], rows: Array<{ line: number; text: string; kind: string }>): number[] | "unlocated" | undefined { + const isEligible = (r: { text: string }) => !JINJA.test(r.text) && r.text.replace(/\s/g, "").length >= 8 + const matchesRaw = (r: { line: number; text: string }) => lines[r.line - 1]?.trim() === r.text.trim() + const eligible = rows.filter((r) => r.kind === "+" && isEligible(r)) + if (!eligible.length) { + const context = rows.filter((r) => r.kind !== "+" && isEligible(r)) + return context.length && !context.every(matchesRaw) ? "unlocated" : undefined + } + if (eligible.every(matchesRaw)) return undefined + const found: number[] = [] + const used = new Set() + let from = 0 + for (const r of eligible) { + const want = r.text.trim() + let at = -1 + for (let i = from; i < lines.length; i++) + if (!used.has(i) && lines[i].trim() === want) { + at = i + break + } + if (at < 0) continue + used.add(at) + found.push(at + 1) + from = at + 1 + } + return found.length * 2 >= eligible.length ? found : "unlocated" +} + +/** + * The parts of a file around its changed lines (±20 head-side lines each, + * merged), up to `maxChars`, then as much of the file head as still fits. + * Gaps are marked with their real line numbers. Without changed lines the file + * head is kept, as before. When the content is not the raw file (compiled SQL), + * windows are anchored on the added lines' text instead of raw line numbers. + */ +export function excerptFile(content: string, diff: string | undefined, maxChars: number): { text: string; truncated: boolean } { + if (content.length <= maxChars) return { text: content, truncated: false } + const lines = content.split("\n") + const n = lines.length + const rows = [...walkDiffLines(diff)] + const located = locateAddedLines(lines, rows) + if (located === "unlocated") return { text: truncate(content, maxChars) + COMPILED_UNLOCATED_MARKER, truncated: true } + const added = located ?? rows.filter((r) => r.kind === "+").map((r) => r.line) + const anchors = (added.length ? added : rows.map((r) => r.line)).filter((l) => l >= 1 && l <= n).sort((a, b) => a - b) + if (!anchors.length) return { text: truncate(content, maxChars), truncated: true } + const windows: Array<[number, number]> = [] + for (const a of anchors) { + const s = Math.max(1, a - EXCERPT_CONTEXT_LINES) + const e = Math.min(n, a + EXCERPT_CONTEXT_LINES) + const last = windows[windows.length - 1] + if (last && s <= last[1] + 1) last[1] = Math.max(last[1], e) + else windows.push([s, e]) + } + const MARKER = 40 // room reserved per omission marker + const size = (s: number, e: number) => lines.slice(s - 1, e).reduce((t, l) => t + l.length + 1, 0) + const kept: Array<[number, number]> = [] + let used = 0 + for (const [s, e] of windows) { + const sz = size(s, e) + MARKER + if (used + sz <= maxChars) { + kept.push([s, e]) + used += sz + continue + } + if (!kept.length) { + // The first window alone is over budget: keep as many of its lines as fit. + let end = s - 1 + while (end < e && used + size(s, end + 1) + MARKER <= maxChars) end++ + if (end >= s) { + kept.push([s, end]) + used += size(s, end) + MARKER + } + } + break + } + if (!kept.length) return { text: truncate(content, maxChars), truncated: true } + // Fill what is left with the file head, before the first window. + let head = 0 + while (head + 1 < kept[0][0] && used + size(1, head + 1) + MARKER <= maxChars) head++ + if (head) { + if (head + 1 >= kept[0][0]) kept[0][0] = 1 + else kept.unshift([1, head]) + } + const out: string[] = [] + let next = 1 + for (const [s, e] of kept) { + if (s > next) out.push(`… (${lineRange(next, s - 1)} omitted)`) + out.push(lines.slice(s - 1, e).join("\n")) + next = e + 1 + } + if (next <= n) out.push(`… (${lineRange(next, n)} omitted)`) + return { text: out.join("\n"), truncated: true } +} + +/** Hunks of `diff` whose text lies wholly within its first `maxChars` characters. */ +function headHunkStats(diff: string | undefined, maxChars: number): Omit { + if (!diff) return { hunksSent: 0, hunksTotal: 0 } + const { preamble, hunks } = splitHunks(diff) + let end = preamble ? preamble.length + 1 : 0 + let sent = 0 + for (const h of hunks) { + end += h.text.length + if (end > maxChars) break + sent++ + end += 1 + } + return { hunksSent: sent, hunksTotal: hunks.length } +} + +/** How coverage mode renders a file: the diff section's own character budget. */ +export interface AiCoverageRender { + diffChars: number +} +/** `true` means coverage with the default diff budget; `false` keeps today's rendering. */ +type CoverageArg = boolean | AiCoverageRender | undefined + +function coverageRender(c: CoverageArg): AiCoverageRender | undefined { + if (!c) return undefined + return c === true ? { diffChars: COVERAGE_DIFF_CHARS } : c +} + +/** One changed file as it appears in the prompt, with what it carries. */ +function renderFile(f: AiReviewFile, maxChars: number, coverageArg: CoverageArg, withCard: boolean): { text: string } & SectionStats { + const coverage = coverageRender(coverageArg) const label = f.kind && KIND_LABEL[f.kind] const parts = [`\n### ${f.path} (${f.status}${label ? `, ${label}` : ""})`] - if (f.diff) parts.push("Diff:\n```diff\n" + truncate(f.diff, maxChars) + "\n```") + if (withCard && f.card) parts.push(`dbt context (from manifest):\n${f.card}`) + let stats: SectionStats = { ...headHunkStats(f.diff, maxChars), truncated: (f.diff?.length ?? 0) > maxChars } + // Under coverage a long diff sent whole makes the file windows redundant: keep a short head instead. + let fileChars = maxChars + if (f.diff) { + if (coverage && f.diff.length > maxChars) { + const { text: excerpt, ...ex } = excerptDiff(f.diff, coverage.diffChars) + stats = ex + if (ex.hunksSent === ex.hunksTotal && !ex.truncated) fileChars = Math.min(COVERAGE_FULL_DIFF_FILE_CHARS, maxChars) + parts.push("Diff:\n```diff\n" + excerpt + "\n```") + } else parts.push("Diff:\n```diff\n" + truncate(f.diff, maxChars) + "\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, maxChars) + "\n```") + const body = !coverage + ? truncate(f.sql, maxChars) + : fileChars < maxChars + ? truncate(f.sql, fileChars) + : excerptFile(f.sql, f.diff, maxChars).text + if (f.sql.length > fileChars) stats = { ...stats, truncated: true } + parts.push(`${isModel ? "Current SQL" : "Current file"}:\n\`\`\`${FENCE[f.kind ?? ""] ?? "sql"}\n` + body + "\n```") } - return parts.join("\n") + return { text: parts.join("\n"), ...stats } +} + +function fileSection(f: AiReviewFile, maxChars: number, coverage: CoverageArg = false): string { + return renderFile(f, maxChars, coverage, true).text } /** Split files, in order, into groups whose sections fit `batchChars` (an oversized file goes alone). */ @@ -106,7 +438,7 @@ export function batchAiFiles(files: AiReviewFile[], budget: AiInputBudget): AiRe let cur: AiReviewFile[] = [] let size = 0 for (const f of files) { - const n = fileSection(f, budget.maxFileChars).length + const n = fileSection(f, budget.maxFileChars, budget.coverage).length if (cur.length && size + n > budget.batchChars) { batches.push(cur) cur = [] @@ -166,6 +498,23 @@ export interface AiLaneStatus { replayed?: boolean /** The replayed envelope did not list every generator finding. */ replayIncomplete?: boolean + /** What each file sent carried; recorded under coverage, context cards or dry run. */ + input?: AiInputRecord[] + /** Sendable files left out by the coverage quotas (they still went through the deterministic lanes). */ + unsent?: string[] +} +/** One file as sent to the model. */ +export interface AiInputRecord { + path: string + kind: string + /** Characters of the file's prompt section. */ + chars: number + /** Some of the diff or file content was left out. */ + truncated: boolean + hunksSent: number + hunksTotal: number + /** A manifest context card went with the file. */ + card: boolean } /** Token counts as `Session.getUsage` reports them: `input` excludes cached * tokens, `inputTotal` includes them. */ @@ -229,7 +578,15 @@ function relatedContext(input: AiReviewInput): string[] { } /** Assemble the user message (mechanical formatting — not IP). */ -export function buildUserMessage(input: AiReviewInput, maxFileChars = MAX_DIFF_CHARS): string { +export function buildUserMessage(input: AiReviewInput, maxFileChars = MAX_DIFF_CHARS, coverage: CoverageArg = false): string { + return buildBatchMessage(input, maxFileChars, coverage).message +} + +/** The user message for one call and a record of each file it carries. A context + * card is added only while the call's cards total at most 6,000 characters; a card + * that would pass that is skipped and later, smaller ones may still fit. */ +function buildBatchMessage(input: AiReviewInput, maxFileChars: number, coverage: CoverageArg): { message: string; records: AiInputRecord[] } { + const records: AiInputRecord[] = [] 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)}`) @@ -244,7 +601,22 @@ export function buildUserMessage(input: AiReviewInput, maxFileChars = MAX_DIFF_C } parts.push("\n## CHANGED FILES (review for what the engine missed):") - for (const f of input.files) parts.push(fileSection(f, maxFileChars)) + let cardChars = 0 + for (const f of input.files) { + const withCard = !!f.card && cardChars + f.card.length <= CARD_BATCH_MAX_CHARS + if (withCard) cardChars += f.card!.length + const r = renderFile(f, maxFileChars, coverage, withCard) + parts.push(r.text) + records.push({ + path: f.path, + kind: f.kind ?? "model_sql", + chars: r.text.length, + truncated: r.truncated, + hunksSent: r.hunksSent, + hunksTotal: r.hunksTotal, + card: withCard, + }) + } const context = relatedContext(input) if (context.length) { parts.push( @@ -253,7 +625,38 @@ export function buildUserMessage(input: AiReviewInput, maxFileChars = MAX_DIFF_C ) } parts.push("\nReturn ONLY the JSON array per the output contract.") - return parts.join("\n") + return { message: parts.join("\n"), records } +} + +/** What the lane would send: files, the unsent rest, one message per call, and a record per file. */ +export interface AiInputPlan { + budget: AiInputBudget + files: AiReviewFile[] + /** Sendable files left out (by the quotas under coverage, by `maxFiles` otherwise). */ + unsent: AiReviewFile[] + batches: AiReviewFile[][] + messages: string[] + input: AiInputRecord[] +} + +/** + * Plan the lane's input without calling a model: selection, batching, the exact + * user messages and what each file carries. Pure: the budget comes from the + * `env` argument; `runAiReview` sends exactly these messages. + */ +export function planAiInput(input: AiReviewInput, env: Record = process.env): AiInputPlan { + const budget = aiInputBudget(env) + let files: AiReviewFile[] + let unsent: AiReviewFile[] + if (budget.coverage) ({ sent: files, unsent } = selectAiFilesByQuota(input.files, budget.coverage.quotas)) + else { + files = selectAiFiles(input.files, budget.maxFiles) + const chosen = new Set(files) + unsent = input.files.filter((f) => sendable(f) && !chosen.has(f)) + } + const batches = files.length ? batchAiFiles(files, budget) : [] + const built = batches.map((b) => buildBatchMessage({ ...input, files: b }, budget.maxFileChars, budget.coverage)) + return { budget, files, unsent, batches, messages: built.map((b) => b.message), input: built.flatMap((b) => b.records) } } /** @@ -265,15 +668,23 @@ export function buildUserMessage(input: AiReviewInput, maxFileChars = MAX_DIFF_C */ export async function runAiReview(input: AiReviewInput): Promise { const replay = aiReplayPath() - if (replay) return runAiReplay(input, replay) const startedAt = Date.now() + const plan = planAiInput(input) + const { files, budget } = plan + const dryRun = aiDryRunEnabled() + // Recorded only when an opt-in switch changed or inspects the input, so the default envelope is unchanged. + const recorded: Pick = { + ...(budget.coverage || dryRun || plan.input.some((r) => r.card) ? { input: plan.input } : {}), + ...(budget.coverage && plan.unsent.length ? { unsent: plan.unsent.map((f) => f.path) } : {}), + } const done = (findings: Finding[], status: Omit): AiReviewResult => ({ findings, - status: { ...status, durationMs: Date.now() - startedAt }, + status: { ...status, ...recorded, durationMs: Date.now() - startedAt }, }) - const budget = aiInputBudget() - const files = selectAiFiles(input.files, budget.maxFiles) - if (!files.length) return done([], { state: "skipped", reason: "no changed dbt files to review" }) + if (!files.length && (dryRun || !replay)) return done([], { state: "skipped", reason: "no changed dbt files to review" }) + // Dry run wins over replay: a replayed envelope would still reach the verifier, a model call. + if (dryRun) return done([], { state: "skipped", reason: "dry run (ALTIMATE_REVIEW_AI_DRYRUN=1): no model call" }) + if (replay) return runAiReplay(input, replay) let modelLabel: string | undefined try { @@ -298,11 +709,11 @@ export async function runAiReview(input: AiReviewInput): Promise modelLabel = `${model.providerID}/${model.id}` // One call per batch; a PR that fits the budget is a single call, as before. - const batches = batchAiFiles(files, budget) + const batches = plan.batches const results: BatchResult[] = [] for (let i = 0; i < batches.length; i += BATCH_CONCURRENCY) { const slice = batches.slice(i, i + BATCH_CONCURRENCY) - results.push(...(await Promise.all(slice.map((b) => reviewBatch(input, b, budget, system, model))))) + results.push(...(await Promise.all(slice.map((b, j) => reviewBatch(plan.messages[i + j], b, system, model))))) } let findings = results.flatMap((r) => r.findings) const failed = results.filter((r) => r.state !== "ok") @@ -395,13 +806,11 @@ export async function streamUsage( /** One model call over a group of files: send, wait (bounded), parse and clamp in core. */ async function reviewBatch( - input: AiReviewInput, + message: string, files: AiReviewFile[], - budget: AiInputBudget, system: string, model: Awaited>, ): Promise { - const message = buildUserMessage({ ...input, files }, budget.maxFileChars) const controller = new AbortController() const timeoutMs = aiTimeoutMs(message.length) const timeout = setTimeout(() => controller.abort(), timeoutMs) diff --git a/packages/opencode/src/altimate/review/format.ts b/packages/opencode/src/altimate/review/format.ts index b44145173e..bf2e831947 100644 --- a/packages/opencode/src/altimate/review/format.ts +++ b/packages/opencode/src/altimate/review/format.ts @@ -282,6 +282,16 @@ export function coverageNotes(env: VerdictEnvelope): string[] { // Some calls answered and some failed: the files in the failed calls were not reviewed by the AI. notes.push(`Advisory AI review partly failed: ${env.ai.reason}.`) } + // Files over the AI coverage quotas still went through the deterministic checks, + // so they are a note on the AI lane, not entries in the "Not reviewed" ledger. + const aiUnsent = env.ai?.unsent ?? [] + if (aiUnsent.length) { + const head = aiUnsent.slice(0, 5).map(codeSpan).join(", ") + const more = aiUnsent.length > 5 ? ` and ${aiUnsent.length - 5} more` : "" + notes.push( + `${aiUnsent.length} file(s) were not sent to the advisory AI review (file quota); the deterministic checks still ran on them: ${head}${more}.`, + ) + } const out = notes.length ? [...notes.map((n) => `> ℹ️ ${n}`), ""] : [] return [...out, ...ledgerBlock(ledger)] } diff --git a/packages/opencode/src/altimate/review/orchestrate.ts b/packages/opencode/src/altimate/review/orchestrate.ts index c3361ded49..d244e2f325 100644 --- a/packages/opencode/src/altimate/review/orchestrate.ts +++ b/packages/opencode/src/altimate/review/orchestrate.ts @@ -58,7 +58,8 @@ import { testsOnNullColumns, yamlParseFindings, } from "./project-rules" -import { type AiContextFile, type AiLaneStatus, type AiReviewFile, type AiReviewInput, type AiReviewResult, aiInputBudget, safeReason, selectAiFiles } from "./ai-review" +import { type AiContextFile, type AiLaneStatus, type AiReviewFile, type AiReviewInput, type AiReviewResult, aiInputBudget, safeReason, selectAiFiles, selectAiFilesByQuota } from "./ai-review" +import { type MacroCard, type ModelCard, attachContextCards } from "./ai-context-card" import { ruleTier } from "./rule-tiers" import { type AiVerifyEvidence, numberedSlice } from "./ai-verify" import type { ColumnReach } from "../native/dbt/column-reach" @@ -204,6 +205,14 @@ export interface ReviewRunner { > /** A model's or snapshot's merged dbt `config` from the manifest (project + yml + inline). */ modelConfig?(name: string): Promise | undefined> + /** Manifest facts about a model, snapshot or seed for the AI lane's context card: the + * node defined in `path` in this project, else by name (this project first). Never throws. */ + modelCard?(name: string, opts?: { path?: string }): Promise + /** Models, snapshots and seeds of this project whose yml (`patch_path`) is `path`. Never throws. */ + patchedModels?(path: string): Promise + /** A macro's callers and adapter variants: a macro defined in `path` in this project + * (the one named `name` first), else by name (this project first). Never throws. */ + macroCard?(name: string, opts?: { path?: string; changedModels?: string[] }): Promise /** Column sets the model's `unique` / `unique_combination_of_columns` tests declare unique. */ declaredUniqueKeys?(model: string): Promise /** Column-lineage edges for a query: source `"table"."col"` → target output column. */ @@ -274,6 +283,8 @@ export interface OrchestrateInput { aiModel?: string /** Send the changed files' unchanged dbt parents to the AI lane as context. */ aiGraphContext?: boolean + /** Attach a dbt manifest context card to each AI file (`ALTIMATE_REVIEW_AI_CONTEXT_CARD=1`; needs a manifest). */ + aiContextCard?: boolean /** Give the AI lane what the dbt-grounded verifier reads (`ALTIMATE_REVIEW_AI_VERIFY=1`). */ aiVerify?: boolean /** Fixed-string search over the dbt project at head, for the verifier. */ @@ -2577,8 +2588,11 @@ export async function runReview(input: OrchestrateInput): Promise aiFiles.push({ path: f.path, status: f.status, model: modelNameFromPath(f.path), kind: f.kind, diff: f.diff, sql: contents[i] }), ) - // Only the files within the AI lane's file cap are reached; the rest stay in the ledger. - touch(selectAiFiles(aiFiles, aiInputBudget().maxFiles)) + // Only the files the AI lane will send are reached (its file cap, or the + // per-kind quotas in coverage mode); the rest stay in the ledger. + const aiBudget = aiInputBudget() + touch(aiBudget.coverage ? selectAiFilesByQuota(aiFiles, aiBudget.coverage.quotas).sent : selectAiFiles(aiFiles, aiBudget.maxFiles)) + if (input.aiContextCard && anyManifest) await attachContextCards(aiFiles, input.runner) const context = input.aiGraphContext ? await aiGraphContext(aiFiles.map((f) => f.path), input.runner, getContent) : undefined // The verifier reads engine SQL first (the same SQL the engine lanes saw), then compiled output. const evidence: AiVerifyEvidence | undefined = input.aiVerify @@ -2610,7 +2624,8 @@ export async function runReview(input: OrchestrateInput): Promise { // The project's dbt directories (repo-relative), or one glob inside the project. diff --git a/packages/opencode/src/altimate/review/runner.ts b/packages/opencode/src/altimate/review/runner.ts index 0f3e810411..cc0b847cc5 100644 --- a/packages/opencode/src/altimate/review/runner.ts +++ b/packages/opencode/src/altimate/review/runner.ts @@ -6,6 +6,7 @@ import { columnReach } from "../native/dbt/column-reach" import { isExistenceError } from "../native/existence-error" import type { CheckResult, EquivalenceResult, GradeResult, ImpactResult, ReviewRunner } from "./orchestrate" import { buildReviewSchemaContext, type SchemaContext } from "./schema-context" +import type { MacroCard, ModelCard } from "./ai-context-card" import { errorReason } from "./verdict" /** @@ -39,6 +40,11 @@ function asArray(v: unknown): T[] { return Array.isArray(v) ? (v as T[]) : [] } +/** A repo-relative changed path names the manifest's project-relative `file` (same path, or under a project subdirectory). */ +function pathMatches(path: string, file: unknown): boolean { + return typeof file === "string" && !!file && (path === file || path.endsWith("/" + file)) +} + /** * Copy `primary_key` from `source` (manifest, which has dbt contract PKs) onto * matching tables in `target` (catalog, which has complete columns but no PK). @@ -545,6 +551,157 @@ export function createDispatcherRunner(opts: DispatcherRunnerOptions): ReviewRun } }, + async patchedModels(path: string): Promise { + try { + const raw = loadRawManifest(opts.manifestPath) + if (!raw) return [] + const project = raw.metadata?.project_name + const out: string[] = [] + for (const [id, n] of Object.entries(raw.nodes ?? {})) { + if (!/^(model|snapshot|seed)\./.test(id) || typeof n?.patch_path !== "string") continue + if (project && n.package_name !== project) continue + if (pathMatches(path, n.patch_path.replace(/^[^:/]+:\/\//, ""))) out.push(String(n.name)) + } + return out + } catch (err) { + fail("patchedModels", err) + return [] + } + }, + + async modelCard(name: string, cardOpts?: { path?: string }): Promise { + try { + const raw = loadRawManifest(opts.manifestPath) + if (!raw) return undefined + const project = raw.metadata?.project_name + const nodes = Object.entries(raw.nodes ?? {}) + const isModel = (id: string) => id.startsWith("model.") + const isOther = (id: string) => id.startsWith("snapshot.") || id.startsWith("seed.") + const own = (n: any) => !project || n?.package_name === project + const path = cardOpts?.path + // The changed file itself, then same-named nodes in this project, then any package. + const hit = + (path + ? nodes.find(([id, n]) => (isModel(id) || isOther(id)) && own(n) && pathMatches(path, n?.original_file_path)) + : undefined) ?? + nodes.find(([id, n]) => isModel(id) && own(n) && n?.name === name) ?? + nodes.find(([id, n]) => isOther(id) && own(n) && n?.name === name) ?? + nodes.find(([id, n]) => isModel(id) && n?.name === name) ?? + nodes.find(([id, n]) => isOther(id) && n?.name === name) + if (!hit) return undefined + const [uid, node] = hit + const config = node.config ?? {} + const text = (v: unknown): string | undefined => { + if (v === undefined || v === null || v === "") return undefined + if (Array.isArray(v)) return v.length ? v.map(String).join(", ") : undefined + if (typeof v === "object") return String((v as any).field ?? JSON.stringify(v)) + return String(v) + } + const nameOf = (id: string): string | undefined => { + const n = raw.nodes?.[id] + if (n?.name) return String(n.name) + const src = raw.sources?.[id] + return src ? `${src.source_name}.${src.name}` : undefined + } + const parentIds = asArray(node.depends_on?.nodes) + const parents = parentIds.map(nameOf).filter((n): n is string => !!n) + const children = nodes + .filter(([id, n]) => isModel(id) && asArray(n?.depends_on?.nodes).includes(uid)) + .map(([, n]) => String(n.name)) + const exposures = Object.values(raw.exposures ?? {}) + .filter((e) => asArray(e?.depends_on?.nodes).includes(uid)) + .map((e) => String(e.name ?? "")) + .filter(Boolean) + const testsByColumn = new Map() + for (const [, n] of nodes) { + if (n?.resource_type !== "test" || n.attached_node !== uid || !n.column_name) continue + const col = String(n.column_name).toLowerCase() + const testName = String(n.test_metadata?.name ?? n.name ?? "") + if (!testName) continue + testsByColumn.set(col, [...(testsByColumn.get(col) ?? []), testName]) + } + const columns = Object.values(node.columns ?? {}) + .filter((c) => c?.name) + .slice(0, 25) + .map((c) => ({ + name: String(c.name), + ...(c.data_type ? { dataType: String(c.data_type) } : {}), + tests: testsByColumn.get(String(c.name).toLowerCase()) ?? [], + })) + const dir = (p: unknown) => (typeof p === "string" ? p.slice(0, p.lastIndexOf("/") + 1) : undefined) + const ownDir = dir(node.original_file_path) + const parentSet = new Set(parentIds) + const siblings = nodes + .filter( + ([id, n]) => + isModel(id) && + id !== uid && + ownDir !== undefined && + dir(n?.original_file_path) === ownDir && + asArray(n?.depends_on?.nodes).some((p) => parentSet.has(p)), + ) + .slice(0, 8) + .map(([, n]) => String(n.name)) + return { + resourceType: String(node.resource_type ?? uid.split(".")[0]), + materialized: text(config.materialized), + uniqueKey: text(config.unique_key), + incrementalStrategy: text(config.incremental_strategy), + partitionBy: text(config.partition_by), + clusterBy: text(config.cluster_by), + contractEnforced: config.contract?.enforced === true || node.contract?.enforced === true, + parents, + children, + exposures, + columns, + siblings, + } + } catch (err) { + fail("modelCard", err) + return undefined + } + }, + + async macroCard(name: string, cardOpts?: { path?: string; changedModels?: string[] }): Promise { + try { + const raw = loadRawManifest(opts.manifestPath) + if (!raw) return undefined + const project = raw.metadata?.project_name + const macros = Object.entries(raw.macros ?? {}) + const own = (m: any) => !project || m?.package_name === project + const path = cardOpts?.path + // Macros defined in the changed file within this project (the one named + // like the file first), then by name in this project, then any package. + const inFile = path ? macros.filter(([, m]) => own(m) && pathMatches(path, m?.original_file_path)) : [] + const byName = macros.filter(([, m]) => m?.name === name) + const hit = + inFile.find(([, m]) => m.name === name) ?? inFile[0] ?? byName.find(([, m]) => own(m)) ?? byName[0] + if (!hit) return undefined + const [uid, macro] = hit + const changed = new Set(cardOpts?.changedModels ?? []) + const callers = Object.entries(raw.nodes ?? {}) + .filter(([id, n]) => id.startsWith("model.") && asArray(n?.depends_on?.macros).includes(uid)) + .map(([, n]) => String(n.name)) + .sort((a, b) => Number(changed.has(b)) - Number(changed.has(a))) + .slice(0, 10) + const macroName = String(macro.name) + const sep = macroName.indexOf("__") + const base = sep > 0 ? macroName.slice(sep + 2) : macroName + const variants = macros + .filter(([id, m]) => { + if (id === uid || m?.package_name !== macro.package_name) return false + const n = String(m.name ?? "") + const i = n.indexOf("__") + return n === base || (i > 0 && n.slice(i + 2) === base) + }) + .map(([, m]) => `${m.name}${m.original_file_path ? ` (${m.original_file_path})` : ""}`) + return { name: macroName, callers, variants } + } catch (err) { + fail("macroCard", err) + return undefined + } + }, + async isComplex(sql: string): Promise { try { const res = await callCore("altimate_core.metadata", { sql }) diff --git a/packages/opencode/src/altimate/review/verdict.ts b/packages/opencode/src/altimate/review/verdict.ts index 57ab54e6d1..8d2747845e 100644 --- a/packages/opencode/src/altimate/review/verdict.ts +++ b/packages/opencode/src/altimate/review/verdict.ts @@ -226,6 +226,22 @@ export const VerdictEnvelope = z.object({ replayed: z.boolean().optional(), /** The replayed envelope did not list every generator finding. */ replayIncomplete: z.boolean().optional(), + /** What each file sent to the model carried (opt-in coverage, context cards or dry run). */ + input: z + .array( + z.object({ + path: z.string(), + kind: z.string(), + chars: z.number().int().nonnegative(), + truncated: z.boolean(), + hunksSent: z.number().int().nonnegative(), + hunksTotal: z.number().int().nonnegative(), + card: z.boolean(), + }), + ) + .optional(), + /** Sendable files the opt-in coverage quotas kept from the model (the deterministic lanes still checked them). */ + unsent: z.array(z.string()).optional(), /** The opt-in dbt-grounded verifier (`ALTIMATE_REVIEW_AI_VERIFY=1`): what it kept and dropped. */ verifier: z .object({ diff --git a/packages/opencode/test/altimate/fixtures/review-ai-prompt-default.txt b/packages/opencode/test/altimate/fixtures/review-ai-prompt-default.txt new file mode 100644 index 0000000000..8325f4e5de --- /dev/null +++ b/packages/opencode/test/altimate/fixtures/review-ai-prompt-default.txt @@ -0,0 +1,1018 @@ +PR title: Rework revenue marts +PR description: +Moves cents conversion into a macro. + +Also adds tests. + +## GROUNDING — deterministic engine findings (DO NOT repeat these): +- [warning] warehouse_cost · models/marts/fct_big.sql: SELECT * in a mart + +## CHANGED FILES (review for what the engine missed): + +### models/marts/fct_big.sql (modified) +Diff: +```diff +@@ -10,22 +10,24 @@ + select 0 as ctx_before ++ , coalesce(col_0_0, 'unknown_value_0') as renamed_column_0_0 ++ , coalesce(col_0_1, 'unknown_value_1') as renamed_column_0_1 ++ , coalesce(col_0_2, 'unknown_value_2') as renamed_column_0_2 ++ , coalesce(col_0_3, 'unknown_value_3') as renamed_column_0_3 ++ , coalesce(col_0_4, 'unknown_value_4') as renamed_column_0_4 ++ , coalesce(col_0_5, 'unknown_value_5') as renamed_column_0_5 ++ , coalesce(col_0_6, 'unknown_value_6') as renamed_column_0_6 ++ , coalesce(col_0_7, 'unknown_value_7') as renamed_column_0_7 ++ , coalesce(col_0_8, 'unknown_value_8') as renamed_column_0_8 ++ , coalesce(col_0_9, 'unknown_value_9') as renamed_column_0_9 ++ , coalesce(col_0_10, 'unknown_value_10') as renamed_column_0_10 ++ , coalesce(col_0_11, 'unknown_value_11') as renamed_column_0_11 ++ , coalesce(col_0_12, 'unknown_value_12') as renamed_column_0_12 ++ , coalesce(col_0_13, 'unknown_value_13') as renamed_column_0_13 ++ , coalesce(col_0_14, 'unknown_value_14') as renamed_column_0_14 ++ , coalesce(col_0_15, 'unknown_value_15') as renamed_column_0_15 ++ , coalesce(col_0_16, 'unknown_value_16') as renamed_column_0_16 ++ , coalesce(col_0_17, 'unknown_value_17') as renamed_column_0_17 ++ , coalesce(col_0_18, 'unknown_value_18') as renamed_column_0_18 ++ , coalesce(col_0_19, 'unknown_value_19') as renamed_column_0_19 +- , old_col_0 + from {{ ref('stg_0') }} +@@ -110,22 +110,24 @@ + select 1 as ctx_before ++ , coalesce(col_1_0, 'unknown_value_0') as renamed_column_1_0 ++ , coalesce(col_1_1, 'unknown_value_1') as renamed_column_1_1 ++ , coalesce(col_1_2, 'unknown_value_2') as renamed_column_1_2 ++ , coalesce(col_1_3, 'unknown_value_3') as renamed_column_1_3 ++ , coalesce(col_1_4, 'unknown_value_4') as renamed_column_1_4 ++ , coalesce(col_1_5, 'unknown_value_5') as renamed_column_1_5 ++ , coalesce(col_1_6, 'unknown_value_6') as renamed_column_1_6 ++ , coalesce(col_1_7, 'unknown_value_7') as renamed_column_1_7 ++ , coalesce(col_1_8, 'unknown_value_8') as renamed_column_1_8 ++ , coalesce(col_1_9, 'unknown_value_9') as renamed_column_1_9 ++ , coalesce(col_1_10, 'unknown_value_10') as renamed_column_1_10 ++ , coalesce(col_1_11, 'unknown_value_11') as renamed_column_1_11 ++ , coalesce(col_1_12, 'unknown_value_12') as renamed_column_1_12 ++ , coalesce(col_1_13, 'unknown_value_13') as renamed_column_1_13 ++ , coalesce(col_1_14, 'unknown_value_14') as renamed_column_1_14 ++ , coalesce(col_1_15, 'unknown_value_15') as renamed_column_1_15 ++ , coalesce(col_1_16, 'unknown_value_16') as renamed_column_1_16 ++ , coalesce(col_1_17, 'unknown_value_17') as renamed_column_1_17 ++ , coalesce(col_1_18, 'unknown_value_18') as renamed_column_1_18 ++ , coalesce(col_1_19, 'unknown_value_19') as renamed_column_1_19 +- , old_col_1 + from {{ ref('stg_1') }} +@@ -210,22 +210,24 @@ + select 2 as ctx_before ++ , coalesce(col_2_0, 'unknown_value_0') as renamed_column_2_0 ++ , coalesce(col_2_1, 'unknown_value_1') as renamed_column_2_1 ++ , coalesce(col_2_2, 'unknown_value_2') as renamed_column_2_2 ++ , coalesce(col_2_3, 'unknown_value_3') as renamed_column_2_3 ++ , coalesce(col_2_4, 'unknown_value_4') as renamed_column_2_4 ++ , coalesce(col_2_5, 'unknown_value_5') as renamed_column_2_5 ++ , coalesce(col_2_6, 'unknown_value_6') as renamed_column_2_6 ++ , coalesce(col_2_7, 'unknown_value_7') as renamed_column_2_7 ++ , coalesce(col_2_8, 'unknown_value_8') as renamed_column_2_8 ++ , coalesce(col_2_9, 'unknown_value_9') as renamed_column_2_9 ++ , coalesce(col_2_10, 'unknown_value_10') as renamed_column_2_10 ++ , coalesce(col_2_11, 'unknown_value_11') as renamed_column_2_11 ++ , coalesce(col_2_12, 'unknown_value_12') as renamed_column_2_12 ++ , coalesce(col_2_13, 'unknown_value_13') as renamed_column_2_13 ++ , coalesce(col_2_14, 'unknown_value_14') as renamed_column_2_14 ++ , coalesce(col_2_15, 'unknown_value_15') as renamed_column_2_15 ++ , coalesce(col_2_16, 'unknown_value_16') as renamed_column_2_16 ++ , coalesce(col_2_17, 'unknown_value_17') as renamed_column_2_17 ++ , coalesce(col_2_18, 'unknown_value_18') as renamed_column_2_18 ++ , coalesce(col_2_19, 'unknown_value_19') as renamed_column_2_19 +- , old_col_2 + from {{ ref('stg_2') }} +@@ -310,22 +310,24 @@ + select 3 as ctx_before ++ , coalesce(col_3_0, 'unknown_value_0') as renamed_column_3_0 ++ , coalesce(col_3_1, 'unknown_value_1') as renamed_column_3_1 ++ , coalesce(col_3_2, 'unknown_value_2') as renamed_column_3_2 ++ , coalesce(col_3_3, 'unknown_value_3') as renamed_column_3_3 ++ , coalesce(col_3_4, 'unknown_value_4') as renamed_column_3_4 ++ , coalesce(col_3_5, 'unknown_value_5') as renamed_column_3_5 ++ , coalesce(col_3_6, 'unknown_value_6') as renamed_column_3_6 ++ , coalesce(col_3_7, 'unknown_value_7') as renamed_column_3_7 ++ , coalesce(col_3_8, 'unknown_value_8') as renamed_column_3_8 ++ , coalesce(col_3_9, 'unknown_value_9') as renamed_column_3_9 ++ , coalesce(col_3_10, 'unknown_value_10') as renamed_column_3_10 ++ , coalesce(col_3_11, 'unknown_value_11') as renamed_column_3_11 ++ , coalesce(col_3_12, 'unknown_value_12') as renamed_column_3_12 ++ , coalesce(col_3_13, 'unknown_value_13') as renamed_column_3_13 ++ , coalesce(col_3_14, 'unknown_value_14') as renamed_column_3_14 ++ , coalesce(col_3_15, 'unknown_value_15') as renamed_column_3_15 ++ , coalesce(col_3_16, 'unknown_value_16') as renamed_column_3_16 ++ , coalesce(col_3_17, 'unknown_value_17') as renamed_column_3_17 ++ , coalesce(col_3_18, 'unknown_value_18') as renamed_column_3_18 ++ , coalesce(col_3_19, 'unknown_value_19') as renamed_column_3_19 +- , old_col_3 + from {{ ref('stg_3') }} +@@ -410,22 +410,24 @@ + select 4 as ctx_before ++ , coalesce(col_4_0, 'unknown_value_0') as renamed_column_4_0 ++ , coalesce(col_4_1, 'unknown_value_1') as renamed_column_4_1 ++ , coalesce(col_4_2, 'unknown_value_2') as renamed_column_4_2 ++ , coalesce(col_4_3, 'unknown_value_3') as renamed_column_4_3 ++ , coalesce(col_4_4, 'unknown_value_4') as renamed_column_4_4 ++ , coalesce(col_4_5, 'unknown_value_5') as +… (truncated) +``` +Current SQL: +```sql + , cast(field_0 as varchar) as field_0_text -- line 1 + , cast(field_1 as varchar) as field_1_text -- line 2 + , cast(field_2 as varchar) as field_2_text -- line 3 + , cast(field_3 as varchar) as field_3_text -- line 4 + , cast(field_4 as varchar) as field_4_text -- line 5 + , cast(field_5 as varchar) as field_5_text -- line 6 + , cast(field_6 as varchar) as field_6_text -- line 7 + , cast(field_7 as varchar) as field_7_text -- line 8 + , cast(field_8 as varchar) as field_8_text -- line 9 + , cast(field_9 as varchar) as field_9_text -- line 10 + , cast(field_10 as varchar) as field_10_text -- line 11 + , cast(field_11 as varchar) as field_11_text -- line 12 + , cast(field_12 as varchar) as field_12_text -- line 13 + , cast(field_13 as varchar) as field_13_text -- line 14 + , cast(field_14 as varchar) as field_14_text -- line 15 + , cast(field_15 as varchar) as field_15_text -- line 16 + , cast(field_16 as varchar) as field_16_text -- line 17 + , cast(field_17 as varchar) as field_17_text -- line 18 + , cast(field_18 as varchar) as field_18_text -- line 19 + , cast(field_19 as varchar) as field_19_text -- line 20 + , cast(field_20 as varchar) as field_20_text -- line 21 + , cast(field_21 as varchar) as field_21_text -- line 22 + , cast(field_22 as varchar) as field_22_text -- line 23 + , cast(field_23 as varchar) as field_23_text -- line 24 + , cast(field_24 as varchar) as field_24_text -- line 25 + , cast(field_25 as varchar) as field_25_text -- line 26 + , cast(field_26 as varchar) as field_26_text -- line 27 + , cast(field_27 as varchar) as field_27_text -- line 28 + , cast(field_28 as varchar) as field_28_text -- line 29 + , cast(field_29 as varchar) as field_29_text -- line 30 + , cast(field_30 as varchar) as field_30_text -- line 31 + , cast(field_31 as varchar) as field_31_text -- line 32 + , cast(field_32 as varchar) as field_32_text -- line 33 + , cast(field_33 as varchar) as field_33_text -- line 34 + , cast(field_34 as varchar) as field_34_text -- line 35 + , cast(field_35 as varchar) as field_35_text -- line 36 + , cast(field_36 as varchar) as field_36_text -- line 37 + , cast(field_37 as varchar) as field_37_text -- line 38 + , cast(field_38 as varchar) as field_38_text -- line 39 + , cast(field_39 as varchar) as field_39_text -- line 40 + , cast(field_40 as varchar) as field_40_text -- line 41 + , cast(field_41 as varchar) as field_41_text -- line 42 + , cast(field_42 as varchar) as field_42_text -- line 43 + , cast(field_43 as varchar) as field_43_text -- line 44 + , cast(field_44 as varchar) as field_44_text -- line 45 + , cast(field_45 as varchar) as field_45_text -- line 46 + , cast(field_46 as varchar) as field_46_text -- line 47 + , cast(field_47 as varchar) as field_47_text -- line 48 + , cast(field_48 as varchar) as field_48_text -- line 49 + , cast(field_49 as varchar) as field_49_text -- line 50 + , cast(field_50 as varchar) as field_50_text -- line 51 + , cast(field_51 as varchar) as field_51_text -- line 52 + , cast(field_52 as varchar) as field_52_text -- line 53 + , cast(field_53 as varchar) as field_53_text -- line 54 + , cast(field_54 as varchar) as field_54_text -- line 55 + , cast(field_55 as varchar) as field_55_text -- line 56 + , cast(field_56 as varchar) as field_56_text -- line 57 + , cast(field_57 as varchar) as field_57_text -- line 58 + , cast(field_58 as varchar) as field_58_text -- line 59 + , cast(field_59 as varchar) as field_59_text -- line 60 + , cast(field_60 as varchar) as field_60_text -- line 61 + , cast(field_61 as varchar) as field_61_text -- line 62 + , cast(field_62 as varchar) as field_62_text -- line 63 + , cast(field_63 as varchar) as field_63_text -- line 64 + , cast(field_64 as varchar) as field_64_text -- line 65 + , cast(field_65 as varchar) as field_65_text -- line 66 + , cast(field_66 as varchar) as field_66_text -- line 67 + , cast(field_67 as varchar) as field_67_text -- line 68 + , cast(field_68 as varchar) as field_68_text -- line 69 + , cast(field_69 as varchar) as field_69_text -- line 70 + , cast(field_70 as varchar) as field_70_text -- line 71 + , cast(field_71 as varchar) as field_71_text -- line 72 + , cast(field_72 as varchar) as field_72_text -- line 73 + , cast(field_73 as varchar) as field_73_text -- line 74 + , cast(field_74 as varchar) as field_74_text -- line 75 + , cast(field_75 as varchar) as field_75_text -- line 76 + , cast(field_76 as varchar) as field_76_text -- line 77 + , cast(field_77 as varchar) as field_77_text -- line 78 + , cast(field_78 as varchar) as field_78_text -- line 79 + , cast(field_79 as varchar) as field_79_text -- line 80 + , cast(field_80 as varchar) as field_80_text -- line 81 + , cast(field_81 as varchar) as field_81_text -- line 82 + , cast(field_82 as varchar) as field_82_text -- line 83 + , cast(field_83 as varchar) as field_83_text -- line 84 + , cast(field_84 as varchar) as field_84_text -- line 85 + , cast(field_85 as varchar) as field_85_text -- line 86 + , cast(field_86 as varchar) as field_86_text -- line 87 + , cast(field_87 as varchar) as field_87_text -- line 88 + , cast(field_88 as varchar) as field_88_text -- line 89 + , cast(field_89 as varchar) as field_89_text -- line 90 + , cast(field_90 as varchar) as field_90_text -- line 91 + , cast(field_91 as varchar) as field_91_text -- line 92 + , cast(field_92 as varchar) as field_92_text -- line 93 + , cast(field_93 as varchar) as field_93_text -- line 94 + , cast(field_94 as varchar) as field_94_text -- line 95 + , cast(field_95 as varchar) as field_95_text -- line 96 + , cast(field_96 as varchar) as field_96_text -- line 97 + , cast(field_97 as varchar) as field_97_text -- line 98 + , cast(field_98 as varchar) as field_98_text -- line 99 + , cast(field_99 as varchar) as field_99_text -- line 100 + , cast(field_100 as varchar) as field_100_text -- line 101 + , cast(field_101 as varchar) as field_101_text -- line 102 + , cast(field_102 as varchar) as field_102_text -- line 103 + , cast(field_103 as varchar) as field_103_t +… (truncated) +``` + +### models/staging/stg_0.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_0 ++ b_0 + +``` +Current SQL: +```sql +select b_0 from {{ source('raw', 't0') }} +``` + +### models/staging/stg_1.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_1 ++ b_1 + +``` +Current SQL: +```sql +select b_1 from {{ source('raw', 't1') }} +``` + +### models/staging/stg_2.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_2 ++ b_2 + +``` +Current SQL: +```sql +select b_2 from {{ source('raw', 't2') }} +``` + +### models/staging/stg_3.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_3 ++ b_3 + +``` +Current SQL: +```sql +select b_3 from {{ source('raw', 't3') }} +``` + +### models/staging/stg_4.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_4 ++ b_4 + +``` +Current SQL: +```sql +select b_4 from {{ source('raw', 't4') }} +``` + +### models/staging/stg_5.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_5 ++ b_5 + +``` +Current SQL: +```sql +select b_5 from {{ source('raw', 't5') }} +``` + +### models/staging/stg_6.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_6 ++ b_6 + +``` +Current SQL: +```sql +select b_6 from {{ source('raw', 't6') }} +``` + +### models/staging/stg_7.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_7 ++ b_7 + +``` +Current SQL: +```sql +select b_7 from {{ source('raw', 't7') }} +``` + +### models/staging/stg_8.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_8 ++ b_8 + +``` +Current SQL: +```sql +select b_8 from {{ source('raw', 't8') }} +``` + +### models/staging/stg_9.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_9 ++ b_9 + +``` +Current SQL: +```sql +select b_9 from {{ source('raw', 't9') }} +``` + +### models/staging/stg_10.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_10 ++ b_10 + +``` +Current SQL: +```sql +select b_10 from {{ source('raw', 't10') }} +``` + +### models/staging/stg_11.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_11 ++ b_11 + +``` +Current SQL: +```sql +select b_11 from {{ source('raw', 't11') }} +``` + +### models/staging/stg_12.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_12 ++ b_12 + +``` +Current SQL: +```sql +select b_12 from {{ source('raw', 't12') }} +``` + +### models/staging/stg_13.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_13 ++ b_13 + +``` +Current SQL: +```sql +select b_13 from {{ source('raw', 't13') }} +``` + +### models/staging/stg_14.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_14 ++ b_14 + +``` +Current SQL: +```sql +select b_14 from {{ source('raw', 't14') }} +``` + +### models/staging/stg_15.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_15 ++ b_15 + +``` +Current SQL: +```sql +select b_15 from {{ source('raw', 't15') }} +``` + +### models/staging/stg_16.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_16 ++ b_16 + +``` +Current SQL: +```sql +select b_16 from {{ source('raw', 't16') }} +``` + +### models/staging/stg_17.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_17 ++ b_17 + +``` +Current SQL: +```sql +select b_17 from {{ source('raw', 't17') }} +``` + +### models/py/pm.py (added, Python model) +Diff: +```diff ++def model(dbt, session): + +``` +Current file: +```python +def model(dbt, session): + return dbt.ref('fct') + +``` + +## RELATED FILES — unchanged dbt parents of the changed files (context only; comment on the changed files, not these): + +### models/staging/stg_orders.sql (model `stg_orders`, used by models/marts/fct_big.sql) +```sql +select id, amount from raw.orders +``` + +Return ONLY the JSON array per the output contract. +=====BATCH===== +PR title: Rework revenue marts +PR description: +Moves cents conversion into a macro. + +Also adds tests. + +## GROUNDING — deterministic engine findings (DO NOT repeat these): +- [warning] warehouse_cost · models/marts/fct_big.sql: SELECT * in a mart + +## CHANGED FILES (review for what the engine missed): + +### models/marts/fct_big.sql (modified) +Diff: +```diff +@@ -10,22 +10,24 @@ + select 0 as ctx_before ++ , coalesce(col_0_0, 'unknown_value_0') as renamed_column_0_0 ++ , coalesce(col_0_1, 'unknown_value_1') as renamed_column_0_1 ++ , coalesce(col_0_2, 'unknown_value_2') as renamed_column_0_2 ++ , coalesce(col_0_3, 'unknown_value_3') as renamed_column_0_3 ++ , coalesce(col_0_4, 'unknown_value_4') as renamed_column_0_4 ++ , coalesce(col_0_5, 'unknown_value_5') as renamed_column_0_5 ++ , coalesce(col_0_6, 'unknown_value_6') as renamed_column_0_6 ++ , coalesce(col_0_7, 'unknown_value_7') as renamed_column_0_7 ++ , coalesce(col_0_8, 'unknown_value_8') as renamed_column_0_8 ++ , coalesce(col_0_9, 'unknown_value_9') as renamed_column_0_9 ++ , coalesce(col_0_10, 'unknown_value_10') as renamed_column_0_10 ++ , coalesce(col_0_11, 'unknown_value_11') as renamed_column_0_11 ++ , coalesce(col_0_12, 'unknown_value_12') as renamed_column_0_12 ++ , coalesce(col_0_13, 'unknown_value_13') as renamed_column_0_13 ++ , coalesce(col_0_14, 'unknown_value_14') as renamed_column_0_14 ++ , coalesce(col_0_15, 'unknown_value_15') as renamed_column_0_15 ++ , coalesce(col_0_16, 'unknown_value_16') as renamed_column_0_16 ++ , coalesce(col_0_17, 'unknown_value_17') as renamed_column_0_17 ++ , coalesce(col_0_18, 'unknown_value_18') as renamed_column_0_18 ++ , coalesce(col_0_19, 'unknown_value_19') as renamed_column_0_19 +- , old_col_0 + from {{ ref('stg_0') }} +@@ -110,22 +110,24 @@ + select 1 as ctx_before ++ , coalesce(col_1_0, 'unknown_value_0') as renamed_column_1_0 ++ , coalesce(col_1_1, 'unknown_value_1') as renamed_column_1_1 ++ , coalesce(col_1_2, 'unknown_value_2') as renamed_column_1_2 ++ , coalesce(col_1_3, 'unknown_value_3') as renamed_column_1_3 ++ , coalesce(col_1_4, 'unknown_value_4') as renamed_column_1_4 ++ , coalesce(col_1_5, 'unknown_value_5') as renamed_column_1_5 ++ , coalesce(col_1_6, 'unknown_value_6') as renamed_column_1_6 ++ , coalesce(col_1_7, 'unknown_value_7') as renamed_column_1_7 ++ , coalesce(col_1_8, 'unknown_value_8') as renamed_column_1_8 ++ , coalesce(col_1_9, 'unknown_value_9') as renamed_column_1_9 ++ , coalesce(col_1_10, 'unknown_value_10') as renamed_column_1_10 ++ , coalesce(col_1_11, 'unknown_value_11') as renamed_column_1_11 ++ , coalesce(col_1_12, 'unknown_value_12') as renamed_column_1_12 ++ , coalesce(col_1_13, 'unknown_value_13') as renamed_column_1_13 ++ , coalesce(col_1_14, 'unknown_value_14') as renamed_column_1_14 ++ , coalesce(col_1_15, 'unknown_value_15') as renamed_column_1_15 ++ , coalesce(col_1_16, 'unknown_value_16') as renamed_column_1_16 ++ , coalesce(col_1_17, 'unknown_value_17') as renamed_column_1_17 ++ , coalesce(col_1_18, 'unknown_value_18') as renamed_column_1_18 ++ , coalesce(col_1_19, 'unknown_value_19') as renamed_column_1_19 +- , old_col_1 + from {{ ref('stg_1') }} +@@ -210,22 +210,24 @@ + select 2 as ctx_before ++ , coalesce(col_2_0, 'unknown_value_0') as renamed_column_2_0 ++ , coalesce(col_2_1, 'unknown_value_1') as renamed_column_2_1 ++ , coalesce(col_2_2, 'unknown_value_2') as renamed_column_2_2 ++ , coalesce(col_2_3, 'unknown_value_3') as renamed_column_2_3 ++ , coalesce(col_2_4, 'unknown_value_4') as renamed_column_2_4 ++ , coalesce(col_2_5, 'unknown_value_5') as renamed_column_2_5 ++ , coalesce(col_2_6, 'unknown_value_6') as renamed_column_2_6 ++ , coalesce(col_2_7, 'unknown_value_7') as renamed_column_2_7 ++ , coalesce(col_2_8, 'unknown_value_8') as renamed_column_2_8 ++ , coalesce(col_2_9, 'unknown_value_9') as renamed_column_2_9 ++ , coalesce(col_2_10, 'unknown_value_10') as renamed_column_2_10 ++ , coalesce(col_2_11, 'unknown_value_11') as renamed_column_2_11 ++ , coalesce(col_2_12, 'unknown_value_12') as renamed_column_2_12 ++ , coalesce(col_2_13, 'unknown_value_13') as renamed_column_2_13 ++ , coalesce(col_2_14, 'unknown_value_14') as renamed_column_2_14 ++ , coalesce(col_2_15, 'unknown_value_15') as renamed_column_2_15 ++ , coalesce(col_2_16, 'unknown_value_16') as renamed_column_2_16 ++ , coalesce(col_2_17, 'unknown_value_17') as renamed_column_2_17 ++ , coalesce(col_2_18, 'unknown_value_18') as renamed_column_2_18 ++ , coalesce(col_2_19, 'unknown_value_19') as renamed_column_2_19 +- , old_col_2 + from {{ ref('stg_2') }} +@@ -310,22 +310,24 @@ + select 3 as ctx_before ++ , coalesce(col_3_0, 'unknown_value_0') as renamed_column_3_0 ++ , coalesce(col_3_1, 'unknown_value_1') as renamed_column_3_1 ++ , coalesce(col_3_2, 'unknown_value_2') as renamed_column_3_2 ++ , coalesce(col_3_3, 'unknown_value_3') as renamed_column_3_3 ++ , coalesce(col_3_4, 'unknown_value_4') as renamed_column_3_4 ++ , coalesce(col_3_5, 'unknown_value_5') as renamed_column_3_5 ++ , coalesce(col_3_6, 'unknown_value_6') as renamed_column_3_6 ++ , coalesce(col_3_7, 'unknown_value_7') as renamed_column_3_7 ++ , coalesce(col_3_8, 'unknown_value_8') as renamed_column_3_8 ++ , coalesce(col_3_9, 'unknown_value_9') as renamed_column_3_9 ++ , coalesce(col_3_10, 'unknown_value_10') as renamed_column_3_10 ++ , coalesce(col_3_11, 'unknown_value_11') as renamed_column_3_11 ++ , coalesce(col_3_12, 'unknown_value_12') as renamed_column_3_12 ++ , coalesce(col_3_13, 'unknown_value_13') as renamed_column_3_13 ++ , coalesce(col_3_14, 'unknown_value_14') as renamed_column_3_14 ++ , coalesce(col_3_15, 'unknown_value_15') as renamed_column_3_15 ++ , coalesce(col_3_16, 'unknown_value_16') as renamed_column_3_16 ++ , coalesce(col_3_17, 'unknown_value_17') as renamed_column_3_17 ++ , coalesce(col_3_18, 'unknown_value_18') as renamed_column_3_18 ++ , coalesce(col_3_19, 'unknown_value_19') as renamed_column_3_19 +- , old_col_3 + from {{ ref('stg_3') }} +@@ -410,22 +410,24 @@ + select 4 as ctx_before ++ , coalesce(col_4_0, 'unknown_value_0') as renamed_column_4_0 ++ , coalesce(col_4_1, 'unknown_value_1') as renamed_column_4_1 ++ , coalesce(col_4_2, 'unknown_value_2') as renamed_column_4_2 ++ , coalesce(col_4_3, 'unknown_value_3') as renamed_column_4_3 ++ , coalesce(col_4_4, 'unknown_value_4') as renamed_column_4_4 ++ , coalesce(col_4_5, 'unknown_value_5') as +… (truncated) +``` +Current SQL: +```sql + , cast(field_0 as varchar) as field_0_text -- line 1 + , cast(field_1 as varchar) as field_1_text -- line 2 + , cast(field_2 as varchar) as field_2_text -- line 3 + , cast(field_3 as varchar) as field_3_text -- line 4 + , cast(field_4 as varchar) as field_4_text -- line 5 + , cast(field_5 as varchar) as field_5_text -- line 6 + , cast(field_6 as varchar) as field_6_text -- line 7 + , cast(field_7 as varchar) as field_7_text -- line 8 + , cast(field_8 as varchar) as field_8_text -- line 9 + , cast(field_9 as varchar) as field_9_text -- line 10 + , cast(field_10 as varchar) as field_10_text -- line 11 + , cast(field_11 as varchar) as field_11_text -- line 12 + , cast(field_12 as varchar) as field_12_text -- line 13 + , cast(field_13 as varchar) as field_13_text -- line 14 + , cast(field_14 as varchar) as field_14_text -- line 15 + , cast(field_15 as varchar) as field_15_text -- line 16 + , cast(field_16 as varchar) as field_16_text -- line 17 + , cast(field_17 as varchar) as field_17_text -- line 18 + , cast(field_18 as varchar) as field_18_text -- line 19 + , cast(field_19 as varchar) as field_19_text -- line 20 + , cast(field_20 as varchar) as field_20_text -- line 21 + , cast(field_21 as varchar) as field_21_text -- line 22 + , cast(field_22 as varchar) as field_22_text -- line 23 + , cast(field_23 as varchar) as field_23_text -- line 24 + , cast(field_24 as varchar) as field_24_text -- line 25 + , cast(field_25 as varchar) as field_25_text -- line 26 + , cast(field_26 as varchar) as field_26_text -- line 27 + , cast(field_27 as varchar) as field_27_text -- line 28 + , cast(field_28 as varchar) as field_28_text -- line 29 + , cast(field_29 as varchar) as field_29_text -- line 30 + , cast(field_30 as varchar) as field_30_text -- line 31 + , cast(field_31 as varchar) as field_31_text -- line 32 + , cast(field_32 as varchar) as field_32_text -- line 33 + , cast(field_33 as varchar) as field_33_text -- line 34 + , cast(field_34 as varchar) as field_34_text -- line 35 + , cast(field_35 as varchar) as field_35_text -- line 36 + , cast(field_36 as varchar) as field_36_text -- line 37 + , cast(field_37 as varchar) as field_37_text -- line 38 + , cast(field_38 as varchar) as field_38_text -- line 39 + , cast(field_39 as varchar) as field_39_text -- line 40 + , cast(field_40 as varchar) as field_40_text -- line 41 + , cast(field_41 as varchar) as field_41_text -- line 42 + , cast(field_42 as varchar) as field_42_text -- line 43 + , cast(field_43 as varchar) as field_43_text -- line 44 + , cast(field_44 as varchar) as field_44_text -- line 45 + , cast(field_45 as varchar) as field_45_text -- line 46 + , cast(field_46 as varchar) as field_46_text -- line 47 + , cast(field_47 as varchar) as field_47_text -- line 48 + , cast(field_48 as varchar) as field_48_text -- line 49 + , cast(field_49 as varchar) as field_49_text -- line 50 + , cast(field_50 as varchar) as field_50_text -- line 51 + , cast(field_51 as varchar) as field_51_text -- line 52 + , cast(field_52 as varchar) as field_52_text -- line 53 + , cast(field_53 as varchar) as field_53_text -- line 54 + , cast(field_54 as varchar) as field_54_text -- line 55 + , cast(field_55 as varchar) as field_55_text -- line 56 + , cast(field_56 as varchar) as field_56_text -- line 57 + , cast(field_57 as varchar) as field_57_text -- line 58 + , cast(field_58 as varchar) as field_58_text -- line 59 + , cast(field_59 as varchar) as field_59_text -- line 60 + , cast(field_60 as varchar) as field_60_text -- line 61 + , cast(field_61 as varchar) as field_61_text -- line 62 + , cast(field_62 as varchar) as field_62_text -- line 63 + , cast(field_63 as varchar) as field_63_text -- line 64 + , cast(field_64 as varchar) as field_64_text -- line 65 + , cast(field_65 as varchar) as field_65_text -- line 66 + , cast(field_66 as varchar) as field_66_text -- line 67 + , cast(field_67 as varchar) as field_67_text -- line 68 + , cast(field_68 as varchar) as field_68_text -- line 69 + , cast(field_69 as varchar) as field_69_text -- line 70 + , cast(field_70 as varchar) as field_70_text -- line 71 + , cast(field_71 as varchar) as field_71_text -- line 72 + , cast(field_72 as varchar) as field_72_text -- line 73 + , cast(field_73 as varchar) as field_73_text -- line 74 + , cast(field_74 as varchar) as field_74_text -- line 75 + , cast(field_75 as varchar) as field_75_text -- line 76 + , cast(field_76 as varchar) as field_76_text -- line 77 + , cast(field_77 as varchar) as field_77_text -- line 78 + , cast(field_78 as varchar) as field_78_text -- line 79 + , cast(field_79 as varchar) as field_79_text -- line 80 + , cast(field_80 as varchar) as field_80_text -- line 81 + , cast(field_81 as varchar) as field_81_text -- line 82 + , cast(field_82 as varchar) as field_82_text -- line 83 + , cast(field_83 as varchar) as field_83_text -- line 84 + , cast(field_84 as varchar) as field_84_text -- line 85 + , cast(field_85 as varchar) as field_85_text -- line 86 + , cast(field_86 as varchar) as field_86_text -- line 87 + , cast(field_87 as varchar) as field_87_text -- line 88 + , cast(field_88 as varchar) as field_88_text -- line 89 + , cast(field_89 as varchar) as field_89_text -- line 90 + , cast(field_90 as varchar) as field_90_text -- line 91 + , cast(field_91 as varchar) as field_91_text -- line 92 + , cast(field_92 as varchar) as field_92_text -- line 93 + , cast(field_93 as varchar) as field_93_text -- line 94 + , cast(field_94 as varchar) as field_94_text -- line 95 + , cast(field_95 as varchar) as field_95_text -- line 96 + , cast(field_96 as varchar) as field_96_text -- line 97 + , cast(field_97 as varchar) as field_97_text -- line 98 + , cast(field_98 as varchar) as field_98_text -- line 99 + , cast(field_99 as varchar) as field_99_text -- line 100 + , cast(field_100 as varchar) as field_100_text -- line 101 + , cast(field_101 as varchar) as field_101_text -- line 102 + , cast(field_102 as varchar) as field_102_text -- line 103 + , cast(field_103 as varchar) as field_103_t +… (truncated) +``` + +## RELATED FILES — unchanged dbt parents of the changed files (context only; comment on the changed files, not these): + +### models/staging/stg_orders.sql (model `stg_orders`, used by models/marts/fct_big.sql) +```sql +select id, amount from raw.orders +``` + +Return ONLY the JSON array per the output contract. +=====BATCH===== +PR title: Rework revenue marts +PR description: +Moves cents conversion into a macro. + +Also adds tests. + +## GROUNDING — deterministic engine findings (DO NOT repeat these): +- [warning] warehouse_cost · models/marts/fct_big.sql: SELECT * in a mart + +## CHANGED FILES (review for what the engine missed): + +### models/staging/stg_0.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_0 ++ b_0 + +``` +Current SQL: +```sql +select b_0 from {{ source('raw', 't0') }} +``` + +### models/staging/stg_1.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_1 ++ b_1 + +``` +Current SQL: +```sql +select b_1 from {{ source('raw', 't1') }} +``` + +### models/staging/stg_2.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_2 ++ b_2 + +``` +Current SQL: +```sql +select b_2 from {{ source('raw', 't2') }} +``` + +### models/staging/stg_3.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_3 ++ b_3 + +``` +Current SQL: +```sql +select b_3 from {{ source('raw', 't3') }} +``` + +### models/staging/stg_4.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_4 ++ b_4 + +``` +Current SQL: +```sql +select b_4 from {{ source('raw', 't4') }} +``` + +### models/staging/stg_5.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_5 ++ b_5 + +``` +Current SQL: +```sql +select b_5 from {{ source('raw', 't5') }} +``` + +### models/staging/stg_6.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_6 ++ b_6 + +``` +Current SQL: +```sql +select b_6 from {{ source('raw', 't6') }} +``` + +### models/staging/stg_7.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_7 ++ b_7 + +``` +Current SQL: +```sql +select b_7 from {{ source('raw', 't7') }} +``` + +### models/staging/stg_8.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_8 ++ b_8 + +``` +Current SQL: +```sql +select b_8 from {{ source('raw', 't8') }} +``` + +### models/staging/stg_9.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_9 ++ b_9 + +``` +Current SQL: +```sql +select b_9 from {{ source('raw', 't9') }} +``` + +### models/staging/stg_10.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_10 ++ b_10 + +``` +Current SQL: +```sql +select b_10 from {{ source('raw', 't10') }} +``` + +### models/staging/stg_11.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_11 ++ b_11 + +``` +Current SQL: +```sql +select b_11 from {{ source('raw', 't11') }} +``` + +### models/staging/stg_12.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_12 ++ b_12 + +``` +Current SQL: +```sql +select b_12 from {{ source('raw', 't12') }} +``` + +### models/staging/stg_13.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_13 ++ b_13 + +``` +Current SQL: +```sql +select b_13 from {{ source('raw', 't13') }} +``` + +### models/staging/stg_14.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_14 ++ b_14 + +``` +Current SQL: +```sql +select b_14 from {{ source('raw', 't14') }} +``` + +### models/staging/stg_15.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_15 ++ b_15 + +``` +Current SQL: +```sql +select b_15 from {{ source('raw', 't15') }} +``` + +### models/staging/stg_16.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_16 ++ b_16 + +``` +Current SQL: +```sql +select b_16 from {{ source('raw', 't16') }} +``` + +### models/staging/stg_17.sql (modified) +Diff: +```diff +@@ -1,2 +1,2 @@ + select +- a_17 ++ b_17 + +``` +Current SQL: +```sql +select b_17 from {{ source('raw', 't17') }} +``` + +### models/py/pm.py (added, Python model) +Diff: +```diff ++def model(dbt, session): + +``` +Current file: +```python +def model(dbt, session): + return dbt.ref('fct') + +``` + +Return ONLY the JSON array per the output contract. \ No newline at end of file diff --git a/packages/opencode/test/altimate/fixtures/review-ai-prompt-input.ts b/packages/opencode/test/altimate/fixtures/review-ai-prompt-input.ts new file mode 100644 index 0000000000..908af5207a --- /dev/null +++ b/packages/opencode/test/altimate/fixtures/review-ai-prompt-input.ts @@ -0,0 +1,64 @@ +// A representative AI-lane input whose default prompt is pinned by +// `review-ai-prompt-default.txt` (captured before the opt-in coverage and +// context-card switches existed): with both switches off the prompt must not change. +import { makeFinding } from "../../../src/altimate/review/finding" +import type { AiReviewFile, AiReviewInput } from "../../../src/altimate/review/ai-review" + +/** A diff with `hunks` hunks of `size` added lines each, spaced 100 lines apart. */ +function bigDiff(hunks: number, size: number): string { + const out: string[] = [] + for (let h = 0; h < hunks; h++) { + const start = 10 + h * 100 + out.push(`@@ -${start},${size + 2} +${start},${size + 4} @@`) + out.push(` select ${h} as ctx_before`) + for (let i = 0; i < size; i++) out.push(`+ , coalesce(col_${h}_${i}, 'unknown_value_${i}') as renamed_column_${h}_${i}`) + out.push(`- , old_col_${h}`) + out.push(` from {{ ref('stg_${h}') }}`) + } + return out.join("\n") + "\n" +} + +function bigSql(lines: number): string { + return Array.from({ length: lines }, (_, i) => ` , cast(field_${i} as varchar) as field_${i}_text -- line ${i + 1}`).join("\n") +} + +export function defaultPromptInput(): AiReviewInput { + const files: AiReviewFile[] = [ + { path: "models/marts/_marts.yml", status: "modified", model: "_marts.yml", kind: "schema_yml", diff: "+ - name: id\n+ tests: [unique]\n", sql: "models:\n - name: fct\n" }, + { path: "macros/to_cents.sql", status: "modified", model: "to_cents", kind: "macro", diff: "-round({{ c }})\n+round({{ c }} * 100)\n", sql: "{% macro to_cents(c) %}round({{ c }} * 100){% endmacro %}" }, + { path: "models/marts/fct_big.sql", status: "modified", model: "fct_big", kind: "model_sql", diff: bigDiff(5, 20), sql: bigSql(300) }, + { path: "tests/assert_positive.sql", status: "added", model: "assert_positive", kind: "test", diff: "+select * from {{ ref('fct') }} where id < 0\n", sql: "select * from {{ ref('fct') }} where id < 0" }, + { path: "seeds/codes.csv", status: "modified", model: "codes", kind: "seed", diff: "+US,United States\n" }, + { path: "snapshots/snap.sql", status: "modified", model: "snap", kind: "snapshot", diff: "+ unique_key='id',\n", sql: "{% snapshot snap %}select 1{% endsnapshot %}" }, + { path: "dbt_project.yml", status: "modified", model: "dbt_project.yml", kind: "project_config", diff: "+ +materialized: table\n", sql: "name: shop\n" }, + { path: "models/py/pm.py", status: "added", model: "pm", kind: "python_model", diff: "+def model(dbt, session):\n", sql: "def model(dbt, session):\n return dbt.ref('fct')\n" }, + { path: "macros/gone.sql", status: "deleted", model: "gone", kind: "macro", diff: "-x\n" }, + ...Array.from({ length: 18 }, (_, i): AiReviewFile => ({ + path: `models/staging/stg_${i}.sql`, + status: "modified", + model: `stg_${i}`, + kind: "model_sql", + diff: `@@ -1,2 +1,2 @@\n select\n- a_${i}\n+ b_${i}\n`, + sql: `select b_${i} from {{ source('raw', 't${i}') }}`, + })), + ] + return { + prTitle: "Rework revenue marts", + prBody: "Moves cents conversion into a macro.\n\nAlso adds tests.", + grounding: [ + makeFinding({ + severity: "warning", + category: "warehouse_cost", + title: "SELECT * in a mart", + body: "Project columns explicitly.", + file: "models/marts/fct_big.sql", + model: "fct_big", + }), + ], + context: [ + { path: "models/staging/stg_orders.sql", kind: "model", name: "stg_orders", forPaths: ["models/marts/fct_big.sql"], content: "select id, amount from raw.orders" }, + { path: "seeds/unused.csv", kind: "seed", name: "unused", forPaths: ["models/other.sql"], content: "a\n1" }, + ], + files, + } +} diff --git a/packages/opencode/test/altimate/review-ai-inputs.test.ts b/packages/opencode/test/altimate/review-ai-inputs.test.ts index 0a28606943..28ba434ae7 100644 --- a/packages/opencode/test/altimate/review-ai-inputs.test.ts +++ b/packages/opencode/test/altimate/review-ai-inputs.test.ts @@ -7,19 +7,26 @@ import { aiInputBudget, batchAiFiles, buildUserMessage, + excerptDiff, + excerptFile, laneUsage, + planAiInput, + runAiReview, selectAiFiles, + selectAiFilesByQuota, streamUsage, type AiLaneStatus, type AiReviewFile, type AiReviewInput, } from "../../src/altimate/review/ai-review" +import { renderContextCard, type ModelCard } from "../../src/altimate/review/ai-context-card" +import { defaultPromptInput } from "./fixtures/review-ai-prompt-input" import { inlineComments, renderSummary } from "../../src/altimate/review/format" import { makeFinding } from "../../src/altimate/review/finding" import { emitReviewRun } from "../../src/altimate/review/telemetry" import { Telemetry } from "../../src/altimate/telemetry" import { createDispatcherRunner } from "../../src/altimate/review/runner" -import { writeFileSync } from "node:fs" +import { readFileSync, writeFileSync } from "node:fs" import path from "node:path" import { tmpdir } from "../fixture/fixture" import type { ChangedFile } from "../../src/altimate/review/diff-filter" @@ -158,6 +165,29 @@ describe("files sent to the advisory AI lane", () => { expect(renderSummary(env)).toContain("Not reviewed: 1 file(s)") }) + test("coverage mode marks every quota-selected file reviewed, past the default file cap", async () => { + const seeds = Array.from({ length: 21 }, (_, i) => mod(`seeds/s${String(i).padStart(2, "0")}.csv`, "+a,b\n")) + const prev = process.env.ALTIMATE_REVIEW_AI_COVERAGE + process.env.ALTIMATE_REVIEW_AI_COVERAGE = "1" + try { + const env = await runReview({ + changedFiles: seeds, + config: { ...DEFAULT_REVIEW_CONFIG }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + getContent: async () => undefined, + generatedAt: "2026-11-14T00:00:00Z", + forceTier: "full", + aiReview: async () => ({ findings: [], status: { state: "ok" } }), + }) + expect(env.scope?.notReviewed ?? []).toEqual([]) + } finally { + if (prev === undefined) delete process.env.ALTIMATE_REVIEW_AI_COVERAGE + else process.env.ALTIMATE_REVIEW_AI_COVERAGE = prev + } + }) + test("a partial AI failure is shown even when the lane state is ok", async () => { const env = await runReview({ changedFiles: [mod("models/marts/m.sql")], @@ -458,3 +488,482 @@ describe("verifier evidence (opt-in)", () => { expect(ev.dialect).toBe(DEFAULT_REVIEW_CONFIG.dialect) }) }) + +describe("input coverage (opt-in ALTIMATE_REVIEW_AI_COVERAGE=1)", () => { + const COVERAGE = { ALTIMATE_REVIEW_AI_COVERAGE: "1" } + const f = (path: string, kind: string, changed = 1): AiReviewFile => ({ + path, + status: "modified", + kind, + model: path, + diff: "@@ -1,1 +1,1 @@\n" + "+x\n".repeat(changed), + }) + + test("with both switches unset the prompt is byte-identical to the pre-change snapshot", () => { + const input = defaultPromptInput() + const messages = [{}, { ALTIMATE_REVIEW_AI_BATCH_CHARS: "12000" }].flatMap((env) => planAiInput(input, env).messages) + const snapshot = readFileSync(path.join(import.meta.dir, "fixtures/review-ai-prompt-default.txt"), "utf8") + expect(messages.join("\n=====BATCH=====\n")).toBe(snapshot) + // Default selection is the old cap; nothing is recorded as unsent to the AI on the envelope. + expect(planAiInput(input, {}).files).toEqual(selectAiFiles(input.files)) + }) + + test("30 models + 3 macros + 2 yml: every macro and yml goes, models fill the rest up to 30", () => { + const models = Array.from({ length: 30 }, (_, i) => f(`m${i}.sql`, "model_sql", i + 1)) + const files = [...models, f("a.sql", "macro"), f("b.sql", "macro"), f("c.sql", "macro"), f("_a.yml", "schema_yml"), f("_b.yml", "source_yml")] + const { sent, unsent } = selectAiFilesByQuota(files) + expect(sent).toHaveLength(30) + const paths = sent.map((x) => x.path) + for (const p of ["a.sql", "b.sql", "c.sql", "_a.yml", "_b.yml"]) expect(paths).toContain(p) + // Most-changed models first; the five least-changed are left out. + expect(paths.slice(0, 3)).toEqual(["m29.sql", "m28.sql", "m27.sql"]) + expect(unsent.map((x) => x.path)).toEqual(["m0.sql", "m1.sql", "m2.sql", "m3.sql", "m4.sql"]) + // Today's cap would have sent no macro at all. + expect(selectAiFiles(files).some((x) => x.kind === "macro")).toBe(false) + }) + + test("unused quota flows to other kinds; env overrides the quotas", () => { + const files = [...Array.from({ length: 40 }, (_, i) => f(`m${i}.sql`, "model_sql")), f("mac.sql", "macro")] + const { sent } = selectAiFilesByQuota(files) + expect(sent.filter((x) => x.kind === "model_sql")).toHaveLength(29) + expect(sent.map((x) => x.path)).toContain("mac.sql") + const budget = aiInputBudget({ ...COVERAGE, ALTIMATE_REVIEW_AI_QUOTA_MODEL: "2", ALTIMATE_REVIEW_AI_QUOTA_MACRO: "1", ALTIMATE_REVIEW_AI_QUOTA_TEST: "1", ALTIMATE_REVIEW_AI_QUOTA_YML: "1" }) + expect(budget.coverage?.quotas).toEqual({ model: 2, macro: 1, test: 1, yml: 1 }) + expect(selectAiFilesByQuota(files, budget.coverage!.quotas).sent).toHaveLength(5) + expect(aiInputBudget({}).coverage).toBeUndefined() + }) + + test("coverage bounds the per-call size by default; the env still overrides it", () => { + expect(aiInputBudget(COVERAGE).batchChars).toBe(120_000) + expect(aiInputBudget({ ...COVERAGE, ALTIMATE_REVIEW_AI_BATCH_CHARS: "50000" }).batchChars).toBe(50_000) + expect(aiInputBudget({}).batchChars).toBe(Infinity) + // 30 files with ~16k-char diffs no longer land in one call. + const diff = "@@ -1,200 +1,200 @@\n" + ("+" + "x".repeat(79) + "\n").repeat(200) + const big = Array.from({ length: 30 }, (_, i) => ({ ...f(`m${i}.sql`, "model_sql"), diff })) + expect(batchAiFiles(big, aiInputBudget(COVERAGE)).length).toBeGreaterThan(1) + }) + + test("a 20k-char file with one hunk near char 15k: the hunk and its ±20-line window are sent", () => { + const lines = Array.from({ length: 400 }, (_, i) => ` , col_${String(i + 1).padStart(3, "0")} as alias_${String(i + 1).padStart(3, "0")} -- padding text, more padding`) + lines[299] = " , amount / 0 as broken_ratio" + const sql = lines.join("\n") + expect(sql.length).toBeGreaterThan(19_000) + expect(sql.indexOf("broken_ratio")).toBeGreaterThan(14_000) + const diff = "@@ -298,5 +298,5 @@\n" + [lines[297], lines[298]].map((l) => " " + l).join("\n") + "\n- , amount as ratio\n+" + lines[299] + "\n " + lines[300] + "\n " + lines[301] + "\n" + const file: AiReviewFile = { path: "models/wide.sql", status: "modified", model: "wide", diff, sql } + // Today: the head cut drops the changed line. + const today = buildUserMessage({ grounding: [], files: [file] }) + expect(today.slice(today.indexOf("Current SQL"))).not.toContain("broken_ratio") + const msg = buildUserMessage({ grounding: [], files: [file] }, 6_000, true) + const current = msg.slice(msg.indexOf("Current SQL")) + expect(current).toContain(lines[279]) // line 280 + expect(current).toContain(lines[319]) // line 320 + expect(current).toContain("broken_ratio") + const markers = [...current.matchAll(/… \(lines (\d+)–(\d+) omitted\)/g)].map((m) => [Number(m[1]), Number(m[2])]) + expect(markers.length).toBe(2) + // The head fills the budget up to the first gap; the gap ends at line 279; the tail runs to the last line. + expect(markers[0][1]).toBe(279) + expect(markers[1]).toEqual([321, 400]) + expect(excerptFile(sql, diff, 6_000).text.length).toBeLessThanOrEqual(6_000) + }) + + test("compiled SQL: windows follow the changed text, not the raw diff's line numbers", () => { + // Raw: the change is on line 12. Compiled: a Jinja loop above it expands into 300 lines. + const raw = ["select", ...Array.from({ length: 9 }, (_, i) => ` , c${i}`), " {% for x in xs %} , {{ x }} {% endfor %}", " , amount / nullif(qty, 0) as unit_price", "from t"] + const diff = "@@ -11,3 +11,3 @@\n " + raw[10] + "\n- , amount / qty as unit_price\n+" + raw[11] + "\n " + raw[12] + "\n" + const expanded = Array.from({ length: 300 }, (_, i) => ` , expanded_column_${String(i).padStart(3, "0")} -- generated by the loop`) + const compiled = [...raw.slice(0, 10), ...expanded, raw[11], ...Array.from({ length: 100 }, (_, i) => ` , tail_${i} -- more compiled output here`), "from t"].join("\n") + expect(compiled.length).toBeGreaterThan(6_000) + const ex = excerptFile(compiled, diff, 6_000) + expect(ex.text).toContain("amount / nullif(qty, 0) as unit_price") + expect(ex.text).toMatch(/… \(lines \d+–290 omitted\)/) // window starts 20 lines above compiled line 311 + // Raw content keeps raw line numbers. + const rawBig = [...raw, ...Array.from({ length: 300 }, (_, i) => ` -- trailing filler line ${i} with padding text`)].join("\n") + expect(excerptFile(rawBig, diff, 6_000).text).toContain(raw[11]) + // Changed text that cannot be found: head cut, marked approximate. + const lost = excerptFile(compiled.replace("nullif(qty, 0)", "nullif(qty, 1)"), diff, 6_000).text + expect(lost).toContain("windows would be approximate") + expect(lost).not.toContain("unit_price") + }) + + test("the diff gets its own 16k budget: a 20-hunk 14k diff goes whole, a 30k diff stops at 16k", () => { + const hunk = (k: number, size: number) => + `@@ -${10 + k * 100},${size} +${10 + k * 100},${size} @@\n` + Array.from({ length: size }, (_, i) => `+ , hub_hashkey_${k}_${i} as hk_${k}_${i} -- macro col`).join("\n") + const diffOf = (hunks: number, size: number) => Array.from({ length: hunks }, (_, k) => hunk(k, size)).join("\n") + "\n" + // The current file holds every added line at its head-side line number. + const fileLines = Array.from({ length: 4_000 }, (_, i) => ` , select_line_${i} -- current file content`) + for (let k = 0; k < 40; k++) for (let i = 0; i < 15; i++) fileLines[10 + k * 100 + i - 1] = ` , hub_hashkey_${k}_${i} as hk_${k}_${i} -- macro col` + const sql = fileLines.join("\n") + const small = diffOf(20, 15) + expect(small.length).toBeGreaterThan(13_000) + expect(small.length).toBeLessThan(16_000) + const file: AiReviewFile = { path: "macros/hub.sql", status: "modified", model: "hub", kind: "macro", diff: small, sql } + const plan = planAiInput({ grounding: [], files: [file] }, COVERAGE) + expect(plan.input[0]).toMatchObject({ hunksSent: 20, hunksTotal: 20, truncated: true }) + const msg = plan.messages[0] + expect(msg).toContain(hunk(19, 15)) + expect(msg).not.toContain("hunk omitted") + // Every hunk is in the diff, so the current file is a 2,000-char head. + const current = msg.slice(msg.indexOf("Current file:")) + expect(current).toContain("… (truncated)") + expect(current.length).toBeLessThan(2_200) + expect(plan.input[0].chars).toBe(msg.slice(msg.indexOf("\n### macros/hub.sql"), msg.indexOf("\n\nReturn ONLY")).length) + + const big = diffOf(40, 15) + expect(big.length).toBeGreaterThan(28_000) + const bigPlan = planAiInput({ grounding: [], files: [{ ...file, diff: big }] }, COVERAGE) + const rec = bigPlan.input[0] + expect(rec.hunksTotal).toBe(40) + expect(rec.hunksSent).toBeGreaterThan(20) + expect(rec.hunksSent).toBeLessThan(40) + const diffPart = bigPlan.messages[0].slice(bigPlan.messages[0].indexOf("Diff:"), bigPlan.messages[0].indexOf("Current file:")) + expect(diffPart.length).toBeLessThan(16_300) + expect(diffPart).toMatch(/more hunks omitted, at lines \d+–\d+/) + // Not every hunk went, so the current file keeps its ±20-line windows. + expect(bigPlan.messages[0]).toMatch(/Current file:[\s\S]*lines \d+–\d+ omitted/) + // The budget is env-overridable; the default lane still cuts the head at 6,000. + expect(aiInputBudget({ ...COVERAGE, ALTIMATE_REVIEW_AI_COVERAGE_DIFF_CHARS: "8000" }).coverage?.diffChars).toBe(8_000) + const plain = planAiInput({ grounding: [], files: [file] }, {}) + expect(plain.messages[0]).toBe(buildUserMessage({ grounding: [], files: [file] })) + expect(plain.input[0].hunksSent).toBeLessThan(20) + }) + + test("diff excerpts keep whole hunks and name the omitted ones by line; one oversized hunk keeps its head", () => { + const hunk = (start: number) => `@@ -${start},40 +${start},40 @@\n` + Array.from({ length: 40 }, (_, i) => `+ , value_${start}_${i} as column_${start}_${i}`).join("\n") + const diff = [10, 200, 400, 600, 800].map(hunk).join("\n") + "\n" + expect(diff.length).toBeGreaterThan(6_000) + const ex = excerptDiff(diff, 6_000) + expect(ex.text.length).toBeLessThanOrEqual(6_100) + expect(ex.hunksTotal).toBe(5) + expect(ex.hunksSent).toBeGreaterThan(0) + expect(ex.hunksSent).toBeLessThan(5) + // Every hunk sent is whole. + for (const start of [10, 200, 400, 600, 800].slice(0, ex.hunksSent)) expect(ex.text).toContain(hunk(start)) + const omitted = [10, 200, 400, 600, 800].slice(ex.hunksSent).map((s) => `${s}–${s + 39}`) + expect(ex.text).toContain(`omitted, at lines ${omitted.join(", ")})`) + const one = excerptDiff(hunk(5) + "\n" + hunk(5).split("\n").slice(1).join("\n").repeat(3), 1_000) + expect(one.text).toContain("… (hunk truncated)") + expect(one).toMatchObject({ hunksSent: 1, hunksTotal: 1, truncated: true }) + }) + + test("dry run records ai.input, lists files over quota as not reviewed and skips the model", async () => { + const saved = { ...process.env } + try { + Object.assign(process.env, COVERAGE, { ALTIMATE_REVIEW_AI_DRYRUN: "1" }) + const env = await runReview({ + changedFiles: [mod("models/marts/m.sql", "@@ -1 +1 @@\n-select 1\n+select 2 as id\n"), mod("models/marts/n.sql"), mod("macros/to_cents.sql")], + config: { ...DEFAULT_REVIEW_CONFIG }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + forceTier: "full", + getContent: async (p, side) => (side === "old" ? undefined : FILES[p] ?? "select 1"), + generatedAt: "2026-11-14T00:00:00Z", + aiReview: runAiReview, + }) + expect(env.ai?.state).toBe("skipped") + expect(env.ai?.reason).toContain("dry run") + expect(env.ai?.input?.map((r) => r.path).sort()).toEqual(["macros/to_cents.sql", "models/marts/m.sql", "models/marts/n.sql"]) + const m = env.ai?.input?.find((r) => r.path === "models/marts/m.sql") + expect(m).toMatchObject({ kind: "model_sql", truncated: false, hunksSent: 1, hunksTotal: 1, card: false }) + expect(m!.chars).toBeGreaterThan(0) + } finally { + for (const k of Object.keys(process.env)) if (!(k in saved)) delete process.env[k] + Object.assign(process.env, saved) + } + }) + + test("files over quota are an AI-lane note, not 'Not reviewed'; the default lane records nothing new", async () => { + const saved = { ...process.env } + // Quotas of 1 each make a total of 4: one macro and three of the six models (most changed first). + const changed = [ + mod("models/marts/m.sql", "@@ -1 +1 @@\n-select 1\n+select 2 as id\n"), + ...["n", "o", "p", "q", "r"].map((n) => mod(`models/marts/${n}.sql`)), + mod("macros/to_cents.sql"), + ] + const run = () => + runReview({ + changedFiles: changed, + config: { ...DEFAULT_REVIEW_CONFIG }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + forceTier: "full", + getContent: async (p, side) => (side === "old" ? undefined : FILES[p] ?? "select 1"), + generatedAt: "2026-11-14T00:00:00Z", + aiReview: runAiReview, + }) + try { + Object.assign(process.env, { ALTIMATE_REVIEW_AI_DRYRUN: "1" }) + const plain = await run() + expect(plain.ai?.input?.length).toBe(7) + expect(plain.ai?.unsent).toBeUndefined() + const one = { ALTIMATE_REVIEW_AI_QUOTA_MODEL: "1", ALTIMATE_REVIEW_AI_QUOTA_MACRO: "1", ALTIMATE_REVIEW_AI_QUOTA_TEST: "1", ALTIMATE_REVIEW_AI_QUOTA_YML: "1" } + Object.assign(process.env, COVERAGE, one) + const env = await run() + expect(env.ai?.input?.map((r) => r.path)).toEqual(["models/marts/m.sql", "models/marts/n.sql", "models/marts/o.sql", "macros/to_cents.sql"]) + // The deterministic lanes checked these files, so the ledger does not list them. + expect(env.ai?.unsent).toEqual(["models/marts/p.sql", "models/marts/q.sql", "models/marts/r.sql"]) + expect(env.scope?.notReviewed?.some((e) => /file quota/.test(e.reason)) ?? false).toBe(false) + const summary = renderSummary(env, { allowSilent: false }) + expect(summary).toContain("3 file(s) were not sent to the advisory AI review (file quota); the deterministic checks still ran on them") + expect(summary).not.toMatch(/Not reviewed[^\n]*\n[\s\S]*models\/marts\/p\.sql/) + } finally { + for (const k of Object.keys(process.env)) if (!(k in saved)) delete process.env[k] + Object.assign(process.env, saved) + } + }) +}) + +describe("dbt manifest context card (opt-in ALTIMATE_REVIEW_AI_CONTEXT_CARD=1)", () => { + const card: ModelCard = { + resourceType: "model", + materialized: "incremental", + uniqueKey: "order_id", + incrementalStrategy: "merge", + contractEnforced: true, + parents: ["stg_orders", "raw.payments"], + children: ["fct_revenue"], + exposures: ["finance_dash"], + columns: [{ name: "order_id", dataType: "int", tests: ["unique", "not_null"] }, { name: "amount", tests: [] }], + siblings: ["fct_refunds"], + } + + test("a model card names keys, parents, children, columns with tests and siblings", () => { + const text = renderContextCard({ name: "fct_orders", model: card }) + expect(text).toBe( + [ + "model `fct_orders`: materialized: incremental; strategy: merge; unique_key: order_id; contract: enforced", + "parents: stg_orders, raw.payments", + "children: fct_revenue", + "exposures: finance_dash", + "columns: order_id int [unique, not_null], amount", + "siblings: fct_refunds", + ].join("\n"), + ) + }) + + test("a macro card lists callers and adapter variants; long lists shrink to +N more within 600 chars", () => { + const text = renderContextCard({ + name: "hash", + macro: { name: "hash", callers: ["a", "b"], variants: ["snowflake__hash (macros/sf/hash.sql)", "default__hash (macros/hash.sql)"] }, + }) + expect(text).toBe("macro `hash`\ncallers: a, b\nvariants: snowflake__hash (macros/sf/hash.sql), default__hash (macros/hash.sql)") + const big = renderContextCard({ + name: "wide", + model: { ...card, children: Array.from({ length: 60 }, (_, i) => `downstream_model_number_${i}`), columns: Array.from({ length: 25 }, (_, i) => ({ name: `column_${i}`, tests: ["not_null"] })) }, + }) + expect(big.length).toBeLessThanOrEqual(600) + expect(big).toMatch(/children: .*\+\d+ more/) + }) + + test("the card sits right under the file header; a call holds at most 6,000 chars of cards", () => { + const files: AiReviewFile[] = Array.from({ length: 15 }, (_, i) => ({ path: `m${i}.sql`, status: "modified", model: `m${i}`, sql: "select 1", card: "c".repeat(500) })) + const plan = planAiInput({ grounding: [], files }, {}) + expect(plan.messages[0]).toContain("### m0.sql (modified)\ndbt context (from manifest):\n" + "c".repeat(500) + "\nCurrent SQL:") + expect(plan.input.filter((r) => r.card)).toHaveLength(12) + }) + + const cardRunner = { + ...runner, + async manifestAvailable() { + return true + }, + async modelCard(name: string) { + return name === "m" ? card : undefined + }, + async macroCard(name: string) { + return { name, callers: ["m"], variants: [] } + }, + async graphParents(paths: string[]) { + return paths[0].endsWith(".yml") ? [{ path: "models/marts/m.sql", kind: "model", name: "m", forPaths: paths }] : [] + }, + } as ReviewRunner + const run = async (aiContextCard: boolean, r: ReviewRunner = cardRunner) => { + let seen: AiReviewInput | undefined + await runReview({ + changedFiles: [mod("models/marts/m.sql"), mod("macros/to_cents.sql"), mod("models/marts/_marts.yml")], + config: { ...DEFAULT_REVIEW_CONFIG }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner: r, + forceTier: "full", + aiContextCard, + getContent: async (p, side) => (side === "old" ? undefined : FILES[p]), + generatedAt: "2026-11-14T00:00:00Z", + aiReview: async (input) => { + seen = input + return { findings: [], status: { state: "ok" } } + }, + }) + return new Map(seen!.files.map((f) => [f.path, f.card])) + } + + test("the orchestrator attaches cards only with the switch on and a manifest", async () => { + const on = await run(true) + expect(on.get("models/marts/m.sql")).toContain("model `m`") + expect(on.get("macros/to_cents.sql")).toBe("macro `to_cents`\ncallers: m") + expect(on.get("models/marts/_marts.yml")).toContain("model `m`") + expect([...(await run(false)).values()].every((c) => c === undefined)).toBe(true) + const noManifest = { + ...cardRunner, + async manifestAvailable() { + return false + }, + async impact() { + return { hasManifest: false, severity: "UNKNOWN", directCount: 0, transitiveCount: 0, testCount: 0 } + }, + } as ReviewRunner + expect([...(await run(true, noManifest)).values()].every((c) => c === undefined)).toBe(true) + }) + + test("modelCard and macroCard read the manifest and never throw", async () => { + await using tmp = await tmpdir() + const manifestPath = path.join(tmp.path, "manifest.json") + writeFileSync( + manifestPath, + JSON.stringify({ + metadata: { project_name: "shop" }, + nodes: { + "model.shop.fct": { + resource_type: "model", name: "fct", original_file_path: "models/marts/fct.sql", + config: { materialized: "incremental", unique_key: ["order_id", "line"], incremental_strategy: "merge", partition_by: { field: "day", data_type: "date" }, contract: { enforced: true } }, + columns: { order_id: { name: "order_id", data_type: "int" }, amount: { name: "amount" } }, + depends_on: { nodes: ["model.shop.stg", "source.shop.raw.orders"], macros: ["macro.shop.hash"] }, + }, + "model.shop.fct_refunds": { resource_type: "model", name: "fct_refunds", original_file_path: "models/marts/fct_refunds.sql", depends_on: { nodes: ["model.shop.stg"], macros: ["macro.shop.hash"] } }, + "model.shop.other_dir": { resource_type: "model", name: "other_dir", original_file_path: "models/x/other_dir.sql", depends_on: { nodes: ["model.shop.stg"] } }, + "model.shop.rev": { resource_type: "model", name: "rev", original_file_path: "models/marts/rev.sql", depends_on: { nodes: ["model.shop.fct"] } }, + "model.shop.stg": { resource_type: "model", name: "stg", original_file_path: "models/stg.sql", depends_on: { nodes: [] } }, + "test.shop.unique_fct_order_id": { resource_type: "test", name: "unique_fct_order_id", attached_node: "model.shop.fct", column_name: "order_id", test_metadata: { name: "unique" }, depends_on: { nodes: ["model.shop.fct"] } }, + }, + sources: { "source.shop.raw.orders": { source_name: "raw", name: "orders", original_file_path: "models/_sources.yml" } }, + exposures: { "exposure.shop.dash": { name: "dash", depends_on: { nodes: ["model.shop.fct"] } } }, + macros: { + "macro.shop.hash": { name: "hash", package_name: "shop", original_file_path: "macros/hash.sql" }, + "macro.shop.snowflake__hash": { name: "snowflake__hash", package_name: "shop", original_file_path: "macros/sf/hash.sql" }, + "macro.shop.default__hash": { name: "default__hash", package_name: "shop", original_file_path: "macros/hash.sql" }, + "macro.shop.hash_other": { name: "hash_other", package_name: "shop", original_file_path: "macros/o.sql" }, + }, + }), + ) + const r = createDispatcherRunner({ manifestPath }) + expect(await r.modelCard!("fct")).toEqual({ + resourceType: "model", + materialized: "incremental", + uniqueKey: "order_id, line", + incrementalStrategy: "merge", + partitionBy: "day", + clusterBy: undefined, + contractEnforced: true, + parents: ["stg", "raw.orders"], + children: ["rev"], + exposures: ["dash"], + columns: [{ name: "order_id", dataType: "int", tests: ["unique"] }, { name: "amount", tests: [] }], + siblings: ["fct_refunds"], + }) + expect(await r.modelCard!("missing")).toBeUndefined() + expect(await r.macroCard!("hash", { changedModels: ["fct_refunds"] })).toEqual({ + name: "hash", + callers: ["fct_refunds", "fct"], + variants: ["snowflake__hash (macros/sf/hash.sql)", "default__hash (macros/hash.sql)"], + }) + // A file whose base name is no macro resolves through its path. + expect((await r.macroCard!("sf_file", { path: "proj/macros/sf/hash.sql" }))?.variants).toEqual(["hash (macros/hash.sql)", "default__hash (macros/hash.sql)"]) + const broken = createDispatcherRunner({ manifestPath: path.join(tmp.path, "nope.json") }) + expect(await broken.modelCard!("fct")).toBeUndefined() + expect(await broken.macroCard!("hash")).toBeUndefined() + }) +}) + +describe("review feedback: input planning and card resolution", () => { + const COVERAGE = { ALTIMATE_REVIEW_AI_COVERAGE: "1" } + + test("a quota of 0 disables that bucket; invalid values keep the default", () => { + const q = aiInputBudget({ ...COVERAGE, ALTIMATE_REVIEW_AI_QUOTA_TEST: "0", ALTIMATE_REVIEW_AI_QUOTA_YML: "-1" }).coverage!.quotas + expect(q.test).toBe(0) + expect(q.yml).toBe(aiInputBudget(COVERAGE).coverage!.quotas.yml) + const t: AiReviewFile = { path: "tests/a.sql", status: "modified", kind: "test", model: "a", diff: "+x\n" } + expect(selectAiFilesByQuota([t], q).sent).toHaveLength(0) + }) + + test("compiled SQL with only Jinja or short added lines: context decides, else the head is marked approximate", () => { + const raw = ["select", ...Array.from({ length: 9 }, (_, i) => ` , column_number_${i}`), " {{ dbt_utils.star(ref('x')) }}", " , amount_total_value", "from t"] + // The added line is Jinja-only; context lines 10 and 12 sit at those raw line numbers. + const diff = "@@ -10,3 +10,3 @@\n " + raw[9] + "\n- {{ star(ref('x')) }}\n+" + raw[10] + "\n " + raw[11] + "\n" + const expanded = Array.from({ length: 300 }, (_, i) => ` , expanded_column_${String(i).padStart(3, "0")} -- generated by the macro`) + const compiled = [...raw.slice(0, 10), ...expanded, ...raw.slice(11)].join("\n") + expect(excerptFile(compiled, diff, 6_000).text).toContain("windows would be approximate") + // Raw content: the context matches, so raw line numbers are used for the window. + const rawBig = [...raw, ...Array.from({ length: 300 }, (_, i) => ` -- trailing filler line ${i} with padding text`)].join("\n") + const ex = excerptFile(rawBig, diff, 6_000).text + expect(ex).not.toContain("approximate") + expect(ex).toContain(raw[10]) + }) + + test("truncated is true when MAX_FILE_CHARS is below the full-diff file budget", () => { + const diff = "@@ -1,30 +1,30 @@\n" + Array.from({ length: 30 }, (_, i) => `+ , col_${i} as alias_${i}`).join("\n") + "\n" + expect(diff.length).toBeGreaterThan(500) + const file: AiReviewFile = { path: "models/m.sql", status: "modified", model: "m", diff, sql: "select\n" + " , padded_column_name_x\n".repeat(60) } + expect(file.sql!.length).toBeGreaterThan(1_000) + const plan = planAiInput({ grounding: [], files: [file] }, { ...COVERAGE, ALTIMATE_REVIEW_AI_MAX_FILE_CHARS: "500" }) + expect(plan.messages[0]).toContain("(truncated)") + expect(plan.input[0].truncated).toBe(true) + }) + + test("dry run is honoured before replay: no verifier call, ai.input recorded", async () => { + const saved = { ...process.env } + try { + Object.assign(process.env, { ALTIMATE_REVIEW_AI_DRYRUN: "1", ALTIMATE_REVIEW_AI_REPLAY: "/nonexistent/envelope.json", ALTIMATE_REVIEW_AI_VERIFY: "1" }) + const res = await runAiReview({ grounding: [], files: [{ path: "models/m.sql", status: "modified", model: "m", diff: "+select 1\n", sql: "select 1" }] }) + expect(res.findings).toEqual([]) + expect(res.status.state).toBe("skipped") + expect(res.status.reason).toContain("dry run") + expect(res.status.replayed).toBeUndefined() + expect(res.status.input?.map((r) => r.path)).toEqual(["models/m.sql"]) + } finally { + for (const k of Object.keys(process.env)) if (!(k in saved)) delete process.env[k] + Object.assign(process.env, saved) + } + }) + + test("cards resolve by path and project before name; yml cards come from patch_path", async () => { + await using tmp = await tmpdir() + const manifestPath = path.join(tmp.path, "manifest.json") + writeFileSync( + manifestPath, + JSON.stringify({ + metadata: { project_name: "shop" }, + nodes: { + // A package model with the same name, listed first. + "model.pkg.orders": { resource_type: "model", name: "orders", package_name: "pkg", original_file_path: "models/orders.sql", config: { materialized: "view" } }, + "model.shop.orders": { resource_type: "model", name: "orders", package_name: "shop", original_file_path: "models/marts/orders.sql", patch_path: "shop://models/marts/_marts.yml", config: { materialized: "table" } }, + "seed.shop.countries": { resource_type: "seed", name: "countries", package_name: "shop", original_file_path: "seeds/countries.csv", patch_path: "shop://seeds/_seeds.yml" }, + }, + macros: { + "macro.pkg.helpers": { name: "helpers", package_name: "pkg", original_file_path: "macros/helpers.sql" }, + "macro.shop.foo": { name: "foo", package_name: "shop", original_file_path: "macros/helpers.sql" }, + "macro.dbt_utils.date_spine": { name: "date_spine", package_name: "dbt_utils", original_file_path: "macros/date_spine.sql" }, + "macro.shop.date_spine": { name: "date_spine", package_name: "shop", original_file_path: "macros/spine.sql" }, + }, + }), + ) + const r = createDispatcherRunner({ manifestPath }) + expect((await r.modelCard!("orders"))?.materialized).toBe("table") + expect((await r.modelCard!("orders", { path: "dbt/models/marts/orders.sql" }))?.materialized).toBe("table") + expect((await r.macroCard!("helpers", { path: "macros/helpers.sql" }))?.name).toBe("foo") + expect((await r.macroCard!("date_spine", { path: "macros/date_spine.sql" }))?.name).toBe("date_spine") + expect(await r.patchedModels!("dbt/models/marts/_marts.yml")).toEqual(["orders"]) + expect(await r.patchedModels!("models/other.yml")).toEqual([]) + + // A schema yml with no generic tests still gets its model's card; a seed gets its own. + const files: AiReviewFile[] = [ + { path: "models/marts/_marts.yml", status: "modified", kind: "schema_yml", model: "_marts", diff: "+x\n" }, + { path: "seeds/countries.csv", status: "modified", kind: "seed", model: "countries", diff: "+x\n" }, + ] + const { attachContextCards } = await import("../../src/altimate/review/ai-context-card") + await attachContextCards(files, { ...r, graphParents: async () => [] }) + expect(files[0].card).toContain("model `orders`: materialized: table") + expect(files[1].card).toContain("seed `countries`") + }) +})