Fortify the border, re-enforce it with attack helicopters and drones where most likely of incidents and identify and find ways to sanctions those responsible functional leadership (and their supporters) behind the local gunmen, to include aiding their rivals with preferential access to trade business and other perks to shape the dynamics inside Afghanistan. Money talks.
Also, turn the border into a double wall, with a 500 meter to 1 km buffer zone in between to detect and better restrict unregulated movement and smuggling. This will also help prevent resupply by the TTP.
Passive defenses are the key, shape the landscape such that movement of people and goods has to come through official crossings. Intelligence will be key to detecting those already inside Pakistan supporting the miscreants at the hordes as well as the TTP. Avoid alienating regular afghans that are just trying to earn a living and support their families in Afghanistan, we need to isolate the worst of the worst from the majority of the population. No neighboring nations of Afghanistan is willing to put up with their behavior, so it’s a matter of time before they have to come to terms with Pakistan.