Why does the cost basis of the investment in your HSA matter in terms of tracking the amount you can withdraw?
My understanding is that the amount you are allowed to withdraw is accounted using the cash value you withdraw *at the time you make the withdrawal*. If my understanding is correct, you would not need to account the cost basis of your investments in the HSA any differently than any other brokerage. The accounting of how much you are allowed to withdraw could be done independently.
What if you setup two accounts like:
Expenses:Medical:HSA-Qualified
Expenses:Medical:HSA-Withdrawn
And whenever you incurred a qualified medical expense, book it against 'Expenses:Medical:HSA-Qualified'. Then, whenever you pull money out of your HSA, move that same amount to 'HSA-Withdrawn'? It would probably be preferable to come up with some way to tie the two events together, but I haven't conceived of a simple way to do so.
-Aaron