Files
CyberGril/tests/test_hard_gates_hel882.py
T
zonggongandmultica-agent e291058754 HEL-882 rework: execution-table caps become real hard gates at the wire, SDK hidden retries and mid-add failures recover without loss, the 8360 release package runs offline, and the HEL-874 embedding evidence is corrected - every real provider exit (create/dialogue/life/gist/judgment/growth/proactive phrasing + mem0's internal extraction LLM) now persistently pre-allocates request count (per-category daily + one-shot totals in request_usage/budget_total, BEGIN IMMEDIATE) and a conservative token estimate before sending, refuses at the tightest cap (BudgetStop, no more downgrade-and-keep-calling) and survives restarts; known usage settles to actual, unknown/disconnect/failure keeps the full reservation; ledger guards gain per-day tops (embed_daily/llm_daily) and verify_ledger_authority rejects a fresh file bypassing the frozen 498/500 prefix; the mem0 SDK client's hidden OpenAI retries are disabled (max_retries=0) so every actual send is gated and answered failures (even 5xx) settle as used with only httpx.ConnectError-grade unsent requests released; embed refusals the SDK swallows into empty adds are re-raised as budget errors so the batch re-queues without burning an attempt and resumes when the cap returns, add_group resumes at vector-store level (src_group anchors: only missing rows resent, crash/restart/concurrency never duplicate), sem coverage is per-backend (stub batches no longer block a real rebuild, real done batches never resend) with backend-scoped batch keys; the release package is offline-executable: fresh-dir git clone, cybergril service account, PYTHONPATH in the unit so the lock-only venv imports the product, shipped caps explicitly 0 (=refuse by design until approved), deploy/mem0-stack.lock + accurate 3.11 wheel gap with a no-download path (hel857-venv is 3.10; never present stub/FTS as the real memory experience), and tools/check_release_offline_hel882.py verifies checkout/import/health/fail-closed/approved-run/data-preservation on loopback with echo stand-ins; evidence corrected per manager review: docs/rework/hel874/erratum-hel882.json + README appendix record that e978e87's verification did real local embeddings from cache (provable lower bound 12, exact total unrecoverable, temp sandbox gone) while zero-real-LLM/zero-download/zero-outbound stand, old JSONs and authoritative ledgers untouched, and tools/verify_mem0_real_hel874.py now installs the deterministic 512-dim stand-in before TextEmbedding construction plus non-loopback socket rejection (zero model loads) with final_embed_rows persisted before temp cleanup; verification: 272 python tests pass on 3.11.16 venv + requirements.lock offline (16 new: manager api repro blocked with 0 provider calls, sdk repro 1-HTTP/max_retries=0/llm_ops=1, mid-add swallow requeued-then-resumed, resume-no-duplicate, stub-to-mem0 rebuild, daily embed cap, unified mem0-LLM gate, authority refusal, restart-persistent counters, tightest-constraint settle/release), ruff clean (src/tests/tools), web tsc + 42 vitest + build via npm ci --offline, pinned-install deterministic re-run 12/12, manager review scripts re-run evidence attached in delivery, dual-target requirement checks 4/4 with handoff-HEL-882.json; zero real calls, zero real embeddings, zero non-loopback outbound, authoritative ledgers and freezes untouched, not deployed, PR not merged, no visual changes
Co-authored-by: multica-agent <github@multica.ai>
2026-10-26 23:20:18 +08:00

189 lines
8.9 KiB
Python

"""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
guard.record(4999, "review_almost_total") # one-shot total nearly spent
calls_before = len(provider.calls)
assert [e["t"] for e in events(turn(client, "还会发吗"))] == ["chat.error"]
assert len(provider.calls) == calls_before