Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(agent-ingress): keepalive heartbeat on /listen SSE so it survives idle timeouts (#64616)

Ben White37a8942dc63f
diff --git a/products/agent_platform/services/agent-ingress/src/triggers/chat.ts b/products/agent_platform/services/agent-ingress/src/triggers/chat.ts
index 0c8d951bb524..acedd994310f 100644
--- a/products/agent_platform/services/agent-ingress/src/triggers/chat.ts
+++ b/products/agent_platform/services/agent-ingress/src/triggers/chat.ts
@@ -213,7 +213,17 @@ async function listenHandler(ctx: AuthedRouteCtx<z.infer<typeof ChatListenQueryS
const unsubscribe = deps.bus.subscribe(sessionId, (event) => {
res.write(`data: ${JSON.stringify(event)}\n\n`)
})
- req.on('close', () => unsubscribe())
+ // Keepalive: a comment frame every 20s keeps bytes flowing while the agent
+ // is mid-turn and emitting no events, so a proxy idle/response timeout
+ // (Envoy stream-idle, ALB) can't reset the stream and surface as a "network
+ // error" in the client. The client's SSE parser keeps only `data:` lines,
+ // so comment frames are discarded. unref() so it never blocks shutdown.
+ const heartbeat = setInterval(() => res.write(': keepalive\n\n'), 20_000)
+ heartbeat.unref()
+ req.on('close', () => {
+ clearInterval(heartbeat)
+ unsubscribe()
+ })
}
async function clientToolResultHandler(