fix(agents): bound AI alert card DB readback
Some checks failed
CD Pipeline / workflow-shape (push) Successful in 0s
CD Pipeline / cancel-stale-cd (push) Has been skipped
CD Pipeline / build-and-deploy (push) Has been cancelled
CD Pipeline / post-deploy-checks (push) Has been cancelled
CD Pipeline / tests (push) Has been cancelled

This commit is contained in:
ogt
2026-07-09 22:47:29 +08:00
parent f125589f7d
commit abd7ec0815
2 changed files with 93 additions and 3 deletions

View File

@@ -99,6 +99,18 @@ _CALLBACK_REPLY_CACHE_TTL_SECONDS = int(
_AI_ALERT_CARD_CACHE_TTL_SECONDS = int(
os.getenv("AWOOOP_AI_ALERT_CARD_CACHE_TTL_SECONDS", "20")
)
_AI_ALERT_CARD_DB_CONTEXT_TIMEOUT_SECONDS = float(
os.getenv("AWOOOP_AI_ALERT_CARD_DB_CONTEXT_TIMEOUT_SECONDS", "0.25")
)
_AI_ALERT_CARD_DB_CONTEXT_EXIT_TIMEOUT_SECONDS = float(
os.getenv("AWOOOP_AI_ALERT_CARD_DB_CONTEXT_EXIT_TIMEOUT_SECONDS", "0.20")
)
_AI_ALERT_CARD_QUERY_TIMEOUT_SECONDS = float(
os.getenv("AWOOOP_AI_ALERT_CARD_QUERY_TIMEOUT_SECONDS", "0.25")
)
_AI_ALERT_CARD_STATEMENT_TIMEOUT_MS = int(
os.getenv("AWOOOP_AI_ALERT_CARD_STATEMENT_TIMEOUT_MS", "200")
)
_AI_ALERT_CARD_LEARNING_REGISTRY_TARGETS: tuple[dict[str, str], ...] = (
{
"target": "km",
@@ -1476,11 +1488,44 @@ async def list_ai_alert_card_delivery_readback(
""")
try:
async with get_db_context(normalized_project_id) as db:
summary_result = await db.execute(summary_sql, params)
db_context = get_db_context(normalized_project_id)
db = await asyncio.wait_for(
db_context.__aenter__(),
timeout=_AI_ALERT_CARD_DB_CONTEXT_TIMEOUT_SECONDS,
)
try:
await asyncio.wait_for(
db.execute(
text(
"SET LOCAL statement_timeout = "
f"'{int(_AI_ALERT_CARD_STATEMENT_TIMEOUT_MS)}ms'"
),
{},
),
timeout=_AI_ALERT_CARD_QUERY_TIMEOUT_SECONDS,
)
summary_result = await asyncio.wait_for(
db.execute(summary_sql, params),
timeout=_AI_ALERT_CARD_QUERY_TIMEOUT_SECONDS,
)
summary_row = summary_result.mappings().first() or {}
rows_result = await db.execute(list_sql, params)
rows_result = await asyncio.wait_for(
db.execute(list_sql, params),
timeout=_AI_ALERT_CARD_QUERY_TIMEOUT_SECONDS,
)
rows = list(rows_result.mappings().all())
finally:
try:
await asyncio.wait_for(
db_context.__aexit__(None, None, None),
timeout=_AI_ALERT_CARD_DB_CONTEXT_EXIT_TIMEOUT_SECONDS,
)
except Exception as exit_exc: # pragma: no cover - pool pressure
logger.warning(
"operator_ai_alert_card_delivery_readback_context_exit_unavailable",
project_id=normalized_project_id,
error_type=type(exit_exc).__name__,
)
except Exception as exc: # pragma: no cover - production DB drift/pressure
logger.warning(
"operator_ai_alert_card_delivery_readback_source_unavailable",