HEL-919 rework: old ledger verbatim in copy (no seal rename) + scoped budget keys; running-expiry drill evidence

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
施工员2号
2026-11-04 11:56:00 +08:00
co-authored by multica-agent
parent 9c3789af06
commit 99c5de1d60
12 changed files with 185 additions and 101 deletions
+37 -24
View File
@@ -165,10 +165,16 @@ def test_migrate_preserves_same_her_and_freezes_old_ledger(synth, tmp_path):
assert conn.execute("SELECT count(*) n FROM mem_episodic").fetchone()["n"] == 1
total = {r["key"]: r["value"] for r in conn.execute("SELECT * FROM budget_total")}
conn.close()
# 旧账冻结:live seal 移入 cont_prior_*,历史用量与总量保留
assert "sealed" not in total and "cont_prior_sealed" in total
assert total["cont_prior_sealed"] == 1
assert total["cont_prior_sealed_requests"] == 1 # 迁移前真实用量 1 次
# 旧账逐字保留:旧键(含 sealed/sealed_*)一个不改、不删、不改名,
# 只新增 cont_scope/cont_source_sha256/cont_migrated_at 三个出处标记
cont_added = {"cont_scope", "cont_source_sha256", "cont_migrated_at"}
sconn = sqlite3.connect(f"file:{src}?mode=ro", uri=True)
sconn.row_factory = sqlite3.Row
src_total = MIG._accounting(sconn)["budget_total"]
sconn.close()
assert {k: v for k, v in total.items() if k not in cont_added} == src_total
assert cont_added <= set(total)
assert total["sealed"] == 1 and total["sealed_requests"] == 1 # 旧封账原样(迁移前真实用量 1 次)
assert total["total_requests"] == 1 and total["total_tokens"] == 1000
# 源库逐字节不变
assert src.read_bytes() == before_bytes
@@ -230,39 +236,46 @@ def test_new_usage_isolated_persistent_and_old_seal_still_enforced(synth):
src, base = synth
assert _migrate(src, base)["ok"]
tdb = base / "data" / "companion.db"
# 旧库(源)封账依旧拒绝一切
sconn = Store(src, init=False)
old = PersistentBudget(sconn, BudgetGuard(BudgetConfig()))
with pytest.raises(BudgetStop):
old.acquire("dialogue_main", tokens=1)
sconn.close()
# 副本:新授权范围生效,可继续体验;新计数在旧账之上单独可核
store = Store(tdb, init=False)
cfg = BudgetConfig(daily_tokens=5000, daily_requests={
"dialogue": 5, "life": 5, "background": 5, "semantic": 5})
guard = PersistentBudget(store, BudgetGuard(cfg))
assert guard.total_requests_used() == 1 # 旧计数保留
# 旧口径(scope="")在源库与副本上都被旧封账拒绝:旧账没被改写、没被绕过
for db in (src, tdb):
legacy = Store(db, init=False)
with pytest.raises(BudgetStop):
PersistentBudget(legacy, BudgetGuard(cfg)).acquire("dialogue_main", tokens=1)
legacy.close()
# 接续实例(scope="hel919"):新授权范围生效,新计数落在独立 hel919:* 键
store = Store(tdb, init=False)
guard = PersistentBudget(store, BudgetGuard(cfg), scope="hel919")
assert guard.total_requests_used() == 0 # 新 scope 从零起,不借旧计数也不清旧键
hold = guard.acquire("dialogue_main", tokens=500)
guard.settle(hold, actual_tokens=400)
assert guard.total_requests_used() == 2
assert guard.total_tokens_used() == 1400
assert guard.total_requests_used() == 1 and guard.total_tokens_used() == 400
t = {r["key"]: r["value"] for r in store.query("SELECT key,value FROM budget_total")}
assert t["total_requests"] == 1 and t["total_tokens"] == 1000 # 旧键未动
assert t["hel919:total_requests"] == 1 and t["hel919:total_tokens"] == 400
store.close()
# 进程"重启":重开库,计数与期限类状态不重置
# 进程"重启":重开库,新 scope 计数不重置;旧口径读者依旧被旧封账拒绝
store2 = Store(tdb, init=False)
guard2 = PersistentBudget(store2, BudgetGuard(cfg))
assert guard2.total_requests_used() == 2
assert guard2.total_tokens_used() == 1400
guard2 = PersistentBudget(store2, BudgetGuard(cfg), scope="hel919")
assert guard2.total_requests_used() == 1 and guard2.total_tokens_used() == 400
hold = guard2.acquire("life_event", tokens=100)
guard2.settle(hold, actual_tokens=100)
assert guard2.total_requests_used() == 3
assert guard2.total_requests_used() == 2
with pytest.raises(BudgetStop):
PersistentBudget(store2, BudgetGuard(cfg)).acquire("dialogue_main", tokens=1)
store2.close()
# 新账封账后同样拒绝(close 路径的库内效果)
# 新账封账(close 路径的库内效果):新 scope 拒绝一切;旧键依旧原样
store3 = Store(tdb, init=False)
guard3 = PersistentBudget(store3, BudgetGuard(cfg))
guard3.seal()
guard3 = PersistentBudget(store3, BudgetGuard(cfg), scope="hel919")
marker = guard3.seal()
with pytest.raises(BudgetStop):
guard3.acquire("dialogue_main", tokens=1)
t = {r["key"]: r["value"] for r in store3.query("SELECT key,value FROM budget_total")}
assert t["sealed"] == 1 and t["sealed_requests"] == 1 # 旧封账未被触碰
assert t["hel919:sealed"] == 1 and t["hel919:sealed_requests"] == 2
store3.close()
assert marker["requests"] == 2
# ---------------------------------------------------------------- window guard