@ultimat3/policy 31.22.3 → 31.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CLAUDE.md CHANGED
@@ -180,7 +180,7 @@ reappearing there is a failing test.
180
180
  | File | Job |
181
181
  |---|---|
182
182
  | `policy.ts` | `can`/`allow`/`deny`/`and`/`or`/`not` + decision recording, and `policyPermissions` |
183
- | `scope.ts` | `scoped(policy, scope)` / `policyScope` — the rule's SQL projection, a conjunction in entity's predicate shape restated structurally (same tier). Not tenancy; combinators carry none |
183
+ | `scope.ts` | `scoped(policy, scope)` / `policyScope` — the rule's SQL projection: a `ScopeNode` tree (condition, `or`, `and`, `via`) in entity's `PredicateNode` shape restated structurally (same tier), frozen at every depth. Not tenancy; combinators carry none |
184
184
  | `policy-anonymous.ts` | `admitsAnonymous` — the one question a SURFACE asks of a built tree, apart from the file that builds them |
185
185
  | `evaluate.ts` | the single entry point; builds the trace, emits the one decision event |
186
186
  | `decisions.ts` | the `policy.decision` bus event — built only while subscribed, one call site, never PII. No slot (`X_INSTRUMENT_SLOT_FORBIDDEN`) |
package/README.md CHANGED
@@ -119,10 +119,10 @@ arguments, so no predicate is ever consulted and the tree alone decides. `true`
119
119
  ## Scope — the rule's SQL projection
120
120
 
121
121
  `scoped(policy, scope)` (`As of 2026-11`) attaches `scope(actor) → where` to a policy: the
122
- conjunction a read hands the database, in the entity chain's predicate shape (`{ column, op,
123
- value }`), so the visible rows arrive as one page an index can serve — not a JS filter over an
124
- over-fetch that leaves a viewer with many hidden rows a short page. One definition, two
125
- projections. The policy decides exactly as before; the JS rule stays the authority.
122
+ predicate a read hands the database, in the entity chain's shape, so the visible rows arrive as ONE
123
+ statement's page — not a JS filter over an over-fetch that leaves a viewer with many hidden rows a
124
+ short page. One definition, two projections. The policy decides exactly as before; the JS rule stays
125
+ the authority and still runs on every row returned.
126
126
 
127
127
  ```ts
128
128
  import type { Actor } from '@ultimat3/core';
@@ -133,24 +133,54 @@ const postRule = can('post:read');
133
133
 
134
134
  export const seePost = scoped(postRule, (actor) =>
135
135
  actor === null
136
- ? [{ column: 'audience', op: 'eq', value: 'public' }, { column: 'deletedAt', op: 'is-null' }]
137
- : [{ column: 'orgId', op: 'eq', value: actor.orgId }],
136
+ ? [{ column: 'audience', op: 'eq', value: 'public' }]
137
+ : [
138
+ {
139
+ or: [
140
+ { column: 'authorId', op: 'eq', value: actor.id },
141
+ { column: 'audience', op: 'eq', value: 'public' },
142
+ {
143
+ and: [
144
+ { column: 'audience', op: 'eq', value: 'friends' },
145
+ {
146
+ via: { column: 'authorId', entity: 'friendships', target: 'friendId' },
147
+ where: [{ column: 'userId', op: 'eq', value: actor.id }],
148
+ },
149
+ ],
150
+ },
151
+ ],
152
+ },
153
+ ],
138
154
  );
139
- policyScope(seePost, actor); // the frozen where, or `undefined` for a policy nobody scoped
155
+ policyScope(seePost, actor); // the tree, frozen at every depth, or `undefined` for none
140
156
  ```
141
157
 
142
- | Answer | Means |
158
+ | Node (`ScopeNode`) | Means |
159
+ |---|---|
160
+ | `{ column, op, value? }` | a condition. `op` is one of `eq neq in gt gte lt lte is-null is-not-null` |
161
+ | `{ or: [...] }` | any member holds; `[]` holds for no row |
162
+ | `{ and: [...] }` | every member holds — an `or` arm that needs two conditions |
163
+ | `{ via: { column, entity, target }, where: [...] }` | `column in (select target from entity where …)`: `entity` by name, its tenant and soft delete applied inside |
164
+
165
+ | `policyScope` answers | Means |
143
166
  |---|---|
144
167
  | `undefined` | no scope declared: nothing is handed down |
145
168
  | `[]` | every row (an admin) — an empty conjunction |
146
- | `[…]` | every condition must hold. `op` is one of `eq neq in gt gte lt lte is-null is-not-null` |
147
-
169
+ | `[…]` | every top-level node must hold. At most four levels deep (`X_ENTITY_PREDICATE_NODE_INVALID`) |
170
+
171
+ - **One statement.** A repository read takes the tree with `@ultimat3/entity`'s
172
+ `.andWhereNode(node)`; a `query` read composes it and hands it to the provider on
173
+ `RowWindow.nodes`. A `via` is a subselect, never a second round trip.
174
+ - **Refused, never guessed** (`X_QUERY_SCOPE_UNCOMPOSABLE`): an `or`/`via` scope on a LIVE read (the
175
+ matcher patches a flat shape) and a `via` over `from()` given a list of rows.
176
+ - **A `via` cannot cross tenants.** Its subselect is planned through the target's own tenant guard;
177
+ an untenanted target behind a tenanted source must declare `shared:` or `global:` (`X_TENANCY_UNSCOPED`).
148
178
  - **A scope belongs to the clause it is declared on.** `and`/`or`/`not` over a scoped policy carry
149
179
  none: the conjunction of children's scopes is not the scope of `or`. Scope the outermost policy.
150
180
  - **Not tenancy.** Tenant isolation is the entity layer's (`where({ orgId })`, the tenancy guard);
151
181
  a scope narrows what one actor sees inside it and never replaces it.
152
- - Composing it into a read is `@ultimat3/query`'s; a scope that disagrees with its rule on the
153
- exercise seed is to be a finding (`X_POLICY_SCOPE_MISMATCH`, registered with its raiser).
182
+ - A scope that disagrees with its rule on the exercise seed is to be a finding
183
+ (`X_POLICY_SCOPE_MISMATCH`, registered with its raiser).
154
184
 
155
185
  ## Four surfaces, four adapters, one rule
156
186
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/policy",
3
- "version": "31.22.3",
3
+ "version": "31.24.0",
4
4
  "description": "The one authz rule, evaluated identically in every surface",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -42,6 +42,6 @@
42
42
  "test": "bun test"
43
43
  },
44
44
  "dependencies": {
45
- "@ultimat3/core": "31.22.3"
45
+ "@ultimat3/core": "31.24.0"
46
46
  }
47
47
  }
package/src/index.ts CHANGED
@@ -73,7 +73,16 @@ export {
73
73
  roleDefinitions,
74
74
  rolesGranting,
75
75
  } from './roles';
76
- export type { PolicyScope, ScopeColumn, ScopeCondition, ScopeOperator } from './scope';
76
+ export type {
77
+ PolicyScope,
78
+ ScopeAnd,
79
+ ScopeColumn,
80
+ ScopeCondition,
81
+ ScopeNode,
82
+ ScopeOperator,
83
+ ScopeOr,
84
+ ScopeVia,
85
+ } from './scope';
77
86
  export { policyScope, scoped } from './scope';
78
87
  export type {
79
88
  DenialStatus,
package/src/scope.ts CHANGED
@@ -30,7 +30,7 @@ export type ScopeOperator =
30
30
  */
31
31
  export type ScopeColumn<R> = unknown extends R ? string : Extract<keyof NonNullable<R>, string>;
32
32
 
33
- /** One conjunct: entity's `Predicate` (`{ column, op, value }`), typed over the policy's row. */
33
+ /** One condition: entity's `Predicate` (`{ column, op, value }`), typed over the policy's row. */
34
34
  export interface ScopeCondition<R = unknown> {
35
35
  readonly column: ScopeColumn<R>;
36
36
  readonly op: ScopeOperator;
@@ -38,21 +38,52 @@ export interface ScopeCondition<R = unknown> {
38
38
  readonly value?: unknown;
39
39
  }
40
40
 
41
+ /** Any one member holds. `[]` holds for no row — a scope that admits nobody. */
42
+ export interface ScopeOr<R = unknown> {
43
+ readonly or: readonly ScopeNode<R>[];
44
+ }
45
+
46
+ /** Every member holds — the conjunction one arm of an `or` needs (`friends` AND a friend's). */
47
+ export interface ScopeAnd<R = unknown> {
48
+ readonly and: readonly ScopeNode<R>[];
49
+ }
50
+
51
+ /**
52
+ * A relation hop: the row's `column` is the `target` column of some row of `entity` (by NAME)
53
+ * matching `where` — `author_id in (select friend_id from friendships where …)`. The inner level is
54
+ * the target's, untyped here; its tenant and soft delete apply inside, as a read of it would.
55
+ */
56
+ export interface ScopeVia<R = unknown> {
57
+ readonly via: {
58
+ readonly column: ScopeColumn<R>;
59
+ readonly entity: string;
60
+ readonly target: string;
61
+ };
62
+ readonly where: readonly ScopeNode[];
63
+ }
64
+
41
65
  /**
42
- * Every condition must hold — a conjunction, because the chain's `where` is one. `[]` is "every
66
+ * One node of a scope — `@ultimat3/entity`'s `PredicateNode`, restated structurally (same tier):
67
+ * a condition, an `or` or `and` group, or a `via` hop. At most four levels deep (entity refuses).
68
+ */
69
+ export type ScopeNode<R = unknown> = ScopeCondition<R> | ScopeOr<R> | ScopeAnd<R> | ScopeVia<R>;
70
+
71
+ /**
72
+ * Every node must hold — the top level is a conjunction, as the chain's `where` is. `[]` is "every
43
73
  * row" (an admin), which is not the same as a policy with no scope: that one hands NOTHING down and
44
74
  * its read stays the JS rule over whatever was read.
45
75
  *
46
- * AND-only, decided (2026-11): no `{ or: [...] }` group. A scope lands as `@ultimat3/query`
47
- * `Builder` filters, so it reaches the live matcher's shape, every provider's `RowWindow.filters`
48
- * and the entity chain — four conjunctive contracts an OR would widen at once. A rule whose
49
- * visibility is a disjunction over a graph (a friend OR the author OR public, minus a block set)
50
- * stays unscoped: its read keeps the JS rule over what it reads.
76
+ * `or` and `via`, decided (2026-11, reversing the AND-only decision of earlier that month): apps
77
+ * whose visibility is a disjunction over a graph — the author, OR a friend, OR public, minus a block
78
+ * set — over-fetched a feed and ORed in JS, which is the short page `scope` exists to end. The
79
+ * whole scope still compiles to ONE statement (`@ultimat3/entity`'s `andWhereNode`). Where a source
80
+ * cannot take a tree — the live matcher's flat shape, an in-memory `from()` over a `via` — the read
81
+ * is refused (`X_QUERY_SCOPE_UNCOMPOSABLE`), never served unscoped.
51
82
  *
52
83
  * Called only for an actor the policy's own gate admitted: a caller refused before the read never
53
84
  * reaches it. Pure and synchronous, like a predicate — it runs once per read, not once per row.
54
85
  */
55
- export type PolicyScope<R = unknown> = (actor: Actor | null) => readonly ScopeCondition<R>[];
86
+ export type PolicyScope<R = unknown> = (actor: Actor | null) => readonly ScopeNode<R>[];
56
87
 
57
88
  /**
58
89
  * `policy`, deciding exactly as before, carrying `scope` beside it: one definition, two projections
@@ -91,14 +122,28 @@ export const scoped = <I = unknown, R = unknown>(
91
122
  );
92
123
  };
93
124
 
125
+ /** A node and everything under it, frozen — a caller still holding the tree cannot edit the read. */
126
+ const frozen = (node: ScopeNode): ScopeNode => {
127
+ if ('or' in node) return Object.freeze({ or: Object.freeze(node.or.map(frozen)) });
128
+ if ('and' in node) return Object.freeze({ and: Object.freeze(node.and.map(frozen)) });
129
+ if ('via' in node) {
130
+ return Object.freeze({
131
+ via: Object.freeze({ ...node.via }),
132
+ where: Object.freeze(node.where.map(frozen)),
133
+ });
134
+ }
135
+ return Object.freeze({ ...node });
136
+ };
137
+
94
138
  /**
95
- * The where a read composes for `actor`, frozen, or `undefined` when the policy declares no scope.
96
- * What `@ultimat3/query` calls — never `policy.scope` directly, so the freeze cannot be skipped.
139
+ * The where a read composes for `actor`, frozen at every depth, or `undefined` when the policy
140
+ * declares no scope. What `@ultimat3/query` calls — never `policy.scope` directly, so the freeze
141
+ * cannot be skipped.
97
142
  */
98
143
  export const policyScope = <I = unknown, R = unknown>(
99
144
  policy: Policy<I, R>,
100
145
  actor: Actor | null,
101
- ): readonly ScopeCondition[] | undefined => {
146
+ ): readonly ScopeNode[] | undefined => {
102
147
  if (policy.scope === undefined) return undefined;
103
- return Object.freeze(policy.scope(actor).map((condition) => Object.freeze({ ...condition })));
148
+ return Object.freeze(policy.scope(actor).map(frozen));
104
149
  };