diff --git a/.changeset/33940-cbp-omitted-required-lint-default-door.md b/.changeset/33940-cbp-omitted-required-lint-default-door.md new file mode 100644 index 00000000000..a83a15911df --- /dev/null +++ b/.changeset/33940-cbp-omitted-required-lint-default-door.md @@ -0,0 +1,15 @@ +--- +'@objectstack/spec': patch +'@objectstack/lint': patch +--- + +fix(spec,lint): `os lint` reports an omitted `required` on a `controlled_by_parent` object's `master_detail` on the default strict `defineStack` door + +Clause-②: yes +Metadata-protocol: none + +The semantic migration entry `cbp-optional-master-admitted` (maintainer ruling A on the optional master, ADR-0055 §1 as amended) says that a raw parse reads an omitted `required` as the optional master and that `os lint` reports the omission at `error`, so the author writes `true` or `false`. On a project whose `objectstack.config.ts` calls `defineStack({ … })` with its default strict parse, `os lint` did not: the parse had already written `false` into the omitted key before any command loaded the stack, so `relationship/master-detail-required` read the explicit optional master and passed. Only `defineStack(…, { strict: false })` and a raw object config reached the finding. + +**`@objectstack/spec`.** A strict `defineStack` now records, on each parsed `master_detail` field of a `controlled_by_parent` object whose authored definition writes no `required`, that the key was omitted. The new reader `masterDetailRequiredOmitted(field)` (`@objectstack/spec/data`) answers it. The record is a non-enumerable symbol on the field: it is not a key, so the field schema, `JSON.stringify` and the compiled `dist/objectstack.json` never see it, and any copy of the field drops it. Nothing parsed changes: `required` is still `false`, the stack still loads, and the runtime still reads the field as the optional master, as ruled. + +**`@objectstack/lint`.** `relationship/master-detail-required` reads that record, so on the default door it now reports the omitted `required` on the object's only `master_detail` at `error`, with the same message, path and fix it already gave on `{ strict: false }`: write `required: true` (every detail names its master) or `required: false` (an optional master). An explicit `required: false` or `required: true` still lints clean. `os validate` and `os build` are unchanged. diff --git a/docs/qa/platform-checklist/areas/cli.json b/docs/qa/platform-checklist/areas/cli.json index 0e08eb0bc4e..304e708eeed 100644 --- a/docs/qa/platform-checklist/areas/cli.json +++ b/docs/qa/platform-checklist/areas/cli.json @@ -2424,7 +2424,7 @@ "title": "`os validate` / `os build` verdicts new on v18: an unknown seed record key (with did-you-mean), a misspelt record[…] / previous[…] field, a duplicate stack entry name, a controlled_by_parent object with no master_detail or with two required ones (also through objectExtensions), a component-props finding (now an error), and a credential in stack email/sms config each fail the build", "since": "v18", "status": "active", - "revision": 5, + "revision": 6, "priority": "P1", "surface": "cli", "personas": [ @@ -2440,7 +2440,7 @@ "steps": [ "for each arm run `os validate --json` and `os build`, recording exit code + rule id + path: seed key created_atx; record[\"typo\"] in a validation; two datasources named alike; a cbp object with no master_detail; a cbp object with two master_details both declared required: true, once inline and once with the second arriving through objectExtensions; an object-grid with defaultFilters; email:{apiKey:\"x\"}", "controls: seed key created_at, record.name == \"record.typo\", email.provider, and a cbp object whose ONLY master_detail declares required: false (the optional master, ruling A of #30905) — all pass", - "the os lint arm: in the scratch app switch the stack call to `defineStack({ … }, { strict: false })` — the one door where the omission survives to `os lint` — and author a cbp object as a plain object literal (not through ObjectSchema.create(), which forces an omitted required to true) whose ONLY master_detail omits `required`; run `os lint --json`, then `os validate --json` and `os build`, recording each exit code and rule id. Not the default strict `defineStack({ … })`: its parse defaults the omitted `required` to false before any CLI verb loads the stack, so `os lint` reads the optional master and reports nothing. Not a raw-object default export either: `os lint` reports the rule there, but `os validate` and `os build` refuse any export `defineStack` did not build" + "the os lint arm: in the scratch app keep the default strict `defineStack({ … })` and author a cbp object as a plain object literal (not through ObjectSchema.create(), which forces an omitted required to true) whose ONLY master_detail omits `required`; run `os lint --json`, then `os validate --json` and `os build`, recording each exit code and rule id. The strict parse defaults the omitted `required` to false (ruling A of #30905, the optional master) and records the omission on the field it returns, which the lint rule reads. `defineStack({ … }, { strict: false })` reaches the same finding (the key is still absent there). Not a raw-object default export: `os lint` reports the rule there, but `os validate` and `os build` refuse any export `defineStack` did not build" ], "acceptance": [ { @@ -2462,9 +2462,9 @@ "evidence": "four lines" }, { - "clause": "the lint-only shape is `os lint`'s verdict, not the build's: on a stack built by `defineStack(…, { strict: false })` (steps[2]'s door), a cbp object whose ONLY master_detail omits `required` fails `os lint` with relationship/master-detail-required at error, while `os validate` and `os build` accept it", + "clause": "the lint-only shape is `os lint`'s verdict, not the build's: on a stack built by the default strict `defineStack({ … })` (steps[2]'s door), a cbp object whose ONLY master_detail omits `required` fails `os lint` with relationship/master-detail-required at error, while `os validate` and `os build` accept it", "oracle": "build", - "verify": "on the `strict: false` door, `os lint` exits ≠ 0 naming relationship/master-detail-required at the master_detail's path (it is a lintDataModel rule, packages/lint/src/data-model-rules.ts#RELATIONSHIP_MASTER_DETAIL_REQUIRED, which `os lint` runs and `os validate` / `os build` do not; the same run also reports security-controlled-by-parent-no-relation at error on the object's sharingModel); the door matters because the rule reads the stack as loaded and, as its docblock says, the strict parse cannot see the omission: on the default strict `defineStack` the same object reaches every verb as `required: false`, `os lint` exits 0 with no relationship/master-detail-required, and that run is not this clause's arm; `os validate --json` and `os build` exit 0, because the parse reads the parsed `required: false` and admits the optional master (packages/spec/src/data/controlled-by-parent-master.ts#controlledByParentMasterRefusal, ruling A of #30905). An explicit `required: false` on the ONLY master_detail is the optional master and passes all three (a control, steps[1]); the rule's cases are pinned at unit by packages/lint/src/data-model-rules.master-detail-required.test.ts, acceptance[0]'s pin", + "verify": "on the default strict door, `os lint` exits ≠ 0 naming relationship/master-detail-required at the master_detail's `.required` path (it is a lintDataModel rule, packages/lint/src/data-model-rules.ts#RELATIONSHIP_MASTER_DETAIL_REQUIRED, which `os lint` runs and `os validate` / `os build` do not); the rule sees the omission there because `defineStack` records it on the parsed field (packages/spec/src/data/master-detail-required-omission.ts#masterDetailRequiredOmitted) after the parse has written `required: false`. The same run does NOT report security-controlled-by-parent-no-relation: the parsed field is the optional master the runtime derives access from. On `defineStack(…, { strict: false })` the key stays absent, so `os lint` reports relationship/master-detail-required at the same path and also security-controlled-by-parent-no-relation at the object's sharingModel. `os validate --json` and `os build` exit 0 on both doors, because the parse reads the parsed `required: false` and admits the optional master (packages/spec/src/data/controlled-by-parent-master.ts#controlledByParentMasterRefusal, ruling A of #30905). An explicit `required: false` on the ONLY master_detail is the optional master and passes all three (a control, steps[1]); the rule's cases are pinned at unit by packages/lint/src/data-model-rules.master-detail-required.test.ts, acceptance[0]'s pin", "evidence": "the os lint, os validate and os build exits + the lint finding line" } ], @@ -2509,6 +2509,12 @@ "date": "2026-11-15", "change": "acceptance[0].verify names `--reporter=tree`, the reporter that prints the one line per pin file this is scored on: in a runner's non-TTY shell vitest's default reporter prints the run's totals only (RUNNER.md's environment facts; QA run #33784's note 10, comment 6893243542). No clause or threshold changed. Card #33969", "ref": "#34068" + }, + { + "revision": 6, + "date": "2026-11-15", + "change": "steps[2] and acceptance[3] move the os lint arm back to the default strict `defineStack({ … })` (card #33940, the reach half of ruling A of #30905: `os lint` errors on the omitted spelling). `defineStack` now records an omitted `required` on a cbp master_detail on the parsed field (packages/spec/src/data/master-detail-required-omission.ts#masterDetailRequiredOmitted) and relationship/master-detail-required reads it. Driven live in a scratch copy of examples/app-todo with the CLI from the tree: default strict door `os lint --json` exit 1 with relationship/master-detail-required at objects[2].fields.hdr.required (no security-controlled-by-parent-no-relation), `os validate --json` / `os build` exit 0; `required: false` and `required: true` lint clean; `{ strict: false }` unchanged from rev 4. The other clauses are unchanged", + "ref": "#33940" } ] }, diff --git a/packages/lint/src/data-model-rules.master-detail-required.test.ts b/packages/lint/src/data-model-rules.master-detail-required.test.ts index 4413761a5ff..d487c21b21e 100644 --- a/packages/lint/src/data-model-rules.master-detail-required.test.ts +++ b/packages/lint/src/data-model-rules.master-detail-required.test.ts @@ -26,7 +26,8 @@ */ import { describe, expect, it } from 'vitest'; -import { Field, ObjectSchema } from '@objectstack/spec/data'; +import { defineStack } from '@objectstack/spec'; +import { Field, ObjectSchema, masterDetailRequiredOmitted } from '@objectstack/spec/data'; import { lintDataModel as lintDataModelUnrecorded, RELATIONSHIP_DELETE_BEHAVIOR, @@ -229,6 +230,67 @@ describe('R2 over objects built by ObjectSchema.create()', () => { }); }); +// [#33940 · ruling A, record 6687830530] What reaches the rule from the +// DEFAULT door: a config module that calls `defineStack` itself, strict. The +// parse writes `false` into an omitted `required` (the optional master, as +// ruled) and records the omission on the field it returns; the rule reads that +// record, so the omitted spelling is reported here exactly as on the raw +// literal and on `{ strict: false }`. The stack still loads in every case. +describe('R2 over a stack a strict defineStack built — the omitted spelling is still reported (#33940)', () => { + const built = (shape: Record, options?: { strict: boolean }) => + ( + defineStack( + { + manifest: { id: 'com.example.r2-door', namespace: 'wo', version: '0.0.1', type: 'app', name: 'R2' }, + objects: [ + { name: 'wo_order', label: 'Order', fields: { name: { type: 'text', label: 'Name' } } }, + { + name: 'wo_item', + label: 'Item', + sharingModel: 'controlled_by_parent', + fields: { order: { type: 'master_detail', label: 'Order', reference: 'wo_order', deleteBehavior: 'cascade', ...shape } }, + }, + ], + } as never, + options, + ) as unknown as { objects: any[] } + ).objects; + + it('the sole master_detail without `required`: one error at `.required`, naming both spellings', () => { + const objects = built({}); + // The parsed value is the ruled one: `false`, the optional master — recorded as omitted. + expect(objects[1].fields.order.required).toBe(false); + expect(masterDetailRequiredOmitted(objects[1].fields.order)).toBe(true); + const issues = r2(objects); + expect(issues).toHaveLength(1); + expect(issues[0]).toMatchObject({ + severity: 'error', + rule: R2, + path: 'objects[1].fields.order.required', + fix: 'required: true', + }); + expect(issues[0]!.message).toContain('declares no required: write true (every detail names its master) or false (an optional master)'); + }); + + it('CONTROL: an explicit `required: false` (the optional master) and `required: true` lint clean', () => { + expect(r2(built({ required: false }))).toEqual([]); + expect(r2(built({ required: true }))).toEqual([]); + }); + + it('CONTROL: `{ strict: false }` reports the same finding, on the same path, as before', () => { + const issues = r2(built({}, { strict: false })); + expect(issues.map((issue) => [issue.severity, issue.path, issue.fix])).toEqual([ + ['error', 'objects[1].fields.order.required', 'required: true'], + ]); + }); + + it('a copy of the built field drops the record and reads as the explicit `false` — the pre-record reading', () => { + const objects = built({}); + const copied = [objects[0], { ...objects[1], fields: { order: { ...objects[1].fields.order } } }]; + expect(r2(copied)).toEqual([]); + }); +}); + describe('[#22161] one-line verdicts — relationship/master-detail-required, relationship/delete-behavior', () => { it('the published constants spell the ids the findings carry', () => { expect(RELATIONSHIP_MASTER_DETAIL_REQUIRED).toBe(R2); diff --git a/packages/lint/src/data-model-rules.ts b/packages/lint/src/data-model-rules.ts index 63d9e3df2a3..a8f4393ba46 100644 --- a/packages/lint/src/data-model-rules.ts +++ b/packages/lint/src/data-model-rules.ts @@ -21,6 +21,7 @@ import { BOOLEAN_VALUE_TYPES, NUMERIC_VALUE_TYPES, isAggregateCompatibleWithFieldType, + masterDetailRequiredOmitted, referenceCarrierOf, type AggregationFunction, } from '@objectstack/spec/data'; @@ -348,6 +349,12 @@ const CBP_ORPHAN_CONSEQUENCE = * `false`, so it admits the optional master) while `ObjectSchema.create()` * forces it to `true`: one source, two meanings, until the author writes one. * The fix stays `required: true`, the reading the builder already gives it. + * This rule sees the omission on every stack `os lint` loads: an unparsed one + * still lacks the key, and on one a strict `defineStack` parsed (the default + * door, where the field arrives as `false`) the producer RECORDED it on the + * field — `masterDetailRequiredOmitted` (`@objectstack/spec/data`) is that + * record's reader (#33940). A field that lost the record (a copy) reads as the + * explicit `false`, the optional master: the pre-record reading. * * Why these three. A `controlled_by_parent` detail derives ALL of its record * access from the master its `master_detail` reference names (ADR-0055: such an @@ -413,8 +420,12 @@ function cbpMasterReferenceFinding( // `master_detail`, declared `required: false`, is its optional master — the // spelling passes. Beside another `master_detail` an explicit `false` is // judged as before; an omitted `required` is reported on every field. + // [#33940] On a stack `defineStack` parsed, an omitted `required` arrives as + // the parsed `false`; the producer's record of the omission + // (`masterDetailRequiredOmitted`) is what tells the two apart, so the + // omitted spelling is reported on the default strict door too. const soleMasterDetail = masterDetailCount === 1; - const optionalMaster = soleMasterDetail && def.required === false; + const optionalMaster = soleMasterDetail && def.required === false && !masterDetailRequiredOmitted(def); const missingRequired = def.required !== true && !optionalMaster; // On the sole `master_detail`, an omitted (or non-boolean) `required` has // two honest spellings; elsewhere only `required: true` is one. diff --git a/packages/spec/api-surface/data.json b/packages/spec/api-surface/data.json index 232ca32bc7d..c51647d2e2a 100644 --- a/packages/spec/api-surface/data.json +++ b/packages/spec/api-surface/data.json @@ -1039,6 +1039,7 @@ "lintAuthoredRecordKeys (function)", "lowerFilterCondition (function)", "markFilterSubtreeProvenance (function)", + "masterDetailRequiredOmitted (function)", "matchesLikePattern (function)", "materializeTreeSelfReferences (function)", "missingFieldValues (function)", diff --git a/packages/spec/export-origins/data.json b/packages/spec/export-origins/data.json index 62c0528ff73..075eb431885 100644 --- a/packages/spec/export-origins/data.json +++ b/packages/spec/export-origins/data.json @@ -1025,6 +1025,7 @@ "lintAuthoredRecordKeys": "src/data/authoring-key-lint.ts#lintAuthoredRecordKeys (function)", "lowerFilterCondition": "src/data/filter-lowering.ts#lowerFilterCondition (function)", "markFilterSubtreeProvenance": "src/data/filter-subtree-provenance.ts#markFilterSubtreeProvenance (function)", + "masterDetailRequiredOmitted": "src/data/master-detail-required-omission.ts#masterDetailRequiredOmitted (function)", "matchesLikePattern": "src/data/filter.zod.ts#matchesLikePattern (function)", "materializeTreeSelfReferences": "src/data/object.zod.ts#materializeTreeSelfReferences (function)", "missingFieldValues": "src/data/autonumber-format.ts#missingFieldValues (function)", diff --git a/packages/spec/src/data/controlled-by-parent-master.ts b/packages/spec/src/data/controlled-by-parent-master.ts index aabd7e02da7..97fb496d292 100644 --- a/packages/spec/src/data/controlled-by-parent-master.ts +++ b/packages/spec/src/data/controlled-by-parent-master.ts @@ -56,7 +56,10 @@ * `error` — and an object registered without the parse keeps failing closed * on it. A raw parse cannot see the omission (the field default has already * written `false` when the object refinement runs), so there an omitted - * `required` reads as the optional master. + * `required` reads as the optional master. `defineStack` keeps that reading + * and records the omission beside it, on the parsed field + * (`master-detail-required-omission.ts`), so the linter can still report the + * omitted spelling; nothing here reads that record. * `readonly` (`z.boolean().default(false)`) and `system` (optional) are read * the same way: only `=== true` marks the one master unwritable. */ diff --git a/packages/spec/src/data/index.ts b/packages/spec/src/data/index.ts index bfaa2ac4eaf..79f1d73f44c 100644 --- a/packages/spec/src/data/index.ts +++ b/packages/spec/src/data/index.ts @@ -423,6 +423,12 @@ export * from './injected-system-column-provenance'; // that writes `controlled_by_parent` asks the spec instead of restating it. export * from './controlled-by-parent-master'; +// [ADR-0055 §1 as amended · ruling A] `defineStack`'s record that an author +// omitted `required` on a `controlled_by_parent` object's `master_detail` — +// the parse has already written `false` there. Only the reader is published; +// `@objectstack/lint` reads it to report the omitted spelling. +export { masterDetailRequiredOmitted } from './master-detail-required-omission'; + // record-currency path refusals (ADR-0144 D2/D6, amended) — the one wording of // "segment 2 of a `PARENT_FIELD.CODE_FIELD` path names no code field" and of a // `record` roll-up over a child amount that follows no parent. Read by diff --git a/packages/spec/src/data/master-detail-required-omission.test.ts b/packages/spec/src/data/master-detail-required-omission.test.ts new file mode 100644 index 00000000000..c9225a60371 --- /dev/null +++ b/packages/spec/src/data/master-detail-required-omission.test.ts @@ -0,0 +1,112 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +import { describe, expect, it } from 'vitest'; + +// Read through the `@objectstack/spec/data` entry, not the module: the pin is +// on the PUBLISHED reader `@objectstack/lint` imports. +import { controlledByParentMasterOf, masterDetailRequiredOmitted } from './index'; +import { FieldSchema } from './field.zod'; +import { ObjectSchema } from './object.zod'; +import { defineStack } from '../stack.zod'; + +/** + * [ADR-0055 §1 as amended · maintainer ruling A, record 6687830530] A strict + * `defineStack` reads an omitted `required` on a `controlled_by_parent` + * object's `master_detail` as the parsed `false` (the optional master, as + * ruled) and RECORDS the omission on the field it returns, so `os lint` can + * still report the omitted spelling (#33940). The parsed value, and every + * reading of it, is unchanged. + */ + +const md = (reference: string, extra: Record = {}) => ({ + type: 'master_detail', + label: reference, + reference, + ...extra, +}); + +type BuiltField = { required?: boolean }; +type BuiltStack = { objects: Array<{ name: string; fields: Record }> }; + +/** A strict (default) `defineStack` over a header and one detail; returns the detail's fields. */ +function detailFields( + detailFieldsIn: Record, + sharingModel: string = 'controlled_by_parent', + options?: { strict: boolean }, +): Record { + const stack = defineStack( + { + manifest: { id: 'com.example.cbp-omission', namespace: 'cbpom', version: '0.0.1', type: 'app', name: 'CBP' }, + objects: [ + { name: 'cbpom_header', label: 'Header', fields: { name: { type: 'text', label: 'Name' } } }, + { name: 'cbpom_other', label: 'Other', fields: { name: { type: 'text', label: 'Name' } } }, + { name: 'cbpom_line', label: 'Line', sharingModel, fields: detailFieldsIn }, + ], + } as never, + options, + ) as unknown as BuiltStack; + return stack.objects.find((o) => o.name === 'cbpom_line')!.fields; +} + +describe('masterDetailRequiredOmitted — defineStack records an omitted cbp master required (#33940)', () => { + it('the sole master_detail without required: still the parsed optional master, and recorded as omitted', () => { + const fields = detailFields({ order: md('cbpom_header') }); + // The ruled reading is unchanged: the parse wrote `false`, the optional master. + expect(fields.order!.required).toBe(false); + expect(controlledByParentMasterOf(fields)).toEqual({ field: 'order', optional: true }); + // …and the producer recorded what the author wrote. + expect(masterDetailRequiredOmitted(fields.order)).toBe(true); + }); + + it('records nothing for an explicit required: false (the optional master as written) or required: true', () => { + expect(masterDetailRequiredOmitted(detailFields({ order: md('cbpom_header', { required: false }) }).order)).toBe(false); + expect(masterDetailRequiredOmitted(detailFields({ order: md('cbpom_header', { required: true }) }).order)).toBe(false); + }); + + it('records every master_detail that omits required, beside a required master', () => { + const fields = detailFields({ + order: md('cbpom_header', { required: true }), + other: md('cbpom_other'), + }); + expect(masterDetailRequiredOmitted(fields.order)).toBe(false); + expect(masterDetailRequiredOmitted(fields.other)).toBe(true); + }); + + it('records nothing off controlled_by_parent, and nothing on a field that is not a master_detail', () => { + expect(masterDetailRequiredOmitted(detailFields({ order: md('cbpom_header') }, 'private').order)).toBe(false); + const fields = detailFields({ + order: md('cbpom_header', { required: true }), + ref: { type: 'lookup', label: 'Ref', reference: 'cbpom_other' }, + }); + expect(masterDetailRequiredOmitted(fields.ref)).toBe(false); + }); + + it('a non-strict defineStack records nothing: its field still lacks the key, as written', () => { + const fields = detailFields({ order: md('cbpom_header') }, 'controlled_by_parent', { strict: false }); + expect(fields.order!.required).toBeUndefined(); + expect(masterDetailRequiredOmitted(fields.order)).toBe(false); + }); + + it('is invisible to every data reader, and a copy drops it (the pre-record reading)', () => { + const field = detailFields({ order: md('cbpom_header') }).order!; + expect(masterDetailRequiredOmitted(field)).toBe(true); + expect(Object.keys(field)).not.toContain('masterDetailRequiredOmitted'); + expect(JSON.parse(JSON.stringify(field))).toEqual({ ...field }); + expect(masterDetailRequiredOmitted({ ...field })).toBe(false); + expect(masterDetailRequiredOmitted(JSON.parse(JSON.stringify(field)))).toBe(false); + // The strict field schema still accepts the built field: the record is no key. + expect(FieldSchema.safeParse(field).success).toBe(true); + }); + + it('answers false for anything a producer did not stamp', () => { + for (const value of [undefined, null, 'x', 1, {}, md('x'), md('x', { required: false })]) { + expect(masterDetailRequiredOmitted(value)).toBe(false); + } + // ObjectSchema.safeParse alone is not the producer: it records nothing. + const parsed = ObjectSchema.safeParse({ + name: 'cbpom_line', label: 'Line', sharingModel: 'controlled_by_parent', fields: { order: md('cbpom_header') }, + }); + expect(parsed.success).toBe(true); + expect(masterDetailRequiredOmitted(parsed.data!.fields.order)).toBe(false); + }); +}); diff --git a/packages/spec/src/data/master-detail-required-omission.ts b/packages/spec/src/data/master-detail-required-omission.ts new file mode 100644 index 00000000000..df7881a28e0 --- /dev/null +++ b/packages/spec/src/data/master-detail-required-omission.ts @@ -0,0 +1,118 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [ADR-0055 §1 as amended · maintainer ruling A on the optional master] The + * spec-declared record that an AUTHOR omitted `required` on a `master_detail` + * field of a `controlled_by_parent` object — kept by `defineStack` on the + * field it returns, read by `@objectstack/lint`. + * + * ## The gap this record closes + * + * `required` is `z.boolean().default(false)` (`field.zod.ts`), so the parse + * writes `false` into an omitted `required` before anything downstream reads + * the field. Under `controlled_by_parent` that makes the omission + * indistinguishable from the explicit `required: false` that ruling A admits + * as the OPTIONAL master. The ruling keeps exactly that reading at the parse + * (an omitted `required` is admitted as the optional master; nothing refuses + * the stack), and pairs it with a lint promise: `os lint`'s + * `relationship/master-detail-required` reports the omitted spelling at + * `error`, so the author writes `true` or `false`. A user's + * `objectstack.config.ts` calls `defineStack` itself, so on the default + * (strict) door every author-time verb receives the parsed stack, where the + * omission is already gone — and the promised finding never fired. + * + * So the one party that sees both the authored field and the parsed one — + * `defineStack`'s strict path, which holds the normalized input it parses — + * RECORDS the omission on the parsed field it returns, and + * {@link masterDetailRequiredOmitted} is the one reader. Nothing about the + * parsed value changes: `required` stays `false`, so every runtime reader + * (the security plugin's master resolution, `controlledByParentMasterOf`) + * reads the optional master exactly as the ruling has it. The record only + * tells the linter what the author wrote. + * + * ## Why a symbol on the field, not a key in it + * + * The record must be invisible to everything that reads the field as DATA: + * the strict `FieldSchema` (which refuses an undeclared key), the compiled + * `dist/objectstack.json`, and every `Object.keys` walk. It lives on the + * FIELD rather than on the stack (where `stack-provenance.ts` keeps the + * producer's mark) because the CLI's config load spreads the default export + * into a new top-level object, which drops a stack-level symbol, while the + * `objects` array — and each field definition in it — reaches the linter by + * reference. `Symbol.for` (the global registry) rather than a module-local + * symbol, so a CLI that loads its own copy of this package reads the key the + * config's copy wrote. Precedent: `stack-provenance.ts`, + * `filter-subtree-provenance.ts`. + * + * ## The fail direction is the pre-record behaviour + * + * Non-enumerable, non-writable and non-configurable, so a later actor cannot + * flip it; every copy (`{ ...spread }`, `JSON.stringify`, structured clone, a + * copy-on-write conversion) DROPS it. A field that lost the record reads as + * an explicit `required: false` — the optional master — which is exactly what + * every door read before the record existed: no stack is refused because of + * it, and no finding appears that the author did not earn. + * + * Only the reader is published (`@objectstack/spec/data`); stamping belongs + * to `defineStack` alone, so no third party can attest an authored spelling + * it never saw. + */ + +/** The symbol key the record lives under on a parsed `master_detail` field. */ +const MASTER_DETAIL_REQUIRED_OMITTED: symbol = Symbol.for('objectstack.field.masterDetailRequiredOmitted'); + +function isRecord(value: unknown): value is Record { + return value !== null && typeof value === 'object' && !Array.isArray(value); +} + +/** + * Does this parsed field carry `defineStack`'s record that its author omitted + * `required` (see the module header)? `true` only for a field the producer + * stamped: a `master_detail` field of a `controlled_by_parent` object whose + * authored definition wrote no `required`. Any other value — an unstamped + * field, a copy, a non-object — answers `false`, and reads as written. + */ +export function masterDetailRequiredOmitted(field: unknown): boolean { + if (field === null || typeof field !== 'object') return false; + return (field as Record)[MASTER_DETAIL_REQUIRED_OMITTED] === true; +} + +/** + * Stamp the record on every parsed `master_detail` field of a + * `controlled_by_parent` object whose AUTHORED definition omits `required` + * (the key absent, or `undefined`). `authoredObjects` is the normalized input + * the parse read, `parsedObjects` what it returned; the parse keeps the + * array's order and length, and each pair is matched by object `name` too, so + * a pair that does not line up is skipped (no record, the pre-record reading). + * + * The sharing model and the field type are read on the PARSED side: they are + * what the stack carries. A non-extensible field cannot take the property and + * is left unstamped. + * + * Internal to `defineStack`: NOT re-exported from the package entry. + */ +export function recordMasterDetailRequiredOmissions(authoredObjects: unknown, parsedObjects: unknown): void { + if (!Array.isArray(authoredObjects) || !Array.isArray(parsedObjects)) return; + for (let i = 0; i < parsedObjects.length; i++) { + const parsed = parsedObjects[i]; + const authored = authoredObjects[i]; + if (!isRecord(parsed) || !isRecord(authored)) continue; + if (parsed.sharingModel !== 'controlled_by_parent') continue; + if (authored.name !== parsed.name) continue; + const parsedFields = parsed.fields; + const authoredFields = authored.fields; + if (!isRecord(parsedFields) || !isRecord(authoredFields)) continue; + for (const [key, field] of Object.entries(parsedFields)) { + if (!isRecord(field) || field.type !== 'master_detail') continue; + const written = authoredFields[key]; + if (!isRecord(written) || written.required !== undefined) continue; + if (!Object.isExtensible(field) || masterDetailRequiredOmitted(field)) continue; + Object.defineProperty(field, MASTER_DETAIL_REQUIRED_OMITTED, { + value: true, + enumerable: false, + writable: false, + configurable: false, + }); + } + } +} diff --git a/packages/spec/src/stack.zod.ts b/packages/spec/src/stack.zod.ts index c1eebd00341..222a98e9c4f 100644 --- a/packages/spec/src/stack.zod.ts +++ b/packages/spec/src/stack.zod.ts @@ -40,6 +40,7 @@ import { lintUnknownAuthoringKeys, lintUnknownStackKeys } from './kernel/metadat // Data Protocol import { ObjectSchema, ObjectExtensionSchema } from './data/object.zod'; import { controlledByParentMasterRefusal } from './data/controlled-by-parent-master'; +import { recordMasterDetailRequiredOmissions } from './data/master-detail-required-omission'; import { fieldGroupBodyTableRefusal, type NamedInboundRelationship } from './data/related-list-body-table'; import { externalBindingDatasourceRefusal } from './data/external-binding-datasource'; import { InlineGridColumnSchema, summaryFunctionReadsField } from './data/field.zod'; @@ -7066,6 +7067,19 @@ function buildDefinedStack( // `aiStudio`/`aiSeat` spellings were removed in #3308. const data = result.data; + // [ADR-0055 §1 as amended · ruling A] The parse just wrote `false` into every + // omitted `required`, so on a `controlled_by_parent` object an omitted one + // now reads as the optional master — the reading the ruling keeps. The + // omission itself is still visible here, in the input the parse read, and + // nowhere downstream: record it on the parsed field, where `os lint`'s + // `relationship/master-detail-required` reads it to report the omitted + // spelling the ruling promises it reports. Nothing parsed changes, and + // nothing is refused (`master-detail-required-omission.ts`). + recordMasterDetailRequiredOmissions( + (normalized as Record).objects, + (data as Record).objects, + ); + const capErrors = validateKnownCapabilities(data); if (capErrors.length > 0) { const header = `defineStack capability validation failed (${capErrors.length} issue${capErrors.length === 1 ? '' : 's'}):`;