Founder Notes
Your State Machine Has No States
I spent part of this week on a record that had contradicted itself, and the thing that kept me interested was that every piece of code which touched it was correct.
The setup was ordinary. Whether an item was finished and ready to be seen came down to a single predicate: several conditions combined into one expression, in one place, easy to read. The item is live, it has not been removed, its review came back approved, its owner is a real account rather than a sample one.
There was also a separate flag marking an item as a draft. It was not part of that predicate. Nothing that read the item consulted it.
I assumed that was the bug, and that the fix was to add the flag to the predicate. It was not the bug. It was the thing that made the real bug visible.
The generic setter
The only write path that could mark an item live did it the way a hundred write paths do it: take the incoming payload, walk its keys, apply each one to the record. The request that finished an item sent a single field — live, true — and the loop did exactly that and nothing more. The draft flag was not mentioned in the payload, so it was not touched.
That is correct behaviour for a generic setter. It is the whole point of a generic setter. It is also the wrong behaviour for a transition, and the code had no way to tell which of those two things it was being asked to be.
The half-undone transition
The second mechanism is the one I actually want to talk about.
Replacing one of an item's supporting documents did two things together: it reset the review to unknown, and it set the item back to draft. That pair is coherent. The item has changed in a way that means somebody needs to look at it again, and both fields say so.
Later, when the review was approved again, that path wrote the review field alone. Which is defensible — approving a review has no opinion about whether something is a draft, and a function that quietly reached over and changed a neighbouring flag would be the kind of surprise I complain about in other people's code.
So the item came out of the far side of that sequence with its review approved, its live flag never disturbed, and still marked as a draft. It had walked itself backwards into a combination of values that no intended transition produces.
Nobody owned the combination
Here is what I had wrong. I thought of that item as having a status, and of those fields as the way the status was stored. That is backwards. The item did not have a status. It had four independent fields, each with its own writer, and the status was something I was computing in my head.
Every writer was right about the field it owned. The document-replacement path was right to set two fields, because it understood a relationship between them. The re-approval path was right to set one, because it did not. The generic setter was right to touch only what it was given. Not one of them was wrong, and no reviewer looking at any one of them in isolation would have asked for a change.
What was missing was anyone whose job was the combination. There was no function that took a record from one named condition to another named condition and was accountable for every field that distinction implies. There was, in fact, exactly one function in the codebase that set both of the related flags together in the way a transition should — and the reason it had not prevented any of this is a separate story I will not get into here.
The generalisation
A state machine implemented as independent field writers does not have states. It has coordinates.
The difference matters because of what each one lets you check. If you have states, you can enumerate the legal ones, name the transitions between them, and ask whether a given change is one of them. If you have coordinates, every combination of values is reachable by definition, because reachability is just whichever writers happened to run and in what order. There is nothing to violate, so there is nothing to catch.
The test I now apply is narrow and has been useful twice since: pick a field that is part of what you would call an object's status, and find every line that writes it. If that set is larger than one, and the members of it do not know about each other, then the composite condition you believe the object is in is not a property of the object. It is a belief you hold about the object, and the code has never agreed to maintain it.