diff --git a/packages/opencode/src/altimate/tools/dbt-run-results.ts b/packages/opencode/src/altimate/tools/dbt-run-results.ts index c6f2cf396c..1b8b56ea2c 100644 --- a/packages/opencode/src/altimate/tools/dbt-run-results.ts +++ b/packages/opencode/src/altimate/tools/dbt-run-results.ts @@ -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 { + 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 { + 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 { + 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() +} diff --git a/packages/opencode/test/altimate/dbt-run-results.test.ts b/packages/opencode/test/altimate/dbt-run-results.test.ts new file mode 100644 index 0000000000..3f3cce4747 --- /dev/null +++ b/packages/opencode/test/altimate/dbt-run-results.test.ts @@ -0,0 +1,296 @@ +// dbt_run_results reads the last run of a project from fixture run_results.json files: a build +// (nodes that ran), a compile (success rows that ran nothing) and no file at all. No dbt, no warehouse. +import { afterAll, beforeAll, describe, expect, test } from "bun:test" +import { mkdtemp, mkdir, rm, utimes, writeFile } from "node:fs/promises" +import os from "node:os" +import path from "node:path" +import { Effect } from "effect" +import { SessionID, MessageID } from "@/session/schema" +import type { Tool } from "@/tool/tool" +import { dbtAdapterInternals, resetDbtAdapter } from "@/altimate/native/connections/register" +import { DbtRunResultsTool } from "@/altimate/tools/dbt-run-results" +import { Instance } from "@/project/instance" +import { initTool } from "./tool-fixture" + +const ctx: Tool.Context = { + sessionID: SessionID.make("ses_dbt_run_results"), + messageID: MessageID.make("msg_dbt_run_results"), + agent: "analyst", + abort: new AbortController().signal, + messages: [], + metadata: () => Effect.void, + ask: () => Effect.void, +} + +const BUILD = { + metadata: { invocation_id: "11111111-aaaa-bbbb-cccc-000000000001" }, + args: { which: "build" }, + elapsed_time: 12.5, + results: [ + { unique_id: "model.shop.stg_orders", status: "error", message: "Database Error: relation missing", execution_time: 1.25 }, + { unique_id: "model.shop.fct_sales", status: "success", message: "CREATE TABLE (10 rows)", execution_time: 3.5 }, + { unique_id: "test.shop.not_null_orders_id.abc", status: "pass", message: null, execution_time: 0.5 }, + { unique_id: "model.shop.dim_accounts.v2", status: "skipped", message: "SKIP", execution_time: 0 }, + ], +} + +const COMPILE = { + metadata: { invocation_id: "22222222-aaaa-bbbb-cccc-000000000002" }, + args: { which: "compile" }, + elapsed_time: 2, + results: [{ unique_id: "model.shop.fct_sales", status: "success", message: null, execution_time: 0.01 }], +} + +let root: string +const run = async (dir: string | undefined, target_path?: string) => + (await initTool(DbtRunResultsTool)).execute({ ...(dir === undefined ? {} : { project_dir: dir }), ...(target_path ? { target_path } : {}) }, ctx) + +async function project(name: string, artifact?: unknown) { + const dir = path.join(root, name) + await mkdir(path.join(dir, "target"), { recursive: true }) + await writeFile(path.join(dir, "dbt_project.yml"), `name: ${name}\n`) + if (artifact !== undefined) await writeFile(path.join(dir, "target", "run_results.json"), JSON.stringify(artifact)) + return dir +} + +const savedTarget = process.env["DBT_TARGET_PATH"] +beforeAll(async () => { + delete process.env["DBT_TARGET_PATH"] + root = await mkdtemp(path.join(os.tmpdir(), "dbt-run-results-")) +}) +afterAll(async () => { + if (savedTarget !== undefined) process.env["DBT_TARGET_PATH"] = savedTarget + await rm(root, { recursive: true, force: true }) +}) + +describe("dbt_run_results", () => { + test("a build: invocation id, command, elapsed seconds, per-node status, seconds and message", async () => { + const dir = await project("build", BUILD) + const when = new Date("2026-11-01T10:00:00.000Z") + await utimes(path.join(dir, "target", "run_results.json"), when, when) + + const result = await run(dir) + + expect(result.metadata).toEqual({ + truncated: false, + found: true, + projectDir: expect.any(String), + invocationId: "11111111-aaaa-bbbb-cccc-000000000001", + command: "build", + elapsedSec: 12.5, + completedAt: "2026-11-01T10:00:00.000Z", + total: 4, + passed: 2, + failed: 1, + skipped: 1, + results: [ + { uniqueId: "model.shop.stg_orders", name: "stg_orders", status: "error", executionTimeSec: 1.25, message: "Database Error: relation missing" }, + { uniqueId: "model.shop.fct_sales", name: "fct_sales", status: "success", executionTimeSec: 3.5, message: "CREATE TABLE (10 rows)" }, + { uniqueId: "test.shop.not_null_orders_id.abc", name: "not_null_orders_id", status: "pass", executionTimeSec: 0.5, message: null }, + { uniqueId: "model.shop.dim_accounts.v2", name: "dim_accounts", status: "skipped", executionTimeSec: 0, message: "SKIP" }, + ], + }) + expect(result.title).toBe("dbt build: 2/4 ok") + expect(result.output).toContain("error stg_orders 1.25s") + }) + + test("a compile is reported as a compile: its success rows ran nothing", async () => { + const dir = await project("compile", COMPILE) + + const result = await run(dir) + + expect(result.metadata).toEqual({ + truncated: false, + found: true, + projectDir: expect.any(String), + invocationId: "22222222-aaaa-bbbb-cccc-000000000002", + command: "compile", + elapsedSec: 2, + completedAt: expect.any(String), + total: 1, + passed: 1, + failed: 0, + skipped: 0, + results: [{ uniqueId: "model.shop.fct_sales", name: "fct_sales", status: "success", executionTimeSec: 0.01, message: null }], + }) + expect(result.title).toBe("dbt compile: 1/1 ok") + }) + + test("a run written with --target-path is read from that folder, relative to the project or absolute; the project's own target is not", async () => { + const dir = await project("elsewhere", COMPILE) + await mkdir(path.join(dir, "ci-out"), { recursive: true }) + await writeFile(path.join(dir, "ci-out", "run_results.json"), JSON.stringify(BUILD)) + const other = path.join(root, "absolute-out") + await mkdir(other, { recursive: true }) + await writeFile(path.join(other, "run_results.json"), JSON.stringify(BUILD)) + + expect((await run(dir)).metadata.command).toBe("compile") + const relative = await run(dir, "ci-out") + expect(relative.metadata.command).toBe("build") + expect(relative.metadata.invocationId).toBe("11111111-aaaa-bbbb-cccc-000000000001") + expect(relative.metadata.results[0]).toEqual({ uniqueId: "model.shop.stg_orders", name: "stg_orders", status: "error", executionTimeSec: 1.25, message: "Database Error: relation missing" }) + expect((await run(dir, other)).metadata.command).toBe("build") + const missing = await run(dir, "nowhere") + expect(missing.metadata.found).toBe(false) + expect(missing.output).toContain("(in nowhere)") + }) + + test("no run_results.json: found is false and the answer is empty, not an error", async () => { + const dir = await project("empty") + + const result = await run(dir) + + expect(result.metadata).toEqual({ + truncated: false, + found: false, + projectDir: null, + invocationId: null, + command: null, + elapsedSec: null, + completedAt: null, + total: 0, + passed: 0, + failed: 0, + skipped: 0, + results: [], + }) + expect(result.title).toBe("dbt run results: none") + }) + + test("an artifact without an invocation id or timings keeps its rows", async () => { + const dir = await project("bare", { args: { which: "run" }, results: [{ unique_id: "model.shop.a", status: "success" }] }) + + const result = await run(dir) + + expect(result.metadata).toEqual({ + truncated: false, + found: true, + projectDir: expect.any(String), + invocationId: null, + command: "run", + elapsedSec: null, + completedAt: expect.any(String), + total: 1, + passed: 1, + failed: 0, + skipped: 0, + results: [{ uniqueId: "model.shop.a", name: "a", status: "success", executionTimeSec: null, message: null }], + }) + }) + + test("a long message is cut", async () => { + const dir = await project("long", { + metadata: { invocation_id: "x" }, + args: { which: "run" }, + results: [{ unique_id: "model.shop.a", status: "error", message: "e".repeat(5000), execution_time: 1 }], + }) + + const result = await run(dir) + + const message = (result.metadata as any).results[0].message as string + expect(message).toBe(`${"e".repeat(2000)}…`) + }) + + test("a build of many nodes lists the failures first, at most 200 rows, and counts every node", async () => { + const results = Array.from({ length: 450 }, (_, i) => ({ + unique_id: `model.shop.m${i}`, + status: i >= 400 ? "error" : "success", + message: null, + execution_time: 1, + })) + const dir = await project("many", { metadata: { invocation_id: "m" }, args: { which: "build" }, elapsed_time: 9, results }) + + const view = (await run(dir)).metadata as any + + expect([view.total, view.passed, view.failed, view.skipped]).toEqual([450, 400, 50, 0]) + expect(view.results).toHaveLength(200) + expect(view.results.slice(0, 50).every((row: any) => row.status === "error")).toBe(true) + expect(view.results[50].name).toBe("m0") + }) + + test("an artifact with no command is titled as unknown, not as a run", async () => { + const dir = await project("nocmd", { results: [{ unique_id: "model.shop.a", status: "success" }] }) + + expect((await run(dir)).title).toBe("dbt (unknown command): 1/1 ok") + }) + + test("a corrupt artifact is not found, and the answer says it may not be a run artifact", async () => { + const dir = await project("corrupt") + await writeFile(path.join(dir, "target", "run_results.json"), "{not json") + + const result = await run(dir) + + expect((result.metadata as any).found).toBe(false) + expect(result.output).toContain("not a run artifact") + }) + + test("a missing target_path longer than the bound is cut in the answer", async () => { + const dir = await project("long-target", BUILD) + + const result = await run(dir, "y".repeat(5000)) + + expect((result.metadata as any).found).toBe(false) + expect(result.output).toContain(`(in ${"y".repeat(300)}…)`) + expect(result.output.length).toBeLessThan(1000) + }) + + test("a node id longer than the bound is cut, so the answer stays small", async () => { + const long = "x".repeat(5000) + const dir = await project("long-id", { + metadata: { invocation_id: "33333333-aaaa-bbbb-cccc-000000000003" }, + args: { which: "run" }, + elapsed_time: 1, + results: [{ unique_id: `model.shop.${long}`, status: "success", message: null, execution_time: 1 }], + }) + + const row = ((await run(dir)).metadata as any).results[0] + + expect(row.uniqueId).toBe(`model.shop.${"x".repeat(5000)}`.slice(0, 300) + "…") + expect(row.name).toBe("x".repeat(300) + "…") + }) + + test("a folder inside the project reads that project", async () => { + const dir = await project("nested", BUILD) + await mkdir(path.join(dir, "models", "marts"), { recursive: true }) + + const view = (await run(path.join(dir, "models", "marts"))).metadata as any + + expect(view.projectDir).toBe(dir) + expect(view.invocationId).toBe("11111111-aaaa-bbbb-cccc-000000000001") + }) + + test("with no project_dir, the project the session's folder is in is read before the adapter's", async () => { + const here = await project("session-project", BUILD) + const other = await project("adapter-project", COMPILE) + dbtAdapterInternals.readConfig = async () => ({ projectRoot: other }) + resetDbtAdapter() + try { + const result = await Instance.provide({ directory: here, fn: () => run(undefined) }) + + expect((result.metadata as any).invocationId).toBe("11111111-aaaa-bbbb-cccc-000000000001") + } finally { + delete dbtAdapterInternals.readConfig + resetDbtAdapter() + } + }) + + test("an invocation id longer than the bound is cut", async () => { + const dir = await project("long-invocation", { ...BUILD, metadata: { invocation_id: "i".repeat(5000) } }) + + expect(((await run(dir)).metadata as any).invocationId).toBe("i".repeat(300) + "…") + }) + + test("with no project_dir and none around the session's folder, the project the dbt adapter is configured for is read", async () => { + const dir = await project("configured", BUILD) + dbtAdapterInternals.readConfig = async () => ({ projectRoot: dir }) + resetDbtAdapter() + try { + const result = await run(undefined) + + expect((result.metadata as any).invocationId).toBe("11111111-aaaa-bbbb-cccc-000000000001") + } finally { + delete dbtAdapterInternals.readConfig + resetDbtAdapter() + } + }) +})