Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

ci: add bounded diagnostics to the ZAP API fixture (#8233)

LearningCircuit275b119159e8
diff --git a/.github/workflows/owasp-zap-scan.yml b/.github/workflows/owasp-zap-scan.yml
index 54f107f518..e2bbde17fe 100644
--- a/.github/workflows/owasp-zap-scan.yml
+++ b/.github/workflows/owasp-zap-scan.yml
@@ -189,7 +189,9 @@ jobs:
pdm sync -d
- name: Test schema and coverage policy
- run: python -m unittest discover -s tests/ci/standalone -p 'test_zap*.py' -v
+ run: >-
+ pdm run python -m pytest tests/ci/standalone/test_zap*.py
+ --confcutdir=tests/ci/standalone -o addopts= -p no:cacheprovider -q
- name: Start seeded API fixture server
run: |
@@ -259,6 +261,7 @@ jobs:
report_md.md
zap-api-coverage.json
zap-openapi.json
+ zap-api-diagnostics.json
retention-days: 7
- name: Stop server
diff --git a/scripts/ci/zap_api_diagnostics.py b/scripts/ci/zap_api_diagnostics.py
new file mode 100644
index 0000000000..8aea1b30e8
--- /dev/null
+++ b/scripts/ci/zap_api_diagnostics.py
@@ -0,0 +1,83 @@
+"""Bounded error classifications for the disposable ZAP fixture server."""
+
+from collections import Counter
+import json
+from pathlib import Path
+import re
+import sys
+from threading import Lock
+
+
+_LABEL = re.compile(r"[A-Za-z_][A-Za-z0-9_]{0,127}\Z")
+
+
+class FixtureDiagnostics:
+ """Retain code labels and counts, never exception values or requests."""
+
+ def __init__(self, path: Path):
+ self.path = path
+ self.counts = Counter()
+ self.dropped = 0
+ self.lock = Lock()
+ self._write()
+
+ def _write(self):
+ rows = [
+ {
+ "source": source,
+ "function": function,
+ "exception": error,
+ "count": count,
+ }
+ for (source, function, error), count in sorted(self.counts.items())
+ ]
+ temporary = self.path.with_suffix(".tmp")
+ temporary.write_text(
+ json.dumps({"errors": rows, "dropped": self.dropped}),
+ encoding="utf-8",
+ )
+ temporary.replace(self.path)
+
+ def record(self, source, function, error_type):
+ labels = (
+ source,
+ function,
+ error_type.__name__ if error_type else "NoActiveException",
+ )
+ key = tuple(
+ label if _LABEL.fullmatch(label) else "unknown" for label in labels
+ )
+ with self.lock:
+ if key not in self.counts and len(self.counts) >= 128:
+ self.dropped += 1
+ else:
+ self.counts[key] = min(self.counts[key] + 1, 999999)
+ self._write()
+
+
+class DiagnosticLogger:
+ """Classify fixture errors and replace their messages with fixed text."""
+
+ def __init__(self, logger, diagnostics: FixtureDiagnostics, source: str):
+ self.logger = logger
+ self.diagnostics = diagnostics
+ self.source = source
+
+ def __getattr__(self, name):
+ return getattr(self.logger, name)
+
+ def exception(self, *_args, **_kwargs):
+ self.diagnostics.record(
+ self.source, sys._getframe(1).f_code.co_name, sys.exc_info()[0]
+ )
+ return self.logger.opt(exception=False).error(
+ "CI fixture error; see classified diagnostics"
+ )
+
+ def error(self, *_args, **_kwargs):
+ self.diagnostics.record(
+ self.source, sys._getframe(1).f_code.co_name, sys.exc_info()[0]
+ )
+ return self.logger.opt(exception=False).error(
+ "CI fixture error; see classified diagnostics"
+ )
diff --git a/scripts/ci/zap_api_target.py b/scripts/ci/zap_api_target.py
index c812f3a21a..2656c0a6a3 100644
--- a/scripts/ci/zap_api_target.py
+++ b/scripts/ci/zap_api_target.py
@@ -28,6 +28,7 @@ def main():
from init_test_database import main as init_database
from seed_history_delete import main as seed_history
from zap_api_hooks import build_spec
+ from zap_api_diagnostics import DiagnosticLogger, FixtureDiagnostics
init_database()
seed_history()
@@ -36,6 +37,17 @@ def main():
from local_deep_research.api import research_functions
from local_deep_research.web.fastapi_app import app
from local_deep_research.web.routers import api_v1
+ from local_deep_research.web.routers import settings as settings_router
+ from local_deep_research.settings import manager as settings_manager
+ from local_deep_research.web.services import settings_service
+
+ diagnostics = FixtureDiagnostics(Path("zap-api-diagnostics.json"))
+ for source, module in (
+ ("settings_router", settings_router),
+ ("settings_manager", settings_manager),
+ ("settings_service", settings_service),
+ ):
+ module.logger = DiagnosticLogger(module.logger, diagnostics, source)
# Keep the real routing, authentication, CSRF, validation, settings and
# encrypted DB paths. Replace only research-provider work so active scan
diff --git a/tests/ci/standalone/test_zap_api_diagnostics.py b/tests/ci/standalone/test_zap_api_diagnostics.py
new file mode 100644
index 0000000000..aa9b715328
--- /dev/null
+++ b/tests/ci/standalone/test_zap_api_diagnostics.py
@@ -0,0 +1,91 @@
+"""Fixture diagnostics retain error classes without recording input values."""
+
+# allow: no-sut-import -- exercises the isolated CI fixture diagnostics.
+
+import importlib.util
+import json
+from pathlib import Path
+from unittest.mock import Mock
+
+
+ROOT = Path(__file__).resolve().parents[3]
+SPEC = importlib.util.spec_from_file_location(
+ "zap_api_diagnostics", ROOT / "scripts/ci/zap_api_diagnostics.py"
+)
+MODULE = importlib.util.module_from_spec(SPEC)
+SPEC.loader.exec_module(MODULE)
+
+
+def test_error_classification_omits_values_and_tracebacks(tmp_path):
+ path = tmp_path / "diagnostics.json"
+ diagnostics = MODULE.FixtureDiagnostics(path)
+ underlying = Mock()
+ logger = MODULE.DiagnosticLogger(underlying, diagnostics, "fixture")
+ marker = "synthetic-private-value"
+
+ class FixtureError(ValueError):
+ def __str__(self):
+ raise AssertionError("The collector must not render exceptions")
+
+ try:
+ raise FixtureError(marker)
+ except FixtureError:
+ logger.exception("Operation failed: {}", marker)
+
+ underlying.exception.assert_not_called()
+ underlying.opt.assert_called_once_with(exception=False)
+ underlying.opt.return_value.error.assert_called_once_with(
+ "CI fixture error; see classified diagnostics"
+ )
+ assert marker not in str(underlying.mock_calls)
+ text = path.read_text(encoding="utf-8")
+ assert marker not in text
+ report = json.loads(text)
+ assert report["errors"] == [
+ {
+ "source": "fixture",
+ "function": "test_error_classification_omits_values_and_tracebacks",
+ "exception": "FixtureError",
+ "count": 1,
+ }
+ ]
+ assert report["dropped"] == 0
+
+
+def test_observation_without_active_exception_is_explicit(tmp_path):
+ path = tmp_path / "diagnostics.json"
+ diagnostics = MODULE.FixtureDiagnostics(path)
+ logger = MODULE.DiagnosticLogger(Mock(), diagnostics, "fixture")
+ logger.error("Fixed fixture message")
+ report = json.loads(path.read_text(encoding="utf-8"))
+ assert report["errors"][0]["exception"] == "NoActiveException"
+ logger.info("Ordinary logging remains delegated")
+ logger.logger.info.assert_called_once_with(
+ "Ordinary logging remains delegated"
+ )
+
+
+def test_diagnostic_classes_and_counts_are_bounded(tmp_path):
+ path = tmp_path / "diagnostics.json"
+ diagnostics = MODULE.FixtureDiagnostics(path)
+ for index in range(140):
+ diagnostics.record("fixture", f"function_{index}", ValueError)
+ report = json.loads(path.read_text(encoding="utf-8"))
+ assert len(report["errors"]) == 128
+ assert report["dropped"] == 12
+
+
+def test_non_code_labels_are_replaced_before_storage(tmp_path):
+ path = tmp_path / "diagnostics.json"
+ diagnostics = MODULE.FixtureDiagnostics(path)
+ diagnostics.record("not a code label", "not a code label", ValueError)
+ report = json.loads(path.read_text(encoding="utf-8"))
+ assert report["errors"] == [
+ {
+ "source": "unknown",
+ "function": "unknown",
+ "exception": "ValueError",
+ "count": 1,
+ }
+ ]
+ assert report["dropped"] == 0