This commit is contained in:
@@ -44,6 +44,9 @@ DIRECT_MAPPING_CANDIDATE_EXCEPTION_AUTO_RESOLUTION_POLICY = (
|
||||
DIRECT_MAPPING_CANDIDATE_EXCEPTION_RESOLUTION_CLOSEOUT_POLICY = (
|
||||
"read_only_pchome_growth_direct_mapping_candidate_exception_resolution_closeout"
|
||||
)
|
||||
DIRECT_MAPPING_RETRY_CANDIDATE_DECISION_PACKAGE_POLICY = (
|
||||
"read_only_pchome_growth_direct_mapping_retry_candidate_decision_package"
|
||||
)
|
||||
AI_AUTOMATION_READINESS_POLICY = "read_only_pchome_growth_ai_automation_readiness"
|
||||
EVIDENCE_ENRICHMENT_PREVIEW_POLICY = "read_only_pchome_growth_evidence_enrichment_preview"
|
||||
EVIDENCE_SOURCE_PREVIEW_POLICY = "read_only_pchome_growth_evidence_source_preview"
|
||||
@@ -1840,6 +1843,83 @@ def _summarize_exception_resolution_closeout_receipts(receipts: list[dict[str, A
|
||||
}
|
||||
|
||||
|
||||
def _retry_candidates_from_closeout_receipts(receipts: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
retry_candidates: list[dict[str, Any]] = []
|
||||
seen: set[str] = set()
|
||||
for receipt in receipts:
|
||||
retry_search = receipt.get("retry_search") or {}
|
||||
for candidate in retry_search.get("candidates") or []:
|
||||
candidate = dict(candidate)
|
||||
candidate.setdefault("source_resolution_closeout_receipt_id", receipt.get("receipt_id"))
|
||||
candidate.setdefault("source_resolution_artifact_id", receipt.get("source_artifact_id"))
|
||||
dedupe_key = "|".join([
|
||||
str(candidate.get("target_pchome_product_id") or ""),
|
||||
str(candidate.get("product_id") or ""),
|
||||
str(candidate.get("source_resolution_artifact_id") or ""),
|
||||
])
|
||||
if dedupe_key in seen:
|
||||
continue
|
||||
seen.add(dedupe_key)
|
||||
retry_candidates.append(candidate)
|
||||
return retry_candidates
|
||||
|
||||
|
||||
def _build_no_write_auto_compare_verifier_receipt(decision: dict[str, Any]) -> dict[str, Any]:
|
||||
subject = decision.get("subject") or {}
|
||||
receipt_basis = {
|
||||
"decision_id": decision.get("decision_id"),
|
||||
"subject": subject,
|
||||
"confidence": decision.get("confidence"),
|
||||
"decision": decision.get("decision"),
|
||||
}
|
||||
receipt_hash = hashlib.sha256(
|
||||
json.dumps(receipt_basis, ensure_ascii=False, sort_keys=True, default=str).encode("utf-8")
|
||||
).hexdigest()
|
||||
checks = [
|
||||
{
|
||||
"check": "routes_to_no_write_auto_compare_receipt",
|
||||
"passed": decision.get("decision") == "route_to_no_write_auto_compare_receipt",
|
||||
},
|
||||
{
|
||||
"check": "target_pchome_product_id_present",
|
||||
"passed": bool(subject.get("target_pchome_product_id")),
|
||||
},
|
||||
{
|
||||
"check": "momo_product_id_present",
|
||||
"passed": bool(subject.get("momo_product_id")),
|
||||
},
|
||||
{
|
||||
"check": "target_match_score_present",
|
||||
"passed": decision.get("confidence") not in (None, ""),
|
||||
},
|
||||
{
|
||||
"check": "database_write_locked",
|
||||
"passed": True,
|
||||
},
|
||||
]
|
||||
ready = all(check["passed"] for check in checks)
|
||||
return {
|
||||
"receipt_id": f"pchome-direct-mapping-no-write-verifier-{receipt_hash[:16]}",
|
||||
"source_decision_id": decision.get("decision_id"),
|
||||
"stage": "P2_retry_candidate_no_write_verifier_input",
|
||||
"receipt_status": "NO_WRITE_VERIFIER_INPUT_READY" if ready else "NO_WRITE_VERIFIER_INPUT_BLOCKED",
|
||||
"subject": subject,
|
||||
"confidence": decision.get("confidence"),
|
||||
"verification_checks": checks,
|
||||
"ready_for_no_write_verifier": ready,
|
||||
"ready_for_controlled_apply": False,
|
||||
"next_package": "auto_policy_db_apply_verifier_artifact_preview_after_no_write_receipt",
|
||||
"guardrails": {
|
||||
"machine_actionable": True,
|
||||
"writes_database": False,
|
||||
"persists_candidate": False,
|
||||
"requires_no_write_receipt": True,
|
||||
"requires_verifier_before_persistence": True,
|
||||
"requires_rollback_and_readback": True,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def build_pchome_direct_mapping_auto_search_package(
|
||||
payload: dict[str, Any],
|
||||
batch_size: int = 5,
|
||||
@@ -2271,6 +2351,125 @@ def build_pchome_direct_mapping_candidate_exception_resolution_closeout_package(
|
||||
}
|
||||
|
||||
|
||||
def build_pchome_direct_mapping_retry_candidate_decision_package(
|
||||
payload: dict[str, Any],
|
||||
batch_size: int = 5,
|
||||
*,
|
||||
execute_search: bool = False,
|
||||
execute_retry_search: bool = False,
|
||||
limit_per_product: int = 8,
|
||||
max_terms_per_product: int = 5,
|
||||
min_score: float = 0.45,
|
||||
search_func: Any = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Route retry-search candidates back into machine decisions and no-write verifier receipts."""
|
||||
closeout = build_pchome_direct_mapping_candidate_exception_resolution_closeout_package(
|
||||
payload,
|
||||
batch_size=batch_size,
|
||||
execute_search=execute_search,
|
||||
execute_retry_search=execute_retry_search,
|
||||
limit_per_product=limit_per_product,
|
||||
max_terms_per_product=max_terms_per_product,
|
||||
min_score=min_score,
|
||||
search_func=search_func,
|
||||
)
|
||||
closeout_package = closeout.get("closeout_package") or {}
|
||||
closeout_receipts = list(closeout_package.get("closeout_receipts") or [])
|
||||
retry_candidates = _retry_candidates_from_closeout_receipts(closeout_receipts)
|
||||
try:
|
||||
effective_min_score = max(0.35, min(float(min_score), 0.95))
|
||||
except (TypeError, ValueError):
|
||||
effective_min_score = 0.45
|
||||
|
||||
retry_decisions = [
|
||||
_build_candidate_decision_envelope(candidate, min_score=effective_min_score)
|
||||
for candidate in retry_candidates
|
||||
]
|
||||
no_write_decisions = [
|
||||
decision
|
||||
for decision in retry_decisions
|
||||
if decision.get("decision") == "route_to_no_write_auto_compare_receipt"
|
||||
]
|
||||
machine_review_decisions = [
|
||||
decision
|
||||
for decision in retry_decisions
|
||||
if decision.get("decision") == "route_to_machine_review_decision"
|
||||
]
|
||||
no_write_verifier_receipts = [
|
||||
_build_no_write_auto_compare_verifier_receipt(decision)
|
||||
for decision in no_write_decisions
|
||||
]
|
||||
machine_review_exception_receipts = [
|
||||
_build_candidate_exception_receipt(decision)
|
||||
for decision in machine_review_decisions
|
||||
]
|
||||
|
||||
selected_direct_count = int((closeout.get("summary") or {}).get("selected_direct_mapping_count") or 0)
|
||||
if not selected_direct_count:
|
||||
result = "NO_DIRECT_MAPPING_TARGETS"
|
||||
elif retry_decisions:
|
||||
result = "DIRECT_MAPPING_RETRY_CANDIDATE_DECISION_PACKAGE_READY"
|
||||
elif int((closeout.get("summary") or {}).get("retry_search_ready_count") or 0):
|
||||
result = "WAITING_FOR_RETRY_CANDIDATES"
|
||||
else:
|
||||
result = "WAITING_FOR_EXCEPTION_RESOLUTION_CLOSEOUT"
|
||||
|
||||
return {
|
||||
"policy": DIRECT_MAPPING_RETRY_CANDIDATE_DECISION_PACKAGE_POLICY,
|
||||
"result": result,
|
||||
"success": bool(closeout.get("success")),
|
||||
"generated_at": closeout.get("generated_at"),
|
||||
"source_policy": closeout.get("policy"),
|
||||
"stats": closeout.get("stats") or {},
|
||||
"backlog": closeout.get("backlog") or {},
|
||||
"summary": {
|
||||
"direct_mapping_count": int((closeout.get("summary") or {}).get("direct_mapping_count") or 0),
|
||||
"selected_direct_mapping_count": selected_direct_count,
|
||||
"exception_resolution_closeout_receipt_count": int(
|
||||
(closeout.get("summary") or {}).get("exception_resolution_closeout_receipt_count") or 0
|
||||
),
|
||||
"retry_candidate_count": len(retry_candidates),
|
||||
"retry_candidate_decision_count": len(retry_decisions),
|
||||
"retry_no_write_verifier_input_count": len(no_write_verifier_receipts),
|
||||
"retry_machine_review_exception_count": len(machine_review_exception_receipts),
|
||||
"ready_for_no_write_verifier_count": sum(
|
||||
1 for receipt in no_write_verifier_receipts if receipt.get("ready_for_no_write_verifier")
|
||||
),
|
||||
"ready_for_controlled_apply_count": 0,
|
||||
"writes_database_count": 0,
|
||||
"persists_candidate_count": 0,
|
||||
},
|
||||
"retry_candidate_decision_package": {
|
||||
"stage": "P2_retry_candidate_machine_decision",
|
||||
"execute_search": bool(execute_search),
|
||||
"execute_retry_search": bool(execute_retry_search),
|
||||
"retry_candidates": retry_candidates,
|
||||
"retry_candidate_decisions": retry_decisions,
|
||||
"no_write_verifier_receipts": no_write_verifier_receipts,
|
||||
"machine_review_exception_receipts": machine_review_exception_receipts,
|
||||
"manual_review_mode": "exception_only",
|
||||
},
|
||||
"upstream_closeout_summary": closeout.get("summary") or {},
|
||||
"next_actions": [
|
||||
"Send ready no-write verifier receipts into verifier artifact preview before any persistence.",
|
||||
"Route retry machine-review exceptions back through exception auto-resolution.",
|
||||
"Only controlled apply can reduce direct_mapping_count after verifier, rollback, and production readback pass.",
|
||||
],
|
||||
"safety": {
|
||||
"read_only_preview": True,
|
||||
"executes_search": bool(execute_search),
|
||||
"executes_retry_search": bool(execute_retry_search),
|
||||
"writes_database": False,
|
||||
"persists_candidate": False,
|
||||
"syncs_external_offers": False,
|
||||
"dispatches_telegram": False,
|
||||
"llm_calls_in_preview": False,
|
||||
"gemini_allowed": False,
|
||||
"requires_production_version_truth": True,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def build_pchome_evidence_enrichment_preview(payload: dict[str, Any], batch_size: int = 5) -> dict[str, Any]:
|
||||
"""Build a read-only evidence enrichment package for mapping targets."""
|
||||
operator_preview = build_pchome_mapping_operator_preview(payload, batch_size=batch_size)
|
||||
|
||||
Reference in New Issue
Block a user