Skip to content

Commit 2aa8b0e

Browse files
anandgupta42claude
andauthored
Make dbt PR reviewer demo-parity reliable (#919)
* Document dbt review improvement loop * Reduce safe refactor review noise * Tighten PII review precision * Add DuckDB data diff e2e coverage * fix: address dbt review parity comments * chore: remove non-functional planning docs from dbt review PR * chore: drop non-functional dbt review docs edit * test: harden dbt review parity edge cases * fix: address consensus review findings on dbt PR reviewer (PR #919) Fixes raised by the multi-model review of #919, prioritizing the PII false-negative that 5 models flagged. - PII recall (MAJOR): the diff-scoped PII dedup keyed off whether the classifier *ran* and off ALL introduced columns, so a high-risk column core's classifier MISSED (e.g. `ssn`) was dropped from the broad `schema.detect_pii` fallback AND had its regex twin suppressed — silently. Now dedup against the columns core actually CLASSIFIED (`classifiedColumns`), and make the regex-twin suppression column-aware (`pii_into_mart` via its matched `result.line`; `select-pii-columns` only when a precise detector actually emitted a finding). A column core never classified now stays eligible for the broad detector and the regex net — including on trivial/lite tiers that don't run the `pii_exposure` lane. Low-confidence name/address suppression is preserved (core *considered* those columns). - Structural escalation (MAJOR): only `join_risk` (SC010 join-key regression) escalates a core `error` to critical/blocking. Other structural rules core marks `error` stay advisory (warning), so the lane never over-blocks on a rule it wasn't designed to gate. - DuckDB (MINOR): arm the 2s fallback timer BEFORE replaying a synchronous open callback so a sync resolve/reject can clear it (no dangling timer delaying process exit); release the half-open handle on open error. - Concurrency (MINOR): run the diff-scoped PII lane as a concurrent task instead of an inline `await`, so its native lineage/classify calls overlap the other per-file engine lanes. - Polish: align `ReviewRunner.equivalence` dialect param to optional (matches the impl); named constants for the PII confidence thresholds; document that PII exposure severity intentionally escalates only for `marts/reporting`; comment the `--no-ai`/`--ai=false` dual check. Adds regression tests: core-missed column surfaces via broad fallback; core-missed high-risk PII still caught by the regex twin when the broad PII lane is off; non-join `error` structural rule stays advisory; sub-error join-key regression stays a warning. Existing 76 review + 26 driver tests still pass. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * fix: address PR #919 review comments on dbt PR reviewer Resolve the three unresolved bot-review threads on PR #919: - `orchestrate.ts` (`piiClassifyLane`): when `columnLineage` resolves but derives zero output targets, return `empty` (`completed: false`) instead of `completed: true`. Zero head targets is lineage *uncertainty* (the classifier considered nothing), not a confident "no new PII", so the file must not be marked PII-complete — keeping the deterministic regex fallback alive. Distinct from the `!newCols.length` case, where lineage resolved columns but none are newly introduced (genuinely done). Defensive/intent fix: behavior-neutral for the current detectors (the tokenless suppression path is unreachable because `pii_into_mart` always carries a recoverable column token), but it makes the `completed` contract honest and removes a latent footgun. - `review.ts`: correct the misleading `noAi` comment. Only `--no-ai` is registered, so `--ai=false` is not a supported CLI flag; `args.ai === false` only covers programmatic callers. - `driver-security.test.ts`: assert the DuckDB lock retry actually requests `READ_ONLY` (record `access_mode` per attempt), not just that two attempts occurred. Tests: add two `review.test.ts` regressions — a zero-target-lineage guard and a token-gated suppression guard (core muted a low-confidence column → the coarse regex twin is suppressed). Review suite 82 pass, driver suite 26 pass. Reviewed via multi-model consensus (Claude + 7 models); the lone objection was test completeness, now incorporated. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * fix: drop unsupported dialect arg from checkEquivalence call `core.checkEquivalence` ([email protected]) is typed `(sqlA, sqlB, schema)` — 3 params, no dialect (unlike `columnLineage`/`compareQueries`). A 4th `dialect` arg was added to this call earlier on the branch (b5060e0), which the napi binding silently ignores at runtime but which fails `tsgo` typecheck (TS2554) and blocks the pre-push hook. Align with the binding contract and the sibling call site in `native/sql/register.ts`. Behavior-neutral. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 1748c37 commit 2aa8b0e

9 files changed

Lines changed: 995 additions & 55 deletions

File tree

‎packages/drivers/src/duckdb.ts‎

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,57 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
5656
new Promise<any>((resolve, reject) => {
5757
let resolved = false
5858
let timeout: ReturnType<typeof setTimeout> | undefined
59+
let instance: any
60+
// Sentinel for an open callback that fired synchronously (before
61+
// `instance` was assigned): `undefined` = not yet fired, `null` =
62+
// fired with success, `Error` = fired with failure. Replayed once
63+
// `instance` exists.
64+
let pendingOpen: Error | null | undefined
5965
const opts = accessMode ? { access_mode: accessMode } : undefined
60-
const instance = new duckdb.Database(
61-
dbPath,
62-
opts,
63-
(err: Error | null) => {
64-
if (resolved) { if (instance && typeof instance.close === "function") instance.close(); return }
65-
resolved = true
66-
if (timeout) clearTimeout(timeout)
67-
if (err) {
68-
const msg = err.message || String(err)
69-
if (msg.toLowerCase().includes("locked") || msg.includes("SQLITE_BUSY") || msg.includes("DUCKDB_LOCKED")) {
70-
reject(new Error("DUCKDB_LOCKED"))
71-
} else {
72-
reject(err)
73-
}
66+
const closeQuietly = () => {
67+
try {
68+
if (instance && typeof instance.close === "function") instance.close()
69+
} catch {
70+
// best-effort cleanup of a half-open handle
71+
}
72+
}
73+
const onOpen = (err: Error | null) => {
74+
if (!instance) {
75+
pendingOpen = err
76+
return
77+
}
78+
if (resolved) {
79+
closeQuietly()
80+
return
81+
}
82+
resolved = true
83+
if (timeout) clearTimeout(timeout)
84+
if (err) {
85+
// Open failed — release the half-open handle so it doesn't leak.
86+
closeQuietly()
87+
const msg = err.message || String(err)
88+
if (msg.toLowerCase().includes("locked") || msg.includes("SQLITE_BUSY") || msg.includes("DUCKDB_LOCKED")) {
89+
reject(new Error("DUCKDB_LOCKED"))
7490
} else {
75-
resolve(instance)
91+
reject(err)
7692
}
77-
},
78-
)
79-
// Bun: native callback may not fire; fall back after 2s
93+
} else {
94+
resolve(instance)
95+
}
96+
}
97+
instance = opts
98+
? new duckdb.Database(dbPath, opts, onOpen)
99+
: new duckdb.Database(dbPath, onOpen)
100+
// Bun: native callback may not fire; fall back after 2s. Arm the timer
101+
// BEFORE replaying a synchronous callback so a sync resolve/reject can
102+
// actually clear it (otherwise it lingers ~2s and delays process exit).
80103
timeout = setTimeout(() => {
81104
if (!resolved) {
82105
resolved = true
83106
reject(new Error(`Timed out opening DuckDB database "${dbPath}"`))
84107
}
85108
}, 2000)
109+
if (pendingOpen !== undefined) onOpen(pendingOpen)
86110
})
87111

88112
try {

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

Lines changed: 106 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 {
@@ -132,17 +136,21 @@ describe("DuckDB driver", () => {
132136
describe("connect retry with READ_ONLY", () => {
133137
test("retries with READ_ONLY when file DB is locked on initial connect", async () => {
134138
let connectAttempts = 0
139+
const accessModes: Array<string | undefined> = []
135140
mock.module("duckdb", () => ({
136141
default: {
137142
Database: class {
138-
constructor(_path: string, opts: any, cb: (err: Error | null) => void) {
143+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
144+
const opts = typeof optsOrCb === "function" ? undefined : optsOrCb
145+
const done = openCallback(optsOrCb, cb)
146+
accessModes.push(opts?.access_mode)
139147
connectAttempts++
140148
if (connectAttempts === 1 && !opts?.access_mode) {
141149
// First attempt fails with lock error
142-
setTimeout(() => cb(new Error("DUCKDB_LOCKED: file is locked")), 0)
150+
setTimeout(() => done(new Error("DUCKDB_LOCKED: file is locked")), 0)
143151
} else {
144152
// READ_ONLY retry succeeds
145-
setTimeout(() => cb(null), 0)
153+
setTimeout(() => done(null), 0)
146154
}
147155
}
148156
connect() {
@@ -163,6 +171,10 @@ describe("DuckDB driver", () => {
163171
const connector = await connect({ type: "duckdb", path: "/tmp/test.duckdb" })
164172
await connector.connect()
165173
expect(connectAttempts).toBe(2) // First failed, second succeeded in READ_ONLY
174+
// The retry must specifically request READ_ONLY — two attempts alone don't
175+
// prove the lock was worked around correctly.
176+
expect(accessModes[0]).toBeUndefined()
177+
expect(accessModes[1]).toBe("READ_ONLY")
166178

167179
await connector.close()
168180
})
@@ -171,8 +183,8 @@ describe("DuckDB driver", () => {
171183
mock.module("duckdb", () => ({
172184
default: {
173185
Database: class {
174-
constructor(_path: string, _opts: any, cb: (err: Error | null) => void) {
175-
setTimeout(() => cb(new Error("DUCKDB_LOCKED")), 0)
186+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
187+
setTimeout(() => openCallback(optsOrCb, cb)(new Error("DUCKDB_LOCKED")), 0)
176188
}
177189
connect() {
178190
return {}
@@ -195,6 +207,89 @@ describe("DuckDB driver", () => {
195207
expect(e.message).toBe("DUCKDB_LOCKED")
196208
}
197209
})
210+
211+
test("handles native open callback invoked synchronously", async () => {
212+
mock.module("duckdb", () => ({
213+
default: {
214+
Database: class {
215+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
216+
openCallback(optsOrCb, cb)(null)
217+
}
218+
connect() {
219+
return {
220+
all: (_sql: string, cb: (err: Error | null, rows: any[]) => void) => {
221+
cb(null, [{ ok: 1 }])
222+
},
223+
}
224+
}
225+
close(cb: any) {
226+
if (cb) cb(null)
227+
}
228+
},
229+
},
230+
}))
231+
232+
const { connect } = await import("../src/duckdb")
233+
const connector = await connect({ type: "duckdb", path: ":memory:" })
234+
await connector.connect()
235+
expect(await connector.execute("SELECT 1")).toMatchObject({ columns: ["ok"], rows: [[1]], row_count: 1 })
236+
await connector.close()
237+
})
238+
239+
test("retries read-only when native open synchronously reports a file lock", async () => {
240+
let connectAttempts = 0
241+
mock.module("duckdb", () => ({
242+
default: {
243+
Database: class {
244+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
245+
const opts = typeof optsOrCb === "function" ? undefined : optsOrCb
246+
const done = openCallback(optsOrCb, cb)
247+
connectAttempts++
248+
done(connectAttempts === 1 && !opts?.access_mode ? new Error("DUCKDB_LOCKED: file is locked") : null)
249+
}
250+
connect() {
251+
return {
252+
all: (_sql: string, cb: (err: Error | null, rows: any[]) => void) => {
253+
cb(null, [{ ok: 1 }])
254+
},
255+
}
256+
}
257+
close(cb: any) {
258+
if (cb) cb(null)
259+
}
260+
},
261+
},
262+
}))
263+
264+
const { connect } = await import("../src/duckdb")
265+
const connector = await connect({ type: "duckdb", path: "/tmp/sync-lock.duckdb" })
266+
await connector.connect()
267+
expect(connectAttempts).toBe(2)
268+
expect(await connector.execute("SELECT 1")).toMatchObject({ columns: ["ok"], rows: [[1]], row_count: 1 })
269+
await connector.close()
270+
})
271+
272+
test("propagates synchronous non-lock open errors without connecting undefined db", async () => {
273+
mock.module("duckdb", () => ({
274+
default: {
275+
Database: class {
276+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
277+
openCallback(optsOrCb, cb)(new Error("catalog is corrupt"))
278+
}
279+
connect() {
280+
throw new Error("should not connect")
281+
}
282+
close(cb: any) {
283+
if (cb) cb(null)
284+
}
285+
},
286+
},
287+
}))
288+
289+
const { connect } = await import("../src/duckdb")
290+
const connector = await connect({ type: "duckdb", path: "/tmp/corrupt.duckdb" })
291+
await expect(connector.connect()).rejects.toThrow("catalog is corrupt")
292+
})
198293
})
199294
})
200295

‎packages/opencode/src/altimate/native/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,7 @@ export interface AltimateCoreTestgenParams {
879879
export interface AltimateCoreEquivalenceParams {
880880
sql1: string
881881
sql2: string
882+
dialect?: string
882883
schema_path?: string
883884
schema_context?: Record<string, any>
884885
}

0 commit comments

Comments
 (0)