When Romania's President Nicolae Ceausescu attends a Warsaw Pact summit later this month in Moscow, his most embarrassing task will be to explain to the assembled Soviet bloc leaders why the former head of his feared security service is in the United States as the guest of the CIA.

The defection last July of Gen. lon Pacepa, 53, originally described as "a high-ranking aide to President Ceausescu but since revealed by State Department sources as the chief of the Romanian secret police, has seriously complicated otherwise good relations between Washington and Bucharest.

In Romania, the affair has contributed to the most thorough purge of ranking Communist Party and government officials since Ceausescu came to power 13 pears ago. Western diplomats in Bucharest say the shakeup began in March in response to mounting economic difficulties, but Pacepa's defection is believed to have directly responsible for the firing of 12 key officials, including Interior Minister Teodor Coman and his deputy, lon Savu.

According to latest reports from Bucharest, the purge is now being felt in the lower ranks of the government as the new ministers of interior, foreign affairs, foreign trade, health, tourism, and technical supply take on their own staffs.

In personnel changes that may or may not be linked to the Pacepa case. Romania's ambassadors to the United States and the United Nations also have recently been replaced.

Meanwhile, thinly disguised attacks on Pacepa for his "betrayal of the fatherland" have been appearing in the Romanian press. In a person entitled "a curse upon that traitor," the well connected writer Adrian Paunescu addressed himself directly to the former secret police chief, desicribing him as a spy, parasite, sub-human brute, two-faced miscreant, and despoiler of the peace."

"Pace" was the word in Romanian and therefore a pun on Pacepa's name.

In a message congratulating the Romanian secret police on its 30th anniversary. Ceausesu himself condemned "rotten elements in the socialist countries who are ready to betray their homelands for a handful of silver."

In order to understand the bitterness of this reaction, it is necessary to understand also the special position of the security services in Romania. Despite Romania's independent foreign policy, internally it remains probably the harshest and the most tightly disciplined of all the East European countries - and the prime instrument for maintaining Communist Party control is the secret police.

The author Paul Goma, who launched an appeal last year for greater respect for human rights in Romania, once joked that there were only two people in the whole country unafraid of the secret police - "President Ceausescu and myself." Since making that joke, Goma has left Romania after a month-long detention by the security service that he says left him "a broken man."

Furthermore, Ceausescu owes much of his own power to the loyalty of the military and security establishment and Western analysts believe that any crisis in this area is bound to affect his personal standing and hitherto undisputed authority.

American officials involved in the Romanian leader's U.S. tour in April have privately described the obsession of his aides with his personal security as "almost medieval." Ironically, it was Pacepa himself who was assigned to make advance arrangements for the visit, which included the stipulation that a special food taster check Ceausescu's meals in advance for possible poisoning.

Pacepa's defection on July 29 came at a bad moment for Ceausescu, who was preoccupied with a wide range of internal and foreign policy problems. At home he was faced with the aftermath of a serious strike by 35,000 miners in the Jiu Valley last year, corruption in government departments and unrest among the large Hungarian minority. At the same time, he was about to court Soviet criticism by playing host to the Chinese leader, Chairman Hua Kuo-feng.

Western diplomats believe the Soviet leadership will cite the Pacepa case at the Moscow summit as proof of the dangers of Romania's ambivalent attitude toward its Warsaw Pact commitments. Ceausescu also is expected to come under some pressure to fall into line with the Soviet Union's other allies in condemning Peking.

Apparently signaling that he intends to resist such pressure, he traveled to neighboring Yugoslavia earlier this week to confer with his friend and mentor, President Tito, who was the first Communist leader to break away from the Soviet bloc.

According to Western diplomatic sources, one of Pacepa's more interesting revelations is the extent to which the Romanian secret service was infiltrated by East and West. He himself is believed to have been cooperating with the CIA for some time before his defection.

Given the secrecy surrounding Pacepa's defection, much is still unclear about the case. All that is known for sure is that he disappeared in West Germany while on a business trip there, ostensibly to negotiate a contract for construction of German aircraft in Romania. Two weeks later, U.S. officials confirmed he had efected and was in the United States.

Questioned about the case recently, a senior State Department official said Pacepa, who was first named an aide to Ceausescu in 1975, appeared to have acted for personal rather than political motives. It is possible that he may have sensed that he too was about to be affected by Ceausescu's sweeping reshuffle.

The official confirmed that the Pacepa case had caused temporary strains in U.S.-Romanian relations, but added that the difficulties appeared to be largely ones of mood rather than substance.

Western diplomats believe that good relations with Washington are an important element in Ceausescu's attempts to win Romania a greater degree of independence within the Soviet bloc, and that this policy is unlikely to be affected by Pacepa's defection.