Switch runner to 14-category CWE prompt and strict scoring

- Add sast_v2 prompt covering all BenchmarkPython CWEs
- Accept boolean vulnerable plus CWE and derive category from CWE
- Score flag accuracy separately from strict flag+CWE accuracy
- Report wrong-category true positives explicitly
- Run DeepSeek v2 on all 200 original and obfuscated samples
- Extend LLM and SAST exports with strict correctness and CWE fields
This commit is contained in:
Joren committed 2026-11-10 22:58:36 +01:00
1 parent 033e5d0ea3
commit 26da3173e9
29 files changed
+15498 -1629

No files matched your search

+117 -10
View File
@@ -30,8 +30,8 @@ from pathlib import Path
BASE_DIR = Path(__file__).resolve().parents[1]
DEFAULT_DB = BASE_DIR / "results" / "sast_runs.sqlite3"
DEFAULT_OUTPUT = BASE_DIR / "validation" / "spss"
DEFAULT_ORIGINAL = "deepseek-original"
DEFAULT_OBFUSCATED = "deepseek-v4.1-flash"
DEFAULT_ORIGINAL = "deepseek-v2-original"
DEFAULT_OBFUSCATED = "deepseek-v2-obfuscated"
CATEGORY_CODES = {
"None": 0,
@@ -111,6 +111,10 @@ def outcome_flags(row: sqlite3.Row) -> dict[str, int]:
and category_code(row["category"]) == category_code(row["expected_category"])
)
cwe_correct = int(tp and cwe_num(row["cwe"]) == cwe_num(row["expected_cwe"]))
wrong_category_tp = int(tp and not category_correct)
strict_correct = int(
correct and (expected == 0 or cwe_num(row["cwe"]) == cwe_num(row["expected_cwe"]))
)
return {
"expected_vuln": expected,
"predicted_vuln": predicted,
@@ -121,6 +125,8 @@ def outcome_flags(row: sqlite3.Row) -> dict[str, int]:
"fn": fn,
"category_correct": category_correct,
"cwe_correct": cwe_correct,
"strict_correct": strict_correct,
"wrong_category_tp": wrong_category_tp,
}
@@ -208,6 +214,8 @@ def build_wide(
"orig_fn": of["fn"],
"orig_category_correct": of["category_correct"],
"orig_cwe_correct": of["cwe_correct"],
"orig_strict_correct": of["strict_correct"],
"orig_wrong_category_tp": of["wrong_category_tp"],
"orig_total_tokens": o["total_tokens"] or 0,
"orig_latency_ms": o["latency_ms"] or 0,
"obf_predicted_vuln": bf["predicted_vuln"],
@@ -220,12 +228,26 @@ def build_wide(
"obf_fn": bf["fn"],
"obf_category_correct": bf["category_correct"],
"obf_cwe_correct": bf["cwe_correct"],
"obf_strict_correct": bf["strict_correct"],
"obf_wrong_category_tp": bf["wrong_category_tp"],
"obf_total_tokens": b["total_tokens"] or 0,
"obf_latency_ms": b["latency_ms"] or 0,
"both_correct": int(of["correct_flag"] and bf["correct_flag"]),
"orig_only_correct": int(of["correct_flag"] and not bf["correct_flag"]),
"obf_only_correct": int(not of["correct_flag"] and bf["correct_flag"]),
"both_wrong": int(not of["correct_flag"] and not bf["correct_flag"]),
"both_strict_correct": int(
of["strict_correct"] and bf["strict_correct"]
),
"orig_only_strict_correct": int(
of["strict_correct"] and not bf["strict_correct"]
),
"obf_only_strict_correct": int(
not of["strict_correct"] and bf["strict_correct"]
),
"both_strict_wrong": int(
not of["strict_correct"] and not bf["strict_correct"]
),
}
)
return rows
@@ -252,8 +274,17 @@ def summarize(
if precision + recall
else 0.0
)
fpr = fp / (fp + tn) if fp + tn else 0.0
tnr = tn / (tn + fp) if tn + fp else 0.0
balanced_accuracy = (recall + tnr) / 2
mcc_denominator = (
(tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)
) ** 0.5
mcc = (tp * tn - fp * fn) / mcc_denominator if mcc_denominator else 0.0
cat_correct = sum(int(row["category_correct"]) for row in selected)
cwe_correct = sum(int(row["cwe_correct"]) for row in selected)
strict_correct = sum(int(row["strict_correct"]) for row in selected)
wrong_category_tp = sum(int(row["wrong_category_tp"]) for row in selected)
in_scope_tokens = sum(int(row["total_tokens"]) for row in selected)
final_tokens = sum(int(row["total_tokens"]) for row in all_selected)
mean_tokens = in_scope_tokens / n if n else 0.0
@@ -274,11 +305,17 @@ def summarize(
"tn": tn,
"fn": fn,
"accuracy": round(accuracy, 6),
"strict_accuracy": round(strict_correct / n, 6) if n else 0.0,
"precision": round(precision, 6),
"recall": round(recall, 6),
"fpr": round(fpr, 6),
"tnr": round(tnr, 6),
"balanced_accuracy": round(balanced_accuracy, 6),
"f1": round(f1, 6),
"mcc": round(mcc, 6),
"category_accuracy_tp": round(cat_correct / tp, 6) if tp else 0.0,
"cwe_accuracy_tp": round(cwe_correct / tp, 6) if tp else 0.0,
"wrong_category_tp": wrong_category_tp,
"total_tokens_in_scope_final": in_scope_tokens,
"total_tokens_all_final": final_tokens,
"total_tokens_all_attempts": run_totals["total_tokens"],
@@ -318,6 +355,8 @@ def write_codebook(path: Path) -> None:
("predicted_category", "Predicted category label", "string", "see category codes"),
("predicted_category_code", "Predicted category code", "numeric", "same as expected_category_code"),
("category_correct", "Predicted category matches ground truth among TP", "numeric", "0=no; 1=yes"),
("strict_correct", "Flag and CWE both match ground truth", "numeric", "0=no; 1=yes"),
("wrong_category_tp", "Vulnerable sample flagged vulnerable but wrong category", "numeric", "0=no; 1=yes"),
("expected_cwe", "Ground-truth CWE label", "string", "CWE-N or None"),
("expected_cwe_num", "Ground-truth CWE number", "numeric", "0=None"),
("predicted_cwe", "Predicted CWE label", "string", "CWE-N or None"),
@@ -374,8 +413,17 @@ def write_codebook_md(path: Path) -> None:
## Notes
- `in_scope = 1` restricts analyses to the nine categories targeted by the prompt.
- `in_scope = 1` restricts analyses to the categories targeted by the prompt.
The v2 prompt covers all fourteen BenchmarkPython categories, so all 200
samples are in scope for v2 runs. Older v1 runs retain `in_scope = 0` for the
five categories that prompt omitted.
- `correct_flag` is the agreement between `predicted_vuln` and `expected_vuln`.
- `strict_correct` requires the flag to be correct and, for vulnerable samples,
the predicted CWE to match the benchmark CWE. Safe samples are strict-correct
when they are correctly rejected.
- `wrong_category_tp` counts vulnerable samples that were flagged vulnerable but
assigned the wrong CWE/category. These are detection true positives but not
correct classifications.
- `category_correct` and `cwe_correct` are only meaningful for true positives
(`expected_vuln = 1` and `predicted_vuln = 1`).
- The long file is the right shape for mixed models or condition-by-category
@@ -389,8 +437,9 @@ def write_codebook_md(path: Path) -> None:
- `n_all` / `n_in_scope`
- `tp`, `fp`, `tn`, `fn`
- `accuracy`, `precision`, `recall`, `f1`
- `category_accuracy_tp`, `cwe_accuracy_tp`
- `accuracy`, `strict_accuracy`, `precision`, `recall`, `fpr`, `tnr`,
`balanced_accuracy`, `f1`, `mcc`
- `category_accuracy_tp`, `cwe_accuracy_tp`, `wrong_category_tp`
- `total_tokens_in_scope_final` — final-attempt tokens for in-scope samples
- `total_tokens_all_final` — final-attempt tokens for all samples
- `total_tokens_all_attempts` — includes retries and repair calls
@@ -428,6 +477,8 @@ def spss_import_syntax(long_fields: list[tuple[str, str]]) -> str:
"predicted_cwe_num": "Predicted CWE number",
"category_correct": "Category correct among TP",
"cwe_correct": "CWE correct among TP",
"strict_correct": "Flag and CWE both correct",
"wrong_category_tp": "Vulnerable but wrong category (TP)",
"parse_ok": "Valid JSON response",
"prompt_tokens": "Prompt tokens",
"completion_tokens": "Completion tokens",
@@ -463,7 +514,8 @@ VALUE LABELS
condition 0 'Original' 1 'Obfuscated v2'
in_scope 0 'Out of scope' 1 'In scope'
expected_vuln predicted_vuln correct_flag parse_ok 0 'No' 1 'Yes'
tp fp tn fn category_correct cwe_correct 0 'No' 1 'Yes'
tp fp tn fn category_correct cwe_correct strict_correct wrong_category_tp
0 'No' 1 'Yes'
expected_category_code predicted_category_code
0 'None' 1 'pathtraver' 2 'sqli' 3 'xpathi' 4 'xxe' 5 'cmdi'
6 'deserialization' 7 'xss' 8 'weakrand' 9 'trustbound'
@@ -476,7 +528,8 @@ EXECUTE.
* SELECT IF (in_scope = 1).
* EXECUTE.
FREQUENCIES VARIABLES=condition in_scope expected_vuln predicted_vuln correct_flag.
FREQUENCIES VARIABLES=condition in_scope expected_vuln predicted_vuln
correct_flag strict_correct wrong_category_tp.
SORT CASES BY condition.
SPLIT FILE LAYERED BY condition.
@@ -523,6 +576,8 @@ VARIABLE LABELS
orig_tp 'Original TP' orig_fp 'Original FP' orig_tn 'Original TN' orig_fn 'Original FN'
orig_category_correct 'Original category correct among TP'
orig_cwe_correct 'Original CWE correct among TP'
orig_strict_correct 'Original flag and CWE correct'
orig_wrong_category_tp 'Original vulnerable but wrong category'
orig_total_tokens 'Original total tokens'
orig_latency_ms 'Original latency (ms)'
obf_predicted_vuln 'Obfuscated predicted vulnerability'
@@ -532,12 +587,18 @@ VARIABLE LABELS
obf_tp 'Obfuscated TP' obf_fp 'Obfuscated FP' obf_tn 'Obfuscated TN' obf_fn 'Obfuscated FN'
obf_category_correct 'Obfuscated category correct among TP'
obf_cwe_correct 'Obfuscated CWE correct among TP'
obf_strict_correct 'Obfuscated flag and CWE correct'
obf_wrong_category_tp 'Obfuscated vulnerable but wrong category'
obf_total_tokens 'Obfuscated total tokens'
obf_latency_ms 'Obfuscated latency (ms)'
both_correct 'Both correct'
orig_only_correct 'Original only correct'
obf_only_correct 'Obfuscated only correct'
both_wrong 'Both wrong'
both_strict_correct 'Both strict-correct'
orig_only_strict_correct 'Original only strict-correct'
obf_only_strict_correct 'Obfuscated only strict-correct'
both_strict_wrong 'Both strict-wrong'
.
VALUE LABELS
@@ -546,7 +607,11 @@ VALUE LABELS
obf_tp obf_fp obf_tn obf_fn
orig_category_correct obf_category_correct
orig_cwe_correct obf_cwe_correct
orig_strict_correct obf_strict_correct
orig_wrong_category_tp obf_wrong_category_tp
both_correct orig_only_correct obf_only_correct both_wrong
both_strict_correct orig_only_strict_correct
obf_only_strict_correct both_strict_wrong
0 'No' 1 'Yes'
expected_category_code orig_category_code obf_category_code
0 'None' 1 'pathtraver' 2 'sqli' 3 'xpathi' 4 'xxe' 5 'cmdi'
@@ -556,15 +621,20 @@ VALUE LABELS
.
EXECUTE.
* Restrict paired analyses to the 157 in-scope samples.
* Restrict paired analyses to in-scope samples (all 200 for the v2 prompt).
SELECT IF (in_scope = 1).
EXECUTE.
* McNemar test on paired correctness.
* McNemar test on paired detection correctness.
NPAR TESTS
/MCNEMAR=orig_correct WITH obf_correct
/STATISTICS DESCRIPTIVES.
* McNemar test on paired strict correctness (flag + CWE).
NPAR TESTS
/MCNEMAR=orig_strict_correct WITH obf_strict_correct
/STATISTICS DESCRIPTIVES.
* Paired correctness crosstab.
CROSSTABS
/TABLES=orig_correct BY obf_correct
@@ -603,11 +673,17 @@ GET DATA
tn F5.0
fn F5.0
accuracy F8.6
strict_accuracy F8.6
precision F8.6
recall F8.6
fpr F8.6
tnr F8.6
balanced_accuracy F8.6
f1 F8.6
mcc F8.6
category_accuracy_tp F8.6
cwe_accuracy_tp F8.6
wrong_category_tp F5.0
total_tokens_in_scope_final F10.0
total_tokens_all_final F10.0
total_tokens_all_attempts F10.0
@@ -626,9 +702,13 @@ VARIABLE LABELS
n_in_scope 'Samples in target scope'
tp 'True positives' fp 'False positives'
tn 'True negatives' fn 'False negatives'
accuracy 'Flag accuracy' precision 'Precision' recall 'Recall' f1 'F1'
accuracy 'Flag accuracy' strict_accuracy 'Strict accuracy (flag + CWE)'
precision 'Precision' recall 'Recall'
fpr 'False-positive rate' tnr 'True-negative rate'
balanced_accuracy 'Balanced accuracy' f1 'F1' mcc 'Matthews correlation'
category_accuracy_tp 'Category accuracy among TP'
cwe_accuracy_tp 'CWE accuracy among TP'
wrong_category_tp 'Wrong-category true positives'
total_tokens_in_scope_final 'Final-attempt tokens, in-scope'
total_tokens_all_final 'Final-attempt tokens, all samples'
total_tokens_all_attempts 'Tokens across all attempts'
@@ -712,6 +792,8 @@ def main() -> int:
("predicted_cwe_num", "F5.0"),
("category_correct", "F1.0"),
("cwe_correct", "F1.0"),
("strict_correct", "F1.0"),
("wrong_category_tp", "F1.0"),
("parse_ok", "F1.0"),
("prompt_tokens", "F8.0"),
("completion_tokens", "F8.0"),
@@ -745,6 +827,8 @@ def main() -> int:
("orig_fn", "F1.0"),
("orig_category_correct", "F1.0"),
("orig_cwe_correct", "F1.0"),
("orig_strict_correct", "F1.0"),
("orig_wrong_category_tp", "F1.0"),
("orig_total_tokens", "F8.0"),
("orig_latency_ms", "F10.0"),
("obf_predicted_vuln", "F1.0"),
@@ -757,12 +841,18 @@ def main() -> int:
("obf_fn", "F1.0"),
("obf_category_correct", "F1.0"),
("obf_cwe_correct", "F1.0"),
("obf_strict_correct", "F1.0"),
("obf_wrong_category_tp", "F1.0"),
("obf_total_tokens", "F8.0"),
("obf_latency_ms", "F10.0"),
("both_correct", "F1.0"),
("orig_only_correct", "F1.0"),
("obf_only_correct", "F1.0"),
("both_wrong", "F1.0"),
("both_strict_correct", "F1.0"),
("orig_only_strict_correct", "F1.0"),
("obf_only_strict_correct", "F1.0"),
("both_strict_wrong", "F1.0"),
]
write_csv(
output_dir / "sast_wide.csv",
@@ -785,6 +875,12 @@ def main() -> int:
obf_only = sum(int(row["obf_only_correct"]) for row in in_scope_wide)
both_correct = sum(int(row["both_correct"]) for row in in_scope_wide)
both_wrong = sum(int(row["both_wrong"]) for row in in_scope_wide)
orig_only_strict = sum(
int(row["orig_only_strict_correct"]) for row in in_scope_wide
)
obf_only_strict = sum(
int(row["obf_only_strict_correct"]) for row in in_scope_wide
)
paired_rows = [
{
"n_in_scope": len(in_scope_wide),
@@ -793,6 +889,17 @@ def main() -> int:
"obf_only_correct": obf_only,
"both_wrong": both_wrong,
"mcnemar_exact_p": round(mcnemar_exact(orig_only, obf_only), 6),
"both_strict_correct": sum(
int(row["both_strict_correct"]) for row in in_scope_wide
),
"orig_only_strict_correct": orig_only_strict,
"obf_only_strict_correct": obf_only_strict,
"both_strict_wrong": sum(
int(row["both_strict_wrong"]) for row in in_scope_wide
),
"strict_mcnemar_exact_p": round(
mcnemar_exact(orig_only_strict, obf_only_strict), 6
),
}
]
write_csv(