Skip to content

Commit 5026b15

Browse files
committed
feat(objectql): report a bound action whose name its target already lists with a different declaration, at kernel:ready and for each late artifact
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PmE7dZP8v5iv3z1dgybAtT
1 parent 3ae0b77 commit 5026b15

2 files changed

Lines changed: 140 additions & 3 deletions

File tree

‎packages/objectql/src/plugin.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import {
4646
import { SecurityCatalogNameConflictError, findEnvironmentHeldSecurityCatalogNames } from './registry.js';
4747
// [ADR-0029 D3] The registry's own refusal of a second owner — read, never
4848
// re-derived, when a residual object's name is already owned.
49-
import { ObjectOwnershipConflictError, collectManifestBoundActions, collectManifestObjectNames, describeUnboundActionTargets } from './registry.js';
49+
import { ObjectOwnershipConflictError, collectManifestBoundActions, collectManifestObjectNames, describeBoundActionCollision, describeUnboundActionTargets, type BoundActionCollision } from './registry.js';
5050

5151
export type { Plugin, PluginContext };
5252

@@ -782,6 +782,15 @@ export class ObjectQLPlugin implements Plugin {
782782
// bodies (a composed stack keeps one member's manifest) that body's
783783
// record must not be replaced.
784784
if (residual) this.registerUnclaimedTopLevelObjects(ctx, residual, ordered);
785+
// [#24472] The boot reported every same-name pair it found
786+
// (`kernel:ready`, below); an artifact arriving later is judged once it
787+
// has registered, for the pairs naming one of its packages on either
788+
// side. Reported, never refused — nothing above is undone.
789+
if (this.boundActionTargetsSealed) {
790+
const arrived = new Set(scope.packageIds);
791+
if (residual?.ownerId !== undefined) arrived.add(residual.ownerId);
792+
this.reportBoundActionCollisions(ctx, ql.registry.findBoundActionCollisions(arrived));
793+
}
785794
// Manifests registered AFTER start() (marketplace install / ledger
786795
// rehydrate arrive on `kernel:ready` or an HTTP request) land in the
787796
// SchemaRegistry only — the one-shot startup bridge already ran — so
@@ -932,6 +941,12 @@ export class ObjectQLPlugin implements Plugin {
932941
const unboundActions = describeUnboundActionTargets(this.ql?.registry.findUnboundActionTargets() ?? []);
933942
if (unboundActions) throw unboundActions;
934943
this.boundActionTargetsSealed = true;
944+
// [#24472] A bound action named like one its target already lists, with
945+
// a different declaration, is warned once per pair, naming the object,
946+
// the action and both packages — judged here for the same reason, and
947+
// reported, never refused: the object keeps serving its own (rung 1)
948+
// and the boot goes on. Each late artifact is judged as it arrives.
949+
this.reportBoundActionCollisions(ctx, this.ql?.registry.findBoundActionCollisions() ?? []);
935950
// #7737 — FIRST, before anything that might read data: bind every
936951
// declared federated object to its remote table now that every
937952
// plugin's `start()` (including the declared-datasource auto-connect
@@ -2870,6 +2885,18 @@ export class ObjectQLPlugin implements Plugin {
28702885
}
28712886
}
28722887

2888+
/**
2889+
* [#24472] One author-visible warning per same-name pair
2890+
* ({@link SchemaRegistry.findBoundActionCollisions}): the object, the action
2891+
* and both packages. `warn`, by the degradation-level question — the object
2892+
* visibly serves one declaration and nothing claimed persisted is lost.
2893+
*/
2894+
private reportBoundActionCollisions(ctx: PluginContext, collisions: readonly BoundActionCollision[]): void {
2895+
for (const collision of collisions) {
2896+
ctx.logger.warn(`[ObjectQLPlugin] ${describeBoundActionCollision(collision)}`, { ...collision });
2897+
}
2898+
}
2899+
28732900
/**
28742901
* [#24182] The withdrawal counterpart of {@link bridgeManifestObjectsToMetadataService}
28752902
* for a package that EXTENDS objects other packages own: put the registry's

‎packages/objectql/src/registry.ts‎

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,74 @@ export function describeUnboundActionTargets(
153153
return err;
154154
}
155155

156+
/**
157+
* [#24472] A bound top-level action that its target object does NOT list,
158+
* because the object already lists a DIFFERENT declaration under the same
159+
* name — see {@link SchemaRegistry.findBoundActionCollisions}.
160+
*/
161+
export interface BoundActionCollision {
162+
/** The object both declarations are for. */
163+
objectName: string;
164+
/** The action name they share. */
165+
action: string;
166+
/** The package whose declaration the object serves — its owner, for the object's own action. */
167+
servedPackageId: string | undefined;
168+
/** The package whose bound declaration the object does not list. */
169+
shadowedPackageId: string | undefined;
170+
}
171+
172+
const describePackage = (packageId: string | undefined): string =>
173+
packageId ? `package '${packageId}'` : 'a registration with no package';
174+
175+
/**
176+
* [#24472] The author-visible warning for one {@link BoundActionCollision}.
177+
* Reported, never refused: a refusal would be a narrowing, and the decision
178+
* for this collision is to report it first.
179+
*/
180+
export function describeBoundActionCollision(collision: BoundActionCollision): string {
181+
const { objectName, action, servedPackageId, shadowedPackageId } = collision;
182+
return `Action '${action}' on object '${objectName}' is declared twice, and the two declarations differ: `
183+
+ `${describePackage(servedPackageId)} declares the one the object serves, and `
184+
+ `${describePackage(shadowedPackageId)} binds another by \`objectName\`, which the object does not list. `
185+
+ `Both register their handler under the one key '${objectName}:${action}', so when both carry one, the `
186+
+ 'handler that runs is whichever package registered last — not necessarily the declaration the object '
187+
+ 'lists. Rename one of the two actions, or drop the duplicate declaration.';
188+
}
189+
190+
/**
191+
* Keys that say where an action declaration came from or where it sits, not
192+
* what it declares: the registry's stamps (`_packageId`, `_provenance`, the
193+
* `_lock` envelope…), the `protection` block those stamps translate, and
194+
* `objectName`, which an object's own action leaves to its position.
195+
*/
196+
const isActionProvenanceKey = (key: string): boolean =>
197+
key.startsWith('_') || key === 'protection' || key === 'objectName';
198+
199+
/**
200+
* [#24472] Are two action declarations the SAME declaration — one carried
201+
* twice (a same-stack merge's copy beside the top-level item, one declaration
202+
* shipped by two packages, a copy re-bridged under another package's stamp)?
203+
* Structural equality minus {@link isActionProvenanceKey} keys at the top
204+
* level; a key set to `undefined` reads as absent, callables compare by
205+
* reference, and past depth 32 the answer is "different" — the reported side.
206+
*/
207+
function sameActionDeclaration(a: unknown, b: unknown, depth = 0): boolean {
208+
if (Object.is(a, b)) return true;
209+
if (depth >= 32 || typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false;
210+
if (Array.isArray(a) !== Array.isArray(b)) return false;
211+
if (Array.isArray(a)) {
212+
const other = b as unknown[];
213+
return a.length === other.length && a.every((item, i) => sameActionDeclaration(item, other[i], depth + 1));
214+
}
215+
const left = a as Record<string, unknown>;
216+
const right = b as Record<string, unknown>;
217+
const keysOf = (o: Record<string, unknown>) =>
218+
Object.keys(o).filter((k) => o[k] !== undefined && (depth > 0 || !isActionProvenanceKey(k)));
219+
const leftKeys = keysOf(left);
220+
return leftKeys.length === keysOf(right).length
221+
&& leftKeys.every((k) => right[k] !== undefined && sameActionDeclaration(left[k], right[k], depth + 1));
222+
}
223+
156224
/** A manifest and its nested `plugins[]`, which register under its package (`registerPlugin`). */
157225
function manifestAndNestedPlugins(manifest: any): any[] {
158226
const plugins = Array.isArray(manifest?.plugins)
@@ -3503,8 +3571,10 @@ export class SchemaRegistry {
35033571
* MetadataService copy bridged from {@link getAllObjects}, or a stored row
35043572
* written back from a served read. An action the object already declares
35053573
* keeps the object's declaration, the precedence the router applies (rung 1
3506-
* before rung 2). A target no package registers is refused at boot, not
3507-
* here — see {@link findUnboundActionTargets}.
3574+
* before rung 2); when the two declarations differ, the ObjectQL plugin
3575+
* reports the pair at boot ({@link findBoundActionCollisions}). A target no
3576+
* package registers is refused at boot, not here — see
3577+
* {@link findUnboundActionTargets}.
35083578
*
35093579
* The object's own `actions` keep their order exactly — re-sorting them by
35103580
* `order` here would reorder an object no package bound anything to the
@@ -3572,6 +3642,46 @@ export class SchemaRegistry {
35723642
return out;
35733643
}
35743644

3645+
/**
3646+
* [#24472] The bound top-level actions their target object does not list
3647+
* because it already lists a DIFFERENT declaration under the same name —
3648+
* the object's own (rung 1 before rung 2, so {@link attachBoundActions}
3649+
* skips the name) or another package's bound action attached first.
3650+
*
3651+
* A pair that is the same declaration ({@link sameActionDeclaration}) is not
3652+
* a collision: a same-stack merge leaves a copy on the object beside the
3653+
* top-level item, and one declaration can be shipped by two packages or
3654+
* re-bridged under another package's stamp. Nothing here changes what is
3655+
* served or refuses anything — the ObjectQL plugin reports each pair
3656+
* ({@link describeBoundActionCollision}).
3657+
*
3658+
* Only meaningful once every package has registered (`kernel:ready`, for
3659+
* {@link findUnboundActionTargets}' reason). `packageIds` narrows the answer
3660+
* to the pairs naming one of those packages on either side — a late
3661+
* artifact's, so a pair the boot already reported is not reported again.
3662+
*
3663+
* @internal
3664+
*/
3665+
findBoundActionCollisions(packageIds?: ReadonlySet<string>): BoundActionCollision[] {
3666+
const out: BoundActionCollision[] = [];
3667+
for (const action of this.boundCodeActions()) {
3668+
const objectName = standaloneActionOwnerKey(action);
3669+
const fqn = this.resolveObjectKey(objectName);
3670+
if (fqn === undefined) continue; // no target at all: findUnboundActionTargets' case
3671+
const served = (this.resolveObject(fqn) as { actions?: unknown } | undefined)?.actions;
3672+
const listed: any = Array.isArray(served) ? served.find((a: any) => a?.name === action.name) : undefined;
3673+
if (listed === undefined || listed === action || sameActionDeclaration(listed, action)) continue;
3674+
const servedPackageId: string | undefined = isCodeArtifactBody(listed)
3675+
? listed._packageId
3676+
: this.getObjectOwner(fqn)?.packageId;
3677+
const shadowedPackageId: string | undefined = action._packageId;
3678+
const names = (id: string | undefined) => id !== undefined && packageIds!.has(id);
3679+
if (packageIds && !names(servedPackageId) && !names(shadowedPackageId)) continue;
3680+
out.push({ objectName, action: action.name, servedPackageId, shadowedPackageId });
3681+
}
3682+
return out;
3683+
}
3684+
35753685
/**
35763686
* [ADR-0029 D9.2a] Which of the three fold scalars the BASE layer carries a
35773687
* TENANT-AUTHORED value for — i.e. one that no longer equals the packaged

0 commit comments

Comments
 (0)