@@ -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`). */
157225function 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