Skip to content

Commit 24baf39

Browse files
committed
fix(rest, service-automation): the import door and the pull also recognise a package-scoped refusal by its shape
A by-name read's refusal is recognised by class and, as the runtime's /meta door does, by shape (code RESOURCE_CONFLICT with refusal.status 'refused'), so one that crossed a realm or a second core copy is still answered as its own 409, never 404. Each reader is pinned with a plain object of that shape. The pull's comments now say the request carries callerPackageId and the pull does not read it yet; the changeset gains a TypeScript note for mapping_name_refused. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzhpDMvAM1Pvdqg4Qky
1 parent 02300a7 commit 24baf39

5 files changed

Lines changed: 87 additions & 11 deletions

File tree

‎.changeset/32071-mapping-name-by-package.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ Metadata-protocol: none
1818
1. `GET /api/v1/meta/mapping/NAME` for a name two packages hold answered `200` with the first registered package's mapping. It is now `409 RESOURCE_CONFLICT` naming both.
1919
2. An import by `mappingName` for such a name read the first holder's mapping: `200` when its target was the request's object, else `400 MAPPING_TARGET_MISMATCH` (the importer of the other package's object could never use its own mapping). It is now `409 RESOURCE_CONFLICT` naming both, and no row is written.
2020
3. A connector pull of such a name pulled through the first holder's mapping, writing into that mapping's target. It is now refused before any call or write: the pull fails with `409 RESOURCE_CONFLICT` (reason `mapping_name_refused`) naming both, and the job run records that failure.
21-
4. `GET /api/v1/meta/mapping/NAME?package=X`, `/meta/mapping/NS/NAME` and `/meta/mapping/_env/NAME`, where `X`, `NS` or the environment does not hold the name, answered `200` with the holder's mapping. They are now `409 RESOURCE_CONFLICT` naming the holder: the address is the exact qualifier (ADR-0150 D5), as it already is for apps, pages, dashboards, reports, books, analytics cubes, tools, skills and agents.
21+
4. `GET /api/v1/meta/mapping/NAME?package=X`, `/meta/mapping/NS/NAME` and `/meta/mapping/_env/NAME`, where `X`, `NS` or the environment does not hold the name, answered `200` with the holder's mapping. They are now `409 RESOURCE_CONFLICT` naming the holder: the address is the exact qualifier (ADR-0150 D5), as it already is for apps, pages, dashboards, reports, books, analytics cubes, tools, skills, agents and APIs.
2222

2323
The fix for each:
2424

2525
- FROM a bare read of a two-holder name TO `?package=<id>` or `/meta/mapping/<namespace>/NAME`.
2626
- FROM importing or pulling by a two-holder name TO renaming one of the two mappings; the import door and the pull do not yet take a package.
2727
- FROM an address naming a package that does not hold the name TO the holder the refusal names.
28+
29+
**TypeScript:** `ConnectorPullRefusalReason` (`@objectstack/service-automation`) gains the member `mapping_name_refused`. A `switch` over the union that must be exhaustive needs a case for it; code that only reads `reason` is unaffected.

‎packages/rest/src/import-integration.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { MetadataManager } from '@objectstack/metadata';
3131
import { RestServer } from './rest-server';
3232
import { loadExcelJs } from './xlsx-module.js';
3333
import { installDefaultOrganization } from './default-organization.testkit.js';
34+
import { resolveNamedMapping } from './import-mapping.js';
3435

3536
// ---------------------------------------------------------------------------
3637
// The real backend: better-sqlite3 `:memory:`, constructed the canonical way
@@ -657,6 +658,15 @@ describe('import route — named mapping artifact (#2611)', () => {
657658
expect(await engine.findOne('task', { where: { id: 'k1' } })).toBeFalsy();
658659
});
659660

661+
it('a refusal recognised by shape (a plain object, no class): answered with its own 409 and sentence, never 404', async () => {
662+
const message = `The mapping 'task_pkg_feed' is held by ${ALPHA} and ${BETA}. Nothing picks one of them: address it by its package.`;
663+
const res = await resolveNamedMapping(
664+
{ getMetaItem: () => Promise.reject({ code: 'RESOURCE_CONFLICT', status: 409, message, refusal: { status: 'refused', message } }) },
665+
{ mappingName: 'task_pkg_feed', objectName: 'task', detectedFormat: 'csv' },
666+
);
667+
expect(res).toEqual({ ok: false, status: 409, code: 'RESOURCE_CONFLICT', error: message });
668+
});
669+
660670
it('CONTROL: one package holder of a mapping into an object no package owns imports as before', async () => {
661671
shipMapping(ALPHA);
662672
const res = await call(route, { format: 'csv', csv: CSV, mappingName: 'task_pkg_feed' });

‎packages/rest/src/import-mapping.ts‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ export {
4141

4242
export type ResolveMappingResult = { ok: true; artifact: MappingArtifactLike } | MappingFailure;
4343

44+
/**
45+
* [ADR-0150 D3, #32071] The refusal a by-name read threw, or `undefined` for
46+
* any other failure. Recognised by class and, as the runtime's `/meta` door
47+
* recognises it (`isPackageScopedRefusal`), by shape: `code ===
48+
* 'RESOURCE_CONFLICT'` with a `refusal` whose `status` is `'refused'`, which
49+
* is what reaches this door when the error crossed a realm or a second copy
50+
* of `@objectstack/core` and `instanceof` cannot see it. The answer is the
51+
* error's own code, status and sentence.
52+
*/
53+
function packageScopedRefusalOf(err: unknown): { code: string; status: number; message: string } | undefined {
54+
if (err instanceof PackageScopedNameRefusedError) return { code: err.code, status: err.status, message: err.message };
55+
const thrown = err as {
56+
code?: unknown; status?: unknown; httpStatus?: unknown; message?: unknown;
57+
refusal?: { status?: unknown; message?: unknown };
58+
} | null | undefined;
59+
if (thrown?.code !== 'RESOURCE_CONFLICT' || thrown.refusal?.status !== 'refused') return undefined;
60+
const status = typeof thrown.status === 'number' ? thrown.status
61+
: typeof thrown.httpStatus === 'number' ? thrown.httpStatus : 409;
62+
const message = typeof thrown.message === 'string' && thrown.message ? thrown.message : String(thrown.refusal.message ?? '');
63+
return { code: thrown.code, status, message };
64+
}
65+
4466
/**
4567
* Resolve a named mapping artifact and check it against the request:
4668
* target object must match the URL object, and the artifact's declared
@@ -77,9 +99,8 @@ export async function resolveNamedMapping(
7799
// [#32071] The by-name read refused: the name is held by more than
78100
// one package. Answered with the refusal's own code, status and
79101
// sentence, which names every candidate.
80-
if (err instanceof PackageScopedNameRefusedError) {
81-
return { ok: false, status: err.status, code: err.code, error: err.message };
82-
}
102+
const refused = packageScopedRefusalOf(err);
103+
if (refused) return { ok: false, status: refused.status, code: refused.code, error: refused.message };
83104
/* anything else is treated as not found below */
84105
}
85106
if (!artifact || typeof artifact !== 'object' || !Array.isArray(artifact.fieldMapping)) {

‎packages/services/service-automation/src/connector-pull.test.ts‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,9 @@ describe('a failed pull logs one warn line naming the mapping and the reason', (
496496

497497
/**
498498
* [#32071, ADR-0150 D3] `mapping` is addressed by `(package, name)`, and the
499-
* pull names no package (its request carries none): the mapping is read by the
500-
* protocol's by-name read as a caller with no package. Driven here over the
499+
* pull names no package (its request carries `callerPackageId`, which the pull
500+
* does not read yet): the mapping is read by the protocol's by-name read as a
501+
* caller with no package. Driven here over the
501502
* REAL read — `ObjectStackProtocolImplementation` over a real `SchemaRegistry`
502503
* — for the mapping, the harness for everything else.
503504
*/
@@ -547,6 +548,25 @@ describe('[#32071] connector pull — a mapping name two packages hold is refuse
547548
expect(h.writes).toHaveLength(0);
548549
});
549550

551+
it('a refusal recognised by shape (a plain object, no class): refused with its own code and status, naming both', async () => {
552+
const h = harness();
553+
const message = `The mapping 'crm_contacts' is held by ${ALPHA} and ${BETA}. Nothing picks one of them: address it by its package.`;
554+
const fake = h.deps.protocol;
555+
h.deps.protocol = {
556+
...fake,
557+
getMetaItem: (request) => (request.type === 'mapping'
558+
? Promise.reject({ code: 'RESOURCE_CONFLICT', status: 409, message, refusal: { status: 'refused', message } })
559+
: fake.getMetaItem(request)),
560+
};
561+
const err = await refusal(h);
562+
expect(err.reason).toBe('mapping_name_refused');
563+
expect({ code: err.code, status: err.status }).toEqual({ code: 'RESOURCE_CONFLICT', status: 409 });
564+
expect(err.message).toContain(ALPHA);
565+
expect(err.message).toContain(BETA);
566+
expect(h.handler).not.toHaveBeenCalled();
567+
expect(h.writes).toHaveLength(0);
568+
});
569+
550570
it('CONTROL: one package holder is pulled exactly as before', async () => {
551571
const h = overRealMappingRead([ALPHA]);
552572
const result = await pullConnectorSource(h.deps, { mapping: 'crm_contacts' });

‎packages/services/service-automation/src/connector-pull.ts‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,28 @@ function shapeOf(value: unknown): string {
222222
return typeof value === 'object' ? 'an object' : `a ${typeof value}`;
223223
}
224224

225+
/**
226+
* [ADR-0150 D3, #32071] The refusal a by-name read threw, or `undefined` for
227+
* any other failure. Recognised by class and, as the runtime's `/meta` door
228+
* recognises it (`isPackageScopedRefusal`), by shape: `code ===
229+
* 'RESOURCE_CONFLICT'` with a `refusal` whose `status` is `'refused'`, which
230+
* is what reaches the pull when the error crossed a realm or a second copy of
231+
* `@objectstack/core` and `instanceof` cannot see it. The answer is the
232+
* error's own code, status and sentence.
233+
*/
234+
function packageScopedRefusalOf(err: unknown): { code: string; status: number; message: string } | undefined {
235+
if (err instanceof PackageScopedNameRefusedError) return { code: err.code, status: err.status, message: err.message };
236+
const thrown = err as {
237+
code?: unknown; status?: unknown; httpStatus?: unknown; message?: unknown;
238+
refusal?: { status?: unknown; message?: unknown };
239+
} | null | undefined;
240+
if (thrown?.code !== 'RESOURCE_CONFLICT' || thrown.refusal?.status !== 'refused') return undefined;
241+
const status = typeof thrown.status === 'number' ? thrown.status
242+
: typeof thrown.httpStatus === 'number' ? thrown.httpStatus : 409;
243+
const message = typeof thrown.message === 'string' && thrown.message ? thrown.message : String(thrown.refusal.message ?? '');
244+
return { code: thrown.code, status, message };
245+
}
246+
225247
/**
226248
* Pull one `mapping`'s `connectorSource` and write what it returns. Throws
227249
* {@link ConnectorPullError} on every refusal, before anything is written.
@@ -273,18 +295,19 @@ async function pullOnce(
273295
//
274296
// [ADR-0150 D3, #32071] `mapping` is addressed by `(package, name)`, and
275297
// this read names no package: the pull is a caller with no package until
276-
// the job's package reaches it (the pull request carries none). One
277-
// holder is read as before; a name two packages hold is refused by the
278-
// read naming both, and the pull is refused with that refusal's own code,
298+
// it reads the job's package (the request carries `callerPackageId`, but
299+
// the pull does not read it yet). One holder is read as before; a name
300+
// two packages hold is refused by the read naming both, and the pull is refused with that refusal's own code,
279301
// status and sentence — ⛔ never as "no mapping is registered", and never
280302
// by pulling through whichever package's mapping came first.
281303
let artifact: PulledMapping | undefined;
282304
try {
283305
const res = await protocol.getMetaItem({ type: 'mapping', name: mappingName }) as Record<string, unknown> | undefined;
284306
artifact = res?.item as PulledMapping | undefined;
285307
} catch (err) {
286-
if (err instanceof PackageScopedNameRefusedError) {
287-
return refuse(err.code, err.status, 'mapping_name_refused', `Mapping "${mappingName}": ${err.message}`);
308+
const refused = packageScopedRefusalOf(err);
309+
if (refused) {
310+
return refuse(refused.code, refused.status, 'mapping_name_refused', `Mapping "${mappingName}": ${refused.message}`);
288311
}
289312
/* anything else is treated as not found below */
290313
}

0 commit comments

Comments
 (0)