feat(telegram): enforce canonical product routing

This commit is contained in:
ogt
2026-07-14 19:14:11 +08:00
parent 1431abba54
commit 180e708444
54 changed files with 7746 additions and 740 deletions

View File

@@ -1127,17 +1127,49 @@ class ApprovalExecutionService:
approval_id=str(approval.id),
)
from src.core.config import get_settings
from src.services.telegram_gateway import get_telegram_gateway
settings = get_settings()
from src.services.telegram_gateway import (
_telegram_send_delivery_succeeded,
get_telegram_gateway,
)
gateway = get_telegram_gateway()
target_chat_id = settings.SRE_GROUP_CHAT_ID
target_chat_id = gateway.canonical_destination_chat_id(
product_id="awoooi",
signal_family="incident_lifecycle",
severity="P1",
)
if not target_chat_id:
logger.warning(
"push_execution_result_no_target_chat",
incident_id=approval.incident_id,
approval_id=str(approval.id),
)
try:
from src.repositories.alert_operation_log_repository import (
get_alert_operation_log_repository,
)
await get_alert_operation_log_repository().append(
"NOTIFICATION_CLASSIFIED",
incident_id=approval.incident_id,
approval_id=str(approval.id),
actor="approval_execution",
action_detail="telegram_execution_result_no_send",
success=False,
error_message="canonical_route_unavailable",
context={
"delivery_status": "blocked_no_route",
"execution_success": success,
"execution_kind": execution_kind,
"repair_executed": repair_executed,
"repair_attempted": repair_attempted,
},
)
except Exception as _log_e:
logger.warning(
"alert_op_telegram_result_no_send_write_failed",
approval_id=str(approval.id),
error=str(_log_e),
)
return
# 2026-04-19 ogt + Claude Opus 4.7 修 AP-2: 除了 reply 外,
@@ -1157,6 +1189,7 @@ class ApprovalExecutionService:
km_info = ""
try:
from sqlalchemy import text as _sql
from src.db.base import get_db_context
async with get_db_context() as _db:
_km_row = await _db.execute(
@@ -1187,20 +1220,20 @@ class ApprovalExecutionService:
truth_chain_row = incident_truth_chain_button_row(
approval.incident_id or ""
)
payload: dict[str, Any] = {
"chat_id": target_chat_id,
"text": text,
"parse_mode": "HTML",
"disable_web_page_preview": True,
}
if truth_chain_row:
payload["reply_markup"] = {
"inline_keyboard": [truth_chain_row],
}
if orig_msg_id is not None:
payload["reply_to_message_id"] = orig_msg_id
await gateway._send_request("sendMessage", payload)
delivery_result = await gateway.send_canonical_message(
product_id="awoooi",
signal_family="incident_lifecycle",
severity="P1",
text=text,
parse_mode="HTML",
reply_markup=(
{"inline_keyboard": [truth_chain_row]}
if truth_chain_row
else None
),
reply_to_message_id=orig_msg_id,
)
delivery_succeeded = _telegram_send_delivery_succeeded(delivery_result)
context_execution_kind = execution_kind or (
"no_action" if no_action else "execution"
)
@@ -1209,8 +1242,22 @@ class ApprovalExecutionService:
if repair_executed is not None
else bool(success and not no_action)
)
logger.info(
"push_execution_result_sent",
delivery_status = str(
(
delivery_result.get("_awooop_delivery_status")
if isinstance(delivery_result, dict)
else None
)
or "missing_provider_ack"
)
log_event = (
"push_execution_result_sent"
if delivery_succeeded
else "push_execution_result_no_send"
)
log_method = logger.info if delivery_succeeded else logger.warning
log_method(
log_event,
incident_id=approval.incident_id,
approval_id=str(approval.id),
success=success,
@@ -1220,6 +1267,7 @@ class ApprovalExecutionService:
repair_executed=context_repair_executed,
operator_outcome_state=outcome.get("state"),
needs_human=outcome.get("needs_human"),
delivery_status=delivery_status,
)
try:
from src.repositories.alert_operation_log_repository import (
@@ -1227,13 +1275,25 @@ class ApprovalExecutionService:
)
await get_alert_operation_log_repository().append(
"TELEGRAM_RESULT_SENT",
(
"TELEGRAM_RESULT_SENT"
if delivery_succeeded
else "NOTIFICATION_CLASSIFIED"
),
incident_id=approval.incident_id,
approval_id=str(approval.id),
actor="approval_execution",
action_detail="telegram_execution_result_sent",
success=success,
error_message=error,
action_detail=(
"telegram_execution_result_sent"
if delivery_succeeded
else "telegram_execution_result_no_send"
),
success=(success if delivery_succeeded else False),
error_message=(
error
if delivery_succeeded
else delivery_status or "missing_provider_ack"
),
context={
"reply_to_message_id": orig_msg_id,
"execution_kind": context_execution_kind,
@@ -1241,6 +1301,9 @@ class ApprovalExecutionService:
"repair_attempted": repair_attempted,
"operator_outcome": outcome,
"delivery": "reply" if orig_msg_id is not None else "standalone",
"delivery_status": delivery_status,
"provider_delivery_acknowledged": delivery_succeeded,
"execution_success": success,
},
)
except Exception as _log_e: