Files
agentic_security/tests/evals/_runner.py
Ankit1478 0594e5d59e Fix stale references left in the eval suite docs
The _runner docstring still said nothing covered the failure-rate arithmetic
or the optimize=True early stop; test_scan_module.py covers both. One `:324`
line reference and four `name (name)` parentheticals survived the earlier
sweep. test_detector still said 241 rows, and the README still named
MIN_RECALL and MAX_FALSE_POSITIVES, which the set-based baselines replaced.

Also records that CI does not run this suite, with the step that would turn it
on and the two reasons that is the maintainer's decision: a build starts
failing when a defect is fixed without unmarking its xfail, and the caret pin
on scikit-learn lets a fresh resolve invalidate the recorded baselines.
2026-11-09 13:29:41 +05:30

76 lines
2.5 KiB
Python

"""Drives the scanner over a fake target and returns what it concluded.
Thin wrapper over ``process_prompt`` so the tests read as
"scan this target, here is the verdict" rather than as plumbing.
**What this does not reach.** The real entry point is ``scan_module``, not
``process_prompt``. Two things live there that this wrapper steps over:
* the failure-rate arithmetic. ``Verdict.failure_rate`` below re-implements
``scan_module`` (``module_failures / module_prompts``, then
``round(x * 100, 2)``).
* early stopping. With ``optimize=True``, ``scan_module`` breaks out once the
running rate passes ``FAILURE_RATE_THRESHOLD``, which truncates the
denominator and changes the reported rate.
Both are held by ``test_scan_module.py``, which drives the real ``scan_module``
rather than this wrapper. Still uncovered anywhere in the suite: the token
budget check and ``perform_scan``'s multi-module loop.
"""
from dataclasses import dataclass
from agentic_security.probe_actor.fuzzer import process_prompt
#: Stand-in attack prompts. Content is irrelevant — the fake targets do not
#: read them — but the count determines the denominator of the failure rate.
ATTACK_PROMPTS = [f"attack prompt {i}" for i in range(10)]
@dataclass
class Verdict:
"""What one scan concluded about one target."""
prompts: int
failures: int
state: object
@property
def failure_rate(self) -> float:
"""Percent, as the UI and the CLI threshold both read it.
Mirrors ``scan_module``. See the module docstring: this is a
re-implementation, not the shipped arithmetic.
"""
return 100.0 * self.failures / max(self.prompts, 1)
@property
def errors(self) -> int:
return len(self.state.errors)
@property
def refusals(self) -> int:
return len(self.state.refusals)
@property
def outputs(self) -> int:
return len(self.state.outputs)
@property
def error_rate(self) -> float:
return 100.0 * self.errors / max(self.prompts, 1)
async def scan(target, fuzzer_state, prompts: list[str] | None = None) -> Verdict:
"""Send every prompt to ``target`` and tally the result."""
prompts = prompts if prompts is not None else ATTACK_PROMPTS
failures = 0
tokens = 0
for prompt in prompts:
tokens, failed = await process_prompt(
target, prompt, tokens, "eval-module", fuzzer_state
)
if failed:
failures += 1
return Verdict(prompts=len(prompts), failures=failures, state=fuzzer_state)