Adding new mission items at end prompts AP to restart current command

I’m adding new mission items to the end of a running mission with Lua, but the AP restarts the running mission item when I do so. This seems to happen due to a mission change check in AP_Mission.cpp.

My reading of AP_Mission_ChangeDetector::check_for_mission_change() suggests that so long as the three mission items after the current one are the same, this function should return false, no change.

However, if I add a mission item (at the end of the mission — many mission items away) between when the current mission item is read by ArudPilot but before it has been reached, check_for_mission_change() claims the mission has changed and AP_Mission restarts the current item. This seems to happen because the mis_change_detect structure saves the current command index, but that command index is advanced somewhere in this process, making the items “mismatch.”

Is this the desired behavior? If so, why?

(An alternative, to wait for MISSION_COMPLETE and then load the new items prompts the Copter to slow down and pause before starting the next mission items, which is not ideal.)

While it may not be “desired” behavior for your use case, it appears to be expected behavior if I’m reading this condition correctly:

if (num_cmds != mis_change_detect.cmd_count) {
    cmds_changed = true;
}

Check the Discord #scripting channel for my own inquiry into this.