WHEN General David Petraeus, now America's most celebrated military commander, arrived in Iraq in 2003 at the head of an airborne division, he asked a journalist: “Tell me how this ends?” For years nobody had a good answer. But now, thanks to a military pact between America and Iraq, a conclusion is in sight: America's war in Iraq will end in three years' time, with American troops being shown the door and Iraqi politicians competing to claim credit for getting rid of the foreigners.

A “withdrawal agreement” approved by the Iraqi cabinet on November 16th requires American troops to pull out of Iraqi towns and cities by the end of June next year, and to leave Iraq altogether by December 31st 2011. Those deadlines, said Iraq's prime minister, Nuri al-Maliki, in a televised address, would not be extended. The deal was not perfect, but marked “a solid start for Iraq to regain its full sovereignty in three years.”

There were no secret articles, he said, and there would be no permanent American bases. Iraq could not be used to attack others (ie, Syria or Iran). There would be, he promised, “no detainees any more, no detention centres any more, no searches or raids of buildings or houses, until there is an Iraqi judicial warrant and it is fully co-ordinated with the Iraqi government.”

This is a big moment for America and Iraq, yet the Iraqi government was more regretful than jubilant, calling the deal the best it could achieve after more than a year of negotiations. The Bush administration, now in its last weeks in power, made several concessions. It had long opposed any notion of a fixed timetable for withdrawal, saying any troop pull-out had to be based on security conditions on the ground. The White House said the new deadlines were “aspirational”, but the text leaves less wiggle-room; clauses allowing for a review of the deadline, and the possibility that some American troops would stay on to train and support Iraqi forces, have been deleted. Security has improved markedly. But the political context has also shifted against the Bush administration—and the Iraqis have got their timetable.

In America, Ike Skelton, chairman of the House armed services committee, a Democrat, said he was worried by provisions that could result in American troops facing prosecution in Iraqi courts. But the text suggests that this is a remote possibility. Iraq has legal jurisdiction over American troops only in cases of “major and intentional crimes”, and even then only when they are outside their bases and off-duty.

The agreement should make life easier for Barack Obama, although there is some dispute as to whether it will have to be ratified by the American Congress. The deal supports the president-elect's principle of a firm timetable for leaving Iraq, but allows him to draw out the process beyond the 16-month withdrawal he promised in his campaign. Admiral Mike Mullen, chairman of the joint chiefs of staff, said America had so much equipment that it would take two or three years to withdraw completely—security conditions permitting.

The agreement was strongly endorsed by the Iraqi cabinet but its passage through the Iraqi parliament, which could vote on it as early as November 24th, is less assured. There were brawls among the lawmakers when it was discussed on November 19th.

In a country with a history of intense opposition to military pacts with an invading power, the deal could polarise opinion. Followers of Muqtada al-Sadr, the radical Shia cleric who heads a powerful (though now dormant) private army, oppose the treaty and demand an immediate withdrawal of foreign troops. Ayatollah Ali al-Sistani, Iraq's most senior Shia cleric, says any agreement had to restore Iraqi sovereignty and win “national consensus”. But he did not oppose it, which supporters take as tacit assent.

Some Sunnis want the agreement to be put to a referendum. Another contentious issue is the status of some 17,000 (mostly Sunni) prisoners in American hands. The Iraqi government is due to take responsibility for them, but some Sunni leaders want them all released.

Iran is equivocal. The speaker of its parliament, Ali Larijani, said America was seeking to turn Iraq into a vassal. But the leader of Iran's judiciary, Ayatollah Mahmud Hashemi Shahrudi, said the Iraqi government “has done very well”. Some interpret this as Iranian endorsement. But like so much else in Iran, it is hard to pick out a genuine signal from the political noise.