Federal officials had heard stories about Scott Vitkovic. People at his Cambridge mosque said he wanted to attack the United States. And he had worn an Osama bin Laden T-shirt, outraging one mosque member who called it “completely irrational behavior.”

That was enough to get Vitkovic, an immigrant from the former Czechoslovakia, locked up in the secretive immigration system for the past seven years without ever being charged with a crime. Vitkovic denied in court records that he was a threat to anyone but himself, saying he had once attempted suicide and had been treated for depression, but federal officials remain determined to deport him as a threat to public safety.

“Nobody should be facing any of this kind of persecution,” Vitkovic, 43, said in one of several phone interviews from the Suffolk County House of Correction in Boston after US Immigration and Customs Enforcement denied the Globe’s request for an interview in jail. “I have not done anything against the law.”

Hundreds of immigrants such as Vitkovic have been jailed for months or even years while they await deportation, often on what critics say are shaky legal grounds. And Vitkovic is far from the longest held. That distinction belongs to two Cuban nationals: Aurelio Marquez-Coromina, who has been detained for more than 18 years because Cuba won’t take him back, and Manuel Reyes, locked up for 20 years in the same Indiana prison complex.

These detainees are not imprisoned for crimes, but because the United States wants them out and, for a variety of reasons, the government has not been able to deport them. Officials say Vitkovic himself delayed the deportation process significantly by refusing to apply for a Czech passport that was needed to deport him. The Cuban nationals could not be sent back home either, but they had violent records and mental illness, making their release in the United States impossible. US immigration officials say they regularly review their cases.

But advocates for immigrants say these and other costly incarcerations violate a Supreme Court ruling that barred the immigration agency from jailing immigrants for more than six months after they received deportation orders. They say officials should release people they cannot deport. Otherwise, officials should commit the mentally ill to hospitals or punish nations that refuse to take them back.

“You can’t lock up people forever,” said Michael Tan, a staff attorney with the American Civil Liberties Union’s Immigrants’ Rights Project. “We aren’t arguing that dangerous people should be allowed on the streets. They should be put in the right system.”

On the other side, conservatives, frustrated that the 2001 Supreme Court ruling led to the release of thousands of criminals, are pushing to expand the immigration agency’s powers to jail immigrants longer. People such as Vitkovic have been held longer based on special rules passed after the court’s ruling, they say, but some appeals courts have struck them down.

“We cannot achieve a lasting solution without strong internal enforcement,” said Representative Trey Gowdy of South Carolina, the chairman of the House immigration subcommittee. He is pushing to clarify the agency’s authority to jail specific categories of immigrants, including those who are mentally ill and dangerous and those who refuse to cooperate with their own deportations.

Federal immigration officials say long-term jailings like Vitkovic’s are rare. Nationwide, the average stay for an immigrant in custody is roughly 30 days. But the average stay on one day in September 2012 was more than 80 days, according to records provided to the Globe under the Freedom of Information Act. The same records showed that more than 900 immigrants on that day were jailed longer than six months after their deportation orders, despite the Supreme Court ruling.

But getting clear explanations for the jailings is elusive because federal officials refuse to identify the individual detainees. The Globe identified Vitkovic and others in interviews and through lawsuits they filed in the federal courts, where records are public.

Critics say the immigration agency often seems arbitrary in deciding whom to hold, and whom to set free.

Federal immigration officials jailed Vitkovic, but repeatedly released his fellow citizen Ivan Vaclavik, saying the Czech Republic would not take him back. In Massachusetts, Vaclavik racked up more than 100 criminal charges over almost four decades, including shoplifting, assault, and accosting a child. After the Globe published an article on him last year, officials deported him.

US immigration officials also locked up Marquez-Coromina for his mental illness and criminal record, but released Huang Chen, a Chinese national with a history of erratic behavior who had been convicted of attacking a woman in New York. The last time they set him free, Chen killed her. After he was convicted of the slaying, federal court records say, Chen was sent to a mental hospital.

And the immigration agency’s response to the Globe’s request for a list of detainees raised questions about how well the agency tracks its own prisoners. The agency initially omitted Reyes, the longest-held US detainee, from its list.

Little is known about the 59-year-old Cuban detainee, convicted in 1984 of kidnapping, sexual battery, and grand theft. Reyes has filed more than 100 federal lawsuits across the United States to get out of jail. But the suits have been so nonsensical — he appeared to call himself “the unofficial prison cat man” in one — that judges have dismissed them.

Immigration officials say the detentions are not indefinite and they emphasize that each case is decided on an individual basis. Often an immigration judge must first sign off on a long-term detention, and immigration officials review immigrants’ cases as frequently as every six months. Immigrants can also file federal court lawsuits, which make their cases public.

But the American Civil Liberties Union is so concerned that the agency is not properly monitoring long-term detainees that it filed a lawsuit to examine detention records. A New York federal judge awarded the group thousands of documents last year. Other lawsuits have won bond hearings for immigrants, which gives them a chance to argue for their release.

And, last week, US District Judge Michael Ponsor in Springfield cleared the way for a class-action lawsuit by detainees who are ineligible for bond hearings, saying the immigration agency’s argument for jailing them without a hearing was “ultimately flawed” and could not “withstand scrutiny.”

Because of these and other concerns, advocates for immigrants have called for a sharp reduction in the roughly 30,000 immigrants held on any given day across the United States for deportation. The immigration system is frequently criticized for poor health care and a lack of lawyers for immigrants, who are not entitled to public defenders. Advocates are particularly worried about services for people who are mentally ill: Three agency detainees committed suicide last year.

Vitkovic, the Czech citizen detained for seven years, is a complex figure who came to America to study. Over the past two decades, he has changed schools, religions, and even his name, saying he was misclassified at birth in Czechoslovakia as a girl. He has also suffered severe depression and attempted suicide at least once; he still takes antidepressants.

But federal officials have issued conflicting opinions in the past on whether he is dangerous.

In 2006, a Boston immigration judge said there was “no reliable evidence” that Vitkovic was a danger and ordered him released on $2,500 bond. An FBI agent had testified that Vitkovic told mosque members he wanted to attack the United States, but the judge dismissed his testimony as hearsay because the agent did not investigate it.

The judge also pointed out that Vitkovic had a glowing recommendation from the top administrators at Harvard Extension School’s health careers program, where Vitkovic studied premedicine, comparing him to Gandhi in his “affections and enthusiasms.”

Vitkovic never got out of jail, however, because immigration officials swiftly appealed and won on the grounds that Vitkovic had failed to prove that he was not dangerous.

In 2008, Vitkovic said, the same Boston immigration judge who had granted him bond denied his request for asylum.

Immigration officials said the judge also declared him a threat to national security in 2008 based on witness statements, including from the FBI, and Internet postings in which Vitkovic expressed a desire to “commit terrorist acts” with Islamic militants in the United States. Yet Vitkovic was never charged with any crime.

Vitkovic denies the judge declared him a danger and the immigration courts refused to provide information about the case, citing privacy rules that block an independent review of his file. The immigration agency also declined to provide the judge’s final decision, though Vitkovic gave the agency permission to do so.

He insists he made huge sacrifices to get to the land of the free, working as an operative for American intelligence in communist Czechoslovakia, getting beaten and nearly killed. He even has a 2007 endorsement letter from the brother of the Czech Republic’s late president Vaclav Havel, saying that Vitkovic aided the anticommunist revolution and could be in danger if deported. Ivan Havel confirmed in an e-mail to the Globe that he wrote the letter.

Vitkovic said US officials rewarded him with a visa in 1991 to study at a military school in New Mexico. He graduated and transferred to Norwich University in Vermont.

But immigration officials say Vitkovic transferred illegally to Norwich, giving them grounds to deport him. And when they finally started deportation proceedings, they said Vitkovic stalled the process by refusing to apply for a passport, which is required to put him on a plane and deport him. The agency could have prosecuted him for delaying his deportation and did not.

Vitkovic said he transferred legally to Norwich, but admits that he did not initially cooperate with efforts to deport him because he fears for his life if sent back to his homeland. For years, he said, he has bounced from one jail to the next in Massachusetts and Virginia, exacerbating his health problems.

“I just don’t know what is going to happen,” he said. “They may take me out of here anytime. I may be dead anytime.”

Vitkovic’s mother, who lives in North Carolina, said she is worried about her son. “I don’t know why he is in prison,” said Ludmila Weichel in halting English. “He is very honest, good person.”

Martin Pizinger, spokesman for the Embassy of the Czech Republic in Washington, declined to comment on the case. The FBI referred questions to US Attorney Carmen Ortiz, who declined to comment through a spokeswoman.

Immigration officials said Vitkovic has recently cooperated in his case and they expect to deport him soon.

But Vitkovic could actually be considered fortunate in comparison to Marquez-Coromina, who could be in prison for the rest of his life.

Marquez-Coromina, a 61-year-old former welder, has been behind bars almost since the day he got off the boat in the Mariel Boatlift from Cuba in 1980; he was convicted for armed robbery in a case in which prosecutors said he threatened to blow a shopkeeper’s head off. Federal officials say they have kept him in jail for the past 18 years, though his sentence is long finished, because he is dangerous and Cuba will not take him back.

“In very rare cases, ICE is authorized to detain for extended periods of time individuals who pose an extreme risk to public safety. Marquez-Coromina is one of these individuals,” said agency spokeswoman Gillian Christensen.

According to immigration records, Marquez-Coromina suffers from paranoid schizophrenia and harbors uncontrollable urges to kill people, including the Rev. Jesse Jackson.

He has refused to take medication, and immigration officials have never tried to put him in a mental hospital that could force him to take it.

In 2006, Marquez-Coromina filed a federal lawsuit denying that he was dangerous. He described himself as a devout Christian who participated in sports, Bible studies, and book readings in a Maryland prison, where he was held at the time.

He noted that he had a lesser criminal record than other immigrants the courts had released and that the government was holding him in only a medium-security prison. “Señor Coromina is not a dangerous individual,” he wrote.

Immigration officials — and a US District Court judge in Maryland — disagreed. In 2010, the judge ruled that immigration officials had the authority to hold him longer, another in a series of conflicting federal and appeals court decisions on long-term detention.

Lawyers say the conflict will probably continue, unless the Supreme Court weighs in again. In its 2001 ruling, the high court said the indefinite jailing of immigrants would raise “serious constitutional questions,” but the ruling was vague enough that lower courts are still arguing over who can be held — and for how long. by Maria Sacchetti [GLOBE]