The House gave President Carter a major victory last night as it blocked Republican attempts to force Panama to pay the costs of the Panama Canal treaties.

The late-night action came just hours after the House held its first secret session in 149 years to hear classified information about Panamanian government involvement in running guns to Nicaraguan rebels. A final vote on the controversial canal legislation, which implements the treaties, is expected today.

Instead of Republican amendments that the administration considered a violation of the treaties that could shut down the canal, the House adopted by a 220-to-200 vote a much milder, almost cosmetic, substitute. Twenty-five Republicans and 195 Democrats voted for the substitute, while 68 Democrats and 132 Republicans opposed it.

The substitute was offered by Merchant Marine Committee Chairman John M. Murphy (D-N.Y.). It would require Panama to pay only about $9.3 million, its current debt to the United States for such items as electricity, before receiving the property that is to be transferred to Panama under the terms of the canal agreement.

The Murphy amendment was a substitute for a proposal by Rep. George Hansen (R-Idaho) that would have had Panama paying about $2.3 billion before receiving the $75 million annually that has to be paid to Panama under the terms of the treaties.

Hansen called his amendments "the honest amendments," saying they would help the president keep his promise that the treaties would not cost the U.S. taxpayers anything.

Hansen contended that "the government of Panama is breaking our laws and international laws of the OAS [Organization of American States] and the U.N. by gunrunning. Why are we so hell-bent to keep our commitments to a country that does not keep its commitments?" He said his amendments would stop "the windfall of funds" that Panama could use to greatly increase its revolutionary activities in Nicaragua and elsewhere.

Murphy contended Hansen was "converting a gift to Panama" into a sale of real estate.

Hansen had argued that the treaties would cost $4 billion over their 20-year life. The administration contended that the cost would range from $300 million to $870 million, and that the costs would be primarily to move and maintain U.S. military forces, pay early retirement benefits to employes whose jobs would be phased out, and run a new commission set up to operate the canal until it is turned over to Panama in 20 years.

Although the treaties were approved by the Senate in 1978 and became law, some portions are self-executing while others require the implementing legislation in order to take effect.

The implementing legislation was in deep trouble in the House as members found the canal agreement unpopular back home. They worried about the costs and came under severe pressure to support the Hansen amendments from organizations opposed to the pact such as the American Legion and many right-wing groups.

Administration officials, meanwhile, were fearful of a severe foreign policy setback if the legislation was defeated or crippled. They feared it would damage U.S. credibility in Latin America, and claimed it could cause the shutdown of the canal if Panama declared the agreement had been violated.

The legislation had been held off the House floor twice in the last month because of lack of votes to pass it. Democratic leaders scheduled it this week because they felt confident they had the votes for passage, but were uncertain whether they could beat the Hansen amendments. House sources admitted the Murphy substitute language was cosmetic and added little to the bill. But they felt the Murphy substitute gave wavering members something to vote for.

While the legislation was being held off the floor, right-wing Republicans developed a new issue, that of Panamanian gunrunning to Sandinista rebels attempting to overthrow the Nicaraguan regime of President Anastasio Somoza.

Rep. Robert E. Bauman (R-Md.) yesterday demanded the House go into secret session to hear classified government material about Panamanian officials' involvement in the gunrunning. The session was the first of its kind since 1830 when President Andrew Jackson sent a secret communication to the House.

Murphy, who is leading the fight for passage of the treaty legislation, but who is also a close friend of Somoza's said the secret testimony was new and significant.

The House heard "the fact that Panamanian officials clearly violated the terms and conditions of the charter of the Organization of American States by gunrunning and promoting insurrection in other countries," Murphy told reporters.

Murphy claimed Panama was also involved in inciting insurrection in El Salvador.

According to testimony cited in the debate, Murphy said, high-ranking officials in the Panamanian government were involved in the gunrunning as part of government policy.Murphy would not respond to a question about whether the Panamanian leader, Gen. Omar Torrijos, was involved.

On the other hand, Murphy contended that the testimony would help to pass the bill because it reinforced the need for an American presence in Panama to hold the government in check and help run the canal.

Majority Leader Jim Wright (D-Tex.) said the Democratic leaders did not fight the call for a secret session because they thought it "might have a beneficial effect. We got everything out on the table and proved there were no secrets anybody didn't know all along."

Wright said of the testimony that the Panamanians were aiding the Sandinistas, "Well, they admit it. Torrijos has had a vendetta going against Somoza for some time." But the Costa Ricans, Venezuelans, and Mexicans are against Somoza and aiding the Sandinistas too, Wright said.

Wright said there was nothing in the treaties that said the United States could interfere in Panama's foreign policy.

"I have to assume the establishment in Panama's government does favor the Sandinistas. I don't . . . The canal delivers 400,000 barrels a day of Alaskan crude oil to the lower 48 states. It's more important to keep the canal open," Wright said. He echoed the administration argument that the canal would probably be shut down if the treaty enabling legislation isn't passed.

Sources said the material revealed in yesterday's secret House session was based on closed-door testimony to the Merchant Marine Committee by Lt. Gen. Dennis McAuliffe, commander of the U.S. Southern Command stationed in Panama.