Skip to content

Commit 89fed2c

Browse files
committed
feat(spec): app-scoped record pages (PageSchema.apps) refused per app at the three default-record-page doors
Claude-Session: https://claude.ai/code/session_01ADHkmSGMLCTCPxZfV3GqHr Co-authored-by: Claude <noreply@anthropic.com>
1 parent 2f7977c commit 89fed2c

6 files changed

Lines changed: 368 additions & 130 deletions

File tree

‎packages/metadata-protocol/src/protocol-save-door-refusals.ts‎

Lines changed: 79 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,21 @@ export async function defaultRecordPageSaveRefusal(
204204
verb: 'saved' | 'published' | 'restored' = 'saved',
205205
): Promise<(Error & { code: 'DEFAULT_RECORD_PAGE_CONFLICT'; status: 422 }) | undefined> {
206206
if (type !== 'page') return undefined;
207+
// Every slot the page holds: its object's default (no app), and the record
208+
// page of its object inside each app its `apps` names (ruling A on
209+
// objectstack#28979) — each judged by the one reading, `defaultRecordPageObject`.
210+
const slots: Array<{ object: string; app?: string }> = [];
207211
const object = defaultRecordPageObject(item);
208-
if (object === undefined) return undefined;
212+
if (object !== undefined) slots.push({ object });
213+
const apps = (item as { apps?: unknown } | null | undefined)?.apps;
214+
if (Array.isArray(apps)) {
215+
for (const app of new Set(apps.filter((a): a is string => typeof a === 'string'))) {
216+
const scoped = defaultRecordPageObject(item, app);
217+
if (scoped !== undefined) slots.push({ object: scoped, app });
218+
}
219+
}
220+
if (slots.length === 0) return undefined;
209221

210-
// `shipped`: a page a package ships through the registry, which no save
211-
// can edit; a stored row (bound to a package or not) is editable here.
212-
type Holder = { page: string; packageId?: string; shipped: boolean };
213-
const holders: Holder[] = [];
214222
let records: any[] = [];
215223
try {
216224
// [ADR-0131 D6] A save lands environment-wide, so the pages it meets
@@ -221,52 +229,75 @@ export async function defaultRecordPageSaveRefusal(
221229
}
222230
const stored = this.storedOverlayEntries({ type }, records);
223231
const storedNames = new Set(stored.map((entry) => entry.name));
224-
for (const entry of stored) {
225-
if (entry.name === saveName || defaultRecordPageObject(entry.data) !== object) continue;
226-
holders.push(entry.packageId
227-
? { page: entry.name, packageId: entry.packageId, shipped: false }
228-
: { page: entry.name, shipped: false });
229-
}
230232
const registry = this.engine.registry;
231-
if (typeof registry?.defaultRecordPageHoldersOf === 'function') {
232-
const registered = registry.defaultRecordPageHoldersOf(object) as Array<
233-
{ kind: 'package'; packageId: string; page: string } | { kind: 'environment'; page: string }
234-
>;
235-
for (const holder of registered) {
236-
if (holder.page === saveName || storedNames.has(holder.page)) continue;
237-
holders.push(holder.kind === 'package'
238-
? { page: holder.page, packageId: holder.packageId, shipped: true }
239-
: { page: holder.page, shipped: false });
233+
234+
// `shipped`: a page a package ships through the registry, which no save
235+
// can edit; a stored row (bound to a package or not) is editable here.
236+
type Holder = { page: string; packageId?: string; shipped: boolean };
237+
for (const { object, app } of slots) {
238+
const holders: Holder[] = [];
239+
for (const entry of stored) {
240+
if (entry.name === saveName || defaultRecordPageObject(entry.data, app) !== object) continue;
241+
holders.push(entry.packageId
242+
? { page: entry.name, packageId: entry.packageId, shipped: false }
243+
: { page: entry.name, shipped: false });
240244
}
245+
if (typeof registry?.defaultRecordPageHoldersOf === 'function') {
246+
const registered = (app === undefined
247+
? registry.defaultRecordPageHoldersOf(object)
248+
: registry.defaultRecordPageHoldersOf(object, app)) as Array<
249+
{ kind: 'package'; packageId: string; page: string } | { kind: 'environment'; page: string }
250+
>;
251+
for (const holder of registered) {
252+
if (holder.page === saveName || storedNames.has(holder.page)) continue;
253+
holders.push(holder.kind === 'package'
254+
? { page: holder.page, packageId: holder.packageId, shipped: true }
255+
: { page: holder.page, shipped: false });
256+
}
257+
}
258+
if (holders.length === 0) continue;
259+
260+
const seen = new Set<string>();
261+
const unique = holders.filter((h) => {
262+
const key = `${h.packageId ?? ''}|${h.page}`;
263+
if (seen.has(key)) return false;
264+
seen.add(key);
265+
return true;
266+
});
267+
const describe = (h: Holder) =>
268+
h.packageId ? `page '${h.page}' of package '${h.packageId}'` : `page '${h.page}', authored in this system`;
269+
const named = unique.map(describe);
270+
const others = named.length === 1 ? `${named[0]} already is` : `${named.join(' and ')} already are`;
271+
const message = app === undefined
272+
? `Default record page conflict: page '${saveName}' cannot be ${verb} as the default record page of object `
273+
+ `'${object}': ${others}. `
274+
+ (unique.some((h) => h.shipped)
275+
? `Save '${saveName}' with \`isDefault: false\`: a page a package ships stays the default unless the `
276+
+ `deployment is composed without that package's page.`
277+
: `Save '${saveName}' with \`isDefault: false\`, or first save ${unique.map((h) => `'${h.page}'`).join(' and ')} `
278+
+ `with \`isDefault: false\` to make '${saveName}' the default instead.`)
279+
+ ` At most one \`isDefault: true\` record page may exist for an object `
280+
+ `in a deployment: the record view opens that page with no tie-break.`
281+
: `Default record page conflict: page '${saveName}' cannot be ${verb} as the record page of object `
282+
+ `'${object}' in app '${app}': ${others}. `
283+
+ (unique.some((h) => h.shipped)
284+
? `Save '${saveName}' without '${app}' in \`apps\`: a page a package ships stays that app's page unless the `
285+
+ `deployment is composed without that package's page.`
286+
: `Save '${saveName}' without '${app}' in \`apps\`, or first save ${unique.map((h) => `'${h.page}'`).join(' and ')} `
287+
+ `without it to make '${saveName}' that app's page instead.`)
288+
+ ` At most one record page may name an app in \`apps\` for an object: that app's record view `
289+
+ `opens it with no tie-break.`;
290+
const err = new Error(message) as Error & {
291+
code: 'DEFAULT_RECORD_PAGE_CONFLICT'; status: 422; object: string; app?: string; holders: readonly Holder[];
292+
};
293+
err.code = 'DEFAULT_RECORD_PAGE_CONFLICT';
294+
err.status = 422;
295+
err.object = object;
296+
if (app !== undefined) err.app = app;
297+
err.holders = unique;
298+
return err;
241299
}
242-
if (holders.length === 0) return undefined;
243-
244-
const seen = new Set<string>();
245-
const unique = holders.filter((h) => {
246-
const key = `${h.packageId ?? ''}|${h.page}`;
247-
if (seen.has(key)) return false;
248-
seen.add(key);
249-
return true;
250-
});
251-
const describe = (h: Holder) =>
252-
h.packageId ? `page '${h.page}' of package '${h.packageId}'` : `page '${h.page}', authored in this system`;
253-
const named = unique.map(describe);
254-
const others = named.length === 1 ? `${named[0]} already is` : `${named.join(' and ')} already are`;
255-
const remedy = unique.some((h) => h.shipped)
256-
? `Save '${saveName}' with \`isDefault: false\`: a page a package ships stays the default unless the `
257-
+ `deployment is composed without that package's page.`
258-
: `Save '${saveName}' with \`isDefault: false\`, or first save ${unique.map((h) => `'${h.page}'`).join(' and ')} `
259-
+ `with \`isDefault: false\` to make '${saveName}' the default instead.`;
260-
const err = new Error(
261-
`Default record page conflict: page '${saveName}' cannot be ${verb} as the default record page of object `
262-
+ `'${object}': ${others}. ${remedy} At most one \`isDefault: true\` record page may exist for an object `
263-
+ `in a deployment: the record view opens that page with no tie-break.`,
264-
) as Error & { code: 'DEFAULT_RECORD_PAGE_CONFLICT'; status: 422; object: string; holders: readonly Holder[] };
265-
err.code = 'DEFAULT_RECORD_PAGE_CONFLICT';
266-
err.status = 422;
267-
err.object = object;
268-
err.holders = unique;
269-
return err;
300+
return undefined;
270301
}
271302

272303
/**

‎packages/objectql/src/default-record-page.ts‎

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,18 @@
6868
* unrefused when the deployment restarts. A hot install of that package is
6969
* refused (the stored page is already in the registry then).
7070
* - ⛔ A downgrade. `OS_METADATA_COLLISION=warn` softens the ADR-0048 Phase 1
71-
* namespace gate only.
71+
* namespace gate only. *
72+
* ## The app-scoped record page
73+
*
74+
* A record page may name the apps whose routes open it for its object
75+
* (`PageSchema.apps`; maintainer ruling A on objectstack#28979, record
76+
* 6573557633). The rule above holds one level down, per app: at most one
77+
* record page per object per app, refused here by the same holders, claims
78+
* and overlay rules, keyed by the SLOT ({@link RecordPageSlot}) instead of by
79+
* the object alone. The deployment's default is the slot with no app; it and
80+
* an app's slot never meet. "The record page of object X inside app A" is
81+
* `@objectstack/spec/ui`'s `defaultRecordPageObject(page, A)`, the reading
82+
* `defineStack` and the metadata save door ask too.
7283
*/
7384

7485
import { defaultRecordPageObject, misplacedDefaultPageRefusal } from '@objectstack/spec/ui';
@@ -78,10 +89,48 @@ export type DefaultRecordPageHolder =
7889
| { readonly kind: 'package'; readonly packageId: string; readonly page: string }
7990
| { readonly kind: 'environment'; readonly page: string };
8091

81-
/** One default record page a manifest declares. */
82-
export interface DeclaredDefaultRecordPage {
83-
/** The object the page is the default record page of. */
92+
/**
93+
* One record-page slot: an object's deployment default (`app` absent), or the
94+
* record page an app's routes open for an object (`app` set).
95+
*/
96+
export interface RecordPageSlot {
97+
/** The object whose record view the slot chooses the page of. */
8498
readonly object: string;
99+
/** The app the slot is scoped to; absent for the deployment's default. */
100+
readonly app?: string;
101+
}
102+
103+
/** The registry's key for a slot — the object alone for the default, so the default's keys are unchanged. */
104+
export function recordPageSlotKey(slot: RecordPageSlot): string {
105+
return slot.app === undefined ? slot.object : JSON.stringify([slot.object, slot.app]);
106+
}
107+
108+
/**
109+
* Every slot `item` holds: its object's default when it is one, and one slot
110+
* per distinct string entry of its `apps` that `defaultRecordPageObject` places
111+
* (a record page bound to an object). Reads an unparsed body.
112+
*/
113+
export function recordPageSlotsOf(item: unknown): RecordPageSlot[] {
114+
const slots: RecordPageSlot[] = [];
115+
const object = defaultRecordPageObject(item);
116+
if (object !== undefined) slots.push({ object });
117+
const apps = (item as { apps?: unknown } | null | undefined)?.apps;
118+
if (Array.isArray(apps)) {
119+
for (const app of new Set(apps.filter((a): a is string => typeof a === 'string'))) {
120+
const scoped = defaultRecordPageObject(item, app);
121+
if (scoped !== undefined) slots.push({ object: scoped, app });
122+
}
123+
}
124+
return slots;
125+
}
126+
127+
/** Whether `item` holds `slot`. */
128+
export function holdsRecordPageSlot(item: unknown, slot: RecordPageSlot): boolean {
129+
return defaultRecordPageObject(item, slot.app) === slot.object;
130+
}
131+
132+
/** One record-page slot a manifest declares a page for. */
133+
export interface DeclaredDefaultRecordPage extends RecordPageSlot {
85134
/** The page's name. */
86135
readonly page: string;
87136
}
@@ -100,7 +149,8 @@ function pageName(item: unknown): string | undefined {
100149
}
101150

102151
/**
103-
* Every default record page a manifest declares, read from the SAME sources
152+
* Every record-page slot a manifest declares a page for — each default record
153+
* page, and each app a scoped record page names — read from the SAME sources
104154
* the engine's registration seams read: the manifest's own `pages` and each
105155
* nested `plugins[]` entry's (a nested plugin contributes under its parent
106156
* package — `ObjectQL.registerPlugin`), one level deep, arrays only. In
@@ -113,9 +163,9 @@ export function declaredDefaultRecordPages(manifest: unknown): DeclaredDefaultRe
113163
const pages = (source as { pages?: unknown }).pages;
114164
if (!Array.isArray(pages)) return;
115165
for (const item of pages) {
116-
const object = defaultRecordPageObject(item);
117166
const page = pageName(item);
118-
if (object !== undefined && page !== undefined) out.push({ object, page });
167+
if (page === undefined) continue;
168+
for (const slot of recordPageSlotsOf(item)) out.push({ ...slot, page });
119169
}
120170
};
121171
read(manifest);

0 commit comments

Comments
 (0)