The basic problem is treating Afghanistan like a country - it is not one. Just because someone drew lines on a map doesn’t mean the people within those lines are a country. To the people of the region, family and tribe have always been more real and more important than any notion of “country”. I don’t have an easy answer; Just stating the root of the problem.
Instead we just embolden small weak nations that they can always count on us to restrict ourselves to defeat no matter how well trained our troops are, or how advanced our weaponry is because of self imposed restrictions. History is full of wars and how to win and lose them. We don't have to be as brutal as some in history, but it can only go so far before you are hindering yourself. Even in WW2 guys like General Patton got in trouble for stating obvious truths about war. We like to romanticize it instead of accepting the truths of it that we all know. We are so obsessed with the lives of civilians, many who are sympathizers, to the point we will lose the war to spare a life. That doesn't make us a great moral people, but an ignorant people. Accept war for what it is or don't support it. Those who support wars and the ridiculous ways we now fight them are the worst people, and responsible for many lost lives and a weakened nation. The war could still be won in a short and brutal fashion, but that's not going to happen. We will just lose it.