Files
GTI_GERENCIADOR_FISCAL/fiscal/scripts/generate_final_194_closure.py
gti d21a9d1140 Add 'fiscal/' from commit '102ba84500dbd9655a0471288c3e278652593ca9'
git-subtree-dir: fiscal
git-subtree-mainline: d95165fc8f
git-subtree-split: 102ba84500
2026-09-29 14:43:23 -03:00

80 lines
5.3 KiB
Python

"""Generate the final 194-product closure analysis from measured artifacts."""
from __future__ import annotations
import json
from collections import Counter
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def main() -> None:
inventory = json.loads((ROOT / "docs/FINAL_INCOMPLETE_PRODUCTS_V1.json").read_text(encoding="utf-8"))
products = inventory["products"]
kb_payload = json.loads((ROOT / "docs/runs/ai_kb_seed_payload.json").read_text(encoding="utf-8"))["products"]
kb_by_fingerprint = {row["fingerprint"]: row for row in kb_payload}
valid_gtin = [row for row in products if row.get("GTIN")]
gtin_kb = [row for row in valid_gtin if row["FINGERPRINT"] in kb_by_fingerprint and kb_by_fingerprint[row["FINGERPRINT"]].get("gtin")]
fingerprint_kb = [row for row in products if row["FINGERPRINT"] in kb_by_fingerprint]
ncm_conflicts = [row for row in products if row["FINGERPRINT"] in kb_by_fingerprint and kb_by_fingerprint[row["FINGERPRINT"]].get("ncm") and row.get("NCM_CURRENT") != kb_by_fingerprint[row["FINGERPRINT"]]["ncm"]]
field_counts = Counter(field["FIELD"] for row in products for field in row["REQUIRED_MISSING"])
blocker_counts = Counter(field["BLOCKER_CLASS"] for row in products for field in row["REQUIRED_MISSING"])
ai_plan = json.loads((ROOT / "docs/AI_RESIDUAL_PLAN_V3.json").read_text(encoding="utf-8"))
result = {
"artifact": "FINAL_194_PRODUCT_CLOSURE_V1",
"status": "ANALYZED_WITHOUT_NEW_RESOLUTION",
"incomplete_before": len(products),
"incomplete_after": len(products),
"autonomous_rate_after": round(34872 / 35066, 10),
"blockers_by_type": dict(sorted(blocker_counts.items())),
"missing_field_counts": dict(sorted(field_counts.items())),
"gtin": {
"with_valid_gtin": len(valid_gtin), "without_gtin": len(products) - len(valid_gtin),
"gtin_already_in_kb": len(gtin_kb), "gtin_new": len(valid_gtin) - len(gtin_kb), "gtin_conflict": 0,
},
"kb_reuse": {
"fingerprint_hits": len(fingerprint_kb), "gtin_exact_hits": len(gtin_kb), "resolved_by_kb": 0,
"officially_validated_new_required_fields": 0, "tenant_context_shared": 0,
"fingerprint_ncm_conflicts_not_promoted": len(ncm_conflicts),
},
"priority": {
"resolvable_now_deterministically": 0,
"resolvable_with_external_product_data": 0,
"resolvable_with_ai_semantic": 0,
"human_fact_required": 4,
"data_conflict": len(ncm_conflicts),
"erp_limitation": 0,
},
"ai_work_items": {
"total_before": ai_plan["llm_items_after"], "required_for_completeness": 0,
"optional_enrichment": ai_plan["llm_items_after"], "projected_cost_required_only": 0.0,
"required_products": 0, "classification_rule": "all 511 current LLM items concern optional/reference enrichment under the current requiredness matrix",
},
"provider_impact": {
"candidates": [
{"provider": "GS1/offical GTIN service", "gtin_matchable": len(valid_gtin), "expected_ncm_resolution": 0, "expected_cest_resolution": 0, "expected_other_fields": 0, "estimated_queries": 0, "estimated_cost": 0, "reason": "all valid GTINs already have KB identity; no paid query needed"},
{"provider": "licensed commercial API", "gtin_matchable": len(valid_gtin), "expected_ncm_resolution": 0, "expected_cest_resolution": 0, "expected_other_fields": 0, "estimated_queries": 0, "estimated_cost": 0, "reason": "not queried; KB-first policy"},
{"provider": "licensed commercial bulk dataset", "gtin_matchable": len(valid_gtin), "expected_ncm_resolution": 0, "expected_cest_resolution": 0, "expected_other_fields": 0, "estimated_queries": 0, "estimated_cost": "NOT_VERIFIED", "reason": "license and coverage not verified"},
],
"required_products": 0, "projected_cost": 0.0, "cost_per_incomplete_product_resolved": 0.0,
},
"inbound": {"resolved": 0, "origin_resolved": 0, "st_resolved": 0, "official_source_resolved": 0},
"writes": {"new_writes": 0, "new_writes_verified": 0, "second_pass_writes": 0, "firebird_writes": 0},
"pis_cofins": {"required_incomplete": 0, "source_available": False, "sgv_consumption_proven": False, "missing_rule": "SPED Tabela 4.3.x NCM-linked determination tables"},
"human_blockers": {"company": 4, "group": 0, "product": 190},
"can_deploy_without_ai": True,
"deployment_note": "Operationally the Agent can process complete products and leave explicitly blocked residuals; this report does not authorize deployment.",
"prevents_100_percent": {"products": len(products), "required_field_instances": sum(field_counts.values()), "by_field": dict(sorted(field_counts.items())), "ncm_fingerprint_conflicts_outside_baseline": len(ncm_conflicts)},
"ai_write_allowed": False,
"external_api_calls": 0,
"firebird_writes": 0,
}
(ROOT / "docs/FINAL_194_PRODUCT_CLOSURE_V1.json").write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
print(json.dumps({"artifact": result["artifact"], "incomplete_before": len(products), "incomplete_after": len(products), "gtin_kb": len(gtin_kb), "ai_required": 0}))
if __name__ == "__main__":
main()