From b7cfc0426d60963a83f22a8f5c6053c7b7fce8e4 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 16:30:43 +0530 Subject: [PATCH 01/27] 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 21d0494cfbb1c87a2b1b3ba6e347f4f1be149856 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 17:01:49 +0530 Subject: [PATCH 02/27] 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 165ef43b9af0afb64ff0c17490250d9251bcb795 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 17:03:13 +0530 Subject: [PATCH 03/27] =?UTF-8?q?feat:=20dbt=5Fsql=5Fgraph=20tool=20?= =?UTF-8?q?=E2=80=94=20a=20model's=20CTEs,=20joins=20and=20filters=20as=20?= =?UTF-8?q?a=20graph?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Compiles the model, reads its parents' columns from the warehouse's metadata, and asks the Altimate backend's sql_lineage endpoint; the graph's final select is named after the model and its relations are tied to their dbt nodes. Co-Authored-By: Claude Sonnet 5.5 --- .../opencode/src/altimate/api/sql-lineage.ts | 41 +++ .../src/altimate/tools/dbt-sql-graph.ts | 246 +++++++++++++++++- .../test/altimate/dbt-sql-graph.test.ts | 240 +++++++++++++++++ 3 files changed, 520 insertions(+), 7 deletions(-) create mode 100644 packages/opencode/src/altimate/api/sql-lineage.ts create mode 100644 packages/opencode/test/altimate/dbt-sql-graph.test.ts diff --git a/packages/opencode/src/altimate/api/sql-lineage.ts b/packages/opencode/src/altimate/api/sql-lineage.ts new file mode 100644 index 0000000000..4ad48b5c62 --- /dev/null +++ b/packages/opencode/src/altimate/api/sql-lineage.ts @@ -0,0 +1,41 @@ +import { AltimateApi } from "./client" + +/** One parent relation handed to the backend: its warehouse location and its columns. */ +export type SqlLineageModelNode = { + database: string + schema: string + name: string + alias: string + uniqueId: string + columns: Record + path: string | undefined +} + +export type SqlLineageRequest = { + compiled_sql: string + model_info: { model_node: SqlLineageModelNode }[] + model_dialect: string + session_id: string +} + +export type SqlLineageNodeDetails = { + name: string + type: string + nodeType?: string + nodeId?: string + sql: string + columns: { name: string; datatype?: string; expression?: string }[] +} + +export type SqlLineageResponse = { + tableEdges: [string, string][] + details: Record + nodePositions?: Record +} + +export const SQL_LINEAGE_ENDPOINT = "/dbt/v3/sql_lineage" + +/** The backend's graph of a compiled model's CTEs, joins and filters. */ +export async function sqlLineage(req: SqlLineageRequest): Promise { + return (await AltimateApi.apiRequest("POST", SQL_LINEAGE_ENDPOINT, req)) as SqlLineageResponse +} diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index b2af1b0f18..2cfb4920e1 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -1,16 +1,248 @@ import z from "zod" +import crypto from "crypto" +import fs from "fs" +import path from "path" import { Tool } from "../../tool/tool" +import { sqlLineage, type SqlLineageModelNode, type SqlLineageRequest, type SqlLineageResponse } from "../api/sql-lineage" +import { dbtAdapterFor } from "./dbt-adapter-access" + +const FINAL_SELECT = "__final_select__" + +export type SqlGraphNode = { + name: string + /** The backend's node type ("cte", "table", ...); a relation that maps to a dbt node carries its resource type ("model", "source", ...). */ + type: string + nodeId?: string + sql: string + columns: { name: string; datatype?: string; expression?: string }[] +} + +export type SqlGraph = { nodes: SqlGraphNode[]; edges: [string, string][] } + +type ManifestNode = { + unique_id: string + name: string + alias: string + resource_type: string + path?: string + database: string + schema: string + columns: SqlLineageModelNode["columns"] +} +type ManifestSource = { + name: string + database: string + schema: string + tables: { name: string; identifier: string; path?: string; columns: SqlLineageModelNode["columns"] }[] +} + +/** The slice of dbt-integration's adapter this tool uses. */ +export type SqlGraphAdapter = { + parseManifest(): Promise< + | { + nodeMetaMap: { lookupByUniqueId(id: string): ManifestNode | undefined; lookupByBaseName(name: string): ManifestNode | undefined } + sourceMetaMap: { get(name: string): ManifestSource | undefined } + } + | undefined + > + unsafeCompileQuery(query: string, originalModelName?: string): Promise + getNonEphemeralParents(keys: string[]): string[] + getBulkSchemaFromDB( + nodes: { unique_id: string; name: string; resource_type: string; table?: string }[], + signal: AbortSignal, + ): Promise> + getAdapterType(): string +} + +export type SqlGraphDeps = { + fetchLineage: (req: SqlLineageRequest) => Promise + readFile: (file: string) => string + /** The dbt project's root, to resolve a model's project-relative path. */ + projectRoot: string | undefined +} + +/** A column name as the warehouse and the manifest agree on it: lower case unless quoted. */ +function columnKey(name: string): string { + return /^(["`]).*\1$/.test(name) ? name : name.toLowerCase() +} + +/** The parent's manifest columns plus any the warehouse reports that the manifest lacks. */ +export function mergeColumns( + manifestColumns: SqlLineageModelNode["columns"], + fromDb: { column: string; dtype: string }[] | undefined, +): SqlLineageModelNode["columns"] { + const merged: SqlLineageModelNode["columns"] = {} + for (const [k, v] of Object.entries(manifestColumns ?? {})) merged[columnKey(k)] = { ...v } + for (const c of fromDb ?? []) { + const key = columnKey(c.column) + const existing = merged[key] + if (existing) { + existing.data_type = (existing.data_type || c.dtype)?.toLowerCase() + continue + } + merged[key] = { name: key, data_type: c.dtype?.toLowerCase(), description: "", meta: {} } + } + return merged +} + +/** + * The backend names the model's own select `__final_select__`: name it after the model, tie each + * relation node to the dbt node it stands for, and drop the layout positions the list doesn't use. + */ +export function remapSqlLineage( + response: SqlLineageResponse, + model: { name: string; uniqueId: string; resourceType: string }, + parents: { uniqueId: string; alias?: string; sourceTable?: string; resourceType: string }[], +): SqlGraph { + const details = { ...response.details } + const mapping: Record = {} + for (const p of parents) { + // A source's relation is its table name; any other node's, its alias. + const wanted = (p.sourceTable ?? p.alias ?? "").toLowerCase() + if (!wanted) continue + const key = Object.keys(details).find((k) => details[k].type === "table" && k.toLowerCase() === wanted) + if (key) mapping[key] = { nodeId: p.uniqueId, type: p.resourceType } + } + mapping[model.name] = { nodeId: model.uniqueId, type: model.resourceType } + const edges = response.tableEdges.map( + (edge) => edge.map((item) => (item === FINAL_SELECT ? model.name : item)) as [string, string], + ) + if (details[FINAL_SELECT]) { + details[model.name] = details[FINAL_SELECT] + delete details[FINAL_SELECT] + } + const nodes: SqlGraphNode[] = Object.entries(details).map(([key, d]) => { + const merged = { ...d, ...mapping[key] } + return { + name: key === model.name ? model.name : (merged.name ?? key), + type: merged.type, + ...(merged.nodeId ? { nodeId: merged.nodeId } : {}), + sql: merged.sql ?? "", + columns: merged.columns ?? [], + } + }) + return { nodes, edges } +} + +/** Compile the model, read its parents' columns, ask the backend, and return the remapped graph. */ +export async function buildSqlGraph( + adapter: SqlGraphAdapter, + model: string, + deps: SqlGraphDeps, + signal: AbortSignal, +): Promise { + const manifest = await adapter.parseManifest() + if (!manifest) throw new Error("the dbt manifest is not available") + const node = manifest.nodeMetaMap.lookupByUniqueId(model) ?? manifest.nodeMetaMap.lookupByBaseName(model) + if (!node) throw new Error(`no model named ${model} in the manifest`) + if (!node.path) throw new Error(`${node.name} has no SQL file`) + const file = path.isAbsolute(node.path) ? node.path : path.join(deps.projectRoot ?? "", node.path) + const compiled = await adapter.unsafeCompileQuery(deps.readFile(file), node.name) + if (!compiled) throw new Error(`unable to compile ${node.name}`) + + const parentIds = adapter.getNonEphemeralParents([node.unique_id]) + const requests: { unique_id: string; name: string; resource_type: string; table?: string }[] = [] + const infos: SqlLineageModelNode[] = [] + const parents: { uniqueId: string; alias?: string; sourceTable?: string; resourceType: string }[] = [] + for (const id of parentIds) { + const parts = id.split(".") + if (parts[0] === "source") { + const source = manifest.sourceMetaMap.get(parts[2]) + const table = source?.tables.find((t) => t.name === parts[3]) + if (!source || !table) continue + requests.push({ unique_id: id, name: source.name, resource_type: "source", table: table.name }) + infos.push({ + database: source.database, + schema: source.schema, + name: table.name, + alias: table.identifier, + uniqueId: id, + columns: table.columns, + path: table.path, + }) + parents.push({ uniqueId: id, sourceTable: table.name, resourceType: "source" }) + continue + } + const parent = manifest.nodeMetaMap.lookupByUniqueId(id) + if (!parent) continue + requests.push({ unique_id: id, name: parent.name, resource_type: parent.resource_type }) + infos.push({ + database: parent.database, + schema: parent.schema, + name: parent.name, + alias: parent.alias, + uniqueId: id, + columns: parent.columns, + path: parent.path, + }) + parents.push({ uniqueId: id, alias: parent.alias, resourceType: parent.resource_type }) + } + // The warehouse metadata read: once per call, only for the parents. + const fromDb = requests.length ? await adapter.getBulkSchemaFromDB(requests, signal) : {} + signal.throwIfAborted() + const hash = crypto.createHash("md5").update(compiled).digest("hex") + const response = await deps.fetchLineage({ + compiled_sql: compiled, + model_info: infos.map((n) => ({ model_node: { ...n, columns: mergeColumns(n.columns, fromDb[n.uniqueId]) } })), + model_dialect: adapter.getAdapterType(), + session_id: `altimate-code-${hash}`, + }) + signal.throwIfAborted() + return remapSqlLineage( + response, + { name: node.name, uniqueId: node.unique_id, resourceType: node.resource_type }, + parents, + ) +} + +/** The graph as a list in dependency order: each node, its inputs, its column count. */ +export function formatSqlGraph(model: string, graph: SqlGraph): string { + const inputs = new Map() + for (const [from, to] of graph.edges) inputs.set(to, [...(inputs.get(to) ?? []), from]) + const lines = [`SQL graph for ${model}: ${graph.nodes.length} nodes, ${graph.edges.length} edges`] + for (const n of graph.nodes) { + const from = inputs.get(n.name) + lines.push(`- ${n.name} (${n.type}${n.nodeId ? `, ${n.nodeId}` : ""}): ${n.columns.length} columns${from?.length ? `, from ${from.join(", ")}` : ""}`) + } + return lines.join("\n") +} 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.", + description: + "The CTEs, joins and filters of a dbt model as a graph, from the Altimate backend: nodes with their SQL and columns, and the edges between them. Compiles the model with the project's dbt integration, reads its parent relations' columns from the warehouse's metadata, and sends the compiled SQL to the Altimate backend. Needs write permission (starts the dbt integration) and an Altimate key.", parameters: z.object({ - model: z.string().describe("Model name"), + model: z.string().describe("Model name or unique_id (e.g. 'orders', 'model.shop.orders')"), }), - 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.", + async execute(args, ctx) { + const access = await dbtAdapterFor(ctx) + if (!access.adapter) { + return { + title: "dbt SQL graph: not available", + metadata: { success: false, model: args.model, reason: access.reason }, + output: `Not available: ${access.reason}`, + } + } + try { + const { getDbtProjectRoot } = await import("../native/connections/register") + const graph = await buildSqlGraph( + access.adapter as SqlGraphAdapter, + args.model, + { fetchLineage: sqlLineage, readFile: (f) => fs.readFileSync(f, "utf8"), projectRoot: await getDbtProjectRoot() }, + ctx.abort, + ) + return { + title: `dbt SQL graph: ${args.model} (${graph.nodes.length} nodes)`, + metadata: { success: true, model: args.model, nodes: graph.nodes, edges: graph.edges }, + output: formatSqlGraph(args.model, graph), + } + } catch (e) { + if (ctx.abort.aborted) throw e + const msg = e instanceof Error ? e.message : String(e) + return { + title: "dbt SQL graph: ERROR", + metadata: { success: false, model: args.model, error: msg }, + output: `Failed: ${msg}`, + } } }, }) diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts new file mode 100644 index 0000000000..c52ef13384 --- /dev/null +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -0,0 +1,240 @@ +// altimate_change - new file +// +// dbt_sql_graph: the request built from the compiled model and its parents' columns, and the +// remap of the backend's reply (`__final_select__` to the model, relation nodes to dbt unique ids). +// The adapter and the endpoint are fakes, so no dbt, warehouse or backend is involved. +import { describe, expect, test } from "bun:test" +import { + buildSqlGraph, + formatSqlGraph, + mergeColumns, + remapSqlLineage, + type SqlGraphAdapter, +} from "@/altimate/tools/dbt-sql-graph" +import type { SqlLineageRequest, SqlLineageResponse } from "@/altimate/api/sql-lineage" + +const col = (name: string, data_type = "") => ({ name, description: "", data_type, meta: {} }) + +const orders = { + unique_id: "model.shop.orders", + name: "orders", + alias: "orders", + resource_type: "model", + path: "/proj/models/orders.sql", + database: "db", + schema: "analytics", + columns: {}, +} +const stgCustomers = { + unique_id: "model.shop.stg_customers", + name: "stg_customers", + alias: "stg_customers", + resource_type: "model", + path: "/proj/models/stg_customers.sql", + database: "db", + schema: "analytics", + columns: { id: col("id", "int") }, +} +const rawOrders = { + name: "raw", + database: "db", + schema: "raw", + tables: [{ name: "raw_orders", identifier: "RAW_ORDERS_T", columns: {} }], +} + +function adapter(overrides: Partial = {}): SqlGraphAdapter & { calls: string[] } { + const calls: string[] = [] + const nodes = [orders, stgCustomers] + return { + calls, + parseManifest: async () => ({ + nodeMetaMap: { + lookupByUniqueId: (id) => nodes.find((n) => n.unique_id === id), + lookupByBaseName: (name) => nodes.find((n) => n.name === name), + }, + sourceMetaMap: { get: (name) => (name === "raw" ? rawOrders : undefined) }, + }), + unsafeCompileQuery: async (q) => { + calls.push(`compile:${q}`) + return "with o as (select * from raw.orders) select * from o join stg_customers c on 1=1" + }, + getNonEphemeralParents: () => ["source.shop.raw.raw_orders", "model.shop.stg_customers"], + getBulkSchemaFromDB: async (requested) => { + calls.push(`schema:${requested.map((r) => r.unique_id).join(",")}`) + return { + "source.shop.raw.raw_orders": [{ column: "ID", dtype: "INT" }, { column: "AMOUNT", dtype: "NUMBER" }], + "model.shop.stg_customers": [{ column: "ID", dtype: "INT" }, { column: "NAME", dtype: "TEXT" }], + } + }, + getAdapterType: () => "snowflake", + ...overrides, + } +} + +const reply: SqlLineageResponse = { + tableEdges: [ + ["raw_orders", "o"], + ["o", "__final_select__"], + ["stg_customers", "__final_select__"], + ], + details: { + raw_orders: { name: "raw_orders", type: "table", sql: "", columns: [{ name: "id" }, { name: "amount" }] }, + o: { name: "o", type: "cte", sql: "select * from raw.orders", columns: [{ name: "id" }] }, + stg_customers: { name: "stg_customers", type: "table", sql: "", columns: [{ name: "id" }, { name: "name" }] }, + __final_select__: { name: "__final_select__", type: "final", sql: "select * from o", columns: [{ name: "id" }] }, + }, + nodePositions: { o: [1, 1], __final_select__: [2, 2] }, +} + +const signal = new AbortController().signal + +describe("mergeColumns", () => { + test("adds warehouse columns the manifest lacks, lower-cased, and fills a missing type", () => { + const merged = mergeColumns({ id: col("id") }, [ + { column: "ID", dtype: "INT" }, + { column: "NEW_ONE", dtype: "TEXT" }, + ]) + expect(Object.keys(merged)).toEqual(["id", "new_one"]) + expect(merged.id.data_type).toBe("int") + expect(merged.new_one).toEqual({ name: "new_one", data_type: "text", description: "", meta: {} }) + }) + + test("keeps a quoted name as written and tolerates no warehouse answer", () => { + expect(Object.keys(mergeColumns({}, [{ column: '"MixedCase"', dtype: "int" }]))).toEqual(['"MixedCase"']) + expect(mergeColumns({ a: col("a", "int") }, undefined).a.data_type).toBe("int") + }) +}) + +describe("remapSqlLineage", () => { + const parents = [ + { uniqueId: "source.shop.raw.raw_orders", sourceTable: "raw_orders", resourceType: "source" }, + { uniqueId: "model.shop.stg_customers", alias: "STG_CUSTOMERS", resourceType: "model" }, + ] + const graph = remapSqlLineage(reply, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, parents) + + test("renames __final_select__ to the model in edges and nodes", () => { + expect(graph.edges).toEqual([ + ["raw_orders", "o"], + ["o", "orders"], + ["stg_customers", "orders"], + ]) + expect(graph.nodes.map((n) => n.name)).toEqual(["raw_orders", "o", "stg_customers", "orders"]) + expect(JSON.stringify(graph)).not.toContain("__final_select__") + }) + + test("ties table nodes to dbt unique ids, case-insensitively, and the model to itself", () => { + const byName = Object.fromEntries(graph.nodes.map((n) => [n.name, n])) + expect(byName.raw_orders).toMatchObject({ nodeId: "source.shop.raw.raw_orders", type: "source" }) + expect(byName.stg_customers).toMatchObject({ nodeId: "model.shop.stg_customers", type: "model" }) + expect(byName.orders).toMatchObject({ nodeId: "model.shop.orders", type: "model", sql: "select * from o" }) + }) + + test("leaves CTEs unmapped and drops the layout positions", () => { + const cte = graph.nodes.find((n) => n.name === "o")! + expect(cte).toEqual({ name: "o", type: "cte", sql: "select * from raw.orders", columns: [{ name: "id" }] }) + expect(graph).not.toHaveProperty("nodePositions") + }) + + test("does not mutate the backend reply", () => { + expect(reply.details.__final_select__).toBeDefined() + expect(reply.tableEdges[1]).toEqual(["o", "__final_select__"]) + }) +}) + +describe("buildSqlGraph", () => { + const read = (file: string) => `-- ${file}\nselect 1` + + test("sends the compiled SQL, parents' merged columns and dialect, then remaps the reply", async () => { + const a = adapter() + let sent: SqlLineageRequest | undefined + const graph = await buildSqlGraph( + a, + "orders", + { + fetchLineage: async (req) => { + sent = req + return reply + }, + readFile: read, + projectRoot: "/proj", + }, + signal, + ) + expect(a.calls).toEqual([ + "compile:-- /proj/models/orders.sql\nselect 1", + "schema:source.shop.raw.raw_orders,model.shop.stg_customers", + ]) + expect(sent?.model_dialect).toBe("snowflake") + expect(sent?.compiled_sql).toContain("join stg_customers") + expect(sent?.session_id).toMatch(/^altimate-code-[0-9a-f]{32}$/) + const infos = Object.fromEntries(sent!.model_info.map((i) => [i.model_node.uniqueId, i.model_node])) + expect(Object.keys(infos["source.shop.raw.raw_orders"].columns)).toEqual(["id", "amount"]) + expect(infos["source.shop.raw.raw_orders"].alias).toBe("RAW_ORDERS_T") + expect(Object.keys(infos["model.shop.stg_customers"].columns)).toEqual(["id", "name"]) + expect(graph.edges).toContainEqual(["o", "orders"]) + expect(graph.nodes.find((n) => n.name === "orders")?.nodeId).toBe("model.shop.orders") + expect(graph.nodes.find((n) => n.name === "raw_orders")?.nodeId).toBe("source.shop.raw.raw_orders") + }) + + test("accepts a unique_id and resolves a project-relative path against the project root", async () => { + const relative = { ...orders, path: "models/orders.sql" } + const a = adapter({ + parseManifest: async () => ({ + nodeMetaMap: { + lookupByUniqueId: (id) => (id === relative.unique_id ? relative : undefined), + lookupByBaseName: () => undefined, + }, + sourceMetaMap: { get: () => undefined }, + }), + getNonEphemeralParents: () => [], + }) + let read: string | undefined + await buildSqlGraph( + a, + "model.shop.orders", + { fetchLineage: async () => reply, readFile: (f) => ((read = f), "select 1"), projectRoot: "/proj" }, + signal, + ) + expect(read).toBe("/proj/models/orders.sql") + // no parents: no warehouse read + expect(a.calls.some((c) => c.startsWith("schema:"))).toBe(false) + }) + + test("an unknown model, an uncompilable model and a missing manifest each fail with a reason", async () => { + const deps = { fetchLineage: async () => reply, readFile: read, projectRoot: "/proj" } + await expect(buildSqlGraph(adapter(), "nope", deps, signal)).rejects.toThrow("no model named nope") + await expect(buildSqlGraph(adapter({ unsafeCompileQuery: async () => undefined }), "orders", deps, signal)).rejects.toThrow( + "unable to compile orders", + ) + await expect(buildSqlGraph(adapter({ parseManifest: async () => undefined }), "orders", deps, signal)).rejects.toThrow( + "manifest is not available", + ) + }) + + test("a backend failure propagates and sends nothing after a cancel", async () => { + const deps = { + fetchLineage: async () => { + throw new Error("API POST /dbt/v3/sql_lineage failed with status 500") + }, + readFile: read, + projectRoot: "/proj", + } + await expect(buildSqlGraph(adapter(), "orders", deps, signal)).rejects.toThrow("status 500") + const aborted = AbortSignal.abort() + let called = false + await expect( + buildSqlGraph(adapter(), "orders", { ...deps, fetchLineage: async () => ((called = true), reply) }, aborted), + ).rejects.toThrow() + expect(called).toBe(false) + }) +}) + +describe("formatSqlGraph", () => { + test("lists each node with its inputs and column count", () => { + const graph = remapSqlLineage(reply, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, []) + const text = formatSqlGraph("orders", graph) + expect(text).toContain("4 nodes, 3 edges") + expect(text).toContain("- orders (model, model.shop.orders): 1 columns, from o, stg_customers") + expect(text).toContain("- o (cte): 1 columns, from raw_orders") + }) +}) From 03d509781c712dc41a679e6fcb4df45fc01bbb59 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 17:09:40 +0530 Subject: [PATCH 04/27] test: dbt_sql_graph is no longer a placeholder; format Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-sql-graph.ts | 22 ++++++++++++--- .../test/altimate/dbt-adapter-access.test.ts | 4 ++- .../test/altimate/dbt-sql-graph.test.ts | 28 +++++++++++++------ 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index 2cfb4920e1..d5557e8e50 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -3,7 +3,12 @@ import crypto from "crypto" import fs from "fs" import path from "path" import { Tool } from "../../tool/tool" -import { sqlLineage, type SqlLineageModelNode, type SqlLineageRequest, type SqlLineageResponse } from "../api/sql-lineage" +import { + sqlLineage, + type SqlLineageModelNode, + type SqlLineageRequest, + type SqlLineageResponse, +} from "../api/sql-lineage" import { dbtAdapterFor } from "./dbt-adapter-access" const FINAL_SELECT = "__final_select__" @@ -40,7 +45,10 @@ type ManifestSource = { export type SqlGraphAdapter = { parseManifest(): Promise< | { - nodeMetaMap: { lookupByUniqueId(id: string): ManifestNode | undefined; lookupByBaseName(name: string): ManifestNode | undefined } + nodeMetaMap: { + lookupByUniqueId(id: string): ManifestNode | undefined + lookupByBaseName(name: string): ManifestNode | undefined + } sourceMetaMap: { get(name: string): ManifestSource | undefined } } | undefined @@ -202,7 +210,9 @@ export function formatSqlGraph(model: string, graph: SqlGraph): string { const lines = [`SQL graph for ${model}: ${graph.nodes.length} nodes, ${graph.edges.length} edges`] for (const n of graph.nodes) { const from = inputs.get(n.name) - lines.push(`- ${n.name} (${n.type}${n.nodeId ? `, ${n.nodeId}` : ""}): ${n.columns.length} columns${from?.length ? `, from ${from.join(", ")}` : ""}`) + lines.push( + `- ${n.name} (${n.type}${n.nodeId ? `, ${n.nodeId}` : ""}): ${n.columns.length} columns${from?.length ? `, from ${from.join(", ")}` : ""}`, + ) } return lines.join("\n") } @@ -227,7 +237,11 @@ export const DbtSqlGraphTool = Tool.define("dbt_sql_graph", { const graph = await buildSqlGraph( access.adapter as SqlGraphAdapter, args.model, - { fetchLineage: sqlLineage, readFile: (f) => fs.readFileSync(f, "utf8"), projectRoot: await getDbtProjectRoot() }, + { + fetchLineage: sqlLineage, + readFile: (f) => fs.readFileSync(f, "utf8"), + projectRoot: await getDbtProjectRoot(), + }, ctx.abort, ) return { diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index b565e5a58a..c36f2882f8 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 package has replaced the placeholder; they have tests of their own. */ +const IMPLEMENTED = new Set(["dbt_sql_graph"]) 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((one) => !IMPLEMENTED.has(one))) { const tool = await initTool(EXPOSED[id] as any) expect(tool.id).toBe(id) const result = await tool.execute(MINIMAL_ARGS[id], ctx) diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts index c52ef13384..832d80b928 100644 --- a/packages/opencode/test/altimate/dbt-sql-graph.test.ts +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -62,8 +62,14 @@ function adapter(overrides: Partial = {}): SqlGraphAdapter & { getBulkSchemaFromDB: async (requested) => { calls.push(`schema:${requested.map((r) => r.unique_id).join(",")}`) return { - "source.shop.raw.raw_orders": [{ column: "ID", dtype: "INT" }, { column: "AMOUNT", dtype: "NUMBER" }], - "model.shop.stg_customers": [{ column: "ID", dtype: "INT" }, { column: "NAME", dtype: "TEXT" }], + "source.shop.raw.raw_orders": [ + { column: "ID", dtype: "INT" }, + { column: "AMOUNT", dtype: "NUMBER" }, + ], + "model.shop.stg_customers": [ + { column: "ID", dtype: "INT" }, + { column: "NAME", dtype: "TEXT" }, + ], } }, getAdapterType: () => "snowflake", @@ -110,7 +116,11 @@ describe("remapSqlLineage", () => { { uniqueId: "source.shop.raw.raw_orders", sourceTable: "raw_orders", resourceType: "source" }, { uniqueId: "model.shop.stg_customers", alias: "STG_CUSTOMERS", resourceType: "model" }, ] - const graph = remapSqlLineage(reply, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, parents) + const graph = remapSqlLineage( + reply, + { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, + parents, + ) test("renames __final_select__ to the model in edges and nodes", () => { expect(graph.edges).toEqual([ @@ -203,12 +213,12 @@ describe("buildSqlGraph", () => { test("an unknown model, an uncompilable model and a missing manifest each fail with a reason", async () => { const deps = { fetchLineage: async () => reply, readFile: read, projectRoot: "/proj" } await expect(buildSqlGraph(adapter(), "nope", deps, signal)).rejects.toThrow("no model named nope") - await expect(buildSqlGraph(adapter({ unsafeCompileQuery: async () => undefined }), "orders", deps, signal)).rejects.toThrow( - "unable to compile orders", - ) - await expect(buildSqlGraph(adapter({ parseManifest: async () => undefined }), "orders", deps, signal)).rejects.toThrow( - "manifest is not available", - ) + await expect( + buildSqlGraph(adapter({ unsafeCompileQuery: async () => undefined }), "orders", deps, signal), + ).rejects.toThrow("unable to compile orders") + await expect( + buildSqlGraph(adapter({ parseManifest: async () => undefined }), "orders", deps, signal), + ).rejects.toThrow("manifest is not available") }) test("a backend failure propagates and sends nothing after a cancel", async () => { From f6aed61e4b5df5a0afa675b590cd2ac0d4a59fd0 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 17:30:00 +0530 Subject: [PATCH 05/27] =?UTF-8?q?fix:=20dbt=5Fsql=5Fgraph=20=E2=80=94=20ad?= =?UTF-8?q?apter-aware=20column=20case,=20session=20id,=20cancel=20before?= =?UTF-8?q?=20compile,=20model-name=20collisions,=20models=20only?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-sql-graph.ts | 60 +++-- .../test/altimate/dbt-sql-graph.test.ts | 220 ++++++++++++++++-- 2 files changed, 247 insertions(+), 33 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index d5557e8e50..bd81673e35 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -67,34 +67,46 @@ export type SqlGraphDeps = { readFile: (file: string) => string /** The dbt project's root, to resolve a model's project-relative path. */ projectRoot: string | undefined + /** Names the caller's session in the backend's session id, with the SQL's hash. */ + sessionId: string } -/** A column name as the warehouse and the manifest agree on it: lower case unless quoted. */ -function columnKey(name: string): string { - return /^(["`]).*\1$/.test(name) ? name : name.toLowerCase() +/** Adapters whose unquoted identifiers fold to lower case; the rest fold to upper case. */ +const LOWER_FOLDING = new Set(["trino", "athena", "postgres", "duckdb", "risingwave"]) + +/** A name the warehouse would only keep as written if it were quoted: it is not in the adapter's own folding case. */ +function isQuotedIdentifier(name: string, adapter: string): boolean { + return LOWER_FOLDING.has(adapter) ? !/^([_a-z]+[_a-z0-9$]*)$/.test(name) : !/^([_A-Z]+[_A-Z0-9$]*)$/.test(name) +} + +/** A column name as dbt Power User keys it: lower case unless the adapter would only keep it as written when quoted. */ +function columnKey(name: string, adapter: string): string { + return isQuotedIdentifier(name, adapter) ? name : name.toLowerCase() } /** The parent's manifest columns plus any the warehouse reports that the manifest lacks. */ export function mergeColumns( manifestColumns: SqlLineageModelNode["columns"], fromDb: { column: string; dtype: string }[] | undefined, + adapter: string, ): SqlLineageModelNode["columns"] { - const merged: SqlLineageModelNode["columns"] = {} - for (const [k, v] of Object.entries(manifestColumns ?? {})) merged[columnKey(k)] = { ...v } + // A Map: a column named `constructor` or `__proto__` is a column, not an inherited property. + const merged = new Map() + for (const [k, v] of Object.entries(manifestColumns ?? {})) merged.set(columnKey(k, adapter), { ...v }) for (const c of fromDb ?? []) { - const key = columnKey(c.column) - const existing = merged[key] + const key = columnKey(c.column, adapter) + const existing = merged.get(key) if (existing) { existing.data_type = (existing.data_type || c.dtype)?.toLowerCase() continue } - merged[key] = { name: key, data_type: c.dtype?.toLowerCase(), description: "", meta: {} } + merged.set(key, { name: key, data_type: c.dtype?.toLowerCase(), description: "", meta: {} }) } - return merged + return Object.fromEntries(merged) } /** - * The backend names the model's own select `__final_select__`: name it after the model, tie each + * The backend names the model's own select `__final_select__`: name it after the model (or ` (final select)` when the SQL has a node of that name), tie each * relation node to the dbt node it stands for, and drop the layout positions the list doesn't use. */ export function remapSqlLineage( @@ -111,18 +123,20 @@ export function remapSqlLineage( const key = Object.keys(details).find((k) => details[k].type === "table" && k.toLowerCase() === wanted) if (key) mapping[key] = { nodeId: p.uniqueId, type: p.resourceType } } - mapping[model.name] = { nodeId: model.uniqueId, type: model.resourceType } + // A CTE or relation the SQL names like the model (`with customers as (…)`, `{{ this }}`) keeps its node: the model's own select is told apart. + const finalKey = Object.hasOwn(details, model.name) ? `${model.name} (final select)` : model.name + mapping[finalKey] = { nodeId: model.uniqueId, type: model.resourceType } const edges = response.tableEdges.map( - (edge) => edge.map((item) => (item === FINAL_SELECT ? model.name : item)) as [string, string], + (edge) => edge.map((item) => (item === FINAL_SELECT ? finalKey : item)) as [string, string], ) if (details[FINAL_SELECT]) { - details[model.name] = details[FINAL_SELECT] + details[finalKey] = details[FINAL_SELECT] delete details[FINAL_SELECT] } const nodes: SqlGraphNode[] = Object.entries(details).map(([key, d]) => { const merged = { ...d, ...mapping[key] } return { - name: key === model.name ? model.name : (merged.name ?? key), + name: key === finalKey ? finalKey : (merged.name ?? key), type: merged.type, ...(merged.nodeId ? { nodeId: merged.nodeId } : {}), sql: merged.sql ?? "", @@ -140,12 +154,16 @@ export async function buildSqlGraph( signal: AbortSignal, ): Promise { const manifest = await adapter.parseManifest() + signal.throwIfAborted() if (!manifest) throw new Error("the dbt manifest is not available") const node = manifest.nodeMetaMap.lookupByUniqueId(model) ?? manifest.nodeMetaMap.lookupByBaseName(model) if (!node) throw new Error(`no model named ${model} in the manifest`) - if (!node.path) throw new Error(`${node.name} has no SQL file`) + // A seed, snapshot or analysis shares the node map: only a model's .sql file is compiled and sent. + if (node.resource_type !== "model" || !node.path?.toLowerCase().endsWith(".sql")) + throw new Error(`${node.name} is not a dbt model with a SQL file`) const file = path.isAbsolute(node.path) ? node.path : path.join(deps.projectRoot ?? "", node.path) const compiled = await adapter.unsafeCompileQuery(deps.readFile(file), node.name) + signal.throwIfAborted() if (!compiled) throw new Error(`unable to compile ${node.name}`) const parentIds = adapter.getNonEphemeralParents([node.unique_id]) @@ -189,11 +207,14 @@ export async function buildSqlGraph( const fromDb = requests.length ? await adapter.getBulkSchemaFromDB(requests, signal) : {} signal.throwIfAborted() const hash = crypto.createHash("md5").update(compiled).digest("hex") + const dialect = adapter.getAdapterType() const response = await deps.fetchLineage({ compiled_sql: compiled, - model_info: infos.map((n) => ({ model_node: { ...n, columns: mergeColumns(n.columns, fromDb[n.uniqueId]) } })), - model_dialect: adapter.getAdapterType(), - session_id: `altimate-code-${hash}`, + model_info: infos.map((n) => ({ + model_node: { ...n, columns: mergeColumns(n.columns, fromDb[n.uniqueId], dialect) }, + })), + model_dialect: dialect, + session_id: `${deps.sessionId}-${hash}`, }) signal.throwIfAborted() return remapSqlLineage( @@ -203,7 +224,7 @@ export async function buildSqlGraph( ) } -/** The graph as a list in dependency order: each node, its inputs, its column count. */ +/** The graph as a list, one line per node: its type, its column count and its inputs. */ export function formatSqlGraph(model: string, graph: SqlGraph): string { const inputs = new Map() for (const [from, to] of graph.edges) inputs.set(to, [...(inputs.get(to) ?? []), from]) @@ -241,6 +262,7 @@ export const DbtSqlGraphTool = Tool.define("dbt_sql_graph", { fetchLineage: sqlLineage, readFile: (f) => fs.readFileSync(f, "utf8"), projectRoot: await getDbtProjectRoot(), + sessionId: ctx.sessionID, }, ctx.abort, ) diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts index 832d80b928..79f10fa373 100644 --- a/packages/opencode/test/altimate/dbt-sql-graph.test.ts +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -1,7 +1,7 @@ // altimate_change - new file // // dbt_sql_graph: the request built from the compiled model and its parents' columns, and the -// remap of the backend's reply (`__final_select__` to the model, relation nodes to dbt unique ids). +// remap of the backend's reply (`__final_select__` to the model's node, relation nodes to dbt unique ids). // The adapter and the endpoint are fakes, so no dbt, warehouse or backend is involved. import { describe, expect, test } from "bun:test" import { @@ -96,18 +96,68 @@ const signal = new AbortController().signal describe("mergeColumns", () => { test("adds warehouse columns the manifest lacks, lower-cased, and fills a missing type", () => { - const merged = mergeColumns({ id: col("id") }, [ - { column: "ID", dtype: "INT" }, - { column: "NEW_ONE", dtype: "TEXT" }, - ]) + const merged = mergeColumns( + { id: col("id") }, + [ + { column: "ID", dtype: "INT" }, + { column: "NEW_ONE", dtype: "TEXT" }, + ], + "snowflake", + ) expect(Object.keys(merged)).toEqual(["id", "new_one"]) expect(merged.id.data_type).toBe("int") expect(merged.new_one).toEqual({ name: "new_one", data_type: "text", description: "", meta: {} }) }) test("keeps a quoted name as written and tolerates no warehouse answer", () => { - expect(Object.keys(mergeColumns({}, [{ column: '"MixedCase"', dtype: "int" }]))).toEqual(['"MixedCase"']) - expect(mergeColumns({ a: col("a", "int") }, undefined).a.data_type).toBe("int") + expect(Object.keys(mergeColumns({}, [{ column: '"MixedCase"', dtype: "int" }], "snowflake"))).toEqual([ + '"MixedCase"', + ]) + expect(mergeColumns({ a: col("a", "int") }, undefined, "snowflake").a.data_type).toBe("int") + }) + + test("a name outside the adapter's folding case stays distinct: Postgres Foo and foo, Snowflake Foo and FOO", () => { + const pg = mergeColumns( + {}, + [ + { column: "Foo", dtype: "int" }, + { column: "foo", dtype: "text" }, + ], + "postgres", + ) + expect(Object.keys(pg)).toEqual(["Foo", "foo"]) + expect(pg.Foo.data_type).toBe("int") + expect(pg.foo.data_type).toBe("text") + const sf = mergeColumns( + {}, + [ + { column: "Foo", dtype: "int" }, + { column: "FOO", dtype: "text" }, + ], + "snowflake", + ) + expect(Object.keys(sf)).toEqual(["Foo", "foo"]) + }) + + test("a manifest column and the warehouse's name for it meet under one key in the adapter's case", () => { + const merged = mergeColumns({ id: col("id") }, [{ column: "id", dtype: "BIGINT" }], "postgres") + expect(Object.keys(merged)).toEqual(["id"]) + expect(merged.id.data_type).toBe("bigint") + }) + + test("columns named like Object members are columns, and nothing is written to the built-ins", () => { + const merged = mergeColumns( + {}, + [ + { column: "constructor", dtype: "text" }, + { column: "__proto__", dtype: "int" }, + ], + "postgres", + ) + expect(Object.keys(merged)).toEqual(["constructor", "__proto__"]) + expect(Object.getOwnPropertyDescriptor(merged, "constructor")?.value.data_type).toBe("text") + expect((Object.prototype as any).data_type).toBeUndefined() + expect((Object as any).data_type).toBeUndefined() }) }) @@ -145,6 +195,63 @@ describe("remapSqlLineage", () => { expect(graph).not.toHaveProperty("nodePositions") }) + test("a CTE or table named like the model keeps its node; the model's own select is told apart", () => { + const collide: SqlLineageResponse = { + tableEdges: [ + ["stg_customers", "orders"], + ["orders", "__final_select__"], + ], + details: { + stg_customers: { name: "stg_customers", type: "table", sql: "", columns: [] }, + orders: { name: "orders", type: "cte", sql: "select * from stg_customers", columns: [{ name: "id" }] }, + __final_select__: { name: "__final_select__", type: "final", sql: "select * from orders", columns: [] }, + }, + } + const out = remapSqlLineage(collide, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, []) + expect(out.edges).toEqual([ + ["stg_customers", "orders"], + ["orders", "orders (final select)"], + ]) + expect(out.nodes).toEqual([ + { name: "stg_customers", type: "table", sql: "", columns: [] }, + { name: "orders", type: "cte", sql: "select * from stg_customers", columns: [{ name: "id" }] }, + { + name: "orders (final select)", + type: "model", + nodeId: "model.shop.orders", + sql: "select * from orders", + columns: [], + }, + ]) + }) + + test("a source table named like the model stays the source's node; a model named like an Object member is not renamed", () => { + const sameName: SqlLineageResponse = { + tableEdges: [["orders", "__final_select__"]], + details: { + orders: { name: "orders", type: "table", sql: "", columns: [] }, + __final_select__: { name: "__final_select__", type: "final", sql: "select * from orders", columns: [] }, + }, + } + const parentsOf = [{ uniqueId: "source.shop.raw.orders", sourceTable: "orders", resourceType: "source" }] + const out = remapSqlLineage( + sameName, + { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, + parentsOf, + ) + expect(out.nodes.map((n) => [n.name, n.nodeId])).toEqual([ + ["orders", "source.shop.raw.orders"], + ["orders (final select)", "model.shop.orders"], + ]) + const member = remapSqlLineage( + { tableEdges: [["x", "__final_select__"]], details: { __final_select__: sameName.details.__final_select__ } }, + { name: "constructor", uniqueId: "model.shop.constructor", resourceType: "model" }, + [], + ) + expect(member.nodes.map((n) => n.name)).toEqual(["constructor"]) + expect(member.edges).toEqual([["x", "constructor"]]) + }) + test("does not mutate the backend reply", () => { expect(reply.details.__final_select__).toBeDefined() expect(reply.tableEdges[1]).toEqual(["o", "__final_select__"]) @@ -167,6 +274,7 @@ describe("buildSqlGraph", () => { }, readFile: read, projectRoot: "/proj", + sessionId: "ses_1", }, signal, ) @@ -176,11 +284,37 @@ describe("buildSqlGraph", () => { ]) expect(sent?.model_dialect).toBe("snowflake") expect(sent?.compiled_sql).toContain("join stg_customers") - expect(sent?.session_id).toMatch(/^altimate-code-[0-9a-f]{32}$/) - const infos = Object.fromEntries(sent!.model_info.map((i) => [i.model_node.uniqueId, i.model_node])) - expect(Object.keys(infos["source.shop.raw.raw_orders"].columns)).toEqual(["id", "amount"]) - expect(infos["source.shop.raw.raw_orders"].alias).toBe("RAW_ORDERS_T") - expect(Object.keys(infos["model.shop.stg_customers"].columns)).toEqual(["id", "name"]) + expect(sent?.session_id).toMatch(/^ses_1-[0-9a-f]{32}$/) + expect(sent?.model_info).toEqual([ + { + model_node: { + database: "db", + schema: "raw", + name: "raw_orders", + alias: "RAW_ORDERS_T", + uniqueId: "source.shop.raw.raw_orders", + columns: { + id: { name: "id", data_type: "int", description: "", meta: {} }, + amount: { name: "amount", data_type: "number", description: "", meta: {} }, + }, + path: undefined, + }, + }, + { + model_node: { + database: "db", + schema: "analytics", + name: "stg_customers", + alias: "stg_customers", + uniqueId: "model.shop.stg_customers", + columns: { + id: { name: "id", description: "", data_type: "int", meta: {} }, + name: { name: "name", data_type: "text", description: "", meta: {} }, + }, + path: "/proj/models/stg_customers.sql", + }, + }, + ]) expect(graph.edges).toContainEqual(["o", "orders"]) expect(graph.nodes.find((n) => n.name === "orders")?.nodeId).toBe("model.shop.orders") expect(graph.nodes.find((n) => n.name === "raw_orders")?.nodeId).toBe("source.shop.raw.raw_orders") @@ -202,7 +336,12 @@ describe("buildSqlGraph", () => { await buildSqlGraph( a, "model.shop.orders", - { fetchLineage: async () => reply, readFile: (f) => ((read = f), "select 1"), projectRoot: "/proj" }, + { + fetchLineage: async () => reply, + readFile: (f) => ((read = f), "select 1"), + projectRoot: "/proj", + sessionId: "ses_1", + }, signal, ) expect(read).toBe("/proj/models/orders.sql") @@ -211,7 +350,7 @@ describe("buildSqlGraph", () => { }) test("an unknown model, an uncompilable model and a missing manifest each fail with a reason", async () => { - const deps = { fetchLineage: async () => reply, readFile: read, projectRoot: "/proj" } + const deps = { fetchLineage: async () => reply, readFile: read, projectRoot: "/proj", sessionId: "ses_1" } await expect(buildSqlGraph(adapter(), "nope", deps, signal)).rejects.toThrow("no model named nope") await expect( buildSqlGraph(adapter({ unsafeCompileQuery: async () => undefined }), "orders", deps, signal), @@ -221,6 +360,58 @@ describe("buildSqlGraph", () => { ).rejects.toThrow("manifest is not available") }) + test("a seed, a snapshot or a model with no .sql file is refused before anything is compiled", async () => { + const deps = { fetchLineage: async () => reply, readFile: read, projectRoot: "/proj", sessionId: "ses_1" } + for (const odd of [ + { ...orders, name: "countries", resource_type: "seed", path: "seeds/countries.csv" }, + { ...orders, name: "snap", resource_type: "snapshot", path: "snapshots/snap.sql" }, + { ...orders, name: "py_model", path: "models/py_model.py" }, + ]) { + const a = adapter({ + parseManifest: async () => ({ + nodeMetaMap: { lookupByUniqueId: () => undefined, lookupByBaseName: () => odd }, + sourceMetaMap: { get: () => undefined }, + }), + }) + await expect(buildSqlGraph(a, odd.name, deps, signal)).rejects.toThrow(`${odd.name} is not a dbt model`) + expect(a.calls).toEqual([]) + } + }) + + test("a cancel while the manifest parses or the model compiles starts no further work", async () => { + const deps = { fetchLineage: async () => reply, readFile: read, projectRoot: "/proj", sessionId: "ses_1" } + const duringParse = new AbortController() + const a = adapter() + const parse = a.parseManifest + a.parseManifest = async () => { + const manifest = await parse() + duringParse.abort() + return manifest + } + await expect(buildSqlGraph(a, "orders", deps, duringParse.signal)).rejects.toThrow() + expect(a.calls).toEqual([]) + + const duringCompile = new AbortController() + const b = adapter({ + unsafeCompileQuery: async () => { + duringCompile.abort() + return "select 1" + }, + }) + await expect(buildSqlGraph(b, "orders", deps, duringCompile.signal)).rejects.toThrow() + expect(b.calls).toEqual([]) + }) + + test("two sessions asking for the same SQL send different session ids", async () => { + const ids: string[] = [] + const fetchLineage = async (req: SqlLineageRequest) => (ids.push(req.session_id), reply) + const base = { fetchLineage, readFile: read, projectRoot: "/proj" } + await buildSqlGraph(adapter(), "orders", { ...base, sessionId: "ses_a" }, signal) + await buildSqlGraph(adapter(), "orders", { ...base, sessionId: "ses_b" }, signal) + expect(ids[0]).not.toBe(ids[1]) + expect(ids[0]?.split("-").slice(-1)[0]).toBe(ids[1]?.split("-").slice(-1)[0]) + }) + test("a backend failure propagates and sends nothing after a cancel", async () => { const deps = { fetchLineage: async () => { @@ -228,6 +419,7 @@ describe("buildSqlGraph", () => { }, readFile: read, projectRoot: "/proj", + sessionId: "ses_1", } await expect(buildSqlGraph(adapter(), "orders", deps, signal)).rejects.toThrow("status 500") const aborted = AbortSignal.abort() From 4a6dc2097e2c4660e11fa471f183ab312595d808 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 21:51:54 +0530 Subject: [PATCH 06/27] =?UTF-8?q?fix:=20dbt=5Fsql=5Fgraph=20=E2=80=94=20no?= =?UTF-8?q?de=20ids=20apart=20from=20names,=20relation=20mapping=20by=20sc?= =?UTF-8?q?hema=20and=20database,=20operation=20SQL,=20cancel=20during=20t?= =?UTF-8?q?he=20backend=20call,=20refusal=20as=20failure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- .../opencode/src/altimate/api/sql-lineage.ts | 14 +- .../src/altimate/tools/dbt-sql-graph.ts | 99 +++++-- .../test/altimate/dbt-sql-graph.test.ts | 277 +++++++++++++++++- 3 files changed, 353 insertions(+), 37 deletions(-) diff --git a/packages/opencode/src/altimate/api/sql-lineage.ts b/packages/opencode/src/altimate/api/sql-lineage.ts index 4ad48b5c62..89f6c55073 100644 --- a/packages/opencode/src/altimate/api/sql-lineage.ts +++ b/packages/opencode/src/altimate/api/sql-lineage.ts @@ -18,13 +18,21 @@ export type SqlLineageRequest = { session_id: string } +/** + * One node of the backend's graph, keyed in `details` by an id that edges use. A CTE or the final select carries `sql`; a relation, join, filter, group, sort or + * subquery carries `expression`. A relation, subquery, CTE and the final select have a `name`; a join, filter, group or sort has none. + */ export type SqlLineageNodeDetails = { - name: string + name?: string type: string nodeType?: string nodeId?: string - sql: string - columns: { name: string; datatype?: string; expression?: string }[] + sql?: string + expression?: string + /** A relation's warehouse database (catalog) and schema. */ + db?: string + schema?: string + columns?: { name: string; datatype?: string; expression?: string }[] } export type SqlLineageResponse = { diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index bd81673e35..744c3c1881 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -14,6 +14,9 @@ import { dbtAdapterFor } from "./dbt-adapter-access" const FINAL_SELECT = "__final_select__" export type SqlGraphNode = { + /** Unique in the graph; the edges name nodes by it. */ + id: string + /** What the node is called on screen; two nodes can share one (a relation used twice). */ name: string /** The backend's node type ("cte", "table", ...); a relation that maps to a dbt node carries its resource type ("model", "source", ...). */ type: string @@ -112,18 +115,35 @@ export function mergeColumns( export function remapSqlLineage( response: SqlLineageResponse, model: { name: string; uniqueId: string; resourceType: string }, - parents: { uniqueId: string; alias?: string; sourceTable?: string; resourceType: string }[], + parents: { + uniqueId: string + alias?: string + sourceTable?: string + schema?: string + database?: string + resourceType: string + }[], ): SqlGraph { const details = { ...response.details } const mapping: Record = {} + // A relation that more than one parent could be is tied to none: an arbitrary dbt node would be shown as the real one. + const claimed = new Map>() for (const p of parents) { - // A source's relation is its table name; any other node's, its alias. + // A source's relation is its table's identifier; any other node's, its alias. const wanted = (p.sourceTable ?? p.alias ?? "").toLowerCase() if (!wanted) continue - const key = Object.keys(details).find((k) => details[k].type === "table" && k.toLowerCase() === wanted) - if (key) mapping[key] = { nodeId: p.uniqueId, type: p.resourceType } + // The backend keys a relation by its location and a per-occurrence suffix, so the match is on its name, in the parent's database and schema when both say one; + // every occurrence of the relation in the SQL stands for the same dbt node. + for (const [k, d] of Object.entries(details)) { + if (d.type !== "table" || (d.name ?? k).toLowerCase() !== wanted) continue + if (d.schema && p.schema && d.schema.toLowerCase() !== p.schema.toLowerCase()) continue + if (d.db && p.database && d.db.toLowerCase() !== p.database.toLowerCase()) continue + claimed.set(k, (claimed.get(k) ?? new Set()).add(p.uniqueId)) + mapping[k] = { nodeId: p.uniqueId, type: p.resourceType } + } } - // A CTE or relation the SQL names like the model (`with customers as (…)`, `{{ this }}`) keeps its node: the model's own select is told apart. + for (const [k, owners] of claimed) if (owners.size > 1) delete mapping[k] + // A CTE the SQL names like the model (`with customers as (…)`) keeps its node: the model's own select is told apart. const finalKey = Object.hasOwn(details, model.name) ? `${model.name} (final select)` : model.name mapping[finalKey] = { nodeId: model.uniqueId, type: model.resourceType } const edges = response.tableEdges.map( @@ -136,16 +156,31 @@ export function remapSqlLineage( const nodes: SqlGraphNode[] = Object.entries(details).map(([key, d]) => { const merged = { ...d, ...mapping[key] } return { + // The key is the node's identity (edges use it); the name is only what is shown. + id: key, name: key === finalKey ? finalKey : (merged.name ?? key), type: merged.type, ...(merged.nodeId ? { nodeId: merged.nodeId } : {}), - sql: merged.sql ?? "", + sql: merged.sql ?? merged.expression ?? "", columns: merged.columns ?? [], } }) return { nodes, edges } } +/** The promise's result, or an abort as soon as the signal fires: the backend call itself is bounded by the API client's own timeout. */ +function untilAborted(work: Promise, signal: AbortSignal): Promise { + if (signal.aborted) { + work.catch(() => {}) + return Promise.reject(signal.reason) + } + return new Promise((resolve, reject) => { + const onAbort = () => reject(signal.reason) + signal.addEventListener("abort", onAbort, { once: true }) + work.then(resolve, reject).finally(() => signal.removeEventListener("abort", onAbort)) + }) +} + /** Compile the model, read its parents' columns, ask the backend, and return the remapped graph. */ export async function buildSqlGraph( adapter: SqlGraphAdapter, @@ -169,7 +204,14 @@ export async function buildSqlGraph( const parentIds = adapter.getNonEphemeralParents([node.unique_id]) const requests: { unique_id: string; name: string; resource_type: string; table?: string }[] = [] const infos: SqlLineageModelNode[] = [] - const parents: { uniqueId: string; alias?: string; sourceTable?: string; resourceType: string }[] = [] + const parents: { + uniqueId: string + alias?: string + sourceTable?: string + schema?: string + database?: string + resourceType: string + }[] = [] for (const id of parentIds) { const parts = id.split(".") if (parts[0] === "source") { @@ -186,7 +228,13 @@ export async function buildSqlGraph( columns: table.columns, path: table.path, }) - parents.push({ uniqueId: id, sourceTable: table.name, resourceType: "source" }) + parents.push({ + uniqueId: id, + sourceTable: table.identifier || table.name, + schema: source.schema, + database: source.database, + resourceType: "source", + }) continue } const parent = manifest.nodeMetaMap.lookupByUniqueId(id) @@ -201,22 +249,32 @@ export async function buildSqlGraph( columns: parent.columns, path: parent.path, }) - parents.push({ uniqueId: id, alias: parent.alias, resourceType: parent.resource_type }) + parents.push({ + uniqueId: id, + alias: parent.alias, + schema: parent.schema, + database: parent.database, + resourceType: parent.resource_type, + }) } // The warehouse metadata read: once per call, only for the parents. const fromDb = requests.length ? await adapter.getBulkSchemaFromDB(requests, signal) : {} signal.throwIfAborted() const hash = crypto.createHash("md5").update(compiled).digest("hex") const dialect = adapter.getAdapterType() - const response = await deps.fetchLineage({ - compiled_sql: compiled, - model_info: infos.map((n) => ({ - model_node: { ...n, columns: mergeColumns(n.columns, fromDb[n.uniqueId], dialect) }, - })), - model_dialect: dialect, - session_id: `${deps.sessionId}-${hash}`, - }) + const response = await untilAborted( + deps.fetchLineage({ + compiled_sql: compiled, + model_info: infos.map((n) => ({ + model_node: { ...n, columns: mergeColumns(n.columns, fromDb[n.uniqueId], dialect) }, + })), + model_dialect: dialect, + session_id: `${deps.sessionId}-${hash}`, + }), + signal, + ) signal.throwIfAborted() + if (!Array.isArray(response?.tableEdges) || !response.details) throw new Error("the backend returned no SQL graph") return remapSqlLineage( response, { name: node.name, uniqueId: node.unique_id, resourceType: node.resource_type }, @@ -227,10 +285,11 @@ export async function buildSqlGraph( /** The graph as a list, one line per node: its type, its column count and its inputs. */ export function formatSqlGraph(model: string, graph: SqlGraph): string { const inputs = new Map() - for (const [from, to] of graph.edges) inputs.set(to, [...(inputs.get(to) ?? []), from]) + const nameOf = new Map(graph.nodes.map((n) => [n.id, n.name])) + for (const [from, to] of graph.edges) inputs.set(to, [...(inputs.get(to) ?? []), nameOf.get(from) ?? from]) const lines = [`SQL graph for ${model}: ${graph.nodes.length} nodes, ${graph.edges.length} edges`] for (const n of graph.nodes) { - const from = inputs.get(n.name) + const from = inputs.get(n.id) lines.push( `- ${n.name} (${n.type}${n.nodeId ? `, ${n.nodeId}` : ""}): ${n.columns.length} columns${from?.length ? `, from ${from.join(", ")}` : ""}`, ) @@ -249,7 +308,7 @@ export const DbtSqlGraphTool = Tool.define("dbt_sql_graph", { if (!access.adapter) { return { title: "dbt SQL graph: not available", - metadata: { success: false, model: args.model, reason: access.reason }, + metadata: { success: false, model: args.model, reason: access.reason, error: access.reason }, output: `Not available: ${access.reason}`, } } diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts index 79f10fa373..bc8973487d 100644 --- a/packages/opencode/test/altimate/dbt-sql-graph.test.ts +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -3,12 +3,22 @@ // dbt_sql_graph: the request built from the compiled model and its parents' columns, and the // remap of the backend's reply (`__final_select__` to the model's node, relation nodes to dbt unique ids). // The adapter and the endpoint are fakes, so no dbt, warehouse or backend is involved. -import { describe, expect, test } from "bun:test" +import { afterEach, 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 { SessionID, MessageID } from "@/session/schema" +import type { Tool } from "@/tool/tool" +import { dbtAdapterInternals, resetDbtAdapter } from "@/altimate/native/connections/register" +import { initTool } from "./tool-fixture" import { buildSqlGraph, formatSqlGraph, mergeColumns, remapSqlLineage, + DbtSqlGraphTool, type SqlGraphAdapter, } from "@/altimate/tools/dbt-sql-graph" import type { SqlLineageRequest, SqlLineageResponse } from "@/altimate/api/sql-lineage" @@ -84,7 +94,7 @@ const reply: SqlLineageResponse = { ["stg_customers", "__final_select__"], ], details: { - raw_orders: { name: "raw_orders", type: "table", sql: "", columns: [{ name: "id" }, { name: "amount" }] }, + raw_orders: { name: "RAW_ORDERS_T", type: "table", columns: [{ name: "id" }, { name: "amount" }] }, o: { name: "o", type: "cte", sql: "select * from raw.orders", columns: [{ name: "id" }] }, stg_customers: { name: "stg_customers", type: "table", sql: "", columns: [{ name: "id" }, { name: "name" }] }, __final_select__: { name: "__final_select__", type: "final", sql: "select * from o", columns: [{ name: "id" }] }, @@ -163,7 +173,7 @@ describe("mergeColumns", () => { describe("remapSqlLineage", () => { const parents = [ - { uniqueId: "source.shop.raw.raw_orders", sourceTable: "raw_orders", resourceType: "source" }, + { uniqueId: "source.shop.raw.raw_orders", sourceTable: "RAW_ORDERS_T", resourceType: "source" }, { uniqueId: "model.shop.stg_customers", alias: "STG_CUSTOMERS", resourceType: "model" }, ] const graph = remapSqlLineage( @@ -178,20 +188,41 @@ describe("remapSqlLineage", () => { ["o", "orders"], ["stg_customers", "orders"], ]) - expect(graph.nodes.map((n) => n.name)).toEqual(["raw_orders", "o", "stg_customers", "orders"]) + expect(graph.nodes.map((n) => n.id)).toEqual(["raw_orders", "o", "stg_customers", "orders"]) expect(JSON.stringify(graph)).not.toContain("__final_select__") }) test("ties table nodes to dbt unique ids, case-insensitively, and the model to itself", () => { - const byName = Object.fromEntries(graph.nodes.map((n) => [n.name, n])) - expect(byName.raw_orders).toMatchObject({ nodeId: "source.shop.raw.raw_orders", type: "source" }) - expect(byName.stg_customers).toMatchObject({ nodeId: "model.shop.stg_customers", type: "model" }) - expect(byName.orders).toMatchObject({ nodeId: "model.shop.orders", type: "model", sql: "select * from o" }) + const byId = Object.fromEntries(graph.nodes.map((n) => [n.id, n])) + expect(byId.raw_orders).toEqual({ + id: "raw_orders", + name: "RAW_ORDERS_T", + type: "source", + nodeId: "source.shop.raw.raw_orders", + sql: "", + columns: [{ name: "id" }, { name: "amount" }], + }) + expect(byId.stg_customers).toEqual({ + id: "stg_customers", + name: "stg_customers", + type: "model", + nodeId: "model.shop.stg_customers", + sql: "", + columns: [{ name: "id" }, { name: "name" }], + }) + expect(byId.orders).toEqual({ + id: "orders", + name: "orders", + type: "model", + nodeId: "model.shop.orders", + sql: "select * from o", + columns: [{ name: "id" }], + }) }) test("leaves CTEs unmapped and drops the layout positions", () => { - const cte = graph.nodes.find((n) => n.name === "o")! - expect(cte).toEqual({ name: "o", type: "cte", sql: "select * from raw.orders", columns: [{ name: "id" }] }) + const cte = graph.nodes.find((n) => n.id === "o")! + expect(cte).toEqual({ id: "o", name: "o", type: "cte", sql: "select * from raw.orders", columns: [{ name: "id" }] }) expect(graph).not.toHaveProperty("nodePositions") }) @@ -213,9 +244,10 @@ describe("remapSqlLineage", () => { ["orders", "orders (final select)"], ]) expect(out.nodes).toEqual([ - { name: "stg_customers", type: "table", sql: "", columns: [] }, - { name: "orders", type: "cte", sql: "select * from stg_customers", columns: [{ name: "id" }] }, + { id: "stg_customers", name: "stg_customers", type: "table", sql: "", columns: [] }, + { id: "orders", name: "orders", type: "cte", sql: "select * from stg_customers", columns: [{ name: "id" }] }, { + id: "orders (final select)", name: "orders (final select)", type: "model", nodeId: "model.shop.orders", @@ -317,7 +349,7 @@ describe("buildSqlGraph", () => { ]) expect(graph.edges).toContainEqual(["o", "orders"]) expect(graph.nodes.find((n) => n.name === "orders")?.nodeId).toBe("model.shop.orders") - expect(graph.nodes.find((n) => n.name === "raw_orders")?.nodeId).toBe("source.shop.raw.raw_orders") + expect(graph.nodes.find((n) => n.id === "raw_orders")?.nodeId).toBe("source.shop.raw.raw_orders") }) test("accepts a unique_id and resolves a project-relative path against the project root", async () => { @@ -437,6 +469,223 @@ describe("formatSqlGraph", () => { const text = formatSqlGraph("orders", graph) expect(text).toContain("4 nodes, 3 edges") expect(text).toContain("- orders (model, model.shop.orders): 1 columns, from o, stg_customers") - expect(text).toContain("- o (cte): 1 columns, from raw_orders") + expect(text).toContain("- o (cte): 1 columns, from RAW_ORDERS_T") + }) +}) + +describe("the backend's real node shapes", () => { + // Relations are keyed by location and a per-occurrence suffix, and carry `name`, `db` and `schema`; joins, filters and the like have no name and carry + // `expression` instead of `sql`. + const real: SqlLineageResponse = { + tableEdges: [ + ["analytics.customers_111", "LEFT_JOIN_9"], + ["raw.customers_222", "LEFT_JOIN_9"], + ["LEFT_JOIN_9", "__final_select__"], + ["analytics.customers_111", "WHERE_7"], + ["WHERE_7", "__final_select__"], + ], + details: { + "analytics.customers_111": { + name: "customers", + type: "table", + expression: "analytics.customers", + db: "db", + schema: "analytics", + columns: [], + }, + "raw.customers_222": { + name: "customers", + type: "table", + expression: "raw.customers", + db: "db", + schema: "raw", + columns: [], + }, + LEFT_JOIN_9: { type: "LEFT_JOIN", expression: "LEFT JOIN raw.customers ON 1 = 1", columns: [] }, + WHERE_7: { type: "FILTER", expression: "WHERE amount > 0" }, + __final_select__: { name: "__final_select__", type: "final", sql: "select 1", columns: [{ name: "id" }] }, + }, + } + const graph = remapSqlLineage(real, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, [ + { uniqueId: "model.shop.customers", alias: "customers", schema: "analytics", resourceType: "model" }, + { uniqueId: "source.shop.raw.customers", sourceTable: "customers", schema: "raw", resourceType: "source" }, + ]) + + test("every edge ends at a node id, though two relations share a name", () => { + const ids = graph.nodes.map((n) => n.id) + expect(new Set(ids).size).toBe(ids.length) + for (const edge of graph.edges) for (const end of edge) expect(ids).toContain(end) + expect(graph.nodes.filter((n) => n.name === "customers")).toHaveLength(2) + }) + + test("a relation is tied to the dbt node in its own schema, and a join or filter keeps its SQL", () => { + const byId = Object.fromEntries(graph.nodes.map((n) => [n.id, n])) + expect(byId["analytics.customers_111"]).toMatchObject({ + nodeId: "model.shop.customers", + type: "model", + sql: "analytics.customers", + }) + expect(byId["raw.customers_222"]).toMatchObject({ nodeId: "source.shop.raw.customers", type: "source" }) + expect(byId.LEFT_JOIN_9).toEqual({ + id: "LEFT_JOIN_9", + name: "LEFT_JOIN_9", + type: "LEFT_JOIN", + sql: "LEFT JOIN raw.customers ON 1 = 1", + columns: [], + }) + expect(byId.WHERE_7.sql).toBe("WHERE amount > 0") + }) + + test("two relations of one name and schema in different databases are told apart by the database", () => { + const twoDbs: SqlLineageResponse = { + tableEdges: [], + details: { + "a.raw.customers_1": { name: "customers", type: "table", db: "a", schema: "raw" }, + "b.raw.customers_2": { name: "customers", type: "table", db: "b", schema: "raw" }, + }, + } + const parentsIn = (first: string, second: string) => [ + { + uniqueId: `source.p.${first}.customers`, + sourceTable: "customers", + schema: "raw", + database: first, + resourceType: "source", + }, + { + uniqueId: `source.p.${second}.customers`, + sourceTable: "customers", + schema: "raw", + database: second, + resourceType: "source", + }, + ] + const model = { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" } + for (const order of [parentsIn("a", "b"), parentsIn("b", "a")]) { + const out = remapSqlLineage(twoDbs, model, order) + expect(out.nodes.map((n) => n.nodeId)).toEqual(["source.p.a.customers", "source.p.b.customers"]) + } + }) + + test("a relation two parents could both be is tied to neither, whatever the parent order", () => { + const ambiguous: SqlLineageResponse = { + tableEdges: [], + details: { "raw.customers_1": { name: "customers", type: "table", schema: "raw" } }, + } + const model = { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" } + const a = { uniqueId: "source.p.raw.customers", sourceTable: "customers", schema: "raw", resourceType: "source" } + const b = { uniqueId: "model.p.customers", alias: "customers", schema: "raw", resourceType: "model" } + for (const order of [ + [a, b], + [b, a], + ]) { + const out = remapSqlLineage(ambiguous, model, order) + expect(out.nodes).toEqual([{ id: "raw.customers_1", name: "customers", type: "table", sql: "", columns: [] }]) + } + }) + + test("every occurrence of one relation in the SQL is tied to its dbt node", () => { + const twice: SqlLineageResponse = { + tableEdges: [], + details: { + "analytics.customers_1": { name: "customers", type: "table", schema: "analytics" }, + "analytics.customers_2": { name: "customers", type: "table", schema: "analytics" }, + }, + } + const out = remapSqlLineage(twice, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, [ + { uniqueId: "model.shop.customers", alias: "customers", schema: "analytics", resourceType: "model" }, + ]) + expect(out.nodes.map((n) => n.nodeId)).toEqual(["model.shop.customers", "model.shop.customers"]) + }) +}) + +describe("a cancel while the backend is answering", () => { + test("ends the call at once, and a late answer is ignored", async () => { + const controller = new AbortController() + let release!: (r: SqlLineageResponse) => void + const slow = new Promise((resolve) => (release = resolve)) + const call = buildSqlGraph( + adapter(), + "orders", + { fetchLineage: () => slow, readFile: (f) => f, projectRoot: "/proj", sessionId: "ses_1" }, + controller.signal, + ) + const settled = call.then( + () => "answered", + () => "aborted", + ) + await Bun.sleep(10) + controller.abort() + expect(await settled).toBe("aborted") + release(reply) + }) + + test("a reply with no graph is a clear failure, not a crash", async () => { + const deps = { + fetchLineage: async () => ({}) as SqlLineageResponse, + readFile: (f: string) => f, + projectRoot: "/proj", + sessionId: "ses_1", + } + await expect(buildSqlGraph(adapter(), "orders", deps, signal)).rejects.toThrow("the backend returned no SQL graph") + }) +}) + +describe("the tool's own answer", () => { + const ctx: Tool.Context = { + sessionID: SessionID.make("ses_sql_graph"), + messageID: MessageID.make("msg_sql_graph"), + agent: "analyst", + abort: new AbortController().signal, + messages: [], + metadata: () => Effect.void, + ask: () => Effect.void, + } + + afterEach(() => { + resetDbtAdapter() + delete dbtAdapterInternals.readConfig + delete dbtAdapterInternals.create + }) + + test("a read-only call is a failure the CLI reports as one: success false and an error", async () => { + const tool = await initTool(DbtSqlGraphTool as any) + const result = await tool.execute({ model: "orders" }, { ...ctx, extra: { forceReadOnly: true } }) + expect(result.metadata).toEqual({ + success: false, + model: "orders", + reason: expect.stringContaining("--allow-write"), + error: expect.stringContaining("--allow-write"), + truncated: false, + }) + expect(result.output).toContain("Not available") + }) + + test("a model the manifest lacks is a failure with the reason", async () => { + const project = realpathSync(await mkdtemp(path.join(os.tmpdir(), "dbt-sql-graph-"))) + await writeFile(path.join(project, "dbt_project.yml"), "name: shop\n") + dbtAdapterInternals.readConfig = async () => ({ + projectRoot: project, + pythonPath: "python3", + dbtIntegration: "corecommand", + queryLimit: 500, + }) + dbtAdapterInternals.create = async () => ({ + isInitialized: () => true, + dispose: async () => {}, + parseManifest: async () => ({ + nodeMetaMap: { lookupByUniqueId: () => undefined, lookupByBaseName: () => undefined }, + sourceMetaMap: { get: () => undefined }, + }), + }) + const tool = await initTool(DbtSqlGraphTool as any) + const result = await tool + .execute({ model: "nope" }, ctx) + .finally(() => rm(project, { recursive: true, force: true })) + expect(result.metadata).toMatchObject({ + success: false, + model: "nope", + error: "no model named nope in the manifest", + }) }) }) From 5bab9574c887a62fdfd719bf290b2a86f8ac2dea Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sun, 15 Nov 2026 22:11:55 +0530 Subject: [PATCH 07/27] =?UTF-8?q?fix:=20dbt=5Fsql=5Fgraph=20=E2=80=94=20Re?= =?UTF-8?q?dshift=20folds=20to=20lower=20case,=20a=20cancelled=20read-only?= =?UTF-8?q?=20call=20is=20cancelled=20not=20refused,=20the=20cancel=20test?= =?UTF-8?q?=20waits=20for=20the=20backend=20call?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-adapter-access.ts | 2 ++ .../src/altimate/tools/dbt-sql-graph.ts | 2 +- .../test/altimate/dbt-adapter-access.test.ts | 9 ++++++ .../test/altimate/dbt-sql-graph.test.ts | 28 +++++++++++++++++-- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts index d73dd7e9c5..2f5d48b924 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 is cancelled, not refused. + signal.throwIfAborted() if (ctx.extra?.forceReadOnly === true || ctx.agent === "plan") return { refusal: "read_only", diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index 744c3c1881..40a39987d2 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -75,7 +75,7 @@ export type SqlGraphDeps = { } /** Adapters whose unquoted identifiers fold to lower case; the rest fold to upper case. */ -const LOWER_FOLDING = new Set(["trino", "athena", "postgres", "duckdb", "risingwave"]) +const LOWER_FOLDING = new Set(["trino", "athena", "postgres", "redshift", "duckdb", "risingwave"]) /** A name the warehouse would only keep as written if it were quoted: it is not in the adapter's own folding case. */ function isQuotedIdentifier(name: string, adapter: string): boolean { diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index 31bbb80a06..ca7b0adb96 100644 --- a/packages/opencode/test/altimate/dbt-adapter-access.test.ts +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -114,6 +114,15 @@ describe("dbtAdapterFor", () => { expect(created).toBe(0) }) + test("a read-only call cancelled beforehand is cancelled, not refused", async () => { + const controller = new AbortController() + controller.abort() + await expect( + dbtAdapterFor({ ...ctx, abort: controller.signal, extra: { forceReadOnly: true } }), + ).rejects.toThrow() + expect(created).toBe(0) + }) + test("refuses a plan-mode call", async () => { const access = await dbtAdapterFor({ ...ctx, agent: "plan" }) expect(access.refusal).toBe("read_only") diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts index bc8973487d..8717cae54b 100644 --- a/packages/opencode/test/altimate/dbt-sql-graph.test.ts +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -149,6 +149,20 @@ describe("mergeColumns", () => { expect(Object.keys(sf)).toEqual(["Foo", "foo"]) }) + test("Redshift folds to lower case like Postgres: FOO and foo stay distinct columns", () => { + const rs = mergeColumns( + {}, + [ + { column: "FOO", dtype: "int" }, + { column: "foo", dtype: "text" }, + ], + "redshift", + ) + expect(Object.keys(rs)).toEqual(["FOO", "foo"]) + expect(rs.FOO.data_type).toBe("int") + expect(rs.foo.data_type).toBe("text") + }) + test("a manifest column and the warehouse's name for it meet under one key in the adapter's case", () => { const merged = mergeColumns({ id: col("id") }, [{ column: "id", dtype: "BIGINT" }], "postgres") expect(Object.keys(merged)).toEqual(["id"]) @@ -603,18 +617,28 @@ describe("a cancel while the backend is answering", () => { test("ends the call at once, and a late answer is ignored", async () => { const controller = new AbortController() let release!: (r: SqlLineageResponse) => void + let reached!: () => void + const inBackend = new Promise((resolve) => (reached = resolve)) const slow = new Promise((resolve) => (release = resolve)) const call = buildSqlGraph( adapter(), "orders", - { fetchLineage: () => slow, readFile: (f) => f, projectRoot: "/proj", sessionId: "ses_1" }, + { + fetchLineage: () => { + reached() + return slow + }, + readFile: (f) => f, + projectRoot: "/proj", + sessionId: "ses_1", + }, controller.signal, ) const settled = call.then( () => "answered", () => "aborted", ) - await Bun.sleep(10) + await inBackend controller.abort() expect(await settled).toBe("aborted") release(reply) From d8c34693b09dd856b0efd2c4a1924c7320346720 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sun, 15 Nov 2026 22:27:01 +0530 Subject: [PATCH 08/27] 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 18a3fc4e0bd124e8f5fb3120fdc4d152be88cbe3 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 06:39:46 +0530 Subject: [PATCH 09/27] 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 10/27] 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 11/27] 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 12/27] 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 aaff4148345d9e6735a119d27ea540a3d2929a9d Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 13:21:13 +0530 Subject: [PATCH 13/27] 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 14/27] 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 67f4dae7f0f487b56522120f5caeaeacd0d43732 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 13:49:53 +0530 Subject: [PATCH 15/27] 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 caa4c7c4f0dab2c59939166221a6ed1fc0a45f7d Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 14:08:59 +0530 Subject: [PATCH 16/27] fix: send API requests only to an http(s) URL "localhost:3000" parses as a URL with the scheme "localhost:", so fetch threw an uncoded TypeError that was reported as a bad header value. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/api/client.ts | 7 ++++--- .../test/altimate/dbt-adapter-access.test.ts | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/opencode/src/altimate/api/client.ts b/packages/opencode/src/altimate/api/client.ts index 54d7625e65..4c1b153ca1 100644 --- a/packages/opencode/src/altimate/api/client.ts +++ b/packages/opencode/src/altimate/api/client.ts @@ -352,9 +352,10 @@ 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.`) + // Checked here so that only the header check can make fetch throw a TypeError before sending. Only + // http(s) is sent: "localhost:3000" parses as a URL with the scheme "localhost:". + if (!URL.canParse(url) || !["http:", "https:"].includes(new URL(url).protocol)) + throw new Error(`API ${method} ${endpoint} could not be sent: the stored API URL is not an http(s) 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 diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index 4f2c9eb8d9..4317672e42 100644 --- a/packages/opencode/test/altimate/dbt-adapter-access.test.ts +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -577,8 +577,20 @@ describe("AltimateApi.apiRequest", () => { 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.") + const realFetch = globalThis.fetch + let urlSends = 0 + globalThis.fetch = (async () => (urlSends++, new Response("{}"))) as unknown as typeof fetch + try { + // Not a URL, or a URL whose scheme isn't http(s): "localhost:3000" parses with the scheme "localhost:". + for (const altimateUrl of ["api.example.test", "localhost:3000", "file:///tmp", "ftp://api.example.test"]) { + await Bun.write(file, JSON.stringify({ altimateUrl, 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 an http(s) URL.") + } + expect(urlSends).toBe(0) + } finally { + globalThis.fetch = realFetch + } await Bun.write( file, JSON.stringify({ altimateUrl: "http://127.0.0.1:9", altimateInstanceName: "a", altimateApiKey: "k1" }), From e5cfabea05d1983ab7d85f12788f618b5822dc37 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 14:13:41 +0530 Subject: [PATCH 17/27] fix: forward the tool's abort to the lineage request, reject malformed replies, keep a __proto__ model's final node Co-Authored-By: Claude Sonnet 5.5 --- .../opencode/src/altimate/api/sql-lineage.ts | 4 +- .../src/altimate/tools/dbt-sql-graph.ts | 36 ++++++--- .../test/altimate/dbt-sql-graph.test.ts | 75 +++++++++++++------ 3 files changed, 77 insertions(+), 38 deletions(-) diff --git a/packages/opencode/src/altimate/api/sql-lineage.ts b/packages/opencode/src/altimate/api/sql-lineage.ts index 89f6c55073..8936a86ddc 100644 --- a/packages/opencode/src/altimate/api/sql-lineage.ts +++ b/packages/opencode/src/altimate/api/sql-lineage.ts @@ -44,6 +44,6 @@ export type SqlLineageResponse = { export const SQL_LINEAGE_ENDPOINT = "/dbt/v3/sql_lineage" /** The backend's graph of a compiled model's CTEs, joins and filters. */ -export async function sqlLineage(req: SqlLineageRequest): Promise { - return (await AltimateApi.apiRequest("POST", SQL_LINEAGE_ENDPOINT, req)) as SqlLineageResponse +export async function sqlLineage(req: SqlLineageRequest, signal?: AbortSignal): Promise { + return (await AltimateApi.apiRequest("POST", SQL_LINEAGE_ENDPOINT, req, signal)) as SqlLineageResponse } diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index 40a39987d2..f5b8848575 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -6,6 +6,7 @@ import { Tool } from "../../tool/tool" import { sqlLineage, type SqlLineageModelNode, + type SqlLineageNodeDetails, type SqlLineageRequest, type SqlLineageResponse, } from "../api/sql-lineage" @@ -66,7 +67,7 @@ export type SqlGraphAdapter = { } export type SqlGraphDeps = { - fetchLineage: (req: SqlLineageRequest) => Promise + fetchLineage: (req: SqlLineageRequest, signal?: AbortSignal) => Promise readFile: (file: string) => string /** The dbt project's root, to resolve a model's project-relative path. */ projectRoot: string | undefined @@ -124,8 +125,9 @@ export function remapSqlLineage( resourceType: string }[], ): SqlGraph { - const details = { ...response.details } - const mapping: Record = {} + // Null-prototype maps: a model or relation named `__proto__` is an ordinary key. + const details: Record = Object.assign(Object.create(null), response.details) + const mapping: Record = Object.create(null) // A relation that more than one parent could be is tied to none: an arbitrary dbt node would be shown as the real one. const claimed = new Map>() for (const p of parents) { @@ -263,18 +265,28 @@ export async function buildSqlGraph( const hash = crypto.createHash("md5").update(compiled).digest("hex") const dialect = adapter.getAdapterType() const response = await untilAborted( - deps.fetchLineage({ - compiled_sql: compiled, - model_info: infos.map((n) => ({ - model_node: { ...n, columns: mergeColumns(n.columns, fromDb[n.uniqueId], dialect) }, - })), - model_dialect: dialect, - session_id: `${deps.sessionId}-${hash}`, - }), + deps.fetchLineage( + { + compiled_sql: compiled, + model_info: infos.map((n) => ({ + model_node: { ...n, columns: mergeColumns(n.columns, fromDb[n.uniqueId], dialect) }, + })), + model_dialect: dialect, + session_id: `${deps.sessionId}-${hash}`, + }, + signal, + ), signal, ) signal.throwIfAborted() - if (!Array.isArray(response?.tableEdges) || !response.details) throw new Error("the backend returned no SQL graph") + const isRecord = (v: unknown) => typeof v === "object" && v !== null && !Array.isArray(v) + if ( + !Array.isArray(response?.tableEdges) || + !isRecord(response.details) || + !response.tableEdges.every((e) => Array.isArray(e) && e.length === 2 && e.every((x) => typeof x === "string")) || + !Object.values(response.details).every(isRecord) + ) + throw new Error("the backend returned no SQL graph") return remapSqlLineage( response, { name: node.name, uniqueId: node.unique_id, resourceType: node.resource_type }, diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts index 8717cae54b..6ea1d6fe74 100644 --- a/packages/opencode/test/altimate/dbt-sql-graph.test.ts +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -468,6 +468,19 @@ describe("buildSqlGraph", () => { sessionId: "ses_1", } await expect(buildSqlGraph(adapter(), "orders", deps, signal)).rejects.toThrow("status 500") + // The tool's abort signal reaches the backend request, and a malformed reply is a failure, not an empty graph. + let seen: AbortSignal | undefined + await buildSqlGraph(adapter(), "orders", { ...deps, fetchLineage: async (_req, s) => ((seen = s), reply) }, signal) + expect(seen).toBe(signal) + for (const bad of [ + { ...reply, details: [] }, + { ...reply, details: { a: 1 } }, + { ...reply, tableEdges: [["a"]] }, + ]) { + await expect( + buildSqlGraph(adapter(), "orders", { ...deps, fetchLineage: async () => bad as any }, signal), + ).rejects.toThrow("no SQL graph") + } const aborted = AbortSignal.abort() let called = false await expect( @@ -685,31 +698,45 @@ describe("the tool's own answer", () => { expect(result.output).toContain("Not available") }) + test("a model named __proto__ keeps its final node", () => { + const out = remapSqlLineage( + JSON.parse( + '{"tableEdges":[["t","__final_select__"]],"details":{"t":{"name":"t","type":"table"},"__final_select__":{"type":"final","sql":"select 1"}}}', + ), + { name: "__proto__", uniqueId: "model.shop.__proto__", resourceType: "model" }, + [], + ) + expect(out.edges).toEqual([["t", "__proto__"]]) + expect(out.nodes.map((n) => n.id)).toEqual(["t", "__proto__"]) + }) + test("a model the manifest lacks is a failure with the reason", async () => { const project = realpathSync(await mkdtemp(path.join(os.tmpdir(), "dbt-sql-graph-"))) - await writeFile(path.join(project, "dbt_project.yml"), "name: shop\n") - dbtAdapterInternals.readConfig = async () => ({ - projectRoot: project, - pythonPath: "python3", - dbtIntegration: "corecommand", - queryLimit: 500, - }) - dbtAdapterInternals.create = async () => ({ - isInitialized: () => true, - dispose: async () => {}, - parseManifest: async () => ({ - nodeMetaMap: { lookupByUniqueId: () => undefined, lookupByBaseName: () => undefined }, - sourceMetaMap: { get: () => undefined }, - }), - }) - const tool = await initTool(DbtSqlGraphTool as any) - const result = await tool - .execute({ model: "nope" }, ctx) - .finally(() => rm(project, { recursive: true, force: true })) - expect(result.metadata).toMatchObject({ - success: false, - model: "nope", - error: "no model named nope in the manifest", - }) + try { + await writeFile(path.join(project, "dbt_project.yml"), "name: shop\n") + dbtAdapterInternals.readConfig = async () => ({ + projectRoot: project, + pythonPath: "python3", + dbtIntegration: "corecommand", + queryLimit: 500, + }) + dbtAdapterInternals.create = async () => ({ + isInitialized: () => true, + dispose: async () => {}, + parseManifest: async () => ({ + nodeMetaMap: { lookupByUniqueId: () => undefined, lookupByBaseName: () => undefined }, + sourceMetaMap: { get: () => undefined }, + }), + }) + const tool = await initTool(DbtSqlGraphTool as any) + const result = await tool.execute({ model: "nope" }, ctx) + expect(result.metadata).toMatchObject({ + success: false, + model: "nope", + error: "no model named nope in the manifest", + }) + } finally { + await rm(project, { recursive: true, force: true }) + } }) }) From 1666ada00ebd2fb91e8341e9cc232cd8fd0e85c1 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 14:26:09 +0530 Subject: [PATCH 18/27] fix: cancellation interrupts the compile and manifest waits; dbt_sql_graph's graph travels as MCP structured content Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/mcp-server/server.ts | 1 + .../src/altimate/tools/dbt-sql-graph.ts | 6 ++--- .../test/altimate/dbt-sql-graph.test.ts | 23 +++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/altimate/mcp-server/server.ts b/packages/opencode/src/altimate/mcp-server/server.ts index c89ebee902..64639f6998 100644 --- a/packages/opencode/src/altimate/mcp-server/server.ts +++ b/packages/opencode/src/altimate/mcp-server/server.ts @@ -51,6 +51,7 @@ export async function serverInstructions(): Promise { // fields are passed, and the whole is capped. const STRUCTURED_FIELDS: Record = { dbt_lineage: ["model_name", "model_unique_id", "confidence", "confidence_factors", "upstream", "columns", "edges", "error"], + dbt_sql_graph: ["model", "nodes", "edges", "error"], impact_analysis: [ "model", "column", diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index f5b8848575..3306058b60 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -190,7 +190,7 @@ export async function buildSqlGraph( deps: SqlGraphDeps, signal: AbortSignal, ): Promise { - const manifest = await adapter.parseManifest() + const manifest = await untilAborted(adapter.parseManifest(), signal) signal.throwIfAborted() if (!manifest) throw new Error("the dbt manifest is not available") const node = manifest.nodeMetaMap.lookupByUniqueId(model) ?? manifest.nodeMetaMap.lookupByBaseName(model) @@ -199,7 +199,7 @@ export async function buildSqlGraph( if (node.resource_type !== "model" || !node.path?.toLowerCase().endsWith(".sql")) throw new Error(`${node.name} is not a dbt model with a SQL file`) const file = path.isAbsolute(node.path) ? node.path : path.join(deps.projectRoot ?? "", node.path) - const compiled = await adapter.unsafeCompileQuery(deps.readFile(file), node.name) + const compiled = await untilAborted(adapter.unsafeCompileQuery(deps.readFile(file), node.name), signal) signal.throwIfAborted() if (!compiled) throw new Error(`unable to compile ${node.name}`) @@ -260,7 +260,7 @@ export async function buildSqlGraph( }) } // The warehouse metadata read: once per call, only for the parents. - const fromDb = requests.length ? await adapter.getBulkSchemaFromDB(requests, signal) : {} + const fromDb = requests.length ? await untilAborted(adapter.getBulkSchemaFromDB(requests, signal), signal) : {} signal.throwIfAborted() const hash = crypto.createHash("md5").update(compiled).digest("hex") const dialect = adapter.getAdapterType() diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts index 6ea1d6fe74..51b70f1382 100644 --- a/packages/opencode/test/altimate/dbt-sql-graph.test.ts +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -3,6 +3,7 @@ // dbt_sql_graph: the request built from the compiled model and its parents' columns, and the // remap of the backend's reply (`__final_select__` to the model's node, relation nodes to dbt unique ids). // The adapter and the endpoint are fakes, so no dbt, warehouse or backend is involved. +import { structuredResult } from "../../src/altimate/mcp-server/server" import { afterEach, describe, expect, test } from "bun:test" import { mkdtemp, rm, writeFile } from "node:fs/promises" import { realpathSync } from "node:fs" @@ -481,6 +482,16 @@ describe("buildSqlGraph", () => { buildSqlGraph(adapter(), "orders", { ...deps, fetchLineage: async () => bad as any }, signal), ).rejects.toThrow("no SQL graph") } + // A compile that never settles does not hold the call past its cancellation. + const stalled = new AbortController() + const pending = buildSqlGraph( + adapter({ unsafeCompileQuery: () => new Promise(() => {}) }), + "orders", + deps, + stalled.signal, + ) + stalled.abort(new Error("cancelled")) + await expect(pending).rejects.toThrow("cancelled") const aborted = AbortSignal.abort() let called = false await expect( @@ -490,6 +501,18 @@ describe("buildSqlGraph", () => { }) }) +describe("dbt_sql_graph over MCP", () => { + test("its graph travels as structured content", () => { + const nodes = [{ id: "a", name: "a", type: "cte", sql: "select 1", columns: [] }] + const out = structuredResult( + "dbt_sql_graph", + { model: "orders", nodes, edges: [], success: true }, + "SQL graph for orders", + ) as any + expect(out).toEqual({ model: "orders", nodes, edges: [], report: "SQL graph for orders" }) + }) +}) + describe("formatSqlGraph", () => { test("lists each node with its inputs and column count", () => { const graph = remapSqlLineage(reply, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, []) From a58a5557325b1332ff89ab5d1d223ee51e03db93 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 16:13:36 +0530 Subject: [PATCH 19/27] fix: reject graph replies with untyped or dangling nodes, URLs with credentials, and pre-aborted work; keep quoted relations of different case apart Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/api/client.ts | 8 +++-- .../src/altimate/tools/dbt-sql-graph.ts | 23 +++++++++---- .../test/altimate/dbt-adapter-access.test.ts | 16 ++++++--- .../test/altimate/dbt-sql-graph.test.ts | 34 ++++++++++++++++++- 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/packages/opencode/src/altimate/api/client.ts b/packages/opencode/src/altimate/api/client.ts index 4c1b153ca1..205a284dd7 100644 --- a/packages/opencode/src/altimate/api/client.ts +++ b/packages/opencode/src/altimate/api/client.ts @@ -354,8 +354,12 @@ export namespace AltimateApi { const url = `${creds.altimateUrl}${endpoint}` // Checked here so that only the header check can make fetch throw a TypeError before sending. Only // http(s) is sent: "localhost:3000" parses as a URL with the scheme "localhost:". - if (!URL.canParse(url) || !["http:", "https:"].includes(new URL(url).protocol)) - throw new Error(`API ${method} ${endpoint} could not be sent: the stored API URL is not an http(s) URL.`) + // Credentials in the URL make fetch throw an uncoded TypeError too, so they are refused here. + const parsed = URL.canParse(url) ? new URL(url) : undefined + if (!parsed || !["http:", "https:"].includes(parsed.protocol) || parsed.username || parsed.password) + throw new Error( + `API ${method} ${endpoint} could not be sent: the stored API URL is not an http(s) URL without credentials.`, + ) // 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 diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index 3306058b60..0b3cad06db 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -132,14 +132,20 @@ export function remapSqlLineage( const claimed = new Map>() for (const p of parents) { // A source's relation is its table's identifier; any other node's, its alias. - const wanted = (p.sourceTable ?? p.alias ?? "").toLowerCase() + const wanted = p.sourceTable ?? p.alias ?? "" if (!wanted) continue // The backend keys a relation by its location and a per-occurrence suffix, so the match is on its name, in the parent's database and schema when both say one; - // every occurrence of the relation in the SQL stands for the same dbt node. - for (const [k, d] of Object.entries(details)) { - if (d.type !== "table" || (d.name ?? k).toLowerCase() !== wanted) continue - if (d.schema && p.schema && d.schema.toLowerCase() !== p.schema.toLowerCase()) continue - if (d.db && p.database && d.db.toLowerCase() !== p.database.toLowerCase()) continue + // every occurrence of the relation in the SQL stands for the same dbt node. A quoted `Foo` and `FOO` can be two relations: + // an exact-case match wins, and only without one does the match ignore case. + const matches = (fold: (v: string) => string) => + Object.entries(details).filter(([k, d]) => { + if (d.type !== "table" || fold(d.name ?? k) !== fold(wanted)) return false + if (d.schema && p.schema && fold(d.schema) !== fold(p.schema)) return false + if (d.db && p.database && fold(d.db) !== fold(p.database)) return false + return true + }) + const exact = matches((v) => v) + for (const [k] of exact.length ? exact : matches((v) => v.toLowerCase())) { claimed.set(k, (claimed.get(k) ?? new Set()).add(p.uniqueId)) mapping[k] = { nodeId: p.uniqueId, type: p.resourceType } } @@ -190,6 +196,7 @@ export async function buildSqlGraph( deps: SqlGraphDeps, signal: AbortSignal, ): Promise { + signal.throwIfAborted() const manifest = await untilAborted(adapter.parseManifest(), signal) signal.throwIfAborted() if (!manifest) throw new Error("the dbt manifest is not available") @@ -199,6 +206,7 @@ export async function buildSqlGraph( if (node.resource_type !== "model" || !node.path?.toLowerCase().endsWith(".sql")) throw new Error(`${node.name} is not a dbt model with a SQL file`) const file = path.isAbsolute(node.path) ? node.path : path.join(deps.projectRoot ?? "", node.path) + signal.throwIfAborted() const compiled = await untilAborted(adapter.unsafeCompileQuery(deps.readFile(file), node.name), signal) signal.throwIfAborted() if (!compiled) throw new Error(`unable to compile ${node.name}`) @@ -284,7 +292,8 @@ export async function buildSqlGraph( !Array.isArray(response?.tableEdges) || !isRecord(response.details) || !response.tableEdges.every((e) => Array.isArray(e) && e.length === 2 && e.every((x) => typeof x === "string")) || - !Object.values(response.details).every(isRecord) + !Object.values(response.details).every((d) => isRecord(d) && typeof (d as { type?: unknown }).type === "string") || + !response.tableEdges.every((e) => e.every((x) => Object.hasOwn(response.details, x))) ) throw new Error("the backend returned no SQL graph") return remapSqlLineage( diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index 72f98844fa..1ef22b9636 100644 --- a/packages/opencode/test/altimate/dbt-adapter-access.test.ts +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -138,9 +138,7 @@ describe("dbtAdapterFor", () => { test("a read-only call cancelled beforehand is cancelled, not refused", async () => { const controller = new AbortController() controller.abort() - await expect( - dbtAdapterFor({ ...ctx, abort: controller.signal, extra: { forceReadOnly: true } }), - ).rejects.toThrow() + await expect(dbtAdapterFor({ ...ctx, abort: controller.signal, extra: { forceReadOnly: true } })).rejects.toThrow() expect(created).toBe(0) }) @@ -593,10 +591,18 @@ describe("AltimateApi.apiRequest", () => { globalThis.fetch = (async () => (urlSends++, new Response("{}"))) as unknown as typeof fetch try { // Not a URL, or a URL whose scheme isn't http(s): "localhost:3000" parses with the scheme "localhost:". - for (const altimateUrl of ["api.example.test", "localhost:3000", "file:///tmp", "ftp://api.example.test"]) { + for (const altimateUrl of [ + "api.example.test", + "localhost:3000", + "file:///tmp", + "ftp://api.example.test", + "https://user:pass@api.example.test", + ]) { await Bun.write(file, JSON.stringify({ altimateUrl, 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 an http(s) URL.") + expect(badUrl.message).toBe( + "API GET /dbt/v1/x could not be sent: the stored API URL is not an http(s) URL without credentials.", + ) } expect(urlSends).toBe(0) } finally { diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts index 51b70f1382..fab30f1525 100644 --- a/packages/opencode/test/altimate/dbt-sql-graph.test.ts +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -299,6 +299,23 @@ describe("remapSqlLineage", () => { expect(member.edges).toEqual([["x", "constructor"]]) }) + test("a quoted Foo and FOO are two relations: each parent keeps its own exact-case match", () => { + const two = { + tableEdges: [] as [string, string][], + details: { + "Foo#1": { type: "table", name: "Foo", columns: [] }, + "FOO#1": { type: "table", name: "FOO", columns: [] }, + }, + } as any + const g = remapSqlLineage(two, { name: "m", uniqueId: "model.p.m", resourceType: "model" }, [ + { uniqueId: "model.p.mixed", alias: "Foo", resourceType: "model" }, + { uniqueId: "model.p.upper", alias: "FOO", resourceType: "model" }, + ]) + const byId = Object.fromEntries(g.nodes.map((n) => [n.id, n.nodeId])) + expect(byId["Foo#1"]).toBe("model.p.mixed") + expect(byId["FOO#1"]).toBe("model.p.upper") + }) + test("does not mutate the backend reply", () => { expect(reply.details.__final_select__).toBeDefined() expect(reply.tableEdges[1]).toEqual(["o", "__final_select__"]) @@ -477,22 +494,37 @@ describe("buildSqlGraph", () => { { ...reply, details: [] }, { ...reply, details: { a: 1 } }, { ...reply, tableEdges: [["a"]] }, + { ...reply, details: { a: {} } }, + { ...reply, details: {}, tableEdges: [["a", "__final_select__"]] }, ]) { await expect( buildSqlGraph(adapter(), "orders", { ...deps, fetchLineage: async () => bad as any }, signal), ).rejects.toThrow("no SQL graph") } // A compile that never settles does not hold the call past its cancellation. + let enteredCompile: () => void = () => {} + const entered = new Promise((resolve) => (enteredCompile = resolve)) const stalled = new AbortController() const pending = buildSqlGraph( - adapter({ unsafeCompileQuery: () => new Promise(() => {}) }), + adapter({ unsafeCompileQuery: () => (enteredCompile(), new Promise(() => {})) }), "orders", deps, stalled.signal, ) + await entered stalled.abort(new Error("cancelled")) await expect(pending).rejects.toThrow("cancelled") const aborted = AbortSignal.abort() + let parsedManifest = false + await expect( + buildSqlGraph( + adapter({ parseManifest: async () => ((parsedManifest = true), undefined as any) }), + "orders", + deps, + aborted, + ), + ).rejects.toThrow() + expect(parsedManifest).toBe(false) let called = false await expect( buildSqlGraph(adapter(), "orders", { ...deps, fetchLineage: async () => ((called = true), reply) }, aborted), From 6e3f06d4b1fae382188f96e39328d0cff4de043d Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 16:21:35 +0530 Subject: [PATCH 20/27] fix: a relation spelled in two cases keeps every occurrence tied to its dbt node unless another parent owns that spelling exactly Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-sql-graph.ts | 21 +++++++++++-------- .../test/altimate/dbt-sql-graph.test.ts | 14 +++++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index 0b3cad06db..3b65f47036 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -132,20 +132,23 @@ export function remapSqlLineage( const claimed = new Map>() for (const p of parents) { // A source's relation is its table's identifier; any other node's, its alias. - const wanted = p.sourceTable ?? p.alias ?? "" - if (!wanted) continue + if (!(p.sourceTable ?? p.alias)) continue // The backend keys a relation by its location and a per-occurrence suffix, so the match is on its name, in the parent's database and schema when both say one; // every occurrence of the relation in the SQL stands for the same dbt node. A quoted `Foo` and `FOO` can be two relations: - // an exact-case match wins, and only without one does the match ignore case. - const matches = (fold: (v: string) => string) => + // an occurrence goes to the parent whose name it matches exactly, and other occurrences match ignoring case unless another parent owns them exactly. + const matches = (q: (typeof parents)[number], fold: (v: string) => string) => Object.entries(details).filter(([k, d]) => { - if (d.type !== "table" || fold(d.name ?? k) !== fold(wanted)) return false - if (d.schema && p.schema && fold(d.schema) !== fold(p.schema)) return false - if (d.db && p.database && fold(d.db) !== fold(p.database)) return false + const w = q.sourceTable ?? q.alias ?? "" + if (d.type !== "table" || fold(d.name ?? k) !== fold(w)) return false + if (d.schema && q.schema && fold(d.schema) !== fold(q.schema)) return false + if (d.db && q.database && fold(d.db) !== fold(q.database)) return false return true }) - const exact = matches((v) => v) - for (const [k] of exact.length ? exact : matches((v) => v.toLowerCase())) { + const exactOf = (q: (typeof parents)[number]) => matches(q, (v) => v).map(([k]) => k) + const others = new Set(parents.filter((q) => q !== p && (q.sourceTable ?? q.alias)).flatMap(exactOf)) + const mine = new Set(exactOf(p)) + for (const [k] of matches(p, (v) => v.toLowerCase())) { + if (!mine.has(k) && others.has(k)) continue claimed.set(k, (claimed.get(k) ?? new Set()).add(p.uniqueId)) mapping[k] = { nodeId: p.uniqueId, type: p.resourceType } } diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts index fab30f1525..87e316edd4 100644 --- a/packages/opencode/test/altimate/dbt-sql-graph.test.ts +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -316,6 +316,20 @@ describe("remapSqlLineage", () => { expect(byId["FOO#1"]).toBe("model.p.upper") }) + test("a repeated relation spelled in two cases still reaches its dbt node", () => { + const rep = { + tableEdges: [] as [string, string][], + details: { + "raw.orders#1": { type: "table", name: "orders", columns: [] }, + "raw.ORDERS#2": { type: "table", name: "ORDERS", columns: [] }, + }, + } as any + const g = remapSqlLineage(rep, { name: "m", uniqueId: "model.p.m", resourceType: "model" }, [ + { uniqueId: "model.p.orders", alias: "orders", resourceType: "model" }, + ]) + expect(g.nodes.filter((n) => n.nodeId === "model.p.orders").length).toBe(2) + }) + test("does not mutate the backend reply", () => { expect(reply.details.__final_select__).toBeDefined() expect(reply.tableEdges[1]).toEqual(["o", "__final_select__"]) From ab57c06dfe372f9a988400f7c7e862dbd9dbb945 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 21:05:58 +0530 Subject: [PATCH 21/27] perf: the exact-match relation sets are built once for all parents instead of per parent Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-sql-graph.ts | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index 3b65f47036..b121fc01fc 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -130,25 +130,25 @@ export function remapSqlLineage( const mapping: Record = Object.create(null) // A relation that more than one parent could be is tied to none: an arbitrary dbt node would be shown as the real one. const claimed = new Map>() - for (const p of parents) { - // A source's relation is its table's identifier; any other node's, its alias. - if (!(p.sourceTable ?? p.alias)) continue - // The backend keys a relation by its location and a per-occurrence suffix, so the match is on its name, in the parent's database and schema when both say one; - // every occurrence of the relation in the SQL stands for the same dbt node. A quoted `Foo` and `FOO` can be two relations: - // an occurrence goes to the parent whose name it matches exactly, and other occurrences match ignoring case unless another parent owns them exactly. - const matches = (q: (typeof parents)[number], fold: (v: string) => string) => - Object.entries(details).filter(([k, d]) => { - const w = q.sourceTable ?? q.alias ?? "" - if (d.type !== "table" || fold(d.name ?? k) !== fold(w)) return false - if (d.schema && q.schema && fold(d.schema) !== fold(q.schema)) return false - if (d.db && q.database && fold(d.db) !== fold(q.database)) return false - return true - }) - const exactOf = (q: (typeof parents)[number]) => matches(q, (v) => v).map(([k]) => k) - const others = new Set(parents.filter((q) => q !== p && (q.sourceTable ?? q.alias)).flatMap(exactOf)) - const mine = new Set(exactOf(p)) + type Parent = (typeof parents)[number] + // The backend keys a relation by its location and a per-occurrence suffix, so the match is on its name, in the parent's database and schema when both say one; + // every occurrence of the relation in the SQL stands for the same dbt node. A quoted `Foo` and `FOO` can be two relations: + // an occurrence goes to the parent whose name it matches exactly, and other occurrences match ignoring case unless another parent owns them exactly. + const matches = (q: Parent, fold: (v: string) => string) => + Object.entries(details).filter(([k, d]) => { + const w = q.sourceTable ?? q.alias ?? "" + if (d.type !== "table" || fold(d.name ?? k) !== fold(w)) return false + if (d.schema && q.schema && fold(d.schema) !== fold(q.schema)) return false + if (d.db && q.database && fold(d.db) !== fold(q.database)) return false + return true + }) + // A source's relation is its table's identifier; any other node's, its alias. + const named = parents.filter((q) => q.sourceTable ?? q.alias) + const exact = new Map>(named.map((q) => [q, new Set(matches(q, (v) => v).map(([k]) => k))])) + for (const p of named) { + const mine = exact.get(p)! for (const [k] of matches(p, (v) => v.toLowerCase())) { - if (!mine.has(k) && others.has(k)) continue + if (!mine.has(k) && named.some((q) => q !== p && exact.get(q)!.has(k))) continue claimed.set(k, (claimed.get(k) ?? new Set()).add(p.uniqueId)) mapping[k] = { nodeId: p.uniqueId, type: p.resourceType } } From 008175060bd601902e20a1d9afaf0c27696021b9 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 21:11:55 +0530 Subject: [PATCH 22/27] Keep this PR to its own files: the MCP structured-field entry and the placeholder-test skip move to a separate shared fix Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/mcp-server/server.ts | 1 - .../test/altimate/dbt-adapter-access.test.ts | 1 - .../opencode/test/altimate/dbt-sql-graph.test.ts | 13 ------------- 3 files changed, 15 deletions(-) diff --git a/packages/opencode/src/altimate/mcp-server/server.ts b/packages/opencode/src/altimate/mcp-server/server.ts index 64639f6998..c89ebee902 100644 --- a/packages/opencode/src/altimate/mcp-server/server.ts +++ b/packages/opencode/src/altimate/mcp-server/server.ts @@ -51,7 +51,6 @@ export async function serverInstructions(): Promise { // fields are passed, and the whole is capped. const STRUCTURED_FIELDS: Record = { dbt_lineage: ["model_name", "model_unique_id", "confidence", "confidence_factors", "upstream", "columns", "edges", "error"], - dbt_sql_graph: ["model", "nodes", "edges", "error"], impact_analysis: [ "model", "column", diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index 0a07b53029..4317672e42 100644 --- a/packages/opencode/test/altimate/dbt-adapter-access.test.ts +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -84,7 +84,6 @@ 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) { - if (id === "dbt_sql_graph") continue const tool = await initTool(EXPOSED[id] as any) expect(tool.id).toBe(id) const result = await tool.execute(MINIMAL_ARGS[id], ctx) diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts index 87e316edd4..cf54620187 100644 --- a/packages/opencode/test/altimate/dbt-sql-graph.test.ts +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -3,7 +3,6 @@ // dbt_sql_graph: the request built from the compiled model and its parents' columns, and the // remap of the backend's reply (`__final_select__` to the model's node, relation nodes to dbt unique ids). // The adapter and the endpoint are fakes, so no dbt, warehouse or backend is involved. -import { structuredResult } from "../../src/altimate/mcp-server/server" import { afterEach, describe, expect, test } from "bun:test" import { mkdtemp, rm, writeFile } from "node:fs/promises" import { realpathSync } from "node:fs" @@ -547,18 +546,6 @@ describe("buildSqlGraph", () => { }) }) -describe("dbt_sql_graph over MCP", () => { - test("its graph travels as structured content", () => { - const nodes = [{ id: "a", name: "a", type: "cte", sql: "select 1", columns: [] }] - const out = structuredResult( - "dbt_sql_graph", - { model: "orders", nodes, edges: [], success: true }, - "SQL graph for orders", - ) as any - expect(out).toEqual({ model: "orders", nodes, edges: [], report: "SQL graph for orders" }) - }) -}) - describe("formatSqlGraph", () => { test("lists each node with its inputs and column count", () => { const graph = remapSqlLineage(reply, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, []) From 58ace8f83dd67517c1c7b699d3458f41d9efe551 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 21:16:01 +0530 Subject: [PATCH 23/27] fix: a replacement final-select key never overwrites another node; the text output lists each node's columns and SQL Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-sql-graph.ts | 6 +++- .../test/altimate/dbt-sql-graph.test.ts | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index b121fc01fc..441917be21 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -155,7 +155,9 @@ export function remapSqlLineage( } for (const [k, owners] of claimed) if (owners.size > 1) delete mapping[k] // A CTE the SQL names like the model (`with customers as (…)`) keeps its node: the model's own select is told apart. - const finalKey = Object.hasOwn(details, model.name) ? `${model.name} (final select)` : model.name + // The replacement key is one no node already uses, so it can't overwrite another CTE. + let finalKey = model.name + while (Object.hasOwn(details, finalKey)) finalKey = `${finalKey} (final select)` mapping[finalKey] = { nodeId: model.uniqueId, type: model.resourceType } const edges = response.tableEdges.map( (edge) => edge.map((item) => (item === FINAL_SELECT ? finalKey : item)) as [string, string], @@ -317,6 +319,8 @@ export function formatSqlGraph(model: string, graph: SqlGraph): string { lines.push( `- ${n.name} (${n.type}${n.nodeId ? `, ${n.nodeId}` : ""}): ${n.columns.length} columns${from?.length ? `, from ${from.join(", ")}` : ""}`, ) + if (n.columns.length) lines.push(` columns: ${n.columns.map((c) => c.name).join(", ")}`) + if (n.sql) lines.push(` sql: ${n.sql.length > 400 ? `${n.sql.slice(0, 400)}…` : n.sql}`.replace(/\s*\n\s*/g, " ")) } return lines.join("\n") } diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts index cf54620187..ecfc5028a9 100644 --- a/packages/opencode/test/altimate/dbt-sql-graph.test.ts +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -796,3 +796,32 @@ describe("the tool's own answer", () => { } }) }) + +describe("final-select identity and the text output", () => { + test("a replacement final-select key never overwrites another node", () => { + const response = { + details: { + orders: { type: "cte", sql: "select 1", columns: [] }, + "orders (final select)": { type: "cte", sql: "select 2", columns: [] }, + __final_select__: { type: "select", sql: "select 3", columns: [] }, + }, + tableEdges: [["orders (final select)", "__final_select__"]], + } as unknown as SqlLineageResponse + const graph = remapSqlLineage(response, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, []) + const ids = graph.nodes.map((n) => n.id) + expect(new Set(ids).size).toBe(3) + const [from, to] = graph.edges[0] + expect(from).toBe("orders (final select)") + expect(to).not.toBe(from) + expect(graph.nodes.find((n) => n.id === to)?.sql).toBe("select 3") + }) + + test("the text lists each node's columns and SQL", () => { + const text = formatSqlGraph("m", { + nodes: [{ id: "a", name: "a", type: "cte", sql: "select\n id from t", columns: [{ name: "id" }, { name: "x" }] }], + edges: [], + } as any) + expect(text).toContain(" columns: id, x") + expect(text).toContain(" sql: select id from t") + }) +}) From 6c6ff4bd497e9e65237de170d5fafb19ac01755b Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 06:58:17 +0530 Subject: [PATCH 24/27] fix: a reply whose node columns are not a list of named columns is refused as no SQL graph Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/tools/dbt-sql-graph.ts | 11 +++++++++-- packages/opencode/test/altimate/dbt-sql-graph.test.ts | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index 441917be21..25ddebb985 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -297,7 +297,14 @@ export async function buildSqlGraph( !Array.isArray(response?.tableEdges) || !isRecord(response.details) || !response.tableEdges.every((e) => Array.isArray(e) && e.length === 2 && e.every((x) => typeof x === "string")) || - !Object.values(response.details).every((d) => isRecord(d) && typeof (d as { type?: unknown }).type === "string") || + !Object.values(response.details).every((d) => { + if (!isRecord(d) || typeof (d as { type?: unknown }).type !== "string") return false + const columns = (d as { columns?: unknown }).columns + return ( + columns === undefined || + (Array.isArray(columns) && columns.every((c) => isRecord(c) && typeof (c as { name?: unknown }).name === "string")) + ) + }) || !response.tableEdges.every((e) => e.every((x) => Object.hasOwn(response.details, x))) ) throw new Error("the backend returned no SQL graph") @@ -308,7 +315,7 @@ export async function buildSqlGraph( ) } -/** The graph as a list, one line per node: its type, its column count and its inputs. */ +/** The graph as a list: a line per node with its type, column count and inputs, then its column names and SQL beneath it. */ export function formatSqlGraph(model: string, graph: SqlGraph): string { const inputs = new Map() const nameOf = new Map(graph.nodes.map((n) => [n.id, n.name])) diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts index ecfc5028a9..6c4dbc5fc3 100644 --- a/packages/opencode/test/altimate/dbt-sql-graph.test.ts +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -508,6 +508,8 @@ describe("buildSqlGraph", () => { { ...reply, details: { a: 1 } }, { ...reply, tableEdges: [["a"]] }, { ...reply, details: { a: {} } }, + { ...reply, details: { a: { type: "cte", columns: "id" } } }, + { ...reply, details: { a: { type: "cte", columns: ["id"] } } }, { ...reply, details: {}, tableEdges: [["a", "__final_select__"]] }, ]) { await expect( From 97f723db0f8c083f273cb4c996fe1379bc0aa30d Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 06:58:23 +0530 Subject: [PATCH 25/27] style: format the columns guard Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/tools/dbt-sql-graph.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index 25ddebb985..1420fed0a5 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -302,7 +302,8 @@ export async function buildSqlGraph( const columns = (d as { columns?: unknown }).columns return ( columns === undefined || - (Array.isArray(columns) && columns.every((c) => isRecord(c) && typeof (c as { name?: unknown }).name === "string")) + (Array.isArray(columns) && + columns.every((c) => isRecord(c) && typeof (c as { name?: unknown }).name === "string")) ) }) || !response.tableEdges.every((e) => e.every((x) => Object.hasOwn(response.details, x))) @@ -327,7 +328,8 @@ export function formatSqlGraph(model: string, graph: SqlGraph): string { `- ${n.name} (${n.type}${n.nodeId ? `, ${n.nodeId}` : ""}): ${n.columns.length} columns${from?.length ? `, from ${from.join(", ")}` : ""}`, ) if (n.columns.length) lines.push(` columns: ${n.columns.map((c) => c.name).join(", ")}`) - if (n.sql) lines.push(` sql: ${n.sql.length > 400 ? `${n.sql.slice(0, 400)}…` : n.sql}`.replace(/\s*\n\s*/g, " ")) + if (n.sql) + lines.push(` sql: ${n.sql.length > 400 ? `${n.sql.slice(0, 400)}…` : n.sql}`.replace(/\s*\n\s*/g, " ")) } return lines.join("\n") } From 8695ac96a1d4e84ddd08c941786c07d93e43a714 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 07:44:52 +0530 Subject: [PATCH 26/27] fix: a manifest column documented in a different case than the warehouse's name is one column, not two Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/altimate/tools/dbt-sql-graph.ts | 4 +++- .../opencode/test/altimate/dbt-sql-graph.test.ts | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index 1420fed0a5..7f71a248d9 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -96,7 +96,9 @@ export function mergeColumns( ): SqlLineageModelNode["columns"] { // A Map: a column named `constructor` or `__proto__` is a column, not an inherited property. const merged = new Map() - for (const [k, v] of Object.entries(manifestColumns ?? {})) merged.set(columnKey(k, adapter), { ...v }) + // A manifest name is documentation spelling: it is case-sensitive only when the column is declared `quote: true`. + for (const [k, v] of Object.entries(manifestColumns ?? {})) + merged.set((v as { quote?: boolean }).quote === true ? columnKey(k, adapter) : k.toLowerCase(), { ...v }) for (const c of fromDb ?? []) { const key = columnKey(c.column, adapter) const existing = merged.get(key) diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts index 6c4dbc5fc3..64bb951a5e 100644 --- a/packages/opencode/test/altimate/dbt-sql-graph.test.ts +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -163,6 +163,12 @@ describe("mergeColumns", () => { expect(rs.foo.data_type).toBe("text") }) + test("a manifest column documented in upper case meets the warehouse's unquoted lower-case name on Postgres", () => { + const merged = mergeColumns({ ID: col("ID") }, [{ column: "id", dtype: "BIGINT" }], "postgres") + expect(Object.keys(merged)).toEqual(["id"]) + expect(merged.id.data_type).toBe("bigint") + }) + test("a manifest column and the warehouse's name for it meet under one key in the adapter's case", () => { const merged = mergeColumns({ id: col("id") }, [{ column: "id", dtype: "BIGINT" }], "postgres") expect(Object.keys(merged)).toEqual(["id"]) @@ -809,7 +815,11 @@ describe("final-select identity and the text output", () => { }, tableEdges: [["orders (final select)", "__final_select__"]], } as unknown as SqlLineageResponse - const graph = remapSqlLineage(response, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, []) + const graph = remapSqlLineage( + response, + { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, + [], + ) const ids = graph.nodes.map((n) => n.id) expect(new Set(ids).size).toBe(3) const [from, to] = graph.edges[0] @@ -820,7 +830,9 @@ describe("final-select identity and the text output", () => { test("the text lists each node's columns and SQL", () => { const text = formatSqlGraph("m", { - nodes: [{ id: "a", name: "a", type: "cte", sql: "select\n id from t", columns: [{ name: "id" }, { name: "x" }] }], + nodes: [ + { id: "a", name: "a", type: "cte", sql: "select\n id from t", columns: [{ name: "id" }, { name: "x" }] }, + ], edges: [], } as any) expect(text).toContain(" columns: id, x") From 0fd235ffb74bcd221e81669607ccade3c6c12f9d Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 07:58:27 +0530 Subject: [PATCH 27/27] fix: the warehouse's own spelling of a documented column fills that column on DuckDB Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/altimate/tools/dbt-sql-graph.ts | 12 +++++++++--- .../opencode/test/altimate/dbt-sql-graph.test.ts | 6 ++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index 7f71a248d9..c3a950902c 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -97,10 +97,16 @@ export function mergeColumns( // A Map: a column named `constructor` or `__proto__` is a column, not an inherited property. const merged = new Map() // A manifest name is documentation spelling: it is case-sensitive only when the column is declared `quote: true`. - for (const [k, v] of Object.entries(manifestColumns ?? {})) - merged.set((v as { quote?: boolean }).quote === true ? columnKey(k, adapter) : k.toLowerCase(), { ...v }) + const documented = new Set() + for (const [k, v] of Object.entries(manifestColumns ?? {})) { + const quoted = (v as { quote?: boolean }).quote === true + if (!quoted) documented.add(k.toLowerCase()) + merged.set(quoted ? columnKey(k, adapter) : k.toLowerCase(), { ...v }) + } for (const c of fromDb ?? []) { - const key = columnKey(c.column, adapter) + let key = columnKey(c.column, adapter) + // The warehouse's own spelling of a column the manifest documents (DuckDB keeps `ID` for a column documented as `id`) is that column. + if (!merged.has(key) && documented.has(c.column.toLowerCase())) key = c.column.toLowerCase() const existing = merged.get(key) if (existing) { existing.data_type = (existing.data_type || c.dtype)?.toLowerCase() diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts index 64bb951a5e..58d6ae311e 100644 --- a/packages/opencode/test/altimate/dbt-sql-graph.test.ts +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -169,6 +169,12 @@ describe("mergeColumns", () => { expect(merged.id.data_type).toBe("bigint") }) + test("a column documented in lower case meets the warehouse's upper-case spelling of it on DuckDB", () => { + const merged = mergeColumns({ id: col("id") }, [{ column: "ID", dtype: "INTEGER" }], "duckdb") + expect(Object.keys(merged)).toEqual(["id"]) + expect(merged.id.data_type).toBe("integer") + }) + test("a manifest column and the warehouse's name for it meet under one key in the adapter's case", () => { const merged = mergeColumns({ id: col("id") }, [{ column: "id", dtype: "BIGINT" }], "postgres") expect(Object.keys(merged)).toEqual(["id"])