fix: add SSE heartbeat to prevent Railway proxy timeout

Railway kills idle connections after ~30s. During long LLM calls
between agent stages, the SSE stream goes silent and gets dropped.
Now sends heartbeat events every 15s to keep the connection alive.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
dtarkent2-sys 2026-02-20 03:25:53 +00:00
parent 1ded94388e
commit 777226722a
1 changed files with 5 additions and 1 deletions

6
app.py
View File

@ -285,7 +285,11 @@ async def stream_analysis(analysis_id: str):
async def event_generator():
while True:
event = await q.get()
try:
event = await asyncio.wait_for(q.get(), timeout=15)
except asyncio.TimeoutError:
yield {"event": "heartbeat", "data": ""}
continue
if event is None:
break
yield {"data": json.dumps(event)}