Skip to content

Commit 571dfe8

Browse files
fix(agentex): recheck task status on keepalive to catch lost terminal events
Pure event-driven termination can't recover if a terminal transition's task_updated publish fails (the DB flips terminal but the event never lands, and the errors are logged-and-suppressed). On each keepalive interval, re-read the task status and end the stream if it is terminal — an authoritative backstop for a dropped event. Status-only, so it never closes a live task on a reclaimed key. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent a62cdeb commit 571dfe8

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

agentex/src/domain/use_cases/streams_use_case.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,17 @@ async def stream_task_events(
161161
# healthy again, so reset the backoff/error counter.
162162
consecutive_errors = 0
163163

164-
# Idle: send keepalive ping so proxies don't reap us.
164+
# Idle: recheck status (catches a lost terminal event), else ping.
165165
if message_count == 0:
166166
current_time = asyncio.get_running_loop().time()
167167
if current_time - last_message_time >= ping_interval:
168+
task = await self.task_service.get_task(id=task_id)
169+
if task.status in TERMINAL_TASK_STATUSES:
170+
logger.info(
171+
f"Ending SSE stream for task {task_id}: "
172+
"terminal on status recheck"
173+
)
174+
return
168175
yield ":ping\n\n"
169176
last_message_time = current_time
170177
await asyncio.sleep(0.1)

0 commit comments

Comments
 (0)