Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(memory): resolve shared-agent memory in the caller's workspace (#4663)
cainbit139b4b481b9b
diff --git a/internal/application/service/memory/scope.go b/internal/application/service/memory/scope.goindex 3d655d7af..7e9785ced 100644--- a/internal/application/service/memory/scope.go+++ b/internal/application/service/memory/scope.go@@ -25,10 +25,20 @@ var ErrNoMemoryScope = errors.New("memory: no principal in context")// which covers web users, IM users, API external users and embed visitors// alike, and it is paired with the workspace so the same person's memories do// not leak between workspaces.+//+// The workspace is the caller's own, not the execution tenant: a shared agent+// runs under its owner's tenant so its models and knowledge resolve there, and+// WithExecutionTenant keeps Caller intact precisely so identity-scoped data+// (memory) still follows the person who is chatting. Reading TenantIDContextKey+// here would look up the owner's workspace and never find the caller's memories.func ResolveScope(ctx context.Context) (interfaces.MemoryScope, error) {- tenantID, ok := types.TenantIDFromContext(ctx)- if !ok || tenantID == 0 {- return interfaces.MemoryScope{}, ErrNoMemoryScope+ tenantID := types.CallerFromContext(ctx).TenantID+ if tenantID == 0 {+ var ok bool+ tenantID, ok = types.TenantIDFromContext(ctx)+ if !ok || tenantID == 0 {+ return interfaces.MemoryScope{}, ErrNoMemoryScope+ }}principal, ok := types.PrincipalFromContext(ctx)if !ok {diff --git a/internal/application/service/memory/scope_test.go b/internal/application/service/memory/scope_test.gonew file mode 100644index 000000000..1ecd15524--- /dev/null+++ b/internal/application/service/memory/scope_test.go@@ -0,0 +1,40 @@+package memory++import (+ "context"+ "testing"++ "github.com/Tencent/WeKnora/internal/types"+ "github.com/stretchr/testify/require"+)++// Memory follows the person who is chatting, not the tenant the turn executes+// in: a shared agent borrows its owner's workspace for models and knowledge,+// while the caller's memories stay in the caller's own workspace.+func TestResolveScopeUsesCallerWorkspaceAcrossExecutionTenant(t *testing.T) {+ ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(7))+ ctx = types.WithPrincipal(ctx, types.Principal{Type: types.PrincipalWebUser, ID: "alice"})+ ctx = types.WithExecutionTenant(ctx, 42)++ scope, err := ResolveScope(ctx)+ require.NoError(t, err)+ require.Equal(t, uint64(7), scope.TenantID)+ require.Equal(t, "web_user:alice", scope.SubjectID)+}++// Contexts that never carried a Caller (embed visitors, background jobs built+// from a bare tenant) keep resolving against the execution tenant.+func TestResolveScopeFallsBackToExecutionTenantWithoutCaller(t *testing.T) {+ ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(42))+ ctx = types.WithPrincipal(ctx, types.Principal{Type: types.PrincipalEmbedVisitor, ID: "visitor-1"})++ scope, err := ResolveScope(ctx)+ require.NoError(t, err)+ require.Equal(t, uint64(42), scope.TenantID)+ require.Equal(t, "embed_visitor:visitor-1", scope.SubjectID)+}++func TestResolveScopeRequiresTenantAndPrincipal(t *testing.T) {+ _, err := ResolveScope(context.Background())+ require.ErrorIs(t, err, ErrNoMemoryScope)+}