TDD would help here, particularly if a human writes - or at least thoroughly reviews - the tests.

https://martinfowler.com/bliki/TestDrivenDevelopment.html