The BBC is to be prevented from naming a man it says exploited his status as an MI5 informant to abuse a partner and poses an ongoing threat to women.
A High Court judge has ruled that identifying the man, known only as X, would create a "real and immediate" risk he would be killed or injured.
The judgement proceeds on the basis that X is a danger to women, but it makes no formal finding.
The BBC said it would still be able to report key parts of its story.
In a statement, it said it had fought the case because it was "important journalism" about "matters of the highest public interest".
Attorney General Suella Braverman applied for an injunction preventing the identification of X by a planned BBC story.
Without confirming or denying whether X was an MI5 agent - a covert human intelligence source - she said the story would place X at risk of harm or death and damage national security by making people less likely to work as agents.
In such circumstances, she submitted there could be no balancing of X's interest against the interests of others, including the interests of any women who might potentially in future be harmed by him.
The BBC did not accept that revealing X's identity would give rise to a real or immediate risk to him - and argued the only people to be deterred from working as agents would be those engaged in similarly abusive conduct.
The corporation argued X had exploited his intelligence status to coerce and terrify a former partner, abused a separate woman, and that identifying him would protect other women.
It also argued MI5 should have known about X's behaviour and realised it was inappropriate to use him.
Much of the government's evidence was heard in secret, without the BBC or its lawyers present.
Security-cleared barristers, known as special advocates, were appointed to act on its behalf in the closed sessions, but they could not communicate directly with the BBC after seeing the sensitive material.
Mr Justice Chamberlain, granting an interim injunction, said he was satisfied that if the alleged agent's identity became public "there would be a real and immediate risk that X would be killed or seriously injured".
The senior judge said he relied on a "closed" MI5 statement containing a "detailed and nuanced assessment of the risks to X and others".
He said he had borne in mind that, according to the BBC, X chose to tell one former partner he worked for a security or intelligence agency.
As a result of evidence heard in secret, he also ruled that if X were named it was likely that measures would be taken to protect him, which would undermine the potential protective effect for other women of naming him.
The judge does not describe the measures, but possibilities might include a change of identity or appearance.
These measures would undermine one of the main public interest justifications for publicising his identity, the judgement says.
'Dangerous enough to kill'
Mr Justice Chamberlain concluded that disclosure of X's identity would have a wider damaging effect on national security because an agent who became aware of the BBC's story "would not necessarily assume that the story was accurate in every respect" and might conclude "it was based principally on the say-so" of one of the women.
The judge said this risked discouraging people from acting as agents, as they might think the trigger for disclosure of their identity was the making of an allegation, whether true or not.
The BBC's evidence included a video showing X attacking one of the women with a "dangerous weapon" and the two women - given the pseudonyms Beth and Ruth - gave statements to court saying they considered that X was serious danger to others. The women have never met.
Ruth said: "I consider that [X] is dangerous enough to kill a woman and I fear that he will do so if he is not challenged and exposed."
The judge also ruled that the BBC journalist working on the story took "proper steps to assess whether its various elements were true".
He said the attorney general nevertheless suggested there was evidence, which cannot be referred to without risk of identifying X, that cast doubt on Beth's "reliability, credibility and motivation".
The judge said that while it was not his role to decide the truth of the claims, the BBC's investigation "comfortably" met the test of showing the allegations it sought to broadcast were "serious and have a credible evidential basis".
'Highly unusual' proceedings
The BBC said it would report the key elements of the story, without identifying X, after clarification of the precise restrictions, expected next week.
It said "we fought the case to try and tell as fully as possible" two women's stories of their alleged abuse.
"This is because we firmly believe these are matters of the highest public interest - the issues of coercive control of women, male abuse of power and the failure of state institutions to address these problems," the statement said.
The broadcaster added that it "does not fully know the reasons why" the court decided to protect X's identity "and nor will the public" due to the "highly unusual" closed proceedings.
It said the secret procedures are a "significant departure from the principles of open and natural justice, as the judge himself states".