Skip to content

M65 (TASK-122-FREE): Production httpx Client & Multi-Span Grounding for verity scan --llm-assist

1. Purpose & Architectural Decisions

  • Problem (P1.4): SemanticAssistClient (src/verity/scanner/semantic_assist.py) uses stdlib urllib.request, which lacks clean proxy/CA handling, granular timeouts, Retry-After backoff, and robust SSE framing, and it only verifies a single-string evidence_span.
  • HTTP Client Upgrade: adopt httpx (BSD-3-Clause, explicitly approved pyproject.toml dependency) with trust_env=True (HTTP_PROXY/HTTPS_PROXY/NO_PROXY), custom CA loading from SSL_CERT_FILE / REQUESTS_CA_BUNDLE into httpx.AsyncClient(verify=ssl_ctx), and base-URL overrides OPENAI_BASE_URL, ANTHROPIC_BASE_URL, GEMINI_BASE_URL, OLLAMA_HOST with normalized paths.
  • Granular Timeouts & Retries: httpx.Timeout(connect=10.0, read=120.0, write=30.0, pool=10.0); retry 408, 429, 500, 502, 503, 504, and httpx.TransportError up to 3 attempts with jitter, honoring Retry-After (seconds or HTTP-date) clamped to 30s.
  • Provider Normalization: OpenAI strict mode, Anthropic tool_use + named event: SSE frames, Gemini ?alt=sse + sanitized responseSchema, Ollama /api/chat format.
  • Multi-Span Grounding: model CodeSpan(file_path, start_line, end_line, excerpt) and GroundedFinding(rule_id, severity, file_path, spans: list[CodeSpan], rationale); verify_multi_span_grounding(...) performs exact or whitespace-normalized substring matching plus [start_line, end_line] window verification, feeding rejections into bounded retry (max_retries = 2) and discarding ungrounded items after retries.
  • Advisory-Only Boundary: --llm-assist remains opt-in; without the flag, verity scan makes zero outbound HTTP requests.
  • Free/Open-Core Boundary: zero Pro dependencies.

2. Target Files

File Change
pyproject.toml Add httpx (BSD-3-Clause) dependency
src/verity/scanner/semantic_assist.py httpx.AsyncClient transport (base URLs, trust_env, custom CAs, granular httpx.Timeout, bounded retries with Retry-After), provider SSE/schema normalization, CodeSpan / GroundedFinding / verify_multi_span_grounding
tests/unit/scanner/test_semantic_assist_httpx.py Unit tests for configuration, retry policy, and multi-span grounding acceptance/rejection

3. ATDD Traceability Matrix

AC ID Acceptance Criterion Paired Benign / Negative Control Verification Tier Test Target
AC-65.1 Without --llm-assist, verity scan makes zero HTTP requests; with --llm-assist, SemanticAssistClient uses httpx honoring base URL overrides, trust_env=True, SSL_CERT_FILE, and granular httpx.Timeout. Negative control: default scan issues zero requests; benign control: overridden base URL is the only contacted origin. unit tests/unit/scanner/test_semantic_assist_httpx.py::test_llm_assist_uses_httpx_and_makes_zero_requests_by_default
AC-65.2 Transient 429 with Retry-After and 503 retry up to 3 attempts (clamped <=30s), while non-retryable 401/404 emit structured ERR_MISSING_CREDENTIALS / ERR_MODEL_NOT_FOUND immediately. Paired control: Retry-After: 3600 is clamped to 30s; 401 performs exactly one attempt. unit tests/unit/scanner/test_semantic_assist_httpx.py::test_httpx_retry_after_and_non_retryable_errors
AC-65.3 verify_multi_span_grounding accepts findings whose CodeSpan excerpts match exact/whitespace-normalized source within [start_line, end_line] and rejects/retries (max_retries=2) fabricated spans. Paired control: exact-span finding accepted; fabricated excerpt retried then discarded with zero emitted advisories. unit tests/unit/scanner/test_semantic_assist_httpx.py::test_verify_multi_span_grounding_accepts_and_rejects
AC-65.4 Ruff linter and full unit test suite pass cleanly. Clean workspace yields zero lint errors. ruff uv run ruff check src tests && uv run pytest tests/

4. Verification Commands

uv run pytest tests/unit/scanner/test_semantic_assist_httpx.py
uv run pytest tests/
uv run ruff check src tests

5. Execution Checklist

  • [ ] AC-65.1 implemented (httpx client, base URLs, trust_env, SSL_CERT_FILE, granular timeout)
  • [ ] AC-65.2 implemented (bounded retries + Retry-After clamp + structured 401/404 errors)
  • [ ] AC-65.3 implemented (CodeSpan / GroundedFinding + verify_multi_span_grounding with max_retries = 2)
  • [ ] AC-65.4 verified green (ruff + full pytest)
  • [ ] Board #5 item PVTI_lAHOD-xmQM4Br351zhOMEAw synced & status updated