Help with Object Avoidance on Plane - turn after waypoint logic

I’m trying to incorporate object avoidance into Plane. Basically doing this in two steps, hopefully to keep it simple. 1. Avoid fixed obstacles - specified by geo fences 2. dynamic object avoidance such as ADS-B detected objects.

My first use case is to simply get the plane to not fly into a geofence that isn’t moving when following a very simple mission. This video shows why that is a problem.

Although this is SITL, I have had exactly this situation in the wild and the resulting geo-fence breach has cost me an airplane because the fence represented real trees.

So far I’ve managed to get the OA code to run on plane, with lots of debug, but it doesn’t avoid this scenario. Any suggestions appreciated. The code is here: GitHub - timtuxworth/ardupilot at BendyRulerPlane

The Bendy Ruler and Traveling salesman code seems to be too coarse grained to deal with this scenario, so my best guess is that I need to incorporate WPNav_OA which means integrating WPNav navigation into (at least) Auto mode on plane. Thats what I’m working on now.

@Yuri_Rage has also suggested that it might be helpful to incorporate some Lua script - not sure how this will work, but it definitely seems like a good idea.
It might also deal with regulatory requirements as it might be that ADSB obstacle avoidance behavior might be regulated differently in different countries. In Canada, what understand is that when a RPAS (drone) is trying to avoid a crewed vehicle, it must immediately turn right and loiter (in a right turn) down to a specific altitude. The desired behavior might be different in other places, which might make Lua a better way to implement behaviors.