Skip to content

Commit 31eef37

Browse files
committed
fix(lint): related lists leave an undeclared child unjudged; type arms skip tied fields
Contract review r1 on the served-fold PR: - validate-related-list-refs no longer builds a child from extensions alone: on the per-package leg a child another body owns read as having only a sibling's extension fields, a false related-list-ref-unknown. - A field two packages write at the deciding priority is served, but which definition wins is registration order. The type arms (measure aggregates, preset comparands, sharing comparisons, widget field types, kanban option values) leave it unjudged; the thank-you panel fails closed on it. - Pins: the per-package leg for every existence rule, byte-equal refusals against a declared-only control, the related-list per-package case, ties. Claude-Session: https://claude.ai/code/session_01WGufzg3UmwhpAtL969c7YG Co-authored-by: Claude <noreply@anthropic.com>
1 parent e27387e commit 31eef37

9 files changed

Lines changed: 193 additions & 33 deletions

‎packages/lint/src/object-graph.test.ts‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,13 @@ describe('[#30383] the field-path rules read the objects as served', () => {
624624
mappings: [{ name: 'm', targetObject: 'xs_deal', fieldMapping: [{ source: 'R', target: f }] }],
625625
})],
626626
];
627+
// The per-package leg of `os build` (`packageBodyAsStack` in the cli): the
628+
// app body is the stack, its own collections at the top level and under
629+
// `manifest`, and the artifact's `packages[]` beside them.
630+
const perPackage = (extra: Rec, objectExtensions: unknown[] = extensions): Rec => {
631+
const body = { id: 'app', objects: [deal, account, note], ...extra };
632+
return { ...body, manifest: body, packages: [{ manifest: body }, { manifest: { id: 'mod', objectExtensions } }] };
633+
};
627634
it.each(existence)('%s: an extension-added field passes; a field nothing adds is still %s', (_rule, id, run, naming) => {
628635
// `ref_no` (an autonumber) is the one name every member admits, the
629636
// thank-you panel included.
@@ -632,6 +639,21 @@ describe('[#30383] the field-path rules read the objects as served', () => {
632639
expect(ids(run, artifact(naming('nowhere')))).toContain(id);
633640
});
634641

642+
it.each(existence)('%s, on the per-package leg: the same verdicts', (_rule, id, run, naming) => {
643+
expect(ids(run, perPackage(naming('ref_no')))).not.toContain(id);
644+
expect(ids(run, perPackage(naming('ref_no'), []))).toContain(id);
645+
expect(ids(run, perPackage(naming('nowhere')))).toContain(id);
646+
});
647+
648+
it.each(existence)('%s: a field nothing adds is refused byte-for-byte as on the declared objects alone', (_rule, id, run, naming) => {
649+
const refusal = (stack: Rec) => run(stack)
650+
.filter((f) => f.rule === id)
651+
.map((f) => { const { severity, where, path, message } = f as Rec; return { severity, where, path, message }; });
652+
const served = refusal(artifact(naming('zq_absent')));
653+
expect(served).toHaveLength(1);
654+
expect(served).toEqual(refusal(artifact(naming('zq_absent'), [])));
655+
});
656+
635657
it('validate-dataset-references: a path through an extension-added relationship resolves', () => {
636658
expect(ids(validateDatasetReferences, artifact({ datasets: [dataset('partner.tier')] }))).toEqual([]);
637659
expect(ids(validateDatasetReferences, artifact({ datasets: [dataset('partner.nowhere')] }))).toEqual([DATASET_FIELD_UNKNOWN]);
@@ -667,6 +689,17 @@ describe('[#30383] the field-path rules read the objects as served', () => {
667689
expect(validateFieldConsumers(artifact({ datasets: [dataset('name')] })).filter((f) => f.field in added)).toEqual([]);
668690
});
669691

692+
it('validate-related-list-refs, on the per-package leg: a child another body owns stays unjudged', () => {
693+
// `lib` owns `xs_note` and its FK; `mod` extends `xs_note` with something
694+
// else. The app body's leg holds neither declaration, so it must not read
695+
// `mod`'s extension as `xs_note`'s whole field set.
696+
const body = { id: 'app', objects: [{ ...deal, relatedLists: ['xs_note', 'xs_note.deal_id'] }, account] };
697+
const lib = { id: 'lib', objects: [{ name: 'xs_note', fields: { deal_id: { type: 'lookup', reference: 'xs_deal' } } }] };
698+
const mod = { id: 'mod', objectExtensions: [{ extend: 'xs_note', fields: { mood: { type: 'text' } } }] };
699+
const stack = { ...body, manifest: body, packages: [{ manifest: body }, { manifest: lib }, { manifest: mod }] };
700+
expect(ids(validateRelatedListRefs, stack)).toEqual([]);
701+
});
702+
670703
it('validate-related-list-refs: an FK a sibling package adds is an inbound relationship', () => {
671704
const lists = (relatedLists: string[]) => ({ objects: [{ ...deal, relatedLists }, account, note] });
672705
expect(ids(validateRelatedListRefs, artifact(lists(['xs_note', 'xs_note.deal_id'])))).toEqual([]);
@@ -690,4 +723,55 @@ describe('[#30383] the field-path rules read the objects as served', () => {
690723
expect(ids(validateMappingTargetFields, artifact(mapping('score'), replaced))).toEqual([]);
691724
expect(ids(validateMappingTargetFields, artifact(mapping('region'), replaced))).toEqual([MAPPING_TARGET_FIELD_UNKNOWN]);
692725
});
726+
727+
// Two packages writing one field at the deciding priority: the name is
728+
// served, but which definition wins is registration order, which the stack
729+
// does not state. The type arms leave it unjudged; the thank-you panel,
730+
// shown to anonymous visitors, fails closed.
731+
describe('a field two packages tie on', () => {
732+
const tie = (a: Rec, b: Rec): Rec => ({
733+
objects: [deal, account, note],
734+
packages: [
735+
{ manifest: { id: 'app', objects: [deal, account, note] } },
736+
{ manifest: { id: 'mod', objectExtensions: [{ extend: 'xs_deal', fields: a }] } },
737+
{ manifest: { id: 'mod2', objectExtensions: [{ extend: 'xs_deal', fields: b }] } },
738+
],
739+
});
740+
const numberOrText = (extra: Rec) => ({ ...tie({ score: { type: 'number' } }, { score: { type: 'text' } }), ...extra });
741+
const dateOrText = (extra: Rec) => ({ ...tie({ closed_on: { type: 'date' } }, { closed_on: { type: 'text' } }), ...extra });
742+
743+
it('is still a served name for the existence rules', () => {
744+
expect(ids(validateDatasetReferences, numberOrText({ datasets: [{ ...dataset('score'), include: [] }] }))).toEqual([]);
745+
expect(ids(validateDatasetReferences, numberOrText({ datasets: [{ ...dataset('zq_absent'), include: [] }] }))).toEqual([DATASET_FIELD_UNKNOWN]);
746+
});
747+
748+
it('is not judged on its type by the type arms', () => {
749+
const sum = { datasets: [{ name: 'deals', object: 'xs_deal', measures: [{ name: 's', aggregate: 'sum', field: 'score' }] }] };
750+
expect(ids(validateDatasetMeasureAggregates, numberOrText(sum))).toEqual([]);
751+
expect(ids(validateDatasetMeasureAggregates, { ...tie({ score: { type: 'text' } }, {}), ...sum }))
752+
.toEqual([MEASURE_AGGREGATE_FIELD_TYPE_REFUSED]);
753+
expect(ids(validatePresetComparands, dateOrText(widget({ closed_on: 'last_30_days' })))).toEqual([]);
754+
const rule = { sharingRules: [{ name: 'sr', object: 'xs_deal', condition: 'record.closed_on == record.name', sharedWith: { type: 'role', value: 'r' }, accessLevel: 'read' }] };
755+
expect(ids(validateSharingRuleEnforceability, dateOrText(rule))).toEqual([]);
756+
});
757+
758+
it('is admitted on a thank-you panel only when every tied definition is engine-generated', () => {
759+
const panel = (stack: Rec) => ({
760+
...stack,
761+
views: [{
762+
name: 'vv',
763+
form: {
764+
type: 'simple', data: { provider: 'object', object: 'xs_deal' }, sections: [{ name: 'a', fields: ['name'] }],
765+
submitBehavior: { kind: 'thank-you', title: 'T', message: 'Ref {{record.ref_no}}' },
766+
},
767+
}],
768+
});
769+
expect(ids(validateThankYouRecordFields, panel(tie({ ref_no: { type: 'autonumber' } }, { ref_no: { type: 'autonumber' } })))).toEqual([]);
770+
for (const [a, b] of [['autonumber', 'text'], ['text', 'autonumber']]) {
771+
const findings = validateThankYouRecordFields(panel(tie({ ref_no: { type: a } }, { ref_no: { type: b } })));
772+
expect(findings.map((f) => f.rule)).toEqual([THANK_YOU_RECORD_FIELD]);
773+
expect(findings[0].message).toContain('is a text field');
774+
}
775+
});
776+
});
693777
});

‎packages/lint/src/object-graph.ts‎

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -500,17 +500,6 @@ export function objectsAsServed(stack: unknown): { objects: AnyRec[]; tied: Map<
500500
return { objects: served, tied };
501501
}
502502

503-
/**
504-
* [#30383] {@link indexObjectGraph} over {@link objectsAsServed}: the graph of
505-
* the objects the registry serves, every `objectExtensions` field folded in.
506-
* The field-path rules of this package resolve through it, so a path naming a
507-
* field a sibling package adds resolves as it does at runtime, and a field no
508-
* body declares or extends is still a miss.
509-
*/
510-
export function indexServedObjectGraph(stack: unknown): ObjectGraph {
511-
return indexObjectGraph({ objects: objectsAsServed(stack).objects });
512-
}
513-
514503
/** What a path resolved to, or precisely how it failed. */
515504
export type FieldPathVerdict =
516505
/**
@@ -803,3 +792,39 @@ export function listNames(names: Iterable<string>): string {
803792
const arr = [...names].sort();
804793
return arr.length > 0 ? arr.join(', ') : '(none)';
805794
}
795+
796+
// [#30383] Kept out of the fold's section on purpose: `validate-security-posture.test.ts`
797+
// tables every receiver read in that section, and these two read only the fold's output.
798+
799+
/**
800+
* [#30383] The served objects with every `tied` field taken out: what a rule
801+
* may judge a field's DEFINITION on. A field two packages write at the
802+
* priority that decides it is served, but which definition wins is the
803+
* registration order, and the registry registers an artifact's packages in
804+
* dependency-topological order (`resolveArtifactPackageOrder`), not in the
805+
* `packages[]` order the fold breaks ties by. So a type arm leaves a tied field
806+
* unjudged, as the membership-key rule does, and a rule that only asks whether
807+
* the name is served keeps it ({@link indexServedObjectGraph}'s default).
808+
*/
809+
export function objectsWithoutTiedFields(served: { objects: AnyRec[]; tied: Map<string, Set<string>> }): AnyRec[] {
810+
if (served.tied.size === 0) return served.objects;
811+
return served.objects.map((obj) => {
812+
const name = strName(obj.name);
813+
const tiedKeys = name ? served.tied.get(name) : undefined;
814+
if (!tiedKeys) return obj;
815+
return { ...obj, fields: recordsOf(obj.fields).filter((f) => !tiedKeys.has(strName(f.name) ?? '')) };
816+
});
817+
}
818+
819+
/**
820+
* [#30383] {@link indexObjectGraph} over {@link objectsAsServed}: the graph of
821+
* the objects the registry serves, every `objectExtensions` field folded in.
822+
* The field-path rules of this package resolve through it, so a path naming a
823+
* field a sibling package adds resolves as it does at runtime, and a field no
824+
* body declares or extends is still a miss. `{ tied: 'drop' }` is the graph a
825+
* type arm judges on ({@link objectsWithoutTiedFields}).
826+
*/
827+
export function indexServedObjectGraph(stack: unknown, options: { tied?: 'keep' | 'drop' } = {}): ObjectGraph {
828+
const served = objectsAsServed(stack);
829+
return indexObjectGraph({ objects: options.tied === 'drop' ? objectsWithoutTiedFields(served) : served.objects });
830+
}

‎packages/lint/src/validate-dataset-measure-aggregates.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,9 @@ export function validateDatasetMeasureAggregates(stack: unknown): DatasetMeasure
651651
const cubes = collectionEntries(stack.analyticsCubes, 'analyticsCubes');
652652
if (datasets.length === 0 && cubes.length === 0) return findings;
653653

654-
const graph: ObjectGraph = indexServedObjectGraph(stack);
654+
// [#30383] Served, a field two packages tie on left out: which definition
655+
// wins is registration order, so its type is not judged here.
656+
const graph: ObjectGraph = indexServedObjectGraph(stack, { tied: 'drop' });
655657

656658
datasets.forEach((ds, di) => {
657659
// ── Skip 1: no base object, or one the graph cannot answer for ──

‎packages/lint/src/validate-list-view-field-refs.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ import {
429429
indexObjectGraph,
430430
isUnjudgeable,
431431
objectsAsServed,
432+
objectsWithoutTiedFields,
432433
recordsOf,
433434
resolveFieldPath,
434435
suggestName,
@@ -964,8 +965,8 @@ export function validateListViewFieldRefs(
964965
// field folded in: the graph and the option map below both read this, so a
965966
// column, filter key or `groupByField` naming a field a sibling package adds
966967
// resolves as it does at runtime.
967-
const served = objectsAsServed(stack).objects;
968-
const graph: ObjectGraph = indexObjectGraph({ objects: served });
968+
const servedStack = objectsAsServed(stack);
969+
const graph: ObjectGraph = indexObjectGraph({ objects: servedStack.objects });
969970
const scope = options?.packageScope;
970971
// [#24668] The whole collection, stated — and only without a closure, which
971972
// is the narrower set and wins.
@@ -1065,10 +1066,12 @@ export function validateListViewFieldRefs(
10651066
/**
10661067
* [#28018] The inline option values each object's fields declare, read from
10671068
* the same served objects the graph indexes. A field with no non-empty
1068-
* inline `options` array is absent: its lanes are not judged.
1069+
* inline `options` array is absent: its lanes are not judged. [#30383] So is
1070+
* a field two packages tie on: which definition's options win is
1071+
* registration order.
10691072
*/
10701073
const optionValues = new Map<string, Map<string, ReadonlySet<string>>>();
1071-
for (const obj of served) {
1074+
for (const obj of objectsWithoutTiedFields(servedStack)) {
10721075
const objName = strName(obj.name);
10731076
if (!objName) continue;
10741077
const byField = new Map<string, ReadonlySet<string>>();

‎packages/lint/src/validate-preset-comparands.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,9 @@ export function validatePresetComparands(
743743
if (!stack || typeof stack !== 'object') return [];
744744
const out: PresetComparandFinding[] = [];
745745

746-
const graph = indexServedObjectGraph(stack);
746+
// [#30383] Served, a field two packages tie on left out: which definition
747+
// wins is registration order, so its type is not judged here.
748+
const graph = indexServedObjectGraph(stack, { tied: 'drop' });
747749
const datasets = new Map<string, AnyRec>();
748750
for (const dataset of recordsOf(stack.datasets)) {
749751
const name = strName(dataset.name);

‎packages/lint/src/validate-related-list-refs.ts‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
* The children are the stack's `objects` (map or array form) as the registry
2727
* serves them, since an extension can be the one that adds the FK: every
2828
* `objectExtensions` field folded in through the package's one fold
29-
* (`objectsAsServed`, #30383), and a child only an extension reaches (another
30-
* package's object) carrying its extensions' fields alone. A field record is
31-
* read by name in either `fields` shape.
29+
* (`objectsAsServed`, #30383). A field record is read by name in either
30+
* `fields` shape. An object the stack does not declare is not a child here
31+
* even when an extension targets it: its own declaration, and so the FKs it
32+
* declares, may live in a package this stack does not carry (the per-package
33+
* leg of `os build` holds one body's objects), so its extensions alone are not
34+
* its field set, and it falls to the rule for an unknown child below.
3235
*
3336
* - A named child that IS in the stack but carries no such FK is refused on
3437
* every surface: that child's fields are known.
@@ -61,7 +64,7 @@
6164
*/
6265

6366
import { fieldGroupBodyTableRefusal, type NamedInboundRelationship } from '@objectstack/spec/data';
64-
import { objectExtensionsByTarget, objectsAsServed, recordsOf } from './object-graph.js';
67+
import { objectsAsServed, recordsOf } from './object-graph.js';
6568

6669
/** A related-list entry that matches no inbound relationship of its object. */
6770
export const RELATED_LIST_REF_UNKNOWN = 'related-list-ref-unknown';
@@ -136,10 +139,6 @@ export function validateRelatedListRefs(stack: AnyRec, options?: RelatedListRefO
136139
for (const obj of objectsAsServed(stack).objects) {
137140
if (typeof obj.name === 'string') merge(obj.name, recordsOf(obj.fields));
138141
}
139-
for (const [target, extensions] of objectExtensionsByTarget(stack)) {
140-
if (fieldsByObject.has(target)) continue; // folded in above
141-
for (const ext of extensions) merge(target, ext.fields);
142-
}
143142

144143
// Parent name → its inbound relationships, in child then field order.
145144
const inbound = new Map<string, NamedInboundRelationship[]>();

‎packages/lint/src/validate-sharing-rule-enforceability.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192

193193
import { compileCelToFilter } from '@objectstack/formula';
194194
import { referenceCarrierOf } from '@objectstack/spec/data';
195-
import { indexObjectGraph, objectsAsServed, recordsOf, type ObjectGraph } from './object-graph.js';
195+
import { indexObjectGraph, objectsAsServed, objectsWithoutTiedFields, recordsOf, type ObjectGraph } from './object-graph.js';
196196
import {
197197
crossClassComparisons,
198198
crossClassVerdict,
@@ -576,11 +576,19 @@ export function validateSharingRuleEnforceability(stack: unknown): SharingRuleEn
576576
// reads the same `sharingModel`, and a `controlled_by_parent` anchor's master
577577
// is the `master_detail` the served object carries, as the security posture
578578
// rule's merge (#23210) reads it.
579+
const served = objectsAsServed(cfg);
579580
const objectsByName = new Map<string, AnyRec>();
580-
for (const obj of objectsAsServed(cfg).objects) {
581+
for (const obj of served.objects) {
581582
const name = str(obj.name);
582583
if (name) objectsByName.set(name, obj);
583584
}
585+
// The comparison arms judge a field's TYPE, so a field two packages tie on
586+
// is left out of their graph: which definition wins is registration order.
587+
const judgedByName = new Map<string, AnyRec>();
588+
for (const obj of objectsWithoutTiedFields(served)) {
589+
const name = str(obj.name);
590+
if (name) judgedByName.set(name, obj);
591+
}
584592
// [#19886] The served field map of a rule's ANCHOR, for the list-holding
585593
// arm — the only object a lowered sharing criterion can address (a
586594
// cross-object path does not lower). Built lazily, once per anchor, and only
@@ -589,7 +597,7 @@ export function validateSharingRuleEnforceability(stack: unknown): SharingRuleEn
589597
// keeps its precedence.
590598
const graphs = new Map<string, ObjectGraph>();
591599
const anchorGraph = (object: string): ObjectGraph | null => {
592-
const target = objectsByName.get(object);
600+
const target = judgedByName.get(object);
593601
if (!target) return null;
594602
let graph = graphs.get(object);
595603
if (!graph) {

‎packages/lint/src/validate-thank-you-record-fields.ts‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ import { RUNTIME_OWNED_FIELD_TYPES } from '@objectstack/spec/data';
5757
import { anonymousFormObjectName, thankYouRecordFieldNames } from '@objectstack/spec/ui';
5858

5959
import { collectionEntries } from './collection-entries.js';
60-
import { indexServedObjectGraph, type GraphObject } from './object-graph.js';
60+
import {
61+
indexObjectGraph,
62+
objectExtensionsByTarget,
63+
objectsAsServed,
64+
type GraphObject,
65+
} from './object-graph.js';
6166
import { walkPageComponents } from './page-walk.js';
6267

6368
export const THANK_YOU_RECORD_FIELD = 'thank-you-record-field';
@@ -162,6 +167,31 @@ function refusalReason(object: GraphObject, name: string, admitted: ReadonlySet<
162167
return 'not a field the object declares';
163168
}
164169

170+
/**
171+
* [#30383] For a field two packages write at the priority that decides it
172+
* (`objectsAsServed`'s `tied`), which definition the registry serves is the
173+
* registration order, which a stack does not state. The panel is shown to an
174+
* anonymous visitor, so this fails closed: such a field is admitted only when
175+
* every definition at that priority is engine-generated, and is otherwise
176+
* refused naming the first one that is not.
177+
*/
178+
function tiedFieldRefusal(
179+
stack: AnyRec,
180+
tied: ReadonlyMap<string, ReadonlySet<string>>,
181+
admitted: ReadonlySet<string>,
182+
): (objectName: string, name: string) => string | undefined {
183+
if (tied.size === 0) return () => undefined;
184+
const extensions = objectExtensionsByTarget(stack);
185+
return (objectName, name) => {
186+
if (!tied.get(objectName)?.has(name)) return undefined;
187+
const writers = (extensions.get(objectName) ?? []).flatMap((ext) =>
188+
ext.fields.filter((f) => f.name === name).map((f) => ({ priority: ext.priority, type: f.type })));
189+
const deciding = Math.max(...writers.map((w) => w.priority));
190+
const refused = writers.find((w) => w.priority === deciding && !(typeof w.type === 'string' && admitted.has(w.type)));
191+
return refused ? `a ${typeof refused.type === 'string' ? refused.type : 'untyped'} field` : undefined;
192+
};
193+
}
194+
165195
/**
166196
* Refuse every name a `thank-you` panel interpolates that is not an
167197
* engine-generated field. Empty = clean.
@@ -178,15 +208,18 @@ export function validateThankYouRecordFields(
178208
const findings: ThankYouRecordFieldFinding[] = [];
179209
const sites = panelSites(stack);
180210
if (sites.length === 0) return findings;
181-
const graph = indexServedObjectGraph(stack);
211+
// [#30383] The objects as served, every `objectExtensions` field folded in.
212+
const served = objectsAsServed(stack);
213+
const graph = indexObjectGraph({ objects: served.objects });
214+
const tiedRefusal = tiedFieldRefusal(stack, served.tied, admitted);
182215

183216
for (const site of sites) {
184217
const names = thankYouRecordFieldNames(site.submitBehavior);
185218
if (names.length === 0 || !site.objectName) continue;
186219
const object = graph.get(site.objectName);
187220
if (!object) continue;
188221
for (const name of names) {
189-
const reason = refusalReason(object, name, admitted);
222+
const reason = refusalReason(object, name, admitted) ?? tiedRefusal(site.objectName, name);
190223
if (reason === undefined) continue;
191224
findings.push({
192225
severity: 'error',

0 commit comments

Comments
 (0)