Skip to content

Commit 4d1ca8d

Browse files
committed
feat(spec)!: a controlled_by_parent detail's one required master is refused at parse when marked readonly or system
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B2EeooonSXrANpkpgUSfFZ
1 parent e3aa111 commit 4d1ca8d

9 files changed

Lines changed: 220 additions & 31 deletions

‎packages/lint/src/data-model-rules.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,16 +310,19 @@ const CBP_ORPHAN_CONSEQUENCE =
310310
* forces `required: true` on every `master_detail` reference under
311311
* `controlled_by_parent` and refuses an explicit `required: false` —
312312
* `forceCbpMasterDetailRequired` in `packages/spec/src/data/object.zod.ts`).
313-
* The builder never inspects `readonly` / `system`, so shapes 2 and 3 pass
314-
* through `create()` untouched, and shape 1 survives every path that does not
315-
* go through `create()` (a plain object literal, raw `.parse()` of stored
316-
* metadata); this rule is where all three meet a refusal at authoring time.
313+
* The spec parse refuses, at every door that parses, every shape that leaves
314+
* the object without exactly one writable required master (`ObjectSchema`'s
315+
* `controlledByParentMasterRefusal`: a missing `required` since ruling B,
316+
* `readonly` / `system` on the one master since #25092, ruling 6328204856
317+
* route A, shape `master-not-writable`). What only this rule reports is a
318+
* shape on a second, non-required `master_detail` beside the master.
317319
*
318320
* ⚠ Reach: `lintDataModel` is `os lint`'s sweep and the metadata-generation
319321
* rubric (`score.ts`), and nothing else — it is not an `AUTHORING_RULES` entry
320322
* (`authoring-rule-wiring.test.ts` → `DIRECT_CALL_RATCHET`), so `os build`,
321-
* `os validate` and the metadata save door do not run it. The `error` moves
322-
* `os lint`'s exit code and the rubric's `valid`, not a publish verdict.
323+
* `os validate` and the metadata save door do not run it; they meet the spec
324+
* parse's refusal above instead. The `error` moves `os lint`'s exit code and
325+
* the rubric's `valid`, not a publish verdict.
323326
*
324327
* One finding per field, located at the first defect it names; the `fix`
325328
* names every edit the field needs.

‎packages/lint/src/rule-explanations.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,10 +2779,11 @@ const RELATIONSHIP_MASTER_DETAIL_REQUIRED_EXPLANATION: RuleExplanation = {
27792779
'security gate (`assertControlledByParentWrite`) is the only thing refusing such an insert.',
27802780
'Every `master_detail` field of such an object is judged, not only the one the runtime resolves ' +
27812781
'as the master: that is the scope `ObjectSchema.create()` enforces when it forces ' +
2782-
'`required: true` and refuses an explicit `required: false`. The builder never inspects ' +
2783-
'`readonly` or `system`, and a plain object literal or a raw `.parse()` of stored metadata ' +
2784-
'never runs it, so this rule is where all three shapes meet a refusal at authoring time. One ' +
2785-
'finding per field, located at its first defect.',
2782+
'`required: true` and refuses an explicit `required: false`. The spec parse also ' +
2783+
'refuses every shape that leaves the object without exactly one writable required master, so ' +
2784+
'`os validate`, `os build` and the save door stop there; a shape on a second, non-required ' +
2785+
'`master_detail` beside the master is this rule\'s report alone. One finding per field, located ' +
2786+
'at its first defect.',
27862787
DATA_MODEL_SWEEP_REACH,
27872788
],
27882789
};

‎packages/spec/src/conversions/refused-shapes.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,27 @@ describe('#23270 — a stored controlled_by_parent object without exactly one re
151151
expect(JSON.parse(todo.from).fields.title).toBeUndefined();
152152
});
153153

154+
it.each([
155+
['readonly: true', md('crm_opportunity', { required: true, readonly: true })],
156+
['system: true', md('crm_opportunity', { required: true, system: true })],
157+
// The Studio shape plus the flag: D2 gives `required: true`, and the flag is still listed.
158+
['readonly: true, `required` omitted', md('crm_opportunity', { readonly: true })],
159+
])('#25092 — a one master marked %s: one TODO under the same entry, and the TODO does not claim the runtime denies it', (_label, master) => {
160+
const row = cbpRow('crm_line', { opportunity: master });
161+
const { item, todos } = storedTodos('object', row);
162+
expect(item.fields.opportunity.readonly ?? item.fields.opportunity.system).toBe(true);
163+
expect(todos).toHaveLength(1);
164+
const todo = todos[0]!;
165+
expect(todo.conversionId).toBe('cbp-master-relation-tiers-retired');
166+
expect(todo.path).toBe('objects[0].fields');
167+
expect(todo.reason).toMatch(/Its one required `master_detail`, `opportunity`, is marked `readonly` or `system`\. Dropping the flag changes who may write the reference/);
168+
expect(todo.reason).toContain('the metadata save door refuses it with 422 INVALID_METADATA until it is corrected');
169+
expect(todo.reason).not.toContain('every read of its rows is denied');
170+
expect(todo.reason).toContain("Object 'crm_line' declares sharingModel 'controlled_by_parent' with its one required `master_detail`");
171+
// The listing and the parse refuse ONE set: the stored row still fails the parse, at the same key.
172+
expect(cbpIssue(item)?.path).toEqual(['fields']);
173+
});
174+
154175
it('the Studio shape — one master_detail, `required` omitted — converts and is NOT listed', () => {
155176
const row = cbpRow('crm_line', { opportunity: md('crm_opportunity') });
156177
const { item, todos, notices } = storedTodos('object', row);

‎packages/spec/src/conversions/refused-shapes.ts‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import { flowNodeValueTemplateRefusals } from '../automation/flow-value-slot-template.js';
5151
import {
5252
controlledByParentMasterRefusal,
53+
isControlledByParentMasterField,
5354
type ControlledByParentMasterRefusal,
5455
} from '../data/controlled-by-parent-master.js';
5556
import { ComponentPropsMap } from '../ui/component.zod.js';
@@ -123,7 +124,31 @@ function controlledByParentFate(refusal: ControlledByParentMasterRefusal, fields
123124
return 'It declares no `master_detail` and no required `lookup`, so there is no master to derive '
124125
+ 'and nothing to convert. Add a `master_detail` field with `required: true` referencing the master '
125126
+ `object, ${ownSharingModel}.`;
127+
case 'master-not-writable': {
128+
const master = refusal.masterDetails.find((n) => isControlledByParentMasterField(fields[n]))!;
129+
return `Its one required \`master_detail\`, \`${master}\`, `
130+
+ 'is marked `readonly` or `system`. Dropping the flag changes who may write the reference, so the '
131+
+ 'upgrade does not write it. Remove the flag and declare the master reference as an ordinary required '
132+
+ `field, ${ownSharingModel}.`;
133+
}
134+
}
135+
}
136+
137+
/**
138+
* What a stored row in a refused shape does once it is left as stored. Every
139+
* shape but one leaves the runtime with no master to derive. A
140+
* `master-not-writable` object still resolves its one required `master_detail`
141+
* as the master at runtime (ADR-0055 §1 refuses it at the parse, not at the
142+
* runtime), so what it loses is the save door: it is re-saved only once
143+
* corrected.
144+
*/
145+
function controlledByParentLeftAsStored(refusal: ControlledByParentMasterRefusal): string {
146+
if (refusal.shape === 'master-not-writable') {
147+
return 'Left as stored, it registers under `[metadata_spec_invalid]` and the metadata save door refuses '
148+
+ 'it with 422 INVALID_METADATA until it is corrected.';
126149
}
150+
return 'Left as stored, it registers under `[metadata_spec_invalid]` and the platform derives no master: '
151+
+ 'every read of its rows is denied and every write refused with 422 INVALID_METADATA.';
127152
}
128153

129154
const controlledByParentWithoutOneMaster: RefusedStoredShape = {
@@ -141,9 +166,8 @@ const controlledByParentWithoutOneMaster: RefusedStoredShape = {
141166
path: `${path}.${refusal.path}`,
142167
from: JSON.stringify({ sharingModel: obj.sharingModel, fields: relation }),
143168
reason: 'No lossless rewrite: protocol 18 derives a controlled-by-parent object\'s master from exactly '
144-
+ 'one `master_detail` declared `required: true`, and this object does not declare exactly one. Left as stored, '
145-
+ 'it registers under `[metadata_spec_invalid]` and the platform derives no master: every read of '
146-
+ 'its rows is denied and every write refused with 422 INVALID_METADATA. '
169+
+ 'one writable `master_detail` declared `required: true`, and this object does not declare exactly one. '
170+
+ `${controlledByParentLeftAsStored(refusal)} `
147171
+ `${controlledByParentFate(refusal, fields)} ${refusal.message}`,
148172
});
149173
return obj;

‎packages/spec/src/data/controlled-by-parent-master.test.ts‎

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
22

33
import { describe, expect, it } from 'vitest';
4+
import { ZodError } from 'zod';
45

56
// Read through the `@objectstack/spec/data` entry, not the module: the pin is
67
// on the PUBLISHED predicate a producer imports (#23272).
@@ -58,6 +59,25 @@ const SHAPES: Array<[string, Record<string, unknown>, ControlledByParentMasterSh
5859
['one master_detail, required omitted', { order: md('work_order') }, 'master-not-required'],
5960
['a required lookup only', { order: lookup('work_order', { required: true }) }, 'required-lookup-only'],
6061
['no relation', { note: { type: 'text', label: 'Note' } }, 'no-master'],
62+
// [#25092] The one master is writable: `readonly` / `system` on it are refused.
63+
['one required master_detail, readonly: true', { order: md('work_order', { required: true, readonly: true }) }, 'master-not-writable'],
64+
['one required master_detail, system: true', { order: md('work_order', { required: true, system: true }) }, 'master-not-writable'],
65+
[
66+
'one required master_detail, readonly: true and system: true',
67+
{ order: md('work_order', { required: true, readonly: true, system: true }) },
68+
'master-not-writable',
69+
],
70+
[
71+
'one required master_detail, readonly: true, beside a required lookup',
72+
{ order: md('work_order', { required: true, readonly: true }), owner_ref: lookup('account', { required: true }) },
73+
'master-not-writable',
74+
],
75+
['one required master_detail, readonly: false and system: false', { order: md('work_order', { required: true, readonly: false, system: false }) }, undefined],
76+
[
77+
'a readonly required master_detail beside a second required one is still two masters',
78+
{ a: md('work_order', { required: true, readonly: true }), b: md('account', { required: true }) },
79+
'several-required-masters',
80+
],
6181
];
6282

6383
describe('controlledByParentMasterRefusal — the export and ObjectSchema agree (#23272)', () => {
@@ -88,6 +108,9 @@ describe('controlledByParentMasterRefusal — the export and ObjectSchema agree
88108
describe('isControlledByParentMasterField — the field half the refusal counts (#23272)', () => {
89109
it.each([
90110
['a required master_detail', md('x', { required: true }), true],
111+
// The flags are judged on the object's one master, not here: the field is still the master.
112+
['a required master_detail, readonly: true', md('x', { required: true, readonly: true }), true],
113+
['a required master_detail, system: true', md('x', { required: true, system: true }), true],
91114
['a master_detail, required: false', md('x', { required: false }), false],
92115
['a master_detail, required omitted', md('x'), false],
93116
['a master_detail, required "true" (not a boolean)', md('x', { required: 'true' }), false],
@@ -100,11 +123,54 @@ describe('isControlledByParentMasterField — the field half the refusal counts
100123
expect(isControlledByParentMasterField(field)).toBe(expected);
101124
});
102125

103-
it('is the count the refusal makes: exactly one counting field satisfies the rule', () => {
126+
it('is the count the refusal makes: exactly one counting field, and a writable one, satisfies the rule', () => {
104127
for (const [, fields] of SHAPES) {
105-
const counted = Object.values(fields).filter(isControlledByParentMasterField).length;
128+
const counted = Object.values(fields).filter(isControlledByParentMasterField) as Array<Record<string, unknown>>;
129+
const writable = counted.length === 1 && counted[0]!.readonly !== true && counted[0]!.system !== true;
106130
const refusal = controlledByParentMasterRefusal('o', 'controlled_by_parent', fields);
107-
expect(refusal === undefined).toBe(counted === 1);
131+
expect(refusal === undefined).toBe(writable);
108132
}
109133
});
110134
});
135+
136+
describe('#25092 a one master marked readonly or system is refused at authoring, through the one rule', () => {
137+
it('names the object, the field and every flag, and prescribes dropping them', () => {
138+
const refusal = controlledByParentMasterRefusal('work_order_item', 'controlled_by_parent', {
139+
order: md('work_order', { required: true, readonly: true, system: true }),
140+
});
141+
expect(refusal).toMatchObject({ path: 'fields', shape: 'master-not-writable', masterDetails: ['order'] });
142+
expect(refusal!.message).toMatch(
143+
/^Object 'work_order_item' declares sharingModel 'controlled_by_parent' with its one required `master_detail`, `order`, marked `readonly: true` and `system: true`\./,
144+
);
145+
expect(refusal!.message).toContain('Remove `readonly: true` and `system: true` from `order`');
146+
});
147+
148+
it('ObjectSchema.create() refuses it too: the builder forces `required` and the parse it ends in refuses the flag', () => {
149+
const build = () =>
150+
ObjectSchema.create({
151+
name: 'work_order_item',
152+
sharingModel: 'controlled_by_parent',
153+
fields: { order: { type: 'master_detail', label: 'Order', reference: 'work_order', readonly: true } },
154+
});
155+
let thrown: unknown;
156+
try {
157+
build();
158+
} catch (err) {
159+
thrown = err;
160+
}
161+
expect(thrown).toBeInstanceOf(ZodError);
162+
const issues = (thrown as ZodError).issues.filter((i) => /declares sharingModel 'controlled_by_parent'/.test(i.message));
163+
expect(issues).toHaveLength(1);
164+
expect(issues[0]!.path).toEqual(['fields']);
165+
expect(issues[0]!.message).toContain('with its one required `master_detail`, `order`, marked `readonly: true`');
166+
});
167+
168+
it('control: the same builder call without the flag builds', () => {
169+
const obj = ObjectSchema.create({
170+
name: 'work_order_item',
171+
sharingModel: 'controlled_by_parent',
172+
fields: { order: { type: 'master_detail', label: 'Order', reference: 'work_order' } },
173+
});
174+
expect(obj.fields.order.required).toBe(true);
175+
});
176+
});

‎packages/spec/src/data/controlled-by-parent-master.ts‎

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
* `z.boolean().default(false)`, so on the parsed map an omitted `required` is
3636
* `false`, and on a stored body it is `undefined`. The predicate counts a
3737
* master only where `required === true`, so the two give the same verdict.
38+
* `readonly` (`z.boolean().default(false)`) and `system` (optional) are read
39+
* the same way: only `=== true` marks the one master unwritable.
3840
*/
3941

4042
/**
@@ -52,18 +54,29 @@
5254
* (or a non-boolean the field parse refuses on its own);
5355
* - `required-lookup-only` — no `master_detail`, one or more required `lookup`
5456
* fields (the retired third tier);
55-
* - `no-master` — no `master_detail` and no required `lookup`.
57+
* - `no-master` — no `master_detail` and no required `lookup`;
58+
* - `master-not-writable` — exactly one required `master_detail`, declared
59+
* `readonly: true` or `system: true`. ADR-0055 §1 derives the master from a
60+
* reference every write of a detail supplies, and those two flags mark a
61+
* field record validation skips before its required check is reached (one
62+
* the platform fills, or one a write may not set), so the one master would be
63+
* required in name only. Dropping the flag is the owner's call: it changes
64+
* who may write the reference.
5665
*/
5766
export type ControlledByParentMasterShape =
5867
| 'several-required-masters'
5968
| 'several-masters'
6069
| 'master-not-required'
6170
| 'required-lookup-only'
62-
| 'no-master';
71+
| 'no-master'
72+
| 'master-not-writable';
6373

6474
/** Why an object fails the one-required-master rule — see {@link controlledByParentMasterRefusal}. */
6575
export interface ControlledByParentMasterRefusal {
66-
/** Where the parse reports it: `fields` (more than one candidate) or `sharingModel` (none). */
76+
/**
77+
* Where the parse reports it: `fields` (more than one candidate, or the one
78+
* master is not writable) or `sharingModel` (none).
79+
*/
6780
readonly path: 'fields' | 'sharingModel';
6881
readonly shape: ControlledByParentMasterShape;
6982
/** The `master_detail` fields the object declares, in field order. */
@@ -79,7 +92,11 @@ export interface ControlledByParentMasterRefusal {
7992
* A `master_detail` field declared `required: true`, and nothing else: a
8093
* non-required `master_detail` and a required `lookup` are the two tiers
8194
* ruling B retired (#23100). An object satisfies the rule when EXACTLY ONE of
82-
* its fields counts — {@link controlledByParentMasterRefusal} is that count.
95+
* its fields counts, and that one is writable (neither `readonly: true` nor
96+
* `system: true`) — {@link controlledByParentMasterRefusal} is that judgement.
97+
* The flags are judged on the one master, not here: a flagged required
98+
* `master_detail` is still the field the runtime resolves as the master, so a
99+
* second one beside it is still two masters.
83100
*
84101
* Reads `required === true` on either the parsed field or a stored body (see
85102
* the module header), and accepts any value: a non-object counts as no master.
@@ -90,6 +107,12 @@ export function isControlledByParentMasterField(field: unknown): boolean {
90107
return f.type === 'master_detail' && f.required === true;
91108
}
92109

110+
/**
111+
* The writability flags a `controlled_by_parent` master may not carry, in the
112+
* order a refusal names them. See the `master-not-writable` shape.
113+
*/
114+
const UNWRITABLE_MASTER_FLAGS = ['readonly', 'system'] as const;
115+
93116
/**
94117
* Judge one object against ADR-0055 §1's one-required-master rule. Returns
95118
* `undefined` when the rule does not apply (another sharing model, no field
@@ -109,7 +132,27 @@ export function controlledByParentMasterRefusal(
109132
.filter(([, f]) => f?.type === 'lookup' && f.required === true)
110133
.map(([key]) => key);
111134
const masters = entries.filter(([, f]) => isControlledByParentMasterField(f)).map(([key]) => key);
112-
if (masters.length === 1) return undefined;
135+
if (masters.length === 1) {
136+
const master = masters[0]!;
137+
const def = (fields as Record<string, Record<string, unknown> | undefined>)[master];
138+
const flags = UNWRITABLE_MASTER_FLAGS.filter((flag) => def?.[flag] === true);
139+
if (flags.length === 0) return undefined;
140+
return {
141+
path: 'fields',
142+
shape: 'master-not-writable',
143+
masterDetails,
144+
requiredLookups,
145+
message:
146+
`Object '${name}' declares sharingModel 'controlled_by_parent' with its one required \`master_detail\`, `
147+
+ `\`${master}\`, marked ${flags.map((f) => `\`${f}: true\``).join(' and ')}. ADR-0055 §1: since `
148+
+ '@objectstack/spec 18, a controlled-by-parent object derives its master from exactly one required '
149+
+ '`master_detail` field that every write of a detail supplies, and a `readonly` or `system` field is '
150+
+ 'one record validation skips before its required check, so this reference would be required in name '
151+
+ `only. Remove ${flags.map((f) => `\`${f}: true\``).join(' and ')} from \`${master}\` and declare the `
152+
+ 'master reference as an ordinary required field; if the rows must outlive their parent, this object '
153+
+ "is not a detail: give it its own sharingModel ('private', 'public_read' or 'public_read_write').",
154+
};
155+
}
113156
if (masters.length > 1) {
114157
return {
115158
path: 'fields',

0 commit comments

Comments
 (0)