@@ -65,7 +65,7 @@ import { SystemObjectName } from '@objectstack/spec/system';
6565import type { AuthzPosture , TenancyPosture } from '@objectstack/spec/security' ;
6666import { postureEnforcesWall } from '@objectstack/spec/security' ;
6767import { isValueDomainMember } from '@objectstack/spec/shared' ;
68- import type { AuthSessionApi } from '@objectstack/spec/contracts' ;
68+ import type { AuthSessionApi , EffectivePermissionSetOrigin } from '@objectstack/spec/contracts' ;
6969
7070import { ApiKeyLifecycleRefusalError , resolveApiKeyAdmission } from './api-key.js' ;
7171import type { ApiKeyLifecyclePolicySource , ApiKeyRefusalReason } from './api-key.js' ;
@@ -879,6 +879,21 @@ export interface UserAuthzGrants {
879879 audience ?: 'external' ;
880880 /** The user's unique email (`sys_user`), for `current_user.email` owner RLS. */
881881 email ?: string ;
882+ /**
883+ * [ADR-0090 D6] Where each name in `permissions` came from, keyed by name —
884+ * present only when the caller asked for it
885+ * ({@link ResolveUserAuthzGrantsOptions.withPermissionOrigins}). Recorded at
886+ * the derivation sites themselves, so it cannot disagree with them: `direct`
887+ * where an applying grant row of the user's own names an active set (the
888+ * `directPermissions` channel), `position` / `default` where a held
889+ * position's definition in force names it (`default` for the `everyone`
890+ * audience anchor, whose bindings are the default ones), and `derived` where
891+ * the configured platform administrators (§6b-config) or the `ai_access` flag
892+ * (§7) confer the name. Every name in `permissions` the resolution derived has
893+ * at least one origin, each origin listed once, in the order the resolution
894+ * reached it; a `seedPermissions` name the caller supplied has none.
895+ */
896+ permissionOrigins ?: Record < string , EffectivePermissionSetOrigin [ ] > ;
882897}
883898
884899export interface ResolveUserAuthzGrantsOptions {
@@ -920,6 +935,16 @@ export interface ResolveUserAuthzGrantsOptions {
920935 * would then trust.
921936 */
922937 bypassGrantsCache ?: boolean ;
938+ /**
939+ * [ADR-0090 D6] Record where each permission-set name came from, in
940+ * {@link UserAuthzGrants.permissionOrigins}. For a surface that DESCRIBES a
941+ * user's access (`ISecurityService.describeEffectiveAccess`), never for
942+ * enforcement: the envelope is otherwise byte-identical to a resolution
943+ * without it. It implies {@link bypassGrantsCache}, for the explainer's ruled
944+ * reason: a description is what an administrator reads to verify a change
945+ * took effect, and an origin map is recorded only by a resolution that ran.
946+ */
947+ withPermissionOrigins ?: boolean ;
923948}
924949
925950/**
@@ -1839,12 +1864,29 @@ export async function resolveUserAuthzGrants(
18391864 // read HERE, before any read of this resolution is issued. Outside that
18401865 // scope (every direct caller of this function) it is `undefined` and nothing
18411866 // changes.
1842- const requestMemo = openRequestGrantsMemo ( ql , userId , opts , readWriteEpoch ( ql ) ) ;
1867+ // [ADR-0090 D6] An origin map is recorded only by a resolution that runs, so
1868+ // asking for one is a fresh resolution (see `withPermissionOrigins`).
1869+ const cacheOpts = opts . withPermissionOrigins ? { ...opts , bypassGrantsCache : true } : opts ;
1870+ const requestMemo = openRequestGrantsMemo ( ql , userId , cacheOpts , readWriteEpoch ( ql ) ) ;
18431871 if ( requestMemo ?. hit ) return requestMemo . hit ;
18441872
1845- const grantsCache = openUserGrantsCache ( ql , userId , opts ) ;
1873+ const grantsCache = openUserGrantsCache ( ql , userId , cacheOpts ) ;
18461874 if ( grantsCache ?. hit ) return grantsCache . hit ;
18471875
1876+ // [ADR-0090 D6] The origin record, when asked for: each origin once per name,
1877+ // in the order the derivation below reaches it.
1878+ const origins = opts . withPermissionOrigins ? new Map < string , EffectivePermissionSetOrigin [ ] > ( ) : undefined ;
1879+ const recordOrigin = ( name : string , origin : EffectivePermissionSetOrigin ) : void => {
1880+ if ( ! origins ) return ;
1881+ const list = origins . get ( name ) ?? [ ] ;
1882+ const same = ( o : EffectivePermissionSetOrigin ) : boolean =>
1883+ o . kind === origin . kind
1884+ && ( o . kind !== 'position' || ( origin . kind === 'position' && o . position === origin . position ) )
1885+ && ( o . kind !== 'derived' || ( origin . kind === 'derived' && o . source === origin . source ) ) ;
1886+ if ( ! list . some ( same ) ) list . push ( origin ) ;
1887+ origins . set ( name , list ) ;
1888+ } ;
1889+
18481890 const { tenantId } = opts ;
18491891 const grants : UserAuthzGrants = {
18501892 positions : [ ] ,
@@ -2138,9 +2180,13 @@ export async function resolveUserAuthzGrants(
21382180 if ( disabled . positions . size > 0 ) {
21392181 grants . positions = grants . positions . filter ( ( n ) => ! disabled . positions . has ( n ) ) ;
21402182 }
2141- for ( const setNames of setsInForceByPosition ( grants . positions , namedByPosition , disabled ) . values ( ) ) {
2183+ for ( const [ positionName , setNames ] of setsInForceByPosition ( grants . positions , namedByPosition , disabled ) ) {
21422184 for ( const setName of setNames ) {
21432185 if ( ! boundSetNames . includes ( setName ) ) boundSetNames . push ( setName ) ;
2186+ // [ADR-0090 D5] The `everyone` anchor's bindings are the default ones.
2187+ recordOrigin ( setName , positionName === 'everyone'
2188+ ? { kind : 'default' }
2189+ : { kind : 'position' , position : positionName } ) ;
21442190 }
21452191 }
21462192
@@ -2168,6 +2214,7 @@ export async function resolveUserAuthzGrants(
21682214 // [#25799] The direct channel, as the explainer reads it: a name this
21692215 // resolution confers through the user's own grant row.
21702216 if ( active && ! grants . directPermissions . includes ( name ) ) grants . directPermissions . push ( name ) ;
2217+ if ( active ) recordOrigin ( name , { kind : 'direct' } ) ;
21712218 // platform_admin (ADR-0068 D2) is DERIVED from the anchoring admin_full_access
21722219 // USER grant — the single source of truth (no trusted stored boolean). The
21732220 // set is the catalog's one `admin_full_access` (ADR-0131 D4); a grant the
@@ -2235,6 +2282,7 @@ export async function resolveUserAuthzGrants(
22352282 // envelope a grant-derived one carries.
22362283 hasPlatformAdminGrant = true ;
22372284 if ( ! grants . permissions . includes ( ADMIN_FULL_ACCESS ) ) grants . permissions . push ( ADMIN_FULL_ACCESS ) ;
2285+ recordOrigin ( ADMIN_FULL_ACCESS , { kind : 'derived' , source : 'platform_admin_config' } ) ;
22382286 for ( const p of ADMIN_FULL_ACCESS_CAPABILITIES . systemPermissions ?? [ ] ) {
22392287 if ( ! grants . systemPermissions . includes ( p ) ) grants . systemPermissions . push ( p ) ;
22402288 }
@@ -2282,9 +2330,26 @@ export async function resolveUserAuthzGrants(
22822330
22832331 // 7. [cloud ADR-0024 §7] Env-side AI seat: synthesize the `ai_seat` capability from the
22842332 // boolean sys_user.ai_access (sqlite returns 1/0; memory returns boolean).
2285- if ( ! grants . permissions . includes ( 'ai_seat' ) ) {
2333+ // An origin record reads the flag even when another channel already named
2334+ // `ai_seat`, so a name reached both ways carries both origins; the row is
2335+ // the memoised one, and the envelope is unchanged.
2336+ if ( ! grants . permissions . includes ( 'ai_seat' ) || origins ) {
22862337 const aiAccess = ( ( await getUserRow ( ) ) as { ai_access ?: unknown } | undefined ) ?. ai_access ;
2287- if ( aiAccess === true || aiAccess === 1 || aiAccess === '1' ) grants . permissions . push ( 'ai_seat' ) ;
2338+ if ( aiAccess === true || aiAccess === 1 || aiAccess === '1' ) {
2339+ if ( ! grants . permissions . includes ( 'ai_seat' ) ) grants . permissions . push ( 'ai_seat' ) ;
2340+ recordOrigin ( 'ai_seat' , { kind : 'derived' , source : 'ai_access' } ) ;
2341+ }
2342+ }
2343+
2344+ // [ADR-0090 D6] Only names the envelope holds keep an origin: a bound name
2345+ // the catalog holds no set for, or a switched-off set, confers nothing.
2346+ if ( origins ) {
2347+ const permissionOrigins : Record < string , EffectivePermissionSetOrigin [ ] > = { } ;
2348+ for ( const name of grants . permissions ) {
2349+ const list = origins . get ( name ) ;
2350+ if ( list ) permissionOrigins [ name ] = list ;
2351+ }
2352+ grants . permissionOrigins = permissionOrigins ;
22882353 }
22892354
22902355 // [#11971] Store the fresh envelope (miss path only; the argument list is
0 commit comments