dodil/erp-brick-manufacturing
DODIL ERP brick: manufacturing
src/brick_manufacturing/domain/ai.py
"""The explain_variance step's contract (module.yaml ai_steps.explain_variance): an assistant BESIDE the process.
It reads an order's movements, confirmations, yield and the same product's recent orders, and suggests up to three likely causes of the
variance, each with its evidence. Nothing is posted or changed by it; the cost accountant decides (accepted or dismissed) and the
decision is audited with the model, a digest of what it was shown, and who decided when. A model's answer that is not usable is no
answer: the accountant keeps the variance split alone."""
import hashlib
import json
STEP = "explain_variance"
MAX_CAUSES = 3
def digest(context: dict) -> str:
return hashlib.sha256(json.dumps(context, sort_keys=True, default=str).encode()).hexdigest()
def causes(out: dict) -> list:
"""The model's causes, checked: at most three, each {cause, evidence} of plain text; [] when nothing usable came back."""
got = []
for c in ((out or {}).get("causes") or [])[:MAX_CAUSES]:
if not isinstance(c, dict):
continue
cause, evidence = str(c.get("cause") or "").strip()[:300], str(c.get("evidence") or "").strip()[:500]
if cause and evidence:
got.append({"cause": cause, "evidence": evidence})
return got