Barack Obama arrives in Iraq just as the political situation there is turning in his favour. The Iraqi government is for the first time asking for a timetable for a military withdrawal of United States forces.
This is in keeping with the Democratic presidential nominee's plan for a pull-out of American combat troops over 16 months and makes the strategy of his Republican rival, John McCain, to retain US troops in Iraq until all America's opponents are vanquished, look out of date.
Mr Obama, like other official visitors to Baghdad, will not see anything of Iraq outside the Green Zone or heavily protected US bases. But he will learn that the political landscape of Iraq has changed considerably over the past six months, though not necessarily to the advantage of the US.
The most important development is that the Iraqi state has become stronger. Its security forces number more than half-a-million men. Thanks to the soaring price of crude, its oil revenues could total $150bn (£75bn) next year. In a series of offensives between March and May, it regained control of territory previously controlled by the Shia militia, the Mehdi Army, in Basra, Sadr City, in Baghdad, and Amara province the south.
The success is not as complete as the government says. None of these military successes would have been achieved without US military support. And the Mehdi Army militiamen did not lose the battle, but were ordered off the street by their leader, the anti-American Shia cleric Muqtada al-Sadr.
However, even if government claims to have won a military victory are overblown, its recent successes have important political consequences for itself as well as for Mr Obama and Mr McCain. The Iraqi Prime Minister, Nouri al-Maliki is confident, even over-confident, according to his allies. "Before the government offensive in Basra in late March most of the political parties in the government had agreed that Maliki had to go because he was ineffective," said one Iraqi political commentator. "But since the attack succeeded, Maliki has become so arrogant that he has stopped paying attention to anybody outside his small inner circle."
Mr Maliki still feels he needs the Americans. The Iraqi army had been fought to a standstill by the Mehdi Army before it was supported by US firepower. But the Iraqi army feels less dependent on the US than it did.
As sectarian strife between Sunni and Shia Arabs ebbs, Iraqi nationalism is reborn. Underestimating these developments, the US was caught by surprise in June by the backlash against a new US-Iraqi agreement defining American military rights in Iraq and limiting Iraqi sovereignty.
The biggest American mistake in Iraq since it overthrew Saddam Hussein with such ease in 2003 has been to assume that US actions alone determine what will happen. This ignores the fact that the US is just is one of a dozen powerful players whom Mr Obama or Mr McCain, whoever becomes president, will be unable to ignore. If, for instance, the Shia Grand Ayatollah Ali al-Sistani were to demand a US withdrawal then America would have to go, regardless of who wins the presidential election.
Mr Maliki dropped the Status of Forces Agreement with the US because the political wind had set against it in Baghdad. He also saw the advantage of becoming an Iraqi nationalist standard-bearer in the run-up to the Iraqi elections at the end of the year. Despite his outward confidence he is not quite sure of surviving without US support. Some Iraqi politicians believe that if Mr Sadr ordered his militiamen back on to the streets they could take half of Baghdad in 48 hours' fighting.
Iraqis often say that things are "better" in Baghdad but usually they mean better than the bloodbath of 2005-07. Some 448 civilians were killed in June compared to 3,000 a month during the worst of the sectarian civil war. One in five of the 30 million Iraqis is a refugee and electricity supply is worse.
But for the moment at least events have played into Mr Obama's hands.
Not only is the Iraqi government asking for a US timetable for a withdrawal, but the Bush administration has started the first tentative negotiations with Iran. This should make it impossible for Mr McCain to portray the Democratic candidate as betraying the US by advocating talks with Tehran. The less Iran is threatened by the US the less incentive it has to keep the crisis in Iraq boiling. Mr Obama can claim that a US withdrawal in the short term is not just necessary but inevitable.