Preheat oven to 400°F (200°C).
Slice the top off the head of garlic to expose the cloves. Drizzle with olive oil, wrap in foil, and place on the baking sheet.
Arrange the tomatoes (cut side up), onion (if using), and thyme on the sheet. Drizzle with olive oil and sprinkle with salt, pepper, oregano, and sugar.
Roast for 45–50 minutes, until tomatoes are soft, caramelized, and slightly blistered.
Squeeze out the garlic: Once cooled slightly, unwrap the garlic and squeeze the roasted cloves into a blender or food processor.
Blend: Add the roasted tomatoes and onions (discard thyme stems) and blend until smooth.
Simmer: Pour the sauce into a saucepan, add red pepper flakes and fresh basil, and simmer for 15–20 minutes over low heat to thicken and deepen the flavor.
Finish: Taste and adjust salt or sugar as needed. Drizzle with more olive oil before serving.