Skip to content

Commit fa30414

Browse files
fix(metadata-protocol)!: an unmanaged rename refuses a package without namespace whose object names all share a prefix other than the derived one (#36138)
<!-- claude-projects-attribution: {"github_login":"os-project-manager"} --> _Requested by **Leehom** · [project thread](https://claude.ai/code/project/chan_01TenzsRopZj4jMbvjX8uz1X?thread=cmsg_01TenzsRopZj4jMbvjX8uz1X7DBJdArz6UfcraENi1dMpg)_ Fixes #35823 Clause-②: no (narrowing) Metadata-protocol: none Skills-docs: updated here — the refusal row in content/docs/deployment/install-modes.mdx; no skills/** file teaches the unmanaged rename (git grep targetNamespace under skills: 0 hits) ## 维护者速读 - 用户能看到什么变化:把未声明命名空间的软件包以副本方式装进新命名空间时,若它的对象名全都带着同一个别的前缀,安装会被拒绝并说清两个前缀,不再生成带双重前缀的副本。 - 为什么做:云端发布快照曾丢掉命名空间,副本里同一个应用出现三种拼法且无任何提示,属于数据静默出错。 - 出错会怎样:只影响带新命名空间的副本安装,拒绝发生在写入前,什么都不会装;声明了命名空间的包、只有一个对象或对象名没有共同前缀的包照旧安装。 - 要你做什么:这次改动让原来能装的一类请求改为拒绝(收紧),按规则需要你本人批准后才能合并。 ## What changes `planUnmanagedImport` (`packages/metadata-protocol/src/package-unmanaged-install.ts`) derives the source namespace from the package id when the body declares no `namespace`. On the rename path (a `targetNamespace` different from that namespace) the rename engine trusted the guess: an object name without the derived prefix takes the target prefix in front of its whole name. A name that carries the producer's real prefix (`demoasset_asset`, under an id that derives `asset_lifecycle_b`) was copied as `trialdemoasset_demoasset_asset`, while its view `demoasset_asset_all` kept its name. Now, on the rename path of a body that declares no `namespace`, the copy is judged by its object names' COMMON prefix (the card's signal; seat reading 7066371354 after L1 R1): when there are at least two objects, none carries the derived prefix, and every object name begins with the same first segment, the copy is refused through the existing rename refusal: `400 INVALID_REQUEST`, one `details.refused` entry per object, each reason naming the derived namespace, the common prefix and the missing `namespace` key, and asking to declare it (no namespace value is suggested). No new error code, no new `details` key; the refusal comes before the base exists, so nothing is written. Object names only (ADR-0129 D2 puts the namespace on object names): the gallery package this came from names its flows, datasets, reports and skills without the prefix, and translation items are named by the planner. Unchanged, and pinned as controls: - a body that declares `namespace` is renamed exactly as before, whatever its names look like (`namespace: 'other'` with `demoasset_*` objects still copies as `trialdemoasset_demoasset_*`; this control reds when the declared-namespace gate is removed); - a multi-segment unprefixed name keeps ADR-0048 §3.5's amended rename: `purchase_order` copies as `trialdemoasset_purchase_order`, alone or beside `asset_owner` (no common first segment); - a single-object package, bare one-segment names, and any package with an object carrying the derived prefix keep the rename; - without a `targetNamespace` nothing changes: the save door's prefix rule already refuses these names (`422 NAMESPACE_PREFIX`, rolled back, nothing kept), measured on main 30b0987. The refusal row is added to the rename table in `content/docs/deployment/install-modes.mdx` (file surface widened by the seat for that one row). Read first, by risk: 1. `package-unmanaged-install.ts`, `derivedNamespaceContradiction` and its one call before the rename loop: the rule that decides what is refused. 2. `package-unmanaged-install.test.ts`, the describe block `a namespace derived from the id, contradicted by the object names' common prefix`: the pins and the controls. 3. The changeset: the BREAKING sentence and the ADR-0087 disposition. ## Why Data silently wrong: one copied app held three spellings of one package's names, with no refusal (card #35823, found on objectstack-ai/cloud#4856 whose publish snapshot dropped `manifest.namespace`; the cloud side is fixed there). Loud over lenient: a derived namespace the names contradict is refused, never renamed. ## Tests - `pnpm --filter @objectstack/metadata-protocol exec vitest run --maxWorkers=2 src/package-unmanaged-install.test.ts`: 105/105 passed (8 cases in the new block). - Ablations on the committed round (a7b3062), on-disk mutation by `scripts/ablation-replace.mjs`, each trap-restored with `git diff HEAD` empty and the HEAD blob hash matched after: - the common-prefix refusal made a no-op (blob 0944eee -> cb234f9d5ba6) => 2 failed / 103 passed: the refusal naming both, and the verb's zero-write refusal; - the declared-namespace gate forced open (blob 0944eee -> d0bfa966ec7f) => 1 failed / 104 passed: the declared-`namespace` control (contract review S2). - The tests import the module from `src/`, so no build is in the path. ## Acceptance notes - The common prefix is the first `_` segment. Two objects sharing a short first word (`purchase_order`, `purchase_item`) in a namespace-less package are refused too, and declaring `namespace` clears it; the docs row says so. - The no-`targetNamespace` path keeps its existing `422 NAMESPACE_PREFIX` answer (base created then rolled back) rather than a plan-time `400`; aligning the two would change an existing answer and is not this card's. - filed #36166 — the silent cases the set-level signal leaves (contract review 7066464020, boundary flag ③): a single-object package with a foreign prefix, one stray object beside the prefixed ones, and a declared `namespace` the object names contradict still copy double-prefixed; plus the dated ADR-0048 §3.5 note for the `purchase_order` + `purchase_item` refusal. ## Verification Two-minute check: run the test file above; the describe block `a namespace derived from the id, contradicted by the object names' common prefix — refused, never double-prefixed` passes, 8 cases. Round 1 (head ca5b93e, the per-name rule): CI all green or skipped, 95/95 derived gates exit 0, full dogfood battery green (PR comment 7066311396). This round's head is re-verified the same way and declared in a PR comment when the runs finish. ## Human Minimum - [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic. - [x] 2. Every name I added uses the casing for its kind. - [ ] 3. No formatter ran; `pnpm lint` passes. (no formatter ran; `pnpm lint` is CI's run, read from its check-run) - [x] 4. One issue, one branch off an up-to-date `main`, one pull request. - [x] 5. A change that publishes carries a changeset, and both declaration lines are filled in here and in the changeset. - [x] 6. `packages/spec` touched: its generated artifacts are regenerated and committed. (not touched) - [x] 7. No hand edits to `content/docs/references/`, `packages/*/CHANGELOG.md` or `content/docs/releases/`. - [x] 8. Title, body and comments are in English. - [x] The *Before Submitting* list in CONTRIBUTING.md is done. --- _Generated by [Claude Code](https://claude.ai/code/session_01FEkQ5nwb5fUkxg7Q9NVxvR)_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01FEkQ5nwb5fUkxg7Q9NVxvR <!-- signed: repo:objectstack#4 · os-project-manager · session_01FEkQ5krBqrrgxCYgjXmfbv --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 7376505 commit fa30414

4 files changed

Lines changed: 202 additions & 1 deletion

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@objectstack/metadata-protocol': patch
3+
---
4+
5+
fix(metadata-protocol)!: an unmanaged install renamed into a `targetNamespace` refuses a package that declares no namespace when its object names all share a prefix other than the derived one, instead of copying them under a second prefix
6+
7+
Clause-②: no (narrowing)
8+
Metadata-protocol: none
9+
10+
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export changes shape. What moves is the answer the unmanaged install gives one request: a package body with no `namespace`, renamed into a `targetNamespace`, whose object names all begin with one first word that is not the namespace derived from its id. No stored row, registry key or authored value changes, so there is nothing for a migration to rewrite and no tombstone. -->
11+
12+
**BREAKING** for a caller of the unmanaged install (`importUnmanagedPackage`, and `POST /api/v1/marketplace/install-local` with `installMode: 'unmanaged'`) that sends a `targetNamespace` with a package body that declares no `namespace`.
13+
14+
Such a body's namespace is derived from its id (`ai.objectstack.gallery.asset-lifecycle-b` derives `asset_lifecycle_b`), and the rename trusted it: an object named `demoasset_asset`, which carries its real prefix, was copied into `trialdemoasset` as `trialdemoasset_demoasset_asset`, while its view `demoasset_asset_all` kept its name, so one copy held three spellings and nothing said why. Now, when such a body has two or more objects, none carries the derived prefix, and every object name begins with the same first word (`demoasset_`), the copy is refused before the base exists: `400 INVALID_REQUEST`, with one rename-refusal `details.refused` entry per object naming the derived namespace, the common prefix and the missing `namespace` key. Nothing is installed. Two objects sharing a short first word (`purchase_order`, `purchase_item`) are refused the same way, and declaring `namespace` clears it.
15+
16+
A body that declares `namespace` is renamed exactly as before, whatever its names look like. In a namespace-less body, every other case keeps the rename: a single object, objects with no common first word (`purchase_order` beside `asset_owner`), or any object carrying the derived prefix each take the target prefix in front of a name that lacks it (`purchase_order` copies as `trialdemoasset_purchase_order`). Without a `targetNamespace` nothing changes: the save door's prefix rule already refuses those names (`422 NAMESPACE_PREFIX`).
17+
18+
The fix is in the package: declare `namespace` on it, the prefix its object names carry, then install again.

‎content/docs/deployment/install-modes.mdx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ refusal is answered before the base exists, so nothing is installed:
267267
|:---|:---|
268268
| A `targetNamespace` the namespace rule refuses, or a shared one | `400 INVALID_REQUEST`, naming `targetNamespace` |
269269
| An object whose copy name another object's copy takes (`ticket` beside `crm_ticket`) | `400 INVALID_REQUEST`, naming the object and the rename that clears it |
270+
| A package that declares no `namespace`, with two or more objects whose names all begin with the same first word, not the namespace derived from the package id (`demoasset_asset` and `demoasset_site` in `com.acme.asset-lifecycle`, which derives `asset_lifecycle`; `purchase_order` and `purchase_item` too) | `400 INVALID_REQUEST`, naming each object, the derived namespace and the common prefix; declaring `namespace` on the package clears it |
270271
| A copy name that is not a legal item name, longer than an object's table name may be (63 characters), or longer than an item name may be (255) | `400 INVALID_REQUEST`, naming each item |
271272
| A renamed key that meets a key the item already holds (a permission set granting both `crm_lead` and `acme_lead`) | `400 INVALID_REQUEST`, naming the item and both keys |
272273
| A capability whose copy name another capability of the package declares (`crm.approve` beside `acme.approve`) | `400 INVALID_REQUEST`, naming the capability |

‎packages/metadata-protocol/src/package-unmanaged-install.test.ts‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,126 @@ describe('importUnmanagedPackage — `targetNamespace` (#30794)', () => {
11381138
});
11391139
});
11401140

1141+
/**
1142+
* A body that declares no `namespace` has one derived from its id. Renamed into
1143+
* a `targetNamespace`, a package whose object names all share another prefix
1144+
* is refused, never double-prefixed; anything else keeps ADR-0048 §3.5's
1145+
* rename. The refused body is the shape a publish snapshot that dropped
1146+
* `manifest.namespace` hands over: a gallery package whose objects carry their
1147+
* real prefix, `demoasset_`, under an id that derives `asset_lifecycle_b`.
1148+
*/
1149+
function namespacelessBody(objectNames: string[], extra: Record<string, unknown> = {}): Record<string, unknown> {
1150+
return {
1151+
id: 'ai.objectstack.gallery.asset-lifecycle-b',
1152+
version: '1.0.0',
1153+
type: 'app',
1154+
name: 'Asset lifecycle',
1155+
objects: objectNames.map((name) => ({
1156+
name, label: name, sharingModel: 'private', fields: { title: { type: 'text', label: 'Title' } },
1157+
})),
1158+
views: [{
1159+
name: `${objectNames[0]}_all`,
1160+
label: 'All',
1161+
object: objectNames[0],
1162+
viewKind: 'list',
1163+
columns: [{ field: 'title', label: 'Title' }],
1164+
}],
1165+
...extra,
1166+
};
1167+
}
1168+
1169+
const renamedObjects = (body: Record<string, unknown>) =>
1170+
plan(body, { targetNamespace: 'trialdemoasset' }).items
1171+
.filter((i) => i.type === 'object')
1172+
.map((i) => `${i.sourceName} -> ${i.name}`);
1173+
1174+
describe('a namespace derived from the id, contradicted by the object names\' common prefix — refused, never double-prefixed', () => {
1175+
const refusedPlan = (body: Record<string, unknown>) =>
1176+
(() => { try { plan(body, { targetNamespace: 'trialdemoasset' }); } catch (e) { return e as any; } throw new Error('expected a refusal'); })();
1177+
1178+
it('every object sharing a prefix other than the derived namespace is refused — INVALID_REQUEST / 400, naming both', () => {
1179+
const body = namespacelessBody(['demoasset_asset', 'demoasset_site']);
1180+
expect(plan(body).sourceNamespace).toBe('asset_lifecycle_b');
1181+
const err = refusedPlan(body);
1182+
const envelope = resolveThrownHttpError(err);
1183+
expect([envelope.status, envelope.code]).toEqual([400, 'INVALID_REQUEST']);
1184+
expect(err.details.targetNamespace).toBe('trialdemoasset');
1185+
expect(err.details.refused.map((r: any) => `${r.type}/${r.name}`)).toEqual(['object/demoasset_asset', 'object/demoasset_site']);
1186+
const [{ reason }] = err.details.refused;
1187+
// Both prefixes named, and the key whose absence made the guess.
1188+
expect(reason).toContain("derived from its id as 'asset_lifecycle_b'");
1189+
expect(reason).toContain("begins 'demoasset_'");
1190+
expect(reason).toContain('declares no `namespace`');
1191+
expect(err.message).toContain("cannot be installed unmanaged into namespace 'trialdemoasset'");
1192+
});
1193+
1194+
it('the verb refuses before anything is written: no base, no row — the snapshot that dropped its namespace', async () => {
1195+
const { protocol, writes, packages, table } = makeProtocol();
1196+
const err = await refusalOf(() => protocol.importUnmanagedPackage({
1197+
manifest: namespacelessBody(['demoasset_asset', 'demoasset_site']), targetNamespace: 'trialdemoasset',
1198+
}));
1199+
expect([resolveThrownHttpError(err).status, resolveThrownHttpError(err).code]).toEqual([400, 'INVALID_REQUEST']);
1200+
expect(err.details.refused.map((r: any) => r.name)).toEqual(['demoasset_asset', 'demoasset_site']);
1201+
expect(writes).toEqual([]);
1202+
expect(packages.size).toBe(0);
1203+
expect(table('sys_metadata')).toEqual([]);
1204+
});
1205+
1206+
it('CONTROL: a multi-segment unprefixed name takes the target prefix in front, as ADR-0048 §3.5 renames it', () => {
1207+
expect(renamedObjects(namespacelessBody(['purchase_order']))).toEqual(['purchase_order -> trialdemoasset_purchase_order']);
1208+
// Two names with no common first segment: no shared prefix to contradict the derived one.
1209+
expect(renamedObjects(namespacelessBody(['purchase_order', 'asset_owner']))).toEqual([
1210+
'purchase_order -> trialdemoasset_purchase_order',
1211+
'asset_owner -> trialdemoasset_asset_owner',
1212+
]);
1213+
// A bare one-segment name too.
1214+
expect(renamedObjects(namespacelessBody(['asset', 'site']))).toEqual([
1215+
'asset -> trialdemoasset_asset',
1216+
'site -> trialdemoasset_site',
1217+
]);
1218+
});
1219+
1220+
it('CONTROL: a single-object package is not refused — one name carries no common prefix', () => {
1221+
expect(renamedObjects(namespacelessBody(['demoasset_asset']))).toEqual(['demoasset_asset -> trialdemoasset_demoasset_asset']);
1222+
});
1223+
1224+
it('CONTROL: one object carrying the derived prefix confirms the derived namespace — nothing is refused', () => {
1225+
expect(renamedObjects(namespacelessBody(['asset_lifecycle_b_asset', 'demoasset_site']))).toEqual([
1226+
'asset_lifecycle_b_asset -> trialdemoasset_asset',
1227+
'demoasset_site -> trialdemoasset_demoasset_site',
1228+
]);
1229+
});
1230+
1231+
it('CONTROL: a declared `namespace` is judged as before, whatever the names look like', () => {
1232+
expect(renamedObjects(namespacelessBody(['demoasset_asset', 'demoasset_site'], { namespace: 'demoasset' }))).toEqual([
1233+
'demoasset_asset -> trialdemoasset_asset',
1234+
'demoasset_site -> trialdemoasset_site',
1235+
]);
1236+
expect(renamedObjects(namespacelessBody(['demoasset_asset', 'demoasset_site'], { namespace: 'other' }))).toEqual([
1237+
'demoasset_asset -> trialdemoasset_demoasset_asset',
1238+
'demoasset_site -> trialdemoasset_demoasset_site',
1239+
]);
1240+
});
1241+
1242+
it('CONTROL: without a rename the plan does not ask — the save door\'s prefix rule answers those names', () => {
1243+
expect(plan(namespacelessBody(['demoasset_asset', 'demoasset_site'])).items.map((i) => i.name))
1244+
.toEqual(['demoasset_asset', 'demoasset_site', 'demoasset_asset_all']);
1245+
});
1246+
1247+
it('CONTROL: the verb copies a namespace-less body whose names carry the derived prefix', async () => {
1248+
const { protocol, table } = makeProtocol();
1249+
const result = await protocol.importUnmanagedPackage({
1250+
manifest: namespacelessBody(['asset_lifecycle_b_asset', 'asset_lifecycle_b_site']), targetNamespace: 'trialdemoasset',
1251+
});
1252+
expect(result).toMatchObject({ namespace: 'trialdemoasset', sourceNamespace: 'asset_lifecycle_b' });
1253+
expect(table('sys_metadata').filter((r) => r.state === 'active').map((r) => `${r.type}/${r.name}`).sort()).toEqual([
1254+
'object/trialdemoasset_asset',
1255+
'object/trialdemoasset_site',
1256+
'view/trialdemoasset_asset_all',
1257+
]);
1258+
});
1259+
});
1260+
11411261
describe('the rename parity — one package, one `targetNamespace`, two doors, identical items (#30794)', () => {
11421262
/** Every active row of one package: `type/name` → its stored body. */
11431263
const itemsOf = (table: (name: string) => Row[], packageId: string) => Object.fromEntries(

‎packages/metadata-protocol/src/package-unmanaged-install.ts‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,8 @@ export function planUnmanagedImport(
519519
// prefix. Without a `targetNamespace` it is the base's too (the stage-0
520520
// ACCEPT's Q3): the base must own that prefix for the save door's
521521
// namespace rule to admit the copied object names.
522-
const sourceNamespace = stringKey(manifest, 'namespace') ?? deriveNamespaceFromPackageId(sourcePackageId) ?? undefined;
522+
const declaredNamespace = stringKey(manifest, 'namespace');
523+
const sourceNamespace = declaredNamespace ?? deriveNamespaceFromPackageId(sourcePackageId) ?? undefined;
523524
if (sourceNamespace === undefined) {
524525
throw invalidRequest(
525526
`Cannot determine the namespace of package '${sourcePackageId}': it declares none, and none derives `
@@ -618,6 +619,15 @@ export function planUnmanagedImport(
618619
items: items.map((item) => ({ type: item.type, name: item.name, body: item.body })),
619620
retryVerb: 'install',
620621
});
622+
if (declaredNamespace === undefined) {
623+
const contradicted = derivedNamespaceContradiction(
624+
sourcePackageId,
625+
sourceNamespace,
626+
namespace,
627+
items.filter((item) => item.type === 'object').map((item) => item.name),
628+
);
629+
if (contradicted !== undefined) throw unmanagedInstallRenameRefusal(sourcePackageId, namespace, contradicted);
630+
}
621631
const refused: UnmanagedImportRenameRefusal[] = [];
622632
for (const item of items) {
623633
const reason = (item.type === 'object' ? planned.objectRefusal(item.name) : undefined)
@@ -664,6 +674,58 @@ export function planUnmanagedImport(
664674
};
665675
}
666676

677+
/**
678+
* ⛔ A DERIVED NAMESPACE THE OBJECT NAMES CONTRADICT IS REFUSED, NEVER RENAMED.
679+
* When the body declares no `namespace`, the source namespace is a guess
680+
* (`deriveNamespaceFromPackageId`), and the rename engine trusts it: an object
681+
* name without that prefix takes the target prefix in front of its whole name
682+
* (ADR-0048 §3.5 as amended). When the producer's real namespace was dropped
683+
* from the manifest, every object still carries it, and that rule turns the
684+
* copy into `<target>_<real>_<name>` beside items the swap left alone, one app
685+
* in three spellings.
686+
*
687+
* The signal is the object names' COMMON prefix, never one name's shape: a
688+
* single name cannot tell a two-word short name (`purchase_order`) from a
689+
* foreign prefix (`demoasset_site`). So only when there are at least two
690+
* objects, every object name begins with the same first segment, that segment
691+
* is not the derived namespace, and no object carries the derived prefix, is
692+
* the copy refused: every object at its own name, naming the derived
693+
* namespace and the common prefix, and asking for the missing key. Otherwise
694+
* the ADR rename applies, unchanged. Object names only: ADR-0129 D2 puts the
695+
* namespace on object names, while a package's flows, datasets or reports may
696+
* be named without it, and its translation items are named by this planner.
697+
* Without a rename the save door's own prefix rule refuses the same names
698+
* (`NAMESPACE_PREFIX`), so this is asked only on the rename path.
699+
*
700+
* @returns one refusal per object, or `undefined` when the names do not
701+
* contradict the derived namespace.
702+
*/
703+
function derivedNamespaceContradiction(
704+
sourcePackageId: string,
705+
derivedNamespace: string,
706+
targetNamespace: string,
707+
objectNames: readonly string[],
708+
): UnmanagedImportRenameRefusal[] | undefined {
709+
if (objectNames.length < 2) return undefined;
710+
if (objectNames.some((name) => name.startsWith(`${derivedNamespace}_`))) return undefined;
711+
const firstSegment = (name: string): string | undefined => {
712+
const separator = name.indexOf('_');
713+
return separator > 0 ? name.slice(0, separator) : undefined;
714+
};
715+
const common = firstSegment(objectNames[0]);
716+
if (common === undefined || common === derivedNamespace) return undefined;
717+
if (!objectNames.every((name) => firstSegment(name) === common)) return undefined;
718+
return objectNames.map((name) => ({
719+
type: 'object',
720+
name,
721+
reason: `Object '${name}' is not copied: package '${sourcePackageId}' declares no \`namespace\`, so its `
722+
+ `namespace was derived from its id as '${derivedNamespace}', but every one of its ${objectNames.length} `
723+
+ `object names begins '${common}_' instead. Renamed into namespace '${targetNamespace}' it would be `
724+
+ `'${targetNamespace}_${name}', keeping a prefix the package never declared. Declare \`namespace\` on `
725+
+ 'the package, the prefix its object names carry, then install again.',
726+
}));
727+
}
728+
667729
/** [#30794] One item the rename into a `targetNamespace` cannot copy, and why. */
668730
export interface UnmanagedImportRenameRefusal {
669731
type: string;

0 commit comments

Comments
 (0)