Repository navigation
feat(review): opt-in AI input coverage, hunk excerpts and a dbt manifest context card #1771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/review-recall-rules
Are you sure you want to change the base?
Changes from all commits
ed17258
550ccc0
36def22
bf6e861
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,134 @@ | ||||||
| // 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 name, else by file path). */ | ||||||
| name: string | ||||||
| /** Models whose `depends_on.macros` include it (at most 10, models changed in the PR first). */ | ||||||
| callers: string[] | ||||||
| /** The dispatcher `<base>` and other `<adapter>__<base>` 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"]) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: Changed seeds never get a context card
|
||||||
| const DECLARING_KINDS = new Set(["schema_yml", "source_yml", "test"]) | ||||||
|
|
||||||
| interface CardSource { | ||||||
| modelCard?(name: string): Promise<ModelCard | undefined> | ||||||
| macroCard?(name: string, opts?: { path?: string; changedModels?: string[] }): Promise<MacroCard | undefined> | ||||||
| graphParents?(paths: string[]): Promise<Array<{ kind: string; name: string }>> | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Attach a rendered card to each file the manifest knows: models and snapshots | ||||||
| * get their own card, macros theirs, a yml or singular test the cards of the | ||||||
| * models it declares or tests (at most 3). Files without facts get none. Never throws. | ||||||
| */ | ||||||
| export async function attachContextCards(files: AiReviewFile[], runner: CardSource): Promise<void> { | ||||||
| const changedModels = files.filter((f) => MODEL_KINDS.has(f.kind ?? "model_sql")).map((f) => f.model) | ||||||
| const cardsFor = async (f: AiReviewFile): Promise<ContextCard[]> => { | ||||||
| const kind = f.kind ?? "model_sql" | ||||||
| if (MODEL_KINDS.has(kind)) { | ||||||
| const model = await runner.modelCard?.(f.model) | ||||||
| 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 parents = (await runner.graphParents?.([f.path])) ?? [] | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: The comment says a yml card covers 'the models it declares or tests', but Prompt for AI agents
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MINOR] YAML declaration cards depend on test-node parents, not
|
||||||
| const names = [...new Set(parents.filter((p) => p.kind === "model").map((p) => p.name))].slice(0, 3) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Schema and test changes attached to seed or snapshot nodes lose their related context card because this filter accepts only models. Include the resource kinds supported by Prompt for AI agents
Suggested change
|
||||||
| 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 | ||||||
| }), | ||||||
| ) | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Seed files never get a context card.
MODEL_KINDSleaves outseed, althoughModelCardandmodelCard()are documented to cover seeds. Addseedto the set, and drop thekind === "model"filter in the yml/test branch if seed parents should be carded there too.Prompt for AI agents