Files
CyberGril/deploy/hel835_guard.py
T

199 lines
7.1 KiB
Python

"""HEL-835 deploy-layer call guard: durable cross-run caps + per-call ledger.
Deployment-layer protection only; no product code is modified. The service entry
wraps every tier provider with GuardedProvider, which:
- classifies each provider.stream() call by its system-prompt instruction
(same feature strings the test doubles use);
- atomically reserves a slot in a file-locked JSON ledger BEFORE the call;
- hard-refuses (RuntimeError, no upstream call) when a capped category is at
its cumulative limit; failures/retries also count (attempts, not successes);
- records every uncapped background category (growth, life, judge, other)
call-by-call without blocking it, so the ledger stays a complete account.
Caps (cumulative across runs/restarts, including failures and auto retries):
memory_gist <= 8, dialogue_main (real chat turns) <= 6, persona_generation <= 1.
Ledger path: $CYBERGRIL_GUARD_LEDGER (default <data>/guard-ledger.json).
Run: python -m deploy.hel835_guard (instead of python -m cybergril.api)
"""
from __future__ import annotations
import fcntl
import json
import logging
import os
import time
from pathlib import Path
import uvicorn
from cybergril.api.app import create_app
from cybergril.config import ModelsConfig
from cybergril.think.budget import BudgetConfig, BudgetGuard
from cybergril.think.router import ModelRouter
from cybergril.think.scenes import Tier
from cybergril.think.wiring import build_routing, build_tier_providers
log = logging.getLogger("hel835.guard")
CAPS = {
"memory_gist": 8,
"dialogue_main": 6,
"persona_generation": 1,
}
# Instruction feature -> ledger category. Order matters (first match wins).
FEATURES = [
("memory_gist", "压缩这条已经成功保存的聊天"),
("persona_generation", "从原始出生资料自主形成有张力"),
("growth_extract", "只从这一条真实已发生资料提炼后天成长"),
("life_event", "生成此刻真实运行中的一条世界内日常经历"),
("sharing_judge", "判定回复是否实际讲述候选中的具体经历"),
]
def classify(messages) -> str:
"""Classify a provider call from its system prompt (messages[0])."""
try:
head = messages[0].content
except (AttributeError, IndexError, TypeError):
return "unknown"
for category, feature in FEATURES:
if feature in head:
return category
if "只返回JSON,不要Markdown" in head:
return "other_structured"
return "dialogue_main"
class GuardLedger:
"""File-locked durable ledger; the single authoritative call account."""
def __init__(self, path):
self.path = Path(path)
self.path.parent.mkdir(parents=True, exist_ok=True)
if not self.path.exists():
raise SystemExit(
f"hel835 guard: refusing to start without an authoritative ledger at {self.path}; "
"create it from the audited call history first (never zero)."
)
def _locked(self, mode="a+"):
fh = open(self.path, mode, encoding="utf-8")
fcntl.flock(fh, fcntl.LOCK_EX)
return fh
@staticmethod
def _save(fh, data):
fh.seek(0)
fh.truncate(0)
json.dump(data, fh, ensure_ascii=False, indent=1)
fh.flush()
def counts(self):
with self._locked() as fh:
fh.seek(0)
data = json.load(fh)
return {c: int(data.get("counts", {}).get(c, 0)) for c in CAPS}, data
def reserve(self, category):
"""Atomically count one attempt; refuse capped categories at limit."""
with self._locked() as fh:
fh.seek(0)
data = json.load(fh)
counts = data.setdefault("counts", {})
used = int(counts.get(category, 0))
cap = CAPS.get(category)
if cap is not None and used >= cap:
self._save(fh, data)
raise RuntimeError(f"hel835_guard_cap:{category}:{used}/{cap}")
counts[category] = used + 1
entry = {
"t": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"category": category,
"pid": os.getpid(),
"outcome": "attempt",
}
calls = data.setdefault("calls", [])
calls.append(entry)
data["calls"] = calls[-2000:]
self._save(fh, data)
return entry
def settle(self, entry, outcome, detail=""):
with self._locked() as fh:
fh.seek(0)
data = json.load(fh)
for call in reversed(data.get("calls", [])):
if (call.get("t") == entry["t"] and call.get("pid") == entry["pid"]
and call.get("category") == entry["category"]):
call["outcome"] = outcome
if detail:
call["detail"] = str(detail)[:300]
break
self._save(fh, data)
class GuardedProvider:
"""Wraps one tier provider; classification happens per stream() call."""
def __init__(self, real, ledger):
self.real = real
self.ledger = ledger
async def stream(self, messages, **kwargs):
"""Async generator, like the wrapped provider; reserves on first pull."""
category = classify(messages)
entry = self.ledger.reserve(category)
log.info("hel835 guard call category=%s", category)
upstream = self.real.stream(messages, **kwargs)
try:
while True:
delta = await upstream.__anext__()
yield delta
except StopAsyncIteration:
self.ledger.settle(entry, "ok")
except BaseException as exc:
self.ledger.settle(entry, "fail", repr(exc))
raise
def __getattr__(self, name):
return getattr(self.real, name)
def build_guarded_router(models, ledger_path):
providers = build_tier_providers(models)
ledger = GuardLedger(ledger_path)
guarded = {tier: (GuardedProvider(p, ledger) if p is not None else None)
for tier, p in providers.items()}
return ModelRouter(
providers=guarded,
routing=build_routing(models),
guard=BudgetGuard(BudgetConfig(
daily_tokens=models.budget.daily_tokens,
warn_ratio=models.budget.warn_ratio,
hard_stop_ratio=models.budget.hard_stop_ratio,
)),
)
def main():
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)s %(levelname)s %(message)s")
import yaml
config = Path(os.environ.get("CYBERGRIL_MODELS", "config/models.yaml"))
models = ModelsConfig(**yaml.safe_load(config.read_text()))
ledger_path = os.environ.get(
"CYBERGRIL_GUARD_LEDGER",
str(Path(os.environ["CYBERGRIL_DB"]).parent / "guard-ledger.json"),
)
router = build_guarded_router(models, ledger_path)
app = create_app(router=router)
uvicorn.run(app, # noqa: PLC0415 - deploy entry, single worker, no reload
host=os.environ.get("CYBERGRIL_HOST", "127.0.0.1"),
port=int(os.environ.get("CYBERGRIL_PORT", "8350")),
workers=1)
if __name__ == "__main__":
main()