From 49dafb3db2030bce1d2cb008a3eafe3e191ab878 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 02:10:37 +0000 Subject: [PATCH 1/4] feat(objectql)!: judge a packages[] stack's residual top-level items by the namespace name rule Claude-Session: https://claude.ai/code/session_01ADHkmQv69sQL5nfT8xuPWs Co-authored-by: Claude --- .changeset/31720-residual-namespace-names.md | 19 ++ packages/objectql/src/index.ts | 2 + ...st-service-residual-namespace-name.test.ts | 219 ++++++++++++++++++ .../objectql/src/namespace-co-owner-names.ts | 50 ++++ packages/objectql/src/plugin.ts | 10 + packages/objectql/src/registry.ts | 25 +- 6 files changed, 324 insertions(+), 1 deletion(-) create mode 100644 .changeset/31720-residual-namespace-names.md create mode 100644 packages/objectql/src/manifest-service-residual-namespace-name.test.ts diff --git a/.changeset/31720-residual-namespace-names.md b/.changeset/31720-residual-namespace-names.md new file mode 100644 index 00000000000..962af751bd4 --- /dev/null +++ b/.changeset/31720-residual-namespace-names.md @@ -0,0 +1,19 @@ +--- +'@objectstack/objectql': minor +--- + +feat(objectql)!: a `packages[]` stack's own top-level items (the residual no package body declares) are judged by the namespace name rule too — a residual sharing rule, queue, assignment rule, hook, app, page, dashboard, report, book, analytics cube or tool under a name another package of the stack's namespace holds is refused, naming every holder. + +Clause-②: no (narrowing) — the `manifest` service (every artifact or config registration, boot included) refuses a stack it accepted before: one whose residual top-level item of these types bears a name an installed package of the stack's `manifest.namespace` holds. Nothing is widened. +Metadata-protocol: narrowed +Skills-docs: none — `skills/**` does not teach that a stack's top level may repeat a name another package of its namespace holds; the rule ships in the refusal text and in ADR-0150 D2. + + + +**BREAKING** accept-set narrowing at the `manifest` service, for one more holder of ADR-0150 D2's namespace name rule (#31717): the residual of a `packages[]` stack, registered under the stack's own id after every body and never through the package door. Measured first-party: 0 stacks carry a residual item of these types (the one first-party `packages[]` stack, `examples/app-multi-package`, has no residual at all). + +- **Why.** ADR-0150 addresses these items by namespace and name. #31717 refuses a package holding a name another package of its namespace holds; the residual registers without an install, so it was not judged, and a residual `crm/home` beside a co-owner's `crm/home` made the address name two items. +- **Before.** The residual item registered under the stack's id beside the other package's item, silently. +- **Now.** Once every body of the stack has installed and before anything of the residual registers, the residual is judged as one holder of the stack's namespace, with the package door's predicate and sentence: `code: 'NAMESPACE_CONFLICT'`, `status: 422`, every holder named, `conflicts[]` structured. A refusal puts the whole artifact back, as any refusal in that run does. A deployment that already holds such a pair no longer boots: the boot names both holders. +- **Who moves.** An author whose stack's top level carries an item of these types that no body declares, under a name an installed package of the stack's namespace already holds. **The one-line fix: rename one of the two items** (or declare it in one of the stack's packages, so it is that package's). +- **Unchanged.** A residual in a namespace no other package holds the name in; another namespace; the shareable platform namespaces (`base`, `system`, `sys`); every type outside these; a residual owned by one of the stack's own bodies (one holder); `os validate` / `os build`, which cannot see installed packages and where a residual can never share a covered name with the stack's own bodies (a body holding the name claims it, so it is not residual); the package door and phase 1's bare-name rule. diff --git a/packages/objectql/src/index.ts b/packages/objectql/src/index.ts index d58510ba5d4..02c1b159aa1 100644 --- a/packages/objectql/src/index.ts +++ b/packages/objectql/src/index.ts @@ -172,11 +172,13 @@ export { namespacePackageOf, namespaceNameConflicts, namespaceNameConflictMessage, + residualNamespacePackageOf, } from './namespace-co-owner-names.js'; export type { NamespaceNameConflict, NamespacePackage, NamespaceUniqueType, + ResidualItems, } from './namespace-co-owner-names.js'; // [ADR-0130 D4] The residual-picklist refusal the `manifest` service throws: a diff --git a/packages/objectql/src/manifest-service-residual-namespace-name.test.ts b/packages/objectql/src/manifest-service-residual-namespace-name.test.ts new file mode 100644 index 00000000000..e4d2e0d8fc7 --- /dev/null +++ b/packages/objectql/src/manifest-service-residual-namespace-name.test.ts @@ -0,0 +1,219 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [ADR-0150 D2, #31720] A `packages[]` stack's RESIDUAL — the top-level items + * no package body declares, which the `manifest` service registers under the + * stack's own id after every body and never through the package door — is one + * more holder of the stack's namespace. An item of the covered types that + * bears a name another package of that namespace holds is refused, in the + * package door's envelope, naming every holder; the whole artifact is put + * back. + * + * Driven through the `manifest` service, the door both boot doors hand a stack + * to. The CONTROLs pin what stays accepted: another namespace, a residual name + * no package holds, a type outside the twelve, the residual owned by one of the + * stack's own bodies, a shareable platform namespace. + */ + +import { afterEach, describe, expect, it } from 'vitest'; +import { ObjectKernel } from '@objectstack/core'; +import { unclaimedTopLevel } from '@objectstack/metadata'; +import { singularToPlural } from '@objectstack/spec/shared'; +import { ObjectQLPlugin } from './plugin.js'; +import type { ObjectQL } from './engine.js'; +import { NAMESPACE_UNIQUE_TYPES, residualNamespacePackageOf, type NamespaceUniqueType } from './namespace-co-owner-names.js'; + +type Refusal = Error & { + code?: string; + status?: number; + httpStatus?: number; + conflicts?: Array<{ namespace: string; type: string; name: string; incomingPackageId: string; existingPackageIds: string[] }>; +}; + +const ALPHA = 'com.acme.alpha'; +const BETA = 'com.acme.beta'; +const RELEASE = 'com.acme.release'; +const NAME = 'shared_item'; + +/** + * The covered types a stack's top level can carry as residual: the residual + * rule walks the stack's metadata collections (`ARTIFACT_FIELD_TO_TYPE` in + * `@objectstack/metadata`), which carry no `jobs`, so a top-level job is never + * a residual item (pinned below). + */ +const RESIDUAL_TYPES = NAMESPACE_UNIQUE_TYPES.filter((t) => t !== 'job'); + +/** A spec-valid item of each type (a package body is parsed on the load path). */ +const item = (type: NamespaceUniqueType, name: string, tag: string): Record => { + const label = `${tag} ${type}`; + switch (type) { + case 'sharing_rule': + return { name, label, type: 'criteria', object: 'crm_account', sharedWith: { type: 'team', value: 'sales' }, condition: 'true', accessLevel: 'read' }; + case 'assignment_rule': + return { name, label, object: 'crm_account', entries: [{ name: 'e1', assignTo: { type: 'queue', value: 'triage' } }] }; + case 'hook': + return { name, label, object: 'crm_account', events: ['beforeInsert'], body: { language: 'js', source: 'return;' } }; + case 'job': + return { name, label, schedule: { type: 'cron', expression: '0 * * * *' }, handler: 'noop' }; + case 'app': + return { name, label, navigation: [] }; + case 'page': + return { name, label, type: 'app', regions: [] }; + case 'dashboard': + return { name, label, widgets: [] }; + case 'report': + return { name, label, dataset: 'ds', values: ['total'] }; + case 'book': + return { name, label, groups: [] }; + case 'analytics_cube': + return { name, title: label, sql: 'select 1', measures: {}, dimensions: {} }; + case 'tool': + return { name, label, description: 'A tool', parameters: {} }; + default: + return { name, label }; + } +}; + +const account = { name: 'crm_account', label: 'Account', fields: { name: { name: 'name', label: 'Name', type: 'text' } } }; + +/** A package body of namespace `ns`. */ +const body = (id: string, ns: string, extra: Record = {}): Record => ({ + id, + name: id.split('.').pop(), + version: '1.0.0', + type: 'app', + namespace: ns, + ...extra, +}); + +/** + * A composed stack whose top level carries `residual` beside `bodies`, under + * the stack's own manifest (`ownerId`, `ns`) — the shape `composeStacks` + * flattens, as `manifest-service-residual-security-seal.test.ts` builds it. + */ +const stack = (ns: string, bodies: unknown[], residual: Record, ownerId = RELEASE) => { + const manifest = { id: ownerId, name: 'release', namespace: ns, version: '1.0.0', type: 'app' }; + return { ...manifest, manifest, packages: bodies.map((m) => ({ manifest: m })), ...residual }; +}; + +const residualOf = (type: NamespaceUniqueType, tag = 'release') => ({ [singularToPlural(type)]: [item(type, NAME, tag)] }); + +/** + * The stack's one body, in `ns`, declaring a dependency on alpha: the + * install-time namespace gate admits a dependent as a co-owner of alpha's + * namespace (ADR-0141 D5), where it refuses a stranger before any name is read. + */ +const betaOf = (ns: string, extra: Record = {}) => body(BETA, ns, { dependencies: { [ALPHA]: '^1.0.0' }, ...extra }); + +const kernels: ObjectKernel[] = []; +afterEach(async () => { + for (const k of kernels.splice(0)) await k.shutdown().catch(() => {}); +}); + +async function boot() { + const kernel = new ObjectKernel({ logger: { level: 'silent' }, gracefulShutdown: false }); + kernels.push(kernel); + await kernel.use(new ObjectQLPlugin()); + await kernel.bootstrap(); + const ql = kernel.getService('objectql'); + const svc = kernel.getService('manifest') as { register(m: unknown): void | Promise }; + const register = async (artifact: unknown): Promise => { + try { + await svc.register(artifact); + return undefined; + } catch (e) { + return e as Refusal; + } + }; + return { ql, register }; +} + +/** Install `alpha` (namespace `crm`) holding one `type` item named NAME, through its own artifact. */ +async function withAlphaHolding(type: NamespaceUniqueType) { + const env = await boot(); + const installed = await env.register({ + packages: [{ manifest: body(ALPHA, 'crm', { objects: [account], [singularToPlural(type)]: [item(type, NAME, 'alpha')] }) }], + }); + expect(installed?.message).toBeUndefined(); + return env; +} + +describe('the residual as a holder', () => { + it('a top-level job is never residual: the residual rule walks no `jobs` collection', () => { + const answer = unclaimedTopLevel(stack('crm', [body(BETA, 'crm')], { jobs: [item('job', NAME, 'release')] })); + expect(answer?.items.map((i) => i.type)).toEqual([]); + }); + + it('reads as ONE holder under the residual owner id, in the stack\'s own namespace', () => { + const s = stack('crm', [body(BETA, 'crm')], { pages: [item('page', 'home', 'r')], tools: [item('tool', 'lookup', 'r')] }); + expect(residualNamespacePackageOf(s, unclaimedTopLevel(s))).toEqual({ + packageId: RELEASE, + namespace: 'crm', + names: [{ type: 'page', name: 'home' }, { type: 'tool', name: 'lookup' }], + }); + }); + + it('is undefined for a stack with no residual, and for a residual holding none of the twelve', () => { + const none = stack('crm', [body(BETA, 'crm', { pages: [item('page', 'home', 'b')] })], { pages: [item('page', 'home', 'b')] }); + expect(residualNamespacePackageOf(none, unclaimedTopLevel(none))).toBeUndefined(); + expect(residualNamespacePackageOf({ manifest: { id: RELEASE, namespace: 'crm' } }, undefined)).toBeUndefined(); + }); +}); + +describe.each(RESIDUAL_TYPES)('%s — a residual item under a name another package of its namespace holds', (type) => { + it('is refused at the manifest service, naming both holders, and the artifact is put back whole', async () => { + const { ql, register } = await withAlphaHolding(type); + const err = await register(stack('crm', [betaOf('crm')], residualOf(type))); + expect([err?.code, err?.status, err?.httpStatus], err?.message).toEqual(['NAMESPACE_CONFLICT', 422, 422]); + expect(err?.conflicts).toEqual([ + { namespace: 'crm', type, name: NAME, incomingPackageId: RELEASE, existingPackageIds: [ALPHA] }, + ]); + expect(err?.message).toContain(`"${RELEASE}"`); + expect(err?.message).toContain(`"${ALPHA}"`); + // Put back whole: the stack's body is not installed, and the name keeps answering alpha's item. + expect(ql.registry.getPackage(BETA)).toBeUndefined(); + expect((ql.registry.getItem(type, NAME) as { label?: string; title?: string } | undefined)?.[type === 'analytics_cube' ? 'title' : 'label']) + .toBe(`alpha ${type}`); + }); + + it('CONTROL — the same residual in a stack of ANOTHER namespace registers', async () => { + const { register } = await withAlphaHolding(type); + expect((await register(stack('hr', [betaOf('hr')], residualOf(type))))?.message).toBeUndefined(); + }); +}); + +describe('what the residual rule leaves alone', () => { + it('CONTROL — a residual name no package of the namespace holds registers, under the residual owner id', async () => { + const { ql, register } = await withAlphaHolding('page'); + expect((await register(stack('crm', [betaOf('crm')], { pages: [item('page', 'release_home', 'release')] })))?.message) + .toBeUndefined(); + expect((ql.registry.getItem('page', 'release_home') as { label?: string } | undefined)?.label).toBe('release page'); + }); + + it('CONTROL — a residual item of a type outside the twelve is not judged (a dataset alpha also holds)', async () => { + const dataset = { name: NAME, label: 'ds', object: 'crm_account', measures: [{ name: 'count', aggregate: 'count' }], dimensions: [] }; + const env = await boot(); + expect((await env.register({ packages: [{ manifest: body(ALPHA, 'crm', { objects: [account], datasets: [dataset] }) }] }))?.message) + .toBeUndefined(); + const err = await env.register(stack('crm', [betaOf('crm')], { datasets: [{ ...dataset, label: 'release ds' }] })); + expect(err?.code, err?.message).not.toBe('NAMESPACE_CONFLICT'); + }); + + it('CONTROL — a shareable platform namespace is not judged', async () => { + const env = await boot(); + expect((await env.register({ packages: [{ manifest: body(ALPHA, 'sys', { pages: [item('page', NAME, 'alpha')] }) }] }))?.message) + .toBeUndefined(); + const err = await env.register(stack('sys', [betaOf('sys')], residualOf('page'))); + expect(err?.code, err?.message).not.toBe('NAMESPACE_CONFLICT'); + }); +}); + +describe('inside one artifact', () => { + it('the residual never shares a covered name with one of the stack\'s own bodies: a body holding it claims the slot, so it is not residual', () => { + for (const type of RESIDUAL_TYPES) { + const s = stack('crm', [body(BETA, 'crm', residualOf(type, 'beta'))], residualOf(type)); + expect(unclaimedTopLevel(s)?.items, type).toEqual([]); + expect(residualNamespacePackageOf(s, unclaimedTopLevel(s)), type).toBeUndefined(); + } + }); +}); diff --git a/packages/objectql/src/namespace-co-owner-names.ts b/packages/objectql/src/namespace-co-owner-names.ts index 650aa7646cf..9cfb4b39f3b 100644 --- a/packages/objectql/src/namespace-co-owner-names.ts +++ b/packages/objectql/src/namespace-co-owner-names.ts @@ -58,6 +58,16 @@ * (`name`, else `id`); for `app`, also the manifest-as-app and plugin-as-app * rows the engine registers under the manifest's (or plugin's) `name`. * + * ## The residual (#31720) + * + * A `packages[]` stack's top-level items that no package body declares (the + * residual, ADR-0130 D4) register under the stack's own id after every body, + * never through the package door. {@link residualNamespacePackageOf} reads + * them as one more holder of the stack's namespace, and the same predicate + * judges it: at the `manifest` service, once the bodies have installed + * (`SchemaRegistry.refuseResidualNamespaceNameConflicts`), and in + * `os validate` / `os build` against the stack's bodies. + * * ⛔ No package-scoped resolution here (that is ADR-0150 child R), and phase * 1's one-holder rule is not touched. */ @@ -200,6 +210,46 @@ export function namespacePackageOf(manifest: unknown): NamespacePackage | undefi }; } +/** + * The residual of a `packages[]` stack — the top-level items no package body + * declares — in the one shape both of its doors read it: `unclaimedTopLevel` + * (`@objectstack/metadata`), the residual rule's own answer. Spelt + * structurally here so this module imports nothing above the registry. + */ +export interface ResidualItems { + /** The id every residual item is registered under (the stack's `manifest.id`). */ + readonly ownerId: string | undefined; + /** The residual items, each with the metadata type it registers as. */ + readonly items: ReadonlyArray<{ readonly type: string; readonly item: unknown }>; +} + +/** + * [#31720] A `packages[]` stack's residual as the predicate sees it: ONE + * holder, under the residual's owner id, in the stack's own + * `manifest.namespace`, holding the residual's items of the covered types — + * named the way {@link declaredNamespaceUniqueNames} names a package's. The + * residual registers under that id and is addressed under that namespace, so + * it is judged as a package of it would be, by {@link namespaceNameConflicts}: + * no second predicate. + * + * `undefined` when there is no residual (a stack without `packages[]`, or a + * normally composed one), when it has no owner id, or when none of its items + * is of a covered type. + */ +export function residualNamespacePackageOf(stack: unknown, residual: ResidualItems | undefined): NamespacePackage | undefined { + if (residual === undefined || residual.ownerId === undefined) return undefined; + const body: Record = { id: residual.ownerId }; + for (const { type, item } of residual.items) { + if (!isNamespaceUniqueType(type)) continue; + const key = singularToPlural(type); + ((body[key] ??= []) as unknown[]).push(item); + } + const names = declaredNamespaceUniqueNames(body); + if (names.length === 0) return undefined; + const namespace = asRecord(asRecord(stack)?.manifest)?.namespace; + return { packageId: residual.ownerId, namespace: typeof namespace === 'string' ? namespace : undefined, names }; +} + /** One `(type, name)` an incoming package would hold beside another package of its namespace. */ export interface NamespaceNameConflict { /** The namespace both hold it in. */ diff --git a/packages/objectql/src/plugin.ts b/packages/objectql/src/plugin.ts index 424ce9bac86..89592c7ae04 100644 --- a/packages/objectql/src/plugin.ts +++ b/packages/objectql/src/plugin.ts @@ -28,6 +28,9 @@ import { applyConversionsToStoredItem, expandViewContainer, isAggregatedViewCont // body's (see `registerUnclaimedTopLevelItems`). import { deriveViewContainerObject } from '@objectstack/metadata/view-container'; import { viewContainerNameRefusal } from './view-container-name-refusal.js'; +// [ADR-0150 D2, #31720] The residual as one holder of the stack's namespace, +// for the namespace name rule (see the residual registration below). +import { residualNamespacePackageOf } from './namespace-co-owner-names.js'; import { StorageNameMapping } from '@objectstack/spec/system'; import { applyProtection } from '@objectstack/spec/shared'; // [#21777] The ONE "is this schema the remote's?" predicate, shared with `ObjectQL.syncSchemas`. @@ -999,6 +1002,13 @@ export class ObjectQLPlugin implements Plugin { id: manifest.id || manifest.name }); } + // [ADR-0150 D2, #31720] The residual is one more holder of the + // stack's namespace: refused, in the package door's envelope, when it + // declares an item of the covered types under a name another package + // of that namespace holds — judged here, once every body has + // installed and before anything of the residual registers, so a + // refusal puts back the whole artifact like any other in this run. + ql.registry.refuseResidualNamespaceNameConflicts(residualNamespacePackageOf(artifact, residualAnswer)); // [ADR-0130 D4] The residual's objects, AFTER every body — the order // the metadata door registers the same stack in — under the id the // residual rule names, through the registry verb a body's own objects diff --git a/packages/objectql/src/registry.ts b/packages/objectql/src/registry.ts index 6f9df2a7bbc..6a2c884308a 100644 --- a/packages/objectql/src/registry.ts +++ b/packages/objectql/src/registry.ts @@ -9180,7 +9180,30 @@ export class SchemaRegistry { * @throws {NamespaceNameConflictError} ADR-0112 envelope, naming every holder. */ private refuseNamespaceNameConflicts(manifest: ObjectStackManifest): void { - const incoming = namespacePackageOf(manifest); + this.refuseNamespaceHolderConflicts(namespacePackageOf(manifest)); + } + + /** + * [ADR-0150 D2, #31720] The same refusal for a `packages[]` stack's + * RESIDUAL — the top-level items no package body declares, which the + * `manifest` service registers under the residual's owner id after every + * body and never through {@link installPackage}. `residual` is that holder + * as the predicate sees it (`residualNamespacePackageOf`): the owner id, the + * stack's own `manifest.namespace`, its covered items. Judged against the + * installed packages of that namespace — the stack's own bodies included, + * once they have installed — exactly as a package is, in the same envelope. + * `undefined` (no residual, or none of the covered types) refuses nothing. + */ + refuseResidualNamespaceNameConflicts(residual: NamespacePackage | undefined): void { + this.refuseNamespaceHolderConflicts(residual); + } + + /** + * The one walk both refusals above share: `incoming` against every installed + * package record of its namespace, read through the same predicate, thrown + * in one envelope. + */ + private refuseNamespaceHolderConflicts(incoming: NamespacePackage | undefined): void { if (incoming === undefined || !isNamespaceJudged(incoming.namespace) || incoming.names.length === 0) return; const sitting: NamespacePackage[] = []; for (const record of this.metadata.get('package')?.values() ?? []) { From 2faaebe929ab4463d78424b57819e3ded3ad9eb7 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 02:12:00 +0000 Subject: [PATCH 2/4] feat(objectql): a registered residual holds its names for what registers after it Claude-Session: https://claude.ai/code/session_01ADHkmQv69sQL5nfT8xuPWs Co-authored-by: Claude --- .changeset/31720-residual-namespace-names.md | 4 +-- ...st-service-residual-namespace-name.test.ts | 28 +++++++++++++++++++ packages/objectql/src/plugin.ts | 9 +++++- packages/objectql/src/registry.ts | 23 +++++++++++++-- 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/.changeset/31720-residual-namespace-names.md b/.changeset/31720-residual-namespace-names.md index 962af751bd4..a4956b2f246 100644 --- a/.changeset/31720-residual-namespace-names.md +++ b/.changeset/31720-residual-namespace-names.md @@ -4,7 +4,7 @@ feat(objectql)!: a `packages[]` stack's own top-level items (the residual no package body declares) are judged by the namespace name rule too — a residual sharing rule, queue, assignment rule, hook, app, page, dashboard, report, book, analytics cube or tool under a name another package of the stack's namespace holds is refused, naming every holder. -Clause-②: no (narrowing) — the `manifest` service (every artifact or config registration, boot included) refuses a stack it accepted before: one whose residual top-level item of these types bears a name an installed package of the stack's `manifest.namespace` holds. Nothing is widened. +Clause-②: no (narrowing) — the `manifest` service (every artifact or config registration, boot included) refuses a stack it accepted before: one whose residual top-level item of these types bears a name an installed package of the stack's `manifest.namespace` holds; and the package door refuses a package it accepted before: one holding such a name that a registered stack's residual of its namespace already holds. Nothing is widened. Metadata-protocol: narrowed Skills-docs: none — `skills/**` does not teach that a stack's top level may repeat a name another package of its namespace holds; the rule ships in the refusal text and in ADR-0150 D2. @@ -14,6 +14,6 @@ Skills-docs: none — `skills/**` does not teach that a stack's top level may re - **Why.** ADR-0150 addresses these items by namespace and name. #31717 refuses a package holding a name another package of its namespace holds; the residual registers without an install, so it was not judged, and a residual `crm/home` beside a co-owner's `crm/home` made the address name two items. - **Before.** The residual item registered under the stack's id beside the other package's item, silently. -- **Now.** Once every body of the stack has installed and before anything of the residual registers, the residual is judged as one holder of the stack's namespace, with the package door's predicate and sentence: `code: 'NAMESPACE_CONFLICT'`, `status: 422`, every holder named, `conflicts[]` structured. A refusal puts the whole artifact back, as any refusal in that run does. A deployment that already holds such a pair no longer boots: the boot names both holders. +- **Now.** Once every body of the stack has installed and before anything of the residual registers, the residual is judged as one holder of the stack's namespace, with the package door's predicate and sentence: `code: 'NAMESPACE_CONFLICT'`, `status: 422`, every holder named, `conflicts[]` structured. A refusal puts the whole artifact back, as any refusal in that run does. A registered residual is then a holder of its namespace for what registers after it, so a package arriving later with the residual's name is refused at the package door, naming the stack's id: the order the two arrive in does not decide. A deployment that already holds such a pair no longer boots: the boot names both holders. - **Who moves.** An author whose stack's top level carries an item of these types that no body declares, under a name an installed package of the stack's namespace already holds. **The one-line fix: rename one of the two items** (or declare it in one of the stack's packages, so it is that package's). - **Unchanged.** A residual in a namespace no other package holds the name in; another namespace; the shareable platform namespaces (`base`, `system`, `sys`); every type outside these; a residual owned by one of the stack's own bodies (one holder); `os validate` / `os build`, which cannot see installed packages and where a residual can never share a covered name with the stack's own bodies (a body holding the name claims it, so it is not residual); the package door and phase 1's bare-name rule. diff --git a/packages/objectql/src/manifest-service-residual-namespace-name.test.ts b/packages/objectql/src/manifest-service-residual-namespace-name.test.ts index e4d2e0d8fc7..5fe124ccc9e 100644 --- a/packages/objectql/src/manifest-service-residual-namespace-name.test.ts +++ b/packages/objectql/src/manifest-service-residual-namespace-name.test.ts @@ -208,6 +208,34 @@ describe('what the residual rule leaves alone', () => { }); }); +describe('order does not decide', () => { + it('a package registered AFTER the stack, holding the residual\'s name, is refused at the package door, naming the residual\'s owner', async () => { + const { ql, register } = await boot(); + expect((await register(stack('crm', [betaOf('crm')], residualOf('page'))))?.message).toBeUndefined(); + const err = await register({ packages: [{ manifest: body(ALPHA, 'crm', { pages: [item('page', NAME, 'alpha')] }) }] }); + expect([err?.code, err?.status], err?.message).toEqual(['NAMESPACE_CONFLICT', 422]); + expect(err?.conflicts).toEqual([ + { namespace: 'crm', type: 'page', name: NAME, incomingPackageId: ALPHA, existingPackageIds: [RELEASE] }, + ]); + expect(ql.registry.getPackage(ALPHA)).toBeUndefined(); + }); + + it('CONTROL — the same stack registering again is not judged against its own residual', async () => { + const { register } = await boot(); + const s = stack('crm', [betaOf('crm')], residualOf('page')); + expect((await register(s))?.message).toBeUndefined(); + expect((await register(s))?.message).toBeUndefined(); + }); + + it('CONTROL — a refused stack records no residual holder: the package after it installs', async () => { + const { register } = await withAlphaHolding('tool'); + expect((await register(stack('crm', [betaOf('crm')], { ...residualOf('tool'), pages: [item('page', 'release_home', 'r')] })))?.code) + .toBe('NAMESPACE_CONFLICT'); + const gamma = body('com.acme.gamma', 'crm', { dependencies: { [ALPHA]: '^1.0.0' }, pages: [item('page', 'release_home', 'gamma')] }); + expect((await register({ packages: [{ manifest: gamma }] }))?.message).toBeUndefined(); + }); +}); + describe('inside one artifact', () => { it('the residual never shares a covered name with one of the stack\'s own bodies: a body holding it claims the slot, so it is not residual', () => { for (const type of RESIDUAL_TYPES) { diff --git a/packages/objectql/src/plugin.ts b/packages/objectql/src/plugin.ts index 89592c7ae04..5014cb6d35a 100644 --- a/packages/objectql/src/plugin.ts +++ b/packages/objectql/src/plugin.ts @@ -995,6 +995,9 @@ export class ObjectQLPlugin implements Plugin { // in the running kernel while the install door answered the refusal // and ledgered nothing. A single-package artifact is the one-body run // `registerApp` opens on its own, answered as before. + // [ADR-0150 D2, #31720] The residual as one holder of the stack's + // namespace — judged below, recorded once the artifact has registered. + const residualHolder = residualNamespacePackageOf(artifact, residualAnswer); ql.registry.restoringOnRefusal(() => { for (const { manifest, source } of entries as Array<{ manifest: any; source?: 'code' | 'marketplace' }>) { ql.registerApp(manifest, scope, source ?? options?.source); @@ -1008,7 +1011,7 @@ export class ObjectQLPlugin implements Plugin { // of that namespace holds — judged here, once every body has // installed and before anything of the residual registers, so a // refusal puts back the whole artifact like any other in this run. - ql.registry.refuseResidualNamespaceNameConflicts(residualNamespacePackageOf(artifact, residualAnswer)); + ql.registry.refuseResidualNamespaceNameConflicts(residualHolder); // [ADR-0130 D4] The residual's objects, AFTER every body — the order // the metadata door registers the same stack in — under the id the // residual rule names, through the registry verb a body's own objects @@ -1034,6 +1037,10 @@ export class ObjectQLPlugin implements Plugin { // own; the door's word, `code` when it states none, is its origin. const residualOwnerId = residual?.ownerId ?? residualItems?.ownerId; if (residualOwnerId !== undefined) ql.recordResidualOwner(residualOwnerId, options?.source ?? 'code'); + // [ADR-0150 D2, #31720] …and the residual as a holder of its namespace, + // so a package registering after this stack is judged against it too. + // Recorded on the same terms: once the artifact has registered whole. + ql.registry.recordResidualNamespaceHolder(residualHolder); // [#24472] The boot reported every same-name pair it found // (`kernel:ready`, below); an artifact arriving later is judged once it // has registered, for the pairs naming one of its packages on either diff --git a/packages/objectql/src/registry.ts b/packages/objectql/src/registry.ts index 6a2c884308a..b54d78abcf1 100644 --- a/packages/objectql/src/registry.ts +++ b/packages/objectql/src/registry.ts @@ -9198,10 +9198,26 @@ export class SchemaRegistry { this.refuseNamespaceHolderConflicts(residual); } + /** + * [#31720] Record a registered residual as a holder of its namespace, so a + * package (or another stack's residual) registering AFTER it is judged + * against it too: the residual has no package record for the walk below to + * read. Called once the stack has registered whole — a refused one records + * nothing — and keyed by the residual's owner id, so the same stack + * registering again replaces its own entry. `undefined` records nothing. + */ + recordResidualNamespaceHolder(residual: NamespacePackage | undefined): void { + if (residual === undefined || !isNamespaceJudged(residual.namespace)) return; + this.residualNamespaceHolders.set(residual.packageId, residual); + } + + /** The residual holders {@link recordResidualNamespaceHolder} recorded, by owner id. */ + private readonly residualNamespaceHolders = new Map(); + /** * The one walk both refusals above share: `incoming` against every installed - * package record of its namespace, read through the same predicate, thrown - * in one envelope. + * package record of its namespace and every recorded residual holder of it, + * read through the same predicate, thrown in one envelope. */ private refuseNamespaceHolderConflicts(incoming: NamespacePackage | undefined): void { if (incoming === undefined || !isNamespaceJudged(incoming.namespace) || incoming.names.length === 0) return; @@ -9212,6 +9228,9 @@ export class SchemaRegistry { const holder = namespacePackageOf(recorded); if (holder !== undefined) sitting.push(holder); } + for (const residual of this.residualNamespaceHolders.values()) { + if (residual.namespace === incoming.namespace) sitting.push(residual); + } const conflicts = namespaceNameConflicts(incoming, sitting); if (conflicts.length > 0) throw new NamespaceNameConflictError(conflicts); } From f3d363a4c5e24d310e3f924052a5dc4d992e1ca5 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 03:27:36 +0000 Subject: [PATCH 3/4] fix(objectql): uninstalling a residual's owner frees the residual's namespace names The residual's items are registered under their owner id, so uninstallPackage(id) withdraws them; the recorded residual holder is now forgotten beside the bare-name claims, and a package declaring the name afterwards installs. The module header names the manifest service as the one door that judges the residual and states the os validate / os build bound; the ADR anchor invariant names the residual call. The type-outside-the-twelve CONTROL now asserts acceptance with a doc, a type no other name rule judges. Claude-Session: https://claude.ai/code/session_01ADHkmQv69sQL5nfT8xuPWs Co-authored-by: Claude --- ...st-service-residual-namespace-name.test.ts | 26 +++++++++++++++---- .../objectql/src/namespace-co-owner-names.ts | 13 +++++++--- packages/objectql/src/registry.ts | 5 ++++ ...tql__src__namespace-co-owner-names.ts.json | 2 +- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/packages/objectql/src/manifest-service-residual-namespace-name.test.ts b/packages/objectql/src/manifest-service-residual-namespace-name.test.ts index 5fe124ccc9e..08617073c4b 100644 --- a/packages/objectql/src/manifest-service-residual-namespace-name.test.ts +++ b/packages/objectql/src/manifest-service-residual-namespace-name.test.ts @@ -190,13 +190,18 @@ describe('what the residual rule leaves alone', () => { expect((ql.registry.getItem('page', 'release_home') as { label?: string } | undefined)?.label).toBe('release page'); }); - it('CONTROL — a residual item of a type outside the twelve is not judged (a dataset alpha also holds)', async () => { - const dataset = { name: NAME, label: 'ds', object: 'crm_account', measures: [{ name: 'count', aggregate: 'count' }], dimensions: [] }; + it('CONTROL — a residual item of a type outside the twelve is not judged (a doc alpha also holds)', async () => { + // Outside the twelve AND outside every other name rule (a dataset is phase + // 1's one-holder type, a position the security catalog's, each refusing + // on its own terms), so nothing judges it. + // A fresh literal per registration: the registry stamps `_packageId` onto + // the object it registers, and a shared literal would carry alpha's stamp. + const doc = (label: string) => ({ name: NAME, label, content: `# ${label}` }); const env = await boot(); - expect((await env.register({ packages: [{ manifest: body(ALPHA, 'crm', { objects: [account], datasets: [dataset] }) }] }))?.message) + expect((await env.register({ packages: [{ manifest: body(ALPHA, 'crm', { docs: [doc('alpha doc')] }) }] }))?.message) .toBeUndefined(); - const err = await env.register(stack('crm', [betaOf('crm')], { datasets: [{ ...dataset, label: 'release ds' }] })); - expect(err?.code, err?.message).not.toBe('NAMESPACE_CONFLICT'); + const err = await env.register(stack('crm', [betaOf('crm')], { docs: [doc('release doc')] })); + expect(err?.message).toBeUndefined(); }); it('CONTROL — a shareable platform namespace is not judged', async () => { @@ -220,6 +225,17 @@ describe('order does not decide', () => { expect(ql.registry.getPackage(ALPHA)).toBeUndefined(); }); + it('uninstalling the residual\'s owner (a body id, the preserve shape) frees the residual\'s names: a package holding the name then installs', async () => { + const { ql, register } = await boot(); + expect((await register(stack('crm', [betaOf('crm')], residualOf('page'), BETA)))?.message).toBeUndefined(); + expect((ql.registry.getItem('page', NAME) as { label?: string } | undefined)?.label).toBe('release page'); + expect(ql.registry.uninstallPackage(BETA)).toBe(true); + expect(ql.registry.getItem('page', NAME)).toBeUndefined(); + expect((await register({ packages: [{ manifest: body(ALPHA, 'crm', { pages: [item('page', NAME, 'alpha')] }) }] }))?.message) + .toBeUndefined(); + expect((ql.registry.getItem('page', NAME) as { label?: string } | undefined)?.label).toBe('alpha page'); + }); + it('CONTROL — the same stack registering again is not judged against its own residual', async () => { const { register } = await boot(); const s = stack('crm', [betaOf('crm')], residualOf('page')); diff --git a/packages/objectql/src/namespace-co-owner-names.ts b/packages/objectql/src/namespace-co-owner-names.ts index 9cfb4b39f3b..7c32976ca35 100644 --- a/packages/objectql/src/namespace-co-owner-names.ts +++ b/packages/objectql/src/namespace-co-owner-names.ts @@ -64,9 +64,16 @@ * residual, ADR-0130 D4) register under the stack's own id after every body, * never through the package door. {@link residualNamespacePackageOf} reads * them as one more holder of the stack's namespace, and the same predicate - * judges it: at the `manifest` service, once the bodies have installed - * (`SchemaRegistry.refuseResidualNamespaceNameConflicts`), and in - * `os validate` / `os build` against the stack's bodies. + * judges it at one door, the `manifest` service, once the bodies have + * installed (`SchemaRegistry.refuseResidualNamespaceNameConflicts`). A + * residual that registers is recorded as a holder + * (`SchemaRegistry.recordResidualNamespaceHolder`), so what registers after it + * is judged against it too; uninstalling its owner id frees its names. + * + * `os validate` / `os build` do not judge the residual, and need not: inside + * one artifact a residual can never share a covered name with one of the + * stack's bodies (a body holding the name claims the slot, so the item is not + * residual), and the packages other artifacts installed are invisible there. * * ⛔ No package-scoped resolution here (that is ADR-0150 child R), and phase * 1's one-holder rule is not touched. diff --git a/packages/objectql/src/registry.ts b/packages/objectql/src/registry.ts index b54d78abcf1..a324c6e74c9 100644 --- a/packages/objectql/src/registry.ts +++ b/packages/objectql/src/registry.ts @@ -9286,6 +9286,11 @@ export class SchemaRegistry { this.forgetDefaultRecordPageClaims(id); // …and its bare names of the covered types. this.forgetBareNameClaims(id); + // [#31720] …and the namespace names a stack's residual holds under this + // id: the residual's items are registered under their owner id, so + // `unregisterItemsByPackage(id)` above has just withdrawn them (the + // owner id is one of the stack's own bodies in the `preserve` shape). + this.residualNamespaceHolders.delete(id); // [#30581] …and the navigation entries it put into other packages' apps: // with no row left, the fold's disabled check cannot withhold them. this.setPackageNavContributions(id, []); diff --git a/scripts/adr-anchors/packages__objectql__src__namespace-co-owner-names.ts.json b/scripts/adr-anchors/packages__objectql__src__namespace-co-owner-names.ts.json index c5b0eff6530..80a4bf05675 100644 --- a/scripts/adr-anchors/packages__objectql__src__namespace-co-owner-names.ts.json +++ b/scripts/adr-anchors/packages__objectql__src__namespace-co-owner-names.ts.json @@ -4,5 +4,5 @@ "ADR-0150", "ADR-0130" ], - "invariant": "ADR-0150 D2 (ruled): behavioural metadata is addressed by (namespace, name) while its identity is (package id, name), and several packages may share one namespace (ADR-0130 D1 artifact co-owners, ADR-0141 D5 dependency co-owners), so the address NAMESPACE/NAME names one item only if no two packages of the namespace hold it. This module is the ONE predicate for the twelve bucket 2/3 types phase 1's one-holder list does not cover (sharing_rule, queue, assignment_rule, hook, job, app, page, dashboard, report, book, analytics_cube, tool): SchemaRegistry.installPackage asks it right after the ADR-0130 D3 object check, ahead of every mutation and at every install, and os validate / os build ask it over an artifact's packages in the boot's install order, printing the same sentence. Every holder is named. ⛔ Never judge a package with no namespace, a shareable platform namespace (base, system, sys: the namespace gate's exemption), the environment catalog, or the same package again; never a type outside the twelve (a wider narrowing is the maintainer's); never package-scoped resolution (ADR-0150 child R); never let OS_METADATA_COLLISION=warn downgrade it; never a second copy of the predicate at either door." + "invariant": "ADR-0150 D2 (ruled): behavioural metadata is addressed by (namespace, name) while its identity is (package id, name), and several packages may share one namespace (ADR-0130 D1 artifact co-owners, ADR-0141 D5 dependency co-owners), so the address NAMESPACE/NAME names one item only if no two packages of the namespace hold it. This module is the ONE predicate for the twelve bucket 2/3 types phase 1's one-holder list does not cover (sharing_rule, queue, assignment_rule, hook, job, app, page, dashboard, report, book, analytics_cube, tool): SchemaRegistry.installPackage asks it right after the ADR-0130 D3 object check, ahead of every mutation and at every install, and os validate / os build ask it over an artifact's packages in the boot's install order, printing the same sentence. A packages[] stack's residual (ADR-0130 D4, registered under the stack's own id, never through installPackage) is one more holder of the stack's namespace: the manifest service asks the same predicate once every body has installed and before any residual item registers, a residual that registered is recorded so what registers after it is judged against it, and uninstalling its owner id frees its names (#31720). Every holder is named. ⛔ Never judge a package with no namespace, a shareable platform namespace (base, system, sys: the namespace gate's exemption), the environment catalog, or the same package again; never a type outside the twelve (a wider narrowing is the maintainer's); never package-scoped resolution (ADR-0150 child R); never let OS_METADATA_COLLISION=warn downgrade it; never a second copy of the predicate at either door." } From 9970d032e898c196b3936d297f2108c042eee13b Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 03:30:00 +0000 Subject: [PATCH 4/4] test(objectql): the other-namespace CONTROL for a tool reads phase 1's refusal Since a tool holds one name per deployment (phase 1's one-holder list), the same tool name in a stack of another namespace is refused by phase 1 on its own terms; the CONTROL now pins that the namespace rule is not the one refusing it. Claude-Session: https://claude.ai/code/session_01ADHkmQv69sQL5nfT8xuPWs Co-authored-by: Claude --- .../manifest-service-residual-namespace-name.test.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/objectql/src/manifest-service-residual-namespace-name.test.ts b/packages/objectql/src/manifest-service-residual-namespace-name.test.ts index 08617073c4b..4c16bdacfe2 100644 --- a/packages/objectql/src/manifest-service-residual-namespace-name.test.ts +++ b/packages/objectql/src/manifest-service-residual-namespace-name.test.ts @@ -176,9 +176,17 @@ describe.each(RESIDUAL_TYPES)('%s — a residual item under a name another packa .toBe(`alpha ${type}`); }); - it('CONTROL — the same residual in a stack of ANOTHER namespace registers', async () => { + it('CONTROL — the same residual in a stack of ANOTHER namespace is not judged by the namespace rule', async () => { const { register } = await withAlphaHolding(type); - expect((await register(stack('hr', [betaOf('hr')], residualOf(type))))?.message).toBeUndefined(); + const err = await register(stack('hr', [betaOf('hr')], residualOf(type))); + if (type === 'tool') { + // A tool holds one name per deployment since #31661 (phase 1's + // one-holder list), so phase 1 refuses it on its own terms, in every + // namespace; the namespace rule still does not judge it. + expect(err?.message).toMatch(/^Bare-name conflict:/); + return; + } + expect(err?.message).toBeUndefined(); }); });