diff --git a/internal/application/service/session_knowledge_qa.go b/internal/application/service/session_knowledge_qa.go index 3b6c0d53..6b8c25c6 100644 --- a/internal/application/service/session_knowledge_qa.go +++ b/internal/application/service/session_knowledge_qa.go @@ -590,6 +590,24 @@ func (s *sessionService) KnowledgeQAByEvent(ctx context.Context, }, nil, spanErr) } + // If the user stopped generation, the context is cancelled. A cancelled + // retrieval stage surfaces as ErrSearchNothing (the search goroutines + // return no results when their embedding/vector calls are aborted), so + // this check MUST come before the ErrSearchNothing handling below. + // Otherwise we would persist the fixed fallback response ("Sorry, I am + // unable to answer this question.") over the intentionally-empty stopped + // message, and the user would see the fallback text after refreshing. + // This is not single-machine specific: the stop arrives via the shared + // StreamManager and cancels asyncCtx on whichever node is generating. + if ctxErr := ctx.Err(); ctxErr != nil { + common.PipelineWarn(ctx, "Pipeline", "stage_cancelled", map[string]interface{}{ + "event": string(eventType), + "duration_ms": stageDuration.Milliseconds(), + "reason": ctxErr.Error(), + }) + return ctxErr + } + if err == chatpipeline.ErrSearchNothing { common.PipelineWarn(ctx, "Pipeline", "stage_fallback", map[string]interface{}{ "event": string(eventType), diff --git a/internal/handler/session/qa.go b/internal/handler/session/qa.go index 7caf2ff2..a8dd9b22 100644 --- a/internal/handler/session/qa.go +++ b/internal/handler/session/qa.go @@ -764,16 +764,24 @@ func (h *Handler) executeQA(reqCtx *qaRequestContext, mode qaMode, generateTitle } if serviceErr != nil { - logger.ErrorWithFields(streamCtx.asyncCtx, serviceErr, nil) - streamCtx.eventBus.Emit(streamCtx.asyncCtx, event.Event{ - Type: event.EventError, - SessionID: sessionID, - Data: event.ErrorData{ - Error: serviceErr.Error(), - Stage: stageName, + // A user-requested stop cancels asyncCtx, which surfaces here as a + // context cancellation. That is an expected outcome, not a failure: + // the stop event already notifies the client, so don't emit a + // spurious error event (which would otherwise show an error toast). + if streamCtx.asyncCtx.Err() != nil { + logger.Infof(streamCtx.asyncCtx, "QA cancelled by user stop for session: %s", sessionID) + } else { + logger.ErrorWithFields(streamCtx.asyncCtx, serviceErr, nil) + streamCtx.eventBus.Emit(streamCtx.asyncCtx, event.Event{ + Type: event.EventError, SessionID: sessionID, - }, - }) + Data: event.ErrorData{ + Error: serviceErr.Error(), + Stage: stageName, + SessionID: sessionID, + }, + }) + } } }()