diff --git a/packages/opencode/src/altimate/api/client.ts b/packages/opencode/src/altimate/api/client.ts index 7a03aeb906..cb53c0ec7b 100644 --- a/packages/opencode/src/altimate/api/client.ts +++ b/packages/opencode/src/altimate/api/client.ts @@ -347,6 +347,26 @@ export namespace AltimateApi { // altimate_change end } + /** + * Calls an Altimate API endpoint with the stored credentials (Bearer key and x-tenant headers) and + * returns the parsed JSON body. Endpoint functions in other files go through this. Fails with a + * message naming the credentials file when none are stored or they can't be read. + */ + export async function apiRequest(method: string, endpoint: string, body?: unknown): Promise { + let creds: AltimateCredentials + try { + creds = await getCredentials() + } catch (e) { + const p = credentialsPath() + if (!(await Filesystem.exists(p))) + throw new Error( + `No Altimate credentials are stored: sign in to Altimate in altimate-code first (they are saved to ${p}).`, + ) + throw new Error(`The Altimate credentials in ${p} can't be used: ${e instanceof Error ? e.message : String(e)}`) + } + return request(creds, method, endpoint, body) + } + export async function listDatamates(creds?: AltimateCredentials) { creds ??= await getCredentials() const data = await request(creds, "GET", "/datamates/") diff --git a/packages/opencode/src/altimate/native/connections/dbt-profiles.ts b/packages/opencode/src/altimate/native/connections/dbt-profiles.ts index e36b5bd0f6..e90c8edf9a 100644 --- a/packages/opencode/src/altimate/native/connections/dbt-profiles.ts +++ b/packages/opencode/src/altimate/native/connections/dbt-profiles.ts @@ -53,11 +53,11 @@ const KEY_MAP: Record = { } /** Resolve Jinja {{ env_var('NAME') }} and {{ env_var('NAME', 'default') }} patterns. */ -function resolveEnvVars(value: unknown): unknown { +export function resolveEnvVars(value: unknown): unknown { if (typeof value !== "string") return value return value.replace( - /\{\{\s*env_var\s*\(\s*'([^']+)'\s*(?:,\s*'([^']*)'\s*)?\)\s*\}\}/g, - (_match, envName: string, defaultValue?: string) => { + /\{\{\s*env_var\s*\(\s*(['"])((?:(?!\1)[\s\S])+)\1\s*(?:,\s*(['"])((?:(?!\3)[\s\S])*)\3\s*)?\)\s*\}\}/g, + (_match, _q1: string, envName: string, _q2?: string, defaultValue?: string) => { return process.env[envName] ?? defaultValue ?? "" }, ) @@ -102,7 +102,7 @@ function mapConfig(dbtType: string, dbtConfig: Record): Connect * 3. Project-local profiles.yml (in dbt project root) * 4. ~/.dbt/profiles.yml (default) */ -function resolveProfilesPath(explicitPath?: string, projectDir?: string): string { +export function resolveProfilesPath(explicitPath?: string, projectDir?: string): string { if (explicitPath) return explicitPath const envDir = process.env.DBT_PROFILES_DIR 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..6de998c0d8 --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-adapter-access.ts @@ -0,0 +1,103 @@ +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 + // A call cancelled before it starts is a cancellation, not a refusal. + signal.throwIfAborted() + 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. + */ +export async function sessionProject(): Promise<{ root?: string; other?: string } | undefined> { + const { findProjectRoot } = await import("../../../../dbt-tools/src/config") + const found = findProjectRoot(projectRoot()) + if (!found) return undefined + 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..1b113f1994 --- /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, error: "not implemented" }, + output: "dbt_cost_estimate is not implemented in this build of altimate-code yet.", + } + }, +}) diff --git a/packages/opencode/src/altimate/tools/dbt-cte-profile.ts b/packages/opencode/src/altimate/tools/dbt-cte-profile.ts new file mode 100644 index 0000000000..0e4913f06d --- /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, error: "not implemented" }, + output: "dbt_cte_profile is not implemented in this build of altimate-code yet.", + } + }, +}) diff --git a/packages/opencode/src/altimate/tools/dbt-defer-state.ts b/packages/opencode/src/altimate/tools/dbt-defer-state.ts new file mode 100644 index 0000000000..af9c4d2a84 --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-defer-state.ts @@ -0,0 +1,258 @@ +import z from "zod" +import fs from "fs" +import path from "path" +import { parse as parseJsonc, stripComments, type ParseError } from "jsonc-parser" +import { Tool } from "../../tool/tool" +import { resolveEnvVars, resolveProfilesPath } from "../native/connections/dbt-profiles" + +// The Power User setting this tool reads: `dbt.deferConfigPerProject`, keyed by the dbt project's path +// relative to its workspace folder. +const SETTING = "dbt.deferConfigPerProject" + +export interface DeferSetting { + deferToProduction?: boolean + favorState?: boolean + manifestPathForDeferral?: string + manifestPathType?: string + dbtCoreIntegrationId?: string | number +} + +export interface DeferStateResult { + enabled: boolean + favorState: boolean + /** "local" | "remote" | "none" (no entry for this project, or a manifest type this tool does not know) */ + mode: "local" | "remote" | "none" + /** Directory holding the manifest to defer to; set whenever the configured local one can be used, also while defer is off. */ + stateDir?: string + /** Why the configured defer cannot be used: no usable state directory, remote mode, an unknown manifest type. */ + reason?: string + /** The settings key that matched, relative to the workspace folder. */ + settingsKey?: string + settingsFile?: string + profile?: string + defaultTarget?: string + targets: string[] +} + +/** Expand `${env:NAME}` and `${workspaceFolder}`; an unset variable is left as written. */ +function expandVariables(value: string, workspaceFolder: string): string { + return value + .replace(/\$\{env:(.*?)\}/g, (match, name: string) => process.env[name] ?? match) + .replace(/\$\{workspaceFolder\}/g, () => workspaceFolder) +} + +const UNREADABLE = Symbol("unreadable settings") + +/** The parsed settings; undefined when there is no file, UNREADABLE when there is one that cannot be trusted. */ +function readSettings(file: string): Record | typeof UNREADABLE | undefined { + let text: string + try { + text = fs.readFileSync(file, "utf-8") + } catch (err) { + return (err as NodeJS.ErrnoException).code === "ENOENT" || (err as NodeJS.ErrnoException).code === "ENOTDIR" ? undefined : UNREADABLE + } + // An empty or whitespace-only file is a settings file with nothing in it. + if (text.trim() === "") return {} + const errors: ParseError[] = [] + // .vscode/settings.json allows comments and trailing commas. + const parsed = parseJsonc(text, errors, { allowTrailingComma: true }) + // A file that does not parse cannot be trusted to say defer is on. + // A file holding only comments has no value to parse, which VS Code reads as empty settings; a stray token is not that. + if (parsed === undefined && stripComments(text).trim() === "") return {} + if (errors.length > 0) return UNREADABLE + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return UNREADABLE + return parsed as Record +} + +/** + * Find the entry for the project. The workspace folder is the nearest ancestor of the project (or the + * project itself) whose `.vscode/settings.json` has an entry under the project's relative path. A settings + * file that cannot be read or parsed ends the search. + */ +export function findDeferSetting(projectDir: string, workspaceDir?: string) { + const real = (p: string) => { + try { + return fs.realpathSync(path.resolve(p)) + } catch { + return path.resolve(p) + } + } + const search = (start: string, workspace?: string) => { + const candidates: string[] = [] + if (workspace) candidates.push(workspace) + else { + for (let dir = start; ; dir = path.dirname(dir)) { + candidates.push(dir) + if (path.dirname(dir) === dir) break + } + } + for (const folder of candidates) { + const file = path.join(folder, ".vscode", "settings.json") + const settings = readSettings(file) + // The nearest settings file that cannot be read ends the search: an outer folder's entry must not stand in for it. + if (settings === UNREADABLE) return UNREADABLE + const perProject = settings?.[SETTING] + if (!perProject || typeof perProject !== "object") continue + const key = path.relative(folder, start) + const entry = (perProject as Record)[key] + if (entry && typeof entry === "object") { + return { entry: entry as DeferSetting, key, file, folder } + } + // A key naming the project through a symlink: compare the real paths. + const target = real(start) + for (const [k, v] of Object.entries(perProject as Record)) { + if (!v || typeof v !== "object") continue + if (real(path.resolve(folder, k)) === target) return { entry: v as DeferSetting, key: k, file, folder } + } + } + return undefined + } + // The paths as given first, as VS Code builds the key; then with symlinks followed, for a workspace reached through a link. + const lexical = [path.resolve(projectDir), workspaceDir ? path.resolve(workspaceDir) : undefined] as const + const resolved = [real(projectDir), workspaceDir ? real(workspaceDir) : undefined] as const + const passes = lexical[0] === resolved[0] && lexical[1] === resolved[1] ? [lexical] : [lexical, resolved] + for (const [i, [start, workspace]] of passes.entries()) { + const found = search(start, workspace) + // An unreadable file ends the search of its own pass; the pass over the real paths may still reach a readable one. + if (found === UNREADABLE) { + if (i === passes.length - 1) return undefined + continue + } + if (found) return found + } + return undefined +} + +function profilesFile(projectDir: string): string | undefined { + const file = resolveProfilesPath(undefined, projectDir) + return fs.existsSync(file) ? file : undefined +} + +async function parseYaml(text: string): Promise { + const mod = await import("yaml") + return (mod.default ?? mod).parse(text) +} + +/** The project's profile name, that profile's default `target` and its output names. */ +export async function readProfileTargets(projectDir: string) { + const out: { profile?: string; defaultTarget?: string; targets: string[] } = { targets: [] } + try { + const project = await parseYaml(fs.readFileSync(path.join(projectDir, "dbt_project.yml"), "utf-8")) + const profile = resolveEnvVars(project?.profile) + if (typeof profile === "string" && profile !== "") out.profile = profile + } catch { + return out + } + if (!out.profile) return out + const file = profilesFile(projectDir) + if (!file) return out + try { + const profiles = await parseYaml(fs.readFileSync(file, "utf-8")) + const profile = profiles?.[out.profile] + if (profile && typeof profile === "object") { + const target = resolveEnvVars(profile.target) + if (typeof target === "string" && target !== "") out.defaultTarget = target + if (profile.outputs && typeof profile.outputs === "object") out.targets = Object.keys(profile.outputs) + } + } catch { + // An unreadable profiles.yml leaves the target unknown. + } + return out +} + +function isFile(file: string): boolean { + try { + return fs.statSync(file).isFile() + } catch { + return false + } +} + +function isDirectory(dir: string): boolean { + try { + return fs.statSync(dir).isDirectory() + } catch { + return false + } +} + +export async function resolveDeferState(projectDir: string, workspaceDir?: string): Promise { + const root = path.resolve(projectDir) + const targets = await readProfileTargets(root) + const base: DeferStateResult = { enabled: false, favorState: false, mode: "none", ...targets } + const found = findDeferSetting(root, workspaceDir) + if (!found) return base + + const { entry } = found + const result: DeferStateResult = { + ...base, + settingsKey: found.key, + settingsFile: found.file, + favorState: entry.favorState === true, + } + const wanted = entry.deferToProduction === true + + // Power User writes "local" or "remote"; an entry without a type is local. Anything else is not understood. + const type = entry.manifestPathType + if (type !== undefined && type !== "" && type !== "local" && type !== "remote") { + return { ...result, reason: `Unknown manifestPathType "${String(type)}": expected "local" or "remote".` } + } + if (type === "remote") { + // The request that downloads the manifest is not implemented, so remote mode cannot supply a state dir. + return { ...result, mode: "remote", reason: "Remote defer mode is not supported yet: download the manifest and use a local path." } + } + result.mode = "local" + + // The directory is resolved whether or not defer is on, so a session can still switch it on. + const raw = entry.manifestPathForDeferral + if (typeof raw !== "string" || raw.trim() === "") { + return { ...result, reason: "manifestPathForDeferral is not set." } + } + // Power User expands ${workspaceFolder} to the dbt project's own folder. + const expanded = expandVariables(raw, root) + const stateDir = path.isAbsolute(expanded) ? expanded : path.resolve(root, expanded) + if (!isDirectory(stateDir)) { + return { ...result, reason: `manifestPathForDeferral is not a directory: ${stateDir}` } + } + if (!isFile(path.join(stateDir, "manifest.json"))) { + return { ...result, reason: `No manifest.json in manifestPathForDeferral: ${stateDir}` } + } + return { ...result, enabled: wanted, stateDir } +} + +export const DbtDeferStateTool = Tool.define("dbt_defer_state", { + description: + "Read the dbt Power User defer setting for a dbt project (`dbt.deferConfigPerProject` in .vscode/settings.json) and the project's profile target. Returns whether defer is on, whether to favor state, the state directory to pass as `dbt --defer --state ` (use it only when `enabled` is true), and the profile's default target and target names. Read-only.", + parameters: z.object({ + project_dir: z.string().describe("The dbt project directory (the folder holding dbt_project.yml)"), + workspace_dir: z + .string() + .optional() + .describe("The workspace folder that holds .vscode/settings.json (default: the nearest ancestor of project_dir whose settings have an entry for the project)"), + }), + async execute(args) { + try { + const result = await resolveDeferState(args.project_dir, args.workspace_dir) + const lines = [ + `defer: ${result.enabled ? "on" : "off"}${result.favorState ? " (favor state)" : ""}`, + `mode: ${result.mode}`, + ] + if (result.stateDir) lines.push(`state dir${result.enabled ? "" : " (not used: defer is off)"}: ${result.stateDir}`) + if (result.reason) lines.push(`note: ${result.reason}`) + if (result.defaultTarget) lines.push(`default target: ${result.defaultTarget}`) + if (result.targets.length) lines.push(`targets: ${result.targets.join(", ")}`) + return { + title: `dbt Defer: ${result.enabled ? "on" : "off"}`, + metadata: { success: true, error: undefined as string | undefined, ...result }, + output: lines.join("\n"), + } + } catch (e) { + const msg = e instanceof Error ? e.message : String(e) + return { + title: "dbt Defer: ERROR", + metadata: { success: false, error: msg as string | undefined, enabled: false }, + output: `Failed: ${msg}`, + } + } + }, +}) 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..8990306b72 --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-docs-coverage.ts @@ -0,0 +1,21 @@ +import z from "zod" +import { Tool } from "../../tool/tool" + +export const DbtDocsCoverageTool = Tool.define("dbt_docs_coverage", { + description: + "Documentation coverage of a dbt model: which output columns have a description, and whether the model has one.", + parameters: z.object({ + model: z.string().describe("Model name"), + manifest_path: z + .string() + .optional() + .describe("manifest.json to read (default: the dbt project's target/manifest.json)"), + }), + async execute() { + return { + title: "dbt_docs_coverage: not implemented", + metadata: { success: false, error: "not implemented" }, + output: "dbt_docs_coverage is not implemented in this build of altimate-code yet.", + } + }, +}) diff --git a/packages/opencode/src/altimate/tools/dbt-health.ts b/packages/opencode/src/altimate/tools/dbt-health.ts new file mode 100644 index 0000000000..096e2bc3f4 --- /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, error: "not implemented" }, + output: "dbt_health is not implemented in this build of altimate-code yet.", + } + }, +}) diff --git a/packages/opencode/src/altimate/tools/dbt-lineage.ts b/packages/opencode/src/altimate/tools/dbt-lineage.ts index a83efb84ae..2403dfab89 100644 --- a/packages/opencode/src/altimate/tools/dbt-lineage.ts +++ b/packages/opencode/src/altimate/tools/dbt-lineage.ts @@ -7,6 +7,7 @@ import type { DbtLineageResult } from "../native/types" import { loadRawManifest } from "../native/dbt/helpers" import { lineageGraph } from "../native/dbt/graph" import { projectRoot } from "../native/connections/registry" +import { sessionProject } from "./dbt-adapter-access" /** The structured result of dbt-integration's `getModelLineage` (version 1), relayed as is. */ export type ModelLineage = { @@ -359,32 +360,6 @@ async function defaultManifestPath(session: () => ReturnType { - 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 } -} - const MAX_TEXT_EDGES = 200 /** A compact reading of the structured result, for clients that show the text. */ 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..f949274620 --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-map-relations.ts @@ -0,0 +1,21 @@ +import z from "zod" +import { Tool } from "../../tool/tool" + +export const DbtMapRelationsTool = Tool.define("dbt_map_relations", { + description: "Map each table a raw SQL query reads to the dbt ref() or source() for it, from the manifest.", + parameters: z.object({ + sql: z.string().describe("Raw SQL to map"), + dialect: z.string().optional().describe("SQL dialect of the query"), + manifest_path: z + .string() + .optional() + .describe("manifest.json to match against (default: the dbt project's target/manifest.json)"), + }), + async execute() { + return { + title: "dbt_map_relations: not implemented", + metadata: { success: false, error: "not implemented" }, + output: "dbt_map_relations is not implemented in this build of altimate-code yet.", + } + }, +}) diff --git a/packages/opencode/src/altimate/tools/dbt-run-results.ts b/packages/opencode/src/altimate/tools/dbt-run-results.ts new file mode 100644 index 0000000000..c6f2cf396c --- /dev/null +++ b/packages/opencode/src/altimate/tools/dbt-run-results.ts @@ -0,0 +1,19 @@ +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, error: "not implemented" }, + 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..0c4db3c980 --- /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, error: "not implemented" }, + output: "dbt_sql_graph is not implemented in this build of altimate-code yet.", + } + }, +}) diff --git a/packages/opencode/src/altimate/tools/docs-share-comments.ts b/packages/opencode/src/altimate/tools/docs-share-comments.ts new file mode 100644 index 0000000000..8963268cdd --- /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, error: "not implemented" }, + 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..48b744352b --- /dev/null +++ b/packages/opencode/test/altimate/dbt-adapter-access.test.ts @@ -0,0 +1,279 @@ +// The tools the dbt Power User features call: their allow-list entries and placeholder answers, the +// one gate every dbt-adapter-backed tool goes through, and the API helper for new endpoint files. The +// dbt adapter is replaced through `dbtAdapterInternals`, so these tests need no Python or dbt. +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from "bun:test" +import { mkdtemp, rm, writeFile } from "node:fs/promises" +import { realpathSync } from "node:fs" +import os from "node:os" +import path from "node:path" +import { Effect } from "effect" +import type { Tool } from "@/tool/tool" +import { SessionID, MessageID } from "@/session/schema" +import { dbtAdapterInternals, resetDbtAdapter } from "@/altimate/native/connections/register" +import { dbtAdapterAccessInternals, dbtAdapterFor } from "@/altimate/tools/dbt-adapter-access" +import { EXPOSED, MCP_OPEN_WRITE_TOOLS, WRITE_TOOLS } from "@/altimate/tool-cli/allow-list" +import { AltimateApi } from "@/altimate/api/client" +import { Instance } from "@/project/instance" +import { disposeInstance } from "@/effect/instance-registry" +import { initTool } from "./tool-fixture" + +/** Each tool with the smallest arguments its parameters accept. */ +const MINIMAL_ARGS: Record> = { + dbt_run_results: {}, + dbt_defer_state: { project_dir: "." }, + dbt_map_relations: { sql: "select 1" }, + docs_share_comments: { action: "list" }, + dbt_cost_estimate: {}, + dbt_health: {}, + dbt_sql_graph: { model: "customers" }, + dbt_docs_coverage: { model: "customers" }, + dbt_cte_profile: { model: "customers" }, +} +const FEATURE_TOOLS = Object.keys(MINIMAL_ARGS) +// Tools that have their own implementation and test: they no longer answer as placeholders. +const IMPLEMENTED = new Set(["dbt_defer_state"]) + +const ctx: Tool.Context = { + sessionID: SessionID.make("ses_dbt_adapter_access"), + messageID: MessageID.make("msg_dbt_adapter_access"), + agent: "analyst", + abort: new AbortController().signal, + messages: [], + metadata: () => Effect.void, + ask: () => Effect.void, +} + +let project: string + +function useConfig() { + dbtAdapterInternals.readConfig = async () => ({ + projectRoot: project, + pythonPath: "python3", + dbtIntegration: "corecommand", + queryLimit: 500, + }) +} + +beforeAll(async () => { + project = await mkdtemp(path.join(os.tmpdir(), "dbt-adapter-access-")) + await writeFile(path.join(project, "dbt_project.yml"), "name: jaffle_shop\n") +}) + +afterAll(async () => { + await rm(project, { recursive: true, force: true }) +}) + +beforeEach(() => resetDbtAdapter()) + +afterEach(() => { + resetDbtAdapter() + delete dbtAdapterInternals.readConfig + delete dbtAdapterInternals.create +}) + +describe("feature tools in the allow-list", () => { + test("all nine are exposed, in the map's alphabetical order, and none is a write tool", () => { + const ids = Object.keys(EXPOSED) + for (const id of FEATURE_TOOLS) { + expect(ids).toContain(id) + expect(WRITE_TOOLS.has(id)).toBe(false) + expect(MCP_OPEN_WRITE_TOOLS.has(id)).toBe(false) + } + const listed = ids.filter((id) => id.startsWith("dbt_") || id.startsWith("docs_")) + expect(listed).toEqual([...listed].sort()) + }) + + test("each placeholder answers that it isn't implemented, as a failure", async () => { + for (const id of FEATURE_TOOLS.filter((id) => !IMPLEMENTED.has(id))) { + const tool = await initTool(EXPOSED[id] as any) + expect(tool.id).toBe(id) + const result = await tool.execute(MINIMAL_ARGS[id], ctx) + expect(result.title).toBe(`${id}: not implemented`) + expect(result.metadata).toEqual({ success: false, error: "not implemented", truncated: false }) + expect(result.output).toContain(id) + } + }) +}) + +describe("dbtAdapterFor abort", () => { + test("a read-only call that is already cancelled is cancelled, not refused", async () => { + const ac = new AbortController() + ac.abort() + await expect(dbtAdapterFor({ abort: ac.signal, agent: "plan", extra: { forceReadOnly: true } } as any)).rejects.toBeDefined() + }) +}) + +describe("dbtAdapterFor", () => { + let created: number + beforeEach(() => { + created = 0 + useConfig() + dbtAdapterInternals.create = async () => { + created += 1 + return { isInitialized: () => true, dispose: async () => {} } + } + }) + + test("refuses a read-only call and never creates the adapter", async () => { + const access = await dbtAdapterFor({ ...ctx, extra: { forceReadOnly: true } }) + expect(access.adapter).toBeUndefined() + expect(access.refusal).toBe("read_only") + expect(access.reason).toContain("--allow-write") + expect(created).toBe(0) + }) + + test("refuses a plan-mode call", async () => { + const access = await dbtAdapterFor({ ...ctx, agent: "plan" }) + expect(access.refusal).toBe("read_only") + expect(created).toBe(0) + }) + + test("returns the adapter where writes are allowed", async () => { + const access = await dbtAdapterFor(ctx) + expect(access.refusal).toBeUndefined() + expect(access.adapter?.isInitialized()).toBe(true) + expect(created).toBe(1) + }) + + test("says when the adapter didn't load the project", async () => { + dbtAdapterInternals.create = async () => ({ isInitialized: () => false, dispose: async () => {} }) + const access = await dbtAdapterFor(ctx) + expect(access).toEqual({ refusal: "not_loaded", reason: "the dbt adapter didn't load the project" }) + }) + + test("says there is no dbt project when the config finds none", async () => { + dbtAdapterInternals.readConfig = async () => null + const access = await dbtAdapterFor(ctx) + expect(access).toEqual({ refusal: "no_project", reason: "no dbt project here" }) + }) + + test("says when the adapter doesn't start in time", async () => { + dbtAdapterInternals.create = () => new Promise(() => {}) + const saved = dbtAdapterAccessInternals.adapterStartTimeoutMs + dbtAdapterAccessInternals.adapterStartTimeoutMs = 20 + try { + const access = await dbtAdapterFor(ctx) + expect(access).toEqual({ refusal: "not_started", reason: "the dbt adapter didn't start within 0.02 s" }) + } finally { + dbtAdapterAccessInternals.adapterStartTimeoutMs = saved + } + }) + + test("says the adapter didn't start when the project exists but creating it fails", async () => { + dbtAdapterInternals.create = async () => { + created += 1 + throw new Error("dbt-integration missing") + } + const access = await dbtAdapterFor(ctx) + expect(access).toEqual({ refusal: "not_started", reason: "the dbt adapter didn't start" }) + expect(created).toBe(1) + }) + + test("a call cancelled while the adapter starts throws and stops waiting", async () => { + let entered = () => {} + const starting = new Promise((resolve) => (entered = resolve)) + dbtAdapterInternals.create = () => { + entered() + return new Promise(() => {}) + } + const controller = new AbortController() + const pending = dbtAdapterFor({ ...ctx, abort: controller.signal }) + await starting + controller.abort(new Error("cancelled")) + await expect(pending).rejects.toThrow("cancelled") + }) + + test("a cancelled call throws rather than answering", async () => { + const controller = new AbortController() + controller.abort() + await expect(dbtAdapterFor({ ...ctx, abort: controller.signal })).rejects.toThrow() + expect(created).toBe(0) + }) + + test("a session in a dbt project the adapter isn't set up for says so", async () => { + dbtAdapterInternals.readConfig = async () => null + try { + const access = await Instance.provide({ directory: project, fn: () => dbtAdapterFor(ctx) }) + expect(access).toEqual({ + refusal: "other_project", + reason: `the dbt adapter isn't set up for ${realpathSync(project)}`, + }) + expect(created).toBe(0) + } finally { + await disposeInstance(project) + } + }) + + test("a session in another dbt project than the adapter's says which", async () => { + const other = await mkdtemp(path.join(os.tmpdir(), "dbt-adapter-other-")) + await writeFile(path.join(other, "dbt_project.yml"), "name: other\n") + dbtAdapterInternals.readConfig = async () => ({ + projectRoot: other, + pythonPath: "python3", + dbtIntegration: "corecommand", + queryLimit: 500, + }) + try { + const access = await Instance.provide({ directory: project, fn: () => dbtAdapterFor(ctx) }) + expect(access).toEqual({ + refusal: "other_project", + reason: `the dbt adapter is set up for ${realpathSync(other)}, not ${realpathSync(project)}`, + }) + expect(created).toBe(0) + } finally { + await disposeInstance(project) + await rm(other, { recursive: true, force: true }) + } + }) +}) + +describe("AltimateApi.apiRequest", () => { + let home: string + let saved: string | undefined + beforeEach(async () => { + home = await mkdtemp(path.join(os.tmpdir(), "altimate-api-home-")) + saved = process.env.OPENCODE_TEST_HOME + process.env.OPENCODE_TEST_HOME = home + }) + afterEach(async () => { + if (saved === undefined) delete process.env.OPENCODE_TEST_HOME + else process.env.OPENCODE_TEST_HOME = saved + await rm(home, { recursive: true, force: true }) + }) + + test("fails with a clear message when no credentials are stored", async () => { + await expect(AltimateApi.apiRequest("GET", "/dbt/v1/x")).rejects.toThrow( + `No Altimate credentials are stored: sign in to Altimate in altimate-code first (they are saved to ${path.join(home, ".altimate", "altimate.json")}).`, + ) + }) + + test("sends the stored key and tenant and returns the JSON body", async () => { + await Bun.write( + path.join(home, ".altimate", "altimate.json"), + JSON.stringify({ altimateUrl: "https://api.example.test/", altimateInstanceName: "acme", altimateApiKey: "k1" }), + ) + const original = globalThis.fetch + const seen: { url: string; init: RequestInit }[] = [] + globalThis.fetch = (async (url: string, init: RequestInit) => { + seen.push({ url, init }) + return new Response(JSON.stringify({ ok: 1 }), { status: 200 }) + }) as unknown as typeof fetch + try { + expect(await AltimateApi.apiRequest("POST", "/dbt/v1/x", { a: 1 })).toEqual({ ok: 1 }) + expect(seen[0].url).toBe("https://api.example.test/dbt/v1/x") + const headers = seen[0].init.headers as Record + expect(headers.Authorization).toBe("Bearer k1") + expect(headers["x-tenant"]).toBe("acme") + expect(seen[0].init.body).toBe(JSON.stringify({ a: 1 })) + } finally { + globalThis.fetch = original + } + }) + + test("names the file when the stored credentials can't be read", async () => { + await Bun.write(path.join(home, ".altimate", "altimate.json"), "{not json") + await expect(AltimateApi.apiRequest("GET", "/dbt/v1/x")).rejects.toThrow( + `The Altimate credentials in ${path.join(home, ".altimate", "altimate.json")} can't be used`, + ) + }) +}) diff --git a/packages/opencode/test/altimate/dbt-defer-state.test.ts b/packages/opencode/test/altimate/dbt-defer-state.test.ts new file mode 100644 index 0000000000..6075277a39 --- /dev/null +++ b/packages/opencode/test/altimate/dbt-defer-state.test.ts @@ -0,0 +1,308 @@ +import { describe, test, expect, beforeEach, afterEach } from "bun:test" +import fs from "fs" +import os from "os" +import path from "path" +import { Effect } from "effect" +import type { Tool } from "../../src/tool/tool" +import { SessionID, MessageID } from "../../src/session/schema" +import { resolveEnvVars } from "../../src/altimate/native/connections/dbt-profiles" +import { DbtDeferStateTool, resolveDeferState, findDeferSetting } from "../../src/altimate/tools/dbt-defer-state" +import { initTool } from "./tool-fixture" + +describe("dbt_defer_state", () => { + let root: string + let savedProfilesDir: string | undefined + + const write = (rel: string, content: string) => { + const file = path.join(root, rel) + fs.mkdirSync(path.dirname(file), { recursive: true }) + fs.writeFileSync(file, content) + } + const settings = (perProject: Record) => + write(".vscode/settings.json", `// comment\n{\n "dbt.deferConfigPerProject": ${JSON.stringify(perProject)},\n}\n`) + + beforeEach(() => { + root = fs.mkdtempSync(path.join(os.tmpdir(), "defer-state-")) + savedProfilesDir = process.env.DBT_PROFILES_DIR + process.env.DBT_PROFILES_DIR = path.join(root, "profiles") + write("profiles/profiles.yml", "shop:\n target: dev\n outputs:\n dev:\n type: duckdb\n prod:\n type: duckdb\n") + write("analytics/dbt_project.yml", "name: shop\nprofile: shop\n") + write("analytics/prod-state/manifest.json", "{}") + }) + + afterEach(() => { + if (savedProfilesDir === undefined) delete process.env.DBT_PROFILES_DIR + else process.env.DBT_PROFILES_DIR = savedProfilesDir + fs.rmSync(root, { recursive: true, force: true }) + }) + + test("local mode returns the state dir, favor state and the profile's targets", async () => { + settings({ analytics: { deferToProduction: true, favorState: true, manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ + enabled: true, + favorState: true, + mode: "local", + stateDir: path.join(root, "analytics", "prod-state"), + settingsKey: "analytics", + defaultTarget: "dev", + targets: ["dev", "prod"], + profile: "shop", + }) + }) + + test("defer off still returns the configured state dir, so a session can switch it on", async () => { + settings({ analytics: { deferToProduction: false, manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.enabled).toBe(false) + expect(result.mode).toBe("local") + expect(result.stateDir).toBe(path.join(root, "analytics", "prod-state")) + expect(result.defaultTarget).toBe("dev") + }) + + test("a project without an entry reports mode none", async () => { + settings({ other: { deferToProduction: true, manifestPathForDeferral: "x" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ enabled: false, mode: "none" }) + }) + + test("a project at the workspace root uses the empty key", async () => { + write("dbt_project.yml", "name: shop\nprofile: shop\n") + write("prod-state/manifest.json", "{}") + settings({ "": { deferToProduction: true, manifestPathForDeferral: "prod-state" } }) + const result = await resolveDeferState(root) + expect(result.enabled).toBe(true) + expect(result.stateDir).toBe(path.join(root, "prod-state")) + }) + + test("a state path that is not a directory is reported, not used", async () => { + write("analytics/prod-state-file", "x") + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/prod-state-file" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.enabled).toBe(false) + expect(result.stateDir).toBeUndefined() + expect(result.reason).toContain("not a directory") + }) + + test("a state dir without manifest.json is reported, not used", async () => { + fs.mkdirSync(path.join(root, "analytics", "empty-state")) + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/empty-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.enabled).toBe(false) + expect(result.stateDir).toBeUndefined() + expect(result.reason).toContain("manifest.json") + }) + + test("a manifest.json that is a directory is not a usable state", async () => { + fs.mkdirSync(path.join(root, "analytics", "dir-state", "manifest.json"), { recursive: true }) + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/dir-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.enabled).toBe(false) + expect(result.stateDir).toBeUndefined() + expect(result.reason).toContain("manifest.json") + }) + + test("${workspaceFolder} is the dbt project's own folder, as in Power User", async () => { + fs.mkdirSync(path.join(root, "prod-state"), { recursive: true }) + fs.writeFileSync(path.join(root, "prod-state", "manifest.json"), "{}") + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.stateDir).toBe(path.join(root, "analytics", "prod-state")) + }) + + test("a settings file that does not parse ends the search; an outer folder's entry does not stand in", async () => { + write("analytics/.vscode/settings.json", '{ "dbt.deferConfigPerProject": oops') + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ enabled: false, mode: "none" }) + }) + + test("an unknown manifest type does not enable defer", async () => { + settings({ analytics: { deferToProduction: true, manifestPathType: "typo", manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ enabled: false, mode: "none" }) + expect(result.stateDir).toBeUndefined() + expect(result.reason).toContain("typo") + }) + + test("a settings file that does not parse is not trusted", async () => { + write(".vscode/settings.json", '{ "dbt.deferConfigPerProject": { "analytics": { "deferToProduction": true, "manifestPathForDeferral": "${workspaceFolder}/prod-state" } } } oops') + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ enabled: false, mode: "none" }) + }) + + test("${env:NAME} in the state path is expanded, and an unset one is left as written", async () => { + const saved = { set: process.env.DEFER_TEST_STATE, unset: process.env.DEFER_TEST_UNSET } + process.env.DEFER_TEST_STATE = path.join(root, "analytics", "prod-state") + delete process.env.DEFER_TEST_UNSET + try { + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${env:DEFER_TEST_STATE}" } }) + expect((await resolveDeferState(path.join(root, "analytics"))).stateDir).toBe(path.join(root, "analytics", "prod-state")) + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${env:DEFER_TEST_UNSET}" } }) + expect((await resolveDeferState(path.join(root, "analytics"))).reason).toContain("${env:DEFER_TEST_UNSET}") + } finally { + if (saved.set === undefined) delete process.env.DEFER_TEST_STATE + else process.env.DEFER_TEST_STATE = saved.set + if (saved.unset === undefined) delete process.env.DEFER_TEST_UNSET + else process.env.DEFER_TEST_UNSET = saved.unset + } + }) + + test("an explicit workspace folder and an absolute state path are used", async () => { + write("elsewhere/.vscode/settings.json", JSON.stringify({ "dbt.deferConfigPerProject": { "../analytics": { deferToProduction: true, manifestPathForDeferral: path.join(root, "analytics", "prod-state") } } })) + const result = await resolveDeferState(path.join(root, "analytics"), path.join(root, "elsewhere")) + expect(result).toMatchObject({ enabled: true, stateDir: path.join(root, "analytics", "prod-state"), settingsKey: "../analytics" }) + }) + + test("a project or target written as env_var() is resolved, with its default", async () => { + write("analytics/dbt_project.yml", "name: shop\nprofile: \"{{ env_var('DEFER_TEST_PROFILE', 'shop') }}\"\n") + write("profiles/profiles.yml", "shop:\n target: \"{{ env_var('DEFER_TEST_TARGET', 'dev') }}\"\n outputs:\n dev:\n type: duckdb\n qa:\n type: duckdb\n") + process.env.DEFER_TEST_TARGET = "qa" + try { + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ profile: "shop", defaultTarget: "qa", targets: ["dev", "qa"] }) + } finally { + delete process.env.DEFER_TEST_TARGET + } + expect((await resolveDeferState(path.join(root, "analytics"))).defaultTarget).toBe("dev") + }) + + test("double-quoted env_var() names and defaults are resolved too", async () => { + write("analytics/dbt_project.yml", "name: shop\nprofile: '{{ env_var(\"DEFER_TEST_PROFILE\", \"shop\") }}'\n") + write("profiles/profiles.yml", "shop:\n target: '{{ env_var(\"DEFER_TEST_TARGET2\", \"dev\") }}'\n outputs:\n dev:\n type: duckdb\n") + delete process.env.DEFER_TEST_PROFILE + delete process.env.DEFER_TEST_TARGET2 + expect(await resolveDeferState(path.join(root, "analytics"))).toMatchObject({ profile: "shop", defaultTarget: "dev" }) + }) + + test("a settings file holding only a comment does not hide the workspace entry", () => { + settings({ analytics: { deferToProduction: true } }) + fs.mkdirSync(path.join(root, "analytics", ".vscode"), { recursive: true }) + fs.writeFileSync(path.join(root, "analytics", ".vscode", "settings.json"), "// nothing yet\n") + expect(findDeferSetting(path.join(root, "analytics"))?.key).toBe("analytics") + }) + + test("a settings file holding a stray token is still unreadable", async () => { + write(".vscode/settings.json", "}") + expect(findDeferSetting(path.join(root, "analytics"))).toBeUndefined() + write(".vscode/settings.json", JSON.stringify({ "dbt.deferConfigPerProject": { analytics: { deferToProduction: true } } })) + fs.mkdirSync(path.join(root, "analytics", ".vscode"), { recursive: true }) + fs.writeFileSync(path.join(root, "analytics", ".vscode", "settings.json"), "}") + expect(findDeferSetting(path.join(root, "analytics"))).toBeUndefined() + }) + + test("an env_var() default may hold the other quote character", async () => { + write("analytics/dbt_project.yml", "name: shop\nprofile: shop\n") + write("profiles/profiles.yml", "shop:\n target: \"{{ env_var('DEFER_TEST_Q', 'say \\\"hi\\\"') }}\"\n outputs:\n dev:\n type: duckdb\n") + delete process.env.DEFER_TEST_Q + expect((await resolveDeferState(path.join(root, "analytics"))).defaultTarget).toBe('say "hi"') + }) + + test("an env_var() default may span lines", () => { + delete process.env.DEFER_TEST_KEY + expect(resolveEnvVars("{{ env_var('DEFER_TEST_KEY', 'line1\nline2') }}")).toBe("line1\nline2") + }) + + test("a settings key naming the project through a symlink matches the real project path", () => { + const outside = fs.mkdtempSync(path.join(path.dirname(root), "ext2-")) + fs.symlinkSync(outside, path.join(root, "linked")) + try { + settings({ linked: { deferToProduction: true } }) + expect(findDeferSetting(outside, root)?.key).toBe("linked") + } finally { + fs.rmSync(path.join(root, "linked")) + fs.rmSync(outside, { recursive: true, force: true }) + } + }) + + test("remote mode is reported as unsupported with no state dir", async () => { + settings({ analytics: { deferToProduction: true, manifestPathType: "remote", dbtCoreIntegrationId: 7 } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ enabled: false, mode: "remote" }) + expect(result.stateDir).toBeUndefined() + expect(result.reason).toContain("Remote defer mode") + }) + + test("missing settings file and unreadable profiles give defaults", async () => { + fs.rmSync(path.join(root, ".vscode"), { recursive: true, force: true }) + // An invalid file in place (not a removed directory) keeps the lookup off the developer's ~/.dbt/profiles.yml. + write("profiles/profiles.yml", "shop: [unclosed\n") + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ enabled: false, mode: "none", targets: [] }) + expect(result.defaultTarget).toBeUndefined() + }) + + test("an empty settings file in the project does not hide the workspace entry", () => { + settings({ analytics: { deferToProduction: true } }) + fs.mkdirSync(path.join(root, "analytics", ".vscode"), { recursive: true }) + fs.writeFileSync(path.join(root, "analytics", ".vscode", "settings.json"), "") + expect(findDeferSetting(path.join(root, "analytics"))?.key).toBe("analytics") + }) + + test("a symlinked workspace path still finds the project's entry", () => { + settings({ analytics: { deferToProduction: true } }) + const link = path.join(root, "..", path.basename(root) + "-link") + fs.symlinkSync(root, link) + try { + expect(findDeferSetting(path.join(root, "analytics"), link)?.key).toBe("analytics") + } finally { + fs.rmSync(link) + } + }) + + test("a symlinked project folder still finds the entry under its path in the workspace", () => { + settings({ analytics: { deferToProduction: true } }) + const outside = fs.mkdtempSync(path.join(path.dirname(root), "ext-")) + fs.rmSync(path.join(root, "analytics"), { recursive: true, force: true }) + fs.symlinkSync(outside, path.join(root, "analytics")) + try { + expect(findDeferSetting(path.join(root, "analytics"), root)?.key).toBe("analytics") + expect(findDeferSetting(path.join(root, "analytics"))?.key).toBe("analytics") + } finally { + fs.rmSync(path.join(root, "analytics")) + fs.rmSync(outside, { recursive: true, force: true }) + } + }) + + test("an unparseable settings file above a symlinked project does not hide the entry on the real path", () => { + const real = fs.mkdtempSync(path.join(path.dirname(root), "real-")) + fs.mkdirSync(path.join(real, "analytics")) + fs.mkdirSync(path.join(real, ".vscode")) + fs.writeFileSync(path.join(real, ".vscode", "settings.json"), JSON.stringify({ "dbt.deferConfigPerProject": { analytics: { deferToProduction: true } } })) + const holder = fs.mkdtempSync(path.join(path.dirname(root), "hold-")) + fs.mkdirSync(path.join(holder, ".vscode")) + fs.writeFileSync(path.join(holder, ".vscode", "settings.json"), "{ not json") + const link = path.join(holder, "analytics") + fs.symlinkSync(path.join(real, "analytics"), link) + try { + expect(findDeferSetting(link)?.entry.deferToProduction).toBe(true) + } finally { + fs.rmSync(real, { recursive: true, force: true }) + fs.rmSync(holder, { recursive: true, force: true }) + } + }) + + test("settings with comments and trailing commas parse", () => { + settings({ analytics: { deferToProduction: true } }) + expect(findDeferSetting(path.join(root, "analytics"))?.key).toBe("analytics") + }) + + test("the tool answers with the result as metadata and a readable report", async () => { + settings({ analytics: { deferToProduction: true, favorState: true, manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const ctx: Tool.Context = { + sessionID: SessionID.make("ses_defer_state"), + messageID: MessageID.make("msg_defer_state"), + agent: "analyst", + abort: new AbortController().signal, + messages: [], + metadata: () => Effect.void, + ask: () => Effect.void, + } + const tool = await initTool(DbtDeferStateTool as any) + const ok = await tool.execute({ project_dir: path.join(root, "analytics") }, ctx) + expect(ok.title).toBe("dbt Defer: on") + expect(ok.metadata).toMatchObject({ success: true, enabled: true, favorState: true, stateDir: path.join(root, "analytics", "prod-state"), defaultTarget: "dev", targets: ["dev", "prod"] }) + expect(ok.output).toContain(`state dir: ${path.join(root, "analytics", "prod-state")}`) + expect(ok.output).toContain("defer: on (favor state)") + }) +})