By Bridget Kendall
BBC Diplomatic correspondent, Moscow
When leaders from eight of the world's most powerful countries assemble, is it surprising that their gathering gets hijacked by events?
The G8 leaders often seemed to be pulling in opposite directions
But even by G8 standards, this year's summit seemed peculiarly sensitive to the current moment.
It was partly because the agenda set by the Russians was not driven by a set of clear targets, as it had been last year in Gleneagles.
The hype surrounding the Live 8 concerts that accompanied it raised public expectations. And finding the political will to meet goals on poverty, debt and trade gave the occasion real momentum.
But, as President Vladimir Putin made abundantly clear in his press briefings, Russia's aim during this G8 summit was more rhetorical.
The point of putting energy security top of the agenda was to require the Western world to examine the problem not just from their own viewpoint as energy consumers, but consider also the needs and concerns of those who produce the stuff and transport it.
It was also, of course, to highlight the fact that in this critical sphere of global activity Russia is a giant whose views matter.
So the drama of this summit was always going to derive from something else.
For a while it looked as though it might be the question of Russia's democratic credentials.
US President George W Bush made a point of hosting a meeting with human rights activists at the US consulate. Tony Blair's wife, Cherie Booth, slipped away from the spouses' programme of tea parties and craft fairs to demonstrate her support for some of Russia's beleaguered NGOs.
British officials revealed that the G8 leaders had conferred beforehand about how to raise their concerns with Mr Putin, and were planning to broach the subject privately at dinner on Sunday night.
Mr Putin appeared quite willing to cut Mr Bush down to size
In the event though, the scale of the crisis in the Middle East and its potential for spiralling into a regional conflagration overshadowed proceedings from the start.
Phone calls to Israel, Lebanon, Syria and Iran began while G8 leaders were still en route to St Petersburg.
From the moment they landed, the Middle East dominated talks around the margins of the summit, at meal times and during many of the formal sessions.
All felt an urgent need to curb the violence for the sake of innocent civilians and to stop the conflict spreading.
But this was also a test of G8 leadership.
If these seven men and one woman who represent such a large chunk of the global economy could not use their collective diplomatic clout to find a way out, what did that say about the relevance of such high-powered meetings for addressing global issues?
An intense drafting session did lead to a detailed statement which blamed unnamed extremists for starting the violence, called on Hezbollah to stop its rocket attacks and release the captured Israeli soldiers immediately, and only then for Israel to stop its bombardment.
Israel was also urged to exercise restraint. But there was no demand for an immediate ceasefire, and no criticism of Israel's response as disproportionate - something the Russians and French had been calling for.
Nor were Syria and Iran named as the backers of Hezbollah, something the American and British leaders had wanted spelt out.
The snatched aside between Bush and Blair revealed much
It was a fudged document, the compromises reached at the end of what seems to have been a difficult session, which ended in Vladimir Putin and Tony Blair sitting down together with the drafters to thrash out a final text.
Shortly afterwards, at a summit press conference, French President Jacques Chirac grandly announced a united position had been reached.
A Lebanese journalist stood up to ask him if Mr Chirac had made the case for an immediate ceasefire.
"Everyone in Lebanon is relying on you to speak for us," added the journalist.
Mr Chirac replied that "quite naturally" everyone wanted that and it was imperative to put an end to the bombing as soon as possible.
But it then emerged he had not seen the final wording. The document had papered over the cracks.
Differences of emphasis remained among the G8 leaders, it seemed.
'Arc of extremism'
Mr Putin revealed to the press why he had insisted Iran and Syria were not mentioned by name.
Partly, he said, because he wanted to keep all channels of communication open. He hinted that Russia was trying to use its influence to get a message to those holding the Israeli soldiers to ask them to release them.
But it also turned out that he had confronted Tony Blair with the so-called Zakayev case.
Vladimir Putin scored more points than most other leaders
He argued that that the G8 could hardly blame Syria and Iran for funding and backing Hezbollah and Hamas when, to Russian eyes, Britain too was harbouring a dangerous Chechen terrorist - Akhmed Zakayev, granted political asylum in the UK.
To blame one country but not the other, Mr Putin announced, would have been unfair. It was one of several barbed asides that Mr Putin made over the course of the weekend at his guests' expense, revealing the tensions that underpinned the gathering.
Perhaps most startling, though, was the joint position of Mr Bush and Mr Blair. Both were categorical that this was a highly dangerous moment for the Middle East and to call a truce would not solve the underlying problem.
In their eyes there was what Mr Blair called an "arc of extremism" reaching across Iraq and Lebanon to the Palestinian territories.
It was no coincidence that the unrest had erupted now, he declared at his final press conference.
It was, he claimed, part of a deliberate attempt to destabilise the region and undermine the still fragile attempts to install moderate democratic governments.
Israel had no option but to defend itself militarily. And Syria and Iran had to take some of the blame for what had happened.
It became clear that Mr Bush and Mr Blair saw this as part of a much wider struggle for the future direction of the Middle East, a high stakes battle which is linked to the violence in Iraq, the role of Syria in Lebanon, and the current dispute between Iran and UN Security Council members about whether it is building nuclear weapons.
As the summit drew to a close, the G8 leaders gathered for a final meal together, an elaborate lunch in one of the fine banquet halls in Mr Putin's restored Tsarist palace.
It was a relaxed occasion. Angela Merkel, the German Chancellor, had her laptop with her and was quietly checking her notes.
Romano Prodi, the Italian prime minister, was on the phone, absently-mindedly picking at morsels as he waited for his call to be answered.
Bush and Blair's supposedly private moment perfectly illustrated the abiding frustration and anxiety of these G8 leaders
Mr Blair wandered over to Mr Bush for a quiet chat. As both men munched on bread rolls, they chewed over the results of the summit.
What they did not realise was that the microphone next to them was switched on, and their words audible to the cameras that were filming in the hall.
Their conversation was snatched, about whether the US Secretary of State, Condoleezza Rice, would travel to the region.
Tony Blair offered to go first to lay the groundwork, so that she would not have to risk a high-profile trip that might end in failure.
Mr Bush did not immediately take him up on that. He was preoccupied with the problem of how to put pressure on the Syrian president.
His concern seemed to be whether UN Secretary General Kofi Annan would really push the case hard enough. At one point he showed a flash of anger.
It was a supposedly private moment. But it perfectly illustrated the abiding frustration and anxiety of these G8 leaders, cooped up in a Russian palace perched on the Gulf of Finland, as they contemplated from afar a crisis with potentially far reaching consequences, unfolding in a way they seemed unable to exert much influence over.