What's happening is any edit or write operation you perform using the user interface or the API is wrapped within a transaction. Transactions are visible in the UI as items you can undo. Even though custom graphics are not saved, they are still transacted so they can be undone within that session. When you run a script, every API call you make that edits or creates data will create a new transaction, which also means a new entry in the undo list.
When you use an add-in and a command, the work the add-in does within the execute event is wrapped within a single transaction so it can be undone with a single undo.
Ideally, the API would expose some low-level transaction support. The Inventor API has this so even in a simple script, you can start a transaction, make API calls, and end the transaction so everything is wrapped within a single undo. Inventor also supported combining your transaction with the previous transaction. This would result in nothing new being added to the undo list, but if the user undid the previous action, whatever happened in the transaction you combined with it would also be undone. I don't know if any of this is coming or not. Hopefully.
The use of any text commands is not supported. They're meant for internal use and primarily for testing. The suggested text command clears the undo stack, so nothing can be undone after you run that command.
---------------------------------------------------------------
Brian EkinsInventor and Fusion 360 API Expert
Website/Blog:
https://EkinsSolutions.com