I have to example sentences about 'be on to something' seemingly contradicting in meaning. I mean 'be on to something' in ❶ is used positively, and then in ❷, negatively.

Can you tell my how this contradiction happened?

❶ Tyler Cowen said, if the economy bounces back strongly in the coming years, he'd have to wonder if Keynes was on to something. Mark Zandi says if the debt from all the spending seems to really weigh down the economy, he'd have to wonder if maybe Keynes was wrong all along.

❷ I think he was on to something when he said that people starving in African deserts should move to where the food is. // I'm with Deke on this one...:-))
he'd have to wonder if Keynes was on to something.
I think he was on to something

They're both positive and both mean the same.

"To be on to something" is to be in pursuit of some principle or truth.
It's irrelevant whether the truth is beneficial or not.

The speaker means that evidence is beginning to surface, indicating that the principle or theory is correct.

Perhaps the use of "wonder" is not clear to you. Sometimes we use "to wonder" to mean "to doubt." But it can also mean "to consider."

"I think he was on to something" is the more common construction.

"I'm beginning to wonder if [perhaps] he was on to something.

The difference in the second one is that it implies that heretofore the speaker's opinion was that "he" was NOT on to anything. That is, his theories were believed to be incorrect.

Sometimes we use the expression to suggest that we've only discovered the tip of the iceberg. We only can say at this point that we believe we might be on the right track. But we're optimistic.
Students: We have free audio pronunciation exercises.
Sometimes we use the expression ironically, or sarcastically.

"I think maybe that Edison guy was on to something with that light bulb!"

Of course Keynesian theory has always been controvercial, and people often change their opinions about it.

Both of the speakers in your first example are Keynesian skeptics.
The first one says, "If things get better, I'll change my mind and say he was right."
The second one says, If things get worse, I'll know I've been right about his being wrong."
Thanks, Avangi.
You're a great helper.