"""HEL-882 hard gates: the execution table's caps stop calls at the wire. Reproduces the manager's independent review failure first (api review: daily_tokens=1 with 2962 already used still fired a provider request and returned chat.final), then proves the gate: every real provider exit — main dialogue, structured background scenes, and the mem0 SDK's internal extraction LLM — pre-allocates persistently before the send and refuses at the tightest cap with ZERO provider calls. Counters survive restarts. """ import pytest from conftest import engineering_budget from fastapi.testclient import TestClient from test_api import CREATE, events, turn from cybergril.api.app import create_app from cybergril.contracts import LLMDelta from cybergril.life.runtime import LifeConfig from cybergril.memory.store import Store from cybergril.think.budget import BudgetConfig, BudgetGuard, BudgetStop from cybergril.think.router import build_router from cybergril.think.scenes import Scene, Tier class GateLLM: """Fake provider that records every stream call.""" def __init__(self): self.calls = [] async def stream(self, messages, **kwargs): self.calls.append(messages) if messages and "只返回JSON" in messages[0].content: yield LLMDelta(text='{"ok": true}') yield LLMDelta(done=True, finish_reason="stop", usage={"total_tokens": 12}) return yield LLMDelta(text="好") yield LLMDelta(text="的") yield LLMDelta(done=True, finish_reason="stop", usage={"total_tokens": 12}) async def aclose(self): return None def gated_app(tmp_path, *, budget=None, db="gates.db"): provider = GateLLM() router = build_router( providers={t: provider for t in (Tier.HIGH, Tier.STANDARD, Tier.LOW)}, budget=budget or engineering_budget(), ) app = create_app(db_path=tmp_path / db, router=router, life_config=LifeConfig(enabled=True, poll_seconds=3600), semantic_mode="off") return app, provider def create_companion(client): assert client.post("/api/companion", json=CREATE).status_code == 201 def test_daily_token_wall_refuses_before_any_provider_call(tmp_path): """The review's exact reproduction: over the daily token top, a turn must not reach the provider — honest chat.error, counters unchanged.""" app, provider = gated_app(tmp_path) with TestClient(app) as client: create_companion(client) guard = app.state.life.router.guard guard.config.daily_tokens = 1 guard.record(2962, "review_budget_already_used") used_before, calls_before = guard.used, len(provider.calls) result = [e["t"] for e in events(turn(client, "合成测试:额度用完后仍能发出请求吗"))] assert result == ["chat.error"] # honest refusal, never a fake reply assert len(provider.calls) == calls_before # ZERO provider requests assert guard.used == used_before # nothing further pre-allocated def test_dialogue_request_cap_persists_across_restart(tmp_path): """Category daily request caps are persistent: 1 dialogue request/day lets exactly one turn through; a second turn — in the SAME process or after a full restart on the same database — refuses with zero provider calls.""" budget = engineering_budget(daily_requests={"dialogue": 1}) app, provider = gated_app(tmp_path, budget=budget) with TestClient(app) as client: create_companion(client) assert [e["t"] for e in events(turn(client, "第一句"))][-1] == "chat.final" calls_after_first = len(provider.calls) assert [e["t"] for e in events(turn(client, "第二句"))] == ["chat.error"] assert len(provider.calls) == calls_after_first # restart: fresh app, fresh router, SAME database — the cap still holds app2, _provider2 = gated_app(tmp_path, budget=budget) with TestClient(app2) as client: assert [e["t"] for e in events(turn(client, "重启后"))] == ["chat.error"] assert app2.state.life.router.guard.requests_used("dialogue") == 1 @pytest.mark.asyncio async def test_background_category_caps_stop_structured_calls(tmp_path): """structured() exits (life events, gists, judgments) count into their own category: a 1/day life cap refuses the second generation before the wire.""" app, provider = gated_app(tmp_path, budget=engineering_budget( daily_requests={"dialogue": 100, "life": 1, "background": 100})) with TestClient(app) as client: create_companion(client) guard = app.state.life.router.guard assert guard.requests_used("background") >= 0 # persistent guard wired calls_before = len(provider.calls) from cybergril.life.model import structured scene = Scene.LIFE_EVENT payload = {"time": "2026-10-26T10:00:00", "persona": "p", "activity_state": {}} raw, _ = await structured(app.state.life.router, app.state.life.models, scene, '返回 {"ok": true}', payload) assert raw == {"ok": True} assert len(provider.calls) == calls_before + 1 with pytest.raises(BudgetStop, match="budget_requests_exhausted:life"): await structured(app.state.life.router, app.state.life.models, scene, '返回 {"ok": true}', payload) assert len(provider.calls) == calls_before + 1 # refused before the wire def test_default_config_is_fail_closed_for_every_category(): guard = BudgetGuard(BudgetConfig()) for scene in ("dialogue_main", "life_event", "memory_gist", "mem0_extract"): with pytest.raises(BudgetStop): guard.acquire(scene, requests=1, tokens=10) def test_in_memory_guard_tightest_constraint_and_settlement(): guard = BudgetGuard(BudgetConfig(daily_tokens=1000, daily_requests={"dialogue": 2}, total_requests=3), today="2026-10-26") hold1 = guard.acquire("dialogue_main", requests=1, tokens=300) assert guard.requests_used("dialogue") == 1 # daily token wall: the tightest cap decides, not the request count with pytest.raises(BudgetStop, match="budget_tokens_exhausted"): guard.acquire("dialogue_main", requests=1, tokens=800) hold2 = guard.acquire("dialogue_main", requests=1, tokens=100) # category daily top reached with pytest.raises(BudgetStop, match="budget_requests_exhausted:dialogue"): guard.acquire("proactive_message", requests=1, tokens=10) # one-shot total: third request would pass the daily top of 2? no — category # top already 2/2; use another category for the total check guard3 = BudgetGuard(BudgetConfig(daily_tokens=1000, daily_requests={"dialogue": 10}, total_requests=2), today="2026-10-26") guard3.acquire("dialogue_main", requests=1, tokens=10) guard3.acquire("dialogue_main", requests=1, tokens=10) with pytest.raises(BudgetStop, match="budget_total_requests_exhausted"): guard3.acquire("dialogue_main", requests=1, tokens=10) # settle: known usage adjusts, unknown keeps the whole reservation guard.settle(hold1, actual_tokens=250) assert guard.used == 350 # 250 + hold2's 100 guard.settle(hold2, actual_tokens=None) # disconnect: keep full reserve assert guard.used == 350 # release: a provably-unsent hold refunds requests and tokens guard4 = BudgetGuard(BudgetConfig(daily_tokens=1000, daily_requests={"dialogue": 1}), today="2026-10-26") hold = guard4.acquire("dialogue_main", requests=1, tokens=100) guard4.release(hold) assert guard4.used == 0 and guard4.requests_used("dialogue") == 0 def test_persistent_budget_counters_survive_restart(tmp_path): from cybergril.life.model import PersistentBudget store = Store(tmp_path / "persist.db") guard = PersistentBudget(store, BudgetGuard(engineering_budget(), today="2026-10-26")) guard.acquire("dialogue_main", requests=1, tokens=111) guard.acquire("mem0_extract", requests=2, tokens=22) store.close() store2 = Store(tmp_path / "persist.db") guard2 = PersistentBudget(store2, BudgetGuard(engineering_budget(), today="2026-10-26")) assert guard2.requests_used("dialogue") == 1 assert guard2.requests_used("semantic") == 2 assert guard2.used == 133 # restart did not reset the day's tokens store2.close() def test_total_tokens_cap_blocks_before_daily_wall(tmp_path): app, provider = gated_app(tmp_path, budget=engineering_budget( daily_requests={"dialogue": 100}, total_tokens=5000)) with TestClient(app) as client: create_companion(client) guard = app.state.life.router.guard # HEL-885: the one-shot TOTAL must be booked in budget_total — the old # setup used record(), which only writes the DAILY table, and passed # merely because total_tokens_used() CRASHED on the then-empty # budget_total (TypeError surfaced as chat.error). Book the total # honestly now that the read is fixed. with guard.store.conn: guard.store.execute( "INSERT INTO budget_total VALUES('total_tokens',4999) " "ON CONFLICT(key) DO UPDATE SET value=4999") calls_before = len(provider.calls) assert [e["t"] for e in events(turn(client, "还会发吗"))] == ["chat.error"] assert len(provider.calls) == calls_before