In short: EU article 50 is a timer. It can be cancelled and restarted any time. Just like the programming timers we know. So, the solution is exactly what we do in programming: cancel and restart.
On 4 December 2018, the responsible Advocate General to the ECJ published his preliminary opinion that "a country could unilaterally cancel its withdrawal from the EU should it wish to do so, by simple notice, prior to actual departure".
Meaning, that the UK can cancel Brexit any time. But the UK can also restart Brexit at any time again. There are no time limits, no grace periods, no rate limiting on article 50 cancelling and triggering.
That makes the solution obvious: send two letters to the EU back to back.
- the first with the cancellation of the article 50 process,
- the second with a new trigger of article 50.
The result would be (almost) the same as the already planned 2 year transition period. The March 2019 date is only the legal departure date. for all practical purposes, the UK leaves 2 years later after the transition period (December 2020). Let's get rid of the transition period and call it a second article 50 process, another two years. Practical separation would then be March 2021, only 3 months later than already planned.
Just make a law in parliament to cancel article 50 AND trigger it immediately again.
Gives you time for a referendum or a general election or just a good transition period. Anything you want. Gives you time and changes nothing. The move just gets rid of the threatening no-deal timer.
Bonus: an extension of article 50 requires the agreement of all EU members. But the cancel/restart-move can be done by the UK alone.
Bonus Bonus: 2 years transition is what the Brits wanted, but the EU wanted a clean end of year date. So they settled for 21 months. A simple timer reset makes that 24 months again. Owned!
The ECJ handed this solution to the UK. They know what they do. This was not an accident. They mean it. That's the way out. Just do what every programmer does with timers: reset.
Afterword: it's clearly an exploit and a bit shady. But it's legal. If people ever wanted to make exploit proof contracts and laws, then they could ask the gaming industry for advice. They have thousands of specialists who make a living by preventing the exploitation of coded rules. If computer games as an example are too low for you, then ask IT security experts or turn to academics in the field of game theory, the study of mathematical models of strategic interaction between rational decision-makers.