feat: close pre-zai local blockers

This commit is contained in:
OpenCode
2026-09-21 16:46:59 -03:00
parent 84b08c2150
commit b10131953c
13 changed files with 2775 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
"""Evidence reconstruction for historical KB conflicts.
The seed payload contains the current 90 observable conflicts but not the
identity/evidence rows for the remaining 73 historical conflict records. This
module makes that limitation explicit instead of fabricating identities.
"""
from collections import Counter
RESOLVED_EQUIVALENT_PRODUCT = "RESOLVED_EQUIVALENT_PRODUCT"
FALSE_MATCH = "FALSE_MATCH"
VALID_NCM_CONFLICT = "VALID_NCM_CONFLICT"
STALE_OBSERVATION = "STALE_OBSERVATION"
INSUFFICIENT_EVIDENCE = "INSUFFICIENT_EVIDENCE"
def reconstruct_historical_conflicts(payload: dict, historical_count: int = 73) -> dict:
products = payload.get("products", [])
gtin_count = sum(1 for product in products if product.get("gtin"))
fingerprint_count = sum(1 for product in products if product.get("fingerprint"))
evidence_summary = {
"payload_products": len(products),
"payload_gtin_products": gtin_count,
"payload_fingerprint_products": fingerprint_count,
"fields_available": ["gtin", "fingerprint", "normalized_description", "unit", "ncm", "observations"],
"historical_identity_records_found": 0,
}
rows = []
for index in range(1, historical_count + 1):
rows.append({
"historical_conflict_id": f"HISTORICAL-{index:03d}",
"classification": INSUFFICIENT_EVIDENCE,
"evidence_search": evidence_summary,
"resolution": "PRESERVE_CONFLICT",
"reason": "original conflict identity/evidence row is absent from available artifacts; no NCM selected",
})
return {
"artifact": "KB_HISTORICAL_CONFLICT_AUDIT_V1",
"historical_conflicts_before": historical_count,
"historical_conflicts_resolved": 0,
"historical_conflicts_remaining": historical_count,
"classification_counts": dict(Counter(row["classification"] for row in rows)),
"conflicts": rows,
"current_observable_conflicts_preserved": 90,
"current_conflict_policy": "NCM_CONFLICT rows remain blocked; no majority resolution",
"evidence_complete": False,
}
@@ -0,0 +1,21 @@
"""Audit the historical KB conflicts without inventing missing evidence."""
import argparse
import json
from pathlib import Path
from central.app.ai.historical_conflicts import reconstruct_historical_conflicts
def main(payload_path="docs/runs/ai_kb_seed_payload.json", output="docs/KB_HISTORICAL_CONFLICT_AUDIT_V1.json"):
payload = json.loads(Path(payload_path).read_text(encoding="utf-8"))
report = reconstruct_historical_conflicts(payload)
Path(output).write_text(json.dumps(report, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
return report
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--payload", default="docs/runs/ai_kb_seed_payload.json")
parser.add_argument("--output", default="docs/KB_HISTORICAL_CONFLICT_AUDIT_V1.json")
args = parser.parse_args()
print(json.dumps(main(args.payload, args.output), indent=2, ensure_ascii=False))
@@ -0,0 +1,13 @@
def test_company_session_is_company_first_review_only_and_has_ipi_impact(tmp_path):
from scripts.run_company_decision_session import build_session
session = build_session(tmp_path, "docs/LOCAL_FISCAL_CONTEXT_QUESTIONS_V2.json") if (tmp_path / "docs/LOCAL_FISCAL_CONTEXT_QUESTIONS_V2.json").exists() else None
# Use a small local fixture because the production artifact is intentionally large.
if session is None:
(tmp_path / "docs/runs/multi_db").mkdir(parents=True)
(tmp_path / "docs").mkdir(exist_ok=True)
(tmp_path / "docs/LOCAL_FISCAL_CONTEXT_QUESTIONS_V2.json").write_text('{"questions":[{"QUESTION_ID":"Q1","DATABASE":"A","SCOPE":"COMPANY","KEY":"entry_scope","VALID_OPTIONS":["INTRA","INTER","MIXED_OR_DYNAMIC"],"QUESTION":"Escopo?"}]}', encoding="utf-8")
(tmp_path / "docs/FISCAL_COVERAGE_FULL_V3.json").write_text('{"total_products":1,"ipi":{"numeric":1,"non_numeric":0,"ambiguous":0}}', encoding="utf-8")
session = build_session(tmp_path)
assert all(question["scope"] == "COMPANY" for question in session["questions"])
assert session["persisted"] is False and session["firebird_writes"] == 0
@@ -0,0 +1,9 @@
def test_missing_historical_identity_is_preserved_not_guessed():
from central.app.ai.historical_conflicts import INSUFFICIENT_EVIDENCE, reconstruct_historical_conflicts
report = reconstruct_historical_conflicts({"products": [{"gtin": "x", "fingerprint": "f"}]}, historical_count=73)
assert report["historical_conflicts_before"] == 73
assert report["historical_conflicts_resolved"] == 0
assert report["historical_conflicts_remaining"] == 73
assert report["classification_counts"] == {INSUFFICIENT_EVIDENCE: 73}
assert all(row["resolution"] == "PRESERVE_CONFLICT" for row in report["conflicts"])
+9
View File
@@ -0,0 +1,9 @@
def test_pre_zai_state_is_explicit_about_human_and_external_blockers(tmp_path):
from scripts.generate_pre_zai_state import build_state
state = build_state(tmp_path)
assert state["DETERMINISTIC_COMPLETE"] is True
assert state["COMPANY_CONTEXT_COMPLETE"] is False
assert state["FIREBIRD_AI_WRITES"] == 0
assert state["READY_FOR_ZAI_GOLD_EVAL"] is True
assert state["REMAINING_EXTERNAL_BLOCKERS"] == ["EXTERNAL_AI_CREDENTIAL_OR_BALANCE"]