By Jonathan Stempel

NEW YORK (Reuters) - A federal judge narrowed but refused to dismiss a former Goldman Sachs Group Inc programmer's malicious prosecution lawsuit against FBI agents in connection with his two criminal convictions, both since reversed, for stealing computer code.

Wednesday's decision by U.S. District Judge Kevin McNulty in Newark, New Jersey adds to a convoluted seven-year odyssey for Sergey Aleynikov, beginning with his first arrest in July 2009.

Prosecutors accused Aleynikov of having stolen computer code from Goldman, as he prepared to join a Chicago high frequency trading startup.

In his 32-page decision, McNulty dismissed claims over the first arrest, saying the FBI agents reasonably believed they had cause, though a federal appeals court later found otherwise and in February 2012 voided Aleynikov's first conviction.

McNulty also declined to rule on similar claims arising from Aleynikov's August 2012 rearrest, at least until a New York state appeals court decides whether last July's voiding of Aleynikov's second conviction was proper.

Aleynikov said the FBI at the time should have known of problems with the state case, but McNulty said: "It is too early to say whether its resolution in Aleynikov's favor will stick."

The judge also said Aleynikov, a Russian-born U.S. citizen, can sue the agents for allegedly seizing his passports and personal property illegally, but also put that part of the case on hold.

Aleynikov spent 11 months in prison on his first jury conviction before the federal appeals court voided it, saying prosecutors misapplied federal laws on corporate espionage.

In a surprise move, Manhattan District Attorney Cyrus Vance Jr. later charged Aleynikov with state crimes based on the same alleged misconduct.

Another jury convicted Aleynikov in May 2015, only to have the trial judge overturn that verdict two months later, saying prosecutors did not prove Aleynikov violated the law under which he was charged. Vance is appealing that ruling.

"We are confident that once the Manhattan district attorney's appeal is decided, we will be permitted to move forward with our very significant claims against the FBI agents," Kevin Marino, Aleynikov's lawyer, said in an interview.

The U.S. Department of Justice, representing FBI agents Michael McSwain and Eugene Casey, did not immediately respond to requests for comment.

Aleynikov has racked up millions of dollars of legal bills. His lawsuit against the FBI agents seeks compensatory and punitive damages, among other remedies.

The case is Aleynikov v. McSwain et al, U.S. District Court, District of New Jersey, No. 15-01170.

(Reporting by Jonathan Stempel in New York; Editing by Andrew Hay)