Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .changeset/runtime-export-artifact-collections-reader.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@objectstack/runtime': patch
---

feat(runtime): export `resolveArtifactCollections`, the reader of every collection of a composed suite

Clause-②: yes (widening)
Metadata-protocol: none

`@objectstack/runtime` now exports `resolveArtifactCollections` (#32827). A suite composed with `composeStacks(packageStacks, { manifest: 'preserve' })` keeps each package's collections in that package's body, with no flattened copy, so `artifact.objects` alone reads none of them. `resolveArtifactCollections(artifact)` returns the artifact with every package-owned collection — objects, apps, flows, translations, hooks and the rest — merged across the top level and every `packages[]` body, the same merge the runtime's boot paths use. It is the route for an app's tooling (a whole-suite test view, a lint pass) that used a flat `composeStacks(packageStacks)` only to get one view of the suite's collections: a flat composition puts every package's app under one package id, which `composeStacks` refuses for a suite (`STACK_SINGLE_APP_VIOLATION`, ADR-0019 D3 as amended).

```ts
import { composeStacks } from '@objectstack/spec';
import { resolveArtifactCollections } from '@objectstack/runtime';

const suite = resolveArtifactCollections(composeStacks(packageStacks, { manifest: 'preserve' }));
```

Nothing that existed changes: an artifact with no `packages[]` comes back as the same object.
112 changes: 112 additions & 0 deletions packages/runtime/src/artifact-collections.public-reader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* `resolveArtifactCollections` as PUBLIC surface (#32827) — pinned from the
* consumer's side.
*
* The consumer: an app suite of one `app` package and several `module`
* packages wants ONE view of every collection of the suite (a test helper, a
* lint pass). It used to get that from a flat `composeStacks(packageStacks)`;
* a flat composition puts every package's app under one package id, which
* `composeStacks` refuses for a suite (`STACK_SINGLE_APP_VIOLATION`, ADR-0019
* D3 as amended). The shape-correct composition is `manifest: 'preserve'`,
* which keeps each collection in its package's body and no flattened copy
* (ADR-0130 D4), and reading every collection out of that is this reader.
*
* So the pin is: `resolveArtifactCollections(composeStacks(stacks, { manifest:
* 'preserve' }))`, with the reader imported from the PACKAGE ENTRY
* (`./index`, which `dist/index.*` is built from) rather than its module,
* yields every input's objects, apps, flows, translations and hooks with the
* contents the flat composition gave.
*
* The flat baseline is taken WITHOUT the apps: a flat composition of the
* app-less inputs is one package with no app, which the single-app rule
* leaves alone, so this pin keeps reading the same baseline once that rule
* ships. For `apps` the flat composition's answer is the inputs' apps
* concatenated in input order — that is all it ever did with them — and that
* is the expectation written here.
*/

import { describe, it, expect } from 'vitest';

// The package entry reaches `@objectstack/metadata` through dynamic imports;
// pay that load during collection, not inside a clocked case
// (`check:test-source-alias`).
import '@objectstack/metadata';
import { composeStacks, defineStack } from '@objectstack/spec';

import * as runtimeEntry from './index';
import { resolveArtifactCollections as moduleReader } from './artifact-collections';

const { resolveArtifactCollections } = runtimeEntry;

type Raw = Record<string, unknown>;

/** One package of the suite, every collection the consumer reads declared. */
function pkg(id: string, type: 'app' | 'module', slug: string, withApps = true): Raw {
return {
manifest: { id, name: slug, version: '1.0.0', type },
objects: [{ name: `${slug}_record`, label: `${slug} record`, fields: { title: { type: 'text', label: 'Title' } } }],
...(withApps ? { apps: [{ name: `${slug}_app`, label: `${slug} app` }] } : {}),
flows: [{ name: `${slug}_flow`, label: `${slug} flow`, type: 'autolaunched', nodes: [], edges: [] }],
translations: [{ en: { objects: { [`${slug}_record`]: { label: `${slug} record` } } } }],
hooks: [{ name: `${slug}_hook`, object: `${slug}_record`, events: ['beforeInsert'], handler: `${slug}_stamp` }],
};
}

/** The suite: two module packages and the app package that surfaces them. */
function suite(withApps = true): Raw[] {
return [
pkg('com.example.sales', 'module', 'sales', withApps),
pkg('com.example.service', 'module', 'service', withApps),
pkg('com.example.suite', 'app', 'suite', withApps),
];
}

const define = (raw: Raw) => defineStack(raw as never, { strict: false });

const COLLECTIONS = ['objects', 'apps', 'flows', 'translations', 'hooks'] as const;

describe('resolveArtifactCollections — the public reader of a composed suite (#32827)', () => {
it('is exported from the package entry, and it is the module\'s own reader, not a second copy of the merge', () => {
expect(typeof resolveArtifactCollections).toBe('function');
expect(resolveArtifactCollections).toBe(moduleReader);
});

it('reads every input\'s collections out of a `manifest: \'preserve\'` composition, with the contents a flat composition gave', () => {
const preserved = composeStacks(suite().map(define), { manifest: 'preserve' } as never) as Raw;

// Anti-vacuity: the preserve artifact carries the collections in its
// package bodies only, so a reader that skipped `packages[]` would
// answer nothing at all for them.
expect(Array.isArray(preserved.packages)).toBe(true);
expect((preserved.packages as unknown[]).length).toBe(3);
for (const key of COLLECTIONS) expect(preserved[key], key).toBeUndefined();

const read = resolveArtifactCollections(preserved) as Record<string, any[]>;

// The flat baseline, taken where the single-app rule leaves it alone
// (see the header): the same inputs without their apps.
const flat = composeStacks(suite(false).map(define)) as Record<string, any[]>;
const byName = (entries: any[] | undefined) => [...(entries ?? [])].sort((a, b) => String(a.name).localeCompare(String(b.name)));

for (const key of ['objects', 'flows', 'hooks'] as const) {
expect(byName(read[key]), key).toEqual(byName(flat[key]));
}
expect(read.translations).toEqual(flat.translations);

// Every input is answered, by name — so an equal-but-empty pair above
// cannot pass.
expect(read.objects.map((o) => o.name).sort()).toEqual(['sales_record', 'service_record', 'suite_record']);
expect(read.flows.map((f) => f.name).sort()).toEqual(['sales_flow', 'service_flow', 'suite_flow']);
expect(read.hooks.map((h) => h.name).sort()).toEqual(['sales_hook', 'service_hook', 'suite_hook']);
expect(read.translations).toHaveLength(3);

// `apps`: every input's app, each the entry its own stack declared —
// what a flat composition did with them (concatenation), read here
// while each app stays under its own package id.
const declaredApps = suite().map(define).flatMap((stack) => (stack as Raw).apps as any[]);
expect(byName(read.apps)).toEqual(byName(declaredApps));
expect(read.apps.map((a) => a.name).sort()).toEqual(['sales_app', 'service_app', 'suite_app']);
});
});
34 changes: 30 additions & 4 deletions packages/runtime/src/artifact-collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ let cachedCollectionKeys: readonly string[] | undefined;
* `ArtifactPackageSchema` — which embeds `AssembledPackageBodySchema` — so the
* shapes are built by the time the first call needs them.
*
* ⚠️ `export` here is MODULE scope, not package surface: this module is not
* named by `packages/runtime/src/index.ts`, so neither this function nor
* {@link resolveArtifactCollections} appears in `dist/index.d.ts` and neither is
* importable from `@objectstack/runtime`. The keyword is here because
* ⚠️ `export` here is MODULE scope, not package surface: `packages/runtime/src/index.ts`
* names {@link resolveArtifactCollections} from this module (#32827) and NOT
* this function, so it is absent from `dist/index.d.ts` and not importable from
* `@objectstack/runtime`. The keyword is here because
* `artifact-collections.test.ts` imports it by module path — the derivation is
* the part of this file that fails silently if it ever stops being a
* derivation, so it is pinned directly rather than inferred from a merge.
Expand Down Expand Up @@ -418,6 +418,32 @@ function mergeCollection(key: string, top: unknown, fromBodies: readonly Contrib
* Read `artifact` with every package-owned collection resolved across BOTH
* artifact shapes — the flattened top level and `packages[]` (ADR-0130 D4).
*
* ## Public reader (`@objectstack/runtime`, #32827)
*
* This is the ONE reader of "every collection of a composed suite", exported
* from the package entry so an app's own tooling (a test view of the whole
* suite, a lint pass, a generator) reads a multi-package artifact the way the
* boot does instead of re-implementing the merge:
*
* ```ts
* import { composeStacks } from '@objectstack/spec';
* import { resolveArtifactCollections } from '@objectstack/runtime';
*
* // Each package keeps its own id and its own app; the suite's collections
* // (objects, apps, flows, translations, hooks, …) are read across every body.
* const suite = resolveArtifactCollections(composeStacks(packageStacks, { manifest: 'preserve' }));
* suite.objects; // every input's objects
* suite.apps; // every input's apps, each still owned by its own package
* ```
*
* A `manifest: 'preserve'` composition carries each collection once, in its
* package's body, and no flattened copy — so reading `artifact.objects`
* directly sees none of it. A FLAT composition (the default `manifest: 'last'`)
* is not the way to get one view: it puts every input's apps under one package
* id, which `composeStacks` refuses for a suite (`STACK_SINGLE_APP_VIOLATION`,
* ADR-0019 D3 as amended). Merge order is the top level first, then each body
* in `resolveArtifactPackageOrder`'s registration order, as at boot.
*
* The returned value is a shallow copy whose envelope keys (`manifest`, `api`,
* `server`, `i18n`, `runtimeModule`, `onEnable`, `packages`) are the caller's
* own references, so it is a drop-in for the artifact at any read site.
Expand Down
7 changes: 7 additions & 0 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ export {
export { loadArtifactBundle, mergeRuntimeModule, isHttpUrl, readArtifactSource } from './load-artifact-bundle.js';
export type { LoadArtifactBundleOptions } from './load-artifact-bundle.js';

// The reader of every collection of a composed suite (#32827): a
// `composeStacks(stacks, { manifest: 'preserve' })` artifact keeps each
// collection in its package's body, and this is the one merge across the
// flattened top level and every `packages[]` body (ADR-0130 D4) — the same one
// the boot paths read. Exported so an app reads its suite without copying it.
export { resolveArtifactCollections } from './artifact-collections.js';

// Artifact-pinned boot (#8368) — `OS_ARTIFACT_URL`, the SRI-style `#sha256=`
// fragment pin, and the verified local materialisation the boot reads.
export {
Expand Down
Loading