ash_onetime usage rules

Copy Markdown View Source
  • Use Ash >= 3.37.2 and < 4.0.0, AshPostgres ~> 2.15 and >= 2.15.2, and AshSql ~> 0.8 and >= 0.8.6. Consumers locked below those floors must update before adopting ash_onetime 1.9.2 (the Ash and AshPostgres floors were raised in 1.7.0, the AshSql floor in 1.9.0); see Upgrading.

  • Choose exactly one strategy for every protected action. Idempotency replays a classified stored result; one-time nonce protection rejects reuse. They are not interchangeable.

  • Give equal weight to the two dangerous misuses: never use idempotency response replay as anti-replay protection, and never let nonce admission inherit idempotency's optional untracked failure direction.

  • Declare a nonempty scope. Missing scope data is an error, never a global fallback. Include every tenant or principal boundary needed to prevent cross-scope blocking or replay.

  • On a resource using multitenancy strategy :attribute, put the tenant discriminator in the scope ({:attribute, <tenant_attribute>} or a {:tenant, module} resolver). Those tenants share one set of claim tables, so the library rejects a tenant-less scope at compile time. Context multitenancy isolates by schema and needs no scope entry for this.

  • Let the library derive operation identity from the protected resource and action. Do not substitute a caller-controlled operation name.

  • Bind idempotency keys to all request arguments and attributes that change the effect. A conflicting fingerprint is an error, not a replay.

  • PostgreSQL is authoritative. A cache may reduce response-payload reads after authoritative admission, but cannot grant or reject admission.

  • One-time nonce admission fails closed whenever authoritative state is unavailable or uncertain. It has no stored-response, external-effect, or configurable failure surface.

  • Idempotency may execute without a stored admission only when the store proves its statement was never dispatched and the DSL explicitly enables that branch. Ambiguity rejects.

  • Treat verification callbacks as trusted local code. Action input may carry raw token material but cannot assert verified keys, timestamps, algorithms, or verification state.

  • Use package-owned HMAC only when the same trust domain signs and verifies. Use Ed25519 or a verifier callback when signing and verification are separated.

  • Application-specific signature schemes belong behind verifier callbacks; do not implement them in this package.

  • Database effects use the protected resource's current AshPostgres repository and transaction. External effects require the idempotent execute/recover protocol and are forbidden for nonces.

  • Cleanup occurs only after the strategy-safe horizon. Caches and cleanup jobs never decide correctness; processing external effects are not ordinary expiry candidates.

  • Nonce max_age controls acceptance. Optional retain_for controls claim retention and defaults to max_age; it must be at least max_age, with retain_for + clock_skew bounded by 2,147,483,647 seconds. Use {count, unit} in the DSL window and integer seconds in Transaction.nonce/2. Read a transaction nonce's stored deadline with Transaction.nonce_retention_deadline/2, using the full authorized locator. Neither retention nor a deadline read grants admission outside the acceptance window.

  • A protected action's failure carries a typed :code that survives the Ash pipeline. Read it with AshOnetime.Error.code/1 to drive HTTP status; do not assume a blanket class→status mapping, because a family of server-fault and transport codes (e.g. :store_invariant, :outcome_unknown, :checkout_unavailable, :lock_timeout) are 500/503, not client input. See Errors and HTTP mapping for the full code→status table.

  • A protected action's success carries a replayed-vs-fresh signal. AshOnetime.replayed?/1 returns true (stored replay), false (fresh execution), or nil (untracked execution, primitive-return action, or unprotected). Treat nil as "cannot tell," not as "fresh." See Replay.

  • Under FORCE row-level security, run partition maintenance as the table owner (the prune decides inside a per-partition savepoint and needs no role or function). A non-owner driver keeps every partition — payload_partitions_kept in the cleanup counts — while claim cleanup keeps working; see Operations.

  • Treat :context_changed as a retry signal, not an error: re-read the context with AshOnetime.Boundary.read/3, decide again, and append with the fresh cutoff. Map it like other client-input-family failures (409-class), never as a 500.

  • Every context-boundary call carries an explicit :tenant; admission fails closed without one. Lock keys, rows, and checks are tenant-scoped — never share fact tables across trust boundaries by omitting it.

  • Context-boundary queries must be tag-anchored (every matching fact carries a named tag); a query naming no tag is refused. Do not bypass validation with raw SQL — the anchoring is what makes the advisory-lock set cover the context.

  • Prefer a counter row for hot numeric totals; prefer a context boundary for "nothing relevant changed since I looked" rules across records. See Context boundaries.

See the DSL guide, idempotency guide, nonce guide, errors guide, replay guide, and security model for examples and the complete contract.