Skip to content

Commit b654e92

Browse files
refactor(opencode): reshape dbt_lineage to the minimum on top of the reviewed base
The manifest answer is main's again; dbt.model_lineage and the depth and column params are gone. Read-only stays manifest-only, now checked in fromAdapter through the call's signal. The adapter wait keeps its deadline, cancels and shape check, with a fixed 3-hop depth. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VEmKQssMKzdJnQoPnWoCBf
1 parent 4900cc1 commit b654e92

10 files changed

Lines changed: 499 additions & 1069 deletions

File tree

‎packages/opencode/src/altimate/mcp-server/adapter.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import { EXPOSED, MCP_OPEN_WRITE_TOOLS, WRITE_TOOLS } from "../tool-cli/allow-li
1818
import { askFor, denied } from "../tool-cli/permissions"
1919
// altimate_change end
2020
import { closeScratch } from "../native/connections/scratch"
21+
// altimate_change start — read-only calls are known by their signal
22+
import { markReadOnlyCall } from "./read-only-calls"
23+
// altimate_change end
2124
import type { SessionID, MessageID } from "@/session/schema"
2225

2326
export const descriptions: Record<string, string> = {
@@ -461,6 +464,9 @@ export async function createAdapter(options: AdapterOptions = {}) {
461464
const retry = options.cli ? "Re-run" : "Restart"
462465
signal = AbortSignal.any([signal, shutdown.signal])
463466
signal.throwIfAborted()
467+
// altimate_change start — a helper that has only the signal can tell a read-only call
468+
if (!allowWrite) markReadOnlyCall(signal)
469+
// altimate_change end
464470
const tool = advertised.get(name)
465471
if (!tool) throw new Error(`Unknown tool: ${name}`)
466472
if (WRITE_TOOLS.has(name) && (options.cli || !MCP_OPEN_WRITE_TOOLS.has(name)) && !allowWrite) throw denied(name, retry)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// altimate_change - new file
2+
/**
3+
* The MCP calls that may not write, by their abort signal. The adapter marks each call it makes
4+
* with `forceReadOnly`; a tool's helper that gets the call's signal but not its context reads it
5+
* here. Tool.define passes `ctx.abort` through unchanged, so the signal identifies the call.
6+
*/
7+
const readOnly = new WeakSet<AbortSignal>()
8+
9+
export function markReadOnlyCall(signal: AbortSignal): void {
10+
readOnly.add(signal)
11+
}
12+
13+
export function isReadOnlyCall(signal: AbortSignal): boolean {
14+
return readOnly.has(signal)
15+
}

‎packages/opencode/src/altimate/native/dbt/lineage.ts‎

Lines changed: 1 addition & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import * as core from "@altimateai/altimate-core"
8-
import type { DbtLineageParams, DbtLineageResult, DbtModelLineageParams, ModelLineage } from "../types"
8+
import type { DbtLineageParams, DbtLineageResult } from "../types"
99
import { loadRawManifest, findModel, getUniqueId, detectDialect, buildSchemaContext } from "./helpers"
1010

1111
/**
@@ -73,236 +73,3 @@ export function dbtLineage(params: DbtLineageParams): DbtLineageResult {
7373
confidence_factors: rawLineage.error ? [String(rawLineage.error)] : [],
7474
}
7575
}
76-
77-
type LineageNode = ModelLineage["nodes"][number]
78-
type LineageEdge = ModelLineage["columnLineage"][number]
79-
80-
/**
81-
* A model's lineage from the manifest alone, in dbt-integration's `getModelLineage` format
82-
* (version 1): the model and its direct parents, with the column edges altimate-core reads from the
83-
* compiled SQL in the manifest. Nothing is compiled and no warehouse is read.
84-
*/
85-
export function dbtModelLineage(params: DbtModelLineageParams): ModelLineage {
86-
const startedAt = Date.now()
87-
const raw = dbtLineage(params)
88-
const errors = [...raw.confidence_factors]
89-
let manifest: any = null
90-
try {
91-
manifest = loadRawManifest(params.manifest_path)
92-
} catch {
93-
// dbtLineage has already reported it.
94-
}
95-
const nodes: Record<string, any> = manifest?.nodes ?? {}
96-
const sources: Record<string, any> = manifest?.sources ?? {}
97-
const model = manifest ? findModel(nodes, params.model) : null
98-
const dialect = params.dialect || (manifest ? detectDialect(manifest) : "")
99-
const focusId: string = model?.unique_id ?? raw.model_unique_id ?? params.model
100-
const focusName: string = model?.name ?? params.model
101-
const sql: string = model?.compiled_code || model?.compiled_sql || ""
102-
const parentIds: string[] = model ? [...new Set<string>(model.depends_on?.nodes ?? [])] : []
103-
104-
const result: LineageNode[] = []
105-
if (model) {
106-
const inResult = new Set([focusId, ...parentIds])
107-
const parentsOf = (node: any, self: string) =>
108-
[...new Set<string>(node?.depends_on?.nodes ?? [])].filter((p) => p !== self && inResult.has(p))
109-
result.push({
110-
uniqueId: focusId,
111-
name: focusName,
112-
type: "model",
113-
...(model.config?.materialized ? { materialization: model.config.materialized } : {}),
114-
direction: "focus",
115-
depth: 0,
116-
parents: parentIds,
117-
...(model.original_file_path ? { path: model.original_file_path } : {}),
118-
...outputColumns(sql, dialect, focusName, raw.raw_lineage, errors),
119-
})
120-
for (const uid of parentIds) {
121-
const node = nodes[uid] ?? sources[uid]
122-
const type: string = node?.resource_type ?? uid.split(".")[0]
123-
if (!node) errors.push(`${uid} is not in the manifest, so it has no columns`)
124-
const columns = Object.entries(node?.columns ?? {}).map(([key, c]: [string, any]) => ({
125-
name: c?.name || key,
126-
...(c?.data_type || c?.type ? { dataType: c.data_type || c.type } : {}),
127-
...(c?.description ? { description: c.description } : {}),
128-
}))
129-
const materialization = type === "source" || type === "seed" ? type : node?.config?.materialized
130-
result.push({
131-
uniqueId: uid,
132-
name:
133-
type === "source" && node
134-
? `${node.source_name}.${node.name}`
135-
: (node?.name ?? uid.split(".").slice(2).join(".")),
136-
type,
137-
...(materialization ? { materialization } : {}),
138-
direction: "upstream",
139-
depth: 1,
140-
parents: parentsOf(node, uid),
141-
...(node?.original_file_path ? { path: node.original_file_path } : {}),
142-
columns,
143-
columnsFrom: columns.length > 0 ? "manifest" : "none",
144-
})
145-
}
146-
}
147-
148-
const lineage = raw.raw_lineage as { column_lineage?: unknown; errors?: unknown }
149-
for (const e of Array.isArray(lineage.errors) ? lineage.errors : []) {
150-
errors.push(`${focusName}: ${typeof e === "string" ? e : (e?.message ?? JSON.stringify(e))}`)
151-
}
152-
const { byTable, shared } = tablesOf(parentIds, nodes, sources)
153-
if (shared.size > 0) {
154-
errors.push(
155-
`${focusName} reads ${[...shared].join(", ")}, which share a table name in altimate-core's schema, so their column edges into it were left out`,
156-
)
157-
}
158-
const edges = lineageEdges(
159-
Array.isArray(lineage.column_lineage) ? lineage.column_lineage : [],
160-
byTable,
161-
{ uniqueId: focusId, name: focusName },
162-
errors,
163-
shared,
164-
)
165-
const wanted = params.column?.toLowerCase()
166-
const columnLineage = wanted ? edges.filter((e) => e.target[1] === wanted) : edges
167-
return {
168-
version: 1,
169-
project: manifest?.metadata?.project_name ?? model?.package_name ?? "",
170-
dialect,
171-
focus: { uniqueId: focusId, name: focusName, ...(params.column ? { column: params.column } : {}) },
172-
nodes: result,
173-
columnLineage,
174-
confidence: errors.length === 0 ? "high" : "low",
175-
errors,
176-
stats: {
177-
nodes: result.length,
178-
edges: columnLineage.length,
179-
compiled: sql ? 1 : 0,
180-
fromWarehouse: 0,
181-
ms: Date.now() - startedAt,
182-
},
183-
}
184-
}
185-
186-
/**
187-
* The focus model's columns: its compiled SQL's output columns. A `select *` the SQL alone can't
188-
* expand takes the output columns altimate-core traced with the parents' schema instead, and says so.
189-
*/
190-
function outputColumns(sql: string, dialect: string, name: string, lineage: Record<string, unknown>, errors: string[]) {
191-
if (!sql) return { columns: [], columnsFrom: "none" }
192-
const resolved = Object.keys((lineage.column_dict as Record<string, unknown> | undefined) ?? {})
193-
let names: string[] = []
194-
let failure: unknown
195-
try {
196-
names = core.extractOutputColumns(sql, dialect)
197-
} catch (e) {
198-
failure = e
199-
}
200-
const explicit = names.filter((c) => c !== "*" && !c.endsWith(".*"))
201-
if (failure === undefined && explicit.length === names.length && names.length > 0) {
202-
return { columns: names.map((c) => ({ name: c })), columnsFrom: "compiled-sql" }
203-
}
204-
const why =
205-
failure !== undefined
206-
? `Could not read ${name}'s output columns (${failure instanceof Error ? failure.message : String(failure)})`
207-
: names.length === 0
208-
? `altimate-core read no output columns from ${name}'s compiled SQL`
209-
: `${name}'s compiled SQL selects *`
210-
if (resolved.length > 0) {
211-
errors.push(`${why}, so its columns are only the ones traced to a parent`)
212-
return { columns: resolved.map((c) => ({ name: c })), columnsFrom: "compiled-sql" }
213-
}
214-
errors.push(explicit.length > 0 ? `${why}, so not all its columns are known` : why)
215-
return { columns: explicit.map((c) => ({ name: c })), columnsFrom: explicit.length > 0 ? "compiled-sql" : "none" }
216-
}
217-
218-
/**
219-
* Each parent's table names, lowercased, as altimate-core can report them. The schema names
220-
* buildSchemaContext uses (`alias || name`) are claimed first, then the other names and a source's
221-
* identifier; the first parent to claim a name keeps it. `shared` holds the parents whose schema
222-
* name another parent has too: the schema keeps only one of them, so a column can bind to the wrong one.
223-
*/
224-
function tablesOf(parentIds: string[], nodes: Record<string, any>, sources: Record<string, any>) {
225-
const byTable = new Map<string, string>()
226-
const bySchemaName = new Map<string, string[]>()
227-
const claim = (key: unknown, uid: string) => {
228-
if (typeof key === "string" && key && !byTable.has(key.toLowerCase())) byTable.set(key.toLowerCase(), uid)
229-
}
230-
const nodeOf = (uid: string) => nodes[uid] ?? sources[uid]
231-
for (const uid of parentIds) {
232-
const schemaName = nodeOf(uid)?.alias || nodeOf(uid)?.name
233-
if (typeof schemaName !== "string" || !schemaName) continue
234-
claim(schemaName, uid)
235-
const key = schemaName.toLowerCase()
236-
bySchemaName.set(key, [...(bySchemaName.get(key) ?? []), uid])
237-
}
238-
for (const uid of parentIds) {
239-
for (const key of [nodeOf(uid)?.alias, nodeOf(uid)?.name, nodeOf(uid)?.identifier]) claim(key, uid)
240-
}
241-
const shared = new Set([...bySchemaName.values()].filter((ids) => ids.length > 1).flat())
242-
return { byTable, shared }
243-
}
244-
245-
/** A dotted SQL name's parts, unquoted: `"db"."schema"."t"."c"` gives db, schema, t, c. */
246-
function nameParts(name: string): string[] {
247-
const parts: string[] = []
248-
let current = ""
249-
let quote: string | undefined
250-
for (const ch of name) {
251-
if (quote) {
252-
if (ch === quote) quote = undefined
253-
else current += ch
254-
} else if (ch === '"' || ch === "`") quote = ch
255-
else if (ch === ".") {
256-
parts.push(current)
257-
current = ""
258-
} else current += ch
259-
}
260-
parts.push(current)
261-
return parts
262-
}
263-
264-
/**
265-
* altimate-core's column edges as `getModelLineage`'s, from a parent's column to the focus model's.
266-
* An edge whose table isn't one of the parents goes to `errors`; one from a parent in `leaveOut`
267-
* (already reported) is dropped. Edges are deduplicated: altimate-core 0.9 can report one edge under
268-
* a qualified and a bare table name.
269-
*/
270-
export function lineageEdges(
271-
raw: any[],
272-
byTable: Map<string, string>,
273-
focus: { uniqueId: string; name: string },
274-
errors: string[],
275-
leaveOut: Set<string> = new Set(),
276-
): LineageEdge[] {
277-
const seen = new Set<string>()
278-
const edges: LineageEdge[] = []
279-
for (const e of raw) {
280-
const from = nameParts(String(e?.source ?? ""))
281-
const column = from.length >= 2 ? from[from.length - 1].toLowerCase() : ""
282-
const table = from.length >= 2 ? from[from.length - 2].toLowerCase() : ""
283-
// The target is an output column's name, which can contain a dot (`as "customer.id"`).
284-
const target = String(e?.target ?? "").toLowerCase()
285-
const parent = byTable.get(table)
286-
if (!parent || !column || !target) {
287-
errors.push(
288-
`${e?.source} -> ${focus.name}.${e?.target}: ${table ? `${table} isn't one of ${focus.name}'s parents` : "no table"}, so the edge was left out`,
289-
)
290-
continue
291-
}
292-
if (leaveOut.has(parent)) continue
293-
const edge: LineageEdge = {
294-
source: [parent, column],
295-
target: [focus.uniqueId, target],
296-
type: String(e?.lineage_type ?? ""),
297-
viewsType: String(e?.lens_type ?? ""),
298-
viewsCode: (Array.isArray(e?.lens_code) ? e.lens_code : [])
299-
.map((step: any) => (typeof step === "string" ? step : step?.expression))
300-
.filter((code: unknown): code is string => typeof code === "string"),
301-
}
302-
const key = [...edge.source, ...edge.target, edge.type, edge.viewsType].join("\u0000")
303-
if (seen.has(key)) continue
304-
seen.add(key)
305-
edges.push(edge)
306-
}
307-
return edges
308-
}

‎packages/opencode/src/altimate/native/dbt/register.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import { register } from "../dispatcher"
66
import { runDbt } from "./runner"
77
import { parseManifest } from "./manifest"
8-
import { dbtLineage, dbtModelLineage } from "./lineage"
8+
import { dbtLineage } from "./lineage"
99
import { generateDbtUnitTests } from "./unit-tests"
1010
import type {
1111
DbtRunParams,
@@ -14,8 +14,6 @@ import type {
1414
DbtManifestResult,
1515
DbtLineageParams,
1616
DbtLineageResult,
17-
DbtModelLineageParams,
18-
ModelLineage,
1917
DbtUnitTestGenParams,
2018
DbtUnitTestGenResult,
2119
} from "../types"
@@ -35,10 +33,6 @@ register("dbt.lineage", async (params: DbtLineageParams): Promise<DbtLineageResu
3533
return dbtLineage(params)
3634
})
3735

38-
register("dbt.model_lineage", async (params: DbtModelLineageParams): Promise<ModelLineage> => {
39-
return dbtModelLineage(params)
40-
})
41-
4236
register("dbt.unit_test_gen", async (params: DbtUnitTestGenParams): Promise<DbtUnitTestGenResult> => {
4337
return generateDbtUnitTests(params)
4438
})

‎packages/opencode/src/altimate/native/types.ts‎

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,43 +1087,6 @@ export interface DbtLineageResult {
10871087
confidence_factors: string[]
10881088
}
10891089

1090-
export interface DbtModelLineageParams extends DbtLineageParams {
1091-
column?: string
1092-
}
1093-
1094-
/**
1095-
* dbt-integration's `getModelLineage` result (version 1). `dbt.model_lineage` builds the same
1096-
* shape from the manifest alone.
1097-
*/
1098-
export interface ModelLineage {
1099-
version: number
1100-
project: string
1101-
dialect: string
1102-
focus: { uniqueId: string; name: string; column?: string }
1103-
nodes: {
1104-
uniqueId: string
1105-
name: string
1106-
type: string
1107-
materialization?: string
1108-
direction: "focus" | "upstream" | "downstream"
1109-
depth: number
1110-
parents: string[]
1111-
path?: string
1112-
columns: { name: string; dataType?: string; description?: string; isDocumentedOnly?: true }[]
1113-
columnsFrom?: string
1114-
}[]
1115-
columnLineage: {
1116-
source: [string, string]
1117-
target: [string, string]
1118-
type: string
1119-
viewsType: string
1120-
viewsCode: string[]
1121-
}[]
1122-
confidence: string
1123-
errors: string[]
1124-
stats: Record<string, number>
1125-
}
1126-
11271090
// --- dbt Profile Discovery ---
11281091

11291092
export interface DbtProfilesParams {
@@ -1273,7 +1236,6 @@ export const BridgeMethods = {
12731236
"dbt.run": {} as { params: DbtRunParams; result: DbtRunResult },
12741237
"dbt.manifest": {} as { params: DbtManifestParams; result: DbtManifestResult },
12751238
"dbt.lineage": {} as { params: DbtLineageParams; result: DbtLineageResult },
1276-
"dbt.model_lineage": {} as { params: DbtModelLineageParams; result: ModelLineage },
12771239
"dbt.unit_test_gen": {} as { params: DbtUnitTestGenParams; result: DbtUnitTestGenResult },
12781240
"warehouse.list": {} as { params: WarehouseListParams; result: WarehouseListResult },
12791241
"warehouse.test": {} as { params: WarehouseTestParams; result: WarehouseTestResult },

0 commit comments

Comments
 (0)