Question: why was hux the spy?
Answer: Hux is later revealed to have been a spy supplying information to the Resistance in order to undermine Ren and take his place as Supreme Leader (or at least seeing Ren falling along with the First Order if necessary); he tells Finn, "I don't care if you win.