Here's the relevant code, which I got from 10% working to 99% working today: https://github.com/sparr/factorio-mod-w ... #L110-L123
Here is a screenshot of some left/right mirrored cliffs:

My problem is that last 1%. There are seemingly arbitrary/random glitches where a cliff will have a gap, or the end of a cliff will turn in the wrong direction. This might be a problem with my code, or something weird with correct_tiles at chunk boundaries (which has caused problems for me in the past with deep water transitions), or something else? Is there any way I can easily/quickly get info about entities in game by pointing at them?