Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

WASHINGTON — The United States and Canada reached a deal on Sunday to salvage NAFTA as a trilateral pact with Mexico, beating a midnight deadline with agreements to substantially boost American access to Canada's dairy market and protect Canada from possible U.S. auto tariffs, sources with direct knowledge of the talks said.

President Donald Trump had threatened to splinter the nearly 25-year-old North American Free Trade Agreement into a bilateral pact with Mexico and tax Canadian vehicle exports to the United States if Ottawa had failed to sign on before a midnight Sunday deadline.

But Trump has approved the "framework" deal with Canada, a source familiar with the decision said, just days after he sharply criticized Canadian Prime Minister Justin Trudeau and his NAFTA negotiating team.

Trump blames NAFTA for the loss of American manufacturing jobs and wants major changes to the pact, which underpins $1.2 trillion in annual trade. Markets fear its demise would cause major economic disruption.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

Negotiators from both sides spent two days talking by phone as they tried to settle a range of difficult issues, such as access to Canada's closed dairy market and U.S. tariffs.

Late last night, our deadline, we reached a wonderful new Trade Deal with Canada, to be added into the deal already reached with Mexico. The new name will be The United States Mexico Canada Agreement, or USMCA. It is a great deal for all three countries, solves the many...... — Donald J. Trump (@realDonaldTrump) October 1, 2018

....deficiencies and mistakes in NAFTA, greatly opens markets to our Farmers and Manufacturers, reduces Trade Barriers to the U.S. and will bring all three Great Nations together in competition with the rest of the world. The USMCA is a historic transaction! — Donald J. Trump (@realDonaldTrump) October 1, 2018

Congratulations to Mexico and Canada! — Donald J. Trump (@realDonaldTrump) October 1, 2018

The deal will preserve a trade dispute settlement mechanism that Canada fought hard to maintain to protect its lumber industry and other sectors from U.S. anti-dumping tariffs, Canadian sources said.

But this came at a cost.

Canada had agreed to provide U.S. dairy farmers access to about 3.5 percent of its approximately $16 billion annual domestic dairy market, the sources said, adding that the Canadian government is prepared to offer compensation to dairy farmers hurt by the deal.

The influential Dairy Farmers of Canada lobby group, which strongly opposes the idea, said in a statement that it insisted "any final NAFTA deal should have no further negative impact on the dairy sector."

Canada also agreed to a quota of 2.6 million vehicles exported to the United States in the event that Trump imposes 25 percent global autos tariffs on national security grounds, Canadian and U.S. auto industry sources said.

The quota would allow for significant growth in tariff-free automotive exports from Canada above current production levels of about 2 million units, safeguarding Canadian plants.

But the deal failed to resolve U.S. tariffs on Canada's steel and aluminum exports, the Canadian sources said.

"We celebrate the trilateral agreement. The door is closed to the fragmentation of the region," Jesus Seade, President-elect Andres Manuel Lopez Obrador's NAFTA negotiator said on Twitter. "NAFTA 2 will give certainty and stability to Mexican trade with its partners in North America."