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

Two bands. write_brand, an optimistic lock on revision: load the brand and read its revision as base; apply the change and set revision to base plus one; conditional save only where revision still equals base; if it matched, the change and the revision land together; if it missed, reload and re-apply, up to 5 times. A note says a caller that sends expected_revision and is behind gets 409 revision_conflict. When extraction meets a field a person wrote: the field origin is user, set by an edit, by creating the brand, or by adopting a candidate; if the value is the same, only the new evidence is added; if the value is different, it is kept aside as a candidate marked conflicts_user; if it was rejected before, the same proposal is never raised again. A note says the browser never writes extracted values and extraction is applied on the server under these rules.
Figure 1. Write rules. Above, every write to a brand is conditional on the revision it read. Below, what extraction may do to a field that a person set.

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.

CallerSends expected_revisionOn a conflict
A person editing in the dashboardyes, the revision their form was loaded withthe route answers 409 revision_conflict with the current revision, and the form reloads
A background jobnothe 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 recordValues
originextracted or user
basisexplicit, observed or inferred
reviewcandidate or confirmed
evidencethe quote records that support the value
run_id, revision, updated_atwhich 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 proposesThe field’s origin is userThe field’s origin is extracted
the same valuethe new evidence is added, the value is untouchedthe value is refreshed with its evidence
a different valuefiled as a candidate with reason conflicts_userthe value is replaced
a value rejected beforenot proposed againnot 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 wrongWhat happensRegression test
Extraction finishes after the person confirmedthe person’s value is kepttest_f1_late_extraction_after_user_confirm_keeps_user_value
The person’s write lands in the middle of extractionthe person’s value winstest_f1_user_write_landing_mid_extraction_wins
A re-read in another sessionearlier human fields are kepttest_f5_rereading_in_another_session_keeps_prior_user_fields
A, B, Athe stale run is rejectedtest_f6_a_b_a_stale_run_is_rejected
A stale form is submitted409 with the current revisiontest_revision_increments_and_expected_revision_conflict_is_409
A brand created before revisions existedwritabletest_legacy_brand_without_revision_field_is_writable
A candidate is adopted or rejectedapplied, and a rejection is rememberedtest_adopt_and_reject_candidates
A save failsthe run ends failed, and a retry appliestest_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.