I will answer both SB and Brumby in one post, this is not to counter argue, but clarification.
1. The treaty is the form or legal cover.
2. The essence to China is security of its own, and by the treaty, NK's security if its leadership works with China. U.S. and SK may have other essence, but it is not necessarily the same as China's.
3. I was only saying that it does not have to be Kim sending the invitation. But I do not exclude his invitation if he choose to.
4. A coup only happens when the NK leadership breaks. In that case, one side may choose China, the other choose US. Which side choose whom is NOT certain in my view. But either way, since it is a break, there will always be a faction that invite China, that makes China's entering being legitimate.
5. Facing a defeat by US/SK, the people who see no chance of survival in the US order will choose China. They will request China's assistance by the treaty. Who are these people in the leadership? Are there anybody in the current NK regime acceptable by US? I leave these questions to SB and Brumby to think over. My own answer is "almost non-existence". That leaves the "invitation to China" a sure thing.
6. Specially about Kim, I believe that he may be defying China all this time, but he would be the first person that US would put in jail or kill in case of a war broke out with NK. So, Kim will be left with no option but to ask China for help, at least China just want him to change policy, not removing him.
7. I once again reject the notion of UN involvement. UN charter grant "collective defense", so there is no way/need for China to obtain an approval from UN or any other party for a specific event regarding NK because the Charter and the treaty. None of anybody's business except China and NK. Remember, US does not need China's consent to station army in Japan nor a specific UN mandate, same same.
8. Brumby may argue that point 7 above is only about formal legal protocol not the essence. I will answer this. The essence to China is that, US did not discuss with China or listen to Chinese complain before US setup all the military bases around China, not openly, not behind doors, so what could make China do differently? So my point 7 is both procedure and essence.