From 09b9c98d545cbdef978068c3ec298313243a07a7 Mon Sep 17 00:00:00 2001 From: lyingbug Date: Mon, 19 Oct 2026 22:14:09 +0800 Subject: [PATCH] fix: preserve business session identity in model requests (#4491) * fix: preserve business session identity in model requests * fix: propagate session identity to auxiliary model calls --- internal/application/service/session.go | 1 + .../service/session_knowledge_qa.go | 1 + .../service/session_model_identity_test.go | 88 ++++++++++++++++++ .../handler/session/model_identity_test.go | 28 ++++++ internal/handler/session/qa.go | 3 + internal/models/api/endpoint.go | 12 ++- internal/models/api/endpoint_test.go | 31 +++++++ .../openaicompletions/session_headers_test.go | 92 +++++++++++++++++++ internal/models/api/prompt_cache.go | 10 +- internal/models/api/prompt_cache_test.go | 10 ++ internal/models/api/retry_test.go | 12 ++- 11 files changed, 283 insertions(+), 5 deletions(-) create mode 100644 internal/application/service/session_model_identity_test.go create mode 100644 internal/handler/session/model_identity_test.go create mode 100644 internal/models/api/openaicompletions/session_headers_test.go diff --git a/internal/application/service/session.go b/internal/application/service/session.go index 896857f9..4455872f 100644 --- a/internal/application/service/session.go +++ b/internal/application/service/session.go @@ -864,6 +864,7 @@ func (s *sessionService) GenerateTitle(ctx context.Context, logger.Error(ctx, "Failed to generate title: session cannot be empty") return "", stderrors.New("session cannot be empty") } + ctx = types.WithSessionID(ctx, session.ID) // Skip if title already exists if session.Title != "" { diff --git a/internal/application/service/session_knowledge_qa.go b/internal/application/service/session_knowledge_qa.go index b94307f2..d2cc7a4c 100644 --- a/internal/application/service/session_knowledge_qa.go +++ b/internal/application/service/session_knowledge_qa.go @@ -692,6 +692,7 @@ func intersectStrings(left []string, right []string) []string { func (s *sessionService) KnowledgeQAByEvent(ctx context.Context, chatManage *types.ChatManage, eventList []types.EventType, ) error { + ctx = types.WithSessionID(ctx, chatManage.SessionID) logger.Info(ctx, "Start processing knowledge base question answering through events") logger.Infof(ctx, "Knowledge base question answering parameters, session ID: %s, query: %s", chatManage.SessionID, chatManage.Query) diff --git a/internal/application/service/session_model_identity_test.go b/internal/application/service/session_model_identity_test.go new file mode 100644 index 00000000..017bce17 --- /dev/null +++ b/internal/application/service/session_model_identity_test.go @@ -0,0 +1,88 @@ +package service + +import ( + "context" + "errors" + "testing" + "time" + + chatpipeline "github.com/Tencent/WeKnora/internal/application/service/chat_pipeline" + "github.com/Tencent/WeKnora/internal/config" + "github.com/Tencent/WeKnora/internal/models/chat" + "github.com/Tencent/WeKnora/internal/types" + "github.com/stretchr/testify/require" +) + +type identityChat = chat.Chat + +type sessionIdentityModel struct { + identityChat + seen chan string + err error +} + +func (m *sessionIdentityModel) Chat( + ctx context.Context, _ []chat.Message, _ *chat.ChatOptions, +) (*types.ChatResponse, error) { + id, _ := types.SessionIDFromContext(ctx) + m.seen <- id + return nil, m.err +} + +func TestGenerateTitlePropagatesBusinessSession(t *testing.T) { + for _, async := range []bool{false, true} { + name := "sync" + if async { + name = "async" + } + t.Run(name, func(t *testing.T) { + model := &sessionIdentityModel{seen: make(chan string, 1), err: errors.New("stop after model call")} + svc := &sessionService{ + cfg: &config.Config{Conversation: &config.ConversationConfig{}}, + modelService: &stubModelService{chatModel: model}, + } + session := &types.Session{ID: "title-session"} + ctx := types.WithSessionID(context.Background(), "parent-session") + if async { + svc.GenerateTitleAsync(ctx, session, "hello", "model", nil) + } else { + _, err := svc.GenerateTitle(ctx, session, []types.Message{{Role: "user", Content: "hello"}}, "model") + require.ErrorIs(t, err, model.err) + } + select { + case id := <-model.seen: + require.Equal(t, session.ID, id) + case <-time.After(5 * time.Second): + t.Fatal("title generation did not reach the model") + } + }) + } +} + +type sessionIdentityPlugin struct{ seen []string } + +func (p *sessionIdentityPlugin) ActivationEvents() []types.EventType { + return []types.EventType{types.CHAT_COMPLETION} +} + +func (p *sessionIdentityPlugin) OnEvent( + ctx context.Context, _ types.EventType, _ *types.ChatManage, next func() *chatpipeline.PluginError, +) *chatpipeline.PluginError { + id, _ := types.SessionIDFromContext(ctx) + p.seen = append(p.seen, id) + return next() +} + +func TestKnowledgeQAByEventPropagatesBusinessSession(t *testing.T) { + plugin := &sessionIdentityPlugin{} + events := chatpipeline.NewEventManager() + events.Register(plugin) + svc := &sessionService{eventManager: events} + ctx := context.Background() + for _, id := range []string{"session-a", "session-a", "session-b", ""} { + cm := &types.ChatManage{} + cm.SessionID = id + require.NoError(t, svc.KnowledgeQAByEvent(ctx, cm, plugin.ActivationEvents())) + } + require.Equal(t, []string{"session-a", "session-a", "session-b", ""}, plugin.seen) +} diff --git a/internal/handler/session/model_identity_test.go b/internal/handler/session/model_identity_test.go new file mode 100644 index 00000000..934e2ad2 --- /dev/null +++ b/internal/handler/session/model_identity_test.go @@ -0,0 +1,28 @@ +package session + +import ( + "testing" + + "github.com/Tencent/WeKnora/internal/stream" + "github.com/Tencent/WeKnora/internal/types" + "github.com/stretchr/testify/require" +) + +func TestSetupSSEStreamPropagatesBusinessSession(t *testing.T) { + for _, mode := range []qaMode{qaModeNormal, qaModeAgent} { + h := &Handler{streamManager: stream.NewMemoryStreamManager()} + reqCtx := &qaRequestContext{ + c: newSteerWiringGinContext(), + ctx: types.WithSessionID(t.Context(), "parent-session"), + sessionID: "business-session", + session: &types.Session{ID: "business-session"}, + assistantMessage: &types.Message{ID: "assistant", SessionID: "business-session"}, + } + streamCtx := h.setupSSEStream(reqCtx, false, mode) + require.NotNil(t, streamCtx) + id, ok := types.SessionIDFromContext(streamCtx.asyncCtx) + require.True(t, ok) + require.Equal(t, reqCtx.session.ID, id) + streamCtx.cancel() + } +} diff --git a/internal/handler/session/qa.go b/internal/handler/session/qa.go index 6b51aac8..32724c5f 100644 --- a/internal/handler/session/qa.go +++ b/internal/handler/session/qa.go @@ -768,6 +768,9 @@ func (h *Handler) setupSSEStream(reqCtx *qaRequestContext, generateTitle bool, m // borrowed tenant above drives everything else, because DeleteSession tears // that sandbox down from a request that only knows the session's tenant. baseCtx = types.WithSandboxTenantID(baseCtx, reqCtx.session.TenantID) + // Attachment analysis and title generation run before the QA service. + // Give all asynchronous work the same business conversation identity. + baseCtx = types.WithSessionID(baseCtx, reqCtx.session.ID) // An agent that opted out of long-term memory has to be opted out of the // write path too, not just recall. The two run from different contexts: diff --git a/internal/models/api/endpoint.go b/internal/models/api/endpoint.go index 14e119ed..d69f497e 100644 --- a/internal/models/api/endpoint.go +++ b/internal/models/api/endpoint.go @@ -13,6 +13,7 @@ import ( "time" "github.com/Tencent/WeKnora/internal/logger" + "github.com/Tencent/WeKnora/internal/types" secutils "github.com/Tencent/WeKnora/internal/utils" ) @@ -126,9 +127,16 @@ func (e Endpoint) newPost(ctx context.Context, url string, data []byte, contentT if e.Auth != nil { e.Auth(req, data) } - // User headers are applied last but the helper skips reserved names so - // they cannot clobber auth or content negotiation. + // The helper skips reserved names so custom headers cannot clobber auth + // or content negotiation. secutils.ApplyCustomHeaders(req, e.Headers) + // Identity belongs to this call, not the shared model configuration or + // a per-request UUID. Derived contexts retain it across turns and retries. + req.Header.Set("User-Agent", "WeKnora") + req.Header.Del("X-Conversation-ID") + if sessionID, ok := types.SessionIDFromContext(ctx); ok { + req.Header.Set("X-Conversation-ID", sessionID) + } return req, nil } diff --git a/internal/models/api/endpoint_test.go b/internal/models/api/endpoint_test.go index b59a63ca..ea73710b 100644 --- a/internal/models/api/endpoint_test.go +++ b/internal/models/api/endpoint_test.go @@ -8,9 +8,40 @@ import ( "testing" "time" + "github.com/Tencent/WeKnora/internal/types" "github.com/stretchr/testify/require" ) +func TestEndpointRequestIdentity(t *testing.T) { + t.Setenv("SSRF_WHITELIST", "127.0.0.1") + // One endpoint is shared by different sessions. Static model headers must + // neither merge them nor leak one session into a call without a session. + ep := Endpoint{ + Auth: BearerAuth("test-key"), + Headers: map[string]string{ + "X-Conversation-ID": "static-id", "User-Agent": "custom-client", + "X-Custom": "preserved", + }, + } + for _, sessionID := range []string{"session-a", "session-b", ""} { + t.Run("session="+sessionID, func(t *testing.T) { + ctx := types.WithSessionID(context.Background(), sessionID) + for _, stream := range []bool{false, true} { + req, _, err := ep.NewRequest(ctx, "http://127.0.0.1/v1/chat/completions", map[string]any{}, stream) + require.NoError(t, err) + require.Equal(t, sessionID, req.Header.Get("X-Conversation-ID")) + require.Equal(t, "WeKnora", req.UserAgent()) + require.Equal(t, "preserved", req.Header.Get("X-Custom")) + require.Equal(t, "Bearer test-key", req.Header.Get("Authorization")) + if sessionID == "" { + require.NotContains(t, req.Header, "X-Conversation-Id") + } + } + require.Equal(t, "static-id", ep.Headers["X-Conversation-ID"]) + }) + } +} + func TestDoKeepsHeadersOnHTTPError(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Retry-After", "12") diff --git a/internal/models/api/openaicompletions/session_headers_test.go b/internal/models/api/openaicompletions/session_headers_test.go new file mode 100644 index 00000000..a55640ca --- /dev/null +++ b/internal/models/api/openaicompletions/session_headers_test.go @@ -0,0 +1,92 @@ +package openaicompletions + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/Tencent/WeKnora/internal/models/api" + "github.com/Tencent/WeKnora/internal/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestConversationHeadersAcrossTurnsAndToolResults(t *testing.T) { + t.Setenv("SSRF_WHITELIST", "127.0.0.1") + for _, cacheKeyEnabled := range []bool{false, true} { + name := "without-cache-key" + if cacheKeyEnabled { + name = "with-cache-key" + } + t.Run(name, func(t *testing.T) { + var headers []http.Header + var requests []map[string]any + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + headers = append(headers, r.Header.Clone()) + var body map[string]any + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + t.Error(err) + w.WriteHeader(http.StatusBadRequest) + return + } + requests = append(requests, body) + if body["stream"] == true { + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: " + + `{"choices":[{"index":0,"delta":{"content":"ok"},"finish_reason":"stop"}]}` + + "\n\ndata: [DONE]\n\n")) + return + } + _, _ = w.Write([]byte( + `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}]}`, + )) + })) + defer server.Close() + settings := api.DefaultOpenAICompletions() + settings.PromptCacheKey = cacheKeyEnabled + client := New(Config{Endpoint: api.Endpoint{BaseURL: server.URL, Model: "m"}, Settings: settings}) + opts := &api.Options{PromptCacheKey: "shared-cache-prefix"} + messages := []api.Message{{Role: "user", Content: "First turn"}} + ctx := types.WithSessionID(context.Background(), "business-session") + _, err := client.Chat(ctx, messages, opts) + require.NoError(t, err) + + messages = append(messages, + api.Message{Role: "assistant", Content: "ok"}, + api.Message{Role: "user", Content: "Second turn"}, + ) + // A new HTTP request context still carries the same business session. + ctx = types.WithSessionID(context.Background(), "business-session") + stream, err := client.ChatStream(ctx, messages, opts) + require.NoError(t, err) + var streamedContent string + for chunk := range stream { + streamedContent += chunk.Content + } + assert.Equal(t, "ok", streamedContent) + + messages = append(messages, + api.Message{Role: "assistant", ToolCalls: []api.ToolCall{{ + ID: "call-1", Type: "function", Function: api.FunctionCall{Name: "search", Arguments: `{}`}, + }}}, + api.Message{Role: "tool", ToolCallID: "call-1", Content: "Search result"}, + ) + _, err = client.Chat(ctx, messages, opts) + require.NoError(t, err) + require.Len(t, headers, 3) + for i, h := range headers { + assert.Equal(t, "business-session", h.Get("X-Conversation-ID")) + assert.Equal(t, "WeKnora", h.Get("User-Agent")) + if cacheKeyEnabled { + assert.Equal(t, "business-session", h.Get("x-session-affinity")) + assert.Equal(t, "shared-cache-prefix", requests[i]["prompt_cache_key"]) + } + } + lastMessages := requests[2]["messages"].([]any) + assert.Equal(t, "tool", lastMessages[len(lastMessages)-1].(map[string]any)["role"]) + assert.Equal(t, "shared-cache-prefix", opts.PromptCacheKey) + }) + } +} diff --git a/internal/models/api/prompt_cache.go b/internal/models/api/prompt_cache.go index cc91260c..7510fb00 100644 --- a/internal/models/api/prompt_cache.go +++ b/internal/models/api/prompt_cache.go @@ -194,7 +194,15 @@ func addCacheControlToMessageContent(msg map[string]any, marker *CacheControlMar // AttachSessionAffinityHeaders sets the sticky-routing headers OpenAI-style // gateways use to keep one conversation on one cache shard. func AttachSessionAffinityHeaders(req *http.Request, sessionID string) { - if req == nil || sessionID == "" { + if req == nil { + return + } + // Gateways may prefer these headers over X-Conversation-ID. Keep them + // on the business session even when the caller uses a different cache key. + if businessSessionID, ok := types.SessionIDFromContext(req.Context()); ok { + sessionID = businessSessionID + } + if sessionID == "" { return } req.Header.Set("session_id", sessionID) diff --git a/internal/models/api/prompt_cache_test.go b/internal/models/api/prompt_cache_test.go index 36d59a66..faab499f 100644 --- a/internal/models/api/prompt_cache_test.go +++ b/internal/models/api/prompt_cache_test.go @@ -53,6 +53,16 @@ func TestAttachSessionAffinityHeaders(t *testing.T) { assert.Equal(t, "sess-1", req.Header.Get("x-session-affinity")) } +func TestSessionAffinityPrefersBusinessSessionOverCacheKey(t *testing.T) { + ctx := types.WithSessionID(context.Background(), "business-session") + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://example.com", nil) + require.NoError(t, err) + AttachSessionAffinityHeaders(req, "shared-cache-prefix") + for _, header := range []string{"session_id", "x-session-affinity", "x-client-request-id"} { + assert.Equal(t, "business-session", req.Header.Get(header)) + } +} + // parseUsage decodes a response envelope the way the Chat Completions client // does, so these cases exercise the same path production takes. func parseUsage(t *testing.T, body string) (OpenAIUsage, bool) { diff --git a/internal/models/api/retry_test.go b/internal/models/api/retry_test.go index b056a4da..4023e650 100644 --- a/internal/models/api/retry_test.go +++ b/internal/models/api/retry_test.go @@ -7,6 +7,7 @@ import ( "testing" "time" + "github.com/Tencent/WeKnora/internal/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -34,7 +35,9 @@ func TestPostJSONWithRetryAlwaysReportsAnUnreachableUpstream(t *testing.T) { func TestPostJSONWithRetrySucceedsAfterATransportFailure(t *testing.T) { t.Setenv("SSRF_WHITELIST", "127.0.0.1") attempts := 0 - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + var headers []http.Header + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + headers = append(headers, r.Header.Clone()) attempts++ if attempts == 1 { // Hang up mid-request so the client sees a transport error. @@ -51,11 +54,16 @@ func TestPostJSONWithRetrySucceedsAfterATransportFailure(t *testing.T) { var out map[string]any err := Endpoint{}.PostJSONWithRetry( - context.Background(), server.URL, map[string]any{}, &out, fastPolicy(), "test", + types.WithSessionID(context.Background(), "retry-session"), + server.URL, map[string]any{}, &out, fastPolicy(), "test", ) require.NoError(t, err) assert.Equal(t, true, out["ok"]) assert.Equal(t, 2, attempts) + for _, h := range headers { + assert.Equal(t, "retry-session", h.Get("X-Conversation-ID")) + assert.Equal(t, "WeKnora", h.Get("User-Agent")) + } } // A non-2xx reply is the vendor answering. Repeating a rejected request costs