feat(sre): close provider and telegram automation receipts
This commit is contained in:
@@ -1536,6 +1536,48 @@ async def list_ai_alert_card_delivery_readback(
|
||||
'{{ai_automation_alert_card,event_type}}',
|
||||
''
|
||||
) <> 'backup_restore_escrow_signal'
|
||||
AND m.send_status = 'sent'
|
||||
AND COALESCE(m.provider_message_id::text, '') <> ''
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,schema_version}}',
|
||||
''
|
||||
) = 'telegram_canonical_egress_receipt_v1'
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,decision}}',
|
||||
''
|
||||
) = 'allowed'
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,provider_send_performed}}',
|
||||
''
|
||||
) = 'true'
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,durable_receipt_persisted}}',
|
||||
''
|
||||
) = 'true'
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,route_id}}',
|
||||
''
|
||||
) <> ''
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,receipt_backend}}',
|
||||
''
|
||||
) NOT IN ('', 'none')
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,alert_category_zh_tw}}',
|
||||
''
|
||||
) <> ''
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,destination_role}}',
|
||||
''
|
||||
) <> ''
|
||||
) AS learning_writeback_ready_total,
|
||||
MAX(m.sent_at) AS latest_sent_at,
|
||||
MAX(m.queued_at) AS latest_queued_at
|
||||
@@ -1620,6 +1662,8 @@ async def list_ai_alert_card_delivery_readback(
|
||||
-> 'ai_automation_alert_card' AS alert_card,
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb)
|
||||
-> 'source_refs' AS source_refs,
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb)
|
||||
-> 'canonical_route_receipt' AS canonical_route_receipt,
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb)
|
||||
-> 'agent99_dispatch_receipt' AS agent99_dispatch_receipt,
|
||||
ar.error_detail AS agent99_current_receipt,
|
||||
@@ -1927,6 +1971,48 @@ async def _load_ai_alert_card_delivery_readback_direct(
|
||||
'{{ai_automation_alert_card,event_type}}',
|
||||
''
|
||||
) <> 'backup_restore_escrow_signal'
|
||||
AND m.send_status = 'sent'
|
||||
AND COALESCE(m.provider_message_id::text, '') <> ''
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,schema_version}}',
|
||||
''
|
||||
) = 'telegram_canonical_egress_receipt_v1'
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,decision}}',
|
||||
''
|
||||
) = 'allowed'
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,provider_send_performed}}',
|
||||
''
|
||||
) = 'true'
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,durable_receipt_persisted}}',
|
||||
''
|
||||
) = 'true'
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,route_id}}',
|
||||
''
|
||||
) <> ''
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,receipt_backend}}',
|
||||
''
|
||||
) NOT IN ('', 'none')
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,alert_category_zh_tw}}',
|
||||
''
|
||||
) <> ''
|
||||
AND COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
'{{canonical_route_receipt,destination_role}}',
|
||||
''
|
||||
) <> ''
|
||||
) AS learning_writeback_ready_total,
|
||||
MAX(m.sent_at) AS latest_sent_at,
|
||||
MAX(m.queued_at) AS latest_queued_at
|
||||
@@ -2028,6 +2114,10 @@ async def _load_ai_alert_card_delivery_readback_direct(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb)
|
||||
-> 'source_refs'
|
||||
)::text AS source_refs_json,
|
||||
(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb)
|
||||
-> 'canonical_route_receipt'
|
||||
)::text AS canonical_route_receipt_json,
|
||||
(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb)
|
||||
-> 'agent99_dispatch_receipt'
|
||||
@@ -2137,6 +2227,9 @@ def _ai_alert_card_direct_row(row: Mapping[str, Any]) -> dict[str, Any]:
|
||||
payload = dict(row)
|
||||
payload["alert_card"] = _json_object(payload.pop("alert_card_json", None))
|
||||
payload["source_refs"] = _json_object(payload.pop("source_refs_json", None))
|
||||
payload["canonical_route_receipt"] = _json_object(
|
||||
payload.pop("canonical_route_receipt_json", None)
|
||||
)
|
||||
payload["agent99_dispatch_receipt"] = _json_object(
|
||||
payload.pop("agent99_dispatch_receipt_json", None)
|
||||
)
|
||||
@@ -2558,6 +2651,11 @@ def _ai_alert_card_delivery_item(row: Mapping[str, Any]) -> dict[str, Any]:
|
||||
"""Convert one AI alert-card outbound mirror row into delivery evidence."""
|
||||
alert_card = _as_dict(row.get("alert_card"))
|
||||
source_refs = _as_dict(row.get("source_refs"))
|
||||
canonical_route_receipt = _as_dict(row.get("canonical_route_receipt"))
|
||||
durable_delivery_receipt = _ai_alert_card_durable_delivery_receipt(
|
||||
row,
|
||||
canonical_route_receipt=canonical_route_receipt,
|
||||
)
|
||||
frozen_agent99_dispatch_receipt = _as_dict(
|
||||
row.get("agent99_dispatch_receipt")
|
||||
)
|
||||
@@ -2580,6 +2678,7 @@ def _ai_alert_card_delivery_item(row: Mapping[str, Any]) -> dict[str, Any]:
|
||||
message_id=row.get("message_id"),
|
||||
run_id=run_id,
|
||||
project_id=project_id,
|
||||
durable_delivery_receipt=durable_delivery_receipt,
|
||||
)
|
||||
backup_restore_automation: dict[str, Any] | None = None
|
||||
gates = (
|
||||
@@ -2702,6 +2801,16 @@ def _ai_alert_card_delivery_item(row: Mapping[str, Any]) -> dict[str, Any]:
|
||||
"event_type": event_type,
|
||||
"lane": lane,
|
||||
"target": target,
|
||||
"alert_category_zh_tw": durable_delivery_receipt[
|
||||
"alert_category_zh_tw"
|
||||
],
|
||||
"route_id": durable_delivery_receipt["route_id"],
|
||||
"destination_role": durable_delivery_receipt["destination_role"],
|
||||
"durable_delivery_receipt_present": durable_delivery_receipt[
|
||||
"durable_delivery_receipt_present"
|
||||
],
|
||||
"delivery_receipt_status": durable_delivery_receipt["status"],
|
||||
"delivery_receipt": durable_delivery_receipt,
|
||||
"gates": gates,
|
||||
"declared_gates": declared_gates,
|
||||
"declared_runtime_write_gate_count": (
|
||||
@@ -2754,6 +2863,72 @@ def _ai_alert_card_delivery_item(row: Mapping[str, Any]) -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def _ai_alert_card_durable_delivery_receipt(
|
||||
row: Mapping[str, Any],
|
||||
*,
|
||||
canonical_route_receipt: Mapping[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
"""Project only provider-acknowledged, DB-persisted Telegram delivery truth."""
|
||||
|
||||
alert_category_zh_tw = str(
|
||||
canonical_route_receipt.get("alert_category_zh_tw") or ""
|
||||
).strip()
|
||||
route_id = str(canonical_route_receipt.get("route_id") or "").strip()
|
||||
destination_role = str(
|
||||
canonical_route_receipt.get("destination_role") or ""
|
||||
).strip()
|
||||
receipt_backend = str(
|
||||
canonical_route_receipt.get("receipt_backend") or ""
|
||||
).strip()
|
||||
checks = {
|
||||
"outbound_row_persisted": bool(row.get("message_id")),
|
||||
"send_status_sent": str(row.get("send_status") or "") == "sent",
|
||||
"provider_message_id_present": bool(
|
||||
str(row.get("provider_message_id") or "").strip()
|
||||
),
|
||||
"canonical_receipt_schema_valid": (
|
||||
canonical_route_receipt.get("schema_version")
|
||||
== "telegram_canonical_egress_receipt_v1"
|
||||
),
|
||||
"canonical_route_allowed": (
|
||||
canonical_route_receipt.get("decision") == "allowed"
|
||||
),
|
||||
"provider_send_performed": (
|
||||
canonical_route_receipt.get("provider_send_performed") is True
|
||||
),
|
||||
"durable_receipt_persisted": (
|
||||
canonical_route_receipt.get("durable_receipt_persisted") is True
|
||||
),
|
||||
"alert_category_present": bool(alert_category_zh_tw),
|
||||
"route_id_present": bool(route_id),
|
||||
"destination_role_present": bool(destination_role),
|
||||
"durable_receipt_backend_declared": bool(
|
||||
receipt_backend and receipt_backend != "none"
|
||||
),
|
||||
}
|
||||
missing = [name for name, passed in checks.items() if not passed]
|
||||
durable = not missing
|
||||
return {
|
||||
"schema_version": "telegram_ai_alert_card_delivery_receipt_readback_v1",
|
||||
"status": "ready" if durable else "durable_delivery_receipt_missing",
|
||||
"durable_delivery_receipt_present": durable,
|
||||
"alert_category_zh_tw": alert_category_zh_tw,
|
||||
"route_id": route_id,
|
||||
"destination_role": destination_role,
|
||||
"receipt_backend": receipt_backend,
|
||||
"destination_alias": str(
|
||||
canonical_route_receipt.get("destination_alias") or ""
|
||||
).strip(),
|
||||
"sender_bot_alias": str(
|
||||
canonical_route_receipt.get("sender_bot_alias") or ""
|
||||
).strip(),
|
||||
"provider_message_id": str(row.get("provider_message_id") or "").strip(),
|
||||
"checks": checks,
|
||||
"missing_required_evidence": missing,
|
||||
"ready": durable,
|
||||
}
|
||||
|
||||
|
||||
def _slug_ref(value: Any, fallback: str) -> str:
|
||||
slug = re.sub(r"[^a-z0-9_.:-]+", "-", str(value or "").strip().lower())
|
||||
slug = slug.strip("-")
|
||||
@@ -2767,12 +2942,23 @@ def _ai_alert_card_learning_writeback_refs(
|
||||
message_id: Any,
|
||||
run_id: Any,
|
||||
project_id: str,
|
||||
durable_delivery_receipt: Mapping[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
"""Build deterministic KM / PlayBook / RAG / MCP refs for one alert receipt."""
|
||||
event_type = str(alert_card.get("event_type") or "")
|
||||
lane = str(alert_card.get("lane") or "")
|
||||
target = str(alert_card.get("target") or "")
|
||||
delivery_required = bool(alert_card.get("delivery_receipt_readback_required"))
|
||||
alert_category_zh_tw = str(
|
||||
durable_delivery_receipt.get("alert_category_zh_tw") or ""
|
||||
).strip()
|
||||
route_id = str(durable_delivery_receipt.get("route_id") or "").strip()
|
||||
destination_role = str(
|
||||
durable_delivery_receipt.get("destination_role") or ""
|
||||
).strip()
|
||||
durable_delivery_present = bool(
|
||||
durable_delivery_receipt.get("durable_delivery_receipt_present") is True
|
||||
)
|
||||
event_slug = _slug_ref(event_type, "unknown-event")
|
||||
lane_slug = _slug_ref(lane, "unknown-lane")
|
||||
target_slug = _slug_ref(target, "unknown-target")
|
||||
@@ -2784,6 +2970,10 @@ def _ai_alert_card_learning_writeback_refs(
|
||||
("event_type", event_type),
|
||||
("lane", lane),
|
||||
("delivery_receipt_readback_required", delivery_required),
|
||||
("alert_category_zh_tw", alert_category_zh_tw),
|
||||
("route_id", route_id),
|
||||
("destination_role", destination_role),
|
||||
("durable_delivery_receipt", durable_delivery_present),
|
||||
)
|
||||
if value in ("", False, None)
|
||||
]
|
||||
@@ -2799,6 +2989,10 @@ def _ai_alert_card_learning_writeback_refs(
|
||||
"receipt_id": str(message_id or ""),
|
||||
"run_id": str(run_id or ""),
|
||||
"project_id": project_id,
|
||||
"alert_category_zh_tw": alert_category_zh_tw,
|
||||
"route_id": route_id,
|
||||
"destination_role": destination_role,
|
||||
"durable_delivery_receipt_present": durable_delivery_present,
|
||||
"km_entry_ref": f"km://{project_id}/telegram-ai-alert-card/{event_slug}",
|
||||
"playbook_candidate_ref": (
|
||||
f"playbook://{project_id}/telegram-ai-alert-card/{lane_slug}/{target_slug}"
|
||||
@@ -2819,11 +3013,11 @@ def _ai_alert_card_learning_writeback_refs(
|
||||
"alert_ids": alert_ids if isinstance(alert_ids, list) else [],
|
||||
"missing_required_fields": missing,
|
||||
"production_write_performed": False,
|
||||
"runtime_send_performed": False,
|
||||
"runtime_send_performed": durable_delivery_present,
|
||||
"next_action": (
|
||||
"none"
|
||||
if status == "ready"
|
||||
else "ensure_ai_alert_cards_include_event_type_lane_and_delivery_receipt"
|
||||
else "persist_provider_acknowledged_canonical_delivery_receipt_with_card_route_metadata"
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user