Skip to content

Commit 7c7b6db

Browse files
committed
test: harden dbt review parity edge cases
1 parent 173f9d9 commit 7c7b6db

4 files changed

Lines changed: 249 additions & 39 deletions

File tree

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,61 @@ describe("DuckDB driver", () => {
229229
expect(await connector.execute("SELECT 1")).toMatchObject({ columns: ["ok"], rows: [[1]], row_count: 1 })
230230
await connector.close()
231231
})
232+
233+
test("retries read-only when native open synchronously reports a file lock", async () => {
234+
let connectAttempts = 0
235+
mock.module("duckdb", () => ({
236+
default: {
237+
Database: class {
238+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
239+
const opts = typeof optsOrCb === "function" ? undefined : optsOrCb
240+
const done = openCallback(optsOrCb, cb)
241+
connectAttempts++
242+
done(connectAttempts === 1 && !opts?.access_mode ? new Error("DUCKDB_LOCKED: file is locked") : null)
243+
}
244+
connect() {
245+
return {
246+
all: (_sql: string, cb: (err: Error | null, rows: any[]) => void) => {
247+
cb(null, [{ ok: 1 }])
248+
},
249+
}
250+
}
251+
close(cb: any) {
252+
if (cb) cb(null)
253+
}
254+
},
255+
},
256+
}))
257+
258+
const { connect } = await import("../src/duckdb")
259+
const connector = await connect({ type: "duckdb", path: "/tmp/sync-lock.duckdb" })
260+
await connector.connect()
261+
expect(connectAttempts).toBe(2)
262+
expect(await connector.execute("SELECT 1")).toMatchObject({ columns: ["ok"], rows: [[1]], row_count: 1 })
263+
await connector.close()
264+
})
265+
266+
test("propagates synchronous non-lock open errors without connecting undefined db", async () => {
267+
mock.module("duckdb", () => ({
268+
default: {
269+
Database: class {
270+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
271+
openCallback(optsOrCb, cb)(new Error("catalog is corrupt"))
272+
}
273+
connect() {
274+
throw new Error("should not connect")
275+
}
276+
close(cb: any) {
277+
if (cb) cb(null)
278+
}
279+
},
280+
},
281+
}))
282+
283+
const { connect } = await import("../src/duckdb")
284+
const connector = await connect({ type: "duckdb", path: "/tmp/corrupt.duckdb" })
285+
await expect(connector.connect()).rejects.toThrow("catalog is corrupt")
286+
})
232287
})
233288
})
234289

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

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -916,22 +916,38 @@ async function siblingConsistencyLane(ctxs: ModelContext[], runner: ReviewRunner
916916
* only NEW PII (a column whose name appears in the added diff), so it never nags
917917
* about pre-existing PII columns the PR didn't touch.
918918
*/
919-
async function piiClassifyLane(ctx: ModelContext, runner: ReviewRunner, dialect: string): Promise<Finding[]> {
920-
if (!runner.classifyPii) return []
919+
async function piiClassifyLane(
920+
ctx: ModelContext,
921+
runner: ReviewRunner,
922+
dialect: string,
923+
): Promise<{ findings: Finding[]; introducedColumns: Set<string>; completed: boolean }> {
924+
const empty = { findings: [], introducedColumns: new Set<string>(), completed: false }
925+
if (!runner.classifyPii) return empty
921926
const { file, engineNewSql } = ctx
922-
if (!engineNewSql || file.status === "deleted") return []
927+
if (!engineNewSql || file.status === "deleted") return empty
923928
const model = modelNameFromPath(file.path)
924-
if (!runner.columnLineage) return []
929+
if (!runner.columnLineage) return empty
925930
// Output columns the change INTRODUCED = head targets − base targets (precise;
926931
// a pre-existing column merely mentioned in the diff is NOT counted).
927-
const headCols = [...new Set((await runner.columnLineage(engineNewSql, dialect)).map((e) => e.target).filter(Boolean))]
928-
if (!headCols.length) return []
929-
const baseCols = ctx.engineOldSql
930-
? new Set((await runner.columnLineage(ctx.engineOldSql, dialect)).map((e) => e.target.toLowerCase()))
931-
: undefined
932-
const newCols = baseCols ? headCols.filter((c) => !baseCols.has(c.toLowerCase())) : headCols
933-
if (!newCols.length) return []
934-
const pii = await runner.classifyPii(newCols)
932+
let newCols: string[]
933+
try {
934+
const headCols = [...new Set((await runner.columnLineage(engineNewSql, dialect)).map((e) => e.target).filter(Boolean))]
935+
if (!headCols.length) return { ...empty, completed: true }
936+
const baseCols = ctx.engineOldSql
937+
? new Set((await runner.columnLineage(ctx.engineOldSql, dialect)).map((e) => e.target.toLowerCase()))
938+
: undefined
939+
newCols = baseCols ? headCols.filter((c) => !baseCols.has(c.toLowerCase())) : headCols
940+
} catch {
941+
return empty
942+
}
943+
const introducedColumns = new Set(newCols.map((c) => c.toLowerCase()))
944+
if (!newCols.length) return { findings: [], introducedColumns, completed: true }
945+
let pii
946+
try {
947+
pii = await runner.classifyPii(newCols)
948+
} catch {
949+
return empty
950+
}
935951
const out: Finding[] = []
936952
const highExposureLayer = /(^|\/)(marts|reporting)\//.test(file.path)
937953
for (const p of pii) {
@@ -959,16 +975,7 @@ async function piiClassifyLane(ctx: ModelContext, runner: ReviewRunner, dialect:
959975
}),
960976
)
961977
}
962-
return out
963-
}
964-
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()))
978+
return { findings: out, introducedColumns, completed: true }
972979
}
973980

974981
function piiLane(file: ChangedFile & { kind: string }, columns: string[]): Finding[] {
@@ -1061,6 +1068,7 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
10611068
const lanes = new Set(input.config.reviewers.length ? input.config.reviewers : TIER_LANES[tier])
10621069

10631070
const all: Finding[][] = []
1071+
const diffScopedPiiFiles = new Set<string>()
10641072
for (const ctx of ctxByPath.values()) {
10651073
const tasks: Promise<Finding[]>[] = []
10661074
// Engine lanes consume COMPILED SQL (rendered by dbt) when available.
@@ -1086,20 +1094,19 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
10861094
tasks.push(columnBreakageLane(ctx, input.runner, getCompiled, dialect))
10871095
}
10881096
if (lanes.has("test_coverage")) tasks.push(missingGrainTestLane(ctx, input.runner))
1089-
const diffScopedPiiCols =
1090-
input.runner.classifyPii && input.runner.columnLineage
1091-
? await introducedOutputColumns(ctx, input.runner, dialect)
1092-
: undefined
1097+
// PII classification always runs (cheap name-pattern check, diff-scoped to
1098+
// newly-introduced columns) — exposing PII is a hard-floor concern that
1099+
// shouldn't depend on the cost tier enabling the pii_exposure lane. If core
1100+
// lineage/classification fails, keep the broad deterministic fallback.
1101+
const diffScopedPii = await piiClassifyLane(ctx, input.runner, dialect)
1102+
if (diffScopedPii.completed) diffScopedPiiFiles.add(ctx.file.path)
10931103
if (lanes.has("pii_exposure")) {
1094-
const fallbackPii = diffScopedPiiCols
1095-
? ctx.pii.filter((col) => !diffScopedPiiCols.has(col.toLowerCase()))
1104+
const fallbackPii = diffScopedPii.completed
1105+
? ctx.pii.filter((col) => !diffScopedPii.introducedColumns.has(col.toLowerCase()))
10961106
: ctx.pii
10971107
all.push(piiLane(ctx.file, fallbackPii))
10981108
}
1099-
// PII classification always runs (cheap name-pattern check, diff-scoped to
1100-
// newly-introduced columns) — exposing PII is a hard-floor concern that
1101-
// shouldn't depend on the cost tier enabling the pii_exposure lane.
1102-
tasks.push(piiClassifyLane(ctx, input.runner, dialect))
1109+
all.push(diffScopedPii.findings)
11031110
// Deterministic dbt anti-pattern detectors run on RAW SQL + diff (need Jinja).
11041111
if (lanes.has("dbt_patterns")) {
11051112
all.push(detectModelPatterns(ctx.file, ctx.newSql, input.rubric))
@@ -1163,12 +1170,9 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
11631170
// regex still covers functions core didn't flag (dialect-suppressed / not in
11641171
// core's set) plus reserved words / types / operators that L033 doesn't do.
11651172
const flat = all.flat()
1166-
const diffScopedPiiFiles = new Set<string>()
1167-
if (input.runner.classifyPii && input.runner.columnLineage) {
1168-
for (const ctx of ctxByPath.values()) {
1169-
if (ctx.engineNewSql && ctx.file.status !== "deleted") diffScopedPiiFiles.add(ctx.file.path)
1170-
}
1171-
}
1173+
const schemaPiiFiles = new Set(
1174+
flat.filter((f) => f.category === "pii_exposure" && f.evidence?.tool === "schema.detect_pii").map((f) => f.file),
1175+
)
11721176
// Per file, the concatenated rule names core's AST lint actually emitted —
11731177
// so we suppress a regex twin only when core genuinely covered that concern.
11741178
const coreRulesByFile = new Map<string, string>()
@@ -1195,7 +1199,7 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
11951199
const fallbackRule = String((f.evidence?.result as any)?.rule ?? "").replace(/_/g, "-")
11961200
if (
11971201
(tool === "dbt-patterns" || tool === "rule-catalog") &&
1198-
diffScopedPiiFiles.has(f.file) &&
1202+
(diffScopedPiiFiles.has(f.file) || schemaPiiFiles.has(f.file)) &&
11991203
f.category === "pii_exposure" &&
12001204
(fallbackRule === "pii-into-mart" || fallbackRule === "select-pii-columns")
12011205
) {

‎packages/opencode/test/altimate/data-diff-duckdb-e2e.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,54 @@ describe("data.diff DuckDB e2e", () => {
4141
expect((result.outcome as any)?.stats?.exclusive_table2).toBe(1)
4242
expect((result.outcome as any)?.stats?.updated).toBe(1)
4343
})
44+
45+
e2eTest("does not report differences for identical DuckDB tables", async () => {
46+
Registry.reset()
47+
Registry.setConfigs({ duck_e2e: { type: "duckdb", path: ":memory:" } })
48+
const conn = await Registry.get("duck_e2e")
49+
50+
await conn.execute("CREATE TABLE base_orders (order_id INTEGER, amount INTEGER)")
51+
await conn.execute("CREATE TABLE head_orders (order_id INTEGER, amount INTEGER)")
52+
await conn.execute("INSERT INTO base_orders VALUES (1, 100), (2, 200)")
53+
await conn.execute("INSERT INTO head_orders VALUES (1, 100), (2, 200)")
54+
55+
const result = await runDataDiff({
56+
source: "base_orders",
57+
target: "head_orders",
58+
key_columns: ["order_id"],
59+
extra_columns: ["amount"],
60+
source_warehouse: "duck_e2e",
61+
target_warehouse: "duck_e2e",
62+
algorithm: "joindiff",
63+
})
64+
65+
expect(result.success).toBe(true)
66+
expect((result.outcome as any)?.stats?.exclusive_table1).toBe(0)
67+
expect((result.outcome as any)?.stats?.exclusive_table2).toBe(0)
68+
expect((result.outcome as any)?.stats?.updated).toBe(0)
69+
})
70+
71+
e2eTest("auto-discovers comparable columns when extra_columns is omitted", async () => {
72+
Registry.reset()
73+
Registry.setConfigs({ duck_e2e: { type: "duckdb", path: ":memory:" } })
74+
const conn = await Registry.get("duck_e2e")
75+
76+
await conn.execute("CREATE TABLE base_orders (order_id INTEGER, amount INTEGER, updated_at TIMESTAMP)")
77+
await conn.execute("CREATE TABLE head_orders (order_id INTEGER, amount INTEGER, updated_at TIMESTAMP)")
78+
await conn.execute("INSERT INTO base_orders VALUES (1, 100, TIMESTAMP '2026-01-01 00:00:00'), (2, 200, TIMESTAMP '2026-01-01 00:00:00')")
79+
await conn.execute("INSERT INTO head_orders VALUES (1, 100, TIMESTAMP '2026-01-02 00:00:00'), (2, 250, TIMESTAMP '2026-01-02 00:00:00')")
80+
81+
const result = await runDataDiff({
82+
source: "base_orders",
83+
target: "head_orders",
84+
key_columns: ["order_id"],
85+
source_warehouse: "duck_e2e",
86+
target_warehouse: "duck_e2e",
87+
algorithm: "joindiff",
88+
})
89+
90+
expect(result.success).toBe(true)
91+
expect((result.outcome as any)?.stats?.updated).toBe(1)
92+
expect(result.excluded_audit_columns).toContain("updated_at")
93+
})
4494
})

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,107 @@ describe("orchestrate", () => {
14101410
expect(env.verdict).toBe("REQUEST_CHANGES")
14111411
})
14121412

1413+
test("PII fallback survives core lineage failure instead of aborting review", async () => {
1414+
const files: ChangedFile[] = [{ path: "models/marts/dim_customers.sql", status: "modified", diff: "+ email\n" }]
1415+
const runner: ReviewRunner = {
1416+
...fakeRunner({}),
1417+
async detectPii() {
1418+
return { columns: ["email"] }
1419+
},
1420+
async columnLineage() {
1421+
throw new Error("lineage parser crashed")
1422+
},
1423+
async classifyPii(columns) {
1424+
return columns.map((column) => ({
1425+
column,
1426+
classification: "Email",
1427+
confidence: 0.95,
1428+
}))
1429+
},
1430+
}
1431+
const env = await runReview({
1432+
changedFiles: files,
1433+
config: { ...DEFAULT_REVIEW_CONFIG },
1434+
rubric: DEFAULT_RUBRIC,
1435+
mode: "gate",
1436+
runner,
1437+
getContent: content("select customer_id, email from customers", "select customer_id from customers"),
1438+
})
1439+
const pii = env.findings.filter((f) => f.category === "pii_exposure")
1440+
expect(pii).toHaveLength(1)
1441+
expect(pii[0].evidence?.tool).toBe("schema.detect_pii")
1442+
expect(env.verdict).toBe("REQUEST_CHANGES")
1443+
})
1444+
1445+
test("PII fallback survives core classification failure instead of hiding risk", async () => {
1446+
const files: ChangedFile[] = [{ path: "models/marts/dim_customers.sql", status: "modified", diff: "+ email\n" }]
1447+
const runner: ReviewRunner = {
1448+
...fakeRunner({}),
1449+
async detectPii() {
1450+
return { columns: ["email"] }
1451+
},
1452+
async columnLineage(sql) {
1453+
return sql.includes("email")
1454+
? [
1455+
{ source: "customers.customer_id", target: "customer_id" },
1456+
{ source: "customers.email", target: "email" },
1457+
]
1458+
: [{ source: "customers.customer_id", target: "customer_id" }]
1459+
},
1460+
async classifyPii() {
1461+
throw new Error("classifier unavailable")
1462+
},
1463+
}
1464+
const env = await runReview({
1465+
changedFiles: files,
1466+
config: { ...DEFAULT_REVIEW_CONFIG },
1467+
rubric: DEFAULT_RUBRIC,
1468+
mode: "gate",
1469+
runner,
1470+
getContent: content("select customer_id, email from customers", "select customer_id from customers"),
1471+
})
1472+
const pii = env.findings.filter((f) => f.category === "pii_exposure")
1473+
expect(pii).toHaveLength(1)
1474+
expect(pii[0].evidence?.tool).toBe("schema.detect_pii")
1475+
expect(env.verdict).toBe("REQUEST_CHANGES")
1476+
})
1477+
1478+
test("case-variant PII detector output does not duplicate core diff-scoped finding", async () => {
1479+
const files: ChangedFile[] = [{ path: "models/marts/dim_customers.sql", status: "modified", diff: "+ Email as EMAIL\n" }]
1480+
const runner: ReviewRunner = {
1481+
...fakeRunner({}),
1482+
async detectPii() {
1483+
return { columns: ["EMAIL", "email"] }
1484+
},
1485+
async columnLineage(sql) {
1486+
return sql.toLowerCase().includes("email")
1487+
? [
1488+
{ source: "customers.customer_id", target: "customer_id" },
1489+
{ source: "customers.email", target: "EMAIL" },
1490+
]
1491+
: [{ source: "customers.customer_id", target: "customer_id" }]
1492+
},
1493+
async classifyPii(columns) {
1494+
return columns.map((column) => ({
1495+
column,
1496+
classification: "Email",
1497+
confidence: 0.95,
1498+
}))
1499+
},
1500+
}
1501+
const env = await runReview({
1502+
changedFiles: files,
1503+
config: { ...DEFAULT_REVIEW_CONFIG },
1504+
rubric: DEFAULT_RUBRIC,
1505+
mode: "gate",
1506+
runner,
1507+
getContent: content("select customer_id, Email as EMAIL from customers", "select customer_id from customers"),
1508+
})
1509+
const pii = env.findings.filter((f) => f.category === "pii_exposure")
1510+
expect(pii).toHaveLength(1)
1511+
expect(pii[0].evidence?.tool).toBe("altimate_core.classify_pii")
1512+
})
1513+
14131514
test("low-confidence name PII does not surface or fall back to regex PII comments", async () => {
14141515
const files: ChangedFile[] = [{ path: "models/marts/dim_customers.sql", status: "modified", diff: "+ first_name\n" }]
14151516
const runner: ReviewRunner = {

0 commit comments

Comments
 (0)