What is wrong with this calculation of work done by an agent bringing a unit mass from infinity into a gravitational field? Let us assume that a gravitational field is created by a mass $M$. An agent is bringing a unit mass from $\infty$ to distance $r < \infty$, both measured from mass $M$.
The agent is always forcing the unit mass with a continuously changing force $\vec F(\vec x)$, $\vec{x}$ being the distance pointing radially out from $M$. 
According to classical mechanics, it holds that $\vec F(\vec x) = \frac{GM}{x^2}\hat{x}$, with $G$ being the gravitational constant.
The work is calculated as follows: $$W = \int_\infty^r\vec F(\vec x)\cdot d\vec x$$
$$=\int_\infty^r{{F(x)}\,dx\cdot cos(\pi)}$$
$$=-\int_\infty^r{{\frac{GM}{x^2}}dx}$$
$$=-GM[-\frac{1}{x}]_\infty^r$$
$$=GM[\frac{1}{x}]_\infty^r$$
$$=GM[\frac{1}{r}-\frac{1}{\infty}]$$
$$=\frac{GM}{r}$$
The body moved against the force's direction (the angle between them was always $\pi$). So the work should have been negative. But since $r$ is the scalar distance from $M$, it is positive like $G$ or $M$, yielding the result always positive.
What is wrong here?  
 A: 
The agent is always forcing the unit mass with a continuously changing Force, $\vec{F}$(x) ... = $\frac{GM}{x^2}\hat{x}$

By your force definition, the agent is not the attractive gravitational force but is something which is restricting the motion to constant velocity because the mass M is pulling in the $-\hat{x}$ direction with a  force equal in magnitude to the gravity but opposite in direction. That's okay, but I wanted to state that explicitly. Also, you are calculating only the work done by that agent.
You also have defined the positive direction to be away from $M$, and that's okay, too.
Your work integral calculates the work done by the force of the agent which is holding the mass back from accelerating toward $M$. Notice that, with your symbols,
$$W = \int_{\infty}^r \frac{GM}{x^2}\hat{x}\cdot dx(\hat{x})= \int_{\infty}^r \frac{GM}{x^2}~ dx.$$
The $\cos \pi$ fator you have is incorrect. The infinitesimal $dx\hat{x}$ in an integral defines the direction of the positive coordinate change, not the direction of the motion. The direction of motion is contained in the integration limits.
The result of the integral (for a unit mass being moved) is $$W = \left.\frac{-GM}{x}\right|_{\infty}^r= \frac{-GM}{r}.$$
The negative value makes sense because the agent is restraining the motion and acting in the positive $x$ direction while the motion is in the negative $x$ direction. And because the object is moving at constant velocity, the work done by the gravitational field will be the negative of the above so that the net work is zero, in agreement with the work-energy principle:
$$\Delta K = W_{net}$$
A: You made a mathematical error in trying to prove the result. It arises in many scenarios.  
To give you an insight into your mistake I would like to tell you the correct method of integration in physics. 
Remember that we always consider an element $dx$ at a distance $x$ from origin in the direction of $x$.
What you did was physically correct but while considering the field at $x$ you displaced the unit mass by $dx $ opposite to $x$.
Always remember that for your physics to be mathematically
correct consider $dx$ in the direction of $x$.
If you are taking $dx$ opposite to $x$ as you did you will have to integrate with a negative sign.
A: Your calculation looks like you want to calculate the work done by gravitation. In that case, you're missing a sign. Newton's law of universal gravitation as a vector equation is actually ($m=1$ in your example):
$$\vec F(\vec x) =- G {M m \over {\vert \vec x \vert}^2} {\hat{x}}$$
The missing sign perpetuates through the whole calculation, leading to the correct sign at the end.
In any case, you are calculating the work done by the gravitational field - if you want to take some other force into account (you are talking about "forcing the unit mass with a continuously changing force"), this is not part of your calculation. In that case, you need to define a second force $\vec F_2$ (e.g. a propulsion engine of a rocket) and then both those forces do work. To conclude, forces doing work add up.
