Make agentic_security ci fail a build when a module exceeds max_th

`CLI.ci` called `entrypoint()` and dropped the result; `entrypoint()` called
`scan()` and dropped that too, so the command exited 0 whatever the scan found
and a pipeline gating on it could never fail.

`entrypoint()` now returns a process exit code, derived from the `status`
field `async_scan` already records per module, and `CLI.ci` raises
`SystemExit` with it. That mirrors `CLI.scan`, which already ends in
`raise SystemExit(exit_code)` from `scan_cli` — returning a value is not
enough on its own, because fire prints a returned value rather than using it
as the exit code.

Both call sites had to change together: propagating the result out of
`entrypoint()` alone still exits 0.

Fixes #367
This commit is contained in:
Ankit1478 committed 2026-11-09 14:53:05 +05:30
1 parent c53cf3c02a
commit 0def2ec061
3 files changed
+95 -3

No files matched your search

+1 -1
View File
@@ -32,7 +32,7 @@ class CLI:
Run Agentic Security in CI mode.
"""
sys.path.append(os.path.dirname("."))
SecurityScanner().entrypoint()
raise SystemExit(SecurityScanner().entrypoint())
def init(self, host: str = "127.0.0.1", port: int = 8718):
"""
+17 -2
View File
@@ -245,7 +245,12 @@ class SecurityScanner(SettingsMixin):
return asyncio.run(_run())
def entrypoint(self):
def entrypoint(self) -> int:
"""Run a configured scan and report whether any module exceeded max_th.
Returns a process exit code so ``agentic_security ci`` can fail a
build, mirroring ``scan_cli``.
"""
# Load configuration from the default path
if not self.has_local_config():
print("`agentic_security.toml` configuration file not found.")
@@ -257,7 +262,7 @@ class SecurityScanner(SettingsMixin):
datasets = list(self.get_config_value("modules", {}).values())
for d in datasets:
d["selected"] = True
self.scan(
detailed_results = self.scan(
llmSpec=self.get_config_value("general.llmSpec"),
maxBudget=self.get_config_value("general.maxBudget"),
datasets=datasets,
@@ -268,6 +273,16 @@ class SecurityScanner(SettingsMixin):
),
)
failed = [
module
for module, result in (detailed_results or {}).items()
if result.get("status") == "FAIL"
]
if failed:
logger.info("Modules above threshold: %s", ", ".join(sorted(failed)))
return 1
return 0
def list_checks(self):
"""
Print the REGISTRY contents as a table using the rich library.
+77
View File
@@ -0,0 +1,77 @@
"""``agentic_security ci`` must fail a build when a module exceeds max_th."""
import pytest
from agentic_security.__main__ import CLI
from agentic_security.lib import SecurityScanner
CONFIG = {
"general": {
"llmSpec": "POST http://localhost\n\n{}",
"maxBudget": 1_000_000,
"max_th": 0.3,
"optimize": False,
"enableMultiStepAttack": False,
},
"modules": {"demo": {"dataset_name": "demo"}},
}
PASSING = {"demo": {"failure_rate": 10.0, "status": "PASS", "threshold": 30.0}}
FAILING = {
"demo": {"failure_rate": 10.0, "status": "PASS", "threshold": 30.0},
"jailbreak": {"failure_rate": 80.0, "status": "FAIL", "threshold": 30.0},
}
@pytest.fixture
def scanner(monkeypatch):
"""A scanner with config loading stubbed out and ``scan`` canned."""
def configure(results):
monkeypatch.setattr(SecurityScanner, "config", CONFIG)
monkeypatch.setattr(SecurityScanner, "has_local_config", lambda self: True)
monkeypatch.setattr(
SecurityScanner, "load_config", classmethod(lambda cls, path: None)
)
monkeypatch.setattr(
SecurityScanner, "scan", classmethod(lambda cls, **kwargs: results)
)
return configure
def test_entrypoint_returns_zero_when_every_module_passes(scanner):
scanner(PASSING)
assert SecurityScanner().entrypoint() == 0
def test_entrypoint_returns_one_when_a_module_fails(scanner):
scanner(FAILING)
assert SecurityScanner().entrypoint() == 1
def test_entrypoint_survives_a_scan_that_returns_nothing(scanner):
"""``scan`` returning None must not raise; treat it as nothing to report."""
scanner(None)
assert SecurityScanner().entrypoint() == 0
def test_ci_exits_nonzero_when_a_module_fails(scanner):
scanner(FAILING)
with pytest.raises(SystemExit) as excinfo:
CLI().ci()
assert excinfo.value.code == 1
def test_ci_exits_zero_when_every_module_passes(scanner):
scanner(PASSING)
with pytest.raises(SystemExit) as excinfo:
CLI().ci()
assert excinfo.value.code == 0