Skip to content

Commit a8d4b7e

Browse files
fix(agentex): don't close a live task's stream when its idle key expires
Address review: the fallback vanished-topic check closed the SSE stream for a still-running task whose sliding-TTL stream key was reclaimed after an idle period, even though a later XADD would recreate the key. Only treat a vanished topic as terminal when the task's status could not be confirmed (lookup failed / hard delete); a confirmed non-terminal task stays live. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 9ec9442 commit a8d4b7e

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

agentex/src/domain/use_cases/streams_use_case.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,27 +247,31 @@ async def _stream_is_finished(
247247
stream_ever_existed: bool,
248248
) -> bool:
249249
"""
250-
Fallback terminal check. True if the task is terminal, or the topic
251-
was reclaimed after having existed (covers hard-deleted tasks whose
252-
lookup fails). A not-yet-written topic is not treated as finished. A
253-
transient lookup failure keeps the stream open.
250+
Fallback terminal check. True if the task is terminal, or the task's
251+
status could not be confirmed AND its topic was reclaimed after having
252+
existed (covers a hard-deleted task or a failing lookup). A confirmed
253+
non-terminal task is treated as live even if its idle key was
254+
reclaimed, and a not-yet-written topic is not treated as finished.
254255
"""
255256
try:
256257
task = await self.task_service.get_task(id=task_id)
257258
except Exception as e:
258259
logger.warning(
259260
f"Terminal-state lookup failed for task {task_id}; "
260-
f"keeping stream open: {e}"
261+
f"treating status as unknown: {e}"
261262
)
262263
task = None
263264
# Normal finish: the DB says the task is done, so no more XADDs.
264265
if task is not None and task.status in _TERMINAL_TASK_STATUSES:
265266
return True
266-
# Vanished topic: the key existed before but its sliding TTL reclaimed
267-
# it, so a reader is now blocking on a key that can never return data
268-
# (also covers a hard-deleted task whose status lookup failed above).
269-
if stream_ever_existed and not await self.stream_repository.stream_exists(
270-
stream_topic
267+
# Vanished topic, but only when the status is unknown (lookup failed /
268+
# hard delete). A confirmed non-terminal task is still live even if its
269+
# idle key was reclaimed by the sliding TTL — a later XADD recreates the
270+
# key, so we must not close its stream on the expired key alone.
271+
if (
272+
task is None
273+
and stream_ever_existed
274+
and not await self.stream_repository.stream_exists(stream_topic)
271275
):
272276
return True
273277
return False

0 commit comments

Comments
 (0)