KABUL — President Obama outlined his plan to end America’s longest foreign war during a visit here Tuesday colored by election-year politics and economic uncertainty, declaring that “this time of war began in Afghanistan, and this is where it will end.”

“We have traveled through more than a decade under the dark cloud of war,” the president said at a U.S. military base. “In the pre-dawn darkness of Afghanistan, we can see the light of a new day on the horizon.”

Obama delivered his address at the end of an unannounced visit here to sign a long-term partnership agreement with the Afghan government and to mark, alongside American troops at Bagram air base outside this capital city, the first anniversary of the raid that killed Osama bin Laden.