Washington -  Defence Secretary Jim Mattis said the US and allies are “holding the line” against the Taliban in Afghanistan as forecasts of a significant offensive by the militants “remain unfulfilled.”

As the US approaches the 16th anniversary of the invasion of Afghanistan, now America’s longest war, Mattis gave his first congressional testimony Tuesday since President Donald Trump’s announcement in August of an expanded US role to train and advise Afghan forces.

Mattis warned that an American withdrawal from Afghanistan would be "to our ultimate peril". "Based on intelligence community analysis and my own evaluation, I am convinced we would absent ourselves from this region at our peril," he said, testifying before the Senate Armed Services Committee.

Mattis, who told Congress in June that “we are not winning in Afghanistan”, was more sanguine this time. “Uncertainty in the region and the NATO campaign has been replaced by certainty due to the implementation of President Trump’s new South Asia Strategy,” he said. While Republican Senator John McCain, the committee’s chairman, praised Trump’s decision to eliminate deadlines for withdrawing US troops, he called it “totally unacceptable” that Congress has been given no detailed information on the revised strategy.

More than 3,000 US troops are being added to about 11,000 already in Afghanistan, according to Mattis. An additional 6,800 NATO and coalition troops are there to assist about 320,000 Afghan national security forces. Marine General Joseph Dunford, chairman of the Joint Chiefs of Staff, told the Senate panel that the revamped US strategy means an “enduring commitment” to Afghanistan.

Dunford said he agreed with the February assessment of the top US general in Afghanistan that the situation was a stalemate. “That’s my assessment of where we are right now,” Dunford said. “We are not at a point where we can bring a successful political solution to the war.”

Mattis’s optimism is at odds with analysts including the US inspector general for Afghanistan reconstruction, who said in a report in August that a group affiliated with Islamic State has laid down roots in the country, part of a broad deterioration that has seen a record number of Afghan civilians killed. The inspector general depicted US personnel as hunkered down behind blast walls.

Mattis, echoing vows by his three predecessors as defence secretary and now by Trump, said the US must increase pressure on Pakistan to seal off its northwestern border to stop the Taliban and other militants from using the territories as a safe haven to plan and execute attacks. “We will firmly address Pakistan’s role,” Mattis said.

Mattis said that “for the first time in this long fight, all six Afghan military corps are engaged in offensive operations” and “during these recent months there have been fewer civilian casualties” because of coalition operations.

The defence chief said US military personnel will be deployed closer to the front lines, embedding with Afghan troops at the battalion and brigade level instead of farther away at the corps or headquarters level.

“Make no mistake, this is combat duty but the Afghan forces remain in the lead for fighting,” he said.

Mattis visited Afghanistan last week with NATO Secretary-General Jens Stoltenberg to reaffirm US commitment.

General John Nicholson, the top US commander in Afghanistan, "is holding the line," Mattis assured the senators. "We must always remember we are in Afghanistan to make America safer and to ensure South Asia cannot be used to plot transnational attacks against the US homeland or our partners and allies," he said.