The taliban came to power by stopping socialist massacres, most famously in a "torture prison" the socialists built. That is what made people choose their side. They killed less people in open warfare than socialist revolutionaries did in peacetime, and much less than the Soviets did when "helping the socialist revolution spread".
Despite what happened since, sadly, I think you'll agree things never got as bad in Afghanistan as they did during the socialist revolution they had there. Not even now.
What does it have to do with the CIA coup installing the shah? Easy:
1) what happened in Iran? Socialist revolution, allied with a religious party ... followed by the religious side attacking their leftist "allies" when the state was toppled.
2) I find it hard to believe that Khomeini ordering his troops, after a referendum, to start executing leftists, and blinding yet more leftists (gouging one or two eyes out, which they have Hezbollah do again in last year's "woman, life, freedom" protests). It's a lot tougher to disagree with this strategy knowing that in the next country over leftists killed, tortured and maimed the clergy.
Like in most socialist revolutions (like in the red vs white terror in the Soviet Union), you were better off as one of the soldiers killing revolutionaries than you were as a "victorious" revolutionary. The reason leftists, at least the leaders, did a revolution turned out, surprise!, not to be social justice but power for themselves, and so they're far more scared of their allies than the police and army forces they need to keep control of what they've won.
Leftists brought the ayatollahs to power. Yes, this was partly in reaction to a British/US ("CIA") coup 20 years earlier, but ...
And I get it. Leftists have no allies willing to destroy western (or even non-Western) states for them anymore. So now they ally with islamists, who also want to destroy western states. What happened in Iran is a very in-your-face example of what would happen if a socialist-islamist alliance actually achieves anything: immediate terror attacks on leftists, followed by constant repression. And, to make matters worse, the Iranian population is screaming about this, regularly drawing attention to the problem. Which is a huge problem to grow leftist parties, because leftists don't have any other allies.
And yes, I'm not very leftist, but I must say, it is absolutely baffling to me that leftists would ally with conservative muslims, even if the Iran example did not exist.