Skip to content
Merged
10 changes: 10 additions & 0 deletions .changeset/33507-bare-name-reason-namespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@objectstack/objectql': patch
---

fix(objectql): the bare-name refusal and cold-boot warning say a narrowed type holds one name per namespace

Clause-②: no
Metadata-protocol: none

The reason a `Bare-name conflict` refusal and the `Bare-name conflict at boot` warning carry (`bareNameReason`) said, for every covered type, that it "holds one name per deployment ... with no package to tell two definitions apart, so with two holders every caller is served the same one, chosen by registration order". Every covered type (picklist, mapping, dataset, API, object-less action, function, flow, webhook, connector, email template, agent, skill, tool) has since narrowed to the namespace (ADR-0150 D2/D9, `NAMESPACE_SCOPED_BARE_NAME_TYPES`), so an administrator refused for one of them was told a rule the doors no longer apply. The reason for a narrowed type now states its rule: a package of another namespace may ship the same name, but within one namespace, or beside a package that declares no namespace, a name has one holder, and a bare name two holders share is refused naming both. For an email template the reason also names the two holdings that still meet whatever the namespaces, after "save the exceptions below": a name the platform requests by its bare spelling (the `auth.*` mails, such as `auth.password_reset`) keeps one holder per deployment, and a package that depends on the name's holder (which does not depend on it back) and ships the name only in locales the holder does not ship is refused as an addition to the holder's email template, as before. A type that joins the covered list before its own narrowing keeps the one-name-per-deployment sentence. No rule changes: the refusals fire where they did, with the same `NAMESPACE_CONFLICT` code and `422` status.
87 changes: 76 additions & 11 deletions packages/objectql/src/bare-name-one-holder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ import {
BARE_NAME_ONE_HOLDER_TYPES,
isPackageScopedType,
isPlatformRequestedBareName,
PLATFORM_REQUESTED_BARE_NAMES_BY_TYPE,
OVERRIDABLE_BASELINE_PACKAGES,
isBareNameOneHolderType,
isOverridableBaseline,
Expand Down Expand Up @@ -604,7 +605,9 @@ export function bareNameTypeLabel(type: BareNameOneHolderType): string {

/**
* How each covered type's consumer names it with no package — the clause a
* refusal's reason carries for the types it lists.
* refusal's reason carries for a type still held one name per deployment (not
* in {@link NAMESPACE_SCOPED_BARE_NAME_TYPES}; a narrowed type's reason states
* the namespace rule instead).
*/
const CONSUMER_CLAUSE: Readonly<Record<BareNameOneHolderType, string>> = Object.freeze({
picklist: 'a field names its picklist',
Expand Down Expand Up @@ -643,20 +646,82 @@ const joinList = (parts: readonly string[]): string =>
parts.length <= 1 ? (parts[0] ?? '') : `${parts.slice(0, -1).join(', ')} and ${parts[parts.length - 1]}`;

/**
* Why a second holder is refused, in one sentence a reader can act on — for
* the covered types `types` lists (every covered type when none), in the
* order {@link BARE_NAME_ONE_HOLDER_TYPES} lists them. Shared by the doors'
* refusal and the cold boot's warning.
* The holdings of a narrowed type that still meet whatever their namespaces
* ("Two holdings still meet under a narrowed type", above), as the clauses a
* reason states them in — empty for a type with neither. Read from the same
* tables {@link bareNameHoldersMeet} and the registry's bundle-addition read
* ask: the names the platform requests by their bare spelling
* (`PLATFORM_REQUESTED_BARE_NAMES_BY_TYPE`), and a bundle's addition
* ({@link isBundleAdditionPair}), for a type whose items are keyed by a
* discriminator (`ITEM_KEY_DISCRIMINATORS`).
*/
function crossNamespaceExceptionClauses(type: BareNameOneHolderType): string[] {
const clauses: string[] = [];
const requested = isPackageScopedType(type) ? (PLATFORM_REQUESTED_BARE_NAMES_BY_TYPE[type] ?? []) : [];
if (requested.length > 0) {
clauses.push(
`a name the platform requests by its bare spelling (${joinList(requested.map((n) => `"${n}"`))}) keeps one ` +
'holder per deployment',
);
}
const disc = ITEM_KEY_DISCRIMINATORS[type];
if (disc !== undefined && isBareNameRegistryType(type)) {
const noun = TYPE_NOUN[type].replace(/^an? /, '');
clauses.push(
`a package that depends on the name's holder (which does not depend on it back) and ships the name only in ` +
`${disc.field}s the holder does not ship is refused as an addition to the holder's ${noun}`,
);
}
return clauses;
}

/**
* Why a second holder is refused, in one sentence a reader can act on per
* rule — for the covered types `types` lists (every covered type when none),
* in the order {@link BARE_NAME_ONE_HOLDER_TYPES} lists them. Shared by the
* doors' refusal and the cold boot's warning.
*
* A narrowed type ({@link NAMESPACE_SCOPED_BARE_NAME_TYPES}, "The narrowing")
* is told its own rule: one holder per namespace, another namespace may ship
* the name, and a bare name two holders share is refused naming both; a
* narrowed type whose holdings can still meet across namespaces is told
* those exceptions too ({@link crossNamespaceExceptionClauses}). A type
* not yet narrowed keeps the one-name-per-deployment sentence and its
* consumers' clause. A list carrying both gets one sentence per rule, the
* narrowed one first.
*/
export function bareNameReason(types: readonly BareNameOneHolderType[] = BARE_NAME_ONE_HOLDER_TYPES): string {
const present = BARE_NAME_ONE_HOLDER_TYPES.filter((t) => types.includes(t));
const listed = present.length > 0 ? present : BARE_NAME_ONE_HOLDER_TYPES;
const nouns = joinList(listed.map((t) => TYPE_NOUN[t]));
return (
`${nouns.charAt(0).toUpperCase()}${nouns.slice(1)} ${listed.length === 1 ? 'holds' : 'each hold'} one name per ` +
`deployment: ${joinList(listed.map((t) => CONSUMER_CLAUSE[t]))}, with no package to tell two definitions ` +
'apart, so with two holders every caller is served the same one, chosen by registration order.'
);
const subject = (group: readonly BareNameOneHolderType[]): string => {
const nouns = joinList(group.map((t) => TYPE_NOUN[t]));
return `${nouns.charAt(0).toUpperCase()}${nouns.slice(1)} ${group.length === 1 ? 'holds' : 'each hold'}`;
};
const narrowed = listed.filter((t) => isNamespaceScopedBareNameType(t));
const perDeployment = listed.filter((t) => !isNamespaceScopedBareNameType(t));
const sentences: string[] = [];
if (narrowed.length > 0) {
const exceptions = narrowed
.map((t) => ({ t, clauses: crossNamespaceExceptionClauses(t) }))
.filter(({ clauses }) => clauses.length > 0);
sentences.push(
`${subject(narrowed)} one name per namespace: a package of another namespace may ship the same name` +
`${exceptions.length > 0 ? ', save the exceptions below' : ''}, but within one namespace, or beside a ` +
'package that declares no namespace, a name has one holder, and a bare name two holders share is refused ' +
'naming both.',
);
for (const { t, clauses } of exceptions) {
sentences.push(`For ${TYPE_NOUN[t]}, whatever the namespaces: ${joinList(clauses)}.`);
}
}
if (perDeployment.length > 0) {
sentences.push(
`${subject(perDeployment)} one name per deployment: ` +
`${joinList(perDeployment.map((t) => CONSUMER_CLAUSE[t]))}, with no package to tell two definitions ` +
'apart, so with two holders every caller is served the same one, chosen by registration order.',
);
}
return sentences.join(' ');
}

/** A stable identity for de-duplicating holders. */
Expand Down
22 changes: 14 additions & 8 deletions packages/objectql/src/hook-binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,21 @@ export function bindHooksToEngine(
// named 'x' is registered", naming the one thing the author had actually
// done.
if (opts.functions && typeof (engine as any).registerFunction === 'function') {
// [#30803] One name, one package holder for a function
// (`bare-name-one-holder.ts`, phase 1 of ADR-0150): once the cold boot
// has sealed the registry a bind bringing a function another package
// holds is refused here, ahead of every registration below, naming both
// holders. Before the seal the claims are only recorded, for the cold
// boot to read. The engine keeps every holder of a name
// [#30803] The bare-name one-holder rule for a function
// (`bare-name-one-holder.ts`), narrowed to the namespace (ADR-0150 D2/D9:
// `function` is in `NAMESPACE_SCOPED_BARE_NAME_TYPES`). Before the seal
// the claims are only recorded, for the cold boot to read. Once the cold
// boot has sealed the registry, `claimBareNameFunctions` throws a
// `BareNameConflictError` naming both holders when this bind brings a
// function a package of its own namespace, a package with no namespace,
// or the platform already holds; a package of another namespace may hold
// the same name. The throw comes ahead of every registration below and is
// not caught here: the runtime's `bindAppArtifactHandlers` catches it per
// owner group and logs "Failed to bind declarative hooks" at `error`, so
// the package still installs and every function and hook of that owner
// group stays unbound. The engine keeps every holder of a name
// (`functionHoldersOf`) and a flow `script` node resolves it in the
// calling flow's package (#32073); this refusal stays until the trailing
// backstop child (ADR-0150 D9) narrows it.
// calling flow's package (#32073).
const claimable = Object.entries(opts.functions)
.filter(([, entry]) => normalizeFlowFunctionEntry(entry) !== undefined)
.map(([name]) => name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,18 +561,45 @@ describe('what the AI family leaves alone', () => {
});
});

describe('the refusal\'s reason names the consumers of the types it lists', () => {
describe('the refusal\'s reason states the rule of the types it lists', () => {
it('one sentence per refusal, naming only the types it carries', async () => {
const { register } = await boot();
expect(await register(pkg('com.acme.alpha', { agent: ['a1'], skill: ['s1'] }))).toBeUndefined();
const err = await register(rival('agent', 'com.acme.beta', { agent: ['a1'], skill: ['s1'] }));
expect(err!.conflicts!.map((c) => `${c.type}/${c.name}`)).toEqual(['agent/a1', 'skill/s1']);
expect(err!.message).toContain(bareNameReason(['agent', 'skill']));
expect(bareNameReason(['agent', 'skill'])).toMatch(
/^An agent and a skill each hold one name per deployment: `\/meta\/agent\/NAME` serves one agent per name and an MCP client fetches a skill's prompt by its name, /,
);
expect(bareNameReason(['skill'])).toMatch(/^A skill holds one name per deployment: an MCP client fetches a skill's prompt by its name, /);
expect(bareNameReason(['tool'])).toMatch(/^A tool holds one name per deployment: a skill's `tools` and an MCP client name a tool, /);
expect(bareNameReason(['agent', 'skill'])).toMatch(/^An agent and a skill each hold one name per namespace: /);
expect(bareNameReason(['skill'])).toMatch(/^A skill holds one name per namespace: /);
expect(bareNameReason(['tool'])).toMatch(/^A tool holds one name per namespace: /);
});

// No covered type is held one name per deployment on main any more, so the
// per-deployment sentence is reached only by a type that joins the covered
// list before its own narrowing: simulated here by reading `tool` as not
// narrowed, in a fresh copy of the module.
it('a type not yet narrowed keeps the one-name-per-deployment sentence, after the narrowed one', async () => {
vi.resetModules();
vi.doMock('@objectstack/core', async (importOriginal) => {
const core = await importOriginal<typeof import('@objectstack/core')>();
return {
...core,
isNamespaceScopedBareNameType: (type: unknown) => type !== 'tool' && core.isNamespaceScopedBareNameType(type),
};
});
try {
const { bareNameReason: reasonWithToolUnnarrowed } = await import('./bare-name-one-holder.js');
expect(reasonWithToolUnnarrowed(['tool'])).toBe(
"A tool holds one name per deployment: a skill's `tools` and an MCP client name a tool, with no package "
+ 'to tell two definitions apart, so with two holders every caller is served the same one, chosen by '
+ 'registration order.',
);
expect(reasonWithToolUnnarrowed(['skill', 'tool'])).toMatch(
/^A skill holds one name per namespace: .+ A tool holds one name per deployment: /,
);
} finally {
vi.doUnmock('@objectstack/core');
vi.resetModules();
}
});

it('a built-in name is refused to a package at the package door, naming the platform', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,11 @@ describe('the narrowing — an email template holds one name per namespace (ADR-
const err = await register(ofBeta({ emailTemplates: [member('beta', RESET, 'zh-CN')] }));
expectRefusal(err, 'com.acme.beta', { kind: 'package', packageId: 'com.acme.alpha' }, `beta_${RESET}`);
expect(err!.message).not.toContain(EMAIL_SERVICE_PKG);
// The reason states the exception that refused it: another namespace may NOT ship this name.
expect(err!.message).toContain('may ship the same name, save the exceptions below');
expect(err!.message).toMatch(
/For an email template, whatever the namespaces: a name the platform requests by its bare spelling \([^)]*"auth\.password_reset"[^)]*\) keeps one holder per deployment/,
);
expect(await register(pkg('com.acme.beta'))).toBeUndefined();
expectRefusal(
await caught(() => registry.registerItem(type, member('beta', RESET, 'zh-CN'), 'name', 'com.acme.beta')),
Expand Down Expand Up @@ -725,7 +730,14 @@ describe('the narrowing — an email template holds one name per namespace (ADR-
it('at the package door, naming the base', async () => {
const { register } = await boot();
expect(await register(pkg('com.acme.alpha', {}, { emailTemplates: [member('alpha', name, 'en-US')] }))).toBeUndefined();
expectRefusal(await register(dependent()), 'com.acme.beta', { kind: 'package', packageId: 'com.acme.alpha' }, `beta_${name}`);
const err = await register(dependent());
expectRefusal(err, 'com.acme.beta', { kind: 'package', packageId: 'com.acme.alpha' }, `beta_${name}`);
// The reason states the exception that refused it: another namespace may NOT ship this addition.
expect(err!.message).toContain('may ship the same name, save the exceptions below');
expect(err!.message).toContain(
"a package that depends on the name's holder (which does not depend on it back) and ships the name only in "
+ "locales the holder does not ship is refused as an addition to the holder's email template",
);
});

it('at the item seam, naming the base', async () => {
Expand Down Expand Up @@ -773,7 +785,7 @@ describe('the narrowing — an email template holds one name per namespace (ADR-

});

describe('the refusal\'s reason names the consumers of the types it lists', () => {
describe('the refusal\'s reason states the rule of the types it lists', () => {
it('one sentence per refusal, naming only the types it carries', async () => {
const { register } = await boot();
expect(await register(pkg('com.acme.alpha', { flow: ['f1'], webhook: ['w1'], connector: ['c1'], email_template: ['e1'] }))).toBeUndefined();
Expand All @@ -782,9 +794,20 @@ describe('the refusal\'s reason names the consumers of the types it lists', () =
expect(err!.conflicts!.map((c) => `${c.type}/${c.name}`)).toEqual(['flow/f1', 'webhook/w1', 'connector/c1', 'email_template/e1']);
expect(err!.message).toContain(bareNameReason(['flow', 'webhook', 'connector', 'email_template']));
expect(bareNameReason(['flow', 'webhook', 'connector', 'email_template'])).toMatch(
/^A flow, a webhook, a connector and an email template each hold one name per deployment: the automation engine arms one flow per name, the webhook dispatcher subscribes one declared webhook per name, /,
/^A flow, a webhook, a connector and an email template each hold one name per namespace: /,
);
expect(bareNameReason(['email_template'])).toMatch(/^An email template holds one name per namespace: /);
});

it('a narrowed type is told the namespace rule, never one name per deployment', () => {
expect(bareNameReason(['flow'])).toBe(
'A flow holds one name per namespace: a package of another namespace may ship the same name, but within '
+ 'one namespace, or beside a package that declares no namespace, a name has one holder, and a bare name '
+ 'two holders share is refused naming both.',
);
expect(bareNameReason(['email_template'])).toMatch(/^An email template holds one name per deployment: a send names its email template, /);
// Every covered type has narrowed, so no reason says the old rule.
expect(bareNameReason()).not.toContain('one name per deployment');
expect(bareNameReason()).not.toContain('chosen by registration order');
});

it('a built-in name is refused to a package at the package door, naming the platform', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,15 +781,15 @@ describe('function — the engine function registry, through the hook binder\'s
});
});

describe('the refusal\'s reason names the consumers of the types it lists', () => {
describe('the refusal\'s reason states the rule of the types it lists', () => {
it('one sentence per refusal, naming only the types it carries', async () => {
const { register } = await boot();
expect(await register(pkg('com.acme.alpha', { api: ['a1'], action: ['r1'] }))).toBeUndefined();
const err = await register(coOwner('com.acme.alpha', 'com.acme.beta', { api: ['a1'], action: ['r1'] }));
expect(err!.conflicts!.map((c) => `${c.type}/${c.name}`)).toEqual(['api/a1', 'action/r1']);
expect(err!.message).toContain(bareNameReason(['api', 'action']));
expect(bareNameReason(['api', 'action'])).toMatch(/^An API and an object-less action each hold one name per deployment: /);
expect(bareNameReason(['function'])).toMatch(/^A function holds one name per deployment: /);
expect(bareNameReason(['api', 'action'])).toMatch(/^An API and an object-less action each hold one name per namespace: /);
expect(bareNameReason(['function'])).toMatch(/^A function holds one name per namespace: /);
});

it('a built-in name is refused to a package at the package door, naming the platform', async () => {
Expand Down
Loading