# Scoped operations

**Problem.** `ListOrders` must only ever return the caller's tenant's orders, and the tenant id must never come from the client.

**Pattern.** Bind a persisted schema field to a named value that a required policy _provides_.

```ts
const policies = orders.policies({
  CurrentTenant: policy()
    .role("authentication")
    .provides({ tenantId: values.TenantId }),
});

const ListOrders = operation()
  .role("list")
  .input(schemas.OrderQuery)
  .output(schemas.OrderRead)
  .requires(policies.CurrentTenant)
  .scope({
    target: schemas.Order.field("tenantId"),
    source: { policy: policies.CurrentTenant, name: "tenantId" },
  });
```

The same shape appears on `detail`, `update` and `delete` operations in the maintained `core-journey` example, so a user can only reach their own record.

## Rules

- **`target`** must be a persisted field of a stored schema.
- **`source.policy`** must be a policy the operation `.requires(...)`, and **`source.name`** must be a value that policy `.provides(...)`.
- Use one binding object per persisted field.
- The compiler keeps both references explicit, sorts bindings deterministically and omits `scope` when none is declared.
- Runtime validation checks role, persistence, references and types.

## What packs do with it

A pack can add the constraint to every query, reject a client-supplied value for the target field, and fail closed when the trusted value is missing. Dryv does not evaluate the policy or run the query: your application provides the trusted value, and the generated code applies the constraint.
