287 lines
18 KiB
Python
287 lines
18 KiB
Python
#!/usr/bin/env python3
|
||
"""HEL-896/879 · A/B/C 分账、阶段切换与提前封账验证(仅临时库 + 替身,零 provider、零网络)。
|
||
|
||
总管独立复审(HEL-896 附件 §1)复现的问题:把 A 的一次性总量 55 写进与 B/C 共用的
|
||
产品库 PersistentBudget 后,B 的 mem0_extract 与 C 的 dialogue_main 都被
|
||
budget_total_requests_exhausted:55/55 挡住,且 budget_total/request_usage/token_usage
|
||
无阶段或授权来源字段。本脚本在同一 head 上给出可执行的分账方案并把每一环验证出来:
|
||
|
||
S0 复现(单账本误配) —— A/B/C 共库时 55 总量挡住 B/C,表格确无阶段/来源字段;
|
||
S1 团队测试实例(A) —— 独立库 + total_requests=55/total_tokens=500000 + 当日
|
||
类别日顶:按 A 表逐步消费到 55,第 56 次任何类别被拒;
|
||
S2 封账 —— 重启不归零;SQLite backup API 一致性快照归档后,归档件
|
||
仍是 55/55 且继续拒绝(封账跨重启、跨归档成立);测试档案
|
||
只留在团队库;
|
||
S6 提前封账(HEL-879) —— 体验链 51/55 走完即显式 seal:真实用量保留 51(不伪造
|
||
55、不清零),第 52 次任何类别拒绝 budget_sealed:51/55;
|
||
误重启、backup API 归档恢复后同样拒绝;seal 幂等不覆盖;
|
||
S3 全新用户实例(B/C) —— 独立新库、不配置一次性总量:A 耗尽后 B(mem0_extract)
|
||
与 C(dialogue_main)照常放行;日顶叠加(dialogue 60、
|
||
semantic 40、daily_tokens)逐项到顶拒绝;拒绝不落账;
|
||
S4 误配守门 —— 用户实例若误写 total 55,B/C 再次被挡(这就是为什么发布
|
||
包禁止把 A 总量写进用户实例配置,发布卡按 §6 复核);
|
||
S5 冻结保全 —— 全程只写临时目录;仓库配置文件前后哈希不变。
|
||
|
||
真实 cap 全部保持 0:本脚本不读取也不写入任何生产/权威账(39/60、498/500、冻结 2 条、
|
||
HEL-835 闸与 hel857 权威账原样不动)。B 部分的 embed_ops/llm_ops 权威账闸(authority
|
||
fail-closed、逐行锚点、公式换算)由既有 verify_semantic_recovery_hel885.py /
|
||
verify_mem0_real_hel874.py 在固定 SDK 边界覆盖,不在本脚本重复。
|
||
|
||
用法:.venv311/bin/python tools/verify_budget_split_hel896.py [输出目录]
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import hashlib
|
||
import json
|
||
import sqlite3
|
||
import subprocess
|
||
import sys
|
||
import tempfile
|
||
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
|
||
|
||
REPO = Path(__file__).resolve().parents[1]
|
||
|
||
|
||
def git_head() -> str:
|
||
try:
|
||
return subprocess.run(["git", "rev-parse", "HEAD"], cwd=REPO, capture_output=True,
|
||
text=True, check=True).stdout.strip()
|
||
except Exception:
|
||
return "unknown"
|
||
|
||
|
||
HEAD = git_head()
|
||
RESULTS: list[tuple[str, bool, str]] = []
|
||
|
||
|
||
def check(name: str, ok: bool, detail: str = "") -> None:
|
||
RESULTS.append((name, ok, detail))
|
||
print(f"{'PASS' if ok else 'FAIL'} {name}" + (f" — {detail}" if detail else ""))
|
||
|
||
|
||
def acquire(guard: PersistentBudget, scene: str, tokens: int = 10, requests: int = 1) -> str:
|
||
try:
|
||
guard.acquire(scene, tokens=tokens, requests=requests)
|
||
return "allowed"
|
||
except BudgetStop as exc:
|
||
return str(exc)
|
||
|
||
|
||
def snapshot_via_backup_api(src: Path, dst: Path) -> None:
|
||
"""一致性快照:SQLite backup API(在线安全;WAL 中已提交行不丢)。"""
|
||
with sqlite3.connect(str(src)) as source, sqlite3.connect(str(dst)) as target:
|
||
source.backup(target)
|
||
|
||
|
||
def main() -> int:
|
||
out_dir = Path(sys.argv[1]).resolve() if len(sys.argv) > 1 else Path(__file__).resolve().parents[1] / "docs/rework/hel896/budget-split"
|
||
out_dir.mkdir(parents=True, exist_ok=True)
|
||
repo = Path(__file__).resolve().parents[1]
|
||
watched = [repo / "config/models.yaml", repo / "config/semantic.example.yaml"]
|
||
before_hashes = {p.name: hashlib.sha256(p.read_bytes()).hexdigest() for p in watched}
|
||
report: dict = {"head": HEAD, "no_provider_calls": True, "temp_only": True, "scenes": {}}
|
||
|
||
with tempfile.TemporaryDirectory(prefix="hel896-budget-split-") as tmp:
|
||
tmp = Path(tmp)
|
||
|
||
# ── S0:复现单账本误配(总管 review_hel895_budget.py 的同形复现) ──
|
||
s0 = Store(tmp / "single-ledger.db")
|
||
guard0 = PersistentBudget(s0, BudgetGuard(BudgetConfig(
|
||
daily_tokens=2_000_000,
|
||
daily_requests={"dialogue": 60, "life": 8, "background": 40, "semantic": 40},
|
||
total_requests=55, total_tokens=500_000)))
|
||
for _ in range(55):
|
||
guard0.acquire("dialogue_main", tokens=1)
|
||
b_blocked = acquire(guard0, "mem0_extract")
|
||
c_blocked = acquire(guard0, "dialogue_main")
|
||
columns = {t: [r["name"] for r in s0.execute(f"PRAGMA table_info({t})").fetchall()]
|
||
for t in ("budget_total", "request_usage", "token_usage")}
|
||
check("S0_single_ledger_repro_A55_blocks_B_and_C",
|
||
b_blocked.startswith("budget_total_requests_exhausted") and c_blocked.startswith("budget_total_requests_exhausted"),
|
||
f"B={b_blocked} C={c_blocked}")
|
||
check("S0_tables_have_no_stage_or_source_columns",
|
||
all("stage" not in c and "source" not in c and "auth" not in c for c in columns.values()),
|
||
json.dumps(columns, ensure_ascii=False))
|
||
report["scenes"]["S0_single_ledger"] = {"team_test_used": guard0.total_requests_used(),
|
||
"B_after_A": b_blocked, "C_after_A": c_blocked,
|
||
"table_columns": columns}
|
||
s0.close()
|
||
|
||
# ── S1:团队测试实例(A)——独立库,一次性总量 + 当日类别日顶,按 A 表逐步消费 ──
|
||
team = Store(tmp / "team-8360.db")
|
||
team_cfg = BudgetConfig(daily_tokens=2_000_000,
|
||
daily_requests={"dialogue": 60, "life": 8, "background": 40, "semantic": 40},
|
||
total_requests=55, total_tokens=500_000)
|
||
team_guard = PersistentBudget(team, BudgetGuard(team_cfg))
|
||
# A 表逐步(budget-plan-hel874 §1):创建/诠释/习惯 4(background)、对话 16(dialogue)、
|
||
# 生活 8(life)、判定/成长/gist 20(background)、主动措辞 3(dialogue),再按表耗到硬顶 55。
|
||
plan = [("fact_extract", 4), ("dialogue_main", 16), ("life_event", 8), ("memory_gist", 20), ("proactive_message", 3)]
|
||
steps_ok = True
|
||
for scene, n in plan:
|
||
for _ in range(n):
|
||
steps_ok &= acquire(team_guard, scene) == "allowed"
|
||
while team_guard.total_requests_used() < 55:
|
||
steps_ok &= acquire(team_guard, "dialogue_main") == "allowed"
|
||
s1_reject = acquire(team_guard, "dialogue_main")
|
||
# life 的当日 8 次在 A 表里已用满:拒绝来自更紧的类别日顶(叠加取最紧,同为硬闸);
|
||
# semantic 当日还有余量,第 56 次由一次性总量拒绝。
|
||
s1_reject_life = acquire(team_guard, "life_event")
|
||
s1_reject_semantic = acquire(team_guard, "mem0_extract")
|
||
check("S1_team_instance_A_consumes_55_then_hard_stops",
|
||
steps_ok and team_guard.total_requests_used() == 55
|
||
and s1_reject.startswith("budget_total_requests_exhausted")
|
||
and s1_reject_life.startswith("budget_requests_exhausted:life")
|
||
and s1_reject_semantic.startswith("budget_total_requests_exhausted"),
|
||
f"used={team_guard.total_requests_used()} dialogue={s1_reject} life={s1_reject_life} semantic={s1_reject_semantic}")
|
||
team_tokens_used = team_guard.total_tokens_used()
|
||
report["scenes"]["S1_team_instance"] = {"used": team_guard.total_requests_used(),
|
||
"tokens_used": team_tokens_used,
|
||
"step_plan": plan,
|
||
"after_55": {"dialogue": s1_reject, "life": s1_reject_life,
|
||
"semantic": s1_reject_semantic}}
|
||
|
||
# ── S2:封账——重启不归零;backup API 归档件同样封死;测试档案只在团队库 ──
|
||
team.close()
|
||
team_reopened = Store(tmp / "team-8360.db", init=False)
|
||
team_guard2 = PersistentBudget(team_reopened, BudgetGuard(team_cfg))
|
||
s2_after_restart = acquire(team_guard2, "dialogue_main")
|
||
archive = tmp / "team-8360-sealed.db"
|
||
snapshot_via_backup_api(tmp / "team-8360.db", archive)
|
||
sealed = Store(archive, init=False)
|
||
sealed_guard = PersistentBudget(sealed, BudgetGuard(team_cfg))
|
||
s2_archive = acquire(sealed_guard, "mem0_extract")
|
||
check("S2_seal_survives_restart_and_archive",
|
||
team_guard2.total_requests_used() == 55 and s2_after_restart.startswith("budget_total_requests_exhausted")
|
||
and sealed_guard.total_requests_used() == 55 and s2_archive.startswith("budget_total_requests_exhausted"),
|
||
f"restart={s2_after_restart} archive={s2_archive} used={sealed_guard.total_requests_used()}")
|
||
report["scenes"]["S2_seal"] = {"after_restart": s2_after_restart, "archive_after_backup_api": s2_archive}
|
||
team_reopened.close()
|
||
sealed.close()
|
||
|
||
# ── S6:提前封账(HEL-879)——体验链 51/55 走完即 seal,误重启/归档恢复均拒绝 ──
|
||
early = Store(tmp / "team-8360-early.db")
|
||
early_cfg = BudgetConfig(daily_tokens=2_000_000,
|
||
daily_requests={"dialogue": 60, "life": 8, "background": 40, "semantic": 40},
|
||
total_requests=55, total_tokens=500_000)
|
||
early_guard = PersistentBudget(early, BudgetGuard(early_cfg))
|
||
# A 表 51 次(创建/诠释/习惯4 + 对话16 + 生活8 + 判定/成长/gist20 + 主动措辞3)
|
||
# 就是体验链全部内容——走完即停,不补到 55。
|
||
early_plan = [("fact_extract", 4), ("dialogue_main", 16), ("life_event", 8),
|
||
("memory_gist", 20), ("proactive_message", 3)]
|
||
early_ok = True
|
||
for scene, n in early_plan:
|
||
for _ in range(n):
|
||
early_ok &= acquire(early_guard, scene) == "allowed"
|
||
used_before_seal = early_guard.total_requests_used()
|
||
marker = early_guard.seal()
|
||
s6_52nd = acquire(early_guard, "dialogue_main")
|
||
s6_other_scene = acquire(early_guard, "mem0_extract")
|
||
s6_daily_still_has_room = early_guard.requests_used("dialogue") # 19/60:日顶有余
|
||
check("S6a_early_finish_51_seals_real_usage_no_fake_no_zero",
|
||
early_ok and used_before_seal == 51 and marker["requests"] == 51
|
||
and s6_52nd.startswith("budget_sealed:51/55") and s6_other_scene.startswith("budget_sealed:51/55")
|
||
and early_guard.total_requests_used() == 51 and s6_daily_still_has_room == 19,
|
||
f"used={used_before_seal} dialogue_next={s6_52nd} semantic_next={s6_other_scene} "
|
||
f"after={early_guard.total_requests_used()} dialogue_daily={s6_daily_still_has_room}/60")
|
||
# seal 幂等:重复 seal 不覆盖原标记(sealed_at 不变)。
|
||
again = early_guard.seal()
|
||
idempotent = again == marker
|
||
# 误重启:重开库后第 52 次仍拒绝。
|
||
early.close()
|
||
early_reopened = Store(tmp / "team-8360-early.db", init=False)
|
||
early_guard2 = PersistentBudget(early_reopened, BudgetGuard(early_cfg))
|
||
s6_after_restart = acquire(early_guard2, "dialogue_main")
|
||
# 归档恢复:backup API 归档封账库 → 打开归档件 → 同样拒绝,用量仍 51。
|
||
early_archive = tmp / "team-8360-early-sealed.db"
|
||
snapshot_via_backup_api(tmp / "team-8360-early.db", early_archive)
|
||
early_arch = Store(early_archive, init=False)
|
||
early_arch_guard = PersistentBudget(early_arch, BudgetGuard(early_cfg))
|
||
s6_after_archive = acquire(early_arch_guard, "dialogue_main")
|
||
arch_marker = early_arch_guard.sealed()
|
||
check("S6b_seal_survives_restart_and_archive_restore",
|
||
idempotent and s6_after_restart.startswith("budget_sealed:51/55")
|
||
and s6_after_archive.startswith("budget_sealed:51/55")
|
||
and early_arch_guard.total_requests_used() == 51
|
||
and arch_marker is not None and arch_marker["requests"] == 51,
|
||
f"restart={s6_after_restart} archive={s6_after_archive} "
|
||
f"arch_used={early_arch_guard.total_requests_used()} marker={arch_marker}")
|
||
report["scenes"]["S6_early_seal"] = {
|
||
"plan_total": sum(n for _, n in early_plan), "used_at_seal": used_before_seal,
|
||
"marker": marker, "next_after_seal": s6_52nd, "other_scene_after_seal": s6_other_scene,
|
||
"after_restart": s6_after_restart, "after_archive_restore": s6_after_archive,
|
||
"idempotent_reseal": idempotent}
|
||
early_reopened.close()
|
||
early_arch.close()
|
||
|
||
# ── S3:全新用户实例(B/C)——独立新库、无一次性总量:A 耗尽后 B/C 照常放行,日顶叠加 ──
|
||
user = Store(tmp / "user-fresh.db")
|
||
user_cfg = BudgetConfig(daily_tokens=2_000_000,
|
||
daily_requests={"dialogue": 60, "life": 8, "background": 40, "semantic": 40},
|
||
total_requests=None, total_tokens=None)
|
||
user_guard = PersistentBudget(user, BudgetGuard(user_cfg))
|
||
b_start = acquire(user_guard, "mem0_extract")
|
||
c_start = acquire(user_guard, "dialogue_main")
|
||
# 无一次性总量 = 无总量闸:budget_total 行只作记录(2 次消费照实累计),永不禁停。
|
||
check("S3_user_instance_B_and_C_unblocked_after_A",
|
||
b_start == "allowed" and c_start == "allowed" and user_guard.config.total_requests is None
|
||
and user_guard.total_requests_used() == 2,
|
||
f"B={b_start} C={c_start} cumulative_counter={user_guard.total_requests_used()} (record-only, no gate)")
|
||
# C 日顶叠加:dialogue 60 到顶拒绝;semantic 40 到顶拒绝;日 token 池到顶拒绝;拒绝不落账。
|
||
for _ in range(59):
|
||
acquire(user_guard, "dialogue_main")
|
||
c_daily = acquire(user_guard, "dialogue_main")
|
||
for _ in range(39):
|
||
acquire(user_guard, "mem0_extract")
|
||
b_daily = acquire(user_guard, "mem0_extract")
|
||
dialogue_used = user_guard.requests_used("dialogue")
|
||
semantic_used = user_guard.requests_used("semantic")
|
||
token_cap = acquire(user_guard, "life_event", tokens=2_000_000_000)
|
||
check("S3_daily_caps_stack_on_user_instance",
|
||
c_daily.startswith("budget_requests_exhausted:dialogue") and b_daily.startswith("budget_requests_exhausted:semantic")
|
||
and dialogue_used == 60 and semantic_used == 40 and token_cap.startswith("budget_tokens_exhausted"),
|
||
f"dialogue={c_daily} semantic={b_daily} tokens={token_cap} used d={dialogue_used} s={semantic_used}")
|
||
report["scenes"]["S3_user_instance"] = {"B_after_A": b_start, "C_after_A": c_start,
|
||
"dialogue_daily_reject": c_daily, "semantic_daily_reject": b_daily,
|
||
"token_pool_reject": token_cap,
|
||
"budget_total_rows": user_guard.total_requests_used()}
|
||
user.close()
|
||
|
||
# ── S4:误配守门——用户实例若误写 total 55,B/C 再次被挡(发布配置复核点) ──
|
||
wrong = Store(tmp / "user-wrong-total.db")
|
||
wrong_guard = PersistentBudget(wrong, BudgetGuard(BudgetConfig(
|
||
daily_tokens=2_000_000, daily_requests={"dialogue": 60, "life": 8, "background": 40, "semantic": 40},
|
||
total_requests=55, total_tokens=500_000)))
|
||
for _ in range(55):
|
||
wrong_guard.acquire("dialogue_main", tokens=1)
|
||
s4_b = acquire(wrong_guard, "mem0_extract")
|
||
s4_c = acquire(wrong_guard, "dialogue_main")
|
||
check("S4_misconfigured_total_on_user_instance_blocks_B_C",
|
||
s4_b.startswith("budget_total_requests_exhausted") and s4_c.startswith("budget_total_requests_exhausted"),
|
||
f"B={s4_b} C={s4_c}")
|
||
report["scenes"]["S4_misconfig"] = {"B": s4_b, "C": s4_c}
|
||
wrong.close()
|
||
|
||
# ── S5:冻结保全——仓库配置与权威事实零改动 ──
|
||
after_hashes = {p.name: hashlib.sha256(p.read_bytes()).hexdigest() for p in watched}
|
||
check("S5_repo_configs_unchanged_no_authoritative_writes", before_hashes == after_hashes,
|
||
json.dumps(before_hashes, ensure_ascii=False))
|
||
report["scenes"]["S5"] = {"config_sha256_before": before_hashes, "config_sha256_after": after_hashes}
|
||
|
||
ok = all(r[1] for r in RESULTS)
|
||
report["checks"] = [{"name": n, "ok": o, "detail": d} for n, o, d in RESULTS]
|
||
report["ok"] = ok
|
||
(out_dir / "budget-split.json").write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
||
print(json.dumps({"ok": ok, "checks": len(RESULTS)}, ensure_ascii=False))
|
||
return 0 if ok else 1
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|