The United States and the Soviet Union have moved toward a compromise solution to the war in Afghanistan and only "a very, very narrow difference" separates the views of the two nations on a possible settlement, Secretary of State James A. Baker III said yesterday.

In testimony to the House Foreign Affairs Committee, Baker said "the one thing" that continues to separate the two sides is whether President Najibullah, the leader of the Soviet-backed regime in Kabul, "would preside over the government of Afghanistan during a transitional period for the conduct and holding of elections."

The United States insists that elections under Najibullah's control would not be acceptable to most Afghans, while the Soviet Union is unwilling to oust the Afghan leader.

Baker's assessment, the most optimistic from a ranking U.S. official in many months, came after full-scale discussions of Afghanistan between himself and Soviet Foreign Minister Eduard Shevardnadze in Moscow in mid-May, between President Bush and Soviet President Mikhail Gorbachev at Camp David on June 2 and a follow-up discussion June 6 between Baker and Shevardnadze.

Immediately after the Camp David meeting, a Soviet participant, Marshal Sergei Akhromeyev, said the sides had "agreed to a very considerable degree on how to resolve the Afghanistan situation," pending only a resolution of "the contradictions of opposing Afghani positions" by different factions there. Akhromeyev said the Soviet side came away believing that the United States now fully understands that Najibullah will not be militarily defeated.

A U.S. participant in the Camp David exchange on Afghanistan called it "the best discussion" that the two leaders had on this subject, but said it did not resolve all the issues.

Two other senior U.S. officials said the two sides were close to agreement on a set of principles to guide a transition to a peaceful settlement of the 10-year-old conflict, which began with the Soviet invasion in the last days of 1979 in support of a Marxist government in Kabul. The U.S. supported mujaheddin rebels with massive arms aid. After the withdrawal of Soviet troops in February 1989, the conflict became more of a civil war.

The principles under discussion, according to U.S. sources, include:Agreement that free and fair elections are the key to political settlement. Acceptance of a credible mechanism to monitor and supervise the elections. Agreement that the United Nations and the Organization of Islamic Countries can supervise the elections during a transition period.

The United States believes that credible elections can only be implemented by a neutral party, which would rule out Najibullah, according to official sources.

Moscow, on the other hand, insists that Najibullah is "a fact of life" who cannot be ignored, according to a U.S. official. At the same time, the official said, "the Soviets are thinking about how to find a solution, much more now than a year ago."

Perhaps as important as the dialogue between Washington and Moscow, according to U.S. sources, are moves by Afghan parties toward early elections. Najibullah and his communist-led movement, which is seeking a more politically neutral stance, are discussing elections in the areas under their control, while the mujaheddin rebels are talking of elections or popular selection of representatives from their areas of control to a national legislative body, an official said.