diff --git a/agentic_security/__main__.py b/agentic_security/__main__.py index 5942383..245a5ef 100644 --- a/agentic_security/__main__.py +++ b/agentic_security/__main__.py @@ -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): """ diff --git a/agentic_security/lib.py b/agentic_security/lib.py index 07e0f32..34463bc 100644 --- a/agentic_security/lib.py +++ b/agentic_security/lib.py @@ -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. diff --git a/tests/unit/test_ci_exit_code.py b/tests/unit/test_ci_exit_code.py new file mode 100644 index 0000000..f2da4f6 --- /dev/null +++ b/tests/unit/test_ci_exit_code.py @@ -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