Repository navigation
fix: dbt tools keep the project's manifest, time CTEs without dbt start-up, name graph clauses and bad arguments, key the daemon on the Python env #1781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f84d2a7
4266798
8eed76a
d46f446
8184687
697a43a
a14c731
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { describe, test, expect } from "bun:test" | ||
| import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs" | ||
| import { tmpdir } from "os" | ||
| import { join } from "path" | ||
| import { create } from "../src/adapter" | ||
|
|
||
| /** A stand-in `python` that records the DBT_TARGET_PATH each run sees and fails the bridge run. */ | ||
| function recordingPython(root: string) { | ||
| const log = join(root, "target-path.log") | ||
| const python = join(root, "python") | ||
| writeFileSync( | ||
| python, | ||
| [ | ||
| "#!/bin/sh", | ||
| `printf '%s\\n' "\${DBT_TARGET_PATH-unset}" >> '${log}'`, | ||
| 'case "$1" in', | ||
| ' --version) echo "Python 3.11.9"; exit 0 ;;', | ||
| ' -c) echo "1.10.0"; exit 0 ;;', | ||
| "esac", | ||
| "exit 3", | ||
| ].join("\n") + "\n", | ||
| ) | ||
| chmodSync(python, 0o755) | ||
| return { python, seen: () => (existsSync(log) ? readFileSync(log, "utf8").trim().split("\n") : []) } | ||
| } | ||
|
|
||
| // The adapter's dbt commands rewrite manifest.json in the target folder they are given; a caller that | ||
| // compiles for its own reading gives one of its own, so the project's target/manifest.json is left alone. | ||
| describe.skipIf(process.platform === "win32")("create's target folder", () => { | ||
| const start = async (targetPath?: string) => { | ||
| const root = mkdtempSync(join(tmpdir(), "dbt-tools-target-")) | ||
| const project = join(root, "project") | ||
| mkdirSync(project) | ||
| writeFileSync(join(project, "dbt_project.yml"), "name: p\nversion: '1.0'\nprofile: p\n") | ||
| const { python, seen } = recordingPython(root) | ||
| const saved = process.env.DBT_TARGET_PATH | ||
| delete process.env.DBT_TARGET_PATH | ||
| try { | ||
| await create( | ||
| { projectRoot: project, pythonPath: python, dbtIntegration: "corecommand", queryLimit: 500 }, | ||
| targetPath ? { targetPath } : undefined, | ||
| ).then( | ||
| (adapter) => adapter.dispose(), | ||
| () => {}, | ||
| ) | ||
| // The host's own environment is not changed: other readers of DBT_TARGET_PATH still see the project's. | ||
| expect(process.env.DBT_TARGET_PATH).toBeUndefined() | ||
| return seen() | ||
| } finally { | ||
| if (saved === undefined) delete process.env.DBT_TARGET_PATH | ||
| else process.env.DBT_TARGET_PATH = saved | ||
| rmSync(root, { recursive: true, force: true }) | ||
| } | ||
| } | ||
|
|
||
| test("every dbt process the adapter starts writes to the folder it is given", async () => { | ||
| const runs = await start("/tmp/project/target/.altimate-code") | ||
| expect(runs.length).toBeGreaterThan(0) | ||
| expect(new Set(runs)).toEqual(new Set(["/tmp/project/target/.altimate-code"])) | ||
| }) | ||
|
|
||
| test("without one, dbt writes where the project says", async () => { | ||
| const runs = await start() | ||
| expect(runs.length).toBeGreaterThan(0) | ||
| expect(new Set(runs)).toEqual(new Set(["unset"])) | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -44,8 +44,10 @@ export function resolveDirectory(directory: string): string { | |||||
| const CONN_ENV_PREFIX = "ALTIMATE_CODE_CONN_" | ||||||
|
|
||||||
| // Variables a daemon reads once at spawn that change what it returns or exposes (value sampling, integration | ||||||
| // routing, workspace tools), so a caller with a different value must not be served by a daemon started without it. | ||||||
| const BEHAVIOUR_ENV = ["ALTIMATE_DISABLE_SCHEMA_VALUES", "ALTIMATE_INTEGRATIONS", "ALTIMATE_DISABLE_WORKSPACE"] | ||||||
| // routing, workspace tools, and the Python environment its dbt adapter runs dbt with), so a caller with a | ||||||
| // different value must not be served by a daemon started without it: a daemon started outside the project's | ||||||
| // virtualenv would otherwise go on answering that dbt didn't load after the caller activated it. | ||||||
| const BEHAVIOUR_ENV = ["ALTIMATE_DISABLE_SCHEMA_VALUES", "ALTIMATE_INTEGRATIONS", "ALTIMATE_DISABLE_WORKSPACE", "VIRTUAL_ENV", "CONDA_PREFIX"] | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: This identity still misses PATH-based Python selection: Prompt for AI agents
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not changing this one. |
||||||
|
|
||||||
| /** A short digest of the caller's `ALTIMATE_CODE_CONN_*` variables and the behaviour variables above, empty when | ||||||
| * none are set. A daemon reads them once at spawn, so callers with different values must not share one. */ | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -176,6 +176,35 @@ export function zodToJsonSchema(schema: z.ZodType): JSONSchema7 { | |||||
| : rest) as JSONSchema7 | ||||||
| } | ||||||
|
|
||||||
| // altimate_change start — argument errors that name the field | ||||||
| /** The value at `path` in `input`, undefined when any step is missing. */ | ||||||
| function valueAt(input: unknown, path: readonly PropertyKey[]): unknown { | ||||||
| let value = input | ||||||
| for (const key of path) { | ||||||
| if (typeof value !== "object" || value === null || !Object.hasOwn(value, key)) return undefined | ||||||
| value = (value as Record<PropertyKey, unknown>)[key] | ||||||
| } | ||||||
| return value | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Each issue of a failed zod parse as one clause that names its field: `change_type is required (expected | ||||||
| * one of "remove"|"rename")` for a missing one, `limit: Too small: expected number to be >=1` for a wrong one. | ||||||
| */ | ||||||
| export function describeZodIssues(error: z.ZodError, input: unknown): string { | ||||||
| const clauses = error.issues.map((issue) => { | ||||||
| if (issue.path.length === 0) return issue.message | ||||||
| const field = issue.path.map((p, i) => (typeof p === "number" ? `[${p}]` : `${i === 0 ? "" : "."}${String(p)}`)).join("") | ||||||
| if (valueAt(input, issue.path) === undefined) { | ||||||
| const expected = issue.message.replace(/^Invalid (?:input|option):\s*/, "").replace(/,\s*received undefined$/, "") | ||||||
| return `${field} is required (${expected})` | ||||||
| } | ||||||
| return `${field}: ${issue.message}` | ||||||
| }) | ||||||
| return clauses.length > 0 ? clauses.join("; ") : error.message | ||||||
| } | ||||||
| // altimate_change end | ||||||
|
|
||||||
| /** Build a `DefWithoutID` from a resolved legacy tool object (no Effect wrapper). */ | ||||||
| function legacyDefToDef(legacy: LegacyToolDef): DefWithoutID { | ||||||
| const validationSchema = zodToJsonSchema(legacy.parameters) | ||||||
|
|
@@ -200,24 +229,24 @@ function legacyDefToDef(legacy: LegacyToolDef): DefWithoutID { | |||||
| // tool needs hidden compatibility arguments that should not be advertised. | ||||||
| jsonSchema: legacy.jsonSchema ?? validationSchema, | ||||||
| // altimate_change end | ||||||
| ...(legacy.formatValidationError | ||||||
| ? { | ||||||
| // altimate_change start — the new Tool API decodes via Schema.declare, so the | ||||||
| // error reaching us is an Effect SchemaIssue (no zod `.issues`/`.path`), not a | ||||||
| // z.ZodError. Recover the original input from Effect SchemaError.issue.actual and | ||||||
| // re-run the zod schema to produce the real z.ZodError the legacy formatter | ||||||
| // expects; fall back to the original error if recovery is impossible. | ||||||
| formatValidationError: (error: unknown) => { | ||||||
| const actual = schemaErrorActual(error) | ||||||
| if (actual.found) { | ||||||
| const parsed = legacy.parameters.safeParse(actual.value) | ||||||
| if (!parsed.success) return legacy.formatValidationError!(parsed.error) | ||||||
| } | ||||||
| return legacy.formatValidationError!(error as z.ZodError) | ||||||
| }, | ||||||
| // altimate_change end | ||||||
| } | ||||||
| : {}), | ||||||
| // altimate_change start — the new Tool API decodes via Schema.declare, so the error reaching | ||||||
| // us is an Effect SchemaIssue (no zod `.issues`/`.path`), not a z.ZodError: on its own it reads | ||||||
| // "Expected <Declaration>" and names no field. Recover the original input from Effect | ||||||
| // SchemaError.issue.actual and re-run the zod schema to produce the real z.ZodError, for the | ||||||
| // legacy tool's own formatter or, when it has none, one that names each field and what it | ||||||
| // expects; fall back to the original error if recovery is impossible. | ||||||
| formatValidationError: (error: unknown) => { | ||||||
| const actual = schemaErrorActual(error) | ||||||
| if (actual.found) { | ||||||
| const parsed = legacy.parameters.safeParse(actual.value) | ||||||
| if (!parsed.success) | ||||||
| return legacy.formatValidationError | ||||||
| ? legacy.formatValidationError(parsed.error) | ||||||
| : describeZodIssues(parsed.error, actual.value) | ||||||
| } | ||||||
| return legacy.formatValidationError ? legacy.formatValidationError(error as z.ZodError) : String(error) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [🔴 HIGH] If the error originates from Effect validation but Zod parsing unexpectedly succeeds (e.g., if the Effect schema is stricter), or if Suggested change:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That fallthrough isn't new: |
||||||
| }, | ||||||
| // altimate_change end | ||||||
| execute: (args: unknown, ctx: NewContext) => | ||||||
| // altimate_change start — bridge the ambient Instance ALS into the Effect InstanceRef so | ||||||
| // legacy tools whose execute calls async facades (Skill.get, Agent.list, …) — which resolve | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: This regression test is never executed by
bun run test: the package script omitstest/adapter-target-path.test.ts. Add the file to the package test command so target-path regressions fail CI.Prompt for AI agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 4266798:
test/adapter-target-path.test.tsis now in dbt-tools'bun run testfile list.