fix(operator): bound AI alert card readback scans
This commit is contained in:
@@ -1515,7 +1515,29 @@ async def list_ai_alert_card_delivery_readback(
|
||||
FROM awooop_outbound_message m
|
||||
WHERE {where_sql}
|
||||
""")
|
||||
# Bound the page and the Agent99 subset before the lateral lookup. Without
|
||||
# both materialized sets PostgreSQL can rescan the full run ledger per card.
|
||||
list_sql = text(f"""
|
||||
WITH candidate_messages AS MATERIALIZED (
|
||||
SELECT m.*
|
||||
FROM awooop_outbound_message m
|
||||
WHERE {where_sql}
|
||||
ORDER BY COALESCE(m.sent_at, m.queued_at) DESC, m.message_id DESC
|
||||
LIMIT :limit OFFSET :offset
|
||||
),
|
||||
agent99_runs AS MATERIALIZED (
|
||||
SELECT
|
||||
ars.error_detail,
|
||||
ars.state,
|
||||
ars.trace_id,
|
||||
ars.run_id,
|
||||
ars.project_id,
|
||||
ars.trigger_ref,
|
||||
ars.created_at
|
||||
FROM awooop_run_state ars
|
||||
WHERE ars.project_id = :project_id
|
||||
AND ars.agent_id = 'agent99_controlled_dispatch'
|
||||
)
|
||||
SELECT
|
||||
m.message_id,
|
||||
m.project_id,
|
||||
@@ -1540,7 +1562,7 @@ async def list_ai_alert_card_delivery_readback(
|
||||
r.agent_id,
|
||||
r.state AS run_state,
|
||||
r.created_at AS run_created_at
|
||||
FROM awooop_outbound_message m
|
||||
FROM candidate_messages m
|
||||
LEFT JOIN awooop_run_state r
|
||||
ON r.project_id = m.project_id
|
||||
AND r.run_id = m.run_id
|
||||
@@ -1549,9 +1571,8 @@ async def list_ai_alert_card_delivery_readback(
|
||||
ars.error_detail,
|
||||
ars.state,
|
||||
ars.trace_id
|
||||
FROM awooop_run_state ars
|
||||
FROM agent99_runs ars
|
||||
WHERE ars.project_id = m.project_id
|
||||
AND ars.agent_id = 'agent99_controlled_dispatch'
|
||||
AND (
|
||||
ars.run_id::text = COALESCE(
|
||||
COALESCE(m.source_envelope::jsonb, '{{}}'::jsonb) #>>
|
||||
@@ -1585,9 +1606,7 @@ async def list_ai_alert_card_delivery_readback(
|
||||
) DESC, ars.created_at DESC
|
||||
LIMIT 1
|
||||
) ar ON TRUE
|
||||
WHERE {where_sql}
|
||||
ORDER BY COALESCE(m.sent_at, m.queued_at) DESC, m.message_id DESC
|
||||
LIMIT :limit OFFSET :offset
|
||||
""")
|
||||
|
||||
try:
|
||||
@@ -1888,8 +1907,32 @@ async def _load_ai_alert_card_delivery_readback_direct(
|
||||
""", *args),
|
||||
timeout=_AI_ALERT_CARD_DIRECT_QUERY_TIMEOUT_SECONDS,
|
||||
)
|
||||
# Mirror the pooled-session plan so the pressure fallback does
|
||||
# not reintroduce a per-card full run-ledger scan.
|
||||
list_rows = await asyncio.wait_for(
|
||||
conn.fetch(f"""
|
||||
WITH candidate_messages AS MATERIALIZED (
|
||||
SELECT m.*
|
||||
FROM awooop_outbound_message m
|
||||
WHERE {where_sql}
|
||||
ORDER BY
|
||||
COALESCE(m.sent_at, m.queued_at) DESC,
|
||||
m.message_id DESC
|
||||
LIMIT ${limit_param} OFFSET ${offset_param}
|
||||
),
|
||||
agent99_runs AS MATERIALIZED (
|
||||
SELECT
|
||||
ars.error_detail,
|
||||
ars.state,
|
||||
ars.trace_id,
|
||||
ars.run_id,
|
||||
ars.project_id,
|
||||
ars.trigger_ref,
|
||||
ars.created_at
|
||||
FROM awooop_run_state ars
|
||||
WHERE ars.project_id = $1
|
||||
AND ars.agent_id = 'agent99_controlled_dispatch'
|
||||
)
|
||||
SELECT
|
||||
m.message_id,
|
||||
m.project_id,
|
||||
@@ -1920,7 +1963,7 @@ async def _load_ai_alert_card_delivery_readback_direct(
|
||||
r.agent_id,
|
||||
r.state AS run_state,
|
||||
r.created_at AS run_created_at
|
||||
FROM awooop_outbound_message m
|
||||
FROM candidate_messages m
|
||||
LEFT JOIN awooop_run_state r
|
||||
ON r.project_id = m.project_id
|
||||
AND r.run_id = m.run_id
|
||||
@@ -1929,9 +1972,8 @@ async def _load_ai_alert_card_delivery_readback_direct(
|
||||
ars.error_detail,
|
||||
ars.state,
|
||||
ars.trace_id
|
||||
FROM awooop_run_state ars
|
||||
FROM agent99_runs ars
|
||||
WHERE ars.project_id = m.project_id
|
||||
AND ars.agent_id = 'agent99_controlled_dispatch'
|
||||
AND (
|
||||
ars.run_id::text = COALESCE(
|
||||
COALESCE(
|
||||
@@ -1971,9 +2013,7 @@ async def _load_ai_alert_card_delivery_readback_direct(
|
||||
) DESC, ars.created_at DESC
|
||||
LIMIT 1
|
||||
) ar ON TRUE
|
||||
WHERE {where_sql}
|
||||
ORDER BY COALESCE(m.sent_at, m.queued_at) DESC, m.message_id DESC
|
||||
LIMIT ${limit_param} OFFSET ${offset_param}
|
||||
""", *args, per_page, offset),
|
||||
timeout=_AI_ALERT_CARD_DIRECT_QUERY_TIMEOUT_SECONDS,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user