He informs me that there will be if my attitude doesn't improve. This is what I don't understand. How is this kind of behavior by a TSO allowed?
It's tacitly "allowed" by the state WRT to all of those in authority.
If you give a customs agent lip, s/he can choose to detain you for an hour and go through all of your bags in great detail before sending you on your way.
If you give a police officer lip at a roadside stop they can spend a bunch of time asking you to do a roadside sobriety test and on and on before sending you on your way - Or give you a speeding ticket instead of a warning.
They can't act outside their legal constraints, but within them they can affect a wife range of nuisance activities.