diff --git a/.changeset/34407-composed-translations-global-uniques.md b/.changeset/34407-composed-translations-global-uniques.md new file mode 100644 index 00000000000..8eb7572c6e9 --- /dev/null +++ b/.changeset/34407-composed-translations-global-uniques.md @@ -0,0 +1,15 @@ +--- +'@objectstack/cloud-connection': patch +--- + +fix(cloud-connection): install-local loads a composed bundle's translations and asks about its installation-wide uniques + +Clause-②: no +Metadata-protocol: none + +A composed bundle (`dist/objectstack.json` with `packages[]`, the ADR-0130 D4 publish shape) carries its `translations` and its `objects` inside each package body, with no top-level copy. Two readers of `MarketplaceInstallLocalPlugin` read the top level only: + +- the install's side effects loaded no translations (`translationsLoaded: 0`, on install, reseed, upgrade and restart), so a composed app's labels never reached the i18n service and zh-CN users saw the source language; +- the ADR-0120 D5e ceremony on an `isolated` deployment listed none of the package bodies' installation-wide uniques (`unique: 'global'`, a declared index's `unique: 'global'`), so a composed app installed without the confirmation and with no attestation recorded. + +Both now read through `resolveStackCollection` from `@objectstack/core`, the reader the seed datasets already use: the top-level list when the body carries one, otherwise each package body's, in the artifact's package order. A flat bundle, and a composed artifact that still carries its flattened top-level list, read exactly that list, once, as before. On an `isolated` deployment, a composed artifact whose `packages` the resolver refuses is answered at the ceremony with the resolver's refusal (`422`, its own code, such as `INVALID_ARTIFACT_PACKAGES`), and nothing is installed or written. diff --git a/packages/cloud-connection/src/marketplace-install-local-composed-top-level-reads.test.ts b/packages/cloud-connection/src/marketplace-install-local-composed-top-level-reads.test.ts new file mode 100644 index 00000000000..4960c3717a3 --- /dev/null +++ b/packages/cloud-connection/src/marketplace-install-local-composed-top-level-reads.test.ts @@ -0,0 +1,264 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#34407, ADR-0130 D4] The translations and the ADR-0120 D5e global-unique + * ceremony of a COMPOSED bundle read every package body, not the top level + * alone. + * + * A multi-package `dist/objectstack.json` carries `packages[]`, one assembled + * body per package, and no flattened `translations` or `objects` beside it. + * Two readers of install-local read only the top level: + * + * - the side effects' translation step answered `translationsLoaded: 0`, so a + * composed app's labels never reached the i18n service (zh-CN users read the + * source language); + * - the `isolated`-posture ceremony collected the installation-wide uniques of + * the top-level `objects` only, so a composed app's `unique: 'global'` + * installed with no question asked and no attestation recorded. + * + * Both now read through `resolveStackCollection`, the reader the seed datasets + * use (`marketplace-install-local-composed-bundle-seeds.test.ts`): the + * top-level list when the body carries one, otherwise every package body's, in + * the artifact's package order. The controls pin that a flat bundle reads its + * own top-level list unchanged, and that a composed artifact which still + * carries its flattened union reads it once, never once more per package. + */ + +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; +import { mkdtempSync, rmSync } from 'node:fs'; +import { join } from 'node:path'; +import { tmpdir } from 'node:os'; + +vi.mock('@objectstack/runtime', () => ({ + SeedLoaderService: class { + async load() { return { summary: { totalInserted: 0, totalUpdated: 0, totalSkipped: 0 }, errors: [] }; } + async buildDependencyGraph(objects: string[]) { return { objects }; } + }, + resolveSeedLocaleForLoad: async () => undefined, + appDefaultSeedLocale: () => undefined, + describeSeedLocale: () => 'none', +})); + +import { MarketplaceInstallLocalPlugin } from './marketplace-install-local-plugin.js'; +import { declareInstallerPlatformOwner, installerAuthService, withInstallerGrants } from './install-local-principal.fixtures.js'; +import { LocalManifestSource } from './local-manifest-source.js'; + +type Handler = (c: any) => Promise; + +const ROUTE = '/api/v1/marketplace/install-local'; +const SUITE = 'com.example.suite'; + +const CORE_ZH = { 'zh-CN': { objects: { crm_account: { label: '客户' } } } }; +const O2C_ZH = { 'zh-CN': { objects: { crm_order: { label: '订单' } } }, 'ja-JP': { objects: { crm_order: { label: '注文' } } } }; + +const body = (id: string, version: string, object: Record, translations: unknown[], dependencies?: Record) => ({ + manifest: { + id, + name: id, + version, + type: 'module', + ...(dependencies ? { dependencies } : {}), + objects: [object], + translations, + }, +}); + +const ACCOUNT = { name: 'crm_account', label: 'Account', fields: { name: { type: 'text', label: 'Name' }, tax_id: { type: 'text', label: 'Tax ID', unique: 'global' } } }; +const ORDER = { + name: 'crm_order', + label: 'Order', + fields: { name: { type: 'text', label: 'Name', unique: true } }, + indexes: [{ name: 'uk_order_ext', fields: ['external_id'], unique: 'global' }], +}; +/** The installation-wide uniques the two package bodies declare, core first. */ +const COMPOSED_FINDINGS = ['crm_account:field:tax_id', 'crm_order:index:external_id']; + +/** + * A composed artifact: `packages[]` and no top-level `translations` or + * `objects`. The order module is listed FIRST and depends on the core one, so + * the artifact's package order (core, then orders) is not its array order. + */ +const composed = (version = '6.3.0') => ({ + id: SUITE, + name: 'Suite', + version, + type: 'app', + packages: [ + body(`${SUITE}.o2c`, version, ORDER, [O2C_ZH], { [`${SUITE}.core`]: '*' }), + body(`${SUITE}.core`, version, ACCOUNT, [CORE_ZH]), + ], +}); + +const FLAT = { + id: 'com.example.flat', + version: '1.0.0', + objects: [ACCOUNT], + translations: [CORE_ZH], +}; + +function makeRawApp() { + const routes = new Map(); + return { + routes, + get: (p: string, h: Handler) => routes.set(`GET ${p}`, h), + post: (p: string, h: Handler) => routes.set(`POST ${p}`, h), + delete: (p: string, h: Handler) => routes.set(`DELETE ${p}`, h), + }; +} + +function makeC(reqBody: any) { + const json = vi.fn((payload: any, status?: number) => ({ payload, status: status ?? 200 })); + return { + req: { + url: `http://localhost:3000${ROUTE}`, + raw: new Request(`http://localhost:3000${ROUTE}`), + json: async () => reqBody, + param: () => undefined, + header: () => undefined, + query: () => undefined, + }, + json, + }; +} + +/** One runtime over the ledger directory: a fresh plugin, booted through `kernel:ready`. */ +async function boot(dir: string) { + const rawApp = makeRawApp(); + const hooks = new Map(); + /** Every bundle the i18n service was handed, as `locale:labels`, in order. */ + const loaded: string[] = []; + const services: Record = { + manifest: { register: vi.fn() }, + auth: installerAuthService(), + objectql: withInstallerGrants({ syncSchemas: async () => undefined }), + metadata: {}, + i18n: { + loadTranslations: (locale: string, data: any) => { + const labels = Object.values(data?.objects ?? {}).map((o: any) => o.label).join('|'); + loaded.push(`${locale}:${labels}`); + }, + }, + }; + const ctx = { + hook: (e: string, h: any) => hooks.set(e, h), + getService: (name: string) => { + if (name === 'http-server') return { getRawApp: () => rawApp }; + const svc = services[name]; + if (svc === undefined) throw new Error(`no ${name}`); + return svc; + }, + registerService: (name: string, svc: unknown) => { services[name] = svc; }, + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, + }; + const plugin = new MarketplaceInstallLocalPlugin({ controlPlaneUrl: 'off', storageDir: dir }); + await plugin.start(ctx as any); + await hooks.get('kernel:ready')?.(); + return { + install: (reqBody: Record) => rawApp.routes.get(`POST ${ROUTE}`)!(makeC(reqBody)), + loaded, + ctx, + }; +} + +let dir: string; +let posture: string | undefined; +let restoreOwner: () => void; +beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), 'mil-composed-reads-')); + posture = process.env.OS_TENANCY_POSTURE; + // The request carries no organization, and the ceremony runs walled: the + // installer is the deployment's declared platform owner. + restoreOwner = declareInstallerPlatformOwner('admin'); +}); +afterEach(() => { + restoreOwner(); + rmSync(dir, { recursive: true, force: true }); + if (posture === undefined) delete process.env.OS_TENANCY_POSTURE; + else process.env.OS_TENANCY_POSTURE = posture; + vi.restoreAllMocks(); +}); + +describe('a composed bundle: the side effects load every package body\'s translations, in package order', () => { + it('install loads each package body\'s locale bundles, core before the module that depends on it', async () => { + const rt = await boot(dir); + const res = await rt.install({ manifest: composed() }); + expect(res.status, JSON.stringify(res.payload)).toBe(200); + expect(res.payload.data.translationsLoaded).toBe(3); + expect(rt.loaded).toEqual(['zh-CN:客户', 'zh-CN:订单', 'ja-JP:注文']); + }); + + it('a restart loads them again from the ledger', async () => { + const first = await boot(dir); + expect((await first.install({ manifest: composed() })).status).toBe(200); + + const restarted = await boot(dir); + expect(restarted.loaded).toEqual(['zh-CN:客户', 'zh-CN:订单', 'ja-JP:注文']); + }); +}); + +describe('controls: a body that carries top-level translations reads that list, once', () => { + it('a flat bundle loads its top-level translations, unchanged', async () => { + const rt = await boot(dir); + const res = await rt.install({ manifest: FLAT }); + expect(res.status, JSON.stringify(res.payload)).toBe(200); + expect(res.payload.data.translationsLoaded).toBe(1); + expect(rt.loaded).toEqual(['zh-CN:客户']); + }); + + it('a composed artifact that still carries its flattened top-level translations loads that union once, never per package as well', async () => { + const rt = await boot(dir); + const res = await rt.install({ manifest: { ...composed(), translations: [CORE_ZH, O2C_ZH] } }); + expect(res.status, JSON.stringify(res.payload)).toBe(200); + expect(res.payload.data.translationsLoaded).toBe(3); + expect(rt.loaded).toEqual(['zh-CN:客户', 'zh-CN:订单', 'ja-JP:注文']); + }); +}); + +describe("ADR-0120 D5e — a composed bundle installed into an 'isolated' environment", () => { + it('stops on the installation-wide uniques every package body declares, core first', async () => { + process.env.OS_TENANCY_POSTURE = 'isolated'; + const rt = await boot(dir); + + const res = await rt.install({ manifest: composed() }); + + expect(res.status, JSON.stringify(res.payload)).toBe(409); + expect(res.payload.error.code).toBe('UNIQUE_SCOPE_CONFIRMATION_REQUIRED'); + expect(res.payload.error.details.findings.map((f: any) => f.id)).toEqual(COMPOSED_FINDINGS); + expect(new LocalManifestSource(dir).read(SUITE).entry).toBeNull(); + }); + + it('confirming records the attestation for every package body\'s uniques and installs', async () => { + process.env.OS_TENANCY_POSTURE = 'isolated'; + const rt = await boot(dir); + + const res = await rt.install({ manifest: composed(), confirmGlobalUniques: true }); + + expect(res.status, JSON.stringify(res.payload)).toBe(200); + expect(new LocalManifestSource(dir).read(SUITE).entry!.globalUniqueAttestation).toMatchObject({ + posture: 'isolated', + confirmed: COMPOSED_FINDINGS.slice().sort(), + }); + }); + + it('control: a composed artifact that still carries its flattened top-level objects lists each unique once', async () => { + process.env.OS_TENANCY_POSTURE = 'isolated'; + const rt = await boot(dir); + + const res = await rt.install({ manifest: { ...composed(), objects: [ACCOUNT, ORDER] } }); + + expect(res.status, JSON.stringify(res.payload)).toBe(409); + expect(res.payload.error.details.findings.map((f: any) => f.id)).toEqual(COMPOSED_FINDINGS); + }); + + it('a composed artifact whose packages the resolver refuses is answered with that refusal, and nothing is written', async () => { + process.env.OS_TENANCY_POSTURE = 'isolated'; + const rt = await boot(dir); + + const res = await rt.install({ manifest: { ...composed(), packages: 'not-a-list' } }); + + expect(res.status, JSON.stringify(res.payload)).toBe(422); + expect(res.payload.success).toBe(false); + expect(res.payload.error.code).toBe('INVALID_ARTIFACT_PACKAGES'); + expect(new LocalManifestSource(dir).read(SUITE).entry).toBeNull(); + }); +}); diff --git a/packages/cloud-connection/src/marketplace-install-local-plugin.ts b/packages/cloud-connection/src/marketplace-install-local-plugin.ts index f8c52c477ed..3b52c79f674 100644 --- a/packages/cloud-connection/src/marketplace-install-local-plugin.ts +++ b/packages/cloud-connection/src/marketplace-install-local-plugin.ts @@ -2936,7 +2936,26 @@ export class MarketplaceInstallLocalPlugin implements Plugin { // everyone. const previousLookup = this.ledger.read(manifestId); const previousEntry = previousLookup.entry; - const gate = this.evaluateGlobalUniqueGate(manifest, previousEntry, body, userId); + // + // [#34407] The gate reads every package body's objects, so a + // `packages` the resolver refuses (ADR-0112 `code` + its declared + // 4xx status, the refusal the `manifest` service raises on the same + // bytes) is answered here, before anything is written — never + // thrown out of the door. An undeclared throw stays a throw. + let gate: ReturnType; + try { + gate = this.evaluateGlobalUniqueGate(manifest, previousEntry, body, userId); + } catch (err: any) { + const refusal = resolveThrownHttpError(err); + if (refusal.declaredStatus === undefined || refusal.status >= 500) throw err; + return c.json({ + success: false, + error: { + code: refusal.code, + message: `Package '${manifestId}' cannot be installed: ${String(err?.message ?? err)} Nothing was installed or written.`, + }, + }, refusal.status); + } if (gate.blocked) { ctx.logger?.warn?.( `[MarketplaceInstallLocal] install of ${manifestId} stopped by the ADR-0120 D5e posture gate ` + @@ -4608,7 +4627,15 @@ export class MarketplaceInstallLocalPlugin implements Plugin { return { blocked: false, posture, pending: [], ...(carried ? { attestation: carried } : {}) }; } - const findings = collectGlobalUniques(manifest?.objects); + // [#34407, ADR-0130 D4] The objects of every package this artifact + // delivers, through the one reader of a package-owned collection: the + // top-level `objects` when the body carries them (a flat bundle, or a + // composed artifact's flattened union — read once), otherwise every + // package body's, in package order. A composed bundle carries no + // top-level `objects`, so the top-level read listed nothing and its + // installation-wide uniques installed unasked. The resolver's refusal + // of a malformed `packages` propagates to the caller, which answers it. + const findings = collectGlobalUniques(resolveStackCollection(manifest, 'objects')); const pending = unconfirmedGlobalUniques(findings, carried, posture); if (pending.length === 0) { return { blocked: false, posture, pending: [], ...(carried ? { attestation: carried } : {}) }; @@ -6976,9 +7003,11 @@ export class MarketplaceInstallLocalPlugin implements Plugin { * Replicate the start-time side-effects that AppPlugin runs for * statically-declared apps but the `manifest` service does NOT: * - * 1. Load `manifest.translations` (array of `Record`) - * into the i18n service — auto-creating an in-memory fallback if - * none is registered, matching AppPlugin's behaviour. + * 1. Load the package's `translations` (arrays of `Record`: + * the top-level list, or every package body's in package order for a + * composed bundle, #34407) into the i18n service — auto-creating an + * in-memory fallback if none is registered, matching AppPlugin's + * behaviour. * * 2. Merge the package's seed datasets ({@link seedDatasetsOf}: the * top-level `data`, or every package body's in package order for a @@ -7010,9 +7039,17 @@ export class MarketplaceInstallLocalPlugin implements Plugin { let seedSummary: any = { mode: 'skipped', reason: 'no-datasets' }; // ── 1. i18n bundles ───────────────────────────────────────────── + // [#34407, ADR-0130 D4] `translations` through the one reader of a + // package-owned collection, as the seed datasets below: the top-level + // list when the body carries one (a flat bundle, or a composed artifact + // that still carries its flattened union — read once), otherwise every + // package body's, in package order. A composed bundle carries no + // top-level `translations`, and the top-level read loaded none of its + // labels. A resolver refusal lands in this step's catch, as any other + // failure of it does. try { const bundles: Array> = []; - if (Array.isArray(manifest?.translations)) bundles.push(...manifest.translations); + bundles.push(...(resolveStackCollection(manifest, 'translations') as Array>)); if (Array.isArray(manifest?.i18n)) bundles.push(...manifest.i18n); if (bundles.length > 0) {