Assuming a technologically advanced civilization that has grown sustainably from the resources of its own planet (and maybe "a few" additional resources from its own solar system), what would be the maximum population the planet could host in a stable ecosystem for millions of years?
We have enough time and refined technology to increase our population, completely re-engineering a new ecology with the most efficient oxygen producing plants / cells / organic microchips, wastewater treatment, growing genetically engineered food from any suitable energy source, have cheap fusion energy and whatever other non-hyper-fantasy-technology we could realistically imagine (i.e. no Star Trek replicator scenario).
Or in other words: Imagine a highly efficient CELSS (Closed ecological life-support system) of Earth's size. How many people could live there, taking into consideration the abundancy of natural resources?
Edit: Maybe I should explain the background of my thoughts and be more explicit about the constraints that I see.
I try to imagine where Earth and mankind could develop in the next 10k, 100k or 1M years (hard to set a timeframe for such a development). I assume mankind learns from the ecological mistakes we are currently doing, but still will have a tendency to grow as technological progress allows it. Also the desired standard of living will presumably grow.
So everyone wants to live comfortably. We could define limiting factors for a comfortable life and calculate different scenarios, depending on what people still would consider comfortable. Let's think about space and resources.
For the space factor we need maybe at least 50 m² for privacy/living space, plus space for community life, recreation, work, transit. Makes 200, 500, 5000 or whatever m² per person.
Yes, we have fusion energy, but no arbitrary conversion of matter. Some technologies might still need rare earths, precious metals or other elements. We probably can't build everything just from carbon and silica since we might want to make use of what physics has to offer.
An approach to estimate that resource factor could be to consider the abundancy of several technologically relevant elements on earth or the solar system today and define a usage per person based on todays usage divided by a "technology efficiency" factor of our future Earth of 10, 100, 1000 or whatever.
Oh, and of course we need water to drink and maybe to shower. Considering all of Earth's water, how many people could live from it if one person needs 100, 1000, 10000 l of water in circulation?
Really ultimate limits could be waste heat and gravity like @Ryan_L and @Chickens pointed out.
So what are the limits for which scenario?
Did I miss other relevant constraints in my considerations?