Skip to content

Commit 5915671

Browse files
committed
feat(objectql)!: by-name readers of presentation and AI types resolve (package, name)
getApp, MetadataFacade get/getEntry/exists and the protocol's package-less by-name read now answer through the one package-scoped resolver for app, page, dashboard, report, book, analytics_cube, tool, skill and agent. A name the resolver refuses throws a coded 409 RESOURCE_CONFLICT naming every candidate; one holder and zero holders answer as before. Claude-Session: https://claude.ai/code/session_01ADHkmTH1oAwgDMigQ9pj37 Co-authored-by: Claude <noreply@anthropic.com>
1 parent afb40c6 commit 5915671

6 files changed

Lines changed: 190 additions & 42 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'@objectstack/objectql': patch
3+
'@objectstack/metadata-protocol': patch
4+
---
5+
6+
feat(objectql)!: the by-name reads of apps, pages, dashboards, reports, books, analytics cubes, tools, skills and agents consult `(package, name)` (ADR-0150 D3)
7+
8+
Clause-②: no (narrowing)
9+
Metadata-protocol: none
10+
11+
<!-- adr-0087: not-required (no-migration-prescription) no metadata shape, schema, stored row or registered node type changes: the change is which item a bare name resolves to at read time, the stored metadata is read exactly as before, so there is nothing for `objectstack migrate meta` to rewrite -->
12+
13+
`SchemaRegistry.getApp`, `MetadataFacade.get` / `getEntry` / `exists` and the protocol's by-name read that names no package (`GET /api/v1/meta/TYPE/NAME`, and MCP `agent_prompt`, which reads it) now answer these nine types through the one package-scoped resolver. A package caller gets its own item, else the one holder in its declared dependencies (plus `sys`). A caller with no package gets the environment's own item, else the one package holder. A name with one holder, and a name with none, answer exactly as before.
14+
15+
**BREAKING** for three bare-name reads that were served a guess and are now refused with `409 RESOURCE_CONFLICT`, the message naming every holder:
16+
17+
1. A caller with no package reads a name that two packages both ship (for app, page, dashboard, report, book, analytics cube or tool; skills and agents already allow one holder only). It was served whichever package registered first.
18+
2. A package caller reads a name that only the environment's own item holds.
19+
3. A package caller reads a name whose one holder is a package it does not declare as a dependency.
20+
21+
The fix is one line. For 1 and 3, declare the holding package in the reading package's `dependencies`, or name the package the read means with `?package=`. For 2, ship the item in the reading package or in a package it depends on. None of the four example apps in this repository hits any of the three.

‎packages/metadata-protocol/src/protocol-meta-item-reads.ts‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ export async function readMetaItem(host: Pick<ProtocolHost, 'artifactLockLayerAt
9292
const wantedMember = bundleMemberDiscriminator(request.type, member);
9393
const isWantedMember = (candidate: unknown): boolean =>
9494
wantedMember === undefined || itemDiscriminator(request.type, candidate) === wantedMember;
95+
// [ADR-0150 D3, #31680 child E8] A read that names no package is a caller
96+
// with no package. For a type whose by-name readers consult
97+
// `(package, name)` (the registry's `readsByPackage`), the registry's
98+
// answer is the ONE package-scoped resolver's (`resolveItem`): the
99+
// environment's own item, else the unique package holder. A name two
100+
// packages hold is refused HERE, before any layer below is read, naming
101+
// both (a thrown `409 RESOURCE_CONFLICT`); it served the first registered.
102+
// Step 3 serves this answer in place of `getItem`'s, and nothing else
103+
// below changes. A read that names a package (`?package=`) keeps
104+
// `getItem`'s prefer-local read: that address is the exact qualifier of
105+
// child G1 (ADR-0150 D5), not a caller.
106+
const registry = host.engine?.registry;
107+
const packagelessScopedRead: { item: unknown } | undefined =
108+
!request.packageId && typeof registry?.readsByPackage === 'function' && registry.readsByPackage(request.type)
109+
? { item: registry.resolveItem(request.type, request.name, null, member) }
110+
: undefined;
95111
let item: unknown;
96112
// [ADR-0131 D6] Environment → code: the environment's stored row,
97113
// else the code packages' definition. No organization's row is read —
@@ -566,10 +582,17 @@ export async function readMetaItem(host: Pick<ProtocolHost, 'artifactLockLayerAt
566582
if (artifact !== undefined && isWantedMember(artifact)) item = artifact;
567583
}
568584
if (item === undefined) {
569-
item = host.engine.registry.getItem(request.type, request.name, request.packageId, member);
570-
if (item === undefined) {
571-
const alt = PLURAL_TO_SINGULAR[request.type] ?? SINGULAR_TO_PLURAL[request.type];
572-
if (alt) item = host.engine.registry.getItem(alt, request.name, request.packageId, member);
585+
// [ADR-0150 D3, #31680] The package-scoped resolver's answer, read
586+
// once above, for a caller with no package of a type it covers; no
587+
// first-composite read and no plural-spelling retry beside it.
588+
if (packagelessScopedRead) {
589+
item = packagelessScopedRead.item;
590+
} else {
591+
item = host.engine.registry.getItem(request.type, request.name, request.packageId, member);
592+
if (item === undefined) {
593+
const alt = PLURAL_TO_SINGULAR[request.type] ?? SINGULAR_TO_PLURAL[request.type];
594+
if (alt) item = host.engine.registry.getItem(alt, request.name, request.packageId, member);
595+
}
573596
}
574597
// [#22877] A registry that does not narrow by member (a host
575598
// double, an older registry) can answer another member of the

‎packages/objectql/src/index.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ export type {
140140
PackageScopedRefusalReason,
141141
} from './package-scoped-resolver.js';
142142

143+
// [#31680, ADR-0150 D3] Child E8: the by-name readers of the presentation and
144+
// AI types consult `(package, name)` through the resolver above — the types
145+
// wired, the read every reader shares, and the coded refusal it throws.
146+
export {
147+
PACKAGE_SCOPED_READ_TYPES,
148+
isPackageScopedReadType,
149+
readPackageScopedItem,
150+
PackageScopedNameRefusedError,
151+
} from './package-scoped-read.js';
152+
export type { PackageScopedReadType } from './package-scoped-read.js';
153+
143154
// [#31673, ADR-0150 D4] The action engine's `(package, objectName, name)` key:
144155
// the declaration half — which package's action a bare name on an object means
145156
// to a caller, resolved by the resolver above over the items that own that

‎packages/objectql/src/metadata-facade.ts‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
canonicalMetadataServiceType,
66
} from '@objectstack/core';
77
import { SchemaRegistry } from './registry.js';
8+
import { PackageScopedNameRefusedError } from './package-scoped-read.js';
89

910
/**
1011
* Is this (already canonical) type the object metadata type?
@@ -225,25 +226,36 @@ export class MetadataFacade {
225226
* path can only corrupt — so there is none, and no replacement envelope key
226227
* either (any key chosen would just collide with the next authorable field).
227228
*
228-
* `currentPackageId` (ADR-0048) opts into package-scoped resolution: when two
229-
* installed packages ship an item of the same `type`/`name`, the registry
230-
* prefers the one owned by `currentPackageId` (composite key
231-
* `${packageId}:${name}`) before falling back to first-match. Omit it for the
232-
* legacy context-free lookup.
229+
* `currentPackageId` (ADR-0048) is the caller's package. [ADR-0150 D3,
230+
* #31680 child E8] For a type whose by-name readers consult
231+
* `(package, name)` (`PACKAGE_SCOPED_READ_TYPES`) the read is the one
232+
* package-scoped resolver's (`SchemaRegistry.resolveItem`): the caller's own
233+
* item, else the unique holder in its declared closure; with no package, the
234+
* environment's own item, else the unique package holder; a name that is
235+
* ambiguous, or held only where the caller may not reach, is refused naming
236+
* every candidate. Every other type keeps `getItem`'s prefer-local read.
237+
*
238+
* @throws {PackageScopedNameRefusedError} when the resolver refuses.
233239
*/
234240
async get(type: string, name: string, currentPackageId?: string): Promise<any> {
235241
// [#7378 row 2] Read the store `register` wrote: the canonical type.
236-
return this.registry.getItem(canonicalMetadataServiceType(type), name, currentPackageId);
242+
return this.registry.resolveItem(canonicalMetadataServiceType(type), name, currentPackageId);
237243
}
238244

239245
/**
240246
* Get the raw stored entry, synchronously and without package-scoped
241247
* resolution. ([#7519] Historically documented as "with metadata wrapper" —
242248
* there is no wrapper; {@link get} returns the same stored document. This
243249
* member survives as the sync, scope-free variant.)
250+
*
251+
* [ADR-0150 D3, #31680 child E8] Scope-free is a caller with no package: a
252+
* type of `PACKAGE_SCOPED_READ_TYPES` resolves environment-wide through the
253+
* one package-scoped resolver, and a name two packages hold is refused.
254+
*
255+
* @throws {PackageScopedNameRefusedError} when the resolver refuses.
244256
*/
245257
getEntry(type: string, name: string): any {
246-
return this.registry.getItem(canonicalMetadataServiceType(type), name);
258+
return this.registry.resolveItem(canonicalMetadataServiceType(type), name);
247259
}
248260

249261
/**
@@ -285,9 +297,19 @@ export class MetadataFacade {
285297

286298
/**
287299
* Check if a metadata item exists
300+
*
301+
* [ADR-0150 D3, #31680 child E8] Read as {@link getEntry} reads it. A name
302+
* the package-scoped resolver refuses exists: its holders are a fact, and
303+
* no item is served for it here.
288304
*/
289305
async exists(type: string, name: string): Promise<boolean> {
290-
const item = this.registry.getItem(canonicalMetadataServiceType(type), name);
306+
let item: unknown;
307+
try {
308+
item = this.registry.resolveItem(canonicalMetadataServiceType(type), name);
309+
} catch (err) {
310+
if (err instanceof PackageScopedNameRefusedError) return true;
311+
throw err;
312+
}
291313
return item !== undefined && item !== null;
292314
}
293315

‎packages/objectql/src/package-scoped-read.ts‎

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,60 @@
33
/**
44
* # The by-name readers that consult `(package, name)` (ADR-0150 D3/D4)
55
*
6-
* Child R (#31575) built ONE resolver for a bare name and wired no reader to
7-
* it. Each child E of epic #31527 wires the readers of its types; this module
8-
* holds what those readers share beside the resolver itself: which types are
9-
* wired, and the refusal a reader raises.
6+
* Child R (#31575, made a lean core by R2 #31716) built ONE resolver for a
7+
* bare name, {@link resolvePackageScopedName}, and wired no reader to it.
8+
* Each child E of epic #31527 wires the readers of its types, and every one of
9+
* them goes through {@link readPackageScopedItem} here — the resolver's answer
10+
* turned into what a by-name reader returns — so no reader re-states the rule.
1011
*
1112
* ## The types wired so far
1213
*
1314
* {@link PACKAGE_SCOPED_READ_TYPES}: the presentation and AI types of child E8
1415
* (#31680) — app, page, dashboard, report, book, analytics cube, tool, skill
15-
* and agent. A later child adds its own types to the list in its own change.
16+
* and agent. A later child adds its own types to the list in its own change;
17+
* until then a type's by-name read stays `SchemaRegistry.getItem`'s, exactly
18+
* as before (`SchemaRegistry.resolveItem`).
1619
*
1720
* ## What a reader answers
1821
*
19-
* - **resolved** → the item, from the step that reached it;
22+
* - **resolved** → the item, from the step that reached it (the caller
23+
* package's own, the unique holder in its declared closure, or for a
24+
* caller with no package the environment's own item, else the unique
25+
* package holder);
2026
* - **not-found** → `undefined`, as before: zero holders is never a refusal;
2127
* - **refused** → {@link PackageScopedNameRefusedError}, thrown: the
2228
* resolver's refusal, its sentence unchanged, naming every candidate.
2329
* ⛔ Never `undefined`: a refusal read as a miss would tell the caller the
2430
* name means nothing, when it means more than one thing (red line 2).
2531
*
26-
* Lean on purpose: it imports nothing, so the registry, the engine and
27-
* `MetadataFacade` — all inside the `./core` entry's closure (ADR-0076 D2) —
28-
* may read it.
32+
* ## What this changes against `getItem` (declared on #31680)
33+
*
34+
* `getItem` answers the bare slot, then the caller's composite slot, then the
35+
* FIRST composite slot of any package. Through the resolver:
36+
*
37+
* 1. a bare name two packages hold, read by a caller with no package, is
38+
* refused naming both (it served the first registered);
39+
* 2. a package caller's bare name whose only holder is the environment's own
40+
* item is refused, naming the environment (ADR-0150 D3 (a));
41+
* 3. a package caller's bare name whose one package holder is outside its
42+
* declared dependency closure (plus `sys`) is refused, naming the holder
43+
* (D3 step 3).
44+
*
45+
* Every other answer is unchanged: one holder is served to every caller that
46+
* may reach it, and an overlay of the one holder is still served first.
47+
*
48+
* Inside the lean `./core` entry's closure (ADR-0076 D2): it reads the
49+
* resolver, which since R2 reads only `@objectstack/core` and
50+
* `@objectstack/metadata-core`.
2951
*/
3052

53+
import type { PackageScopedRefusal } from '@objectstack/core';
54+
import {
55+
resolvePackageScopedName,
56+
type PackageScopedQuery,
57+
type PackageScopedRegistry,
58+
} from './package-scoped-resolver.js';
59+
3160
/**
3261
* The types whose by-name readers consult `(package, name)` today — child E8's
3362
* nine (#31680), as the registry spells them. Each later child of epic #31527
@@ -53,21 +82,6 @@ export function isPackageScopedReadType(type: unknown): type is PackageScopedRea
5382
return typeof type === 'string' && (PACKAGE_SCOPED_READ_TYPES as readonly string[]).includes(type);
5483
}
5584

56-
/**
57-
* The resolver's refusal as a reader carries it: the reason, the item, the
58-
* caller's package, every candidate, and the resolver's one sentence
59-
* (`PackageScopedRefusal`, described here by shape so this module stays lean).
60-
*/
61-
export interface PackageScopedReadRefusal {
62-
readonly status: 'refused';
63-
readonly reason: string;
64-
readonly type: string;
65-
readonly name: string;
66-
readonly callerPackageId: string | null;
67-
readonly candidates: readonly unknown[];
68-
readonly message: string;
69-
}
70-
7185
/**
7286
* A by-name read the package-scoped resolver refused (ADR-0150 D3): the name
7387
* means more than one item here, or only items the caller may not reach. The
@@ -83,11 +97,25 @@ export class PackageScopedNameRefusedError extends Error {
8397
/** The same number under ADR-0112 D5's spelling — what a consumer holding the THROWN error reads. */
8498
readonly httpStatus = 409;
8599
/** The resolver's refusal: the reason, the caller's package and every candidate. */
86-
readonly refusal: PackageScopedReadRefusal;
100+
readonly refusal: PackageScopedRefusal;
87101

88-
constructor(refusal: PackageScopedReadRefusal) {
102+
constructor(refusal: PackageScopedRefusal) {
89103
super(refusal.message);
90104
this.name = 'PackageScopedNameRefusedError';
91105
this.refusal = refusal;
92106
}
93107
}
108+
109+
/**
110+
* Read `(type, name)` for a caller through {@link resolvePackageScopedName}:
111+
* the item, `undefined` when nothing holds the name, or a thrown
112+
* {@link PackageScopedNameRefusedError} naming every candidate.
113+
*
114+
* @throws {PackageScopedNameRefusedError} when the resolver refuses.
115+
*/
116+
export function readPackageScopedItem<T>(registry: PackageScopedRegistry, query: PackageScopedQuery): T | undefined {
117+
const resolution = resolvePackageScopedName<T>(registry, query);
118+
if (resolution.status === 'resolved') return resolution.item;
119+
if (resolution.status === 'refused') throw new PackageScopedNameRefusedError(resolution);
120+
return undefined;
121+
}

‎packages/objectql/src/registry.ts‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ import {
9999
type BareNameHolder,
100100
type BareNameOneHolderType,
101101
} from './bare-name-one-holder.js';
102+
// [ADR-0150 D3, #31680 child E8] The by-name readers that consult (package, name):
103+
// `resolveItem` hands the types they cover to the one package-scoped resolver.
104+
import { isPackageScopedReadType, readPackageScopedItem } from './package-scoped-read.js';
102105
// [ADR-0150 D2, child N] Inside one namespace, a `(type, name)` of the twelve
103106
// uncovered bucket 2/3 types has one package holder — the one predicate the
104107
// package door and `os validate` both ask (module doc).
@@ -8482,6 +8485,37 @@ export class SchemaRegistry {
84828485
return unbound[0]?.item as T | undefined;
84838486
}
84848487

8488+
/**
8489+
* [ADR-0150 D3, #31680 child E8] Does a by-name read of `type` consult
8490+
* `(package, name)` — is it one of `PACKAGE_SCOPED_READ_TYPES`
8491+
* (`package-scoped-read.ts`)? Asked by a reader that picks its path before
8492+
* it reads, such as the protocol's by-name read.
8493+
*/
8494+
readsByPackage(type: string): boolean {
8495+
return isPackageScopedReadType(type);
8496+
}
8497+
8498+
/**
8499+
* [ADR-0150 D3, #31680 child E8] The by-name read for a caller. A type of
8500+
* `PACKAGE_SCOPED_READ_TYPES` resolves through the ONE package-scoped
8501+
* resolver (`readPackageScopedItem` over `resolvePackageScopedName`): the
8502+
* caller's own item, else the unique holder in its declared closure; for a
8503+
* caller with no package the environment's own item, else the unique package
8504+
* holder. Every other type is {@link getItem}'s, unchanged, until its own
8505+
* child of epic #31527 wires it.
8506+
*
8507+
* @param callerPackageId - The package the caller belongs to; omitted or
8508+
* `null` is a caller with no package (the whole environment).
8509+
* @returns the item, or `undefined` when nothing holds the name.
8510+
* @throws {PackageScopedNameRefusedError} when the resolver refuses — never
8511+
* `undefined` for a name that means more than one item, or only items the
8512+
* caller may not reach.
8513+
*/
8514+
resolveItem<T>(type: string, name: string, callerPackageId?: string | null, member?: string): T | undefined {
8515+
if (!isPackageScopedReadType(type)) return this.getItem<T>(type, name, callerPackageId ?? undefined, member);
8516+
return readPackageScopedItem<T>(this, { type, name, callerPackageId, member });
8517+
}
8518+
84858519
/** The bare-slot entries of `(type, name)`: one, or a bundle's members, canonical first. */
84868520
private bareSlotEntriesOf(type: string, collection: Map<string, any>, name: string): BundleEntry[] {
84878521
const disc = ITEM_KEY_DISCRIMINATORS[type];
@@ -9442,11 +9476,20 @@ export class SchemaRegistry {
94429476
this.registerItem('app', app, 'name', packageId);
94439477
}
94449478

9479+
/**
9480+
* One app by name, with its navigation contributions merged in.
9481+
*
9482+
* [ADR-0150 D3, #31680 child E8] Resolved through the one package-scoped
9483+
* resolver ({@link resolveItem}): `currentPackageId` is the caller's
9484+
* package (its own app, else the unique holder in its declared closure);
9485+
* omitted, the caller has no package (the environment's own app, else the
9486+
* unique package holder). A name two holders make ambiguous, or one the
9487+
* caller may not reach, is refused naming every candidate.
9488+
*
9489+
* @throws {PackageScopedNameRefusedError} when the resolver refuses.
9490+
*/
94459491
getApp(name: string, currentPackageId?: string): any {
9446-
// ADR-0048 §3.1 — apps are addressed by package id (one app per package).
9447-
// When the caller knows it (route segment / `_packageId`), resolve
9448-
// prefer-local; otherwise this is a best-effort by-name lookup.
9449-
const app = this.getItem('app', name, currentPackageId);
9492+
const app = this.resolveItem<any>('app', name, currentPackageId);
94509493
if (!app) return app;
94519494
return this.applyNavContributions(app);
94529495
}

0 commit comments

Comments
 (0)