From b7cfc0426d60963a83f22a8f5c6053c7b7fce8e4 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 16:30:43 +0530 Subject: [PATCH 01/32] 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/32] 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 5971ad2bfd2ba370a03a539c4c7b645295dd7207 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 17:08:32 +0530 Subject: [PATCH 03/32] feat: dbt_map_relations maps the tables of raw SQL to ref() and source() calls Each table the SQL reads is matched against the relation_name of the manifest's models, snapshots, seeds and sources, with the match reported as exact, case_insensitive, partial or none and every candidate listed. Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-map-relations.ts | 449 +++++++++++++++++- .../test/altimate/dbt-map-relations.test.ts | 339 +++++++++++++ 2 files changed, 779 insertions(+), 9 deletions(-) create mode 100644 packages/opencode/test/altimate/dbt-map-relations.test.ts diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 1216d6b2ee..5036de5852 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -1,18 +1,449 @@ import z from "zod" +import fs from "fs" +import path from "path" import { Tool } from "../../tool/tool" +import { Dispatcher } from "../native" +import { loadRawManifest, detectDialect } from "../native/dbt/helpers" +import { projectRoot } from "../native/connections/registry" + +export type RelationMatch = "exact" | "case_insensitive" | "partial" | "none" +export type RelationKind = "model" | "snapshot" | "seed" | "source" + +export type RelationCandidate = { + replaceWith: string + kind: RelationKind + name: string + uniqueId: string + relationName: string + match: Exclude +} + +export type RelationMapping = { + /** The table as written in the SQL, qualifiers included. */ + raw: string + /** A `ref()` or `source()` call, or `raw` itself when nothing matched. */ + replaceWith: string + match: RelationMatch + /** Every manifest relation the table matched, best first. */ + candidates: RelationCandidate[] +} + +type Relation = { + kind: RelationKind + name: string + uniqueId: string + relationName: string + parts: string[] + call: string +} + +const RANK: Record, number> = { exact: 0, case_insensitive: 1, partial: 2 } +const KIND_RANK: Record = { model: 0, snapshot: 1, seed: 2, source: 3 } + +/** + * Split a dotted name into its parts, honouring "double", `back` and [bracket] quotes. Dots inside + * backticks split the name when `splitBacktickDots` is set: BigQuery reads `proj.ds.tbl` as a path. + */ +export function splitQualified(text: string, splitBacktickDots = false): string[] { + const parts: string[] = [] + let cur = "" + let i = 0 + const push = () => { + parts.push(cur) + cur = "" + } + while (i < text.length) { + const ch = text[i] + if (ch === '"' || ch === "`" || ch === "[") { + const close = ch === "[" ? "]" : ch + let j = i + 1 + let body = "" + while (j < text.length) { + if (text[j] === close) { + if (text[j + 1] === close && close !== "]") { + body += close + j += 2 + continue + } + break + } + body += text[j++] + } + if (ch === "`" && splitBacktickDots) { + const sub = body.split(".") + cur += sub[0] + for (const s of sub.slice(1)) { + push() + cur = s + } + } else cur += body + i = j + 1 + } else if (ch === ".") { + push() + i++ + } else { + cur += ch + i++ + } + } + push() + return parts +} + +/** A table reference found in the SQL text: the text as written and its unquoted parts. */ +type Reference = { raw: string; parts: string[] } + +const IDENT_START = /[A-Za-z_$"`[]/ +const RESERVED = new Set([ + "select", + "from", + "where", + "join", + "inner", + "left", + "right", + "full", + "outer", + "cross", + "natural", + "lateral", + "on", + "using", + "group", + "order", + "having", + "limit", + "offset", + "union", + "intersect", + "except", + "minus", + "qualify", + "window", + "set", + "when", + "then", + "else", + "end", + "as", + "values", + "returning", + "fetch", + "for", + "pivot", + "unpivot", + "tablesample", + "inner", + "straight_join", + "asof", +]) + +/** One identifier chain (`a.b.c`, each part quoted or not) starting at `i`; null when there is none. */ +function readChain(sql: string, start: number): { raw: string; end: number } | null { + let i = start + const n = sql.length + while (i < n) { + const ch = sql[i] + if (ch === '"' || ch === "`" || ch === "[") { + const close = ch === "[" ? "]" : ch + i++ + while (i < n) { + if (sql[i] === close) { + if (sql[i + 1] === close && close !== "]") { + i += 2 + continue + } + break + } + i++ + } + if (i >= n) return null + i++ + } else if (/[A-Za-z0-9_$\-]/.test(ch) && (i > start || /[A-Za-z_$]/.test(ch))) { + while (i < n && /[A-Za-z0-9_$]/.test(sql[i])) i++ + // BigQuery project ids may contain hyphens (`my-proj.ds.tbl` unquoted). + while (sql[i] === "-" && /[A-Za-z0-9_]/.test(sql[i + 1] ?? "")) { + i++ + while (i < n && /[A-Za-z0-9_$]/.test(sql[i])) i++ + } + } else return null + if (sql[i] === ".") { + i++ + continue + } + break + } + return i > start ? { raw: sql.slice(start, i), end: i } : null +} + +/** Replace comments and string literals with spaces so the scan never reads inside them. */ +function blank(sql: string): string { + let out = "" + let i = 0 + while (i < sql.length) { + const two = sql.slice(i, i + 2) + if (two === "--") { + while (i < sql.length && sql[i] !== "\n") { + out += " " + i++ + } + } else if (two === "/*") { + const end = sql.indexOf("*/", i + 2) + const stop = end < 0 ? sql.length : end + 2 + out += sql.slice(i, stop).replace(/[^\n]/g, " ") + i = stop + } else if (sql[i] === "'") { + let j = i + 1 + while (j < sql.length) { + if (sql[j] === "\\") j += 2 + else if (sql[j] === "'") { + if (sql[j + 1] === "'") j += 2 + else break + } else j++ + } + const stop = Math.min(j + 1, sql.length) + out += sql.slice(i, stop).replace(/[^\n]/g, " ") + i = stop + } else if (sql[i] === '"' || sql[i] === "`") { + // Keep quoted identifiers intact; skip over them so a quote inside one is not misread. + const close = sql[i] + let j = i + 1 + while (j < sql.length && sql[j] !== close) j++ + out += sql.slice(i, Math.min(j + 1, sql.length)) + i = Math.min(j + 1, sql.length) + } else { + out += sql[i] + i++ + } + } + return out +} + +function skipSpace(sql: string, i: number): number { + while (i < sql.length && /\s/.test(sql[i])) i++ + return i +} + +/** + * The table references of FROM, JOIN, INTO and UPDATE clauses, in text order, written as in the SQL. + * altimate-core's table list keeps only the last name part, so the schema and database a person wrote + * are read back from the text here; the list decides which of these are real tables. + */ +export function scanTableReferences(sql: string, splitBacktickDots: boolean): Reference[] { + const text = blank(sql) + const found: Reference[] = [] + const re = /\b(from|join|into|update|using|table)\b/gi + let m: RegExpExecArray | null + while ((m = re.exec(text))) { + let i = skipSpace(text, m.index + m[0].length) + for (;;) { + if (!IDENT_START.test(text[i] ?? "")) break + const chain = readChain(text, i) + if (!chain) break + i = skipSpace(text, chain.end) + // A function call (`flatten(...)`, `unnest(...)`) is not a table. + if (text[i] === "(") break + if (!RESERVED.has(chain.raw.toLowerCase())) { + found.push({ raw: chain.raw, parts: splitQualified(chain.raw, splitBacktickDots) }) + } else break + // An alias, with or without AS, then a comma continues a FROM list. + const asM = /^as\s+/i.exec(text.slice(i)) + if (asM) i = skipSpace(text, i + asM[0].length) + const alias = readChain(text, i) + if (alias && !alias.raw.includes(".") && !RESERVED.has(alias.raw.toLowerCase())) i = skipSpace(text, alias.end) + else if (asM && alias) i = skipSpace(text, alias.end) + if (text[i] === ",") { + i = skipSpace(text, i + 1) + continue + } + break + } + } + return found +} + +/** The model, snapshot, seed and source nodes of a manifest, each with its relation's name parts. */ +export function manifestRelations(manifest: any): Relation[] { + const rootProject: string | undefined = manifest?.metadata?.project_name + const out: Relation[] = [] + const relationOf = (node: any): string | undefined => { + if (typeof node?.relation_name === "string" && node.relation_name) return node.relation_name + const db = node?.database + const schema = node?.schema + const name = node?.alias ?? node?.identifier ?? node?.name + if (typeof schema === "string" && schema && typeof name === "string" && name) { + return [db, schema, name].filter((p) => typeof p === "string" && p).join(".") + } + return undefined + } + const nodes: Record = manifest?.nodes ?? {} + for (const [uniqueId, node] of Object.entries(nodes)) { + const kind = node?.resource_type + if (kind !== "model" && kind !== "snapshot" && kind !== "seed") continue + // Ephemeral models are inlined by dbt and have no relation to match. + if (node?.config?.materialized === "ephemeral") continue + const relationName = relationOf(node) + if (!relationName || typeof node.name !== "string") continue + const pkg = + typeof node.package_name === "string" && rootProject && node.package_name !== rootProject + ? node.package_name + : undefined + const call = pkg ? `{{ ref('${pkg}', '${node.name}') }}` : `{{ ref('${node.name}') }}` + out.push({ kind, name: node.name, uniqueId, relationName, parts: splitQualified(relationName, true), call }) + } + const sources: Record = manifest?.sources ?? {} + for (const [uniqueId, node] of Object.entries(sources)) { + const relationName = relationOf(node) + if (!relationName || typeof node?.source_name !== "string" || typeof node?.name !== "string") continue + out.push({ + kind: "source", + name: node.name, + uniqueId, + relationName, + parts: splitQualified(relationName, true), + call: `{{ source('${node.source_name}', '${node.name}') }}`, + }) + } + return out +} + +/** How a table written as `parts` matches a relation, or null when it does not. */ +export function matchParts(parts: string[], relation: string[]): Exclude | null { + if (parts.length === 0 || parts.length > relation.length) return null + const tail = relation.slice(relation.length - parts.length) + const same = parts.every((p, i) => p === tail[i]) + const fold = parts.every((p, i) => p.toLowerCase() === tail[i].toLowerCase()) + if (parts.length < relation.length) return fold ? "partial" : null + return same ? "exact" : fold ? "case_insensitive" : null +} + +/** Map each table of `sql` to the manifest's ref() or source() call that builds it. */ +export function mapRelations(tables: string[], sql: string, relations: Relation[], dialect: string): RelationMapping[] { + const splitDots = dialect === "bigquery" + const refs = scanTableReferences(sql, splitDots) + // Reference list keyed by the written form, in text order; only tables altimate-core reported count. + const wanted = new Set(tables) + const entries: Reference[] = [] + const seen = new Set() + const add = (r: Reference) => { + const key = r.parts.join("\u0000") + if (seen.has(key)) return + seen.add(key) + entries.push(r) + } + const covered = new Set() + for (const r of refs) { + const last = r.parts[r.parts.length - 1] + const whole = r.parts.join(".") + if (wanted.has(last) || wanted.has(whole)) { + add(r) + covered.add(wanted.has(last) ? last : whole) + } + } + for (const t of tables) { + if (covered.has(t)) continue + add({ raw: t, parts: splitQualified(t, splitDots) }) + } + return entries.map((entry) => { + const hits: RelationCandidate[] = [] + for (const rel of relations) { + const how = matchParts(entry.parts, rel.parts) + if (!how) continue + hits.push({ + replaceWith: rel.call, + kind: rel.kind, + name: rel.name, + uniqueId: rel.uniqueId, + relationName: rel.relationName, + match: how, + }) + } + hits.sort( + (a, b) => + RANK[a.match] - RANK[b.match] || KIND_RANK[a.kind] - KIND_RANK[b.kind] || a.uniqueId.localeCompare(b.uniqueId), + ) + const best = hits[0] + return { + raw: entry.raw, + replaceWith: best ? best.replaceWith : entry.raw, + match: best ? best.match : "none", + candidates: hits, + } + }) +} + +export function formatMapping(rows: RelationMapping[]): string { + if (rows.length === 0) return "No tables found in the SQL." + const order: RelationMatch[] = ["none", "partial", "case_insensitive", "exact"] + const lines: string[] = [] + for (const match of order) { + for (const r of rows.filter((x) => x.match === match)) { + const more = r.candidates.length > 1 ? ` (${r.candidates.length} candidates)` : "" + lines.push( + match === "none" ? `none: ${r.raw} -> no model or source` : `${match}: ${r.raw} -> ${r.replaceWith}${more}`, + ) + } + } + return lines.join("\n") +} + +type MapRelationsMetadata = { + success: boolean + mappings: RelationMapping[] + error?: string + dialect?: string + manifest_path?: string +} 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.", + description: + "Map the tables in a SQL query to dbt ref() and source() calls, from the project's manifest: each table is matched to the relation_name of a model, snapshot, seed or source. Returns, per table, the replacement, how it matched (exact, case_insensitive, partial, none) and the candidates. Read-only.", 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)"), + sql: z.string().describe("The raw SQL to map"), + dialect: z.string().optional().describe("SQL dialect (default: the manifest's adapter)"), + 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.", + async execute(args): Promise<{ title: string; metadata: MapRelationsMetadata; output: string }> { + const fail = (msg: string) => ({ + title: "dbt Map Relations: ERROR", + metadata: { success: false, error: msg, mappings: [] } as MapRelationsMetadata, + output: msg, + }) + try { + const manifestPath = args.manifest_path ?? (await defaultManifestPath()) + const manifest = loadRawManifest(manifestPath) + if (!manifest) return fail(`No manifest at ${manifestPath}. Run \`dbt parse\` or pass manifest_path.`) + const dialect = args.dialect || detectDialect(manifest) + const result = await Dispatcher.call("altimate_core.metadata", { sql: args.sql, dialect }) + const data = (result.data ?? {}) as Record + const error = result.error ?? data.error + if (!result.success || error) return fail(`Could not read the tables of the SQL: ${error ?? "parse failed"}`) + const tables: string[] = Array.isArray(data.tables) + ? data.tables.filter((t: unknown) => typeof t === "string") + : [] + const mappings = mapRelations(tables, args.sql, manifestRelations(manifest), dialect) + const count = (m: RelationMatch) => mappings.filter((r) => r.match === m).length + return { + title: `dbt Map Relations: ${mappings.length} tables, ${count("none")} unmatched`, + metadata: { success: true, dialect, manifest_path: manifestPath, mappings }, + output: formatMapping(mappings), + } + } catch (e) { + return fail(e instanceof Error ? e.message : String(e)) } }, }) + +async function defaultManifestPath(): Promise { + const { findProjectRoot } = await import("../../../../dbt-tools/src/config") + const found = findProjectRoot(projectRoot()) + const root = found ?? (await (await import("../native/connections/register")).getDbtProjectRoot()) + let real = root ?? "" + try { + real = root ? fs.realpathSync(root) : "" + } catch {} + return path.join(real, "target", "manifest.json") +} diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts new file mode 100644 index 0000000000..f568090e77 --- /dev/null +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -0,0 +1,339 @@ +/** + * dbt_map_relations: maps the tables of a raw SQL query to ref() and source() calls. + * + * The first block pins what altimate-core's extractMetadata returns for quoted, upper-case and + * qualified names: it keeps only the last name part (quotes removed, case kept) and merges tables + * that share it, so the matching rules read the qualifiers back from the SQL text. + */ +import { describe, test, expect, afterEach } from "bun:test" +import { mkdtempSync, rmSync, writeFileSync } from "fs" +import { tmpdir } from "os" +import { join } from "path" +import { Effect } from "effect" +import type { Tool } from "@/tool/tool" +import { SessionID, MessageID } from "@/session/schema" +import { Dispatcher } from "../../src/altimate/native" +import { initTool } from "./tool-fixture" +import { + DbtMapRelationsTool, + manifestRelations, + mapRelations, + matchParts, + scanTableReferences, + splitQualified, +} from "../../src/altimate/tools/dbt-map-relations" + +async function tablesOf(sql: string, dialect: string): Promise { + const result = await Dispatcher.call("altimate_core.metadata", { sql, dialect }) + expect(result.success).toBe(true) + return ((result.data ?? {}) as { tables?: string[] }).tables ?? [] +} + +describe("altimate-core extractMetadata table names", () => { + test("Snowflake: last name part only, quotes removed, case kept, one entry per bare name", async () => { + expect( + await tablesOf( + 'select * from RAW.JAFFLE.Customers c join "raw"."jaffle"."orders" o on 1=1 join analytics.stg_x x on 1=1', + "snowflake", + ), + ).toEqual(["Customers", "orders", "stg_x"]) + expect(await tablesOf("select * from db.sch.orders o join other.orders o2 on 1=1", "snowflake")).toEqual(["orders"]) + expect( + await tablesOf("select * from RAW.JAFFLE.CUSTOMERS a join raw.jaffle.customers b on 1=1", "snowflake"), + ).toEqual(["CUSTOMERS", "customers"]) + expect(await tablesOf('select * from "My Schema"."My Table" t', "snowflake")).toEqual(["My Table"]) + }) + + test("BigQuery: a single backtick path comes back whole, a split one by its last part", async () => { + expect( + await tablesOf( + "select * from `proj.ds.Customers` a join `proj`.`ds`.`Orders` b on 1=1 join ds.x c on 1=1", + "bigquery", + ), + ).toEqual(["Orders", "proj.ds.Customers", "x"]) + expect(await tablesOf("select * from proj-1.ds.Customers a", "bigquery")).toEqual(["Customers"]) + }) + + test("Databricks: three-part and backticked names reduce to the last part", async () => { + expect( + await tablesOf( + "select * from main.raw.Customers a join raw.orders b on 1=1 join `main`.`raw`.`items` c on 1=1", + "databricks", + ), + ).toEqual(["Customers", "items", "orders"]) + }) + + test("CTE names are not tables", async () => { + expect(await tablesOf("with a as (select * from raw.customers) select * from a", "snowflake")).toEqual([ + "customers", + ]) + }) +}) + +describe("splitQualified", () => { + test("splits on dots outside quotes", () => { + expect(splitQualified('"RAW"."JAFFLE"."CUSTOMERS"')).toEqual(["RAW", "JAFFLE", "CUSTOMERS"]) + expect(splitQualified('"a.b"."c"')).toEqual(["a.b", "c"]) + expect(splitQualified("`proj`.`ds`.`t`")).toEqual(["proj", "ds", "t"]) + expect(splitQualified("[db].[dbo].[t]")).toEqual(["db", "dbo", "t"]) + }) + test("backtick dots split only when asked", () => { + expect(splitQualified("`proj.ds.t`", true)).toEqual(["proj", "ds", "t"]) + expect(splitQualified("`proj.ds.t`")).toEqual(["proj.ds.t"]) + }) +}) + +describe("scanTableReferences", () => { + test("reads qualified names from FROM, JOIN and comma lists, written as in the SQL", () => { + const refs = scanTableReferences( + 'select * from raw.jaffle.customers c, "RAW"."JAFFLE"."orders" as o left join other.payments p on 1=1', + false, + ) + expect(refs.map((r) => r.raw)).toEqual(["raw.jaffle.customers", '"RAW"."JAFFLE"."orders"', "other.payments"]) + expect(refs[1].parts).toEqual(["RAW", "JAFFLE", "orders"]) + }) + test("ignores table functions, subqueries, comments and strings", () => { + const refs = scanTableReferences( + "select 'from fake.t' as s from table(flatten(x)) f join (select 1) q on 1=1 /* from c.d */ -- from e.f\n join real.t t on 1=1", + false, + ) + expect(refs.map((r) => r.raw)).toEqual(["real.t"]) + }) + test("reads hyphenated BigQuery project ids", () => { + expect(scanTableReferences("select * from my-proj.ds.tbl", true)[0].parts).toEqual(["my-proj", "ds", "tbl"]) + }) +}) + +describe("matchParts", () => { + const rel = ["RAW", "JAFFLE", "CUSTOMERS"] + test("exact, case_insensitive, partial and no match", () => { + expect(matchParts(["RAW", "JAFFLE", "CUSTOMERS"], rel)).toBe("exact") + expect(matchParts(["raw", "jaffle", "customers"], rel)).toBe("case_insensitive") + expect(matchParts(["jaffle", "customers"], rel)).toBe("partial") + expect(matchParts(["customers"], rel)).toBe("partial") + expect(matchParts(["other", "jaffle", "customers"], rel)).toBeNull() + expect(matchParts(["x", "raw", "jaffle", "customers"], rel)).toBeNull() + }) +}) + +const MANIFEST = { + metadata: { adapter_type: "snowflake", project_name: "shop" }, + nodes: { + "model.shop.customers": { + resource_type: "model", + name: "customers", + package_name: "shop", + relation_name: '"ANALYTICS"."MARTS"."CUSTOMERS"', + config: { materialized: "table" }, + }, + "model.shop.stg_orders": { + resource_type: "model", + name: "stg_orders", + package_name: "shop", + relation_name: '"ANALYTICS"."STAGING"."STG_ORDERS"', + config: { materialized: "view" }, + }, + "model.shop.inlined": { + resource_type: "model", + name: "inlined", + package_name: "shop", + relation_name: null, + config: { materialized: "ephemeral" }, + }, + "model.utils.date_spine": { + resource_type: "model", + name: "date_spine", + package_name: "utils", + relation_name: '"ANALYTICS"."UTILS"."DATE_SPINE"', + config: { materialized: "table" }, + }, + "seed.shop.countries": { + resource_type: "seed", + name: "countries", + package_name: "shop", + relation_name: '"ANALYTICS"."SEEDS"."COUNTRIES"', + }, + "test.shop.not_null": { resource_type: "test", name: "not_null", relation_name: '"X"."Y"."Z"' }, + }, + sources: { + "source.shop.jaffle.customers": { + resource_type: "source", + source_name: "jaffle", + name: "customers", + relation_name: '"RAW"."JAFFLE"."CUSTOMERS"', + }, + "source.shop.jaffle.payments": { + resource_type: "source", + source_name: "jaffle", + name: "payments", + relation_name: '"RAW"."JAFFLE"."PAYMENTS"', + }, + }, +} + +describe("mapRelations", () => { + const relations = manifestRelations(MANIFEST) + const map = (sql: string, tables: string[], dialect = "snowflake") => mapRelations(tables, sql, relations, dialect) + + test("manifestRelations skips ephemeral models and non-relations, and names other packages' models", () => { + const names = relations.map((r) => r.uniqueId).sort() + expect(names).not.toContain("model.shop.inlined") + expect(names).not.toContain("test.shop.not_null") + expect(relations.find((r) => r.name === "date_spine")?.call).toBe("{{ ref('utils', 'date_spine') }}") + }) + + test("exact: every part written, same case", () => { + const [row] = map('select * from "RAW"."JAFFLE"."PAYMENTS"', ["PAYMENTS"]) + expect(row.match).toBe("exact") + expect(row.replaceWith).toBe("{{ source('jaffle', 'payments') }}") + expect(row.raw).toBe('"RAW"."JAFFLE"."PAYMENTS"') + expect(row.candidates).toHaveLength(1) + }) + + test("case_insensitive: every part written, different case", () => { + const [row] = map("select * from raw.jaffle.payments", ["payments"]) + expect(row.match).toBe("case_insensitive") + expect(row.replaceWith).toBe("{{ source('jaffle', 'payments') }}") + }) + + test("partial: a missing database or schema, a model preferred over a source of the same name", () => { + const [row] = map("select * from customers", ["customers"]) + expect(row.match).toBe("partial") + expect(row.replaceWith).toBe("{{ ref('customers') }}") + expect(row.candidates.map((c) => c.kind)).toEqual(["model", "source"]) + }) + + test("a written schema narrows the match to one relation", () => { + const [row] = map("select * from jaffle.customers", ["customers"]) + expect(row.match).toBe("partial") + expect(row.replaceWith).toBe("{{ source('jaffle', 'customers') }}") + expect(row.candidates).toHaveLength(1) + }) + + test("a fully written name picks the source, not the model that shares its last part", () => { + const [row] = map("select * from raw.jaffle.customers", ["customers"]) + expect(row.match).toBe("case_insensitive") + expect(row.replaceWith).toBe("{{ source('jaffle', 'customers') }}") + }) + + test("a better match beats a preferred kind", () => { + const [row] = map('select * from "RAW"."JAFFLE"."CUSTOMERS"', ["CUSTOMERS"]) + expect(row.match).toBe("exact") + expect(row.replaceWith).toBe("{{ source('jaffle', 'customers') }}") + expect(row.candidates).toHaveLength(1) + }) + + test("none: keeps the table as written, no candidates", () => { + const [row] = map("select * from legacy.orders_v1", ["orders_v1"]) + expect(row).toEqual({ raw: "legacy.orders_v1", replaceWith: "legacy.orders_v1", match: "none", candidates: [] }) + }) + + test("a seed is matched with ref()", () => { + const [row] = map("select * from analytics.seeds.countries", ["countries"]) + expect(row.replaceWith).toBe("{{ ref('countries') }}") + expect(row.candidates[0].kind).toBe("seed") + }) + + test("tables that share a last part in different schemas stay separate", () => { + const rows = map("select * from raw.jaffle.customers a join analytics.marts.customers b on 1=1", ["customers"]) + expect(rows.map((r) => [r.raw, r.replaceWith, r.match])).toEqual([ + ["raw.jaffle.customers", "{{ source('jaffle', 'customers') }}", "case_insensitive"], + ["analytics.marts.customers", "{{ ref('customers') }}", "case_insensitive"], + ]) + }) + + test("BigQuery: a backtick path in one piece is split into its parts", () => { + const bq = manifestRelations({ + metadata: { adapter_type: "bigquery", project_name: "shop" }, + nodes: {}, + sources: { + "source.shop.raw.orders": { + resource_type: "source", + source_name: "raw", + name: "orders", + relation_name: "`proj`.`ds`.`orders`", + }, + }, + }) + const [row] = mapRelations(["proj.ds.orders"], "select * from `proj.ds.orders`", bq, "bigquery") + expect(row.match).toBe("exact") + expect(row.replaceWith).toBe("{{ source('raw', 'orders') }}") + }) + + test("a table the text scan does not find is matched by its bare name", () => { + const [row] = map("select 1", ["stg_orders"]) + expect(row.match).toBe("partial") + expect(row.replaceWith).toBe("{{ ref('stg_orders') }}") + }) + + test("a name altimate-core did not report is not a table", () => { + expect(map("with customers as (select 1) select * from customers", [])).toEqual([]) + }) +}) + +const ctx: Tool.Context = { + sessionID: SessionID.make("ses_map_relations"), + messageID: MessageID.make("msg_map_relations"), + agent: "analyst", + abort: new AbortController().signal, + messages: [], + metadata: () => Effect.void, + ask: () => Effect.void, + extra: { forceReadOnly: true }, +} + +describe("dbt_map_relations tool", () => { + let dirs: string[] = [] + afterEach(() => { + for (const d of dirs) rmSync(d, { recursive: true, force: true }) + dirs = [] + }) + function manifestPath(manifest: unknown = MANIFEST): string { + const dir = mkdtempSync(join(tmpdir(), "map-relations-")) + dirs.push(dir) + const p = join(dir, "manifest.json") + writeFileSync(p, JSON.stringify(manifest)) + return p + } + async function run(args: Record) { + const tool = await initTool(DbtMapRelationsTool) + return tool.execute(args, ctx) + } + + test("maps every table, takes the dialect from the manifest, returns rows in metadata", async () => { + const result = await run({ + sql: 'select * from raw.jaffle.customers c join "ANALYTICS"."STAGING"."STG_ORDERS" o on 1=1 join legacy.thing t on 1=1 join payments p on 1=1', + manifest_path: manifestPath(), + }) + expect(result.metadata.success).toBe(true) + expect(result.metadata.dialect).toBe("snowflake") + const rows = result.metadata.mappings as { raw: string; replaceWith: string; match: string }[] + expect(rows.map((r) => [r.raw, r.match])).toEqual([ + ["raw.jaffle.customers", "case_insensitive"], + ['"ANALYTICS"."STAGING"."STG_ORDERS"', "exact"], + ["legacy.thing", "none"], + ["payments", "partial"], + ]) + expect(result.output.split("\n")[0]).toBe("none: legacy.thing -> no model or source") + expect(result.title).toContain("1 unmatched") + }) + + test("a missing manifest is an error that names the path", async () => { + const result = await run({ sql: "select * from t", manifest_path: "/nonexistent/manifest.json" }) + expect(result.metadata.success).toBe(false) + expect(result.output).toContain("/nonexistent/manifest.json") + }) + + test("SQL that does not parse is an error", async () => { + const result = await run({ sql: "select * from {{ ref('x') }}", manifest_path: manifestPath() }) + expect(result.metadata.success).toBe(false) + expect(result.output).toContain("Could not read the tables") + }) + + test("SQL without tables returns no rows", async () => { + const result = await run({ sql: "select 1", manifest_path: manifestPath() }) + expect(result.metadata.success).toBe(true) + expect(result.metadata.mappings).toEqual([]) + expect(result.output).toBe("No tables found in the SQL.") + }) +}) From 8a5c42a7ca840abad99090190bbf7f1e9cf7cce4 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 17:12:22 +0530 Subject: [PATCH 04/32] fix: dbt_map_relations says so when it is run outside a dbt project Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-map-relations.ts | 10 +++--- .../test/altimate/dbt-map-relations.test.ts | 33 ++++++++++++++++++- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 5036de5852..241416a00f 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -159,7 +159,7 @@ function readChain(sql: string, start: number): { raw: string; end: number } | n } if (i >= n) return null i++ - } else if (/[A-Za-z0-9_$\-]/.test(ch) && (i > start || /[A-Za-z_$]/.test(ch))) { + } else if (/[A-Za-z0-9_$-]/.test(ch) && (i > start || /[A-Za-z_$]/.test(ch))) { while (i < n && /[A-Za-z0-9_$]/.test(sql[i])) i++ // BigQuery project ids may contain hyphens (`my-proj.ds.tbl` unquoted). while (sql[i] === "-" && /[A-Za-z0-9_]/.test(sql[i + 1] ?? "")) { @@ -414,6 +414,7 @@ export const DbtMapRelationsTool = Tool.define("dbt_map_relations", { }) try { const manifestPath = args.manifest_path ?? (await defaultManifestPath()) + if (!manifestPath) return fail("No dbt project found here. Run from a dbt project or pass manifest_path.") const manifest = loadRawManifest(manifestPath) if (!manifest) return fail(`No manifest at ${manifestPath}. Run \`dbt parse\` or pass manifest_path.`) const dialect = args.dialect || detectDialect(manifest) @@ -437,13 +438,14 @@ export const DbtMapRelationsTool = Tool.define("dbt_map_relations", { }, }) -async function defaultManifestPath(): Promise { +async function defaultManifestPath(): Promise { const { findProjectRoot } = await import("../../../../dbt-tools/src/config") const found = findProjectRoot(projectRoot()) const root = found ?? (await (await import("../native/connections/register")).getDbtProjectRoot()) - let real = root ?? "" + if (!root) return null + let real = root try { - real = root ? fs.realpathSync(root) : "" + real = fs.realpathSync(root) } catch {} return path.join(real, "target", "manifest.json") } diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index f568090e77..1f55b1de43 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -6,7 +6,7 @@ * that share it, so the matching rules read the qualifiers back from the SQL text. */ import { describe, test, expect, afterEach } from "bun:test" -import { mkdtempSync, rmSync, writeFileSync } from "fs" +import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "fs" import { tmpdir } from "os" import { join } from "path" import { Effect } from "effect" @@ -324,6 +324,37 @@ describe("dbt_map_relations tool", () => { expect(result.output).toContain("/nonexistent/manifest.json") }) + test("without manifest_path the dbt project around the working directory supplies target/manifest.json", async () => { + const dir = realpathSync(mkdtempSync(join(tmpdir(), "map-relations-project-"))) + dirs.push(dir) + writeFileSync(join(dir, "dbt_project.yml"), "name: jaffle\n") + mkdirSync(join(dir, "target")) + writeFileSync(join(dir, "target", "manifest.json"), JSON.stringify(MANIFEST)) + const cwd = process.cwd() + process.chdir(dir) + try { + const result = await run({ sql: "select * from legacy.thing" }) + expect(result.metadata.success).toBe(true) + expect(result.metadata.manifest_path).toBe(join(dir, "target", "manifest.json")) + } finally { + process.chdir(cwd) + } + }) + + test("outside a dbt project, without manifest_path, the error says so", async () => { + const dir = realpathSync(mkdtempSync(join(tmpdir(), "map-relations-bare-"))) + dirs.push(dir) + const cwd = process.cwd() + process.chdir(dir) + try { + const result = await run({ sql: "select * from t" }) + expect(result.metadata.success).toBe(false) + expect(result.output).toBe("No dbt project found here. Run from a dbt project or pass manifest_path.") + } finally { + process.chdir(cwd) + } + }) + test("SQL that does not parse is an error", async () => { const result = await run({ sql: "select * from {{ ref('x') }}", manifest_path: manifestPath() }) expect(result.metadata.success).toBe(false) From 0f33f9a68b7a4129c586e604948b0aa8a7af3ca6 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 17:39:37 +0530 Subject: [PATCH 05/32] fix: dbt_map_relations reads the SQL with a tokenizer, handles CTEs and versioned models The table scan now walks tokens instead of matching keywords in text: quoted names, strings and dollar quotes are skipped by dialect, dots may have whitespace or comments around them, tables after a subquery or table function in a FROM list are read, a CTE's name is not a table, and a physical table a CTE shares its name with keeps its qualifiers. Versioned models are referenced with v=. Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-map-relations.ts | 329 ++++++++++++------ .../test/altimate/dbt-map-relations.test.ts | 154 +++++++- 2 files changed, 364 insertions(+), 119 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 241416a00f..d297bd1b6e 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -91,9 +91,21 @@ export function splitQualified(text: string, splitBacktickDots = false): string[ } /** A table reference found in the SQL text: the text as written and its unquoted parts. */ -type Reference = { raw: string; parts: string[] } +type Reference = { raw: string; parts: string[]; start?: number } + +/** What the scan of a query found: its table references in text order, and the names its CTEs define. */ +export type Scan = { + refs: Reference[] + /** By lower-cased name: whether the WITH is recursive, and the text offsets of each body that defines the name. */ + ctes: Map +} + +// Dialects whose double quotes (and, for some, backslashes) belong to string literals. +const DOUBLE_QUOTE_STRING = new Set(["bigquery", "databricks", "spark", "mysql", "hive"]) +const BACKSLASH_ESCAPE = new Set(["bigquery", "databricks", "spark", "mysql", "hive", "clickhouse", "snowflake"]) +const DOLLAR_QUOTE = new Set(["snowflake", "postgres", "redshift", "duckdb"]) +const BRACKET_QUOTE = new Set(["tsql", "sqlserver", "fabric", "synapse"]) -const IDENT_START = /[A-Za-z_$"`[]/ const RESERVED = new Set([ "select", "from", @@ -133,133 +145,220 @@ const RESERVED = new Set([ "pivot", "unpivot", "tablesample", - "inner", "straight_join", "asof", + "with", + "and", + "or", + "not", + "is", + "in", ]) +// Clause words that start a list of tables. `from` inside these calls reads a column or a string instead. +const TABLE_KEYWORDS = new Set(["from", "join", "into", "update", "using"]) +const FROM_INSIDE_CALL = new Set(["extract", "trim", "substring", "overlay", "position"]) -/** One identifier chain (`a.b.c`, each part quoted or not) starting at `i`; null when there is none. */ -function readChain(sql: string, start: number): { raw: string; end: number } | null { - let i = start +type Token = { kind: "word" | "quoted" | "punct" | "other"; start: number; end: number; text: string } + +/** The tokens of the SQL with comments and string literals dropped; a quoted name stays one token. */ +function tokenize(sql: string, dialect: string): Token[] { + const out: Token[] = [] const n = sql.length - while (i < n) { - const ch = sql[i] - if (ch === '"' || ch === "`" || ch === "[") { - const close = ch === "[" ? "]" : ch - i++ - while (i < n) { - if (sql[i] === close) { - if (sql[i + 1] === close && close !== "]") { - i += 2 - continue - } - break - } - i++ - } - if (i >= n) return null - i++ - } else if (/[A-Za-z0-9_$-]/.test(ch) && (i > start || /[A-Za-z_$]/.test(ch))) { - while (i < n && /[A-Za-z0-9_$]/.test(sql[i])) i++ - // BigQuery project ids may contain hyphens (`my-proj.ds.tbl` unquoted). - while (sql[i] === "-" && /[A-Za-z0-9_]/.test(sql[i + 1] ?? "")) { - i++ - while (i < n && /[A-Za-z0-9_$]/.test(sql[i])) i++ - } - } else return null - if (sql[i] === ".") { - i++ - continue + let i = 0 + const doubleIsString = DOUBLE_QUOTE_STRING.has(dialect) + const backslash = BACKSLASH_ESCAPE.has(dialect) + const push = (kind: Token["kind"], start: number, end: number) => + out.push({ kind, start, end, text: sql.slice(start, end) }) + const skipString = (quote: string, from: number) => { + let j = from + while (j < n) { + if (backslash && sql[j] === "\\") j += 2 + else if (sql[j] === quote) { + if (sql[j + 1] === quote) j += 2 + else return j + 1 + } else j++ } - break + return n } - return i > start ? { raw: sql.slice(start, i), end: i } : null -} - -/** Replace comments and string literals with spaces so the scan never reads inside them. */ -function blank(sql: string): string { - let out = "" - let i = 0 - while (i < sql.length) { + const lastWord = () => { + const last = out[out.length - 1] + return last?.kind === "word" ? last.text.toLowerCase() : "" + } + while (i < n) { + const ch = sql[i] const two = sql.slice(i, i + 2) - if (two === "--") { - while (i < sql.length && sql[i] !== "\n") { - out += " " - i++ - } + if (/\s/.test(ch)) i++ + else if (two === "--") { + while (i < n && sql[i] !== "\n") i++ } else if (two === "/*") { const end = sql.indexOf("*/", i + 2) - const stop = end < 0 ? sql.length : end + 2 - out += sql.slice(i, stop).replace(/[^\n]/g, " ") - i = stop - } else if (sql[i] === "'") { + i = end < 0 ? n : end + 2 + } else if (ch === "'" || (ch === '"' && doubleIsString)) { + const triple = dialect === "bigquery" && sql.startsWith(ch.repeat(3), i) + if (triple) { + const end = sql.indexOf(ch.repeat(3), i + 3) + i = end < 0 ? n : end + 3 + } else i = skipString(ch, i + 1) + } else if ( + ch === "$" && + DOLLAR_QUOTE.has(dialect) && + /^\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$/.test(sql.slice(i, i + 40)) + ) { + const tag = /^\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$/.exec(sql.slice(i, i + 40))![0] + const end = sql.indexOf(tag, i + tag.length) + i = end < 0 ? n : end + tag.length + } else if (ch === '"' || ch === "`" || (ch === "[" && BRACKET_QUOTE.has(dialect))) { + const close = ch === "[" ? "]" : ch let j = i + 1 - while (j < sql.length) { - if (sql[j] === "\\") j += 2 - else if (sql[j] === "'") { - if (sql[j + 1] === "'") j += 2 + while (j < n) { + if (sql[j] === close) { + if (sql[j + 1] === close && close !== "]") j += 2 else break } else j++ } - const stop = Math.min(j + 1, sql.length) - out += sql.slice(i, stop).replace(/[^\n]/g, " ") - i = stop - } else if (sql[i] === '"' || sql[i] === "`") { - // Keep quoted identifiers intact; skip over them so a quote inside one is not misread. - const close = sql[i] + const end = Math.min(j + 1, n) + push("quoted", i, end) + i = end + } else if (/[A-Za-z_]/.test(ch)) { + let j = i + 1 + while (j < n && /[A-Za-z0-9_$]/.test(sql[j])) j++ + // BigQuery project ids may contain hyphens (`my-proj.ds.tbl` unquoted), but only where a table is named. + if (dialect === "bigquery" && (TABLE_KEYWORDS.has(lastWord()) || out[out.length - 1]?.text === ",")) { + while (sql[j] === "-" && /[A-Za-z0-9_]/.test(sql[j + 1] ?? "")) { + j++ + while (j < n && /[A-Za-z0-9_$]/.test(sql[j])) j++ + } + } + push("word", i, j) + i = j + } else if (/[0-9]/.test(ch)) { let j = i + 1 - while (j < sql.length && sql[j] !== close) j++ - out += sql.slice(i, Math.min(j + 1, sql.length)) - i = Math.min(j + 1, sql.length) + while (j < n && /[A-Za-z0-9_]/.test(sql[j])) j++ + push("other", i, j) + i = j } else { - out += sql[i] + push(/[.,()]/.test(ch) ? "punct" : "other", i, i + 1) i++ } } return out } -function skipSpace(sql: string, i: number): number { - while (i < sql.length && /\s/.test(sql[i])) i++ - return i +/** The name a quoted token or word holds: quotes removed, doubled quotes undone. */ +function nameOf(token: Token, dialect: string): string[] { + if (token.kind !== "quoted") return [token.text] + const open = token.text[0] + const close = open === "[" ? "]" : open + const body = token.text.slice(1, token.text.endsWith(close) && token.text.length > 1 ? -1 : undefined) + const name = close === "]" ? body : body.split(close + close).join(close) + // BigQuery reads `proj.ds.tbl` in one pair of backticks as a path. + return open === "`" && dialect === "bigquery" ? name.split(".") : [name] } +/** Index of the token after the `)` that closes the `(` at `at`. */ +function afterParens(tokens: Token[], at: number): number { + let depth = 0 + for (let i = at; i < tokens.length; i++) { + if (tokens[i].text === "(" && tokens[i].kind === "punct") depth++ + else if (tokens[i].text === ")" && tokens[i].kind === "punct" && --depth === 0) return i + 1 + } + return tokens.length +} + +const isPunct = (token: Token | undefined, text: string) => token?.kind === "punct" && token.text === text + /** - * The table references of FROM, JOIN, INTO and UPDATE clauses, in text order, written as in the SQL. - * altimate-core's table list keeps only the last name part, so the schema and database a person wrote - * are read back from the text here; the list decides which of these are real tables. + * The table references of FROM, JOIN, INTO, UPDATE and USING clauses, in text order, written as in the SQL, + * and the names the query's CTEs define. altimate-core's table list keeps only the last name part and leaves + * out any table named like a CTE, so the schema and database a person wrote are read back from the text here. */ -export function scanTableReferences(sql: string, splitBacktickDots: boolean): Reference[] { - const text = blank(sql) - const found: Reference[] = [] - const re = /\b(from|join|into|update|using|table)\b/gi - let m: RegExpExecArray | null - while ((m = re.exec(text))) { - let i = skipSpace(text, m.index + m[0].length) +export function scanTableReferences(sql: string, dialect: string): Scan { + const tokens = tokenize(sql, dialect) + const refs: Reference[] = [] + const ctes: Scan["ctes"] = new Map() + const calls: string[] = [] + + // `name [(columns)] as [not] [materialized] (`, then `,` for the next CTE. + const readCtes = (start: number) => { + let i = start + const recursive = tokens[i]?.text.toLowerCase() === "recursive" + if (recursive) i++ + while (tokens[i] && (tokens[i].kind === "word" || tokens[i].kind === "quoted")) { + const name = nameOf(tokens[i], dialect)[0].toLowerCase() + let j = i + 1 + if (isPunct(tokens[j], "(")) j = afterParens(tokens, j) + if (tokens[j]?.text.toLowerCase() !== "as") return + j++ + if (tokens[j]?.text.toLowerCase() === "not") j++ + if (tokens[j]?.text.toLowerCase() === "materialized") j++ + if (!isPunct(tokens[j], "(")) return + const open = tokens[j].start + j = afterParens(tokens, j) + const known = ctes.get(name) ?? { recursive, bodies: [] } + known.bodies.push([open, tokens[j - 1].end]) + ctes.set(name, known) + if (!isPunct(tokens[j], ",")) return + i = j + 1 + } + } + + const readList = (start: number, strict: boolean) => { + let i = start for (;;) { - if (!IDENT_START.test(text[i] ?? "")) break - const chain = readChain(text, i) - if (!chain) break - i = skipSpace(text, chain.end) - // A function call (`flatten(...)`, `unnest(...)`) is not a table. - if (text[i] === "(") break - if (!RESERVED.has(chain.raw.toLowerCase())) { - found.push({ raw: chain.raw, parts: splitQualified(chain.raw, splitBacktickDots) }) - } else break - // An alias, with or without AS, then a comma continues a FROM list. - const asM = /^as\s+/i.exec(text.slice(i)) - if (asM) i = skipSpace(text, i + asM[0].length) - const alias = readChain(text, i) - if (alias && !alias.raw.includes(".") && !RESERVED.has(alias.raw.toLowerCase())) i = skipSpace(text, alias.end) - else if (asM && alias) i = skipSpace(text, alias.end) - if (text[i] === ",") { - i = skipSpace(text, i + 1) - continue + if (isPunct(tokens[i], "(")) i = afterParens(tokens, i) + else { + const first = tokens[i] + if (!first || (first.kind !== "word" && first.kind !== "quoted")) return + if (first.kind === "word" && RESERVED.has(first.text.toLowerCase())) return + const parts: string[] = [...nameOf(first, dialect)] + let at = i + while (isPunct(tokens[at + 1], ".") && ["word", "quoted"].includes(tokens[at + 2]?.kind ?? "")) { + at += 2 + parts.push(...nameOf(tokens[at], dialect)) + } + const last = tokens[at] + i = at + 1 + if (isPunct(tokens[i], "(") && strict) i = afterParens(tokens, i) + else refs.push({ raw: sql.slice(first.start, last.end), parts, start: first.start }) + } + // An alias with or without AS, then its column list, then a comma continues a FROM list. + if (tokens[i]?.text.toLowerCase() === "as") i++ + const alias = tokens[i] + if (alias && (alias.kind === "quoted" || (alias.kind === "word" && !RESERVED.has(alias.text.toLowerCase())))) { + i++ + if (isPunct(tokens[i], "(")) i = afterParens(tokens, i) + } + if (isPunct(tokens[i], ",")) i++ + else return + } + } + + for (let i = 0; i < tokens.length; i++) { + const token = tokens[i] + if (isPunct(token, "(")) { + const before = tokens[i - 1] + calls.push(before?.kind === "word" ? before.text.toLowerCase() : "") + } else if (isPunct(token, ")")) calls.pop() + else if (token.kind === "word") { + const word = token.text.toLowerCase() + if (word === "with") readCtes(i + 1) + else if (TABLE_KEYWORDS.has(word)) { + if ( + word === "from" && + (FROM_INSIDE_CALL.has(calls[calls.length - 1] ?? "") || tokens[i - 1]?.text.toLowerCase() === "distinct") + ) + continue + // A name followed by "(" after FROM, JOIN or USING is a table function; after INTO or UPDATE it is a table. + readList( + word === "into" && tokens[i + 1]?.text.toLowerCase() === "table" ? i + 2 : i + 1, + word === "from" || word === "join" || word === "using", + ) } - break } } - return found + // An outer list is read before the subquery inside it: put them back in text order. + refs.sort((a, b) => a.start! - b.start!) + return { refs, ctes } } /** The model, snapshot, seed and source nodes of a manifest, each with its relation's name parts. */ @@ -288,7 +387,12 @@ export function manifestRelations(manifest: any): Relation[] { typeof node.package_name === "string" && rootProject && node.package_name !== rootProject ? node.package_name : undefined - const call = pkg ? `{{ ref('${pkg}', '${node.name}') }}` : `{{ ref('${node.name}') }}` + // Every version of a model shares its name; a bare ref() resolves to the latest one, so a version is named. + const version = + node.version === null || node.version === undefined || node.version === "" + ? "" + : `, v=${JSON.stringify(node.version)}` + const call = pkg ? `{{ ref('${pkg}', '${node.name}'${version}) }}` : `{{ ref('${node.name}'${version}) }}` out.push({ kind, name: node.name, uniqueId, relationName, parts: splitQualified(relationName, true), call }) } const sources: Record = manifest?.sources ?? {} @@ -319,14 +423,13 @@ export function matchParts(parts: string[], relation: string[]): Exclude t.toLowerCase())) const entries: Reference[] = [] const seen = new Set() const add = (r: Reference) => { - const key = r.parts.join("\u0000") + const key = r.raw.replace(/\s+/g, " ") if (seen.has(key)) return seen.add(key) entries.push(r) @@ -335,14 +438,18 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ for (const r of refs) { const last = r.parts[r.parts.length - 1] const whole = r.parts.join(".") - if (wanted.has(last) || wanted.has(whole)) { + const cte = ctes.get(last.toLowerCase()) + // A CTE's own name inside its own body is the real table unless the WITH is recursive. + const inOwnBody = !!cte && !cte.recursive && cte.bodies.some(([from, to]) => r.start! >= from && r.start! < to) + if (r.parts.length === 1 && cte && !inOwnBody) continue + if (wanted.has(last.toLowerCase()) || wanted.has(whole.toLowerCase())) { add(r) - covered.add(wanted.has(last) ? last : whole) - } + covered.add((wanted.has(last.toLowerCase()) ? last : whole).toLowerCase()) + } else if (cte) add(r) } for (const t of tables) { - if (covered.has(t)) continue - add({ raw: t, parts: splitQualified(t, splitDots) }) + if (covered.has(t.toLowerCase()) || ctes.has(t.toLowerCase())) continue + add({ raw: t, parts: splitQualified(t, dialect === "bigquery") }) } return entries.map((entry) => { const hits: RelationCandidate[] = [] @@ -417,7 +524,7 @@ export const DbtMapRelationsTool = Tool.define("dbt_map_relations", { if (!manifestPath) return fail("No dbt project found here. Run from a dbt project or pass manifest_path.") const manifest = loadRawManifest(manifestPath) if (!manifest) return fail(`No manifest at ${manifestPath}. Run \`dbt parse\` or pass manifest_path.`) - const dialect = args.dialect || detectDialect(manifest) + const dialect = (args.dialect || detectDialect(manifest)).toLowerCase() const result = await Dispatcher.call("altimate_core.metadata", { sql: args.sql, dialect }) const data = (result.data ?? {}) as Record const error = result.error ?? data.error diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index 1f55b1de43..83331de958 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -84,23 +84,79 @@ describe("splitQualified", () => { }) describe("scanTableReferences", () => { + const raws = (sql: string, dialect = "snowflake") => scanTableReferences(sql, dialect).refs.map((r) => r.raw) + test("reads qualified names from FROM, JOIN and comma lists, written as in the SQL", () => { - const refs = scanTableReferences( + const { refs } = scanTableReferences( 'select * from raw.jaffle.customers c, "RAW"."JAFFLE"."orders" as o left join other.payments p on 1=1', - false, + "snowflake", ) expect(refs.map((r) => r.raw)).toEqual(["raw.jaffle.customers", '"RAW"."JAFFLE"."orders"', "other.payments"]) expect(refs[1].parts).toEqual(["RAW", "JAFFLE", "orders"]) }) test("ignores table functions, subqueries, comments and strings", () => { - const refs = scanTableReferences( - "select 'from fake.t' as s from table(flatten(x)) f join (select 1) q on 1=1 /* from c.d */ -- from e.f\n join real.t t on 1=1", - false, - ) - expect(refs.map((r) => r.raw)).toEqual(["real.t"]) + expect( + raws( + "select 'from fake.t' as s from table(flatten(x)) f join (select 1) q on 1=1 /* from c.d */ -- from e.f\n join real.t t on 1=1", + ), + ).toEqual(["real.t"]) }) test("reads hyphenated BigQuery project ids", () => { - expect(scanTableReferences("select * from my-proj.ds.tbl", true)[0].parts).toEqual(["my-proj", "ds", "tbl"]) + expect(scanTableReferences("select * from my-proj.ds.tbl", "bigquery").refs[0].parts).toEqual([ + "my-proj", + "ds", + "tbl", + ]) + }) + test("whitespace or a comment around a dot does not drop the qualifier", () => { + expect(raws("select * from db . right . orders")).toEqual(["db . right . orders"]) + expect(scanTableReferences("select * from other/*gap*/.marts.customers", "snowflake").refs[0].parts).toEqual([ + "other", + "marts", + "customers", + ]) + }) + test("a table after a subquery or table function in a comma list is still read", () => { + expect(raws("select * from (select 1) x, db.right.orders")).toEqual(["db.right.orders"]) + expect(raws("select * from table(generator(rowcount => 1)) x, db.right.orders")).toEqual(["db.right.orders"]) + expect(raws("select * from (select * from db.wrong.orders) x, db.right.orders")).toEqual([ + "db.wrong.orders", + "db.right.orders", + ]) + }) + test("brackets quote names for SQL Server dialects only; ClickHouse double quotes are identifiers", () => { + expect(raws("select * from [db].[s].[t]", "tsql")).toEqual(["[db].[s].[t]"]) + expect(raws('select * from "db"."orders"', "clickhouse")).toEqual(['"db"."orders"']) + }) + test("a hyphenated BigQuery project id is read after a comma and `insert into table` names its table", () => { + expect(raws("select * from a, my-proj.ds.t", "bigquery")).toEqual(["a", "my-proj.ds.t"]) + expect(raws("insert into table db.t select 1", "databricks")).toEqual(["db.t"]) + }) + test("clause words inside quoted names, strings and dollar quotes are not clauses", () => { + expect(raws('select "from orders", "join orders" as "using orders" from db.right.orders')).toEqual([ + "db.right.orders", + ]) + expect(raws("select $$from orders $$ as s from db.right.orders")).toEqual(["db.right.orders"]) + expect(raws("select $q1$from orders $q1$ as s from db.right.orders", "postgres")).toEqual(["db.right.orders"]) + expect(raws('select "from orders", r"from orders " from db.right.orders', "bigquery")).toEqual(["db.right.orders"]) + expect(raws('select "from orders" from db.right.orders', "databricks")).toEqual(["db.right.orders"]) + }) + test("EXTRACT, TRIM and IS DISTINCT FROM are not table clauses", () => { + expect(raws("select extract(year from d), trim(both 'x' from s) from t where a is distinct from orders")).toEqual([ + "t", + ]) + }) + test("a backslash ends a string in dialects where it does not escape", () => { + expect(raws("select 'C:\\' as p from db.right.orders", "postgres")).toEqual(["db.right.orders"]) + expect(raws("select 'it\\'s' as p from db.right.orders", "snowflake")).toEqual(["db.right.orders"]) + }) + test("CTE names are collected, and a table named like a CTE keeps its qualifiers", () => { + const { refs, ctes } = scanTableReferences( + "with orders as (select * from db.raw.orders), b(x) as materialized (select 1) select * from orders", + "snowflake", + ) + expect([...ctes.keys()]).toEqual(["orders", "b"]) + expect(refs.map((r) => r.raw)).toEqual(["db.raw.orders", "orders"]) }) }) @@ -266,6 +322,68 @@ describe("mapRelations", () => { expect(row.replaceWith).toBe("{{ ref('stg_orders') }}") }) + test("a CTE named like a table is not mapped, and the physical table it reads keeps its mapping", () => { + const sql = "with customers as (select * from raw.jaffle.customers) select * from customers" + expect(map(sql, []).map((r) => [r.raw, r.match])).toEqual([["raw.jaffle.customers", "case_insensitive"]]) + expect(map(sql, ["customers"]).map((r) => [r.raw, r.match])).toEqual([["raw.jaffle.customers", "case_insensitive"]]) + }) + + test("a table named like its own CTE, read inside that CTE, is the real table; a recursive CTE's own name is not", () => { + expect(map("with orders as (select * from orders) select * from orders", []).map((r) => r.raw)).toEqual(["orders"]) + expect( + map("with recursive customers as (select 1 union all select * from customers) select * from customers", []), + ).toEqual([]) + }) + + test("differently written forms of one table stay separate rows; an identical repeat does not", () => { + const sql = + 'select * from raw.jaffle.payments a join "RAW"."JAFFLE"."PAYMENTS" b on 1=1 join raw.jaffle.payments c on 1=1' + expect(map(sql, ["PAYMENTS"]).map((r) => r.raw)).toEqual(["raw.jaffle.payments", '"RAW"."JAFFLE"."PAYMENTS"']) + }) + + test("a versioned model is referenced by its version", () => { + const versioned = manifestRelations({ + metadata: { project_name: "shop" }, + nodes: { + "model.shop.orders.v1": { + resource_type: "model", + name: "orders", + package_name: "shop", + version: 1, + relation_name: '"A"."B"."ORDERS_V1"', + config: {}, + }, + "model.shop.orders.v2": { + resource_type: "model", + name: "orders", + package_name: "shop", + version: "2", + relation_name: '"A"."B"."ORDERS"', + config: {}, + }, + "model.pkg.dim.v3": { + resource_type: "model", + name: "dim", + package_name: "pkg", + version: 3, + relation_name: '"A"."B"."DIM"', + config: {}, + }, + }, + }) + const rows = mapRelations( + ["ORDERS_V1", "ORDERS", "DIM"], + 'select * from "A"."B"."ORDERS_V1", "A"."B"."ORDERS", "A"."B"."DIM"', + versioned, + "snowflake", + ) + expect(rows.map((r) => r.replaceWith)).toEqual([ + "{{ ref('orders', v=1) }}", + "{{ ref('orders', v=\"2\") }}", + "{{ ref('pkg', 'dim', v=3) }}", + ]) + }) + test("a name altimate-core did not report is not a table", () => { expect(map("with customers as (select 1) select * from customers", [])).toEqual([]) }) @@ -331,6 +449,8 @@ describe("dbt_map_relations tool", () => { mkdirSync(join(dir, "target")) writeFileSync(join(dir, "target", "manifest.json"), JSON.stringify(MANIFEST)) const cwd = process.cwd() + const home = process.env.HOME + process.env.HOME = dir process.chdir(dir) try { const result = await run({ sql: "select * from legacy.thing" }) @@ -338,6 +458,8 @@ describe("dbt_map_relations tool", () => { expect(result.metadata.manifest_path).toBe(join(dir, "target", "manifest.json")) } finally { process.chdir(cwd) + if (home === undefined) delete process.env.HOME + else process.env.HOME = home } }) @@ -345,6 +467,8 @@ describe("dbt_map_relations tool", () => { const dir = realpathSync(mkdtempSync(join(tmpdir(), "map-relations-bare-"))) dirs.push(dir) const cwd = process.cwd() + const home = process.env.HOME + process.env.HOME = dir process.chdir(dir) try { const result = await run({ sql: "select * from t" }) @@ -352,9 +476,23 @@ describe("dbt_map_relations tool", () => { expect(result.output).toBe("No dbt project found here. Run from a dbt project or pass manifest_path.") } finally { process.chdir(cwd) + if (home === undefined) delete process.env.HOME + else process.env.HOME = home } }) + test("with the real parser, a table a CTE shares its name with and a dot spaced out are both mapped", async () => { + const result = await run({ + sql: "with customers as (select * from raw.jaffle.customers) select * from customers c join analytics . staging . stg_orders o on 1=1", + manifest_path: manifestPath(), + }) + const rows = result.metadata.mappings as { raw: string; match: string }[] + expect(rows.map((r) => [r.raw, r.match])).toEqual([ + ["raw.jaffle.customers", "case_insensitive"], + ["analytics . staging . stg_orders", "case_insensitive"], + ]) + }) + test("SQL that does not parse is an error", async () => { const result = await run({ sql: "select * from {{ ref('x') }}", manifest_path: manifestPath() }) expect(result.metadata.success).toBe(false) From acd8faa7a6f08f861e1df1c35cf07557d1a3799c Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 21:33:10 +0530 Subject: [PATCH 06/32] fix: dbt_map_relations tests register the parser handlers and the placeholder test skips the implemented tool Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/test/altimate/dbt-adapter-access.test.ts | 4 +++- packages/opencode/test/altimate/dbt-map-relations.test.ts | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/opencode/test/altimate/dbt-adapter-access.test.ts b/packages/opencode/test/altimate/dbt-adapter-access.test.ts index b2a012ed58..a0b84aa447 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) +/** Already implemented, so it is not a placeholder. */ +const IMPLEMENTED_TOOL = "dbt_map_relations" const ctx: Tool.Context = { sessionID: SessionID.make("ses_dbt_adapter_access"), @@ -82,7 +84,7 @@ describe("feature tools in the allow-list", () => { }) test("each placeholder answers that it isn't implemented, as a failure", async () => { - for (const id of FEATURE_TOOLS) { + for (const id of FEATURE_TOOLS.filter((id) => id !== IMPLEMENTED_TOOL)) { 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-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index 83331de958..de23eed06e 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -5,7 +5,7 @@ * qualified names: it keeps only the last name part (quotes removed, case kept) and merges tables * that share it, so the matching rules read the qualifiers back from the SQL text. */ -import { describe, test, expect, afterEach } from "bun:test" +import { describe, test, expect, afterEach, beforeAll } from "bun:test" import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "fs" import { tmpdir } from "os" import { join } from "path" @@ -13,6 +13,7 @@ import { Effect } from "effect" import type { Tool } from "@/tool/tool" import { SessionID, MessageID } from "@/session/schema" import { Dispatcher } from "../../src/altimate/native" +import { registerAll as registerAltimateCore } from "../../src/altimate/native/altimate-core" import { initTool } from "./tool-fixture" import { DbtMapRelationsTool, @@ -23,6 +24,9 @@ import { splitQualified, } from "../../src/altimate/tools/dbt-map-relations" +// Other test files reset the dispatcher; register the parser handlers this file depends on. +beforeAll(() => registerAltimateCore()) + async function tablesOf(sql: string, dialect: string): Promise { const result = await Dispatcher.call("altimate_core.metadata", { sql, dialect }) expect(result.success).toBe(true) From efc72f6204bbf2aaa97ec5ea691c774b4350b0fc Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sun, 15 Nov 2026 22:13:58 +0530 Subject: [PATCH 07/32] fix: dbt_map_relations escapes names in generated calls, splits backtick dots only for BigQuery, continues past LATERAL and keeps quoted whitespace Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/api/client.ts | 1 + .../src/altimate/tools/dbt-adapter-access.ts | 1 + .../src/altimate/tools/dbt-map-relations.ts | 28 ++++++++++++---- .../test/altimate/dbt-map-relations.test.ts | 32 +++++++++++++++++++ 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/packages/opencode/src/altimate/api/client.ts b/packages/opencode/src/altimate/api/client.ts index cb53c0ec7b..76f91303af 100644 --- a/packages/opencode/src/altimate/api/client.ts +++ b/packages/opencode/src/altimate/api/client.ts @@ -353,6 +353,7 @@ export namespace AltimateApi { * 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 { + // The body is whatever JSON the endpoint returns; each endpoint function in other files types its own result. let creds: AltimateCredentials try { creds = await getCredentials() diff --git a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts index d73dd7e9c5..4b46a7fbf6 100644 --- a/packages/opencode/src/altimate/tools/dbt-adapter-access.ts +++ b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts @@ -8,6 +8,7 @@ export type DbtAdapterRefusal = "read_only" | "other_project" | "no_project" | " /** The project's dbt adapter, or why this call may not use it (`reason` is a sentence for the user). */ export type DbtAdapterAccess = + // The adapter is the untyped dbt integration object from the dbt-integration package. | { adapter: any; refusal?: undefined; reason?: undefined } | { adapter?: undefined; refusal: DbtAdapterRefusal; reason: string } diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index d297bd1b6e..137e47edcc 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -305,6 +305,8 @@ export function scanTableReferences(sql: string, dialect: string): Scan { const readList = (start: number, strict: boolean) => { let i = start for (;;) { + // LATERAL prefixes a table expression in the list; the list goes on after it. + if (tokens[i]?.kind === "word" && tokens[i].text.toLowerCase() === "lateral") i++ if (isPunct(tokens[i], "(")) i = afterParens(tokens, i) else { const first = tokens[i] @@ -361,9 +363,17 @@ export function scanTableReferences(sql: string, dialect: string): Scan { return { refs, ctes } } +/** A single-quoted Jinja string literal. */ +function jinjaString(value: string): string { + return `'${value.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'` +} + /** The model, snapshot, seed and source nodes of a manifest, each with its relation's name parts. */ +// The manifest is parsed JSON of a shape dbt owns and versions; its fields are checked where they are read. export function manifestRelations(manifest: any): Relation[] { const rootProject: string | undefined = manifest?.metadata?.project_name + // Only BigQuery names put dots inside backticks; elsewhere a dot in a quoted name is part of the name. + const splitDots = detectDialect(manifest).toLowerCase() === "bigquery" const out: Relation[] = [] const relationOf = (node: any): string | undefined => { if (typeof node?.relation_name === "string" && node.relation_name) return node.relation_name @@ -392,8 +402,10 @@ export function manifestRelations(manifest: any): Relation[] { node.version === null || node.version === undefined || node.version === "" ? "" : `, v=${JSON.stringify(node.version)}` - const call = pkg ? `{{ ref('${pkg}', '${node.name}'${version}) }}` : `{{ ref('${node.name}'${version}) }}` - out.push({ kind, name: node.name, uniqueId, relationName, parts: splitQualified(relationName, true), call }) + const call = pkg + ? `{{ ref(${jinjaString(pkg)}, ${jinjaString(node.name)}${version}) }}` + : `{{ ref(${jinjaString(node.name)}${version}) }}` + out.push({ kind, name: node.name, uniqueId, relationName, parts: splitQualified(relationName, splitDots), call }) } const sources: Record = manifest?.sources ?? {} for (const [uniqueId, node] of Object.entries(sources)) { @@ -404,8 +416,8 @@ export function manifestRelations(manifest: any): Relation[] { name: node.name, uniqueId, relationName, - parts: splitQualified(relationName, true), - call: `{{ source('${node.source_name}', '${node.name}') }}`, + parts: splitQualified(relationName, splitDots), + call: `{{ source(${jinjaString(node.source_name)}, ${jinjaString(node.name)}) }}`, }) } return out @@ -418,7 +430,9 @@ export function matchParts(parts: string[], relation: string[]): Exclude p === tail[i]) const fold = parts.every((p, i) => p.toLowerCase() === tail[i].toLowerCase()) if (parts.length < relation.length) return fold ? "partial" : null - return same ? "exact" : fold ? "case_insensitive" : null + if (same) return "exact" + if (fold) return "case_insensitive" + return null } /** Map each table of `sql` to the manifest's ref() or source() call that builds it. */ @@ -429,7 +443,7 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ const entries: Reference[] = [] const seen = new Set() const add = (r: Reference) => { - const key = r.raw.replace(/\s+/g, " ") + const key = r.raw if (seen.has(key)) return seen.add(key) entries.push(r) @@ -552,7 +566,7 @@ async function defaultManifestPath(): Promise { if (!root) return null let real = root try { - real = fs.realpathSync(root) + real = await fs.promises.realpath(root) } catch {} return path.join(real, "target", "manifest.json") } diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index de23eed06e..418b0a9510 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -510,3 +510,35 @@ describe("dbt_map_relations tool", () => { expect(result.output).toBe("No tables found in the SQL.") }) }) + +describe("review regressions", () => { + test("a name with a quote or backslash is escaped in the generated call", () => { + const rel = manifestRelations({ + nodes: { "model.p.o'x": { resource_type: "model", name: "o'x", schema: "s", database: "d", package_name: "p" } }, + sources: { "source.p.a.b": { source_name: "a\\b", name: "b'c", schema: "s", database: "d" } }, + }) + expect(rel.find((r) => r.kind === "model")?.call).toBe("{{ ref('o\\'x') }}") + expect(rel.find((r) => r.kind === "source")?.call).toBe("{{ source('a\\\\b', 'b\\'c') }}") + }) + + test("a dot inside backticks is split for BigQuery manifests only", () => { + const node = { resource_type: "model", name: "orders", relation_name: "`main`.`raw`.`orders.v1`", schema: "raw" } + const dbx = manifestRelations({ metadata: { adapter_type: "databricks" }, nodes: { "model.p.orders": node } }) + expect(dbx[0].parts).toEqual(["main", "raw", "orders.v1"]) + const bq = manifestRelations({ metadata: { adapter_type: "bigquery" }, nodes: { "model.p.orders": node } }) + expect(bq[0].parts).toEqual(["main", "raw", "orders", "v1"]) + }) + + test("the table list goes on after LATERAL", () => { + const { refs } = scanTableReferences( + "select * from db.one.orders a, lateral flatten(input => a.items) f, db.two.orders b", + "snowflake", + ) + expect(refs.map((r) => r.raw)).toEqual(["db.one.orders", "db.two.orders"]) + }) + + test("quoted names that differ only in inner whitespace are different tables", () => { + const rows = mapRelations(['"A B"', '"A B"'], 'select * from "A B", "A B"', [], "snowflake") + expect(rows.map((r) => r.raw)).toEqual(['"A B"', '"A B"']) + }) +}) From 18f72640b9770c22e60a6f7452ca3ee1c9ab05ba Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sun, 15 Nov 2026 22:20:44 +0530 Subject: [PATCH 08/32] fix: dbt_map_relations splits backtick dots by the dialect it resolved and tolerates a null manifest Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/tools/dbt-map-relations.ts | 6 +++--- packages/opencode/test/altimate/dbt-map-relations.test.ts | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 137e47edcc..e9f49e5106 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -370,10 +370,10 @@ function jinjaString(value: string): string { /** The model, snapshot, seed and source nodes of a manifest, each with its relation's name parts. */ // The manifest is parsed JSON of a shape dbt owns and versions; its fields are checked where they are read. -export function manifestRelations(manifest: any): Relation[] { +export function manifestRelations(manifest: any, dialect?: string): Relation[] { const rootProject: string | undefined = manifest?.metadata?.project_name // Only BigQuery names put dots inside backticks; elsewhere a dot in a quoted name is part of the name. - const splitDots = detectDialect(manifest).toLowerCase() === "bigquery" + const splitDots = (dialect ?? detectDialect(manifest ?? {})).toLowerCase() === "bigquery" const out: Relation[] = [] const relationOf = (node: any): string | undefined => { if (typeof node?.relation_name === "string" && node.relation_name) return node.relation_name @@ -546,7 +546,7 @@ export const DbtMapRelationsTool = Tool.define("dbt_map_relations", { const tables: string[] = Array.isArray(data.tables) ? data.tables.filter((t: unknown) => typeof t === "string") : [] - const mappings = mapRelations(tables, args.sql, manifestRelations(manifest), dialect) + const mappings = mapRelations(tables, args.sql, manifestRelations(manifest, dialect), dialect) const count = (m: RelationMatch) => mappings.filter((r) => r.match === m).length return { title: `dbt Map Relations: ${mappings.length} tables, ${count("none")} unmatched`, diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index 418b0a9510..eb32e2fbac 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -529,6 +529,13 @@ describe("review regressions", () => { expect(bq[0].parts).toEqual(["main", "raw", "orders", "v1"]) }) + test("an explicit dialect decides the backtick split, and a null manifest does not throw", () => { + const node = { resource_type: "model", name: "orders", relation_name: "`p.ds.orders`", schema: "ds" } + const rel = manifestRelations({ nodes: { "model.p.orders": node } }, "bigquery") + expect(rel[0].parts).toEqual(["p", "ds", "orders"]) + expect(manifestRelations(null)).toEqual([]) + }) + test("the table list goes on after LATERAL", () => { const { refs } = scanTableReferences( "select * from db.one.orders a, lateral flatten(input => a.items) f, db.two.orders b", From d8c34693b09dd856b0efd2c4a1924c7320346720 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sun, 15 Nov 2026 22:27:01 +0530 Subject: [PATCH 09/32] 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 d7e0800c470aa583b4d6f135676425d553b401be Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 06:14:47 +0530 Subject: [PATCH 10/32] fix: dbt_map_relations reads implicit recursion, hash and slash comments, the project's own model first, and the configured target path Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-map-relations.ts | 38 +++++++++++++---- .../test/altimate/dbt-map-relations.test.ts | 42 ++++++++++++++++++- 2 files changed, 69 insertions(+), 11 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index e9f49e5106..3bf5c05e3f 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -35,6 +35,8 @@ type Relation = { relationName: string parts: string[] call: string + /** False for a model that comes from an installed package; a bare ref() resolves to the project's own first. */ + root: boolean } const RANK: Record, number> = { exact: 0, case_insensitive: 1, partial: 2 } @@ -104,6 +106,8 @@ export type Scan = { const DOUBLE_QUOTE_STRING = new Set(["bigquery", "databricks", "spark", "mysql", "hive"]) const BACKSLASH_ESCAPE = new Set(["bigquery", "databricks", "spark", "mysql", "hive", "clickhouse", "snowflake"]) const DOLLAR_QUOTE = new Set(["snowflake", "postgres", "redshift", "duckdb"]) +const IMPLICIT_RECURSION = new Set(["snowflake", "tsql", "sqlserver", "fabric", "synapse", "oracle"]) +const HASH_COMMENT = new Set(["bigquery", "mysql", "mariadb"]) const BRACKET_QUOTE = new Set(["tsql", "sqlserver", "fabric", "synapse"]) const RESERVED = new Set([ @@ -188,7 +192,7 @@ function tokenize(sql: string, dialect: string): Token[] { const ch = sql[i] const two = sql.slice(i, i + 2) if (/\s/.test(ch)) i++ - else if (two === "--") { + else if (two === "--" || (ch === "#" && HASH_COMMENT.has(dialect)) || (two === "//" && dialect === "snowflake")) { while (i < n && sql[i] !== "\n") i++ } else if (two === "/*") { const end = sql.indexOf("*/", i + 2) @@ -281,8 +285,10 @@ export function scanTableReferences(sql: string, dialect: string): Scan { // `name [(columns)] as [not] [materialized] (`, then `,` for the next CTE. const readCtes = (start: number) => { let i = start - const recursive = tokens[i]?.text.toLowerCase() === "recursive" - if (recursive) i++ + // Snowflake, T-SQL and Oracle recurse without the keyword: a CTE that names itself is the recursion there. + const keyword = tokens[i]?.text.toLowerCase() === "recursive" + if (keyword) i++ + const recursive = keyword || IMPLICIT_RECURSION.has(dialect) while (tokens[i] && (tokens[i].kind === "word" || tokens[i].kind === "quoted")) { const name = nameOf(tokens[i], dialect)[0].toLowerCase() let j = i + 1 @@ -405,7 +411,15 @@ export function manifestRelations(manifest: any, dialect?: string): Relation[] { const call = pkg ? `{{ ref(${jinjaString(pkg)}, ${jinjaString(node.name)}${version}) }}` : `{{ ref(${jinjaString(node.name)}${version}) }}` - out.push({ kind, name: node.name, uniqueId, relationName, parts: splitQualified(relationName, splitDots), call }) + out.push({ + kind, + name: node.name, + uniqueId, + relationName, + parts: splitQualified(relationName, splitDots), + call, + root: !pkg, + }) } const sources: Record = manifest?.sources ?? {} for (const [uniqueId, node] of Object.entries(sources)) { @@ -418,6 +432,7 @@ export function manifestRelations(manifest: any, dialect?: string): Relation[] { relationName, parts: splitQualified(relationName, splitDots), call: `{{ source(${jinjaString(node.source_name)}, ${jinjaString(node.name)}) }}`, + root: true, }) } return out @@ -458,7 +473,8 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ if (r.parts.length === 1 && cte && !inOwnBody) continue if (wanted.has(last.toLowerCase()) || wanted.has(whole.toLowerCase())) { add(r) - covered.add((wanted.has(last.toLowerCase()) ? last : whole).toLowerCase()) + covered.add(last.toLowerCase()) + covered.add(whole.toLowerCase()) } else if (cte) add(r) } for (const t of tables) { @@ -466,7 +482,7 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ add({ raw: t, parts: splitQualified(t, dialect === "bigquery") }) } return entries.map((entry) => { - const hits: RelationCandidate[] = [] + const hits: (RelationCandidate & { root: boolean })[] = [] for (const rel of relations) { const how = matchParts(entry.parts, rel.parts) if (!how) continue @@ -477,18 +493,21 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ uniqueId: rel.uniqueId, relationName: rel.relationName, match: how, + root: rel.root, }) } hits.sort( (a, b) => - RANK[a.match] - RANK[b.match] || KIND_RANK[a.kind] - KIND_RANK[b.kind] || a.uniqueId.localeCompare(b.uniqueId), + RANK[a.match] - RANK[b.match] || KIND_RANK[a.kind] - KIND_RANK[b.kind] || + Number(b.root) - Number(a.root) || + a.uniqueId.localeCompare(b.uniqueId), ) const best = hits[0] return { raw: entry.raw, replaceWith: best ? best.replaceWith : entry.raw, match: best ? best.match : "none", - candidates: hits, + candidates: hits.map(({ root: _root, ...candidate }) => candidate), } }) } @@ -568,5 +587,6 @@ async function defaultManifestPath(): Promise { try { real = await fs.promises.realpath(root) } catch {} - return path.join(real, "target", "manifest.json") + const { resolveDbtTargetPath } = await import("../validators/validator-utils") + return path.join(await resolveDbtTargetPath(real), "manifest.json") } diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index eb32e2fbac..b2526c1c17 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -333,7 +333,8 @@ describe("mapRelations", () => { }) test("a table named like its own CTE, read inside that CTE, is the real table; a recursive CTE's own name is not", () => { - expect(map("with orders as (select * from orders) select * from orders", []).map((r) => r.raw)).toEqual(["orders"]) + const sql = "with orders as (select * from orders) select * from orders" + expect(mapRelations([], sql, [], "postgres").map((r) => r.raw)).toEqual(["orders"]) expect( map("with recursive customers as (select 1 union all select * from customers) select * from customers", []), ).toEqual([]) @@ -545,7 +546,44 @@ describe("review regressions", () => { }) test("quoted names that differ only in inner whitespace are different tables", () => { - const rows = mapRelations(['"A B"', '"A B"'], 'select * from "A B", "A B"', [], "snowflake") + const rows = mapRelations(["A B", "A B"], 'select * from "A B", "A B"', [], "snowflake") expect(rows.map((r) => r.raw)).toEqual(['"A B"', '"A B"']) }) + test("a CTE that names itself is the recursion in Snowflake and T-SQL without the RECURSIVE keyword", () => { + const sql = "with c as (select 0 n union all select n + 1 from c where n < 5) select * from c" + for (const dialect of ["snowflake", "tsql"]) { + expect(mapRelations(["c"], sql, [], dialect)).toEqual([]) + } + expect(mapRelations(["c"], sql, [], "postgres").map((r) => r.raw)).toEqual(["c"]) + }) + + test("a model of the project outranks a same-named model of an installed package", () => { + const node = (pkg: string) => ({ + resource_type: "model", + name: "date_spine", + package_name: pkg, + relation_name: `"db"."s"."date_spine"`, + schema: "s", + }) + const manifest = { + metadata: { project_name: "shop" }, + nodes: { "model.dbt_utils.date_spine": node("dbt_utils"), "model.shop.date_spine": node("shop") }, + } + const rows = mapRelations(["date_spine"], "select * from date_spine", manifestRelations(manifest, "snowflake"), "snowflake") + expect(rows[0].replaceWith).toBe("{{ ref('date_spine') }}") + expect(rows[0].candidates.length).toBe(2) + expect(Object.keys(rows[0].candidates[0])).not.toContain("root") + }) + + test("# and // line comments are skipped where the dialect has them", () => { + const bq = scanTableReferences("select * from a.orders # don't remove\njoin a.payments", "bigquery") + expect(bq.refs.map((r) => r.raw)).toEqual(["a.orders", "a.payments"]) + const sf = scanTableReferences("select * from a.orders // it's here\njoin a.payments", "snowflake") + expect(sf.refs.map((r) => r.raw)).toEqual(["a.orders", "a.payments"]) + }) + + test("a backticked path and a bare name of the same table add no row the SQL cannot show", () => { + const rows = mapRelations(["orders", "proj.ds.orders"], "select * from orders o join `proj.ds.orders`", [], "bigquery") + expect(rows.map((r) => r.raw)).toEqual(["orders", "`proj.ds.orders`"]) + }) }) From 18a3fc4e0bd124e8f5fb3120fdc4d152be88cbe3 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 06:39:46 +0530 Subject: [PATCH 11/32] 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 12/32] 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 13/32] 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 14/32] 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 f4f1b435efee06b3b524328a940e16190c1290f5 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 11:29:21 +0530 Subject: [PATCH 15/32] fix: keep quoted CTE names case-sensitive where the dialect does, read grouped joins, sample clauses and ON conditions Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-map-relations.ts | 95 ++++++++++++++++-- .../test/altimate/dbt-map-relations.test.ts | 99 ++++++++++++++++++- 2 files changed, 183 insertions(+), 11 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 3bf5c05e3f..248dc86ad9 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -93,12 +93,12 @@ export function splitQualified(text: string, splitBacktickDots = false): string[ } /** A table reference found in the SQL text: the text as written and its unquoted parts. */ -type Reference = { raw: string; parts: string[]; start?: number } +type Reference = { raw: string; parts: string[]; start?: number; /** The last part was written in double quotes. */ quoted?: boolean } /** What the scan of a query found: its table references in text order, and the names its CTEs define. */ export type Scan = { refs: Reference[] - /** By lower-cased name: whether the WITH is recursive, and the text offsets of each body that defines the name. */ + /** By identity (see `identityOf`): whether the WITH is recursive, and the text offsets of each body that defines the name. */ ctes: Map } @@ -108,6 +108,8 @@ const BACKSLASH_ESCAPE = new Set(["bigquery", "databricks", "spark", "mysql", "h const DOLLAR_QUOTE = new Set(["snowflake", "postgres", "redshift", "duckdb"]) const IMPLICIT_RECURSION = new Set(["snowflake", "tsql", "sqlserver", "fabric", "synapse", "oracle"]) const HASH_COMMENT = new Set(["bigquery", "mysql", "mariadb"]) +// Dialects where a double-quoted name keeps its case while an unquoted one is folded, so `"orders"` and `ORDERS` differ. +const FOLDS_UNQUOTED: Record = { snowflake: "upper", oracle: "upper", postgres: "lower", postgresql: "lower" } const BRACKET_QUOTE = new Set(["tsql", "sqlserver", "fabric", "synapse"]) const RESERVED = new Set([ @@ -160,6 +162,13 @@ const RESERVED = new Set([ ]) // Clause words that start a list of tables. `from` inside these calls reads a column or a string instead. const TABLE_KEYWORDS = new Set(["from", "join", "into", "update", "using"]) +// Words that end a join's ON condition: the next join, or the next clause of the query. +const JOIN_CONDITION_END = new Set([ + "join", "inner", "left", "right", "full", "cross", "natural", "outer", "where", "group", "order", "having", + "union", "intersect", "except", "minus", "limit", "offset", "fetch", "window", "qualify", "for", "returning", + "when", "set", "do", "select", "lateral", "distribute", "cluster", "sort", "settings", +]) +const SUBQUERY_START = new Set(["select", "with", "values", "table", "from"]) const FROM_INSIDE_CALL = new Set(["extract", "trim", "substring", "overlay", "position"]) type Token = { kind: "word" | "quoted" | "punct" | "other"; start: number; end: number; text: string } @@ -269,6 +278,17 @@ function afterParens(tokens: Token[], at: number): number { return tokens.length } +/** + * What a name written once refers to: its lower-cased text, except where the dialect keeps the case of a + * double-quoted name and folds an unquoted one, which there is the folded text and the quoted one its own text. + */ +function identityOf(name: string, quoted: boolean, dialect: string): string { + const fold = FOLDS_UNQUOTED[dialect] + if (!fold) return name.toLowerCase() + if (quoted) return name + return fold === "upper" ? name.toUpperCase() : name.toLowerCase() +} + const isPunct = (token: Token | undefined, text: string) => token?.kind === "punct" && token.text === text /** @@ -290,7 +310,7 @@ export function scanTableReferences(sql: string, dialect: string): Scan { if (keyword) i++ const recursive = keyword || IMPLICIT_RECURSION.has(dialect) while (tokens[i] && (tokens[i].kind === "word" || tokens[i].kind === "quoted")) { - const name = nameOf(tokens[i], dialect)[0].toLowerCase() + const name = identityOf(nameOf(tokens[i], dialect)[0], tokens[i].kind === "quoted" && tokens[i].text[0] === '"', dialect) let j = i + 1 if (isPunct(tokens[j], "(")) j = afterParens(tokens, j) if (tokens[j]?.text.toLowerCase() !== "as") return @@ -310,14 +330,36 @@ export function scanTableReferences(sql: string, dialect: string): Scan { const readList = (start: number, strict: boolean) => { let i = start - for (;;) { + const groups = ["from", "join"].includes(tokens[start - 1]?.text.toLowerCase() ?? "") + let groupEnd = -1 + for (let item = 0; ; item++) { // LATERAL prefixes a table expression in the list; the list goes on after it. if (tokens[i]?.kind === "word" && tokens[i].text.toLowerCase() === "lateral") i++ - if (isPunct(tokens[i], "(")) i = afterParens(tokens, i) - else { + // `(a join b on ...)` groups tables: its first table is read like any other, and the list goes on after the + // group's `)`. The tables after the first are found by the JOIN in the group. Any other `(` is a subquery. + while ( + item === 0 && + groups && + isPunct(tokens[i], "(") && + !SUBQUERY_START.has(tokens[i + 1]?.text.toLowerCase() ?? "") && + (["word", "quoted"].includes(tokens[i + 1]?.kind ?? "") || isPunct(tokens[i + 1], "(")) + ) { + if (groupEnd < 0) groupEnd = afterParens(tokens, i) + i++ + } + if (isPunct(tokens[i], "(")) { + i = afterParens(tokens, i) + if (groupEnd >= 0) i = Math.max(i, groupEnd) + groupEnd = -1 + } else { const first = tokens[i] if (!first || (first.kind !== "word" && first.kind !== "quoted")) return if (first.kind === "word" && RESERVED.has(first.text.toLowerCase())) return + // PostgreSQL's `FROM ONLY name` leaves out the table's children; the name follows. + if (dialect.startsWith("postgres") && first.kind === "word" && first.text.toLowerCase() === "only" && ["word", "quoted"].includes(tokens[i + 1]?.kind ?? "")) { + i++ + continue + } const parts: string[] = [...nameOf(first, dialect)] let at = i while (isPunct(tokens[at + 1], ".") && ["word", "quoted"].includes(tokens[at + 2]?.kind ?? "")) { @@ -327,7 +369,15 @@ export function scanTableReferences(sql: string, dialect: string): Scan { const last = tokens[at] i = at + 1 if (isPunct(tokens[i], "(") && strict) i = afterParens(tokens, i) - else refs.push({ raw: sql.slice(first.start, last.end), parts, start: first.start }) + else + refs.push({ + raw: sql.slice(first.start, last.end), + parts, + start: first.start, + quoted: last.kind === "quoted" && last.text[0] === '"', + }) + if (groupEnd >= 0) i = groupEnd + groupEnd = -1 } // An alias with or without AS, then its column list, then a comma continues a FROM list. if (tokens[i]?.text.toLowerCase() === "as") i++ @@ -336,11 +386,35 @@ export function scanTableReferences(sql: string, dialect: string): Scan { i++ if (isPunct(tokens[i], "(")) i = afterParens(tokens, i) } + // A sample clause or a join condition sits between a table and the comma that goes on with the list. + i = skipSampleAndCondition(i) if (isPunct(tokens[i], ",")) i++ else return } } + // After a table and its alias: `TABLESAMPLE [method] (n) [REPEATABLE (n)]`, then a join's `ON ...` up to a comma. + const skipSampleAndCondition = (start: number) => { + let i = start + const word = () => (tokens[i]?.kind === "word" ? tokens[i].text.toLowerCase() : "") + while (["tablesample", "sample", "repeatable", "seed", "bernoulli", "system", "block", "row"].includes(word())) { + i++ + if (isPunct(tokens[i], "(")) i = afterParens(tokens, i) + } + if (word() !== "on") return i + i++ + let cases = 0 + while (i < tokens.length) { + if (tokens[i].kind === "word" && word() === "case") cases++ + else if (tokens[i].kind === "word" && word() === "end" && cases > 0) cases-- + if (isPunct(tokens[i], "(")) i = afterParens(tokens, i) + else if (isPunct(tokens[i], ",")) return i + else if (isPunct(tokens[i], ")") || tokens[i].text === ";" || (tokens[i].kind === "word" && cases === 0 && JOIN_CONDITION_END.has(word()))) return i + else i++ + } + return i + } + for (let i = 0; i < tokens.length; i++) { const token = tokens[i] if (isPunct(token, "(")) { @@ -464,10 +538,11 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ entries.push(r) } const covered = new Set() + const cteNames = new Set([...ctes.keys()].map((k) => k.toLowerCase())) for (const r of refs) { const last = r.parts[r.parts.length - 1] const whole = r.parts.join(".") - const cte = ctes.get(last.toLowerCase()) + const cte = ctes.get(identityOf(last, !!r.quoted, dialect)) // A CTE's own name inside its own body is the real table unless the WITH is recursive. const inOwnBody = !!cte && !cte.recursive && cte.bodies.some(([from, to]) => r.start! >= from && r.start! < to) if (r.parts.length === 1 && cte && !inOwnBody) continue @@ -475,10 +550,10 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ add(r) covered.add(last.toLowerCase()) covered.add(whole.toLowerCase()) - } else if (cte) add(r) + } else if (cteNames.has(last.toLowerCase())) add(r) } for (const t of tables) { - if (covered.has(t.toLowerCase()) || ctes.has(t.toLowerCase())) continue + if (covered.has(t.toLowerCase()) || cteNames.has(t.toLowerCase())) continue add({ raw: t, parts: splitQualified(t, dialect === "bigquery") }) } return entries.map((entry) => { diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index b2526c1c17..d2c183d4ba 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -159,7 +159,8 @@ describe("scanTableReferences", () => { "with orders as (select * from db.raw.orders), b(x) as materialized (select 1) select * from orders", "snowflake", ) - expect([...ctes.keys()]).toEqual(["orders", "b"]) + // Snowflake folds an unquoted name to upper case, so that is the name a CTE is known by. + expect([...ctes.keys()]).toEqual(["ORDERS", "B"]) expect(refs.map((r) => r.raw)).toEqual(["db.raw.orders", "orders"]) }) }) @@ -587,3 +588,99 @@ describe("review regressions", () => { expect(rows.map((r) => r.raw)).toEqual(["orders", "`proj.ds.orders`"]) }) }) + +describe("table lists after a join condition, a sample clause or ONLY", () => { + const raws = (sql: string, dialect = "snowflake") => scanTableReferences(sql, dialect).refs.map((r) => r.raw) + + test("a comma after a join's ON condition goes on with the list", () => { + expect(raws("select * from db.z.items i join db.a.orders a on i.id = a.id, db.z.orders z")).toEqual([ + "db.z.items", + "db.a.orders", + "db.z.orders", + ]) + expect(raws("select * from a join b on f(a.x, b.y) = 1, c where c.k = 1")).toEqual(["a", "b", "c"]) + expect(raws("select * from a join b using (id), c")).toEqual(["a", "b", "c"]) + }) + + test("a join condition ends at the next join or clause, not at a comma inside the select list", () => { + expect(raws("select * from a join b on a.id = b.id join c on c.id = b.id where a.x in (1, 2)")).toEqual(["a", "b", "c"]) + }) + + test("the first table of a parenthesized join is read; a subquery is not a table", () => { + expect(raws("select * from (raw.jaffle.a x join raw.jaffle.b y on x.id = y.id)")).toEqual(["raw.jaffle.a", "raw.jaffle.b"]) + expect(raws("select * from (select * from raw.s.t) q, raw.s.u")).toEqual(["raw.s.t", "raw.s.u"]) + }) + + test("a table after a parenthesized join is still read, however deep the group", () => { + expect(raws("select * from (a join b on a.id = b.id), c")).toEqual(["a", "b", "c"]) + expect(raws("select * from (a join b on a.id = b.id) j, c")).toEqual(["a", "b", "c"]) + expect(raws("select * from ((a join b on a.id = b.id) join c on c.id = a.id), d")).toEqual(["a", "b", "c", "d"]) + }) + + test("a group that starts with a subquery or ONLY, and a CASE in a join condition, do not cut the list", () => { + expect(raws("select * from ((select * from x) q join b on 1=1), c")).toEqual(["x", "b", "c"]) + expect(raws("select * from (only a join b on 1=1), c", "postgres")).toEqual(["a", "b", "c"]) + expect(raws("select * from a join b on case when a.x = 1 then 1 else 0 end = 1, c")).toEqual(["a", "b", "c"]) + }) + + test("a join condition does not run on into MERGE, ON CONFLICT or later statements", () => { + expect(raws("merge into t using s on t.id = s.id when matched then update set a = s.a, b = s.b", "snowflake")).toEqual(["t", "s"]) + expect(raws("insert into t select * from s on conflict (id) do update set a = excluded.a, b = excluded.b", "postgres")).toEqual(["t", "s"]) + expect(raws("select * from a join b on a.id = b.id; select x, y from c")).toEqual(["a", "b", "c"]) + }) + + test("ONLY is skipped for PostgreSQL only", () => { + expect(raws("select * from only join b on 1=1", "snowflake")).toEqual(["only", "b"]) + }) + + test("a sample clause does not end the list", () => { + expect(raws("select * from db.z.items tablesample (10), db.z.orders")).toEqual(["db.z.items", "db.z.orders"]) + expect(raws("select * from db.z.items i tablesample bernoulli (10) repeatable (3), db.z.orders", "postgres")).toEqual([ + "db.z.items", + "db.z.orders", + ]) + }) + + test("PostgreSQL's ONLY is not a table name", () => { + expect(raws("select * from only db.z.orders", "postgres")).toEqual(["db.z.orders"]) + expect(raws("select * from only orders o, only db.z.items", "postgres")).toEqual(["orders", "db.z.items"]) + }) + + test("each of them reaches the mapped rows with the schema as written", () => { + const relations = manifestRelations(MANIFEST) + const rows = mapRelations( + ["items", "orders"], + "select * from db.z.items i join db.a.orders a on i.id = a.id, db.z.orders z", + relations, + "snowflake", + ) + expect(rows.map((r) => r.raw)).toEqual(["db.z.items", "db.a.orders", "db.z.orders"]) + }) +}) + +describe("a quoted CTE name and a table that differs only in case", () => { + const rows = (sql: string, dialect: string) => mapRelations(["orders"], sql, [], dialect).map((r) => r.raw) + + test("Snowflake: a quoted lower-case CTE is not the unquoted table", () => { + expect(rows('with "orders" as (select 1) select * from ORDERS', "snowflake")).toEqual(["ORDERS"]) + expect(rows('with "orders" as (select 1) select * from "orders"', "snowflake")).toEqual([]) + expect(rows("with orders as (select 1) select * from ORDERS", "snowflake")).toEqual([]) + }) + + test("PostgreSQL: a quoted upper-case CTE is not the unquoted table", () => { + expect(rows('with "ORDERS" as (select 1) select * from orders', "postgres")).toEqual(["orders"]) + expect(rows("with orders as (select 1) select * from ORDERS", "postgres")).toEqual([]) + }) + + test("with the real parser: a quoted CTE name beside a physical table of another case", async () => { + // altimate-core reports the table list the mapper starts from; the physical table must survive both steps. + const sql = 'with "orders" as (select * from raw."orders") select * from ORDERS o join "orders" x on 1=1' + const tables = await tablesOf(sql, "snowflake") + expect(mapRelations(tables, sql, [], "snowflake").map((r) => r.raw)).toEqual(expect.arrayContaining(["ORDERS"])) + }) + + test("elsewhere quoting does not change the case a name is compared in", () => { + expect(rows("with `Orders` as (select 1) select * from orders", "databricks")).toEqual([]) + expect(rows("with [Orders] as (select 1) select * from orders", "tsql")).toEqual([]) + }) +}) From aaff4148345d9e6735a119d27ea540a3d2929a9d Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 13:21:13 +0530 Subject: [PATCH 16/32] 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 17/32] 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 18/32] 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 d3570afb4e254cdc42abba7d642ae2182ff6c95f Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 13:51:48 +0530 Subject: [PATCH 19/32] fix: read ClickHouse comments, mssql and redshift spellings, grouped joins after a comma, and a bare SAMPLE clause Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-map-relations.ts | 24 ++++++++++++------- .../test/altimate/dbt-map-relations.test.ts | 15 ++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 248dc86ad9..843cd26480 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -106,11 +106,11 @@ export type Scan = { const DOUBLE_QUOTE_STRING = new Set(["bigquery", "databricks", "spark", "mysql", "hive"]) const BACKSLASH_ESCAPE = new Set(["bigquery", "databricks", "spark", "mysql", "hive", "clickhouse", "snowflake"]) const DOLLAR_QUOTE = new Set(["snowflake", "postgres", "redshift", "duckdb"]) -const IMPLICIT_RECURSION = new Set(["snowflake", "tsql", "sqlserver", "fabric", "synapse", "oracle"]) -const HASH_COMMENT = new Set(["bigquery", "mysql", "mariadb"]) +const IMPLICIT_RECURSION = new Set(["snowflake", "tsql", "sqlserver", "mssql", "fabric", "synapse", "oracle"]) +const HASH_COMMENT = new Set(["bigquery", "mysql", "mariadb", "clickhouse"]) // Dialects where a double-quoted name keeps its case while an unquoted one is folded, so `"orders"` and `ORDERS` differ. -const FOLDS_UNQUOTED: Record = { snowflake: "upper", oracle: "upper", postgres: "lower", postgresql: "lower" } -const BRACKET_QUOTE = new Set(["tsql", "sqlserver", "fabric", "synapse"]) +const FOLDS_UNQUOTED: Record = { snowflake: "upper", oracle: "upper", postgres: "lower", postgresql: "lower", redshift: "lower" } +const BRACKET_QUOTE = new Set(["tsql", "sqlserver", "mssql", "fabric", "synapse"]) const RESERVED = new Set([ "select", @@ -201,7 +201,7 @@ function tokenize(sql: string, dialect: string): Token[] { const ch = sql[i] const two = sql.slice(i, i + 2) if (/\s/.test(ch)) i++ - else if (two === "--" || (ch === "#" && HASH_COMMENT.has(dialect)) || (two === "//" && dialect === "snowflake")) { + else if (two === "--" || (ch === "#" && HASH_COMMENT.has(dialect)) || (two === "//" && (dialect === "snowflake" || dialect === "clickhouse"))) { while (i < n && sql[i] !== "\n") i++ } else if (two === "/*") { const end = sql.indexOf("*/", i + 2) @@ -330,17 +330,22 @@ export function scanTableReferences(sql: string, dialect: string): Scan { const readList = (start: number, strict: boolean) => { let i = start - const groups = ["from", "join"].includes(tokens[start - 1]?.text.toLowerCase() ?? "") + // FROM and JOIN lists may hold a parenthesised group at any position. After USING a `(` is a column list + // unless a JOIN sits inside it. + const using = tokens[start - 1]?.text.toLowerCase() === "using" + const groups = strict + const isGroup = (at: number) => + !using || tokens.slice(at, afterParens(tokens, at)).some((t) => t.kind === "word" && t.text.toLowerCase() === "join") let groupEnd = -1 for (let item = 0; ; item++) { // LATERAL prefixes a table expression in the list; the list goes on after it. if (tokens[i]?.kind === "word" && tokens[i].text.toLowerCase() === "lateral") i++ - // `(a join b on ...)` groups tables: its first table is read like any other, and the list goes on after the + // `(a join b on ...)` groups tables, at any position in the list: its first table is read like any other, and the list goes on after the // group's `)`. The tables after the first are found by the JOIN in the group. Any other `(` is a subquery. while ( - item === 0 && groups && isPunct(tokens[i], "(") && + isGroup(i) && !SUBQUERY_START.has(tokens[i + 1]?.text.toLowerCase() ?? "") && (["word", "quoted"].includes(tokens[i + 1]?.kind ?? "") || isPunct(tokens[i + 1], "(")) ) { @@ -382,7 +387,8 @@ export function scanTableReferences(sql: string, dialect: string): Scan { // An alias with or without AS, then its column list, then a comma continues a FROM list. if (tokens[i]?.text.toLowerCase() === "as") i++ const alias = tokens[i] - if (alias && (alias.kind === "quoted" || (alias.kind === "word" && !RESERVED.has(alias.text.toLowerCase())))) { + const startsSample = alias?.kind === "word" && ["sample", "tablesample"].includes(alias.text.toLowerCase()) && (isPunct(tokens[i + 1], "(") || ["bernoulli", "system", "block", "row"].includes(tokens[i + 1]?.text.toLowerCase() ?? "")) + if (alias && !startsSample && (alias.kind === "quoted" || (alias.kind === "word" && !RESERVED.has(alias.text.toLowerCase())))) { i++ if (isPunct(tokens[i], "(")) i = afterParens(tokens, i) } diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index d2c183d4ba..a40a1fb28c 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -646,6 +646,21 @@ describe("table lists after a join condition, a sample clause or ONLY", () => { expect(raws("select * from only orders o, only db.z.items", "postgres")).toEqual(["orders", "db.z.items"]) }) + test("a parenthesized join after a comma, a USING group and a bare SAMPLE clause keep every table", () => { + expect(raws("select * from s.items i, (one.orders a join two.orders b on a.id = b.id)")).toEqual(["s.items", "one.orders", "two.orders"]) + expect(raws("merge into t using (one.s a join two.s b on a.id = b.id) on 1=1")).toEqual(["t", "one.s", "two.s"]) + expect(raws("select * from db.z.items sample (10), db.z.orders")).toEqual(["db.z.items", "db.z.orders"]) + expect(raws("select * from db.z.items sample join db.z.orders on 1=1")).toEqual(["db.z.items", "db.z.orders"]) + }) + + test("ClickHouse comments, and the mssql and redshift spellings, follow their dialect", () => { + expect(raws("select * from a.orders # it's fine\n join a.payments", "clickhouse")).toEqual(["a.orders", "a.payments"]) + expect(raws("select * from a.orders // from legacy.x\n join a.payments", "clickhouse")).toEqual(["a.orders", "a.payments"]) + expect(mapRelations(["c"], "with c as (select 0 union all select n + 1 from c) select * from c", [], "mssql")).toEqual([]) + expect(raws("select * from [db].[s].[t]", "mssql")).toEqual(["[db].[s].[t]"]) + expect(mapRelations(["orders"], 'with "Orders" as (select 1) select * from orders', [], "redshift").map((r) => r.raw)).toEqual(["orders"]) + }) + test("each of them reaches the mapped rows with the schema as written", () => { const relations = manifestRelations(MANIFEST) const rows = mapRelations( From caa4c7c4f0dab2c59939166221a6ed1fc0a45f7d Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 14:08:59 +0530 Subject: [PATCH 20/32] 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 7ecdef3eab47547fea2f65de96e50cdd3e69140e Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 21:12:53 +0530 Subject: [PATCH 21/32] fix: scope CTEs to their statement, keep reading a table list past T-SQL hints and PIVOT, nest block comments where the dialect does Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-map-relations.ts | 50 ++++++++++++++++--- .../test/altimate/dbt-map-relations.test.ts | 17 +++++++ 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 843cd26480..0ef22002dc 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -98,8 +98,8 @@ type Reference = { raw: string; parts: string[]; start?: number; /** The last pa /** What the scan of a query found: its table references in text order, and the names its CTEs define. */ export type Scan = { refs: Reference[] - /** By identity (see `identityOf`): whether the WITH is recursive, and the text offsets of each body that defines the name. */ - ctes: Map + /** By identity (see `identityOf`): whether the WITH is recursive, the text offsets of each body that defines the name, and the text range each definition is visible in (its statement or enclosing parentheses). */ + ctes: Map } // Dialects whose double quotes (and, for some, backslashes) belong to string literals. @@ -107,6 +107,8 @@ const DOUBLE_QUOTE_STRING = new Set(["bigquery", "databricks", "spark", "mysql", const BACKSLASH_ESCAPE = new Set(["bigquery", "databricks", "spark", "mysql", "hive", "clickhouse", "snowflake"]) const DOLLAR_QUOTE = new Set(["snowflake", "postgres", "redshift", "duckdb"]) const IMPLICIT_RECURSION = new Set(["snowflake", "tsql", "sqlserver", "mssql", "fabric", "synapse", "oracle"]) +// Dialects whose block comments nest: an inner comment does not end the outer one. +const NESTED_COMMENTS = new Set(["postgres", "postgresql", "redshift", "cockroachdb", "duckdb", "tsql", "sqlserver", "mssql", "fabric", "synapse"]) const HASH_COMMENT = new Set(["bigquery", "mysql", "mariadb", "clickhouse"]) // Dialects where a double-quoted name keeps its case while an unquoted one is folded, so `"orders"` and `ORDERS` differ. const FOLDS_UNQUOTED: Record = { snowflake: "upper", oracle: "upper", postgres: "lower", postgresql: "lower", redshift: "lower" } @@ -204,8 +206,19 @@ function tokenize(sql: string, dialect: string): Token[] { else if (two === "--" || (ch === "#" && HASH_COMMENT.has(dialect)) || (two === "//" && (dialect === "snowflake" || dialect === "clickhouse"))) { while (i < n && sql[i] !== "\n") i++ } else if (two === "/*") { - const end = sql.indexOf("*/", i + 2) - i = end < 0 ? n : end + 2 + if (NESTED_COMMENTS.has(dialect)) { + let depth = 1 + let j = i + 2 + while (j < n && depth > 0) { + if (sql.startsWith("/*", j)) (depth++, (j += 2)) + else if (sql.startsWith("*/", j)) (depth--, (j += 2)) + else j++ + } + i = j + } else { + const end = sql.indexOf("*/", i + 2) + i = end < 0 ? n : end + 2 + } } else if (ch === "'" || (ch === '"' && doubleIsString)) { const triple = dialect === "bigquery" && sql.startsWith(ch.repeat(3), i) if (triple) { @@ -309,6 +322,15 @@ export function scanTableReferences(sql: string, dialect: string): Scan { const keyword = tokens[i]?.text.toLowerCase() === "recursive" if (keyword) i++ const recursive = keyword || IMPLICIT_RECURSION.has(dialect) + // A CTE is visible to the end of its statement, or of the parentheses around its WITH. + let depth = 0 + let scopeEnd = sql.length + for (let k = start; k < tokens.length; k++) { + if (isPunct(tokens[k], "(")) depth++ + else if (isPunct(tokens[k], ")") && depth-- === 0) (scopeEnd = tokens[k].start), (k = tokens.length) + else if (tokens[k].text === ";" && depth === 0) (scopeEnd = tokens[k].start), (k = tokens.length) + } + const scope: [number, number] = [tokens[start - 1].start, scopeEnd] while (tokens[i] && (tokens[i].kind === "word" || tokens[i].kind === "quoted")) { const name = identityOf(nameOf(tokens[i], dialect)[0], tokens[i].kind === "quoted" && tokens[i].text[0] === '"', dialect) let j = i + 1 @@ -320,8 +342,9 @@ export function scanTableReferences(sql: string, dialect: string): Scan { if (!isPunct(tokens[j], "(")) return const open = tokens[j].start j = afterParens(tokens, j) - const known = ctes.get(name) ?? { recursive, bodies: [] } + const known = ctes.get(name) ?? { recursive, bodies: [], scopes: [] } known.bodies.push([open, tokens[j - 1].end]) + known.scopes.push(scope) ctes.set(name, known) if (!isPunct(tokens[j], ",")) return i = j + 1 @@ -387,7 +410,7 @@ export function scanTableReferences(sql: string, dialect: string): Scan { // An alias with or without AS, then its column list, then a comma continues a FROM list. if (tokens[i]?.text.toLowerCase() === "as") i++ const alias = tokens[i] - const startsSample = alias?.kind === "word" && ["sample", "tablesample"].includes(alias.text.toLowerCase()) && (isPunct(tokens[i + 1], "(") || ["bernoulli", "system", "block", "row"].includes(tokens[i + 1]?.text.toLowerCase() ?? "")) + const startsSample = alias?.kind === "word" && ["sample", "tablesample", "pivot", "unpivot"].includes(alias.text.toLowerCase()) && (isPunct(tokens[i + 1], "(") || ["bernoulli", "system", "block", "row"].includes(tokens[i + 1]?.text.toLowerCase() ?? "")) if (alias && !startsSample && (alias.kind === "quoted" || (alias.kind === "word" && !RESERVED.has(alias.text.toLowerCase())))) { i++ if (isPunct(tokens[i], "(")) i = afterParens(tokens, i) @@ -403,9 +426,18 @@ export function scanTableReferences(sql: string, dialect: string): Scan { const skipSampleAndCondition = (start: number) => { let i = start const word = () => (tokens[i]?.kind === "word" ? tokens[i].text.toLowerCase() : "") - while (["tablesample", "sample", "repeatable", "seed", "bernoulli", "system", "block", "row"].includes(word())) { + while ( + ["tablesample", "sample", "repeatable", "seed", "bernoulli", "system", "block", "row"].includes(word()) || + (["with", "pivot", "unpivot"].includes(word()) && isPunct(tokens[i + 1], "(")) + ) { + // T-SQL table hints `WITH (...)` and PIVOT / UNPIVOT (with their alias) sit between a table and the comma that continues the list. + const pivot = ["pivot", "unpivot"].includes(word()) i++ if (isPunct(tokens[i], "(")) i = afterParens(tokens, i) + if (pivot) { + if (tokens[i]?.text.toLowerCase() === "as") i++ + if (tokens[i] && (tokens[i].kind === "quoted" || (tokens[i].kind === "word" && !RESERVED.has(word())))) i++ + } } if (word() !== "on") return i i++ @@ -548,7 +580,9 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ for (const r of refs) { const last = r.parts[r.parts.length - 1] const whole = r.parts.join(".") - const cte = ctes.get(identityOf(last, !!r.quoted, dialect)) + const named = ctes.get(identityOf(last, !!r.quoted, dialect)) + // A CTE hides a table only where it is visible: a later statement can name a real table the same way. + const cte = named && named.scopes.some(([from, to]) => r.start! >= from && r.start! < to) ? named : undefined // A CTE's own name inside its own body is the real table unless the WITH is recursive. const inOwnBody = !!cte && !cte.recursive && cte.bodies.some(([from, to]) => r.start! >= from && r.start! < to) if (r.parts.length === 1 && cte && !inOwnBody) continue diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index a40a1fb28c..50f4e4368f 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -661,6 +661,23 @@ describe("table lists after a join condition, a sample clause or ONLY", () => { expect(mapRelations(["orders"], 'with "Orders" as (select 1) select * from orders', [], "redshift").map((r) => r.raw)).toEqual(["orders"]) }) + test("a CTE hides a table only inside its own statement", () => { + const rows = mapRelations(["orders"], "with orders as (select 1) select * from orders; select * from orders", [], "postgres") + expect(rows.map((r) => r.raw)).toEqual(["orders"]) + expect(mapRelations(["orders"], "with orders as (select 1) select * from orders", [], "postgres")).toEqual([]) + }) + + test("T-SQL table hints and PIVOT do not end the list", () => { + expect(raws("select * from db.a.orders with (nolock), db.z.orders", "mssql")).toEqual(["db.a.orders", "db.z.orders"]) + expect(raws("select * from db.a.orders pivot (sum(x) for y in ([a])) p, db.z.orders", "mssql")).toEqual(["db.a.orders", "db.z.orders"]) + }) + + test("block comments nest where the dialect says so", () => { + expect(raws("select * from db.a.orders /* outer /* nested */ from db.wrong.orders */", "postgres")).toEqual(["db.a.orders"]) + expect(raws("select /* a /* b */ from fake.orders */ * from real.orders", "mssql")).toEqual(["real.orders"]) + expect(raws("select * from a.orders /* x /* y */ join a.payments", "mysql")).toEqual(["a.orders", "a.payments"]) + }) + test("each of them reaches the mapped rows with the schema as written", () => { const relations = manifestRelations(MANIFEST) const rows = mapRelations( From ddab1c520e4f0a9b70abc54e5d9692097208214f Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 21:24:53 +0530 Subject: [PATCH 22/32] fix: scope a CTE from its own definition, read escape strings, ONLY (name), db..table and non-ASCII names Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-map-relations.ts | 37 ++++++++++++------- .../test/altimate/dbt-map-relations.test.ts | 15 ++++++++ 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 0ef22002dc..68701b9380 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -109,6 +109,7 @@ const DOLLAR_QUOTE = new Set(["snowflake", "postgres", "redshift", "duckdb"]) const IMPLICIT_RECURSION = new Set(["snowflake", "tsql", "sqlserver", "mssql", "fabric", "synapse", "oracle"]) // Dialects whose block comments nest: an inner comment does not end the outer one. const NESTED_COMMENTS = new Set(["postgres", "postgresql", "redshift", "cockroachdb", "duckdb", "tsql", "sqlserver", "mssql", "fabric", "synapse"]) +const ESCAPE_STRING = new Set(["postgres", "postgresql", "redshift", "cockroachdb"]) const HASH_COMMENT = new Set(["bigquery", "mysql", "mariadb", "clickhouse"]) // Dialects where a double-quoted name keeps its case while an unquoted one is folded, so `"orders"` and `ORDERS` differ. const FOLDS_UNQUOTED: Record = { snowflake: "upper", oracle: "upper", postgres: "lower", postgresql: "lower", redshift: "lower" } @@ -184,10 +185,10 @@ function tokenize(sql: string, dialect: string): Token[] { const backslash = BACKSLASH_ESCAPE.has(dialect) const push = (kind: Token["kind"], start: number, end: number) => out.push({ kind, start, end, text: sql.slice(start, end) }) - const skipString = (quote: string, from: number) => { + const skipString = (quote: string, from: number, escaped = false) => { let j = from while (j < n) { - if (backslash && sql[j] === "\\") j += 2 + if ((backslash || escaped) && sql[j] === "\\") j += 2 else if (sql[j] === quote) { if (sql[j + 1] === quote) j += 2 else return j + 1 @@ -224,7 +225,12 @@ function tokenize(sql: string, dialect: string): Token[] { if (triple) { const end = sql.indexOf(ch.repeat(3), i + 3) i = end < 0 ? n : end + 3 - } else i = skipString(ch, i + 1) + } else { + // PostgreSQL's E'...' string reads a backslash as an escape. + const prefix = out[out.length - 1] + const escapeString = ch === "'" && ESCAPE_STRING.has(dialect) && prefix?.kind === "word" && prefix.end === i && /^e$/i.test(prefix.text) + i = skipString(ch, i + 1, escapeString) + } } else if ( ch === "$" && DOLLAR_QUOTE.has(dialect) && @@ -245,14 +251,14 @@ function tokenize(sql: string, dialect: string): Token[] { const end = Math.min(j + 1, n) push("quoted", i, end) i = end - } else if (/[A-Za-z_]/.test(ch)) { + } else if (/[\p{L}_]/u.test(ch)) { let j = i + 1 - while (j < n && /[A-Za-z0-9_$]/.test(sql[j])) j++ + while (j < n && /[\p{L}\p{N}_$]/u.test(sql[j])) j++ // BigQuery project ids may contain hyphens (`my-proj.ds.tbl` unquoted), but only where a table is named. if (dialect === "bigquery" && (TABLE_KEYWORDS.has(lastWord()) || out[out.length - 1]?.text === ",")) { while (sql[j] === "-" && /[A-Za-z0-9_]/.test(sql[j + 1] ?? "")) { j++ - while (j < n && /[A-Za-z0-9_$]/.test(sql[j])) j++ + while (j < n && /[\p{L}\p{N}_$]/u.test(sql[j])) j++ } } push("word", i, j) @@ -330,7 +336,6 @@ export function scanTableReferences(sql: string, dialect: string): Scan { else if (isPunct(tokens[k], ")") && depth-- === 0) (scopeEnd = tokens[k].start), (k = tokens.length) else if (tokens[k].text === ";" && depth === 0) (scopeEnd = tokens[k].start), (k = tokens.length) } - const scope: [number, number] = [tokens[start - 1].start, scopeEnd] while (tokens[i] && (tokens[i].kind === "word" || tokens[i].kind === "quoted")) { const name = identityOf(nameOf(tokens[i], dialect)[0], tokens[i].kind === "quoted" && tokens[i].text[0] === '"', dialect) let j = i + 1 @@ -344,7 +349,8 @@ export function scanTableReferences(sql: string, dialect: string): Scan { j = afterParens(tokens, j) const known = ctes.get(name) ?? { recursive, bodies: [], scopes: [] } known.bodies.push([open, tokens[j - 1].end]) - known.scopes.push(scope) + // Visible after its own definition, so an earlier CTE or its own body names the real table; a recursive one sees itself. + known.scopes.push([recursive ? open : tokens[j - 1].end, scopeEnd]) ctes.set(name, known) if (!isPunct(tokens[j], ",")) return i = j + 1 @@ -384,13 +390,20 @@ export function scanTableReferences(sql: string, dialect: string): Scan { if (!first || (first.kind !== "word" && first.kind !== "quoted")) return if (first.kind === "word" && RESERVED.has(first.text.toLowerCase())) return // PostgreSQL's `FROM ONLY name` leaves out the table's children; the name follows. - if (dialect.startsWith("postgres") && first.kind === "word" && first.text.toLowerCase() === "only" && ["word", "quoted"].includes(tokens[i + 1]?.kind ?? "")) { + if (dialect.startsWith("postgres") && first.kind === "word" && first.text.toLowerCase() === "only" && (["word", "quoted"].includes(tokens[i + 1]?.kind ?? "") || (isPunct(tokens[i + 1], "(") && ["word", "quoted"].includes(tokens[i + 2]?.kind ?? "")))) { i++ continue } const parts: string[] = [...nameOf(first, dialect)] let at = i - while (isPunct(tokens[at + 1], ".") && ["word", "quoted"].includes(tokens[at + 2]?.kind ?? "")) { + for (;;) { + // T-SQL's `db..table` leaves the schema out: the gap is kept, so the name stays as written and matches nothing it should not. + const gap = isPunct(tokens[at + 1], ".") && isPunct(tokens[at + 2], ".") && ["word", "quoted"].includes(tokens[at + 3]?.kind ?? "") + if (gap) { + parts.push("") + at += 1 + } + if (!isPunct(tokens[at + 1], ".") || !["word", "quoted"].includes(tokens[at + 2]?.kind ?? "")) break at += 2 parts.push(...nameOf(tokens[at], dialect)) } @@ -583,9 +596,7 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ const named = ctes.get(identityOf(last, !!r.quoted, dialect)) // A CTE hides a table only where it is visible: a later statement can name a real table the same way. const cte = named && named.scopes.some(([from, to]) => r.start! >= from && r.start! < to) ? named : undefined - // A CTE's own name inside its own body is the real table unless the WITH is recursive. - const inOwnBody = !!cte && !cte.recursive && cte.bodies.some(([from, to]) => r.start! >= from && r.start! < to) - if (r.parts.length === 1 && cte && !inOwnBody) continue + if (r.parts.length === 1 && cte) continue if (wanted.has(last.toLowerCase()) || wanted.has(whole.toLowerCase())) { add(r) covered.add(last.toLowerCase()) diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index 50f4e4368f..c4b49613e4 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -667,6 +667,21 @@ describe("table lists after a join condition, a sample clause or ONLY", () => { expect(mapRelations(["orders"], "with orders as (select 1) select * from orders", [], "postgres")).toEqual([]) }) + test("a CTE hides a table only after its own definition", () => { + expect(mapRelations(["orders"], "with a as (select * from orders), orders as (select 1) select * from a", [], "postgres").map((r) => r.raw)).toEqual(["orders"]) + expect(mapRelations(["orders"], "with recursive orders as (select 1 union all select * from orders) select * from orders", [], "postgres")).toEqual([]) + expect( + mapRelations(["orders"], "with recursive orders as (select 1 union all select * from orders) select 1; with orders as (select * from orders) select * from orders", [], "postgres").map((r) => r.raw), + ).toEqual(["orders"]) + }) + + test("an escape string, a parenthesized ONLY, an omitted schema and non-ASCII names are read as written", () => { + expect(raws("select E'it\\'s from wrong.orders' as s from real.orders", "postgres")).toEqual(["real.orders"]) + expect(raws("select * from only (raw.orders) a join other.orders b on true", "postgres")).toEqual(["raw.orders", "other.orders"]) + expect(raws("select * from db..orders a join other.orders b on 1=1", "mssql")).toEqual(["db..orders", "other.orders"]) + expect(raws("select * from public.café", "postgres")).toEqual(["public.café"]) + }) + test("T-SQL table hints and PIVOT do not end the list", () => { expect(raws("select * from db.a.orders with (nolock), db.z.orders", "mssql")).toEqual(["db.a.orders", "db.z.orders"]) expect(raws("select * from db.a.orders pivot (sum(x) for y in ([a])) p, db.z.orders", "mssql")).toEqual(["db.a.orders", "db.z.orders"]) From 6056dd3d9e53806c7ea26f698103a8487a72d9c9 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 21:48:46 +0530 Subject: [PATCH 23/32] fix: match unquoted names as the dialect folds them, see every CTE in a recursive WITH, read MySQL index hints and STRAIGHT_JOIN Co-Authored-By: Claude Sonnet 5.5 --- .../src/altimate/tools/dbt-map-relations.ts | 31 ++++++++++++++----- .../test/altimate/dbt-map-relations.test.ts | 12 +++++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 68701b9380..50719051f7 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -164,10 +164,10 @@ const RESERVED = new Set([ "in", ]) // Clause words that start a list of tables. `from` inside these calls reads a column or a string instead. -const TABLE_KEYWORDS = new Set(["from", "join", "into", "update", "using"]) +const TABLE_KEYWORDS = new Set(["from", "join", "straight_join", "into", "update", "using"]) // Words that end a join's ON condition: the next join, or the next clause of the query. const JOIN_CONDITION_END = new Set([ - "join", "inner", "left", "right", "full", "cross", "natural", "outer", "where", "group", "order", "having", + "join", "straight_join", "inner", "left", "right", "full", "cross", "natural", "outer", "where", "group", "order", "having", "union", "intersect", "except", "minus", "limit", "offset", "fetch", "window", "qualify", "for", "returning", "when", "set", "do", "select", "lateral", "distribute", "cluster", "sort", "settings", ]) @@ -349,8 +349,8 @@ export function scanTableReferences(sql: string, dialect: string): Scan { j = afterParens(tokens, j) const known = ctes.get(name) ?? { recursive, bodies: [], scopes: [] } known.bodies.push([open, tokens[j - 1].end]) - // Visible after its own definition, so an earlier CTE or its own body names the real table; a recursive one sees itself. - known.scopes.push([recursive ? open : tokens[j - 1].end, scopeEnd]) + // Visible after its own definition, so an earlier CTE or its own body names the real table; a recursive one sees every CTE of its WITH, later ones included. + known.scopes.push([recursive ? tokens[start - 1].start : tokens[j - 1].end, scopeEnd]) ctes.set(name, known) if (!isPunct(tokens[j], ",")) return i = j + 1 @@ -423,7 +423,8 @@ export function scanTableReferences(sql: string, dialect: string): Scan { // An alias with or without AS, then its column list, then a comma continues a FROM list. if (tokens[i]?.text.toLowerCase() === "as") i++ const alias = tokens[i] - const startsSample = alias?.kind === "word" && ["sample", "tablesample", "pivot", "unpivot"].includes(alias.text.toLowerCase()) && (isPunct(tokens[i + 1], "(") || ["bernoulli", "system", "block", "row"].includes(tokens[i + 1]?.text.toLowerCase() ?? "")) + const startsHint = alias?.kind === "word" && ["use", "force", "ignore"].includes(alias.text.toLowerCase()) && ["index", "key"].includes(tokens[i + 1]?.text.toLowerCase() ?? "") + const startsSample = startsHint || alias?.kind === "word" && ["sample", "tablesample", "pivot", "unpivot"].includes(alias.text.toLowerCase()) && (isPunct(tokens[i + 1], "(") || ["bernoulli", "system", "block", "row"].includes(tokens[i + 1]?.text.toLowerCase() ?? "")) if (alias && !startsSample && (alias.kind === "quoted" || (alias.kind === "word" && !RESERVED.has(alias.text.toLowerCase())))) { i++ if (isPunct(tokens[i], "(")) i = afterParens(tokens, i) @@ -439,13 +440,21 @@ export function scanTableReferences(sql: string, dialect: string): Scan { const skipSampleAndCondition = (start: number) => { let i = start const word = () => (tokens[i]?.kind === "word" ? tokens[i].text.toLowerCase() : "") + // MySQL's `USE|FORCE|IGNORE INDEX|KEY [FOR ...] (...)` also sits there. + const indexHint = () => ["use", "force", "ignore"].includes(word()) && ["index", "key"].includes(tokens[i + 1]?.text.toLowerCase() ?? "") while ( + indexHint() || ["tablesample", "sample", "repeatable", "seed", "bernoulli", "system", "block", "row"].includes(word()) || (["with", "pivot", "unpivot"].includes(word()) && isPunct(tokens[i + 1], "(")) ) { // T-SQL table hints `WITH (...)` and PIVOT / UNPIVOT (with their alias) sit between a table and the comma that continues the list. const pivot = ["pivot", "unpivot"].includes(word()) + const hint = indexHint() i++ + if (hint) { + i++ + while (tokens[i] && !isPunct(tokens[i], "(") && tokens[i].kind === "word" && ["for", "join", "order", "group", "by"].includes(tokens[i].text.toLowerCase())) i++ + } if (isPunct(tokens[i], "(")) i = afterParens(tokens, i) if (pivot) { if (tokens[i]?.text.toLowerCase() === "as") i++ @@ -484,7 +493,7 @@ export function scanTableReferences(sql: string, dialect: string): Scan { // A name followed by "(" after FROM, JOIN or USING is a table function; after INTO or UPDATE it is a table. readList( word === "into" && tokens[i + 1]?.text.toLowerCase() === "table" ? i + 2 : i + 1, - word === "from" || word === "join" || word === "using", + word === "from" || word === "join" || word === "straight_join" || word === "using", ) } } @@ -608,7 +617,11 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ add({ raw: t, parts: splitQualified(t, dialect === "bigquery") }) } return entries.map((entry) => { - const hits: (RelationCandidate & { root: boolean })[] = [] + const hits: (RelationCandidate & { root: boolean; folds: boolean })[] = [] + // Where an unquoted name is folded (Snowflake reads `orders` as ORDERS), the relation it really names comes first. + const direction = FOLDS_UNQUOTED[dialect] + const written = entry.parts[entry.parts.length - 1] + const folded = direction && !entry.raw.includes('"') && written ? (direction === "upper" ? written.toUpperCase() : written.toLowerCase()) : null for (const rel of relations) { const how = matchParts(entry.parts, rel.parts) if (!how) continue @@ -620,10 +633,12 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ relationName: rel.relationName, match: how, root: rel.root, + folds: folded === null || rel.parts[rel.parts.length - 1] === folded, }) } hits.sort( (a, b) => + Number(!a.folds) - Number(!b.folds) || RANK[a.match] - RANK[b.match] || KIND_RANK[a.kind] - KIND_RANK[b.kind] || Number(b.root) - Number(a.root) || a.uniqueId.localeCompare(b.uniqueId), @@ -633,7 +648,7 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ raw: entry.raw, replaceWith: best ? best.replaceWith : entry.raw, match: best ? best.match : "none", - candidates: hits.map(({ root: _root, ...candidate }) => candidate), + candidates: hits.map(({ root: _root, folds: _folds, ...candidate }) => candidate), } }) } diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index c4b49613e4..b17228192c 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -682,6 +682,18 @@ describe("table lists after a join condition, a sample clause or ONLY", () => { expect(raws("select * from public.café", "postgres")).toEqual(["public.café"]) }) + test("a recursive WITH sees every CTE of its list; MySQL index hints and STRAIGHT_JOIN keep their qualifiers", () => { + expect(mapRelations(["orders"], "with recursive a as (select * from orders), orders as (select 1) select * from a", [], "postgres")).toEqual([]) + expect(raws("select * from raw.items use index (idx_id), archive.orders", "mysql")).toEqual(["raw.items", "archive.orders"]) + expect(raws("select * from raw.items force index for join (i) a straight_join correct.orders b on a.id = b.id", "mysql")).toEqual(["raw.items", "correct.orders"]) + }) + + test("an unquoted name is matched as the dialect folds it", () => { + const rel = (name: string, id: string) => ({ parts: ["DB", "S", name], call: `{{ ref('${id}') }}`, kind: "model" as const, name: id, uniqueId: id, relationName: `"DB"."S"."${name}"`, root: true }) + const rows = mapRelations(["orders"], "select * from DB.S.orders", [rel("orders", "a_lower"), rel("ORDERS", "b_upper")], "snowflake") + expect(rows[0].replaceWith).toBe("{{ ref('b_upper') }}") + }) + test("T-SQL table hints and PIVOT do not end the list", () => { expect(raws("select * from db.a.orders with (nolock), db.z.orders", "mssql")).toEqual(["db.a.orders", "db.z.orders"]) expect(raws("select * from db.a.orders pivot (sum(x) for y in ([a])) p, db.z.orders", "mssql")).toEqual(["db.a.orders", "db.z.orders"]) From 9661ee29bc6d2bf033af0c6b8f8cac82c6e723c2 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 21:48:53 +0530 Subject: [PATCH 24/32] fix: match unquoted names as the dialect folds them, see every CTE in a recursive WITH, read MySQL index hints and STRAIGHT_JOIN Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/test/altimate/dbt-map-relations.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index b17228192c..e0e5a31d80 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -685,7 +685,7 @@ describe("table lists after a join condition, a sample clause or ONLY", () => { test("a recursive WITH sees every CTE of its list; MySQL index hints and STRAIGHT_JOIN keep their qualifiers", () => { expect(mapRelations(["orders"], "with recursive a as (select * from orders), orders as (select 1) select * from a", [], "postgres")).toEqual([]) expect(raws("select * from raw.items use index (idx_id), archive.orders", "mysql")).toEqual(["raw.items", "archive.orders"]) - expect(raws("select * from raw.items force index for join (i) a straight_join correct.orders b on a.id = b.id", "mysql")).toEqual(["raw.items", "correct.orders"]) + expect(raws("select * from raw.items a force index for join (i) straight_join correct.orders b on a.id = b.id", "mysql")).toEqual(["raw.items", "correct.orders"]) }) test("an unquoted name is matched as the dialect folds it", () => { From 6714590b1ebf3d9a72beb9e05df417095937770f Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 21:49:36 +0530 Subject: [PATCH 25/32] fix: a FOR JOIN index hint names no table Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/tools/dbt-map-relations.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 50719051f7..09f4cda946 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -490,6 +490,8 @@ export function scanTableReferences(sql: string, dialect: string): Scan { (FROM_INSIDE_CALL.has(calls[calls.length - 1] ?? "") || tokens[i - 1]?.text.toLowerCase() === "distinct") ) continue + // `FOR JOIN` inside a MySQL index hint names no table. + if (word === "join" && tokens[i - 1]?.text.toLowerCase() === "for") continue // A name followed by "(" after FROM, JOIN or USING is a table function; after INTO or UPDATE it is a table. readList( word === "into" && tokens[i + 1]?.text.toLowerCase() === "table" ? i + 2 : i + 1, From 067fa2ad56652e861d80b217d6bc43f6111d4b3a Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 21:56:42 +0530 Subject: [PATCH 26/32] fix: fold only an unquoted last name part Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/tools/dbt-map-relations.ts | 2 +- packages/opencode/test/altimate/dbt-map-relations.test.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 09f4cda946..f0e51583c4 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -623,7 +623,7 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ // Where an unquoted name is folded (Snowflake reads `orders` as ORDERS), the relation it really names comes first. const direction = FOLDS_UNQUOTED[dialect] const written = entry.parts[entry.parts.length - 1] - const folded = direction && !entry.raw.includes('"') && written ? (direction === "upper" ? written.toUpperCase() : written.toLowerCase()) : null + const folded = direction && !entry.quoted && written ? (direction === "upper" ? written.toUpperCase() : written.toLowerCase()) : null for (const rel of relations) { const how = matchParts(entry.parts, rel.parts) if (!how) continue diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index e0e5a31d80..e40a9b7eb6 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -692,6 +692,8 @@ describe("table lists after a join condition, a sample clause or ONLY", () => { const rel = (name: string, id: string) => ({ parts: ["DB", "S", name], call: `{{ ref('${id}') }}`, kind: "model" as const, name: id, uniqueId: id, relationName: `"DB"."S"."${name}"`, root: true }) const rows = mapRelations(["orders"], "select * from DB.S.orders", [rel("orders", "a_lower"), rel("ORDERS", "b_upper")], "snowflake") expect(rows[0].replaceWith).toBe("{{ ref('b_upper') }}") + const mixed = mapRelations(["orders"], 'select * from "DB"."S".orders', [rel("orders", "a_lower"), rel("ORDERS", "b_upper")], "snowflake") + expect(mixed[0].replaceWith).toBe("{{ ref('b_upper') }}") }) test("T-SQL table hints and PIVOT do not end the list", () => { From 3d646521acae414fed45d36fb7c7db5a82f5f2ca Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 22:01:54 +0530 Subject: [PATCH 27/32] fix: read the target of INSERT OVERWRITE TABLE and CREATE TABLE with its qualifiers Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/tools/dbt-map-relations.ts | 7 ++++++- packages/opencode/test/altimate/dbt-map-relations.test.ts | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index f0e51583c4..f93d74eba8 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -484,7 +484,12 @@ export function scanTableReferences(sql: string, dialect: string): Scan { else if (token.kind === "word") { const word = token.text.toLowerCase() if (word === "with") readCtes(i + 1) - else if (TABLE_KEYWORDS.has(word)) { + else if (word === "table" && ["overwrite", "create", "replace", "temporary", "temp", "transient", "external", "volatile", "unlogged", "global", "local"].includes(tokens[i - 1]?.text.toLowerCase() ?? "")) { + // `INSERT OVERWRITE TABLE t` and `CREATE [OR REPLACE] TABLE t`: the target is read with the qualifiers written. + let at = i + 1 + while (["if", "not", "exists"].includes(tokens[at]?.text.toLowerCase() ?? "")) at++ + readList(at, false) + } else if (TABLE_KEYWORDS.has(word)) { if ( word === "from" && (FROM_INSIDE_CALL.has(calls[calls.length - 1] ?? "") || tokens[i - 1]?.text.toLowerCase() === "distinct") diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index e40a9b7eb6..28667e7946 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -696,6 +696,12 @@ describe("table lists after a join condition, a sample clause or ONLY", () => { expect(mixed[0].replaceWith).toBe("{{ ref('b_upper') }}") }) + test("the target of INSERT OVERWRITE TABLE and CREATE TABLE keeps its qualifiers", () => { + expect(raws("insert overwrite table main.live.orders select * from main.raw.input", "databricks")).toEqual(["main.live.orders", "main.raw.input"]) + expect(raws("create or replace table main.live.orders as select * from main.raw.input", "snowflake")).toEqual(["main.live.orders", "main.raw.input"]) + expect(raws("create table if not exists main.live.orders as select * from main.raw.input", "postgres")).toEqual(["main.live.orders", "main.raw.input"]) + }) + test("T-SQL table hints and PIVOT do not end the list", () => { expect(raws("select * from db.a.orders with (nolock), db.z.orders", "mssql")).toEqual(["db.a.orders", "db.z.orders"]) expect(raws("select * from db.a.orders pivot (sum(x) for y in ([a])) p, db.z.orders", "mssql")).toEqual(["db.a.orders", "db.z.orders"]) From 27ddc95d4413fc729bbd1cd1f6202580ca853fd4 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 22:08:21 +0530 Subject: [PATCH 28/32] fix: read the target of INSERT OVERWRITE without TABLE Co-Authored-By: Claude Sonnet 5.5 --- packages/opencode/src/altimate/tools/dbt-map-relations.ts | 3 +++ packages/opencode/test/altimate/dbt-map-relations.test.ts | 1 + 2 files changed, 4 insertions(+) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index f93d74eba8..0cd66ec081 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -489,6 +489,9 @@ export function scanTableReferences(sql: string, dialect: string): Scan { let at = i + 1 while (["if", "not", "exists"].includes(tokens[at]?.text.toLowerCase() ?? "")) at++ readList(at, false) + } else if (word === "overwrite" && !["table", "directory", "local"].includes(tokens[i + 1]?.text.toLowerCase() ?? "")) { + // Databricks' `INSERT OVERWRITE name`, without the optional TABLE. + readList(i + 1, false) } else if (TABLE_KEYWORDS.has(word)) { if ( word === "from" && diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index 28667e7946..f4f0af3b90 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -698,6 +698,7 @@ describe("table lists after a join condition, a sample clause or ONLY", () => { test("the target of INSERT OVERWRITE TABLE and CREATE TABLE keeps its qualifiers", () => { expect(raws("insert overwrite table main.live.orders select * from main.raw.input", "databricks")).toEqual(["main.live.orders", "main.raw.input"]) + expect(raws("insert overwrite main.live.orders select * from main.raw.input", "databricks")).toEqual(["main.live.orders", "main.raw.input"]) expect(raws("create or replace table main.live.orders as select * from main.raw.input", "snowflake")).toEqual(["main.live.orders", "main.raw.input"]) expect(raws("create table if not exists main.live.orders as select * from main.raw.input", "postgres")).toEqual(["main.live.orders", "main.raw.input"]) }) From 241effe24fa18c13e3448ea1fc5cf288d553a12c Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 07:00:52 +0530 Subject: [PATCH 29/32] fix: read DDL and select-option edge cases, match db..table, rank unquoted relations by the dialect's fold Trino folds unquoted names, DuckDB reads E strings, block comments nest as the shared tokenizer says, BigQuery hyphenated ids survive in CREATE TABLE targets, SELECT STRAIGHT_JOIN is an option, INSERT OVERWRITE only follows INSERT, and db..table matches any schema as a partial match. Co-Authored-By: Claude Opus 5.5 --- .../src/altimate/tools/dbt-map-relations.ts | 55 ++++++++++++------- .../test/altimate/dbt-map-relations.test.ts | 50 +++++++++++++++++ 2 files changed, 85 insertions(+), 20 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 0cd66ec081..5714692ddb 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -98,8 +98,8 @@ type Reference = { raw: string; parts: string[]; start?: number; /** The last pa /** What the scan of a query found: its table references in text order, and the names its CTEs define. */ export type Scan = { refs: Reference[] - /** By identity (see `identityOf`): whether the WITH is recursive, the text offsets of each body that defines the name, and the text range each definition is visible in (its statement or enclosing parentheses). */ - ctes: Map + /** By identity (see `identityOf`): the text range each definition of the name is visible in (its statement or enclosing parentheses). */ + ctes: Map } // Dialects whose double quotes (and, for some, backslashes) belong to string literals. @@ -108,11 +108,11 @@ const BACKSLASH_ESCAPE = new Set(["bigquery", "databricks", "spark", "mysql", "h const DOLLAR_QUOTE = new Set(["snowflake", "postgres", "redshift", "duckdb"]) const IMPLICIT_RECURSION = new Set(["snowflake", "tsql", "sqlserver", "mssql", "fabric", "synapse", "oracle"]) // Dialects whose block comments nest: an inner comment does not end the outer one. -const NESTED_COMMENTS = new Set(["postgres", "postgresql", "redshift", "cockroachdb", "duckdb", "tsql", "sqlserver", "mssql", "fabric", "synapse"]) -const ESCAPE_STRING = new Set(["postgres", "postgresql", "redshift", "cockroachdb"]) +const NESTED_COMMENTS = new Set(["postgres", "postgresql", "cockroachdb", "duckdb", "tsql", "sqlserver", "mssql", "fabric", "synapse", "clickhouse", "spark", "databricks"]) +const ESCAPE_STRING = new Set(["postgres", "postgresql", "redshift", "cockroachdb", "duckdb"]) const HASH_COMMENT = new Set(["bigquery", "mysql", "mariadb", "clickhouse"]) // Dialects where a double-quoted name keeps its case while an unquoted one is folded, so `"orders"` and `ORDERS` differ. -const FOLDS_UNQUOTED: Record = { snowflake: "upper", oracle: "upper", postgres: "lower", postgresql: "lower", redshift: "lower" } +const FOLDS_UNQUOTED: Record = { snowflake: "upper", oracle: "upper", postgres: "lower", postgresql: "lower", redshift: "lower", trino: "lower" } const BRACKET_QUOTE = new Set(["tsql", "sqlserver", "mssql", "fabric", "synapse"]) const RESERVED = new Set([ @@ -171,6 +171,13 @@ const JOIN_CONDITION_END = new Set([ "union", "intersect", "except", "minus", "limit", "offset", "fetch", "window", "qualify", "for", "returning", "when", "set", "do", "select", "lateral", "distribute", "cluster", "sort", "settings", ]) +// Words that can stand between `SELECT` and a `STRAIGHT_JOIN` select option; there it names no table. +const SELECT_OPTIONS = new Set(["select", "all", "distinct", "distinctrow", "high_priority", "sql_small_result", "sql_big_result", "sql_buffer_result", "sql_no_cache", "sql_calc_found_rows"]) +// Words that stand before `TABLE` and name a statement that creates or overwrites one. +const DDL_TABLE_PREFIX = new Set(["overwrite", "create", "replace", "temporary", "temp", "transient", "external", "volatile", "unlogged", "global", "local"]) +const CREATE_TABLE_MODIFIERS = new Set(["if", "not", "exists"]) +// After these a BigQuery project id may carry hyphens. +const HYPHEN_AFTER = new Set([...TABLE_KEYWORDS, "table", "exists"]) const SUBQUERY_START = new Set(["select", "with", "values", "table", "from"]) const FROM_INSIDE_CALL = new Set(["extract", "trim", "substring", "overlay", "position"]) @@ -253,9 +260,9 @@ function tokenize(sql: string, dialect: string): Token[] { i = end } else if (/[\p{L}_]/u.test(ch)) { let j = i + 1 - while (j < n && /[\p{L}\p{N}_$]/u.test(sql[j])) j++ + while (j < n && /[\p{L}\p{M}\p{N}_$]/u.test(sql[j])) j++ // BigQuery project ids may contain hyphens (`my-proj.ds.tbl` unquoted), but only where a table is named. - if (dialect === "bigquery" && (TABLE_KEYWORDS.has(lastWord()) || out[out.length - 1]?.text === ",")) { + if (dialect === "bigquery" && (HYPHEN_AFTER.has(lastWord()) || out[out.length - 1]?.text === ",")) { while (sql[j] === "-" && /[A-Za-z0-9_]/.test(sql[j + 1] ?? "")) { j++ while (j < n && /[\p{L}\p{N}_$]/u.test(sql[j])) j++ @@ -345,10 +352,8 @@ export function scanTableReferences(sql: string, dialect: string): Scan { if (tokens[j]?.text.toLowerCase() === "not") j++ if (tokens[j]?.text.toLowerCase() === "materialized") j++ if (!isPunct(tokens[j], "(")) return - const open = tokens[j].start j = afterParens(tokens, j) - const known = ctes.get(name) ?? { recursive, bodies: [], scopes: [] } - known.bodies.push([open, tokens[j - 1].end]) + const known = ctes.get(name) ?? { scopes: [] } // Visible after its own definition, so an earlier CTE or its own body names the real table; a recursive one sees every CTE of its WITH, later ones included. known.scopes.push([recursive ? tokens[start - 1].start : tokens[j - 1].end, scopeEnd]) ctes.set(name, known) @@ -357,6 +362,9 @@ export function scanTableReferences(sql: string, dialect: string): Scan { } } + const isIndexHint = (at: number) => + tokens[at]?.kind === "word" && ["use", "force", "ignore"].includes(tokens[at].text.toLowerCase()) && ["index", "key"].includes(tokens[at + 1]?.text.toLowerCase() ?? "") + const readList = (start: number, strict: boolean) => { let i = start // FROM and JOIN lists may hold a parenthesised group at any position. After USING a `(` is a column list @@ -366,7 +374,7 @@ export function scanTableReferences(sql: string, dialect: string): Scan { const isGroup = (at: number) => !using || tokens.slice(at, afterParens(tokens, at)).some((t) => t.kind === "word" && t.text.toLowerCase() === "join") let groupEnd = -1 - for (let item = 0; ; item++) { + for (;;) { // LATERAL prefixes a table expression in the list; the list goes on after it. if (tokens[i]?.kind === "word" && tokens[i].text.toLowerCase() === "lateral") i++ // `(a join b on ...)` groups tables, at any position in the list: its first table is read like any other, and the list goes on after the @@ -423,7 +431,7 @@ export function scanTableReferences(sql: string, dialect: string): Scan { // An alias with or without AS, then its column list, then a comma continues a FROM list. if (tokens[i]?.text.toLowerCase() === "as") i++ const alias = tokens[i] - const startsHint = alias?.kind === "word" && ["use", "force", "ignore"].includes(alias.text.toLowerCase()) && ["index", "key"].includes(tokens[i + 1]?.text.toLowerCase() ?? "") + const startsHint = isIndexHint(i) const startsSample = startsHint || alias?.kind === "word" && ["sample", "tablesample", "pivot", "unpivot"].includes(alias.text.toLowerCase()) && (isPunct(tokens[i + 1], "(") || ["bernoulli", "system", "block", "row"].includes(tokens[i + 1]?.text.toLowerCase() ?? "")) if (alias && !startsSample && (alias.kind === "quoted" || (alias.kind === "word" && !RESERVED.has(alias.text.toLowerCase())))) { i++ @@ -441,7 +449,7 @@ export function scanTableReferences(sql: string, dialect: string): Scan { let i = start const word = () => (tokens[i]?.kind === "word" ? tokens[i].text.toLowerCase() : "") // MySQL's `USE|FORCE|IGNORE INDEX|KEY [FOR ...] (...)` also sits there. - const indexHint = () => ["use", "force", "ignore"].includes(word()) && ["index", "key"].includes(tokens[i + 1]?.text.toLowerCase() ?? "") + const indexHint = () => isIndexHint(i) while ( indexHint() || ["tablesample", "sample", "repeatable", "seed", "bernoulli", "system", "block", "row"].includes(word()) || @@ -453,7 +461,7 @@ export function scanTableReferences(sql: string, dialect: string): Scan { i++ if (hint) { i++ - while (tokens[i] && !isPunct(tokens[i], "(") && tokens[i].kind === "word" && ["for", "join", "order", "group", "by"].includes(tokens[i].text.toLowerCase())) i++ + while (tokens[i] && ["for", "join", "order", "group", "by"].includes(word())) i++ } if (isPunct(tokens[i], "(")) i = afterParens(tokens, i) if (pivot) { @@ -484,12 +492,12 @@ export function scanTableReferences(sql: string, dialect: string): Scan { else if (token.kind === "word") { const word = token.text.toLowerCase() if (word === "with") readCtes(i + 1) - else if (word === "table" && ["overwrite", "create", "replace", "temporary", "temp", "transient", "external", "volatile", "unlogged", "global", "local"].includes(tokens[i - 1]?.text.toLowerCase() ?? "")) { + else if (word === "table" && DDL_TABLE_PREFIX.has(tokens[i - 1]?.text.toLowerCase() ?? "")) { // `INSERT OVERWRITE TABLE t` and `CREATE [OR REPLACE] TABLE t`: the target is read with the qualifiers written. let at = i + 1 - while (["if", "not", "exists"].includes(tokens[at]?.text.toLowerCase() ?? "")) at++ + while (CREATE_TABLE_MODIFIERS.has(tokens[at]?.text.toLowerCase() ?? "")) at++ readList(at, false) - } else if (word === "overwrite" && !["table", "directory", "local"].includes(tokens[i + 1]?.text.toLowerCase() ?? "")) { + } else if (word === "overwrite" && tokens[i - 1]?.text.toLowerCase() === "insert" && !["table", "into", "directory", "local"].includes(tokens[i + 1]?.text.toLowerCase() ?? "")) { // Databricks' `INSERT OVERWRITE name`, without the optional TABLE. readList(i + 1, false) } else if (TABLE_KEYWORDS.has(word)) { @@ -500,6 +508,10 @@ export function scanTableReferences(sql: string, dialect: string): Scan { continue // `FOR JOIN` inside a MySQL index hint names no table. if (word === "join" && tokens[i - 1]?.text.toLowerCase() === "for") continue + // `INSERT OVERWRITE DIRECTORY 'path' USING format` names a file format. + if (word === "using" && tokens[i - 1]?.text.toLowerCase() === "directory") continue + // `SELECT STRAIGHT_JOIN ` is a select option, not a join. + if (word === "straight_join" && SELECT_OPTIONS.has(tokens[i - 1]?.text.toLowerCase() ?? "")) continue // A name followed by "(" after FROM, JOIN or USING is a table function; after INTO or UPDATE it is a table. readList( word === "into" && tokens[i + 1]?.text.toLowerCase() === "table" ? i + 2 : i + 1, @@ -586,9 +598,11 @@ export function manifestRelations(manifest: any, dialect?: string): Relation[] { export function matchParts(parts: string[], relation: string[]): Exclude | null { if (parts.length === 0 || parts.length > relation.length) return null const tail = relation.slice(relation.length - parts.length) + // T-SQL's `db..table` leaves the schema out: the empty part stands for whatever schema the relation has, so it is only a partial match. + const gap = parts.includes("") const same = parts.every((p, i) => p === tail[i]) - const fold = parts.every((p, i) => p.toLowerCase() === tail[i].toLowerCase()) - if (parts.length < relation.length) return fold ? "partial" : null + const fold = parts.every((p, i) => p === "" || p.toLowerCase() === tail[i].toLowerCase()) + if (parts.length < relation.length || gap) return fold ? "partial" : null if (same) return "exact" if (fold) return "case_insensitive" return null @@ -643,7 +657,8 @@ export function mapRelations(tables: string[], sql: string, relations: Relation[ relationName: rel.relationName, match: how, root: rel.root, - folds: folded === null || rel.parts[rel.parts.length - 1] === folded, + // A relation written without quotes in the manifest is folded by the warehouse like the name in the query, so it is the one named. + folds: folded === null || rel.parts[rel.parts.length - 1] === folded || !/["`\]]$/.test(rel.relationName), }) } hits.sort( diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index f4f0af3b90..6ae31b68ea 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -703,6 +703,56 @@ describe("table lists after a join condition, a sample clause or ONLY", () => { expect(raws("create table if not exists main.live.orders as select * from main.raw.input", "postgres")).toEqual(["main.live.orders", "main.raw.input"]) }) + test("INSERT OVERWRITE skips DIRECTORY, LOCAL DIRECTORY and INTO, and only follows INSERT", () => { + expect(raws("insert overwrite directory '/tmp/x' using parquet select * from main.raw.input", "databricks")).toEqual(["main.raw.input"]) + expect(raws("insert overwrite local directory '/tmp/x' select * from main.raw.input", "databricks")).toEqual(["main.raw.input"]) + expect(raws("insert overwrite into main.live.orders select * from main.raw.input", "snowflake")).toEqual(["main.live.orders", "main.raw.input"]) + expect(raws("select * from main.raw.input order by overwrite desc", "databricks")).toEqual(["main.raw.input"]) + }) + + test("SELECT STRAIGHT_JOIN is an option and a bare STRAIGHT_JOIN joins a table", () => { + expect(raws("select straight_join orders, count(*) from db.orders", "mysql")).toEqual(["db.orders"]) + expect(raws("select distinct straight_join orders from db.orders", "mysql")).toEqual(["db.orders"]) + expect(raws("select * from db.one.orders o straight_join db.two.payments p on o.id = p.id", "mysql")).toEqual(["db.one.orders", "db.two.payments"]) + }) + + test("BigQuery hyphenated project ids survive in a CREATE TABLE target", () => { + expect(raws("create table my-proj.ds.orders as select * from my-proj.ds.input", "bigquery")).toEqual(["my-proj.ds.orders", "my-proj.ds.input"]) + expect(raws("create table if not exists my-proj.ds.orders as select 1", "bigquery")).toEqual(["my-proj.ds.orders"]) + }) + + test("a name with a combining mark stays one name", () => { + expect(raws("select * from raw.cafe\u0301", "snowflake")).toEqual(["raw.cafe\u0301"]) + }) + + test("E strings with an escaped quote hide nothing in DuckDB; block comments nest as sql-tokenize says", () => { + expect(raws("select E'it\\'s' as p from db.right.orders", "duckdb")).toEqual(["db.right.orders"]) + expect(raws("select * from db.a.orders /* outer /* nested */ from db.wrong.orders */", "spark")).toEqual(["db.a.orders"]) + expect(raws("select * from db.a.orders /* outer /* nested */ from db.wrong.orders */", "clickhouse")).toEqual(["db.a.orders"]) + }) + + test("Trino folds an unquoted name to lower case, so a quoted CTE of another case hides nothing", () => { + expect(mapRelations(["orders"], 'with "Orders" as (select 1) select * from orders', [], "trino").map((r) => r.raw)).toEqual(["orders"]) + }) + + test("T-SQL db..table matches the relation of any schema as a partial match", () => { + const rel = { parts: ["db", "dbo", "orders"], call: "{{ ref('orders') }}", kind: "model" as const, name: "orders", uniqueId: "model.p.orders", relationName: "db.dbo.orders", root: true } + const rows = mapRelations(["orders"], "select * from db..orders", [rel], "mssql") + expect(rows.map((r) => [r.raw, r.match])).toEqual([["db..orders", "partial"]]) + expect(matchParts(["db", "", "orders"], ["other", "dbo", "orders"])).toBeNull() + }) + + test("an unquoted manifest relation that folds to the written name outranks a quoted one that does not", () => { + const rel = (relationName: string, parts: string[], id: string, match: string) => ({ parts, call: `{{ ref('${id}') }}`, kind: "model" as const, name: id, uniqueId: id, relationName, root: true, match }) + const rows = mapRelations( + ["orders"], + "select * from DB.S.orders", + [rel('"DB"."S"."ORDERS"', ["DB", "S", "ORDERS"], "quoted_upper", ""), rel("DB.S.orders", ["DB", "S", "orders"], "plain", "")], + "snowflake", + ) + expect(rows[0].replaceWith).toBe("{{ ref('plain') }}") + }) + test("T-SQL table hints and PIVOT do not end the list", () => { expect(raws("select * from db.a.orders with (nolock), db.z.orders", "mssql")).toEqual(["db.a.orders", "db.z.orders"]) expect(raws("select * from db.a.orders pivot (sum(x) for y in ([a])) p, db.z.orders", "mssql")).toEqual(["db.a.orders", "db.z.orders"]) From 052c1c417afc5966a41da09b1a64c2e58b6dffed Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 07:30:06 +0530 Subject: [PATCH 30/32] feat: dbt_map_relations takes a project folder and finds its manifest as dbt does Co-Authored-By: Claude Opus 5.5 --- .../src/altimate/tools/dbt-map-relations.ts | 10 +++++--- .../test/altimate/dbt-map-relations.test.ts | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 5714692ddb..9fee0db7a8 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -711,6 +711,10 @@ export const DbtMapRelationsTool = Tool.define("dbt_map_relations", { .string() .optional() .describe("manifest.json to match against (default: the dbt project's target/manifest.json)"), + project_dir: z + .string() + .optional() + .describe("dbt project folder whose manifest to use, found as dbt finds it (DBT_TARGET_PATH, then target-path); ignored when manifest_path is given"), }), async execute(args): Promise<{ title: string; metadata: MapRelationsMetadata; output: string }> { const fail = (msg: string) => ({ @@ -719,7 +723,7 @@ export const DbtMapRelationsTool = Tool.define("dbt_map_relations", { output: msg, }) try { - const manifestPath = args.manifest_path ?? (await defaultManifestPath()) + const manifestPath = args.manifest_path ?? (await defaultManifestPath(args.project_dir)) if (!manifestPath) return fail("No dbt project found here. Run from a dbt project or pass manifest_path.") const manifest = loadRawManifest(manifestPath) if (!manifest) return fail(`No manifest at ${manifestPath}. Run \`dbt parse\` or pass manifest_path.`) @@ -744,9 +748,9 @@ export const DbtMapRelationsTool = Tool.define("dbt_map_relations", { }, }) -async function defaultManifestPath(): Promise { +async function defaultManifestPath(projectDir?: string): Promise { const { findProjectRoot } = await import("../../../../dbt-tools/src/config") - const found = findProjectRoot(projectRoot()) + const found = projectDir ?? findProjectRoot(projectRoot()) const root = found ?? (await (await import("../native/connections/register")).getDbtProjectRoot()) if (!root) return null let real = root diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index 6ae31b68ea..c1a2e66844 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -469,6 +469,31 @@ describe("dbt_map_relations tool", () => { } }) + test("project_dir names the project, and its manifest is found as dbt finds it: DBT_TARGET_PATH first, then a templated target-path at its env_var default", async () => { + const dir = realpathSync(mkdtempSync(join(tmpdir(), "map-relations-pd-"))) + dirs.push(dir) + writeFileSync(join(dir, "dbt_project.yml"), "name: jaffle\ntarget-path: \"{{ env_var('MAP_REL_TARGET', 'built') }}\"\n") + for (const folder of ["built", "ci_out"]) { + mkdirSync(join(dir, folder)) + writeFileSync(join(dir, folder, "manifest.json"), JSON.stringify(MANIFEST)) + } + const saved = { target: process.env.DBT_TARGET_PATH, own: process.env.MAP_REL_TARGET } + try { + delete process.env.DBT_TARGET_PATH + delete process.env.MAP_REL_TARGET + expect((await run({ sql: "select * from legacy.thing", project_dir: dir })).metadata.manifest_path).toBe(join(dir, "built", "manifest.json")) + process.env.MAP_REL_TARGET = "ci_out" + expect((await run({ sql: "select * from legacy.thing", project_dir: dir })).metadata.manifest_path).toBe(join(dir, "ci_out", "manifest.json")) + process.env.DBT_TARGET_PATH = "built" + expect((await run({ sql: "select * from legacy.thing", project_dir: dir })).metadata.manifest_path).toBe(join(dir, "built", "manifest.json")) + } finally { + for (const [key, value] of [["DBT_TARGET_PATH", saved.target], ["MAP_REL_TARGET", saved.own]] as const) { + if (value === undefined) delete process.env[key] + else process.env[key] = value + } + } + }) + test("outside a dbt project, without manifest_path, the error says so", async () => { const dir = realpathSync(mkdtempSync(join(tmpdir(), "map-relations-bare-"))) dirs.push(dir) From 31a6a3d10e211fbd5d32f844ed61d46dc080dc36 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 09:45:27 +0530 Subject: [PATCH 31/32] fix: an empty project_dir is treated as absent Co-Authored-By: Claude Opus 5.5 --- .../src/altimate/tools/dbt-map-relations.ts | 2 +- .../test/altimate/dbt-map-relations.test.ts | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/altimate/tools/dbt-map-relations.ts b/packages/opencode/src/altimate/tools/dbt-map-relations.ts index 9fee0db7a8..cafbf26adf 100644 --- a/packages/opencode/src/altimate/tools/dbt-map-relations.ts +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -750,7 +750,7 @@ export const DbtMapRelationsTool = Tool.define("dbt_map_relations", { async function defaultManifestPath(projectDir?: string): Promise { const { findProjectRoot } = await import("../../../../dbt-tools/src/config") - const found = projectDir ?? findProjectRoot(projectRoot()) + const found = projectDir?.trim() || findProjectRoot(projectRoot()) const root = found ?? (await (await import("../native/connections/register")).getDbtProjectRoot()) if (!root) return null let real = root diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index c1a2e66844..da42686c17 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -494,6 +494,29 @@ describe("dbt_map_relations tool", () => { } }) + test("an empty or blank project_dir is absent: the project around the working directory is found", async () => { + const dir = realpathSync(mkdtempSync(join(tmpdir(), "map-relations-blank-"))) + dirs.push(dir) + writeFileSync(join(dir, "dbt_project.yml"), "name: jaffle\n") + mkdirSync(join(dir, "target")) + writeFileSync(join(dir, "target", "manifest.json"), JSON.stringify(MANIFEST)) + const cwd = process.cwd() + const home = process.env.HOME + process.env.HOME = dir + process.chdir(dir) + try { + for (const blank of ["", " "]) { + const result = await run({ sql: "select * from legacy.thing", project_dir: blank }) + expect(result.metadata.success).toBe(true) + expect(result.metadata.manifest_path).toBe(join(dir, "target", "manifest.json")) + } + } finally { + process.chdir(cwd) + if (home === undefined) delete process.env.HOME + else process.env.HOME = home + } + }) + test("outside a dbt project, without manifest_path, the error says so", async () => { const dir = realpathSync(mkdtempSync(join(tmpdir(), "map-relations-bare-"))) dirs.push(dir) From 73b7350f1e203d88bd76d98b437970def32fed01 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 09:54:58 +0530 Subject: [PATCH 32/32] test: make the map-relations tool tests hermetic and the fold ranking test discriminating Co-Authored-By: Claude Opus 5.5 --- .../test/altimate/dbt-map-relations.test.ts | 85 +++++++++---------- 1 file changed, 38 insertions(+), 47 deletions(-) diff --git a/packages/opencode/test/altimate/dbt-map-relations.test.ts b/packages/opencode/test/altimate/dbt-map-relations.test.ts index da42686c17..8f878a36e0 100644 --- a/packages/opencode/test/altimate/dbt-map-relations.test.ts +++ b/packages/opencode/test/altimate/dbt-map-relations.test.ts @@ -419,6 +419,32 @@ describe("dbt_map_relations tool", () => { writeFileSync(p, JSON.stringify(manifest)) return p } + /** Runs `fn` with a temp dir as the working directory and HOME and no DBT_TARGET_PATH, all restored after; a dbt project with a manifest is written there when `manifest` is given. */ + async function inTempProject(manifest: unknown | null, fn: (dir: string) => Promise): Promise { + const dir = realpathSync(mkdtempSync(join(tmpdir(), "map-relations-project-"))) + dirs.push(dir) + if (manifest) { + writeFileSync(join(dir, "dbt_project.yml"), "name: jaffle\n") + mkdirSync(join(dir, "target")) + writeFileSync(join(dir, "target", "manifest.json"), JSON.stringify(manifest)) + } + const cwd = process.cwd() + const home = process.env.HOME + const target = process.env.DBT_TARGET_PATH + delete process.env.DBT_TARGET_PATH + process.env.HOME = dir + process.chdir(dir) + try { + return await fn(dir) + } finally { + process.chdir(cwd) + if (home === undefined) delete process.env.HOME + else process.env.HOME = home + if (target === undefined) delete process.env.DBT_TARGET_PATH + else process.env.DBT_TARGET_PATH = target + } + } + async function run(args: Record) { const tool = await initTool(DbtMapRelationsTool) return tool.execute(args, ctx) @@ -449,24 +475,11 @@ describe("dbt_map_relations tool", () => { }) test("without manifest_path the dbt project around the working directory supplies target/manifest.json", async () => { - const dir = realpathSync(mkdtempSync(join(tmpdir(), "map-relations-project-"))) - dirs.push(dir) - writeFileSync(join(dir, "dbt_project.yml"), "name: jaffle\n") - mkdirSync(join(dir, "target")) - writeFileSync(join(dir, "target", "manifest.json"), JSON.stringify(MANIFEST)) - const cwd = process.cwd() - const home = process.env.HOME - process.env.HOME = dir - process.chdir(dir) - try { + await inTempProject(MANIFEST, async (dir) => { const result = await run({ sql: "select * from legacy.thing" }) expect(result.metadata.success).toBe(true) expect(result.metadata.manifest_path).toBe(join(dir, "target", "manifest.json")) - } finally { - process.chdir(cwd) - if (home === undefined) delete process.env.HOME - else process.env.HOME = home - } + }) }) test("project_dir names the project, and its manifest is found as dbt finds it: DBT_TARGET_PATH first, then a templated target-path at its env_var default", async () => { @@ -495,44 +508,21 @@ describe("dbt_map_relations tool", () => { }) test("an empty or blank project_dir is absent: the project around the working directory is found", async () => { - const dir = realpathSync(mkdtempSync(join(tmpdir(), "map-relations-blank-"))) - dirs.push(dir) - writeFileSync(join(dir, "dbt_project.yml"), "name: jaffle\n") - mkdirSync(join(dir, "target")) - writeFileSync(join(dir, "target", "manifest.json"), JSON.stringify(MANIFEST)) - const cwd = process.cwd() - const home = process.env.HOME - process.env.HOME = dir - process.chdir(dir) - try { + await inTempProject(MANIFEST, async (dir) => { for (const blank of ["", " "]) { const result = await run({ sql: "select * from legacy.thing", project_dir: blank }) expect(result.metadata.success).toBe(true) expect(result.metadata.manifest_path).toBe(join(dir, "target", "manifest.json")) } - } finally { - process.chdir(cwd) - if (home === undefined) delete process.env.HOME - else process.env.HOME = home - } + }) }) test("outside a dbt project, without manifest_path, the error says so", async () => { - const dir = realpathSync(mkdtempSync(join(tmpdir(), "map-relations-bare-"))) - dirs.push(dir) - const cwd = process.cwd() - const home = process.env.HOME - process.env.HOME = dir - process.chdir(dir) - try { + await inTempProject(null, async () => { const result = await run({ sql: "select * from t" }) expect(result.metadata.success).toBe(false) expect(result.output).toBe("No dbt project found here. Run from a dbt project or pass manifest_path.") - } finally { - process.chdir(cwd) - if (home === undefined) delete process.env.HOME - else process.env.HOME = home - } + }) }) test("with the real parser, a table a CTE shares its name with and a dot spaced out are both mapped", async () => { @@ -790,15 +780,16 @@ describe("table lists after a join condition, a sample clause or ONLY", () => { expect(matchParts(["db", "", "orders"], ["other", "dbo", "orders"])).toBeNull() }) - test("an unquoted manifest relation that folds to the written name outranks a quoted one that does not", () => { - const rel = (relationName: string, parts: string[], id: string, match: string) => ({ parts, call: `{{ ref('${id}') }}`, kind: "model" as const, name: id, uniqueId: id, relationName, root: true, match }) + test("an unquoted manifest relation outranks a quoted one of another case when the match is otherwise the same", () => { + const rel = (relationName: string, parts: string[], id: string) => ({ parts, call: `{{ ref('${id}') }}`, kind: "model" as const, name: id, uniqueId: id, relationName, root: true }) + // Both are a case-insensitive match for `db.s.orders`; Snowflake reads that as ORDERS, which only the unquoted relation is. const rows = mapRelations( ["orders"], - "select * from DB.S.orders", - [rel('"DB"."S"."ORDERS"', ["DB", "S", "ORDERS"], "quoted_upper", ""), rel("DB.S.orders", ["DB", "S", "orders"], "plain", "")], + "select * from db.s.orders", + [rel('"DB"."S"."orders"', ["DB", "S", "orders"], "a_quoted_lower"), rel("DB.S.orders", ["DB", "S", "orders"], "b_unquoted")], "snowflake", ) - expect(rows[0].replaceWith).toBe("{{ ref('plain') }}") + expect(rows[0].replaceWith).toBe("{{ ref('b_unquoted') }}") }) test("T-SQL table hints and PIVOT do not end the list", () => {