Skip to content

Commit 8f1feb1

Browse files
committed
fix(app-shell): yield the band's Recall only while the bar really draws one
The band's Recall now steps aside when the decision bar reports that it draws the request's declared approval_recall, rather than whenever the bar is mounted. Before the request definition loads, or on a backend that sends no viewer block (every declared gate fails closed), the bar draws no Recall and the band stays the submitter's lever. DeclaredActionsBar reports the declared actions it draws through the new optional onDrawnActionsChange; folded levers report through a visibility probe while their menu is closed, and the override menu appears only once a folded lever is shown. Claude-Session: https://claude.ai/code/session_014N3u93mMkVGhq6DBKXqx4J Co-authored-by: Claude <noreply@anthropic.com>
1 parent f1b4338 commit 8f1feb1

4 files changed

Lines changed: 274 additions & 73 deletions

File tree

‎ui/packages/app-shell/src/views/DeclaredActionsBar.tsx‎

Lines changed: 164 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ export interface DeclaredActionsBarProps {
147147
* server sent no `viewer` block for render exactly as without it.
148148
*/
149149
foldOverrides?: boolean;
150+
/**
151+
* Told which declared actions the bar DRAWS for this viewer, by name, each
152+
* time that set changes, and an empty list when the bar goes away
153+
* (objectstack#25877). "Drawn" is after the capability gate and the action's
154+
* own `visible` verdict — a button on the bar or an item in the folded
155+
* menu — so a host can yield a lever it would otherwise draw itself exactly
156+
* when the bar really draws it, and not merely when the bar is mounted (the
157+
* request's definition may not have loaded, or a backend too old to send a
158+
* `viewer` block fails every gate closed). Pass a stable function.
159+
*/
160+
onDrawnActionsChange?: (names: string[]) => void;
150161
}
151162

152163
/**
@@ -167,6 +178,60 @@ function approvalRouteVerb(action: ActionDef): ApprovalRouteVerb | undefined {
167178
return match ? (match[1] as ApprovalRouteVerb) : undefined;
168179
}
169180

181+
/**
182+
* Is this declared action SHOWN to the viewer? The one evaluation of its
183+
* `visible` gate, shared by the button and by the visibility probe the folded
184+
* override menu reports through (objectstack#25877), so the two cannot reach
185+
* different verdicts on one action.
186+
*
187+
* `visible` fails CLOSED on a throwing predicate — mirrors action:button and
188+
* ActionEngine.getActionsForLocation: a guard that can't be evaluated hides
189+
* the action rather than exposing one whose precondition is broken.
190+
*
191+
* Does the action DECLARE a `visible` gate? `hasDeclaredVisibilityGate`
192+
* (`!= null && !== ''`) is the one definition on the question, imported rather
193+
* than re-spelled. This gate used to ask truthiness, which classified
194+
* `visible: false` — the most explicit "never show this" an author can write —
195+
* as "no gate declared", skipped the verdict, and rendered the action for
196+
* everyone (objectui#3835, the fifth member of the objectui#3492 family).
197+
*
198+
* The stakes here are the highest of the family: the actions are
199+
* SERVER-declared (`objectDef.actions[]`), so "the spec's `visible` has no
200+
* boolean member, `objectstack build` cannot emit one" does not apply, and this
201+
* bar is mounted as plain JSX by its hosts — `packages/react`'s
202+
* `SchemaRenderer`, which hides a `visible`-carrying node before its component
203+
* mounts, is not on this path. This is the only gate on it, in front of the
204+
* approvals inbox's Approve / Reject buttons.
205+
*
206+
* The verdict stays with the evaluation entry: `toPredicateInput` passes a
207+
* boolean through untouched and `useCondition` short-circuits it instead of
208+
* calling the expression engine, so a declared `false` is `false`.
209+
*/
210+
function useDeclaredActionShown(action: ActionDef, predicateRecord: ReturnType<typeof usePredicateRecordContext>): boolean {
211+
const isVisible = useCondition(toPredicateInput(action.visible), predicateRecord, {
212+
throwOnError: true,
213+
label: `declared action "${action.name ?? action.label ?? 'action'}" (visible)`,
214+
});
215+
return !(hasDeclaredVisibilityGate(action.visible) && !isVisible);
216+
}
217+
218+
/**
219+
* Report whether a declared action is drawn, and withdraw the report when the
220+
* reporter unmounts (objectstack#25877). `onDrawn` is the toolbar's stable
221+
* setter, so the effect re-runs only when the verdict or the name moves.
222+
*/
223+
function useReportDrawn(
224+
name: string | undefined,
225+
drawn: boolean,
226+
onDrawn: ((name: string, drawn: boolean) => void) | undefined,
227+
): void {
228+
React.useEffect(() => {
229+
if (!onDrawn || !name) return undefined;
230+
onDrawn(name, drawn);
231+
return () => onDrawn(name, false);
232+
}, [name, drawn, onDrawn]);
233+
}
234+
170235
/**
171236
* One declared-action button. Extracted so the `visible` CEL predicate can be
172237
* evaluated with a hook (rules-of-hooks) and so the dispatch can carry the
@@ -184,7 +249,9 @@ const DeclaredActionButton: React.FC<{
184249
* shared, so the two forms cannot disagree about what the lever does.
185250
*/
186251
asMenuItem?: boolean;
187-
}> = ({ action, objectName, record, asMenuItem = false }) => {
252+
/** The toolbar's drawn-set reporter (objectstack#25877). */
253+
onDrawn?: (name: string, drawn: boolean) => void;
254+
}> = ({ action, objectName, record, asMenuItem = false, onDrawn }) => {
188255
const { execute } = useAction();
189256
const [loading, setLoading] = useState(false);
190257
// Localize the SERVER-DECLARED strings through the `_actions.<name>.*`
@@ -242,13 +309,12 @@ const DeclaredActionButton: React.FC<{
242309
* `record is not defined` and `throwOnError` turns that into "hidden".
243310
*/
244311
const predicateRecord = usePredicateRecordContext(record);
245-
// `visible` fails CLOSED on a throwing predicate — mirrors action:button and
246-
// ActionEngine.getActionsForLocation: a guard that can't be evaluated hides
247-
// the action rather than exposing one whose precondition is broken.
248-
const isVisible = useCondition(toPredicateInput(action.visible), predicateRecord, {
249-
throwOnError: true,
250-
label: `declared action "${action.name ?? action.label ?? 'action'}" (visible)`,
251-
});
312+
// The `visible` verdict — one evaluation, shared with the folded menu's
313+
// visibility probe (`useDeclaredActionShown` states the rule).
314+
const shown = useDeclaredActionShown(action, predicateRecord);
315+
// A menu item does not report: it is mounted only while its menu is open,
316+
// so its unmount would read as "not drawn". The probe reports for it.
317+
useReportDrawn(action.name, shown, asMenuItem ? undefined : onDrawn);
252318
// Spec `disabled` — the same three arms as `visible` (`boolean | CEL string |
253319
// { dialect, source }`, disabled when TRUE), evaluated against the same record
254320
// context. objectstack-ai/objectstack#1885 wired it in action-button only; this bar ignored it, so a
@@ -475,25 +541,8 @@ const DeclaredActionButton: React.FC<{
475541
}
476542
}, [action, execute, loading, objectName, record, localizeActionTexts, t, overrideNotice, overrideLabel]);
477543

478-
// Does the action DECLARE a `visible` gate? `hasDeclaredVisibilityGate`
479-
// (`!= null && !== ''`) is the one definition on the question, imported rather
480-
// than re-spelled. This gate used to ask truthiness, which classified
481-
// `visible: false` — the most explicit "never show this" an author can write —
482-
// as "no gate declared", skipped the verdict, and rendered the action for
483-
// everyone (objectui#3835, the fifth member of the objectui#3492 family).
484-
//
485-
// The stakes here are the highest of the family: the actions are
486-
// SERVER-declared (`objectDef.actions[]`), so "the spec's `visible` has no
487-
// boolean member, `objectstack build` cannot emit one" does not apply, and this
488-
// bar is mounted as plain JSX by its hosts — `packages/react`'s
489-
// `SchemaRenderer`, which hides a `visible`-carrying node before its component
490-
// mounts, is not on this path. This is the only gate on it, in front of the
491-
// approvals inbox's Approve / Reject buttons.
492-
//
493-
// The verdict stays with the evaluation entry above: `toPredicateInput` passes
494-
// a boolean through untouched and `useCondition` short-circuits it instead of
495-
// calling the expression engine, so a declared `false` is `false`.
496-
if (hasDeclaredVisibilityGate(action.visible) && !isVisible) return null;
544+
// The declared `visible` gate (`useDeclaredActionShown` above).
545+
if (!shown) return null;
497546

498547
const iconName = typeof (action as any).icon === 'string' ? (action as any).icon as string : undefined;
499548
// Map the spec's action `variant` enum (primary|secondary|danger|ghost|link)
@@ -752,12 +801,31 @@ const ApprovalWaitingStatus: React.FC<{ record: any }> = ({ record }) => {
752801
* action that survives. An override-only viewer gets no exemption from a
753802
* declared capability, and gets no new exposure either.
754803
*/
804+
/**
805+
* Evaluates a folded lever's `visible` gate while its menu is closed and its
806+
* item is not mounted, and reports the verdict (objectstack#25877). Draws
807+
* nothing.
808+
*/
809+
const DeclaredActionVisibilityProbe: React.FC<{
810+
action: ActionDef;
811+
record: any;
812+
onDrawn: (name: string, drawn: boolean) => void;
813+
}> = ({ action, record, onDrawn }) => {
814+
const predicateRecord = usePredicateRecordContext(record);
815+
const shown = useDeclaredActionShown(action, predicateRecord);
816+
useReportDrawn(action.name, shown, onDrawn);
817+
return null;
818+
};
819+
755820
const DeclaredActionsToolbar: React.FC<
756-
Pick<DeclaredActionsBarProps, 'objectName' | 'record' | 'className' | 'label' | 'foldOverrides'> & {
821+
Pick<
822+
DeclaredActionsBarProps,
823+
'objectName' | 'record' | 'className' | 'label' | 'foldOverrides' | 'onDrawnActionsChange'
824+
> & {
757825
/** The located set, pre-filtered by `location` / `exclude`, ungated. */
758826
actions: ActionDef[];
759827
}
760-
> = ({ actions, objectName, record, className, label, foldOverrides }) => {
828+
> = ({ actions, objectName, record, className, label, foldOverrides, onDrawnActionsChange }) => {
761829
const { t } = useObjectTranslation();
762830
const mayInvoke = useCapabilityGate();
763831
const permitted = useMemo(
@@ -776,6 +844,27 @@ const DeclaredActionsToolbar: React.FC<
776844
[foldOverrides, record, permitted],
777845
);
778846

847+
// objectstack#25877 — which declared actions are DRAWN, by name. Each bar
848+
// button and each folded lever's probe reports its own `visible` verdict;
849+
// the set is read against `permitted`, so a lever the capability gate
850+
// removed never counts however it last reported.
851+
const [drawnByName, setDrawnByName] = useState<Readonly<Record<string, boolean>>>({});
852+
const reportDrawn = useCallback((name: string, isDrawn: boolean) => {
853+
setDrawnByName((prev) => ((prev[name] ?? false) === isDrawn ? prev : { ...prev, [name]: isDrawn }));
854+
}, []);
855+
const drawnNames = permitted
856+
.map((action) => action.name)
857+
.filter((name): name is string => !!name && drawnByName[name] === true);
858+
// Keyed on the names themselves, not on an array identity (AGENTS.md #10).
859+
const drawnKey = drawnNames.join('\n');
860+
const onDrawnRef = React.useRef(onDrawnActionsChange);
861+
onDrawnRef.current = onDrawnActionsChange;
862+
React.useEffect(() => {
863+
onDrawnRef.current?.(drawnKey ? drawnKey.split('\n') : []);
864+
}, [drawnKey]);
865+
React.useEffect(() => () => onDrawnRef.current?.([]), []);
866+
const foldedDrawn = folded.some((action) => !!action.name && drawnByName[action.name] === true);
867+
779868
// Nothing this viewer may invoke renders nothing — no toolbar chrome, no
780869
// orphan divider, same degrade as "nothing declared at this location".
781870
if (permitted.length === 0) return null;
@@ -804,38 +893,51 @@ const DeclaredActionsToolbar: React.FC<
804893
action={action}
805894
objectName={objectName}
806895
record={record}
896+
onDrawn={reportDrawn}
897+
/>
898+
))}
899+
{folded.map((action) => (
900+
<DeclaredActionVisibilityProbe
901+
key={action.name}
902+
action={action}
903+
record={record}
904+
onDrawn={reportDrawn}
807905
/>
808906
))}
809-
<DropdownMenu>
810-
<DropdownMenuTrigger asChild>
811-
{/* One marked control for the whole exception path: the
812-
override shield the folded levers wear, in the same amber,
813-
beside the overflow glyph. */}
814-
<Button
815-
type="button"
816-
variant="outline"
817-
size="sm"
818-
className="gap-1 px-2 border-amber-500/60 text-amber-700 hover:bg-amber-50 hover:text-amber-800 dark:text-amber-400 dark:hover:bg-amber-500/10 dark:hover:text-amber-300"
819-
aria-label={overrideMenuLabel}
820-
title={overrideMenuLabel}
821-
data-testid="declared-actions-override-menu"
822-
>
823-
<ShieldAlert className="h-4 w-4" />
824-
<MoreHorizontal className="h-4 w-4" />
825-
</Button>
826-
</DropdownMenuTrigger>
827-
<DropdownMenuContent align="end" className="min-w-48">
828-
{folded.map((action) => (
829-
<DeclaredActionButton
830-
key={action.name}
831-
action={action}
832-
objectName={objectName}
833-
record={record}
834-
asMenuItem
835-
/>
836-
))}
837-
</DropdownMenuContent>
838-
</DropdownMenu>
907+
{/* The trigger appears once a folded lever is actually shown, so a
908+
set whose every gate said no leaves no empty menu behind. */}
909+
{foldedDrawn && (
910+
<DropdownMenu>
911+
<DropdownMenuTrigger asChild>
912+
{/* One marked control for the whole exception path: the
913+
override shield the folded levers wear, in the same amber,
914+
beside the overflow glyph. */}
915+
<Button
916+
type="button"
917+
variant="outline"
918+
size="sm"
919+
className="gap-1 px-2 border-amber-500/60 text-amber-700 hover:bg-amber-50 hover:text-amber-800 dark:text-amber-400 dark:hover:bg-amber-500/10 dark:hover:text-amber-300"
920+
aria-label={overrideMenuLabel}
921+
title={overrideMenuLabel}
922+
data-testid="declared-actions-override-menu"
923+
>
924+
<ShieldAlert className="h-4 w-4" />
925+
<MoreHorizontal className="h-4 w-4" />
926+
</Button>
927+
</DropdownMenuTrigger>
928+
<DropdownMenuContent align="end" className="min-w-48">
929+
{folded.map((action) => (
930+
<DeclaredActionButton
931+
key={action.name}
932+
action={action}
933+
objectName={objectName}
934+
record={record}
935+
asMenuItem
936+
/>
937+
))}
938+
</DropdownMenuContent>
939+
</DropdownMenu>
940+
)}
839941
</div>
840942
</div>
841943
</div>
@@ -857,6 +959,7 @@ const DeclaredActionsToolbar: React.FC<
857959
action={action}
858960
objectName={objectName}
859961
record={record}
962+
onDrawn={reportDrawn}
860963
/>
861964
))}
862965
</div>
@@ -874,6 +977,7 @@ export function DeclaredActionsBar({
874977
className,
875978
label,
876979
foldOverrides,
980+
onDrawnActionsChange,
877981
}: DeclaredActionsBarProps) {
878982
const dataSource = useAdapter();
879983
// Fetch the object def (and its declared actions) unless the host passed
@@ -929,6 +1033,7 @@ export function DeclaredActionsBar({
9291033
className={className}
9301034
label={label}
9311035
foldOverrides={foldOverrides}
1036+
onDrawnActionsChange={onDrawnActionsChange}
9321037
/>
9331038
{runtime.dialogs}
9341039
</ActionProvider>

‎ui/packages/app-shell/src/views/RecordDetailView.approvalRecallGate.test.tsx‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,37 @@ function stubApprovalsApi(row: Record<string, unknown> | null, listStatus = 200)
159159
vi.stubGlobal('fetch', approvalsFetch);
160160
}
161161

162+
/**
163+
* The request object's declared recall, so the page's decision bar can draw it
164+
* (objectstack#25877). The gate is the shipped one in the reduced spelling the
165+
* sibling approval suites use: the submitter arm OR the override arm.
166+
*/
167+
const SYS_APPROVAL_REQUEST_DEF = {
168+
name: 'sys_approval_request',
169+
label: 'Approval Request',
170+
fields: { id: { type: 'text', label: 'Id' }, status: { type: 'text', label: 'Status' } },
171+
actions: [
172+
{
173+
name: 'approval_recall', label: 'Recall', type: 'api', method: 'POST',
174+
target: '/api/v1/approvals/requests/{id}/recall',
175+
params: [{ name: 'comment', label: 'Comment', type: 'textarea', required: false }],
176+
visible: '(record.status == "pending" || record.status == "returned") && record.viewer.is_submitter'
177+
+ ' || record.viewer.can_override',
178+
locations: ['record_section'],
179+
},
180+
],
181+
};
182+
162183
const METADATA = {
163-
objects: OBJECTS,
184+
objects: [...OBJECTS, SYS_APPROVAL_REQUEST_DEF],
164185
pages: [],
165186
loading: false,
166187
error: null,
167188
refresh: async () => {},
168189
invalidate: () => {},
169190
ensureType: async () => [],
170-
getItem: async () => null,
191+
getItem: async (type: string, name: string) =>
192+
(type === 'object' && name === SYS_APPROVAL_REQUEST_DEF.name ? SYS_APPROVAL_REQUEST_DEF : null),
171193
getItemsByType: () => [],
172194
} as any;
173195

@@ -296,14 +318,28 @@ describe('record page → band: who may see recall (objectui#6464)', () => {
296318
* measures the band; this measures the seam.
297319
*/
298320
describe('record page → band: who draws recall (objectstack#25877)', () => {
299-
it('tells the band the page draws recall when the decision bar is up', async () => {
321+
it('tells the band the page draws recall while the decision bar draws it', async () => {
300322
stubApprovalsApi(pendingRequest({ viewer: { can_act: false, is_submitter: true, can_override: false } }));
301323
renderRecordPage();
302324

303325
const probe = await settledProbe(true);
304326
await waitFor(() =>
305327
expect(probe.getAttribute('data-approval-recall-drawn-by-host')).toBe('true'),
306328
);
329+
expect(screen.getByTestId('declared-action-approval_recall')).toBeInTheDocument();
330+
});
331+
332+
it('leaves the band its recall when the bar is up but draws none', async () => {
333+
// A server too old to send a `viewer` block: every declared gate fails
334+
// closed, so the bar draws no Recall, and the band, which falls back to
335+
// comparing `submitter_id`, must stay the submitter's lever.
336+
stubApprovalsApi(pendingRequest({ submitter_id: SIGNED_IN_USER }));
337+
renderRecordPage();
338+
339+
const probe = await settledProbe(true);
340+
await waitFor(() => expect(probe.getAttribute('data-approval-is-submitter')).toBe('true'));
341+
expect(screen.queryByTestId('declared-action-approval_recall')).toBeNull();
342+
expect(probe.getAttribute('data-approval-recall-drawn-by-host')).toBe('false');
307343
});
308344

309345
it('leaves the band as the recall lever when there is no approvals API', async () => {

0 commit comments

Comments
 (0)