fix(sre): verify Telegram learning receipts
This commit is contained in:
@@ -586,7 +586,9 @@ def _build_consumer_readback(
|
||||
"verifier_consumer_binding_count": _target_count(bindings, "verifier"),
|
||||
"ai_agent_consumer_binding_count": _target_count(bindings, "ai_agent"),
|
||||
"target_context_receipt_write_count": sum(
|
||||
1 for binding in bindings if binding["target_write_performed"] is True
|
||||
1
|
||||
for binding in bindings
|
||||
if _binding_has_verified_target_context_write(binding)
|
||||
),
|
||||
"km_context_receipt_write_count": _target_write_count(bindings, "km"),
|
||||
"rag_context_receipt_write_count": _target_write_count(bindings, "rag"),
|
||||
@@ -895,7 +897,7 @@ def _target_rollups(bindings: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
if binding["target"] == target
|
||||
),
|
||||
"target_write_performed": any(
|
||||
binding["target_write_performed"] is True
|
||||
_binding_has_verified_target_context_write(binding)
|
||||
for binding in bindings
|
||||
if binding["target"] == target
|
||||
),
|
||||
@@ -912,7 +914,23 @@ def _target_write_count(bindings: list[dict[str, Any]], target: str) -> int:
|
||||
return sum(
|
||||
1
|
||||
for binding in bindings
|
||||
if binding["target"] == target and binding["target_write_performed"] is True
|
||||
if binding["target"] == target
|
||||
and _binding_has_verified_target_context_write(binding)
|
||||
)
|
||||
|
||||
|
||||
def _binding_has_verified_target_context_write(
|
||||
binding: Mapping[str, Any],
|
||||
) -> bool:
|
||||
receipt = _dict(binding.get("target_write_receipt"))
|
||||
consumer_receipt_id = str(receipt.get("consumer_receipt_id") or "")
|
||||
return bool(
|
||||
binding.get("target_write_performed") is True
|
||||
and consumer_receipt_id
|
||||
and consumer_receipt_id == str(binding.get("consumer_receipt_id") or "")
|
||||
and receipt.get("status") == "success"
|
||||
and receipt.get("target_context_receipt_write_performed") is True
|
||||
and receipt.get("runtime_target_write_performed") is True
|
||||
)
|
||||
|
||||
|
||||
@@ -1050,6 +1068,7 @@ def _telegram_alert_context_receipts(
|
||||
status = str(binding.get("status") or "")
|
||||
ready = status == "ready_for_consumer_context" and bool(ref)
|
||||
receipts.append({
|
||||
"receipt_kind": "alert_card_learning_registry",
|
||||
"receipt_id": (
|
||||
"telegram_alert_learning_context::"
|
||||
f"{message_id or 'unknown-message'}::{target}"
|
||||
@@ -1172,7 +1191,7 @@ def _telegram_alert_learning_context_with_consumer_fallback(
|
||||
binding
|
||||
for binding in bindings
|
||||
if binding.get("status") == "ready_for_consumer_context"
|
||||
and binding.get("target_write_performed") is True
|
||||
and _binding_has_verified_target_context_write(binding)
|
||||
and binding.get("raw_payload_included") is False
|
||||
and binding.get("target") in _TARGETS
|
||||
]
|
||||
@@ -1237,6 +1256,7 @@ def _telegram_alert_context_receipt_from_consumer_binding(
|
||||
)
|
||||
verifier_refs = _strings(binding.get("post_apply_verifier_refs"))
|
||||
return {
|
||||
"receipt_kind": "log_controlled_writeback_consumer",
|
||||
"receipt_id": (
|
||||
"telegram_alert_learning_context_fallback::"
|
||||
f"{source_receipt_id or target}"
|
||||
@@ -1256,7 +1276,18 @@ def _telegram_alert_context_receipt_from_consumer_binding(
|
||||
"lane": "metadata_feedback_controlled_writeback",
|
||||
"status": "ready_for_ai_loop_context",
|
||||
"metadata_only": True,
|
||||
"target_write_performed": True,
|
||||
"target_write_performed": binding.get("target_write_performed") is True,
|
||||
"consumer_write_receipt": {
|
||||
"consumer_receipt_id": source_receipt_id,
|
||||
"status": str(consumer_receipt.get("status") or ""),
|
||||
"target_context_receipt_write_performed": (
|
||||
consumer_receipt.get("target_context_receipt_write_performed") is True
|
||||
),
|
||||
"runtime_target_write_performed": (
|
||||
consumer_receipt.get("runtime_target_write_performed") is True
|
||||
),
|
||||
},
|
||||
"verifier_write_performed": False,
|
||||
"raw_payload_included": False,
|
||||
"target_selector": {
|
||||
"project_id": project_id,
|
||||
|
||||
@@ -18,19 +18,34 @@ from src.services.ai_agent_log_controlled_writeback_consumer_readback import (
|
||||
SCHEMA_VERSION = "telegram_alert_learning_context_post_apply_verifier_v1"
|
||||
DEFAULT_PROJECT_ID = "awoooi"
|
||||
_TARGETS = ("km", "rag", "playbook", "mcp", "verifier", "ai_agent")
|
||||
_REQUIRED_TARGET_SELECTOR_FIELDS = (
|
||||
_ALERT_CARD_RECEIPT_KIND = "alert_card_learning_registry"
|
||||
_CONSUMER_RECEIPT_KIND = "log_controlled_writeback_consumer"
|
||||
_ALERT_CARD_TARGET_SELECTOR_FIELDS = (
|
||||
"project_id",
|
||||
"run_id",
|
||||
"message_id",
|
||||
"target",
|
||||
"source_ref",
|
||||
)
|
||||
_REQUIRED_CHECKS = (
|
||||
_CONSUMER_TARGET_SELECTOR_FIELDS = (
|
||||
"project_id",
|
||||
"dispatch_receipt_id",
|
||||
"consumer_receipt_id",
|
||||
"target",
|
||||
"source_ref",
|
||||
)
|
||||
_ALERT_CARD_REQUIRED_CHECKS = (
|
||||
"ai_alert_card_delivery_receipt_present",
|
||||
"learning_registry_binding_ready",
|
||||
"metadata_only_raw_payload_absent",
|
||||
"ai_agent_context_ref_present",
|
||||
)
|
||||
_CONSUMER_REQUIRED_CHECKS = (
|
||||
"consumer_binding_ready",
|
||||
"consumer_apply_receipt_present",
|
||||
"metadata_only_raw_payload_absent",
|
||||
"post_apply_verifier_refs_present",
|
||||
)
|
||||
|
||||
|
||||
async def load_latest_telegram_alert_learning_context_post_apply_verifier(
|
||||
@@ -163,6 +178,11 @@ def build_telegram_alert_learning_context_post_apply_verifier(
|
||||
"post_apply_verifier_ref_count": sum(
|
||||
len(item["post_apply_verifier_refs"]) for item in verifier_results
|
||||
),
|
||||
"verified_target_context_write_count": sum(
|
||||
1
|
||||
for item in verifier_results
|
||||
if item["verified_target_context_write_performed"] is True
|
||||
),
|
||||
"metadata_only_receipt_count": sum(
|
||||
1 for item in verifier_results if item["metadata_only"] is True
|
||||
),
|
||||
@@ -200,27 +220,54 @@ def build_telegram_alert_learning_context_post_apply_verifier(
|
||||
|
||||
def _verify_context_receipt(receipt: dict[str, Any]) -> dict[str, Any]:
|
||||
target = str(receipt.get("target") or "")
|
||||
receipt_kind = _receipt_kind(receipt)
|
||||
receipt_kind_supported = receipt_kind in {
|
||||
_ALERT_CARD_RECEIPT_KIND,
|
||||
_CONSUMER_RECEIPT_KIND,
|
||||
}
|
||||
consumer_receipt = receipt_kind == _CONSUMER_RECEIPT_KIND
|
||||
target_selector = _dict(receipt.get("target_selector"))
|
||||
source_diff = _dict(receipt.get("source_of_truth_diff"))
|
||||
check_mode = _dict(receipt.get("check_mode"))
|
||||
rollback = _dict(receipt.get("rollback"))
|
||||
post_apply_verifier = _dict(receipt.get("post_apply_verifier"))
|
||||
verifier_refs = _strings(post_apply_verifier.get("verifier_refs"))
|
||||
required_target_selector_fields = (
|
||||
_CONSUMER_TARGET_SELECTOR_FIELDS
|
||||
if consumer_receipt
|
||||
else _ALERT_CARD_TARGET_SELECTOR_FIELDS
|
||||
)
|
||||
required_checks = (
|
||||
_CONSUMER_REQUIRED_CHECKS
|
||||
if consumer_receipt
|
||||
else _ALERT_CARD_REQUIRED_CHECKS
|
||||
)
|
||||
|
||||
target_selector_missing = [
|
||||
key for key in _REQUIRED_TARGET_SELECTOR_FIELDS if not target_selector.get(key)
|
||||
key for key in required_target_selector_fields if not target_selector.get(key)
|
||||
]
|
||||
checks = _strings(check_mode.get("checks"))
|
||||
missing_checks = [check for check in _REQUIRED_CHECKS if check not in checks]
|
||||
missing_checks = [check for check in required_checks if check not in checks]
|
||||
source_diff_verified = _source_diff_verified(
|
||||
source_diff,
|
||||
target,
|
||||
receipt_kind=receipt_kind,
|
||||
)
|
||||
target_write_contract_verified = _target_write_contract_verified(
|
||||
receipt,
|
||||
receipt_kind=receipt_kind,
|
||||
)
|
||||
failed_checks: list[str] = []
|
||||
|
||||
if target not in _TARGETS:
|
||||
failed_checks.append("target_in_allowed_set")
|
||||
if not receipt_kind_supported:
|
||||
failed_checks.append("receipt_kind_supported")
|
||||
if receipt.get("status") != "ready_for_ai_loop_context":
|
||||
failed_checks.append("receipt_ready_for_ai_loop_context")
|
||||
if target_selector_missing:
|
||||
failed_checks.append("target_selector_required_fields_present")
|
||||
if not _source_diff_verified(source_diff, target):
|
||||
if not source_diff_verified:
|
||||
failed_checks.append("source_of_truth_diff_verified")
|
||||
if check_mode.get("enabled") is not True or missing_checks:
|
||||
failed_checks.append("check_mode_required_checks_present")
|
||||
@@ -232,13 +279,21 @@ def _verify_context_receipt(receipt: dict[str, Any]) -> dict[str, Any]:
|
||||
failed_checks.append("metadata_only_receipt")
|
||||
if receipt.get("raw_payload_included") is not False:
|
||||
failed_checks.append("raw_payload_absent")
|
||||
if receipt.get("target_write_performed") is not False:
|
||||
failed_checks.append("target_write_not_performed_by_verifier")
|
||||
target_write_check = (
|
||||
"consumer_write_receipt_verified"
|
||||
if consumer_receipt
|
||||
else "target_write_not_performed_by_verifier"
|
||||
)
|
||||
if not target_write_contract_verified:
|
||||
failed_checks.append(target_write_check)
|
||||
if receipt.get("verifier_write_performed") is True:
|
||||
failed_checks.append("verifier_write_not_performed")
|
||||
if not receipt.get("ai_agent_context_ref"):
|
||||
failed_checks.append("ai_agent_context_ref_present")
|
||||
|
||||
passed_checks = [
|
||||
"target_in_allowed_set",
|
||||
"receipt_kind_supported",
|
||||
"receipt_ready_for_ai_loop_context",
|
||||
"target_selector_required_fields_present",
|
||||
"source_of_truth_diff_verified",
|
||||
@@ -247,7 +302,8 @@ def _verify_context_receipt(receipt: dict[str, Any]) -> dict[str, Any]:
|
||||
"post_apply_verifier_refs_present",
|
||||
"metadata_only_receipt",
|
||||
"raw_payload_absent",
|
||||
"target_write_not_performed_by_verifier",
|
||||
target_write_check,
|
||||
"verifier_write_not_performed",
|
||||
"ai_agent_context_ref_present",
|
||||
]
|
||||
passed_checks = [check for check in passed_checks if check not in failed_checks]
|
||||
@@ -258,6 +314,7 @@ def _verify_context_receipt(receipt: dict[str, Any]) -> dict[str, Any]:
|
||||
"source_run_id": str(receipt.get("source_run_id") or ""),
|
||||
"source_message_id": str(receipt.get("source_message_id") or ""),
|
||||
"work_item_id": str(receipt.get("work_item_id") or "CIR-P0-TG-001"),
|
||||
"receipt_kind": receipt_kind,
|
||||
"target": target,
|
||||
"consumer_surface": str(receipt.get("consumer_surface") or ""),
|
||||
"verifier_status": "passed" if not failed_checks else "failed",
|
||||
@@ -265,13 +322,19 @@ def _verify_context_receipt(receipt: dict[str, Any]) -> dict[str, Any]:
|
||||
"failed_checks": failed_checks,
|
||||
"target_selector_verified": not target_selector_missing,
|
||||
"target_selector_missing_fields": target_selector_missing,
|
||||
"source_of_truth_diff_verified": _source_diff_verified(source_diff, target),
|
||||
"source_of_truth_diff_verified": source_diff_verified,
|
||||
"check_mode_verified": check_mode.get("enabled") is True and not missing_checks,
|
||||
"check_mode_missing_checks": missing_checks,
|
||||
"rollback_verified": (
|
||||
rollback.get("required") is True and bool(rollback.get("rollback_ref"))
|
||||
),
|
||||
"post_apply_verifier_refs": verifier_refs,
|
||||
"target_write_contract_verified": target_write_contract_verified,
|
||||
"verified_target_context_write_performed": (
|
||||
receipt.get("target_write_performed") is True
|
||||
and target_write_contract_verified
|
||||
),
|
||||
"verifier_write_performed": False,
|
||||
"metadata_only": receipt.get("metadata_only") is True,
|
||||
"raw_payload_included": receipt.get("raw_payload_included") is True,
|
||||
"runtime_target_write_performed": False,
|
||||
@@ -310,7 +373,36 @@ def _active_blockers(
|
||||
return blockers
|
||||
|
||||
|
||||
def _source_diff_verified(source_diff: dict[str, Any], target: str) -> bool:
|
||||
def _receipt_kind(receipt: Mapping[str, Any]) -> str:
|
||||
explicit = str(receipt.get("receipt_kind") or "")
|
||||
if explicit:
|
||||
return explicit
|
||||
receipt_id = str(receipt.get("receipt_id") or "")
|
||||
target_selector = _dict(receipt.get("target_selector"))
|
||||
if receipt_id.startswith("telegram_alert_learning_context_fallback::") or (
|
||||
target_selector.get("dispatch_receipt_id")
|
||||
and target_selector.get("consumer_receipt_id")
|
||||
):
|
||||
return _CONSUMER_RECEIPT_KIND
|
||||
return _ALERT_CARD_RECEIPT_KIND
|
||||
|
||||
|
||||
def _source_diff_verified(
|
||||
source_diff: dict[str, Any],
|
||||
target: str,
|
||||
*,
|
||||
receipt_kind: str,
|
||||
) -> bool:
|
||||
if receipt_kind == _CONSUMER_RECEIPT_KIND:
|
||||
return bool(
|
||||
source_diff.get("current_state")
|
||||
== "telegram_alert_registry_unavailable_or_not_ready"
|
||||
and source_diff.get("desired_state")
|
||||
== "ai_loop_agent_context_receipt_available"
|
||||
and source_diff.get("delta_kind")
|
||||
== f"log_controlled_writeback_{target}_context_fallback"
|
||||
and source_diff.get("raw_payload_included") is False
|
||||
)
|
||||
return bool(
|
||||
source_diff.get("current_state") == "telegram_alert_learning_registry_readable"
|
||||
and source_diff.get("desired_state")
|
||||
@@ -320,6 +412,26 @@ def _source_diff_verified(source_diff: dict[str, Any], target: str) -> bool:
|
||||
)
|
||||
|
||||
|
||||
def _target_write_contract_verified(
|
||||
receipt: Mapping[str, Any],
|
||||
*,
|
||||
receipt_kind: str,
|
||||
) -> bool:
|
||||
if receipt.get("verifier_write_performed") is True:
|
||||
return False
|
||||
if receipt_kind != _CONSUMER_RECEIPT_KIND:
|
||||
return receipt.get("target_write_performed") is False
|
||||
consumer_write_receipt = _dict(receipt.get("consumer_write_receipt"))
|
||||
return bool(
|
||||
receipt.get("target_write_performed") is True
|
||||
and consumer_write_receipt.get("consumer_receipt_id")
|
||||
and consumer_write_receipt.get("status") == "success"
|
||||
and consumer_write_receipt.get("target_context_receipt_write_performed")
|
||||
is True
|
||||
and consumer_write_receipt.get("runtime_target_write_performed") is True
|
||||
)
|
||||
|
||||
|
||||
def _dict(value: Any) -> dict[str, Any]:
|
||||
return value if isinstance(value, dict) else {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user