The day after parliament returned on 12 June, I was sitting with Andrew Fisher, Jeremy Corbyn’s director of policy and the author of Labour’s manifesto, in Portcullis House, where many MPs have their offices. We were interrupted by Karl Turner, the MP for Hull East. He bounded over to Fisher and shook his hand. “Well done!” he cried. “Well done! A lot of us are eating humble pie!”

The scene was particularly remarkable, as Turner had quit his role as shadow attorney general in 2016 in the mass shadow cabinet walkout over Corbyn’s leadership. But it wasn’t the only time this had happened to Fisher that day.

For the first time in at least five years, there is a feel-good factor among the professional ranks of the Labour Party. There is a particular glee to unexpected success, and the party went into the election expecting to be not only beaten but smashed. Instead, it has advanced. After the 2015 election, ­Labour needed to gain 94 seats to end up with a majority of one – better than any of its election performances other than the landslides of 1997 and 1945.

To take power, it needed to work out how to win either Kirkcaldy, which the SNP held with a majority of 9,974, or Kensington in west London, whose incumbent, Victoria Borwick, had a majority of more than 7,000.

In the event, Labour won both Kirkcaldy and Kensington. A Parliamentary Labour Party expecting to be shrunk has dozens of extra members. This is the first election in which Labour has gained seats since 1997. The picture is less 1997, more 1992 – a surprise result that leaves Labour tantalisingly close to power, needing just a 3.5 per cent swing to win a majority. But this time, Labour has the benefit that the surprise has injected optimism into the party, not plunged it into despair and recrimination.

No one working for Labour expected the exit poll that showed the party not only holding on but making advances. For the leader’s aides watching the results at Southside, Labour’s London headquarters, it was only when the result from North Swindon came in at midnight that they began to believe something remarkable was happening. The immediate result has been to unify the PLP behind Corbyn. One of the party’s most influential operators in the parallel whipping operation that Corbynsceptics have been running summed up the mood: “I’m a utilitarian. He’s shown he can win, and that’s enough for me.”

Although a few outspoken ultras – both Chris Leslie, the MP for Nottingham East, and John Spellar, the MP for Warley, criticised the result – will remain outside the tent, the Labour leader will enter this new period with a party united behind him.

Having expected to be in a fight for survival, Corbyn has a more congenial but even more difficult task: finishing the job and taking power at the next election, whenever it may be. That requires changes to how the campaign is run. “What is the point,” one senior trade union official texted me on the morning after polling day, “of spending our members’ time and money on a system that tells us we’re losing by 5,000 [in a seat] when we’re winning by 10,000?”

The leader’s office hopes that this failure will lead to greater support for the organisational review that is being conducted by Bob Kerslake, the former head of the civil service and cross-bench peer, into how Labour operates and organises.

He already reorganised the leader’s office last year. That reorganisation and the appointment of Karie Murphy as Corbyn’s chief of staff have been credited with sharpening the effectiveness of the operation. One friend says that Murphy would “pick a fight with her own reflection”, and another senior staffer describes her admiringly as a “battering ram”.

Part of Labour’s success this time came from expanding the electorate: more than 1.5 million people voted in the 2017 election who did not vote in 2015. Corbyn’s team hopes to build on this by running voter registration drives, particularly in areas where students might not be on the rolls.

The astonishing gain in Canterbury – a student-heavy seat that has been Conservative for longer than the Labour Party has been in existence – has already inspired former Corbynsceptics. “Look at Camborne, big campus there,” one New Labour grandee gushed. “Look at Loughborough, big campus there. We can do there what we did in Canterbury.”

Corbyn will also continue to maintain a permanent office at the party’s headquarters, a convention that was abandoned by Ed Miliband. By working alongside party staff, he has eased suspicions on both sides. “There are still structural problems,” one aide to the leader observes. “But I think what needs to change is how HQ operates, not who works there, for the most part.”

While Jeremy Hunt remains in place as Secretary of State for Health, the leader’s office believes that the NHS will stay a major election issue. It thinks that hospital cuts can be as effective a cudgel as school spending cuts were in this campaign. And it believes that the health issue will help Labour hold on to and extend the small gains that it made with older voters, though the assumption has to be that the Conservative Party will not fight such a helpfully maladroit campaign again.

Whoever leads the Conservatives into the next election – and the leader’s office assumes it will not be Theresa May – Lab­our’s settled view is that it will be Jeremy Corbyn, not a successor sharing his politics, who will face the next Tory prime minister. It’s not just Corbyn’s staffers who believe in his unique appeal now. One newly elected Blairite MP summed up the mood: “This wouldn’t have happened with another candidate. It was him.” What a difference an election makes.