Heat the olive oil in a large Dutch oven over medium heat. Add the onion, carrots, and celery. Cook for 8 to 10 minutes, stirring occasionally, until the vegetables become soft and lightly caramelized.
Add the garlic, tomato paste, oregano, thyme, red pepper flakes, salt, and black pepper. Stir constantly for about 1 minute until the tomato paste darkens slightly and the spices become fragrant.
Pour in the red wine, if using, and cook for 2 to 3 minutes until most of the liquid has evaporated.
Stir in the crushed tomatoes, vegetable broth, rinsed lentils, and chopped walnuts. Bring the mixture to a gentle boil.
Reduce the heat to low and simmer uncovered for 35 to 40 minutes, stirring every few minutes, until the lentils are completely tender and the sauce has thickened. Add a splash of broth if it becomes too thick.
While the sauce simmers, cook the pasta in well-salted boiling water until al dente. Reserve about ½ cup of pasta water before draining.
Remove the sauce from the heat and stir in the balsamic vinegar and fresh parsley or basil. Taste and adjust the seasoning with additional salt and pepper if needed.
Add the cooked pasta directly to the sauce along with a splash of reserved pasta water. Toss until every strand is evenly coated.
Serve immediately with fresh basil, grated Parmesan or vegan Parmesan, and freshly cracked black pepper.