#!/usr/bin/env python3 """Валидация карточек знаний в data/knowledge/ по pydantic-схеме. python scripts/validate_knowledge.py # все файлы python scripts/validate_knowledge.py <файл.json> # один файл Ключи с префиксом «_» — метаданные прогона (фрагменты, usage), не карточки. Выход 1 при любой ошибке — пригодно для CI и самопроверки агентов. """ import json import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) # корень в путь from pydantic import ValidationError # noqa: E402 from rag import config # noqa: E402 from rag.extract.cards import ExtractionResult # noqa: E402 def validate_file(path: Path) -> bool: try: data = json.loads(path.read_text(encoding="utf-8")) except json.JSONDecodeError as e: print(f"✗ {path.name}: некорректный JSON — {e}") return False core = {k: v for k, v in data.items() if not k.startswith("_")} try: result = ExtractionResult.model_validate(core) except ValidationError as e: print(f"✗ {path.name}: схема нарушена —\n{e}") return False by_type: dict[str, int] = {} for card in result.cards: by_type[card.type] = by_type.get(card.type, 0) + 1 types = ", ".join(f"{t}: {n}" for t, n in sorted(by_type.items())) print(f"✓ {path.name}: {len(result.cards)} карточек ({types})") return True def main() -> int: if len(sys.argv) > 1: files = [Path(sys.argv[1])] else: files = sorted(config.KNOWLEDGE_DIR.glob("*.json")) if not files: print(f"Нет файлов карточек в {config.KNOWLEDGE_DIR}") return 1 ok = all([validate_file(f) for f in files]) # list: проверить ВСЕ, не лениво return 0 if ok else 1 if __name__ == "__main__": raise SystemExit(main())