@@ -6,6 +6,7 @@ import { Dispatcher } from "../native"
66import type { DbtLineageResult } from "../native/types"
77import { loadRawManifest } from "../native/dbt/helpers"
88import { lineageGraph } from "../native/dbt/graph"
9+ import { projectRoot } from "../native/connections/registry"
910
1011/** The structured result of dbt-integration's `getModelLineage` (version 1), relayed as is. */
1112export type ModelLineage = {
@@ -59,7 +60,10 @@ export const DbtLineageTool = Tool.define("dbt_lineage", {
5960 // A call without write permission (MCP, `tool run`) and a plan-mode session's own calls are read-only;
6061 // a subagent plan mode starts runs under its own agent's permissions.
6162 const readOnly = ctx . extra ?. forceReadOnly === true || ctx . agent === "plan"
62- let viaAdapter = await fromAdapter ( args . model , args . column , ctx . abort , readOnly )
63+ // Read once per call, and only when needed: it reads the dbt config.
64+ let project : ReturnType < typeof sessionProject > | undefined
65+ const session = ( ) => ( project ??= sessionProject ( ) )
66+ let viaAdapter = await fromAdapter ( args . model , args . column , ctx . abort , readOnly , session )
6367 // The adapter answers for the project it found; a manifest named outside that project's folder
6468 // wins, so its graph and its lineage are never mixed with this one's.
6569 if ( viaAdapter . result && args . manifest_path ) {
@@ -70,7 +74,7 @@ export const DbtLineageTool = Tool.define("dbt_lineage", {
7074 const result = viaAdapter . result
7175 const what = result . focus . column ? `${ result . focus . name } .${ result . focus . column } ` : result . focus . name
7276 // A lineage viewer also wants the manifest's graph: exposures and tests sit there, not here.
73- const graph = args . include_graph ? manifestGraph ( args . manifest_path ?? ( await defaultManifestPath ( ) ) , result . focus . uniqueId ) : undefined
77+ const graph = args . include_graph ? manifestGraph ( args . manifest_path ?? ( await defaultManifestPath ( session ) ) , result . focus . uniqueId ) : undefined
7478 return {
7579 title : `dbt Lineage: ${ what } [${ result . confidence } ]` ,
7680 metadata : {
@@ -85,7 +89,7 @@ export const DbtLineageTool = Tool.define("dbt_lineage", {
8589 }
8690 }
8791 const notUsed = viaAdapter . reason ? `dbt integration not used: ${ viaAdapter . reason } ` : undefined
88- const manifestPath = args . manifest_path ?? ( await defaultManifestPath ( ) )
92+ const manifestPath = args . manifest_path ?? ( await defaultManifestPath ( session ) )
8993 ctx . abort . throwIfAborted ( )
9094 try {
9195 const result = await Dispatcher . call ( "dbt.lineage" , {
@@ -203,13 +207,15 @@ function manifestGraph(manifestPath: string, uniqueId: string): { graph?: Return
203207/**
204208 * The lineage from the project's dbt adapter, used only where writes are allowed: starting it runs
205209 * dbt deps and writes target/, and it compiles and reads column metadata. A read-only call never
206- * starts or uses it. `reason` says why it wasn't used.
210+ * starts or uses it, nor does a session in another dbt project than the adapter's. `reason` says why
211+ * it wasn't used.
207212 */
208213async function fromAdapter (
209214 model : string ,
210215 column : string | undefined ,
211216 signal : AbortSignal ,
212217 readOnly : boolean ,
218+ session : ( ) => ReturnType < typeof sessionProject > ,
213219) : Promise < { result ?: ModelLineage ; reason ?: string } > {
214220 try {
215221 const { getDbtAdapter, getDbtProjectRoot } = await import ( "../native/connections/register" )
@@ -219,6 +225,10 @@ async function fromAdapter(
219225 // target/, and compiling renders Jinja, which can run SQL.
220226 // It answers from the manifest as main does, with no note: there's nothing for the user to fix.
221227 if ( readOnly ) return { }
228+ const project = await session ( )
229+ if ( project ?. other && project . root ) {
230+ return { reason : `the dbt adapter is set up for ${ project . root } , not ${ project . other } ` }
231+ }
222232 // Starting the adapter can take a while (dbt deps, a parse): a cancel doesn't wait for it, and
223233 // past the deadline the manifest answers. The start goes on, for the next call.
224234 let stop = ( ) => { }
@@ -278,18 +288,29 @@ export function isModelLineage(value: unknown): value is ModelLineage {
278288 typeof v . confidence === "string" &&
279289 typeof v . stats === "object" &&
280290 v . stats !== null &&
291+ ! Array . isArray ( v . stats ) &&
292+ Object . values ( v . stats ) . every ( ( n ) => typeof n === "number" ) &&
281293 typeof v . focus ?. uniqueId === "string" &&
282294 typeof v . focus ?. name === "string" &&
295+ optional ( v . focus . column , "string" ) &&
283296 Array . isArray ( v . nodes ) &&
284297 v . nodes . every (
285298 ( n ) =>
286299 typeof n ?. uniqueId === "string" &&
287300 typeof n . name === "string" &&
288301 typeof n . type === "string" &&
302+ optional ( n . materialization , "string" ) &&
289303 ( n . direction === "focus" || n . direction === "upstream" || n . direction === "downstream" ) &&
290304 typeof n . depth === "number" &&
305+ Array . isArray ( n . parents ) &&
306+ n . parents . every ( ( p ) => typeof p === "string" ) &&
291307 Array . isArray ( n . columns ) &&
292- n . columns . every ( ( c ) => typeof c ?. name === "string" ) ,
308+ n . columns . every (
309+ ( c ) =>
310+ typeof c ?. name === "string" &&
311+ optional ( c . dataType , "string" ) &&
312+ ( c . isDocumentedOnly === undefined || c . isDocumentedOnly === true ) ,
313+ ) ,
293314 ) &&
294315 Array . isArray ( v . columnLineage ) &&
295316 v . columnLineage . every (
@@ -306,15 +327,50 @@ export function isModelLineage(value: unknown): value is ModelLineage {
306327 )
307328}
308329
330+ /** An optional field: absent, or of the type. */
331+ function optional ( value : unknown , type : "string" ) : boolean {
332+ return value === undefined || typeof value === type
333+ }
334+
309335/** A [node id, column] pair, as the lineage's edges name each end. */
310336function isPair ( value : unknown ) : value is [ string , string ] {
311337 return Array . isArray ( value ) && value . length === 2 && value . every ( ( part ) => typeof part === "string" )
312338}
313339
314- /** target/manifest.json in the project the dbt adapter finds, which may be above the working directory. */
315- async function defaultManifestPath ( ) : Promise < string > {
340+ /**
341+ * target/manifest.json in the project the dbt adapter finds, which may be above the session's
342+ * directory, or in the session's own project when that is another one.
343+ */
344+ async function defaultManifestPath ( session : ( ) => ReturnType < typeof sessionProject > ) : Promise < string > {
345+ const project = await session ( )
346+ const root = project ? ( project . other ?? project . root ) : await ( await import ( "../native/connections/register" ) ) . getDbtProjectRoot ( )
347+ return path . join ( root ?? "" , "target" , "manifest.json" )
348+ }
349+
350+ /**
351+ * The adapter's project root and, when the session's directory is in another dbt project, that one
352+ * (`other`, both links resolved): a dbt.json saved for one project, or a server started in one,
353+ * doesn't answer for a session in another. Undefined, the dbt config not read, when the session's
354+ * directory is in no dbt project: the adapter's own discovery (a dbt.json, or a walk up from the
355+ * process's working directory) decides then.
356+ */
357+ async function sessionProject ( ) : Promise < { root ?: string ; other ?: string } | undefined > {
358+ const { findProjectRoot } = await import ( "../../../../dbt-tools/src/config" )
359+ const found = findProjectRoot ( projectRoot ( ) )
360+ if ( ! found ) return undefined
316361 const { getDbtProjectRoot } = await import ( "../native/connections/register" )
317- return path . join ( ( await getDbtProjectRoot ( ) ) ?? "" , "target" , "manifest.json" )
362+ const root = await getDbtProjectRoot ( )
363+ const real = ( p : string ) => {
364+ try {
365+ return fs . realpathSync ( p )
366+ } catch {
367+ return path . resolve ( p )
368+ }
369+ }
370+ const here = real ( found )
371+ // path.relative ignores case on win32.
372+ if ( ! root ) return { other : here }
373+ return path . relative ( real ( root ) , here ) ? { root : real ( root ) , other : here } : { root }
318374}
319375
320376const MAX_TEXT_EDGES = 200
0 commit comments