The government says it is "very disappointed" by a decision to block the deportation of two Libyan terror suspects and will appeal against it.
Colonel Gaddafi agreed not to torture suspects
The men, known only as DD and AS, argued they could be jailed and tortured if sent home despite a deal between the two countries.
The Special Immigration Appeals Commission ruled in their favour.
Under international laws, the UK does not deport people to regimes where they may face persecution.
But in October 2005, the government signed an historic deal with Libya, under which Colonel Gaddafi's government pledged not to mistreat anyone deported to Tripoli from the UK.
Similar memorandums of understanding (MOU) have been signed with Jordan and Lebanon. A different diplomatic agreement is in place with Algeria covering the treatment of returnees.
But SIAC chairman Mr Justice Ouseley said that the Libyan men faced a real risk of mistreatment as well as "a complete denial of a fair trial".
The European Convention on Human Rights could be breached if the two men were removed to Libya, he said, although he indicated it was not a "probable risk".
The two men, held in Long Lartin prison, have been granted bail in principle, with restrictive terms to be finalised.
In a statement, the Home Office said the men posed "a real risk" to national security.
FOREIGN SUSPECTS FACING DEPORTATION
Four have lost appeals
Three withdrawn appeals
Two won appeals
14 awaiting hearings or decisions
"We believe that the assurances given to us by the Libyans do provide effective safeguards for the proper treatment of individuals being returned and do ensure that their rights will be respected.
"We intend, therefore, to appeal to the Court of Appeal to seek to overturn this decision."
Kate Allen, of Amnesty International said: "Today's judgement underlines the principle that no-one should be returned to a country where they may face torture. MOUs offer no protection from torture and are not worth the paper they're written on."
And Liberty's Shami Chakrabarti said the government would "be better off trying to persuade new-found friends like Colonel Gaddafi to clean up their general record on human rights and torture in particular".
"And then I guarantee that the courts would be very happy to be shot of this particular problem," she told BBC News.