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
24 changes: 24 additions & 0 deletions .changeset/35711-residual-extension-and-jobs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@objectstack/spec': minor
'@objectstack/metadata': minor
'@objectstack/objectql': minor
---

fix(objectql)!: a composition's top-level `objectExtensions` entry that no package body carries is registered and served; composition refuses two such jobs of one name, and residual extensions that would share one package's slot

Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: none — no skill teaches placing `objectExtensions` or `jobs` in a manifest-less input of a `'preserve'` composition; the refusal names both entries and prescribes the fix

<!-- adr-0087: registered stack-residual-extension-registered-and-jobs-judged -->

**BREAKING** accept-set narrowing, shipped as `minor`, beside a runtime fix.

- **Fixed (`@objectstack/objectql`, `@objectstack/metadata`).** A composed artifact that carries `packages[]` keeps the entries no package body claims at its top level, and the boot registers that residual under the artifact's `manifest.id` (ADR-0130 D4). Under `composeStacks(stacks, { manifest: 'preserve' })` that is an input with no `manifest`; under `'first'`, `'last'` or an index it is also the own top-level entries of an input that is itself a multi-package artifact. Its `objectExtensions` entries were registered by no door: the target object was served without their fields (`INVALID_FIELD` on a write naming one) and nothing was logged, through `os serve` and through `os build`'s artifact alike. The residual rule (`unclaimedTopLevel`) now lists such an entry (type `object_extension`) when no package body carries an entry equal to it, and the engine registers it as an `extend` contribution under `manifest.id`, as a body's entry registers. An entry whose field names a picklist no package body declares is not registered and is named at boot, like a residual object.
- **Refused now, at composition (`STACK_CROSS_REFERENCE_INVALID`, 422), under every manifest strategy once the result carries `packages[]`.** Two residual jobs of one name: the boot's job binder schedules neither (`NOT SCHEDULED` at `error`). A residual `objectExtensions` entry for an object that the body carrying `manifest.id` also extends with other fields: one extension of an object registers per package, so only the last one's fields would be served; the finding names that body's entry first, then the residual's: `Object extension of 'crm_line' is declared twice in \`objectExtensions\`: 'com.example.billing' (stack #2) objectExtensions[0] and stack #1 objectExtensions[0]. …`. Two residual `objectExtensions` entries for one object, also when another package body extends that object: a residual entry is now told from a body's flattened copy by value, not by target, so the two are one package's pair (they used to count as that body's copies and compose).
- **Not refused (known gap).** A residual job that shares its name with a package body's job, with other content, still composes: it is read as that body's copy, and the boot holds two enabled jobs of the name and schedules neither.
- **Wording.** The `jobs` finding at every door now says what the runtime does with the pair: `The runtime keys jobs by name: the metadata keeps only the last of these entries, and the job scheduler refuses a name two enabled jobs share, so when both are enabled neither runs.`
- **Unchanged.** One residual extension for an object another package extends composes, and both register. A residual entry equal to a body's entry is that body's copy. Package-claimed extensions and a single residual job compose and boot as before.
- **Installed artifacts.** A residual extension sharing its package's slot registers, the last one served, with one `error` line per object naming the entries. A residual extension now passes the registry's `extend` door as a package body's entry does, so one that door refuses (reachable only through `strict: false` or a hand-built artifact) now fails a boot that used to load and drop it.

One-line fix: declare each job name once; move a top-level extension into the package that owns it, or merge it into that package's own entry for the object.
58 changes: 57 additions & 1 deletion packages/metadata/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,52 @@ function claimedArtifactSlots(bodies: readonly unknown[]): (type: string, name:
return (type, name) => owned.get(type)?.has(name) === true;
}

/**
* Whether a top-level `objectExtensions` entry of a stack that carries
* `packages[]` is a package body's: a body carries an entry EQUAL to it by
* value. An extension has no slot of its own (it is keyed by `extend`, and its
* `name` spelling is a retired alias the schema refuses), so it is told from
* the residual by what it holds, never by its target: the flattened top level
* of a `composeStacks(…, { manifest: 'preserve' })` artifact repeats every
* body's entry, and a built artifact has lost the references, but each copy
* still holds exactly what its body's entry holds. An entry extending a target
* some body also extends, with other fields, is the residual's own.
*
* Equal by value is also the case where nothing is lost: the body's entry
* registers exactly what the copy holds. Keys set to `undefined` count as
* absent, as `deepEqualAuthored` (`@objectstack/spec`) and the composition
* door's mirror read them.
*/
function claimedArtifactExtensions(bodies: readonly unknown[]): (item: unknown) => boolean {
const declared: unknown[] = [];
for (const body of bodies) {
const items = (body as any)?.objectExtensions;
if (Array.isArray(items)) declared.push(...items);
}
return (item) => declared.some((entry) => entry === item || sameAuthoredValue(entry, item, 0));
}

/** Structural equality of two authored values; keys holding `undefined` count as absent. */
function sameAuthoredValue(a: unknown, b: unknown, depth: number): boolean {
if (Object.is(a, b)) return true;
if (depth >= 32 || typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false;
if (Array.isArray(a) !== Array.isArray(b)) return false;
if (Array.isArray(a) && Array.isArray(b)) {
return a.length === b.length && a.every((v, i) => sameAuthoredValue(v, b[i], depth + 1));
}
const left = a as Record<string, unknown>;
const right = b as Record<string, unknown>;
const keys = Object.keys(left).filter((k) => left[k] !== undefined);
if (keys.length !== Object.keys(right).filter((k) => right[k] !== undefined).length) return false;
return keys.every((k) => sameAuthoredValue(left[k], right[k], depth + 1));
}

/** The object an `objectExtensions` entry extends, or `undefined` when it names none. */
function extensionTarget(item: unknown): string | undefined {
const target = (item as { extend?: unknown } | null | undefined)?.extend;
return typeof target === 'string' && target !== '' ? target : undefined;
}

/** One top-level item of a multi-package stack that none of its package bodies declares. */
export interface UnclaimedTopLevelItem {
/** The stack collection the item sits in, e.g. `views`. */
Expand Down Expand Up @@ -419,7 +465,12 @@ export interface UnclaimedTopLevel {
* An item is unclaimed when it occupies a registry slot (`artifactItemSlot`)
* that no package body occupies (`claimedArtifactSlots`): a container is
* compared on its own slot, every other item on its name. An item with no
* derivable slot is registered by no door, so it is not listed.
* derivable slot is registered by no door, so it is not listed — with one
* exception: an `objectExtensions` entry, which has no slot, is listed (type
* `object_extension`) when it names a target and no body carries an entry
* equal to it (`claimedArtifactExtensions`). The metadata door registers no
* extension, a body's or the residual's (it has no slot to file one under);
* the engine registers the residual's under this id, as it does a body's.
*
* @returns `undefined` for a stack without `packages[]`: its top level IS its
* one package's body, so it has no residual.
Expand All @@ -428,11 +479,16 @@ export function unclaimedTopLevel(stack: unknown): UnclaimedTopLevel | undefined
if (!Array.isArray((stack as any)?.packages)) return undefined;
const bodies = resolveArtifactPackageOrder(stack);
const claimed = claimedArtifactSlots(bodies);
const claimedExtension = claimedArtifactExtensions(bodies);
const items: UnclaimedTopLevelItem[] = [];
for (const [field, metaType] of Object.entries(ARTIFACT_FIELD_TO_TYPE)) {
const collection = (stack as any)[field];
if (!Array.isArray(collection)) continue;
collection.forEach((item: unknown, index: number) => {
if (metaType === 'object_extension') {
if (extensionTarget(item) !== undefined && !claimedExtension(item)) items.push({ field, index, type: metaType, item });
return;
}
const slot = artifactItemSlot(metaType, item);
if (!slot) return;
if (claimed(metaType, slot.container ? slot.object : slot.name)) return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* [ADR-0130 D4] The residual rule (`unclaimedTopLevel`) lists a multi-package
* stack's residual `objectExtensions` entries, so the engine registers them
* under the stack's `manifest.id` as it registers the residual's objects.
*
* An extension has no slot (it is keyed by `extend`; its `name` is a retired
* alias), so the rule used to list none, and no door registered one: measured
* through `os serve` on main `c30ce97cd7`, a `composeStacks(…, { manifest:
* 'preserve' })` artifact with an input that has no manifest served the target
* without the extension's field (`INVALID_FIELD` on write) and logged nothing.
*
* It is told from a body's flattened copy by VALUE, never by target: the
* flattened top level repeats every body's entry, a built artifact has lost the
* references, and an entry extending a target some body also extends with other
* fields is the residual's own.
*/

import { describe, it, expect } from 'vitest';
// Paid at collection, not inside a case's clocked window: `plugin.ts` loads it lazily.
import '@objectstack/metadata-fs';
import { composeStacks, defineStack } from '@objectstack/spec';
import { unclaimedTopLevel } from './plugin.js';

const owner = { id: 'com.probe.owner', namespace: 'probe', name: 'probe', version: '1.0.0', type: 'module' as const };
const other = { id: 'com.probe.other', namespace: 'probe', name: 'probe_other', version: '1.0.0', type: 'module' as const };
const account = { name: 'probe_account', label: 'Account', sharingModel: 'private' as const, fields: { title: { type: 'text' as const, label: 'Title' } } };
const extension = (field: string) => ({ extend: 'probe_account', fields: { [field]: { type: 'text' as const, label: field } } });

const residualExtensions = (stack: unknown) =>
(unclaimedTopLevel(stack)?.items ?? [])
.filter((entry) => entry.type === 'object_extension')
.map((entry) => ({ field: entry.field, index: entry.index, target: (entry.item as { extend: string }).extend, fields: Object.keys((entry.item as { fields: object }).fields) }));

describe('unclaimedTopLevel lists a residual objectExtensions entry', () => {
// `'preserve'` keeps the last manifest (owner's) as the residual's id, and
// owner does not extend the target: the residual's entry has a slot of its own.
const composed = () => composeStacks(
[
defineStack({ manifest: other, objectExtensions: [extension('other_note')] }),
defineStack({ manifest: owner, objects: [account] }),
defineStack({ objectExtensions: [extension('residual_note')] }),
],
{ manifest: 'preserve' },
);

it('the manifest-less input\'s entry, as composed and as built JSON; never the body\'s flattened copy', () => {
for (const [leg, stack] of [['composed', composed()], ['built', JSON.parse(JSON.stringify(composed()))]] as const) {
expect(residualExtensions(stack), leg).toEqual([
{ field: 'objectExtensions', index: 1, target: 'probe_account', fields: ['residual_note'] },
]);
expect(unclaimedTopLevel(stack)?.ownerId, leg).toBe(owner.id);
}
});

it('CONTROL: a stack whose every extension a body carries lists none', () => {
const stack = composeStacks(
[
defineStack({ manifest: owner, objects: [account] }),
defineStack({ manifest: other, objectExtensions: [extension('other_note')] }),
defineStack({ pages: [{ name: 'probe_home', label: 'Home', type: 'app', regions: [] }] }),
],
{ manifest: 'preserve' },
);
expect((stack as unknown as { objectExtensions: unknown[] }).objectExtensions, 'anti-vacuity: the flattened copy is there').toHaveLength(1);
expect(residualExtensions(stack)).toEqual([]);
expect(residualExtensions(JSON.parse(JSON.stringify(stack)))).toEqual([]);
});

it('CONTROL: a stack without packages[] has no residual', () => {
expect(unclaimedTopLevel(defineStack({ manifest: owner, objects: [account], objectExtensions: [extension('x_note')] }))).toBeUndefined();
});
});
73 changes: 49 additions & 24 deletions packages/objectql/src/engine-registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,50 @@ function logSameTargetObjectExtensions(
}
}

/**
* The partial object definition an `objectExtensions` entry registers as an
* `extend` contribution to its target, and the priority it registers at — the
* ONE reading of an entry, taken by a package body's entries
* ({@link RegistrationFacet.registerApp} step 2b) and by a multi-package
* stack's RESIDUAL entries (`ObjectQLPlugin`, ADR-0130 D4) alike.
*
* ⛔ Never a second literal at either caller: a key added here reaches both, so
* a residual extension registers exactly what the same entry registers from a
* package body (pinned by `manifest-service-residual-object-extension.test.ts`,
* which compares the object both registrations serve).
*/
export function objectExtensionContribution(ext: any): {
definition: {
name: string; fields: unknown; fieldPatches: unknown; label: unknown; pluralLabel: unknown;
description: unknown; validations: unknown; indexes: unknown; place: unknown;
};
priority: number;
} {
return {
definition: {
name: ext.extend, // Use the target FQN as name
fields: ext.fields,
// [ADR-0146 D10, second rule] The per-field display patches,
// the slot as `ObjectExtensionSchema.fieldPatches` declares
// it — applied by the registry's `mergeObjectDefinitions`,
// whose fold every object read shares.
fieldPatches: ext.fieldPatches,
label: ext.label,
pluralLabel: ext.pluralLabel,
description: ext.description,
validations: ext.validations,
indexes: ext.indexes,
// [#35901] Where its own fields, groups and child lists go —
// applied by the registry fold after every contributor's
// fields are merged (`applyExtensionPlacements`).
place: ext.place,
},
priority: ext.priority ?? 200,
};
}

/** Structural equality of two JSON-shaped values: same primitives, same arrays in order, same keys with equal values. */
function equalByValue(a: unknown, b: unknown): boolean {
export function equalByValue(a: unknown, b: unknown): boolean {
if (a === b) return true;
if (a === null || b === null || typeof a !== 'object' || typeof b !== 'object') return false;
if (Array.isArray(a) !== Array.isArray(b)) return false;
Expand Down Expand Up @@ -395,30 +437,13 @@ export class RegistrationFacet {
// outcome is unchanged.
logSameTargetObjectExtensions(this.logger, id, manifest.objectExtensions);
for (const ext of manifest.objectExtensions) {
const targetFqn = ext.extend;
const priority = ext.priority ?? 200;
// Create a partial object definition for the extension
const extDef = {
name: targetFqn, // Use the target FQN as name
fields: ext.fields,
// [ADR-0146 D10, second rule] The per-field display patches,
// the slot as `ObjectExtensionSchema.fieldPatches` declares
// it — applied by the registry's `mergeObjectDefinitions`,
// whose fold every object read shares.
fieldPatches: ext.fieldPatches,
label: ext.label,
pluralLabel: ext.pluralLabel,
description: ext.description,
validations: ext.validations,
indexes: ext.indexes,
// [#35901] Where its own fields, groups and child lists go —
// applied by the registry fold after every contributor's
// fields are merged (`applyExtensionPlacements`).
place: ext.place,
};
// The partial object definition for the extension — the one
// reading of an entry, shared with the residual's
// ({@link objectExtensionContribution}).
const { definition: extDef, priority } = objectExtensionContribution(ext);
// Register as extension (namespace is undefined since we're targeting by FQN)
this._registry.registerObject(stampPackage(extDef, id, version), id, undefined, 'extend', priority);
this.logger.debug('Registered Object Extension', { target: targetFqn, priority, from: id });
this._registry.registerObject(stampPackage(extDef as any, id, version), id, undefined, 'extend', priority);
this.logger.debug('Registered Object Extension', { target: extDef.name, priority, from: id });
}
}

Expand Down
Loading
Loading