OK I think I have worked out why the code gives the results it does. Once I found the error in my vessel (a misaligned MechJeb control pod), I started to get consistent results.
The "magic" works like this (if I am not wrong of course):
1. Create an orientation where the vessel's pitch,roll and yaw are said to be zero. Call this the reference orientation.
2. Create a relative rotation by multiplying the reference orientation with the inverse of the vessel's orientation. In effect this relative rotation then contains the roll, pitch and yaw of the vessel relative to the reference orientation.
3. Take the inverse of the relative rotation. This has the effect of converting the relative rotation's roll, pitch and yaw back into absolute terms which can then be simply read from the X,Y,Z values of the quaternion.
a. I get confused because quaternions are used to store both rotations and orientations. That is because rotations and orientations are the same thing, it's the context/semantics that is different.
b. I still have not worked why Euler(90,0,0) is necessary in the MechJeb C# code, but is not necessary in kOS. I am aware that kOS "tweaks" some of the Unity API details, so maybe this is the explanation.