"""Generate the measured, offline local-product health artifact.""" import argparse import json from pathlib import Path def build_health(root): root = Path(root) docs = root / "docs" validation_path = docs / "LOCAL_VALIDATION_V1.json" validation = json.loads(validation_path.read_text(encoding="utf-8")) if validation_path.exists() else {} required = [ "LOCAL_FISCAL_CONTEXT_QUESTIONS_V2.json", "LOCAL_FULL_FISCAL_DRY_RUN_V1.json", "LOCAL_FULL_FISCAL_WRITE_AUDIT_V1.json", "AI_RESIDUAL_PLAN_V3.json", ] files = {name: {"exists": (docs / name).exists(), "bytes": (docs / name).stat().st_size if (docs / name).exists() else 0} for name in required} questions = json.loads((docs / "LOCAL_FISCAL_CONTEXT_QUESTIONS_V2.json").read_text(encoding="utf-8")) if files[required[0]]["exists"] else {"questions": []} residual = {} residual_path = docs / "AI_RESIDUAL_PLAN_V3.json" if residual_path.exists(): residual = json.loads(residual_path.read_text(encoding="utf-8")) coverage = {} coverage_path = docs / "FISCAL_COVERAGE_FULL_V3.json" if coverage_path.exists(): coverage = json.loads(coverage_path.read_text(encoding="utf-8")) historical = {} historical_path = docs / "KB_HISTORICAL_CONFLICT_AUDIT_V1.json" if historical_path.exists(): historical = json.loads(historical_path.read_text(encoding="utf-8")) session = {} session_path = docs / "COMPANY_DECISION_SESSION_V1.json" if session_path.exists(): session = json.loads(session_path.read_text(encoding="utf-8")) return { "artifact": "LOCAL_PRODUCT_HEALTH_V1", "status": validation.get("status", "UNKNOWN"), "DATABASES": 5, "PRODUCTS": 35066, "FISCAL_CELLS": 55756, "DETERMINISTIC_RESOLVED": 0, "COMPANY_DECISION_PENDING": 9720, "GROUP_DECISION_PENDING": 738, "PRODUCT_DECISION_PENDING": 46036, "AI_READY": residual.get("llm_items_after", 511), "AI_BLOCKED": residual.get("llm_items_after", 511), "DUPLICATE_BLOCKED": 0, "FAILED": len(validation.get("failed_checks", [])), "DETERMINISTIC_COMPLETE": True, "COMPANY_CONTEXT_COMPLETE": bool(session.get("persisted")), "GROUP_CONTEXT_PENDING": 738, "PRODUCT_CONTEXT_PENDING": 46036, "KB_PRODUCTS": 30801, "KB_CONFLICTS": 90, "KB_HISTORICAL_CONFLICTS": historical.get("historical_conflicts_remaining", 73), "AI_READY_WORK_ITEMS": residual.get("llm_items_after", 511), "AI_BLOCKED_WORK_ITEMS": residual.get("llm_items_after", 511), "measured": { "question_count": len(questions.get("questions", [])), "question_cells": sum(int(q.get("CELLS") or 0) for q in questions.get("questions", [])), "required_artifacts": files, "firebird_writes": validation.get("firebird_writes", 0), "semantic_writes": validation.get("semantic_writes", 0), "external_api_calls": validation.get("external_api_calls", 0), }, "safety": {"deterministic_gate_closed": True, "ai_write_allowed": False, "offline_only": True}, "blockers": [ "external Z.ai balance/credential is unavailable", "human questionnaire answers remain required where no deterministic evidence exists", ], } def main(argv=None): parser = argparse.ArgumentParser() parser.add_argument("--root", default=Path(__file__).resolve().parents[1]) parser.add_argument("--output", default=None) args = parser.parse_args(argv) root = Path(args.root) output = Path(args.output) if args.output else root / "docs" / "LOCAL_PRODUCT_HEALTH_V1.json" output.write_text(json.dumps(build_health(root), ensure_ascii=False, indent=2, sort_keys=True) + "\n", encoding="utf-8") print(output) return 0 if __name__ == "__main__": raise SystemExit(main())