diff --git a/packages/opencode/src/altimate/api/client.ts b/packages/opencode/src/altimate/api/client.ts index 7a03aeb906..2541ed93b2 100644 --- a/packages/opencode/src/altimate/api/client.ts +++ b/packages/opencode/src/altimate/api/client.ts @@ -335,7 +335,7 @@ export namespace AltimateApi { "x-tenant": creds.altimateInstanceName, ...clientHeaders(), }, - ...(body ? { body: JSON.stringify(body) } : {}), + ...(body !== undefined ? { body: JSON.stringify(body) } : {}), }) if (!res.ok) { throw new Error(`API ${method} ${endpoint} failed with status ${res.status}`) @@ -347,6 +347,26 @@ export namespace AltimateApi { // altimate_change end } + /** + * Calls an Altimate API endpoint with the stored credentials (Bearer key and x-tenant headers) and + * returns the parsed JSON body. Endpoint functions in other files go through this. Fails with a + * message naming the credentials file when none are stored or they can't be read. + */ + export async function apiRequest(method: string, endpoint: string, body?: unknown): Promise { + let creds: AltimateCredentials + try { + creds = await getCredentials() + } catch (e) { + const p = credentialsPath() + if (!(await Filesystem.exists(p))) + throw new Error( + `No Altimate credentials are stored: sign in to Altimate in altimate-code first (they are saved to ${p}).`, + ) + throw new Error(`The Altimate credentials in ${p} can't be used: ${e instanceof Error ? e.message : String(e)}`) + } + return request(creds, method, endpoint, body) + } + export async function listDatamates(creds?: AltimateCredentials) { creds ??= await getCredentials() const data = await request(creds, "GET", "/datamates/") diff --git a/packages/opencode/src/altimate/tool-cli/allow-list.ts b/packages/opencode/src/altimate/tool-cli/allow-list.ts index 33b452d75c..0bb4f18473 100644 --- a/packages/opencode/src/altimate/tool-cli/allow-list.ts +++ b/packages/opencode/src/altimate/tool-cli/allow-list.ts @@ -35,10 +35,19 @@ import { AltimateCoreTestgenTool } from "../tools/altimate-core-testgen" import { AltimateCoreTrackLineageTool } from "../tools/altimate-core-track-lineage" import { AltimateCoreValidateTool } from "../tools/altimate-core-validate" import { DataDiffTool } from "../tools/data-diff" +import { DbtCostEstimateTool } from "../tools/dbt-cost-estimate" +import { DbtCteProfileTool } from "../tools/dbt-cte-profile" +import { DbtDeferStateTool } from "../tools/dbt-defer-state" +import { DbtDocsCoverageTool } from "../tools/dbt-docs-coverage" +import { DbtHealthTool } from "../tools/dbt-health" import { DbtLineageTool } from "../tools/dbt-lineage" import { DbtManifestTool } from "../tools/dbt-manifest" +import { DbtMapRelationsTool } from "../tools/dbt-map-relations" import { DbtProfilesTool } from "../tools/dbt-profiles" +import { DbtRunResultsTool } from "../tools/dbt-run-results" +import { DbtSqlGraphTool } from "../tools/dbt-sql-graph" import { DbtUnitTestGenTool } from "../tools/dbt-unit-test-gen" +import { DocsShareCommentsTool } from "../tools/docs-share-comments" import { FinopsAnalyzeCreditsTool } from "../tools/finops-analyze-credits" import { FinopsExpensiveQueriesTool } from "../tools/finops-expensive-queries" import { FinopsQueryHistoryTool } from "../tools/finops-query-history" @@ -106,10 +115,19 @@ export const EXPOSED: Record = { altimate_core_track_lineage: AltimateCoreTrackLineageTool, altimate_core_validate: AltimateCoreValidateTool, data_diff: DataDiffTool, + dbt_cost_estimate: DbtCostEstimateTool, + dbt_cte_profile: DbtCteProfileTool, + dbt_defer_state: DbtDeferStateTool, + dbt_docs_coverage: DbtDocsCoverageTool, + dbt_health: DbtHealthTool, dbt_lineage: DbtLineageTool, dbt_manifest: DbtManifestTool, + dbt_map_relations: DbtMapRelationsTool, dbt_profiles: DbtProfilesTool, + dbt_run_results: DbtRunResultsTool, + dbt_sql_graph: DbtSqlGraphTool, dbt_unit_test_gen: DbtUnitTestGenTool, + docs_share_comments: DocsShareCommentsTool, finops_analyze_credits: FinopsAnalyzeCreditsTool, finops_expensive_queries: FinopsExpensiveQueriesTool, finops_query_history: FinopsQueryHistoryTool, diff --git a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts new file mode 100644 index 0000000000..5260dca415 --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts @@ -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): Promise { + 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 | undefined + const waited = new Promise((_, 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) } + } +} diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts new file mode 100644 index 0000000000..7e90c39101 --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -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.", + } + }, +}) diff --git a/packages/opencode/src/altimate/tools/dbt-cte-profile.ts b/packages/opencode/src/altimate/tools/dbt-cte-profile.ts new file mode 100644 index 0000000000..ede4eae8fb --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-cte-profile.ts @@ -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.", + } + }, +}) diff --git a/packages/opencode/src/altimate/tools/dbt-defer-state.ts b/packages/opencode/src/altimate/tools/dbt-defer-state.ts new file mode 100644 index 0000000000..0cd7207bc7 --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-defer-state.ts @@ -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.", + } + }, +}) diff --git a/packages/opencode/src/altimate/tools/dbt-docs-coverage.ts b/packages/opencode/src/altimate/tools/dbt-docs-coverage.ts new file mode 100644 index 0000000000..927da81c86 --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-docs-coverage.ts @@ -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.", + } + }, +}) diff --git a/packages/opencode/src/altimate/tools/dbt-health.ts b/packages/opencode/src/altimate/tools/dbt-health.ts new file mode 100644 index 0000000000..397700927b --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-health.ts @@ -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.", + } + }, +}) diff --git a/packages/opencode/src/altimate/tools/dbt-lineage.ts b/packages/opencode/src/altimate/tools/dbt-lineage.ts index a83efb84ae..5c300bc759 100644 --- a/packages/opencode/src/altimate/tools/dbt-lineage.ts +++ b/packages/opencode/src/altimate/tools/dbt-lineage.ts @@ -366,7 +366,7 @@ async function defaultManifestPath(session: () => ReturnType { +export async function sessionProject(): Promise<{ root?: string; other?: string } | undefined> { const { findProjectRoot } = await import("../../../../dbt-tools/src/config") const found = findProjectRoot(projectRoot()) if (!found) return undefined diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts new file mode 100644 index 0000000000..e4d8642e4f --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -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.", + } + }, +}) diff --git a/packages/opencode/src/altimate/tools/dbt-run-results.ts b/packages/opencode/src/altimate/tools/dbt-run-results.ts new file mode 100644 index 0000000000..bfb677dde7 --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-run-results.ts @@ -0,0 +1,156 @@ +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, from one read of its run_results.json: `readRunResults` gives the path, + * the command, the invocation id, the elapsed time and each row's status, execution time and message, + * so every field belongs to the same file. `targetPath` names the folder a run with `--target-path` + * wrote to. + */ +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: + "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("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(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 ? ` (in ${args.target_path})` : ""}: 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: `${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/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts new file mode 100644 index 0000000000..c98d1c0a16 --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -0,0 +1,16 @@ +import z from "zod" +import { Tool } from "../../tool/tool" + +export const DbtSqlGraphTool = Tool.define("dbt_sql_graph", { + description: "Not implemented yet: always returns an error. A dbt model's SQL as a graph: its CTEs, joins and filters, and the edges between them.", + parameters: z.object({ + model: z.string().describe("Model name"), + }), + async execute() { + return { + title: "dbt_sql_graph: not implemented", + metadata: { success: false, error: "not implemented" }, + output: "dbt_sql_graph is not implemented in this build of altimate-code yet.", + } + }, +}) diff --git a/packages/opencode/src/altimate/tools/docs-share-comments.ts b/packages/opencode/src/altimate/tools/docs-share-comments.ts new file mode 100644 index 0000000000..a309265a9d --- /dev/null +++ b/packages/opencode/src/altimate/tools/docs-share-comments.ts @@ -0,0 +1,20 @@ +import z from "zod" +import { Tool } from "../../tool/tool" + +export const DocsShareCommentsTool = Tool.define("docs_share_comments", { + description: "Not implemented yet: always returns an error. Comment threads on shared dbt docs: list open threads, reply to one, or resolve one.", + parameters: z.object({ + action: z.enum(["list", "reply", "resolve"]).describe("What to do"), + project: z.string().optional().describe("dbt project name the shared docs belong to"), + share_id: z.number().int().optional().describe("Shared docs id"), + group_id: z.number().int().optional().describe("Comment thread id"), + text: z.string().optional().describe("Reply text"), + }), + async execute() { + return { + title: "docs_share_comments: not implemented", + metadata: { success: false, error: "not implemented" }, + output: "docs_share_comments is not implemented in this build of altimate-code yet.", + } + }, +}) diff --git a/packages/opencode/src/altimate/validators/validator-utils.ts b/packages/opencode/src/altimate/validators/validator-utils.ts index e1d85c476e..1e87ef7a4b 100644 --- a/packages/opencode/src/altimate/validators/validator-utils.ts +++ b/packages/opencode/src/altimate/validators/validator-utils.ts @@ -838,10 +838,15 @@ function collectDeliverableTokens(tokens: string[]): { models: string[]; files: // --------------------------------------------------------------------------- /** - * Resolve the project's artifact directory. Honours `DBT_TARGET_PATH` and a - * `target-path:` key in `dbt_project.yml`; defaults to `target`. - */ -export async function resolveDbtTargetPath(dbtRoot: string): Promise { + * Resolve the project's artifact directory. A `targetPath` the caller names (the + * folder a `--target-path` run wrote to) wins; else `DBT_TARGET_PATH` and a + * `target-path:` key in `dbt_project.yml` are honoured; defaults to `target`. + */ +export async function resolveDbtTargetPath(dbtRoot: string, targetPath?: string): Promise { + // A folder the caller names (the one a `--target-path` run wrote to) wins over the environment and the project file. + if (targetPath && targetPath.trim().length > 0) { + return isAbsolutePath(targetPath) ? targetPath : join(dbtRoot, targetPath) + } const fromEnv = process.env.DBT_TARGET_PATH if (fromEnv && fromEnv.trim().length > 0) { return isAbsolutePath(fromEnv) ? fromEnv : join(dbtRoot, fromEnv) @@ -1191,6 +1196,8 @@ export interface RunResultNode { status: string /** dbt's message for the node, when present. */ message: string | null + /** Seconds dbt spent on the node (`execution_time`), when it wrote a number. */ + executionTimeSec: number | null } /** A parsed `run_results.json` plus its freshness. */ @@ -1212,6 +1219,10 @@ export interface RunResultsArtifact { * not evidence that anything was built. */ command: string | null + /** `metadata.invocation_id`, when the file carries one. */ + invocationId: string | null + /** The run's `elapsed_time` in seconds, when the file carries one. */ + elapsedSec: number | null } /** @@ -1348,14 +1359,26 @@ export function isFailedRunStatus(status: string): boolean { * Read and parse `/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 { - const targetPath = await resolveDbtTargetPath(dbtRoot) +export async function readRunResults(dbtRoot: string, targetPathOverride?: string): Promise { + const targetPath = await resolveDbtTargetPath(dbtRoot, targetPathOverride) const path = join(targetPath, "run_results.json") try { - const stat = await fs.stat(path) + const stat = await fs.stat(path, { bigint: true }) if (!stat.isFile()) return null const raw = await fs.readFile(path, "utf8") - const parsed = JSON.parse(raw) as { results?: unknown; args?: unknown } + // The time keeps the plain stat's own rounding: every other caller compares it with mtimes read the same way, and a different rounding of equal timestamps would order them apart. It is taken between the two bigint stats, so the comparison below proves it describes the same file version as the rows. + const plain = await fs.stat(path) + // A rewrite during the read changes the file's identity, size or times (nanosecond precision): the rows and the time would belong to different runs, so there is no evidence. + const after = await fs.stat(path, { bigint: true }) + if ( + after.ino !== stat.ino || + after.size !== stat.size || + after.mtimeNs !== stat.mtimeNs || + after.ctimeNs !== stat.ctimeNs + ) + return null + const mtimeMs = plain.mtimeMs + const parsed = JSON.parse(raw) as { results?: unknown; args?: unknown; metadata?: 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 +1406,24 @@ export async function readRunResults(dbtRoot: string): Promise) + : null + const invocation = meta && typeof meta["invocation_id"] === "string" ? meta["invocation_id"] : "" + const elapsed = typeof parsed.elapsed_time === "number" && Number.isFinite(parsed.elapsed_time) ? parsed.elapsed_time : null + return { + path, + mtimeMs, + results, + command, + invocationId: invocation || null, + elapsedSec: elapsed, + } } catch { return null } diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts new file mode 100644 index 0000000000..a9ae7406e0 --- /dev/null +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -0,0 +1,291 @@ +// The tools the dbt Power User features call: their allow-list entries and placeholder answers, the +// one gate every dbt-adapter-backed tool goes through, and the API helper for new endpoint files. The +// dbt adapter is replaced through `dbtAdapterInternals`, so these tests need no Python or dbt. +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from "bun:test" +import { mkdtemp, rm, writeFile } from "node:fs/promises" +import { realpathSync } from "node:fs" +import os from "node:os" +import path from "node:path" +import { Effect } from "effect" +import type { Tool } from "@/tool/tool" +import { SessionID, MessageID } from "@/session/schema" +import { dbtAdapterInternals, resetDbtAdapter } from "@/altimate/native/connections/register" +import { dbtAdapterAccessInternals, dbtAdapterFor } from "@/altimate/tools/dbt-adapter-access" +import { EXPOSED, MCP_OPEN_WRITE_TOOLS, WRITE_TOOLS } from "@/altimate/tool-cli/allow-list" +import { AltimateApi } from "@/altimate/api/client" +import { Instance } from "@/project/instance" +import { disposeInstance } from "@/effect/instance-registry" +import { initTool } from "./tool-fixture" + +/** Each tool with the smallest arguments its parameters accept. */ +const MINIMAL_ARGS: Record> = { + dbt_run_results: {}, + dbt_defer_state: { project_dir: "." }, + dbt_map_relations: { sql: "select 1" }, + docs_share_comments: { action: "list" }, + dbt_cost_estimate: {}, + dbt_health: {}, + dbt_sql_graph: { model: "customers" }, + dbt_docs_coverage: { model: "customers" }, + dbt_cte_profile: { model: "customers" }, +} +const FEATURE_TOOLS = Object.keys(MINIMAL_ARGS) +/** Tools whose placeholder has been replaced by the real tool: their own test file covers them. */ +const IMPLEMENTED = new Set(["dbt_run_results"]) + +const ctx: Tool.Context = { + sessionID: SessionID.make("ses_dbt_adapter_access"), + messageID: MessageID.make("msg_dbt_adapter_access"), + agent: "analyst", + abort: new AbortController().signal, + messages: [], + metadata: () => Effect.void, + ask: () => Effect.void, +} + +let project: string + +function useConfig() { + dbtAdapterInternals.readConfig = async () => ({ + projectRoot: project, + pythonPath: "python3", + dbtIntegration: "corecommand", + queryLimit: 500, + }) +} + +beforeAll(async () => { + project = await mkdtemp(path.join(os.tmpdir(), "dbt-adapter-access-")) + await writeFile(path.join(project, "dbt_project.yml"), "name: jaffle_shop\n") +}) + +afterAll(async () => { + await rm(project, { recursive: true, force: true }) +}) + +beforeEach(() => resetDbtAdapter()) + +afterEach(() => { + resetDbtAdapter() + delete dbtAdapterInternals.readConfig + delete dbtAdapterInternals.create +}) + +describe("feature tools in the allow-list", () => { + test("all nine are exposed, in the map's alphabetical order, and none is a write tool", () => { + const ids = Object.keys(EXPOSED) + for (const id of FEATURE_TOOLS) { + expect(ids).toContain(id) + expect(WRITE_TOOLS.has(id)).toBe(false) + expect(MCP_OPEN_WRITE_TOOLS.has(id)).toBe(false) + } + const listed = ids.filter((id) => id.startsWith("dbt_") || id.startsWith("docs_")) + expect(listed).toEqual([...listed].sort()) + }) + + test("each placeholder answers that it isn't implemented, as a failure", async () => { + for (const id of FEATURE_TOOLS.filter((id) => !IMPLEMENTED.has(id))) { + const tool = await initTool(EXPOSED[id] as any) + expect(tool.id).toBe(id) + const result = await tool.execute(MINIMAL_ARGS[id], ctx) + expect(result.title).toBe(`${id}: not implemented`) + expect(result.metadata).toEqual({ success: false, error: "not implemented", truncated: false }) + expect(result.output).toContain(id) + } + }) +}) + +describe("dbtAdapterFor", () => { + let created: number + beforeEach(() => { + created = 0 + useConfig() + dbtAdapterInternals.create = async () => { + created += 1 + return { isInitialized: () => true, dispose: async () => {} } + } + }) + + test("refuses a read-only call and never creates the adapter", async () => { + const access = await dbtAdapterFor({ ...ctx, extra: { forceReadOnly: true } }) + expect(access.adapter).toBeUndefined() + expect(access.refusal).toBe("read_only") + expect(access.reason).toContain("--allow-write") + expect(created).toBe(0) + }) + + test("refuses a plan-mode call", async () => { + const access = await dbtAdapterFor({ ...ctx, agent: "plan" }) + expect(access.refusal).toBe("read_only") + expect(created).toBe(0) + }) + + test("returns the adapter where writes are allowed", async () => { + const access = await dbtAdapterFor(ctx) + expect(access.refusal).toBeUndefined() + expect(access.adapter?.isInitialized()).toBe(true) + expect(created).toBe(1) + }) + + test("says when the adapter didn't load the project", async () => { + dbtAdapterInternals.create = async () => ({ isInitialized: () => false, dispose: async () => {} }) + const access = await dbtAdapterFor(ctx) + expect(access).toEqual({ refusal: "not_loaded", reason: "the dbt adapter didn't load the project" }) + }) + + test("says there is no dbt project when the config finds none", async () => { + dbtAdapterInternals.readConfig = async () => null + const access = await dbtAdapterFor(ctx) + expect(access).toEqual({ refusal: "no_project", reason: "no dbt project here" }) + }) + + test("says when the adapter doesn't start in time", async () => { + dbtAdapterInternals.create = () => new Promise(() => {}) + const saved = dbtAdapterAccessInternals.adapterStartTimeoutMs + dbtAdapterAccessInternals.adapterStartTimeoutMs = 20 + try { + const access = await dbtAdapterFor(ctx) + expect(access).toEqual({ refusal: "not_started", reason: "the dbt adapter didn't start within 0.02 s" }) + } finally { + dbtAdapterAccessInternals.adapterStartTimeoutMs = saved + } + }) + + test("says the adapter didn't start when the project exists but creating it fails", async () => { + dbtAdapterInternals.create = async () => { + created += 1 + throw new Error("dbt-integration missing") + } + const access = await dbtAdapterFor(ctx) + expect(access).toEqual({ refusal: "not_started", reason: "the dbt adapter didn't start" }) + expect(created).toBe(1) + }) + + test("a call cancelled while the adapter starts throws and stops waiting", async () => { + let entered = () => {} + const starting = new Promise((resolve) => (entered = resolve)) + dbtAdapterInternals.create = () => { + entered() + return new Promise(() => {}) + } + const controller = new AbortController() + const pending = dbtAdapterFor({ ...ctx, abort: controller.signal }) + await starting + controller.abort(new Error("cancelled")) + await expect(pending).rejects.toThrow("cancelled") + }) + + test("a cancelled call throws rather than answering", async () => { + const controller = new AbortController() + controller.abort() + await expect(dbtAdapterFor({ ...ctx, abort: controller.signal })).rejects.toThrow() + expect(created).toBe(0) + }) + + test("a session in a dbt project the adapter isn't set up for says so", async () => { + dbtAdapterInternals.readConfig = async () => null + try { + const access = await Instance.provide({ directory: project, fn: () => dbtAdapterFor(ctx) }) + expect(access).toEqual({ + refusal: "other_project", + reason: `the dbt adapter isn't set up for ${realpathSync(project)}`, + }) + expect(created).toBe(0) + } finally { + await disposeInstance(project) + } + }) + + test("a session in another dbt project than the adapter's says which", async () => { + const other = await mkdtemp(path.join(os.tmpdir(), "dbt-adapter-other-")) + await writeFile(path.join(other, "dbt_project.yml"), "name: other\n") + dbtAdapterInternals.readConfig = async () => ({ + projectRoot: other, + pythonPath: "python3", + dbtIntegration: "corecommand", + queryLimit: 500, + }) + try { + const access = await Instance.provide({ directory: project, fn: () => dbtAdapterFor(ctx) }) + expect(access).toEqual({ + refusal: "other_project", + reason: `the dbt adapter is set up for ${realpathSync(other)}, not ${realpathSync(project)}`, + }) + expect(created).toBe(0) + } finally { + await disposeInstance(project) + await rm(other, { recursive: true, force: true }) + } + }) +}) + +describe("AltimateApi.apiRequest", () => { + let home: string + let saved: string | undefined + beforeEach(async () => { + home = await mkdtemp(path.join(os.tmpdir(), "altimate-api-home-")) + saved = process.env.OPENCODE_TEST_HOME + process.env.OPENCODE_TEST_HOME = home + }) + afterEach(async () => { + if (saved === undefined) delete process.env.OPENCODE_TEST_HOME + else process.env.OPENCODE_TEST_HOME = saved + await rm(home, { recursive: true, force: true }) + }) + + test("fails with a clear message when no credentials are stored", async () => { + await expect(AltimateApi.apiRequest("GET", "/dbt/v1/x")).rejects.toThrow( + `No Altimate credentials are stored: sign in to Altimate in altimate-code first (they are saved to ${path.join(home, ".altimate", "altimate.json")}).`, + ) + }) + + test("sends a falsey body (false, 0, empty string, null) unchanged", async () => { + await Bun.write( + path.join(home, ".altimate", "altimate.json"), + JSON.stringify({ altimateUrl: "https://api.example.test/", altimateInstanceName: "acme", altimateApiKey: "k1" }), + ) + const original = globalThis.fetch + const bodies: unknown[] = [] + globalThis.fetch = (async (_url: string, init: RequestInit) => { + bodies.push(init.body) + return new Response("{}", { status: 200 }) + }) as unknown as typeof fetch + try { + for (const v of [false, 0, "", null]) await AltimateApi.apiRequest("POST", "/dbt/v1/x", v) + await AltimateApi.apiRequest("GET", "/dbt/v1/x") + expect(bodies).toEqual(["false", "0", '""', "null", undefined]) + } finally { + globalThis.fetch = original + } + }) + + test("sends the stored key and tenant and returns the JSON body", async () => { + await Bun.write( + path.join(home, ".altimate", "altimate.json"), + JSON.stringify({ altimateUrl: "https://api.example.test/", altimateInstanceName: "acme", altimateApiKey: "k1" }), + ) + const original = globalThis.fetch + const seen: { url: string; init: RequestInit }[] = [] + globalThis.fetch = (async (url: string, init: RequestInit) => { + seen.push({ url, init }) + return new Response(JSON.stringify({ ok: 1 }), { status: 200 }) + }) as unknown as typeof fetch + try { + expect(await AltimateApi.apiRequest("POST", "/dbt/v1/x", { a: 1 })).toEqual({ ok: 1 }) + expect(seen[0].url).toBe("https://api.example.test/dbt/v1/x") + const headers = seen[0].init.headers as Record + expect(headers.Authorization).toBe("Bearer k1") + expect(headers["x-tenant"]).toBe("acme") + expect(seen[0].init.body).toBe(JSON.stringify({ a: 1 })) + } finally { + globalThis.fetch = original + } + }) + + test("names the file when the stored credentials can't be read", async () => { + await Bun.write(path.join(home, ".altimate", "altimate.json"), "{not json") + await expect(AltimateApi.apiRequest("GET", "/dbt/v1/x")).rejects.toThrow( + `The Altimate credentials in ${path.join(home, ".altimate", "altimate.json")} can't be used`, + ) + }) +}) 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..4ee5e14be4 --- /dev/null +++ b/packages/opencode/test/altimate/dbt-run-results.test.ts @@ -0,0 +1,286 @@ +// 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("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 run that wrote to another target folder is read from that folder, whatever DBT_TARGET_PATH says", async () => { + const dir = await project("custom-target", COMPILE) + await mkdir(path.join(dir, "ci-out"), { recursive: true }) + await writeFile(path.join(dir, "ci-out", "run_results.json"), JSON.stringify(BUILD)) + const before = process.env["DBT_TARGET_PATH"] + process.env["DBT_TARGET_PATH"] = "elsewhere" + let view: any + try { + view = (await run(dir, "ci-out")).metadata + } finally { + if (before === undefined) delete process.env["DBT_TARGET_PATH"] + else process.env["DBT_TARGET_PATH"] = before + } + const own = (await run(dir)).metadata as any + + expect(view.invocationId).toBe("11111111-aaaa-bbbb-cccc-000000000001") + expect(own.invocationId).toBe("22222222-aaaa-bbbb-cccc-000000000002") + }) + + 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() + } + }) +}) diff --git a/packages/opencode/test/altimate/validators/dbt-build-green.test.ts b/packages/opencode/test/altimate/validators/dbt-build-green.test.ts index df91fd655a..fbacda0529 100644 --- a/packages/opencode/test/altimate/validators/dbt-build-green.test.ts +++ b/packages/opencode/test/altimate/validators/dbt-build-green.test.ts @@ -113,6 +113,19 @@ describe("DbtBuildGreenValidator — fresh artifact", () => { expect(r.details!["verdict"]).toBe("fresh-build") }) + test("a model and an artifact stamped with the same instant read as built, not edited after the build", async () => { + await makeProject() + await writeModel("stg_orders") + await writeRunResults([{ id: "model.t.stg_orders", status: "success" }]) + // A coarse filesystem clock gives writes in one tick the same nanosecond timestamp. + const t = 1_800_000_000.004123 + await fs.utimes(join(dir, "models", "stg_orders.sql"), t, t) + await fs.utimes(join(dir, "target", "run_results.json"), t, t) + const r = await DbtBuildGreenValidator.check(ctx()) + expect(r.ok).toBe(true) + expect(r.details!["verdict"]).toBe("fresh-build") + }) + test("treats warn as a clean build", async () => { await makeProject() await writeModel("stg_orders") diff --git a/packages/opencode/test/altimate/validators/dbt-nothing-built.test.ts b/packages/opencode/test/altimate/validators/dbt-nothing-built.test.ts index 7dfaef7d38..fa03785e65 100644 --- a/packages/opencode/test/altimate/validators/dbt-nothing-built.test.ts +++ b/packages/opencode/test/altimate/validators/dbt-nothing-built.test.ts @@ -1,5 +1,5 @@ // altimate_change start — tests for the nothing-built inverse completion gate -import { describe, expect, test, beforeEach, afterEach } from "bun:test" +import { describe, expect, test, beforeEach, afterEach, spyOn } from "bun:test" import { promises as fs } from "fs" import { tmpdir } from "os" import { join } from "path" @@ -274,6 +274,25 @@ describe("run_results and inventory helpers", () => { expect(await resolveDbtTargetPath(dir)).toBe(join(dir, "alt_target")) }) + test("readRunResults takes the time from between the two identity stats, so it describes the version the rows came from", async () => { + const dir = await fs.mkdtemp(join(tmpdir(), "rr-order-")) + await fs.mkdir(join(dir, "target"), { recursive: true }) + await fs.writeFile(join(dir, "target", "run_results.json"), JSON.stringify({ args: { which: "build" }, results: [] })) + const real = fs.stat + const kinds: string[] = [] + const spy = spyOn(fs, "stat").mockImplementation(((p: any, o?: any) => { + if (String(p).endsWith("run_results.json")) kinds.push(o?.bigint ? "bigint" : "plain") + return real(p, o) + }) as any) + try { + await readRunResults(dir) + } finally { + spy.mockRestore() + await fs.rm(dir, { recursive: true, force: true }) + } + expect(kinds).toEqual(["bigint", "plain", "bigint"]) + }) + test("readRunResults returns null when the artifact is missing", async () => { await makeProject() expect(await readRunResults(dir)).toBeNull()