Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions packages/opencode/src/altimate/review/ai-context-card.ts
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"])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: Seed files never get a context card. MODEL_KINDS leaves out seed, although ModelCard and modelCard() are documented to cover seeds. Add seed to the set, and drop the kind === "model" filter in the yml/test branch if seed parents should be carded there too.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/opencode/src/altimate/review/ai-context-card.ts, line 95:

<comment>Seed files never get a context card. `MODEL_KINDS` leaves out `seed`, although `ModelCard` and `modelCard()` are documented to cover seeds. Add `seed` to the set, and drop the `kind === "model"` filter in the yml/test branch if seed parents should be carded there too.</comment>

<file context>
@@ -0,0 +1,134 @@
+  return text.slice(0, maxChars - 1) + "…"
+}
+
+const MODEL_KINDS = new Set(["model_sql", "python_model", "snapshot"])
+const DECLARING_KINDS = new Set(["schema_yml", "source_yml", "test"])
+
</file context>
Suggested change
const MODEL_KINDS = new Set(["model_sql", "python_model", "snapshot"])
const MODEL_KINDS = new Set(["model_sql", "python_model", "snapshot", "seed"])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: Changed seeds never get a context card

MODEL_KINDS omits "seed" and seeds are not in DECLARING_KINDS, so cardsFor returns [] for every seed file. But runner.modelCard explicitly resolves seed. nodes (runner.ts:543) and ModelCard is documented as facts about "one model, snapshot or seed", so that branch is unreachable through this feature. Either add "seed" to MODEL_KINDS (a changed seed is a legitimate review target) or drop the seed lookup from modelCard.

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])) ?? []

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P3: The comment says a yml card covers 'the models it declares or tests', but graphParents([f.path]) only returns models that generic tests in that yml depend on. A yml that declares a model without tests gets no card. Either fix the comment to say 'tests', or resolve declared models separately.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/opencode/src/altimate/review/ai-context-card.ts, line 122:

<comment>The comment says a yml card covers 'the models it declares or tests', but `graphParents([f.path])` only returns models that generic tests in that yml depend on. A yml that declares a model without tests gets no card. Either fix the comment to say 'tests', or resolve declared models separately.</comment>

<file context>
@@ -0,0 +1,134 @@
+      return macro ? [{ name: macro.name, macro }] : []
+    }
+    if (!DECLARING_KINDS.has(kind)) return []
+    const parents = (await runner.graphParents?.([f.path])) ?? []
+    const names = [...new Set(parents.filter((p) => p.kind === "model").map((p) => p.name))].slice(0, 3)
+    const cards = await Promise.all(names.map(async (name) => ({ name, model: await runner.modelCard?.(name) })))
</file context>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[MINOR] YAML declaration cards depend on test-node parents, not patch_path

const parents = (await runner.graphParents?.([f.path])) ?? []

graphParents links a YAML file to nodes via tests, while models are tied to their YAML by patch_path. A schema YAML with no generic tests gets no card even though the manifest has its models' config; removing the last test removes the card. The orchestrator test mocks graphParents, which hides this. Fix: resolve declarations through patch_path. (Codex, Claude)

const names = [...new Set(parents.filter((p) => p.kind === "model").map((p) => p.name))].slice(0, 3)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: 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 modelCard.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/opencode/src/altimate/review/ai-context-card.ts, line 123:

<comment>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 `modelCard`.</comment>

<file context>
@@ -0,0 +1,134 @@
+    }
+    if (!DECLARING_KINDS.has(kind)) return []
+    const parents = (await runner.graphParents?.([f.path])) ?? []
+    const names = [...new Set(parents.filter((p) => p.kind === "model").map((p) => p.name))].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 }] : []))
</file context>
Suggested change
const names = [...new Set(parents.filter((p) => p.kind === "model").map((p) => p.name))].slice(0, 3)
const names = [...new Set(parents.filter((p) => ["model", "seed", "snapshot"].includes(p.kind)).map((p) => p.name))].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
}),
)
}
Loading
Loading