A brand document has two writers that do not know about each other. One is a person, correcting a field in the dashboard. The other is a background job that read the brand's site a minute ago and is about to apply what it found. The rule for what happens when they meet is short: the person wins, and the machine's disagreement is kept for the person to look at.
The problem
Three of the failures found in the old flow were the same collision in different timings.
- The customer corrected a field. A request carrying the model’s earlier value arrived afterwards and put it back.
- The customer confirmed their brand in one session. A re-read in another session overwrote what they had entered.
- The customer read site A, then B, then A again, and a result from the wrong reading was the one that stuck.
Each is a write that did not know what it was replacing. There are two separate questions inside that. Is the document I am writing still the one I read? And is the value I am replacing mine to replace?
The first question: revision

Every change to a brand goes through one function. The signature is shortened here.
async def write_brand(brand_id, workspace_id, mutate, *, expected_revision=None, attempts=5):
for _ in range(attempts):
brand = await load_brand(brand_id, workspace_id)
if expected_revision is not None and brand.revision != expected_revision:
raise RevisionConflictError(brand.revision)
out = mutate(brand)
base = brand.revision
brand.revision = base + 1
where = {"revision": base} if base else {"revision": {"$in": [0, None]}}
if await save_existing(brand, where=where):
return brand, out
raise RevisionConflictError(brand.revision)
The caller passes a function that changes the brand. write_brand loads the document, applies the function, and saves with a condition: the stored revision must still be the one that was read. save_existing is a conditional update that never inserts, so a brand deleted in the meantime is not brought back.
If another writer got there first, the condition fails and nothing is written. The loop loads the new state and applies the function again, up to five times. The change is therefore always applied to the latest document, never to a stale copy.
There are two kinds of caller, and they want different things from a conflict.
| Caller | Sends expected_revision | On a conflict |
|---|---|---|
| A person editing in the dashboard | yes, the revision their form was loaded with | the route answers 409 revision_conflict with the current revision, and the form reloads |
| A background job | no | the change is re-applied to the latest state |
A person who edits a stale form should be told, because their edit was based on something that is no longer there. A job applying rules has no such attachment to what it read. Its rules are evaluated again against the current document, which is what makes the second question answerable.
Documents created before the revision field existed have none. The condition treats a missing revision as zero, so old brands are writable without a migration.
The second question: origin
Each field has a metadata record beside it.
| Field of the record | Values |
|---|---|
origin | extracted or user |
basis | explicit, observed or inferred |
review | candidate or confirmed |
evidence | the quote records that support the value |
run_id, revision, updated_at | which readout and which write set it |
A field becomes user in three ways: the person edits it, the person provides it when creating the brand, or the person adopts a candidate. From then on, extraction treats it differently.
| Extraction proposes | The field’s origin is user | The field’s origin is extracted |
|---|---|---|
| the same value | the new evidence is added, the value is untouched | the value is refreshed with its evidence |
| a different value | filed as a candidate with reason conflicts_user | the value is replaced |
| a value rejected before | not proposed again | not proposed again |
The middle row is the point of the design. The site may have changed since the person wrote the field, and the new reading may be right. It may also be the model misreading a page. The system cannot tell which, so it does neither thing silently: the person’s value stays in force, and the reading is put in front of them with its evidence.
Because the job’s rules run inside write_brand, they run against the latest document. If a person’s edit lands while extraction is in flight, the retry sees origin: user on that field and files a candidate. The edit does not have to arrive first to win.
The third timing: which reading
The A, B, A case is handled one level up, by the run id described on the pipeline page. Starting a readout writes its id onto the brand through write_brand. Applying a result checks that id inside the same locked write. A result from an older run fails the check and is refused as superseded, whatever order the jobs finish in.
No extracted values from the browser
The earliest version of the fix was a rule for the client: do not send extracted values after the user has confirmed. That is a rule about timing in a browser, and it cannot be enforced there. The actual fix removed the path. The server applies extraction itself, and the browser’s save request carries only what the person typed. A test on the web side asserts that the browser never sends extracted values at all.
Failure modes
| What goes wrong | What happens | Regression test |
|---|---|---|
| Extraction finishes after the person confirmed | the person’s value is kept | test_f1_late_extraction_after_user_confirm_keeps_user_value |
| The person’s write lands in the middle of extraction | the person’s value wins | test_f1_user_write_landing_mid_extraction_wins |
| A re-read in another session | earlier human fields are kept | test_f5_rereading_in_another_session_keeps_prior_user_fields |
| A, B, A | the stale run is rejected | test_f6_a_b_a_stale_run_is_rejected |
| A stale form is submitted | 409 with the current revision | test_revision_increments_and_expected_revision_conflict_is_409 |
| A brand created before revisions existed | writable | test_legacy_brand_without_revision_field_is_writable |
| A candidate is adopted or rejected | applied, and a rejection is remembered | test_adopt_and_reject_candidates |
| A save fails | the run ends failed, and a retry applies | test_f4_failed_save_is_failed_not_success_and_a_retry_applies |
Limits I know about
Fields with no metadata are not protected. Protection depends on a record that says origin: user. A brand created before this pipeline has fields with no record at all. For generation those are treated as a person’s, and used. For extraction they are not protected, and a readout can replace them. This was a deliberate decision not to migrate existing data, and it means the promise in the title holds for values written since the pipeline shipped.
Five attempts is a bound, not a proof. A brand under continuous concurrent writes could exhaust the retries and surface a conflict to a background job. With one person and one job per brand that has not happened.
Candidates need a person. A conflict that nobody looks at leaves an outdated human value in force indefinitely. The cap of 60 pending candidates keeps the list from growing without limit and does nothing to get it read.
