Skip to content

Commit 9ceb1f4

Browse files
committed
wip: ensure door answers the App pair; reads serve legacy per-package pairs
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017ihL2dM9PvPPB9BMVdhf2R
1 parent f1dfda0 commit 9ceb1f4

14 files changed

Lines changed: 227 additions & 89 deletions

‎packages/lint/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
},
5252
"dependencies": {
5353
"@objectstack/formula": "workspace:*",
54+
"@objectstack/metadata-core": "workspace:*",
5455
"@objectstack/sdui-parser": "workspace:*",
5556
"@objectstack/spec": "workspace:*",
5657
"ajv": "^8.20.0",

‎packages/lint/src/validate-customization-namespace.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
* customization namespace — so its installers can never customize it — is
66
* told so while it is still being authored.
77
*
8-
* Every installed base gets one paired writable package, `<base>.custom`, the
9-
* first time an installer customizes it; its namespace is the base's followed
8+
* Every installed App gets one paired writable package, `<app id>.custom`,
9+
* the first time an installer customizes any of its packages (ADR-0146 D3 as
10+
* amended, ruling record 6691022506); its namespace is the App's followed
1011
* by a suffix, and a base namespace too long for that suffix gets NO pair
1112
* namespace (the #28404 ruling, point 1: a named refusal, no truncation and no
1213
* hashing). The ensure door then refuses to create the pair, so the author of
@@ -36,6 +37,10 @@
3637
* - A package that is itself a pair (`isCustomizationPackage`, the one
3738
* spelling of "some base derives this id"): a pair is never paired again, and its
3839
* namespace is the pair namespace, longer than a base's by construction.
40+
* - A member of a delivered App (`appOf`, `@objectstack/metadata-core`, read
41+
* over the delivered manifests): its customizations land in the App's pair,
42+
* whose namespace is the App's, so its own namespace has no pair to fit —
43+
* the App's row is judged instead.
3944
* - The helper's other two refusals. A missing namespace is not this rule's
4045
* question, and a namespace the manifest grammar refuses for another reason
4146
* (an uppercase letter, a leading digit) is `os validate`'s refusal, with
@@ -55,6 +60,7 @@ import {
5560
customizationNamespace,
5661
isCustomizationPackage,
5762
} from '@objectstack/spec/kernel';
63+
import { appOf, installedManifestsOf } from '@objectstack/metadata-core';
5864

5965
import type { AnyRec } from './page-walk.js';
6066

@@ -104,8 +110,16 @@ export function validateCustomizationNamespace(stack: AnyRec): CustomizationName
104110
const findings: CustomizationNamespaceFinding[] = [];
105111
if (!isRec(stack)) return findings;
106112

107-
for (const { manifest, at } of deliveredManifests(stack)) {
113+
const delivered = deliveredManifests(stack);
114+
const installed = installedManifestsOf({
115+
getPackage: (id: string) => {
116+
const found = delivered.find(({ manifest }) => manifest.id === id);
117+
return found ? { manifest: found.manifest } : undefined;
118+
},
119+
});
120+
for (const { manifest, at } of delivered) {
108121
if (isCustomizationPackage(manifest.id)) continue;
122+
if (typeof manifest.id === 'string' && manifest.id !== '' && appOf(manifest.id, installed) !== manifest.id) continue;
109123

110124
const pairNamespace = customizationNamespace(manifest.namespace);
111125
if (pairNamespace.ok || pairNamespace.reason !== 'base_namespace_too_long') continue;

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

Lines changed: 75 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
*
88
* ## The door
99
*
10+
* The unit is the App (ADR-0146 D3 as amended, ruling record 6691022506): the
11+
* pair of a base is the pair of the App that carries it ({@link pairOf},
12+
* `@objectstack/metadata-core`), so the door asked for any package of an App
13+
* answers that App's one pair, `<app id>.custom` — creating it on the first
14+
* ask and answering it after, never `422` for a package of the same App. A
15+
* package that declares no App is its own unit, as before.
16+
*
1017
* {@link ensureCustomizationPackage} is the body of
1118
* `ObjectStackProtocolImplementation#ensureCustomizationPackage`, served over
1219
* HTTP as `POST /api/v1/packages/:id/ensure-customization`
@@ -15,17 +22,18 @@
1522
* the pair (ADR-0146 D3: "no second door builds the pair"), and the one Studio,
1623
* the clone door and the migration call on a customer's first customization.
1724
*
18-
* Everything the pair is made of is the base's, through the pairing rules of
19-
* `@objectstack/spec/kernel` (`customization-package.ts`), ⛔ never a local
20-
* spelling:
25+
* Everything the pair is made of is its App's — the asked base's App, which
26+
* is the base itself for an App or an own-unit package — through the pairing
27+
* rules of `@objectstack/spec/kernel` (`customization-package.ts`), ⛔ never a
28+
* local spelling:
2129
*
22-
* - id — `customizationPackageId`, asked through {@link pairOf}: `<base>.custom`;
23-
* - namespace — {@link customizationNamespace}: `<base namespace>_custom`, SET
30+
* - id — `customizationPackageId`, asked through {@link pairOf}: `<app id>.custom`;
31+
* - namespace — {@link customizationNamespace}: `<app namespace>_custom`, SET
2432
* EXPLICITLY on the manifest so `installPackage`'s id-derived default
2533
* (`deriveNamespaceFromPackageId('hotcrm.custom')` = `custom`) never runs
2634
* (ADR-0146 D3, Q4 「全部同意」);
27-
* - dependency — {@link customizationDependencyRange}: `^<major>` of the base's
28-
* `manifest.version` at creation, declared on the base in
35+
* - dependency — {@link customizationDependencyRange}: `^<major>` of the App's
36+
* `manifest.version` at creation, declared on the App in
2937
* `manifest.dependencies` (ADR-0141 D1);
3038
* - writable — a scope-less authored package, the same record `POST
3139
* /packages` writes for a Studio-created base.
@@ -59,6 +67,9 @@
5967
*
6068
* - `400` — the request is malformed, or the base id has no pair (it is
6169
* itself a pair, or `<base>.custom` is not a valid package id);
70+
* - `409` — the base reaches several Apps and its namespace picks none or
71+
* more than one of them (ADR-0146 D3 merge question 11: no single App, so
72+
* no pair, never a guess);
6273
* - `404` — the base is not installed;
6374
* - `409` — the base's state rules the pair out: the base is a kernel
6475
* package ({@link kernelPackageScopeOf}), whose objects are not
@@ -68,8 +79,12 @@
6879
* pair namespace fails the manifest grammar (longer than
6980
* {@link MAX_CUSTOMIZATION_BASE_NAMESPACE_LENGTH}, no truncation and no
7081
* hashing); its version is not SemVer, so no range can be declared; or an
71-
* installed `<base>.custom` lacks the namespace or the dependency;
72-
* - `422 NAMESPACE_CONFLICT` — another package owns the pair's namespace: the
82+
* installed pair lacks the namespace or the dependency; or the pair's
83+
* namespace is held by the per-package pair an earlier version created for
84+
* a member of the same App (ADR-0146 D3 merge question 12: that pair stays
85+
* served, and the App's pair waits for it to be re-identified — never
86+
* `422` for a package of the same App);
87+
* - `422 NAMESPACE_CONFLICT` — a package outside the App owns the pair's namespace: the
7388
* code and status `SchemaRegistry.installPackage` stamps for the same
7489
* condition (`@objectstack/objectql`; that package depends on this one, so
7590
* the string is spelled in each).
@@ -102,10 +117,11 @@ import type {
102117
import {
103118
MAX_CUSTOMIZATION_BASE_NAMESPACE_LENGTH,
104119
customizationBasePackageId,
120+
customizationPackageId,
105121
customizationDependencyRange,
106122
customizationNamespace,
107123
} from '@objectstack/spec/kernel';
108-
import { pairOf } from '@objectstack/metadata-core';
124+
import { appOf, pairOf } from '@objectstack/metadata-core';
109125
import { installedManifestsOfEngine } from './installed-manifests.js';
110126
import type { CustomizationNamespaceRefusal } from '@objectstack/spec/kernel';
111127

@@ -287,6 +303,11 @@ export async function ensureCustomizationPackage(
287303
const baseId = declared.data.basePackageId;
288304

289305
const pair = pairOf(baseId, installedManifestsOfEngine(this.engine));
306+
if (!pair.ok && pair.reason === 'no_single_app') {
307+
throw refusal(409, `Package '${baseId}' declares several Apps and its namespace picks no single one of them, `
308+
+ 'so it has no paired customization package: a customization lands in the pair of the one App that '
309+
+ 'carries the package, and none is chosen by guess (ADR-0146 D3).');
310+
}
290311
if (!pair.ok) {
291312
throw refusal(400, pair.reason === 'base_is_customization_package'
292313
? `Package '${baseId}' is itself a paired customization package, and a pair is never paired again: `
@@ -374,9 +395,18 @@ export function customizationPairEnsureRefusal(
374395
+ `('${pairId}') holds the changes to a read-only base — one provided by code or an installed app `
375396
+ '(ADR-0146 D3).');
376397
}
377-
const namespace = customizationNamespace(base.manifest?.namespace);
398+
// [ruling record 6691022506] The pair is its App's: its namespace, its
399+
// dependency and its range are read off the App (the base itself for an
400+
// App or an own-unit package).
401+
const appId = customizationBasePackageId(pairId) ?? baseId;
402+
const app = appId === baseId ? base : registry.getPackage(appId);
403+
if (!app) {
404+
return refusal(404, `Package '${appId}', the App that carries '${baseId}', is not installed: its paired `
405+
+ `customization package '${pairId}' is ensured for an installed App (ADR-0146 D3).`);
406+
}
407+
const namespace = customizationNamespace(app.manifest?.namespace);
378408
if (!namespace.ok) {
379-
return refusal(409, `Cannot ensure '${pairId}': ${noPairNamespaceClause(baseId, base.manifest?.namespace, namespace.reason)} `
409+
return refusal(409, `Cannot ensure '${pairId}': ${noPairNamespaceClause(appId, app.manifest?.namespace, namespace.reason)} `
380410
+ '(ADR-0146 D3).');
381411
}
382412
// An existing pair is returned unchanged, or refused naming what it lacks.
@@ -387,28 +417,42 @@ export function customizationPairEnsureRefusal(
387417
if (declaredNamespace !== namespace.value) {
388418
lacks.push(`namespace '${namespace.value}' (it declares ${typeof declaredNamespace === 'string' && declaredNamespace !== '' ? `'${declaredNamespace}'` : 'none'})`);
389419
}
390-
const declaredRange = declaredRangeOn(existing.manifest, baseId);
391-
if (declaredRange === undefined) lacks.push(`a dependency on '${baseId}' in manifest.dependencies`);
420+
const declaredRange = declaredRangeOn(existing.manifest, appId);
421+
if (declaredRange === undefined) lacks.push(`a dependency on '${appId}' in manifest.dependencies`);
392422
if (lacks.length > 0) {
393423
return refusal(409, `Package '${pairId}' is installed but is not the paired customization package of `
394-
+ `'${baseId}': it lacks ${lacks.join(', and ')}. It is not adopted or rewritten — delete it, or `
424+
+ `'${appId}': it lacks ${lacks.join(', and ')}. It is not adopted or rewritten — delete it, or `
395425
+ 'install a manifest over it that declares both, then ensure the pair again (ADR-0146 D3).');
396426
}
397427
return undefined;
398428
}
399-
const range = customizationDependencyRange(base.manifest?.version);
429+
const range = customizationDependencyRange(app.manifest?.version);
400430
if (!range.ok) {
401-
return refusal(409, `Cannot ensure '${pairId}': package '${baseId}' is at version `
402-
+ `'${String(base.manifest?.version)}', which is not a SemVer 2.0.0 version, so the pair's dependency `
403-
+ 'range — the major of the base\'s version — cannot be declared (ADR-0146 D3).');
431+
return refusal(409, `Cannot ensure '${pairId}': package '${appId}' is at version `
432+
+ `'${String(app.manifest?.version)}', which is not a SemVer 2.0.0 version, so the pair's dependency `
433+
+ 'range — the major of the App\'s version — cannot be declared (ADR-0146 D3).');
404434
}
405435
// The pair's namespace is the pair's alone: an owner other than the pair
406436
// is refused here, before the install, whatever co-ownership the registry
407437
// would read.
408438
const owner = registry.getNamespaceOwners?.(namespace.value)?.find((id) => id !== pairId);
409439
if (owner !== undefined) {
440+
// [ADR-0146 D3 merge question 12] The per-package pair an earlier
441+
// version created for a member of this App holds the namespace: it
442+
// stays served as it is, and the App's pair waits for it to be
443+
// re-identified as the App's pair — a resource-state conflict inside
444+
// the App, never the `422` kept for a package outside it.
445+
const ownerBase = customizationBasePackageId(owner);
446+
const installed = installedManifestsOfEngine(engine);
447+
if (ownerBase !== null && customizationPackageId(ownerBase).ok && appOf(ownerBase, installed) === appId) {
448+
return refusal(409, `Cannot ensure '${pairId}' yet: its namespace '${namespace.value}' is held by `
449+
+ `'${owner}', the paired customization package an earlier version created for '${ownerBase}', a `
450+
+ `package of the App '${appId}'. That package stays installed and its customizations stay served; `
451+
+ `'${pairId}' is created once '${owner}' is re-identified as the App's pair, and is not created `
452+
+ 'beside it (ADR-0146 D3).');
453+
}
410454
return refusal(422, `Cannot ensure '${pairId}': its namespace '${namespace.value}' is already owned by package `
411-
+ `'${owner}'. The pair's namespace is the base's followed by '_custom' and has no alternative; uninstall `
455+
+ `'${owner}'. The pair's namespace is the App's followed by '_custom' and has no alternative; uninstall `
412456
+ `'${owner}' or move it off that namespace, then ensure the pair again (ADR-0146 D3).`,
413457
'NAMESPACE_CONFLICT');
414458
}
@@ -425,38 +469,40 @@ async function ensurePairOnce(
425469
// Every refusal the registry decides, read once ({@link customizationPairEnsureRefusal}).
426470
const refused = customizationPairEnsureRefusal(this.engine, registry, baseId, pairId);
427471
if (refused) throw refused;
428-
const base = registry.getPackage(baseId)!;
429-
const namespace = customizationNamespace(base.manifest?.namespace);
472+
// The pair is its App's (the refusal above read the same App).
473+
const appId = customizationBasePackageId(pairId) ?? baseId;
474+
const app = registry.getPackage(appId)!;
475+
const namespace = customizationNamespace(app.manifest?.namespace);
430476
if (!namespace.ok) throw new Error('unreachable: the namespace was judged above');
431477

432478
// Idempotent: an existing pair is returned unchanged — ⛔ never adopted,
433479
// never rewritten (its refusal is above).
434480
const existing = registry.getPackage(pairId);
435481
if (existing) {
436482
return {
437-
basePackageId: baseId,
483+
basePackageId: appId,
438484
packageId: pairId,
439485
namespace: namespace.value,
440-
dependencyRange: declaredRangeOn(existing.manifest, baseId)!,
486+
dependencyRange: declaredRangeOn(existing.manifest, appId)!,
441487
created: false,
442488
writable: isWritablePackage(this.engine, pairId),
443489
};
444490
}
445491

446-
const range = customizationDependencyRange(base.manifest?.version);
492+
const range = customizationDependencyRange(app.manifest?.version);
447493
if (!range.ok) throw new Error('unreachable: the version was judged above');
448-
const baseName = typeof base.manifest?.name === 'string' && base.manifest.name !== '' ? base.manifest.name : baseId;
494+
const appName = typeof app.manifest?.name === 'string' && app.manifest.name !== '' ? app.manifest.name : appId;
449495
const manifest = {
450496
id: pairId,
451-
name: `${baseName} (custom)`,
497+
name: `${appName} (custom)`,
452498
version: CREATED_PAIR_VERSION,
453499
type: CREATED_PAIR_TYPE,
454500
namespace: namespace.value,
455-
dependencies: { [baseId]: range.value },
501+
dependencies: { [appId]: range.value },
456502
};
457503
await this.installPackage({ manifest } as InstallPackageRequest);
458504
return {
459-
basePackageId: baseId,
505+
basePackageId: appId,
460506
packageId: pairId,
461507
namespace: namespace.value,
462508
dependencyRange: range.value,

‎packages/metadata-protocol/src/protocol-discovery-and-bases.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import {
4646
getMetadataTypeActions,
4747
getMetadataTypeSchema,
4848
} from '@objectstack/spec/kernel';
49-
import { pairOf } from '@objectstack/metadata-core';
49+
import { pairsOf } from '@objectstack/metadata-core';
5050
import { installedManifestsOfEngine } from './installed-manifests.js';
5151
import { storedBodyForCarryForward } from './protocol-credential-carry-forward.js';
5252
import type { Equal, MetaTypesListing } from './protocol-meta-types-listing.js';
@@ -408,11 +408,14 @@ export class ProtocolDiscoveryAndBasesFacet {
408408
*/
409409
static async getPairTranslationBundle(host: Pick<ProtocolHost, 'engine' | 'findEnvironmentRows'>, basePackageId: string): Promise<TranslationBundle | undefined> {
410410
if (typeof basePackageId !== 'string' || basePackageId === '') return undefined;
411-
const pair = pairOf(basePackageId, installedManifestsOfEngine(host.engine));
412-
if (!pair.ok) return undefined;
411+
// [ADR-0146 D3 merge question 12] The App's pair, and a per-package
412+
// pair an earlier version created while it is installed (`pairsOf`).
413+
const pairs = pairsOf(basePackageId, installedManifestsOfEngine(host.engine));
414+
if (pairs.length === 0) return undefined;
415+
const binding = pairs.length === 1 ? pairs[0] : { $in: pairs };
413416
const layer = await readAuthoredTranslationLayer({
414417
find: (table: string, opts: { where?: Record<string, unknown>; [key: string]: unknown } = {}) =>
415-
host.findEnvironmentRows(table, { ...opts, where: { ...opts.where, package_id: pair.value } }),
418+
host.findEnvironmentRows(table, { ...opts, where: { ...opts.where, package_id: binding } }),
416419
});
417420
// `null` is a read that failed: the layer's own rule is "keep what you
418421
// had", and a translation input has nothing to keep, so the caller

‎packages/metadata-protocol/src/protocol-environment-rows.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* not move.
4545
*/
4646

47-
import { pairOf, type InstalledManifestLookup } from '@objectstack/metadata-core';
47+
import { isPairOf, type InstalledManifestLookup } from '@objectstack/metadata-core';
4848
import { PLURAL_TO_SINGULAR, SINGULAR_TO_PLURAL } from '@objectstack/spec/shared';
4949
import type { StoredOverlayRow } from './item-lock.js';
5050
import { overlayMemberWhere } from './migrations/overlay-index.js';
@@ -300,6 +300,5 @@ export function baseAddressServesMember(
300300
): boolean {
301301
if (bound === undefined || bound === base) return true;
302302
if (baseShipsMember) return false;
303-
const pair = pairOf(base, installed);
304-
return pair.ok && pair.value === bound;
303+
return isPairOf(bound, base, installed);
305304
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import {
2626
// {@link ObjectStackProtocolImplementation.getMetaItemLayered}'s code-layer
2727
// fallback so a hydrated row is never answered as the code layer.
2828
isTenantAuthored,
29-
pairOf,
29+
isPairOf,
30+
pairsOf,
3031
} from '@objectstack/metadata-core';
3132
import {
3233
type MetadataLock,
@@ -407,13 +408,15 @@ export async function getMetaItemLayered(host: Pick<ProtocolHost, 'artifactLockL
407408
const owner = boundTo(row);
408409
if (owner === undefined || owner === base) return false;
409410
if (isServedOverrideRowOf(row, base, installed)) return false;
410-
const pair = pairOf(base, installed);
411-
if (pair.ok && pair.value === owner) return true;
411+
if (isPairOf(owner, base, installed)) return true;
412412
return host.resolveWritePackageScope(owner)?.dependencies.includes(base) === true;
413413
})
414414
.sort((a, b) => {
415-
const pair = pairOf(base, installed);
416-
const rank = (row: StoredOverlayRow) => (pair.ok && boundTo(row) === pair.value ? 0 : 1);
415+
const pairs = pairsOf(base, installed);
416+
const rank = (row: StoredOverlayRow) => {
417+
const at = pairs.indexOf(boundTo(row) ?? '');
418+
return at === -1 ? pairs.length : at;
419+
};
417420
return rank(a) - rank(b) || String(boundTo(a)).localeCompare(String(boundTo(b)));
418421
})[0];
419422
if (orphan !== undefined) {

0 commit comments

Comments
 (0)