Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(im): remove pipeline-level timeout that kills multi-round agent reasoning

wizardchen80a007cc069f
diff --git a/internal/agent/think.go b/internal/agent/think.go
index 2633214c0..102adb2d1 100644
--- a/internal/agent/think.go
+++ b/internal/agent/think.go
@@ -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
}
diff --git a/internal/im/service.go b/internal/im/service.go
index c8a3aa245..0767da77c 100644
--- a/internal/im/service.go
+++ b/internal/im/service.go
@@ -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()