AT THE LAST moment, it turned out that both President Reagan and Prime Minister Brian Mulroney wanted a free-trade agreement badly enough to take risks. They were right to do it, for this agreement can bring important benefits to both countries. But the risks are not minor. The two governments now have to enact legislation to bring the agreement into effect, and each commander will now have to deal with heavy sniper fire from entrenched protectionists on his own side of the border.

This free-trade zone was conceived by the two governments to set the world an example of enlightened commercial relations. At first some of the trade experts objected that it pointed the wrong way, toward bilateral deals rather than general worldwide rules. The answer was that the system of worldwide rules -- the General Agreement on Tariffs and Trade -- was becoming creaky and obsolescent. The latest attempt to broaden it and bring it up to date, the Uruguay round of trade negotiations, is off to an uncertain start. The Canadian-American agreement can set a standard for the GATT.

Over the past year, as the North American talks went on and on, they inevitably got entangled with shoes and ships and sealing wax -- all the grievances over specific commodities that are the substance of trade policy. By the end of the summer, it looked as though the whole endeavor might collapse under the weight of them. The turnaround was late in the long session Saturday at the Treasury, with each side pressing its case almost to the deadline at midnight.

There's no complete text yet, but the most difficult of the differences seems to have been resolved ingeniously. Canadians do quite a lot of subsidizing, but much of it is irrelevant to exports. They feared that American companies would use exaggerated complaints about these subsidies to tie Canadian exports up in endless and expensive litigation. The solution is to be an international tribunal as court of last appeal in these cases. It will abide by each country's law, but guarantee that the law is being fairly applied. If American companies are right in saying that they only want orderly enforcement of the rules against unfair subsidies, they have nothing to lose. If Canadians are right in saying that the American government has increasingly indulged in stretched and tendentious readings of the law, this tribunal will correct it.

Next comes a fierce attack on the agreement by all the industries in both countries that find the idea of expanded competition to be deeply threatening. They are entitled to a hearing. But they don't deserve to win