generated from coulomb/repo-seed
Implement llm-connect ADHOC diagnostics
This commit is contained in:
@@ -2,14 +2,22 @@
|
||||
Tests for LLMServer HTTP serve mode (FR-1).
|
||||
"""
|
||||
|
||||
import threading
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import json
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
import pytest
|
||||
|
||||
from llm_connect._diagnostics import (
|
||||
record_adapter_transformation,
|
||||
record_provider_request,
|
||||
record_provider_response,
|
||||
)
|
||||
from llm_connect.adapter import MockLLMAdapter, ErrorLLMAdapter
|
||||
from llm_connect.models import RunConfig
|
||||
from llm_connect.models import LLMResponse, RunConfig
|
||||
from llm_connect.server import LLMServer
|
||||
|
||||
|
||||
@@ -45,6 +53,35 @@ def _post(url: str, body: dict) -> tuple[int, dict]:
|
||||
return exc.code, json.loads(exc.read())
|
||||
|
||||
|
||||
class DiagnosticLLMAdapter(MockLLMAdapter):
|
||||
def execute_prompt(self, prompt: str, config: RunConfig) -> LLMResponse:
|
||||
record_provider_request(
|
||||
url="https://provider.example/v1/chat",
|
||||
payload={"prompt": prompt, "model": config.model_name},
|
||||
headers={"Authorization": "Bearer secret-token"},
|
||||
)
|
||||
response = super().execute_prompt(prompt, config)
|
||||
response.metadata["provider"] = "diagnostic"
|
||||
response.metadata["response_id"] = "diag-response"
|
||||
record_provider_response(status=200, body={"id": "diag-response", "content": response.content})
|
||||
record_adapter_transformation(
|
||||
"diagnostic_transform",
|
||||
{"before": prompt},
|
||||
{"after": response.content},
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
class BarrierLLMAdapter(MockLLMAdapter):
|
||||
def __init__(self):
|
||||
super().__init__(mock_response="parallel")
|
||||
self._barrier = threading.Barrier(2)
|
||||
|
||||
def execute_prompt(self, prompt: str, config: RunConfig) -> LLMResponse:
|
||||
self._barrier.wait(timeout=2.0)
|
||||
return super().execute_prompt(prompt, config)
|
||||
|
||||
|
||||
class TestHealth:
|
||||
def test_health_returns_200(self, server):
|
||||
status, body = _get(f"http://127.0.0.1:{server.port}/health")
|
||||
@@ -65,6 +102,7 @@ class TestExecute:
|
||||
assert status == 200
|
||||
assert body["content"] == "hello world"
|
||||
assert body["finish_reason"] == "stop"
|
||||
assert "debug" not in body
|
||||
|
||||
def test_response_includes_usage(self, server):
|
||||
status, body = _post(
|
||||
@@ -150,3 +188,86 @@ class TestExecute:
|
||||
)
|
||||
assert status == 400
|
||||
assert "config" in body["error"]
|
||||
|
||||
def test_debug_query_returns_diagnostics(self):
|
||||
s = LLMServer(adapter=DiagnosticLLMAdapter(mock_response="debug body"), port=0)
|
||||
s.start()
|
||||
try:
|
||||
status, body = _post(
|
||||
f"http://127.0.0.1:{s.port}/execute?debug=1",
|
||||
{"prompt": "inspect", "config": {"model_name": "diagnostic-model"}},
|
||||
)
|
||||
finally:
|
||||
s.stop()
|
||||
|
||||
assert status == 200
|
||||
assert body["content"] == "debug body"
|
||||
debug = body["debug"]
|
||||
assert debug["provider_request"]["payload"] == {
|
||||
"prompt": "inspect",
|
||||
"model": "diagnostic-model",
|
||||
}
|
||||
assert debug["provider_request"]["headers_redacted"]["Authorization"] == "Bearer <redacted>"
|
||||
assert debug["provider_response"]["status"] == 200
|
||||
assert debug["adapter_transformations"][0]["step"] == "diagnostic_transform"
|
||||
|
||||
def test_debug_env_returns_diagnostics(self, monkeypatch):
|
||||
monkeypatch.setenv("LLM_CONNECT_DEBUG", "1")
|
||||
s = LLMServer(adapter=DiagnosticLLMAdapter(mock_response="debug body"), port=0)
|
||||
s.start()
|
||||
try:
|
||||
status, body = _post(
|
||||
f"http://127.0.0.1:{s.port}/execute",
|
||||
{"prompt": "inspect"},
|
||||
)
|
||||
finally:
|
||||
s.stop()
|
||||
|
||||
assert status == 200
|
||||
assert "debug" in body
|
||||
|
||||
def test_audit_dir_records_replayable_call(self, monkeypatch, tmp_path):
|
||||
monkeypatch.setenv("LLM_CONNECT_AUDIT_DIR", str(tmp_path))
|
||||
s = LLMServer(adapter=DiagnosticLLMAdapter(mock_response="audit body"), port=0)
|
||||
s.start()
|
||||
try:
|
||||
status, body = _post(
|
||||
f"http://127.0.0.1:{s.port}/execute",
|
||||
{"prompt": "audit me", "config": {"model_name": "audit-model"}},
|
||||
)
|
||||
finally:
|
||||
s.stop()
|
||||
|
||||
assert status == 200
|
||||
assert "debug" not in body
|
||||
files = list(tmp_path.glob("*.json"))
|
||||
assert len(files) == 1
|
||||
record = json.loads(files[0].read_text(encoding="utf-8"))
|
||||
assert record["prompt"] == "audit me"
|
||||
assert record["config"]["model_name"] == "audit-model"
|
||||
assert record["parsed_content"] == "audit body"
|
||||
assert record["provider_request"]["headers_redacted"]["Authorization"] == "Bearer <redacted>"
|
||||
assert record["provider_response"]["body"]["id"] == "diag-response"
|
||||
assert record["latency_seconds"] >= 0
|
||||
|
||||
def test_execute_requests_run_concurrently(self):
|
||||
s = LLMServer(adapter=BarrierLLMAdapter(), port=0)
|
||||
s.start()
|
||||
try:
|
||||
start = time.monotonic()
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
futures = [
|
||||
pool.submit(
|
||||
_post,
|
||||
f"http://127.0.0.1:{s.port}/execute",
|
||||
{"prompt": f"request {idx}"},
|
||||
)
|
||||
for idx in range(2)
|
||||
]
|
||||
results = [future.result(timeout=3.0) for future in futures]
|
||||
elapsed = time.monotonic() - start
|
||||
finally:
|
||||
s.stop()
|
||||
|
||||
assert [status for status, _body in results] == [200, 200]
|
||||
assert elapsed < 1.5
|
||||
|
||||
Reference in New Issue
Block a user