Tightening steerer expander/compressor and spacer positions with a carbon fork
how is it possible that it was completely loose?


It's impossible to tell.  Since the compression plug really doesn't do anything other than hold the stem in place until the stem's pinch bolts are tightened (it's the stem's pinch bolts that hold a bicycle's fork in place), it's likely that the compression plug wasn't assembled with sufficient torque to remain tight.  Given you stated, "I only remember the cap was very loose once soon after the purchase."  I'd say that's a very likely cause, although it's really impossible to know for sure.


  Another point is that the referenced manual recommends a 2 mm spacer above the stem. There is no such spacer in my setup, nor there is one in other pictures of bikes of the same manufacturer. There is a 4 mm spacer below the stem. Should I move it above the stem? Is it connected with the fact that the there is no gap now at the upper pinch bolt?


That's almost certainly direction from the manufacturer intended to ensure the steer tube isn't cut too short.  A spacer above the stem allows the steer tube to extend above the stem, ensuring the steer tube/stem interface is strong and stable.  If the steer tube is too short, the stem might not have enough length of steer tube to grip to.  If the steer tube is too short, the top portion of the stem's clamp won't have anything underneath supporting it, making the stem clamp prone to failure through distortion and cracking.

From the looks of your stem, if the steer tube has been cut so that it's only a mm or two short of the top of your stem, you should be fine.

But take the cap off and look, because it seems as if whoever assembled your bike didn't properly torque the stem cap and may not have put the dust cap in place.

If the steer tube is cut so the it ends below the level of the top bolt, I'd definitely say you need to remove the spacer from underneath the stem and lower the stem.  I personally would lower the stem if the steer tube doesn't clearly extend past the level of the top edge of the top pinch bolt so that it's very close to the top edge of the stem clamp itself. A steer tube that long means the pressure from the top pinch bolt can't collapse the stem's clamp. (Well, without the pressure being enough to collapse the steer tube too - don't overtorque it!) But I've seen steer tubes cut a bit shorter than that and not present any problems.

You may have to loosen the stem's pinch bolts to get the stem cap off to look.


  Is [the dust cap] important for keeping the dust out of the headset?


No.  It just keeps debris/dirt out of the hex head.  The cap and bolt will keep dust out of the headset.  (Or not - the gaps necessary to allow the fork to rotate will allow dust/dirt in to some degree in any case.)