This idiom comes from an old fable:
A man had sheep in a pen. One morning, he discovered a sheep was gone — it had escaped through a hole in the fence.
A neighbor said, “You should fix the hole.”
But the man thought, “Too late now, it’s already gone.”
The next day, another sheep was missing.
He finally fixed the fence.
After that, no more sheep were lost.
From then on, people said 亡羊补牢 (wáng yáng bǔ láo) to mean:
“Fixing the pen after losing sheep — it’s not too late.”