Remote / Declared writes

Enforcing a postcondition with becomes

Attach becomes on a write to guarantee the resulting row satisfies a named Entity.refine state:

ts
const CrateFields = Entity.define(
  'Crate',
  Schema.Struct({ id: Schema.String, stock: Schema.Int, reserved: Schema.Int }),
)
const InStock = Entity.refine(CrateFields, 'InStock', ({ row }) =>
  Expr.gt(row.stock, row.reserved),
)
const SellCrate = Mutation.write(
  'SellCrate',
  Write.update(
    Entity.input(CrateFields, Schema.Struct({ id: Schema.String, quantity: Schema.Int }), {
      quantity: Entity.unmapped,
    }),
    { id: 'id', increment: { stock: 'quantity' }, becomes: InStock },
  ),
)
const soldOut = Write.ensured(SellCrate.write) // { id: 'becomes:InStock', entity: 'Crate', ... }

When becomes spans finite fields, Write.update proves or disproves it at declaration time. For numeric expressions like SellCrate, Write.update attaches a postcondition (becomes:InStock) checked by the writer at commit time (Invalid on failure, matched via Refusal.isInvalidBy(refusal, soldOut)) and analyzed by Laws.check.

For any write to commit, all three checks must pass together: authorization (Forbidden), preconditions (Unmet), and invariants plus postconditions (Invalid).

Retries: three different guarantees

Write kindRetry after lost responseTwo concurrent writes from one read
Sets fixed values (name = ?)Re-applies safely; returns updated rowLater write wins
Pinned with expect: 'revision'Returns Conflict; client re-readsSecond write returns Conflict
Increment/compute with ledgerReturns ledgered result; not re-appliedBoth writes apply

Setting done: true rather than toggling a boolean is naturally idempotent; revision pinning provides optimistic concurrency; ledgers deduplicate non-idempotent retries by operation ID.