By Wednesday, a senior Amazon executive in charge of real estate, John Schoettler, arrived from Seattle for a meeting convened by Mr. Cuomo in his Manhattan offices between Amazon and unions. By the end, the unions and the executives seemed to be making progress toward a resolution.

That night, the company decided internally to pull the plug.

The choice blindsided Mr. Cuomo and Mr. de Blasio.

“Out of nowhere, they took their ball and went home,” Mr. de Blasio said on Thursday night.

He learned of the decision in a phone call from Jay Carney, an Amazon vice president and a former spokesman for President Barack Obama, according to a person briefed on the call. Even as the deal was in peril, Mr. Carney, who oversees the company’s press and government relations, never went to New York to meet with officials, three people with knowledge of the meetings said.

Amazon can deliver toothpaste in traffic-snarled Manhattan on the same day an order is placed. But when it came to navigating the politics of New York, the company appeared out of step, a giant stumbling onto a political stage that — despite its data-driven success — it never fully understood.

“Amazon underestimated the power of a vocal minority and miscalculated how much it needed to engage with those audiences to make HQ2 a success,” Joseph Parilla, a fellow at the Brookings Institution, said, referring to the second headquarters search.

The company, in particular, failed to develop a robust strategy to address the growing influence of the progressive left in New York, led by Representative Alexandria Ocasio-Cortez of Queens, who was elected in November and was a fervent skeptic of the deal.

The political winds changed so swiftly that local lawmakers in Queens who had signed a letter in 2017 trying to woo Amazon refashioned themselves as champions of the opposition in recent months.