Skip to content

Commit 39fa82b

Browse files
fix(components): header-action predicates wait for the record instead of reporting "No such key" (#34035)
<!-- claude-projects-attribution: {"github_login":"os-steve"} --> _Requested by **Steve** · [project thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9E8CzBpSUdLhr9L1Yhzma3U)_ Fixes #33987 Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:记录页头部按钮的 `visible` / `hidden` / `disabled` / `pressed` 条件,在记录还没加载完时不再求值,也不再往控制台报错;记录到了再正常求值。 - 为什么改:页面一打开就拿一条空记录去算条件,每个 `record.status` 这类条件都报 “No such key… check the field names and CEL”,让作者去改其实没错的元数据。 - 风险与回滚:只动 `page:header` 一个组件;加载中各条件取各自的安全默认值(隐藏 / 不隐藏 / 禁用 / 未按下),与之前出错时的取值一致,记录到了以后行为不变。回滚即还原这一个提交。 - 先看:`ui/packages/components/src/renderers/layout/containers.tsx` 的 `headerRecordPending` 与 `evalHeaderPredicate`;再看 `page-header-predicate-dialect.test.tsx` 末尾新增的一组测试。 - 两分钟验证:`cd ui && pnpm exec vitest run packages/components/src/__tests__/page-header-predicate-dialect.test.tsx`,预期 33 条全绿。 - 需要你做的:无,席位落地。 ## What changes A record page binds its record id at once and its row only when the read answers (`RecordDetailView` hands `data: null` until then; `RecordContextValue.data` is documented as "undefined while loading"). `page:header` evaluated its action predicates against `ctx?.data ?? {}`, so during that window every `record.FIELD` predicate faulted with "No such key", and the warn-once fault report (keyed by label and predicate) told the author to check the field names of a field the object has, on every page load. - `headerRecordPending` names the state: a record context is present, it carries a record id, and its row is `null`/`undefined`. - `evalHeaderPredicate` answers each key's own fallback while pending and evaluates nothing, so nothing is reported. All four call sites (`visible`, `hidden`, `disabled`, `pressed`) go through it. - `disabledReasonFor` gives no "Not available for this record" reason while pending: the closed `disabled` fallback is not a fact about a record that has not arrived. Same defect class (a pending record reported as a fact about the record), same file, same gate families. - Unchanged: a header with no record bound (no context, or no id) still evaluates, so a `record.*` predicate there still faults and says so; a loaded record that really lacks the key still warns once. No change in `ui/packages/core/src/evaluator/**`: the evaluator is right to fault on an empty record; the root cause is the caller spelling an absent record as an empty one. ## Tests One new top-level `describe` in the existing shared harness `page-header-predicate-dialect.test.tsx` (no new file): - pending → no fault report, `visible` answers hidden; record arrives → predicate honoured (shown), still no report; - loaded negative direction → hidden, no report; - pending `hidden` / `disabled` / `pressed` → rendered, disabled, `aria-pressed="false"`, no `aria-describedby` reason, no report; after load → enabled, pressed, no report; - CONTROL: a loaded record missing `f_status` → hidden and exactly one fault naming `f_status`; - CONTROL: no record id bound → still faults once. No existing test was edited. ## Acceptance notes - Triage asked whether the rendered buttons are wrong after the record loads. Measured on the shipped code (fix ablated, same harness): pending `[false,false]` with 2 fault reports; after load `[true,false]`, which is the predicates' own verdict. So only the pre-load pass was wrong; the authored rule was already honoured once the record arrived. With the fix: pending `[false,false]`, 0 reports; loaded `[true,false]`, 0 reports. - While pending, a predicate that reads only the host scope (for example `ctx.user.isPlatformAdmin == true`) also waits for the record, where it used to evaluate at once. The header shows its record-gated and user-gated actions together, once the row arrives. ## Verification <details><summary>Ablations (fix committed first; restored blob == HEAD each time)</summary> - `headerRecordPending` forced to `false`: 3 of the 5 new tests red (`expected [ Array(1) ] to have a length of +0 but got 1` on both pending fault-count pins; the legs test red on `aria-describedby`); both CONTROL tests stay green. - only the `disabledReasonFor` pending line deleted: 1 red (the legs test, `aria-describedby`). </details> <details><summary>Local runs at a1d4af3</summary> - `cd ui && pnpm exec vitest run packages/components/src/__tests__/page-header-predicate-dialect.test.tsx`: 33/33 passed. - `pnpm --filter @object-ui/components test --maxWorkers=2` (under the verify lock): Test Files 479 passed, 1 skipped (480); Tests 4595 passed, 24 skipped. - `pnpm --filter @object-ui/components type-check` (after building the dependency closure): exit 0. - `node scripts/pm/dispatch-gates.mjs --commands`: 37 derived families, each run, all exit 0; `--ran` reconciliation: 37 run, 0 NOT-MEASURED, 0 UNRUN. `pnpm --dir ui exec vitest run scripts/` first exited 1 only because `@objectstack/client` had no build in this container (unresolved-entry list); after the build it passed 171 files / 5065 tests. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01ADHkmQj66qNAyERYxzoNgw)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 63db724 commit 39fa82b

3 files changed

Lines changed: 143 additions & 8 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@object-ui/components': patch
3+
'@object-ui/console': patch
4+
---
5+
6+
fix(components): record page header-action predicates wait for the record instead of reporting "No such key" for fields the object has
7+
8+
Clause-②: no
9+
Metadata-protocol: none
10+
11+
A record page binds its record id at once and its row only when the read answers. Until then the `page:header` actions evaluated their `visible`, `hidden`, `disabled` and `pressed` predicates against an empty record, so every `record.<field>` predicate faulted, and the console said "A conditional predicate failed to evaluate … No such key: status … Check the field names and CEL" once per action on every page load, for a field the object has. Nothing was wrong with the metadata.
12+
13+
A record that is bound but not loaded is now its own state. Each predicate answers its key's safe default (hidden, not hidden, disabled, not pressed) and reports nothing; a `disabled` action does not claim it is "not available for this record" while there is no record yet. Once the record arrives the predicates run as before, and the warning still fires when a key is really missing from the loaded record. A header with no record bound at all evaluates as before.

‎ui/packages/components/src/__tests__/page-header-predicate-dialect.test.tsx‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,103 @@ describe('page:header — fail-closed stays fail-closed, and says so once (#3521
361361
}
362362
});
363363
});
364+
365+
describe('page:header — a record still loading is its own state, not an empty record (objectstack#33987)', () => {
366+
// The record page binds its record id at once and hands `data: null` until
367+
// the read answers. Evaluating then meant evaluating against an empty
368+
// record: every `record.<field>` predicate faulted with "No such key", and
369+
// the warn-once report told the author to check the field names of a field
370+
// the object HAS — on every load, before the record arrived.
371+
const faultsFor = (warn: ReturnType<typeof vi.spyOn>, name: string) =>
372+
warn.mock.calls.filter(
373+
(c: unknown[]) => String(c[0]).includes('failed to evaluate') && String(c[0]).includes(`"${name}"`),
374+
);
375+
376+
function headerTree(action: Record<string, unknown>, recordId: string | null, data: any) {
377+
return (
378+
<PredicateScopeProvider scope={SCOPE}>
379+
<ActionProvider>
380+
<RecordContextProvider
381+
objectName="showcase_field_zoo"
382+
recordId={recordId}
383+
data={data}
384+
objectSchema={OBJECT_SCHEMA}
385+
>
386+
<PageHeader
387+
schema={{
388+
type: 'page:header',
389+
title: 'Specimen',
390+
actions: [{ locations: ['record_header'], label: 'Zoo Action', type: 'api', ...action }],
391+
}}
392+
/>
393+
</RecordContextProvider>
394+
</ActionProvider>
395+
</PredicateScopeProvider>
396+
);
397+
}
398+
399+
let warn: ReturnType<typeof vi.spyOn>;
400+
beforeEach(() => {
401+
warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
402+
return () => warn.mockRestore();
403+
});
404+
405+
it('pending: no fault report; once the record arrives the predicate is honoured, still with no report', () => {
406+
const action = { name: 'zoo_pending_visible_33987', visible: 'record.f_status == "open"' };
407+
const { rerender } = render(headerTree(action, 'r1', null));
408+
// Pending answers the key's own fallback (hidden), and says nothing.
409+
expect(shown()).toBe(false);
410+
expect(faultsFor(warn, 'zoo_pending_visible_33987')).toHaveLength(0);
411+
412+
rerender(headerTree(action, 'r1', RECORD));
413+
expect(shown()).toBe(true);
414+
expect(faultsFor(warn, 'zoo_pending_visible_33987')).toHaveLength(0);
415+
});
416+
417+
it('the loaded verdict is the predicate’s own in the negative direction too', () => {
418+
const action = { name: 'zoo_pending_negative_33987', visible: 'record.f_status == "open"' };
419+
const { rerender } = render(headerTree(action, 'r1', undefined));
420+
rerender(headerTree(action, 'r1', { ...RECORD, f_status: 'closed' }));
421+
expect(shown()).toBe(false);
422+
expect(faultsFor(warn, 'zoo_pending_negative_33987')).toHaveLength(0);
423+
});
424+
425+
it('pending: `hidden`, `disabled` and `pressed` report nothing either, and `disabled` claims no reason about the record', () => {
426+
const action = {
427+
name: 'zoo_pending_legs_33987',
428+
hidden: 'record.f_status == "closed"',
429+
disabled: 'record.f_status == "closed"',
430+
pressed: 'record.f_status == "open"',
431+
};
432+
const { rerender } = render(headerTree(action, 'r1', null));
433+
// Each leg answers its own fallback: rendered, disabled, not pressed.
434+
expect(shown()).toBe(true);
435+
expect(button()).toBeDisabled();
436+
expect(button()).toHaveAttribute('aria-pressed', 'false');
437+
expect(button()).not.toHaveAttribute('aria-describedby');
438+
expect(faultsFor(warn, 'zoo_pending_legs_33987')).toHaveLength(0);
439+
440+
rerender(headerTree(action, 'r1', RECORD));
441+
expect(shown()).toBe(true);
442+
expect(button()).not.toBeDisabled();
443+
expect(button()).toHaveAttribute('aria-pressed', 'true');
444+
expect(faultsFor(warn, 'zoo_pending_legs_33987')).toHaveLength(0);
445+
});
446+
447+
it('CONTROL: a LOADED record really missing the key still faults, fails closed and says so once', () => {
448+
const { f_status: _omitted, ...withoutStatus } = RECORD;
449+
const action = { name: 'zoo_loaded_missing_33987', visible: 'record.f_status == "open"' };
450+
render(headerTree(action, 'r1', withoutStatus));
451+
expect(shown()).toBe(false);
452+
const faults = faultsFor(warn, 'zoo_loaded_missing_33987');
453+
expect(faults).toHaveLength(1);
454+
expect(String(faults[0][0])).toContain('f_status');
455+
});
456+
457+
it('CONTROL: a header with no record bound (no id) still evaluates, so a `record.*` predicate there faults and says so', () => {
458+
const action = { name: 'zoo_unbound_33987', visible: 'record.f_status == "open"' };
459+
render(headerTree(action, null, undefined));
460+
expect(shown()).toBe(false);
461+
expect(faultsFor(warn, 'zoo_unbound_33987')).toHaveLength(1);
462+
});
463+
});

‎ui/packages/components/src/renderers/layout/containers.tsx‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,15 @@ export const PageHeaderActions: React.FC<PageHeaderActionsProps> = ({
20532053
// `warnOnError: true`), so a broken predicate hides its button here exactly
20542054
// as it does on the other surfaces, and says so once.
20552055
const headerPredicateFields = headerObjectSchema?.fields;
2056+
// The record this header is ABOUT has not arrived yet: a record is bound (the
2057+
// host named its id) and its row is still absent — `RecordContextValue.data`
2058+
// is "undefined while loading", and the record page hands `null` until its
2059+
// read answers. That is its own state, not an empty record: an empty record
2060+
// makes every `record.<field>` predicate fault, which is an authoring error
2061+
// only when the key is missing from a record that DID load. A header with no
2062+
// record bound at all (no context, or no id) keeps evaluating as before, so a
2063+
// `record.*` predicate there still faults and says so.
2064+
const headerRecordPending = ctx != null && ctx.recordId != null && ctx.data == null;
20562065
const headerPredicateScope = React.useMemo(() => {
20572066
const scopeUser = (predicateScope as any)?.user;
20582067
// Relation fields bind as the FOREIGN KEY the server stores, not as the
@@ -2104,17 +2113,27 @@ export const PageHeaderActions: React.FC<PageHeaderActionsProps> = ({
21042113
* share `visible`'s `false`, which on this key means ENABLED — so
21052114
* `disabled: !current_user.can(…)` left the action pressable until the
21062115
* payload arrived, the one leg of this surface that failed open.
2116+
*
2117+
* While the record is PENDING (see `headerRecordPending`) nothing is
2118+
* evaluated: every predicate answers its `fallback` and reports nothing.
2119+
* Evaluating then meant evaluating against an empty record, where every
2120+
* `record.<field>` faults with "No such key" — and the fault report, which
2121+
* is warn-once per (label, predicate), told the author to "check the field
2122+
* names" of a field the object has, on every load, before the record it
2123+
* names had arrived. The predicate runs, and reports, once the record does.
21072124
*/
21082125
const evalHeaderPredicate = React.useCallback(
21092126
(pred: unknown, label: string, fallback: boolean): boolean =>
2110-
evalRowPredicate(pred as never, ctx?.data ?? {}, {
2111-
fallback,
2112-
scope: headerPredicateScope,
2113-
fields: headerPredicateFields,
2114-
warnOnError: true,
2115-
label,
2116-
}),
2117-
[ctx?.data, headerPredicateScope, headerPredicateFields],
2127+
headerRecordPending
2128+
? fallback
2129+
: evalRowPredicate(pred as never, ctx?.data ?? {}, {
2130+
fallback,
2131+
scope: headerPredicateScope,
2132+
fields: headerPredicateFields,
2133+
warnOnError: true,
2134+
label,
2135+
}),
2136+
[ctx?.data, headerRecordPending, headerPredicateScope, headerPredicateFields],
21182137
);
21192138

21202139
const headerActionPlan = React.useMemo<{ actions: any[]; authoredKeys: ReadonlySet<string> }>(() => {
@@ -2557,6 +2576,9 @@ export const PageHeaderActions: React.FC<PageHeaderActionsProps> = ({
25572576
const disabledReasonFor = (action: any): string | undefined => {
25582577
const key = (action?.name || action?.id || '') as string;
25592578
if (key === '' || !authoredHeaderActionKeys.has(key)) return undefined;
2579+
// A pending record answers `disabled`'s fallback (closed), which is not
2580+
// a fact about the record — so it is not explained as one.
2581+
if (headerRecordPending) return undefined;
25602582
if (!resolveDisabled(action?.disabled, action?.name ?? action?.id)) return undefined;
25612583
const declared = typeof action?.disabledReason === 'string' ? action.disabledReason : '';
25622584
return declared.trim() !== ''

0 commit comments

Comments
 (0)