It's quite possible the other person did a lot of the "people" work that enabled the code to be written and distributed. In that case, it's a "we", not an "I", and focusing entirely on "I wrote the code", ignoring how it fit into the organization, is dismissing someone else's well-deserved credit.
I have seen the case you're wary of: a technical, but poorly communicating, employee that resents the success of a well-meaning, less technical but better communicating coworker.
I have seen good people like that, and I have seen snakes that pull the "we/I" as in the OP's story.
Generally, I don't see a lot of these "snakes" out in the real world, but I see a tremendous amount of misunderstandings, resentment, and righteous indignation. As I told the OP elsewhere, we're all the hero of our own stories, and everyone we disagree with is a villain. But ask the villain, and they think they're the hero. It's human nature. We don't want to admit that we might be wrong, that the people we disagree with might be right (or at least, the disagreement is not founded in malice).
So when I see situations like this, I rarely see snakes. I see misunderstandings that can be addressed with better communication and a good dose of humility.
I'm pretty sure this was simply because he thought it couldn't be done, and I just put the niggling thought in his mind so he tried and was successful.
Weirdly enough he was the introvert and I less so, and I got the promotion. No hard feelings on either end, as far as I know. We all have our quirks.
I have nothing to complain about. I've done very well career wise, and have no reason to beef with any of these people. But at the same time, I've concretely seen individuals lie outright, blackmail, threaten, insult, and bully their way to success. That you say you rarely see this makes me raise an eyebrow. I've gone through the "maybe I'm just seeing evil where there isn't any" thought process many times, but after watching multiple instances of negative actors take advantage of others while I was a disinterested third party, I can't but have conviction. People will abuse others when it serves them and they think they can get away with it, and in situations where power dynamics, visibility and relationships (so soft, highly non-fungible elements) drive your career, someone in power can get away with a LOT, even via only subtlety misaligning incentives.
Let me be clear, this is not some dominating majority of the population, and one should not start seeing enemies around every corner, but I've seen enough that instructing a junior engineer to watch out for those who would seek to take advantage of them seems very pragmatic to my eyes.
People who let bitterness rule their lives are far, far more common than actual snakes.
Assuming my observations are correct (I may be wrong, I'm often wrong), then what is the pragmatic advice to give? "Be wary of snakes!", or "Be wary of your own human desire to be the hero of your own story and make everyone you disagree with the villain"?
To me, this is a variant on the Pragmatic Programmers' advice that "SELECT isn't broken".
I think perhaps the issue here is that I'm coming at the discussion from over-correcting the opposite direction; I was raised with very much a "people are All Good(TM)" outlook, regularly being fed the "Things get better after highschool/people grow up" line as a child. I've found that these both gave me ineffective priors for navigating adult life, largely due to that if you assume people are acting with your best interests at heart, you may be less inclined to look out for your own best interests yourself, or at the very least how other people may be compromising it, consciously or not.
Framing it in terms of incentives also has the wonderful side effect of circumventing a lot of they "are they malicious or just self-serving" question since it hops right to "how do you fit your success into whatever equation everyone else is trying to route into"; my inquiry of whether they were TRULY malicious is usually made post-factum (or given sufficient evidence)
As a final statement (and to couch my own incompetence with tone over the internet, please don't let any of this come across as a You're Wrong!), I'm not sure I'd agree with your statement that people with bitterness outnumber those who would manipulate others, at least in my work context. In the lense of my "incentives" stance, both are continuums of behavior, but the bitterness continuum is counter-productive to career advancement while the "manipulating incentives" continuum is productive, so I'd logically expect to see more of the latter. (I guess the real crux here is "when does it become malicious," but that's a whole separate discussion)
This dovetails into your last paragraph, by way of saying that the fact that paranoid behavior is counterproductive is in no way inhibiting paranoid behavior. People believe it works. What people believe is far, far more important to them than what actually is. We engage in counterproductive, even self-destructive behavior all the time. And not just the "incompetent" people, but everyone I know. We are all stupid. (fwiw, I do not consider "smart" and "stupid" to be opposites, but rather different qualities that happily coexist. Just because you're smart doesn't mean you aren't also stupid.)
This is also aggravated by the human distrust of those who are different. How many employees think their managers are ignorant fools at best, and actively malicious at worst? A majority, probably. How many of those people really have a good understanding of their manager's job, and the pressures and conflicts the manager deals with? Not many, I'd wager. This is the root of racism, nationalism, and other forms of tribalism, if you think about it. Those feelings are irrational, but they're common as dirt.
This view of humans as stupid rather than malicious was reinforced for me recently, when I read Factfulness, by Hans Rosling (which you should definitely read!). Rosling, a doctor/professor/world health expert, tested thousands of people with basic multiple-choice three-options questions regarding well-documented facts on the subjects of poverty, global health, and the link. What he found was that every audience, no matter how smart, well-educated, caring, or involved in the field, actually performed worse than random chance. Start on his questions, without reading his book first, and you'll find yourself doing worse than if you'd just pulled letters out of a hat. Don't feel bad, the same is true of UN leaders, CEOs, med students, etc. Rosling went on to identify ten different thought patterns that lead us to misunderstand the world, even the most basic facts about it.
Read it, and then look around the corporate workplace, seeing it through Rosling's eyes. You'll see very few snakes and a tremendous number of idiots.
You don't seem to understand why the term is "snake" in the first place. Of course you don't see them. They're hiding in the grass, and you're not going to notice them until you step on one. That doesn't mean the people around you aren't getting bit.
I'm not saying it's great, but it also sounds like the OP needs to be more proactive about speaking roles in meetings when it comes to value of their practices, why things were done, etc. Now, would I work for a person if I hated them? Nope. Would probably be out of there.
In terms of "How can you!?", this stuff happens all the time all over the place and people work together. I think you can work through it with colleagues - it's not easy, but it's important if you think the mission / company are worthwhile. Otherwise, you're just going to dig deeper "into the box" on the credit thing and it won't be a good reflection on your work.