You only need the last 2 lines to reproduce the issue, and the last 2 lines can be further simplified to
$Post = # /. f_[{x__}] :> aaa[f, {x}] &;
{{-1, 0}, {0, -1}} // MatrixForm
(* aaa[MatrixForm, {{-1, 0}, {0, -1}}] *)
As we can see, the rule in $Post is executed later than MatrixForm i.e. the code above is amount to
$Post =.;
{{-1, 0}, {0, -1}} // MatrixForm // (# /. f_[{x__}] :> aaa[f, {x}] &)
This is consistent with the description in the document of $Post:
$Post is a global variable whose value, if set, is applied to every output expression.
See also the introduction in the tutorial The Main Loop.
MatrixFormto$Post, too. (Alternatively,$PrePrint. ) See this post for more info: https://mathematica.stackexchange.com/q/3098/1871 – xzczd Nov 02 '22 at 16:09$Post = ReleaseHold[Hold[#] /. f_[{x__}] :> MatrixFunction[f, {x}]]//MatrixForm &;– Anixx Nov 02 '22 at 16:40MatrixFormthree times, and you'll see what's wrong. – xzczd Nov 02 '22 at 17:08