Order by Creation, Not by Clock

My merge gate kept reporting a superseded CI run as green. Five review rounds and five fixes later, the bug turned out to be one shape: comparing timestamps from different actors' lifecycles. The fix was to order by creation order, and where no id exists, to compare only like for like.

December 02, 2026
Bob
7 min read

A rerun is supposed to replace the old result. GitHub gives you a new check run for the same workflow and job name, and every consumer is supposed to look at the newest one. My merge gate tried to do exactly that. It kept looking at the old one.

Not in a subtle, occasionally-wrong way. The gate reported checks_green: true for a PR whose rerun had not started yet. That is the fail-open direction: the gate exists to answer “may I merge this?”, and it answered yes on evidence from a run that had been superseded.

The fix took five review rounds. Every round fixed a real bug and exposed the next one, because I kept solving the same shape from a different angle. The shape is this:

A check run’s state is a small state machine, and different states carry different timestamps. Comparing those timestamps across two runs is comparing numbers that do not mean the same thing.

The setup

The function is latest_checks. It takes the check runs for a commit, groups them by (workflow, name), and keeps the newest entry per group. checks_green then reads the surviving entries. If any surviving entry is pending or failed, CI is not green.

The intent is simple. The implementation is where it gets interesting, because GitHub’s CheckRun object is not a flat record:

  • a queued job has status: QUEUED, no startedAt, no completedAt
  • a running job has status: IN_PROGRESS and a startedAt
  • a completed job has startedAt, completedAt, and a conclusion
  • a job cancelled before it ever started has status: COMPLETED, conclusion: CANCELLED, no startedAt, and a real completedAt

Four shapes, four different subsets of populated fields. And the API serialises absent startedAt as Go’s zero time:

"startedAt": "0001-01-01T00:00:00Z"

That is a truthy string. It sorts lexically as older than everything, which is semantically right and catastrophically wrong for the comparison, because “no start time” is not “started a long time ago”. It means “this job has not begun”. The comparison read it as a fact about ordering and picked the wrong survivor.

Round 1: the zero time

First fix: treat the zero time and the Unix epoch as “no start time”, and pass such entries through the collapse untouched, so checks_green sees the pending status and fails closed. Tests for both sentinels. That is a good fix for the bug the reviewer described.

Round 2: passing through re-broke the original bug

A job cancelled while still queued has status: COMPLETED, conclusion: CANCELLED, an unset startedAt, and a real completedAt. Under the passthrough rule it survived every collapse, including the one where a newer rerun had already completed successfully. Now the gate reported red forever: the cancelled ghost outlived the run that replaced it.

So the ordering grew: startedAt, then completedAt, then “unset is newest”. Locally sensible. Globally still wrong.

Round 3: two runs, two lifecycles

Here is the part that makes this a distributed-systems problem rather than a parsing problem. A re-trigger does not happen at a single instant across both runs. The new job can start before GitHub gets around to cancelling the old queued job. So this ordering is perfectly possible:

old run (queued, then cancelled):  startedAt=<unset>  completedAt=10:05:31
new run (the rerun):               startedAt=10:05:29  completedAt=<unset>

The new run started before the old run finished being cancelled. Any comparison that mixes startedAt and completedAt across the two runs is comparing an event from one lifecycle against an event from another. The numbers are individually correct and jointly meaningless. The superseded CANCELLED entry still won, because 10:05:31 > 10:05:29.

The fix: when both entries expose a creation-ordered id — databaseId, or the /job/<id> path in detailsUrl — order on the id. Ids are allocated in creation order by one authority. They are a total order that no clock skew, no retry, and no async cancellation can reorder.

Round 4: the other kind of check

latest_checks also handles legacy StatusContext entries, which are a different API object entirely: they carry context, not name, and they have no timestamps at all. Keying them into the same collapse made their missing timestamps read as “newest”, so a green status could shadow a failing check run with the same label. Fix: collapse on name only, and pass statuses through untouched. Different type, different rule. Do not force two schemas into one comparison.

Round 5: when one side has no id

The id fast path only fired when both entries in a group carried an id. Any other combination fell back to the timestamp comparison — the same cross-run mixing from round 3. So the ghost could still win whenever either side lacked an id.

The final fix has two parts, and the second is the interesting one:

  1. Compare timestamps only like for like: startedAt against startedAt, then completedAt against completedAt. Never mix.
  2. If that still does not decide it, use a semantic invariant instead of inventing an ordering: an entry that completed as CANCELLED without ever starting yields to its sibling. GitHub only cancels a still-queued job when a newer run for the same context supersedes it. So that entry is the older run, regardless of what its completedAt says.

That second rule is not a heuristic bolted on to make a test pass. It is the domain fact that makes the comparison well-defined in the first place. When you cannot order two events by data, you can sometimes order them by what the system guarantees about how they came to exist.

The general shape

Three rules came out of this, and they generalize well beyond GitHub check runs:

1. Prefer creation-ordered ids to timestamps for ordering anything. Timestamps answer “when did this happen”, which is not the same question as “which one is newer” when the two events come from different actors. A monotonic id from a single allocator answers the second question directly. If the API gives you both, use the id and treat the timestamp as display data.

2. Never compare fields that are only populated in some states. startedAt exists for running and completed jobs, and not for queued ones. Comparing it against another run’s startedAt is fine; comparing it against a completedAt is a type error dressed as arithmetic. If the fallback needs a total order over heterogeneous states, that is a signal that the fallback is wrong, not that it needs more branches.

3. When the data cannot decide, encode the system’s invariant explicitly. “A queued job is only cancelled by a superseding run” is a fact about GitHub’s behaviour. It resolves the case where timestamps interleave. Written down as a named rule with a comment explaining why it holds, it is maintainable. Written down as if a is None or b is None: return True, it is a landmine.

What this cost

Five rounds, five pushes, five re-reviews, and about 410 tests across two files by the end. That is the honest number for a bug in a merge gate. I would rather spend it than ship a gate that says yes on evidence it already knows is stale.

The part I keep thinking about: every one of the five fixes was locally correct and reviewed as such. Each one made the code better at the thing the previous review had asked for, and the next review found the next state combination. That is not a failure of review. It is what happens when the underlying model — “newest by timestamp” — is wrong, and you keep patching the model instead of replacing it.

The replacement is one sentence: order by creation, and only compare like for like. The other four rounds were the cost of not writing that sentence first.