For the past four years, the imposing São Bento palace, seat of Portugal’s parliament, has been the scene of one of the more unlikely feats of European governance.

While populist parties have erupted at both ends of the political spectrum elsewhere across the continent, Portugal has proved an enduring, if improbable, beacon of social democracy.

Although the rightwing Portugal Ahead coalition, led by pro-austerity then prime minister Pedro Passos Coelho, took the most votes in the October 2015 general election, its minority government lasted a mere 11 days.

It was toppled by the socialist leader, António Costa, thanks to a promise to “turn the page on austerity” and a jury-rigged, non-coalition alliance with the far-left.

Since then, Costa and his backers in the Left Bloc and Communist parties have confounded expectations by holding together their unprecedented deal, known as the geringonça – or improvised solution.

They have also brought political and economic stability to a country that received a €78bn bailout from the European Union and the International Monetary Fund during the financial crisis eight years ago.

As Portugal prepares to vote in Sunday’s general election, Costa is hoping that his track record will win him another term in office.

Portuguese party leaders chat before ta televised election debate in September. Photograph: Armando Franca/AP

The polls suggest his Socialist party (PS) will win the election but fall short of an absolute majority in parliament.

Speaking in Coimbra this week, Costa said his party had “torn down the wall of incommunicability” between the Portuguese left and paved the way for the country’s recovery.

Without the PS government, he added, Portugal would not have managed to “turn the page on austerity, regain international credibility, maintain investor confidence, reduce inequalities and balance the books”.

Costa has argued that a “strong PS” is needed to keep things on track and has warned that a surge in support for the Left Bloc could lead to the kind of impasse seen in Spain, where the socialist party’s inability to reach a deal with the far-left, anti-austerity Unidas Podemos has triggered the country’s fourth election in as many years.

André Azevedo Alves, a political scientist at the Catholic University of Portugal and St Mary’s University, London, said Costa’s main domestic achievement has been to deliver four years of relatively stable government.

“Most people didn’t think the government would last more than one or two years,” he said.

“Political stability is one of the big factors that Costa and the Socialists have on their side. There was also a very high level of scepticism about a socialist government, with parliamentary support from the far left, being able to comply with the public finance objectives established by the European Union.”

Costa’s other successes include raising the minimum wage to €600 a month, reversing civil service cuts and reducing unemployment.

Externally, added Azevedo Alves, Costa has been able to keep the traditional social democratic party as the main party “at a time when the traditional centre-left parties are in deep trouble – or, in some places, have almost disappeared”.

Despite all the talk of the “Portuguese miracle”, however, Costa’s administration has hardly been immune to criticism, mistakes, scandal or corruption.

Socialist party leader António Costa during a political rally for the 2015 elections, after which he eventually took power. Photograph: Andre Kosters/EPA

Marina Costa Lobo, a political scientist at the University of Lisbon, pointed out that the country’s much-vaunted economic stability has come at a price: “[Costa’s] achievements on anti-austerity were achieved through pragmatism, which meant that public investment has decreased dramatically, with resulting difficulties in key areas of public policy like health, transport and others.”

Underfunding of public services such as education and healthcare has provoked strikes and anger, while the government’s handling of the 2017 forest fires that killed more than 100 people was bitterly criticised and led to the resignation of the interior minister.

Meanwhile, Costa’s former defence minister, José Azeredo Lopes, is to be tried over his alleged role in a suspected cover-up of a weapons theft at a military base two years ago.

Perhaps most damaging of all though, are the corruption charges faced by José Sócrates, Portugal’s socialist prime minister from 2005 to 2011.

He is accused of pocketing €34m through top-level corruption and has been charged with corruption in public office, money laundering, forgery and tax fraud.

Sócrates has denied any wrongdoing, but corruption remains an issue for many Portuguese people. A recent survey by the European Council on Foreign Relations found that 64% of those polled see it as a significant problem for Portugal.

Former prime minister José Sócrates’ corruption charges have marred the Socialist party. Photograph: Reuters

“Large segments of the electorate would regard Costa as skilled and politically able but perhaps not necessarily the most trustworthy leader,” said Azevedo Alves.

“That explains the socialists’ substantial lead in the polls but their lack of a clear majority in parliament.”

If, as expected, a majority eludes the socialists, they could reactivate the geringonça, again relying on the support of the Left Bloc and Communist party from outside government.

But if the PS finds itself closer to a majority than had been anticipated, it could instead turn to the small but growing green People-Animals-Nature (PAN) party, which is less ideologically fixed than the far-left groups.

Whatever happens, though, and despite the protests and scandals, Costa and his allies have shown that the geringonça has evolved from being a punchline to a resilient and effective political machine.

“It has worked well; it has produced political stability,” said Costa Lobo. “Economics trumps policy overall and ensures the PS will win.”

Azevedo Alves also sees a definite shift in the public mood. “In 2015, people were thinking we’ll be bankrupt again a year from now,” he said.

“I think there’s an idea within a large sector of the electorate that things haven’t been wonderful but they have gone pretty well. It has certainly all been above most people’s expectations.”