3333 * applied, a malformed item skipped with one warning per name per process,
3434 * never a crashed refresh.
3535 *
36+ * ## One declaration per name, by `(package, name)` (ADR-0150 D3/D4)
37+ *
38+ * A webhook is addressed by `(package, name)`. Which declaration a name
39+ * serves is answered by the ONE package-scoped resolver
40+ * (`resolvePackageScopedName`, `@objectstack/objectql`), never by list order:
41+ * every reader here (the dispatcher, the redelivery veto, the activation door,
42+ * the name guard) is context-free, so it asks as a caller with no package —
43+ * the environment's own item, else the unique package holder, else a refusal
44+ * naming every candidate. A refused name is not dispatched (reported once per
45+ * name per process), and {@link DeclaredWebhookReader.declaredWebhookNamed}
46+ * throws {@link DeclaredWebhookAmbiguousError} for it, so no door reads "not
47+ * declared" for a name two packages declare. While phase 1's one-holder
48+ * refusal stands (`webhook` is one of its types) no two packages hold a name,
49+ * so this answers every name as before. Items the registry does not hold (the
50+ * metadata service's, on a host whose registry holds no webhook) have no
51+ * package coordinate to resolve: one item per name serves, and a name listed
52+ * twice is refused the same way, never first-picked.
53+ *
3654 * ## The switch: regime C (ADR-0131 D6)
3755 *
3856 * A declared webhook is read-only. It is switched on or off for the deployment
7896
7997import { WebhookSchema , type WebhookParsed } from '@objectstack/spec/automation' ;
8098import { METADATA_ACTIVATION_TABLE } from '@objectstack/core' ;
99+ import {
100+ packageScopedRefusalMessage ,
101+ resolvePackageScopedName ,
102+ type PackageScopedHolder ,
103+ type PackageScopedRefusal ,
104+ type PackageScopedRegistry ,
105+ } from '@objectstack/objectql' ;
81106
82107/** System read context — the dispatcher reads its own configuration. */
83108const SYSTEM_CTX = { isSystem : true } as const ;
@@ -132,10 +157,94 @@ export interface DeclaredWebhook {
132157export interface DeclaredWebhookReader {
133158 /** Every valid declaration, each with its switch resolved. */
134159 list ( ) : Promise < DeclaredWebhook [ ] > ;
135- /** The declaration named `name`, switched on or off, or `null` when nothing declares it. */
160+ /**
161+ * The declaration named `name`, switched on or off, or `null` when nothing
162+ * declares it. Throws {@link DeclaredWebhookAmbiguousError} when the name
163+ * resolves to no single declaration (ADR-0150 D3).
164+ */
136165 declaredWebhookNamed ( name : string ) : Promise < DeclaredWebhook | null > ;
137166}
138167
168+ /**
169+ * [ADR-0150 D3, epic #31527 red line 2] A declared webhook name that resolves
170+ * to no single declaration: two packages declare it, and nothing picks one.
171+ * `code` + `status` are the conflict envelope (409 `RESOURCE_CONFLICT`, the
172+ * catalogue's code for a 409, the status ADR-0150 D5 gives an ambiguous bare
173+ * name); `candidates` names every holder, and so does the message.
174+ */
175+ export class DeclaredWebhookAmbiguousError extends Error {
176+ readonly code = 'RESOURCE_CONFLICT' ;
177+ readonly status = 409 ;
178+ /** The declared webhook name. */
179+ readonly webhook : string ;
180+ /** Every holder of the name, in the resolver's order. */
181+ readonly candidates : readonly PackageScopedHolder [ ] ;
182+ constructor ( refusal : PackageScopedRefusal ) {
183+ super ( refusal . message ) ;
184+ this . name = 'DeclaredWebhookAmbiguousError' ;
185+ this . webhook = refusal . name ;
186+ this . candidates = refusal . candidates ;
187+ }
188+ }
189+
190+ /** The engine's `SchemaRegistry`, when it carries the slice the package-scoped resolver reads. */
191+ function packageScopedRegistryOf ( engine : unknown ) : PackageScopedRegistry | undefined {
192+ let reg : unknown ;
193+ try {
194+ reg = ( engine as { _registry ?: unknown } | undefined ) ?. _registry ;
195+ } catch {
196+ return undefined ;
197+ }
198+ const r = reg as Partial < Record < keyof PackageScopedRegistry , unknown > > | undefined ;
199+ return typeof r ?. packageScopedHoldersOf === 'function'
200+ && typeof r . getPackageScopedItem === 'function'
201+ && typeof r . getEnvironmentScopedItem === 'function'
202+ && typeof r . getPackage === 'function'
203+ && typeof r . getAllPackages === 'function'
204+ ? ( reg as PackageScopedRegistry )
205+ : undefined ;
206+ }
207+
208+ /**
209+ * Which raw declaration serves `name` (ADR-0150 D3, a caller with no
210+ * package): the resolver's answer when the registry holds the name; else the
211+ * one listed item; else a refusal naming every listed holder.
212+ */
213+ function chooseDeclaration (
214+ name : string ,
215+ listed : readonly unknown [ ] ,
216+ registry : PackageScopedRegistry | undefined ,
217+ ) : { item : unknown } | { refusal : PackageScopedRefusal } {
218+ if ( registry ) {
219+ const resolved = resolvePackageScopedName ( registry , { type : 'webhook' , name } ) ;
220+ if ( resolved . status === 'resolved' ) return { item : resolved . item } ;
221+ if ( resolved . status === 'refused' ) return { refusal : resolved } ;
222+ // not-found: the listed items are not the registry's (the metadata service's).
223+ }
224+ if ( listed . length === 1 ) return { item : listed [ 0 ] } ;
225+ const candidates : PackageScopedHolder [ ] = [ ] ;
226+ const seen = new Set < string > ( ) ;
227+ for ( const item of listed ) {
228+ const pkg = ( item as { _packageId ?: unknown } ) ?. _packageId ;
229+ const holder : PackageScopedHolder = typeof pkg === 'string' && pkg !== '' && pkg !== 'sys_metadata'
230+ ? { kind : 'package' , packageId : pkg }
231+ : { kind : 'environment' } ;
232+ const key = holder . kind === 'package' ? holder . packageId : '' ;
233+ if ( seen . has ( key ) ) continue ;
234+ seen . add ( key ) ;
235+ candidates . push ( holder ) ;
236+ }
237+ const refusal = {
238+ status : 'refused' as const ,
239+ reason : 'ambiguous-in-environment' as const ,
240+ type : 'webhook' as const ,
241+ name,
242+ callerPackageId : null ,
243+ candidates,
244+ } ;
245+ return { refusal : { ...refusal , message : packageScopedRefusalMessage ( refusal ) } } ;
246+ }
247+
139248/**
140249 * The raw declared `webhook` items, registry first, metadata service second.
141250 * Never throws: a reader that cannot answer yields nothing from that reader.
@@ -181,6 +290,10 @@ export function readDeclaredWebhookNames(engine: unknown, metadataService: unkno
181290export class DeclaredWebhookSource implements DeclaredWebhookReader {
182291 /** Names already reported as failing validation — said once per process. */
183292 private readonly invalidReported = new Set < string > ( ) ;
293+ /** Names already reported as resolving to no single declaration — said once per process. */
294+ private readonly refusalReported = new Set < string > ( ) ;
295+ /** The names the last read refused (ADR-0150 D3), each with its refusal. */
296+ private refusals = new Map < string , PackageScopedRefusal > ( ) ;
184297
185298 constructor (
186299 private readonly engine : unknown ,
@@ -201,13 +314,48 @@ export class DeclaredWebhookSource implements DeclaredWebhookReader {
201314
202315 async declaredWebhookNamed ( name : string ) : Promise < DeclaredWebhook | null > {
203316 if ( ! name ) return null ;
204- return ( await this . list ( ) ) . find ( ( d ) => d . webhook . name === name ) ?? null ;
317+ const declared = await this . list ( ) ;
318+ const refusal = this . refusals . get ( name ) ;
319+ if ( refusal ) throw new DeclaredWebhookAmbiguousError ( refusal ) ;
320+ return declared . find ( ( d ) => d . webhook . name === name ) ?? null ;
205321 }
206322
207323 private parseDeclared ( ) : Array < { webhook : WebhookParsed ; packageId ?: string } > {
208324 const out : Array < { webhook : WebhookParsed ; packageId ?: string } > = [ ] ;
209- const seen = new Set < string > ( ) ;
210- for ( const raw of readDeclaredWebhookItems ( this . engine , this . metadataService ) ) {
325+ const raws = readDeclaredWebhookItems ( this . engine , this . metadataService ) ;
326+ const listedByName = new Map < string , unknown [ ] > ( ) ;
327+ for ( const raw of raws ) {
328+ const name = ( raw as { name ?: unknown } ) ?. name ;
329+ if ( typeof name !== 'string' || name === '' ) continue ;
330+ const listed = listedByName . get ( name ) ;
331+ if ( listed ) listed . push ( raw ) ;
332+ else listedByName . set ( name , [ raw ] ) ;
333+ }
334+ const registry = packageScopedRegistryOf ( this . engine ) ;
335+ const refusals = new Map < string , PackageScopedRefusal > ( ) ;
336+ const answered = new Set < string > ( ) ;
337+ for ( const listedRaw of raws ) {
338+ let raw = listedRaw ;
339+ const listedName = ( listedRaw as { name ?: unknown } ) ?. name ;
340+ // A name serves ONE declaration (ADR-0150 D3): asked once, at its
341+ // first listing; a nameless item goes on to fail validation below.
342+ if ( typeof listedName === 'string' && listedName !== '' ) {
343+ if ( answered . has ( listedName ) ) continue ;
344+ answered . add ( listedName ) ;
345+ const chosen = chooseDeclaration ( listedName , listedByName . get ( listedName ) ?? [ listedRaw ] , registry ) ;
346+ if ( 'refusal' in chosen ) {
347+ refusals . set ( listedName , chosen . refusal ) ;
348+ if ( ! this . refusalReported . has ( listedName ) ) {
349+ this . refusalReported . add ( listedName ) ;
350+ this . logger ?. warn ?.(
351+ `[webhook] declared webhook '${ listedName } ' is not dispatched: ${ chosen . refusal . message } ` ,
352+ { name : listedName , candidates : chosen . refusal . candidates } ,
353+ ) ;
354+ }
355+ continue ;
356+ }
357+ raw = chosen . item ;
358+ }
211359 let webhook : WebhookParsed ;
212360 try {
213361 webhook = WebhookSchema . parse ( raw ) ;
@@ -222,13 +370,10 @@ export class DeclaredWebhookSource implements DeclaredWebhookReader {
222370 }
223371 continue ;
224372 }
225- // Two registrations of one name: the first wins, as the seeder's
226- // name-keyed upsert let the first one hold the row.
227- if ( seen . has ( webhook . name ) ) continue ;
228- seen . add ( webhook . name ) ;
229373 const pkg = ( raw as { _packageId ?: unknown } ) . _packageId ;
230374 out . push ( { webhook, ...( typeof pkg === 'string' && pkg ? { packageId : pkg } : { } ) } ) ;
231375 }
376+ this . refusals = refusals ;
232377 return out ;
233378 }
234379}
0 commit comments