By Kevin Connolly
BBC News, Washington
The long-running and so-far inconclusive battle between the Supreme Court and the Bush administration over Guantanamo Bay has been like a High School political science class brought to life.
Both Barack Obama and John McCain have pledged to close the camp
A power struggle between two branches of government has been playing out over one of the defining issues of this presidential term.
Over the course of the last few years, the executive has had the upper hand over the judiciary.
Consider, for example, how the White House and the Pentagon managed to work their way around a previous Supreme Court decision in 2004 which appeared to guarantee the Guantanamo detainees a right of appeal to the civilian justice system.
In response to that judgement, the US government began what it called Combatant Status Reviews of the detainees and created legislation to set up military commissions - the quasi-legal bodies which have already begun hearing the evidence against the accused.
But with this latest ruling, the Supreme Court appears to have restored its authority in these matters by producing a ruling which will be much more difficult for the administration to ignore.
For example, proceedings at those military commissions may now be interrupted or suspended while lawyers defending the accused assess how best to take advantage of the new Supreme Court ruling.
All are likely to ask federal judges to assess the legality of their detention.
Whatever you think about the morality and legitimacy of the Guantanamo detention facility, there can be no doubting the determination of the Bush administration to stick to its policy, regardless of judicial attempts to undermine it.
The legal implications of Thursday's judgement are huge - after all, it is at least possible that it might end with American judges ordering the American government to release foreign detainees.
It means that even though the US government deliberately built this camp in an American base on the island of Cuba - so that it is not on American soil - and even though the detainees are not US citizens, they are deemed to have rights under the constitution.
The Bush argument that those rights are forfeit because America is fighting a "war on terror" was dismissed.
Mr Bush has been blocked three times on Guantanamo by the Supreme Court
The nine justices of the Supreme Court split five-four on the issue with one of the dissenting conservative judges warning that the decision could make the war on terror more dangerous.
But Anthony Kennedy spoke for the majority of the justices when he argued that the US Constitution and its laws are designed to function even in extraordinary times.
The political impact of today's Supreme Court ruling may not be quite so extensive, however.
It will mean of course that the Bush administration's decision to create this unprecedented system of detention will not be remembered by history as a decisive or successful means of dealing with a terrorist threat.
It has been mired in international disapproval and undermined by legal challenges from the very start, and diplomatically at least it has been counter-productive.
It will also ensure that this issue dominates the closing months of the Bush presidency - he has already said he will abide by the court's decision while making clear that he does not agree with it.
But the timing of the announcement really serves to draw attention to just how close we now find ourselves to the end of the George W Bush era.
At a moment like this, our attention focuses automatically on the known attitudes of the two men competing to be the next president.
As it happens, both John McCain and Barack Obama (the Republican and Democrat respectively) have talked about closing Guantanamo Bay, so much of the political and legal heat may simply evaporate from the issue a few months down the road.
That does not mean that the issue would be over, of course.
No new American president could afford to make himself look weak on terrorism by simply releasing the detainees, so there would be difficult issues to resolve which might take time.
But when Mr Bush completes his second term, there will no longer be in the White House a powerful advocate for the system at Guantanamo Bay grimly determined to find a way of keeping it open regardless of the view of the courts.
It is hard to see what form a solution will take which balances America's perception of its own security priorities against the concerns of its own senior judges.
But one prediction can now safely be made.
Guantanamo may have been created on the watch of America's 43rd President - but its fate is almost certain to be determined during the first term of its 44th.