Ok, I had a deeper look in the code, but I had a lot of trouble following what was going on, so I tried to strip everything to the bare minimum.
I made this version with a somewhat decent solution. I know it's not the droid you were looking for, but maybe there's some process here that's helpful.
Concerning finding the error in your original code, while stripping everything, it came to mind that after you disable Platform and Solid, you should wait 0.1 or even 0.01 before moving the cube, so the engine can have a tick to compute the behavior change.
I don't know if this is the real fix for the original code, but it is quite possibly it.
drive.google.com/file/d/1g6BgxU0rej4YTsfpOFCy7St_l0aZaoBg/view
EDIT: I'm also not using the "Push Out Solid" addon, the behavior "Custom" has the action "Push out solid" that fixes the edge case where you place a cube above you and then you get trapped inside it.