From b7cfc0426d60963a83f22a8f5c6053c7b7fce8e4 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 16:30:43 +0530 Subject: [PATCH 01/28] feat: foundation for the dbt Power User tools Nine tool stubs exposed through `altimate tool run` (dbt_run_results, dbt_defer_state, dbt_map_relations, docs_share_comments, dbt_cost_estimate, dbt_health, dbt_sql_graph, dbt_docs_coverage, dbt_cte_profile), an exported `AltimateApi.apiRequest` that calls the backend with the stored credentials, and `dbtAdapterFor`, the one gate every adapter-backed tool goes through: it refuses read-only calls, other projects, and an adapter that did not start. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/api/client.ts | 18 ++ .../src/altimate/tool-cli/allow-list.ts | 18 ++ .../src/altimate/tools/dbt-adapter-access.ts | 100 ++++++++ .../src/altimate/tools/dbt-cost-estimate.ts | 17 ++ .../src/altimate/tools/dbt-cte-profile.ts | 17 ++ .../src/altimate/tools/dbt-defer-state.ts | 16 ++ .../src/altimate/tools/dbt-docs-coverage.ts | 17 ++ .../opencode/src/altimate/tools/dbt-health.ts | 16 ++ .../src/altimate/tools/dbt-map-relations.ts | 18 ++ .../src/altimate/tools/dbt-run-results.ts | 16 ++ .../src/altimate/tools/dbt-sql-graph.ts | 16 ++ .../src/altimate/tools/docs-share-comments.ts | 20 ++ .../test/altimate/dbt-adapter-access.test.ts | 221 ++++++++++++++++++ 13 files changed, 510 insertions(+) create mode 100644 packages/opencode/src/altimate/tools/dbt-adapter-access.ts create mode 100644 packages/opencode/src/altimate/tools/dbt-cost-estimate.ts create mode 100644 packages/opencode/src/altimate/tools/dbt-cte-profile.ts create mode 100644 packages/opencode/src/altimate/tools/dbt-defer-state.ts create mode 100644 packages/opencode/src/altimate/tools/dbt-docs-coverage.ts create mode 100644 packages/opencode/src/altimate/tools/dbt-health.ts create mode 100644 packages/opencode/src/altimate/tools/dbt-map-relations.ts create mode 100644 packages/opencode/src/altimate/tools/dbt-run-results.ts create mode 100644 packages/opencode/src/altimate/tools/dbt-sql-graph.ts create mode 100644 packages/opencode/src/altimate/tools/docs-share-comments.ts create mode 100644 packages/opencode/test/altimate/dbt-adapter-access.test.ts diff --git a/packages/opencode/src/altimate/api/client.ts b/packages/opencode/src/altimate/api/client.ts index 7a03aeb906..069a182b7a 100644 --- a/packages/opencode/src/altimate/api/client.ts +++ b/packages/opencode/src/altimate/api/client.ts @@ -347,6 +347,24 @@ 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..a2711e35d1 --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts @@ -0,0 +1,100 @@ +import fs from "fs" +import path from "path" +import type { Tool } from "../../tool/tool" +import { projectRoot } from "../native/connections/registry" + +/** 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) } + } +} + +/** + * The adapter's project root and, when the session's directory is in another dbt project, that one + * (`other`, both links resolved). Undefined when the session's directory is in no dbt project: the + * adapter's own discovery (a dbt.json, or a walk up from the working directory) decides then. + */ +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 + const { getDbtProjectRoot } = await import("../native/connections/register") + const root = await getDbtProjectRoot() + const real = (p: string) => { + try { + return fs.realpathSync(p) + } catch { + return path.resolve(p) + } + } + const here = real(found) + // path.relative ignores case on win32. + if (!root) return { other: here } + return path.relative(real(root), here) ? { root: real(root), other: here } : { root } +} 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..6529d828c6 --- /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: "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 }, + 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..e7a5bde647 --- /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: "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 }, + 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..75cbfe19cf --- /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: "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 }, + 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..c4e0ec42d3 --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-docs-coverage.ts @@ -0,0 +1,17 @@ +import z from "zod" +import { Tool } from "../../tool/tool" + +export const DbtDocsCoverageTool = Tool.define("dbt_docs_coverage", { + description: "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 }, + 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..a3f26352c4 --- /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: "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 }, + output: "dbt_health is not implemented in this build of altimate-code yet.", + } + }, +}) 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..1216d6b2ee --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -0,0 +1,18 @@ +import z from "zod" +import { Tool } from "../../tool/tool" + +export const DbtMapRelationsTool = Tool.define("dbt_map_relations", { + description: "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 }, + 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..86e68a1668 --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-run-results.ts @@ -0,0 +1,16 @@ +import z from "zod" +import { Tool } from "../../tool/tool" + +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.", + parameters: z.object({ + project_dir: z.string().optional().describe("dbt project folder (default: the dbt project found from the working directory)"), + }), + async execute() { + return { + title: "dbt_run_results: not implemented", + metadata: { success: false }, + output: "dbt_run_results is not implemented in this build of altimate-code yet.", + } + }, +}) 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..b2af1b0f18 --- /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: "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 }, + 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..2c8c0a2ab3 --- /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: "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 }, + output: "docs_share_comments is not implemented in this build of altimate-code yet.", + } + }, +}) 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..b565e5a58a --- /dev/null +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -0,0 +1,221 @@ +// 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) + +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) { + 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.success).toBe(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("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) + } + }) +}) + +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 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("can't be used") + }) +}) From d5e7bc864b5a2051ab83851822d7d2d0847bf67d Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 16:44:58 +0530 Subject: [PATCH 02/28] feat: dbt_cost_estimate tool for BigQuery dry-run scan estimates Co-Authored-By: Claude Opus 5.5 --- .../src/altimate/tools/dbt-cost-estimate.ts | 100 +++++++- .../test/altimate/dbt-cost-estimate.test.ts | 218 ++++++++++++++++++ 2 files changed, 312 insertions(+), 6 deletions(-) create mode 100644 packages/opencode/test/altimate/dbt-cost-estimate.test.ts diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index 6529d828c6..2473a89969 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -1,17 +1,105 @@ +import fs from "fs" +import path from "path" import z from "zod" import { Tool } from "../../tool/tool" +import { dbtAdapterFor } from "./dbt-adapter-access" +import { findModel, loadRawManifest } from "../native/dbt/helpers" + +/** The units dbt-bigquery's `format_bytes` writes, smallest first; each is 1024 times the one before. */ +const UNITS = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"] as const + +/** + * The byte count in a dry run's `bytes_processed`, which dbt-bigquery formats as text ("4.2 TiB", + * "512.0 Bytes"). The text carries one decimal, so a figure past a KiB is an approximation. Undefined + * for anything else, so a changed format is reported, never read as a count. + */ +export function parseBytesProcessed(display: unknown): number | undefined { + if (typeof display === "number") return Number.isFinite(display) && display >= 0 ? Math.round(display) : undefined + if (typeof display !== "string") return undefined + const match = /^\s*(\d+(?:\.\d+)?)\s*(Bytes|KiB|MiB|GiB|TiB|PiB)\s*$/i.exec(display) + if (!match) return undefined + const unit = UNITS.findIndex((u) => u.toLowerCase() === match[2].toLowerCase()) + return Math.round(Number(match[1]) * 1024 ** unit) +} + +/** The SQL a model's file holds, as Power User reads it, or the manifest's copy when the file is gone. */ +async function modelSource(model: string, root: string | undefined): Promise<{ sql?: string; name?: string; error?: string }> { + const manifestPath = path.join(root ?? "", "target", "manifest.json") + let node: any + try { + node = findModel(loadRawManifest(manifestPath)?.nodes ?? {}, model) + } catch (e) { + return { error: `couldn't read ${manifestPath}: ${e instanceof Error ? e.message : String(e)}` } + } + if (!node) return { error: `no model "${model}" in ${manifestPath}: run dbt parse or dbt compile first` } + const file = root && node.original_file_path ? path.join(root, node.original_file_path) : undefined + let sql: string | undefined + try { + if (file) sql = fs.readFileSync(file, "utf-8") + } catch {} + sql ??= typeof node.raw_code === "string" ? node.raw_code : undefined + return sql?.trim() ? { sql, name: node.name } : { error: `model "${model}" has no SQL` } +} + +function failure(title: string, message: string, extra: Record = {}) { + return { title: `dbt_cost_estimate: ${title}`, metadata: { success: false, error: message, ...extra }, output: message } +} export const DbtCostEstimateTool = Tool.define("dbt_cost_estimate", { - description: "Estimate the bytes a dbt model or a SQL query would scan, from a warehouse dry run (BigQuery).", + description: + "Estimate the bytes a dbt model or a SQL query would scan, from a warehouse dry run. Only BigQuery has a dry run: any other warehouse answers `supported: false` and nothing is compiled. Starts the dbt adapter, so it needs write permission (`--allow-write` on `tool run`). Pass `model` (a model name; its file is compiled) or `sql` (may use Jinja).", 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 }, - output: "dbt_cost_estimate is not implemented in this build of altimate-code yet.", + async execute(args, ctx) { + if (!args.model && !args.sql) return failure("nothing to estimate", "Pass a model name or SQL.") + const access = await dbtAdapterFor(ctx) + if (!access.adapter) return failure("not available", `No estimate: ${access.reason}.`, { refusal: access.refusal }) + const adapter = access.adapter + const type: string | undefined = adapter.getAdapterType?.() + // Every dbt-integration refuses the dry run off BigQuery, so nothing is compiled there. + if (type !== "bigquery") { + const adapterName = type ?? "unknown" + return { + title: "dbt_cost_estimate: no estimate", + metadata: { success: true, supported: false, adapter: adapterName }, + output: `No cost estimate: dry runs are only available on BigQuery (this project uses ${adapterName}).`, + } + } + try { + let query = args.sql + let name = args.model + if (!query) { + const root = await (await import("../native/connections/register")).getDbtProjectRoot() + const source = await modelSource(args.model!, root) + if (!source.sql) return failure("model not found", source.error ?? "No SQL to estimate.", { adapter: type }) + query = source.sql + name = source.name ?? name + } + ctx.abort.throwIfAborted() + const compiled = await adapter.unsafeCompileQuery(query, name) + ctx.abort.throwIfAborted() + if (typeof compiled !== "string" || !compiled.trim()) { + return failure("compile failed", "The query compiled to nothing.", { adapter: type }) + } + const result = await adapter.validateSQLDryRun(compiled) + ctx.abort.throwIfAborted() + const display = result?.bytes_processed + const bytes = parseBytesProcessed(display) + if (bytes === undefined) { + return failure("unreadable estimate", `The dry run returned an amount this tool can't read: ${JSON.stringify(display)}.`, { + adapter: type, + }) + } + return { + title: `dbt_cost_estimate: ${display}`, + metadata: { success: true, supported: true, adapter: type, bytes, display: String(display), ...(name && { model: name }) }, + output: `${name ? `${name} ` : "The query "}would process ${display} (about ${bytes} bytes).`, + } + } catch (e) { + if (ctx.abort.aborted) throw e + return failure("dry run failed", e instanceof Error ? e.message : String(e), { adapter: type }) } }, }) diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts new file mode 100644 index 0000000000..01758a711b --- /dev/null +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -0,0 +1,218 @@ +// dbt_cost_estimate: the dry run's formatted size read back into bytes for each unit, the answer for a +// warehouse with no dry run, a read-only call, and the model's file being compiled. The dbt adapter is +// replaced through `dbtAdapterInternals`, so these tests need no Python, dbt or warehouse. +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from "bun:test" +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises" +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 { DbtCostEstimateTool, parseBytesProcessed } from "@/altimate/tools/dbt-cost-estimate" +import { initTool } from "./tool-fixture" + +const ctx: Tool.Context = { + sessionID: SessionID.make("ses_dbt_cost_estimate"), + messageID: MessageID.make("msg_dbt_cost_estimate"), + agent: "analyst", + abort: new AbortController().signal, + messages: [], + metadata: () => Effect.void, + ask: () => Effect.void, +} + +let project: string + +type Calls = { compiled: [string, string | undefined][]; dryRuns: string[] } + +function fakeAdapter(calls: Calls, opts: { type?: string; bytes?: unknown; compileFails?: boolean; dryRunFails?: boolean } = {}) { + return { + isInitialized: () => true, + dispose: async () => {}, + getAdapterType: () => opts.type ?? "bigquery", + unsafeCompileQuery: async (query: string, name?: string) => { + calls.compiled.push([query, name]) + if (opts.compileFails) throw new Error("compilation failed") + return `compiled(${query})` + }, + validateSQLDryRun: async (query: string) => { + calls.dryRuns.push(query) + if (opts.dryRunFails) throw new Error("Access Denied: BigQuery job") + return { bytes_processed: "bytes" in opts ? opts.bytes : "4.2 TiB" } + }, + } +} + +function useAdapter(calls: Calls, opts?: Parameters[1]) { + dbtAdapterInternals.readConfig = async () => ({ + projectRoot: project, + pythonPath: "python3", + dbtIntegration: "corecommand", + queryLimit: 500, + }) + dbtAdapterInternals.create = async () => fakeAdapter(calls, opts) +} + +beforeAll(async () => { + project = await mkdtemp(path.join(os.tmpdir(), "dbt-cost-estimate-")) + await writeFile(path.join(project, "dbt_project.yml"), "name: shop\n") + await mkdir(path.join(project, "models"), { recursive: true }) + await mkdir(path.join(project, "target"), { recursive: true }) + await writeFile(path.join(project, "models", "orders.sql"), "select * from {{ ref('stg_orders') }}\n") + await writeFile( + path.join(project, "target", "manifest.json"), + JSON.stringify({ + metadata: { adapter_type: "bigquery" }, + nodes: { + "model.shop.orders": { + resource_type: "model", + name: "orders", + original_file_path: "models/orders.sql", + raw_code: "select 1 -- stale copy", + }, + "model.shop.gone": { + resource_type: "model", + name: "gone", + original_file_path: "models/gone.sql", + raw_code: "select * from {{ source('raw', 'gone') }}", + }, + }, + }), + ) +}) + +afterAll(async () => { + await rm(project, { recursive: true, force: true }) +}) + +let calls: Calls +beforeEach(() => { + resetDbtAdapter() + calls = { compiled: [], dryRuns: [] } +}) +afterEach(() => { + resetDbtAdapter() + delete dbtAdapterInternals.readConfig + delete dbtAdapterInternals.create +}) + +describe("parseBytesProcessed", () => { + test.each([ + ["0.0 Bytes", 0], + ["512.0 Bytes", 512], + ["1.0 KiB", 1024], + ["1.5 KiB", 1536], + ["2.0 MiB", 2 * 1024 ** 2], + ["4.2 GiB", Math.round(4.2 * 1024 ** 3)], + ["4.2 TiB", Math.round(4.2 * 1024 ** 4)], + ["1.0 PiB", 1024 ** 5], + ["10.5 PiB", Math.round(10.5 * 1024 ** 5)], + [" 7.0 MiB ", 7 * 1024 ** 2], + ])("reads %s as %d bytes", (display, bytes) => { + expect(parseBytesProcessed(display)).toBe(bytes) + }) + + test("takes a number as the count", () => { + expect(parseBytesProcessed(2048)).toBe(2048) + }) + + test.each([["4.2 EiB"], ["4.2"], ["TiB"], [""], ["about 4 GiB"], ["-1.0 KiB"], [null], [undefined], [{}], [Number.NaN]])( + "does not read %p", + (value) => { + expect(parseBytesProcessed(value)).toBeUndefined() + }, + ) +}) + +describe("dbt_cost_estimate", () => { + test("estimates a model from its file, not the manifest's older copy", async () => { + useAdapter(calls) + const result = await (await initTool(DbtCostEstimateTool)).execute({ model: "orders" }, ctx) + expect(calls.compiled).toEqual([["select * from {{ ref('stg_orders') }}\n", "orders"]]) + expect(calls.dryRuns).toEqual(["compiled(select * from {{ ref('stg_orders') }}\n)"]) + expect(result.metadata).toMatchObject({ + success: true, + supported: true, + adapter: "bigquery", + bytes: Math.round(4.2 * 1024 ** 4), + display: "4.2 TiB", + model: "orders", + }) + expect(result.output).toContain("orders would process 4.2 TiB") + }) + + test("falls back to the manifest's SQL when the model's file is gone", async () => { + useAdapter(calls, { bytes: "300.0 MiB" }) + const result = await (await initTool(DbtCostEstimateTool)).execute({ model: "gone" }, ctx) + expect(calls.compiled).toEqual([["select * from {{ source('raw', 'gone') }}", "gone"]]) + expect(result.metadata.bytes).toBe(300 * 1024 ** 2) + }) + + test("estimates SQL as given", async () => { + useAdapter(calls, { bytes: "1.0 GiB" }) + const result = await (await initTool(DbtCostEstimateTool)).execute({ sql: "select 1" }, ctx) + expect(calls.compiled).toEqual([["select 1", undefined]]) + expect(result.metadata).toMatchObject({ supported: true, bytes: 1024 ** 3, display: "1.0 GiB" }) + expect(result.metadata.model).toBeUndefined() + }) + + test.each(["snowflake", "databricks", "duckdb"])("answers supported:false on %s and compiles nothing", async (type) => { + useAdapter(calls, { type }) + const result = await (await initTool(DbtCostEstimateTool)).execute({ model: "orders" }, ctx) + expect(result.metadata).toMatchObject({ success: true, supported: false, adapter: type }) + expect(result.metadata.bytes).toBeUndefined() + expect(result.output).toContain("only available on BigQuery") + expect(calls).toEqual({ compiled: [], dryRuns: [] }) + }) + + test("refuses a read-only call and starts no adapter", async () => { + let created = 0 + useAdapter(calls) + const make = dbtAdapterInternals.create! + dbtAdapterInternals.create = async (config) => { + created += 1 + return make(config) + } + const result = await (await initTool(DbtCostEstimateTool)).execute({ model: "orders" }, { ...ctx, extra: { forceReadOnly: true } }) + expect(created).toBe(0) + expect(result.metadata).toMatchObject({ success: false, refusal: "read_only" }) + expect(result.output).toContain("--allow-write") + }) + + test("asks for something to estimate", async () => { + useAdapter(calls) + const result = await (await initTool(DbtCostEstimateTool)).execute({}, ctx) + expect(result.metadata.success).toBe(false) + expect(calls).toEqual({ compiled: [], dryRuns: [] }) + }) + + test("names a model the manifest doesn't have", async () => { + useAdapter(calls) + const result = await (await initTool(DbtCostEstimateTool)).execute({ model: "nope" }, ctx) + expect(result.metadata.success).toBe(false) + expect(result.output).toContain('no model "nope"') + expect(calls).toEqual({ compiled: [], dryRuns: [] }) + }) + + test("reports a compile error without a dry run", async () => { + useAdapter(calls, { compileFails: true }) + const result = await (await initTool(DbtCostEstimateTool)).execute({ sql: "select" }, ctx) + expect(result.metadata).toMatchObject({ success: false, error: "compilation failed" }) + expect(calls.dryRuns).toEqual([]) + }) + + test("reports a dry run the warehouse refused", async () => { + useAdapter(calls, { dryRunFails: true }) + const result = await (await initTool(DbtCostEstimateTool)).execute({ sql: "select 1" }, ctx) + expect(result.metadata).toMatchObject({ success: false, error: "Access Denied: BigQuery job" }) + }) + + test("reports a size it can't read instead of guessing", async () => { + useAdapter(calls, { bytes: "lots" }) + const result = await (await initTool(DbtCostEstimateTool)).execute({ sql: "select 1" }, ctx) + expect(result.metadata.success).toBe(false) + expect(result.metadata.bytes).toBeUndefined() + expect(result.output).toContain('"lots"') + }) +}) From d1913551ef5aaa50e7c65c8926ad3a15785a357d Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 16:54:43 +0530 Subject: [PATCH 03/28] test: dbt_cost_estimate is no longer a placeholder Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/test/altimate/dbt-adapter-access.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index b565e5a58a..ea97517cd0 100644 --- a/packages/opencode/test/altimate/dbt-adapter-access.test.ts +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -30,6 +30,8 @@ const MINIMAL_ARGS: Record> = { dbt_cte_profile: { model: "customers" }, } const FEATURE_TOOLS = Object.keys(MINIMAL_ARGS) +/** Tools whose real implementation has landed; they no longer answer as placeholders. */ +const IMPLEMENTED = new Set(["dbt_cost_estimate"]) const ctx: Tool.Context = { sessionID: SessionID.make("ses_dbt_adapter_access"), @@ -82,7 +84,7 @@ describe("feature tools in the allow-list", () => { }) test("each placeholder answers that it isn't implemented, as a failure", async () => { - for (const id of FEATURE_TOOLS) { + 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) From 21d0494cfbb1c87a2b1b3ba6e347f4f1be149856 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 17:01:49 +0530 Subject: [PATCH 04/28] fix: harden the dbt Power User tool foundation Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/api/client.ts | 4 +- .../src/altimate/tools/dbt-adapter-access.ts | 3 +- .../src/altimate/tools/dbt-cost-estimate.ts | 2 +- .../src/altimate/tools/dbt-cte-profile.ts | 2 +- .../src/altimate/tools/dbt-defer-state.ts | 2 +- .../src/altimate/tools/dbt-docs-coverage.ts | 10 ++-- .../opencode/src/altimate/tools/dbt-health.ts | 2 +- .../src/altimate/tools/dbt-map-relations.ts | 7 ++- .../src/altimate/tools/dbt-run-results.ts | 7 ++- .../src/altimate/tools/dbt-sql-graph.ts | 2 +- .../src/altimate/tools/docs-share-comments.ts | 2 +- .../test/altimate/dbt-adapter-access.test.ts | 52 ++++++++++++++++++- 12 files changed, 78 insertions(+), 17 deletions(-) diff --git a/packages/opencode/src/altimate/api/client.ts b/packages/opencode/src/altimate/api/client.ts index 069a182b7a..cb53c0ec7b 100644 --- a/packages/opencode/src/altimate/api/client.ts +++ b/packages/opencode/src/altimate/api/client.ts @@ -359,7 +359,9 @@ export namespace AltimateApi { } 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( + `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) diff --git a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts index a2711e35d1..d73dd7e9c5 100644 --- a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts +++ b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts @@ -67,7 +67,8 @@ export async function dbtAdapterFor(ctx: Pick { expect(tool.id).toBe(id) const result = await tool.execute(MINIMAL_ARGS[id], ctx) expect(result.title).toBe(`${id}: not implemented`) - expect(result.metadata.success).toBe(false) + expect(result.metadata).toEqual({ success: false, error: "not implemented", truncated: false }) expect(result.output).toContain(id) } }) @@ -149,6 +149,30 @@ describe("dbtAdapterFor", () => { } }) + 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() @@ -169,6 +193,28 @@ describe("dbtAdapterFor", () => { 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", () => { @@ -216,6 +262,8 @@ describe("AltimateApi.apiRequest", () => { 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("can't be used") + await expect(AltimateApi.apiRequest("GET", "/dbt/v1/x")).rejects.toThrow( + `The Altimate credentials in ${path.join(home, ".altimate", "altimate.json")} can't be used`, + ) }) }) From d90e33493a51ca468ea0d1f0b7f4be615648b43e Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 17:15:09 +0530 Subject: [PATCH 05/28] fix: compile a model by name, give the scan an upper bound, stop waiting when cancelled Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-cost-estimate.ts | 107 ++++++------ .../test/altimate/dbt-cost-estimate.test.ts | 162 ++++++++++-------- 2 files changed, 143 insertions(+), 126 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index 2473a89969..97597f5e00 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -1,44 +1,39 @@ -import fs from "fs" -import path from "path" import z from "zod" import { Tool } from "../../tool/tool" import { dbtAdapterFor } from "./dbt-adapter-access" -import { findModel, loadRawManifest } from "../native/dbt/helpers" /** The units dbt-bigquery's `format_bytes` writes, smallest first; each is 1024 times the one before. */ const UNITS = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"] as const /** * The byte count in a dry run's `bytes_processed`, which dbt-bigquery formats as text ("4.2 TiB", - * "512.0 Bytes"). The text carries one decimal, so a figure past a KiB is an approximation. Undefined - * for anything else, so a changed format is reported, never read as a count. + * "512.0 Bytes"), and the most it can be. The text carries one decimal, so a figure past a byte is + * rounded: the true count is within half a tenth of its unit. Undefined for anything else, so a changed + * format is reported, never read as a count. */ -export function parseBytesProcessed(display: unknown): number | undefined { - if (typeof display === "number") return Number.isFinite(display) && display >= 0 ? Math.round(display) : undefined +export function parseBytesProcessed(display: unknown): { bytes: number; maxBytes: number } | undefined { + if (typeof display === "number") { + if (!Number.isFinite(display) || display < 0) return undefined + const bytes = Math.round(display) + return { bytes, maxBytes: bytes } + } if (typeof display !== "string") return undefined const match = /^\s*(\d+(?:\.\d+)?)\s*(Bytes|KiB|MiB|GiB|TiB|PiB)\s*$/i.exec(display) if (!match) return undefined const unit = UNITS.findIndex((u) => u.toLowerCase() === match[2].toLowerCase()) - return Math.round(Number(match[1]) * 1024 ** unit) + const scale = 1024 ** unit + const bytes = Math.round(Number(match[1]) * scale) + return { bytes, maxBytes: unit === 0 ? bytes : Math.ceil((Number(match[1]) + 0.05) * scale) } } -/** The SQL a model's file holds, as Power User reads it, or the manifest's copy when the file is gone. */ -async function modelSource(model: string, root: string | undefined): Promise<{ sql?: string; name?: string; error?: string }> { - const manifestPath = path.join(root ?? "", "target", "manifest.json") - let node: any - try { - node = findModel(loadRawManifest(manifestPath)?.nodes ?? {}, model) - } catch (e) { - return { error: `couldn't read ${manifestPath}: ${e instanceof Error ? e.message : String(e)}` } - } - if (!node) return { error: `no model "${model}" in ${manifestPath}: run dbt parse or dbt compile first` } - const file = root && node.original_file_path ? path.join(root, node.original_file_path) : undefined - let sql: string | undefined - try { - if (file) sql = fs.readFileSync(file, "utf-8") - } catch {} - sql ??= typeof node.raw_code === "string" ? node.raw_code : undefined - return sql?.trim() ? { sql, name: node.name } : { error: `model "${model}" has no SQL` } +/** Waits for `work`, but gives up as soon as the call is cancelled. */ +function untilAbort(work: Promise, signal: AbortSignal): Promise { + if (signal.aborted) return Promise.reject(signal.reason) + return new Promise((resolve, reject) => { + const stop = () => reject(signal.reason) + signal.addEventListener("abort", stop, { once: true }) + work.then(resolve, reject).finally(() => signal.removeEventListener("abort", stop)) + }) } function failure(title: string, message: string, extra: Record = {}) { @@ -47,7 +42,7 @@ function failure(title: string, message: string, extra: Record export const DbtCostEstimateTool = Tool.define("dbt_cost_estimate", { description: - "Estimate the bytes a dbt model or a SQL query would scan, from a warehouse dry run. Only BigQuery has a dry run: any other warehouse answers `supported: false` and nothing is compiled. Starts the dbt adapter, so it needs write permission (`--allow-write` on `tool run`). Pass `model` (a model name; its file is compiled) or `sql` (may use Jinja).", + "Estimate the bytes a dbt model or a SQL query would scan, from a warehouse dry run. Only BigQuery has a dry run: any other warehouse answers `supported: false` and nothing is compiled. Starts the dbt adapter, so it needs write permission (`--allow-write` on `tool run`). Pass `model` (a model name, compiled as dbt would) or `sql` (may use Jinja).", 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"), @@ -67,39 +62,41 @@ export const DbtCostEstimateTool = Tool.define("dbt_cost_estimate", { output: `No cost estimate: dry runs are only available on BigQuery (this project uses ${adapterName}).`, } } + const signal = ctx.abort + let compiled: unknown try { - let query = args.sql - let name = args.model - if (!query) { - const root = await (await import("../native/connections/register")).getDbtProjectRoot() - const source = await modelSource(args.model!, root) - if (!source.sql) return failure("model not found", source.error ?? "No SQL to estimate.", { adapter: type }) - query = source.sql - name = source.name ?? name - } - ctx.abort.throwIfAborted() - const compiled = await adapter.unsafeCompileQuery(query, name) - ctx.abort.throwIfAborted() - if (typeof compiled !== "string" || !compiled.trim()) { - return failure("compile failed", "The query compiled to nothing.", { adapter: type }) - } - const result = await adapter.validateSQLDryRun(compiled) - ctx.abort.throwIfAborted() - const display = result?.bytes_processed - const bytes = parseBytesProcessed(display) - if (bytes === undefined) { - return failure("unreadable estimate", `The dry run returned an amount this tool can't read: ${JSON.stringify(display)}.`, { - adapter: type, - }) - } - return { - title: `dbt_cost_estimate: ${display}`, - metadata: { success: true, supported: true, adapter: type, bytes, display: String(display), ...(name && { model: name }) }, - output: `${name ? `${name} ` : "The query "}would process ${display} (about ${bytes} bytes).`, + // A model compiles by name, so its node context (`this`, config) is kept; SQL compiles inline. + compiled = await untilAbort(args.model ? adapter.unsafeCompileNode(args.model) : adapter.unsafeCompileQuery(args.sql!, undefined), signal) + } catch (e) { + if (signal.aborted) throw e + return failure("compile failed", e instanceof Error ? e.message : String(e), { adapter: type }) + } + if (typeof compiled !== "string" || !compiled.trim()) { + return failure("compile failed", "The query compiled to nothing.", { adapter: type }) + } + let display: unknown + try { + const result = await untilAbort(adapter.validateSQLDryRun(compiled), signal) + if (result == null) { + return failure("dry run failed", "The dry run returned no estimate: dbt-core before 1.6 can't dry run.", { adapter: type }) } + display = (result as { bytes_processed?: unknown }).bytes_processed } catch (e) { - if (ctx.abort.aborted) throw e + if (signal.aborted) throw e return failure("dry run failed", e instanceof Error ? e.message : String(e), { adapter: type }) } + const parsed = parseBytesProcessed(display) + if (!parsed) { + return failure("unreadable estimate", `The dry run returned an amount this tool can't read: ${JSON.stringify(display)}.`, { + adapter: type, + }) + } + const name = args.model + const shown = typeof display === "number" ? `${parsed.bytes} Bytes` : String(display) + return { + title: `dbt_cost_estimate: ${shown}`, + metadata: { success: true, supported: true, adapter: type, ...parsed, display: shown, ...(name && { model: name }) }, + output: `${name ? `${name} ` : "The query "}would process ${shown} (at most ${parsed.maxBytes} bytes).`, + } }, }) diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index 01758a711b..9a17faac16 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -1,8 +1,8 @@ -// dbt_cost_estimate: the dry run's formatted size read back into bytes for each unit, the answer for a -// warehouse with no dry run, a read-only call, and the model's file being compiled. The dbt adapter is +// dbt_cost_estimate: the dry run's formatted size read back into bytes (and its upper bound) for each unit, +// the answer for a warehouse with no dry run, a read-only call, and a model compiled by name. The dbt adapter is // replaced through `dbtAdapterInternals`, so these tests need no Python, dbt or warehouse. import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from "bun:test" -import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises" +import { mkdtemp, rm, writeFile } from "node:fs/promises" import os from "node:os" import path from "node:path" import { Effect } from "effect" @@ -24,22 +24,31 @@ const ctx: Tool.Context = { let project: string -type Calls = { compiled: [string, string | undefined][]; dryRuns: string[] } +type Calls = { nodes: string[]; queries: [string, string | undefined][]; dryRuns: string[] } -function fakeAdapter(calls: Calls, opts: { type?: string; bytes?: unknown; compileFails?: boolean; dryRunFails?: boolean } = {}) { +function fakeAdapter( + calls: Calls, + opts: { type?: string; bytes?: unknown; compileFails?: boolean; dryRunFails?: boolean; compile?: () => Promise } = {}, +) { return { isInitialized: () => true, dispose: async () => {}, getAdapterType: () => opts.type ?? "bigquery", + unsafeCompileNode: async (model: string) => { + calls.nodes.push(model) + if (opts.compile) return opts.compile() + if (opts.compileFails) throw new Error("compilation failed") + return `compiled(${model})` + }, unsafeCompileQuery: async (query: string, name?: string) => { - calls.compiled.push([query, name]) + calls.queries.push([query, name]) if (opts.compileFails) throw new Error("compilation failed") return `compiled(${query})` }, validateSQLDryRun: async (query: string) => { calls.dryRuns.push(query) if (opts.dryRunFails) throw new Error("Access Denied: BigQuery job") - return { bytes_processed: "bytes" in opts ? opts.bytes : "4.2 TiB" } + return "bytes" in opts && opts.bytes === null ? null : { bytes_processed: "bytes" in opts ? opts.bytes : "4.2 TiB" } }, } } @@ -57,29 +66,6 @@ function useAdapter(calls: Calls, opts?: Parameters[1]) { beforeAll(async () => { project = await mkdtemp(path.join(os.tmpdir(), "dbt-cost-estimate-")) await writeFile(path.join(project, "dbt_project.yml"), "name: shop\n") - await mkdir(path.join(project, "models"), { recursive: true }) - await mkdir(path.join(project, "target"), { recursive: true }) - await writeFile(path.join(project, "models", "orders.sql"), "select * from {{ ref('stg_orders') }}\n") - await writeFile( - path.join(project, "target", "manifest.json"), - JSON.stringify({ - metadata: { adapter_type: "bigquery" }, - nodes: { - "model.shop.orders": { - resource_type: "model", - name: "orders", - original_file_path: "models/orders.sql", - raw_code: "select 1 -- stale copy", - }, - "model.shop.gone": { - resource_type: "model", - name: "gone", - original_file_path: "models/gone.sql", - raw_code: "select * from {{ source('raw', 'gone') }}", - }, - }, - }), - ) }) afterAll(async () => { @@ -89,7 +75,7 @@ afterAll(async () => { let calls: Calls beforeEach(() => { resetDbtAdapter() - calls = { compiled: [], dryRuns: [] } + calls = { nodes: [], queries: [], dryRuns: [] } }) afterEach(() => { resetDbtAdapter() @@ -99,25 +85,33 @@ afterEach(() => { describe("parseBytesProcessed", () => { test.each([ - ["0.0 Bytes", 0], - ["512.0 Bytes", 512], - ["1.0 KiB", 1024], - ["1.5 KiB", 1536], - ["2.0 MiB", 2 * 1024 ** 2], - ["4.2 GiB", Math.round(4.2 * 1024 ** 3)], - ["4.2 TiB", Math.round(4.2 * 1024 ** 4)], - ["1.0 PiB", 1024 ** 5], - ["10.5 PiB", Math.round(10.5 * 1024 ** 5)], - [" 7.0 MiB ", 7 * 1024 ** 2], - ])("reads %s as %d bytes", (display, bytes) => { - expect(parseBytesProcessed(display)).toBe(bytes) + ["0.0 Bytes", 0, 0], + ["512.0 Bytes", 512, 512], + ["1.0 KiB", 1024, Math.ceil(1.05 * 1024)], + ["1.5 KiB", 1536, Math.ceil(1.55 * 1024)], + ["2.0 MiB", 2 * 1024 ** 2, Math.ceil(2.05 * 1024 ** 2)], + ["4.2 GiB", Math.round(4.2 * 1024 ** 3), Math.ceil(4.25 * 1024 ** 3)], + ["4.2 TiB", Math.round(4.2 * 1024 ** 4), Math.ceil(4.25 * 1024 ** 4)], + ["1.0 PiB", 1024 ** 5, Math.ceil(1.05 * 1024 ** 5)], + ["10.5 PiB", Math.round(10.5 * 1024 ** 5), Math.ceil(10.55 * 1024 ** 5)], + [" 7.0 MiB ", 7 * 1024 ** 2, Math.ceil(7.05 * 1024 ** 2)], + ])("reads %s as %d bytes, at most %d", (display, bytes, maxBytes) => { + expect(parseBytesProcessed(display)).toEqual({ bytes, maxBytes }) }) - test("takes a number as the count", () => { - expect(parseBytesProcessed(2048)).toBe(2048) + test("a rounded figure's upper bound covers a scan just over a limit", () => { + // 10 GiB + 20 MiB is shown as "10.0 GiB": the point estimate sits on the limit, the bound above it. + const over = 10 * 1024 ** 3 + 20 * 1024 ** 2 + const parsed = parseBytesProcessed("10.0 GiB")! + expect(parsed.bytes).toBe(10 * 1024 ** 3) + expect(parsed.maxBytes).toBeGreaterThanOrEqual(over) }) - test.each([["4.2 EiB"], ["4.2"], ["TiB"], [""], ["about 4 GiB"], ["-1.0 KiB"], [null], [undefined], [{}], [Number.NaN]])( + test("takes a number as the exact count", () => { + expect(parseBytesProcessed(2048)).toEqual({ bytes: 2048, maxBytes: 2048 }) + }) + + test.each([["4.2 EiB"], ["4.2"], ["TiB"], [""], ["about 4 GiB"], ["-1.0 KiB"], [null], [undefined], [{}], [Number.NaN], [-5]])( "does not read %p", (value) => { expect(parseBytesProcessed(value)).toBeUndefined() @@ -126,35 +120,43 @@ describe("parseBytesProcessed", () => { }) describe("dbt_cost_estimate", () => { - test("estimates a model from its file, not the manifest's older copy", async () => { + test("estimates a model compiled by name", async () => { useAdapter(calls) const result = await (await initTool(DbtCostEstimateTool)).execute({ model: "orders" }, ctx) - expect(calls.compiled).toEqual([["select * from {{ ref('stg_orders') }}\n", "orders"]]) - expect(calls.dryRuns).toEqual(["compiled(select * from {{ ref('stg_orders') }}\n)"]) - expect(result.metadata).toMatchObject({ + expect(calls).toEqual({ nodes: ["orders"], queries: [], dryRuns: ["compiled(orders)"] }) + expect(result.metadata).toEqual({ success: true, supported: true, adapter: "bigquery", bytes: Math.round(4.2 * 1024 ** 4), + maxBytes: Math.ceil(4.25 * 1024 ** 4), display: "4.2 TiB", model: "orders", + truncated: false, }) expect(result.output).toContain("orders would process 4.2 TiB") }) - test("falls back to the manifest's SQL when the model's file is gone", async () => { - useAdapter(calls, { bytes: "300.0 MiB" }) - const result = await (await initTool(DbtCostEstimateTool)).execute({ model: "gone" }, ctx) - expect(calls.compiled).toEqual([["select * from {{ source('raw', 'gone') }}", "gone"]]) - expect(result.metadata.bytes).toBe(300 * 1024 ** 2) - }) - test("estimates SQL as given", async () => { useAdapter(calls, { bytes: "1.0 GiB" }) const result = await (await initTool(DbtCostEstimateTool)).execute({ sql: "select 1" }, ctx) - expect(calls.compiled).toEqual([["select 1", undefined]]) - expect(result.metadata).toMatchObject({ supported: true, bytes: 1024 ** 3, display: "1.0 GiB" }) - expect(result.metadata.model).toBeUndefined() + expect(calls).toEqual({ nodes: [], queries: [["select 1", undefined]], dryRuns: ["compiled(select 1)"] }) + expect(result.metadata).toEqual({ + success: true, + supported: true, + adapter: "bigquery", + bytes: 1024 ** 3, + maxBytes: Math.ceil(1.05 * 1024 ** 3), + display: "1.0 GiB", + truncated: false, + }) + }) + + test("a number from the dry run is shown with its unit and is exact", async () => { + useAdapter(calls, { bytes: 2048 }) + const result = await (await initTool(DbtCostEstimateTool)).execute({ sql: "select 1" }, ctx) + expect(result.metadata).toMatchObject({ bytes: 2048, maxBytes: 2048, display: "2048 Bytes" }) + expect(result.output).toContain("2048 Bytes") }) test.each(["snowflake", "databricks", "duckdb"])("answers supported:false on %s and compiles nothing", async (type) => { @@ -163,7 +165,7 @@ describe("dbt_cost_estimate", () => { expect(result.metadata).toMatchObject({ success: true, supported: false, adapter: type }) expect(result.metadata.bytes).toBeUndefined() expect(result.output).toContain("only available on BigQuery") - expect(calls).toEqual({ compiled: [], dryRuns: [] }) + expect(calls).toEqual({ nodes: [], queries: [], dryRuns: [] }) }) test("refuses a read-only call and starts no adapter", async () => { @@ -184,27 +186,45 @@ describe("dbt_cost_estimate", () => { useAdapter(calls) const result = await (await initTool(DbtCostEstimateTool)).execute({}, ctx) expect(result.metadata.success).toBe(false) - expect(calls).toEqual({ compiled: [], dryRuns: [] }) + expect(calls).toEqual({ nodes: [], queries: [], dryRuns: [] }) }) - test("names a model the manifest doesn't have", async () => { - useAdapter(calls) + test("reports a model dbt can't compile, as a compile failure, without a dry run", async () => { + useAdapter(calls, { compileFails: true }) const result = await (await initTool(DbtCostEstimateTool)).execute({ model: "nope" }, ctx) - expect(result.metadata.success).toBe(false) - expect(result.output).toContain('no model "nope"') - expect(calls).toEqual({ compiled: [], dryRuns: [] }) + expect(result.title).toBe("dbt_cost_estimate: compile failed") + expect(result.metadata).toMatchObject({ success: false, error: "compilation failed" }) + expect(calls.dryRuns).toEqual([]) }) - test("reports a compile error without a dry run", async () => { - useAdapter(calls, { compileFails: true }) - const result = await (await initTool(DbtCostEstimateTool)).execute({ sql: "select" }, ctx) - expect(result.metadata).toMatchObject({ success: false, error: "compilation failed" }) + test("reports a compile that produced nothing, without a dry run", async () => { + useAdapter(calls, { compile: async () => " " }) + const result = await (await initTool(DbtCostEstimateTool)).execute({ model: "orders" }, ctx) + expect(result.metadata).toMatchObject({ success: false, error: "The query compiled to nothing." }) + expect(calls.dryRuns).toEqual([]) + }) + + test("reports a dry run that returns nothing, as dbt-core before 1.6 does", async () => { + useAdapter(calls, { bytes: null }) + const result = await (await initTool(DbtCostEstimateTool)).execute({ sql: "select 1" }, ctx) + expect(result.metadata).toMatchObject({ success: false }) + expect(result.output).toContain("before 1.6") + }) + + test("a cancelled call stops waiting for a compile that never finishes", async () => { + const abort = new AbortController() + useAdapter(calls, { compile: () => new Promise(() => {}) }) + const run = (await initTool(DbtCostEstimateTool)).execute({ model: "orders" }, { ...ctx, abort: abort.signal }) + await Bun.sleep(20) + abort.abort(new Error("cancelled")) + await expect(run).rejects.toThrow("cancelled") expect(calls.dryRuns).toEqual([]) }) test("reports a dry run the warehouse refused", async () => { useAdapter(calls, { dryRunFails: true }) const result = await (await initTool(DbtCostEstimateTool)).execute({ sql: "select 1" }, ctx) + expect(result.title).toBe("dbt_cost_estimate: dry run failed") expect(result.metadata).toMatchObject({ success: false, error: "Access Denied: BigQuery job" }) }) From 9f19a755adee4a4d241b757a963974bdd2638c57 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 17:24:17 +0530 Subject: [PATCH 06/28] fix: start no compile for a cancelled call, and bound a rounded scan exactly Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-cost-estimate.ts | 27 ++++++++++++++----- .../test/altimate/dbt-cost-estimate.test.ts | 14 ++++++++-- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index 97597f5e00..20e9ba840a 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -23,16 +23,28 @@ export function parseBytesProcessed(display: unknown): { bytes: number; maxBytes const unit = UNITS.findIndex((u) => u.toLowerCase() === match[2].toLowerCase()) const scale = 1024 ** unit const bytes = Math.round(Number(match[1]) * scale) - return { bytes, maxBytes: unit === 0 ? bytes : Math.ceil((Number(match[1]) + 0.05) * scale) } + if (unit === 0) return { bytes, maxBytes: bytes } + // Exact in integers: the shown figure is rounded to its last digit, so the true count is at most half + // of that digit above it. A float product is off by whole bytes past a few TiB. + const [whole, fraction = ""] = match[1].split(".") + const digits = BigInt(whole + fraction) + let upper = ((digits * 2n + 1n) * 1024n ** BigInt(unit) + 2n * 10n ** BigInt(fraction.length) - 1n) / (2n * 10n ** BigInt(fraction.length)) + // Python divides in floats, so a count within a float's last place of a tie is shown rounded down. + upper += upper >> 50n + let maxBytes = Number(upper) + if (BigInt(Math.trunc(maxBytes)) < upper) maxBytes *= 1 + Number.EPSILON + return { bytes, maxBytes: Math.max(bytes, maxBytes) } } -/** Waits for `work`, but gives up as soon as the call is cancelled. */ -function untilAbort(work: Promise, signal: AbortSignal): Promise { +/** Starts `start` and waits for it, but gives up as soon as the call is cancelled; a cancelled call starts nothing. */ +function untilAbort(start: () => Promise, signal: AbortSignal): Promise { if (signal.aborted) return Promise.reject(signal.reason) return new Promise((resolve, reject) => { const stop = () => reject(signal.reason) signal.addEventListener("abort", stop, { once: true }) - work.then(resolve, reject).finally(() => signal.removeEventListener("abort", stop)) + start() + .then(resolve, reject) + .finally(() => signal.removeEventListener("abort", stop)) }) } @@ -66,7 +78,10 @@ export const DbtCostEstimateTool = Tool.define("dbt_cost_estimate", { let compiled: unknown try { // A model compiles by name, so its node context (`this`, config) is kept; SQL compiles inline. - compiled = await untilAbort(args.model ? adapter.unsafeCompileNode(args.model) : adapter.unsafeCompileQuery(args.sql!, undefined), signal) + compiled = await untilAbort( + () => (args.model ? adapter.unsafeCompileNode(args.model) : adapter.unsafeCompileQuery(args.sql!, undefined)), + signal, + ) } catch (e) { if (signal.aborted) throw e return failure("compile failed", e instanceof Error ? e.message : String(e), { adapter: type }) @@ -76,7 +91,7 @@ export const DbtCostEstimateTool = Tool.define("dbt_cost_estimate", { } let display: unknown try { - const result = await untilAbort(adapter.validateSQLDryRun(compiled), signal) + const result = await untilAbort(() => adapter.validateSQLDryRun(compiled), signal) if (result == null) { return failure("dry run failed", "The dry run returned no estimate: dbt-core before 1.6 can't dry run.", { adapter: type }) } diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index 9a17faac16..80cf538351 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -95,8 +95,12 @@ describe("parseBytesProcessed", () => { ["1.0 PiB", 1024 ** 5, Math.ceil(1.05 * 1024 ** 5)], ["10.5 PiB", Math.round(10.5 * 1024 ** 5), Math.ceil(10.55 * 1024 ** 5)], [" 7.0 MiB ", 7 * 1024 ** 2, Math.ceil(7.05 * 1024 ** 2)], - ])("reads %s as %d bytes, at most %d", (display, bytes, maxBytes) => { - expect(parseBytesProcessed(display)).toEqual({ bytes, maxBytes }) + ])("reads %s as %d bytes, at most about %d", (display, bytes, maxBytes) => { + const parsed = parseBytesProcessed(display)! + expect(parsed.bytes).toBe(bytes) + // The bound is the figure plus half its last digit; past a PiB it carries a few bytes of float margin. + expect(parsed.maxBytes).toBeGreaterThanOrEqual(maxBytes) + expect(parsed.maxBytes).toBeLessThanOrEqual(maxBytes + maxBytes * 1e-14 + 1) }) test("a rounded figure's upper bound covers a scan just over a limit", () => { @@ -107,6 +111,12 @@ describe("parseBytesProcessed", () => { expect(parsed.maxBytes).toBeGreaterThanOrEqual(over) }) + test("the upper bound reaches a count just past a tie that the dry run shows rounded down", () => { + // 9.25 PiB is a tie in the dry run's float formatting, which shows it as "9.2": the bound sits above the tie. + const tie = 37n * 1024n ** 5n / 4n + expect(BigInt(parseBytesProcessed("9.2 PiB")!.maxBytes)).toBeGreaterThan(tie) + }) + test("takes a number as the exact count", () => { expect(parseBytesProcessed(2048)).toEqual({ bytes: 2048, maxBytes: 2048 }) }) From 1c23d351bfa05bf390eb1039b0e39d774879ccc5 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 17:31:52 +0530 Subject: [PATCH 07/28] fix: clean up the cancel listener when starting the work throws Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/tools/dbt-cost-estimate.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index 20e9ba840a..120cb52d78 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -42,7 +42,8 @@ function untilAbort(start: () => Promise, signal: AbortSignal): Promise return new Promise((resolve, reject) => { const stop = () => reject(signal.reason) signal.addEventListener("abort", stop, { once: true }) - start() + Promise.resolve() + .then(start) .then(resolve, reject) .finally(() => signal.removeEventListener("abort", stop)) }) From 4fa4703d51c22da411fdcb126d43599d9f051c5a Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sat, 14 Nov 2026 08:04:14 +0530 Subject: [PATCH 08/28] fix: a call cancelled before its queued work runs starts no compile Co-Authored-By: Claude Sonnet 5.5 --- .../opencode/src/altimate/tools/dbt-cost-estimate.ts | 2 +- .../opencode/test/altimate/dbt-cost-estimate.test.ts | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index 120cb52d78..8f2846443e 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -43,7 +43,7 @@ function untilAbort(start: () => Promise, signal: AbortSignal): Promise const stop = () => reject(signal.reason) signal.addEventListener("abort", stop, { once: true }) Promise.resolve() - .then(start) + .then(() => (signal.aborted ? Promise.reject(signal.reason) : start())) .then(resolve, reject) .finally(() => signal.removeEventListener("abort", stop)) }) diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index 80cf538351..0c73b929f0 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -231,6 +231,17 @@ describe("dbt_cost_estimate", () => { expect(calls.dryRuns).toEqual([]) }) + test("a call cancelled before the queued work runs starts no compile", async () => { + const abort = new AbortController() + useAdapter(calls, {}) + const run = (await initTool(DbtCostEstimateTool)).execute({ model: "orders" }, { ...ctx, abort: abort.signal }) + abort.abort(new Error("cancelled")) + await expect(run).rejects.toThrow("cancelled") + await Bun.sleep(20) + expect(calls.nodes).toEqual([]) + expect(calls.dryRuns).toEqual([]) + }) + test("reports a dry run the warehouse refused", async () => { useAdapter(calls, { dryRunFails: true }) const result = await (await initTool(DbtCostEstimateTool)).execute({ sql: "select 1" }, ctx) From 9f4ae46db02ac55ee5017b50ee87c9c3e2380638 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sat, 14 Nov 2026 08:08:16 +0530 Subject: [PATCH 09/28] test: cover work queued before a cancellation with a signal that flips between the checks Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-cost-estimate.ts | 2 +- .../test/altimate/dbt-cost-estimate.test.ts | 25 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index 8f2846443e..935819ff6a 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -37,7 +37,7 @@ export function parseBytesProcessed(display: unknown): { bytes: number; maxBytes } /** Starts `start` and waits for it, but gives up as soon as the call is cancelled; a cancelled call starts nothing. */ -function untilAbort(start: () => Promise, signal: AbortSignal): Promise { +export function untilAbort(start: () => Promise, signal: AbortSignal): Promise { if (signal.aborted) return Promise.reject(signal.reason) return new Promise((resolve, reject) => { const stop = () => reject(signal.reason) diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index 0c73b929f0..c3efdcdd16 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -9,7 +9,7 @@ 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 { DbtCostEstimateTool, parseBytesProcessed } from "@/altimate/tools/dbt-cost-estimate" +import { DbtCostEstimateTool, parseBytesProcessed, untilAbort } from "@/altimate/tools/dbt-cost-estimate" import { initTool } from "./tool-fixture" const ctx: Tool.Context = { @@ -231,15 +231,20 @@ describe("dbt_cost_estimate", () => { expect(calls.dryRuns).toEqual([]) }) - test("a call cancelled before the queued work runs starts no compile", async () => { - const abort = new AbortController() - useAdapter(calls, {}) - const run = (await initTool(DbtCostEstimateTool)).execute({ model: "orders" }, { ...ctx, abort: abort.signal }) - abort.abort(new Error("cancelled")) - await expect(run).rejects.toThrow("cancelled") - await Bun.sleep(20) - expect(calls.nodes).toEqual([]) - expect(calls.dryRuns).toEqual([]) + test("work queued before a cancellation is not started by it", async () => { + // The signal reads as live at the first check and cancelled once the queued work comes to run. + let reads = 0 + const signal = { + get aborted() { + return ++reads > 1 + }, + reason: new Error("cancelled"), + addEventListener() {}, + removeEventListener() {}, + } as unknown as AbortSignal + let started = 0 + await expect(untilAbort(async () => void started++, signal)).rejects.toThrow("cancelled") + expect(started).toBe(0) }) test("reports a dry run the warehouse refused", async () => { From 99e953c9d21939cd9207b781bcda7fb1cd7e9797 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sun, 15 Nov 2026 22:16:53 +0530 Subject: [PATCH 10/28] fix: reject a call with both model and sql, and word the read-only refusal for plan mode Co-Authored-By: Claude Sonnet 5.5 --- .../opencode/src/altimate/tools/dbt-adapter-access.ts | 2 +- packages/opencode/src/altimate/tools/dbt-cost-estimate.ts | 1 + .../opencode/test/altimate/dbt-adapter-access.test.ts | 1 + packages/opencode/test/altimate/dbt-cost-estimate.test.ts | 8 ++++++++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts index d73dd7e9c5..fe54f0dd63 100644 --- a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts +++ b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts @@ -28,7 +28,7 @@ export async function dbtAdapterFor(ctx: Pick { expect(access.adapter).toBeUndefined() expect(access.refusal).toBe("read_only") expect(access.reason).toContain("--allow-write") + expect(access.reason).toContain("leave plan mode") expect(created).toBe(0) }) diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index c3efdcdd16..5ce8e8b76d 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -192,6 +192,14 @@ describe("dbt_cost_estimate", () => { expect(result.output).toContain("--allow-write") }) + test("rejects a call that passes both a model and SQL, estimating neither", async () => { + useAdapter(calls) + const result = await (await initTool(DbtCostEstimateTool)).execute({ model: "orders", sql: "select 1" }, ctx) + expect(result.metadata.success).toBe(false) + expect(result.output).toContain("not both") + expect(calls).toEqual({ nodes: [], queries: [], dryRuns: [] }) + }) + test("asks for something to estimate", async () => { useAdapter(calls) const result = await (await initTool(DbtCostEstimateTool)).execute({}, ctx) From 6c2ab6fbe7a0b4bca0ed465e5ab75ec5f8cde65a Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sun, 15 Nov 2026 22:24:24 +0530 Subject: [PATCH 11/28] fix: name the MCP server in the read-only refusal Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/tools/dbt-adapter-access.ts | 2 +- packages/opencode/test/altimate/dbt-adapter-access.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts index fe54f0dd63..ed4cd620c3 100644 --- a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts +++ b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts @@ -28,7 +28,7 @@ export async function dbtAdapterFor(ctx: Pick { expect(access.adapter).toBeUndefined() expect(access.refusal).toBe("read_only") expect(access.reason).toContain("--allow-write") - expect(access.reason).toContain("leave plan mode") + expect(access.reason).toContain("MCP server") expect(created).toBe(0) }) From d8c34693b09dd856b0efd2c4a1924c7320346720 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sun, 15 Nov 2026 22:27:01 +0530 Subject: [PATCH 12/28] fix: send falsy API bodies, follow cancellation in apiRequest, check abort before a read-only refusal - apiRequest sends any body but undefined (null, false, 0 and "" were dropped) and takes an optional signal that cancels the request along with its 15 s timeout. - dbtAdapterFor throws for a cancelled call before it would refuse a read-only or plan-mode one. - Tests cover these and a session inside the adapter's own project. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/api/client.ts | 25 ++++++-- .../src/altimate/tools/dbt-adapter-access.ts | 2 + .../test/altimate/dbt-adapter-access.test.ts | 63 +++++++++++++++++++ 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/packages/opencode/src/altimate/api/client.ts b/packages/opencode/src/altimate/api/client.ts index cb53c0ec7b..6ea7d96354 100644 --- a/packages/opencode/src/altimate/api/client.ts +++ b/packages/opencode/src/altimate/api/client.ts @@ -318,7 +318,13 @@ export namespace AltimateApi { return data.auth_token } - async function request(creds: AltimateCredentials, method: string, endpoint: string, body?: unknown) { + async function request( + creds: AltimateCredentials, + method: string, + endpoint: string, + body?: unknown, + signal?: AbortSignal, + ) { const url = `${creds.altimateUrl}${endpoint}` // altimate_change start — upstream_fix: bound every API request. Without a // signal a stalled server holds the caller indefinitely. The abort stays @@ -327,7 +333,7 @@ export namespace AltimateApi { const timeout = setTimeout(() => controller.abort(), 15_000) try { const res = await fetch(url, { - signal: controller.signal, + signal: signal ? AbortSignal.any([controller.signal, signal]) : controller.signal, method, headers: { "Content-Type": "application/json", @@ -335,7 +341,8 @@ export namespace AltimateApi { "x-tenant": creds.altimateInstanceName, ...clientHeaders(), }, - ...(body ? { body: JSON.stringify(body) } : {}), + // Any body but undefined is sent: null, false, 0 and "" are JSON values too. + ...(body !== undefined ? { body: JSON.stringify(body) } : {}), }) if (!res.ok) { throw new Error(`API ${method} ${endpoint} failed with status ${res.status}`) @@ -350,9 +357,15 @@ export namespace AltimateApi { /** * 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. + * message naming the credentials file when none are stored or they can't be read. `signal` (a tool + * call's `ctx.abort`) cancels the request as well as the 15 s timeout. */ - export async function apiRequest(method: string, endpoint: string, body?: unknown): Promise { + export async function apiRequest( + method: string, + endpoint: string, + body?: unknown, + signal?: AbortSignal, + ): Promise { let creds: AltimateCredentials try { creds = await getCredentials() @@ -364,7 +377,7 @@ export namespace AltimateApi { ) 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) + return request(creds, method, endpoint, body, signal) } export async function listDatamates(creds?: AltimateCredentials) { diff --git a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts index d73dd7e9c5..1ff9047e9a 100644 --- a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts +++ b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts @@ -25,6 +25,8 @@ export const dbtAdapterAccessInternals = { */ export async function dbtAdapterFor(ctx: Pick): Promise { const signal = ctx.abort + // A cancelled call answers nothing, refused or not. + signal.throwIfAborted() if (ctx.extra?.forceReadOnly === true || ctx.agent === "plan") return { refusal: "read_only", diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index b2a012ed58..75a2fed64d 100644 --- a/packages/opencode/test/altimate/dbt-adapter-access.test.ts +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -125,6 +125,18 @@ describe("dbtAdapterFor", () => { expect(created).toBe(1) }) + test("returns the adapter to a session inside the adapter's own project", async () => { + try { + const access = await Instance.provide({ directory: project, fn: () => dbtAdapterFor(ctx) }) + expect(access.refusal).toBeUndefined() + expect(access.reason).toBeUndefined() + expect(access.adapter?.isInitialized()).toBe(true) + expect(created).toBe(1) + } finally { + await disposeInstance(project) + } + }) + test("says when the adapter didn't load the project", async () => { dbtAdapterInternals.create = async () => ({ isInitialized: () => false, dispose: async () => {} }) const access = await dbtAdapterFor(ctx) @@ -180,6 +192,15 @@ describe("dbtAdapterFor", () => { expect(created).toBe(0) }) + test("a cancelled read-only or plan-mode call throws rather than being refused", async () => { + const controller = new AbortController() + controller.abort() + const abort = controller.signal + await expect(dbtAdapterFor({ ...ctx, abort, extra: { forceReadOnly: true } })).rejects.toThrow() + await expect(dbtAdapterFor({ ...ctx, abort, agent: "plan" })).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 { @@ -260,6 +281,48 @@ describe("AltimateApi.apiRequest", () => { } }) + test("sends a falsy JSON body rather than dropping it", 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 body of [null, false, 0, ""]) await AltimateApi.apiRequest("POST", "/dbt/v1/x", body) + await AltimateApi.apiRequest("GET", "/dbt/v1/x") + expect(bodies).toEqual(["null", "false", "0", '""', undefined]) + } finally { + globalThis.fetch = original + } + }) + + test("a cancelled call stops the request", 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 + globalThis.fetch = ((_url: string, init: RequestInit) => + new Promise((_, reject) => { + // Like fetch: a signal already aborted rejects at once, else the abort does. + if (init.signal?.aborted) return reject(init.signal.reason) + init.signal?.addEventListener("abort", () => reject(init.signal?.reason), { once: true }) + })) as unknown as typeof fetch + const controller = new AbortController() + try { + const pending = AltimateApi.apiRequest("GET", "/dbt/v1/x", undefined, controller.signal) + controller.abort(new Error("cancelled by the user")) + await expect(pending).rejects.toThrow("cancelled by the user") + } 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( From 2d3c5fb06473d88a5cf7ef24cf116ce1f9c8c65d Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 06:24:05 +0530 Subject: [PATCH 13/28] fix: the cost tool description names the MCP server as a way to grant write Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/tools/dbt-cost-estimate.ts | 2 +- packages/opencode/test/altimate/dbt-cost-estimate.test.ts | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index a3ccc67e99..8bbb561767 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -55,7 +55,7 @@ function failure(title: string, message: string, extra: Record export const DbtCostEstimateTool = Tool.define("dbt_cost_estimate", { description: - "Estimate the bytes a dbt model or a SQL query would scan, from a warehouse dry run. Only BigQuery has a dry run: any other warehouse answers `supported: false` and nothing is compiled. Starts the dbt adapter, so it needs write permission (`--allow-write` on `tool run`). Pass `model` (a model name, compiled as dbt would) or `sql` (may use Jinja).", + "Estimate the bytes a dbt model or a SQL query would scan, from a warehouse dry run. Only BigQuery has a dry run: any other warehouse answers `supported: false` and nothing is compiled. Starts the dbt adapter, so it needs write permission (`--allow-write` on `tool run` or the MCP server). Pass `model` (a model name, compiled as dbt would) or `sql` (may use Jinja).", 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"), diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index 5ce8e8b76d..f74a9da1ac 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -192,6 +192,11 @@ describe("dbt_cost_estimate", () => { expect(result.output).toContain("--allow-write") }) + test("the tool description names the MCP server as a way to grant write", async () => { + const tool = await initTool(DbtCostEstimateTool) + expect(tool.description).toContain("`--allow-write` on `tool run` or the MCP server") + }) + test("rejects a call that passes both a model and SQL, estimating neither", async () => { useAdapter(calls) const result = await (await initTool(DbtCostEstimateTool)).execute({ model: "orders", sql: "select 1" }, ctx) From 18a3fc4e0bd124e8f5fb3120fdc4d152be88cbe3 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 06:39:46 +0530 Subject: [PATCH 14/28] fix: keep the stored API key out of API errors, bound the dbt config read - apiRequest names what is wrong with the credentials file (not valid JSON, which field is missing) instead of quoting the parser's message, which can quote the key. - request() redacts the key from a failed send: Bun's header check quotes `Bearer ` for a key with CR/LF, and throws before fetch returns a promise, so both a throw and a rejection are caught. - A request that times out says so; a cancelled apiRequest reads nothing. - dbtAdapterFor bounds the project lookup with the same deadline and abort as the adapter's start, and checks the abort after each wait. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/api/client.ts | 75 ++++++-- .../src/altimate/tools/dbt-adapter-access.ts | 52 ++--- .../test/altimate/dbt-adapter-access.test.ts | 179 +++++++++++++++++- 3 files changed, 265 insertions(+), 41 deletions(-) diff --git a/packages/opencode/src/altimate/api/client.ts b/packages/opencode/src/altimate/api/client.ts index 6ea7d96354..d3cfd1a887 100644 --- a/packages/opencode/src/altimate/api/client.ts +++ b/packages/opencode/src/altimate/api/client.ts @@ -318,6 +318,17 @@ export namespace AltimateApi { return data.auth_token } + /** Test seams. */ + export const requestInternals = { + /** How long an API request may take, body included, before it is aborted. */ + timeoutMs: 15_000, + } + + /** `text` with every occurrence of `secret` replaced, so an error can be shown without the key. */ + function redact(text: string, secret: string): string { + return secret ? text.split(secret).join("[redacted]") : text + } + async function request( creds: AltimateCredentials, method: string, @@ -326,24 +337,43 @@ export namespace AltimateApi { signal?: AbortSignal, ) { const url = `${creds.altimateUrl}${endpoint}` + // Bun's fetch throws at once, not as a rejection, when it refuses a header: both are caught. + const send = async (start: () => Promise) => { + try { + return await start() + } catch (e) { + // A timeout or the caller's abort rejects with its own reason, which holds no header. + if (controller.signal.aborted || signal?.aborted) throw e + // Others can quote a header: Bun's check of a header value with CR/LF quotes `Bearer `. + const message = redact(e instanceof Error ? e.message : String(e), creds.altimateApiKey) + throw new Error(`API ${method} ${endpoint} could not be sent: ${message}`) + } + } // altimate_change start — upstream_fix: bound every API request. Without a // signal a stalled server holds the caller indefinitely. The abort stays // armed until the BODY is read: `fetch` resolves on headers. const controller = new AbortController() - const timeout = setTimeout(() => controller.abort(), 15_000) + const seconds = requestInternals.timeoutMs / 1000 + const timeout = setTimeout( + () => + controller.abort(new DOMException(`API ${method} ${endpoint} timed out after ${seconds} s`, "TimeoutError")), + requestInternals.timeoutMs, + ) try { - const res = await fetch(url, { - signal: signal ? AbortSignal.any([controller.signal, signal]) : controller.signal, - method, - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${creds.altimateApiKey}`, - "x-tenant": creds.altimateInstanceName, - ...clientHeaders(), - }, - // Any body but undefined is sent: null, false, 0 and "" are JSON values too. - ...(body !== undefined ? { body: JSON.stringify(body) } : {}), - }) + const res = await send(() => + fetch(url, { + signal: signal ? AbortSignal.any([controller.signal, signal]) : controller.signal, + method, + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${creds.altimateApiKey}`, + "x-tenant": creds.altimateInstanceName, + ...clientHeaders(), + }, + // Any body but undefined is sent: null, false, 0 and "" are JSON values too. + ...(body !== undefined ? { body: JSON.stringify(body) } : {}), + }), + ) if (!res.ok) { throw new Error(`API ${method} ${endpoint} failed with status ${res.status}`) } @@ -354,11 +384,25 @@ export namespace AltimateApi { // altimate_change end } + /** What is wrong with the stored credentials, naming fields but never quoting the file's text. */ + function credentialsProblem(e: unknown): string { + // The parser's message quotes the text it stopped at, which can be the key. + if (e instanceof SyntaxError) return "the file is not valid JSON." + if (e instanceof z.ZodError) { + const fields = [...new Set(e.issues.map((issue) => issue.path.join(".")))] + if (fields.length === 0 || fields.includes("")) return "the file does not hold the credentials object." + return `${fields.join(", ")} ${fields.length === 1 ? "is" : "are"} missing or not valid.` + } + // A file system error or a missing ${env:NAME} variable: names a path or a variable, no value. + return e instanceof Error ? e.message : String(e) + } + /** * 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. `signal` (a tool - * call's `ctx.abort`) cancels the request as well as the 15 s timeout. + * call's `ctx.abort`) cancels the request as well as the 15 s timeout. No error it raises quotes the + * stored key. */ export async function apiRequest( method: string, @@ -366,6 +410,7 @@ export namespace AltimateApi { body?: unknown, signal?: AbortSignal, ): Promise { + signal?.throwIfAborted() let creds: AltimateCredentials try { creds = await getCredentials() @@ -375,7 +420,7 @@ export namespace AltimateApi { 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)}`) + throw new Error(`The Altimate credentials in ${p} can't be used: ${credentialsProblem(e)}`) } return request(creds, method, endpoint, body, signal) } diff --git a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts index 1ff9047e9a..df8fb597dc 100644 --- a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts +++ b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts @@ -21,7 +21,8 @@ export const dbtAdapterAccessInternals = { * 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. + * adapter isn't set up for. Every wait, the project lookup and the adapter's start, is bounded by one + * deadline and follows the call's abort. */ export async function dbtAdapterFor(ctx: Pick): Promise { const signal = ctx.abort @@ -32,12 +33,30 @@ export async function dbtAdapterFor(ctx: Pick {} + 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, + ) + }) + // It can reject between two races, when nothing awaits it: the next race still sees the rejection. + waited.catch(() => {}) + const bounded = async (work: Promise): Promise => { + const value = await Promise.race([work, waited]) + // An abort queued while the work settled still wins. signal.throwIfAborted() + return value + } + try { + const { getDbtAdapter, getDbtProjectRoot } = await bounded(import("../native/connections/register")) + const project = await bounded(sessionProject()) if (project?.other) { return { refusal: "other_project", @@ -46,24 +65,10 @@ export async function dbtAdapterFor(ctx: Pick {} - 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) - }) + const adapter = await bounded(getDbtAdapter()) if (!adapter) { // No adapter also when the project exists but the adapter failed to start: say which. - const root = await getDbtProjectRoot() + const root = await bounded(getDbtProjectRoot()) const found = !!root && fs.existsSync(path.join(root, "dbt_project.yml")) return found ? { refusal: "not_started", reason: "the dbt adapter didn't start" } @@ -75,6 +80,9 @@ export async function dbtAdapterFor(ctx: Pick { } }) + test("a stalled dbt config read gives up at the start timeout", async () => { + let release = () => {} + dbtAdapterInternals.readConfig = () => new Promise((resolve) => (release = () => resolve(null))) + const saved = dbtAdapterAccessInternals.adapterStartTimeoutMs + dbtAdapterAccessInternals.adapterStartTimeoutMs = 20 + try { + const access = await Instance.provide({ directory: project, fn: () => dbtAdapterFor(ctx) }) + expect(access).toEqual({ refusal: "not_started", reason: "the dbt adapter didn't start within 0.02 s" }) + expect(created).toBe(0) + } finally { + release() + dbtAdapterAccessInternals.adapterStartTimeoutMs = saved + await disposeInstance(project) + } + }) + + test("a call cancelled during a stalled dbt config read throws at once", async () => { + let release = () => {} + let entered = () => {} + const reading = new Promise((resolve) => (entered = resolve)) + dbtAdapterInternals.readConfig = () => { + entered() + return new Promise((resolve) => (release = () => resolve(null))) + } + const controller = new AbortController() + try { + const pending = Instance.provide({ + directory: project, + fn: () => dbtAdapterFor({ ...ctx, abort: controller.signal }), + }) + await reading + controller.abort(new Error("cancelled")) + await expect(pending).rejects.toThrow("cancelled") + } finally { + release() + await disposeInstance(project) + } + }) + + test("removes its abort listener and its timer whatever the outcome", async () => { + const controller = new AbortController() + const signal = controller.signal + let listening = 0 + const add = signal.addEventListener.bind(signal) + const remove = signal.removeEventListener.bind(signal) + signal.addEventListener = ((...a: Parameters) => { + if (a[0] === "abort") listening += 1 + return add(...a) + }) as typeof signal.addEventListener + signal.removeEventListener = ((...a: Parameters) => { + if (a[0] === "abort") listening -= 1 + return remove(...a) + }) as typeof signal.removeEventListener + const timers = new Set() + const set = globalThis.setTimeout + const clear = globalThis.clearTimeout + globalThis.setTimeout = ((fn: () => void, ms?: number) => { + const t = set(fn, ms) + timers.add(t) + return t + }) as typeof setTimeout + globalThis.clearTimeout = ((t: ReturnType) => { + timers.delete(t) + return clear(t) + }) as typeof clearTimeout + try { + expect((await dbtAdapterFor({ ...ctx, abort: signal })).adapter).toBeDefined() + dbtAdapterInternals.create = async () => { + throw new Error("dbt-integration missing") + } + resetDbtAdapter() + expect((await dbtAdapterFor({ ...ctx, abort: signal })).refusal).toBe("not_started") + } finally { + globalThis.setTimeout = set + globalThis.clearTimeout = clear + } + expect(listening).toBe(0) + expect(timers.size).toBe(0) + }) + 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") @@ -323,10 +403,101 @@ describe("AltimateApi.apiRequest", () => { } }) - 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`, + test("names the file and the failure, never the file's text, when the credentials aren't valid JSON", async () => { + const file = path.join(home, ".altimate", "altimate.json") + await Bun.write( + file, + '{"altimateUrl":"https://x.test","altimateInstanceName":"acme","altimateApiKey": sk_SECRET123}', + ) + const error = await AltimateApi.apiRequest("GET", "/dbt/v1/x").catch((e: Error) => e) + expect(error).toBeInstanceOf(Error) + expect(error.message).toBe(`The Altimate credentials in ${file} can't be used: the file is not valid JSON.`) + expect(error.message).not.toContain("sk_SECRET123") + }) + + test("names the missing field, never a value, when the credentials are incomplete", async () => { + const file = path.join(home, ".altimate", "altimate.json") + await Bun.write(file, JSON.stringify({ altimateUrl: "https://x.test", altimateApiKey: "sk_SECRET123" })) + const error = await AltimateApi.apiRequest("GET", "/dbt/v1/x").catch((e: Error) => e) + expect(error.message).toBe( + `The Altimate credentials in ${file} can't be used: altimateInstanceName is missing or not valid.`, ) + expect(error.message).not.toContain("sk_SECRET123") + }) + + test("never shows the stored key when sending the request fails", async () => { + const key = "sk_SECRET123\r\nX: y" + await Bun.write( + path.join(home, ".altimate", "altimate.json"), + JSON.stringify({ altimateUrl: "http://127.0.0.1:9", altimateInstanceName: "acme", altimateApiKey: key }), + ) + // The real fetch: it rejects a header value with CR/LF, quoting the value. + const error = await AltimateApi.apiRequest("GET", "/dbt/v1/x").catch((e: Error) => e) + expect(error).toBeInstanceOf(Error) + expect(error.message).not.toContain("sk_SECRET123") + expect(error.message).toContain("API GET /dbt/v1/x could not be sent") + }) + + test("never shows the stored key from any fetch failure", async () => { + await Bun.write( + path.join(home, ".altimate", "altimate.json"), + JSON.stringify({ + altimateUrl: "https://api.example.test", + altimateInstanceName: "acme", + altimateApiKey: "sk_SECRET123", + }), + ) + const original = globalThis.fetch + globalThis.fetch = (async (_url: string, init: RequestInit) => { + throw new TypeError(`bad header: '${(init.headers as Record).Authorization}'`) + }) as unknown as typeof fetch + try { + const error = await AltimateApi.apiRequest("GET", "/dbt/v1/x").catch((e: Error) => e) + expect(error.message).toBe("API GET /dbt/v1/x could not be sent: bad header: 'Bearer [redacted]'") + } finally { + globalThis.fetch = original + } + }) + + test("a timed-out request says it timed out", 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 + globalThis.fetch = ((_url: string, init: RequestInit) => + new Promise((_, reject) => { + init.signal?.addEventListener("abort", () => reject(init.signal?.reason), { once: true }) + })) as unknown as typeof fetch + const saved = AltimateApi.requestInternals.timeoutMs + AltimateApi.requestInternals.timeoutMs = 10 + try { + await expect(AltimateApi.apiRequest("GET", "/dbt/v1/x")).rejects.toThrow( + "API GET /dbt/v1/x timed out after 0.01 s", + ) + } finally { + AltimateApi.requestInternals.timeoutMs = saved + globalThis.fetch = original + } + }) + + test("a call cancelled before it starts reads no credentials and sends nothing", async () => { + const original = globalThis.fetch + let sent = 0 + globalThis.fetch = (async () => { + sent += 1 + return new Response("{}") + }) as unknown as typeof fetch + const controller = new AbortController() + controller.abort(new Error("cancelled by the user")) + try { + // No credentials are stored: reading them would fail with another message. + await expect(AltimateApi.apiRequest("GET", "/dbt/v1/x", undefined, controller.signal)).rejects.toThrow( + "cancelled by the user", + ) + expect(sent).toBe(0) + } finally { + globalThis.fetch = original + } }) }) From 6024ac9b208400ff2c70f8ef07e98b1568163276 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 06:47:20 +0530 Subject: [PATCH 15/28] fix: redact the API key when a refused header races the caller's abort; assert stub failure classification Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/api/client.ts | 5 ++-- .../test/altimate/dbt-adapter-access.test.ts | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/altimate/api/client.ts b/packages/opencode/src/altimate/api/client.ts index d3cfd1a887..25a78d8bd0 100644 --- a/packages/opencode/src/altimate/api/client.ts +++ b/packages/opencode/src/altimate/api/client.ts @@ -342,8 +342,9 @@ export namespace AltimateApi { try { return await start() } catch (e) { - // A timeout or the caller's abort rejects with its own reason, which holds no header. - if (controller.signal.aborted || signal?.aborted) throw e + // A timeout or the caller's abort rejects with its own reason, which holds no header. An abort + // that raced a refused header leaves the header error, so match the reason, not the flag. + if (e === controller.signal.reason || e === signal?.reason) throw e // Others can quote a header: Bun's check of a header value with CR/LF quotes `Bearer `. const message = redact(e instanceof Error ? e.message : String(e), creds.altimateApiKey) throw new Error(`API ${method} ${endpoint} could not be sent: ${message}`) diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index 53592c8bc6..f998a83334 100644 --- a/packages/opencode/test/altimate/dbt-adapter-access.test.ts +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -11,6 +11,7 @@ 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 { resultFailed } from "@/altimate/mcp-server/adapter" 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" @@ -89,6 +90,7 @@ describe("feature tools in the allow-list", () => { expect(result.title).toBe(`${id}: not implemented`) expect(result.metadata).toEqual({ success: false, error: "not implemented", truncated: false }) expect(result.output).toContain(id) + expect(resultFailed(result)).toBe(true) } }) }) @@ -438,6 +440,31 @@ describe("AltimateApi.apiRequest", () => { expect(error.message).toContain("API GET /dbt/v1/x could not be sent") }) + test("never shows the stored key when a header refusal races the caller's abort", async () => { + await Bun.write( + path.join(home, ".altimate", "altimate.json"), + JSON.stringify({ + altimateUrl: "https://api.example.test", + altimateInstanceName: "acme", + altimateApiKey: "sk_SECRET123", + }), + ) + const original = globalThis.fetch + const controller = new AbortController() + globalThis.fetch = (async (_url: string, init: RequestInit) => { + controller.abort(new Error("cancelled by the user")) + throw new TypeError(`bad header: '${(init.headers as Record).Authorization}'`) + }) as unknown as typeof fetch + try { + const error = await AltimateApi.apiRequest("GET", "/dbt/v1/x", undefined, controller.signal).catch( + (e: Error) => e, + ) + expect(error.message).toBe("API GET /dbt/v1/x could not be sent: bad header: 'Bearer [redacted]'") + } finally { + globalThis.fetch = original + } + }) + test("never shows the stored key from any fetch failure", async () => { await Bun.write( path.join(home, ".altimate", "altimate.json"), From f5159c51d1fcc7b8024a6d475ecc39106bbe8371 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 06:47:45 +0530 Subject: [PATCH 16/28] docs: say which tools use the dbt adapter helper Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/tools/dbt-adapter-access.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts index df8fb597dc..7f93d822e9 100644 --- a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts +++ b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts @@ -18,7 +18,7 @@ export const dbtAdapterAccessInternals = { } /** - * The dbt adapter for a tool call, which every adapter-backed tool goes through. Starting the adapter + * The dbt adapter for a tool call, which the adapter-backed tools added after dbt_lineage go 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. Every wait, the project lookup and the adapter's start, is bounded by one From f57de3da23aab9065822038372fbb2eb5e120299 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 06:52:44 +0530 Subject: [PATCH 17/28] docs: rewrap and correct the adapter helper comments Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/altimate/tools/dbt-adapter-access.ts | 10 +++++----- .../opencode/test/altimate/dbt-adapter-access.test.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts index 7f93d822e9..2fc2aba423 100644 --- a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts +++ b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts @@ -18,11 +18,11 @@ export const dbtAdapterAccessInternals = { } /** - * The dbt adapter for a tool call, which the adapter-backed tools added after dbt_lineage go 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. Every wait, the project lookup and the adapter's start, is bounded by one - * deadline and follows the call's abort. + * The dbt adapter for a tool call, which the adapter-backed tools added after dbt_lineage go 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. Every wait, the project lookup and the + * adapter's start, is bounded by one deadline and follows the call's abort. */ export async function dbtAdapterFor(ctx: Pick): Promise { const signal = ctx.abort diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index f998a83334..ddcf0ea183 100644 --- a/packages/opencode/test/altimate/dbt-adapter-access.test.ts +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -1,5 +1,5 @@ // 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 +// one gate the new dbt-adapter-backed tools go 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" From 6514705b0ae0db03bf63a06227fbe53eea3d6810 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 11:51:07 +0530 Subject: [PATCH 18/28] fix: escape the compiled SQL for the dbt integrations that write it into a Jinja argument Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-cost-estimate.ts | 16 +++++++++++++++- .../test/altimate/dbt-cost-estimate.test.ts | 13 +++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index 8bbb561767..f57503285a 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -2,6 +2,15 @@ import z from "zod" import { Tool } from "../../tool/tool" import { dbtAdapterFor } from "./dbt-adapter-access" +/** + * Every dbt integration but the Python bridge writes the SQL into a `{{ validate_sql('...') }}` Jinja + * argument, so a quote or backslash in it must be escaped or any filter on a string breaks the dry run. + * The bridge binds the SQL as a parameter and takes it as is. + */ +export function forDryRun(sql: string, integration: unknown): string { + return integration === "core" ? sql : sql.replace(/\\/g, "\\\\").replace(/'/g, "\\'") +} + /** The units dbt-bigquery's `format_bytes` writes, smallest first; each is 1024 times the one before. */ const UNITS = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"] as const @@ -93,7 +102,12 @@ export const DbtCostEstimateTool = Tool.define("dbt_cost_estimate", { } let display: unknown try { - const result = await untilAbort(() => adapter.validateSQLDryRun(compiled), signal) + const integration = await untilAbort(async () => { + const { dbtAdapterInternals } = await import("../native/connections/register") + const read = dbtAdapterInternals.readConfig ?? (await import("../../../../dbt-tools/src/config")).read + return ((await read()) as { dbtIntegration?: unknown } | null)?.dbtIntegration + }, signal) + const result = await untilAbort(() => adapter.validateSQLDryRun(forDryRun(compiled as string, integration)), signal) if (result == null) { return failure("dry run failed", "The dry run returned no estimate: dbt-core before 1.6 can't dry run.", { adapter: type }) } diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index f74a9da1ac..be2ce5d003 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -274,4 +274,17 @@ describe("dbt_cost_estimate", () => { expect(result.metadata.bytes).toBeUndefined() expect(result.output).toContain('"lots"') }) + + test("escapes quotes and backslashes for the integrations that write the SQL into a Jinja argument", async () => { + useAdapter(calls) + await (await initTool(DbtCostEstimateTool)).execute({ sql: "where s = 'a\\b'" }, ctx) + expect(calls.dryRuns).toEqual(["compiled(where s = \\'a\\\\b\\')"]) + }) + + test("hands the SQL to the Python bridge as is", async () => { + useAdapter(calls) + dbtAdapterInternals.readConfig = async () => ({ projectRoot: project, pythonPath: "python3", dbtIntegration: "core", queryLimit: 500 }) + await (await initTool(DbtCostEstimateTool)).execute({ sql: "where s = 'a'" }, ctx) + expect(calls.dryRuns).toEqual(["compiled(where s = 'a')"]) + }) }) From d7938df7c5eaebe8cc460dfdd1220e9f00090d21 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 11:57:53 +0530 Subject: [PATCH 19/28] fix: a config that names no integration is the Python bridge Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/tools/dbt-cost-estimate.ts | 4 ++-- packages/opencode/test/altimate/dbt-cost-estimate.test.ts | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index f57503285a..cf05aa7f24 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -5,10 +5,10 @@ import { dbtAdapterFor } from "./dbt-adapter-access" /** * Every dbt integration but the Python bridge writes the SQL into a `{{ validate_sql('...') }}` Jinja * argument, so a quote or backslash in it must be escaped or any filter on a string breaks the dry run. - * The bridge binds the SQL as a parameter and takes it as is. + * The bridge, which a config without an integration also means, binds the SQL as a parameter and takes it as is. */ export function forDryRun(sql: string, integration: unknown): string { - return integration === "core" ? sql : sql.replace(/\\/g, "\\\\").replace(/'/g, "\\'") + return integration == null || integration === "core" ? sql : sql.replace(/\\/g, "\\\\").replace(/'/g, "\\'") } /** The units dbt-bigquery's `format_bytes` writes, smallest first; each is 1024 times the one before. */ diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index be2ce5d003..95b8b21a9b 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -287,4 +287,11 @@ describe("dbt_cost_estimate", () => { await (await initTool(DbtCostEstimateTool)).execute({ sql: "where s = 'a'" }, ctx) expect(calls.dryRuns).toEqual(["compiled(where s = 'a')"]) }) + + test("a config that names no integration is the Python bridge: the SQL goes as is", async () => { + useAdapter(calls) + dbtAdapterInternals.readConfig = async () => ({ projectRoot: project, pythonPath: "python3", queryLimit: 500 }) + await (await initTool(DbtCostEstimateTool)).execute({ sql: "where s = 'a'" }, ctx) + expect(calls.dryRuns).toEqual(["compiled(where s = 'a')"]) + }) }) From aaff4148345d9e6735a119d27ea540a3d2929a9d Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 13:21:13 +0530 Subject: [PATCH 20/28] fix: never echo a send failure's text, so a trimmed key can't leak Bun trims a header value before quoting it in its validation error, so redacting exact copies of the stored key missed keys with a line break and trailing whitespace. A send failure now reports only a fixed reason for a refused header, or the error's name and code otherwise. Also: a missing ${env:...} variable no longer quotes its name from the credentials file; a credentials directory that can't be searched is reported as unreadable, not as no credentials; the adapter deadline says "wasn't ready" since it covers the config read too. Regression tests go through the real fetch for CR/LF and trailing-whitespace keys, via apiRequest and listDatamates, and check stubs still validate input. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/api/client.ts | 40 ++++-- .../src/altimate/tools/dbt-adapter-access.ts | 2 +- .../test/altimate/dbt-adapter-access.test.ts | 121 ++++++++++++++---- 3 files changed, 126 insertions(+), 37 deletions(-) diff --git a/packages/opencode/src/altimate/api/client.ts b/packages/opencode/src/altimate/api/client.ts index 25a78d8bd0..a7a668183e 100644 --- a/packages/opencode/src/altimate/api/client.ts +++ b/packages/opencode/src/altimate/api/client.ts @@ -1,6 +1,6 @@ import z from "zod" import path from "path" -import { rm } from "node:fs/promises" +import { rm, stat } from "node:fs/promises" import { Global } from "../../global" import { Filesystem } from "../../util/filesystem" import { currentCredentials } from "./credentials-context" @@ -86,11 +86,14 @@ export namespace AltimateApi { return Filesystem.exists(credentialsPath()) } + /** A `${env:NAME}` in the credentials file names a variable that isn't set. */ + class MissingEnvVar extends Error {} + function resolveEnvVars(obj: unknown): unknown { if (typeof obj === "string") { return obj.replace(/\$\{env:([^}]+)\}/g, (_, envVar) => { const value = process.env[envVar] - if (value === undefined) throw new Error(`Environment variable ${envVar} not found`) + if (value === undefined) throw new MissingEnvVar(`Environment variable ${envVar} not found`) return value }) } @@ -324,9 +327,18 @@ export namespace AltimateApi { timeoutMs: 15_000, } - /** `text` with every occurrence of `secret` replaced, so an error can be shown without the key. */ - function redact(text: string, secret: string): string { - return secret ? text.split(secret).join("[redacted]") : text + /** + * Why a request couldn't be sent, never quoting the error's text. Bun refuses a header value with a + * control character as a TypeError that quotes the value after trimming it, so no redaction of the + * stored key can be relied on: only the error's name and code are shown. + */ + function sendFailure(e: unknown): string { + if (e instanceof TypeError) + return "the request was refused before it was sent (TypeError): the stored API key or tenant name may not be a valid header value." + const word = (v: unknown) => (typeof v === "string" && /^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(v) ? v : undefined) + const name = (e instanceof Error && word(e.name)) || "Error" + const code = word((e as { code?: unknown } | null)?.code) + return code ? `${name} (${code}).` : `${name}.` } async function request( @@ -346,8 +358,7 @@ export namespace AltimateApi { // that raced a refused header leaves the header error, so match the reason, not the flag. if (e === controller.signal.reason || e === signal?.reason) throw e // Others can quote a header: Bun's check of a header value with CR/LF quotes `Bearer `. - const message = redact(e instanceof Error ? e.message : String(e), creds.altimateApiKey) - throw new Error(`API ${method} ${endpoint} could not be sent: ${message}`) + throw new Error(`API ${method} ${endpoint} could not be sent: ${sendFailure(e)}`) } } // altimate_change start — upstream_fix: bound every API request. Without a @@ -394,7 +405,9 @@ export namespace AltimateApi { if (fields.length === 0 || fields.includes("")) return "the file does not hold the credentials object." return `${fields.join(", ")} ${fields.length === 1 ? "is" : "are"} missing or not valid.` } - // A file system error or a missing ${env:NAME} variable: names a path or a variable, no value. + // The variable's name is the file's text, which can be a key written in its place. + if (e instanceof MissingEnvVar) return "an ${env:…} variable it refers to is not set." + // A file system error names a path, not a value. return e instanceof Error ? e.message : String(e) } @@ -403,7 +416,7 @@ export namespace AltimateApi { * 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. `signal` (a tool * call's `ctx.abort`) cancels the request as well as the 15 s timeout. No error it raises quotes the - * stored key. + * stored key or the credentials file's text. */ export async function apiRequest( method: string, @@ -417,7 +430,14 @@ export namespace AltimateApi { creds = await getCredentials() } catch (e) { const p = credentialsPath() - if (!(await Filesystem.exists(p))) + // Only a file that isn't there is "not stored": a directory without search permission hides it too. + const unseen = await stat(p).then( + () => undefined, + (err: NodeJS.ErrnoException) => err, + ) + if (unseen && unseen.code !== "ENOENT" && unseen.code !== "ENOTDIR") + throw new Error(`The Altimate credentials in ${p} can't be read (${unseen.code ?? unseen.name}).`) + if (unseen) throw new Error( `No Altimate credentials are stored: sign in to Altimate in altimate-code first (they are saved to ${p}).`, ) diff --git a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts index 2fc2aba423..d51d2b4d2b 100644 --- a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts +++ b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts @@ -42,7 +42,7 @@ export async function dbtAdapterFor(ctx: Pick reject(new Error(`the dbt adapter didn't start within ${seconds} s`)), + () => reject(new Error(`the dbt adapter wasn't ready within ${seconds} s`)), dbtAdapterAccessInternals.adapterStartTimeoutMs, ) }) diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index ddcf0ea183..6e3d7958c6 100644 --- a/packages/opencode/test/altimate/dbt-adapter-access.test.ts +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -2,7 +2,7 @@ // one gate the new dbt-adapter-backed tools go 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 { chmod, mkdtemp, rm, writeFile } from "node:fs/promises" import { realpathSync } from "node:fs" import os from "node:os" import path from "node:path" @@ -93,6 +93,25 @@ describe("feature tools in the allow-list", () => { expect(resultFailed(result)).toBe(true) } }) + + test("each placeholder still checks its arguments before it refuses", async () => { + for (const id of FEATURE_TOOLS) { + const tool = await initTool(EXPOSED[id] as any) + // A string where an object is expected fails every schema, whatever its required fields. + const failed = await Promise.resolve() + .then(() => tool.execute("not an object" as any, ctx)) + .then( + (result) => ({ result }), + (error: unknown) => ({ error }), + ) + if ("result" in failed) { + expect(failed.result.title).not.toBe(`${id}: not implemented`) + expect(resultFailed(failed.result)).toBe(true) + } else { + expect(String(failed.error)).toContain("invalid arguments") + } + } + }) }) describe("dbtAdapterFor", () => { @@ -157,7 +176,7 @@ describe("dbtAdapterFor", () => { 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" }) + expect(access).toEqual({ refusal: "not_started", reason: "the dbt adapter wasn't ready within 0.02 s" }) } finally { dbtAdapterAccessInternals.adapterStartTimeoutMs = saved } @@ -224,7 +243,7 @@ describe("dbtAdapterFor", () => { dbtAdapterAccessInternals.adapterStartTimeoutMs = 20 try { const access = await Instance.provide({ directory: project, fn: () => dbtAdapterFor(ctx) }) - expect(access).toEqual({ refusal: "not_started", reason: "the dbt adapter didn't start within 0.02 s" }) + expect(access).toEqual({ refusal: "not_started", reason: "the dbt adapter wasn't ready within 0.02 s" }) expect(created).toBe(0) } finally { release() @@ -427,17 +446,53 @@ describe("AltimateApi.apiRequest", () => { expect(error.message).not.toContain("sk_SECRET123") }) - test("never shows the stored key when sending the request fails", async () => { - const key = "sk_SECRET123\r\nX: y" + const HEADER_REFUSED = + "could not be sent: the request was refused before it was sent (TypeError): the stored API key or tenant name may not be a valid header value." + + /** Every substring of `key` long enough to identify it, so a trimmed or split copy is caught too. */ + function expectNoPartOf(text: string, key: string) { + for (const part of key.split(/\s+/).filter((p) => p.length >= 4)) expect(text).not.toContain(part) + expect(text).not.toContain("Bearer") + } + + // The real fetch: Bun refuses a header value with CR/LF, quoting it after trimming its whitespace. + for (const key of [ + "sk_SECRET123\r\nX: y", + "sk_SECRET123ABCDEFGH\nPART2\n", + "sk_SECRET123ABCDEFGH\r\nX \t", + " sk_SECRET123ABCDEFGH\nPART2 ", + ]) { + test(`never shows any part of a key that isn't a valid header value: ${JSON.stringify(key)}`, async () => { + await Bun.write( + path.join(home, ".altimate", "altimate.json"), + JSON.stringify({ altimateUrl: "http://127.0.0.1:9", altimateInstanceName: "acme", altimateApiKey: key }), + ) + const viaRequest = await AltimateApi.apiRequest("GET", "/dbt/v1/x").catch((e: Error) => e) + expect(viaRequest).toBeInstanceOf(Error) + expect(viaRequest.message).toBe(`API GET /dbt/v1/x ${HEADER_REFUSED}`) + expectNoPartOf(`${viaRequest.message}\n${viaRequest.stack}`, key) + const viaList = (await AltimateApi.listDatamates().catch((e: Error) => e)) as Error + expect(viaList).toBeInstanceOf(Error) + expect(viaList.message).toBe(`API GET /datamates/ ${HEADER_REFUSED}`) + expectNoPartOf(`${viaList.message}\n${viaList.stack}`, key) + }) + } + + test("names a network failure by its code, never by the error's text", async () => { await Bun.write( path.join(home, ".altimate", "altimate.json"), - JSON.stringify({ altimateUrl: "http://127.0.0.1:9", altimateInstanceName: "acme", altimateApiKey: key }), + JSON.stringify({ altimateUrl: "http://127.0.0.1:9", altimateInstanceName: "acme", altimateApiKey: "k1" }), ) - // The real fetch: it rejects a header value with CR/LF, quoting the value. - const error = await AltimateApi.apiRequest("GET", "/dbt/v1/x").catch((e: Error) => e) - expect(error).toBeInstanceOf(Error) - expect(error.message).not.toContain("sk_SECRET123") - expect(error.message).toContain("API GET /dbt/v1/x could not be sent") + const original = globalThis.fetch + globalThis.fetch = (async () => { + throw Object.assign(new Error("Unable to connect: Bearer k1"), { code: "ConnectionRefused" }) + }) as unknown as typeof fetch + try { + const error = await AltimateApi.apiRequest("GET", "/dbt/v1/x").catch((e: Error) => e) + expect(error.message).toBe("API GET /dbt/v1/x could not be sent: Error (ConnectionRefused).") + } finally { + globalThis.fetch = original + } }) test("never shows the stored key when a header refusal races the caller's abort", async () => { @@ -459,33 +514,47 @@ describe("AltimateApi.apiRequest", () => { const error = await AltimateApi.apiRequest("GET", "/dbt/v1/x", undefined, controller.signal).catch( (e: Error) => e, ) - expect(error.message).toBe("API GET /dbt/v1/x could not be sent: bad header: 'Bearer [redacted]'") + expect(error.message).toBe(`API GET /dbt/v1/x ${HEADER_REFUSED}`) } finally { globalThis.fetch = original } }) - test("never shows the stored key from any fetch failure", async () => { + test("never quotes the credentials file when an ${env:…} variable it names isn't set", async () => { + const file = path.join(home, ".altimate", "altimate.json") await Bun.write( - path.join(home, ".altimate", "altimate.json"), + file, JSON.stringify({ - altimateUrl: "https://api.example.test", + altimateUrl: "https://x.test", altimateInstanceName: "acme", - altimateApiKey: "sk_SECRET123", + altimateApiKey: "${env:sk_SECRET123_UNSET}", }), ) - const original = globalThis.fetch - globalThis.fetch = (async (_url: string, init: RequestInit) => { - throw new TypeError(`bad header: '${(init.headers as Record).Authorization}'`) - }) as unknown as typeof fetch - try { - const error = await AltimateApi.apiRequest("GET", "/dbt/v1/x").catch((e: Error) => e) - expect(error.message).toBe("API GET /dbt/v1/x could not be sent: bad header: 'Bearer [redacted]'") - } finally { - globalThis.fetch = original - } + const error = await AltimateApi.apiRequest("GET", "/dbt/v1/x").catch((e: Error) => e) + expect(error.message).toBe( + `The Altimate credentials in ${file} can't be used: an \${env:…} variable it refers to is not set.`, + ) }) + test.skipIf(process.platform === "win32" || process.getuid?.() === 0)( + "says the credentials can't be read, not that none are stored, when their directory can't be searched", + async () => { + const dir = path.join(home, ".altimate") + const file = path.join(dir, "altimate.json") + await Bun.write( + file, + JSON.stringify({ altimateUrl: "https://x.test", altimateInstanceName: "a", altimateApiKey: "k" }), + ) + await chmod(dir, 0o600) + try { + const error = await AltimateApi.apiRequest("GET", "/dbt/v1/x").catch((e: Error) => e) + expect(error.message).toBe(`The Altimate credentials in ${file} can't be read (EACCES).`) + } finally { + await chmod(dir, 0o700) + } + }, + ) + test("a timed-out request says it timed out", async () => { await Bun.write( path.join(home, ".altimate", "altimate.json"), From 3a98dff47e9a501dae418bbe16098ee2fe742550 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 13:29:38 +0530 Subject: [PATCH 21/28] fix: a rejection with no reason is a send failure, not an abort Before either signal aborts, its reason is undefined, so a fetch that rejected with undefined matched it and was rethrown raw. Match a reason only once its signal has aborted. The cleanup test now also covers a timed-out and a cancelled call. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/api/client.ts | 4 +- .../test/altimate/dbt-adapter-access.test.ts | 79 +++++++++++++++---- 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/packages/opencode/src/altimate/api/client.ts b/packages/opencode/src/altimate/api/client.ts index a7a668183e..123630d58b 100644 --- a/packages/opencode/src/altimate/api/client.ts +++ b/packages/opencode/src/altimate/api/client.ts @@ -356,7 +356,9 @@ export namespace AltimateApi { } catch (e) { // A timeout or the caller's abort rejects with its own reason, which holds no header. An abort // that raced a refused header leaves the header error, so match the reason, not the flag. - if (e === controller.signal.reason || e === signal?.reason) throw e + // A signal that isn't aborted has no reason yet: a rejection with none is a send failure. + if ((controller.signal.aborted && e === controller.signal.reason) || (signal?.aborted && e === signal.reason)) + throw e // Others can quote a header: Bun's check of a header value with CR/LF quotes `Bearer `. throw new Error(`API ${method} ${endpoint} could not be sent: ${sendFailure(e)}`) } diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index 6e3d7958c6..d3a587135e 100644 --- a/packages/opencode/test/altimate/dbt-adapter-access.test.ts +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -276,19 +276,26 @@ describe("dbtAdapterFor", () => { }) test("removes its abort listener and its timer whatever the outcome", async () => { - const controller = new AbortController() - const signal = controller.signal - let listening = 0 - const add = signal.addEventListener.bind(signal) - const remove = signal.removeEventListener.bind(signal) - signal.addEventListener = ((...a: Parameters) => { - if (a[0] === "abort") listening += 1 - return add(...a) - }) as typeof signal.addEventListener - signal.removeEventListener = ((...a: Parameters) => { - if (a[0] === "abort") listening -= 1 - return remove(...a) - }) as typeof signal.removeEventListener + /** A signal that counts the abort listeners still attached to it. */ + const tracked = () => { + const controller = new AbortController() + const signal = controller.signal + const state = { controller, signal, listening: 0 } + const add = signal.addEventListener.bind(signal) + const remove = signal.removeEventListener.bind(signal) + signal.addEventListener = ((...a: Parameters) => { + if (a[0] === "abort") state.listening += 1 + return add(...a) + }) as typeof signal.addEventListener + signal.removeEventListener = ((...a: Parameters) => { + if (a[0] === "abort") state.listening -= 1 + return remove(...a) + }) as typeof signal.removeEventListener + return state + } + const settled = tracked() + const cancelled = tracked() + const signal = settled.signal const timers = new Set() const set = globalThis.setTimeout const clear = globalThis.clearTimeout @@ -308,11 +315,37 @@ describe("dbtAdapterFor", () => { } resetDbtAdapter() expect((await dbtAdapterFor({ ...ctx, abort: signal })).refusal).toBe("not_started") + // Timed out. + dbtAdapterInternals.create = () => new Promise(() => {}) + resetDbtAdapter() + const saved = dbtAdapterAccessInternals.adapterStartTimeoutMs + dbtAdapterAccessInternals.adapterStartTimeoutMs = 20 + try { + expect(await dbtAdapterFor({ ...ctx, abort: signal })).toEqual({ + refusal: "not_started", + reason: "the dbt adapter wasn't ready within 0.02 s", + }) + } finally { + dbtAdapterAccessInternals.adapterStartTimeoutMs = saved + } + // Cancelled while waiting. + let entered = () => {} + const starting = new Promise((resolve) => (entered = resolve)) + dbtAdapterInternals.create = () => { + entered() + return new Promise(() => {}) + } + resetDbtAdapter() + const pending = dbtAdapterFor({ ...ctx, abort: cancelled.signal }) + await starting + cancelled.controller.abort(new Error("cancelled")) + await expect(pending).rejects.toThrow("cancelled") } finally { globalThis.setTimeout = set globalThis.clearTimeout = clear } - expect(listening).toBe(0) + expect(settled.listening).toBe(0) + expect(cancelled.listening).toBe(0) expect(timers.size).toBe(0) }) @@ -478,6 +511,24 @@ describe("AltimateApi.apiRequest", () => { }) } + test("a fetch that rejects with no reason is a send failure, not taken for an abort", async () => { + await Bun.write( + path.join(home, ".altimate", "altimate.json"), + JSON.stringify({ altimateUrl: "http://127.0.0.1:9", altimateInstanceName: "acme", altimateApiKey: "k1" }), + ) + const original = globalThis.fetch + globalThis.fetch = (() => Promise.reject(undefined)) as unknown as typeof fetch + try { + const error = await AltimateApi.apiRequest("GET", "/dbt/v1/x", undefined, new AbortController().signal).catch( + (e: unknown) => e, + ) + expect(error).toBeInstanceOf(Error) + expect((error as Error).message).toBe("API GET /dbt/v1/x could not be sent: Error.") + } finally { + globalThis.fetch = original + } + }) + test("names a network failure by its code, never by the error's text", async () => { await Bun.write( path.join(home, ".altimate", "altimate.json"), From ac43c53b65b9c6e2575a62679757e15ea9a2cd18 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 13:49:40 +0530 Subject: [PATCH 22/28] docs: the cost estimate tool says compiling can run billed queries and the adapter start runs dbt deps Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/tools/dbt-cost-estimate.ts | 2 +- packages/opencode/test/altimate/dbt-cost-estimate.test.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index cf05aa7f24..08b1f34c28 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -64,7 +64,7 @@ function failure(title: string, message: string, extra: Record export const DbtCostEstimateTool = Tool.define("dbt_cost_estimate", { description: - "Estimate the bytes a dbt model or a SQL query would scan, from a warehouse dry run. Only BigQuery has a dry run: any other warehouse answers `supported: false` and nothing is compiled. Starts the dbt adapter, so it needs write permission (`--allow-write` on `tool run` or the MCP server). Pass `model` (a model name, compiled as dbt would) or `sql` (may use Jinja).", + "Estimate the bytes a dbt model or a SQL query would scan, from a warehouse dry run. Only BigQuery has a dry run: any other warehouse answers `supported: false` and nothing is compiled. Starts the dbt adapter (which runs `dbt deps`), so it needs write permission (`--allow-write` on `tool run` or the MCP server). The dry run never executes the final query, but compiling a model renders its Jinja, and macros such as `run_query` run real queries that are billed. Pass `model` (a model name, compiled as dbt would) or `sql` (may use Jinja).", 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"), diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index 95b8b21a9b..2d2ff11e2d 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -192,6 +192,12 @@ describe("dbt_cost_estimate", () => { expect(result.output).toContain("--allow-write") }) + test("the tool description says compiling can run billed queries and the adapter start runs dbt deps", async () => { + const tool = await DbtCostEstimateTool.init() + expect(tool.description).toContain("runs `dbt deps`") + expect(tool.description).toContain("macros such as `run_query` run real queries that are billed") + }) + test("the tool description names the MCP server as a way to grant write", async () => { const tool = await initTool(DbtCostEstimateTool) expect(tool.description).toContain("`--allow-write` on `tool run` or the MCP server") From 67f4dae7f0f487b56522120f5caeaeacd0d43732 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 13:49:53 +0530 Subject: [PATCH 23/28] fix: a coded or pre-send TypeError is not a header refusal; bind the adapter to the checked project A send failure with a network code now reports that code even when it is a TypeError. The URL is checked and the body serialised before fetch, so an uncoded TypeError there can only be the header check. dbtAdapterFor compares the root the adapter started from with the project it checked, refusing as other_project when a later config read named another. The leak assertions now check every 8-character run of the key. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/api/client.ts | 18 +++-- .../src/altimate/tools/dbt-adapter-access.ts | 25 +++++-- .../test/altimate/dbt-adapter-access.test.ts | 71 ++++++++++++++++++- 3 files changed, 100 insertions(+), 14 deletions(-) diff --git a/packages/opencode/src/altimate/api/client.ts b/packages/opencode/src/altimate/api/client.ts index 123630d58b..54d7625e65 100644 --- a/packages/opencode/src/altimate/api/client.ts +++ b/packages/opencode/src/altimate/api/client.ts @@ -333,12 +333,15 @@ export namespace AltimateApi { * stored key can be relied on: only the error's name and code are shown. */ function sendFailure(e: unknown): string { - if (e instanceof TypeError) - return "the request was refused before it was sent (TypeError): the stored API key or tenant name may not be a valid header value." const word = (v: unknown) => (typeof v === "string" && /^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(v) ? v : undefined) const name = (e instanceof Error && word(e.name)) || "Error" const code = word((e as { code?: unknown } | null)?.code) - return code ? `${name} (${code}).` : `${name}.` + // A network failure carries its code, whatever its class. + if (code) return `${name} (${code}).` + // The URL and the body are checked before sending, so an uncoded TypeError is the header check. + if (e instanceof TypeError) + return "the request was refused before it was sent (TypeError): the stored API key or tenant name may not be a valid header value." + return `${name}.` } async function request( @@ -349,6 +352,12 @@ export namespace AltimateApi { signal?: AbortSignal, ) { const url = `${creds.altimateUrl}${endpoint}` + // Checked here so that only the header check can make fetch throw a TypeError before sending. + if (!URL.canParse(url)) + throw new Error(`API ${method} ${endpoint} could not be sent: the stored API URL is not a valid URL.`) + // Any body but undefined is sent: null, false, 0 and "" are JSON values too. A body that can't be + // serialised fails here with its own error, which quotes no credential. + const payload = body !== undefined ? JSON.stringify(body) : undefined // Bun's fetch throws at once, not as a rejection, when it refuses a header: both are caught. const send = async (start: () => Promise) => { try { @@ -384,8 +393,7 @@ export namespace AltimateApi { "x-tenant": creds.altimateInstanceName, ...clientHeaders(), }, - // Any body but undefined is sent: null, false, 0 and "" are JSON values too. - ...(body !== undefined ? { body: JSON.stringify(body) } : {}), + ...(payload !== undefined ? { body: payload } : {}), }), ) if (!res.ok) { diff --git a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts index d51d2b4d2b..8c81cb0980 100644 --- a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts +++ b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts @@ -74,6 +74,15 @@ export async function dbtAdapterFor(ctx: Pick { - try { - return fs.realpathSync(p) - } catch { - return path.resolve(p) - } - } const here = real(found) // path.relative ignores case on win32. if (!root) return { other: here } diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index d3a587135e..4f2c9eb8d9 100644 --- a/packages/opencode/test/altimate/dbt-adapter-access.test.ts +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -349,6 +349,29 @@ describe("dbtAdapterFor", () => { expect(timers.size).toBe(0) }) + test("refuses an adapter started from a later config read naming another project", async () => { + const other = await mkdtemp(path.join(os.tmpdir(), "dbt-adapter-moved-")) + await writeFile(path.join(other, "dbt_project.yml"), "name: other\n") + let reads = 0 + // The session check reads the session's project; the adapter's own start reads another. + dbtAdapterInternals.readConfig = async () => ({ + projectRoot: reads++ === 0 ? project : 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 ${other}, not ${project}`, + }) + } finally { + await disposeInstance(project) + await rm(other, { recursive: true, force: true }) + } + }) + 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") @@ -482,9 +505,11 @@ describe("AltimateApi.apiRequest", () => { const HEADER_REFUSED = "could not be sent: the request was refused before it was sent (TypeError): the stored API key or tenant name may not be a valid header value." - /** Every substring of `key` long enough to identify it, so a trimmed or split copy is caught too. */ + /** No 8-character run of `key` (or of a shorter piece of it) appears, so a trimmed or split copy is caught too. */ function expectNoPartOf(text: string, key: string) { - for (const part of key.split(/\s+/).filter((p) => p.length >= 4)) expect(text).not.toContain(part) + for (const piece of key.split(/\s+/).filter((p) => p.length >= 4)) + for (let i = 0; i + Math.min(8, piece.length) <= piece.length; i++) + expect(text).not.toContain(piece.slice(i, i + Math.min(8, piece.length))) expect(text).not.toContain("Bearer") } @@ -529,6 +554,48 @@ describe("AltimateApi.apiRequest", () => { } }) + test("a coded TypeError is a network failure, not a header refusal", async () => { + await Bun.write( + path.join(home, ".altimate", "altimate.json"), + JSON.stringify({ altimateUrl: "http://127.0.0.1:9", altimateInstanceName: "acme", altimateApiKey: "k1" }), + ) + const original = globalThis.fetch + globalThis.fetch = (async () => { + throw Object.assign(new TypeError("fetch failed"), { code: "ECONNREFUSED" }) + }) as unknown as typeof fetch + try { + const error = await AltimateApi.apiRequest("GET", "/dbt/v1/x").catch((e: Error) => e) + expect(error.message).toBe("API GET /dbt/v1/x could not be sent: TypeError (ECONNREFUSED).") + } finally { + globalThis.fetch = original + } + }) + + test("a stored URL that isn't one, or a body that can't be serialised, is not called a bad header", async () => { + const file = path.join(home, ".altimate", "altimate.json") + await Bun.write( + file, + JSON.stringify({ altimateUrl: "api.example.test", altimateInstanceName: "a", altimateApiKey: "k1" }), + ) + const badUrl = await AltimateApi.apiRequest("GET", "/dbt/v1/x").catch((e: Error) => e) + expect(badUrl.message).toBe("API GET /dbt/v1/x could not be sent: the stored API URL is not a valid URL.") + await Bun.write( + file, + JSON.stringify({ altimateUrl: "http://127.0.0.1:9", altimateInstanceName: "a", altimateApiKey: "k1" }), + ) + const original = globalThis.fetch + let sent = 0 + globalThis.fetch = (async () => (sent++, new Response("{}"))) as unknown as typeof fetch + try { + const badBody = await AltimateApi.apiRequest("POST", "/dbt/v1/x", { n: 1n }).catch((e: Error) => e) + expect(badBody).toBeInstanceOf(TypeError) + expect(badBody.message).not.toContain("header") + expect(sent).toBe(0) + } finally { + globalThis.fetch = original + } + }) + test("names a network failure by its code, never by the error's text", async () => { await Bun.write( path.join(home, ".altimate", "altimate.json"), From 5c61e7bdbe219c2e567864c13a38c8ed6f7eeba0 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 13:49:57 +0530 Subject: [PATCH 24/28] fix: the description test reads the tool the way the others do --- packages/opencode/test/altimate/dbt-cost-estimate.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index 2d2ff11e2d..cf1ef83df0 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -193,7 +193,7 @@ describe("dbt_cost_estimate", () => { }) test("the tool description says compiling can run billed queries and the adapter start runs dbt deps", async () => { - const tool = await DbtCostEstimateTool.init() + const tool = await initTool(DbtCostEstimateTool) expect(tool.description).toContain("runs `dbt deps`") expect(tool.description).toContain("macros such as `run_query` run real queries that are billed") }) From 3bad7cc48c3cdb4a1be290d6b7248da10ad6845f Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 16:14:19 +0530 Subject: [PATCH 25/28] fix: the tool description says compiling either input can render Jinja; drop a redundant Math.min in a test Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/tools/dbt-cost-estimate.ts | 2 +- packages/opencode/test/altimate/dbt-adapter-access.test.ts | 2 +- packages/opencode/test/altimate/dbt-cost-estimate.test.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index 08b1f34c28..e3f5fbb7bd 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -64,7 +64,7 @@ function failure(title: string, message: string, extra: Record export const DbtCostEstimateTool = Tool.define("dbt_cost_estimate", { description: - "Estimate the bytes a dbt model or a SQL query would scan, from a warehouse dry run. Only BigQuery has a dry run: any other warehouse answers `supported: false` and nothing is compiled. Starts the dbt adapter (which runs `dbt deps`), so it needs write permission (`--allow-write` on `tool run` or the MCP server). The dry run never executes the final query, but compiling a model renders its Jinja, and macros such as `run_query` run real queries that are billed. Pass `model` (a model name, compiled as dbt would) or `sql` (may use Jinja).", + "Estimate the bytes a dbt model or a SQL query would scan, from a warehouse dry run. Only BigQuery has a dry run: any other warehouse answers `supported: false` and nothing is compiled. Starts the dbt adapter (which runs `dbt deps`), so it needs write permission (`--allow-write` on `tool run` or the MCP server). The dry run never executes the final query, but compiling either input can render Jinja, and macros such as `run_query` can run real queries that are billed. Pass `model` (a model name, compiled as dbt would) or `sql` (may use Jinja).", 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"), diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index 517a0d3348..5f17bf3d27 100644 --- a/packages/opencode/test/altimate/dbt-adapter-access.test.ts +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -512,7 +512,7 @@ describe("AltimateApi.apiRequest", () => { function expectNoPartOf(text: string, key: string) { for (const piece of key.split(/\s+/).filter((p) => p.length >= 4)) for (let i = 0; i + Math.min(8, piece.length) <= piece.length; i++) - expect(text).not.toContain(piece.slice(i, i + Math.min(8, piece.length))) + expect(text).not.toContain(piece.slice(i, i + 8)) expect(text).not.toContain("Bearer") } diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index cf1ef83df0..246f909397 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -195,7 +195,7 @@ describe("dbt_cost_estimate", () => { test("the tool description says compiling can run billed queries and the adapter start runs dbt deps", async () => { const tool = await initTool(DbtCostEstimateTool) expect(tool.description).toContain("runs `dbt deps`") - expect(tool.description).toContain("macros such as `run_query` run real queries that are billed") + expect(tool.description).toContain("compiling either input can render Jinja, and macros such as `run_query` can run real queries that are billed") }) test("the tool description names the MCP server as a way to grant write", async () => { From 10a9f1f115f57eef5af4d929f8f386813ea70baa Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 06:59:23 +0530 Subject: [PATCH 26/28] fix: a zero dry-run byte count is a lower bound, not an upper bound Co-Authored-By: Claude Opus 5.5 --- .../src/altimate/tools/dbt-cost-estimate.ts | 16 ++++++++++++++-- .../test/altimate/dbt-cost-estimate.test.ts | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index e3f5fbb7bd..1976f0a109 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -124,10 +124,22 @@ export const DbtCostEstimateTool = Tool.define("dbt_cost_estimate", { } const name = args.model const shown = typeof display === "number" ? `${parsed.bytes} Bytes` : String(display) + // A dry run can report 0 for a query that still reads data (BigQuery federated sources), so 0 is a + // lower bound and carries no upper bound. + const zero = parsed.bytes === 0 return { title: `dbt_cost_estimate: ${shown}`, - metadata: { success: true, supported: true, adapter: type, ...parsed, display: shown, ...(name && { model: name }) }, - output: `${name ? `${name} ` : "The query "}would process ${shown} (at most ${parsed.maxBytes} bytes).`, + metadata: { + success: true, + supported: true, + adapter: type, + ...(zero ? { bytes: 0, lowerBound: true } : parsed), + display: shown, + ...(name && { model: name }), + }, + output: `${name ? `${name} ` : "The query "}would process ${shown} ${ + zero ? "(a dry run can report 0 for a query that still reads data, so this is a lower bound, not a cap)." : `(at most ${parsed.maxBytes} bytes).` + }`, } }, }) diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index 246f909397..cbe68ea2e2 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -169,6 +169,22 @@ describe("dbt_cost_estimate", () => { expect(result.output).toContain("2048 Bytes") }) + test("a zero from the dry run is a lower bound, not an upper bound", async () => { + useAdapter(calls, { bytes: 0 }) + const result = await (await initTool(DbtCostEstimateTool)).execute({ sql: "select 1" }, ctx) + expect(result.metadata).toEqual({ + success: true, + supported: true, + adapter: "bigquery", + bytes: 0, + lowerBound: true, + display: "0 Bytes", + truncated: false, + }) + expect(result.output).toContain("lower bound") + expect(result.output).not.toContain("at most") + }) + test.each(["snowflake", "databricks", "duckdb"])("answers supported:false on %s and compiles nothing", async (type) => { useAdapter(calls, { type }) const result = await (await initTool(DbtCostEstimateTool)).execute({ model: "orders" }, ctx) From b48df04dd2de64367dddcfd06ff1fe8b5f672179 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 07:13:21 +0530 Subject: [PATCH 27/28] fix: a script's dry run is a lower bound, not an upper bound Co-Authored-By: Claude Opus 5.5 --- .../src/altimate/tools/dbt-cost-estimate.ts | 19 +++++++++++++---- .../test/altimate/dbt-cost-estimate.test.ts | 21 +++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index 1976f0a109..e4cfd45ec8 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -20,6 +20,15 @@ const UNITS = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"] as const * rounded: the true count is within half a tenth of its unit. Undefined for anything else, so a changed * format is reported, never read as a count. */ +/** Whether the SQL is a script (more than one statement, or EXECUTE IMMEDIATE): a dry run only sees its first statements. */ +export function isScript(sql: string): boolean { + const bare = sql + .replace(/--[^\n]*|#[^\n]*|\/\*[\s\S]*?\*\//g, " ") + .replace(/'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`[^`]*`/g, " ") + .replace(/;\s*$/, "") + return bare.includes(";") || /\bexecute\s+immediate\b/i.test(bare) +} + export function parseBytesProcessed(display: unknown): { bytes: number; maxBytes: number } | undefined { if (typeof display === "number") { if (!Number.isFinite(display) || display < 0) return undefined @@ -124,21 +133,23 @@ export const DbtCostEstimateTool = Tool.define("dbt_cost_estimate", { } const name = args.model const shown = typeof display === "number" ? `${parsed.bytes} Bytes` : String(display) - // A dry run can report 0 for a query that still reads data (BigQuery federated sources), so 0 is a - // lower bound and carries no upper bound. + // A dry run can report 0 for a query that still reads data (BigQuery federated sources), and it skips + // what a script runs after its first statement or inside EXECUTE IMMEDIATE: those figures are lower + // bounds and carry no upper bound. const zero = parsed.bytes === 0 + const lower = zero || isScript(compiled as string) return { title: `dbt_cost_estimate: ${shown}`, metadata: { success: true, supported: true, adapter: type, - ...(zero ? { bytes: 0, lowerBound: true } : parsed), + ...(lower ? { bytes: parsed.bytes, lowerBound: true } : parsed), display: shown, ...(name && { model: name }), }, output: `${name ? `${name} ` : "The query "}would process ${shown} ${ - zero ? "(a dry run can report 0 for a query that still reads data, so this is a lower bound, not a cap)." : `(at most ${parsed.maxBytes} bytes).` + lower ? "(a dry run can undercount a script or a zero estimate, so this is a lower bound, not a cap)." : `(at most ${parsed.maxBytes} bytes).` }`, } }, diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index cbe68ea2e2..c57623e5b5 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -185,6 +185,27 @@ describe("dbt_cost_estimate", () => { expect(result.output).not.toContain("at most") }) + test.each([ + "select 1; select * from big", + "select 1; execute immediate 'select * from big'", + "execute immediate 'select * from big'", + ])("a script (%s) is a lower bound, not an upper bound", async (sql) => { + useAdapter(calls, { bytes: "1.0 GiB" }) + const result = await (await initTool(DbtCostEstimateTool)).execute({ sql }, ctx) + expect(result.metadata).toMatchObject({ bytes: 1024 ** 3, lowerBound: true }) + expect(result.metadata).not.toHaveProperty("maxBytes") + expect(result.output).not.toContain("at most") + }) + + test("a semicolon in a string or a comment is not a script", async () => { + useAdapter(calls, { bytes: "1.0 GiB" }) + const tool = await initTool(DbtCostEstimateTool) + for (const sql of ["select 'a;b' from t", "select 1 -- x; y\nfrom t", "select `p.d;t`.c from `p.d.t`"]) { + const result = await tool.execute({ sql }, ctx) + expect(result.metadata).toHaveProperty("maxBytes") + } + }) + test.each(["snowflake", "databricks", "duckdb"])("answers supported:false on %s and compiles nothing", async (type) => { useAdapter(calls, { type }) const result = await (await initTool(DbtCostEstimateTool)).execute({ model: "orders" }, ctx) From 29b8f0588a767a3bdb2d6303e3a068c3cb386175 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 07:21:16 +0530 Subject: [PATCH 28/28] fix: script detection reads comments and quoted text in one pass Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/tools/dbt-cost-estimate.ts | 4 ++-- packages/opencode/test/altimate/dbt-cost-estimate.test.ts | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts index e4cfd45ec8..758dfc2631 100644 --- a/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts +++ b/packages/opencode/src/altimate/tools/dbt-cost-estimate.ts @@ -22,9 +22,9 @@ const UNITS = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"] as const */ /** Whether the SQL is a script (more than one statement, or EXECUTE IMMEDIATE): a dry run only sees its first statements. */ export function isScript(sql: string): boolean { + // One pass over comments and quoted text together: a quote inside a comment, or a comment marker inside a quote, is not either. const bare = sql - .replace(/--[^\n]*|#[^\n]*|\/\*[\s\S]*?\*\//g, " ") - .replace(/'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`[^`]*`/g, " ") + .replace(/--[^\n]*|#[^\n]*|\/\*[\s\S]*?\*\/|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`[^`]*`/g, " ") .replace(/;\s*$/, "") return bare.includes(";") || /\bexecute\s+immediate\b/i.test(bare) } diff --git a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts index c57623e5b5..885e982b95 100644 --- a/packages/opencode/test/altimate/dbt-cost-estimate.test.ts +++ b/packages/opencode/test/altimate/dbt-cost-estimate.test.ts @@ -189,6 +189,8 @@ describe("dbt_cost_estimate", () => { "select 1; select * from big", "select 1; execute immediate 'select * from big'", "execute immediate 'select * from big'", + "select * from t where m = '--'; execute immediate 'select * from big'", + "select * from t where m = '#sale'; select * from big", ])("a script (%s) is a lower bound, not an upper bound", async (sql) => { useAdapter(calls, { bytes: "1.0 GiB" }) const result = await (await initTool(DbtCostEstimateTool)).execute({ sql }, ctx)