fix(im): remove pipeline-level timeout that kills multi-round agent reasoning
The IM service applied a hard 120s deadline (`qaTimeout`) to the entire QA pipeline context. Multi-round ReAct agents easily exceed this — when the deadline fires mid-stream the LLM provider returns "context deadline exceeded" which leaks into the user-visible answer (fixes #1000). Changes: - Replace `context.WithTimeout(ctx, qaTimeout)` with `context.WithCancel(ctx)` in both `handleMessageStream` and `runQA`, matching the web handler's approach. Each agent round still has its own per-call LLMCallTimeout (default 120s) which is sufficient. - Filter `ResponseTypeError` stream chunks in `streamLLMToEventBus` so error messages (e.g. "context deadline exceeded") are never appended to the LLM content. Instead they are captured in `StreamError` and surfaced as a Go error when no usable content was produced. - Bump Redis inflight mapping TTL from qaTimeout+30s to 10 minutes to accommodate longer agent runs. - Remove the now-unused `qaTimeout` constant.
This commit is contained in:
@@ -19,6 +19,7 @@ type streamLLMResult struct {
|
||||
ToolCalls []types.LLMToolCall
|
||||
Usage *types.TokenUsage
|
||||
FinishReason string // actual finish_reason from LLM (captured from last stream chunk)
|
||||
StreamError string // error message from stream (e.g., timeout), kept separate from Content
|
||||
}
|
||||
|
||||
// streamLLMToEventBus streams LLM response through EventBus (generic method)
|
||||
@@ -52,6 +53,14 @@ func (e *AgentEngine) streamLLMToEventBus(
|
||||
}
|
||||
responseTypeCounts[string(chunk.ResponseType)]++
|
||||
|
||||
// Capture error messages from the stream (e.g., "context deadline exceeded")
|
||||
// but do NOT append them to result.Content — they would leak to the user
|
||||
// as if they were part of the LLM answer.
|
||||
if chunk.ResponseType == types.ResponseTypeError {
|
||||
result.StreamError = chunk.Content
|
||||
continue
|
||||
}
|
||||
|
||||
if chunk.Content != "" {
|
||||
isExtracted := chunk.Data != nil && chunk.Data["source"] != nil
|
||||
if !isExtracted {
|
||||
@@ -86,6 +95,12 @@ func (e *AgentEngine) streamLLMToEventBus(
|
||||
chunkCount, len(result.Content), len(result.ToolCalls),
|
||||
streamDuration.Milliseconds(), responseTypeCounts)
|
||||
|
||||
// If the stream produced an error and no usable content/tool calls,
|
||||
// surface it as a Go error so the caller can retry or degrade gracefully.
|
||||
if result.StreamError != "" && result.Content == "" && len(result.ToolCalls) == 0 {
|
||||
return result, fmt.Errorf("LLM stream error: %s", result.StreamError)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
|
||||
+11
-10
@@ -29,8 +29,6 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// qaTimeout is the maximum time to wait for the QA pipeline to complete.
|
||||
qaTimeout = 120 * time.Second
|
||||
// dedupTTL is how long processed message IDs are retained.
|
||||
dedupTTL = 5 * time.Minute
|
||||
// dedupCleanupInterval is how often the dedup map is cleaned.
|
||||
@@ -647,7 +645,7 @@ func (s *Service) storeInflightMapping(ctx context.Context, userKey, sessionID,
|
||||
return
|
||||
}
|
||||
val := sessionID + ":" + messageID
|
||||
if err := s.redis.Set(ctx, RedisKeyInflight+userKey, val, qaTimeout+30*time.Second).Err(); err != nil {
|
||||
if err := s.redis.Set(ctx, RedisKeyInflight+userKey, val, 10*time.Minute).Err(); err != nil {
|
||||
logger.Warnf(ctx, "[IM] Failed to store inflight mapping: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -1473,7 +1471,9 @@ func (s *Service) handleMessageStream(ctx context.Context, msg *IncomingMessage,
|
||||
}
|
||||
|
||||
// Prepare the QA pipeline
|
||||
qaCtx, qaCancel := context.WithTimeout(ctx, qaTimeout)
|
||||
// No total deadline: each agent round has its own LLMCallTimeout (default 120s).
|
||||
// A hard pipeline deadline would kill multi-round agent reasoning prematurely.
|
||||
qaCtx, qaCancel := context.WithCancel(ctx)
|
||||
defer qaCancel()
|
||||
|
||||
eventBus := event.NewEventBus()
|
||||
@@ -1770,8 +1770,9 @@ func (s *Service) fallbackNonStream(ctx context.Context, msg *IncomingMessage, s
|
||||
|
||||
// runQA executes the WeKnora QA pipeline and returns the full answer text.
|
||||
func (s *Service) runQA(ctx context.Context, session *types.Session, query string, customAgent *types.CustomAgent, kbIDs []string, userKey string, quote *QuotedMessage) (string, error) {
|
||||
// Add timeout to prevent indefinite blocking
|
||||
ctx, cancel := context.WithTimeout(ctx, qaTimeout)
|
||||
// Cancellable context (no hard deadline): each agent round has its own
|
||||
// LLMCallTimeout. The context can still be cancelled by /stop.
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
eventBus := event.NewEventBus()
|
||||
@@ -1877,18 +1878,18 @@ func (s *Service) runQA(ctx context.Context, session *types.Session, query strin
|
||||
}
|
||||
}()
|
||||
|
||||
// Wait for completion or timeout
|
||||
// Wait for completion or cancellation (e.g., /stop)
|
||||
select {
|
||||
case <-done:
|
||||
case <-ctx.Done():
|
||||
// Mark assistant message as completed to avoid dangling incomplete records
|
||||
assistantMsg.Content = "抱歉,回答超时,请稍后再试。"
|
||||
assistantMsg.Content = "抱歉,回答已被取消。"
|
||||
assistantMsg.IsCompleted = true
|
||||
// Use a fresh context since the original is cancelled
|
||||
if updateErr := s.messageService.UpdateMessage(context.WithoutCancel(ctx), assistantMsg); updateErr != nil {
|
||||
logger.Warnf(ctx, "[IM] Failed to update timed-out assistant message: %v", updateErr)
|
||||
logger.Warnf(ctx, "[IM] Failed to update cancelled assistant message: %v", updateErr)
|
||||
}
|
||||
return "", fmt.Errorf("QA timed out after %v", qaTimeout)
|
||||
return "", fmt.Errorf("QA cancelled: %w", ctx.Err())
|
||||
}
|
||||
|
||||
answerMu.Lock()
|
||||
|
||||
Reference in New Issue
Block a user