@@ -135,7 +135,12 @@ import type { FlowNodeParsed, FlowEdgeParsed, FlowTriggerKind } from '@objectsta
135135// shared refusal to prevent.
136136import { EvaluatedExpressionInputSchema , EVALUATED_EXPRESSION_SOURCE_REQUIRED } from '@objectstack/spec/shared' ;
137137import { referenceCarrierOf , referenceTargetOf , REFERENCE_VALUE_TYPES } from '@objectstack/spec/data' ;
138- import { isStoredMetadataBodyObject , STORED_METADATA_BODY_PRESCRIPTION } from '@objectstack/spec/kernel' ;
138+ import {
139+ isStoredMetadataBodyObject ,
140+ STORED_METADATA_BODY_PRESCRIPTION ,
141+ PUBLIC_AUTH_FEATURE_NAMES ,
142+ featureGatePredicate ,
143+ } from '@objectstack/spec/kernel' ;
139144// [#22394] The acting user's declared members — the contract half of the option
140145// verdict's member allowlist for `current_user` and its ADR-0068 aliases. Read
141146// off the schema at first use, never restated here.
@@ -1023,6 +1028,43 @@ const FIELD_RULE_USER_ROOTS = ['current_user', 'user', 'ctx', 'os'] as const;
10231028 * and worded for it ({@link FIELD_RULE_SLOT_CONSEQUENCE}, {@link fieldRuleRootIssue}).
10241029 */
10251030const FIELD_GROUP_VISIBLE_WHEN_SLOT = 'field group visibleWhen' ;
1031+
1032+ /**
1033+ * [#30692] A field group's `visibleWhen` with the gate its `requiresFeature`
1034+ * lowered into it taken off, so the root verdict judges what the AUTHOR wrote.
1035+ *
1036+ * `fieldGroups[].requiresFeature` (`ObjectFieldGroupSchema`) is lowered at
1037+ * parse by the spec's `lowerRequiresFeature` into one of exactly two shapes —
1038+ * the gate alone, or `(<authored>) && <gate>` — where the gate is
1039+ * `featureGatePredicate(flag)` for a flag of `PUBLIC_AUTH_FEATURES`. Both
1040+ * renderers evaluate a group's predicate with the host's `features` root in
1041+ * scope (the form's section gate and the record page's `record:details`), so
1042+ * that gate is enforced where it is read; it is the ruled shape (#30210 row 4
1043+ * A, ruling 6670372892), and only it is accepted. A `features.*` read anywhere
1044+ * else in the predicate keeps its root verdict: this takes off the trailing
1045+ * gate the lowering appends, and nothing more.
1046+ *
1047+ * Returns `undefined` when the whole predicate is the gate (nothing authored
1048+ * left to judge), the authored part in the input's own spelling when the gate
1049+ * was composed onto one, and the input untouched otherwise — including when
1050+ * the part before the gate does not parse on its own, which is how a
1051+ * hand-written `(a) && (b) && <gate>` (whose "authored part" would be
1052+ * `a) && (b`) is told apart from the lowering's single parenthesised group.
1053+ */
1054+ export function withoutLoweredFeatureGate ( raw : unknown ) : unknown {
1055+ const source = celSourceOf ( raw ) ;
1056+ if ( source === undefined ) return raw ;
1057+ for ( const flag of PUBLIC_AUTH_FEATURE_NAMES ) {
1058+ const gate = featureGatePredicate ( flag ) ;
1059+ if ( source === gate ) return undefined ;
1060+ const tail = `) && ${ gate } ` ;
1061+ if ( ! source . startsWith ( '(' ) || ! source . endsWith ( tail ) ) continue ;
1062+ const authored = source . slice ( 1 , source . length - tail . length ) ;
1063+ if ( ! collectCelRootIdentifiers ( authored ) . ok ) return raw ;
1064+ return typeof raw === 'string' ? authored : { ...( raw as AnyRec ) , source : authored } ;
1065+ }
1066+ return raw ;
1067+ }
10261068/**
10271069 * The slot-agnostic clause — provably true for any slot, specific to none.
10281070 * Used where the honest answer is "not measured for this slot", never as a
@@ -1120,7 +1162,15 @@ export function fieldRuleRootIssue(
11201162 // [#26646] A field group has no options and is not a field: its user-root
11211163 // prescription keeps only the two answers that apply to it.
11221164 const userRoot = ( FIELD_RULE_USER_ROOTS as readonly string [ ] ) . includes ( root ) ;
1123- const prescription = userRoot && slot === FIELD_GROUP_VISIBLE_WHEN_SLOT
1165+ const prescription = root === 'features' && slot === FIELD_GROUP_VISIBLE_WHEN_SLOT
1166+ // [#30692] A field group has its own door for a deployment feature: the
1167+ // `requiresFeature` key, whose lowered gate this rule accepts.
1168+ ? `To show the group only where a deployment feature is on, declare it on the group ` +
1169+ `instead — \`requiresFeature: 'FLAG'\`, the same flag names an action's \`requiresFeature\` ` +
1170+ `takes — and keep \`visibleWhen\` for record state; the gate it lowers to is the one ` +
1171+ `\`features\` read this predicate accepts. To gate the group on record state, rewrite the ` +
1172+ `predicate against \`record\`.`
1173+ : userRoot && slot === FIELD_GROUP_VISIBLE_WHEN_SLOT
11241174 ? `To hide the group's fields by role, declare field-level security on a permission set ` +
11251175 `for each field in the group (\`fields: { 'OBJECT.FIELD': { readable: false } }\`), which ` +
11261176 `the server enforces. To gate the group on record state, rewrite the predicate against ` +
@@ -3830,10 +3880,13 @@ export function runStackExpressionPasses(stack: AnyRec, options: StackExpression
38303880 const groupKey = typeof fieldGroup . key === 'string' ? fieldGroup . key : '?' ;
38313881 const where = `object '${ objectName } ' · field group '${ groupKey } ' visibleWhen` ;
38323882 const raw : unknown = fieldGroup . visibleWhen ;
3833- const verdict = fieldRuleRootVerdict ( FIELD_GROUP_VISIBLE_WHEN_SLOT , raw ) ;
3883+ // [#30692] The root verdict judges the predicate without the gate a
3884+ // `requiresFeature` lowered into it ({@link withoutLoweredFeatureGate});
3885+ // every other check still reads the whole predicate.
3886+ const verdict = fieldRuleRootVerdict ( FIELD_GROUP_VISIBLE_WHEN_SLOT , withoutLoweredFeatureGate ( raw ) ) ;
38343887 check ( where , raw , objectName , 'record' , verdict !== null ) ;
38353888 if ( verdict ) {
3836- issues . push ( { where, message : verdict . message , source : verdict . source , severity : 'error' } ) ;
3889+ issues . push ( { where, message : verdict . message , source : celSourceOf ( raw ) ?? verdict . source , severity : 'error' } ) ;
38373890 }
38383891 }
38393892 }
0 commit comments