A few steps from the Boone County Courthouse in the northern Kentucky town of Burlington, Thomas Massie was about to hold a public event a little over a week before voters would decide whether to keep him in office.A boy walked up and offered him a piece of Pez candy.“You know, president Trump hands out endorsements like you hand out candy,” Massie told the boy with a wry grin as he accepted the gift. “Just not to me.”Massie, the Kentuckian engineer turned politician, was referring to Donald Trump’s support for his opponent Ed Gallrein, as payback for his frequent apostasies. Massie had voted against the president’s tax legislation, calling it not frugal enough. He had voted repeatedly against the war in Iran and condemned aid to Israel as a misuse of taxpayer money. Most conspicuously, Massie had helped lead the charge to compel the release of the Epstein files despite the president’s resistance.Trump has vowed to replace the man he has variously termed a “moron”, “loser” and “total disaster” with someone more loyal. In October, he settled on Gallrein, a dairy farmer and retired Navy Seal captain.The result is that Massie, who has never faced serious opposition since first winning office in 2012, now finds himself in a tough fight. He says his campaign’s internal polling shows the race deadlocked, but some recent public surveys have shown Gallrein ahead.The Republican contest for the 4th congressional district is likely to be remembered as one of the costliest, ugliest and most consequential primaries this year. It also serves as the latest indicator of Trump’s political clout, which showed continued durability over the weekend when senator Bill Cassidy of Louisiana, whom the president had targeted for defeat, lost his primary and a Trump-backed challenger was the top vote-getter.Thomas Massie is a fifth-generation farmer who lives on a small cattle ranch. Photograph: Jon Cherry/The New York Times A victory by Gallrein would be seen by other Republicans as the latest evidence that the president’s hold over the party remains absolute, even in the face of his sagging approval ratings. Massie hopes voters will opt to keep him and prove the opposite.“You can send him a message,” Massie told his audience inside the courthouse last week, referring to the president. “He needs to work with me because I ain’t going anywhere.” ‘He can’t get anything done’ The race is also a referendum on Massie.Despite being a seven-term incumbent, the Kentucky maverick is more comfortable in the role of outsider.An eccentric who lives off the grid on a small cattle ranch and guzzles raw milk, the West Virginia native (55) holds an engineering degree from the Massachusetts Institute of Technology as well as several patents.Trey Grayson, a Republican and former secretary of state in Kentucky who lives in the district and is neutral in the race, recalled once seeing Massie tell a classroom of teenage students that he had disliked cleaning his room when he was their age and therefore invented a robot to do the job for him.Like his political mentors Ron and Rand Paul – the former a retired Texas congressman, the latter a US senator representing Kentucky – Massie has libertarian leanings.In Congress, he quickly developed a reputation for opposing nearly every federal spending measure that came up for a vote. In an interview, Grayson recalled the senior Kentucky senator, Mitch McConnell, once asking Massie: “Thomas, have you voted ‘yes’ to anything since I last saw you?”Massie said his independent streak was very much in keeping with the anti-establishment predilections of Trump’s base.“I’m not a go-along-to-get-along congressman,” he said in an interview. “And that has endeared me to a lot of the constituents who are part of this coalition that put us in the White House.”But it has also tested the patience of fellow Republicans.“I backed him for a number of races,” said Steve Frank, a former city commissioner in Covington who now supports Gallrein. “But the heart of the problem with Massie is that he has no throw-weight. He can’t get anything done.”Downtown Covington, Kentucky. Photograph: Michael Swensen/The New York Times Grayson concurred with Frank’s assessment.“He has allies, but he hasn’t turned them into an effective coalition,” he said.His defenders argue that Massie should not be faulted for being the rare officeholder in Washington to refuse to compromise.“Massie’s principled,” said Patty Brueggemann, who attended the congressman’s event at the Boone County Courthouse with her teenage son. “Everybody knows where he stands.”As evidence that Massie could in fact get things done, Brueggemann said, “He got the Epstein files released. For me, that’s big.”But Massie did so alongside Democrats, and in defiance of Trump’s wishes, which did not sit well with every constituent.One such voter, Elizabeth Smith, a graphic designer and former candidate for Boone County commissioner, said in an interview that she had voted for Massie in the past but was now “on the fence.”“I’ve just felt like as of late he’s been grandstanding,” she said. “But maybe – I’m kind of having an epiphany right here – maybe it takes a strong personality to work with president Trump.”Smith said that she admired Massie’s willingness to take nearly two hours’ worth of questions from constituents that evening, while his opponent, Gallrein, seemed to shy away from such engagement.“He just doesn’t impress me at all,” she said.More than $14 million has been spent by Ed Gallrein’s campaign and outside groups supporting it, in a district whose largest city, Covington, has a population of fewer than 50,000. Photograph; Madeleine Hordinski/The New York Times ‘For Thomas or Against Thomas’ In March, Trump, a former reality TV star, bestowed the ultimate compliment on his handpicked opponent to challenge Massie, saying on social media that Gallrein was out of “central casting”.He is a fifth-generation farmer, a recipient of four bronze stars, at least one from Afghanistan, for valour in combat, and a churchgoing conservative with a ramrod bearing. The war veteran has been known to bring along his military regalia to campaign events and to litter his monologues with references to corn-growing and hay-baling.Gallrein (68) is also quick to say, “I’m not a politician,” though this is his second attempt at elective office after being defeated in a state senate primary two years ago.His most salient credential is Trump’s seal of approval. In speeches, the candidate often points out that 85 per cent of the district voted for Trump in the 2024 primary, asserting that residents deserved a congressman who would be in lockstep with the president.Massie, he said, “has a problem for every solution”.Massie has maintained, in turn, that his opponent would be little more than a yes-man to the president. When a New York Times reporter put this claim to Gallrein, he bridled.“I guess I should have gotten five bronze stars to demonstrate my personal courage and independence to act on my own,” he said, adding: “I am no rubber stamp. Will never happen.”However, when asked in a brief interview what house committees he would like to serve on, Gallrein said he would defer to the president and his allies, including speaker Mike Johnson and vice-president JD Vance.“I’m gonna go to speaker Johnson and the president and JD,” he said. “I’m gonna say, ‘Where do you need me, coach?’”Gallrein had not been the president’s first choice to run against Massie. That distinction went to the man who defeated Gallrein in his 2024 state senate race, Aaron Reed, who is also a retired Navy Seal as well as a gun store owner.In an interview, Reed said he had visited the White House last summer to discuss challenging Massie in the Republican primary but had ultimately decided against it and was staying neutral out of respect for Trump. Still, Reed’s assessment of Gallrein was less than glowing.“He has the personality of a rock,” he said. “I don’t think anybody’s going to vote for Ed Gallrein. They’ll either vote for Thomas or against Thomas.”A victory by Ed Gallrein would be seen by other Republicans as evidence that Donald Trump’s hold over the party remains absolute. Photograph: Madeleine Hordinski/The New York Times