#!/usr/bin/env python3 """Check cross-surface counts, release metadata, and generated discovery files.""" from __future__ import annotations import csv import json import re import sys from pathlib import Path from build_hf_card import project_context, render_card ROOT = Path(__file__).resolve().parents[1] REQUIRED_RESOURCE_FIELDS = { "row_id", "title", "url", "canonical_url", "resource_type", "annotation", "key_contribution", "novelty", "impact", "signal", "signal_strength", "collection", "user_goal", "lifecycle_stages", "audience", "loop_layer", "scope_fit", "evidence_class", "evidence_tier", "source_status", "metadata_source", "audited_at", } ALLOWED_EVIDENCE_TIERS = {"A", "B", "C", "D"} ALLOWED_SIGNAL_STRENGTHS = {"high", "medium", "contextual", "unverified"} ALLOWED_SOURCE_STATUSES = {"ok", "local_ok", "restricted", "broken", "unreachable", "local_missing"} ALLOWED_LOOP_LAYERS = {"model", "agent", "harness", "workflow", "operations", "evaluation", "cross-layer"} ALLOWED_SCOPE_FITS = {"direct", "enabling", "adjacent"} def require(path: Path, snippets: list[str], failures: list[str]) -> None: text = path.read_text(encoding="utf-8") for snippet in snippets: if snippet not in text: failures.append(f"{path.relative_to(ROOT)}: missing {snippet!r}") def validate_resource_rows(rows: list[dict[str, str]], failures: list[str]) -> None: if not rows: failures.append("data/resources.csv: no resource rows") return missing_columns = REQUIRED_RESOURCE_FIELDS - set(rows[0]) if missing_columns: failures.append(f"data/resources.csv: missing columns {sorted(missing_columns)}") seen_urls: set[str] = set() seen_ids: set[str] = set() for index, row in enumerate(rows, 1): row_label = row.get("row_id") or f"row {index}" missing = sorted(field for field in REQUIRED_RESOURCE_FIELDS if not row.get(field, "").strip()) if missing: failures.append(f"data/resources.csv: {row_label} missing required values {missing}") normalized_url = row.get("url", "").strip().lower().rstrip("/") if normalized_url in seen_urls: failures.append(f"data/resources.csv: duplicate URL at {row_label}: {row.get('url', '')}") seen_urls.add(normalized_url) row_id = row.get("row_id", "") if row_id in seen_ids: failures.append(f"data/resources.csv: duplicate row_id {row_id}") seen_ids.add(row_id) expected_id = f"ale-{index:04d}" if row_id != expected_id: failures.append(f"data/resources.csv: expected {expected_id}, found {row_id or ''}") if row.get("evidence_tier") not in ALLOWED_EVIDENCE_TIERS: failures.append(f"data/resources.csv: {row_label} has invalid evidence_tier {row.get('evidence_tier')!r}") if row.get("signal_strength") not in ALLOWED_SIGNAL_STRENGTHS: failures.append(f"data/resources.csv: {row_label} has invalid signal_strength {row.get('signal_strength')!r}") if row.get("source_status") not in ALLOWED_SOURCE_STATUSES: failures.append(f"data/resources.csv: {row_label} has invalid source_status {row.get('source_status')!r}") if row.get("loop_layer") not in ALLOWED_LOOP_LAYERS: failures.append(f"data/resources.csv: {row_label} has invalid loop_layer {row.get('loop_layer')!r}") if row.get("scope_fit") not in ALLOWED_SCOPE_FITS: failures.append(f"data/resources.csv: {row_label} has invalid scope_fit {row.get('scope_fit')!r}") year = row.get("publication_year", "") if year and not re.fullmatch(r"(?:19|20)\d{2}", year): failures.append(f"data/resources.csv: {row_label} has invalid publication_year {year!r}") if row.get("resource_type") == "Paper": for field in ("authors", "publication_year"): if not row.get(field, "").strip(): failures.append(f"data/resources.csv: {row_label} paper is missing {field}") if "arxiv.org" in row.get("url", "") and not row.get("arxiv_id", "").strip(): failures.append(f"data/resources.csv: {row_label} arXiv work is missing arxiv_id") def main() -> int: context = project_context() count = context["RESOURCE_COUNT"] version = context["VERSION"] failures: list[str] = [] require( ROOT / "README.md", [ f"resources-{count}-", f"**{count} source-audited resources**", f"covers all {count} rows", ], failures, ) require( ROOT / "docs" / "index.html", [ f'content="{count} source-audited resources', f">{count}audited resources", f"Filter {count} source-audited works", f'"version": "{version}"', ], failures, ) require(ROOT / "meta" / "social-preview.html", [f">{count}"], failures) require(ROOT / "posts" / "launch.md", [f"# Awesome Loop Engineering v{version}", f"{count} source-audited resources"], failures) require(ROOT / "posts" / "launch.zh-CN.md", [f"# Awesome Loop Engineering v{version}", f"{count} 条"], failures) require(ROOT / "meta" / "DISTRIBUTION.md", [f"v{version}", f"{count} source-audited resources"], failures) for translation in sorted(ROOT.glob("README.*.md")): require(translation, [count], failures) with (ROOT / "data" / "resources.csv").open(encoding="utf-8", newline="") as handle: resource_rows = list(csv.DictReader(handle)) csv_count = len(resource_rows) if csv_count != int(count): failures.append(f"data/resources.csv: expected {count} rows, found {csv_count}") validate_resource_rows(resource_rows, failures) site_payload = json.loads((ROOT / "docs" / "assets" / "resources.json").read_text(encoding="utf-8")) if site_payload.get("count") != int(count) or len(site_payload.get("resources", [])) != int(count): failures.append("docs/assets/resources.json: count or resources array is stale") _, card_failures = render_card() failures.extend(card_failures) if failures: print("Project consistency check failed:", file=sys.stderr) for failure in failures: print(f"- {failure}", file=sys.stderr) return 1 print( f"Validated {count} resources, {context['PATTERN_COUNT']} patterns, " f"{context['CONTRACT_COUNT']} contracts, and release v{version}." ) return 0 if __name__ == "__main__": raise SystemExit(main())