At 10.40am on Monday Michel Barnier arrived at the Irish delegation room on the ninth floor of European Council’s Europa Building.

On his way in, accompanied by adviser Georg Riekeles, he passed the Irish tricolour and EU flags, standing just outside.

Next door is the UK delegation room, with the Union Jack outside the door, but no EU flag.

Mr Barnier was exhausted. He had been working up until 3am. The deal was not yet done, but it was close.

Simon Coveney, the Tánaiste, was waiting in the small but bright Irish conference room.

He was joined by Minister for European Affairs Helen McEntee, Ireland’s ambassador to the EU Declan Kelleher, diplomats Émer Deane from the Brussels Brexit Unit and Ronan Gargan, director of the EU-UK Unit in the EU division of the Department of Foreign Affairs.

Mr Coveney had already met Mr Barnier’s deputy negotiator Sabine Weyand and Nina Obermaier, the lead Task Force official on Ireland.

Barnier himself had just briefed ministers from the 27 member states. But on the ninth floor, the Irish delegation room were given a private account of the state of play.

As he spoke, EU leaders gazed down from portraits of seven bygone Irish EU presidencies, including the April 1990 summit at Dublin Castle.

Margaret Thatcher, Charles Haughey and Francois Mitterand are captured looking down with those faintly-smiling family photo expressions.

It seems now like a different world. The mood that Monday morning 28 years later could not have been more starkly different.

"Barnier was uber, uber cautious," says one source present.

In fact, the meeting started 20 minutes early because he had cancelled a news conference. Things were so sensitive that communication of any kind was deemed not advisable.

When Mr Coveney spoke to the waiting media after the meeting, so brief was his statement that it was greeted by an audible "for f**k sake" from one frustrated journalist, a remark picked up – and later deleted – by the European Council’s in-house TV service.

What Mr Coveney did not tell journalists was that there was cautious optimism.

"It was clear this was where Barnier wanted to be at that moment," says one source. "He said there was a deal to be done. This was a man on his game."

At 11am the two teams of negotiators, led by Weyand and Olly Robbins on the British side, returned to the "tunnel", the negotiating space figuratively sealed against leaks. They would stay there until 9pm that night.

In the weeks leading up to Monday, the world was told that 95% of the Withdrawal Agreement had been concluded. The 5% was Ireland, and how to avoid a hard border.

Ireland had been honed down to a set of fundamental elements, granite-like in their resistance to compromise.

The landmarks to this moment were well known.

On 8 December 2017, the EU and UK signed up to a Joint Report which said that, whatever happens, there would be no hard border, the Good Friday Agreement in all its parts would be protected, there would be unfettered access to Great Britain for Northern Ireland goods, North-South cooperation and the all-island economy would be preserved.

This could be done through the future trade deal, technology, or – if neither of those options worked – Northern Ireland would remain aligned with the EU customs union and single market.

In other words, the backstop.

On 28 February, the EU’s first draft text of the Withdrawal Agreement spoke of a common regulatory area between Northern Ireland and the EU, with the free movement of goods across the border.

The "territory of Northern Ireland" would be considered part of the customs territory of the EU.

The same day Theresa May told the House of Commons the EU’s draft would "undermine the UK common market and threaten the constitutional integrity of the United Kingdom," and that no British prime minister could ever agree to it.

On 7 June, the UK presented its alternative, the Temporary Customs Arrangement.

This was, according to the UK text, "a proposal for the customs element of the backstop that would apply to customs arrangements between the UK and EU and avoid a hard border between Northern Ireland and Ireland."

If ever the backstop was needed, "a temporary customs arrangement should exist between the UK and the EU."It would combine two territories: the EU’s customs territory, and the UK’s customs territory.

So the elements were clear enough. The EU wanted a Northern Ireland-specific backstop for both customs and the single market; London wanted a UK-wide backstop for customs only, and one that was temporary.

This was the divide that had to be bridged.

Michel Barnier with a copy of the draft Withdrawal Agreement

But the gravitational forces were formidable. Ireland wanted to be sure that whatever was agreed was "all-weather," lasting unless and until something better came along, and not subject to the slings and arrows of UK parliamentary or electoral politics.

Ireland also wanted the original backstop as the ultimate safety net, sitting underneath the UK’s customs idea. The UK wanted one single backstop.

London also wanted it to be invisible – all but inconceivable.In the twilight of the talks Dominic Raab wanted it to be time limited, with the UK keeping one hand on a pull-chord.

However, avoiding a hard border also meant removing those EU controls for industrial goods and agrifood products that ensured they complied with single market regulations.

But for London customs – the sovereign realm – was really the problem.

"There are lots of different ways you can package it on the regulatory side," said one British source several months back.

"Customs is binary. Either Northern Ireland is in the EU customs territory, or it’s in the same customs territory as the rest of the UK."

Having initially rejected the idea of a Temporary Customs Arrangement that would keep Northern Ireland in the UK’s customs territory, the Task Force eventually decided to contemplate it.

But it could not be in the Withdrawal Agreement, because that, according to Brussels, was the wrong legal base: having a customs union with the EU was a future issue, not a divorce issue.

One idea was to put it as a commitment in the Political Declaration that would accompany the treaty.

Out of the question, said Downing Street.

"If we sign up to a customs border down the Irish Sea with vague promises of flexiblity but with nothing in legal form," said one official, "then we’ve got nothing."

With October approaching, the EU moved.

The temporary UK-wide customs idea could be upgraded into the Withdrawal Agreement first as a reference in the preamble, then as a legally-binding promise, and later still as a customs union proper.

But the commission launched a simultaneous counter-measure to "de-dramatise" the backstop. In the event of it ever taking effect, any regulatory or customs checks on goods moving from Great Britian to Northern Ireland could be minimal.

Customs (as well as VAT and excise) would be electronically pre-cleared, with barcodes scanned on ferries or in transit (ie, Dublin Port).

Industrial goods could be checked "in market", ie on the factory floor or at a depot, and only on the basis of an empirical risk, defined by data monitoring.

Live animal controls would increase from 10% today to 100%, but later falling if a veterinary agreement was concluded between the EU and UK.

In the meantime, the commission began redrafting the text to make it more palatable to London.

Out went any references to Northern Ireland being in the EU’s customs territory.

The idea of the backstop being "superseded" by a future free trade deal was reinforced.

"There’s a lot of tinkering around with the text," said one official in September, "trying to make it less offensive. It’s more about language than a normal legal text. It’s all to do with signalling."

But as the talks intensified, what shape would the backstop take?Would it be UK-wide only?Would it be UK-wide combined with Northern Ireland-specific, or a hybrid of the two?

Dublin was insisting on at least a Northern Ireland-specific arrangement that had no expiry date, but one which would be temporary on the basis that all sides wanted something better to come along.

But the EU considered granting a joint customs territory as a key concession, even though Brussels would still insist on Northern Ireland staying aligned for goods regulations.

"However you name it," says one EU diplomat, "the big thing for the UK is to be able to say that Northern Ireland and Great Britain form part of the same customs territory. Yet, alignment [for single market rules] in the Northern Ireland part will have to be deeper than in the GB part."

In October, the European Council came and went with no breakthrough. The talks were deadlocked over backstop: how it would function, whether it would cascade, and if the UK could leave unilaterally.

But both teams returned to the tunnel after the summit, and the negotiations inched forwards.

In Westminster the cacophony continued.

Despite attempts to seal off the talks, cages were being rattled right, left and centre. Whatever was going on in the tunnel, the DUP and eurosceptics did not like it.

Leaked correspondence between Arlene Foster and Theresa May betrayed deep paranoia on the DUP side.

Yet progress was being made.

"The issues started to narrow down during the tunnel phase," says a senior diplomatic close to the negotiations.

"The architecture of the backstop, the issue of it being time-limited, and who made the decision to end it – in each of those a way through was found, but one which preserved the integrity of the EU’s position."

But it was slow progress.

Olly Robbins returned on Sunday 11 November. A deal would have to be done this week if there was going to be an emergency summit in November.

Officials were downbeat.

"There are still a lot of problematic things across the board, some small some big," said one on Monday morning.

"I don’t have my hopes up for a landing any time soon."

Among the big problematic things were the temporary customs union, and how to end it.

It was now safely inside the draft Irish Protocol as the pre-eminent backstop, from which all other issues on Ireland would flow. Yet, shoe-horning a customs union into a divorce treaty was a vast, complex legal undertaking.

It would also carry strict conditions, because it gave the UK tariff-, quota- and rules of origin-free access to the single market before a free trade agreement was even started.

"The problem is," said one EU source, "that you have to flesh out and negotiate the terms of this in some separate agreement, because there is no way that on the basis of a quick flourish, and simply saying ‘a customs union’ that all those goods are going to be allowed to flood into other member states if they haven’t signed up to EU rules."

Those rules were coming into shape.

The more the UK pushed the temporary customs union idea, the more member states insisted the UK would have to sign up to EU environmental, social, labour, competition and state aid law.

It was also up to London to do the spade-work on how this customs union would function legally.

That threatened to hold up the whole process.

"There’s no deal," said another senior EU official ruefully.

"It’s a mess in London. Brussels is waiting for a piece of paper some time in the second half of the week. There will be nothing tomorrow. I reckon there won’t be anything before Thursday and Friday."

In fact, negotiators were getting there.

By 9pm on Monday, the night of the Barnier-Coveney meeting, Sabine Weyand and Olly Robbins felt they had finally "stabilised" the text as far as they could.

Overnight the text went to London.

Theresa May had a cabinet meeting the next morning, but she did not reveal the news that the text had been stabilised the night before.

In fact, the fact a deal had been done remained a secret until RTÉ News broke the story at 3.55pm Irish time, pushing sterling up to $1.30.

"There is a text," a source told this correspondent around 3.45pm Brussels time, "but the problem now is that the politicians have to look at it."

The breakthrough was confirmed by a second source.

There would be one overall backstop, not two, but with deeper provisions for Northern Ireland on both the customs and the regulatory side.

And there would be no unilateral ending of the backstop: it would have to be agreed jointly by the EU and UK.

Within minutes of the RTÉ report, Downing Street announced a special cabinet meeting for Wednesday afternoon.

Theresa May began to hold one-on-one meetings with her ministers.

Irish officials held their breath, desparate to refrain from any sense of relief at best, or triumphalism at worst.

"She is meeting ministers individually," said one Dublin source, "but we don’t know are we going to see resignations, are we going to see acceptance in the UK.

"We really want to see can the prime minister land this tomorrow, and if she can it will be over to Barnier to do what he is instructed to do. Only then will the Irish government have something solid to say."

So cautious was Dublin that the Department of Foreign Affairs issued a statement late Tuesday aftternoon that "negotiations between the EU and UK on a Withdrawal Agreement are ongoing and have not concluded. Negotiators are still engaged and a number of issues are outstanding. We are not commenting further on leaks in the media."

In fact, the Irish Government had been appraised of the breakthrough the night before and Leo Varadkar had even briefed the Cabinet that a deal had been done on Tuesday morning.

So, when the 585-page draft treaty was published at 8.45pm Brussels time on Wednesday night, what did it say about the backstop?

At first glance, it was clear that a lot of drafting had been done to render the Irish Protocol less offensive to London.

There were numerous references to the constitutional integrity of the UK, and to the backstop being temporary and being superseded when appropriate.

Article 1 made it clear it was "without prejudice to the provisions of the [Good Friday]… Agreement regarding the constitutional status of Northern Ireland and the principle of consent," meaning "any change in that status can only be made with the consent of a majority of its people."

The text respected the "territorial integrity of the United Kingdom" and was designed to "address the unique circumstances on the island of Ireland, maintain the necessary conditions for continued North-South cooperation, avoid a hard border," and protect the GFA "in all its dimensions".

The objective was not to establish a "permanent relationship" between the EU and UK so the backstop would "apply only temporarily," and its provisions would apply "unless and until they are superseded, in whole or in part, by a subsequent agreement."

As reported on RTÉ the previous afternoon, there was indeed only one backstop – a UK-wide customs arrangement.

Until that future free trade deal came along, there would be "a single customs territory between the [European] Union and the United Kingdom.

Accordingly, Northern Ireland was in the "same customs territory as Great Britain."

This new customs territory would therefore be a combination of the EU’s customs territory, set out in EU law, and the UK’s customs territory.

But there were extremely important caveats to this one backstop.

There would be extra provisions for Northern Ireland so that it remained aligned to the rules of the single market, and while the North would be in the UK’s customs territory, it would be subject to what is called the Union Customs Code, ie the EU’s customs rule book.

Why was this?

A senior EU official explains that the current EU customs union is a combination of a basic customs union and a regulatory union (ie, where harmonised, common rules apply).

"The UK will only be part of the basic customs union, whereas Northern Ireland will be part of the basic customs union and the regulatory union to avoid a hard border," the official said.

So the extra provisions applying to Northern Ireland – but not to the rest of the UK – would include the regulation of industrial goods, the so-called sanitary and phytosanitary (SPS) rules for veterinary controls, rules on the production and marketing of certain agricultural products, VAT and excise, and state aid rules.

For that reason, it is clear that the UK in the negotiations conceded that there would have to be regulatory controls on goods going from Great Britain to Northern Ireland, because they would not be subject to the same EU regulations.

Here it seems London has largely accepted the "de-dramatised" controls long advocated by Michel Barnier.

What would then happen to a product being sent from Liverpool to Belfast?

Under the backstop, the "importer" would fill in a transit declaration in Liverpool.

If it was an industrial good there would be a barcode on the container to be scanned either on the ferry or at the transit port (ie, Dublin).

There would be checks at the destination in Northern Ireland, but these checks would not be systematic, in other words, they would only occur if a risk had been identified by the authorities.

The regulatory compliance checks, to ensure goods comply with EU safety standards would be done through "market surveillance authorities", for example consumer organisations that test for the safety of, say, lighters, at the company premises.

It looks like the only checks that would actually happen at ports and airports would be those SPS checks for live animals and animal products.

At present 10% of consignments are checked anyway, but this would rise to 100% in a backstop scenario, but, as mentioned, falling if the EU and UK can conclude a separate bilateral veterinary treaty.

Overall, one way of looking at it is that the EU customs union is a delivery system for the complete range of regulatory checks that come with the single market.

Since most of those rules will still apply in Northern Ireland under the backstop, then the Union Customs Code must also apply.

That, at least, is the legal logic of the treaty, but it will be politically very hard to swallow for the DUP.

Officials close to the negotiations insist that every effort has been made to reassure London and the DUP on two counts: that nothing here is a threat to the constitutional integrity of the UK, and secondly that both sides will make their "best endeavours" to ensure the backstop is not needed.

Part of that is the rendezvous clause which provides for a stocktaking in July 2020.

At that point we will be one year into the transition, and the free trade negotiations will have been under way.

Both sides will assess if that FTA is ready and fit for purpose – to ensure no hard border – and if not there will be two other options: an extension of the transition by an as yet indeterminate period, or the temporary customs backstop, with the extra provisions listed above.

This is intended to address Brexiteer worries that, with the backstop legally binding and in the Withdrawal Agreement, the EU will simply sit back and take their time in negotiating the future free trade deal in good faith.

Despite the carnage in London, both Brussels and Dublin entertain somewhat forlorn hopes that Tory hardliners and the DUP might give the treaty a second glance when the dust settles.

Statements of support from the Ulster Farmers Union and key manufacturing, retail and trade groups in Northern Ireland are raising hopes in Dublin that the DUP may reconsider its hardline stance.

In the meantime, Brussels is holding its breath and limiting its public pronouncements on what has been happening in London.

However, there is no appetite to reopen negotiations.

"There’s clearly an awareness that the situation in London is difficult and unclear," says one EU diplomat, with commendable understatement.

"The EU side are very conscious of the need to tread very carefully on this. No-one wants to distort or play into what is a difficult process in London.

"But at the same time the Withdrawal Agreement has been agreed between the two sets of negotiators, and there is absolutely no appetite to go back to that."

That is why the summit called for on Sunday 25 November will not be a negotiating summit.

It will provide Theresa May with the optics of support and endorsement, in the hope that this will start to turn the tide in favour of the treaty’s ultimate ratification in the House of Commons.

For now, that seems a remote prospect. But no one Tory candidate has put forward a credible alternative.

No Deal is seen as a hopeless option, beyond the most avid Brexiteers, with no Commons majority. Nor is there a majority for a second vote.

And the EU believe they have gone as far as they can.

They have agreed to shoehorn a customs union into a divorce, and next week the Political Declaration will spell out a free trade agreement that will look like no other.

Some EU diplomats, sitting back after 17 months of gruelling negotiation, and spellbinding turmoil in London, wonder how things got to this point.

"All in all," muses a diplomat from one member state, "I think it’s a fair and balanced deal and I really regret that it’s deteriorated into this Byzantine discussion about the backstop.

"No other trade agreement will ever be judged on these terms. Not in a million years."

More: Brexit stories