From 31e95a8dc7afeff196f63be946a4815d634e3265 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 03:29:02 +0000 Subject: [PATCH 1/3] fix(cloud-connection): install-local connects and judges a composed bundle's package-body datasources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Step 2a, the 4b′ connect (install and upgrade) and the kernel:ready rehydrate read datasources and objects through resolveStackCollection, so a composed bundle hands connectPackageDatasources every package body's datasources and objects in package order, and a body's datasource name is judged against held names before any write. The install answer's credentialRefs read every body's connectors the same way. The dead top-level array i18n read in applySideEffects is removed. Claude-Session: https://claude.ai/code/session_01ADHkmRXuRn93HphKp7kXEz Co-authored-by: Claude --- ...all-local-composed-top-level-reads.test.ts | 179 ++++++++++++++++++ .../src/marketplace-install-local-plugin.ts | 80 ++++++-- 2 files changed, 244 insertions(+), 15 deletions(-) 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 index 4960c3717a3..a8ac14013b3 100644 --- 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 @@ -29,6 +29,15 @@ import { mkdtempSync, rmSync } from 'node:fs'; import { join } from 'node:path'; import { tmpdir } from 'node:os'; +/** + * [#34431] What the runtime's datasource path is handed, one entry per call: + * the datasource names, the object names, each datasource's owner and whether + * the call was an installed one. + */ +const connectCalls = vi.hoisted(() => [] as Array<{ datasources: string[]; objects: string[]; owners: string[]; installed: boolean }>); +/** [#34431] The datasource names another holder owns, as the runtime's judge answers them. */ +const heldDatasources = vi.hoisted(() => new Map()); + vi.mock('@objectstack/runtime', () => ({ SeedLoaderService: class { async load() { return { summary: { totalInserted: 0, totalUpdated: 0, totalSkipped: 0 }, errors: [] }; } @@ -37,6 +46,17 @@ vi.mock('@objectstack/runtime', () => ({ resolveSeedLocaleForLoad: async () => undefined, appDefaultSeedLocale: () => undefined, describeSeedLocale: () => 'none', + connectPackageDatasources: async (_ctx: unknown, options: any) => { + connectCalls.push({ + datasources: options.datasources.map((d: any) => d.name), + objects: options.objects.map((o: any) => o.name), + owners: options.owned.map((o: any) => o.owner.packageId), + installed: options.installed === true, + }); + return options.datasources.map((d: any) => ({ name: d.name, status: 'connected' })); + }, + datasourceNameHolder: async (_ctx: unknown, _appId: string, name: string) => + heldDatasources.has(name) ? { status: 'held', holder: heldDatasources.get(name) } : { status: 'free' }, })); import { MarketplaceInstallLocalPlugin } from './marketplace-install-local-plugin.js'; @@ -132,6 +152,8 @@ async function boot(dir: string) { auth: installerAuthService(), objectql: withInstallerGrants({ syncSchemas: async () => undefined }), metadata: {}, + // [#34431] Every credential reference is unset: the install answer names each one. + automation: { listConnectorCredentialRefs: async () => [] }, i18n: { loadTranslations: (locale: string, data: any) => { const labels = Object.values(data?.objects ?? {}).map((o: any) => o.label).join('|'); @@ -169,6 +191,8 @@ beforeEach(() => { // The request carries no organization, and the ceremony runs walled: the // installer is the deployment's declared platform owner. restoreOwner = declareInstallerPlatformOwner('admin'); + connectCalls.length = 0; + heldDatasources.clear(); }); afterEach(() => { restoreOwner(); @@ -262,3 +286,158 @@ describe("ADR-0120 D5e — a composed bundle installed into an 'isolated' enviro expect(new LocalManifestSource(dir).read(SUITE).entry).toBeNull(); }); }); + +// ── [#34431] Datasources and connectors ──────────────────────────────────── + +const ERP_DB = { name: 'erp_db', driver: 'memory', config: {} }; +const WMS_DB = { name: 'wms_db', driver: 'memory', config: {} }; +const ERP_API = { name: 'erp_api', label: 'ERP API', type: 'api', provider: 'rest', providerConfig: { baseUrl: 'https://erp.example.test' }, auth: { type: 'bearer', credentialRef: 'ERP_TOKEN' } }; +const WMS_API = { name: 'wms_api', label: 'WMS API', type: 'api', provider: 'rest', providerConfig: { baseUrl: 'https://wms.example.test' }, auth: { type: 'bearer', credentialRef: 'WMS_TOKEN' } }; + +const sourceBody = ( + id: string, + version: string, + object: Record, + datasources: unknown[], + connectors: unknown[], + dependencies?: Record, +) => ({ + manifest: { + id, + name: id, + version, + type: 'module', + ...(dependencies ? { dependencies } : {}), + objects: [object], + datasources, + connectors, + }, +}); + +/** + * A composed artifact whose package bodies each declare a datasource and a + * connector, with no top-level `datasources`, `connectors` or `objects`. The + * order module is listed FIRST and depends on the core one, so package order + * (core, then orders) is not array order. + */ +const composedSources = (version = '2.0.0', extra: Record = {}) => ({ + id: SUITE, + name: 'Suite', + version, + type: 'app', + packages: [ + sourceBody(`${SUITE}.o2c`, version, ORDER, [WMS_DB], [WMS_API], { [`${SUITE}.core`]: '*' }), + sourceBody(`${SUITE}.core`, version, ACCOUNT, [ERP_DB], [ERP_API]), + ], + ...extra, +}); + +/** The flat control: the same declarations at the top level, in package order. */ +const flatSources = (version = '2.0.0') => ({ + id: SUITE, + name: 'Suite', + version, + type: 'app', + objects: [ACCOUNT, ORDER], + datasources: [ERP_DB, WMS_DB], + connectors: [ERP_API, WMS_API], +}); + +const CONNECTED = [{ name: 'erp_db', status: 'connected' }, { name: 'wms_db', status: 'connected' }]; +const HANDED = { datasources: ['erp_db', 'wms_db'], objects: ['crm_account', 'crm_order'], owners: [SUITE, SUITE], installed: true }; + +describe('[#34431] a composed bundle: install connects every package body\'s datasources, in package order', () => { + it('install hands connectPackageDatasources every body\'s datasources and objects, core first, and the answer lists them', async () => { + const rt = await boot(dir); + const res = await rt.install({ manifest: composedSources() }); + expect(res.status, JSON.stringify(res.payload)).toBe(200); + expect(connectCalls).toEqual([HANDED]); + expect(res.payload.data.datasources).toEqual(CONNECTED); + }); + + it('a composed bundle and its flat control answer the same datasources and hand connectPackageDatasources the same lists', async () => { + const composedRt = await boot(dir); + const composedRes = await composedRt.install({ manifest: composedSources() }); + const composedCalls = connectCalls.splice(0); + + const flatDir = mkdtempSync(join(tmpdir(), 'mil-composed-reads-flat-')); + try { + const flatRt = await boot(flatDir); + const flatRes = await flatRt.install({ manifest: flatSources() }); + expect(flatRes.status, JSON.stringify(flatRes.payload)).toBe(200); + expect(composedRes.status, JSON.stringify(composedRes.payload)).toBe(200); + expect(composedCalls).toEqual(connectCalls); + expect(composedRes.payload.data.datasources).toEqual(flatRes.payload.data.datasources); + expect(composedRes.payload.data.credentialRefs).toEqual(flatRes.payload.data.credentialRefs); + } finally { + rmSync(flatDir, { recursive: true, force: true }); + } + }); + + it('an upgrade connects the new version\'s datasources across every body', async () => { + const rt = await boot(dir); + expect((await rt.install({ manifest: composedSources('2.0.0') })).status).toBe(200); + connectCalls.length = 0; + + const res = await rt.install({ manifest: composedSources('2.1.0') }); + expect(res.status, JSON.stringify(res.payload)).toBe(200); + expect(connectCalls).toEqual([HANDED]); + expect(res.payload.data.datasources).toEqual(CONNECTED); + }); + + it('a restart\'s rehydrate connects them again from the ledger', async () => { + const first = await boot(dir); + expect((await first.install({ manifest: composedSources() })).status).toBe(200); + connectCalls.length = 0; + + await boot(dir); + expect(connectCalls).toEqual([HANDED]); + }); + + it('the answer names the connector credentials every package body declares', async () => { + const rt = await boot(dir); + const res = await rt.install({ manifest: composedSources() }); + expect(res.status, JSON.stringify(res.payload)).toBe(200); + expect(res.payload.data.credentialRefs).toEqual([ + { ref: 'ERP_TOKEN', connectors: ['erp_api'], status: 'missing', source: null }, + { ref: 'WMS_TOKEN', connectors: ['wms_api'], status: 'missing', source: null }, + ]); + }); + + it('control: a composed artifact that still carries its flattened top-level datasources connects that union once', async () => { + const rt = await boot(dir); + const res = await rt.install({ + manifest: composedSources('2.0.0', { objects: [ACCOUNT, ORDER], datasources: [ERP_DB, WMS_DB] }), + }); + expect(res.status, JSON.stringify(res.payload)).toBe(200); + expect(connectCalls).toEqual([HANDED]); + expect(res.payload.data.datasources).toEqual(CONNECTED); + }); +}); + +describe('[#34431] step 2a judges a composed body\'s datasource names as a flat bundle\'s', () => { + it('a name another holder owns, declared by a package body, refuses the install 409 before any write', async () => { + heldDatasources.set('wms_db', "the datasource 'wms_db' created in Setup"); + const rt = await boot(dir); + + const res = await rt.install({ manifest: composedSources() }); + + expect(res.status, JSON.stringify(res.payload)).toBe(409); + expect(res.payload.success).toBe(false); + expect(res.payload.error.code).toBe('RESOURCE_CONFLICT'); + expect(res.payload.error.details).toEqual({ datasource: 'wms_db' }); + expect(new LocalManifestSource(dir).read(SUITE).entry).toBeNull(); + expect(connectCalls).toEqual([]); + }); + + it('control: the flat bundle declaring the same name is refused the same way', async () => { + heldDatasources.set('wms_db', "the datasource 'wms_db' created in Setup"); + const rt = await boot(dir); + + const res = await rt.install({ manifest: flatSources() }); + + expect(res.status, JSON.stringify(res.payload)).toBe(409); + expect(res.payload.error.code).toBe('RESOURCE_CONFLICT'); + expect(res.payload.error.details).toEqual({ datasource: 'wms_db' }); + }); +}); diff --git a/packages/cloud-connection/src/marketplace-install-local-plugin.ts b/packages/cloud-connection/src/marketplace-install-local-plugin.ts index c6ebcab0b33..8405743f5ad 100644 --- a/packages/cloud-connection/src/marketplace-install-local-plugin.ts +++ b/packages/cloud-connection/src/marketplace-install-local-plugin.ts @@ -1330,16 +1330,32 @@ function planUnitWithdrawal( } /** - * [#28039] The datasources a package body declares, as `AppPlugin` reads a - * stack's: a list, or a map keyed by name. + * [#28039] The datasources a package artifact declares, as `AppPlugin` reads a + * stack's: a list, or a map keyed by name. The ONE reading for every door of + * this plugin that judges or connects them — the held-name check (2a), the + * connect of an install or an upgrade (4b′) and the `kernel:ready` rehydrate. + * + * [#34431, ADR-0130 D4] The list is read through `resolveStackCollection`, as + * the seed datasets are ({@link seedDatasetsOf}): the top-level list when the + * artifact carries one (a flat bundle, or a composed artifact that still + * carries its flattened union — read once, never again per package), + * otherwise every package body's, in the artifact's package order. A composed + * bundle carries no top-level `datasources`, and a top-level-only read + * connected none of its package bodies' datasources and judged none of their + * names. Entries are kept as declared, a name declared twice included, as a + * flat bundle's pair is: the runtime's path judges the pair. + * + * The map form is read at the top level only, where a flat bundle may carry + * it: a package body's `datasources` is a list by the package entry schema, + * and the resolver refuses a body that carries a map. + * + * @throws The resolver's ADR-0112 refusal for a malformed `packages` + * (`INVALID_ARTIFACT_PACKAGES`, `INVALID_ARTIFACT_PACKAGE_ENTRY`, …). */ -function declaredDatasources(body: unknown): any[] { - const defs = isPlainObject(body) ? body.datasources : undefined; - return Array.isArray(defs) - ? defs - : isPlainObject(defs) - ? Object.entries(defs).map(([name, def]) => ({ name, ...(def as any) })) - : []; +function declaredDatasources(artifact: unknown): any[] { + const top = isPlainObject(artifact) ? artifact.datasources : undefined; + if (isPlainObject(top)) return Object.entries(top).map(([name, def]) => ({ name, ...(def as any) })); + return resolveStackCollection(artifact, 'datasources'); } /** @@ -3094,7 +3110,19 @@ export class MarketplaceInstallLocalPlugin implements Plugin { // engine as well (a driver the legacy `onEnable` bridge registered // has no metadata item), and a holder that cannot be read refuses // `503`: a check that did not run is never taken for "no holder". - const heldDatasource = await this.findHeldDatasourceName(ctx, manifest, manifestId); + // [#34431] The names are every package body's on a composed + // bundle, so a `packages` the resolver refuses is answered here + // with that refusal, before any write + // ({@link declaredRefusalAnswer}), as 2b and 2c″ answer it. An + // undeclared throw stays a throw. + let heldDatasource: Awaited>; + try { + heldDatasource = await this.findHeldDatasourceName(ctx, manifest, manifestId); + } catch (err: unknown) { + const refused = declaredRefusalAnswer(manifestId, err); + if (!refused) throw err; + return c.json(refused.body, refused.status); + } if (heldDatasource && 'reading' in heldDatasource) { ctx.logger?.warn?.( `[MarketplaceInstallLocal] install of ${manifestId} refused: whether another holder owns its datasource ` @@ -6886,6 +6914,13 @@ export class MarketplaceInstallLocalPlugin implements Plugin { * `failed-credentials` verdict, which the datasource answers and a read of * a bound object names (`ERR_DATASOURCE_UNAVAILABLE`). * + * [#34431, ADR-0130 D4] The datasources and the objects are read through + * the one reader of a package-owned collection ({@link declaredDatasources}, + * `resolveStackCollection`), so a composed bundle hands the path every + * package body's, in package order, as a flat bundle hands its own. Every + * one is stamped with `manifestId`, the id the ledger entry, the uninstall + * cleanup and the held-name judge key on. + * * Answers each declared datasource's verdict, or `undefined` when the * package declares none (or the runtime predates the path). Never throws: * the package is installed either way. @@ -6896,7 +6931,20 @@ export class MarketplaceInstallLocalPlugin implements Plugin { manifestId: string, version: string | undefined, ): Promise | undefined> => { - const declared = declaredDatasources(manifest); + let declared: any[]; + let objects: unknown[]; + try { + declared = declaredDatasources(manifest); + objects = resolveStackCollection(manifest, 'objects'); + } catch (err: any) { + // The register before this step resolved the same bytes, so this + // is not expected; nothing is connected, and the log says so. + ctx.logger?.error?.( + `[MarketplaceInstallLocal] the datasources of ${manifestId} could not be read, so none is registered or connected: ` + + `${err?.message ?? err}. The objects bound to them have no driver until a fixed version is installed.`, + ); + return undefined; + } let connect: typeof import('@objectstack/runtime')['connectPackageDatasources'] | undefined; try { const mod: any = await import('@objectstack/runtime'); @@ -6911,7 +6959,6 @@ export class MarketplaceInstallLocalPlugin implements Plugin { } return undefined; } - const objects = isPlainObject(manifest) && Array.isArray(manifest.objects) ? manifest.objects : []; try { // Called with an empty list too: a reinstall whose new version // declares none withdraws what the previous version registered. @@ -7186,14 +7233,18 @@ export class MarketplaceInstallLocalPlugin implements Plugin { * automation service, or one that cannot list, the status is `unknown`. * `writable` is the references the credential door can store, as the * listing says; `undefined` when there is no listing to say it. + * + * [#34431, ADR-0130 D4] `connectors` through `resolveStackCollection`: the + * top-level list when the body carries one, otherwise every package + * body's, in package order. A composed bundle carries no top-level + * `connectors`, and the top-level read named none of its credentials. */ private describeInstalledCredentialRefs = async ( ctx: PluginContext, converted: unknown, ): Promise<{ entries: InstalledCredentialRef[]; writable?: ReadonlySet } | undefined> => { - const connectors = (converted as { connectors?: unknown } | null)?.connectors; const declared = new Map(); - for (const raw of Array.isArray(connectors) ? connectors : []) { + for (const raw of resolveStackCollection(converted, 'connectors')) { const entry = raw as { name?: unknown; provider?: unknown; auth?: { credentialRef?: unknown } } | null; if (typeof entry?.name !== 'string' || typeof entry.provider !== 'string') continue; const ref = entry.auth?.credentialRef; @@ -7327,7 +7378,6 @@ export class MarketplaceInstallLocalPlugin implements Plugin { try { const bundles: Array> = []; bundles.push(...(resolveStackCollection(manifest, 'translations') as Array>)); - if (Array.isArray(manifest?.i18n)) bundles.push(...manifest.i18n); if (bundles.length > 0) { let i18nService: any; From c72c1a340cfe17b83882c8c701e43e3c257f6c7b Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 03:31:12 +0000 Subject: [PATCH 2/3] chore(cloud-connection): changeset and harness header for composed datasource reads Claude-Session: https://claude.ai/code/session_01ADHkmRXuRn93HphKp7kXEz Co-authored-by: Claude --- .changeset/34431-composed-bundle-datasources.md | 16 ++++++++++++++++ ...nstall-local-composed-top-level-reads.test.ts | 7 +++++++ 2 files changed, 23 insertions(+) create mode 100644 .changeset/34431-composed-bundle-datasources.md diff --git a/.changeset/34431-composed-bundle-datasources.md b/.changeset/34431-composed-bundle-datasources.md new file mode 100644 index 00000000000..0aca9238760 --- /dev/null +++ b/.changeset/34431-composed-bundle-datasources.md @@ -0,0 +1,16 @@ +--- +'@objectstack/cloud-connection': patch +--- + +fix(cloud-connection): install-local connects a composed bundle's package-body datasources, judges their names before any write, and names their connector credentials + +Clause-②: no +Metadata-protocol: none + +A composed bundle (`dist/objectstack.json` with `packages[]`, ADR-0130 D4) carries its datasources, connectors and objects in each package body, and none at the top level. `POST /api/v1/marketplace/install-local` read those three from the top level only. A composed install answered 200, but `connectPackageDatasources` was handed no datasources and no objects, so a datasource declared in a package body was never registered or connected, and the objects bound to it had no driver. The install answer carried no `datasources` and no `credentialRefs`. The held-name check (step 2a) never saw a body's datasource names either. + +Install, upgrade and the restart's rehydrate now read datasources and objects through `resolveStackCollection`, the reader the seed datasets and translations already use: the top-level list when the bundle carries one, otherwise every package body's, in package order. A composed bundle now hands the runtime the same lists as the same declarations in a flat bundle, and the answer lists the same `datasources` and `credentialRefs`. + +Step 2a now judges every package body's datasource names. A composed install that declares a name another holder owns is refused `409 RESOURCE_CONFLICT` before any write, as a flat bundle declaring that name already was. Before this, such an install answered 200 and left the name with its holder. A `packages` the artifact resolver refuses is answered at this step with the resolver's own `422`, and nothing is written. + +The side effects no longer read a top-level array `i18n` as extra translation bundles. `i18n` is a configuration object in the spec, and the boot path never read an array there. 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 index a8ac14013b3..ac58b8aca88 100644 --- 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 @@ -22,6 +22,13 @@ * 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. + * + * [#34431] The same holds for the datasources and objects the install, the + * upgrade and the rehydrate hand `connectPackageDatasources`, for the + * datasource names step 2a judges against held names, and for the connectors + * whose credentials the install answer names: a composed bundle and the flat + * control carrying the same declarations hand the runtime the same lists and + * answer the same `datasources` and `credentialRefs`. */ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; From eaade3078babacfd0fcda611b8771a31937a06a6 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 03:34:10 +0000 Subject: [PATCH 3/3] fix(cloud-connection): type the resolved objects list as the runtime path reads it Claude-Session: https://claude.ai/code/session_01ADHkmRXuRn93HphKp7kXEz Co-authored-by: Claude --- .../cloud-connection/src/marketplace-install-local-plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cloud-connection/src/marketplace-install-local-plugin.ts b/packages/cloud-connection/src/marketplace-install-local-plugin.ts index 8405743f5ad..c81043840d2 100644 --- a/packages/cloud-connection/src/marketplace-install-local-plugin.ts +++ b/packages/cloud-connection/src/marketplace-install-local-plugin.ts @@ -6932,7 +6932,7 @@ export class MarketplaceInstallLocalPlugin implements Plugin { version: string | undefined, ): Promise | undefined> => { let declared: any[]; - let objects: unknown[]; + let objects: any[]; try { declared = declaredDatasources(manifest); objects = resolveStackCollection(manifest, 'objects');