style(platform): Apply linters and formatters to AllQuiet integration

- Applied isort for import sorting
- Applied black for code formatting
- Applied ruff for linting
- Fixed type annotations for severity parameter in retry.py
- All formatters and linters now pass successfully

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Nicholas Tindle
2025-10-21 15:36:18 -05:00
parent c559431131
commit 2f9f74fce9
5 changed files with 102 additions and 67 deletions

View File

@@ -1224,16 +1224,16 @@ class ExecutionProcessor:
content=alert_message,
channel=DiscordChannel.PRODUCT,
correlation_id=correlation_id,
severity='critical',
status='open',
severity="critical",
status="open",
extra_attributes={
'user_id': user_id,
'user_email': user_email or 'unknown',
'balance': f"${e.balance / 100:.2f}",
'attempted_cost': f"${abs(e.amount) / 100:.2f}",
'shortfall': f"${abs(shortfall) / 100:.2f}",
'agent_name': metadata.name if metadata else 'Unknown',
}
"user_id": user_id,
"user_email": user_email or "unknown",
"balance": f"${e.balance / 100:.2f}",
"attempted_cost": f"${abs(e.amount) / 100:.2f}",
"shortfall": f"${abs(shortfall) / 100:.2f}",
"agent_name": metadata.name if metadata else "Unknown",
},
)
except Exception as alert_error:
logger.error(
@@ -1287,15 +1287,15 @@ class ExecutionProcessor:
content=alert_message,
channel=DiscordChannel.PRODUCT,
correlation_id=correlation_id,
severity='warning',
status='open',
severity="warning",
status="open",
extra_attributes={
'user_id': user_id,
'user_email': user_email or 'unknown',
'current_balance': f"${current_balance / 100:.2f}",
'transaction_cost': f"${transaction_cost / 100:.2f}",
'threshold': f"${LOW_BALANCE_THRESHOLD / 100:.2f}",
}
"user_id": user_id,
"user_email": user_email or "unknown",
"current_balance": f"${current_balance / 100:.2f}",
"transaction_cost": f"${transaction_cost / 100:.2f}",
"threshold": f"${LOW_BALANCE_THRESHOLD / 100:.2f}",
},
)
except Exception as e:
logger.error(f"Failed to send low balance Discord alert: {e}")

View File

@@ -12,7 +12,7 @@ from backend.util.clients import (
get_database_manager_client,
get_notification_manager_client,
)
from backend.util.metrics import sentry_capture_error, DiscordChannel
from backend.util.metrics import DiscordChannel, sentry_capture_error
from backend.util.settings import Config
logger = logging.getLogger(__name__)
@@ -79,22 +79,26 @@ class BlockErrorMonitor:
# Send alert with correlation ID for block errors
# We'll create a simple hash of the block IDs that have errors
blocks_with_errors = [
stats.block_id for name, stats in block_stats.items()
if stats.total_executions >= 10 and stats.error_rate >= threshold * 100
stats.block_id
for name, stats in block_stats.items()
if stats.total_executions >= 10
and stats.error_rate >= threshold * 100
]
correlation_id = f"block_errors_{len(blocks_with_errors)}_blocks_{end_time.date()}"
correlation_id = (
f"block_errors_{len(blocks_with_errors)}_blocks_{end_time.date()}"
)
self.notification_client.system_alert(
content=msg,
channel=DiscordChannel.PLATFORM,
correlation_id=correlation_id,
severity='warning',
status='open',
severity="warning",
status="open",
extra_attributes={
'blocks_affected': str(len(critical_alerts)),
'date': end_time.date().isoformat(),
'threshold': f"{threshold * 100}%",
}
"blocks_affected": str(len(critical_alerts)),
"date": end_time.date().isoformat(),
"threshold": f"{threshold * 100}%",
},
)
logger.info(
@@ -115,13 +119,13 @@ class BlockErrorMonitor:
content=top_blocks_msg,
channel=DiscordChannel.PLATFORM,
correlation_id=correlation_id,
severity='minor',
status='open',
severity="minor",
status="open",
extra_attributes={
'type': 'daily_summary',
'date': end_time.date().isoformat(),
'top_blocks_count': str(self.include_top_blocks),
}
"type": "daily_summary",
"date": end_time.date().isoformat(),
"top_blocks_count": str(self.include_top_blocks),
},
)
logger.info("Sent top blocks summary")
@@ -144,12 +148,12 @@ class BlockErrorMonitor:
content=msg,
channel=DiscordChannel.PLATFORM,
correlation_id=correlation_id,
severity='critical',
status='open',
severity="critical",
status="open",
extra_attributes={
'error_type': type(e).__name__,
'error_message': str(e)[:200],
}
"error_type": type(e).__name__,
"error_message": str(e)[:200],
},
)
return msg

View File

@@ -33,7 +33,12 @@ from backend.data.user import (
from backend.notifications.email import EmailSender
from backend.util.clients import get_database_manager_async_client
from backend.util.logging import TruncatedLogger
from backend.util.metrics import AllQuietAlert, DiscordChannel, discord_send_alert, send_allquiet_alert
from backend.util.metrics import (
AllQuietAlert,
DiscordChannel,
discord_send_alert,
send_allquiet_alert,
)
from backend.util.retry import continuous_retry
from backend.util.service import (
AppService,
@@ -427,8 +432,8 @@ class NotificationManager(AppService):
content: str,
channel: DiscordChannel = DiscordChannel.PLATFORM,
correlation_id: str | None = None,
severity: Literal['warning', 'critical', 'minor'] = 'warning',
status: Literal['resolved', 'open'] = 'open',
severity: Literal["warning", "critical", "minor"] = "warning",
status: Literal["resolved", "open"] = "open",
extra_attributes: dict[str, str] | None = None,
):
"""Send both Discord and AllQuiet alerts for system events."""
@@ -438,10 +443,18 @@ class NotificationManager(AppService):
# Send AllQuiet alert if correlation_id is provided
if correlation_id:
# Extract title from content (first line or first sentence)
lines = content.split('\n')
lines = content.split("\n")
title = lines[0] if lines else content[:100]
# Remove Discord formatting from title
title = title.replace('**', '').replace('🚨', '').replace('⚠️', '').replace('', '').replace('', '').replace('📊', '').strip()
title = (
title.replace("**", "")
.replace("🚨", "")
.replace("⚠️", "")
.replace("", "")
.replace("", "")
.replace("📊", "")
.strip()
)
alert = AllQuietAlert(
severity=severity,

View File

@@ -48,13 +48,16 @@ def sentry_capture_error(error: BaseException):
class AllQuietAlert(BaseModel):
severity: Literal['warning'] | Literal['critical'] | Literal['minor']
status: Literal['resolved'] | Literal['open']
severity: Literal["warning"] | Literal["critical"] | Literal["minor"]
status: Literal["resolved"] | Literal["open"]
title: str | None = None
description: str | None = None
correlation_id: str | None = None
extra_attributes: dict[str, str] = Field(default_factory=dict)
environment: str = f"app:{settings.config.app_env.value}-behave:{settings.config.behave_as.value}"
environment: str = (
f"app:{settings.config.app_env.value}-behave:{settings.config.behave_as.value}"
)
async def send_allquiet_alert(alert: AllQuietAlert):
hook_url = settings.secrets.allquiet_webhook_url
@@ -64,10 +67,12 @@ async def send_allquiet_alert(alert: AllQuietAlert):
return
import httpx
async with httpx.AsyncClient() as client:
response = await client.post(hook_url, json=alert.model_dump())
response.raise_for_status()
async def discord_send_alert(
content: str, channel: DiscordChannel = DiscordChannel.PLATFORM
):

View File

@@ -4,6 +4,7 @@ import os
import threading
import time
from functools import wraps
from typing import Literal
from uuid import uuid4
from tenacity import (
@@ -43,8 +44,14 @@ def should_send_alert(func_name: str, exception: Exception, context: str = "") -
def send_rate_limited_discord_alert(
func_name: str, exception: Exception, context: str, alert_msg: str, channel=None,
correlation_id: str | None = None, severity: str = 'critical', extra_attributes: dict | None = None
func_name: str,
exception: Exception,
context: str,
alert_msg: str,
channel=None,
correlation_id: str | None = None,
severity: Literal["warning", "critical", "minor"] = "critical",
extra_attributes: dict | None = None,
) -> bool:
"""
Send a Discord alert with rate limiting.
@@ -64,8 +71,8 @@ def send_rate_limited_discord_alert(
channel=channel or DiscordChannel.PLATFORM,
correlation_id=correlation_id,
severity=severity,
status='open',
extra_attributes=extra_attributes or {}
status="open",
extra_attributes=extra_attributes or {},
)
return True
@@ -83,7 +90,9 @@ def _send_critical_retry_alert(
error_type = type(exception).__name__
# Create correlation ID based on context, function name, and error type
correlation_id = f"retry_failure_{context}_{func_name}_{error_type}".replace(" ", "_").replace(":", "")
correlation_id = f"retry_failure_{context}_{func_name}_{error_type}".replace(
" ", "_"
).replace(":", "")
if send_rate_limited_discord_alert(
func_name,
@@ -94,14 +103,14 @@ def _send_critical_retry_alert(
f"Error: {error_type}: {exception}\n\n"
f"This operation is about to fail permanently. Investigate immediately.",
correlation_id=correlation_id,
severity='critical',
severity="critical",
extra_attributes={
'function_name': func_name,
'attempt_number': str(attempt_number),
'max_attempts': str(EXCESSIVE_RETRY_THRESHOLD),
'error_type': error_type,
'context': context or 'none',
}
"function_name": func_name,
"attempt_number": str(attempt_number),
"max_attempts": str(EXCESSIVE_RETRY_THRESHOLD),
"error_type": error_type,
"context": context or "none",
},
):
logger.critical(
f"CRITICAL ALERT SENT: Operation {func_name} at attempt {attempt_number}"
@@ -207,7 +216,11 @@ def conn_retry(
if attempt_number == EXCESSIVE_RETRY_THRESHOLD:
error_type = type(exception).__name__
# Create correlation ID for infrastructure issues
correlation_id = f"infrastructure_{resource_name}_{action_name}_{func_name}".replace(" ", "_")
correlation_id = (
f"infrastructure_{resource_name}_{action_name}_{func_name}".replace(
" ", "_"
)
)
if send_rate_limited_discord_alert(
func_name,
@@ -221,15 +234,15 @@ def conn_retry(
f"Error: {error_type}: {str(exception)[:200]}{'...' if len(str(exception)) > 200 else ''}\n\n"
f"Infrastructure component is approaching failure threshold. Investigate immediately.",
correlation_id=correlation_id,
severity='critical',
severity="critical",
extra_attributes={
'resource_name': resource_name,
'action_name': action_name,
'function_name': func_name,
'attempt_number': str(attempt_number),
'max_attempts': str(max_retry + 1),
'error_type': error_type,
}
"resource_name": resource_name,
"action_name": action_name,
"function_name": func_name,
"attempt_number": str(attempt_number),
"max_attempts": str(max_retry + 1),
"error_type": error_type,
},
):
logger.critical(
f"INFRASTRUCTURE ALERT SENT: {resource_name} at {attempt_number} attempts"