Repository navigation
fix(opencode): say when dbt_lineage answers from a partial dbt Fusion parse
#1855
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
Open
ralphstodomingo
wants to merge
3
commits into
main
Choose a base branch
from
fix/dbt-lineage-partial-graph-note
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+503
−9
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e251251
fix(dbt_lineage): say when the graph comes from a partial dbt Fusion …
ralphstodomingo 7cf3b17
fix(opencode): keep `partial_manifest` in the MCP result of adapter l…
ralphstodomingo 8f07229
fix(opencode): bound the `partial_manifest` errors and stop a cancell…
ralphstodomingo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import path from "path" | ||
| import type { ParsedManifest, PartialManifest } from "@altimateai/dbt-integration" | ||
|
|
||
| // Each adapter's last parse errors, kept while the manifest dbt Fusion wrote for it is missing what failed to parse. | ||
| const partial = new WeakMap<object, PartialManifest>() | ||
|
|
||
| /** Takes each manifest an adapter parsed: a later parse with no errors clears the mark. */ | ||
| export function recordPartialManifest(adapter: object, parsed: ParsedManifest): void { | ||
| if (parsed.partial?.errors.length) partial.set(adapter, parsed.partial) | ||
| else partial.delete(adapter) | ||
| } | ||
|
|
||
| /** Files named in the note's sentence; the rest are counted. */ | ||
| const NAMED_FILES = 5 | ||
| /** Errors the note carries; the rest are counted. Every answer carries the note, so a parse failing everywhere can't swell it. */ | ||
| const KEPT_ERRORS = 10 | ||
|
|
||
| /** | ||
| * What an answer built from the adapter's manifest adds, or undefined when its last parse had no errors. | ||
| * Paths inside `projectRoot` are given relative to it. | ||
| */ | ||
| export function partialManifestNote(adapter: object, projectRoot?: string) { | ||
| const mark = partial.get(adapter) | ||
| if (!mark) return undefined | ||
| const shown = (file: string) => { | ||
| if (!projectRoot) return file | ||
| const inside = path.relative(projectRoot, file) | ||
|
ralphstodomingo marked this conversation as resolved.
Resolved
|
||
| const outside = !inside || inside === ".." || inside.startsWith(`..${path.sep}`) || path.isAbsolute(inside) | ||
| return outside ? file : inside.split(path.sep).join("/") | ||
| } | ||
| const errors = mark.errors.map((e) => ({ file_path: shown(e.filePath), message: e.message })) | ||
|
ralphstodomingo marked this conversation as resolved.
Resolved
ralphstodomingo marked this conversation as resolved.
Resolved
|
||
| const files = [...new Set(errors.map((e) => e.file_path))] | ||
| const named = files.slice(0, NAMED_FILES) | ||
| const more = files.length - named.length | ||
| const list = | ||
| more > 0 | ||
| ? `${named.join(", ")} and ${more} more file${more === 1 ? "" : "s"}` | ||
| : named.length > 1 | ||
| ? `${named.slice(0, -1).join(", ")} and ${named[named.length - 1]}` | ||
| : named[0] | ||
| return { | ||
| message: `This graph is missing parts because ${list} failed to parse.`, | ||
| errors: errors.slice(0, KEPT_ERRORS), | ||
| ...(errors.length > KEPT_ERRORS && { omitted_errors: errors.length - KEPT_ERRORS }), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { describe, expect, spyOn, test } from "bun:test" | ||
| import { DBTProjectIntegrationAdapter, DBTProjectIntegrationAdapterEvents } from "@altimateai/dbt-integration" | ||
| import type { ParsedManifest } from "@altimateai/dbt-integration" | ||
| import { create } from "../src/adapter" | ||
| import { partialManifestNote, recordPartialManifest } from "../src/partial-manifest" | ||
|
|
||
| const ROOT = "/p" | ||
| const MISSING_REF = "Ref 'stg_orderz' not found in project. Searched for 'jaffle_shop.stg_orderz, stg_orderz'" | ||
|
|
||
| const parsed = (...errors: { filePath: string; message: string }[]) => | ||
| (errors.length ? { partial: { errors } } : {}) as ParsedManifest | ||
|
|
||
| describe("the partial manifest note", () => { | ||
| test("is absent for an adapter whose last parse had no errors", () => { | ||
| const adapter = {} | ||
| expect(partialManifestNote(adapter, ROOT)).toBeUndefined() | ||
| recordPartialManifest(adapter, parsed()) | ||
| expect(partialManifestNote(adapter, ROOT)).toBeUndefined() | ||
| }) | ||
|
|
||
| test("names the file that failed to parse, relative to the project, with its error", () => { | ||
| const adapter = {} | ||
| recordPartialManifest(adapter, parsed({ filePath: "/p/models/customers.sql", message: MISSING_REF })) | ||
| expect(partialManifestNote(adapter, ROOT)).toEqual({ | ||
| message: "This graph is missing parts because models/customers.sql failed to parse.", | ||
| errors: [{ file_path: "models/customers.sql", message: MISSING_REF }], | ||
| }) | ||
| }) | ||
|
|
||
| test("keeps a path outside the project, or one given with no project, as dbt wrote it", () => { | ||
| const adapter = {} | ||
| recordPartialManifest(adapter, parsed({ filePath: "/elsewhere/models/a.sql", message: "x" })) | ||
| expect(partialManifestNote(adapter, ROOT)?.errors[0]?.file_path).toBe("/elsewhere/models/a.sql") | ||
| // A sibling folder whose name starts with the project's is outside it. | ||
| recordPartialManifest(adapter, parsed({ filePath: "/p2/models/a.sql", message: "x" })) | ||
| expect(partialManifestNote(adapter, ROOT)?.errors[0]?.file_path).toBe("/p2/models/a.sql") | ||
| recordPartialManifest(adapter, parsed({ filePath: "models/a.sql", message: "x" })) | ||
| expect(partialManifestNote(adapter)?.errors[0]?.file_path).toBe("models/a.sql") | ||
| }) | ||
|
|
||
| test("names each file once, however many errors it has", () => { | ||
| const adapter = {} | ||
| recordPartialManifest( | ||
| adapter, | ||
| parsed( | ||
| { filePath: "/p/models/a.sql", message: "one" }, | ||
| { filePath: "/p/models/a.sql", message: "two" }, | ||
| { filePath: "/p/models/b.sql", message: "three" }, | ||
| ), | ||
| ) | ||
| const note = partialManifestNote(adapter, ROOT) | ||
| expect(note?.message).toBe("This graph is missing parts because models/a.sql and models/b.sql failed to parse.") | ||
| expect(note?.errors).toHaveLength(3) | ||
| }) | ||
|
|
||
| test("counts the files past the fifth instead of naming them", () => { | ||
| const adapter = {} | ||
| const errors = ["a", "b", "c", "d", "e", "f", "g"].map((name) => ({ filePath: `/p/models/${name}.sql`, message: "x" })) | ||
| recordPartialManifest(adapter, parsed(...errors)) | ||
| expect(partialManifestNote(adapter, ROOT)?.message).toBe( | ||
| "This graph is missing parts because models/a.sql, models/b.sql, models/c.sql, models/d.sql, models/e.sql and 2 more files failed to parse.", | ||
| ) | ||
| recordPartialManifest(adapter, parsed(...errors.slice(0, 6))) | ||
| expect(partialManifestNote(adapter, ROOT)?.message).toContain("models/e.sql and 1 more file failed to parse.") | ||
| }) | ||
|
|
||
| test("carries the first ten errors and counts the rest", () => { | ||
| const adapter = {} | ||
| const errors = Array.from({ length: 12 }, (_, i) => ({ filePath: `/p/models/m${i}.sql`, message: `error ${i}` })) | ||
| recordPartialManifest(adapter, parsed(...errors)) | ||
| const note = partialManifestNote(adapter, ROOT) | ||
| expect(note?.errors.map((e) => e.message)).toEqual(errors.slice(0, 10).map((e) => e.message)) | ||
| expect(note?.omitted_errors).toBe(2) | ||
| expect(note?.message).toContain("and 7 more files failed to parse.") | ||
| recordPartialManifest(adapter, parsed(...errors.slice(0, 10))) | ||
| expect(partialManifestNote(adapter, ROOT)).not.toHaveProperty("omitted_errors") | ||
| }) | ||
|
|
||
| test("goes once a later parse has no errors", () => { | ||
| const adapter = {} | ||
| recordPartialManifest(adapter, parsed({ filePath: "/p/models/customers.sql", message: MISSING_REF })) | ||
| recordPartialManifest(adapter, parsed()) | ||
| expect(partialManifestNote(adapter, ROOT)).toBeUndefined() | ||
| }) | ||
|
|
||
| test("is each adapter's own", () => { | ||
| const broken = {} | ||
| const whole = {} | ||
| recordPartialManifest(broken, parsed({ filePath: "/p/models/customers.sql", message: MISSING_REF })) | ||
| recordPartialManifest(whole, parsed()) | ||
| expect(partialManifestNote(broken, ROOT)).toBeDefined() | ||
| expect(partialManifestNote(whole, ROOT)).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe("create", () => { | ||
| test("hears the partial parse `initialize` runs, and the clean one after it", async () => { | ||
| const partial = { errors: [{ filePath: "/p/models/customers.sql", message: MISSING_REF }] } | ||
| const initialize = spyOn(DBTProjectIntegrationAdapter.prototype, "initialize").mockImplementation(async function ( | ||
| this: DBTProjectIntegrationAdapter, | ||
| ) { | ||
| this.emit(DBTProjectIntegrationAdapterEvents.MANIFEST_PARSED, { partial }) | ||
| }) | ||
| try { | ||
| const adapter = await create({ projectRoot: ROOT, pythonPath: "python3", dbtIntegration: "fusion", queryLimit: 500 }) | ||
| expect(partialManifestNote(adapter, ROOT)?.errors).toEqual([{ file_path: "models/customers.sql", message: MISSING_REF }]) | ||
| adapter.emit(DBTProjectIntegrationAdapterEvents.MANIFEST_PARSED, {}) | ||
| expect(partialManifestNote(adapter, ROOT)).toBeUndefined() | ||
| } finally { | ||
| initialize.mockRestore() | ||
| } | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.