Ctrl+Z acts on the context of what has focus, and the History panel shows them all.
A command
A step is aHistoryCommand: a label and two functions.
do and undo may be async. Other fields: mergeKey (commands with the same key pushed close together become one step, for a drag or a slider), preview (what changed, for the History panel).
A context of your own
push runs the command: do not apply the change yourself first.
Several changes, one step
compositeCommand builds one command from several when you have them up front.
Point undo at your context
Ctrl+Z needs to know which context your panel uses.
"historyContext": "counter" on the widget in the manifest.
While a text field has focus, Ctrl+Z stays the field’s own undo.
Changing files
Do not write to files directly when the change should be undoable: edit them through the document service. The step then lands in the file’s history (file:<path>), next to what the user typed, and works whether the file is open in the code editor or not.
{ start, end, text } offsets in the file’s current text. A step whose file changed underneath it (the content it produced is gone) is dropped rather than applied to the wrong text.
To change code by its meaning rather than by offsets, see code and code generation.