Skip to content

Commit a537238

Browse files
committed
fix(runtime)!: a kernel-shipped permission set's copy lands environment-wide, the kernel having no pair
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibzCo6pa7SSUFmosQD8
1 parent 6c336ba commit a537238

5 files changed

Lines changed: 83 additions & 12 deletions

File tree

‎packages/metadata-protocol/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export { unmanagedInstallPostureRefusal } from './package-unmanaged-install.js';
272272
// install primitive and the duplicate ask it; exported so `POST /packages`
273273
// (`@objectstack/runtime`) asks the same one ahead of both its install limbs,
274274
// its no-protocol fallback included, instead of keeping a second copy.
275-
export { customizationPairManifestRefusal } from './protocol-customization-package.js';
275+
export { customizationPairManifestRefusal, kernelPackageScope } from './protocol-customization-package.js';
276276

277277
// [ADR-0029 D7] The refusal of `navigationContributions` on a package created
278278
// at runtime, whose install never registers them. The install primitive asks

‎packages/metadata-protocol/src/protocol-customization-package.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ function kernelPackageScopeOf(row: PairPackageRow | undefined): string | undefin
165165
return typeof scope === 'string' && READ_ONLY_PACKAGE_SCOPES.includes(scope) ? scope : undefined;
166166
}
167167

168+
/**
169+
* [ruling record 6691217245] The kernel scope of `packageId` as `engine`'s
170+
* registry holds it ({@link kernelPackageScopeOf}), or `undefined` for a
171+
* package that is not a kernel package or is not installed. The door's own
172+
* reading, for the callers that land something in a package's pair and must
173+
* know beforehand that a kernel package has none (the "new similar" doors).
174+
*/
175+
export function kernelPackageScope(engine: unknown, packageId: string): string | undefined {
176+
const registry = (engine as { registry?: Partial<PairRegistry> } | null | undefined)?.registry;
177+
if (!registry || typeof registry.getPackage !== 'function') return undefined;
178+
return kernelPackageScopeOf(registry.getPackage(packageId));
179+
}
180+
168181
/**
169182
* The calls in flight, per registry and per pair id — see the module header,
170183
* "Idempotent, and one pair under concurrency". Keyed weakly by the registry,

‎packages/qa/dogfood/test/permission-set-clone-door.dogfood.test.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,24 @@ describe('the permission-set clone door, on a booted showcase', () => {
228228
}
229229
});
230230

231-
it('a platform set (the lock-and-clone rule\'s commonest source) copies into its shipping package\'s pair', async () => {
231+
// [#29594, ruling record 6691217245] A kernel package has no paired
232+
// customization package, so a platform set's copy is saved where an
233+
// environment-authored set's copy is: environment-wide, the notice naming
234+
// the kernel package and `package`.
235+
it('a platform set (the lock-and-clone rule\'s commonest source) copies environment-wide: the kernel has no pair', async () => {
232236
const reader = securityCatalogReaderOf(ql)!;
233237
const source = await reader.resolve('permission', 'member_default');
234238
expect(source?.packageId, 'member_default is not packaged on this boot').toBeTruthy();
235239

236240
const res = await clone(token, 'member_default', { name: 'my_member_baseline', label: 'My Member Baseline' });
237241
const text = await res.clone().text();
238242
expect(res.status, text).toBe(200);
239-
const landed = ((await res.json()) as any).data.package;
240-
expect(landed).toBe(`${source!.packageId}.custom`);
241-
expect((await reader.resolve('permission', 'my_member_baseline'))?.packageId).toBe(landed);
243+
const data = ((await res.json()) as any).data;
244+
expect(data.package).toBeNull();
245+
expect(data.noticeCode).toBe('PERMISSION_SET_CLONE_ENVIRONMENT_WIDE_KERNEL_SOURCE');
246+
expect(data.notice).toContain(`kernel package '${source!.packageId}'`);
247+
expect((await reader.resolve('permission', 'my_member_baseline'))?.packageId).toBeUndefined();
248+
expect((await stack.apiAs(token, 'GET', `/packages/${source!.packageId}.custom`)).status).toBe(404);
242249
});
243250

244251
it('CONTROL — an environment-authored source still copies environment-wide, with no package', async () => {

‎packages/runtime/src/domains/permission-set-clone.test.ts‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ const SYS_ONLY_SET = { name: 'sys_only', label: 'Sys Only', objects: { sys_user:
6767
/** A set `crm` ships that grants on a platform object only: its package is the one thing out of an island's reach. */
6868
const CRM_SYS_SET = { name: 'crm_user_reader', label: 'CRM User Reader', objects: { sys_user: { allowRead: true } }, _packageId: 'crm' };
6969

70-
const CATALOG: TestSecurityCatalog = { permissions: [PACKAGED, ENV_SET, OWNED_SET, SYS_ONLY_SET, CRM_SYS_SET] };
70+
/** [#29594] A set the platform's kernel ships (scope `system`): the commonest lock-and-clone source. */
71+
const KERNEL = 'com.objectstack.plugin-security';
72+
const KERNEL_SET = { name: 'member_default', label: 'Member', objects: { account: { allowRead: true } }, _packageId: KERNEL, _lock: 'full' };
73+
const CATALOG: TestSecurityCatalog = { permissions: [PACKAGED, ENV_SET, OWNED_SET, SYS_ONLY_SET, CRM_SYS_SET, KERNEL_SET] };
7174

7275
/**
7376
* The package registry the door reads: `crm` is a booted CODE package
@@ -82,6 +85,7 @@ const PACKAGES: Record<string, { manifest: Record<string, unknown> }> = {
8285
'com.acme.ext': { manifest: { id: 'com.acme.ext', dependencies: { crm: '^1' } } },
8386
'com.acme.island': { manifest: { id: 'com.acme.island' } },
8487
'com.vendor.app': { manifest: { id: 'com.vendor.app', scope: 'cloud' } },
88+
[KERNEL]: { manifest: { id: KERNEL, namespace: 'sys', version: '18.3.0', scope: 'system' } },
8589
};
8690

8791
interface Saved { type: string; name: string; item: Record<string, unknown>; parentVersion?: string | null; packageId?: string; inheritedGrantObjects?: readonly string[] }
@@ -238,6 +242,22 @@ describe('the permission-set clone door — success', () => {
238242
expect((res as any).response.body.data.notice).toBe(permissionSetCloneNotice({ packageId: null, created: false }));
239243
});
240244

245+
it('[#29594] a set a kernel package ships copies environment-wide, with no ensure: the kernel has no pair', async () => {
246+
const h = boot();
247+
248+
const res = await clone(h, HOLDER(), KERNEL_SET.name, { name: 'my_member', label: 'My Member' });
249+
250+
expect(statusOf(res)).toBe(200);
251+
expect(h.ensures).toEqual([]);
252+
expect(h.saves[0]).not.toHaveProperty('packageId');
253+
expect(h.saves[0].item).toEqual({ name: 'my_member', label: 'My Member', objects: KERNEL_SET.objects });
254+
const data = (res as any).response.body.data;
255+
expect(data.package).toBeNull();
256+
expect(data.noticeCode).toBe('PERMISSION_SET_CLONE_ENVIRONMENT_WIDE_KERNEL_SOURCE');
257+
expect(data.notice).toBe(permissionSetCloneNotice({ packageId: null, created: false, kernelSource: KERNEL }));
258+
expect(data.notice).toContain(`kernel package '${KERNEL}'`);
259+
});
260+
241261
it('a set already in a writable package copies into that package, with no ensure', async () => {
242262
const h = boot();
243263

@@ -331,7 +351,7 @@ describe('the permission-set clone door — authority', () => {
331351
});
332352

333353
describe('[#29727] the clone notice is named by one stable code per landing', () => {
334-
// CONTROL: the three sentences, spelled out as they were served before the
354+
// CONTROL: the sentences (the fourth since #29594), spelled out as they were served before the
335355
// codes arrived, so a change to `notice` while adding a code reds here.
336356
const TAIL =
337357
'The copy is a new permission set authored in this system, with no link to the set it was copied '
@@ -344,6 +364,13 @@ describe('[#29727] the clone notice is named by one stable code per landing', ()
344364
code: 'PERMISSION_SET_CLONE_ENVIRONMENT_WIDE',
345365
notice: 'The copy was saved environment-wide, in no package, as the set it was copied from is. ' + TAIL,
346366
},
367+
{
368+
landing: { packageId: null, created: false, kernelSource: 'com.objectstack.plugin-security' },
369+
code: 'PERMISSION_SET_CLONE_ENVIRONMENT_WIDE_KERNEL_SOURCE',
370+
notice: "The copy was saved environment-wide, in no package: the set it was copied from is shipped by kernel "
371+
+ "package 'com.objectstack.plugin-security', which has no paired customization package in this version. "
372+
+ 'Name a writable package with `package` to keep the copy in it instead. ' + TAIL,
373+
},
347374
{
348375
landing: { packageId: 'com.acme.base', created: false },
349376
code: 'PERMISSION_SET_CLONE_IN_PACKAGE',
@@ -363,9 +390,9 @@ describe('[#29727] the clone notice is named by one stable code per landing', ()
363390
expect(permissionSetCloneNotice(landing)).toBe(notice);
364391
});
365392

366-
it('the three codes are distinct and the table holds exactly them', () => {
393+
it('the four codes are distinct and the table holds exactly them', () => {
367394
expect(Object.values(PERMISSION_SET_CLONE_NOTICE_CODES).sort()).toEqual(VARIANTS.map((v) => v.code).sort());
368-
expect(new Set(VARIANTS.map((v) => permissionSetCloneNotice(v.landing))).size).toBe(3);
395+
expect(new Set(VARIANTS.map((v) => permissionSetCloneNotice(v.landing))).size).toBe(4);
369396
});
370397
});
371398

‎packages/runtime/src/domains/permission-set-clone.ts‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@
7474
* grants on; the one check both "new similar" doors ask
7575
* (`newSimilarTargetRefusal`, `./new-similar-target.ts`), else refused with
7676
* nothing written;
77+
* - **omitted, the source shipped by a kernel package** (scope `system` /
78+
* `cloud`: the platform's own sets, `member_default` among them) — the
79+
* save is environment-wide, as for a source authored in the environment:
80+
* a kernel package has no paired customization package in this version
81+
* (ruling record 6691217245), and the copy is a new set, not a change to
82+
* the kernel's. The notice says why, and that `package` keeps the copy in
83+
* a package the caller names;
7784
* - **omitted, the source shipped by a read-only package X** — X's paired
7885
* customization package `<X>.custom`, answered by N-13's ensure door
7986
* (`ensureCustomizationPackage`, `@objectstack/metadata-protocol`), the
@@ -110,7 +117,7 @@ import { SnakeCaseIdentifierSchema } from '@objectstack/spec/shared';
110117
import { metaWriteCapabilityVerdict, walledCatalogWriteVerdict } from '@objectstack/metadata-core';
111118
import { readEffectiveTenancyPosture } from './activation-gate.js';
112119
import { securityCatalogReaderOf, type SecurityCatalogEntry } from '@objectstack/core';
113-
import { isWritablePackage, type ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol';
120+
import { isWritablePackage, kernelPackageScope, type ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol';
114121
import { newSimilarTargetRefusal, newSimilarTargetRefusalAnswer } from './new-similar-target.js';
115122
import { validationFailure } from '../validation-failure.js';
116123
import type { HttpProtocolContext, HttpDispatcherResult } from '../http-dispatcher.js';
@@ -158,6 +165,11 @@ export const PERMISSION_SET_CLONE_NOTICE =
158165
export interface PermissionSetCloneLanding {
159166
readonly packageId: string | null;
160167
readonly created: boolean;
168+
/**
169+
* The kernel package that shipped the source, when the copy went
170+
* environment-wide because that package has no pair; absent otherwise.
171+
*/
172+
readonly kernelSource?: string;
161173
}
162174

163175
/**
@@ -173,16 +185,17 @@ export interface PermissionSetCloneLanding {
173185
*/
174186
export const PERMISSION_SET_CLONE_NOTICE_CODES = Object.freeze({
175187
environmentWide: 'PERMISSION_SET_CLONE_ENVIRONMENT_WIDE',
188+
environmentWideKernelSource: 'PERMISSION_SET_CLONE_ENVIRONMENT_WIDE_KERNEL_SOURCE',
176189
inPackage: 'PERMISSION_SET_CLONE_IN_PACKAGE',
177190
inCreatedPackage: 'PERMISSION_SET_CLONE_IN_CREATED_PACKAGE',
178191
} as const);
179192

180-
/** Which of the three notices a landing is told with. */
193+
/** Which of the four notices a landing is told with. */
181194
export type PermissionSetCloneLandingKind = keyof typeof PERMISSION_SET_CLONE_NOTICE_CODES;
182195

183196
/** The one place a landing is sorted into its notice variant. */
184197
export function permissionSetCloneLandingKind(landing: PermissionSetCloneLanding): PermissionSetCloneLandingKind {
185-
if (landing.packageId === null) return 'environmentWide';
198+
if (landing.packageId === null) return landing.kernelSource !== undefined ? 'environmentWideKernelSource' : 'environmentWide';
186199
return landing.created ? 'inCreatedPackage' : 'inPackage';
187200
}
188201

@@ -196,6 +209,12 @@ export function permissionSetCloneNotice(landing: PermissionSetCloneLanding): st
196209
return 'The copy was saved environment-wide, in no package, as the set it was copied from is. '
197210
+ PERMISSION_SET_CLONE_NOTICE;
198211
}
212+
if (kind === 'environmentWideKernelSource') {
213+
return `The copy was saved environment-wide, in no package: the set it was copied from is shipped by kernel `
214+
+ `package '${landing.kernelSource}', which has no paired customization package in this version. Name a `
215+
+ 'writable package with `package` to keep the copy in it instead. '
216+
+ PERMISSION_SET_CLONE_NOTICE;
217+
}
199218
return `The copy was saved in package '${landing.packageId}'`
200219
+ (kind === 'inCreatedPackage' ? ', created for it to hold your changes to the package the set came from' : '')
201220
+ ', beside the original rather than in place of it, and travels with that package. '
@@ -297,6 +316,11 @@ export async function resolveDefaultCloneLanding(
297316
const sourcePackageId = source.packageId;
298317
if (sourcePackageId === undefined) return { packageId: null, created: false };
299318
if (isWritablePackage(ql, sourcePackageId)) return { packageId: sourcePackageId, created: false };
319+
// [ruling record 6691217245] A kernel package has no pair: the copy of
320+
// its set is a new set, saved where an environment-authored set's is.
321+
if (kernelPackageScope(ql, sourcePackageId) !== undefined) {
322+
return { packageId: null, created: false, kernelSource: sourcePackageId };
323+
}
300324
if (typeof protocol.ensureCustomizationPackage !== 'function') {
301325
throw Object.assign(
302326
new Error(

0 commit comments

Comments
 (0)