--- license: other library_name: scikit-learn pipeline_tag: tabular-classification tags: - cybersecurity - vulnerability-detection - defect-prediction - random-forest - scikit-learn - joblib - nasa-promise --- # Agent 1 — Structural Risk Random Forest Random Forest that estimates defect risk from 21 NASA/PROMISE code metrics. Part of a three-agent DevSecOps auditor (metrics → transformer → gatekeeper). ## Model details | Field | Value | |---|---| | Version | 1.0.0 (initial release) | | Artifact | `model.joblib` (bundle: `model`, `raw_cols`, `feature_cols`, `threshold`) | | Task | Binary tabular classification (defect / no defect) | | Threshold | **0.3183** (tuned on training CV with F2) | | Training data | NASA/PROMISE PC1 (`pc1.csv`) | | Dataset version | Not pinned — static PC1 snapshot, no revision hash recorded | | Seed | 42 | `metadata.json` mirrors the dataset, threshold, and best params. ## Input features 21 numeric metrics: ``` loc, vg, evg, ivg, n, v, l, d, i, e, b, t, locode, locomment, loccodeandcomment, loblank, uniq_op, uniq_opnd, total_op, total_opnd, branchcount ``` Five derived features are added before prediction. Final `feature_cols` order (26 columns, must match exactly): ``` loc, vg, evg, ivg, n, v, l, d, i, e, b, t, locode, locomment, loccodeandcomment, loblank, uniq_op, uniq_opnd, total_op, total_opnd, branchcount, complexity_density, effort_per_loc, comment_ratio, branch_density, operand_reuse ``` ## Preprocessing - All 21 inputs must be numeric; no missing values (nothing is imputed). - Derive ratios with `+1` denominators: ``` complexity_density = vg / (loc + 1) effort_per_loc = e / (loc + 1) comment_ratio = locomment / (loc + 1) branch_density = branchcount / (loc + 1) operand_reuse = total_opnd / (uniq_opnd + 1) ``` - Select `b["feature_cols"]` in the order above. ## Output Raw: `predict_proba(X)[0, 1]` — defect risk in `[0, 1]`, uncalibrated. `high_risk = score >= 0.3183`. Reference wrapper output: ```json {"struct_risk_score": 0.5105, "high_risk": true, "threshold": 0.3183, "status": "ok", "reason": ""} ``` ## Usage ```python import joblib import pandas as pd b = joblib.load("model.joblib") df = pd.DataFrame([metrics])[b["raw_cols"]].apply(pd.to_numeric) df["complexity_density"] = df["vg"] / (df["loc"] + 1) df["effort_per_loc"] = df["e"] / (df["loc"] + 1) df["comment_ratio"] = df["locomment"] / (df["loc"] + 1) df["branch_density"] = df["branchcount"] / (df["loc"] + 1) df["operand_reuse"] = df["total_opnd"] / (df["uniq_opnd"] + 1) X = df[b["feature_cols"]] score = b["model"].predict_proba(X)[0, 1] high_risk = score >= b["threshold"] ``` Trained on NASA PROMISE PC1. Metrics from other languages or domains are out-of-distribution. ## License Not specified. Update the `license` field before publishing.