'Double border" when autotile by gdscript

Godot Version

v4.5

Question

Hi I am new to Godot and game making. I have been trying to apply autotile to a procedural generated map with gdscript. I am not able to get the tiles to set as I intend. There is always a “double border” around areas of different terrains like this.

Every types of terrain has a set of tilesheet like such and they are all property painted with the corresponding terrain types:

I have tried to use set_cells_terrain_connect() and BetterTerrain, both get the same problem. What should I do to get the programmme to update either one of the terrain at borders but not both? Thanks.

func _process_tile_jobs()-> void:
	var processed := 0
	while processed < MAX_TILES_PER_FRAME and not pending_tile_jobs.is_empty():
		var job = pending_tile_jobs.pop_front()
		var coord: Vector2i = job["coord"]
		
		if job.get("is_water", false):
			shared_terrain_layer.set_cell(coord, 1, Vector2i(9, 2))
		else:
			BetterTerrain.set_cell(shared_terrain_layer, coord, job["terrain_id"])
		
		if job.has("chunk_pos"):
			var cp: Vector2i = job["chunk_pos"]
			if chunk_pending_jobs.has(cp):
				chunk_pending_jobs[cp] -= 1
				if chunk_pending_jobs[cp] <= 0:
					chunk_pending_jobs.erase(cp)
					chunks_waiting_update.append(cp)
		
		processed += 1
	
	for cp in chunks_waiting_update:
		if chunks.has(cp):
			var rect: Rect2i = chunks[cp]["rect"]
			BetterTerrain.update_terrain_area(shared_terrain_layer, rect, true)
	chunks_waiting_update.clear()

Have you tried drawing these terrains by hand to make sure they are set up correctly?
We don’t have the actual tileset for the image but it looks to me like there is more than just the border that is messing up.

Yes, I have. The terrains might not align perfectly when there are more than two types of terrains meeting each other, but it does not have this kind of ‘double border’. Sorry if the tiles look primitive, because I generated them in a rush for testing,.

I don’t think it is a double border. I think it is placing two incorrect pieces.
Going back to the first image here is an example of what I mean (sloppy cropping by me):

So if you could post the actual tileset used in the first image and/or manually draw the exact map from the first image that would be definitive.
If you do draw the map manually, try to mimic your algorithm by placing the tiles in the order it does.