From d11ad49a0b2949e2665c6180abd7ad512000ef4d Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 29 Nov 2026 16:47:53 +0000 Subject: [PATCH 1/3] fix(verify): a bootStack artifact boot registers the metadata door ahead of the app, as os dev does Co-authored-by: Claude Claude-Session: https://claude.ai/code/session_01ADHkmRar3Kkg6SpwVgaaVE --- .../src/harness.seed-extension-lookup.test.ts | 161 ++++++++++++++++++ packages/verify/src/harness.ts | 99 ++++++++--- 2 files changed, 240 insertions(+), 20 deletions(-) create mode 100644 packages/verify/src/harness.seed-extension-lookup.test.ts diff --git a/packages/verify/src/harness.seed-extension-lookup.test.ts b/packages/verify/src/harness.seed-extension-lookup.test.ts new file mode 100644 index 00000000000..ce9d8d6a08b --- /dev/null +++ b/packages/verify/src/harness.seed-extension-lookup.test.ts @@ -0,0 +1,161 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * #36274 — a `bootStack` boot of a compiled artifact seeds the way + * `objectstack dev` seeds it, so it can pin a seed defect the real boot shows. + * + * The shape is #35254's: a module package adds a lookup to the app package's + * object through `objectExtensions`, and seeds it by the target's natural key. + * On `os dev` the metadata service holds each object as its own package wrote + * it (the artifact door loads it before the app seeds), so the extension's + * lookup is in the engine's folded schema only, and the seed loader resolved + * the natural key only once it read the engine first (PR #36272). + * + * Measured before this change (`origin/main` eec2bc3e, the resolver of + * `8925d5bc^` put back): both `bootStack` legs below stored the quote's id, and + * the metadata service's copy read at seed time carried `quote_ref`. The + * `MetadataPlugin` that loads the artifact was registered after the app (a + * caller's, in the `extraPlugins` slot) or not composed at all (the + * `artifact` option), so the seed read ObjectQL's bridged, folded copy. A + * `createStandaloneStack` boot of the same artifact (the `os dev` composition) + * read `quote_ref` absent from that copy at seed time. + * + * Every leg has a CONTROL: `account_ref`, the lookup the owning package + * authored on the same object. The `metadata` assertion is the precondition: + * the boot holds the copy `os dev` holds, or the seed pin proves nothing. + */ + +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { MetadataPlugin } from '@objectstack/metadata'; +import { composeStacks, defineStack } from '@objectstack/spec'; +import { bootStack, type VerifyStack } from './harness.js'; + +const BOOT_TIMEOUT = 120_000; +const APP = 'com.example.sxl.app'; +const MOD = 'com.example.sxl.mod'; + +/** The two-package artifact, built again per call: the instance rule keys on identity. */ +function artifact(): Record { + const app = { + manifest: { id: APP, namespace: 'sxl', name: 'sxl_app', version: '1.0.0', type: 'app' }, + objects: [ + { name: 'sxl_account', label: 'Account', sharingModel: 'public_read_write', fields: { name: { type: 'text', label: 'Name' } } }, + { + name: 'sxl_order', + label: 'Order', + sharingModel: 'public_read_write', + fields: { + name: { type: 'text', label: 'Name' }, + account_ref: { type: 'lookup', label: 'Account', reference: 'sxl_account' }, + }, + }, + ], + data: [{ object: 'sxl_account', mode: 'upsert', externalId: 'name', records: [{ name: 'Acme' }] }], + }; + const mod = { + manifest: { + id: MOD, namespace: 'sxl', name: 'sxl_mod', version: '1.0.0', type: 'module', dependencies: { [APP]: '^1.0.0' }, + }, + objects: [{ name: 'sxl_quote', label: 'Quote', sharingModel: 'public_read_write', fields: { name: { type: 'text', label: 'Name' } } }], + objectExtensions: [{ extend: 'sxl_order', fields: { quote_ref: { type: 'lookup', label: 'Quote', reference: 'sxl_quote' } } }], + data: [ + { object: 'sxl_quote', mode: 'upsert', externalId: 'name', records: [{ name: 'Acme Quote' }] }, + { object: 'sxl_order', mode: 'upsert', externalId: 'name', records: [{ name: 'O-1', account_ref: 'Acme', quote_ref: 'Acme Quote' }] }, + ], + }; + // JSON, as an artifact is: the inputs carry no callable to lose. + return JSON.parse( + JSON.stringify( + composeStacks( + [defineStack(app as never), defineStack(mod as never, { artifactObjects: ['sxl_account', 'sxl_order'] } as never)], + { manifest: 'preserve' }, + ), + ), + ) as Record; +} + +interface Reading { + quoteRef: unknown; + accountRef: unknown; + quoteId: unknown; + accountId: unknown; + /** Whether the metadata service's copy of the order carries the extension's field. */ + metadataCopyHasQuoteRef: boolean; +} + +async function read(stack: VerifyStack): Promise { + const [order] = await stack.rows('sxl_order'); + const [quote] = await stack.rows('sxl_quote'); + const [account] = await stack.rows('sxl_account'); + const copy = (await stack.kernel.getService<{ getObject(name: string): Promise }>('metadata').getObject('sxl_order')) as + | { fields?: Record } + | undefined; + return { + quoteRef: order?.quote_ref, + accountRef: order?.account_ref, + quoteId: quote?.id, + accountId: account?.id, + metadataCopyHasQuoteRef: Boolean(copy?.fields && 'quote_ref' in copy.fields), + }; +} + +let dir: string; +let path: string; +const readings = new Map(); + +beforeAll(async () => { + dir = mkdtempSync(join(tmpdir(), 'verify-seed-extension-lookup-')); + path = join(dir, 'objectstack.json'); + writeFileSync(path, JSON.stringify(artifact())); + + // The `artifact` option: verify's own door. + const viaArtifact = await bootStack(artifact(), { artifact: path, hostRoot: dir }); + try { + readings.set('artifact', await read(viaArtifact)); + } finally { + await viaArtifact.stop(); + } + + // A caller's `MetadataPlugin` over the artifact: the dogfood suites' shape. + const viaCallerDoor = await bootStack(artifact(), { + hostRoot: dir, + extraPlugins: [ + new MetadataPlugin({ + rootDir: dir, watch: false, artifactWatch: false, registerSystemObjects: false, + artifactSource: { mode: 'local-file', path }, + }), + ], + }); + try { + readings.set('caller-door', await read(viaCallerDoor)); + } finally { + await viaCallerDoor.stop(); + } +}, BOOT_TIMEOUT * 2); + +afterAll(() => { + if (dir) rmSync(dir, { recursive: true, force: true }); +}); + +describe('[#36274] a bootStack artifact boot seeds a lookup another package adds through objectExtensions as os dev does', () => { + for (const leg of ['artifact', 'caller-door'] as const) { + it(`${leg}: the metadata service holds the order as its package wrote it, without the extension's field`, () => { + expect(readings.get(leg)?.metadataCopyHasQuoteRef).toBe(false); + }); + + it(`${leg}: the extension's lookup stores the quote's id, not its natural key`, () => { + const r = readings.get(leg)!; + expect(r.quoteId).toEqual(expect.any(String)); + expect(r.quoteRef).toBe(r.quoteId); + }); + + it(`${leg}: CONTROL — the owner's own lookup stores the account's id`, () => { + const r = readings.get(leg)!; + expect(r.accountId).toEqual(expect.any(String)); + expect(r.accountRef).toBe(r.accountId); + }); + } +}); diff --git a/packages/verify/src/harness.ts b/packages/verify/src/harness.ts index 41206c47612..f9118ac414c 100644 --- a/packages/verify/src/harness.ts +++ b/packages/verify/src/harness.ts @@ -537,6 +537,15 @@ export interface BootOptions { * is refused too (`RESOURCE_NOT_FOUND`, `status: 404`), never replaced by * the source. A stale artifact boots stale bodies: build before the run. * + * [#36274] The `metadata` slot is the door `objectstack dev` composes for + * an artifact: a `MetadataPlugin` that loads this same file, ahead of the + * app, which then leaves the four security collections to it + * (`securityMetadataRegistrar: 'artifact-door'`). So the metadata service + * holds each object as its own package wrote it, the copy a seed reads on + * `os dev`, and a field another package adds through `objectExtensions` + * is in the engine's folded schema only. A `MetadataPlugin` in + * {@link BootOptions.extraPlugins} takes the door's place. + * * ⛔ Nothing is lowered here. The build's lowering lives in * `@objectstack/cli`, which depends on this package, so the caller builds * the artifact and this boot reads it. @@ -639,6 +648,13 @@ export interface BootOptions { * mounted, and this instance is the one the boot keeps. * * So a suite that needs one of those configured its own way passes it here. + * + * [#36274] A `MetadataPlugin` (`@objectstack/metadata`) here is the one + * exception to the slot: it is registered in the `metadata` slot, ahead of + * the app, where `objectstack dev` registers its artifact door, so one that + * loads an artifact has loaded it before the app's inline seed reads the + * metadata service. The harness then composes no `MetadataPlugin` of its own. + * * {@link BootOptions.security} and {@link BootOptions.analytics} take * precedence over an app plugin of the same `name` the same way. */ @@ -793,7 +809,7 @@ async function loadCompiledArtifact( artifact: unknown, config: unknown, hostRoot: string, -): Promise> { +): Promise<{ bundle: Record; location: string }> { if (typeof artifact !== 'string' || artifact.trim() === '') { throw bootRefusal( `verify: BootOptions.artifact must name a compiled artifact (a path or an http(s):// URL), not ` + @@ -839,7 +855,15 @@ async function loadCompiledArtifact( 409, ); } - return bundle as Record; + return { bundle: bundle as Record, location }; +} + +/** Whether a plugin is `@objectstack/metadata`'s `MetadataPlugin`, by its registered name or its class name. */ +function isMetadataPlugin(plugin: unknown): boolean { + return ( + registeredName(plugin) === 'com.objectstack.metadata' || + (plugin as { constructor?: { name?: unknown } } | null | undefined)?.constructor?.name === 'MetadataPlugin' + ); } /** @@ -938,8 +962,10 @@ function claimConfiguration(config: unknown): ConfigurationClaim { * harness's service set, its `metadata` slot held by the artifact-less * `MetadataPlugin` (`@objectstack/metadata`) `serve` composes for a host * configuration, so a declared `apis` endpoint is matched as `serve` matches - * it (a `MetadataPlugin` in {@link BootOptions.extraPlugins} or in the app's - * `plugins` takes its place); the providers `serve` mounts for it — the ones the + * it (a `MetadataPlugin` in {@link BootOptions.extraPlugins}, registered in + * that slot ahead of the app, or one in the app's `plugins` takes its place; + * with {@link BootOptions.artifact}, the `MetadataPlugin` that loads the + * artifact, as `objectstack dev` composes it); the providers `serve` mounts for it — the ones the * app's `requires` names and the always-on slate `serve` mounts for every app * (`queue`, `job`, `cache`, `settings`, `email`, `storage`, `sms`, `sharing`, * `messaging`, `analytics`, `package-registry`), each built from the app's @@ -1052,8 +1078,9 @@ async function bootClaimed( // artifact built from it (see BootOptions.artifact). Read before the posture // is requested and the kernel is built, so a refused artifact leaves nothing // to undo. - const appBundle: unknown = - opts.artifact === undefined ? config : await loadCompiledArtifact(opts.artifact, config, hostRoot); + const compiled = + opts.artifact === undefined ? undefined : await loadCompiledArtifact(opts.artifact, config, hostRoot); + const appBundle: unknown = compiled === undefined ? config : compiled.bundle; // [ADR-0105 D1] `multiTenant: true` REQUESTS the hard organization wall — // posture `isolated`, what `OS_MULTI_ORG_ENABLED=true` historically meant. @@ -1145,28 +1172,57 @@ async function bootClaimed( // collections included. // - `rootDir` is the app's root, as `serve` hands it, so the repository the // plugin attaches sits under the app; attaching writes nothing. - // - A `MetadataPlugin` the caller hands in `extraPlugins`, or the app - // declares in its own `plugins`, takes its place by identity — `serve`'s - // "no MetadataPlugin in `plugins`" condition, read on the same two keys. + // - A `MetadataPlugin` the app declares in its own `plugins` takes its + // place — `serve`'s "no MetadataPlugin in `plugins`" condition, read on + // the same two keys. + // + // [#36274] Two boots fill the slot the way `objectstack dev` fills it for a + // compiled artifact (`createStandaloneStack`, and the CLI's dev artifact + // door): a `MetadataPlugin` that loads the artifact, registered HERE, ahead + // of the app. + // - `BootOptions.artifact`: the harness composes that door over the same + // file, and the app is told the door registers the four security + // collections (`securityMetadataRegistrar: 'artifact-door'`), as both + // dev boots declare it. One value decides both. + // - A `MetadataPlugin` the caller hands in `extraPlugins` is registered in + // this slot, not in the `extraPlugins` slot after the app. + // The order is what a seed reads. The door's `start()` loads each object as + // its own package wrote it, so a field another package adds through + // `objectExtensions` is absent from the metadata service's copy, while + // ObjectQL's one-shot bridge (this kernel has no `environmentId`) had copied + // the registry's folded object into the empty service a moment earlier. + // After the app, the door loaded after the inline seed had run, so the seed + // read the folded copy, and a verify boot resolved a seed that `os dev` + // stored as a raw natural key. Before the app, the seed reads the copy + // `os dev` reads. // No catch: a boot that cannot compose its `metadata` occupant fails, rather // than falling back to the matcher-less one quietly. const declaredPlugins = (config as { plugins?: unknown } | null | undefined)?.plugins; - const holdsMetadataPlugin = [ - ...(opts.extraPlugins ?? []), - ...(Array.isArray(declaredPlugins) ? declaredPlugins : []), - ].some( - (plugin) => - registeredName(plugin) === 'com.objectstack.metadata' || - (plugin as { constructor?: { name?: unknown } } | null | undefined)?.constructor?.name === 'MetadataPlugin', - ); - if (!holdsMetadataPlugin) { - await kernel.use(new MetadataPlugin({ watch: false, config: { bootstrap: 'lazy' }, rootDir: hostRoot })); + const callerMetadataPlugins = (opts.extraPlugins ?? []).filter(isMetadataPlugin); + const declaresMetadataPlugin = Array.isArray(declaredPlugins) && declaredPlugins.some(isMetadataPlugin); + let composesArtifactDoor = false; + if (callerMetadataPlugins.length > 0) { + for (const plugin of callerMetadataPlugins) await kernel.use(plugin as any); + } else if (!declaresMetadataPlugin) { + composesArtifactDoor = compiled !== undefined; + await kernel.use( + compiled === undefined + ? new MetadataPlugin({ watch: false, config: { bootstrap: 'lazy' }, rootDir: hostRoot }) + : new MetadataPlugin({ + watch: false, + artifactWatch: false, + artifactSource: { mode: 'local-file', path: compiled.location }, + rootDir: hostRoot, + }), + ); } // The app under test (objects, datasets, cubes, flows, seed data): the // configuration, or [#22301] the compiled artifact `BootOptions.artifact` // names, whose hook and action `body`s AppPlugin binds to the sandbox. - await kernel.use(new AppPlugin(appBundle)); + await kernel.use( + new AppPlugin(appBundle, undefined, composesArtifactDoor ? { securityMetadataRegistrar: 'artifact-door' } : {}), + ); // Platform infrastructure `os serve` auto-injects into every served kernel: // the `sys_migration` flag ledger (#4243) and the `sys_secret` cipher store @@ -1355,7 +1411,10 @@ async function bootClaimed( // Caller-supplied optional service pairs (see BootOptions.extraPlugins). // Before SecurityPlugin, mirroring the CLI's ordering for service pairs. + // A `MetadataPlugin` among them is already registered, in the `metadata` + // slot above (#36274). for (const plugin of opts.extraPlugins ?? []) { + if (callerMetadataPlugins.includes(plugin)) continue; handHarnessCryptoToAuditPlugin(plugin, cryptoProvider); await kernel.use(plugin as any); } From da23bd2705df2ca0858eabab4bc9edc62ee055af Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 29 Nov 2026 16:54:54 +0000 Subject: [PATCH 2/3] docs(verify): name the artifact metadata door and its slot; changeset Co-authored-by: Claude Claude-Session: https://claude.ai/code/session_01ADHkmRar3Kkg6SpwVgaaVE --- .../36274-verify-artifact-metadata-door.md | 17 +++++++++++++++++ packages/verify/README.md | 11 +++++++++++ 2 files changed, 28 insertions(+) create mode 100644 .changeset/36274-verify-artifact-metadata-door.md diff --git a/.changeset/36274-verify-artifact-metadata-door.md b/.changeset/36274-verify-artifact-metadata-door.md new file mode 100644 index 00000000000..d10887a1f0e --- /dev/null +++ b/.changeset/36274-verify-artifact-metadata-door.md @@ -0,0 +1,17 @@ +--- +'@objectstack/verify': patch +--- + +fix(verify): a `bootStack` boot of a compiled artifact fills its `metadata` slot the way `os dev` does, so it seeds the way `os dev` seeds + +Clause-②: no +Metadata-protocol: none + +`bootStack` could pass a seed that `os dev` stored wrongly. The case measured: a module package adds a lookup to the app package's object through `objectExtensions`, and seeds it by the target's natural key. On `os dev` the metadata service holds each object as its own package wrote it, because the `MetadataPlugin` that loads the artifact starts before the app seeds. Under `bootStack` that plugin started after the app's seed, or was not composed at all, so the seed read the registry's copy with the extension folded in. With the seed loader as it was before it learned to read the engine's schema first, `os dev` stored the raw key and `bootStack` stored the target's id, so a `bootStack` test could not catch the defect. + +Now: + +- `bootStack(config, { artifact })` composes a `MetadataPlugin` that loads the same artifact, ahead of the app, and the app leaves the four security collections (`positions`, `permissions`, `capabilities`, `sharingRules`) to it, as `os dev` declares it. +- A `MetadataPlugin` passed in `extraPlugins` is registered in the `metadata` slot, ahead of the app, instead of after it. The harness composes no `MetadataPlugin` of its own beside it, as before. + +A source boot (no `artifact`, no `MetadataPlugin` in `extraPlugins`) is unchanged. diff --git a/packages/verify/README.md b/packages/verify/README.md index b55153be439..1ffcfd68fda 100644 --- a/packages/verify/README.md +++ b/packages/verify/README.md @@ -450,6 +450,17 @@ await expect(shipped.hooks.run('crm_lead', 'insert', { last_name: 'Ada' }, { sys in-process here as under `objectstack start`. `objectstack build --strict-body` refuses it at build time instead, and its artifact is body-only. +- The `metadata` slot is the door `objectstack dev` composes for an artifact: + a `MetadataPlugin` that loads the same file, registered ahead of the app, + which leaves the four security collections to it. So a seed reads the + metadata copy `os dev` reads: each object as its own package wrote it, with + a field another package adds through `objectExtensions` in the engine's + folded schema only. A `MetadataPlugin` you pass in `extraPlugins` takes the + door's place, and is registered in the same slot, ahead of the app, so one + that loads an artifact has loaded it before the app's inline seed runs. + Before this, the door loaded after the seed (or was not composed), the seed + read the registry's folded copy, and a verify boot could resolve a seed + reference that `os dev` stored as the raw natural key. - Refused before anything boots: an artifact that cannot be loaded (`RESOURCE_NOT_FOUND` / 404; the boot never falls back to the source), one built from another app (`RESOURCE_CONFLICT` / 409), and a configuration From ce65d57f4d9e8f8bfb86838a5320184b81fa98ed Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 29 Nov 2026 17:41:21 +0000 Subject: [PATCH 3/3] test(verify): pay the seed-extension pin's service loads at collection Co-authored-by: Claude Claude-Session: https://claude.ai/code/session_01ADHkmRar3Kkg6SpwVgaaVE --- packages/verify/src/harness.seed-extension-lookup.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/verify/src/harness.seed-extension-lookup.test.ts b/packages/verify/src/harness.seed-extension-lookup.test.ts index ce9d8d6a08b..0a77b48de4d 100644 --- a/packages/verify/src/harness.seed-extension-lookup.test.ts +++ b/packages/verify/src/harness.seed-extension-lookup.test.ts @@ -29,6 +29,9 @@ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +// Paid at collection, not inside a case's clock (check:test-source-alias). +import '@objectstack/service-datasource'; +import '@objectstack/service-automation'; import { MetadataPlugin } from '@objectstack/metadata'; import { composeStacks, defineStack } from '@objectstack/spec'; import { bootStack, type VerifyStack } from './harness.js';