HEL-879 release closure: the four HEL-896 audit findings become enforced behavior - (1) archive consistency: tools/release_snapshot_hel896.py v2 checks the wal_checkpoint(TRUNCATE) return (busy!=0 = hard failure, no deliverable tar, half-built dir removed - 'stopped service first' is a precondition, never a reason to ignore an actual failed result), aborts packaging on any source-vs-snapshot table-count mismatch instead of recording rows_match=False and shipping anyway, and verify_archive now rejects legacy packages that recorded rows_match=False while mapping a real rollback drill (unpack to an isolated dir, open every DB, check rows AND hashes against both recorded source and snapshot counts, emit a source-path rollback_plan - no more tar-internal self-attestation); selftest 9/9 reproduces every old failure shape first (naive WAL copy loses companion, held-reader checkpoint=(1,9,8) leaves the copied image malformed/missing the events row, the manager's rows_match=False legacy package verifying ok) then proves each fix (busy fails with zero artifacts, stale-snapshot mismatch fails with zero artifacts, online mode refuses --file dirs containing SQLite by magic-header scan, stop mode recursively takes embedded SQLite like history_root/mem0_public_history.db through checkpoint+per-table verification, verify maps source paths and passes); (2) early-finish seal: PersistentBudget gains sealed()/seal() writing sealed/sealed_requests/sealed_tokens/sealed_at atomically into budget_total beside the real usage (never faking 55, never zeroing), acquire checks the seal FIRST so every category and every remaining allowance rejects budget_sealed:51/55, the marker survives restart and backup-API archive/restore, reseal is idempotent, and there is deliberately no unseal path (re-test = new approval + new instance per budget-package 6.2.4); tools/seal_budget_hel879.py makes the closure an explicit auditable operator command with a post-seal rejection self-check; verify_budget_split_hel896.py gains S6 (51/55 experience chain completes -> seal keeps usage 51 -> 52nd and cross-category rejected with dialogue daily still 19/60 proving it is the seal not the caps, mistaken restart rejected, archive restore rejected, marker requests=51 intact) now 10/10, HEAD made dynamic instead of hardcoded 04eff61; budget-package-hel895.md 6.2 rewritten to the executable seal order (stop -> seal -> consistency snapshot); (3) single install entry: deploy/hel895-release-candidate.md 2 is now the only copy-paste install (uv --offline, npm --offline, cache-miss = stop and record, network only inside an authorized release window with per-item sha256) and 4 is the only fixed-SDK entry as a five-step chain (verify tarball sha256 9dcef930...dda23 against mem0-stack.lock -> build the wheel from the fixed source with hatchling honestly listed as the missing build backend -> cp311 dependency prep with greenlet/pillow/truststore confirmed cache-reusable on re-run -> local-wheels-only --no-index install where mem0ai must come from the fixed-source wheel, the lock line is version-only -> same-process co-import plus the 92MB fastembed cache placement); deploy/hel874-release.md 2/4 carry superseded banners pointing here, mem0-stack.lock no longer routes installs through the index mem0ai==2.2.1 wheel; (4) exact-commit honesty: audit_sdk_offline_hel896.py derives head from git rev-parse and accepts an output dir, fresh evidence written to docs/rework/hel879/sdk-audit/ (hel896 evidence left untouched) recording closure 55 / missing 41 (28 pure + 13 native) / cache-reusable [greenlet, pillow, truststore] / tarball-matches-lock at c121c42; regression on this head: pytest 299/299, ruff clean (src/tests/tools), release-snapshot selftest 9/9, budget-split 10/10 including the new S6 negatives; zero provider calls, zero downloads, authoritative ledgers/freezes/HEL-835 gate untouched, not deployed, PR#11 untouched

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
zonggong
2026-10-27 03:26:30 +08:00
co-authored by multica-agent
parent c121c42e39
commit 01ff919859
13 changed files with 1682 additions and 142 deletions
+11 -2
View File
@@ -34,6 +34,7 @@ import hashlib
import importlib.metadata as md
import json
import re
import sys
from datetime import datetime
from pathlib import Path
@@ -85,7 +86,15 @@ def cache_hits(name: str, version: str) -> list[dict]:
def main() -> int:
out_dir = REPO / "docs/rework/hel896/sdk-audit"
import subprocess as _sp
try:
head = _sp.run(["git", "rev-parse", "HEAD"], cwd=REPO, capture_output=True,
text=True, check=True).stdout.strip()
except Exception:
head = "unknown"
# HEL-879:head 动态取当前提交,不再硬编码旧父提交冒充最新 head;
# 可选参数改写证据目录(默认保持 HEL-896 历史目录不动,本卡证据写 hel879/)。
out_dir = Path(sys.argv[1]).resolve() if len(sys.argv) > 1 else REPO / "docs/rework/hel896/sdk-audit"
out_dir.mkdir(parents=True, exist_ok=True)
installed = {key(d.metadata["Name"]): d for d in md.distributions(path=[str(SDK)])}
base = {key(d.metadata["Name"]): d for d in md.distributions(path=[str(PROD)])}
@@ -215,7 +224,7 @@ def main() -> int:
top = {n: rows.get(n) for n in ("mem0ai", "qdrant-client", "fastembed")}
result = {
"head": "04eff611e7d85e539d43717711172441420ac853",
"head": head,
"scope": ("只读审计:3.10 固定栈安装元数据按 3.11 Linux markers+extras 求闭包,对照产品 3.11 venv 与本机 uv/pip 缓存;"
"这是依赖闭包清单,不是已完成的 cp311 解析或下载结果"),
"read_only": True, "zero_downloads_this_card": True,
+372 -88
View File
@@ -1,24 +1,40 @@
#!/usr/bin/env python3
"""HEL-896 · 发布归档一致性快照与回退证据(修复 WAL 丢失;不冒充 R6 完整备份)。
"""HEL-879 · 发布归档一致性快照与回退证据(HEL-896 有限收口版;不冒充 R6 完整备份)。
总管独立复审(HEL-896 附件 §4)复现:在线库只复制主 `.db` 会丢 WAL 里已提交的资料
(复现件连表都不在;SQLite backup API 则完整)。本工具给出发布卡可直接照抄的归档方式:
HEL-896 工具的总管独立复审发现四个失败边界,本版全部收口:
停服务 + `PRAGMA wal_checkpoint(TRUNCATE)` 后整目录归档(离线口径,推荐);或
在线用 SQLite backup API 逐库做一致性快照(在线兜底口径)。
1. checkpoint 返回值必须核对:`PRAGMA wal_checkpoint(TRUNCATE)` 返回 (busy, log,
checkpointed),busy≠0 即未完成——旧版照抄复制并在 manifest 记 rows_match=False
仍打包成功。本版 busy≠0 / 源表行数≠快照行数一律**明确失败**:CLI 非 0、不产生
可交付归档(tar 不生成,半成品目录清除)。「先停服」是操作前提,不是忽略实际
失败结果的理由。
2. 目录内真实 SQLite 走一致性路径:history_root/vector_root 等数据目录里的
`mem0_public_history.db`、qdrant 存储等 SQLite 文件按文件头魔数递归识别
(`semantic.py` 的 history_db_path 就在 history_root 内)。停服口径对它们逐个
checkpoint+复制+逐表核对;**在线口径只允许显式单库 --db(backup API),传入含
SQLite 的目录直接拒绝**——在线目录裸复制正是丢 WAL 的不安全口径。
3. verify 拒绝 rows_match=False 的旧包,并按源路径映射做回退演练:解包到隔离新
目录、实际打开每个库核对行数与哈希(对照 manifest 记录的源行数与快照行数,
两者必须相等且与解包实测一致),非只打开 tar 中快照自证;输出 rollback_plan
(源路径 → 解包恢复路径)。
4. 完整实例归档只支持停服/释放全部写入方;在线模式仅显式单库 backup API。
产物:`<out>/<name>/` 下的 `databases/`(快照库)、`files/`(其余数据目录原样)、
`MANIFEST.json`(每个文件的 SHA256、源路径、代码精确提交、逐表行数核对结果)、
`<name>.tar.gz`。`--verify` 对既有归档重算哈希并重开快照库核对行数,作为回退演练。
产物:`<out>/<name>/` 下的 `databases/`(一致性快照库,含目录内发现的 SQLite)、
`files/`(其余数据文件原样)、`MANIFEST.json`(逐文件 SHA256、源路径、代码精确
提交、逐表行数核对结果)、`<name>.tar.gz`。归档永不删除;本工具不实现 R6。
自测(`--selftest`,临时目录,零产品数据)复现两种口径的差异并证明修复,输出
`docs/rework/hel896/release-snapshot/selftest.json`。
自测(`--selftest`,临时目录,零产品数据)复现:旧版失败形状(busy checkpoint 仍
打包、rows_match=False 旧包 verify 仍 ok、在线目录含 SQLite)与本版修复,输出
`docs/rework/hel879/release-snapshot/selftest.json`。
用法:
python3 tools/release_snapshot_hel896.py --selftest [outDir]
python3 tools/release_snapshot_hel896.py snapshot --name hel8xx-release \
--db path/a.db --db path/b.sqlite3 --file path/vecdir --file path/hist \
--out archives/ [--online]
--out archives/ # 停服口径(推荐,目录内 SQLite 一并一致性处理)
python3 tools/release_snapshot_hel896.py snapshot --name ... --db path/a.db \
--out archives/ --online # 在线口径:仅显式单库 backup API,含 SQLite 的
# --file 目录会被拒绝
python3 tools/release_snapshot_hel896.py verify --archive archives/hel8xx-release.tar.gz
"""
@@ -36,6 +52,12 @@ import time
from pathlib import Path
REPO = Path(__file__).resolve().parents[1]
SQLITE_MAGIC = b"SQLite format 3\x00"
TOOL_VERSION = 2
class SnapshotError(RuntimeError):
"""快照/验证明确失败:CLI 非 0,不形成可交付成功包。"""
def sha256(path: Path) -> str:
@@ -46,6 +68,26 @@ def sha256(path: Path) -> str:
return h.hexdigest()
def is_sqlite(path: Path) -> bool:
"""按文件头魔数识别真实 SQLite 库(覆盖 .db/.sqlite3/无后缀;qdrant 本地存储)。"""
try:
with open(path, "rb") as f:
return f.read(16) == SQLITE_MAGIC
except OSError:
return False
def find_sqlite(root: Path) -> list[Path]:
"""递归找出目录内全部真实 SQLite 文件(按魔数,不按扩展名)。"""
if root.is_file():
return [root] if is_sqlite(root) else []
found: list[Path] = []
for p in sorted(root.rglob("*")):
if p.is_file() and is_sqlite(p):
found.append(p)
return found
def table_counts(db: Path) -> dict[str, int]:
con = sqlite3.connect(f"file:{db}?mode=ro", uri=True)
try:
@@ -56,18 +98,25 @@ def table_counts(db: Path) -> dict[str, int]:
con.close()
def checkpoint(db: Path) -> None:
def checkpoint(db: Path) -> tuple[int, int, int]:
"""TRUNCATE checkpoint,返回 (busy, log, checkpointed)。busy≠0 = 未完成。"""
con = sqlite3.connect(str(db))
try:
con.execute("PRAGMA wal_checkpoint(TRUNCATE)")
row = con.execute("PRAGMA wal_checkpoint(TRUNCATE)").fetchone()
return (row[0], row[1], row[2]) if row else (1, -1, -1)
finally:
con.close()
def snapshot_db(src: Path, dst: Path, *, online: bool) -> str:
"""一致性快照单库。online=False 先 checkpoint(停服口径);True 用 backup API。"""
"""一致性快照单库。online=False 先 checkpoint(必须完成);True 用 backup API。"""
if not online:
checkpoint(src)
busy, log, ckpt = checkpoint(src)
if busy != 0:
raise SnapshotError(
f"checkpoint 未完成(busy={busy}, log={log}, checkpointed={ckpt}):"
f"{src} 仍有读取/写入方持有 WAL。停服口径要求先停止全部相关写入方,"
f"不得忽略该失败继续打包。")
shutil.copy2(src, dst)
return "stop+checkpoint+copy"
with sqlite3.connect(str(src)) as source, sqlite3.connect(str(dst)) as target:
@@ -84,61 +133,135 @@ def git_head() -> str:
def make_snapshot(name: str, dbs: list[Path], files: list[Path], out_dir: Path, *, online: bool) -> dict:
"""做一致性快照。任一环节失败:不产生 tar、清除半成品、抛 SnapshotError。
收口语义(HEL-879):
- 全部 --db 与目录内发现的 SQLite 都走一致性路径并逐表核对源/快照行数;
- 行数不一致或 checkpoint busy = 明确失败(旧版记 rows_match=False 仍打包);
- 在线模式含 SQLite 的 --file 目录直接拒绝(在线目录裸复制丢 WAL)。
"""
stamp = time.strftime("%Y%m%d-%H%M%S")
root = out_dir / f"{name}-{stamp}"
(root / "databases").mkdir(parents=True, exist_ok=True)
manifest: dict = {"name": name, "created": stamp, "code_commit": git_head(), "tool": "release_snapshot_hel896.py",
manifest: dict = {"name": name, "created": stamp, "code_commit": git_head(),
"tool": "release_snapshot_hel896.py", "tool_version": TOOL_VERSION,
"mode": "online-backup-api" if online else "stop-checkpoint-copy",
"databases": [], "files": [], "verify": "not-run"}
for db in dbs:
dst = root / "databases" / db.name
method = snapshot_db(db, dst, online=online)
src_counts = table_counts(db)
dst_counts = table_counts(dst)
ok = src_counts == dst_counts
manifest["databases"].append({"source": str(db), "snapshot": str(dst.relative_to(root)),
"sha256": sha256(dst), "method": method,
"source_table_counts": src_counts, "snapshot_table_counts": dst_counts,
"rows_match": ok})
for f in files:
src = Path(f)
dst = root / "files" / src.name
if src.is_dir():
shutil.copytree(src, dst)
entries = {str(p.relative_to(dst)): sha256(p) for p in sorted(dst.rglob("*")) if p.is_file()}
else:
shutil.copy2(src, dst)
entries = {src.name: sha256(dst)}
manifest["files"].append({"source": str(src), "snapshot": str(dst.relative_to(root)), "entries": entries})
manifest["verify"] = "rows_match=" + str(all(d["rows_match"] for d in manifest["databases"]))
(root / "MANIFEST.json").write_text(json.dumps(manifest, ensure_ascii=False, indent=2), encoding="utf-8")
tar = out_dir / f"{name}-{stamp}.tar.gz"
with tarfile.open(tar, "w:gz") as tf:
tf.add(root, arcname=root.name)
manifest["archive"] = str(tar)
manifest["archive_sha256"] = sha256(tar)
return manifest
try:
(root / "databases").mkdir(parents=True, exist_ok=True)
for db in dbs:
if not Path(db).is_file():
raise SnapshotError(f"--db 不是文件:{db}")
dst = root / "databases" / db.name
method = snapshot_db(Path(db), dst, online=online)
src_counts = table_counts(Path(db))
dst_counts = table_counts(dst)
if src_counts != dst_counts:
raise SnapshotError(
f"源/快照行数不一致,不打包:{db} source={src_counts} snapshot={dst_counts}")
manifest["databases"].append({"source": str(db), "snapshot": str(dst.relative_to(root)),
"sha256": sha256(dst), "method": method,
"source_table_counts": src_counts,
"snapshot_table_counts": dst_counts,
"rows_match": True})
for f in files:
src = Path(f)
if not src.exists():
raise SnapshotError(f"--file 不存在:{src}")
embedded = find_sqlite(src)
if online and embedded:
raise SnapshotError(
f"在线模式只允许显式单库 backup API(--db);--file {src} 内含 SQLite:"
f"{[str(e) for e in embedded]}。含活动库的目录在线裸复制会丢 WAL,"
f"拒绝。完整实例归档请用停服口径(默认模式)。")
for emb in embedded:
# 停服口径:目录内真实 SQLite 一并走 checkpoint+复制+逐表核对。
rel = emb.relative_to(src) if emb.is_relative_to(src) else Path(emb.name)
dst = root / "databases" / f"{src.name}__{rel}".replace("/", "__")
method = snapshot_db(emb, dst, online=False)
src_counts = table_counts(emb)
dst_counts = table_counts(dst)
if src_counts != dst_counts:
raise SnapshotError(
f"目录内 SQLite 源/快照行数不一致,不打包:{emb} "
f"source={src_counts} snapshot={dst_counts}")
manifest["databases"].append({"source": str(emb), "snapshot": str(dst.relative_to(root)),
"sha256": sha256(dst), "method": method + "+dir-scan",
"source_table_counts": src_counts,
"snapshot_table_counts": dst_counts,
"rows_match": True})
dst = root / "files" / src.name
if src.is_dir():
shutil.copytree(src, dst)
entries = {str(p.relative_to(dst)): sha256(p) for p in sorted(dst.rglob("*")) if p.is_file()}
else:
shutil.copy2(src, dst)
entries = {src.name: sha256(dst)}
manifest["files"].append({"source": str(src), "snapshot": str(dst.relative_to(root)),
"entries": entries})
if not manifest["databases"]:
raise SnapshotError("没有任何数据库进入一致性路径(--db 为空)")
manifest["verify"] = "rows_match=True (all, enforced; mismatch aborts packaging)"
(root / "MANIFEST.json").write_text(json.dumps(manifest, ensure_ascii=False, indent=2), encoding="utf-8")
tar = out_dir / f"{name}-{stamp}.tar.gz"
with tarfile.open(tar, "w:gz") as tf:
tf.add(root, arcname=root.name)
manifest["archive"] = str(tar)
manifest["archive_sha256"] = sha256(tar)
return manifest
except Exception:
# 失败不形成可交付包:清除半成品目录与可能已生成的 tar。
shutil.rmtree(root, ignore_errors=True)
for partial in out_dir.glob(f"{name}-{stamp}.tar.gz"):
partial.unlink(missing_ok=True)
raise
def verify_archive(archive: Path) -> dict:
"""回退演练:隔离新目录解包,实际打开各库核对行数与哈希,按源路径映射。
收口语义(HEL-879):
- manifest 记录 rows_match=False(旧版失败包)→ ok=False 拒绝;
- 行数同时对照 source_table_counts 与 snapshot_table_counts:两者必须相等,
且与解包实测一致(旧版只比快照自证,源=1 行/快照=0 行的漏行包仍 ok)。
"""
result: dict = {"archive": str(archive), "sha256": sha256(archive), "checks": []}
with tempfile.TemporaryDirectory(prefix="hel896-verify-") as tmp:
with tempfile.TemporaryDirectory(prefix="hel879-verify-") as tmp:
with tarfile.open(archive) as tf:
tf.extractall(tmp)
root = next(p for p in Path(tmp).iterdir() if p.is_dir())
manifest = json.loads((root / "MANIFEST.json").read_text(encoding="utf-8"))
result["code_commit"] = manifest.get("code_commit")
result["mode"] = manifest.get("mode")
result["tool_version"] = manifest.get("tool_version", 1)
ok_all = True
rollback_plan = []
for d in manifest["databases"]:
snap = root / d["snapshot"]
digest = sha256(snap)
counts = table_counts(snap)
ok = digest == d["sha256"] and counts == d.get("snapshot_table_counts")
try:
counts = table_counts(snap)
except sqlite3.Error as exc:
result["checks"].append({"db": d["snapshot"], "sha256_ok": digest == d["sha256"],
"rows_ok": False, "error": str(exc),
"rows_match_recorded": d.get("rows_match")})
ok_all = False
continue
recorded_rows_match = d.get("rows_match")
src_counts = d.get("source_table_counts")
snap_counts = d.get("snapshot_table_counts")
rows_ok = counts == snap_counts and (
recorded_rows_match is True or (src_counts is not None and counts == src_counts))
legacy_reject = recorded_rows_match is False or (
recorded_rows_match is None and src_counts != snap_counts)
ok = digest == d["sha256"] and rows_ok and not legacy_reject
ok_all &= ok
result["checks"].append({"db": d["snapshot"], "sha256_ok": digest == d["sha256"],
"rows_ok": counts == d.get("snapshot_table_counts"),
"counts": counts})
"rows_ok": rows_ok, "counts": counts,
"source_counts": src_counts,
"rows_match_recorded": recorded_rows_match,
"rejected_legacy_package": legacy_reject})
rollback_plan.append({"source": d.get("source"), "restored_path": str(snap),
"restore_action": "停服后复制回源路径(行数与哈希已在本演练核对)"})
for f in manifest["files"]:
base = root / f["snapshot"]
for rel, want in f["entries"].items():
@@ -146,26 +269,36 @@ def verify_archive(archive: Path) -> dict:
ok = sha256(p) == want
ok_all &= ok
result["checks"].append({"file": str(p.relative_to(root)), "sha256_ok": ok})
rollback_plan.append({"source": f.get("source"), "restored_path": str(base),
"restore_action": "停服后按相对结构复制回源目录"})
result["rollback_plan"] = rollback_plan
result["ok"] = ok_all
return result
def _make_wal_db(path: Path) -> sqlite3.Connection:
path.parent.mkdir(parents=True, exist_ok=True)
con = sqlite3.connect(str(path))
con.execute("PRAGMA journal_mode=WAL")
con.execute("CREATE TABLE companion(id INTEGER PRIMARY KEY, payload TEXT)")
con.execute("CREATE TABLE chat_message(id INTEGER PRIMARY KEY, text TEXT)")
con.execute("CREATE TABLE events(id INTEGER PRIMARY KEY, kind TEXT)")
con.commit()
con.execute("INSERT INTO companion VALUES(1,'her')")
con.execute("INSERT INTO chat_message VALUES(1,'hello')")
con.commit()
return con
def selftest(out_dir: Path) -> int:
"""临时库复现:在线裸复制主 .db 丢 WAL 已提交行;checkpoint 与 backup API 都完整。"""
"""临时库复现全部失败边界并证明修复(零产品数据、零网络)。"""
checks: list[dict] = []
with tempfile.TemporaryDirectory(prefix="hel896-snapshot-selftest-") as tmp:
with tempfile.TemporaryDirectory(prefix="hel879-snapshot-selftest-") as tmp:
tmp = Path(tmp)
db = tmp / "instance" / "companion.db"
db.parent.mkdir(parents=True)
con = sqlite3.connect(str(db))
con.execute("PRAGMA journal_mode=WAL")
con.execute("CREATE TABLE companion(id INTEGER PRIMARY KEY, payload TEXT)")
con.execute("CREATE TABLE chat_message(id INTEGER PRIMARY KEY, text TEXT)")
con.commit()
con.execute("INSERT INTO companion VALUES(1,'her')")
con.execute("INSERT INTO chat_message VALUES(1,'hello')")
con.commit()
# WAL 保持持有(连接不关、不 checkpoint):已提交行仍在 WAL,不在主 .db。
con = _make_wal_db(db)
# ── 旧失败形状 1 复现:WAL 持有(读者未放)时 naive 复制丢已提交行。 ──
naive = tmp / "naive-copy.db"
shutil.copy2(db, naive)
try:
@@ -174,37 +307,180 @@ def selftest(out_dir: Path) -> int:
naive_result = f"rows={row}"
except sqlite3.OperationalError as exc:
naive_result = f"error={exc}"
checks.append({"name": "naive_online_copy_loses_wal", "ok": "rows=1" not in naive_result,
"detail": naive_result})
# backup API(在线口径)
via_api = tmp / "via-backup-api.db"
with sqlite3.connect(str(db)) as source, sqlite3.connect(str(via_api)) as target:
checks.append({"name": "naive_online_copy_loses_wal_reproduced",
"ok": "rows=1" not in naive_result, "detail": naive_result})
# ── 旧失败形状 2 复现(总管 edges 场景):读者先持读事务,随后写入方提交
# events=1;TRUNCATE checkpoint 返回 busy=[1,1,0],naive 快照丢该行,
# 旧工具此时仍打包成功。 ──
reader = sqlite3.connect(str(db))
reader.execute("BEGIN")
reader.execute("SELECT count(*) FROM companion").fetchone()
con.execute("INSERT INTO events VALUES(1,'reach_out')")
con.commit()
busy_state = checkpoint(db)
naive2 = tmp / "naive2.db"
shutil.copy2(db, naive2)
try:
naive2_events = sqlite3.connect(f"file:{naive2}?mode=ro", uri=True).execute(
"SELECT count(*) FROM events").fetchone()[0]
except sqlite3.Error as exc:
naive2_events = f"error={type(exc).__name__}:{exc}"
checks.append({"name": "busy_checkpoint_state_reproduced",
"ok": busy_state[0] != 0 and naive2_events != 1,
"detail": f"checkpoint={busy_state} naive_snapshot_events={naive2_events}"})
# ── 修复 1:busy checkpoint 必须明确失败,不形成归档。 ──
out_busy = tmp / "archives-busy"
out_busy.mkdir()
failed_busy = None
try:
make_snapshot("busy-must-fail", [db], [], out_busy, online=False)
except SnapshotError as exc:
failed_busy = str(exc)
leftovers = list(out_busy.iterdir())
checks.append({"name": "fix1_busy_checkpoint_fails_no_archive",
"ok": failed_busy is not None and "busy" in failed_busy and not leftovers,
"detail": f"error={'yes' if failed_busy else 'none'} leftovers={len(leftovers)}"})
reader.rollback()
reader.close()
# ── 修复 1b:源/快照行数不一致(快照停在某刻、源继续前进的形状)必须
# 明确失败、不形成归档。用停当快照替换法直接触发比对分支。 ──
out_mis = tmp / "archives-mismatch"
out_mis.mkdir()
stale = out_mis / "stale.db"
with sqlite3.connect(str(db)) as source, sqlite3.connect(str(stale)) as target:
source.backup(target)
api_counts = table_counts(via_api)
checks.append({"name": "backup_api_snapshot_complete", "ok": api_counts == {"companion": 1, "chat_message": 1},
"detail": json.dumps(api_counts)})
# 停服 + checkpoint(离线口径)
con.execute("INSERT INTO events VALUES(2,'after')")
con.commit()
real_snapshot_db = snapshot_db
_self = globals()
def _stale_snapshot(src: Path, dst: Path, *, online: bool) -> str:
shutil.copy2(stale, dst)
return "stop+checkpoint+copy(stale-standin)"
_self["snapshot_db"] = _stale_snapshot
failed_mis = None
try:
make_snapshot("mismatch-must-fail", [db], [], out_mis, online=False)
except SnapshotError as exc:
failed_mis = str(exc)
finally:
_self["snapshot_db"] = real_snapshot_db
mis_leftovers = list(out_mis.glob("mismatch-must-fail*"))
checks.append({"name": "fix1b_row_mismatch_fails_no_archive",
"ok": failed_mis is not None and "不一致" in failed_mis
and not [p for p in mis_leftovers if p.suffix == ".gz"],
"detail": f"error={failed_mis[:60] if failed_mis else 'none'} "
f"artifacts={[p.name for p in mis_leftovers]}"})
con.execute("DELETE FROM events WHERE id=2")
con.commit()
# ── 旧失败形状 3 复现:手工构造 rows_match=False 的旧包,旧版 verify 仍 ok。 ──
legacy_root = tmp / "legacy-pkg"
legacy_root.mkdir()
(legacy_root / "databases").mkdir()
legacy_snap = legacy_root / "databases" / "companion.db"
with sqlite3.connect(str(db)) as source, sqlite3.connect(str(legacy_snap)) as target:
source.backup(target)
# 漏行形状:快照内删掉 events 行,manifest 记 rows_match=False(旧版真实输出)。
with sqlite3.connect(str(legacy_snap)) as lk:
lk.execute("DELETE FROM events")
legacy_manifest = {
"name": "legacy", "code_commit": "legacy-shape", "tool": "release_snapshot_hel896.py",
"tool_version": 1, "mode": "stop-checkpoint-copy",
"databases": [{"source": str(db), "snapshot": "databases/companion.db",
"sha256": sha256(legacy_snap), "method": "stop+checkpoint+copy",
"source_table_counts": {"companion": 1, "chat_message": 1, "events": 1},
"snapshot_table_counts": {"companion": 1, "chat_message": 1, "events": 0},
"rows_match": False}],
"files": [], "verify": "rows_match=False",
}
(legacy_root / "MANIFEST.json").write_text(
json.dumps(legacy_manifest, ensure_ascii=False), encoding="utf-8")
legacy_tar = tmp / "legacy-pkg.tar.gz"
with tarfile.open(legacy_tar, "w:gz") as tf:
tf.add(legacy_root, arcname=legacy_root.name)
legacy_verify = verify_archive(legacy_tar)
checks.append({"name": "fix_verify_rejects_rows_match_false_legacy_package",
"ok": legacy_verify["ok"] is False,
"detail": f"ok={legacy_verify['ok']} checks={legacy_verify['checks'][:1]}"})
# ── 修复 2:目录内 SQLite(history_root 形状)。 ──
hist = tmp / "instance" / "history_root"
hist.mkdir(parents=True, exist_ok=True)
mem_hist = hist / "mem0_public_history.db"
mem_con = sqlite3.connect(str(mem_hist))
mem_con.execute("PRAGMA journal_mode=WAL")
mem_con.execute("CREATE TABLE history(id INTEGER PRIMARY KEY, payload TEXT)")
mem_con.execute("INSERT INTO history VALUES(1,'mem')")
mem_con.commit()
mem_reader = sqlite3.connect(str(mem_hist))
mem_reader.execute("BEGIN")
mem_reader.execute("SELECT count(*) FROM history").fetchone()
# 在线模式 + 含 SQLite 的目录 → 拒绝。
out_online = tmp / "archives-online"
out_online.mkdir()
refused = None
try:
make_snapshot("online-dir-refused", [db], [hist], out_online, online=True)
except SnapshotError as exc:
refused = str(exc)
checks.append({"name": "fix2_online_mode_refuses_dir_with_sqlite",
"ok": refused is not None and "拒绝" in refused and not list(out_online.iterdir()),
"detail": refused[:120] if refused else "not refused"})
mem_reader.rollback()
mem_reader.close()
# 停服口径:目录内 SQLite 走 checkpoint+复制+逐表核对(mem_reader 已放,
# 但 mem_con 仍持有 WAL——checkpoint 必须真实完成才算过)。
mem_con.close()
# 主库也关闭,进入停服状态。
con.close()
checkpoint(db)
via_ckpt = tmp / "via-checkpoint.db"
shutil.copy2(db, via_ckpt)
ckpt_counts = table_counts(via_ckpt)
checks.append({"name": "stop_checkpoint_copy_complete", "ok": ckpt_counts == {"companion": 1, "chat_message": 1},
"detail": json.dumps(ckpt_counts)})
# 完整归档 + 回退验证
sem_dir = tmp / "instance" / "vec"
sem_dir.mkdir(exist_ok=True)
(sem_dir / "vectors.bin").write_bytes(b"\x00\x01\x02cg")
vec = tmp / "instance" / "vec"
vec.mkdir(exist_ok=True)
(vec / "vectors.bin").write_bytes(b"\x00\x01\x02cg")
out = tmp / "archives"
out.mkdir()
manifest = make_snapshot("hel896-selftest", [db], [sem_dir], out, online=False)
manifest = make_snapshot("hel879-selftest", [db], [vec, hist], out, online=False)
# 从解包侧核对 manifest(目录内 SQLite 是否真走了逐表核对)。
with tarfile.open(manifest["archive"]) as tf:
tf.extractall(out / "unpack")
unpack_root = out / "unpack" / Path(manifest["archive"]).name.replace(".tar.gz", "")
unpack_manifest = json.loads((unpack_root / "MANIFEST.json").read_text(encoding="utf-8"))
embedded_dbs = [d for d in unpack_manifest["databases"] if "+dir-scan" in d["method"]]
hist_ok = any("mem0_public_history.db" in d["source"] and d["rows_match"] is True
and d["source_table_counts"].get("history") == 1 for d in embedded_dbs)
checks.append({"name": "fix2_stop_mode_embeds_sqlite_via_consistency_path",
"ok": bool(embedded_dbs) and hist_ok,
"detail": f"embedded={[d['source'] for d in embedded_dbs]}"})
# ── 修复 3:回退演练按源路径映射、实测行数与哈希、旧包拒绝已验。 ──
verify = verify_archive(Path(manifest["archive"]))
checks.append({"name": "archive_manifest_hashes_and_rows_verify", "ok": verify["ok"],
"detail": f"commit={verify.get('code_commit')} checks={len(verify['checks'])}"})
plan_sources = {entry.get("source") for entry in verify.get("rollback_plan", [])}
mapped = str(db) in plan_sources and any(
"mem0_public_history.db" in (s or "") for s in plan_sources)
checks.append({"name": "fix3_verify_maps_source_paths_and_passes",
"ok": verify["ok"] and mapped,
"detail": f"ok={verify['ok']} commit={verify.get('code_commit')} "
f"plan_entries={len(verify.get('rollback_plan', []))}"})
# backup API(在线口径,仅显式单库)依旧完整。
with sqlite3.connect(str(db)) as source, sqlite3.connect(str(tmp / "via-api.db")) as target:
source.backup(target)
api_counts = table_counts(tmp / "via-api.db")
checks.append({"name": "backup_api_snapshot_complete",
"ok": api_counts.get("companion") == 1 and api_counts.get("chat_message") == 1,
"detail": json.dumps(api_counts)})
ok = all(c["ok"] for c in checks)
out_dir.mkdir(parents=True, exist_ok=True)
(out_dir / "selftest.json").write_text(
json.dumps({"ok": ok, "code_commit": git_head(), "checks": checks}, ensure_ascii=False, indent=2),
json.dumps({"ok": ok, "code_commit": git_head(),
"tool_version": TOOL_VERSION, "checks": checks}, ensure_ascii=False, indent=2),
encoding="utf-8")
for c in checks:
print(f"{'PASS' if c['ok'] else 'FAIL'} {c['name']} — {c['detail']}")
@@ -220,15 +496,23 @@ def main() -> int:
ap.add_argument("--db", action="append", default=[], type=Path)
ap.add_argument("--file", action="append", default=[], type=Path)
ap.add_argument("--out", type=Path, default=REPO / "deploy" / "archives")
ap.add_argument("--online", action="store_true", help="在线口径:backup API(默认停服+checkpoint)")
ap.add_argument("--online", action="store_true",
help="在线口径:仅显式单库 backup API(默认停服+checkpoint;含 SQLite 的 --file 目录会被拒绝)")
ap.add_argument("--archive", type=Path)
args = ap.parse_args()
if args.selftest:
return selftest(REPO / "docs/rework/hel896/release-snapshot")
return selftest(REPO / "docs/rework/hel879/release-snapshot")
if args.command == "snapshot":
if not args.name or not args.db:
ap.error("snapshot 需要 --name 与至少一个 --db")
manifest = make_snapshot(args.name, args.db, args.file, args.out, online=args.online)
out_dir = args.out
out_dir.mkdir(parents=True, exist_ok=True)
try:
manifest = make_snapshot(args.name, args.db, args.file, out_dir, online=args.online)
except SnapshotError as exc:
print(json.dumps({"ok": False, "error": str(exc),
"deliverable_archive": None}, ensure_ascii=False, indent=1))
return 1
print(json.dumps({k: manifest[k] for k in ("archive", "archive_sha256", "code_commit", "mode", "verify")},
ensure_ascii=False, indent=1))
return 0
+66
View File
@@ -0,0 +1,66 @@
#!/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="封账原因(记录在交付评论,不写入库)")
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()))
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())
+72 -2
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
"""HEL-896 · A/B/C 分账与阶段切换验证(仅临时库 + 替身,零 provider、零网络)。
"""HEL-896/879 · A/B/C 分账、阶段切换与提前封账验证(仅临时库 + 替身,零 provider、零网络)。
总管独立复审(HEL-896 附件 §1)复现的问题:把 A 的一次性总量 55 写进与 B/C 共用的
产品库 PersistentBudget 后,B 的 mem0_extract 与 C 的 dialogue_main 都被
@@ -12,6 +12,9 @@ budget_total_requests_exhausted:55/55 挡住,且 budget_total/request_usage/to
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)逐项到顶拒绝;拒绝不落账;
@@ -32,6 +35,7 @@ from __future__ import annotations
import hashlib
import json
import sqlite3
import subprocess
import sys
import tempfile
from pathlib import Path
@@ -42,7 +46,18 @@ 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
HEAD = "04eff611e7d85e539d43717711172441420ac853"
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]] = []
@@ -150,6 +165,61 @@ def main() -> int:
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,