WASHINGTON — One day in October 1979, an American diplomat named Archer K. Blood arrived at Afghanistan’s government headquarters, summoned by the new president, whose ousted predecessor had just been smothered to death with a pillow.

While the Kabul government was a client of the Soviet Union, the new president, Hafizullah Amin, had something else in mind. “I think he wants an improvement in U.S.-Afghan relations,” Mr. Blood wrote in a cable back to Washington. It was possible, he added, that Mr. Amin wanted “a long-range hedge against over-dependence on the Soviet Union.”

Mr. Blood’s newly published cable sheds light on what really drove the Soviet Union to invade Afghanistan only two months after his meeting with Mr. Amin. Spoiler alert: It was not because of terrorism, as claimed this month by President Trump, who said the Soviets were right to invade. Among the real motivations, the cable and other documents suggest, was a fear that Afghanistan might switch loyalties to the West.

“This was a key moment that raised the Soviet sense of threat,” said Thomas S. Blanton, the director of the National Security Archive, a research organization at George Washington University that recently obtained the cable through the Freedom of Information Act and posted it online on Tuesday. “It’s a fascinating case study of the necessity in all of these international affairs of putting yourself in the other guy’s place — what does it look like over there?”