Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.
Python agentic-aiai-securityalert-triageblue-teamcybersecuritydetection-engineeringincident-responselanggraphllm-agentsmcpmitre-attackmodel-context-protocolpurple-teamsecurity-operationsself-hostedsiemsoarsocthreat-detectionthreat-intelligence
feat(evals): playbook completion rate gate (WS-C3)
Beenu Arora91cfa589f904
diff --git a/.github/PULL_REQUEST_TEMPLATE/detection_rule.md b/.github/PULL_REQUEST_TEMPLATE/detection_rule.mdindex 11a91c0e1..364fad3e9 100644--- a/.github/PULL_REQUEST_TEMPLATE/detection_rule.md+++ b/.github/PULL_REQUEST_TEMPLATE/detection_rule.md@@ -41,6 +41,7 @@- [ ] Rule validates with `python3 scripts/validate_detections.py`- [ ] Tested against sample log data (describe below)- [ ] False-positive scenarios documented above+- [ ] Re-graded the eval harness with `python3 scripts/run_evals.py` and confirmed no axis regressed (paste before/after summary blocks below if any axis moved)<details><summary>Test evidence</summary>@@ -50,6 +51,18 @@</details>+<details>+<summary>Eval harness deltas (only required if an axis moved)</summary>++```text+# scripts/run_evals.py — before++# scripts/run_evals.py — after++```++</details>+### Checklist- [ ] Rule YAML follows the schema in `detections/` (id, name, description, version, severity, tags, category, log_source, detection, false_positives, playbook, enabled, author, created, modified)diff --git a/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md b/.github/PULL_REQUEST_TEMPLATE/pull_request_template.mdindex 61627e191..1e5b18deb 100644--- a/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md+++ b/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md@@ -51,3 +51,37 @@ Closes #<!-- issue number -->- [ ] I have updated documentation as needed (README, CHANGELOG, CONTRIBUTING)- [ ] My changes do not introduce new linter warnings- [ ] I have checked for sensitive data / credentials in my diff++## Eval harness (required for substrate / playbook / detection changes)++> **Required if this PR touches:** `services/agents/`, `services/api/app/orchestrator/`,+> `playbooks/packs/`, `detections/`, `services/agents/tests/eval_data/`,+> `scripts/generate_eval_incidents.py`, `scripts/run_evals.py`, or any prompt /+> RAG corpus / response template under those trees.+>+> Run `python3 scripts/run_evals.py --json --out /tmp/report.json` before and+> after your change and paste both summary blocks below. Any axis that+> regresses must be called out explicitly. Playbook-touching PRs must include+> the `playbook_completion_rate` block — orphan playbooks / templates fail CI.++<details>+<summary><strong>Before</strong> (paste <code>scripts/run_evals.py</code> output on base branch)</summary>++```text++```++</details>++<details>+<summary><strong>After</strong> (paste <code>scripts/run_evals.py</code> output on this branch)</summary>++```text++```++</details>++- [ ] Not applicable — this PR does not touch substrate, playbooks, detections, or eval inputs+- [ ] No axis regressed+- [ ] An axis regressed; rationale and follow-up are described in the Summary abovediff --git a/apps/docs/docs/benchmark.md b/apps/docs/docs/benchmark.mdindex 2dad4e85a..964724de5 100644--- a/apps/docs/docs/benchmark.md+++ b/apps/docs/docs/benchmark.md@@ -1,7 +1,7 @@---sidebar_position: 4title: Public Eval Harness-description: AiSOC's open, deterministic regression harness. 200 synthetic incidents drawn from 55 distinct templates with backing telemetry (Sysmon, M365, CloudTrail, Azure sign-in, Linux auditd, …). Per-case and per-template CI gates over the substrate. Honest about what it measures — and what it doesn't.+description: AiSOC's open, deterministic regression harness. 200 synthetic incidents drawn from 55 distinct templates with backing telemetry (Sysmon, M365, CloudTrail, Azure sign-in, Linux auditd, …). Per-case and per-template CI gates over the substrate, plus operational coverage gates (synthetic telemetry corpus, playbook completion rate). Honest about what it measures — and what it doesn't.---# AiSOC Public Eval Harness@@ -66,12 +66,13 @@ the numbers.## Latest results-The four numbers below are produced by `scripts/run_evals.py`. The MITRE,+The numbers below are produced by `scripts/run_evals.py`. The MITRE,completeness, and response-quality suites run against the 200-incidentsynthetic dataset; the alert-reduction suite runs against a separately-generated 1 000-alert noisy stream. The whole run takes roughly 25 ms total-(no LLM calls, no DB) so it's cheap enough to gate every PR targeting `main`-or `develop`.+generated 1 000-alert noisy stream; the playbook-completion suite runs+against the same 200-incident corpus and the v1 playbook pack. The whole run+takes roughly 35 ms total (no LLM calls, no DB) so it's cheap enough to gate+every PR targeting `main` or `develop`.| Suite | Metric | Per-case | Per-template macro | Target | What it checks ||--------------------------------|-------------------------|------------|------------------------|---------|----------------|@@ -79,6 +80,7 @@ or `develop`.| MITRE ATT&CK tactic accuracy | accuracy | 97.0 % | 96.4 % (n=55) | ≥ 80 % | Substrate self-consistency — keyword extractor vs. dataset written for it || Investigation completeness | mean keyword coverage | 94.2 % | 94.3 % (n=55) | ≥ 85 % | Substrate self-consistency — report template wraps the description; judge finds keywords from the description || Response-plan quality | mean rubric score | 1.000 | 1.000 (n=55) | ≥ 0.80 | Substrate self-consistency — synthesizer embeds the keywords the rubric checks for |+| Playbook completion rate | completion rate | 50.5 % | 100 % H/C (mapped) | ≥ 50 % | Operational coverage gate — every incident in scope has a matching playbook with aligned response action; orphan playbooks/templates fail CI |> The synthetic telemetry suite is a **schema/coverage gate**, not a scoring> suite, so it does not appear in the table. It checks that every incident has@@ -88,6 +90,15 @@ or `develop`.> It currently passes against 361 events spanning 14 distinct log sources> wired to all 200 incidents.+> The playbook completion rate is also an **operational coverage gate** —+> it grades the v1 playbook pack itself, not the substrate's per-incident+> output. The headline 50.5 % overall figure is the share of synthetic+> incidents whose category, severity, and response action are matched by at+> least one playbook in `playbooks/packs/v1/`. The 100 % high+critical figure+> is over the *mapped* subset (incidents whose template the pack claims to+> cover) — the raw H/C figure is reported in JSON but not gated, because the+> dataset includes documented v1 coverage gaps. See [section 7](#7-playbook-completion-rate).+These numbers move with the codebase. The current snapshot lives at[`eval-results/eval/results/latest.json`](https://github.com/beenuar/AiSOC/blob/eval-results/eval/results/latest.json).@@ -137,6 +148,10 @@ That's it. No Docker, no API key, no GPU, no LLM. Expected output:per-template macro 0.943 (target >= 0.80, n=55 templates) [PASS][PASS] response_quality mean_rubric_score 1.000 (target >= 0.80)per-template macro 1.000 (target >= 0.75, n=55 templates) [PASS]+ [PASS] playbook_completion_rate completion_rate 0.505 (target >= 0.50)+ high+critical (mapped) 1.000 (target >= 0.95) [PASS]+ action alignment 0.939 (target >= 0.85) [PASS]+ orphan playbooks / templates 0 / 0 [PASS]------------------------------------------------------------------------------Synthetic telemetry: 361 events across 14 sources,200 incidents wired up@@ -356,6 +371,50 @@ To regenerate the adversary dataset:python3 scripts/generate_adversary_incidents.py```+### 7. Playbook completion rate — `Operational coverage gate` {#7-playbook-completion-rate}++**Source:** [`services/agents/tests/test_playbook_completion_rate.py`](https://github.com/beenuar/AiSOC/blob/main/services/agents/tests/test_playbook_completion_rate.py)+· **Dataset:** [`eval_data/synthetic_incidents.json`](https://github.com/beenuar/AiSOC/blob/main/services/agents/tests/eval_data/synthetic_incidents.json)+· **Pack under test:** [`playbooks/packs/v1/`](https://github.com/beenuar/AiSOC/tree/main/playbooks/packs/v1)++This suite is **not** a quality measurement of playbook execution — it is a+coverage gate over the v1 playbook pack itself. For every one of the 200+synthetic incidents, the harness asks: *does at least one playbook in the+pack match this incident's category, severity, and expected response action?*+That answer is then rolled up into a small set of CI-enforced sub-gates so a+PR that touches `playbooks/packs/` cannot silently shrink coverage.++The metric reports five things; the suite passes only when **all** of them+pass:++| Sub-gate | Floor / value | What it catches |+|----------|---------------|-----------------|+| Overall completion rate | ≥ 0.50 | Wholesale regressions (e.g. a deletion or category rename that drops dozens of incidents into the "no playbook" bucket). The floor is intentionally honest about v1's coverage, not aspirational. |+| High+critical completion rate (**mapped**) | ≥ 0.95 | Among incidents whose templates the pack *claims* to cover, every severe one must have a containment playbook. We measure this over the **mapped** subset to avoid forcing a pass by inflating coverage with mismatched playbooks. The raw rate (over all H/C templates) is reported for transparency but not gated. |+| Action alignment rate | ≥ 0.85 | Among matched incidents, the playbook's first-line steps must align with the dataset's `response_class` (e.g. `block` / `quarantine` / `disable_user` / `reset_credentials`). Stops "any matching playbook is good enough" drift. |+| Orphan playbooks | exactly 0 (with allowlist) | Playbooks that match zero incidents in the corpus. An explicit allowlist (`_PLAYBOOKS_NOT_IN_BENCHMARK_DATASET`) documents playbooks that are deliberately off-corpus (e.g. removable-media exfil, volumetric DDoS) so adding new orphans is loud. |+| Orphan templates | exactly 0 (with allowlist) | Mapped templates with zero playbook hits. Templates whose category isn't in v1 scope are explicitly listed in `_TEMPLATES_WITHOUT_PACK_COVERAGE`; the gate only triggers when a *previously-mapped* template loses coverage. |++The mapped-vs-raw distinction matters: the synthetic dataset includes ~22+endpoint-compromise / persistence / defense-evasion templates that are+documented v1 coverage gaps. Gating on raw high+critical coverage would+either force dishonest coverage (mapping a credential-stuffing playbook to+ransomware just to clear the floor) or punish CI for known scope decisions.+Gating on **mapped** coverage instead lets v1's scope evolve cleanly: as new+playbooks land and templates move from `_TEMPLATES_WITHOUT_PACK_COVERAGE`+into `_TEMPLATE_CATEGORIES`, the mapped denominator grows with them.++What this suite **does not** do: it does not execute playbooks, time their+steps, or measure their reliability against live telemetry. Step execution+is covered by the playbook engine's own unit tests; this gate is the+*inventory check* that runs before every push.++The full per-suite payload is emitted as JSON via+`scripts/run_evals.py --json --out report.json` under+`suites.playbook_completion_rate.details` — including per-severity breakdown,+per-category coverage, and the orphan lists — so the same diff that adds a+playbook can be reviewed alongside its coverage delta.+## Community benchmark scoreboardThe dataset and the harness are MIT-licensed and fully reproducible. Any third@@ -418,6 +477,12 @@ A few caveats:consistent. They will fail if it is not. The per-template macro adds anon-tautological dimension on top: a single broken template stops beinghidden behind 199 working duplicates.+- **The playbook completion gate is a coverage check, not a quality check.**+ It verifies that every in-scope incident has a playbook with the right+ category and response action — it does not execute the playbook, time its+ steps, or measure its reliability against live telemetry. Step execution is+ covered by the playbook engine's own unit tests in+ `services/agents/tests/test_playbook_engine.py`.- **"Public eval harness" means this harness, not a third-party leaderboard.**These numbers are reproducible by anyone with `python3`. They are notcomparable to MITRE Engenuity, MLPerf, or any other external evaluator.diff --git a/scripts/run_evals.py b/scripts/run_evals.pyindex 8f6c2ac94..15f070bae 100755--- a/scripts/run_evals.py+++ b/scripts/run_evals.py@@ -12,6 +12,10 @@ Suites:4. Response-plan quality (services/agents/tests/test_response_quality.py)5. Hunt corpus coverage (services/agents/tests/test_hunt_corpus.py)6. AI-vs-AI adversary degradation (services/agents/tests/test_adversary_eval.py)+ 7. Confidence calibration (services/agents/tests/test_confidence_calibration.py)+ 8. Memory recall fidelity (services/agents/tests/test_memory_recall.py)+ 9. Override accuracy (services/agents/tests/test_override_accuracy.py)+ 10. Playbook completion rate (services/agents/tests/test_playbook_completion_rate.py)Each substrate suite reports two metrics:@@ -90,6 +94,12 @@ from tests.test_override_accuracy import ( # type: ignoreOVERRIDE_ACCURACY_FLOOR as _OVERRIDE_FLOOR,run_evaluation as _run_override_accuracy_eval,)+from tests.test_playbook_completion_rate import ( # type: ignore+ OVERALL_COMPLETION_FLOOR as _PLAYBOOK_OVERALL_FLOOR,+ HIGH_CRIT_MAPPED_FLOOR as _PLAYBOOK_HIGH_CRIT_FLOOR,+ ACTION_ALIGNMENT_FLOOR as _PLAYBOOK_ALIGN_FLOOR,+ evaluate_playbook_completion,+)# Per-suite floors (must match what tests assert)@@ -105,6 +115,11 @@ _TARGETS = {"confidence_calibration": _CALIB_BRIER_INV,"memory_recall": _RECALL_FLOOR,"override_accuracy": _OVERRIDE_FLOOR,+ # Playbook completion gate (WS-C3): we surface the *overall* coverage+ # rate as the headline metric but the pass condition combines three+ # sub-gates (overall, mapped high+critical, action alignment, no+ # orphan playbooks/templates). See _run_playbook_completion().+ "playbook_completion_rate": _PLAYBOOK_OVERALL_FLOOR,}# Per-template macro floors (kept slightly below per-case floors because each@@ -430,6 +445,84 @@ def _run_override_accuracy() -> dict:}+def _run_playbook_completion() -> dict:+ """WS-C3 gate: playbook eval-harness validation.++ Measures playbook coverage against the deterministic 200-incident+ benchmark dataset. Reports the *overall* completion rate as the headline+ metric for diff-friendly trend tracking, but the suite passes only when+ *all* sub-gates pass:++ * overall completion rate ≥ ``OVERALL_COMPLETION_FLOOR`` —+ catches accidental playbook deletions / wholesale regressions;+ * high+critical completion rate over *mapped* templates ≥+ ``HIGH_CRIT_MAPPED_FLOOR`` — every severe incident the pack claims+ to cover must have a containment playbook;+ * action alignment rate ≥ ``ACTION_ALIGNMENT_FLOOR`` — among matched+ incidents, the playbook's first-line steps align with the dataset's+ ``response_class`` (block / quarantine / disable / etc.);+ * no orphan playbooks (firing on zero incidents, except the documented+ allowlist of medium-severity / off-corpus playbooks);+ * no orphan templates (mapped templates with zero playbook hits).++ The high+critical gate intentionally measures *mapped* coverage rather+ than raw severity coverage — the dataset includes ~22 endpoint-compromise+ / persistence / defense-evasion templates that are documented v1 coverage+ gaps, and gating on them would either force inflating coverage with+ mismatched playbooks or punish CI for known scope decisions.+ """+ t0 = time.perf_counter()+ res = evaluate_playbook_completion(keep_per_incident=True)+ dur = (time.perf_counter() - t0) * 1000++ per_incident = res.per_incident or []+ hc_rate, hc_covered, hc_total = res.severity_completion_rate_mapped(+ ("high", "critical"), per_incident+ )+ hc_raw_rate = res.severity_completion_rate(("high", "critical"))+ overall_pass = res.completion_rate >= _PLAYBOOK_OVERALL_FLOOR+ high_crit_pass = hc_rate >= _PLAYBOOK_HIGH_CRIT_FLOOR+ align_pass = res.action_alignment_rate >= _PLAYBOOK_ALIGN_FLOOR+ no_orphan_playbooks = not res.orphan_playbooks+ no_orphan_templates = not res.orphan_templates++ return {+ "metric": "completion_rate",+ "value": round(res.completion_rate, 4),+ "target": _PLAYBOOK_OVERALL_FLOOR,+ "passed": (+ overall_pass+ and high_crit_pass+ and align_pass+ and no_orphan_playbooks+ and no_orphan_templates+ ),+ "duration_ms": round(dur, 1),+ "details": {+ "incidents": res.incidents,+ "covered": res.covered,+ "aligned": res.aligned,+ "overall_completion_rate": round(res.completion_rate, 4),+ "overall_completion_pass": overall_pass,+ "high_critical_completion_rate_mapped": round(hc_rate, 4),+ "high_critical_completion_rate_raw": round(hc_raw_rate, 4),+ "high_critical_covered_mapped": hc_covered,+ "high_critical_total_mapped": hc_total,+ "high_critical_floor": _PLAYBOOK_HIGH_CRIT_FLOOR,+ "high_critical_pass": high_crit_pass,+ "action_alignment_rate": round(res.action_alignment_rate, 4),+ "action_alignment_floor": _PLAYBOOK_ALIGN_FLOOR,+ "action_alignment_pass": align_pass,+ "per_severity": res.per_severity,+ "per_category": res.per_category,+ "orphan_templates": res.orphan_templates,+ "orphan_playbooks": res.orphan_playbooks,+ "no_orphan_playbooks": no_orphan_playbooks,+ "no_orphan_templates": no_orphan_templates,+ },+ }++def _summarise_telemetry() -> dict:"""Summarise the synthetic-telemetry corpus produced alongside the dataset.@@ -510,6 +603,7 @@ def main() -> None:"confidence_calibration": _run_confidence_calibration(),"memory_recall": _run_memory_recall(),"override_accuracy": _run_override_accuracy(),+ "playbook_completion_rate": _run_playbook_completion(),},"telemetry": _summarise_telemetry(),}diff --git a/services/agents/tests/test_playbook_completion_rate.py b/services/agents/tests/test_playbook_completion_rate.pynew file mode 100644index 000000000..e1c6f8ffa--- /dev/null+++ b/services/agents/tests/test_playbook_completion_rate.py@@ -0,0 +1,735 @@+"""+Pillar-1 Evaluation: Playbook Completion Rate (WS-C3)+=====================================================++For every incident in the 200-case synthetic benchmark, ask:++ "Does the shipped playbook pack contain at least one playbook that:+ (a) is tagged with one of the incident's mapped categories,+ (b) has a trigger.severity that includes the incident's severity, and+ (c) declares at least one step whose action aligns with the incident's+ expected ``response_class``?"++If yes, the incident is considered **covered**. The headline metric is++ completion_rate = covered_incidents / total_incidents++Three derived gates run alongside it:++ * **Severity gate** — high+critical incidents must clear a stricter floor+ (0.60) because that is the band where containment playbooks matter most.+ * **Per-category presence gate** — every shipped playbook category+ (account-takeover, bec, cloud-misconfig, …) must cover ≥ 1 incident in+ the dataset. A category that matches *no* incidents is dead weight and a+ regression in the dataset↔pack alignment.+ * **Action-alignment gate** — among the matched incidents, ≥ 0.85 must+ pair with a playbook whose primary step type aligns with the incident's+ response class (isolate_host → ``isolate_host`` step; block_indicator →+ ``block_ip`` step; etc.). This is what stops a "matches by tag only"+ playbook from being scored as a real response.++This is a **substrate self-consistency gate**, not an LLM judgement: the+matcher uses the dataset's deterministic ``template_id`` and the pack's+declarative ``trigger`` + ``tags`` + step-type metadata. CI gates on it so+that:++ * Removing a playbook → coverage drops → CI fails.+ * Adding a broken playbook (bad trigger / no aligned step) → orphan+ playbook gate fails.+ * Adding new playbooks that cover previously-orphaned templates → coverage+ rises (no false negatives because per-template macro is also reported).++What this does **not** prove:++ * It does not assert the playbook actually mitigates the incident at+ runtime (that would need an executor with mocked SOAR APIs — orthogonal,+ tracked separately).+ * It does not enforce per-template coverage for the ~22 endpoint-+ compromise / persistence templates the v1 pack does not yet cover; those+ are explicitly listed in ``_TEMPLATES_WITHOUT_PACK_COVERAGE`` and the+ floor is set so the regression signal is preserved without falsely+ blocking PRs.++Run:+ pytest services/agents/tests/test_playbook_completion_rate.py -v+"""+from __future__ import annotations++import json+import re+import unittest+from collections import defaultdict+from dataclasses import dataclass, field+from pathlib import Path+from typing import Any, Iterable++_TESTS_DIR = Path(__file__).parent+_DATASET_PATH = _TESTS_DIR / "eval_data" / "synthetic_incidents.json"+_REPO_ROOT = _TESTS_DIR.parent.parent.parent+_PACK_ROOT = _REPO_ROOT / "playbooks" / "packs" / "v1"+++# ---------------------------------------------------------------------------+# Floors+# ---------------------------------------------------------------------------++OVERALL_COMPLETION_FLOOR: float = 0.50+# This is the *mapped* high+critical floor: covered/mapped, where "mapped" means+# the template has at least one entry in _TEMPLATE_CATEGORIES. The raw+# severity-only rate is reported alongside but not gated, because it bakes in+# the pack's documented coverage gaps (endpoint compromise, persistence,+# defense-evasion) and would punish CI for known v1 scope decisions.+HIGH_CRIT_MAPPED_FLOOR: float = 0.95+ACTION_ALIGNMENT_FLOOR: float = 0.85+++# ---------------------------------------------------------------------------+# Template -> playbook-category mapping+#+# Hand-authored against the v1 pack (50 playbooks, 9 categories). Each+# template_id maps to the *set* of categories whose playbooks should be+# considered candidate responders. Templates that the v1 pack legitimately+# does not yet cover (endpoint compromise, persistence, defense-evasion,+# pure execution) are mapped to an empty list so the metric exposes the+# coverage gap honestly without silently inflating itself.+# ---------------------------------------------------------------------------++_TEMPLATE_CATEGORIES: dict[str, list[str]] = {+ # account-takeover+ "azure-ad-impossible-travel": ["account-takeover"],+ "credential-spray": ["account-takeover"],+ "github-pat-leak": ["account-takeover", "supply-chain"],+ "helpdesk-password-reset-abuse": ["account-takeover"],+ "oauth-refresh-token-theft": ["account-takeover"],+ "saml-golden-ticket": ["account-takeover", "lateral-movement"],+ "vpn-new-geography": ["account-takeover"],+ # bec+ "bec-wire-fraud": ["bec"],+ "oauth-consent-phish": ["bec", "account-takeover"],+ "outlook-auto-forward-rule": ["bec"],+ "phishing-macro-email": ["bec"],+ # cloud-misconfig+ "ec2-imds-credential-theft": ["cloud-misconfig"],+ "malicious-container-image": ["cloud-misconfig", "supply-chain"],+ "public-s3-bucket-pii": ["cloud-misconfig", "data-exfil"],+ # data-exfil+ "bulk-pii-download": ["data-exfil", "insider-risk"],+ "dns-tunnel-exfil": ["data-exfil"],+ "insider-mailbox-export": ["data-exfil", "insider-risk"],+ "personal-drive-exfil": ["data-exfil", "insider-risk"],+ "s3-exfil-cloud-storage": ["data-exfil"],+ # ddos+ "ddos-syn-flood": ["ddos"],+ # insider-risk+ "service-account-privileged-command": ["insider-risk"],+ # lateral-movement+ "ad-dcsync": ["lateral-movement"],+ "kerberoasting": ["lateral-movement"],+ "pass-the-hash-lateral": ["lateral-movement"],+ "rdp-lateral-movement": ["lateral-movement"],+ "wmi-lateral-execution": ["lateral-movement"],+ # ransomware+ "ransomware-encryption": ["ransomware"],+ # supply-chain+ "compromised-ci-runner": ["supply-chain"],+ "npm-supply-chain": ["supply-chain"],+}++# Templates the v1 pack legitimately does not cover. Listed explicitly so+# adding a new template without either a pack mapping or an explicit+# no-coverage entry will trip ``test_every_template_is_classified``.+_TEMPLATES_WITHOUT_PACK_COVERAGE: set[str] = {+ # Endpoint compromise / persistence / defense-evasion (the v1 pack focuses+ # on identity, network, cloud, exfil, and ransomware response — endpoint-+ # native containment playbooks are slated for the v1.1 pack).+ "certutil-download-cradle",+ "clipboard-keylogger",+ "confluence-watering-hole",+ "cron-backdoor",+ "dga-c2",+ "disable-edr-tooling",+ "docker-runtime-abuse",+ "event-log-cleared",+ "https-c2-beacon",+ "k8s-privileged-pod-escape",+ "linux-journald-tampering",+ "linux-suid-abuse",+ "lsass-memory-dump",+ "office-vsto-addin",+ "powershell-obfuscated-dropper",+ "process-hollowing-svchost",+ "registry-run-persistence",+ "scheduled-task-persistence",+ "uac-bypass-fodhelper",+ "uefi-firmware-implant",+ "usb-autorun-airgap",+ "webapp-sqli-ssrf-exploit",+ "wmi-event-subscription",+ # Medium-severity reconnaissance / early-stage templates. The dataset+ # generator emits these at medium severity (pre-exploitation signals),+ # while the pack's containment playbooks (lateral, ransomware) are+ # correctly scoped to high+critical post-exploitation events. Mapping+ # them would either force a noisy false-positive match or require+ # downgrading the playbooks. Listed here so the regression gate stays+ # meaningful for the templates that *should* match.+ "ldap-bloodhound-discovery",+ "smb-share-enumeration",+ "xmrig-cryptominer",+}++# Playbooks that ship in the v1 pack for real-world coverage but are not+# exercised by the deterministic 200-incident benchmark. These are *not*+# bugs — the dataset is biased toward high/critical post-exploitation+# incidents, while these playbooks target medium-severity edge cases+# (removable-media exfil, after-hours insider access, critical-only+# volumetric DDoS). Removing one of these from the pack should still trip+# CI elsewhere (the pack inventory test); excluding them from the+# orphan-playbook gate keeps that gate honest about *broken* triggers.+_PLAYBOOKS_NOT_IN_BENCHMARK_DATASET: set[str] = {+ "exfil-removable-media-v1",+ "ddos-volumetric-l3-v1",+ "insider-after-hours-access-v1",+}++# All categories the pack actually ships. Loaded dynamically below from disk+# so an analyst adding a new category folder can't drift this list silently.+++# ---------------------------------------------------------------------------+# response_class -> action-alignment signals+#+# When matching a playbook against an incident, we ask: does the playbook+# have at least one step whose declared type or human-readable name implies+# the incident's expected response action? Step-type matching is the strong+# signal; step-name keyword matching is the fallback for response classes+# that v1 expresses as generic ``http`` or ``investigate`` steps (e.g.+# disable_account → POST to IDP /reset endpoint).+# ---------------------------------------------------------------------------++_RESPONSE_ACTION_SIGNALS: dict[str, dict[str, set[str]]] = {+ "isolate_host": {+ "step_types": {"isolate_host"},+ "step_name_keywords": {"isolate", "contain", "quarantine"},+ },+ "block_indicator": {+ "step_types": {"block_ip"},+ "step_name_keywords": {"block", "blocklist", "deny", "sinkhole"},+ },+ "disable_account": {+ "step_types": set(),+ "step_name_keywords": {+ "disable",+ "reset",+ "revoke",+ "lock account",+ "force reset",+ "force log",+ "kill session",+ },+ },+ "rollback_change": {+ "step_types": set(),+ "step_name_keywords": {+ "rollback",+ "roll back",+ "revert",+ "restore",+ "remove rule",+ "remove forwarding",+ "rotate",+ "patch",+ },+ },+ "escalate": {+ "step_types": {"create_ticket", "notify"},+ "step_name_keywords": {"escalate", "page", "notify", "alert", "ticket"},+ },+ "monitor": {+ "step_types": {"investigate"},+ "step_name_keywords": {"investigate", "monitor", "audit", "watch", "hunt"},+ },+}+++# ---------------------------------------------------------------------------+# IO+# ---------------------------------------------------------------------------+++def _load_dataset() -> list[dict[str, Any]]:+ if not _DATASET_PATH.exists():+ raise FileNotFoundError(+ f"Synthetic incidents dataset missing at {_DATASET_PATH}. "+ "Run `python3 scripts/generate_eval_incidents.py` to regenerate."+ )+ with _DATASET_PATH.open() as f:+ return json.load(f)+++def _load_playbooks() -> tuple[list[dict[str, Any]], list[str]]:+ """Walk the pack root and return (playbooks, categories).++ A "category" is the immediate subfolder under ``packs/v1/`` so the list+ is canonical and stays in sync with disk layout. Each playbook is+ augmented with a ``__category`` field (the on-disk folder name) and a+ ``__path`` field for diagnostics.+ """+ if not _PACK_ROOT.exists():+ raise FileNotFoundError(+ f"Playbook pack missing at {_PACK_ROOT}. "+ "Verify `playbooks/packs/v1/` is checked in."+ )+ playbooks: list[dict[str, Any]] = []+ categories: set[str] = set()+ for category_dir in sorted(p for p in _PACK_ROOT.iterdir() if p.is_dir()):+ category = category_dir.name+ if category.startswith("."):+ continue+ categories.add(category)+ for path in sorted(category_dir.glob("*.playbook.json")):+ try:+ pb = json.loads(path.read_text())+ except json.JSONDecodeError as exc:+ raise ValueError(f"Invalid playbook JSON: {path} -- {exc}") from exc+ pb["__category"] = category+ pb["__path"] = str(path.relative_to(_REPO_ROOT))+ playbooks.append(pb)+ return playbooks, sorted(categories)+++SYNTHETIC_INCIDENTS_DATA: list[dict[str, Any]] = _load_dataset()+PLAYBOOK_PACK, PACK_CATEGORIES = _load_playbooks()+++# ---------------------------------------------------------------------------+# Matching+# ---------------------------------------------------------------------------+++def _normalize(text: str) -> str:+ return re.sub(r"\s+", " ", text or "").strip().lower()+++def _playbook_severity_set(playbook: dict[str, Any]) -> set[str]:+ trigger = playbook.get("trigger") or {}+ sevs = trigger.get("severity") or []+ return {str(s).lower() for s in sevs}+++def _playbook_tag_set(playbook: dict[str, Any]) -> set[str]:+ return {str(t).lower() for t in (playbook.get("tags") or [])}+++def _step_signals(playbook: dict[str, Any]) -> tuple[set[str], str]:+ """Return (step_types, concatenated_step_name_text)."""+ types: set[str] = set()+ name_blob_parts: list[str] = []+ for step in playbook.get("steps") or []:+ st = str(step.get("type", "")).lower()+ if st:+ types.add(st)+ name_blob_parts.append(_normalize(str(step.get("name", ""))))+ return types, " | ".join(name_blob_parts)+++def _action_aligned(+ playbook: dict[str, Any], response_class: str+) -> bool:+ sig = _RESPONSE_ACTION_SIGNALS.get(response_class)+ if sig is None:+ # Unknown response class -> any playbook with at least one step+ # qualifies. Conservative because we'd rather over-credit a brand-+ # new response class than block the gate when the dataset evolves.+ return bool(playbook.get("steps"))+ types, name_blob = _step_signals(playbook)+ if sig["step_types"] & types:+ return True+ return any(kw in name_blob for kw in sig["step_name_keywords"])+++@dataclass+class PlaybookMatch:+ """One playbook<->incident match record."""++ playbook_id: str+ playbook_name: str+ category: str+ severity_match: bool+ tag_match: bool+ action_aligned: bool++ @property+ def is_full_match(self) -> bool:+ return self.severity_match and self.tag_match and self.action_aligned++ @property+ def is_partial_match(self) -> bool:+ # Severity + tag is enough to "cover" the incident even if no step+ # aligns with the response class — we want the action-alignment+ # gate to surface separately so a partial match isn't masked.+ return self.severity_match and self.tag_match+++@dataclass+class IncidentEvaluation:+ incident_id: str+ template_id: str+ severity: str+ response_class: str+ mapped_categories: list[str]+ matches: list[PlaybookMatch] = field(default_factory=list)++ @property+ def is_covered(self) -> bool:+ return any(m.is_partial_match for m in self.matches)++ @property+ def has_aligned_match(self) -> bool:+ return any(m.is_full_match for m in self.matches)+++@dataclass+class PlaybookCompletionResult:+ incidents: int = 0+ covered: int = 0+ aligned: int = 0+ per_severity: dict[str, dict[str, int]] = field(default_factory=dict)+ per_category: dict[str, dict[str, int]] = field(default_factory=dict)+ per_template: dict[str, dict[str, int]] = field(default_factory=dict)+ orphan_templates: list[str] = field(default_factory=list)+ orphan_playbooks: list[dict[str, str]] = field(default_factory=list)+ per_incident: list[IncidentEvaluation] | None = None++ @property+ def completion_rate(self) -> float:+ return self.covered / self.incidents if self.incidents else 0.0++ @property+ def action_alignment_rate(self) -> float:+ # Alignment rate is conditional on coverage: of the matched+ # incidents, how many had a step-aligned playbook match? An+ # incident with zero matches contributes neither numerator nor+ # denominator, so adding fully-uncovered templates can't game it.+ return self.aligned / self.covered if self.covered else 0.0++ def severity_completion_rate(self, severities: Iterable[str]) -> float:+ target = {s.lower() for s in severities}+ total = 0+ covered = 0+ for sev, bucket in self.per_severity.items():+ if sev not in target:+ continue+ total += bucket["incidents"]+ covered += bucket["covered"]+ return covered / total if total else 0.0++ def severity_completion_rate_mapped(+ self,+ severities: Iterable[str],+ per_incident: Iterable["IncidentEvaluation"],+ ) -> tuple[float, int, int]:+ """Severity completion measured *only* over mapped templates.++ Returns (rate, covered, total). "Mapped" means the incident's+ template appears in ``_TEMPLATE_CATEGORIES``; templates explicitly+ listed in ``_TEMPLATES_WITHOUT_PACK_COVERAGE`` are excluded so the+ rate doesn't bake in v1's documented coverage gaps. This is the+ gated metric for high+critical severity.+ """+ target = {s.lower() for s in severities}+ total = 0+ covered = 0+ for ev in per_incident:+ if ev.severity not in target:+ continue+ if ev.template_id not in _TEMPLATE_CATEGORIES:+ continue+ if not _TEMPLATE_CATEGORIES[ev.template_id]:+ continue+ total += 1+ if ev.is_covered:+ covered += 1+ return (covered / total if total else 0.0, covered, total)+++def _empty_severity_bucket() -> dict[str, int]:+ return {"incidents": 0, "covered": 0, "aligned": 0}+++def _evaluate_one(+ incident: dict[str, Any], playbooks: list[dict[str, Any]]+) -> IncidentEvaluation:+ template_id = str(incident.get("template_id", ""))+ severity = str(incident.get("severity", "")).lower()+ response_class = str(incident.get("response_class", "")).lower()+ mapped = _TEMPLATE_CATEGORIES.get(template_id, [])+ mapped_lower = {c.lower() for c in mapped}+ ev = IncidentEvaluation(+ incident_id=str(incident.get("id", "")),+ template_id=template_id,+ severity=severity,+ response_class=response_class,+ mapped_categories=list(mapped),+ )+ if not mapped:+ return ev+ for pb in playbooks:+ pb_tags = _playbook_tag_set(pb)+ pb_sevs = _playbook_severity_set(pb)+ tag_match = bool(pb_tags & mapped_lower)+ if not tag_match:+ # Cheap reject: if the playbook is tagged for an entirely+ # different scenario we don't even score severity/action.+ continue+ severity_match = severity in pb_sevs if pb_sevs else False+ action_aligned = _action_aligned(pb, response_class)+ ev.matches.append(+ PlaybookMatch(+ playbook_id=str(pb.get("id", "")),+ playbook_name=str(pb.get("name", "")),+ category=str(pb.get("__category", "")),+ severity_match=severity_match,+ tag_match=tag_match,+ action_aligned=action_aligned,+ )+ )+ return ev+++def evaluate_playbook_completion(+ dataset: list[dict[str, Any]] | None = None,+ playbooks: list[dict[str, Any]] | None = None,+ *,+ keep_per_incident: bool = False,+) -> PlaybookCompletionResult:+ data = dataset if dataset is not None else SYNTHETIC_INCIDENTS_DATA+ pb_pack = playbooks if playbooks is not None else PLAYBOOK_PACK++ result = PlaybookCompletionResult(+ per_incident=[] if keep_per_incident else None+ )++ # Per-category presence: which categories actually fired against any+ # incident? Initialised from the pack so an empty bucket surfaces.+ pack_categories = sorted({pb.get("__category", "") for pb in pb_pack if pb.get("__category")})+ for cat in pack_categories:+ result.per_category[cat] = {"incidents": 0, "covered": 0, "aligned": 0}++ # Per-playbook firing: tracks orphan playbooks (no incident matches).+ pb_firing: dict[str, dict[str, Any]] = {+ str(pb.get("id", "")): {+ "name": pb.get("name", ""),+ "category": pb.get("__category", ""),+ "path": pb.get("__path", ""),+ "covers": 0,+ "aligned_covers": 0,+ }+ for pb in pb_pack+ }++ template_buckets: dict[str, dict[str, int]] = defaultdict(_empty_severity_bucket)++ for inc in data:+ ev = _evaluate_one(inc, pb_pack)+ result.incidents += 1++ sev_bucket = result.per_severity.setdefault(ev.severity or "unknown", _empty_severity_bucket())+ sev_bucket["incidents"] += 1++ tpl_bucket = template_buckets[ev.template_id or "unknown"]+ tpl_bucket["incidents"] += 1++ if ev.is_covered:+ result.covered += 1+ sev_bucket["covered"] += 1+ tpl_bucket["covered"] += 1+ for m in ev.matches:+ if m.is_partial_match:+ cat_bucket = result.per_category.setdefault(+ m.category, {"incidents": 0, "covered": 0, "aligned": 0}+ )+ cat_bucket["covered"] += 1+ pb_firing[m.playbook_id]["covers"] += 1++ if ev.has_aligned_match:+ result.aligned += 1+ sev_bucket["aligned"] += 1+ tpl_bucket["aligned"] += 1+ for m in ev.matches:+ if m.is_full_match:+ cat_bucket = result.per_category.setdefault(+ m.category, {"incidents": 0, "covered": 0, "aligned": 0}+ )+ cat_bucket["aligned"] += 1+ pb_firing[m.playbook_id]["aligned_covers"] += 1++ # per-category incident counter (mapped categories side, not+ # firing side, so a category with mapped templates but no firing+ # playbooks shows up as "incidents>0, covered=0").+ for cat in ev.mapped_categories:+ cat_bucket = result.per_category.setdefault(+ cat, {"incidents": 0, "covered": 0, "aligned": 0}+ )+ cat_bucket["incidents"] += 1++ if keep_per_incident and result.per_incident is not None:+ result.per_incident.append(ev)++ # Roll up per-template stats.+ for tpl, bucket in sorted(template_buckets.items()):+ result.per_template[tpl] = bucket+ if (+ bucket["covered"] == 0+ and tpl not in _TEMPLATES_WITHOUT_PACK_COVERAGE+ and tpl in _TEMPLATE_CATEGORIES+ and _TEMPLATE_CATEGORIES[tpl]+ ):+ # Templates the pack *should* cover but didn't — distinct from+ # the documented gap list. These are real regressions.+ result.orphan_templates.append(tpl)++ for pb_id, info in pb_firing.items():+ if info["covers"] == 0 and pb_id not in _PLAYBOOKS_NOT_IN_BENCHMARK_DATASET:+ result.orphan_playbooks.append(+ {+ "playbook_id": pb_id,+ "name": info["name"],+ "category": info["category"],+ "path": info["path"],+ }+ )++ return result+++# ---------------------------------------------------------------------------+# Tests+# ---------------------------------------------------------------------------+++class TestPlaybookCompletionRate(unittest.TestCase):+ """Pack-vs-dataset coverage gates (WS-C3)."""++ @classmethod+ def setUpClass(cls) -> None:+ cls.result = evaluate_playbook_completion(keep_per_incident=True)++ def test_dataset_and_pack_loaded(self) -> None:+ self.assertGreaterEqual(+ len(SYNTHETIC_INCIDENTS_DATA),+ 200,+ "Expected the 200-incident benchmark to be present.",+ )+ self.assertGreaterEqual(+ len(PLAYBOOK_PACK),+ 10,+ f"Expected v1 playbook pack to ship at least 10 playbooks; got {len(PLAYBOOK_PACK)}.",+ )++ def test_every_template_is_classified(self) -> None:+ """Every template in the dataset must either map to a category or be+ explicitly listed in the no-pack-coverage allowlist."""+ seen = {str(i["template_id"]) for i in SYNTHETIC_INCIDENTS_DATA}+ classified = set(_TEMPLATE_CATEGORIES) | _TEMPLATES_WITHOUT_PACK_COVERAGE+ unclassified = sorted(seen - classified)+ self.assertFalse(+ unclassified,+ f"Unclassified templates (add to _TEMPLATE_CATEGORIES or "+ f"_TEMPLATES_WITHOUT_PACK_COVERAGE): {unclassified}",+ )++ def test_overall_completion_rate_floor(self) -> None:+ rate = self.result.completion_rate+ print(+ "\n[eval] playbook_completion_rate "+ f"overall: {rate:.3f} | covered: {self.result.covered}/{self.result.incidents} | "+ f"aligned: {self.result.aligned} | "+ f"orphan_templates: {len(self.result.orphan_templates)} | "+ f"orphan_playbooks: {len(self.result.orphan_playbooks)}"+ )+ self.assertGreaterEqual(+ rate,+ OVERALL_COMPLETION_FLOOR,+ f"Overall completion {rate:.3f} below floor {OVERALL_COMPLETION_FLOOR:.2f}. "+ "Either ship a new playbook for an uncovered category or "+ "verify the matcher's mapping is still accurate.",+ )++ def test_high_critical_completion_rate_floor(self) -> None:+ """High+critical severity gate, measured over *mapped* templates only.++ We deliberately don't gate the raw severity rate because the+ dataset includes ~22 endpoint-compromise / persistence templates+ that are documented as v1 coverage gaps in+ ``_TEMPLATES_WITHOUT_PACK_COVERAGE`` — a hard severity gate would+ either force inflating coverage with mismatched playbooks or+ penalise CI for known scope decisions. The mapped rate answers+ the right question: of the high+critical incidents the pack+ *claims* to cover, how many actually have a matching playbook?+ """+ per_incident = self.result.per_incident or []+ rate, covered, total = self.result.severity_completion_rate_mapped(+ ("high", "critical"), per_incident+ )+ raw_rate = self.result.severity_completion_rate(("high", "critical"))+ print(+ "\n[eval] playbook_completion_rate high+critical "+ f"mapped: {rate:.3f} ({covered}/{total}) | raw: {raw_rate:.3f}"+ )+ self.assertGreaterEqual(+ rate,+ HIGH_CRIT_MAPPED_FLOOR,+ f"high+critical (mapped) completion {rate:.3f} below floor "+ f"{HIGH_CRIT_MAPPED_FLOOR:.2f}. Severe mapped incidents must "+ "have containment playbooks.",+ )++ def test_action_alignment_when_matched(self) -> None:+ rate = self.result.action_alignment_rate+ self.assertGreaterEqual(+ rate,+ ACTION_ALIGNMENT_FLOOR,+ f"Action-alignment rate {rate:.3f} below floor "+ f"{ACTION_ALIGNMENT_FLOOR:.2f}. A matched playbook that lacks a "+ "step aligned with the incident's response_class doesn't count "+ "as a real response.",+ )++ def test_every_pack_category_fires(self) -> None:+ """Every category folder under packs/v1 must match at least one+ incident in the dataset. Dead-weight categories are a regression."""+ dead = [+ cat+ for cat, bucket in self.result.per_category.items()+ if cat in PACK_CATEGORIES and bucket["covered"] == 0+ ]+ self.assertFalse(+ dead,+ f"Pack categories with zero matched incidents: {dead}",+ )++ def test_no_orphan_playbooks(self) -> None:+ """Every playbook in the pack must match at least one incident."""+ self.assertFalse(+ self.result.orphan_playbooks,+ "Orphan playbooks (no matching incident in the dataset):\n"+ + "\n".join(+ f" - [{p['category']}] {p['playbook_id']} "+ f"({p['path']})"+ for p in self.result.orphan_playbooks+ ),+ )++ def test_no_unexpected_template_regressions(self) -> None:+ """Templates that *should* be covered (i.e. mapped to a category)+ but produced zero matches indicate either a broken mapping or a+ broken playbook trigger. Distinct from documented gaps."""+ self.assertFalse(+ self.result.orphan_templates,+ f"Mapped templates with zero playbook matches (regression): "+ f"{self.result.orphan_templates}",+ )+++if __name__ == "__main__":+ unittest.main()