Repository navigation
feat(tool): dbt_run_results reads the last dbt run's id, timings and per-node results #1729
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
hkaltai
wants to merge
14
commits into
main
Choose a base branch
from
feat/dbt-run-results-tool
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.
+1,075
−12
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b7cfc04
feat: foundation for the dbt Power User tools
hkaltai 1eb722e
Merge remote-tracking branch 'origin/main' into feat/pu-mods-foundation
hkaltai 1ee47f3
feat(tool): dbt_run_results reads the last dbt run's id, timings and …
hkaltai 21d0494
fix: harden the dbt Power User tool foundation
hkaltai 3728f95
fix(tool): dbt_run_results reads one coherent run, bounds its answer …
hkaltai 2c46312
fix(tool): dbt_run_results looks for the project above the folder it …
hkaltai 60bca0f
test(tool): name the nested-folder case as what it checks
hkaltai 2e84742
Merge branch 'feat/pu-mods-foundation' into feat/dbt-run-results-tool
hkaltai 6d497bb
Merge remote-tracking branch 'origin/main' into feat/dbt-run-results-…
hkaltai 1aec5ac
fix(tool): dbt_run_results reads each run from one parse, takes a tar…
hkaltai b1e36d6
fix(validators): a run_results.json rewritten during the read is no e…
hkaltai 8c67725
fix(validators): detect a rewrite during the run_results.json read by…
hkaltai a609c63
fix(validators): run_results.json keeps the filesystem's own mtime ro…
hkaltai 4845d65
fix(tool): review follow-ups — falsey API bodies, one stat snapshot f…
hkaltai 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
78 changes: 78 additions & 0 deletions
78
packages/opencode/src/altimate/tools/dbt-adapter-access.ts
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,78 @@ | ||
| import fs from "fs" | ||
| import path from "path" | ||
| import type { Tool } from "../../tool/tool" | ||
| import { projectRoot } from "../native/connections/registry" | ||
| import { sessionProject } from "./dbt-lineage" | ||
|
|
||
| /** Why a tool may not use the dbt adapter. */ | ||
| export type DbtAdapterRefusal = "read_only" | "other_project" | "no_project" | "not_started" | "not_loaded" | ||
|
|
||
| /** The project's dbt adapter, or why this call may not use it (`reason` is a sentence for the user). */ | ||
| export type DbtAdapterAccess = | ||
| | { adapter: any; refusal?: undefined; reason?: undefined } | ||
| | { adapter?: undefined; refusal: DbtAdapterRefusal; reason: string } | ||
|
|
||
| /** Test seams. */ | ||
| export const dbtAdapterAccessInternals = { | ||
| /** How long a call waits for the dbt adapter to start before it gives up; the start goes on for the next call. */ | ||
| adapterStartTimeoutMs: 120_000, | ||
| } | ||
|
|
||
| /** | ||
| * The dbt adapter for a tool call, which every adapter-backed tool goes through. Starting the adapter | ||
| * runs dbt deps and writes target/, and compiling renders Jinja, which can run SQL: a read-only call | ||
| * (no write permission, or plan mode) never starts or uses it. Nor does a session in a dbt project the | ||
| * adapter isn't set up for. The wait for the adapter to start is bounded and follows the call's abort. | ||
| */ | ||
| export async function dbtAdapterFor(ctx: Pick<Tool.Context, "abort" | "agent" | "extra">): Promise<DbtAdapterAccess> { | ||
| const signal = ctx.abort | ||
| if (ctx.extra?.forceReadOnly === true || ctx.agent === "plan") | ||
| return { | ||
| refusal: "read_only", | ||
| reason: "this needs the dbt adapter, which a read-only call doesn't start: re-run with --allow-write", | ||
| } | ||
| try { | ||
| const { getDbtAdapter, getDbtProjectRoot } = await import("../native/connections/register") | ||
| // The import yields: a call cancelled meanwhile starts nothing. | ||
| signal.throwIfAborted() | ||
| const project = await sessionProject() | ||
| signal.throwIfAborted() | ||
| if (project?.other) { | ||
| return { | ||
| refusal: "other_project", | ||
| reason: project.root | ||
| ? `the dbt adapter is set up for ${project.root}, not ${project.other}` | ||
| : `the dbt adapter isn't set up for ${project.other}`, | ||
| } | ||
| } | ||
| let stop = () => {} | ||
| let timer: ReturnType<typeof setTimeout> | undefined | ||
| const waited = new Promise<never>((_, reject) => { | ||
| stop = () => reject(signal.reason) | ||
| signal.addEventListener("abort", stop, { once: true }) | ||
| const seconds = dbtAdapterAccessInternals.adapterStartTimeoutMs / 1000 | ||
| timer = setTimeout( | ||
| () => reject(new Error(`the dbt adapter didn't start within ${seconds} s`)), | ||
| dbtAdapterAccessInternals.adapterStartTimeoutMs, | ||
| ) | ||
| }) | ||
| const adapter = await Promise.race([getDbtAdapter(), waited]).finally(() => { | ||
| signal.removeEventListener("abort", stop) | ||
| clearTimeout(timer) | ||
| }) | ||
| if (!adapter) { | ||
| // No adapter also when the project exists but the adapter failed to start: say which. | ||
| const root = await getDbtProjectRoot() | ||
| const found = !!root && fs.existsSync(path.join(root, "dbt_project.yml")) | ||
| return found | ||
| ? { refusal: "not_started", reason: "the dbt adapter didn't start" } | ||
| : { refusal: "no_project", reason: "no dbt project here" } | ||
| } | ||
| if (adapter.isInitialized?.() === false) | ||
| return { refusal: "not_loaded", reason: "the dbt adapter didn't load the project" } | ||
| return { adapter } | ||
| } catch (e) { | ||
| if (signal.aborted) throw e | ||
| return { refusal: "not_started", reason: e instanceof Error ? e.message : String(e) } | ||
| } | ||
| } |
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,17 @@ | ||
| import z from "zod" | ||
| import { Tool } from "../../tool/tool" | ||
|
|
||
| export const DbtCostEstimateTool = Tool.define("dbt_cost_estimate", { | ||
| description: "Not implemented yet: always returns an error. Estimate the bytes a dbt model or a SQL query would scan, from a warehouse dry run (BigQuery).", | ||
| parameters: z.object({ | ||
| model: z.string().optional().describe("Model name to compile and estimate"), | ||
| sql: z.string().optional().describe("SQL (may use Jinja) to compile and estimate"), | ||
| }), | ||
| async execute() { | ||
| return { | ||
| title: "dbt_cost_estimate: not implemented", | ||
| metadata: { success: false, error: "not implemented" }, | ||
| output: "dbt_cost_estimate is not implemented in this build of altimate-code yet.", | ||
| } | ||
| }, | ||
| }) |
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,17 @@ | ||
| import z from "zod" | ||
| import { Tool } from "../../tool/tool" | ||
|
|
||
| export const DbtCteProfileTool = Tool.define("dbt_cte_profile", { | ||
| description: "Not implemented yet: always returns an error. List a dbt model's CTEs, or time one CTE with a row-count query.", | ||
| parameters: z.object({ | ||
| model: z.string().describe("Model name"), | ||
| cte: z.string().optional().describe("CTE to time; without it, the CTE list is returned"), | ||
| }), | ||
| async execute() { | ||
| return { | ||
| title: "dbt_cte_profile: not implemented", | ||
| metadata: { success: false, error: "not implemented" }, | ||
| output: "dbt_cte_profile is not implemented in this build of altimate-code yet.", | ||
| } | ||
| }, | ||
| }) |
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,16 @@ | ||
| import z from "zod" | ||
| import { Tool } from "../../tool/tool" | ||
|
|
||
| export const DbtDeferStateTool = Tool.define("dbt_defer_state", { | ||
| description: "Not implemented yet: always returns an error. dbt Power User's defer-to-production setting for a dbt project, and the profile's default target.", | ||
| parameters: z.object({ | ||
| project_dir: z.string().describe("dbt project folder"), | ||
| }), | ||
| async execute() { | ||
| return { | ||
| title: "dbt_defer_state: not implemented", | ||
| metadata: { success: false, error: "not implemented" }, | ||
| output: "dbt_defer_state is not implemented in this build of altimate-code yet.", | ||
| } | ||
| }, | ||
| }) |
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,21 @@ | ||
| import z from "zod" | ||
| import { Tool } from "../../tool/tool" | ||
|
|
||
| export const DbtDocsCoverageTool = Tool.define("dbt_docs_coverage", { | ||
| description: | ||
| "Not implemented yet: always returns an error. Documentation coverage of a dbt model: which output columns have a description, and whether the model has one.", | ||
| parameters: z.object({ | ||
| model: z.string().describe("Model name"), | ||
| manifest_path: z | ||
| .string() | ||
| .optional() | ||
| .describe("manifest.json to read (default: the dbt project's target/manifest.json)"), | ||
| }), | ||
| async execute() { | ||
| return { | ||
| title: "dbt_docs_coverage: not implemented", | ||
| metadata: { success: false, error: "not implemented" }, | ||
| output: "dbt_docs_coverage is not implemented in this build of altimate-code yet.", | ||
| } | ||
| }, | ||
| }) |
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,16 @@ | ||
| import z from "zod" | ||
| import { Tool } from "../../tool/tool" | ||
|
|
||
| export const DbtHealthTool = Tool.define("dbt_health", { | ||
| description: "Not implemented yet: always returns an error. DataPilot project health insights for the dbt project.", | ||
| parameters: z.object({ | ||
| config_path: z.string().optional().describe("DataPilot config file to use"), | ||
| }), | ||
| async execute() { | ||
| return { | ||
| title: "dbt_health: not implemented", | ||
| metadata: { success: false, error: "not implemented" }, | ||
| output: "dbt_health is not implemented in this build of altimate-code yet.", | ||
| } | ||
| }, | ||
| }) |
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,21 @@ | ||
| import z from "zod" | ||
| import { Tool } from "../../tool/tool" | ||
|
|
||
| export const DbtMapRelationsTool = Tool.define("dbt_map_relations", { | ||
| description: "Not implemented yet: always returns an error. Map each table a raw SQL query reads to the dbt ref() or source() for it, from the manifest.", | ||
| parameters: z.object({ | ||
| sql: z.string().describe("Raw SQL to map"), | ||
| dialect: z.string().optional().describe("SQL dialect of the query"), | ||
| manifest_path: z | ||
| .string() | ||
| .optional() | ||
| .describe("manifest.json to match against (default: the dbt project's target/manifest.json)"), | ||
| }), | ||
| async execute() { | ||
| return { | ||
| title: "dbt_map_relations: not implemented", | ||
| metadata: { success: false, error: "not implemented" }, | ||
| output: "dbt_map_relations is not implemented in this build of altimate-code yet.", | ||
| } | ||
| }, | ||
| }) |
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.