From 4801f6534758a82f9d09c63bb0489faa672f6e7b Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 08:02:23 +0000 Subject: [PATCH 1/2] refactor(core): one PackageScopedNameRefusedError class for every package-scoped by-name refusal The class moves to @objectstack/core beside PackageScopedRefusal, carrying the union of both copies' fields (code, status, httpStatus, refusal). objectql and plugin-sharing re-export the same binding under the same name, and the lean @objectstack/objectql/core entry exports it too, so one instanceof matches a refusal from either package. check-app-nav-i18n reports a refused app read as a composition finding that names every candidate instead of letting the 409 escape the gate. Claude-Session: https://claude.ai/code/session_01ADHkmQHXhv9SKTU1qQ9CNC Co-authored-by: Claude --- packages/cli/scripts/check-app-nav-i18n.mjs | 102 ++++++++++++++++-- .../src/package-scoped-resolution.test.ts | 17 +++ .../core/src/package-scoped-resolution.ts | 31 ++++++ packages/objectql/src/core.ts | 6 ++ .../src/package-scoped-e8-readers.test.ts | 25 +++++ packages/objectql/src/package-scoped-read.ts | 28 ++--- .../src/package-scoped-declarations.test.ts | 35 +++++- .../src/package-scoped-declarations.ts | 19 ++-- 8 files changed, 219 insertions(+), 44 deletions(-) diff --git a/packages/cli/scripts/check-app-nav-i18n.mjs b/packages/cli/scripts/check-app-nav-i18n.mjs index d8c2a3a1313..626c6dfc769 100644 --- a/packages/cli/scripts/check-app-nav-i18n.mjs +++ b/packages/cli/scripts/check-app-nav-i18n.mjs @@ -96,7 +96,7 @@ // removed there under #6660, and `nav_sso_providers` — labelled in #6659 // although no composition this gate boots ever merges it — is pinned as the // converse case. Neither verdict is one this gate could honestly reach. -import { existsSync } from 'node:fs'; +import { existsSync, readFileSync } from 'node:fs'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; // The exit-code contract for a refusal, from the frame that owns it — @@ -231,6 +231,43 @@ export function contributionsForApp(contributions, appName) { .map((c) => ({ source: c.source, ids: c.idsByApp?.[appName] ?? [] })); } +/** + * Read one app back from the composition: its merged nav ids, `undefined` when + * it was never registered, or — when the by-name read is REFUSED — a finding + * that names every candidate. + * + * `registry.getApp(name)` with no package is a read by a caller with no package + * (ADR-0150 D3), so a name two packages in this composition both ship is + * refused `409 RESOURCE_CONFLICT` (`PackageScopedNameRefusedError`) rather than + * answered. That is a composition finding — there is no ONE app to judge — + * and it is reported as one, beside the other composition errors. ⛔ Never + * swallowed (it would read as "not registered", whose remedy is the opposite), + * and never thrown out of the gate as a bare stack. Any other error is not a + * refusal and is rethrown untouched. + * + * `isRefusal` is the class test, handed in so `--self-test` can drive this + * without a build. + */ +export function readMergedApp(appName, getApp, isRefusal) { + let mergedApp; + try { + mergedApp = getApp(appName); + } catch (err) { + if (!isRefusal(err)) throw err; + const candidates = (err.refusal?.candidates ?? []).map((c) => + c?.kind === 'package' ? `package "${c.packageId}"` : `the ${c?.kind ?? 'unknown holder'}`, + ); + return { + refused: + `the \`${appName}\` app could not be read back: the composition holds more than one app by that name, ` + + `so a read naming no package is refused (${err.code} ${err.status}) — ${err.message} ` + + `Candidates: ${candidates.join(', ') || 'none named'}. This gate judges ONE merged app per name: ` + + `rename or remove the app in the package that should not hold it.`, + }; + } + return { ids: mergedApp ? collectNavIds(mergedApp.navigation) : undefined }; +} + /** * The pass line, as a VALUE so `--self-test` can pin it. * @@ -702,6 +739,51 @@ function selfTest() { expect('#14857 NEGATIVE CONTROL: the literal-advisory pin can still fail', spellsALiteralCode(controlLiteralAdvisory), 'the predicate no longer sees a spelled-out code'); expect('#14857 NEGATIVE CONTROL: the stale-code pin can still fail', /Exit code 1\b/.test(controlLiteralAdvisory()), 'the stale-spelling predicate no longer sees `Exit code 1`'); + // ── A REFUSED app read is a composition finding, never a throw or a miss (#31852) ── + // + // `getApp(name)` naming no package is refused when two packages ship the name + // (ADR-0150 D3). Recorded shape: the refusal class carries `code`, `status` + // and the resolver's `refusal` with every candidate. The local class stands + // in for `PackageScopedNameRefusedError` so this runs without a build. + class RefusedSample extends Error { + code = 'RESOURCE_CONFLICT'; + status = 409; + refusal = { + candidates: [ + { kind: 'package', packageId: 'com.acme.alpha' }, + { kind: 'package', packageId: 'com.acme.beta' }, + ], + }; + } + const isRefusedSample = (err) => err instanceof RefusedSample; + const refusedRead = readMergedApp('setup', () => { throw new RefusedSample('The app "setup" is held by 2 packages.'); }, isRefusedSample); + expect('#31852 a refused read is reported as a finding, not thrown', typeof refusedRead.refused === 'string', JSON.stringify(refusedRead)); + expect( + '#31852 the finding names the app and every candidate', + refusedRead.refused?.includes('`setup`') && refusedRead.refused.includes('com.acme.alpha') && refusedRead.refused.includes('com.acme.beta'), + String(refusedRead.refused), + ); + expect( + '#31852 a refused read never reads as "not registered" (an undefined id list), whose remedy is the opposite', + refusedRead.ids === undefined && !('ids' in refusedRead), + JSON.stringify(refusedRead), + ); + const mergedRead = readMergedApp('setup', () => ({ navigation: MERGED_NAV_SAMPLE }), isRefusedSample); + expect('#31852 a served read answers its merged ids', mergedRead.ids?.includes('nav_webhooks') && !mergedRead.refused, JSON.stringify(mergedRead)); + expect('#31852 an unregistered app still answers undefined ids', readMergedApp('setup', () => undefined, isRefusedSample).ids === undefined, 'undefined'); + let rethrown; + try { + readMergedApp('setup', () => { throw new TypeError('boom'); }, isRefusedSample); + } catch (err) { + rethrown = err; + } + expect('#31852 NEGATIVE CONTROL: an error that is not the refusal is rethrown, never swallowed', rethrown instanceof TypeError, String(rethrown)); + expect( + '#31852 the gate hands readMergedApp the refusal CLASS test, not a code match', + /readMergedApp\(app\.name,[^\n]*instanceof PackageScopedNameRefusedError/.test(readFileSync(fileURLToPath(import.meta.url), 'utf8')), + 'the boot read no longer passes the class test', + ); + if (failures.length) { console.error(`\ncheck-app-nav-i18n --self-test: ${failures.length} failure(s)\n`); for (const f of failures) console.error(` ${f}`); @@ -803,7 +885,7 @@ checkBuildPrerequisite(); // Boot the composition and read the merged app back. // --------------------------------------------------------------------------- -const { ObjectQL } = await import('@objectstack/objectql'); +const { ObjectQL, PackageScopedNameRefusedError } = await import('@objectstack/objectql'); const { SetupAppTranslations } = await import('@objectstack/platform-objects/apps'); const engine = new ObjectQL(); @@ -904,12 +986,13 @@ for (const contributor of CONTRIBUTORS) { * at all" have different remedies, and `collectNavIds(undefined)` answers `[]` * for both. */ -const mergedIdsByApp = new Map( - APPS.map((app) => { - const mergedApp = engine.registry.getApp(app.name); - return [app.name, mergedApp ? collectNavIds(mergedApp.navigation) : undefined]; - }), +const mergedReads = new Map( + APPS.map((app) => [ + app.name, + readMergedApp(app.name, (name) => engine.registry.getApp(name), (err) => err instanceof PackageScopedNameRefusedError), + ]), ); +const mergedIdsByApp = new Map(APPS.map((app) => [app.name, mergedReads.get(app.name).ids])); // The two verdicts are kept apart because their REMEDIES are opposites, and a // footer that prescribes one for the other is the #5862 defect (a confident @@ -928,7 +1011,10 @@ for (const app of APPS) { `ids to check.`, ); } - if (mergedIdsByApp.get(app.name) === undefined) { + const refused = mergedReads.get(app.name).refused; + if (refused) { + compositionErrors.push(refused); + } else if (mergedIdsByApp.get(app.name) === undefined) { compositionErrors.push( `the \`${app.name}\` app is not registered at all — the composition produced no app to judge. ` + `Its shell ships in \`${app.shellPackage}\`, which must be a CONTRIBUTORS entry above.`, diff --git a/packages/core/src/package-scoped-resolution.test.ts b/packages/core/src/package-scoped-resolution.test.ts index 3e3fba561be..ed73d84874a 100644 --- a/packages/core/src/package-scoped-resolution.test.ts +++ b/packages/core/src/package-scoped-resolution.test.ts @@ -16,6 +16,7 @@ import { describe, expect, it } from 'vitest'; import { ENGINE_FUNCTION_TYPE, OVERRIDABLE_BASELINE_PACKAGES, + PackageScopedNameRefusedError, RUNTIME_COMPATIBILITY_PACKAGE_ID, packageScopedHolderSource, resolvePackageScopedHolders, @@ -204,3 +205,19 @@ describe('the compatibility API\'s default package id is a caller with no packag expect(resolve([['__runtime__', 'r'], [A, 'a']], [])).toMatchObject({ status: 'refused', reason: 'ambiguous-in-environment' }); }); }); + +describe('the one refusal class every package-scoped by-name door throws (ADR-0150 D5)', () => { + it('carries the 409 RESOURCE_CONFLICT envelope under both status spellings, and the resolver\'s refusal and sentence', () => { + const refusal = resolve([[A, 'a'], [B, 'b']], []); + expect(refusal.status).toBe('refused'); + if (refusal.status !== 'refused') return; + const err = new PackageScopedNameRefusedError(refusal); + expect(err).toBeInstanceOf(Error); + expect(err.name).toBe('PackageScopedNameRefusedError'); + expect(err.code).toBe('RESOURCE_CONFLICT'); + expect(err.status).toBe(409); + expect(err.httpStatus).toBe(409); + expect(err.refusal).toBe(refusal); + expect(err.message).toBe(refusal.message); + }); +}); diff --git a/packages/core/src/package-scoped-resolution.ts b/packages/core/src/package-scoped-resolution.ts index 8d5cf5e1ea6..5626dbe9b58 100644 --- a/packages/core/src/package-scoped-resolution.ts +++ b/packages/core/src/package-scoped-resolution.ts @@ -472,6 +472,37 @@ export function packageScopedRefusalMessage(refusal: Omit { + it('package-scoped-read, the barrel and the lean `./core` entry all export core\'s class itself', () => { + expect(PackageScopedNameRefusedError).toBe(CorePackageScopedNameRefusedError); + expect(barrel.PackageScopedNameRefusedError).toBe(CorePackageScopedNameRefusedError); + expect(leanEntry.PackageScopedNameRefusedError).toBe(CorePackageScopedNameRefusedError); + }); + + it('a lean consumer matches SchemaRegistry#getApp\'s refusal by class, not only by code and status', () => { + ship('app', 'home', A); + ship('app', 'home', B); + let thrown: unknown; + try { + registry.getApp('home'); + } catch (err) { + thrown = err; + } + expect(registry).toBeInstanceOf(leanEntry.SchemaRegistry); + expect(thrown).toBeInstanceOf(leanEntry.PackageScopedNameRefusedError); + expect(thrown).toMatchObject({ code: 'RESOURCE_CONFLICT', status: 409, httpStatus: 409 }); + }); +}); + describe('narrowing (1) — an uncovered type two packages hold: the bare read refuses naming both', () => { for (const type of UNCOVERED) { it(`${type}: a caller with no package is refused at the protocol read (both faces), the registry and the facade`, async () => { diff --git a/packages/objectql/src/package-scoped-read.ts b/packages/objectql/src/package-scoped-read.ts index 3b302bbcdad..45f07d0a11c 100644 --- a/packages/objectql/src/package-scoped-read.ts +++ b/packages/objectql/src/package-scoped-read.ts @@ -50,7 +50,7 @@ * `@objectstack/metadata-core`. */ -import type { PackageScopedRefusal } from '@objectstack/core'; +import { PackageScopedNameRefusedError } from '@objectstack/core'; import { resolvePackageScopedName, type PackageScopedQuery, @@ -83,28 +83,12 @@ export function isPackageScopedReadType(type: unknown): type is PackageScopedRea } /** - * A by-name read the package-scoped resolver refused (ADR-0150 D3): the name - * means more than one item here, or only items the caller may not reach. The - * message is the resolver's own sentence, which names every candidate. - * - * The ADR-0112 envelope a wire door answers: `409 RESOURCE_CONFLICT`, the - * standard catalog's member for a name in conflict, which ADR-0150 D5 gives a - * by-name door's ambiguity. ⛔ Never the `500` fallback, and never `404`. + * The refusal a by-name read throws: `@objectstack/core`'s one class (ADR-0150 + * D5, `409 RESOURCE_CONFLICT`), re-exported under its own name so every + * existing import keeps resolving — the same binding plugin-sharing's doors + * throw, so one `instanceof` matches both. */ -export class PackageScopedNameRefusedError extends Error { - readonly code = 'RESOURCE_CONFLICT'; - readonly status = 409; - /** The same number under ADR-0112 D5's spelling — what a consumer holding the THROWN error reads. */ - readonly httpStatus = 409; - /** The resolver's refusal: the reason, the caller's package and every candidate. */ - readonly refusal: PackageScopedRefusal; - - constructor(refusal: PackageScopedRefusal) { - super(refusal.message); - this.name = 'PackageScopedNameRefusedError'; - this.refusal = refusal; - } -} +export { PackageScopedNameRefusedError }; /** * Read `(type, name)` for a caller through {@link resolvePackageScopedName}: diff --git a/packages/plugins/plugin-sharing/src/package-scoped-declarations.test.ts b/packages/plugins/plugin-sharing/src/package-scoped-declarations.test.ts index f078bd3f926..3742632bdf1 100644 --- a/packages/plugins/plugin-sharing/src/package-scoped-declarations.test.ts +++ b/packages/plugins/plugin-sharing/src/package-scoped-declarations.test.ts @@ -25,7 +25,8 @@ */ import { describe, it, expect, beforeEach } from 'vitest'; -import { SchemaRegistry } from '@objectstack/objectql'; +import { SchemaRegistry, PackageScopedNameRefusedError as ObjectQLPackageScopedNameRefusedError } from '@objectstack/objectql'; +import { PackageScopedNameRefusedError as CorePackageScopedNameRefusedError } from '@objectstack/core'; import { createDeclaredSharingRuleSource } from './declared-sharing-rules.js'; import { SharingRuleService, bindDeclaredRuleNameGuard } from './sharing-rule-service.js'; import { OwnershipQueueService } from './ownership-queue-service.js'; @@ -538,3 +539,35 @@ describe('a queue read from a caller package resolves by D3 (ADR-0150, the queue expect(await queues.queuesNaming('position', new Set(['gamma_agent']), 'org_a')).toEqual(['support']); }); }); + +describe('one refusal class: objectql\'s readers and this plugin\'s doors throw the same class (#31852)', () => { + it('this plugin re-exports @objectstack/core\'s class, the same binding objectql exports', () => { + expect(PackageScopedNameRefusedError).toBe(CorePackageScopedNameRefusedError); + expect(ObjectQLPackageScopedNameRefusedError).toBe(CorePackageScopedNameRefusedError); + }); + + it('a refusal thrown by objectql\'s by-name reader is instanceof the class this plugin exports', () => { + registry.registerItem('app', { name: 'home', label: 'alpha home' }, 'name', ALPHA); + registry.registerItem('app', { name: 'home', label: 'beta home' }, 'name', BETA); + const refused = (() => { + try { + registry.getApp('home'); + } catch (err) { + return err; + } + return undefined; + })(); + expect(refused).toBeInstanceOf(PackageScopedNameRefusedError); + expect(refused).toMatchObject({ code: 'RESOURCE_CONFLICT', status: 409 }); + }); + + it('a refusal thrown by this plugin\'s by-name door is instanceof the class objectql exports', async () => { + declare('sharing_rule', ALPHA, sharingRule('emea_share', 'alpha_agent')); + declare('sharing_rule', BETA, sharingRule('emea_share', 'beta_agent')); + const source = createDeclaredSharingRuleSource({ engine: { registry } }); + const rules = new SharingRuleService({ engine: makeEngine() as any, sharing: {} as any, declaredRules: source, logger: { warn: () => {} } }); + const refused = await rules.declaredRuleNamed('emea_share').catch((e: unknown) => e); + expect(refused).toBeInstanceOf(ObjectQLPackageScopedNameRefusedError); + expect(refused).toMatchObject({ code: 'RESOURCE_CONFLICT', status: 409 }); + }); +}); diff --git a/packages/plugins/plugin-sharing/src/package-scoped-declarations.ts b/packages/plugins/plugin-sharing/src/package-scoped-declarations.ts index adc8d2ffc39..339e4de5b59 100644 --- a/packages/plugins/plugin-sharing/src/package-scoped-declarations.ts +++ b/packages/plugins/plugin-sharing/src/package-scoped-declarations.ts @@ -67,6 +67,7 @@ * (`409 RESOURCE_CONFLICT`, ADR-0150 D5's status for ambiguity). */ +import { PackageScopedNameRefusedError } from '@objectstack/core'; import { packageScopedRefusalMessage, resolvePackageScopedName, @@ -222,20 +223,12 @@ export function servedDeclarations( /** * A bare name of a declared sharing rule, queue or assignment rule that two or * more packages hold, read where a door needs ONE: the resolver's refusal, - * with every candidate. `code` + `status` are the ADR-0112 envelope, the - * status ADR-0150 D5 gives an ambiguous bare name (`409`), under the - * conflict code this plugin's name guards already answer. + * with every candidate. It is `@objectstack/core`'s one class (`409 + * RESOURCE_CONFLICT`, ADR-0150 D5), re-exported under its own name so every + * existing import keeps resolving — the same binding objectql's by-name + * readers throw, so one `instanceof` matches both. */ -export class PackageScopedNameRefusedError extends Error { - readonly code = 'RESOURCE_CONFLICT'; - readonly status = 409; - readonly refusal: PackageScopedRefusal; - constructor(refusal: PackageScopedRefusal) { - super(refusal.message); - this.name = 'PackageScopedNameRefusedError'; - this.refusal = refusal; - } -} +export { PackageScopedNameRefusedError }; /** * The refusal a reader reports when it leaves a name unserved: once per type, From 02e922a726197a6723bf0608aee1f9e091c96ea1 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 08:04:31 +0000 Subject: [PATCH 2/2] chore: changeset for the shared package-scoped refusal class Claude-Session: https://claude.ai/code/session_01ADHkmQHXhv9SKTU1qQ9CNC Co-authored-by: Claude --- .../31852-one-package-scoped-refusal-class.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .changeset/31852-one-package-scoped-refusal-class.md diff --git a/.changeset/31852-one-package-scoped-refusal-class.md b/.changeset/31852-one-package-scoped-refusal-class.md new file mode 100644 index 00000000000..ca57a2f9e3c --- /dev/null +++ b/.changeset/31852-one-package-scoped-refusal-class.md @@ -0,0 +1,16 @@ +--- +'@objectstack/core': patch +'@objectstack/objectql': patch +'@objectstack/plugin-sharing': patch +--- + +refactor(core,objectql,plugin-sharing): one `PackageScopedNameRefusedError` class for the 409 a package-scoped by-name read refuses with + +Clause-②: yes (widening) +Metadata-protocol: none + +- **`@objectstack/core`: `PackageScopedNameRefusedError`** (new export), beside `PackageScopedRefusal` and `packageScopedRefusalMessage`. It carries `code: 'RESOURCE_CONFLICT'`, `status: 409`, `httpStatus: 409` and the resolver's `refusal`, and its message is the refusal's own sentence, which names every candidate. +- **`@objectstack/objectql`** and **`@objectstack/plugin-sharing`** each declared a class of that name before, and an `instanceof` against one never matched the other. Both now re-export core's class under the same name, so every existing import keeps resolving and one `instanceof` matches a refusal from an objectql reader (`SchemaRegistry#getApp` and the other ADR-0150 by-name readers) and from a plugin-sharing door (a declared sharing rule, queue or assignment rule name) alike. objectql's copy already carried `httpStatus`; plugin-sharing's refusal now carries it too, with the same value. +- **`@objectstack/objectql/core`** (the lean entry) now exports `PackageScopedNameRefusedError` too, since the `SchemaRegistry` it exports throws it. + +No door changes its answer: each still refuses `409 RESOURCE_CONFLICT` with the same sentence.