Two defects that only surface against the real DeepSeek API, both of which break the cost model rather than the code: deepseek-v4-flash enables thinking mode by default. The same single-turn question took 13.5s with it on and 1.8s with it off, and the thinking tokens are billed at the output rate. That misses NFR-PERF-02b (15s per turn) and inflates every conversational call. SceneConfig now carries an explicit `thinking` flag, all four V1 scenes set it to false, and the provider always sends the parameter rather than relying on the upstream default. Reasoning tokens are recorded separately in Usage.tokens_reasoning so the cost of ever turning it back on is visible. DeepSeek moves to peak/off-peak pricing on 2026-08-17, with peak (Beijing 09:00-12:00 and 14:00-18:00) at twice the off-peak rate. Reports are generated mostly during peak hours, so a single blended rate would leave ob_ai_call -- the only input to V2 pricing -- off by nearly a factor of two. PeakAwareRateTable resolves the rate at call time; cost is computed and stored then, never recomputed later at whatever the current rate happens to be. Also here: - Replace the live API key in the redaction test with a synthetic one. The test that proves we redact secrets was itself a secret leak waiting for its first commit. - Correct LLM_COST_RATES_JSON, which held placeholder rates. - Make ids.SnowflakeGenerator take an injectable clock so rollover and clock-rollback are testable without patching a slots class. - Rename exceptions to the -Error suffix (N818) and clear the remaining ruff, mypy and import-linter findings, including two dead lines: a duplicate pytestmark assignment and a no-op assert on a None return. Gates: ruff, mypy (96 files incl. tests), 4/4 import contracts, detect-secrets, 166 unit + 3 integration tests, migration up/down round-trip.
95 lines
3.0 KiB
Python
Executable File
95 lines
3.0 KiB
Python
Executable File
"""Circuit breaker behaviour (架构文档 §8.1 故障转移)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from packages.llm.circuit import CircuitBreaker, CircuitBreakerRegistry, CircuitState
|
|
|
|
|
|
class FakeClock:
|
|
def __init__(self) -> None:
|
|
self.now = 0.0
|
|
|
|
def __call__(self) -> float:
|
|
return self.now
|
|
|
|
def advance(self, seconds: float) -> None:
|
|
self.now += seconds
|
|
|
|
|
|
def state_of(breaker: CircuitBreaker) -> CircuitState:
|
|
"""Read the state through a call so mypy does not narrow it across mutations.
|
|
|
|
`state` is a property; mypy treats it like an attribute and keeps the narrowed
|
|
literal from an earlier assert, then reports later asserts as unreachable.
|
|
"""
|
|
return breaker.state
|
|
|
|
|
|
def test_starts_closed_and_allows_traffic() -> None:
|
|
breaker = CircuitBreaker("p", failure_threshold=3, clock=FakeClock())
|
|
assert state_of(breaker) is CircuitState.CLOSED
|
|
assert breaker.allow()
|
|
|
|
|
|
def test_opens_only_after_threshold_consecutive_failures() -> None:
|
|
breaker = CircuitBreaker("p", failure_threshold=3, clock=FakeClock())
|
|
breaker.record_failure()
|
|
breaker.record_failure()
|
|
assert state_of(breaker) is CircuitState.CLOSED
|
|
breaker.record_failure()
|
|
assert state_of(breaker) is CircuitState.OPEN
|
|
assert not breaker.allow()
|
|
|
|
|
|
def test_success_resets_the_failure_streak() -> None:
|
|
breaker = CircuitBreaker("p", failure_threshold=3, clock=FakeClock())
|
|
breaker.record_failure()
|
|
breaker.record_failure()
|
|
breaker.record_success()
|
|
breaker.record_failure()
|
|
assert state_of(breaker) is CircuitState.CLOSED
|
|
|
|
|
|
def test_half_open_probe_after_cooldown_then_closes_on_success() -> None:
|
|
clock = FakeClock()
|
|
breaker = CircuitBreaker("p", failure_threshold=1, cooldown_seconds=30.0, clock=clock)
|
|
breaker.record_failure()
|
|
assert state_of(breaker) is CircuitState.OPEN
|
|
|
|
clock.advance(29.0)
|
|
assert not breaker.allow()
|
|
|
|
clock.advance(2.0)
|
|
assert breaker.allow()
|
|
assert state_of(breaker) is CircuitState.HALF_OPEN
|
|
# only one probe is admitted
|
|
assert not breaker.allow()
|
|
|
|
breaker.record_success()
|
|
assert state_of(breaker) is CircuitState.CLOSED
|
|
assert breaker.allow()
|
|
|
|
|
|
def test_half_open_failure_reopens_and_restarts_cooldown() -> None:
|
|
clock = FakeClock()
|
|
breaker = CircuitBreaker("p", failure_threshold=1, cooldown_seconds=10.0, clock=clock)
|
|
breaker.record_failure()
|
|
clock.advance(11.0)
|
|
assert breaker.allow()
|
|
|
|
breaker.record_failure()
|
|
assert state_of(breaker) is CircuitState.OPEN
|
|
assert not breaker.allow()
|
|
|
|
clock.advance(11.0)
|
|
assert breaker.allow()
|
|
|
|
|
|
def test_registry_returns_one_breaker_per_provider() -> None:
|
|
registry = CircuitBreakerRegistry(failure_threshold=2, cooldown_seconds=5.0)
|
|
assert registry.get("deepseek") is registry.get("deepseek")
|
|
assert registry.get("deepseek") is not registry.get("zhipu")
|
|
registry.get("deepseek").record_failure()
|
|
registry.get("deepseek").record_failure()
|
|
assert registry.snapshot() == {"deepseek": "open", "zhipu": "closed"}
|