package memory import ( "context" "testing" "github.com/Tencent/WeKnora/internal/logger" "github.com/Tencent/WeKnora/internal/types" "github.com/stretchr/testify/require" ) func TestSharedAgentMemoryStaysWithSessionOwner(t *testing.T) { svc, _, tenants := newMemoryHarness(t) caller := enabledCtx(t, tenants, 1, "alice") provider := enabledCtx(t, tenants, 2, "alice") borrowed := context.WithValue(caller, types.TenantIDContextKey, uint64(2)) borrowed = context.WithValue(borrowed, types.SessionTenantIDContextKey, uint64(1)) borrowed = logger.CloneContext(borrowed) stored, err := svc.Remember(borrowed, types.MemoryItem{Kind: types.MemoryKindPreference, Content: "回答先列要点"}) require.NoError(t, err) require.Equal(t, uint64(1), stored.TenantID) require.Contains(t, svc.Recall(caller, "回答格式").Prompt, "先列要点") require.Contains(t, svc.Recall(borrowed, "回答格式").Prompt, "先列要点") require.Empty(t, svc.Recall(provider, "回答格式").Prompt) otherCaller := enabledCtx(t, tenants, 1, "bob") otherBorrowed := context.WithValue(otherCaller, types.TenantIDContextKey, uint64(2)) otherBorrowed = context.WithValue(otherBorrowed, types.SessionTenantIDContextKey, uint64(1)) require.Empty(t, svc.Recall(otherBorrowed, "回答格式").Prompt) tenants.set(1, &types.MemoryConfig{Enabled: false}) require.Empty(t, svc.Recall(borrowed, "回答格式").Prompt) _, err = svc.Remember(borrowed, types.MemoryItem{Kind: types.MemoryKindPreference, Content: "请写测试"}) require.ErrorIs(t, err, ErrMemoryDisabled) }