* Boeing agrees $4.7 billion deal to buy back Spirit AeroSystems

* Airbus sought compensation to take over loss-making activities

* Spirit to seek buyers for some assets to cover payment to Airbus

WASHINGTON, July 1 (Reuters) - Hashed out in three-way talks between plane giants and one of their key suppliers, Boeing's $4.7 billion deal to buy back Spirit AeroSystems is a rare triangular deal born out of crisis.

The merger, variously code-named "Sphere" and "Sparrow," had been in the making since at least September when Boeing was offering financial support and commercial agreements to help Spirit improve operations, people familiar with the talks said. But last year's efforts to improve Spirit's quality and delivery issues, a persistent problem for some years, reached a tipping point on Jan. 5, when an Alaska Airlines jet lost a panel in mid-flight, freezing output of the affected model.

The incident, linked to bolts going missing in a Boeing plant after unidentified workers addressed flaws in a fuselage shipped from Wichita, Kansas, accelerated discussions between Boeing and Spirit despite tensions - and within days led to deeper talks.

"They had to deal with the problems on the ground first, but then within a week there were formal discussions between Spirit and Boeing about a potential transaction," a person familiar with the deal said.

On March 1, Boeing confirmed the talks, catching markets and Spirit's other key customer, Europe's Airbus, by surprise.

Boeing had sold off Kansas and Oklahoma plants for around $950 million to private equity firm Onex in 2005 to meet targets for returns on net assets. Since then, Spirit had diversified to find new clients. The results included a 500,000-square-foot Airbus A350 composite-fuselage parts plant in North Carolina.

But as production failed to take off as planned, costs were high, driving the new operations into the red and raising questions over the resilience of the world's largest standalone aerostructures firm, analysts said.

Europe's top planemaker had itself been in talks for months with Spirit to help it improve the efficiency of loss-making operations that supply its modern A220 and A350 jetliners.

Forced to rewrite its approach after Boeing revealed its bid plans, Airbus quickly drew a red line around two key plants: the purpose-built factory in Kinston, North Carolina, where rail-mounted robots weave part of the composite body of the A350, and an A220 wings facility in a plant in Belfast, Northern Ireland.

At stake was access to data about costs and strategic decisions about production for its most modern programs. In an interview with Reuters in April, Airbus CEO Guillaume Faury conceded the planemaker was likely to absorb those plants but warned that it reserved the right to use a contractual veto to prevent the sensitive work from falling into the hands of industry rivals.

Boeing, for its part, had no designs on those two plants but the two sides haggled over the European firm's request for compensation to take on Spirit's Airbus-related losses, which were estimated as high as $2 million for each set of wings and other parts for the A220, known as a shipset, worth $7 million.

Boeing initially chafed at the idea, with a person familiar with the talks predicting the company would never pay to release operations of strategic and industrial value to its rival.

What followed were weeks of discussions that delivered a compromise designed to accommodate Boeing's concerns, the sources said. Spirit would pay Airbus $559 million, while looking for a buyer for some assets in Belfast as well as less critical operations in Prestwick, Scotland, and Subang, Malaysia.

Morgan Stanley is tasked with managing these asset sales, ensuring proceeds cover the $559 million payment to Airbus. Even so, some industry sources predict those talks will be tough.

For its part, Airbus was forced to agree that it may have to take those plants anyway, if no seller could be found.

The talks held one final surprise. Boeing had insisted it would buy back its offshoot for cash. But some analysts said that meant a further strain on the debt-laden group's finances.

For months, Spirit had been under the spotlight, with outgoing Boeing CEO Dave Calhoun pushing for a deal before he was due to step down by the end of the year.

When Boeing switched its $35.50 per-share offer to an all-stock deal valued at $37.25 per share, it meant Spirit would have to carry out due diligence on Boeing, ensuring both parties were fully informed about each other's status, the sources said.

After initial hesitation, Spirit's board and Morgan Stanley gave their final OK on Sunday, the sources said. Amid soaring projected demand for planes, its shareholders - minus Onex which exited in 2014 - would receive some $4 billion in stock to sell core Spirit factories and some other assets back to Boeing.

(Reporting by Mike Stone in Washington and Tim Hepher in Paris, Abhijith Ganapavaram in Bengaluru; Editing by Matthew Lewis)