Sequence:
In this flow the changes user did we did not discover and we overwrote them.
To solve this we can do a “verify after write”. In step 2 we also read the latest item in the history. After write we verify that the history has gone ahead by only our change. If we detect a change in history, we re-read latest change before our and re-apply. And to be sure we do the verify after write once again.
Github seems not not expose history as API. We can poll a PR every so often, or at least on PR activities, and see if our changes has been overwritten by someone else, and bring it back.
The first race condition is not to solve for us without history API, all we can do is make the time between 2 and 4 as small as possible to make it less likely.