On 26 July 2016, just over one month after the Brexit Referendum, Enda Kenny had lunch with British Prime Minister Theresa May in Downing Street. Afterwards, the former taoiseach said: "We are both agreed very firmly there will be no return to a hard border as existed."

In the months that followed, Dublin, London and Brussels re-emphasised that determination at every turn. Everyone would protect the peace process and avoid any return to the "borders of the past".

So how is it that the hard fought solution to preserve the peace should now be vilified as the "hated" backstop, a "trap" that would keep the UK in the EU "forever", that would "divide" the United Kingdom, and threaten to pitch all of Europe off a cliff edge into a potentially calamitous No Deal scenario?

"It is only on reading the Attorney General’s legal advice about the implications of the Northern Ireland 'backstop' in the Brexit deal," intoned the Daily Telegraph this week, "that the true enormity of what the government has agreed to becomes apparent."

To understand how we got here, we need to return to the Joint Report, published one year ago this week.

The Joint Report contains the embryo of the backstop, but as this blog has pointed out before, it held a fateful ambiguity.

Paragraph 49 of the report stated that, if no other solutions were to be found: "The United Kingdom will maintain full alignment with those rules of the Internal Market and the Customs Union which, now or in the future, support North-South cooperation, the all-island economy and the protection of the 1998 Agreement."

Was this the UK as a whole aligning? Or was it just the UK aligning on behalf of Northern Ireland?

To Dublin and the EU, it was clear. The backstop was for Northern Ireland only. However, to London the UK as a whole could "align" in a backstop scenario.

At the time, the ambiguity did not go unnoticed.

Listen: Brexit Republic podcast

The London Independent ventured: "The government has offered no detailed explanation of how it could simultaneously leave the single market and customs union and also sign up to UK-wide rules to prevent a hard border in Northern Ireland..."

Since Paragraph 50 of the Joint Report had promised unfettered trade between Northern Ireland and Great Britain, this seemed to suggest that the UK as a whole would be aligning. The London Times recognised that this was "contradictory…, given that Theresa May insisted that she would not sign up to UK-wide regulatory alignment with Brussels after Brexit".

David Davis, the then Brexit secretary, muddied the waters further when he told the House of Commons: "Alignment isn’t harmonisation, it isn’t having exactly the same rules. It is sometimes having mutually recognised rules, mutually recognised inspection, all of that sort of thing as well."

EU and Irish officials were left scratching their heads as to what he meant.

In any event, there was no great drama about the idea that the UK as a whole would "align" with the EU in a backstop scenario, if that was indeed what Paragraph 49 meant.

But there was a very discreet hint during Theresa May’s appearance before the House of Commons on 18 December.

Labour MP Stephen Timms crisply asked Mrs May if the "full alignment with those rules of the internal market and the customs union" would apply "just to Northern Ireland, or to the UK as a whole?"

The Prime Minister repeated her belief that a hard border could be avoided through the overall relationship, or specific solutions (technology) or "failing that, we will move to the concept of full alignment, which is about having the same objectives on both sides".

She added that the Joint Report made it clear how it would operate: "It could be UK-wide or, with the agreement of the Northern Ireland Executive and Assembly, it could be specific to Northern Ireland."

Was Mrs May hedging her bets? Either way, the House did not erupt in fury, and all sides retired for Christmas.

On 27 February the European Commission published the first draft of the Withdrawal Agreement, setting out how a Northern Ireland-specific backstop would work.

It was immediately rejected by Theresa May and the DUP.

It was at this point that British negotiators turned the ambiguity of Paragraph 49 into an opportunity.

Could the wording that "the UK will align…" be used for customs, but not for the single market?

"There are already regulatory differences between GB and Northern Ireland," said one senior British source, "so some of that will be acceptable. But you can’t put a customs border down the Irish Sea."

According to two sources close to the negotiations, London ran the idea of a UK-wide solution to the backstop past Dublin first, before putting it to the European Commission.

Dublin was open to the idea. Ireland still disputed London’s interpretation of Paragraph 49 (if it had intended to be a UK-wide entity, then what was the use of Paragraph 50, spelling out unfettered trade between Northern Ireland and Great Britain?).

But Irish officials acknowledged that it provided Theresa May’s chief negotiator Olly Robbins some cover back in December in explaining why she had signed up to the Joint Report. Furthermore, anything that kept the UK in a close customs relationship with the EU would be no bad thing, given the €65bn in two-way annual trade across the Irish Sea.

"[Paragraph 49] was definitely not written to be open to interpretation that it could be a UK-wide customs union," insists one Irish source.

"It quickly became apparent there was an ambiguity there, and we thought well, we know what you meant, and either way it lands we’re okay with it. Because the point is, it talks about the rules of the Single Market and Customs Union, and if the whole of the UK wants to do that - great."

According to a senior British source, the European Commission was also open to the idea. "We were thinking," the source recalls, "can we work out a solution based on the UK-wide customs area? And they said yes. They thought it could work."

The period in question - March of this year - was a difficult one. The vehemence of Theresa May’s rejection of the first draft of the Irish Protocol took Dublin by surprise, and prompted fears that London was backsliding from the December commitments.

On 22 March a European Council was due to grant the UK a two-year transition period that would begin from the end of March next year. British officials were frantic that Leo Varadkar would hold that up if the Prime Minister did not re-commit to the backstop.

Over an intense 10-day period there was three-way contact between Dublin, London and Brussels. Officials ground out a form of words that would get everyone off the hook.

"It became clear that that was what Varadkar wanted," says one UK source, "some very clear reassurances that there was still going to be a plan C [the backstop], a work programme with clear re-commitments to what we said in December."

London argued that Theresa May was not objecting to the backstop per se, just the way it was spelled out in the first draft of the Protocol. "The PM was very comfortable in giving those commitments, because they weren’t for her fundamentally different from what she had said in December."

Mrs May duly wrote to Donald Tusk, the European Council President, promising she was "committed to agreeing in the Withdrawal Agreement operational legal text for at least the so-called 'backstop option' set out in the Joint Report, in parallel with discussions of these other scenarios".

A breakdown was averted, and the pathway was opened up for intense negotiations on the backstop ahead of the June European Council.

But the May letter didn’t mention the UK-wide customs idea. British officials say when they put they idea to Michel Barnier that March, he was initially open to the offer. Things however went cold once "the lawyers got at it".

Although the idea was the brainchild of Olly Robbins, Theresa May was instantly enthusiastic. In fact, senior members of her team had been talking about a UK-wide customs solution for months, in terms that are remarkably similar to those which have ended up in the final Withdrawal Agreement.

"It came out of a meeting many, many months ago," recalls one source who was present at the meeting,

"There was a whole bunch of people in the room [including Jeremy Heywood, the late Cabinet Secretary]. This is what the prime minister wants to do, they were told. You would have all the benefits of the customs union without any of the downsides.

"Everyone around the room said it couldn’t be done. But then at some point, people said, why can’t we be acting [together] as two customs territories? Why can’t we have the best of both worlds?

"Theresa May was automatically in favour of it. All of us were."

The first iteration was in the August 2017 paper on customs, which proposed a new "customs partnership with the EU, aligning our approach to the customs border in a way that removes the need for a UK-EU customs border".

One option was for the UK to "mirror" the EU’s tariff schedule for imports from the rest of the world where their final destination was the EU. The paper admitted it would be "unprecedented" and "challenging to implement".

It would take a further 10 months, following the drama around the Joint Report in December, before the idea crystallised into the UK’s formal response to the EU’s version of the backstop.

This time London was explicit. The Temporary Customs Arrangement (TCA), published on 7 June, was entirely faithful to the language in Paragraph 49 of the Joint Report.

"The UK's proposal is that in the circumstances in which the backstop is agreed to apply, a temporary customs arrangement should exist between the UK and the EU," the paper said.

It would have to be time-limited, and the UK would have discretion to negotiate and ratify its own global trade deals in some areas. The paper suggested that the draft Withdrawal Agreement text could be changed to read: "the territory of the United Kingdom …shall be considered to be part of the customs territory of the [European] Union."

Michel Barnier was cool on the idea. EU officials said it was full of contradictions. But Barnier did not close the door completely. A UK-wide customs arrangement was, he said, "not necessarily feasible". But that wasn’t an outright rejection.

The following month, July, was a torrid one for Theresa May. She was barely clinging on week to week. On 4 July the Chequers White Paper further elaborated on the UK-wide customs idea, but it was more memorable for triggering the resignations of Boris Johnson and David Davis.

On 16 July the ERG sponsored an amendment, backed by Labour’s Kate Hoey and the DUP, to the government’s trade bill which effectively rendered a customs border on the Irish Sea illegal.

By this stage the EU’s Northern Ireland-specific backstop was still the only serious option on the table. But the clock was ticking. The growing clamour against the backstop in Westminster was prompting a rethink in Brussels.

In September, Northern Ireland Executive officials, on a visit to Brussels, got the distinct impression from the EU Task Force that they were more acutely aware of how difficult a customs barrier along the Irish Sea was for both the Conservative Party and the DUP.

In September, London pushed harder on the UK-wide customs arrangement as a way of avoiding that Irish Sea customs border.

The background was one of growing anxiety. The Salzburg summit turned into a bad-tempered train-wreck, with Theresa May declaring she had not been "respected" by the EU after Donald Tusk rejected key elements of the Chequers paper and posted a supposedly facetious picture on Instagram of Theresa May next to a cake stand.

Because of the increasingly poisonous atmosphere, EU and UK negotiators created the so-called "tunnel", a secretive and tightly control negotiating space in which ideas could be thrashed out without terminally damaging leaks.

The first tunnel-based bid for a deal ahead of the October European Council fell short.

But the EU was now engaging in the UK-wide customs idea, firstly as something that would be in the Political Declaration, then as a promise in the preamble to the treaty.

But London desperately wanted it to be given full legal weight within the Withdrawal Agreement. "It will only work if there’s a UK-wide customs arrangement," said one British source.

"It can’t be Northern Ireland in a separate customs territory to the rest of the UK. That pivots it to UK-wide customs - we don’t really have a plan B on that."

But it was the "temporary" nature of the UK-wide customs arrangement that was causing the hold up.

Dominic Raab, the former Brexit secretary, was insisting on it being time-limited. Dublin said that was impossible. How could it be a backstop if the UK could pull the plug? It had to be there "unless and until" a better solution to ensure no hard border was found.

It was at some point between the first and second "tunnel" that a number of critical changes happened.

Firstly, both sides came to an agreement that there could be a termination to the backstop, but it would have to be mutual, and not unilateral.

But another shift was in the way the EU interpreted and applied Article 50.

The EU has always regarded Article 50 and the Withdrawal Agreement as having three key elements.

The first is to wind down the UK’s membership. On that score, the financial settlement was the most difficult part.

The second is to deal with the "enduring commitments" the UK had made as an EU member, such as how EU citizens in the UK are treated, and how the Good Friday Agreement is protected.

The third is to provide a bridge to the future relationship.

The reality was, from the EU’s point of view, that the UK-wide Temporary Customs Arrangement (TCA) didn’t fit into any of those three boxes.

"So long as they were saying we want this TCA that we can unilaterally dump whenever we want," explains one EU source, "it can never be seen as a bridge to the future. It certainly wasn’t a wind-down of membership. So it didn’t meet the criteria. So the Task Force view was, you can’t really do this under Article 50".

At a key moment, and most likely under the pressure of the clock ticking, Theresa May accepted that the UK-wide customs arrangement could not be time-limited.

It would be temporary, since no one regarded it as a permanent or even an ideal solution. But at this point, in order to re-open a second tunnel, the UK had to make a painful adjustment.

Since the concept of temporary did not fit with the three-part Article 50 structure, London had to qualify this as being temporary only insofar as it was going to be "built on".

"The turning point," continues the source, "was the acceptance by the EU side that while it’s temporary, it’s only temporary because it’s going to be built on. So it’s a starter home. On the UK side they accepted that, and they used the term 'to build on'. It’s a baseline. Once they said this was the baseline for the future relationship, then the Task Force said, ok we can do it under Article 50".

According to the source, the Task Force wanted to be clear that the UK team was fully aware of what they were signing up for: shoe-horning a customs union into the Withdrawal Agreement at the last minute would trigger a deluge of "level-playing field" demands from member states.

If the UK was suddenly going to get tariff-, quota- and rules of origin-free access to the single market, then it would have to sign up to a range of EU rules on labour rights, environmental protections, competition and state aid, and taxation.

The UK side agreed, and they agreed with Theresa May’s blessing. She could see the advantage in having tariff- and quota-free access that was now legally guaranteed ahead of the start of trade talks.

"It was willingly adopted by negotiators," says the source, "and it had to get political buy in from the UK via the Joint Political Declaration and have it expressly articulated there."

Indeed, the Political Declaration is littered with the term "build".

Article 23 declares that the future economic partnership should have: "Ambitious customs arrangements that… build and improve on the single customs territory provided for in the Withdrawal Agreement..."

This shows rather graphically how the UK-wide customs arrangement, once seen as a handy way around a Northern Ireland-specific backstop, has opened up a rather different vista on the UK’s future relationship.

Once agreed by British negotiators, things happened quickly. The European Commission had to frantically assess all the level-playing field issues, in conjunction with member states, while Olly Robbins and his team had to go back to Whitehall to draw up a detailed and legally watertight temporary customs union (with technical support from the European Commission).

But some member states were alarmed that too much was being conceded. In particular, a number of countries were concerned that nothing was being extracted on access to UK waters for European fishing fleets.

Michel Barnier most likely calculated that fisheries had to be kept out of the level-playing field provisions in the Withdrawal Agreement. Since Theresa May was relying on 13 fisheries-minded Scottish Conservative MPs to help get the deal through, it was seen as political to defer that to the Political Declaration.

But in response to member states’ complaints, Michel Barnier’s deputy Sabine Weyand explained that, on the level playing field issues - including fisheries - the Withdrawal Agreement was a starting point.

Therefore, the EU was not playing its best hand too soon.

The key date for that starting point would be July 2020. The Withdrawal Agreement says that on that date both sides would address a host of issues.

The most important issue would be the backstop. Since officials would have by then spent over a year negotiating the future free trade relationship, both sides would decide if that was ready and if it had the effect of creating no harder border.

If not would the transition period be extended for one or two years? Or would the backstop - the UK-wide customs arrangement, with special provisions for Northern Ireland - then kick in?

"But you’re going to have a whole load of other things coming together," said an EU source, "the decision on a backstop or an extension, or a future relationship, but also data protection, fish, financial services, decisions on state aid."

This is where it gets complicated for London.

What the UK has negotiated in terms of the backstop is a basic customs union. That will deliver no tariffs, no quotas, no rules of origin.

But what a basic customs union doesn’t have, but EU membership does have, is trade facilitation.

What will this mean in practical terms?

It is best articulated by way of what’s not (yet) on offer for the UK, rather than what is on offer.

"What we haven’t said," explains one source, "is that we will judge the UK to be low risk, and therefore will only check 50% of their poultry. What is not being said is that we will have a system of mutually trusted traders.

"None of that is there. It will never be frictionless [as the UK has chosen to leave the Single Market]. Right now it will be full of friction. So it’s going to land somewhere between the two. So even with the customs model as currently articulated, you’ll have big lines of trucks and delays and all of that."

So, this is the dilemma the UK is facing. Theresa May has all along been desperate to reassure the business sector, in particular Japanese car manufacturers, that there would be frictionless trade post-Brexit.

While the UK will not be entitled to completely frictionless trade, because of the red lines on exiting the Single Market and Customs Union, there is the tantalising glimpse of trade that is as frictionless as possible delivered through the future relationship.

The UK-wide customs union does not get Britain to that point, but it is something to "build" on to get as close to that point as possible.

The UK will therefore have to negotiate the trade facilitation aspects that come with the EU’s full customs union.

The first incentive is that the closer London gets to that level of integration with the EU’s orbit, the less likely it is for the backstop to take effect.

The second incentive is that it will make trade with the EU - especially the just-in-time supply chain models that have evolved for the automotive and food sectors - that bit more frictionless.

"It will never be completely frictionless because they’re separate," says the source. "You’ll always have to do the paperwork, you’ll always have to have your customs declaration form. You’ll still have to have certain checks, SPS [sanitary and phytosanitary], food and all of that.

"But, for the vast majority of trucks, if you have a fully functioning customs union, only a tiny percentage of them will have to be checked. Every single truck going from the UK to the Netherlands will have customs formalities, but actually stopping the truck when you get off the boat will not be necessary in the vast number of cases where you have a fully developed customs union.

"We don’t have that right now. What we have right now is a very bare bones thing."

What all this reveals is the logic to Theresa May’s Withdrawal Agreement.

It tries to avoid a hard border on the island of Ireland, and tries to avoid a customs border on the Irish Sea. It tries to facilitate a significant section of the UK economy which depends on frictionless, just-in-time trade with Europe.

But it can be argued that the logic has been entirely hidden from her own party and drowned out by the cacophony of "taking back control".

And when Geoffrey Cox revealed all of this, the House of Commons was either too unprepared - or too blind - to understand that this was the plan all along.

The irony is that the UK-wide customs arrangement is almost universally loathed as something the EU is forcing on the UK, when in fact it was London’s invention, and one that it pushed to keep the DUP on board.

It was also ultimately designed out of self interest of an economy which exports heavily to the EU single market.

Downing Street is now in extremis in trying to keep the Withdrawal Agreement alive. There has been outreach across the Commons and across the United Kingdom, to little avail.

There is now a bizarre alliance of the Labour Party, the DUP and the hardline European Research Group attacking the Irish Protocol.

The one constituency in which Theresa May’s ill-fated deal has been going down the best, according to a UK source, is in Northern Ireland.