fix(ТСН): сборка не падает на повторе шифра внутри одной книги; apply не берёт сборку с проваленным аудитом

- В справочнике машин позиция 1-4-1 напечатана дважды (разные группы, цены 58,48 и
  68,87): такой повтор — дефект источника, строка идёт в карантин, сборка остаётся
  годной. Дубли между разными документами по-прежнему ошибка сборки.
- apply отказывается подменять живую базу, если staging собрался с audit_ok=0
  (без --force). Раньше проверялся только verify_db, и база с проваленным аудитом
  могла уехать в работу.

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
anor
2026-11-18 04:00:18 +10:00
co-authored by Claude Code
parent 4ca7faf670
commit c53b7d3dfc
2 changed files with 63 additions and 5 deletions
+26 -5
View File
@@ -120,7 +120,7 @@ def build(snapshots: os.PathLike | str, out_path: os.PathLike | str) -> dict:
con.execute("INSERT INTO meta VALUES('built_at',?)", (built_at,))
seen: dict[tuple, float | None] = {}
seen_items: dict[str, float] = {}
seen_items: dict[str, tuple[float, int]] = {}
src_id = 0
docs_n = works_n = values_n = items_n = 0
for meta, extract in _docs(snapshots):
@@ -161,11 +161,23 @@ def build(snapshots: os.PathLike | str, out_path: os.PathLike | str) -> dict:
for it in extract.get("items", []):
key = it["code"]
price = it.get("price")
if key in seen_items and abs((seen_items[key] or 0) - price) > 0.001:
problems.append(f"ресурс {key} с разными ценами: "
f"{seen_items[key]} и {price}")
if key in seen_items:
prev_price, prev_source = seen_items[key]
if abs((prev_price or 0) - price) > 0.001:
if prev_source == source_id:
# Повтор шифра ВНУТРИ одной книги — дефект источника
# (в справочнике машин позиция 1-4-1 напечатана дважды):
# строка в карантин, сборка остаётся годной.
con.execute(
"INSERT INTO quarantine(release_id, source_id, kind, code,"
" raw, reason) VALUES(1,?,?,?,?,?)",
(source_id, "price", key, str(price),
f"шифр повторяется в книге с другой ценой {prev_price}"))
else:
problems.append(f"ресурс {key} в разных документах с разными "
f"ценами: {prev_price} и {price}")
continue
seen_items[key] = price
seen_items[key] = (price, source_id)
cur = con.execute(
"INSERT OR IGNORE INTO price_items(release_id, source_id, book, kind,"
" code, name, unit, price, page) VALUES(1,?,?,?,?,?,?,?,?)",
@@ -235,6 +247,15 @@ def apply(staging: os.PathLike | str, target: os.PathLike | str, force: bool = F
check = tsn_ref.verify_db(staging)
if not check.get("ok"):
return {"ok": False, "problems": [f"staging не прошёл проверку: {check.get('problem')}"]}
con = sqlite3.connect(staging)
try:
audit_ok = con.execute("SELECT audit_ok FROM release").fetchone()[0]
finally:
con.close()
if not audit_ok and not force:
return {"ok": False, "problems": [
"сборка не прошла собственный аудит (audit_ok=0): дубли с разными ценами — "
"живую базу не подменяю без --force"]}
if target.exists() and not force:
old = tsn_ref.verify_db(target)
if old.get("ok") and check.get("works", 0) < old.get("works", 0) * 0.7: