Files
CyberGril/tools/seal_budget_hel879.py
T

68 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""HEL-879 · A(团队测试实例)显式封账工具——体验链提前走完时的可执行关闭授权。
总管独立复审复现的失败边界:临时库使用 51/55 → 停服/backup 归档 → 原配置重开
后第 52 次仍 allowed——「走完体验链」与「耗尽 55」并不等价,没有可执行封账。
本工具把关闭授权落成一个显式、可审计的动作(budget-package-hel895.md §6.2):
python3 tools/seal_budget_hel879.py --db /opt/<8360实例>/data/<产品库>.sqlite3 [--reason "体验链走完"]
行为:
- 在该库 `budget_total` 原子写入 sealed/sealed_requests/sealed_tokens/sealed_at;
- 真实用量照实保留(如 51),不伪造 55、不清零、不改其他行;
- 此后该库任何 `PersistentBudget.acquire` 在发送前抛 `budget_sealed:<used>/<cap>`,
跨重启、跨 backup API 归档/快照恢复同样拒绝(负例见
`tools/verify_budget_split_hel896.py` S6);
- 无解除封账的代码路径:团队复检 = 新批准 + 新实例/新库(§6.2.4)。
封账顺序:先停服 → 跑本工具 → `tools/release_snapshot_hel896.py` 一致性快照
(封账标记随库一同归档)。本工具只写上述 4 行标记,零 provider 调用、零网络。
"""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from cybergril.life.model import PersistentBudget # noqa: E402
from cybergril.memory.store import Store # noqa: E402
from cybergril.think.budget import BudgetConfig, BudgetGuard, BudgetStop # noqa: E402
def main() -> int:
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("--db", required=True, type=Path, help="团队实例产品库路径")
ap.add_argument("--reason", default="", help="封账原因(记录在交付评论,不写入库)")
ap.add_argument("--scope", default="", help="budget_total 键 scope(如 hel919:);缺省=旧账原键")
args = ap.parse_args()
if not args.db.is_file():
print(json.dumps({"ok": False, "error": f"库不存在:{args.db}"}, ensure_ascii=False))
return 1
store = Store(args.db, init=False)
guard = PersistentBudget(store, BudgetGuard(BudgetConfig()), scope=args.scope)
before = guard.total_requests_used()
marker = guard.seal()
# 自检:封账后 acquire 必须被拒且不落账(拒绝路径在任何写入之前)。
try:
guard.acquire("dialogue_main", tokens=1)
rejected, message = False, "allowed(封账失效!)"
except BudgetStop as exc:
rejected, message = True, str(exc)
after = guard.total_requests_used()
ok = (marker["requests"] == before == after and rejected
and message.startswith(f"budget_sealed:{marker['requests']}"))
print(json.dumps({"ok": ok, "db": str(args.db), "reason": args.reason,
"sealed_marker": marker, "real_usage_preserved": before == after,
"post_seal_acquire": message}, ensure_ascii=False, indent=1))
store.close()
return 0 if ok else 1
if __name__ == "__main__":
raise SystemExit(main())