The CIA's discovery that agency employes helped fulfill a terrorism training contract with Libya touched off a major internal housecleaning that led to the loss by firing, transfer, attrition or forced retirement of 820 agents in the agency's elite clandestine service.

The controversial 1977 housecleaning, only a fraction of which was directly related to the Libyan operation, was initiated by then-CIA director Adm. Stansfield Turner.

It represented the major thrust of the Carter administration's attempt to get control of the agency's covert operations branch and force its agents to adhere to rigid guidelines governing their activities. The controls were mandated in the wake a host of revelations of CIA abuses during the 1960s and early 1970s.

The internal shakeup was triggered when Turner learned from a press inquiry that two active-duty CIA agents appeared to be involved in the Libyan activities of ex-CIA agents Edwin P. Wilson and Francis E. Terpil. Turner fired the two active-duty agents.

In unrelated cases, a third and a fourth agent were dismissed, one of them for using a private operative overseas without informing his CIA superiors. Then Turner, already predisposed to further reductions in covert staffing levels, swollen from the Vietnam-war era, launched a massive overhaul of the operations directorate.

A team of systems analysts was imposed upon the highly autonomous clandestine branch and, by the time Turner was finished, 17 covert agents had been fired, 157 were asked to retire involuntarily, 50 were transferred out of the clandestine service to other CIA divisions and nearly 600 other clandestine jobs were eliminated by attrition.

At the time, CIA officials insisted that the exodus from the clandestine service was a routine reduction in force. But this account, provided by senior intelligence officials, for the first time shows a broader purpose and illustrates the significance that was attached to the Wilson-Terpil case during Turner's four-year tenure. Turner's actions were attacked by veteran intelligence officials as a needless decimation of covert intelligence-gathering capabilities.

During his first month in office in the spring of 1977, Turner was not informed of the investigations that had been initiated nine months earlier by his predecessor, George Bush.

In September, 1976, one of Wilson's partners and one of his employes told the agency that Wilson was exporting terrorist training and materials to Libya's radical dictator, Col. Muammar Qaddafi.

Turner discovered that his predecessor had investigated the charges but had decided not to fire the two active-duty agents. Instead, Bush officially reprimanded and reassigned one of them as punishment for having assisted Wilson in designing and building prototype delay-action timers for mass production, according to senior intelligence officials familiar with the investigation.

The second officer's disciplinary action as well as investigations into the activities of several other active-duty agents were pending when Turner took office.

Bush was traveling in Mexico yesterday and could not be reached.

These discoveries in Turner's opening weeks as CIA director "led to a major change" in his approach to the clandestine service, according to one knowledgeable official.

In his first contact with the agency's internal investigative files Turner saw "four people out of control," the official said, and many others who were "still playing cowboy." Turner is said to have believed that the agency's covert operations branch had yet to respond to a new era of tighter control.

Turner then began his wholesale housecleaning, removing many senior clandestine service veterans, including the clandestine service chief, William Wells, and his deputy, Theodore G. Shackley.

One knowledgeable official said Turner "weighed in heavy" on the tightly knit clique of CIA officers whose tenure dated to the Office of Strategic Services (OSS), a World War II predecessor to the CIA.

This reconstruction of events, dating from the time the CIA was put on notice that its agents and ex-agents were allegedly employed in the support of international terrorism, offers the best explanation to date of how the CIA policed its own ranks.

In the final analysis, the agency did not respond fully to the Wilson case until Turner's attention was focused by a Washington Post inquiry in April, 1977, according to intelligence officials. Before that time, the agency had "fussed around" with several disciplinary investigations of its own agents, according to one knowledgeable source. A single letter of reprimand and reassignment had been ordered before Bush left office in January, 1977.

Wilson and Terpil were indicted in April, 1980, by a federal grand jury here for allegedly supplying explosives, delayed-action timers and terrorist training and for plotting the assassination of a prominent exile critic of Qaddafi's regime. Other indictments are expected.

And other federal regulatory agencies are examining their rules and federal laws to curb what federal officials see as an epidemic of illegal arms and technology exports to hostile nations.

The CIA's investigation into Wilson's dealings with Libya began Labor Day weekend in 1976, when one of Wilson's partners, Kevin P. Mulcahy, telephoned the Langley headquarters of the CIA and told officials that Wilson, Terpil and others were operating a massive terrorism support program for Qaddafi.

Mulcahy, who today claims that he thought Wilson's activities were sanctioned by the CIA, said he decided to break with the operation after Wilson instructed him to attempt to purchase a heat-seeking Redeye missile from General Dynamics Corp. for the Libyans. Mulcahy said he feared the missile might be used in a terrorist attack on a commercial airliner.

Two weeks later, a Cuban CIA contract agent telephoned another CIA official and told him that Wilson was plotting to assassinate one of Qaddafi's expatriate critics.

Within a few more weeks, the agency was told by one of its retired ordnance officers, John Henry Harper, that he had gone to Libya in August, 1976, for Wilson to set up a small factory to manufacture bombs disguised as ashtrays, coat hangers, attache cases and teakettles.

Mulcahy had told CIA officials that one of the agency's active technical engineers, William E. Weisenburger, had assisted Wilson in building the prototype timers and had arranged for their assembly at an electonics firm that did business with the agency.

Weisenburger acknowledged his role when interrogated, but claimed that he was duped by Wilson, who reportedly said the timers would be used in "mine-clearing" operations in Libya.

Mulcahy said that another active agent, Patry E. Loomis, had attended meetings with Wilson concerning the sale of sensitive night vision equipment to Libya while Loomis was in "deep cover" as an aircraft salesman for Fairchild Industries Inc. in Indonesia.

The agency, complying with a presidential order, reported the allegations to the Justice Department, and immediately began its internal investigation, headed by then-CIA inspector general John Waller.

In the final months of the Ford administration, the decision was made by Bush's deputy, Henry Knoche, with Bush's approval, to reprimand Weisenburger and take away his authority to deal with the agency's contracting firms.