Skip to content

Commit a00fb38

Browse files
anandgupta42claude
andcommitted
fix: cubic round-3 polish — no eager NAPI load from tool imports
- Move `piiColumnsFromReport` into `native/engine-coerce.ts` (re-exported from `pii-detector` for compatibility) so `altimate-core-classify-pii` no longer pulls the native NAPI binding eagerly at tool-registry load time. - `bandConfidence`: missing confidence maps to "medium" (unknown), restoring the previous default instead of degrading to "low". - classify-pii error test: temp dir created outside `try` but cleaned in `finally` regardless of setup failure. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent a03901b commit a00fb38

4 files changed

Lines changed: 23 additions & 19 deletions

File tree

‎packages/opencode/src/altimate/native/engine-coerce.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export function dialectHint(dialect: string | undefined | null): string | undefi
2424

2525
/** Map the engine's numeric confidence (0..1) to a string band. */
2626
export function bandConfidence(c: unknown): "high" | "medium" | "low" {
27+
// Missing confidence is unknown, not low.
28+
if (c == null) return "medium"
2729
if (typeof c === "string") {
2830
const s = c.toLowerCase()
2931
if (s === "high" || s === "medium" || s === "low") return s
@@ -33,3 +35,16 @@ export function bandConfidence(c: unknown): "high" | "medium" | "low" {
3335
if (n >= 0.5) return "medium"
3436
return "low"
3537
}
38+
39+
/**
40+
* Extract the real PII rows from an engine PiiReport.
41+
*
42+
* The engine returns `{ columns, pii_count, risk_level, total_columns }` with
43+
* a row for EVERY column — classification "None" means not PII. Lives here
44+
* (not in pii-detector) so tool modules can import it without eagerly loading
45+
* the native NAPI binding at registry-load time.
46+
*/
47+
export function piiColumnsFromReport(piiData: unknown): Array<Record<string, any>> {
48+
const columns = ((piiData as Record<string, any>)?.columns ?? []) as Array<Record<string, any>>
49+
return columns.filter((c) => c.classification !== "None")
50+
}

‎packages/opencode/src/altimate/native/schema/pii-detector.ts‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,17 @@
44
*/
55

66
import * as core from "@altimateai/altimate-core"
7-
import { bandConfidence, classificationToString } from "../engine-coerce"
7+
import { bandConfidence, classificationToString, piiColumnsFromReport } from "../engine-coerce"
88
import { getCache } from "./cache"
9+
10+
export { piiColumnsFromReport }
911
import * as Registry from "../connections/registry"
1012
import type {
1113
PiiDetectParams,
1214
PiiDetectResult,
1315
PiiFinding,
1416
} from "../types"
1517

16-
/**
17-
* Extract the real PII rows from an engine PiiReport.
18-
*
19-
* The engine returns `{ columns, pii_count, risk_level, total_columns }` with
20-
* a row for EVERY column — classification "None" means not PII. (The previous
21-
* consumer read a nonexistent `findings` field, making detection a silent
22-
* no-op.) Exported for tests.
23-
*/
24-
export function piiColumnsFromReport(piiData: unknown): Array<Record<string, any>> {
25-
const columns = ((piiData as Record<string, any>)?.columns ?? []) as Array<Record<string, any>>
26-
return columns.filter((c) => c.classification !== "None")
27-
}
28-
2918
/**
3019
* Detect PII in cached schema metadata by running altimate-core's
3120
* classifyPii() on column names and types.

‎packages/opencode/src/altimate/tools/altimate-core-classify-pii.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import z from "zod"
22
import { Tool } from "../../tool/tool"
33
import { Dispatcher } from "../native"
4-
import { classificationToString } from "../native/engine-coerce"
5-
import { piiColumnsFromReport } from "../native/schema/pii-detector"
4+
import { classificationToString, piiColumnsFromReport } from "../native/engine-coerce"
65

76
/** Engine PiiReport lists EVERY column; classification "None" means not PII. */
87
function realPiiColumns(data: Record<string, any>): any[] {

‎packages/opencode/test/altimate/altimate-core-e2e.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,16 +1251,17 @@ describeIf("consumer contract sync (round 2)", () => {
12511251
const fs = await import("fs/promises")
12521252
const os = await import("os")
12531253
const path = await import("path")
1254-
const badSchema = path.join(await fs.mkdtemp(path.join(os.tmpdir(), "pii-err-")), "schema.json")
1255-
await fs.writeFile(badSchema, "not json{{{")
1254+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "pii-err-"))
12561255
try {
1256+
const badSchema = path.join(dir, "schema.json")
1257+
await fs.writeFile(badSchema, "not json{{{")
12571258
const result = await tool.execute({ schema_path: badSchema }, toolCtx())
12581259
expect(result.title).toBe("PII Classification: ERROR")
12591260
expect(result.metadata.success).toBe(false)
12601261
expect(result.output).toContain("Error")
12611262
expect(result.output).not.toContain("No PII columns detected")
12621263
} finally {
1263-
await fs.rm(path.dirname(badSchema), { recursive: true, force: true })
1264+
await fs.rm(dir, { recursive: true, force: true })
12641265
}
12651266
})
12661267

0 commit comments

Comments
 (0)