Terrain modeling algorithm

That’s an interesting situation, but your code is very long and that someone is going to reverse-engineer it is unlikely.

You have duplicated curves, you use some segment (or sub-curve) more than once and in different places, that result in “dirty” Delaunay and other problems, imho.
I would do it differently.
Create mesh > unweld > create step by moving vertexes on Z > close holes

- Create all the polylines with nothing overlapping.
- Use the polyline control points to make the Delaunay mesh.
- split the mesh where it is supposed to have a vertical wall by using some polylines, and move/edit right side vertexes differently from left side
- patch the mesh by creating vertical quad faces

See this piece of code as reference: