The sequence is, as you know, to first play all the treasures, then to buy the card(s). Autobuy makes you (at the moment) do the selection of one card to buy, then figures out what treasures are needed to be played so you can afford the selected card.
It doesn't take into account all treasures, for example Contraband, which I'm totally fine with, because that obviously has problems with the Autobuy model. So let's keep it that way, all the treasures considered behave nicely and don't do anything strange (like Horn of Plenty). That means there are no complicated on-play abilities of treasures to consider. In addition, you would need to consider such on-play abilities of treasures anyway for the current implementation as well (which is probably exactly the reason why Venture, Loan and others are not considered for Autobuy at the moment).
For the on-buy/on-gain abilities of the bought cards: Those happen when the cards are bought/gained, which happens *after* the treasures are played, which is the primary concern of the Autobuy function. Consider it like this: Autobuy is
1) a fancy interface for the player to select a sequence of cards/events to buy, then
2) an algorithm that figures out which treasures are needed to be played so that the cards selected in 1) can be bought, and
3) a method that returns to "the game engine" by handing it the list of cards/events to buy (in order).
The game engine would then proceed to buy the cards/events one after the other, each time of course checking that this is still possible, but also handling all the on-gain/on-buy abilities as usual. In any of the cases where the buy is no longer possible, it aborts the process, but returns to the usual interface of buying cards in the buy phase.
My edge cases are mostly concerned with correctly dealing with 1), while you seem to think that 3) will give problems. But 3) is handled after 1) and 2) and if any problems occur, the process can safely be aborted. That concerns even the case of a Villa being bought, because returning to the action phase would be a reason for "buying the other selected cards is no longer possible".
At the current implementation, 1) is 'easy' because only one cards needs to selected and this may need some overhaul under my proposal. 2) wouldn't change with more cards to be selected and 3) is just a generalization of what happens now.
A) Player selects to buy 2 Ports, when he has 2 buys and 8 coin "in hand", but only 2 Ports remain in the Port pile. Autobuy will play the 8 coin, then the game engine buys a Port, triggering its on-buy ability, so the players gains 2 Ports already from the first buy. The game continues, trying to buy another Port (as instructed), but fails, as there is no more Port in the pile. The game is in the state of the player having 1 remaining buy and 4 coin to spend.
B) Player selects to buy 2 Heralds, when he has 2 buys and 8 coin "in hand". Autobuy will play the 8 coin, then the game engine buys a Herald, triggering its on-buy overpay interface. The player suddenly realizes that he wants to overpay 2 coin. When all is done with the first Herald, the game continues, trying to buy the second Herald. But since the player only has 2 coin available, this can't be done. The game is now in a state of the player having 1 buy and 2 coin to spend.
Both cases A) and B) are situations where the player better had not used Autoplay, but at least he can spend his remaining coins.
Tl;dnr: There are interface problems to overcome, but all the on-play abilities of treasures are already being taken into account by the current implementation, and all the on-buy/on-gain abilities of bought cards happen later and the process can at this stage easily be aborted if a problem occurs.