Skip to content

Commit 49fc784

Browse files
committed
fix: address dbt review parity comments
1 parent 1b05092 commit 49fc784

6 files changed

Lines changed: 115 additions & 20 deletions

File tree

‎docs/docs/usage/dbt-pr-review.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,9 @@ reviewer does **not** re-implement Jinja — it consumes dbt's own compiled outp
334334
the base and head refs in CI (the base into `target-base/`, the Recce
335335
convention). For proof-grade equivalence, also produce `target/catalog.json`
336336
with `dbt docs generate`; the catalog carries complete warehouse columns that
337-
`manifest.json` often lacks. Without compiled SQL or complete columns the
338-
engine lanes stay *undecidable* (never fabricated) — the `dbt-patterns` lane
339-
still runs.
337+
`manifest.json` often lacks. Without compiled SQL or complete columns,
338+
schema-dependent engine lanes such as equivalence and PII stay *undecidable*
339+
(never fabricated) — the `dbt-patterns` lane still runs.
340340

341341
## What it checks — deterministic rule catalog
342342

‎docs/internal/2026-06-08-dbt-pr-review-self-improvement-loop.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ Quantified acceptance target:
3535
warehouse-observed impact, not advisory AI or heuristic equivalence.
3636

3737
Operating rules:
38-
- Always start by pulling latest main of
39-
/Users/anandgupta/codebase/altimate-core-internal with fast-forward only.
38+
- Always start by pulling latest main of the sibling `altimate-core-internal`
39+
checkout with fast-forward only.
4040
- Prefer implementing deterministic SQL/dbt analysis in altimate-core, not
4141
altimate-code. Avoid regex/custom parsing in altimate-code when core
4242
AST/parser support can own it.
@@ -52,8 +52,8 @@ Operating rules:
5252
only at acceptance checkpoints.
5353
6. Keep only if it improves real review value with no new false positives;
5454
otherwise revert/reject or make advisory-only.
55-
- Use /Users/anandgupta/codebase/altimate-code/demo/dbt-pr-review-demo for demo
56-
branch e2e.
55+
- Use the `demo/dbt-pr-review-demo` checkout under the altimate-code workspace
56+
for demo branch e2e.
5757
- Compile base dbt artifacts into target-base and head artifacts into target
5858
before review when structural/equivalence lanes need base-vs-head compiled SQL.
5959
- Use local DuckDB first; use BigQuery where warehouse behavior matters and

‎packages/drivers/src/duckdb.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
5757
let resolved = false
5858
let timeout: ReturnType<typeof setTimeout> | undefined
5959
let instance: any
60+
let pendingOpen: Error | null | undefined
6061
const opts = accessMode ? { access_mode: accessMode } : undefined
6162
const onOpen = (err: Error | null) => {
63+
if (!instance) {
64+
pendingOpen = err
65+
return
66+
}
6267
if (resolved) {
6368
if (instance && typeof instance.close === "function") instance.close()
6469
return
@@ -79,6 +84,7 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
7984
instance = opts
8085
? new duckdb.Database(dbPath, opts, onOpen)
8186
: new duckdb.Database(dbPath, onOpen)
87+
if (pendingOpen !== undefined) onOpen(pendingOpen)
8288
// Bun: native callback may not fire; fall back after 2s
8389
timeout = setTimeout(() => {
8490
if (!resolved) {

‎packages/drivers/test/driver-security.test.ts‎

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import { describe, test, expect, mock, beforeEach, afterEach } from "bun:test"
1212
// DuckDB: wrapDuckDBError + lock retry
1313
// ---------------------------------------------------------------------------
1414
describe("DuckDB driver", () => {
15+
function openCallback(optsOrCb: any, cb?: (err: Error | null) => void): (err: Error | null) => void {
16+
return typeof optsOrCb === "function" ? optsOrCb : cb!
17+
}
18+
1519
// Test wrapDuckDBError logic inline (it's a closure, so we test via connect behavior)
1620
describe("wrapDuckDBError", () => {
1721
test("wraps SQLITE_BUSY errors with user-friendly message", async () => {
@@ -28,8 +32,8 @@ describe("DuckDB driver", () => {
2832
mock.module("duckdb", () => ({
2933
default: {
3034
Database: class {
31-
constructor(_path: string, _opts: any, cb: (err: Error | null) => void) {
32-
setTimeout(() => cb(null), 0)
35+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
36+
setTimeout(() => openCallback(optsOrCb, cb)(null), 0)
3337
}
3438
connect() {
3539
return mockDb.connect()
@@ -62,8 +66,8 @@ describe("DuckDB driver", () => {
6266
mock.module("duckdb", () => ({
6367
default: {
6468
Database: class {
65-
constructor(_path: string, _opts: any, cb: (err: Error | null) => void) {
66-
setTimeout(() => cb(null), 0)
69+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
70+
setTimeout(() => openCallback(optsOrCb, cb)(null), 0)
6771
}
6872
connect() {
6973
return {
@@ -97,8 +101,8 @@ describe("DuckDB driver", () => {
97101
mock.module("duckdb", () => ({
98102
default: {
99103
Database: class {
100-
constructor(_path: string, _opts: any, cb: (err: Error | null) => void) {
101-
setTimeout(() => cb(null), 0)
104+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
105+
setTimeout(() => openCallback(optsOrCb, cb)(null), 0)
102106
}
103107
connect() {
104108
return {
@@ -135,14 +139,16 @@ describe("DuckDB driver", () => {
135139
mock.module("duckdb", () => ({
136140
default: {
137141
Database: class {
138-
constructor(_path: string, opts: any, cb: (err: Error | null) => void) {
142+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
143+
const opts = typeof optsOrCb === "function" ? undefined : optsOrCb
144+
const done = openCallback(optsOrCb, cb)
139145
connectAttempts++
140146
if (connectAttempts === 1 && !opts?.access_mode) {
141147
// First attempt fails with lock error
142-
setTimeout(() => cb(new Error("DUCKDB_LOCKED: file is locked")), 0)
148+
setTimeout(() => done(new Error("DUCKDB_LOCKED: file is locked")), 0)
143149
} else {
144150
// READ_ONLY retry succeeds
145-
setTimeout(() => cb(null), 0)
151+
setTimeout(() => done(null), 0)
146152
}
147153
}
148154
connect() {
@@ -171,8 +177,8 @@ describe("DuckDB driver", () => {
171177
mock.module("duckdb", () => ({
172178
default: {
173179
Database: class {
174-
constructor(_path: string, _opts: any, cb: (err: Error | null) => void) {
175-
setTimeout(() => cb(new Error("DUCKDB_LOCKED")), 0)
180+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
181+
setTimeout(() => openCallback(optsOrCb, cb)(new Error("DUCKDB_LOCKED")), 0)
176182
}
177183
connect() {
178184
return {}
@@ -195,6 +201,34 @@ describe("DuckDB driver", () => {
195201
expect(e.message).toBe("DUCKDB_LOCKED")
196202
}
197203
})
204+
205+
test("handles native open callback invoked synchronously", async () => {
206+
mock.module("duckdb", () => ({
207+
default: {
208+
Database: class {
209+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
210+
openCallback(optsOrCb, cb)(null)
211+
}
212+
connect() {
213+
return {
214+
all: (_sql: string, cb: (err: Error | null, rows: any[]) => void) => {
215+
cb(null, [{ ok: 1 }])
216+
},
217+
}
218+
}
219+
close(cb: any) {
220+
if (cb) cb(null)
221+
}
222+
},
223+
},
224+
}))
225+
226+
const { connect } = await import("../src/duckdb")
227+
const connector = await connect({ type: "duckdb", path: ":memory:" })
228+
await connector.connect()
229+
expect(await connector.execute("SELECT 1")).toMatchObject({ columns: ["ok"], rows: [[1]], row_count: 1 })
230+
await connector.close()
231+
})
198232
})
199233
})
200234

‎packages/opencode/src/altimate/review/orchestrate.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,15 @@ async function piiClassifyLane(ctx: ModelContext, runner: ReviewRunner, dialect:
962962
return out
963963
}
964964

965+
async function introducedOutputColumns(ctx: ModelContext, runner: ReviewRunner, dialect: string): Promise<Set<string> | undefined> {
966+
if (!runner.columnLineage || !ctx.engineNewSql || ctx.file.status === "deleted") return undefined
967+
const headCols = [...new Set((await runner.columnLineage(ctx.engineNewSql, dialect)).map((e) => e.target).filter(Boolean))]
968+
if (!headCols.length) return new Set()
969+
if (!ctx.engineOldSql) return new Set(headCols.map((c) => c.toLowerCase()))
970+
const baseCols = new Set((await runner.columnLineage(ctx.engineOldSql, dialect)).map((e) => e.target.toLowerCase()))
971+
return new Set(headCols.filter((c) => !baseCols.has(c.toLowerCase())).map((c) => c.toLowerCase()))
972+
}
973+
965974
function piiLane(file: ChangedFile & { kind: string }, columns: string[]): Finding[] {
966975
if (file.status === "deleted" || !columns.length) return []
967976
const model = modelNameFromPath(file.path)
@@ -1077,8 +1086,16 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
10771086
tasks.push(columnBreakageLane(ctx, input.runner, getCompiled, dialect))
10781087
}
10791088
if (lanes.has("test_coverage")) tasks.push(missingGrainTestLane(ctx, input.runner))
1080-
const hasDiffScopedPii = !!input.runner.classifyPii && !!input.runner.columnLineage
1081-
if (lanes.has("pii_exposure") && !hasDiffScopedPii) all.push(piiLane(ctx.file, ctx.pii))
1089+
const diffScopedPiiCols =
1090+
input.runner.classifyPii && input.runner.columnLineage
1091+
? await introducedOutputColumns(ctx, input.runner, dialect)
1092+
: undefined
1093+
if (lanes.has("pii_exposure")) {
1094+
const fallbackPii = diffScopedPiiCols
1095+
? ctx.pii.filter((col) => !diffScopedPiiCols.has(col.toLowerCase()))
1096+
: ctx.pii
1097+
all.push(piiLane(ctx.file, fallbackPii))
1098+
}
10821099
// PII classification always runs (cheap name-pattern check, diff-scoped to
10831100
// newly-introduced columns) — exposing PII is a hard-floor concern that
10841101
// shouldn't depend on the cost tier enabling the pii_exposure lane.

‎packages/opencode/test/altimate/review.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,44 @@ describe("orchestrate", () => {
13721372
expect(env.verdict).toBe("REQUEST_CHANGES")
13731373
})
13741374

1375+
test("broad PII detector remains fallback for existing output columns", async () => {
1376+
const files: ChangedFile[] = [{ path: "models/marts/dim_customers.sql", status: "modified", diff: "+ lower(email) as email\n" }]
1377+
const runner: ReviewRunner = {
1378+
...fakeRunner({}),
1379+
async detectPii() {
1380+
return { columns: ["email"] }
1381+
},
1382+
async columnLineage(sql) {
1383+
return sql.includes("email")
1384+
? [
1385+
{ source: "customers.customer_id", target: "customer_id" },
1386+
{ source: "customers.email", target: "email" },
1387+
]
1388+
: [{ source: "customers.customer_id", target: "customer_id" }]
1389+
},
1390+
async classifyPii(columns) {
1391+
return columns.map((column) => ({
1392+
column,
1393+
classification: "Email",
1394+
confidence: 0.95,
1395+
masking: "'***MASKED***'",
1396+
}))
1397+
},
1398+
}
1399+
const env = await runReview({
1400+
changedFiles: files,
1401+
config: { ...DEFAULT_REVIEW_CONFIG },
1402+
rubric: DEFAULT_RUBRIC,
1403+
mode: "gate",
1404+
runner,
1405+
getContent: content("select customer_id, lower(email) as email from customers", "select customer_id, email from customers"),
1406+
})
1407+
const pii = env.findings.filter((f) => f.category === "pii_exposure")
1408+
expect(pii).toHaveLength(1)
1409+
expect(pii[0].evidence?.tool).toBe("schema.detect_pii")
1410+
expect(env.verdict).toBe("REQUEST_CHANGES")
1411+
})
1412+
13751413
test("low-confidence name PII does not surface or fall back to regex PII comments", async () => {
13761414
const files: ChangedFile[] = [{ path: "models/marts/dim_customers.sql", status: "modified", diff: "+ first_name\n" }]
13771415
const runner: ReviewRunner = {

0 commit comments

Comments
 (0)