From b1b7bfbe69a548f5fa74c903f32290d83f7be6cb Mon Sep 17 00:00:00 2001 From: viettranx Date: Thu, 19 Mar 2026 19:01:19 +0700 Subject: [PATCH] fix(ws): detach agent runs from HTTP request context Agent runs were tied to the HTTP request context, so navigating away cancelled the LLM stream. Now uses context.WithoutCancel() to survive page navigation while preserving explicit chat.abort support. --- internal/gateway/methods/chat.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/gateway/methods/chat.go b/internal/gateway/methods/chat.go index 6cda9c1d..7c0bab22 100644 --- a/internal/gateway/methods/chat.go +++ b/internal/gateway/methods/chat.go @@ -122,8 +122,11 @@ func (m *ChatMethods) handleSend(ctx context.Context, client *gateway.Client, re sessionKey = sessions.SessionKey(params.AgentID, "ws-"+client.ID()) } - // Inject user_id into context for downstream stores/tools - runCtxBase := ctx + // Detach from HTTP request context so agent runs survive page navigation/reconnect. + // WithoutCancel preserves all context values (locale, user ID, etc.) + // but HTTP request cancellation no longer propagates. + // Explicit abort via chat.abort still works through the per-run cancel(). + runCtxBase := context.WithoutCancel(ctx) if userID != "" { runCtxBase = store.WithUserID(runCtxBase, userID) }