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
+12 -24
View File
@@ -79,15 +79,6 @@ QUEUE_ID_SQL: dict[str, str] = {
"reach_out": "SELECT id FROM reach_out WHERE state='pending'",
"life_activity": "SELECT id FROM life_activity WHERE state IN ('active','interrupted')",
}
LIVE_SEALED_KEYS = ("sealed", "sealed_requests", "sealed_tokens", "sealed_at")
PRIOR_KEY = { # live 键 → 冻结出处键(只写进迁移副本)
"sealed": "cont_prior_sealed",
"sealed_requests": "cont_prior_sealed_requests",
"sealed_tokens": "cont_prior_sealed_tokens",
"sealed_at": "cont_prior_sealed_at",
}
def _ro(db: Path) -> sqlite3.Connection:
if not db.is_file():
raise SystemExit(json.dumps({"ok": False, "error": f"db_missing:{db}"}, ensure_ascii=False))
@@ -243,16 +234,14 @@ def migrate(db: Path, base: Path, data_dirs: list[tuple[str, str]]) -> dict:
_consistency_copy(tdb, backup)
# 1) 一致性复制(源只读)
_consistency_copy(db, tdb)
# 2) 旧账冻结接线(只改副本):sealed* → cont_prior_sealed*;其余计数原样保留。
# 2) 旧账逐字保留 + 接续出处标记(只新增 cont_* 三键,不改/不删任何旧键,
# 包括 sealed/sealed_*:旧封账在副本上依旧原样、对旧口径读者仍生效)。
# 新授权范围不在旧键上续写:接续实例以 CYBERGRIL_BUDGET_SCOPE=hel919:
# 运行,新计数/新封账落在独立的 hel919:* budget_total 键(产品代码
# PersistentBudget 的 scope 参数,缺省 "" = 旧实例原键原语义不变)。
conn = sqlite3.connect(str(tdb))
try:
conn.execute("BEGIN IMMEDIATE")
for live_key, prior_key in PRIOR_KEY.items():
row = conn.execute("SELECT value FROM budget_total WHERE key=?", (live_key,)).fetchone()
if row is not None:
conn.execute("INSERT INTO budget_total VALUES(?,?) ON CONFLICT(key) "
"DO UPDATE SET value=excluded.value", (prior_key, row[0]))
conn.execute("DELETE FROM budget_total WHERE key=?", (live_key,))
now_iso = datetime.datetime.now(datetime.timezone.utc).isoformat() # noqa: UP017 -- 本机 3.10 兼容,产品 3.11 同样支持 timezone.utc
conn.execute("INSERT INTO budget_total VALUES('cont_scope',1) "
"ON CONFLICT(key) DO UPDATE SET value=1")
@@ -294,20 +283,19 @@ def migrate(db: Path, base: Path, data_dirs: list[tuple[str, str]]) -> dict:
"accounting": _accounting(conn), "queues": _queues(conn)}
finally:
conn.close()
cont_added = {"cont_scope", "cont_source_sha256", "cont_migrated_at"}
checks = {
"same_her": post["identity"] == pre["identity"],
"provenance_preserved": post["provenance"] == pre["provenance"],
"old_counts_preserved": (post["accounting"]["token_usage_rows"] == pre["accounting"]["token_usage_rows"]
and post["accounting"]["token_used_sum"] == pre["accounting"]["token_used_sum"]
and post["accounting"]["request_usage_rows"] == pre["accounting"]["request_usage_rows"]),
"totals_preserved": (post["accounting"]["budget_total"].get("total_requests")
== pre["accounting"]["budget_total"].get("total_requests")
and post["accounting"]["budget_total"].get("total_tokens")
== pre["accounting"]["budget_total"].get("total_tokens")),
"prior_seal_frozen": all(
post["accounting"]["budget_total"].get(PRIOR_KEY[k]) == pre["accounting"]["budget_total"].get(k)
for k in LIVE_SEALED_KEYS if k in pre["accounting"]["budget_total"]),
"live_seal_removed": all(k not in post["accounting"]["budget_total"] for k in LIVE_SEALED_KEYS),
"old_ledger_verbatim": (
# 副本 budget_total 与源完全一致,仅允许新增 cont_* 三键;
# 旧键(含 sealed/sealed_*/total_*)一个不改、一个不删、不改名。
{k: v for k, v in post["accounting"]["budget_total"].items() if k not in cont_added}
== pre["accounting"]["budget_total"]),
"cont_markers_added": cont_added <= set(post["accounting"]["budget_total"]),
"queues_preserved": post["queues"] == pre["queues"],
}
if not all(checks.values()):