Entity / One domain declaration

How refusals map back to declarations

Failure conditionClient seesOriginRecovery
Caller lacks permissionForbiddenfoldkit-guardUnauthorized; keep hidden
Row is in wrong stateUnmetWrite from or whenRe-read updated row
Row revision changedConflictWrite expectRe-read and retry
Resulting row breaks invariantInvalid + message / pathEntity.invariantShow error at path
Input fails schema validationField errorField SchemaFix input in form

Four edge cases to expect:

  • Stale client prediction: If another user reduced stock before your Reserve arrived, the server returns Invalid, Remote discards the optimistic layer, and Data.refusal surfaces the reason.
  • Missing fields on client: When the client lacks stock, it sends Reserve directly and lets the server decide.
  • Cross-field form invariants: Form errors from multi-field invariants clear on any edit to the form, since editing another referenced field might satisfy the rule.
  • Database CHECK limits: State invariants compile to SQL CHECK constraints via checkConstraints, whereas transition invariants (before + after) are enforced only by the server writer (which logs ServerOnlyRules once at startup).

Use Law.check from foldkit-remote/testing to verify automatically that client predictions match server execution; see examples/writes.

Five rules that prevent drift

  • Declare relations together: Pass all related Entities to one Entity.relate call.
  • Keep Selections declarative: Selections carry schemas and compile into Remote's requirement graph without fetching directly.
  • Share predicates as values: One Expr drives SQL compilation, in-memory evaluate, and browser Judge.
  • Bind the guarded descriptor: Add derived members and invariants to the Entity first, then pass the result of Guards.attach to your server binding so guard rules are enforced at startup.
  • Map operation inputs via Entity.input: Forms, Entity.selectFor, and Entity.valuesFor all derive from that mapping.