Merging surface edges to extend surface

My goal is to extend all the edges of a fairly boring surface. Unfortunately, the 2 of the 4 edges need to be merged first. I can use the MergeAllEdges command from the menu bar but it is not available in the RhinoScriptSyntax. I thought that my journey into RhinoCommon was a solution but it is not working. The Rhino.Geometry.Collections.BrepEdgeList.MergeAllEdges() says that is starts with six edges and ends with four, but Rhino disagrees.

Could somebody provide a suggestion for merging the mergeable edges?

Looking ahead, it seems that extending the edges may also be painful… any suggestions for that task would also be appreciated.

Here is the script that is supposed to merge the edges.

import rhinoscriptsyntax as rs
import scriptcontext as sc

select = rs.GetSurfaceObject("Select surface")
surfaceID = select[0]

rhinoObject = sc.doc.Objects.Find(surfaceID)
brepObject = rhinoObject.Geometry

# Get a Rhino.Geometry.Collections.BrepEdgeList object for the surface
surfaceEdges = brepObject.Edges
mergedEdges = surfaceEdges.MergeAllEdges(0.1)
print(f'Attempting to merge {mergedEdges} edges in the surface')
print(f'Finishing with {surfaceEdges.Count} edges')

After running the script, there are still six edges:

I imagine this would algorithmically be a more difficult problem to solve?
For a polygon boundary you can simplify it by removing duplicate points or collinear segments.
However, for poly curve boundary like this, you’d have to do a curvature or angle comparison (or something even more complex) to identify what to remove.

After you have changed the geometry replace the existing geometry with the new one in the document.

#! python 3
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

select = rs.GetSurfaceObject("Select surface")
surfaceID = select[0]

rhinoObject = sc.doc.Objects.Find(surfaceID)
brepGeom = rhinoObject.Geometry

# Get a Rhino.Geometry.Collections.BrepEdgeList object for the surface
surfaceEdges = brepGeom.Edges
mergedEdges = surfaceEdges.MergeAllEdges(0.1)

# To ensure the changed geometry ends up in the document
# we need to replace the old object with the new geometry
objRef = Rhino.DocObjects.ObjRef(rhinoObject)
sc.doc.Objects.Replace(objRef, brepGeom)

print(f'Attempting to merge {mergedEdges} edges in the surface')
print(f'Finishing with {surfaceEdges.Count} edges')