A few months ago, a former utility commissioner asked me over lunch if I wanted to write a book about electric deregulation in Texas. It’s got everything, he argued—the biggest bankruptcy in U.S. history, Warren Buffett, hedge fund impresario Paul Singer, behind-the-scenes wheeling and dealing. He was referring to the ongoing battle for Oncor, the grid operator that serves 10 million businesses and residential customers in North Texas and whose parent company, the former TXU, has been mired in bankruptcy for more than three years.

The ending of the story was unwritten—until last week: Buffett, the greatest deal maker in history, gets outmaneuvered and loses a $9 billion deal for Oncor to a California utility, which in the fast-paced world of buyouts is like a banana slug beating a jackrabbit in a foot race.

In the end, Sempra Energy of San Diego appears to have snatched Oncor from Buffett’s grasp with a little help from Singer’s Elliott Management. Last month, it looked as if Buffett’s $9 billion cash offer—the third bid submitted for Oncor—would be the charm. His Berkshire Hathaway holding company had agreed to 47 regulatory stipulations that eased concerns from the Public Utility Commission, which even in Texas’s deregulated market still regulates transmission companies like Oncor.

But Elliott, which already held just under $3 billion worth of debt in Oncor’s bankrupt parent company, now known as Energy Future Holdings, felt Buffett’s offer was too low. Last week, catching wind that another bidder might be entering the fray, it bought an additional $60 million worth of EFH’s unsecured notes from Fidelity Investments. That gave Elliott control over most of EFH’s debt, essentially enabling it to block Buffett’s bid with the bankruptcy court in favor of the new bidder.

Sempra then swooped in with its $9.45 billion bid, ensuring that Elliott and other creditors will get a bigger payout. Instead of the 18 cents on the dollar from Buffett, Sempra will pay 45 cents to 50 cents, Bloomberg reported.

Sempra also will establish an independent board for Oncor, something that became a stumbling block for former suitor NextEra Energy of Florida. Sempra also will avoid the complicated ownership structure proposed by Dallas billionaire Ray Hunt’s Sharyland Utilities, which rankled state regulators and caused that offer to crater.

As a buyer, though, Sempra is no Buffett. It’s short on cash and it’s carrying a lot of debt. And while Berkshire is sitting on about $100 million in cash that Buffett wants to spend, Sempra will be financing part of its purchase. Chief financial officer Jeffrey Martin told the Dallas Morning News that the company may finance as much as $3 billion that it would repay over the next three to four years.

Buffett had hoped to write a different ending when he first got into the Oncor bid earlier this year. While he’s been on the hunt for utility purchases for years, he had ample reason to stay away from Oncor and EFH. EFH was formed in 2007 through the $32 billion takeover of TXU by a team of Wall Street and private equity firms that included Goldman Sachs, KKR, and TPG, run by Fort Worth investor David Bonderman. The deal was the biggest leveraged buyout in history and it all pretty much hinged on one thing: a bet that natural gas prices would rise.

But a year later, hydraulic fracturing unleashed an abundance of natural gas, and prices plunged, taking EFH’s profits with them. The company was unable to pay the debt it had amassed to fund the buyout. By then, Berkshire had acquired $2.1 billion worth of EFH bonds and with the utility in a tailspin toward bankruptcy, Buffett sold them for $873 million—a $1.2 billion loss. EFH filed for bankruptcy in 2014.

This time, Buffett stands to collect a $270 million breakup fee for being dumped by Oncor, though that, too, must be approved by the bankruptcy court and Elliott has asked the court to reject it.

In writing the ending to the latest EFH-related debacle, Buffett may want to revisit a letter he wrote to his shareholders in 2013: “Most of you have never heard of Energy Future Holdings. Consider yourselves lucky; I certainly wish I hadn’t.”