periderm-cli 0.1.63 → 0.1.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/defect_model.onnx +0 -0
- package/dist/scanner/index.js +21 -0
- package/dist/scanner/metrics.js +23 -0
- package/dist/scanner/ml-predictor.js +43 -0
- package/package.json +10 -9
|
Binary file
|
package/dist/scanner/index.js
CHANGED
|
@@ -12,6 +12,8 @@ import { enhanceContextWithGroq, triageFindingsWithGroq, resolveGroqApiKey, } fr
|
|
|
12
12
|
import { computeScores, computeVerdict } from "../report/verdict.js";
|
|
13
13
|
import { renderMarkdown } from "../report/markdown.js";
|
|
14
14
|
import { mapWithProgress } from "./progress.js";
|
|
15
|
+
import { extractMetrics } from "./metrics.js";
|
|
16
|
+
import { initPredictor, predictDefect } from "./ml-predictor.js";
|
|
15
17
|
export { formatScanPath } from "./progress.js";
|
|
16
18
|
function isJsLike(rel) {
|
|
17
19
|
return /\.(tsx?|jsx?|mjs|cjs)$/i.test(rel);
|
|
@@ -45,6 +47,8 @@ export async function scan(root, options = {}) {
|
|
|
45
47
|
};
|
|
46
48
|
let policySignalsChecked = 0;
|
|
47
49
|
let policyPagesFound = 0;
|
|
50
|
+
onProgress?.({ type: "phase", label: "Initializing AI Defect Predictor…" });
|
|
51
|
+
await initPredictor();
|
|
48
52
|
await mapWithProgress(files, 6, async (abs) => {
|
|
49
53
|
let src;
|
|
50
54
|
try {
|
|
@@ -66,6 +70,23 @@ export async function scan(root, options = {}) {
|
|
|
66
70
|
if (isMobileSource(rel) || (isJsLike(rel) && /react-native|expo|@react-native/.test(src))) {
|
|
67
71
|
rawFindings.push(...runMobileChecks(src, rel));
|
|
68
72
|
}
|
|
73
|
+
if (isJsLike(rel) || isMobileSource(rel)) {
|
|
74
|
+
const metrics = extractMetrics(src);
|
|
75
|
+
const isDefective = await predictDefect(metrics);
|
|
76
|
+
if (isDefective) {
|
|
77
|
+
rawFindings.push({
|
|
78
|
+
id: "ai-defect-prediction",
|
|
79
|
+
category: "Code Quality",
|
|
80
|
+
severity: "high",
|
|
81
|
+
file: rel,
|
|
82
|
+
line: 1,
|
|
83
|
+
message: "AI Defect Prediction: High Risk",
|
|
84
|
+
why: `The ML model predicts a high probability of defects in this file based on its code metrics (LOC: ${metrics.loc}, Max CC: ${metrics.max_cc.toFixed(1)}).`,
|
|
85
|
+
fix: "Consider refactoring this file to reduce complexity, decouple logic, and add unit tests.",
|
|
86
|
+
aiPrompt: "Refactor this file to reduce cyclomatic complexity and decouple logic into smaller, testable units.",
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
69
90
|
bumpChecks(PER_FILE_CHECK_COUNT);
|
|
70
91
|
}, (_abs, completed, total) => {
|
|
71
92
|
const rel = path.relative(root, _abs);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function extractMetrics(code) {
|
|
2
|
+
const loc = code.split("\n").length;
|
|
3
|
+
// Weighted methods per class / function count
|
|
4
|
+
const wmcMatches = code.match(/\b(function|func|def)\b|=>/g);
|
|
5
|
+
const wmc = wmcMatches ? wmcMatches.length : 1;
|
|
6
|
+
// Number of public methods
|
|
7
|
+
const npmMatches = code.match(/\b(export|public)\b/g);
|
|
8
|
+
const npm = npmMatches ? npmMatches.length : 0;
|
|
9
|
+
// Cyclomatic complexity estimation
|
|
10
|
+
// Count branching operations and keywords
|
|
11
|
+
const ccMatches = code.match(/\b(if|for|while|switch|case|catch)\b|&&|\|\||\?/g);
|
|
12
|
+
const total_cc = (ccMatches ? ccMatches.length : 0) + wmc;
|
|
13
|
+
const avg_cc = total_cc / (wmc || 1);
|
|
14
|
+
// Without AST, max_cc is a heuristic based on avg_cc + some variance
|
|
15
|
+
const max_cc = avg_cc > 1 ? avg_cc * 1.5 : 1;
|
|
16
|
+
return {
|
|
17
|
+
loc,
|
|
18
|
+
wmc,
|
|
19
|
+
npm,
|
|
20
|
+
max_cc,
|
|
21
|
+
avg_cc
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as ort from "onnxruntime-node";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
let session = null;
|
|
7
|
+
export async function initPredictor() {
|
|
8
|
+
if (session)
|
|
9
|
+
return;
|
|
10
|
+
try {
|
|
11
|
+
// Resolve model path relative to execution
|
|
12
|
+
let modelPath = path.join(process.cwd(), "packages/cli/assets/defect_model.onnx");
|
|
13
|
+
if (!fs.existsSync(modelPath)) {
|
|
14
|
+
// In dist/scanner/ml-predictor.js, __dirname is packages/cli/dist/scanner
|
|
15
|
+
// So we go up two levels to get to packages/cli, then into assets
|
|
16
|
+
modelPath = path.join(__dirname, "../../assets/defect_model.onnx");
|
|
17
|
+
}
|
|
18
|
+
if (fs.existsSync(modelPath)) {
|
|
19
|
+
session = await ort.InferenceSession.create(modelPath);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
console.error("[AI Defect Predictor] Failed to load ONNX model:", e);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export async function predictDefect(metrics) {
|
|
27
|
+
if (!session)
|
|
28
|
+
return false;
|
|
29
|
+
try {
|
|
30
|
+
const input = new Float32Array([metrics.loc, metrics.wmc, metrics.npm, metrics.max_cc, metrics.avg_cc]);
|
|
31
|
+
const tensor = new ort.Tensor("float32", input, [1, 5]);
|
|
32
|
+
const feeds = { float_input: tensor };
|
|
33
|
+
const results = await session.run(feeds);
|
|
34
|
+
const label = results.output_label;
|
|
35
|
+
if (label && label.data && label.data.length > 0) {
|
|
36
|
+
return Number(label.data[0]) > 0;
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "periderm-cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.69",
|
|
4
4
|
"description": "Periderm CLI: A pre-launch checklist to catch user-losing bugs, legal liabilities, revenue leaks, runaway cloud bills and so many more before you ship.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"preferGlobal": true,
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
"files": [
|
|
11
11
|
"dist",
|
|
12
12
|
"README.md",
|
|
13
|
-
"scripts"
|
|
13
|
+
"scripts",
|
|
14
|
+
"assets"
|
|
14
15
|
],
|
|
15
16
|
"keywords": [
|
|
16
17
|
"cli",
|
|
@@ -46,12 +47,6 @@
|
|
|
46
47
|
},
|
|
47
48
|
"author": "Periderm",
|
|
48
49
|
"license": "MIT",
|
|
49
|
-
"scripts": {
|
|
50
|
-
"build": "tsc -p tsconfig.json && chmod +x dist/bin.js",
|
|
51
|
-
"postpublish": "cd ../../ && tsx --env-file=.env scripts/broadcast-update.ts",
|
|
52
|
-
"dev": "tsx src/index.ts",
|
|
53
|
-
"start": "node dist/index.js"
|
|
54
|
-
},
|
|
55
50
|
"dependencies": {
|
|
56
51
|
"@babel/parser": "^7.25.0",
|
|
57
52
|
"@babel/traverse": "^7.25.0",
|
|
@@ -59,6 +54,7 @@
|
|
|
59
54
|
"chalk": "^5.3.0",
|
|
60
55
|
"commander": "^12.1.0",
|
|
61
56
|
"fast-glob": "^3.3.2",
|
|
57
|
+
"onnxruntime-node": "^1.29.0",
|
|
62
58
|
"open": "^10.1.0",
|
|
63
59
|
"ora": "^9.4.1"
|
|
64
60
|
},
|
|
@@ -70,5 +66,10 @@
|
|
|
70
66
|
},
|
|
71
67
|
"engines": {
|
|
72
68
|
"node": ">=18"
|
|
69
|
+
},
|
|
70
|
+
"scripts": {
|
|
71
|
+
"build": "tsc -p tsconfig.json && chmod +x dist/bin.js",
|
|
72
|
+
"dev": "tsx src/index.ts",
|
|
73
|
+
"start": "node dist/index.js"
|
|
73
74
|
}
|
|
74
|
-
}
|
|
75
|
+
}
|