How to reproduce: hold one stack of stacked stuff in your hand (with no other stacked items of the same type in your bag), use Z to drop it on top of yourself while also holding F to instantly pick it up. As a result, the newly picked up stack goes into the cursor slot, while the unstacking script gives you the unpacked stuff while failing to remove the stack from your inventory (since the only stack is in your hand).
Potential fixes: either prevent the auto unstacking script from working if the only stack of the stacked item is in your cursor slot, or allow it to remove items from it if there are no in your pocket.