Defying polls that showed Gillespie ahead by as much as 20 percentage points, Stewart held Gillespie to a margin of victory of a mere 1.2 percentage points.

Gillespie only narrowly avoided a possible recount challenge. The state permits losing candidates to challenge results when the margin is under 1 percentage point.

The data needed to discern the exact factors behind Stewart’s strong performance are still limited, but it appears the Minnesota-born defender of Virginia’s Confederate heritage and President Donald Trump enthusiast benefitted from the defection of moderate voters from the Republican Party in Northern Virginia.

The share of the Republican primary electorate from the predominantly moderate and affluent suburbs of Northern Virginia declined from 34 percent in the 2016 presidential primary to 30 percent in Tuesday’s statewide contest, according to an analysis of official data by Geoffrey Skelley, an expert at the University of Virginia Center for Politics.

“There are a lot of moderate, white-collar workers in Northern Virginia who might at one time have been moderate Republicans or independents, but now vote Democratic because they have become functionally Democrats,” Skelley said.

The conservative shift of the national Republican Party had been disenchanting those voters for years, according to Skelley, “but Trump may have been the final straw for some.”

At the same time, if those former Republican primary voters opted to participate in the Democratic primary, they likely boosted the fortunes of the more moderate candidate, Lt. Gov. Ralph Northam, Skelley ventured. Northam thumped former congressman Tom Perriello in the very same Northern Virginia counties whose residents now make up a smaller proportion of the Republican primary electorate.

Skelley also hypothesized that some of the Republicans who voted for the third-place candidate, state Sen. Frank Wagner, might have voted for Gillespie over Stewart in a two-candidate matchup.

As for the failure of the polls to predict Stewart’s surge, Mark Rush, a politics professor at Washington & Lee University in Lexington, Virginia, believes it tracks closely with the inadequacy of polling in the 2016 presidential race. In both cases, Rush posited, populist conservative voters appear to have refused to cooperate with pollsters.

“No one should say the 2016 election is behind them. This is still clearly percolating and it’s going to continue,” Rush said.

Had Stewart won the primary, his racially inflammatory views would likely have further undermined Republicans’ already-weak electoral chances in the general election. Virginia Democrats are expected to benefit from Trump’s low approval rating there.

Notwithstanding his out-of-state roots, Stewart appealed brazenly to pockets of Confederate nostalgia in the state, taking advantage of controversy over the removal of Confederate Civil War monuments in Virginia and elsewhere.

Stewart held rallies to oppose the scheduled removal of a Robert E. Lee statue in Charlottesville, Virginia. He later declined to condemn a torch-bearing demonstration held by white nationalists at the monument.

Stewart even decried New Orleans Mayor Mitch Landrieu’s decision to remove several Confederate monuments in the Louisiana city, implying that Landrieu was a “Yankee” dictating to Southerners how to treat their history. (In fact, Landrieu is a New Orleans native.)

Nothing is worse than a Yankee telling a Southerner that his monuments don't matter.

Stewart also fashioned himself the heir to Trump’s presidential campaign.

“This is the question: Did the Donald Trump revolution stop in 2016?” Stewart said on the campaign trail. “Or did it continue in the commonwealth of Virginia? Because if it works here, it will work everywhere else.”

In his capacity as a co-chairman of the Trump campaign’s Virginia state operation, Stewart actually proved too anti-establishment for the campaign. It fired Stewart in October after he participated in a protest outside the headquarters of the Republican National Committee in Washington, D.C.

Stewart nonetheless benefitted from the support of influential pro-Trump media outlets and personalities like Breitbart and radio host Bill Mitchell. He touted his record of cracking down on illegal immigration at the county level and vowed to force every locality in the state to check the immigration status of criminals in its jails and prisons.

Gillespie, the consummate Beltway insider, was a useful foil for Stewart, who dubbed him “Establishment Ed.” Stewart has yet to instruct his supporters to vote for Gillespie, reassuring them on Tuesday night that “there is one word you will never hear from me: ‘unity.’”

The election outcomes, including Perriello’s more distant challenge, show the ideological cleavages in both parties, according to Rush of Washington & Lee ― albeit more so on the Republican side.

“It’s not so much the Trump phenomenon ― Trump is more the symptom,” Rush said. “Whatever force Trump managed to tap into is still there, and whatever Bernie tapped into is still there.”

“We’ll see where all those Stewart and Perriello voters go in November,” he added.

Sign up for the HuffPost Must Reads newsletter. Each Sunday, we will bring you the best original reporting, long form writing and breaking news from The Huffington Post and around the web, plus behind-the-scenes looks at how it’s all made. Click here to sign up!