MZ Tips&Tricks Auto-Life, but chance-based and as passive?

As per the title. I'm trying to modify the Tips&Tricks entry (from here) to work as a passive and also to function with death from any source. For context, a phoenix-type summon has this ability; if it dies from anything (damaged, slip damage, during its own action as a result of an unrelated debuff state) it has a 20% chance to revive itself with a small amount of HP. Currently, this is what I have:

JavaScript:
<JS Post-Apply As User>
if (user.hp <= 0 || user.isDead()) {
  if (Math.random() < 0.2) {
        $gameTemp.requestAnimation([user], 364);
        const rate = 0.25;
        const heal = Math.floor(user.mhp * rate);
        user.gainHp(heal);
        user.startDamagePopup();
  } else {
        $gameTemp.requestAnimation([user], 365);
  }
}
</JS Post-Apply As User>

<JS Post-Apply As Target>
if (user.hp <= 0 || user.isDead()) {
  if (Math.random() < 0.2) {
        $gameTemp.requestAnimation([user], 364);
        const rate = 0.25;
        const heal = Math.floor(user.mhp * rate);
        user.gainHp(heal);
        user.startDamagePopup();
  } else {
        $gameTemp.requestAnimation([user], 365);
  }
}
</JS Post-Apply As Target>

<JS Post-Regenerate>
if (user.hp <= 0 || user.isDead()) {
  if (Math.random() < 0.2) {
        $gameTemp.requestAnimation([user], 364);
        const rate = 0.25;
        const heal = Math.floor(user.mhp * rate);
        user.gainHp(heal);
        user.startDamagePopup();
  } else {
        $gameTemp.requestAnimation([user], 365);
  }
}
</JS Post-Regenerate>

This will result in the battle freezing after it has died of slip damage or enemy attacks; I don't know if it was designed to work as a passive state or not, and I can see the Post-Regenerate part causing issues eventually (it is only supposed to gain a chance of revival upon death, not to keep checking every turn if it is still dead). Attempts to do it with VS Auto Skill Triggers (the Auto Trigger: Death note tag) result in no HP being restored and both animations constantly playing.
Trying to put the same code above (but with an actor ID check) in an On Add State within the Death state (I think) froze up the battle, too.
I haven't checked whether doing it through troop pages works or not, though, but I want to see if there is a better way of tying it to the passive state - pages are still limited after all, I think.

Could anyone help with accomplishing this? 😅
There's no difference between passive states and states, in terms of code working in them.

What's the point of the Post-Apply as User? Do you have skills that kill yourself?

The Post-Apply as Target is wrong because you need to be referencing target, not user.

I don't know what would be making it freeze, have you tried taking out the animation calls?
I think setting it as user was a copy-paste mistake I made halfway through, but changing it made no difference. The post-apply user part is like I said to check if the user died during its own action (reverse heals, recoils after performing a skill etc), so I guess it potentially is able to kill itself.
The passive is supposed to trigger from any death source after all.
The post-regenerate does play the animation every regeneration phase though, which it's not supposed to do - it should only happen once the summon has died from slip damage, not continue to check every turn if it's still dead.

Just now I replaced the animation calls with console logs, but the battle will still freeze regardless.
(edit: possibly because the passive continues to check if it's dead? Not sure how I would fix that, though...)

(edit 2: nevermind, the console log only triggers once each time)
(edit 3: death by default poison state just in case the code-based ones were the issue, death by getting attacked or moving the check to troop events all still result in battle freezing 😅)

Original thread: https://forums.rpgmakerweb.com/threads/tips-tricks-auto-life-but-chance-based-and-as-passive.160681/