Skip to content

Commit 6d71706

Browse files
hkaltaiclaude
andcommitted
fix: a manifest is placed as the manifest reader opens it, and only link hops count toward the loop limit
loadRawManifest opens a path through realpathSync, which collapses `..` before following links, so the project check does the same: the adapter's lineage and the graph read beside it always come from one project. Climbing to the nearest existing folder no longer counts toward the 64-link limit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5b67069 commit 6d71706

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

‎packages/opencode/src/altimate/tools/dbt-lineage.ts‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { Dispatcher } from "../native"
66
import type { DbtLineageResult } from "../native/types"
77
import { loadRawManifest } from "../native/dbt/helpers"
88
import { lineageGraph } from "../native/dbt/graph"
9-
import { Filesystem } from "../../util/filesystem"
109

1110
/** The structured result of dbt-integration's `getModelLineage` (version 1), relayed as is. */
1211
export type ModelLineage = {
@@ -131,21 +130,38 @@ export const DbtLineageTool = Tool.define("dbt_lineage", {
131130
* by its path, its nearest existing folder's links resolved, so the project's own default path keeps
132131
* the adapter's answer; one with no dbt project above it at all (a default path resolved against
133132
* another folder) names no other project either. A link to a manifest not written yet is placed where
134-
* it points; a link loop cannot be placed.
133+
* it points.
135134
*/
136135
async function manifestOutsideAdapterProject(manifestPath: string): Promise<string | undefined> {
137136
const { getDbtProjectRoot } = await import("../native/connections/register")
138137
const root = await getDbtProjectRoot()
139138
if (!root) return "the dbt adapter has no project root"
140-
// Where the system would open the path: links resolved segment by segment before `..` (so
141-
// `<project>/link/../x` lands where the link points), parts not written yet kept, a link to a missing
142-
// file followed. A relative path is joined to the working folder as written, never normalized first.
139+
// The path with its links resolved, the parts not written yet appended; a link to a missing file is
140+
// followed to where it points. `..` is collapsed first, as the manifest reader (loadRawManifest's
141+
// realpathSync) does, so this places the file that reader opens. Undefined past 64 links (a loop);
142+
// climbing to the nearest existing folder is not counted.
143143
const real = (p: string) => {
144-
try {
145-
return Filesystem.realLocation(path.isAbsolute(p) ? p : `${process.cwd()}${path.sep}${p}`)
146-
} catch {
147-
return undefined
144+
const rest: string[] = []
145+
let at = path.resolve(p)
146+
for (let links = 0; links < 64; ) {
147+
try {
148+
return path.join(fs.realpathSync(at), ...rest)
149+
} catch {}
150+
let link: string | undefined
151+
try {
152+
if (fs.lstatSync(at).isSymbolicLink()) link = fs.readlinkSync(at)
153+
} catch {}
154+
if (link !== undefined) {
155+
links++
156+
// From the link's real folder, as the system resolves it: `..` climbs from there.
157+
at = path.resolve(fs.realpathSync(path.dirname(at)), link)
158+
continue
159+
}
160+
if (path.dirname(at) === at) return undefined
161+
rest.unshift(path.basename(at))
162+
at = path.dirname(at)
148163
}
164+
return undefined
149165
}
150166
const projectDir = real(root)
151167
const manifest = real(manifestPath)

‎packages/opencode/test/altimate/dbt-lineage-structured.test.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,19 @@ describe("dbt_lineage with the project's dbt adapter", () => {
221221
await symlink("../../otherproj/target/manifest.json", path.join(realOther, "proj", "target", "manifest.json"))
222222
const climbed = await tool.execute({ model: "customers", manifest_path: path.join(mine, "linked_target", "manifest.json") }, ctx)
223223
expect(climbed.metadata.structuredContent).toBeUndefined()
224-
// `..` after a link climbs from where the link points, as the system opens it: a manifest reached
225-
// through `<mine>/out/..` is the other project's, though the path reads as this one's.
224+
// `..` after a link is collapsed first, as the manifest reader opens it: `<mine>/out/..` is read
225+
// as this project's manifest, so the adapter answers, matching the graph include_graph reads.
226226
await mkdir(path.join(other, "deep"), { recursive: true })
227227
await symlink(path.join(other, "deep"), path.join(mine, "out"))
228228
const viaDots = await tool.execute({ model: "customers", manifest_path: `${mine}/out/../target/manifest.json` }, ctx)
229-
expect(viaDots.metadata.structuredContent).toBeUndefined()
229+
expect(viaDots.metadata.structuredContent).toEqual(TRACE)
230+
// Many folders not written yet below the project are climbed, not counted as links.
231+
const deep = path.join(mine, ...Array.from({ length: 80 }, (_, i) => `d${i}`), "manifest.json")
232+
expect((await tool.execute({ model: "customers", manifest_path: deep }, ctx)).metadata.structuredContent).toEqual(TRACE)
233+
// A link loop cannot be placed.
234+
await symlink(path.join(mine, "target", "loop-b"), path.join(mine, "target", "loop-a"))
235+
await symlink(path.join(mine, "target", "loop-a"), path.join(mine, "target", "loop-b"))
236+
expect((await tool.execute({ model: "customers", manifest_path: path.join(mine, "target", "loop-a") }, ctx)).metadata.structuredContent).toBeUndefined()
230237
// A path through a file is not a folder: it is placed, not thrown on.
231238
const through = await tool.execute({ model: "customers", manifest_path: path.join(mine, "dbt_project.yml", "manifest.json") }, ctx)
232239
expect(through.metadata.structuredContent).toEqual(TRACE)

0 commit comments

Comments
 (0)