At dinner in Washington last month with a small group of intense types concerned with Asia policy matters, I asked whether they were worried about the situation on the Korean peninsula. “We’re worried,” was the reply. In January, when a direct strike on North Korean nuclear facilities was on the table, one explained: “That’s the only thing people were talking about.”

Now the talk is about a summit between North Korean leader Kim Jong-un and US President Donald Trump – equally unthinkable just a few months ago, but more reassuring, and a sign of how North Korea’s diplomacy has always been more flexible than many have realised. “Little Rocket Man” Kim may be, but he’s a master of diplomatic ju-jitsu. Two years ago, neither Beijing nor Washington would pick up the phone to him. Now he gets his own armoured train ride to see President Xi Jinping, ahead of a meeting with the world’s most powerful man in May (maybe).

Yet anyone who’s observed North Korea’s behaviour over the years won’t be surprised by the turnaround. The conventional wisdom on the Kim regime was that it was unpredictable and downright crazy. But that hasn’t ever really been true. Ruthless, and utterly contemptuous of individual human rights, certainly. But the Kim family has always had its sights set on survival first and foremost. Right now, it has the two countries most able to give it what it wants just where it wants them.

Why Kim’s China visit raises – and dashes – hopes for Trump summit

In 1950, Kim’s grandfather, Kim Il-sung, wanted to reunite the Korean peninsula, as he had control only of the northern part. Rather than undertake a risky solo venture, he played on his relationship with Stalin and Mao, effectively daring them to support him in a war.

Mao was hesitant. His Communist Party had won power in China only a few months earlier, and he was keener to regroup and consolidate his young regime at home. But Kim challenged Mao, with Stalin’s blessing, to show that he really was a leader of a new socialist world. With some trepidation, Mao provided “volunteers” (actually Chinese troops) to help Kim in his assault on the south. Even though they were eventually beaten back, Kim’s regime might not have survived without his Chinese helpers. The pattern was set for a relatively weak regime being able to pressure its ally into helping it to get what it wanted.

A similar pattern has emerged in the past few years. Beijing has been infuriated by North Korea’s unwillingness to listen to its bigger brother, which is almost as unhappy as the US at Pyongyang’s nuclear weapons programme. Less than three years ago, in September 2015, Xi presided over a huge military parade in Tiananmen Square at which the president of South Korea was an honoured guest and Kim’s invitation appeared to have been lost in the post. However, China’s official position, expressed repeatedly through its diplomats, is that the resolution of the issue is a problem for the US and North Korea to sort out alone; any thought that Beijing could help out is misplaced and fails to understand that Beijing has no real control over its troublesome neighbour.

Unfortunately for Beijing, its bluff was finally called by Trump’s erratic diplomacy – no other president would have asked for a direct meeting with Kim (any more than any other president would have compared the relative size of their nuclear buttons). Beijing has surely become alarmed that Kim might cut a direct deal with Washington and cut it out of the negotiations – its sudden enthusiasm to host him last week suggests it wants to remind Kim that any deal needs to go through it first.

Yet this needn’t be a problem for Kim. After all, what he really wants is not nuclear weapons, though he certainly won’t give them up in a hurry; it is regime security. An internationally agreed treaty between the six powers that negotiated the issue in the 1990s (the US, Russia, China, Japan and the two Koreas) could persuade Kim to end his nuclear programme, or at least the long-range part, which is what Trump cares most about.

China would be very pleased with an agreement that guaranteed the North’s long-term survival, as its greatest fear is not a nuclear neighbour, but a Korea reunified on southern terms that would allow US troops to stand on the Chinese border for the first time since 1949.

Why the Trump-Kim meeting hinges on China

Opinion polls in South Korea suggest that the younger generation is less than keen on reunification. If the North stays separate and leaves them alone, many of them can live with that. That leaves the unfortunate population of North Korea stuck for the long term with a murderous and oppressive regime.

But in recent years, the international community has lost its enthusiasm for spreading human rights and democracy. A grand bargain on the Korean peninsula could yet demonstrate that the Kim regime has lost none of its ability to appear irrational in service of cold, grim survival.

Rana Mitter is director of the University China Centre at the University of Oxford and author of ‘A Bitter Revolution: China’s Struggle with the Modern World’ and ‘China’s War with Japan, 1937-45: The Struggle for Survival’