Congressional and union leaders are confident they can come up with legislation to continue the government's flexible work hours program before it expires in mid-July.

Nearly half a million federal workers are now under some kind of program that allows them to either set their own hours, or to work stretched-out shifts four days a week so they can take long weekends.

The three-year-old flexitime experiment almost died in March. The Reagan administration refused to go along with any legislation that did not give agency heads authority to terminate programs when they felt they were counterproductive or added to the cost of government operations. It did, however, agree to a 90-day extension of the current program to see if a compromise could be worked out.

And now it appears that a compromise can be reached--one that will give both sides a lot of what they want.

The Senate's approach, which may come up for a vote this week, would allow agencies to terminate most current flexitime programs. But agencies would be required to negotiate with unions on new or replacement programs and, if labor and management could not reach agreement, the issue would go to the Federal Service Impasses Panel.

FSIP would determine, based on evidence of whether the program was efficient and improved service, whether agencies could drop the flexitime programs or would be required to keep them.

On the House side, Rep. Geraldine Ferraro (D-N.Y.) is planning to introduce a flexitime extension bill that is similar to the Senate plan and is believed generally acceptable to unions and the White House.

Many federal workers, who have built their work schedules and car pools around flexitime programs, have been concerned that the entire program will be abolished. Several agencies--General Services Administration and the Coast Guard, for example--have eliminated flexitime experiments for most or all employes. Many others will follow suit unless the experiment is given a new, permanent lease on life. Observers believe that a new flexitime law will be on the books before the July expedition date