Skip to content

Commit ee6fa50

Browse files
committed
fix(metadata-protocol): the layered read naming no package refuses a name two served packages register, as the plain read does (ADR-0150 D3)
getMetaItemLayered now asks the registry's package-scoped resolver for a type addressed by package, as getMetaItem does, before any layer is read: a name two or more served packages hold is refused naming each (PackageScopedNameRefusedError, 409 RESOURCE_CONFLICT), whatever the metadata service holds. Its code layer used to fall back to the artifact lookup's first composite, so the read in process, and the REST /layers door on a host with no engine wired, answered the holder that registered first. A name one holder holds keeps its layers; stored-row holders stay the door's step 1c. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ
1 parent 0571f19 commit ee6fa50

2 files changed

Lines changed: 126 additions & 0 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,21 @@ export async function getMetaItemLayered(host: Pick<ProtocolHost, 'artifactLockL
250250
? await judgeBareActionRead(host, request.name)
251251
: undefined;
252252
const exactAddress = await admitExactQualifier(host, { type: request.type, name: request.name, packageId: request.packageId, member });
253+
// [ADR-0150 D3, #32914] …and, naming no package, a type addressed by
254+
// package is judged by the registry's package-scoped resolver, as the
255+
// plain read judges it ({@link getMetaItem}, its `packagelessScopedRead`):
256+
// a name two or more served packages hold is refused naming each
257+
// (`PackageScopedNameRefusedError`, `409 RESOURCE_CONFLICT` with its
258+
// candidates), before any layer is read and whatever the metadata service
259+
// holds. The code layer below otherwise fell back to the artifact lookup's
260+
// first composite, so the read answered the holder that registered first.
261+
// Asked for the judgment only: a name one holder holds keeps the layers
262+
// below as they were. [contract review S1] Stored-row holders are not
263+
// this judgment's: the door refuses them through
264+
// {@link packagelessLayeredReadRefusal}, its step 1c.
265+
if (!request.packageId && host.engine?.registry?.readsByPackage?.(request.type) === true) {
266+
host.engine.registry.resolveItem(request.type, request.name, null, member);
267+
}
253268

254269
// [ADR-0131 D6] Environment → code: the overlay layer is the
255270
// environment's stored row; no organization's row is read.

‎packages/rest/src/meta-layers-ambiguous-package.test.ts‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,114 @@ describe('[#28337] RestServer: the layered view of a name two packages hold, nam
221221
}, 60_000);
222222
});
223223

224+
/**
225+
* [#32622, #32914, ADR-0150 D3/D5] A name two packages REGISTER (code, no
226+
* stored row), read naming no package. The plain read refuses it
227+
* `409 RESOURCE_CONFLICT` naming both as `details.candidates` (#32201). The
228+
* layered read answered two ways by composition: with an engine wired, the
229+
* door's owner step asked the plain read first and answered its refusal by the
230+
* sentence alone; with none, the door served the holder that registered first,
231+
* and so did the protocol's layered read in process, whatever the metadata
232+
* service held. Now the layered read itself refuses as the plain read does,
233+
* and the door carries the candidates, on both spellings.
234+
*/
235+
describe('[#32622, #32914] a name two packages register, read naming no package', () => {
236+
const ORDERS = [['alpha first', [PKG_A, PKG_B]], ['beta first', [PKG_B, PKG_A]]] as const;
237+
/** The composition the door runs in: an engine wired for the door chain or not, and the metadata service's copy. */
238+
const SHAPES = [
239+
['engine wired, no service copy', { wired: true, copy: false }],
240+
['no engine wired, no service copy', { wired: false, copy: false }],
241+
['no engine wired, a service copy of the first holder', { wired: false, copy: true }],
242+
] as const;
243+
244+
async function registered(order: readonly string[], shape: { wired: boolean; copy: boolean }) {
245+
// The partial copy: a `metadata` service holding the first holder's body only.
246+
const copyOf = (pkg: string) => ({ name: 'sales', label: `${pkg} code`, widgets: [], _packageId: pkg });
247+
const services = shape.copy
248+
? new Map<string, unknown>([['metadata', { get: async (type: string, name: string) => (type === 'dashboard' && name === 'sales' ? copyOf(order[0]) : undefined) }]])
249+
: undefined;
250+
const h = makeHarness(services);
251+
for (const pkg of order) {
252+
h.registry.installPackage({ id: pkg, name: pkg, version: '1.0.0' } as any, 'code');
253+
h.registry.registerItem('dashboard', { name: 'sales', label: `${pkg} code`, widgets: [] } as any, 'name' as any, pkg);
254+
}
255+
h.registry.installPackage({ id: 'app.solo_demo', name: 'app.solo_demo', version: '1.0.0' } as any, 'code');
256+
h.registry.registerItem('dashboard', { name: 'solo', label: 'solo code', widgets: [] } as any, 'name' as any, 'app.solo_demo');
257+
const server = {
258+
get: vi.fn(), post: vi.fn(), put: vi.fn(), delete: vi.fn(), patch: vi.fn(), use: vi.fn(),
259+
listen: vi.fn().mockResolvedValue(undefined), close: vi.fn().mockResolvedValue(undefined),
260+
};
261+
const rest = new RestServer(server as any, h.protocol as any, { api: { requireAuth: false } } as any);
262+
(rest as any).resolveExecCtx = async () => AUTHOR;
263+
if (shape.wired) (rest as any).objectQLProvider = async () => ({ registry: h.registry });
264+
rest.registerRoutes();
265+
const get = async (pattern: string, path: string, name: string, query: Record<string, string> = {}) => {
266+
const route = rest.getRoutes().find((r: any) => r.method === 'GET' && r.path === `${META}${pattern}`);
267+
if (!route) throw new Error(`GET ${pattern} is not registered`);
268+
const res = makeRes();
269+
await route.handler({ method: 'GET', path: `${META}${path}`, params: { type: 'dashboard', name }, query, body: {}, headers: {} }, res);
270+
return { status: res.statusCode as number, body: res.body };
271+
};
272+
return {
273+
...h,
274+
plain: (name: string) => get('/:type/:name', `/dashboard/${name}`, name),
275+
spellings: (name: string, query: Record<string, string> = {}) => [
276+
['/layers', () => get('/:type/:name/layers', `/dashboard/${name}/layers`, name, query)],
277+
['?layers=true', () => get('/:type/:name', `/dashboard/${name}`, name, { ...query, layers: 'true' })],
278+
] as const,
279+
};
280+
}
281+
282+
const candidatesOf = (body: any) => [...(body?.details?.candidates ?? [])].map((c: any) => `${c.kind}:${c.packageId}`).sort();
283+
const BOTH = [`package:${PKG_A}`, `package:${PKG_B}`];
284+
285+
for (const [shapeLabel, shape] of SHAPES) {
286+
it.each(ORDERS)(`${shapeLabel}, %s: both spellings answer the plain read's 409 with details.candidates, serving neither holder`, async (_label, order) => {
287+
const host = await registered(order, shape);
288+
const plain = await host.plain('sales');
289+
expect([plain.status, codeOf(plain)], JSON.stringify(plain.body)).toEqual([409, 'RESOURCE_CONFLICT']);
290+
expect(candidatesOf(plain.body)).toEqual(BOTH);
291+
for (const [spelling, read] of host.spellings('sales')) {
292+
const refused = await read();
293+
expect([refused.status, codeOf(refused)], `${spelling}: ${JSON.stringify(refused.body)}`).toEqual([409, 'RESOURCE_CONFLICT']);
294+
expect(candidatesOf(refused.body), spelling).toEqual(BOTH);
295+
for (const pkg of order) expect(JSON.stringify(refused.body), spelling).not.toContain(`${pkg} code`);
296+
}
297+
}, 60_000);
298+
}
299+
300+
it.each(ORDERS)('in process, %s: the layered read refuses naming both, with or without a service copy', async (_label, order) => {
301+
for (const [shapeLabel, shape] of SHAPES) {
302+
const host = await registered(order, shape);
303+
const refused: any = await host.protocol.getMetaItemLayered({ type: 'dashboard', name: 'sales' }).then(
304+
(served) => ({ served }),
305+
(error) => error,
306+
);
307+
expect([refused?.code, refused?.status], `${shapeLabel}: ${JSON.stringify(refused?.served ?? refused?.message)}`)
308+
.toEqual(['RESOURCE_CONFLICT', 409]);
309+
expect([...(refused?.refusal?.candidates ?? [])].map((c: any) => `${c.kind}:${c.packageId}`).sort(), shapeLabel).toEqual(BOTH);
310+
// Each holder's layers stay readable at its package.
311+
for (const pkg of order) {
312+
const scoped: any = await host.protocol.getMetaItemLayered({ type: 'dashboard', name: 'sales', packageId: pkg });
313+
expect(scoped.code?.label, `${shapeLabel}, ${pkg}`).toBe(`${pkg} code`);
314+
}
315+
}
316+
}, 60_000);
317+
318+
it('CONTROL: a name one package registers is served, in process and on both spellings', async () => {
319+
for (const [shapeLabel, shape] of SHAPES) {
320+
const host = await registered([PKG_A, PKG_B], shape);
321+
const layered: any = await host.protocol.getMetaItemLayered({ type: 'dashboard', name: 'solo' });
322+
expect(layered.effective?.label, shapeLabel).toBe('solo code');
323+
for (const [spelling, read] of host.spellings('solo')) {
324+
const served = await read();
325+
expect([served.status, served.body?.effective?.label], `${shapeLabel} ${spelling}: ${JSON.stringify(served.body)}`)
326+
.toEqual([200, 'solo code']);
327+
}
328+
for (const [spelling, read] of host.spellings('sales', { package: PKG_B })) {
329+
const served = await read();
330+
expect([served.status, served.body?.code?.label], `${shapeLabel} ${spelling} ?package=`).toEqual([200, `${PKG_B} code`]);
331+
}
332+
}
333+
}, 60_000);
334+
});

0 commit comments

Comments
 (0)