A deal has been reached between government agencies and the owners of Starrett City, the 46-tower complex in Jamaica Bay, Brooklyn, to keep the massive complex affordable for middle-class residents. Last year, opponents blocked the complex’s sale to real estate investor David Bistricer for $1.3 billion. The complex has 5,881 apartments, a power plant, shopping center, post office, churches and a synagogue. Housing advocates say the deal could be a model for preserving affordable housing around the city, which has been under threat as big developers and private equity firms have bought 90,000 apartments in recent years. The complex’s 250 investors, led by Disque and Carol Deane, agreed to an affordable housing provision, while government agencies agreed not to block a sale.