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
base: main
Are you sure you want to change the base?
Changes from all commits
b7cfc04
1eb722e
1ee47f3
21d0494
3728f95
2c46312
60bca0f
2e84742
6d497bb
1aec5ac
b1e36d6
8c67725
a609c63
4845d65
d8c3469
dace1c2
769a7ac
18a3fc4
6024ac9
f5159c5
f57de3d
e65a30f
aaff414
3a98dff
4773866
d0d99c7
32848a3
2025857
cccd13f
910f061
2576266
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,154 @@ | ||
| import path from "node:path" | ||
| import z from "zod" | ||
| import { Tool } from "../../tool/tool" | ||
| import { projectRoot } from "../native/connections/registry" | ||
| import { isFailedRunStatus, readRunResults } from "../validators/validator-utils" | ||
|
|
||
| /** A message longer than this is cut: a build of thousands of nodes must stay readable and small. */ | ||
| const MAX_MESSAGE = 2000 | ||
| /** An id, name, status or command longer than this is cut: the artifact decides how long they are, the answer must not follow. */ | ||
| const MAX_IDENTIFIER = 300 | ||
| /** The rows one answer carries: a plugin reads at most 4 MiB of it, so a bigger build is counted, not listed. */ | ||
| const MAX_ROWS = 200 | ||
|
|
||
| export interface DbtRunResultRow { | ||
| uniqueId: string | ||
| name: string | ||
| status: string | ||
| executionTimeSec: number | null | ||
| message: string | null | ||
| } | ||
|
|
||
| export interface DbtRunResultsView { | ||
| found: boolean | ||
| /** The dbt project folder the artifact was read from. */ | ||
| projectDir: string | null | ||
| invocationId: string | null | ||
| command: string | null | ||
| elapsedSec: number | null | ||
| /** The artifact's modification time, ISO 8601. */ | ||
| completedAt: string | null | ||
| /** Every node dbt reported; `results` holds at most MAX_ROWS of them, the failures first. */ | ||
| total: number | ||
| passed: number | ||
| failed: number | ||
| skipped: number | ||
| results: DbtRunResultRow[] | ||
| } | ||
|
|
||
| const NONE: DbtRunResultsView = { | ||
| found: false, | ||
| projectDir: null, | ||
| invocationId: null, | ||
| command: null, | ||
| elapsedSec: null, | ||
| completedAt: null, | ||
| total: 0, | ||
| passed: 0, | ||
| failed: 0, | ||
| skipped: 0, | ||
| results: [], | ||
| } | ||
|
|
||
| /** A test's unique id ends in a hash (`test.shop.not_null_orders_id.f3a1c`): its name is the part before it. */ | ||
| function displayName(uniqueId: string, name: string): string { | ||
| const parts = uniqueId.split(".") | ||
| return parts[0] === "test" && parts.length === 4 ? (parts[2] ?? name).toLowerCase() : name | ||
| } | ||
|
|
||
| /** Statuses of a node dbt did not run. */ | ||
| const NOT_RUN = new Set(["skipped", "no-op"]) | ||
|
|
||
| function cut(text: string | null, max = MAX_MESSAGE): string | null { | ||
| if (text === null) return null | ||
| return text.length > max ? `${text.slice(0, max)}…` : text | ||
| } | ||
|
|
||
| /** | ||
| * The last dbt run of a project: `readRunResults` gives the path, the command and each row's status and | ||
| * message, the invocation id, the elapsed time and the execution times, all from one parse of the file. | ||
| */ | ||
| export async function readRunView(dbtRoot: string, targetPath?: string): Promise<DbtRunResultsView> { | ||
| const artifact = await readRunResults(dbtRoot, targetPath) | ||
| if (!artifact) return NONE | ||
| const all = artifact.results.map( | ||
| (node): DbtRunResultRow => ({ | ||
| uniqueId: cut(node.uniqueId, MAX_IDENTIFIER) ?? "", | ||
| name: cut(displayName(node.uniqueId, node.name), MAX_IDENTIFIER) ?? "", | ||
| status: cut(node.status, MAX_IDENTIFIER) ?? "", | ||
| executionTimeSec: node.executionTimeSec, | ||
| message: cut(node.message), | ||
| }), | ||
| ) | ||
| const failed = all.filter((row) => isFailedRunStatus(row.status) && !NOT_RUN.has(row.status)) | ||
| const skipped = all.filter((row) => NOT_RUN.has(row.status)).length | ||
| const failing = new Set(failed) | ||
| // The failures first, then the rest in the file's order, whatever the size: a reader that stops early still sees them. | ||
| const listed = [...failed, ...all.filter((row) => !failing.has(row))].slice(0, MAX_ROWS) | ||
| return { | ||
| found: true, | ||
| projectDir: dbtRoot, | ||
| invocationId: cut(artifact.invocationId, MAX_IDENTIFIER), | ||
| command: cut(artifact.command, MAX_IDENTIFIER), | ||
| elapsedSec: artifact.elapsedSec, | ||
| completedAt: new Date(artifact.mtimeMs).toISOString(), | ||
| total: all.length, | ||
| passed: all.length - failed.length - skipped, | ||
| failed: failed.length, | ||
| skipped, | ||
| results: listed, | ||
| } | ||
| } | ||
|
|
||
| export const DbtRunResultsTool = Tool.define("dbt_run_results", { | ||
| description: "The latest dbt run's results from target/run_results.json: command, duration and each node's status.", | ||
| description: | ||
| "Read the last dbt run of a project from its target/run_results.json (or the folder `target_path` names): the invocation id, the dbt command, the elapsed seconds, when it finished, the counts, and one row per node (the failures first, at most 200) with its status, execution seconds and message. Read-only; runs nothing. A compile or parse writes success rows without building anything, so look at `command`.", | ||
| parameters: z.object({ | ||
| project_dir: z | ||
| .string() | ||
| .optional() | ||
| .describe("dbt project folder (default: the dbt project found from the working directory)"), | ||
| .describe("A folder in the dbt project (its dbt_project.yml is looked for there and above; default: the project the session's directory is in, else the one the dbt adapter is configured for)"), | ||
| target_path: z | ||
| .string() | ||
| .optional() | ||
| .describe("The folder the dbt run wrote its artifacts to when it was not the project's own target folder (a relative one is from the project)"), | ||
| }), | ||
| async execute() { | ||
| async execute(args) { | ||
| const root = args.project_dir ? await projectAbove(path.resolve(projectRoot(), args.project_dir)) : await defaultRoot() | ||
| const view = await readRunView(root, args.target_path) | ||
| if (!view.found) { | ||
| return { | ||
| title: "dbt run results: none", | ||
| metadata: { ...view }, | ||
| output: `No readable run_results.json for the dbt project at ${root}${args.target_path?.trim() ? ` (in ${cut(args.target_path.trim(), MAX_IDENTIFIER)})` : ""}: no dbt command has written one there yet, or the file is not a run artifact.`, | ||
| } | ||
| } | ||
| const command = view.command ? `dbt ${view.command}` : "dbt (unknown command)" | ||
| return { | ||
| title: "dbt_run_results: not implemented", | ||
| metadata: { success: false, error: "not implemented" }, | ||
| output: "dbt_run_results is not implemented in this build of altimate-code yet.", | ||
| title: `${command}: ${view.passed}/${view.total} ok`, | ||
| metadata: { ...view }, | ||
| output: [ | ||
| `${command} · invocation ${view.invocationId ?? "unknown"} · ${view.elapsedSec ?? "?"}s · ${view.completedAt}`, | ||
| ...view.results.map( | ||
| (row) => `${row.status} ${row.name} ${row.executionTimeSec ?? "-"}s${row.message ? ` · ${row.message}` : ""}`, | ||
| ), | ||
| ...(view.total > view.results.length ? [`… and ${view.total - view.results.length} more nodes not listed`] : []), | ||
| ].join("\n"), | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| /** The dbt project `dir` is in: the closest folder at or above it holding dbt_project.yml, else `dir`. */ | ||
| async function projectAbove(dir: string): Promise<string> { | ||
| const { findProjectRoot } = await import("../../../../dbt-tools/src/config") | ||
| return findProjectRoot(dir) ?? dir | ||
| } | ||
|
|
||
| /** The dbt project the session's directory is in, else the one the dbt adapter is configured for (as the other dbt tools choose). */ | ||
| async function defaultRoot(): Promise<string> { | ||
| const { findProjectRoot } = await import("../../../../dbt-tools/src/config") | ||
| const here = findProjectRoot(projectRoot()) | ||
| if (here) return here | ||
| const { getDbtProjectRoot } = await import("../native/connections/register") | ||
| return (await getDbtProjectRoot()) ?? projectRoot() | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1191,6 +1191,8 @@ export interface RunResultNode { | |||||
| status: string | ||||||
| /** dbt's message for the node, when present. */ | ||||||
| message: string | null | ||||||
| /** dbt's `execution_time` for the node, when it is a finite number. */ | ||||||
| executionTimeSec: number | null | ||||||
| } | ||||||
|
|
||||||
| /** A parsed `run_results.json` plus its freshness. */ | ||||||
|
|
@@ -1212,6 +1214,10 @@ export interface RunResultsArtifact { | |||||
| * not evidence that anything was built. | ||||||
| */ | ||||||
| command: string | null | ||||||
| /** `metadata.invocation_id`, read from the same parse as `results`. */ | ||||||
| invocationId: string | null | ||||||
| /** `elapsed_time` in seconds, read from the same parse as `results`. */ | ||||||
| elapsedSec: number | null | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -1348,14 +1354,21 @@ export function isFailedRunStatus(status: string): boolean { | |||||
| * Read and parse `<target>/run_results.json`. Returns null when the artifact | ||||||
| * is absent or unparseable — callers decide what that means for their gate. | ||||||
| */ | ||||||
| export async function readRunResults(dbtRoot: string): Promise<RunResultsArtifact | null> { | ||||||
| const targetPath = await resolveDbtTargetPath(dbtRoot) | ||||||
| export async function readRunResults(dbtRoot: string, folder?: string): Promise<RunResultsArtifact | null> { | ||||||
| // `folder` is a run's own `--target-path`; a relative one is from the project. | ||||||
| const named = folder?.trim() | ||||||
| const targetPath = named ? (isAbsolutePath(named) ? named : join(dbtRoot, named)) : await resolveDbtTargetPath(dbtRoot) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: A Windows UNC or drive-rooted
Extending the helper keeps one definition for the env and function isAbsolutePath(p: string): boolean {
return p.startsWith("/") || p.startsWith("\\\\") || /^[A-Za-z]:[\\/]/.test(p)
}There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Windows UNC Prompt for AI agents
Suggested change
|
||||||
| const path = join(targetPath, "run_results.json") | ||||||
| try { | ||||||
| const stat = await fs.stat(path) | ||||||
| if (!stat.isFile()) return null | ||||||
| const raw = await fs.readFile(path, "utf8") | ||||||
| const parsed = JSON.parse(raw) as { results?: unknown; args?: unknown } | ||||||
| const parsed = JSON.parse(raw) as { | ||||||
| results?: unknown | ||||||
| args?: unknown | ||||||
| metadata?: { invocation_id?: unknown } | ||||||
| elapsed_time?: unknown | ||||||
| } | ||||||
| // A file that parses but carries no `results` array is not a run artifact. | ||||||
| // Returning an empty one would read as "a build happened and recorded | ||||||
| // nothing", which lets an unbuilt model through; null means "no evidence". | ||||||
|
|
@@ -1383,9 +1396,20 @@ export async function readRunResults(dbtRoot: string): Promise<RunResultsArtifac | |||||
| version: versioned ? last : null, | ||||||
| status: typeof r["status"] === "string" ? r["status"].toLowerCase() : "", | ||||||
| message: typeof r["message"] === "string" ? r["message"] : null, | ||||||
| executionTimeSec: | ||||||
| typeof r["execution_time"] === "number" && Number.isFinite(r["execution_time"]) ? r["execution_time"] : null, | ||||||
| }) | ||||||
| } | ||||||
| return { path, mtimeMs: stat.mtimeMs, results, command } | ||||||
| const id = parsed.metadata?.invocation_id | ||||||
| const elapsed = typeof parsed.elapsed_time === "number" && Number.isFinite(parsed.elapsed_time) ? parsed.elapsed_time : null | ||||||
| return { | ||||||
| path, | ||||||
| mtimeMs: stat.mtimeMs, | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: The rows,
The PR description says this read "detects a file rewritten mid-read via identity, size, and nanosecond timestamps"; that no longer holds. Reading through one handle keeps today's const fh = await fs.open(path, "r")
try {
const stat = await fh.stat()
if (!stat.isFile()) return null
const raw = await fh.readFile("utf8")
// ... parse, return mtimeMs: stat.mtimeMs
} finally {
await fh.close()
}A re-stat after the read that compares identity + size + mtime (returning |
||||||
| results, | ||||||
| command, | ||||||
| invocationId: typeof id === "string" && id ? id : null, | ||||||
| elapsedSec: elapsed, | ||||||
| } | ||||||
| } catch { | ||||||
| return null | ||||||
| } | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.