The US and Afghanistan governments have reached terms on a deal that would ensure an American presence in the war-ravaged country beyond the formal 2014 end of combat.

US secretary of state John Kerry, who had been in difficult long-distance negotiations with Afghan president Hamid Karzai throughout the week, announced the agreement in Washington on Wednesday, ahead of a large-scale national gathering in Kabul expected to give Karzai wider political cover for agreeing to a residual US presence in Afghanistan.

The deal, known as the Bilateral Security Agreement, ensures that a rump US force will continue to train, advise and equip the roughly 350,000 Afghan soldiers, police and airmen that it has assembled almost from scratch over the last half decade.

A draft accord published on the Afghan government’s website holds that the US shall not conduct any combat operations “unless otherwise mutually agreed”, but appears to give them relatively wide leeway for the counter-terror missions that are a priority for Washington.

US commanders and politicians have said their forces will try to tackle al-Qaida but only train and equip Afghans to fight the Taliban, leaving them to fight alone after 2014.

The final deal came only hours before around 3,000 elders, power brokers and other delegates from around the country meet to discuss the deal and ultimately vote on whether Karzai should sign it. The hand-picked representatives are widely expected to approve the agreement, after airing opposition views.

The agreement paves the way for the longest US conflict in history – one launched in the weeks after September 11 and which has claimed 2,153 US military lives and tens of thousands of Afghan civilian lives – to enter a murkier phase.

Left out of the accord is the war’s other principal combatant. Years of furtive efforts to broker talks with the Taliban have not achieved any tangible results, and the insurgent group have vowed to keep fighting while there are any foreign boots on Afghan soil.

While US, UK and Nato combat will formally end, the US will subsequently negotiate a residual force of as-yet undetermined size, to be stationed throughout a handful of Afghan military bases, as well as some bases of which the accord grants the US “exclusive use.” Pentagon sources have said in the past year that Bagram, Kandahar, Jalalabad and Mazar-e-Sharif are on the list of potential bases.

Marine General Joseph Dunford, the US/Nato military commander in Afghanistan, is said to favor a post-2014 force of roughly 10,000 to 13,000 troops, about a fifth of the current US deployment.

The draft text published by the Afghans explicitly states that “US military operations to defeat al-Qaida and its affiliates may be appropriate in the common fight against terrorism,” leaving the door open both to US raids, probably by special operations forces, after 2014.

“US military counter-terrorism operations are intended to complement and support [Afghan] counter-terrorism operations, with full respect for Afghan sovereignty and full regard for the safety and security of the Afghan people, including in their homes,” the draft text reads.

There is also a concession to Afghan demands that the US offer guarantees against external aggression, but the language is vague indicating the US would have “grave concern[s]” over attacks by foreign entities into Afghanistan – most likely from Pakistan – but not committing to any specific military response.

The draft sidesteps the question of drones based on Afghan soil. They have been used for strikes in neighbouring Pakistan, and are expected to continue to launch missions from the range of bases offered to US force under the deal.

The language about the sanctity of the Afghan homes has been among the most controversial in the document, as has assurances the US demands that it will have the exclusive legal jurisdiction and control over any US personnel suspected of committing crimes in Afghanistan.

Earlier on Wednesday, the State Department strongly denied that the US would issue any apology to Afghanistan over mistakes made in the 12-year old war.

Jen Psaki, the top State Department spokeswoman, said earlier reports of a forthcoming apology were erroneous, but added it was possible that the US would provide “reassurances” to Afghanistan “addressing past issues, such as civilian casualties”.

Although the accord paves the way for a residual US force in Afghanistan on an open-ended stay, it represents a political victory for the Obama administration, which had vowed to a domestic US audience it would bring the war to a conclusion.

Pending the Afghan loya jirga’s anticipated approval of the accord, the deal contrasts sharply with the 2011 exit of practically all US troops from Iraq – a circumstance forced onto Washington by an Iraqi government that would not permit the US to prosecute its own forces, a deal-breaker for the Pentagon.

Instead, whatever comes of the longest US war in history will come by diplomatic accord between Washington and its erstwhile Afghan ally.