Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b7cfc04
feat: foundation for the dbt Power User tools
hkaltai Nov 13, 2026
1eb722e
Merge remote-tracking branch 'origin/main' into feat/pu-mods-foundation
hkaltai Nov 13, 2026
1ee47f3
feat(tool): dbt_run_results reads the last dbt run's id, timings and …
hkaltai Nov 13, 2026
21d0494
fix: harden the dbt Power User tool foundation
hkaltai Nov 13, 2026
3728f95
fix(tool): dbt_run_results reads one coherent run, bounds its answer …
hkaltai Nov 13, 2026
2c46312
fix(tool): dbt_run_results looks for the project above the folder it …
hkaltai Nov 13, 2026
60bca0f
test(tool): name the nested-folder case as what it checks
hkaltai Nov 13, 2026
2e84742
Merge branch 'feat/pu-mods-foundation' into feat/dbt-run-results-tool
hkaltai Nov 13, 2026
6d497bb
Merge remote-tracking branch 'origin/main' into feat/dbt-run-results-…
hkaltai Nov 13, 2026
1aec5ac
fix(tool): dbt_run_results reads each run from one parse, takes a tar…
hkaltai Nov 13, 2026
b1e36d6
fix(validators): a run_results.json rewritten during the read is no e…
hkaltai Nov 14, 2026
8c67725
fix(validators): detect a rewrite during the run_results.json read by…
hkaltai Nov 14, 2026
a609c63
fix(validators): run_results.json keeps the filesystem's own mtime ro…
hkaltai Nov 14, 2026
4845d65
fix(tool): review follow-ups — falsey API bodies, one stat snapshot f…
hkaltai Nov 15, 2026
d8c3469
fix: send falsy API bodies, follow cancellation in apiRequest, check …
hkaltai Nov 15, 2026
dace1c2
Merge remote-tracking branch 'origin/main' into feat/pu-mods-foundation
hkaltai Nov 17, 2026
769a7ac
Merge remote-tracking branch 'origin/main' into feat/dbt-run-results-…
hkaltai Nov 17, 2026
18a3fc4
fix: keep the stored API key out of API errors, bound the dbt config …
hkaltai Nov 17, 2026
6024ac9
fix: redact the API key when a refused header races the caller's abor…
hkaltai Nov 17, 2026
f5159c5
docs: say which tools use the dbt adapter helper
hkaltai Nov 17, 2026
f57de3d
docs: rewrap and correct the adapter helper comments
hkaltai Nov 17, 2026
e65a30f
Merge origin/feat/pu-mods-foundation into feat/dbt-run-results-tool
hkaltai Nov 17, 2026
aaff414
fix: never echo a send failure's text, so a trimmed key can't leak
hkaltai Nov 17, 2026
3a98dff
fix: a rejection with no reason is a send failure, not an abort
hkaltai Nov 17, 2026
4773866
Merge remote-tracking branch 'origin/feat/pu-mods-foundation' into fe…
hkaltai Nov 17, 2026
d0d99c7
Merge remote-tracking branch 'origin/main' into feat/dbt-run-results-…
hkaltai Nov 17, 2026
32848a3
test: abort the in-flight request only after the fetch started
hkaltai Nov 17, 2026
2025857
Merge origin/main: take main's foundation files, keep only dbt_run_re…
hkaltai Nov 17, 2026
cccd13f
Merge remote-tracking branch 'origin/main' into feat/dbt-run-results-…
hkaltai Nov 18, 2026
910f061
fix(tool): dbt_run_results reads a run written with --target-path fro…
hkaltai Nov 18, 2026
2576266
fix(tool): dbt_run_results parses the artifact once and bounds the ec…
hkaltai Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 141 additions & 6 deletions packages/opencode/src/altimate/tools/dbt-run-results.ts
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))
Comment thread
hkaltai marked this conversation as resolved.
Resolved
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()
}
32 changes: 28 additions & 4 deletions packages/opencode/src/altimate/validators/validator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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
}

/**
Expand Down Expand Up @@ -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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: A Windows UNC or drive-rooted folder is no longer treated as absolute

isAbsolutePath (line 515) accepts only a leading / or a C:/C:\ drive letter. The tool code this replaced used path.isAbsolute, which on win32 also returns true for \\server\share\... and \dir. So target_path: "\\\\fileserver\\dbt\\ci-out" (or a root-relative \dbt\out) is now joined onto dbtRoot instead of used as given, and readRunResults returns null: the tool answers "No readable run_results.json for the dbt project at … (in \fileserver\dbt\ci-out)" for a folder that does hold the run.

Extending the helper keeps one definition for the env and dbt_project.yml paths too:

function isAbsolutePath(p: string): boolean {
  return p.startsWith("/") || p.startsWith("\\\\") || /^[A-Za-z]:[\\/]/.test(p)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Windows UNC target_path values are treated as project-relative, so artifacts written to \\server\share\... cannot be read. Recognize UNC roots as absolute before joining with dbtRoot.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/opencode/src/altimate/validators/validator-utils.ts, line 1360:

<comment>Windows UNC `target_path` values are treated as project-relative, so artifacts written to `\\server\share\...` cannot be read. Recognize UNC roots as absolute before joining with `dbtRoot`.</comment>

<file context>
@@ -1348,14 +1354,21 @@ export function isFailedRunStatus(status: string): boolean {
+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)
   const path = join(targetPath, "run_results.json")
   try {
</file context>
Suggested change
const targetPath = named ? (isAbsolutePath(named) ? named : join(dbtRoot, named)) : await resolveDbtTargetPath(dbtRoot)
const targetPath = named ? (isAbsolutePath(named) || named.startsWith("\\\\") ? named : join(dbtRoot, named)) : await resolveDbtTargetPath(dbtRoot)

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".
Expand Down Expand Up @@ -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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: mtimeMs is captured before the read, so the time can belong to a different run than the rows

The rows, invocationId and elapsedSec now come from the single parse at 1366, but mtimeMs comes from the fs.stat at 1363 — there is no identity/size/mtime comparison between that stat and the read (the guard earlier rounds of this PR added is not present at HEAD; git log -S "mtimeNs" finds no commit in this branch). Two consequences in a window that is reachable here, because this lane spawns dbt test on every validation pass and dbt overwrites this same file:

  • If dbt replaces the file between the stat and the read, the new run's rows are returned with the old run's mtimeMs. The tool's completedAt then disagrees with its invocationId, and dbt-build-green.ts:192-196 decides freshness from that stale time — a build that just finished can read as predating the session ("Your edits have never been built").
  • If the read lands mid-write, JSON.parse throws and the function returns null, which both gates read as "no artifact".

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 null semantics and makes rows and time provably one version:

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 null on mismatch, as the earlier version did) also closes the stale-time half, though it still turns a concurrent write into "no artifact".

results,
command,
invocationId: typeof id === "string" && id ? id : null,
elapsedSec: elapsed,
}
} catch {
return null
}
Expand Down
Loading
Loading