Arrange one rack in the top position and a second rack in the middle of the oven. Heat the oven to 375°F. Bring a large pot of heavily salted water to a boil over medium-high heat.
Melt 4 tablespoons unsalted butter in a Dutch oven or large heavy-bottomed pot over medium-high heat. Add 2 pounds thinly sliced large yellow onions, 1 teaspoon kosher salt, and ½ teaspoon black pepper. Stir well to combine. Cover and cook undisturbed for 5 minutes.
Uncover the onions and add 8 ounces thinly sliced baby bella mushrooms. Continue to cook, stirring occasionally at first and then more frequently once the onions start to brown, until the onions are a light caramel brown color, 15 to 20 minutes. Reduce the heat as needed if the bottom of the pot is getting too dark. Make sure to scrape the bottom of the pot when stirring.
Meanwhile, add 1 pound dried spaghetti to the boiling water and cook according to package directions for 1 minute less than al dente. Drain.
When the onions are ready, stir in 3 finely grated garlic cloves, 2 tablespoons Worcestershire sauce, 1 tablespoon fresh thyme leaves, and 2 teaspoons packed brown sugar. Cook for 1 minute. Stir in 1 cup dry white wine and scrape up the browned bits from the bottom of the pot. Stir in 3 tablespoons all-purpose flour and cook until well distributed and it smells toasty, about 2 minutes.
Slowly whisk in 2 ½ cups low-sodium beef broth and 1 cup heavy cream. Bring to a boil, whisking occasionally. Reduce the heat to maintain a simmer and cook, whisking occasionally, until the sauce is slightly thickened, about 5 minutes. Remove the pot from the heat.
Whisk in 4 ounces of the shredded mozzarella cheese and 4 ounces of the shredded Gruyère cheese until completely melted and smooth.
Add the spaghetti and toss until combined. Taste and season with more kosher salt as needed. Transfer to a 9x9- or 9x13-inch broiler-safe baking dish and arrange into an even layer. Sprinkle with the remaining 2 ounces shredded mozzarella and 2 ounces Gruyère cheeses.
Bake on the middle rack until the edges are bubbling and the cheese is melted, about 20 minutes. Move the baking dish to the top rack. Turn the oven onto broil and broil until the cheese is browned, 3 to 4 minutes. Let cool for 5 minutes. Garnish with fresh thyme leaves if desired before cutting into pieces like a lasagna and serving.
