T0: absorb shared/ into backend — cot_logger→src/core, CotJsonFormatter→src/core/cot_formatter.py, _llm_http/_llm_health/ssl→src/core/utils; imports rewritten (26 prod + tests, patch targets); run.sh/backend.Dockerfile/requirements/.axiom source_dirs/semantic_health/AGENTS/INSTALL cleaned; ADR-0022 supersedes ADR-0015; fixed latent CI defects (ss_tools ImportError, record.message in logger tests, same-name test-module collision). ADR-0021 wire enrichment (additive): contract_id/claim/error_code/loc fields; _contract_id ContextVar + resolve_contract_id (explicit > belief_scope > declared-src mirror, derived src never mirrors); EXPLORE auto-loc via single frame walk; facade error auto-fill; 2KB payload cap with payload_truncated/payload_bytes markers; migrated 85 error="CODE" sites to error_code= (12 files); pilot editor/load.py; superset preview payload-bomb inlined bodies removed. Analytics SSOT src/core/log_stats.py (bond transition matrix, orphan-EXPLORE ratio, REFLECT pairing, intent families, coverage, insufficient-sample flag); pretty_cot.py --stats/--digest/--trajectory/--story over one engine; log_gap_service three-tier ground-truth triangulation (FAILED w/o EXPLORE etc.) + GET /api/reports/log-stats|task-log-gaps (polling-suppressed); scripts/cot_audit.py CLI; enriched fields persisted into task_logs.payload for tier queries. Frontend: ReportsAnalyticsModel + AnalyticsStatsPanel (Logs tab) + TaskGapPanel and per-row T1/T2/T3 gap badges (Tasks tab); cot-logger.ts ADR-0021 opts; i18n en/ru. Scheduler console spam fixed: apscheduler logger demoted to WARNING via LoggingConfig.scheduler_log_level. .axiom belief patterns -> $OBJ.* (alias undercount). molecular-cot-logging skill updated (fields, decision rules, tie-break, CLI) and synced. Reviewed orthogonally: F1 cot_span contract pollution, F2 cap boundary accounting, F3 digest over-dedup, F4 trace-state bound, F5 tier metadata — fixed with regression tests. Validation: backend 11287 passed + ruff + compileall; frontend 3446 passed + lint + build; CLI smoke on live app.log.
207 lines
8.7 KiB
Python
207 lines
8.7 KiB
Python
# #region Test.Translate.TestLLMAsyncHttp [C:3] [TYPE Module] [SEMANTICS test, translate, llm, async, httpx, rate-limit]
|
|
# @BRIEF Tests for async LLM HTTP client — rate-limit retry, asyncio.sleep backoff, structured output fallback.
|
|
# @RELATION BINDS_TO -> [Plugin.LlmAsyncHttp.LLMAsyncHttpClient]
|
|
# @RELATION BINDS_TO -> [Plugin.LlmCall.CallOpenaiCompatible]
|
|
# @TEST_CONTRACT: 429 retry uses asyncio.sleep -> no event loop block
|
|
# @TEST_EDGE: rate_limit_429_retries -> VERIFIED_BY: test_llm_rate_limit_uses_asyncio_sleep
|
|
# @TEST_EDGE: empty_choices_raises -> VERIFIED_BY: test_call_openai_compatible_empty_choices
|
|
# @TEST_EDGE: missing_base_url_raises -> VERIFIED_BY: test_call_openai_compatible_no_base_url
|
|
import asyncio
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
|
|
# #region Test.Translate.TestLlmRateLimitUsesAsyncioSleep [C:2] [TYPE Function]
|
|
# @BRIEF Verify 429 backoff doesn't block other requests — mock httpx with 2 failures then success.
|
|
# @TEST_EDGE: rate_limit_429_retries
|
|
@pytest.mark.asyncio
|
|
async def test_llm_rate_limit_uses_asyncio_sleep():
|
|
"""Verify call_openai_compatible retries on 429 using asyncio.sleep, not time.sleep."""
|
|
from src.core.utils.llm_http import call_openai_compatible
|
|
|
|
call_count = 0
|
|
|
|
def mock_post_side_effect(*args, **kwargs):
|
|
nonlocal call_count
|
|
call_count += 1
|
|
if call_count <= 2:
|
|
resp = MagicMock(spec=httpx.Response)
|
|
resp.status_code = 429
|
|
resp.ok = False
|
|
resp.is_success = False
|
|
resp.text = "Rate limited"
|
|
resp.headers = {"Retry-After": "1"}
|
|
resp.json.return_value = {}
|
|
return resp
|
|
resp = MagicMock(spec=httpx.Response)
|
|
resp.status_code = 200
|
|
resp.ok = True
|
|
resp.is_success = True
|
|
resp.text = '{"choices":[{"message":{"content":"{\\"translated\\": \\"hola\\"}"},"finish_reason":"stop"}]}'
|
|
resp.json.return_value = {
|
|
"choices": [{"message": {"content": '{"translated": "hola"}'}, "finish_reason": "stop"}]
|
|
}
|
|
return resp
|
|
|
|
mock_client = AsyncMock()
|
|
mock_client.post = AsyncMock(side_effect=mock_post_side_effect)
|
|
|
|
with patch("src.core.utils.llm_http.get_shared_http_client", return_value=mock_client):
|
|
# Patch asyncio.sleep to verify it's called (not time.sleep)
|
|
sleep_calls = []
|
|
original_sleep = asyncio.sleep
|
|
|
|
async def tracking_sleep(seconds):
|
|
sleep_calls.append(seconds)
|
|
await original_sleep(0) # Don't actually wait
|
|
|
|
with patch("src.core.utils.llm_http.asyncio.sleep", side_effect=tracking_sleep):
|
|
content, finish_reason = await call_openai_compatible(
|
|
base_url="http://fake-llm.local",
|
|
api_key="test-key",
|
|
model="gpt-4",
|
|
prompt="translate: hello",
|
|
)
|
|
|
|
assert call_count == 3, f"Expected 3 HTTP calls (2 retries + 1 success), got {call_count}"
|
|
assert len(sleep_calls) == 2, f"Expected 2 sleep calls for 429 retry, got {len(sleep_calls)}"
|
|
assert finish_reason == "stop"
|
|
# #endregion Test.Translate.TestLlmRateLimitUsesAsyncioSleep
|
|
|
|
|
|
# #region Test.Translate.TestCallOpenaiCompatibleNoBaseUrl [C:2] [TYPE Function]
|
|
# @BRIEF Verify call_openai_compatible raises ValueError when base_url is empty.
|
|
# @TEST_EDGE: missing_base_url_raises
|
|
@pytest.mark.asyncio
|
|
async def test_call_openai_compatible_no_base_url():
|
|
"""Verify empty base_url raises ValueError immediately."""
|
|
from src.core.utils.llm_http import call_openai_compatible
|
|
|
|
with pytest.raises(ValueError, match="no base_url"):
|
|
await call_openai_compatible(
|
|
base_url="",
|
|
api_key="test-key",
|
|
model="gpt-4",
|
|
prompt="hello",
|
|
)
|
|
|
|
|
|
# #endregion Test.Translate.TestCallOpenaiCompatibleNoBaseUrl
|
|
|
|
|
|
# #region Test.Translate.TestCallOpenaiCompatibleEmptyChoices [C:2] [TYPE Function]
|
|
# @BRIEF Verify call_openai_compatible raises ValueError when LLM returns empty choices.
|
|
# @TEST_EDGE: empty_choices_raises
|
|
@pytest.mark.asyncio
|
|
async def test_call_openai_compatible_empty_choices():
|
|
"""Verify LLM response with empty choices raises ValueError."""
|
|
from src.core.utils.llm_http import call_openai_compatible
|
|
|
|
mock_response = MagicMock(spec=httpx.Response)
|
|
mock_response.status_code = 200
|
|
mock_response.is_success = True
|
|
mock_response.json.return_value = {"choices": []}
|
|
mock_response.text = '{"choices": []}'
|
|
|
|
mock_client = AsyncMock()
|
|
mock_client.post = AsyncMock(return_value=mock_response)
|
|
|
|
with patch("src.core.utils.llm_http.get_shared_http_client", return_value=mock_client):
|
|
with patch("src.core.utils.llm_http._do_http_request", AsyncMock(return_value=(mock_response, mock_response.text))):
|
|
with patch("src.core.utils.llm_http._handle_response_format_fallback", AsyncMock(side_effect=lambda _c, resp, text, *a, **k: (resp, text))):
|
|
with pytest.raises(ValueError, match="no choices"):
|
|
await call_openai_compatible(
|
|
base_url="http://fake-llm.local",
|
|
api_key="test-key",
|
|
model="gpt-4",
|
|
prompt="hello",
|
|
)
|
|
|
|
|
|
# #endregion Test.Translate.TestCallOpenaiCompatibleEmptyChoices
|
|
|
|
|
|
# #region Test.Translate.TestCallOpenaiCompatibleRefusal [C:2] [TYPE Function]
|
|
# @BRIEF Verify call_openai_compatible raises ValueError when LLM returns a refusal.
|
|
# @TEST_EDGE: llm_refusal_raises
|
|
@pytest.mark.asyncio
|
|
async def test_call_openai_compatible_refusal():
|
|
"""Verify LLM refusal response raises ValueError."""
|
|
from src.core.utils.llm_http import call_openai_compatible
|
|
|
|
mock_response = MagicMock(spec=httpx.Response)
|
|
mock_response.status_code = 200
|
|
mock_response.is_success = True
|
|
mock_response.json.return_value = {
|
|
"choices": [{"message": {"content": "", "refusal": "I cannot translate this"}, "finish_reason": "stop"}]
|
|
}
|
|
mock_response.text = '{"choices": [{"message": {"content": "", "refusal": "I cannot translate this"}, "finish_reason": "stop"}]}'
|
|
|
|
mock_client = AsyncMock()
|
|
mock_client.post = AsyncMock(return_value=mock_response)
|
|
|
|
with patch("src.core.utils.llm_http.get_shared_http_client", return_value=mock_client):
|
|
with patch("src.core.utils.llm_http._do_http_request", AsyncMock(return_value=(mock_response, mock_response.text))):
|
|
with patch("src.core.utils.llm_http._handle_response_format_fallback", AsyncMock(side_effect=lambda _c, resp, text, *a, **k: (resp, text))):
|
|
with pytest.raises(ValueError, match="refused"):
|
|
await call_openai_compatible(
|
|
base_url="http://fake-llm.local",
|
|
api_key="test-key",
|
|
model="gpt-4",
|
|
prompt="hello",
|
|
)
|
|
|
|
|
|
# #endregion Test.Translate.TestCallOpenaiCompatibleRefusal
|
|
|
|
|
|
# #region Test.Translate.TestRetryDoesNotBlockEventLoop [C:2] [TYPE Function]
|
|
# @BRIEF Verify 429 retry sleep doesn't block concurrent tasks on the same event loop.
|
|
# @TEST_INVARIANT: retry_backoff_non_blocking -> VERIFIED_BY: test_retry_does_not_block_event_loop
|
|
@pytest.mark.asyncio
|
|
async def test_retry_does_not_block_event_loop():
|
|
"""Verify that 429 retry backoff allows other coroutines to run."""
|
|
from src.core.utils.llm_http import _do_http_request
|
|
|
|
concurrent_ran = False
|
|
|
|
async def concurrent_task():
|
|
nonlocal concurrent_ran
|
|
await asyncio.sleep(0.01)
|
|
concurrent_ran = True
|
|
|
|
call_count = 0
|
|
|
|
def mock_post(*args, **kwargs):
|
|
nonlocal call_count
|
|
call_count += 1
|
|
if call_count == 1:
|
|
resp = MagicMock(spec=httpx.Response)
|
|
resp.status_code = 429
|
|
resp.is_success = False
|
|
resp.text = "Rate limited"
|
|
resp.headers = {"Retry-After": "1"}
|
|
return resp
|
|
resp = MagicMock(spec=httpx.Response)
|
|
resp.status_code = 200
|
|
resp.is_success = True
|
|
resp.text = "ok"
|
|
return resp
|
|
|
|
mock_client = AsyncMock()
|
|
mock_client.post = AsyncMock(side_effect=mock_post)
|
|
|
|
with patch("src.core.utils.llm_http.asyncio.sleep", new_callable=AsyncMock) as mock_sleep:
|
|
# Run the HTTP request alongside a concurrent task
|
|
http_task = asyncio.create_task(_do_http_request(mock_client, "http://fake", {}, {}))
|
|
conc_task = asyncio.create_task(concurrent_task())
|
|
await asyncio.gather(http_task, conc_task)
|
|
|
|
assert concurrent_ran, "Concurrent task was blocked by 429 retry"
|
|
assert mock_sleep.called, "asyncio.sleep was not called during 429 retry"
|
|
# #endregion Test.Translate.TestRetryDoesNotBlockEventLoop
|
|
|
|
# #endregion Test.Translate.TestLLMAsyncHttp
|