The United States and Canada today resolved a nasty and long-running trade dispute with an agreement that will open the Canadian advertising market to U.S. publishers wishing to start magazines here or put out special Canadian editions of their existing magazines.

But for the first time, the United States was forced to accept the principle that, even in a free-trade environment, foreign countries could take steps to limit access to their markets by American firms in an effort to protect the viability of local culture -- in this case, a Canadian magazine industry that could provide an outlet for Canadian writers to tell Canadian stories and deal with Canadian themes.

That precedent could eventually come into play as the United States continues its campaign to tear down trade barriers to Hollywood movies and Internet services in a world growing more resistant to American cultural influences.

It was that dispute over trade principles rather than the limited commercial stakes that lay at the heart of the magazine dispute, which appeared to reach an impasse last week after months of public threats and private negotiations between the world's two largest trading partners. In the end, it took the intervention of ambassadors in Ottawa and Washington and an exchange of letters and phone calls between President Clinton and Prime Minister Jean Chretien to force their deadlocked negotiators to accept some sort of compromise.

Earlier this year, the United States had threatened tariffs on Canadian steel, forest products and textiles if Canada pushed ahead with new restrictions on magazine advertising. The prospect of a looming trade war riled industry, inflamed anti-American sentiment, filled newspaper columns and split the Chretien Cabinet.

Under the compromise, a new law to be passed by Parliament will allow U.S. publishers to put out special Canadian editions as long as less than 18 percent of its advertising pages are from Canadian advertisers aimed at Canadian readers.

Canadian magazine publishers claim the added competition from American magazines offering lower ad rates and demographically desirable readers will drive many of them out of business.

"It's a big setback for us and for Canada," said Francois de Gaspe Beaubien, president of Telemedia Communications Inc., one of Canada's leading magazine publishers.

U.S. publishers, by contrast, were generally pleased with the agreement, particularly with other provisions that will make it possible for them to set up new publications and joint ventures in the Canadian market. Tax laws will be revised to lower penalties on firms that advertise in American-owned publications that have substantial Canadian content.

To cushion the blow to the Canadian industry, Chretien announced that the government would provide annual operating subsidies to the Canadian magazine industry.

Despite the grousing from Canadian publishers, Canadian politicians were declaring victory today.

"The Americans wanted the whole pie but they are getting only a slice," said Sheila Copps, the feisty minister of cultural affairs who had decried the "bully-boy tactics" of U.S. negotiators. "For the first time, the U.S. has recognized cultural protection and promotion as a legitimate objective."

But a top U.S. official sought to downplay the precedent, saying this was merely a garden-variety trade dispute between commercial interests in which cultural issues did not come into play. He said the United States was willing to "temporize" its traditional demand for full and open competition "out of respect for the [political] dynamics of this issue in Canada and the respect that President Clinton has for the prime minister."