Sub topic 4
Modeling content as states and transitions
This chapter assumes you can read simple Python and have used a content workflow with writing, editing, and publishing steps. Here you will turn that workflow into a small state machine, so the code decides which moves are legal and nobody has to remember the rules.
Why model states explicitly
Without an explicit model, "status" is often a free-text field or a loose set of flags. Someone sets published = True on a draft nobody reviewed, or edits text after approval. A state machine closes these gaps. Every item is in exactly one state, and only listed transitions can move it to another state.
The states
The pipeline uses five states:
| State | Meaning |
|---|---|
| `draft` | The AI draft or a human edit is in progress. |
| `in_review` | A reviewer has the item. |
| `changes_requested` | The reviewer sent it back with notes. |
| `approved` | A reviewer other than the author accepted this exact text. |
| `published` | The item is live. This is the final state. |
Notice what is missing. No arrow goes from draft straight to approved or published. No arrow leaves approved except to published. The diagram is the policy.
Encoding the transitions
The simplest encoding is a dictionary that maps each state to the set of states it may move to. The following code runs as written:
from enum import Enum
class State(Enum):
DRAFT = "draft"
IN_REVIEW = "in_review"
CHANGES_REQUESTED = "changes_requested"
APPROVED = "approved"
PUBLISHED = "published"
ALLOWED = {
State.DRAFT: {State.IN_REVIEW},
State.IN_REVIEW: {State.APPROVED, State.CHANGES_REQUESTED},
State.CHANGES_REQUESTED: {State.IN_REVIEW},
State.APPROVED: {State.PUBLISHED},
State.PUBLISHED: set(),
}
def move(current, new):
if new not in ALLOWED[current]:
raise ValueError(f"cannot go from {current.value} to {new.value}")
return new
state = State.DRAFT
state = move(state, State.IN_REVIEW)
print(state.value)
try:
move(state, State.PUBLISHED)
except ValueError as error:
print(error)The first call prints in_review. The second raises an error, because publishing straight from review is not in the table. Keeping the rules in one dictionary means a reviewer or editor can audit the policy without reading any logic.
Rules that belong to the state
Transitions are not the only thing a state controls. The full Item class from this tutorial's example code adds three rules around the same table:
- Edits are limited by state. The
revisemethod only works indraftorchanges_requested. Once an item is in review or approved, its text is frozen. - Every move is logged. The
_movemethod appends the time, the actor, the old state, the new state, and a note. The audit trail chapter covers this log in detail. - Approval records a fingerprint. On approval, the item stores a SHA-256 hash of its text. The publish step later compares that hash to the current text.
Common errors
- Forgetting the loop back. If
changes_requestedhas no path toin_review, rejected items get stuck, and people work around the tool by copying text into new items. - Allowing edits in
approved. The approval then covers text nobody reviewed. The hash check at publish time is the backstop, but blocking the edit earlier gives a clearer error. - Adding states for every nuance. Labels such as "waiting for legal" or "waiting for design" are usually better as fields on the item. Add a state only when it changes which transitions are legal.
- Treating
publishedas editable. A correction should create a new item or version that goes through the pipeline again.
Exercise
A teammate proposes adding a direct transition from draft to published for "urgent, low-risk" items. Which part of this design does that weaken, and what could you do instead?
Show answer
It removes the guarantee that every published item was approved by someone other than the author, because the approval step is skipped entirely. Instead, keep the transition table unchanged and let the risk tier control how many approvals are needed (for example, one approval for low-risk items). Urgent items can then be fast, but they still pass through in_review and approved.