Builds %AshVault.Context{} values at the Ash boundary — the only place they are
constructed.
The two Ash callback context structs are different shapes and neither is something the crypto core should have to pattern-match:
Ash.Resource.Change.Context [:actor, :tenant, :authorize?, :tracer,
bulk?: false, source_context: %{}]
Ash.Resource.Calculation.Context [:actor, :tenant, :authorize?, :tracer, :domain,
:resource, :type, :constraints, :arguments,
source_context: %{}]So both are normalized to one plain map before being put in :ash_context:
%{tenant: tenant, actor: actor, source_context: source_context, phase: :write | :read}Three traps this module exists to handle:
source_contextis stale inside a change. It is snapshotted once duringfor_create/for_update; onlytenantis refreshed per change. Anything a caller sets withAsh.Changeset.set_context/2afterwards never reacheschange/3, soAshVault.Changes.Encryptrebuilds it fromchangeset.contextinside the hook.- On the write path
changeset.tenantwins — it is authoritative and current. On the read path the calculation context's:tenantwins, falling back tosource_context[:private][:tenant], which is the same fallback Ash's own calculation builder uses. - The tenant here is the RAW tenant, never normalized through
Ash.ToTenant— it may be a whole%Organization{}struct. Normalizing it to a stable binary scope key isAshVault.Scopes.AshTenant's job.
There is no context \\ nil default anywhere: a missing tenant is not a fallback, it
is AshVault.Errors.MissingScope.
Context.field resolves the immutable stored_as binding identity. Logical field
names remain on calculations, arguments and decryption policy callback context.
Deviation from the spec
EXTENSION_SPEC §4 puts these constructors on AshVault.Context itself. They live here
instead so the crypto-core module — which must not depend on Ash at runtime — stays
free of Ash references.
Summary
Functions
Build a context for a generic lifecycle action from its input and callback context.
Build a context for the read path from a resource and the calculation callback context.
Build a context for the write path from a changeset and the change callback context.
Build a context for a lookup-token computation from a query.
Functions
@spec from_action_input(Ash.ActionInput.t(), map()) :: AshVault.Context.t()
Build a context for a generic lifecycle action from its input and callback context.
Ash.Resource.Actions.Implementation.Context has no :resource, so the resource comes
from input.resource.
@spec from_calculation(module(), atom(), map()) :: AshVault.Context.t()
Build a context for the read path from a resource and the calculation callback context.
@spec from_changeset(Ash.Changeset.t(), atom(), map()) :: AshVault.Context.t()
Build a context for the write path from a changeset and the change callback context.
@spec from_query(Ash.Query.t(), atom(), map()) :: AshVault.Context.t()
Build a context for a lookup-token computation from a query.
query.tenant wins, for the same reason changeset.tenant wins on the write path: it
is authoritative and current, while a callback context's copy was snapshotted earlier.
That symmetry is what makes a lookup resolve the same scope the matching write did —
and therefore what makes a query with no tenant raise AshVault.Errors.MissingScope
exactly as the write would, instead of filtering on a token nobody stored and
returning zero rows.