In a large Dutch oven or skillet with a lid, add the olive oil and white and red onions and set over medium heat. Once sizzling, cook until onions are slightly wilted and starting to sweat but not browned, 5 to 7 minutes. Stir in ½ teaspoon salt, cover, reduce heat to low and simmer, stirring occasionally, until the onions are very soft and translucent but mostly still in strands, about 1 hour. Adjust heat as needed to maintain a simmer, and if the onions are burning, add a few tablespoons of water. (Onions can be made ahead and refrigerated for 1 week or frozen for a couple months.)
When you’re ready to serve, bring a large pot of salted water to a boil. Add the spaghetti and cook until al dente according to package directions. Reserve 1½ cups pasta water and drain.
Add ½ cup pasta water, the pasta and the tarragon leaves to the onions. Raise heat to medium-high and stir vigorously until the pasta is well coated in sauce, 1 to 2 minutes. Add more pasta water as needed until the noodles are glossy. Season to taste with salt.
Transfer the pasta to plates or shallow bowls and cover with cheese (if using) and generous grinds of black pepper.
