@happyvertical/smrt-core 0.55.11 → 0.55.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/agents/cookbook.md +23 -3
  2. package/agents/object-runtime.md +11 -0
  3. package/dist/collection.d.ts.map +1 -1
  4. package/dist/collection.js +4 -1
  5. package/dist/collection.js.map +1 -1
  6. package/dist/consumer-plugin/hosted-sti-collections.d.ts.map +1 -1
  7. package/dist/consumer-plugin/hosted-sti-collections.js +6 -2
  8. package/dist/consumer-plugin/hosted-sti-collections.js.map +1 -1
  9. package/dist/cookbook/engine/document.d.ts.map +1 -1
  10. package/dist/cookbook/engine/document.js +16 -18
  11. package/dist/cookbook/engine/document.js.map +1 -1
  12. package/dist/cookbook/engine/types.d.ts +2 -0
  13. package/dist/cookbook/engine/types.d.ts.map +1 -1
  14. package/dist/cookbook/engine/types.js.map +1 -1
  15. package/dist/cookbook/v1.schema.json +60 -3
  16. package/dist/cookbook/validate.d.ts +40 -4
  17. package/dist/cookbook/validate.d.ts.map +1 -1
  18. package/dist/cookbook/validate.js +228 -58
  19. package/dist/cookbook/validate.js.map +1 -1
  20. package/dist/dispatch/bus.d.ts +9 -4
  21. package/dist/dispatch/bus.d.ts.map +1 -1
  22. package/dist/dispatch/bus.js +11 -6
  23. package/dist/dispatch/bus.js.map +1 -1
  24. package/dist/dispatch/collections/Dispatches.d.ts +15 -5
  25. package/dist/dispatch/collections/Dispatches.d.ts.map +1 -1
  26. package/dist/dispatch/collections/Dispatches.js +29 -9
  27. package/dist/dispatch/collections/Dispatches.js.map +1 -1
  28. package/dist/dispatch/types.d.ts +5 -1
  29. package/dist/dispatch/types.d.ts.map +1 -1
  30. package/dist/manifest/static-manifest.js +1 -1
  31. package/dist/manifest/static-manifest.js.map +1 -1
  32. package/dist/manifest/store.js +1 -1
  33. package/dist/manifest.json +1 -1
  34. package/dist/object.d.ts.map +1 -1
  35. package/dist/object.js +5 -1
  36. package/dist/object.js.map +1 -1
  37. package/dist/scanner/types.d.ts +8 -0
  38. package/dist/scanner/types.d.ts.map +1 -1
  39. package/dist/scanner/types.js.map +1 -1
  40. package/dist/smrt-knowledge.json +7 -7
  41. package/dist/utils.d.ts +21 -0
  42. package/dist/utils.d.ts.map +1 -1
  43. package/dist/utils.js +52 -2
  44. package/dist/utils.js.map +1 -1
  45. package/dist/vite-plugin/sveltekit-generator.d.ts.map +1 -1
  46. package/dist/vite-plugin/sveltekit-generator.js +4 -1
  47. package/dist/vite-plugin/sveltekit-generator.js.map +1 -1
  48. package/dist/vite-plugin/sync-apply-route.d.ts.map +1 -1
  49. package/dist/vite-plugin/sync-apply-route.js +1 -0
  50. package/dist/vite-plugin/sync-apply-route.js.map +1 -1
  51. package/package.json +10 -10
@@ -1 +1 @@
1
- {"version":3,"file":"bus.js","names":[],"sources":["../../src/dispatch/bus.ts"],"sourcesContent":["/**\n * DispatchBus - Central hub for inter-agent communication\n *\n * The DispatchBus provides both in-memory handlers and persistent subscriptions\n * for asynchronous agent-to-agent messaging.\n *\n * @example\n * ```typescript\n * const bus = await createDispatchBus({ db: { type: 'sqlite', url: 'app.db' } });\n *\n * // In-memory handler (immediate)\n * bus.on('campaign.completed', async (payload, metadata) => {\n * console.log(`Campaign ${payload.campaignId} completed`);\n * });\n *\n * // Persistent subscription (processed later)\n * await bus.subscribe({ signalType: 'campaign.*', subscriber: 'fiscus' });\n *\n * // Emit a dispatch\n * await bus.emit('campaign.completed', { campaignId: '123' }, { source: 'suasor' });\n *\n * // Process pending dispatches\n * await bus.process('fiscus');\n * ```\n */\n\nimport { createLogger } from '@happyvertical/logger';\nimport type { DatabaseInterface } from '@happyvertical/sql';\nimport { getDatabase } from '../host.js';\nimport { applyPostgresRuntimeTimeouts } from '../postgres-timeouts.js';\nimport { runSerializedAgainstSystemTableBootstrap } from '../system/bootstrap.js';\nimport {\n assertPostgresSystemTimestampsCurrent,\n ensureDispatchSubscriptionsSystemTableCompatibility,\n ensureDispatchSystemTableCompatibility,\n getDatabaseEngine,\n} from '../system/compatibility.js';\nimport {\n CREATE_SMRT_DISPATCH_SUBSCRIPTIONS_TABLE,\n CREATE_SMRT_DISPATCH_TABLE,\n getSystemTableDDLForEngine,\n} from '../system/schema.js';\nimport { DispatchCollection } from './collections/Dispatches.js';\nimport { DispatchSubscriptionCollection } from './collections/DispatchSubscriptions.js';\nimport { Dispatch } from './models/Dispatch.js';\nimport { DispatchSubscription } from './models/DispatchSubscription.js';\nimport {\n resolveDispatchTenantId,\n resolveDispatchTenantScope,\n} from './tenant-resolver.js';\nimport type {\n DispatchBusOptions,\n DispatchCleanupOptions,\n DispatchCleanupResult,\n DispatchEmitOptions,\n DispatchHandler,\n DispatchListOptions,\n DispatchMetadata,\n DispatchProcessOptions,\n DispatchRetryOptions,\n DispatchSubscribeOptions,\n} from './types.js';\n\nconst logger = createLogger({ level: 'info' });\n\n/**\n * Registered in-memory handler\n */\ninterface RegisteredHandler {\n pattern: string;\n handler: DispatchHandler;\n subscription: DispatchSubscription;\n}\n\n/**\n * Central hub for inter-agent messaging with both in-memory and persistent delivery.\n *\n * The `DispatchBus` combines two complementary delivery models:\n *\n * - **In-memory handlers** (`on(pattern, handler)`) — called synchronously (fire-and-forget)\n * when a dispatch is emitted. Fast, but lost on process restart.\n * - **Persistent subscriptions** (`subscribe({ signalType, subscriber })`) — stored in\n * `_smrt_dispatch_subscriptions` and processed later by calling `process(subscriber, handler)`.\n * Survive restarts; suitable for background workers and scheduled agents.\n *\n * Signal types support single-segment wildcards: `'campaign.*'` matches `'campaign.completed'`\n * and `'campaign.failed'`, but not `'campaign.phase.two'`.\n *\n * Dispatch lifecycle: `pending → processing → completed` (or `failed` on handler error).\n * Use `retry()` to reset failed dispatches back to `pending`.\n *\n * Create instances via `createDispatchBus()` — do not use `new DispatchBus()` directly\n * in application code.\n *\n * @example\n * ```typescript\n * const bus = await createDispatchBus({ db: myDb });\n *\n * // In-memory: immediate, fire-and-forget\n * bus.on('invoice.paid', async (payload) => {\n * console.log('Invoice paid:', payload.invoiceId);\n * });\n *\n * // Persistent: processed by 'notifications' agent on next run\n * await bus.subscribe({ signalType: 'invoice.*', subscriber: 'notifications' });\n *\n * // Emit (notifies in-memory handlers immediately, stores persistent dispatch)\n * await bus.emit('invoice.paid', { invoiceId: 'inv-001' }, { source: 'billing' });\n *\n * // Later, in the notifications agent:\n * await bus.process('notifications', async (payload) => {\n * await sendEmail(payload.invoiceId);\n * });\n * ```\n */\nexport class DispatchBus {\n private db: DatabaseInterface;\n private handlers: Map<string, RegisteredHandler[]> = new Map();\n private initialized: boolean = false;\n\n /**\n * Reserved subscriber name used internally for in-memory `on()` handlers.\n * Callers may not register persistent subscriptions under this name, nor\n * assert it as an emit `source`, so they cannot impersonate the in-memory\n * pseudo-subscriber (S5 #1398).\n */\n private static readonly RESERVED_SUBSCRIBER = '_in_memory_';\n\n /** Maximum stored length of a caller-asserted dispatch source label. */\n private static readonly MAX_SOURCE_LENGTH = 256;\n\n /**\n * Normalize a caller-asserted `source` into untrusted metadata.\n *\n * `source` is a declared label, not an authenticated identity, so it must not\n * be trusted for authorization. This caps its length and **rejects** the\n * reserved internal sentinel (`_in_memory_`) by throwing — the contract is\n * that the sentinel is not an accepted source, so a caller cannot quietly\n * impersonate the in-memory subscriber pseudo-source (S5 #1398). Empty/missing\n * values default to `'unknown'` (unchanged behavior).\n *\n * @throws Error if `source` is the reserved in-memory sentinel.\n */\n private static sanitizeSource(source: string | undefined): string {\n const raw = (source ?? '').trim();\n if (!raw) {\n return 'unknown';\n }\n if (raw === DispatchBus.RESERVED_SUBSCRIBER) {\n throw new Error(\n `DispatchBus.emit: \"${DispatchBus.RESERVED_SUBSCRIBER}\" is a reserved source and cannot be asserted`,\n );\n }\n return raw.slice(0, DispatchBus.MAX_SOURCE_LENGTH);\n }\n\n /**\n * Create a new DispatchBus (use createDispatchBus factory instead)\n */\n constructor(db: DatabaseInterface) {\n this.db = db;\n }\n\n /**\n * Initialize the dispatch tables\n */\n async initialize(): Promise<void> {\n if (this.initialized) return;\n\n // `_smrt_dispatch`/`_smrt_dispatch_subscriptions` are framework-owned\n // system tables (part of `ALL_SYSTEM_TABLES`), but this standalone\n // initializer provisions/verifies them independently of a full\n // `ensureSystemTables()` bootstrap. Without serialization, concurrent\n // `DispatchBus` instances against the same already-warmed PostgreSQL\n // database (e.g. multiple `Suasor`/`Agent` instantiations, or concurrent\n // test files sharing one database) race on unguarded DDL — proven to\n // throw `duplicate key value violates unique constraint\n // \"pg_type_typname_nsp_index\"` on concurrent `CREATE TABLE IF NOT\n // EXISTS` (#2861). Fresh DDL always creates timezone-aware columns (see\n // `getSystemTableDDLForEngine()`), so this race does not itself produce a\n // legacy-typed column; a stale `assertPostgresSystemTimestampsCurrent()`\n // guard requires a *separate* concurrent actor to have put some\n // `_smrt_*` table into a legacy-typed state (e.g. a test deliberately\n // simulating a pre-migration database), which this lock does not by\n // itself rule out — see the issue for that residual risk. Running this\n // under the same advisory lock `ensureSystemTables()` uses makes every\n // writer of these tables mutually exclusive instead of merely\n // idempotent, which is what the proven race requires.\n //\n // Resolve the engine from `this.db` — the caller-supplied handle — once,\n // up front. `runSerializedAgainstSystemTableBootstrap()` hands the\n // callback a *different* handle on PostgreSQL (the lock-holding\n // transaction it opens), and that handle is not guaranteed to carry the\n // `url`/`type` (or `config.url`/`config.type`) fields engine detection\n // reads (see the `db.config?.url`-style adapters `getDatabaseEngine()`\n // already supports elsewhere in this package). Recomputing the engine\n // on the transaction handle can therefore silently disagree with the\n // outer gate that just decided `postgres` — `detectEngine('', undefined)`\n // falls back to `sqlite`, which would create the dispatch tables with\n // `TIMESTAMP` instead of `TIMESTAMPTZ` and skip the PostgreSQL timestamp\n // guard entirely. Threading the one resolved `engine` through — as\n // `ensureSystemTables()`/`bootstrapSystemTables()` already thread\n // `typeHint` — keeps the inner detection bound to the outer decision.\n const engine = getDatabaseEngine(this.db);\n await runSerializedAgainstSystemTableBootstrap(\n this.db,\n engine,\n async (tx) => {\n // Create dispatch tables if they don't exist\n const dispatchExists = await DispatchCollection.tableExists(tx, engine);\n if (!dispatchExists) {\n // Split the DDL into separate statements and execute each\n const statements = getSystemTableDDLForEngine(\n CREATE_SMRT_DISPATCH_TABLE,\n engine,\n )\n .split(';')\n .filter((s) => s.trim());\n for (const stmt of statements) {\n await tx.query(stmt);\n }\n }\n await assertPostgresSystemTimestampsCurrent(tx, engine);\n await ensureDispatchSystemTableCompatibility(tx, engine);\n\n const subsExists = await DispatchSubscriptionCollection.tableExists(\n tx,\n engine,\n );\n if (!subsExists) {\n const statements = getSystemTableDDLForEngine(\n CREATE_SMRT_DISPATCH_SUBSCRIPTIONS_TABLE,\n engine,\n )\n .split(';')\n .filter((s) => s.trim());\n for (const stmt of statements) {\n await tx.query(stmt);\n }\n }\n await assertPostgresSystemTimestampsCurrent(tx, engine);\n await ensureDispatchSubscriptionsSystemTableCompatibility(tx, engine);\n },\n );\n\n this.initialized = true;\n }\n\n /**\n * Emits a dispatch message.\n *\n * Two things happen atomically (from the caller's perspective):\n * 1. One or more `Dispatch` rows are inserted with `status: 'pending'` into `_smrt_dispatch`.\n * 2. All matching in-memory handlers (registered via `on()`) are invoked\n * fire-and-forget (errors are caught and logged, not thrown back to the caller).\n *\n * Persistent subscriptions registered via `subscribe()` will see this dispatch\n * the next time `process(subscriber, handler)` is called for their subscriber name.\n *\n * **Delivery modes** affect how dispatches are created:\n * - `compete` subscribers share a single dispatch (`target_subscriber = NULL`).\n * First subscriber to `process()` claims it (at-most-once delivery).\n * - `fanout` subscribers each get their own dispatch copy (`target_subscriber` set\n * to the subscriber name), so each processes independently.\n *\n * If no subscriptions exist, one dispatch is created for future processing.\n *\n * **Security (S5 #1398):**\n * - `tenant_id` is derived **server-side** from the active tenant context and\n * is never read from caller options, so it cannot be spoofed. Subscribers in\n * a different tenant cannot see or claim this dispatch (see {@link process}).\n * When there is no active tenant context (system/global), `tenant_id` is\n * `NULL` and the dispatch is visible to all scopes — preserving pre-tenancy\n * behavior.\n * - `options.source` is **caller-asserted, untrusted metadata** — it is a\n * declared label only and must not be relied on as an authenticated emitter\n * identity. It is length-capped and the reserved internal sentinel\n * (`_in_memory_`) is rejected so a caller cannot impersonate the in-memory\n * subscriber pseudo-source.\n *\n * @param type - Signal type string, e.g. `'campaign.completed'` or `'invoice.paid'`\n * @param payload - Any JSON-serializable data to attach to the dispatch\n * @param options.source - Declared (untrusted) name of the emitting agent/component (default `'unknown'`)\n * @param options.sourceId - Optional ID of the specific emitting entity\n * @param options.metadata - Optional additional JSON metadata for the dispatch record\n * @returns A persisted `Dispatch` instance (the compete dispatch, or the first fanout copy if fanout-only)\n *\n * @example\n * ```typescript\n * const dispatch = await bus.emit(\n * 'campaign.completed',\n * { campaignId: 'cmp-001', impressions: 10_000 },\n * { source: 'suasor', sourceId: agentId },\n * );\n * console.log(dispatch.id); // UUID of the created dispatch record\n * ```\n *\n * @see {@link on} for in-memory handlers\n * @see {@link subscribe} for persistent subscriptions\n * @see {@link process} to consume pending dispatches\n */\n async emit(\n type: string,\n payload: unknown,\n options: DispatchEmitOptions = {},\n ): Promise<Dispatch> {\n await this.initialize();\n\n // Derive the tenant scope server-side from the active context. This is the\n // trust anchor for cross-tenant isolation (S5 #1398) — it is never taken\n // from caller options, so it cannot be spoofed. `undefined`/`null` means\n // \"no tenant context\" → tenant_id stays NULL (global dispatch).\n const tenantId = resolveDispatchTenantId() ?? null;\n\n // Treat the caller-asserted source as untrusted metadata.\n const source = DispatchBus.sanitizeSource(options.source);\n\n // Query subscriptions to determine if fan-out copies are needed.\n // This runs on every emit() — uses SQL-level exact matching with\n // in-memory fallback only for wildcard subscriptions to minimize I/O.\n // Scoped to the active tenant so emit only fans out to this tenant's\n // (and, for global emits, global) subscriptions (S5 #1398).\n const matchingSubs = await DispatchSubscriptionCollection.findBySignalType(\n this.db,\n type,\n resolveDispatchTenantScope(),\n );\n\n // Separate into compete and fanout subscribers\n const fanoutSubs = matchingSubs.filter((s) => s.delivery === 'fanout');\n const competeSubs = matchingSubs.filter((s) => s.delivery !== 'fanout');\n\n // Create the base dispatch record\n const dispatch = new Dispatch({\n type,\n source,\n source_id: options.sourceId || null,\n payload: JSON.stringify(payload || {}),\n metadata: JSON.stringify(options.metadata || {}),\n status: 'pending',\n correlation_id: options.correlationId || null,\n tenant_id: tenantId,\n });\n\n // Track first persisted dispatch to return to caller\n let returnDispatch = dispatch;\n\n // For fanout subscribers: create a per-subscriber dispatch copy\n for (const sub of fanoutSubs) {\n const fanoutDispatch = new Dispatch({\n type,\n source,\n source_id: options.sourceId || null,\n payload: JSON.stringify(payload || {}),\n metadata: JSON.stringify(options.metadata || {}),\n status: 'pending',\n target_subscriber: sub.subscriber,\n correlation_id: options.correlationId || null,\n tenant_id: tenantId,\n });\n await DispatchCollection.insert(this.db, fanoutDispatch);\n // If no compete dispatch will be persisted, return the first fanout copy\n if (competeSubs.length === 0 && matchingSubs.length > 0) {\n returnDispatch = fanoutDispatch;\n }\n }\n\n // For compete subscribers (or if no subscriptions found): insert the original\n // dispatch with target_subscriber = NULL so any compete subscriber can claim it\n if (competeSubs.length > 0 || matchingSubs.length === 0) {\n await DispatchCollection.insert(this.db, dispatch);\n }\n\n // Notify in-memory handlers immediately (fire-and-forget)\n this.notifyHandlers(type, payload, dispatch.getMetadata());\n\n return returnDispatch;\n }\n\n /**\n * Registers an in-memory handler for a signal type pattern.\n *\n * In-memory handlers are called immediately (fire-and-forget) during `emit()`.\n * They are stored only in memory and lost on process restart — use `subscribe()`\n * for durable, restart-safe subscriptions.\n *\n * Pattern matching supports a single-segment wildcard `*`:\n * - `'campaign.*'` matches `'campaign.completed'` and `'campaign.failed'`\n * - `'campaign.*'` does **not** match `'campaign.phase.two'`\n *\n * Multiple handlers may be registered for the same pattern — all will be called.\n *\n * @param pattern - Signal type pattern, optionally with a trailing `.*` wildcard\n * @param handler - Async or sync function to call with `(payload, metadata)`\n *\n * @example\n * ```typescript\n * bus.on('invoice.*', async (payload, metadata) => {\n * console.log(`Invoice event from ${metadata.source}:`, payload);\n * });\n * ```\n *\n * @see {@link off} to remove a handler\n * @see {@link subscribe} for persistent (restart-safe) subscriptions\n */\n on(pattern: string, handler: DispatchHandler): void {\n const subscription = new DispatchSubscription({\n signal_type: pattern,\n subscriber: '_in_memory_',\n handler: 'callback',\n });\n\n const registered: RegisteredHandler = { pattern, handler, subscription };\n\n if (!this.handlers.has(pattern)) {\n this.handlers.set(pattern, []);\n }\n this.handlers.get(pattern)?.push(registered);\n }\n\n /**\n * Remove an in-memory handler\n *\n * @param pattern - Signal type pattern\n * @param handler - Handler function to remove\n * @returns True if handler was found and removed\n */\n off(pattern: string, handler: DispatchHandler): boolean {\n const handlers = this.handlers.get(pattern);\n if (!handlers) return false;\n\n const index = handlers.findIndex((h) => h.handler === handler);\n if (index === -1) return false;\n\n handlers.splice(index, 1);\n if (handlers.length === 0) {\n this.handlers.delete(pattern);\n }\n return true;\n }\n\n /**\n * Creates or updates a persistent subscription in the database.\n *\n * Persistent subscriptions survive process restarts. When `process(subscriber, handler)`\n * is called later, all pending dispatches matching this subscriber's signal patterns\n * will be delivered.\n *\n * Calling `subscribe()` with the same `signalType`/`subscriber` pair is idempotent\n * (upsert) — it is safe to call on every agent startup.\n *\n * Set `delivery: 'fanout'` to give each subscriber their own dispatch copy.\n * Default is `'compete'` (at-most-once, first subscriber to process claims it).\n *\n * @param options.signalType - Signal type pattern to subscribe to (wildcards supported)\n * @param options.subscriber - Name that identifies this subscriber (e.g. agent class name)\n * @param options.handler - Optional method name on the subscriber to call (default `'handleDispatch'`)\n * @param options.delivery - `'compete'` (default) for at-most-once, `'fanout'` for per-subscriber copies\n * @param options.enabled - If `false`, subscription is created but disabled (default `true`)\n *\n * @example\n * ```typescript\n * // Subscribe on agent startup (idempotent)\n * await bus.subscribe({ signalType: 'campaign.*', subscriber: 'FiscusAgent' });\n *\n * // Fan-out: each subscriber gets their own dispatch copy\n * await bus.subscribe({\n * signalType: 'campaign.*',\n * subscriber: 'AuditorAgent',\n * delivery: 'fanout',\n * });\n *\n * // Later, process matching pending dispatches\n * await bus.process('FiscusAgent', async (payload, metadata) => { ... });\n * ```\n *\n * @see {@link process} to consume pending dispatches for this subscriber\n * @see {@link unsubscribe} to remove the subscription\n * @see {@link on} for non-persistent in-memory handling\n */\n async subscribe(options: DispatchSubscribeOptions): Promise<void> {\n await this.initialize();\n\n // Gate the subscriber namespace (S5 #1398): require a concrete subscriber\n // name and reject the reserved internal sentinel so callers cannot\n // register a persistent subscription that masquerades as the in-memory\n // pseudo-subscriber.\n const subscriber = (options.subscriber ?? '').trim();\n if (!subscriber) {\n throw new Error('DispatchBus.subscribe requires a non-empty subscriber');\n }\n if (subscriber === DispatchBus.RESERVED_SUBSCRIBER) {\n throw new Error(\n `DispatchBus.subscribe: \"${DispatchBus.RESERVED_SUBSCRIBER}\" is a reserved subscriber name`,\n );\n }\n const signalType = (options.signalType ?? '').trim();\n if (!signalType) {\n throw new Error('DispatchBus.subscribe requires a non-empty signalType');\n }\n\n const subscription = new DispatchSubscription({\n signal_type: signalType,\n subscriber,\n handler: options.handler || 'handleDispatch',\n delivery: options.delivery || 'compete',\n enabled: options.enabled !== false ? 1 : 0,\n // Server-derived tenant scope (never from caller options). NULL when there\n // is no active tenant context (global subscription).\n tenant_id: resolveDispatchTenantId() ?? null,\n });\n\n await DispatchSubscriptionCollection.upsert(this.db, subscription);\n }\n\n /**\n * Remove a persistent subscription\n *\n * @param signalType - Signal type pattern\n * @param subscriber - Subscriber name\n */\n async unsubscribe(signalType: string, subscriber: string): Promise<void> {\n await this.initialize();\n // Scoped to the active tenant so one tenant cannot remove another's\n // subscription (S5 #1398).\n await DispatchSubscriptionCollection.deleteByKey(\n this.db,\n signalType,\n subscriber,\n resolveDispatchTenantScope(),\n );\n }\n\n /**\n * Processes pending dispatches for a named subscriber.\n *\n * For each matching `pending` dispatch:\n * 1. Sets status to `processing`\n * 2. Calls `handler(payload, metadata)`\n * 3. On success: sets status to `completed`\n * 4. On error: sets status to `failed` (stores the error message)\n *\n * Uses a wildcard-aware query strategy: subscriptions with `*` patterns fetch\n * all pending dispatches and filter in memory; exact-match subscriptions use\n * a direct SQL `IN` query for efficiency.\n *\n * @param subscriber - The subscriber name (must match a `subscribe()` call)\n * @param handler - Function to call for each pending dispatch\n * @param options.limit - Maximum dispatches to process in one call (default 100)\n * @param options.signalTypes - Optional filter to process only specific signal types\n * @returns Number of dispatches successfully processed (excludes failed)\n *\n * @example\n * ```typescript\n * const count = await bus.process('FiscusAgent', async (payload, metadata) => {\n * if (metadata.signalType === 'campaign.completed') {\n * await generateInvoice(payload.campaignId);\n * }\n * });\n * console.log(`Processed ${count} dispatches`);\n * ```\n *\n * @see {@link subscribe} to register the persistent subscription first\n * @see {@link retry} to reset failed dispatches back to pending\n */\n async process(\n subscriber: string,\n handler: DispatchHandler,\n options: DispatchProcessOptions = {},\n ): Promise<number> {\n await this.initialize();\n\n // Resolve the active tenant scope server-side (S5 #1398). This distinguishes\n // three states (see resolveDispatchTenantScope):\n // - tenancy off → no filter; system/global + non-tenant deployments behave\n // exactly as before.\n // - active tenant T → only T's dispatches plus global (NULL) dispatches are\n // claimable, so a subscriber in tenant A cannot see/claim tenant B's.\n // - tenancy on but no active tenant → fail-closed to global (NULL) rows\n // only; a missing context never leaks other tenants' dispatches.\n const tenantScope = resolveDispatchTenantScope();\n\n // Get subscriber's subscriptions, scoped to the active tenant so a tenant-A\n // processor only matches tenant-A (and, when global, global) subscriptions.\n const subscriptions = await DispatchSubscriptionCollection.findBySubscriber(\n this.db,\n subscriber,\n true,\n tenantScope,\n );\n\n if (subscriptions.length === 0) {\n return 0;\n }\n\n // Get signal types (including wildcards)\n const signalTypes = subscriptions.map((s) => s.signalType);\n\n // For non-wildcard subscriptions, we can query directly\n // For wildcards, we need to get all pending and filter\n const hasWildcards = signalTypes.some((t) => t.includes('*'));\n\n // Helper: check if a dispatch should be visible to this subscriber\n const isVisibleToSubscriber = (dispatch: Dispatch): boolean => {\n // Tenant isolation (defense in depth — the SQL queries below also filter).\n if (tenantScope.enforced) {\n if (tenantScope.tenantId === null) {\n // Tenancy on, no active tenant → only global (NULL) rows are visible.\n if (dispatch.tenantId !== null) {\n return false;\n }\n } else if (\n dispatch.tenantId !== null &&\n dispatch.tenantId !== tenantScope.tenantId\n ) {\n // Active tenant → that tenant's rows plus global (NULL) rows only.\n return false;\n }\n }\n if (dispatch.targetSubscriber === subscriber) {\n // Targeted at this subscriber (fanout copy) — always visible\n return true;\n }\n if (dispatch.targetSubscriber !== null) {\n // Targeted at a different subscriber — not visible\n return false;\n }\n // Null target (compete dispatch): only visible if this subscriber\n // has a compete subscription matching this dispatch type\n const matchingSub = subscriptions.find((sub) =>\n sub.matches(dispatch.type),\n );\n return matchingSub?.delivery === 'compete';\n };\n\n let pendingDispatches: Dispatch[];\n if (hasWildcards) {\n // Get all pending dispatches and filter by subscription patterns + target\n const allPending = await DispatchCollection.list(this.db, {\n status: 'pending',\n limit: options.limit || 100,\n tenantScope,\n });\n\n pendingDispatches = allPending.filter(\n (dispatch) =>\n subscriptions.some((sub) => sub.matches(dispatch.type)) &&\n isVisibleToSubscriber(dispatch),\n );\n } else {\n // Direct query for exact signal types (with target_subscriber filter)\n const raw = await DispatchCollection.findPending(\n this.db,\n signalTypes,\n options.limit || 100,\n subscriber,\n tenantScope,\n );\n // Post-filter for delivery mode correctness\n pendingDispatches = raw.filter(isVisibleToSubscriber);\n }\n\n // Filter by specific signal types if provided\n if (options.signalTypes && options.signalTypes.length > 0) {\n pendingDispatches = pendingDispatches.filter((d) =>\n options.signalTypes?.includes(d.type),\n );\n }\n\n let processed = 0;\n\n for (const dispatch of pendingDispatches) {\n // Atomically claim the dispatch (S5 #1398). The conditional UPDATE\n // (WHERE id = ? AND status = 'pending' [AND tenant predicate]) closes the\n // TOCTOU window: if a competing worker — or a system-scoped processor —\n // already moved this row out of 'pending', our claim affects no rows and\n // we skip it, so a compete/global dispatch is processed at most once.\n const claimed = await DispatchCollection.claim(\n this.db,\n dispatch,\n tenantScope,\n );\n if (!claimed) {\n continue;\n }\n\n try {\n // Invoke handler\n await handler(dispatch.payload, dispatch.getMetadata());\n\n // Mark as completed\n dispatch.markCompleted(subscriber);\n await DispatchCollection.update(this.db, dispatch);\n processed++;\n } catch (error) {\n // Mark as failed\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n dispatch.markFailed(errorMessage);\n await DispatchCollection.update(this.db, dispatch);\n }\n }\n\n return processed;\n }\n\n /**\n * Retry failed dispatches\n *\n * Resets failed dispatches to pending status so they can be processed again.\n *\n * @param options - Retry options\n * @returns Number of dispatches reset\n */\n async retry(options: DispatchRetryOptions = {}): Promise<number> {\n await this.initialize();\n\n // Tenant isolation (S5 #1398): only the active tenant's failed dispatches\n // (plus global ones) are eligible — a tenant's retry() must not reset\n // another tenant's rows. Scope is derived server-side; callers cannot widen\n // it.\n const retryable = await DispatchCollection.findRetryable(\n this.db,\n options,\n resolveDispatchTenantScope(),\n );\n\n for (const dispatch of retryable) {\n dispatch.resetForRetry();\n await DispatchCollection.update(this.db, dispatch);\n }\n\n return retryable.length;\n }\n\n /**\n * Clean up old dispatches\n *\n * @param options - Cleanup options\n * @returns Number of dispatches deleted\n */\n async cleanup(\n options: DispatchCleanupOptions = {},\n ): Promise<DispatchCleanupResult> {\n await this.initialize();\n // Tenant isolation (S5 #1398): only the active tenant's dispatches (plus\n // global ones) are deleted — a tenant's cleanup() must not delete another\n // tenant's rows. Scope is derived server-side; callers cannot widen it.\n return DispatchCollection.cleanup(\n this.db,\n options,\n resolveDispatchTenantScope(),\n );\n }\n\n /**\n * Lists dispatches, scoped to the active tenant context (S5 #1398).\n *\n * The tenant scope is derived **server-side** from the active context and\n * **overrides any caller-supplied scope** — callers cannot select another\n * tenant or widen visibility to all tenants. Scoping rules:\n *\n * - tenancy off → no tenant filter (pre-tenancy behavior).\n * - active tenant T → that tenant's dispatches plus global (NULL) dispatches.\n * - tenancy on but no active tenant → global (NULL) dispatches only\n * (fail-closed; never all tenants).\n *\n * @param options - Non-tenant list filters (status/type/source/etc.). Any\n * `tenantScope` field is ignored and replaced with the server-derived one.\n */\n async list(options: DispatchListOptions = {}): Promise<Dispatch[]> {\n await this.initialize();\n // Strip any caller-supplied scope and inject the server-derived one.\n const { tenantScope: _ignoredCallerScope, ...safeOptions } = options;\n return DispatchCollection.list(this.db, {\n ...safeOptions,\n tenantScope: resolveDispatchTenantScope(),\n });\n }\n\n /**\n * Gets a dispatch by ID, enforcing the active tenant scope (S5 #1398).\n *\n * Applies the same server-derived tenant predicate as {@link list}: a\n * subscriber in tenant A cannot fetch tenant B's dispatch by id, and when\n * tenancy is on with no active tenant only global (NULL) dispatches are\n * returned. Returns `null` when the dispatch exists but is out of scope.\n */\n async get(id: string): Promise<Dispatch | null> {\n await this.initialize();\n return DispatchCollection.getScoped(\n this.db,\n id,\n resolveDispatchTenantScope(),\n );\n }\n\n /**\n * List all subscriptions\n */\n async listSubscriptions(\n subscriber?: string,\n ): Promise<DispatchSubscription[]> {\n await this.initialize();\n\n // Scoped to the active tenant so listing never leaks another tenant's\n // subscriptions (S5 #1398).\n const tenantScope = resolveDispatchTenantScope();\n\n if (subscriber) {\n return DispatchSubscriptionCollection.findBySubscriber(\n this.db,\n subscriber,\n false,\n tenantScope,\n );\n }\n return DispatchSubscriptionCollection.list(this.db, false, tenantScope);\n }\n\n /**\n * Notify in-memory handlers (fire-and-forget)\n */\n private notifyHandlers(\n type: string,\n payload: unknown,\n metadata: DispatchMetadata,\n ): void {\n // Collect all matching handlers\n const matchingHandlers: RegisteredHandler[] = [];\n\n for (const [, handlers] of this.handlers) {\n for (const registered of handlers) {\n if (registered.subscription.matches(type)) {\n matchingHandlers.push(registered);\n }\n }\n }\n\n // Fire-and-forget - don't await\n for (const registered of matchingHandlers) {\n try {\n const result = registered.handler(payload, metadata);\n // Handle both sync and async handlers\n if (result && typeof result.catch === 'function') {\n void result.catch((error: unknown) => {\n logger.error(\n `DispatchBus: Handler for \"${registered.pattern}\" failed`,\n { error },\n );\n });\n }\n } catch (error) {\n logger.error(\n `DispatchBus: Handler for \"${registered.pattern}\" failed`,\n { error },\n );\n }\n }\n }\n}\n\n/**\n * Create a DispatchBus instance\n *\n * @param options - Bus configuration\n * @returns Initialized DispatchBus\n */\nexport async function createDispatchBus(\n options: DispatchBusOptions = {},\n): Promise<DispatchBus> {\n // Resolve database configuration\n const dbConfig = options.db || options.persistence;\n\n if (!dbConfig) {\n throw new Error('DispatchBus requires a database configuration');\n }\n\n // Get or create database interface\n let db: DatabaseInterface;\n if (typeof dbConfig === 'string') {\n // `DispatchBusOptions.db` does not admit a bare string, but this branch is\n // live code and must not be the one unbounded pool a reader finds while\n // auditing this file (#2377).\n db = await getDatabase(\n applyPostgresRuntimeTimeouts({\n url: dbConfig,\n }) as Parameters<typeof getDatabase>[0],\n );\n } else if ('query' in dbConfig) {\n // Already a DatabaseInterface (has query method)\n db = dbConfig as DatabaseInterface;\n } else if ('type' in dbConfig && 'url' in dbConfig) {\n // Database config object - use getDatabase with type and url.\n // Bounded like every other runtime pool so a dispatch-bus connection is\n // not the one unbounded PostgreSQL client in the process (#2377).\n db = await getDatabase(\n applyPostgresRuntimeTimeouts({\n type: dbConfig.type as 'sqlite' | 'postgres' | 'duckdb',\n url: dbConfig.url,\n }) as Parameters<typeof getDatabase>[0],\n );\n } else {\n throw new Error('Invalid database configuration for DispatchBus');\n }\n\n const bus = new DispatchBus(db);\n await bus.initialize();\n\n return bus;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,IAAM,SAAS,aAAa,EAAE,OAAO,OAAO,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoD7C,IAAa,cAAb,MAAa,YAAY;CACvB;CACA,2BAAqD,IAAI,IAAI;CAC7D,cAA+B;;;;;;;CAQ/B,OAAwB,sBAAsB;;CAG9C,OAAwB,oBAAoB;;;;;;;;;;;;;CAc5C,OAAe,eAAe,QAAoC;EAChE,MAAM,OAAO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W,aAAa,QAAQ,IAAI,GACtC,iBAAiB,KAAK,UAAU;EAMtC,KAAK,MAAM,cAAc,kBACvB,IAAI;GACF,MAAM,SAAS,WAAW,QAAQ,SAAS,QAAQ;GAEnD,IAAI,UAAU,OAAO,OAAO,UAAU,YACpC,OAAY,OAAO,UAAmB;IACpC,OAAO,MACL,6BAA6B,WAAW,QAAQ,WAChD,EAAE,MAAM,CACV;GACF,CAAC;EAEL,SAAS,OAAO;GACd,OAAO,MACL,6BAA6B,WAAW,QAAQ,WAChD,EAAE,MAAM,CACV;EACF;CAEJ;AACF;;;;;;;AAQA,eAAsB,kBACpB,UAA8B,CAAC,GACT;CAEtB,MAAM,WAAW,QAAQ,MAAM,QAAQ;CAEvC,IAAI,CAAC,UACH,MAAM,IAAI,MAAM,+CAA+C;CAIjE,IAAI;CACJ,IAAI,OAAO,aAAa,UAItB,KAAK,MAAM,YACT,6BAA6B,EAC3B,KAAK,SACP,CAAC,CACH;MACK,IAAI,WAAW,UAEpB,KAAK;MACA,IAAI,UAAU,YAAY,SAAS,UAIxC,KAAK,MAAM,YACT,6BAA6B;EAC3B,MAAM,SAAS;EACf,KAAK,SAAS;CAChB,CAAC,CACH;MAEA,MAAM,IAAI,MAAM,gDAAgD;CAGlE,MAAM,MAAM,IAAI,YAAY,EAAE;CAC9B,MAAM,IAAI,WAAW;CAErB,OAAO;AACT"}
1
+ {"version":3,"file":"bus.js","names":[],"sources":["../../src/dispatch/bus.ts"],"sourcesContent":["/**\n * DispatchBus - Central hub for inter-agent communication\n *\n * The DispatchBus provides both in-memory handlers and persistent subscriptions\n * for asynchronous agent-to-agent messaging.\n *\n * @example\n * ```typescript\n * const bus = await createDispatchBus({ db: { type: 'sqlite', url: 'app.db' } });\n *\n * // In-memory handler (immediate)\n * bus.on('campaign.completed', async (payload, metadata) => {\n * console.log(`Campaign ${payload.campaignId} completed`);\n * });\n *\n * // Persistent subscription (processed later)\n * await bus.subscribe({ signalType: 'campaign.*', subscriber: 'fiscus' });\n *\n * // Emit a dispatch\n * await bus.emit('campaign.completed', { campaignId: '123' }, { source: 'suasor' });\n *\n * // Process pending dispatches\n * await bus.process('fiscus');\n * ```\n */\n\nimport { createLogger } from '@happyvertical/logger';\nimport type { DatabaseInterface } from '@happyvertical/sql';\nimport { getDatabase } from '../host.js';\nimport { applyPostgresRuntimeTimeouts } from '../postgres-timeouts.js';\nimport { runSerializedAgainstSystemTableBootstrap } from '../system/bootstrap.js';\nimport {\n assertPostgresSystemTimestampsCurrent,\n ensureDispatchSubscriptionsSystemTableCompatibility,\n ensureDispatchSystemTableCompatibility,\n getDatabaseEngine,\n} from '../system/compatibility.js';\nimport {\n CREATE_SMRT_DISPATCH_SUBSCRIPTIONS_TABLE,\n CREATE_SMRT_DISPATCH_TABLE,\n getSystemTableDDLForEngine,\n} from '../system/schema.js';\nimport { DispatchCollection } from './collections/Dispatches.js';\nimport { DispatchSubscriptionCollection } from './collections/DispatchSubscriptions.js';\nimport { Dispatch } from './models/Dispatch.js';\nimport { DispatchSubscription } from './models/DispatchSubscription.js';\nimport {\n resolveDispatchTenantId,\n resolveDispatchTenantScope,\n} from './tenant-resolver.js';\nimport type {\n DispatchBusOptions,\n DispatchCleanupOptions,\n DispatchCleanupResult,\n DispatchEmitOptions,\n DispatchHandler,\n DispatchListOptions,\n DispatchMetadata,\n DispatchProcessOptions,\n DispatchRetryOptions,\n DispatchSubscribeOptions,\n} from './types.js';\n\nconst logger = createLogger({ level: 'info' });\n\n/**\n * Registered in-memory handler\n */\ninterface RegisteredHandler {\n pattern: string;\n handler: DispatchHandler;\n subscription: DispatchSubscription;\n}\n\n/**\n * Central hub for inter-agent messaging with both in-memory and persistent delivery.\n *\n * The `DispatchBus` combines two complementary delivery models:\n *\n * - **In-memory handlers** (`on(pattern, handler)`) — called synchronously (fire-and-forget)\n * when a dispatch is emitted. Fast, but lost on process restart.\n * - **Persistent subscriptions** (`subscribe({ signalType, subscriber })`) — stored in\n * `_smrt_dispatch_subscriptions` and processed later by calling `process(subscriber, handler)`.\n * Survive restarts; suitable for background workers and scheduled agents.\n *\n * Signal types support single-segment wildcards: `'campaign.*'` matches `'campaign.completed'`\n * and `'campaign.failed'`, but not `'campaign.phase.two'`.\n *\n * Dispatch lifecycle: `pending → processing → completed` (or `failed` on handler error).\n * Use `retry()` to reset failed dispatches back to `pending`.\n *\n * Create instances via `createDispatchBus()` — do not use `new DispatchBus()` directly\n * in application code.\n *\n * @example\n * ```typescript\n * const bus = await createDispatchBus({ db: myDb });\n *\n * // In-memory: immediate, fire-and-forget\n * bus.on('invoice.paid', async (payload) => {\n * console.log('Invoice paid:', payload.invoiceId);\n * });\n *\n * // Persistent: processed by 'notifications' agent on next run\n * await bus.subscribe({ signalType: 'invoice.*', subscriber: 'notifications' });\n *\n * // Emit (notifies in-memory handlers immediately, stores persistent dispatch)\n * await bus.emit('invoice.paid', { invoiceId: 'inv-001' }, { source: 'billing' });\n *\n * // Later, in the notifications agent:\n * await bus.process('notifications', async (payload) => {\n * await sendEmail(payload.invoiceId);\n * });\n * ```\n */\nexport class DispatchBus {\n private db: DatabaseInterface;\n private handlers: Map<string, RegisteredHandler[]> = new Map();\n private initialized: boolean = false;\n\n /**\n * Reserved subscriber name used internally for in-memory `on()` handlers.\n * Callers may not register persistent subscriptions under this name, nor\n * assert it as an emit `source`, so they cannot impersonate the in-memory\n * pseudo-subscriber (S5 #1398).\n */\n private static readonly RESERVED_SUBSCRIBER = '_in_memory_';\n\n /** Maximum stored length of a caller-asserted dispatch source label. */\n private static readonly MAX_SOURCE_LENGTH = 256;\n\n /**\n * Normalize a caller-asserted `source` into untrusted metadata.\n *\n * `source` is a declared label, not an authenticated identity, so it must not\n * be trusted for authorization. This caps its length and **rejects** the\n * reserved internal sentinel (`_in_memory_`) by throwing — the contract is\n * that the sentinel is not an accepted source, so a caller cannot quietly\n * impersonate the in-memory subscriber pseudo-source (S5 #1398). Empty/missing\n * values default to `'unknown'` (unchanged behavior).\n *\n * @throws Error if `source` is the reserved in-memory sentinel.\n */\n private static sanitizeSource(source: string | undefined): string {\n const raw = (source ?? '').trim();\n if (!raw) {\n return 'unknown';\n }\n if (raw === DispatchBus.RESERVED_SUBSCRIBER) {\n throw new Error(\n `DispatchBus.emit: \"${DispatchBus.RESERVED_SUBSCRIBER}\" is a reserved source and cannot be asserted`,\n );\n }\n return raw.slice(0, DispatchBus.MAX_SOURCE_LENGTH);\n }\n\n /**\n * Create a new DispatchBus (use createDispatchBus factory instead)\n */\n constructor(db: DatabaseInterface) {\n this.db = db;\n }\n\n /**\n * Initialize the dispatch tables\n */\n async initialize(): Promise<void> {\n if (this.initialized) return;\n\n // `_smrt_dispatch`/`_smrt_dispatch_subscriptions` are framework-owned\n // system tables (part of `ALL_SYSTEM_TABLES`), but this standalone\n // initializer provisions/verifies them independently of a full\n // `ensureSystemTables()` bootstrap. Without serialization, concurrent\n // `DispatchBus` instances against the same already-warmed PostgreSQL\n // database (e.g. multiple `Suasor`/`Agent` instantiations, or concurrent\n // test files sharing one database) race on unguarded DDL — proven to\n // throw `duplicate key value violates unique constraint\n // \"pg_type_typname_nsp_index\"` on concurrent `CREATE TABLE IF NOT\n // EXISTS` (#2861). Fresh DDL always creates timezone-aware columns (see\n // `getSystemTableDDLForEngine()`), so this race does not itself produce a\n // legacy-typed column; a stale `assertPostgresSystemTimestampsCurrent()`\n // guard requires a *separate* concurrent actor to have put some\n // `_smrt_*` table into a legacy-typed state (e.g. a test deliberately\n // simulating a pre-migration database), which this lock does not by\n // itself rule out — see the issue for that residual risk. Running this\n // under the same advisory lock `ensureSystemTables()` uses makes every\n // writer of these tables mutually exclusive instead of merely\n // idempotent, which is what the proven race requires.\n //\n // Resolve the engine from `this.db` — the caller-supplied handle — once,\n // up front. `runSerializedAgainstSystemTableBootstrap()` hands the\n // callback a *different* handle on PostgreSQL (the lock-holding\n // transaction it opens), and that handle is not guaranteed to carry the\n // `url`/`type` (or `config.url`/`config.type`) fields engine detection\n // reads (see the `db.config?.url`-style adapters `getDatabaseEngine()`\n // already supports elsewhere in this package). Recomputing the engine\n // on the transaction handle can therefore silently disagree with the\n // outer gate that just decided `postgres` — `detectEngine('', undefined)`\n // falls back to `sqlite`, which would create the dispatch tables with\n // `TIMESTAMP` instead of `TIMESTAMPTZ` and skip the PostgreSQL timestamp\n // guard entirely. Threading the one resolved `engine` through — as\n // `ensureSystemTables()`/`bootstrapSystemTables()` already thread\n // `typeHint` — keeps the inner detection bound to the outer decision.\n const engine = getDatabaseEngine(this.db);\n await runSerializedAgainstSystemTableBootstrap(\n this.db,\n engine,\n async (tx) => {\n // Create dispatch tables if they don't exist\n const dispatchExists = await DispatchCollection.tableExists(tx, engine);\n if (!dispatchExists) {\n // Split the DDL into separate statements and execute each\n const statements = getSystemTableDDLForEngine(\n CREATE_SMRT_DISPATCH_TABLE,\n engine,\n )\n .split(';')\n .filter((s) => s.trim());\n for (const stmt of statements) {\n await tx.query(stmt);\n }\n }\n await assertPostgresSystemTimestampsCurrent(tx, engine);\n await ensureDispatchSystemTableCompatibility(tx, engine);\n\n const subsExists = await DispatchSubscriptionCollection.tableExists(\n tx,\n engine,\n );\n if (!subsExists) {\n const statements = getSystemTableDDLForEngine(\n CREATE_SMRT_DISPATCH_SUBSCRIPTIONS_TABLE,\n engine,\n )\n .split(';')\n .filter((s) => s.trim());\n for (const stmt of statements) {\n await tx.query(stmt);\n }\n }\n await assertPostgresSystemTimestampsCurrent(tx, engine);\n await ensureDispatchSubscriptionsSystemTableCompatibility(tx, engine);\n },\n );\n\n this.initialized = true;\n }\n\n /**\n * Emits a dispatch message.\n *\n * Two things happen atomically (from the caller's perspective):\n * 1. One or more `Dispatch` rows are inserted with `status: 'pending'` into `_smrt_dispatch`.\n * 2. All matching in-memory handlers (registered via `on()`) are invoked\n * fire-and-forget (errors are caught and logged, not thrown back to the caller).\n *\n * Persistent subscriptions registered via `subscribe()` will see this dispatch\n * the next time `process(subscriber, handler)` is called for their subscriber name.\n *\n * **Delivery modes** affect how dispatches are created:\n * - `compete` subscribers share a single dispatch (`target_subscriber = NULL`).\n * First subscriber to `process()` claims it (at-most-once delivery).\n * - `fanout` subscribers each get their own dispatch copy (`target_subscriber` set\n * to the subscriber name), so each processes independently.\n *\n * If no subscriptions exist, one dispatch is created for future processing.\n *\n * **Security (S5 #1398):**\n * - `tenant_id` is derived **server-side** from the active tenant context and\n * is never read from caller options, so it cannot be spoofed. Subscribers in\n * a different tenant cannot see or claim this dispatch (see {@link process}).\n * When there is no active tenant context (system/global), `tenant_id` is\n * `NULL` and the dispatch is visible to all scopes — preserving pre-tenancy\n * behavior.\n * - `options.source` is **caller-asserted, untrusted metadata** — it is a\n * declared label only and must not be relied on as an authenticated emitter\n * identity. It is length-capped and the reserved internal sentinel\n * (`_in_memory_`) is rejected so a caller cannot impersonate the in-memory\n * subscriber pseudo-source.\n *\n * @param type - Signal type string, e.g. `'campaign.completed'` or `'invoice.paid'`\n * @param payload - Any JSON-serializable data to attach to the dispatch\n * @param options.source - Declared (untrusted) name of the emitting agent/component (default `'unknown'`)\n * @param options.sourceId - Optional ID of the specific emitting entity\n * @param options.metadata - Optional additional JSON metadata for the dispatch record\n * @returns A persisted `Dispatch` instance (the compete dispatch, or the first fanout copy if fanout-only)\n *\n * @example\n * ```typescript\n * const dispatch = await bus.emit(\n * 'campaign.completed',\n * { campaignId: 'cmp-001', impressions: 10_000 },\n * { source: 'suasor', sourceId: agentId },\n * );\n * console.log(dispatch.id); // UUID of the created dispatch record\n * ```\n *\n * @see {@link on} for in-memory handlers\n * @see {@link subscribe} for persistent subscriptions\n * @see {@link process} to consume pending dispatches\n */\n async emit(\n type: string,\n payload: unknown,\n options: DispatchEmitOptions = {},\n ): Promise<Dispatch> {\n await this.initialize();\n\n // Derive the tenant scope server-side from the active context. This is the\n // trust anchor for cross-tenant isolation (S5 #1398) — it is never taken\n // from caller options, so it cannot be spoofed. `undefined`/`null` means\n // \"no tenant context\" → tenant_id stays NULL (global dispatch).\n const tenantId = resolveDispatchTenantId() ?? null;\n\n // Treat the caller-asserted source as untrusted metadata.\n const source = DispatchBus.sanitizeSource(options.source);\n\n // Query subscriptions to determine if fan-out copies are needed.\n // This runs on every emit() — uses SQL-level exact matching with\n // in-memory fallback only for wildcard subscriptions to minimize I/O.\n // Scoped to the active tenant so emit only fans out to this tenant's\n // (and, for global emits, global) subscriptions (S5 #1398).\n const matchingSubs = await DispatchSubscriptionCollection.findBySignalType(\n this.db,\n type,\n resolveDispatchTenantScope(),\n );\n\n // Separate into compete and fanout subscribers\n const fanoutSubs = matchingSubs.filter((s) => s.delivery === 'fanout');\n const competeSubs = matchingSubs.filter((s) => s.delivery !== 'fanout');\n\n // Create the base dispatch record\n const dispatch = new Dispatch({\n type,\n source,\n source_id: options.sourceId || null,\n payload: JSON.stringify(payload || {}),\n metadata: JSON.stringify(options.metadata || {}),\n status: 'pending',\n correlation_id: options.correlationId || null,\n tenant_id: tenantId,\n });\n\n // Track first persisted dispatch to return to caller\n let returnDispatch = dispatch;\n\n // For fanout subscribers: create a per-subscriber dispatch copy\n for (const sub of fanoutSubs) {\n const fanoutDispatch = new Dispatch({\n type,\n source,\n source_id: options.sourceId || null,\n payload: JSON.stringify(payload || {}),\n metadata: JSON.stringify(options.metadata || {}),\n status: 'pending',\n target_subscriber: sub.subscriber,\n correlation_id: options.correlationId || null,\n tenant_id: tenantId,\n });\n await DispatchCollection.insert(this.db, fanoutDispatch);\n // If no compete dispatch will be persisted, return the first fanout copy\n if (competeSubs.length === 0 && matchingSubs.length > 0) {\n returnDispatch = fanoutDispatch;\n }\n }\n\n // For compete subscribers (or if no subscriptions found): insert the original\n // dispatch with target_subscriber = NULL so any compete subscriber can claim it\n if (competeSubs.length > 0 || matchingSubs.length === 0) {\n await DispatchCollection.insert(this.db, dispatch);\n }\n\n // Notify in-memory handlers immediately (fire-and-forget)\n this.notifyHandlers(type, payload, dispatch.getMetadata());\n\n return returnDispatch;\n }\n\n /**\n * Registers an in-memory handler for a signal type pattern.\n *\n * In-memory handlers are called immediately (fire-and-forget) during `emit()`.\n * They are stored only in memory and lost on process restart — use `subscribe()`\n * for durable, restart-safe subscriptions.\n *\n * Pattern matching supports a single-segment wildcard `*`:\n * - `'campaign.*'` matches `'campaign.completed'` and `'campaign.failed'`\n * - `'campaign.*'` does **not** match `'campaign.phase.two'`\n *\n * Multiple handlers may be registered for the same pattern — all will be called.\n *\n * @param pattern - Signal type pattern, optionally with a trailing `.*` wildcard\n * @param handler - Async or sync function to call with `(payload, metadata)`\n *\n * @example\n * ```typescript\n * bus.on('invoice.*', async (payload, metadata) => {\n * console.log(`Invoice event from ${metadata.source}:`, payload);\n * });\n * ```\n *\n * @see {@link off} to remove a handler\n * @see {@link subscribe} for persistent (restart-safe) subscriptions\n */\n on(pattern: string, handler: DispatchHandler): void {\n const subscription = new DispatchSubscription({\n signal_type: pattern,\n subscriber: '_in_memory_',\n handler: 'callback',\n });\n\n const registered: RegisteredHandler = { pattern, handler, subscription };\n\n if (!this.handlers.has(pattern)) {\n this.handlers.set(pattern, []);\n }\n this.handlers.get(pattern)?.push(registered);\n }\n\n /**\n * Remove an in-memory handler\n *\n * @param pattern - Signal type pattern\n * @param handler - Handler function to remove\n * @returns True if handler was found and removed\n */\n off(pattern: string, handler: DispatchHandler): boolean {\n const handlers = this.handlers.get(pattern);\n if (!handlers) return false;\n\n const index = handlers.findIndex((h) => h.handler === handler);\n if (index === -1) return false;\n\n handlers.splice(index, 1);\n if (handlers.length === 0) {\n this.handlers.delete(pattern);\n }\n return true;\n }\n\n /**\n * Creates or updates a persistent subscription in the database.\n *\n * Persistent subscriptions survive process restarts. When `process(subscriber, handler)`\n * is called later, all pending dispatches matching this subscriber's signal patterns\n * will be delivered.\n *\n * Calling `subscribe()` with the same `signalType`/`subscriber` pair is idempotent\n * (upsert) — it is safe to call on every agent startup.\n *\n * Set `delivery: 'fanout'` to give each subscriber their own dispatch copy.\n * Default is `'compete'` (at-most-once, first subscriber to process claims it).\n *\n * @param options.signalType - Signal type pattern to subscribe to (wildcards supported)\n * @param options.subscriber - Name that identifies this subscriber (e.g. agent class name)\n * @param options.handler - Optional method name on the subscriber to call (default `'handleDispatch'`)\n * @param options.delivery - `'compete'` (default) for at-most-once, `'fanout'` for per-subscriber copies\n * @param options.enabled - If `false`, subscription is created but disabled (default `true`)\n *\n * @example\n * ```typescript\n * // Subscribe on agent startup (idempotent)\n * await bus.subscribe({ signalType: 'campaign.*', subscriber: 'FiscusAgent' });\n *\n * // Fan-out: each subscriber gets their own dispatch copy\n * await bus.subscribe({\n * signalType: 'campaign.*',\n * subscriber: 'AuditorAgent',\n * delivery: 'fanout',\n * });\n *\n * // Later, process matching pending dispatches\n * await bus.process('FiscusAgent', async (payload, metadata) => { ... });\n * ```\n *\n * @see {@link process} to consume pending dispatches for this subscriber\n * @see {@link unsubscribe} to remove the subscription\n * @see {@link on} for non-persistent in-memory handling\n */\n async subscribe(options: DispatchSubscribeOptions): Promise<void> {\n await this.initialize();\n\n // Gate the subscriber namespace (S5 #1398): require a concrete subscriber\n // name and reject the reserved internal sentinel so callers cannot\n // register a persistent subscription that masquerades as the in-memory\n // pseudo-subscriber.\n const subscriber = (options.subscriber ?? '').trim();\n if (!subscriber) {\n throw new Error('DispatchBus.subscribe requires a non-empty subscriber');\n }\n if (subscriber === DispatchBus.RESERVED_SUBSCRIBER) {\n throw new Error(\n `DispatchBus.subscribe: \"${DispatchBus.RESERVED_SUBSCRIBER}\" is a reserved subscriber name`,\n );\n }\n const signalType = (options.signalType ?? '').trim();\n if (!signalType) {\n throw new Error('DispatchBus.subscribe requires a non-empty signalType');\n }\n\n const subscription = new DispatchSubscription({\n signal_type: signalType,\n subscriber,\n handler: options.handler || 'handleDispatch',\n delivery: options.delivery || 'compete',\n enabled: options.enabled !== false ? 1 : 0,\n // Server-derived tenant scope (never from caller options). NULL when there\n // is no active tenant context (global subscription).\n tenant_id: resolveDispatchTenantId() ?? null,\n });\n\n await DispatchSubscriptionCollection.upsert(this.db, subscription);\n }\n\n /**\n * Remove a persistent subscription\n *\n * @param signalType - Signal type pattern\n * @param subscriber - Subscriber name\n */\n async unsubscribe(signalType: string, subscriber: string): Promise<void> {\n await this.initialize();\n // Scoped to the active tenant so one tenant cannot remove another's\n // subscription (S5 #1398).\n await DispatchSubscriptionCollection.deleteByKey(\n this.db,\n signalType,\n subscriber,\n resolveDispatchTenantScope(),\n );\n }\n\n /**\n * Processes pending dispatches for a named subscriber.\n *\n * For each matching `pending` dispatch:\n * 1. Sets status to `processing`\n * 2. Calls `handler(payload, metadata)`\n * 3. On success: sets status to `completed`\n * 4. On error: sets status to `failed` (stores the error message)\n *\n * Uses a wildcard-aware query strategy: subscriptions with `*` patterns fetch\n * all pending dispatches and filter in memory; exact-match subscriptions use\n * a direct SQL `IN` query for efficiency.\n *\n * @param subscriber - The subscriber name (must match a `subscribe()` call)\n * @param handler - Function to call for each pending dispatch\n * @param options.limit - Maximum dispatches to process in one call (default 100)\n * @param options.signalTypes - Optional filter to process only specific signal types\n * @returns Number of dispatches successfully processed (excludes failed)\n *\n * @example\n * ```typescript\n * const count = await bus.process('FiscusAgent', async (payload, metadata) => {\n * if (metadata.signalType === 'campaign.completed') {\n * await generateInvoice(payload.campaignId);\n * }\n * });\n * console.log(`Processed ${count} dispatches`);\n * ```\n *\n * @see {@link subscribe} to register the persistent subscription first\n * @see {@link retry} to reset failed dispatches back to pending\n */\n async process(\n subscriber: string,\n handler: DispatchHandler,\n options: DispatchProcessOptions = {},\n ): Promise<number> {\n await this.initialize();\n\n // Resolve the active tenant scope server-side (S5 #1398). This distinguishes\n // three states (see resolveDispatchTenantScope):\n // - tenancy off → no filter; system/global + non-tenant deployments behave\n // exactly as before.\n // - active tenant T → only T's dispatches plus global (NULL) dispatches are\n // claimable, so a subscriber in tenant A cannot see/claim tenant B's.\n // - tenancy on but no active tenant → fail-closed to global (NULL) rows\n // only; a missing context never leaks other tenants' dispatches.\n const tenantScope = resolveDispatchTenantScope();\n\n // Get subscriber's subscriptions, scoped to the active tenant so a tenant-A\n // processor only matches tenant-A (and, when global, global) subscriptions.\n const subscriptions = await DispatchSubscriptionCollection.findBySubscriber(\n this.db,\n subscriber,\n true,\n tenantScope,\n );\n\n if (subscriptions.length === 0) {\n return 0;\n }\n\n // Get signal types (including wildcards)\n const signalTypes = subscriptions.map((s) => s.signalType);\n\n // For non-wildcard subscriptions, we can query directly\n // For wildcards, we need to get all pending and filter\n const hasWildcards = signalTypes.some((t) => t.includes('*'));\n\n // Helper: check if a dispatch should be visible to this subscriber\n const isVisibleToSubscriber = (dispatch: Dispatch): boolean => {\n // Tenant isolation (defense in depth — the SQL queries below also filter).\n if (tenantScope.enforced) {\n if (tenantScope.tenantId === null) {\n // Tenancy on, no active tenant → only global (NULL) rows are visible.\n if (dispatch.tenantId !== null) {\n return false;\n }\n } else if (\n dispatch.tenantId !== null &&\n dispatch.tenantId !== tenantScope.tenantId\n ) {\n // Active tenant → that tenant's rows plus global (NULL) rows only.\n return false;\n }\n }\n if (dispatch.targetSubscriber === subscriber) {\n // Targeted at this subscriber (fanout copy) — always visible\n return true;\n }\n if (dispatch.targetSubscriber !== null) {\n // Targeted at a different subscriber — not visible\n return false;\n }\n // Null target (compete dispatch): only visible if this subscriber\n // has a compete subscription matching this dispatch type\n const matchingSub = subscriptions.find((sub) =>\n sub.matches(dispatch.type),\n );\n return matchingSub?.delivery === 'compete';\n };\n\n let pendingDispatches: Dispatch[];\n if (hasWildcards) {\n // Get all pending dispatches and filter by subscription patterns + target\n const allPending = await DispatchCollection.list(this.db, {\n status: 'pending',\n limit: options.limit || 100,\n tenantScope,\n });\n\n pendingDispatches = allPending.filter(\n (dispatch) =>\n subscriptions.some((sub) => sub.matches(dispatch.type)) &&\n isVisibleToSubscriber(dispatch),\n );\n } else {\n // Direct query for exact signal types (with target_subscriber filter)\n const raw = await DispatchCollection.findPending(\n this.db,\n signalTypes,\n options.limit || 100,\n subscriber,\n tenantScope,\n );\n // Post-filter for delivery mode correctness\n pendingDispatches = raw.filter(isVisibleToSubscriber);\n }\n\n // Filter by specific signal types if provided\n if (options.signalTypes && options.signalTypes.length > 0) {\n pendingDispatches = pendingDispatches.filter((d) =>\n options.signalTypes?.includes(d.type),\n );\n }\n\n let processed = 0;\n\n for (const dispatch of pendingDispatches) {\n // Atomically claim the dispatch (S5 #1398). The conditional UPDATE\n // (WHERE id = ? AND status = 'pending' [AND tenant predicate]) closes the\n // TOCTOU window: if a competing worker — or a system-scoped processor —\n // already moved this row out of 'pending', our claim affects no rows and\n // we skip it, so a compete/global dispatch is processed at most once.\n const claimed = await DispatchCollection.claim(\n this.db,\n dispatch,\n tenantScope,\n );\n if (!claimed) {\n continue;\n }\n\n try {\n // Invoke handler\n await handler(dispatch.payload, dispatch.getMetadata());\n\n // Mark as completed\n dispatch.markCompleted(subscriber);\n await DispatchCollection.update(this.db, dispatch);\n processed++;\n } catch (error) {\n // Mark as failed\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n dispatch.markFailed(errorMessage);\n await DispatchCollection.update(this.db, dispatch);\n }\n }\n\n return processed;\n }\n\n /**\n * Retry failed dispatches\n *\n * Resets failed dispatches to pending status so they can be processed again.\n *\n * @param options - Retry options\n * @returns Number of dispatches reset\n */\n async retry(options: DispatchRetryOptions = {}): Promise<number> {\n await this.initialize();\n\n // Tenant isolation (S5 #1398): only the active tenant's failed dispatches\n // (plus global ones) are eligible — a tenant's retry() must not reset\n // another tenant's rows. Scope is derived server-side; callers cannot widen\n // it.\n const retryable = await DispatchCollection.findRetryable(\n this.db,\n options,\n resolveDispatchTenantScope(),\n );\n\n for (const dispatch of retryable) {\n dispatch.resetForRetry();\n await DispatchCollection.update(this.db, dispatch);\n }\n\n return retryable.length;\n }\n\n /**\n * Clean up old dispatches\n *\n * @param options - Cleanup options\n * @returns Number of dispatches deleted\n */\n async cleanup(\n options: DispatchCleanupOptions = {},\n ): Promise<DispatchCleanupResult> {\n await this.initialize();\n // Tenant isolation (S5 #1398): only the active tenant's dispatches (plus\n // global ones) are deleted — a tenant's cleanup() must not delete another\n // tenant's rows. Scope is derived server-side; callers cannot widen it.\n return DispatchCollection.cleanup(\n this.db,\n options,\n resolveDispatchTenantScope(),\n );\n }\n\n /**\n * Lists dispatches, scoped to the active tenant context (S5 #1398).\n *\n * The tenant scope is derived **server-side** from the active context and\n * **overrides any caller-supplied scope** — callers cannot select another\n * tenant or widen visibility to all tenants. Scoping rules:\n *\n * - tenancy off → no tenant filter (pre-tenancy behavior).\n * - active tenant T → that tenant's dispatches only. Global (NULL-tenant)\n * dispatches are system-only on reads (#3301): a global row's payload (for\n * example a delivery emitted outside tenant context) may carry another\n * tenant's recipients/subjects. Processing (`process`) still lets a tenant\n * worker claim global work.\n * - tenancy on but no active tenant → global (NULL) dispatches only\n * (fail-closed; never all tenants).\n *\n * @param options - Non-tenant list filters (status/type/source/etc.). Any\n * `tenantScope` field is ignored and replaced with the server-derived one.\n */\n async list(options: DispatchListOptions = {}): Promise<Dispatch[]> {\n await this.initialize();\n // Strip any caller-supplied scope and inject the server-derived one.\n const { tenantScope: _ignoredCallerScope, ...safeOptions } = options;\n // `includeGlobal: false` — global (NULL-tenant) rows are system-only on\n // this inspection read (#3301).\n return DispatchCollection.list(\n this.db,\n {\n ...safeOptions,\n tenantScope: resolveDispatchTenantScope(),\n },\n false,\n );\n }\n\n /**\n * Gets a dispatch by ID, enforcing the active tenant scope (S5 #1398).\n *\n * Applies the same server-derived tenant predicate as {@link list}: a\n * subscriber in tenant A cannot fetch tenant B's dispatch, or a global\n * (NULL-tenant) dispatch, by id (#3301), and when tenancy is on with no\n * active tenant only global (NULL) dispatches are returned. Returns `null`\n * when the dispatch exists but is out of scope.\n */\n async get(id: string): Promise<Dispatch | null> {\n await this.initialize();\n return DispatchCollection.getScoped(\n this.db,\n id,\n resolveDispatchTenantScope(),\n false,\n );\n }\n\n /**\n * List all subscriptions\n */\n async listSubscriptions(\n subscriber?: string,\n ): Promise<DispatchSubscription[]> {\n await this.initialize();\n\n // Scoped to the active tenant so listing never leaks another tenant's\n // subscriptions (S5 #1398).\n const tenantScope = resolveDispatchTenantScope();\n\n if (subscriber) {\n return DispatchSubscriptionCollection.findBySubscriber(\n this.db,\n subscriber,\n false,\n tenantScope,\n );\n }\n return DispatchSubscriptionCollection.list(this.db, false, tenantScope);\n }\n\n /**\n * Notify in-memory handlers (fire-and-forget)\n */\n private notifyHandlers(\n type: string,\n payload: unknown,\n metadata: DispatchMetadata,\n ): void {\n // Collect all matching handlers\n const matchingHandlers: RegisteredHandler[] = [];\n\n for (const [, handlers] of this.handlers) {\n for (const registered of handlers) {\n if (registered.subscription.matches(type)) {\n matchingHandlers.push(registered);\n }\n }\n }\n\n // Fire-and-forget - don't await\n for (const registered of matchingHandlers) {\n try {\n const result = registered.handler(payload, metadata);\n // Handle both sync and async handlers\n if (result && typeof result.catch === 'function') {\n void result.catch((error: unknown) => {\n logger.error(\n `DispatchBus: Handler for \"${registered.pattern}\" failed`,\n { error },\n );\n });\n }\n } catch (error) {\n logger.error(\n `DispatchBus: Handler for \"${registered.pattern}\" failed`,\n { error },\n );\n }\n }\n }\n}\n\n/**\n * Create a DispatchBus instance\n *\n * @param options - Bus configuration\n * @returns Initialized DispatchBus\n */\nexport async function createDispatchBus(\n options: DispatchBusOptions = {},\n): Promise<DispatchBus> {\n // Resolve database configuration\n const dbConfig = options.db || options.persistence;\n\n if (!dbConfig) {\n throw new Error('DispatchBus requires a database configuration');\n }\n\n // Get or create database interface\n let db: DatabaseInterface;\n if (typeof dbConfig === 'string') {\n // `DispatchBusOptions.db` does not admit a bare string, but this branch is\n // live code and must not be the one unbounded pool a reader finds while\n // auditing this file (#2377).\n db = await getDatabase(\n applyPostgresRuntimeTimeouts({\n url: dbConfig,\n }) as Parameters<typeof getDatabase>[0],\n );\n } else if ('query' in dbConfig) {\n // Already a DatabaseInterface (has query method)\n db = dbConfig as DatabaseInterface;\n } else if ('type' in dbConfig && 'url' in dbConfig) {\n // Database config object - use getDatabase with type and url.\n // Bounded like every other runtime pool so a dispatch-bus connection is\n // not the one unbounded PostgreSQL client in the process (#2377).\n db = await getDatabase(\n applyPostgresRuntimeTimeouts({\n type: dbConfig.type as 'sqlite' | 'postgres' | 'duckdb',\n url: dbConfig.url,\n }) as Parameters<typeof getDatabase>[0],\n );\n } else {\n throw new Error('Invalid database configuration for DispatchBus');\n }\n\n const bus = new DispatchBus(db);\n await bus.initialize();\n\n return bus;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,IAAM,SAAS,aAAa,EAAE,OAAO,OAAO,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoD7C,IAAa,cAAb,MAAa,YAAY;CACvB;CACA,2BAAqD,IAAI,IAAI;CAC7D,cAA+B;;;;;;;CAQ/B,OAAwB,sBAAsB;;CAG9C,OAAwB,oBAAoB;;;;;;;;;;;;;CAc5C,OAAe,eAAe,QAAoC;EAChE,MAAM,OAAO,UAAU,GAAA,CAAI,KAAK;EAChC,IAAI,CAAC,KACH,OAAO;EAET,IAAI,QAAQ,YAAY,qBACtB,MAAM,IAAI,MACR,sBAAsB,YAAY,oBAAoB,8CACxD;EAEF,OAAO,IAAI,MAAM,GAAG,YAAY,iBAAiB;CACnD;;;;CAKA,YAAY,IAAuB;EACjC,KAAK,KAAK;CACZ;;;;CAKA,MAAM,aAA4B;EAChC,IAAI,KAAK,aAAa;EAoCtB,MAAM,SAAS,kBAAkB,KAAK,EAAE;EACxC,MAAM,yCACJ,KAAK,IACL,QACA,OAAO,OAAO;GAGZ,IAAI,CAAC,MADwB,mBAAmB,YAAY,IAAI,MAAM,GACjD;IAEnB,MAAM,aAAa,2BACjB,4BACA,MACF,CAAC,CACE,MAAM,GAAG,CAAC,CACV,QAAQ,MAAM,EAAE,KAAK,CAAC;IACzB,KAAK,MAAM,QAAQ,YACjB,MAAM,GAAG,MAAM,IAAI;GAEvB;GACA,MAAM,sCAAsC,IAAI,MAAM;GACtD,MAAM,uCAAuC,IAAI,MAAM;GAMvD,IAAI,CAAC,MAJ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AK,MAAM,YACT,6BAA6B;EAC3B,MAAM,SAAS;EACf,KAAK,SAAS;CAChB,CAAC,CACH;MAEA,MAAM,IAAI,MAAM,gDAAgD;CAGlE,MAAM,MAAM,IAAI,YAAY,EAAE;CAC9B,MAAM,IAAI,WAAW;CAErB,OAAO;AACT"}
@@ -30,15 +30,25 @@ export declare class DispatchCollection {
30
30
  /**
31
31
  * Get a dispatch by ID, enforcing the active tenant scope (S5 #1398).
32
32
  *
33
- * Applies the same tenant predicate as {@link list}/{@link findPending}, so a
34
- * cross-tenant or out-of-scope id lookup returns `null` rather than leaking
35
- * the row. The scope is derived server-side by the DispatchBus.
33
+ * Applies the same tenant predicate as {@link list}, so a cross-tenant or
34
+ * out-of-scope id lookup returns `null` rather than leaking the row. The
35
+ * scope is derived server-side by the DispatchBus.
36
+ *
37
+ * @param includeGlobal - Whether an active tenant also matches global
38
+ * (NULL-tenant) rows. `DispatchBus.get()` passes `false` so global rows are
39
+ * system-only on inspection reads (#3301).
36
40
  */
37
- static getScoped(db: DatabaseInterface, id: string, tenantScope?: DispatchTenantScope): Promise<Dispatch | null>;
41
+ static getScoped(db: DatabaseInterface, id: string, tenantScope?: DispatchTenantScope, includeGlobal?: boolean): Promise<Dispatch | null>;
38
42
  /**
39
43
  * List dispatches with filtering
44
+ *
45
+ * @param includeGlobal - Whether an active tenant also matches global
46
+ * (NULL-tenant) rows. Defaults to `true`, which the wildcard path of
47
+ * `DispatchBus.process()` relies on to claim global work. The public
48
+ * `DispatchBus.list()` passes `false` so global rows are system-only on
49
+ * inspection reads (#3301).
40
50
  */
41
- static list(db: DatabaseInterface, options?: DispatchListOptions): Promise<Dispatch[]>;
51
+ static list(db: DatabaseInterface, options?: DispatchListOptions, includeGlobal?: boolean): Promise<Dispatch[]>;
42
52
  /**
43
53
  * Find pending dispatches for a set of signal types
44
54
  *
@@ -1 +1 @@
1
- {"version":3,"file":"Dispatches.d.ts","sourceRoot":"","sources":["../../../src/dispatch/collections/Dispatches.ts"],"names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AC;IAUlB;;OAEG;WACU,MAAM,CAAC,EAAE,EAAE,iBAAiB,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIrE;;;;;;;OAOG;WACU,OAAO,CAClB,EAAE,EAAE,iBAAiB,EACrB,OAAO,GAAE,sBAA2B,EACpC,WAAW,CAAC,EAAE,mBAAmB,GAChC,OAAO,CAAC,qBAAqB,CAAC;IA4CjC;;;;;;;;;;;;OAYG;WACU,WAAW,CACtB,EAAE,EAAE,iBAAiB,EACrB,QAAQ,CAAC,EAAE,MAAM,GAChB,OAAO,CAAC,OAAO,CAAC;CAGpB"}
@@ -10,17 +10,27 @@ import { Dispatch } from "../models/Dispatch.js";
10
10
  *
11
11
  * - tenancy off / no scope → no predicate (pre-tenancy behavior)
12
12
  * - `enforced` + active tenant T → `(tenant_id = ? OR tenant_id IS NULL)`
13
+ * for processing paths, or `tenant_id = ?` only for the bus's public
14
+ * inspection reads (`includeGlobal: false`, #3301)
13
15
  * - `enforced` + no active tenant → `tenant_id IS NULL` (fail-closed global)
14
16
  *
15
17
  * @param placeholder - Engine-specific positional placeholder (e.g. `$3`) used
16
18
  * when a tenant id value is bound. Ignored for the no-value branches.
19
+ * @param includeGlobal - Whether an active tenant also matches global (NULL)
20
+ * rows. Processing paths (claim/findPending/retry/cleanup) keep `true` so a
21
+ * tenant worker can pick up global work (including the wildcard path of
22
+ * `DispatchBus.process()`, which uses `list`). The bus's public inspection
23
+ * reads (`DispatchBus.list()` / `get()`) pass `false`: a global row's
24
+ * payload is not owned by the tenant (e.g. a delivery emitted outside tenant
25
+ * context for another tenant's recipient), so global rows are visible to
26
+ * system scope only.
17
27
  * @returns `true` when a value placeholder was consumed (so the caller can
18
28
  * advance its param index), `false` otherwise.
19
29
  */
20
- function pushTenantPredicate(conditions, params, scope, placeholder) {
30
+ function pushTenantPredicate(conditions, params, scope, placeholder, includeGlobal = true) {
21
31
  if (!scope?.enforced) return false;
22
32
  if (scope.tenantId !== null) {
23
- conditions.push(`(tenant_id = ${placeholder} OR tenant_id IS NULL)`);
33
+ conditions.push(includeGlobal ? `(tenant_id = ${placeholder} OR tenant_id IS NULL)` : `tenant_id = ${placeholder}`);
24
34
  params.push(scope.tenantId);
25
35
  return true;
26
36
  }
@@ -85,22 +95,32 @@ var DispatchCollection = class {
85
95
  /**
86
96
  * Get a dispatch by ID, enforcing the active tenant scope (S5 #1398).
87
97
  *
88
- * Applies the same tenant predicate as {@link list}/{@link findPending}, so a
89
- * cross-tenant or out-of-scope id lookup returns `null` rather than leaking
90
- * the row. The scope is derived server-side by the DispatchBus.
98
+ * Applies the same tenant predicate as {@link list}, so a cross-tenant or
99
+ * out-of-scope id lookup returns `null` rather than leaking the row. The
100
+ * scope is derived server-side by the DispatchBus.
101
+ *
102
+ * @param includeGlobal - Whether an active tenant also matches global
103
+ * (NULL-tenant) rows. `DispatchBus.get()` passes `false` so global rows are
104
+ * system-only on inspection reads (#3301).
91
105
  */
92
- static async getScoped(db, id, tenantScope) {
106
+ static async getScoped(db, id, tenantScope, includeGlobal = true) {
93
107
  const conditions = ["id = $1"];
94
108
  const params = [id];
95
- pushTenantPredicate(conditions, params, tenantScope, "$2");
109
+ pushTenantPredicate(conditions, params, tenantScope, "$2", includeGlobal);
96
110
  const { rows } = await db.query(`SELECT * FROM _smrt_dispatch WHERE ${conditions.join(" AND ")} LIMIT 1`, ...params);
97
111
  if (rows.length === 0) return null;
98
112
  return Dispatch.fromRow(rows[0]);
99
113
  }
100
114
  /**
101
115
  * List dispatches with filtering
116
+ *
117
+ * @param includeGlobal - Whether an active tenant also matches global
118
+ * (NULL-tenant) rows. Defaults to `true`, which the wildcard path of
119
+ * `DispatchBus.process()` relies on to claim global work. The public
120
+ * `DispatchBus.list()` passes `false` so global rows are system-only on
121
+ * inspection reads (#3301).
102
122
  */
103
- static async list(db, options = {}) {
123
+ static async list(db, options = {}, includeGlobal = true) {
104
124
  const conditions = [];
105
125
  const params = [];
106
126
  let paramIndex = 1;
@@ -124,7 +144,7 @@ var DispatchCollection = class {
124
144
  conditions.push(`correlation_id = $${paramIndex++}`);
125
145
  params.push(options.correlationId);
126
146
  }
127
- if (pushTenantPredicate(conditions, params, options.tenantScope, `$${paramIndex}`)) paramIndex++;
147
+ if (pushTenantPredicate(conditions, params, options.tenantScope, `$${paramIndex}`, includeGlobal)) paramIndex++;
128
148
  let sql = "SELECT * FROM _smrt_dispatch";
129
149
  if (conditions.length > 0) sql += ` WHERE ${conditions.join(" AND ")}`;
130
150
  const safeOrderBy = options.orderBy && [
@@ -1 +1 @@
1
- {"version":3,"file":"Dispatches.js","names":[],"sources":["../../../src/dispatch/collections/Dispatches.ts"],"sourcesContent":["/**\n * DispatchCollection - Storage operations for dispatch messages\n *\n * CRUD operations for the _smrt_dispatch system table.\n */\n\nimport type { DatabaseInterface } from '@happyvertical/sql';\nimport { tableExists } from '../../system/compatibility.js';\nimport { toSafeInteger } from '../../utils/safe-integer.js';\nimport { Dispatch, type DispatchData } from '../models/Dispatch.js';\nimport type { DispatchTenantScope } from '../tenant-resolver.js';\nimport type {\n DispatchCleanupOptions,\n DispatchCleanupResult,\n DispatchListOptions,\n DispatchRetryOptions,\n DispatchStatus,\n} from '../types.js';\n\n/**\n * Build the SQL tenant predicate for the active scope (S5 #1398).\n *\n * Appends a parameterized condition to `conditions` (and the corresponding\n * value to `params`) based on the resolved {@link DispatchTenantScope}:\n *\n * - tenancy off / no scope → no predicate (pre-tenancy behavior)\n * - `enforced` + active tenant T → `(tenant_id = ? OR tenant_id IS NULL)`\n * - `enforced` + no active tenant → `tenant_id IS NULL` (fail-closed global)\n *\n * @param placeholder - Engine-specific positional placeholder (e.g. `$3`) used\n * when a tenant id value is bound. Ignored for the no-value branches.\n * @returns `true` when a value placeholder was consumed (so the caller can\n * advance its param index), `false` otherwise.\n */\nfunction pushTenantPredicate(\n conditions: string[],\n params: unknown[],\n scope: DispatchTenantScope | undefined,\n placeholder: string,\n): boolean {\n if (!scope?.enforced) {\n return false;\n }\n if (scope.tenantId !== null) {\n conditions.push(`(tenant_id = ${placeholder} OR tenant_id IS NULL)`);\n params.push(scope.tenantId);\n return true;\n }\n conditions.push('tenant_id IS NULL');\n return false;\n}\n\n/**\n * Count the rows a cleanup predicate selects, then delete them (#2375).\n *\n * Counting first gives a usable figure on every adapter — `rowCount` is not\n * reliably populated across the engines SMRT supports — and is what lets\n * `dryRun` preview the very same predicate the real cleanup runs. The two\n * statements are not transactional, so the figure is approximate under\n * concurrent writers.\n */\nasync function deleteOrCount(\n db: DatabaseInterface,\n conditions: string[],\n params: unknown[],\n dryRun: boolean,\n): Promise<number> {\n const where = conditions.join(' AND ');\n const countResult = await db.query(\n `SELECT COUNT(*) AS total FROM _smrt_dispatch WHERE ${where}`,\n ...params,\n );\n const total = toSafeInteger(\n countResult.rows?.[0]?.total ?? 0,\n 'Dispatch cleanup count',\n );\n if (total <= 0) return 0;\n\n if (!dryRun) {\n await db.query(`DELETE FROM _smrt_dispatch WHERE ${where}`, ...params);\n }\n\n return total;\n}\n\n/**\n * Storage operations for dispatches in _smrt_dispatch table\n */\nexport class DispatchCollection {\n /**\n * Insert a new dispatch\n */\n static async insert(\n db: DatabaseInterface,\n dispatch: Dispatch,\n ): Promise<void> {\n const row = dispatch.toRow();\n await db.query(\n `INSERT INTO _smrt_dispatch\n (id, type, source, source_id, payload, status, attempts, last_error,\n processed_at, processed_by, target_subscriber, correlation_id, tenant_id, metadata, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)`,\n row.id,\n row.type,\n row.source,\n row.source_id,\n row.payload,\n row.status,\n row.attempts,\n row.last_error,\n row.processed_at,\n row.processed_by,\n row.target_subscriber,\n row.correlation_id,\n row.tenant_id,\n row.metadata,\n row.created_at,\n row.updated_at,\n );\n }\n\n /**\n * Update an existing dispatch\n */\n static async update(\n db: DatabaseInterface,\n dispatch: Dispatch,\n ): Promise<void> {\n const row = dispatch.toRow();\n await db.query(\n `UPDATE _smrt_dispatch SET\n type = $1, source = $2, source_id = $3, payload = $4, status = $5,\n attempts = $6, last_error = $7, processed_at = $8, processed_by = $9,\n target_subscriber = $10, correlation_id = $11, metadata = $12, updated_at = $13\n WHERE id = $14`,\n row.type,\n row.source,\n row.source_id,\n row.payload,\n row.status,\n row.attempts,\n row.last_error,\n row.processed_at,\n row.processed_by,\n row.target_subscriber,\n row.correlation_id,\n row.metadata,\n row.updated_at,\n row.id,\n );\n }\n\n /**\n * Get a dispatch by ID (unscoped).\n *\n * Internal/system use only — applies no tenant filter. Public reads go\n * through the DispatchBus, which uses {@link getScoped} so a caller cannot\n * fetch another tenant's dispatch by id (S5 #1398).\n */\n static async get(\n db: DatabaseInterface,\n id: string,\n ): Promise<Dispatch | null> {\n const result = await db.single`\n SELECT * FROM _smrt_dispatch WHERE id = ${id}\n `;\n\n if (!result) return null;\n return Dispatch.fromRow(result as DispatchData);\n }\n\n /**\n * Get a dispatch by ID, enforcing the active tenant scope (S5 #1398).\n *\n * Applies the same tenant predicate as {@link list}/{@link findPending}, so a\n * cross-tenant or out-of-scope id lookup returns `null` rather than leaking\n * the row. The scope is derived server-side by the DispatchBus.\n */\n static async getScoped(\n db: DatabaseInterface,\n id: string,\n tenantScope?: DispatchTenantScope,\n ): Promise<Dispatch | null> {\n const conditions: string[] = ['id = $1'];\n const params: unknown[] = [id];\n\n pushTenantPredicate(conditions, params, tenantScope, '$2');\n\n const { rows } = await db.query(\n `SELECT * FROM _smrt_dispatch WHERE ${conditions.join(' AND ')} LIMIT 1`,\n ...params,\n );\n\n if (rows.length === 0) return null;\n return Dispatch.fromRow(rows[0] as unknown as DispatchData);\n }\n\n /**\n * List dispatches with filtering\n */\n static async list(\n db: DatabaseInterface,\n options: DispatchListOptions = {},\n ): Promise<Dispatch[]> {\n const conditions: string[] = [];\n const params: unknown[] = [];\n let paramIndex = 1;\n\n if (options.status) {\n conditions.push(`status = $${paramIndex++}`);\n params.push(options.status);\n }\n\n if (options.source) {\n conditions.push(`source = $${paramIndex++}`);\n params.push(options.source);\n }\n\n if (options.type) {\n conditions.push(`type = $${paramIndex++}`);\n params.push(options.type);\n }\n\n if (options.targetSubscriber) {\n conditions.push(`target_subscriber = $${paramIndex++}`);\n params.push(options.targetSubscriber);\n }\n\n if (options.correlationId !== undefined) {\n conditions.push(`correlation_id = $${paramIndex++}`);\n params.push(options.correlationId);\n }\n\n // Tenant isolation (S5 #1398). Scope is derived server-side by the bus;\n // callers cannot widen it. See DispatchListOptions.tenantScope.\n if (\n pushTenantPredicate(\n conditions,\n params,\n options.tenantScope,\n `$${paramIndex}`,\n )\n ) {\n paramIndex++;\n }\n\n let sql = 'SELECT * FROM _smrt_dispatch';\n if (conditions.length > 0) {\n sql += ` WHERE ${conditions.join(' AND ')}`;\n }\n\n // Whitelist allowed orderBy values to prevent SQL injection\n const allowedOrderBy = [\n 'created_at DESC',\n 'created_at ASC',\n 'updated_at DESC',\n 'updated_at ASC',\n 'type',\n 'type DESC',\n 'type ASC',\n 'status',\n 'status DESC',\n 'status ASC',\n 'source',\n 'source DESC',\n 'source ASC',\n ];\n const safeOrderBy =\n options.orderBy && allowedOrderBy.includes(options.orderBy)\n ? options.orderBy\n : 'created_at DESC';\n sql += ` ORDER BY ${safeOrderBy}`;\n\n // Use parameterized queries for limit/offset\n if (options.limit !== undefined) {\n sql += ` LIMIT $${paramIndex++}`;\n params.push(options.limit);\n }\n\n if (options.offset !== undefined) {\n sql += ` OFFSET $${paramIndex++}`;\n params.push(options.offset);\n }\n\n const { rows } = await db.query(sql, ...params);\n return rows.map((row: Record<string, unknown>) =>\n Dispatch.fromRow(row as unknown as DispatchData),\n );\n }\n\n /**\n * Find pending dispatches for a set of signal types\n *\n * @param subscriber - When provided, filters to dispatches with no target\n * or targeted specifically at this subscriber (for fan-out delivery)\n * @param tenantScope - Active tenant scope (S5 #1398). Derived server-side by\n * the bus; restricts results so a subscriber in one tenant cannot see\n * another tenant's dispatch. See {@link pushTenantPredicate} for the exact\n * semantics (off → no filter; active tenant → that tenant + global; on but\n * no tenant → global only).\n */\n static async findPending(\n db: DatabaseInterface,\n signalTypes: string[],\n limit: number = 100,\n subscriber?: string,\n tenantScope?: DispatchTenantScope,\n ): Promise<Dispatch[]> {\n if (signalTypes.length === 0) {\n return [];\n }\n\n let paramIndex = 1;\n const placeholders = signalTypes.map(() => `$${paramIndex++}`).join(', ');\n const conditions: string[] = [\n \"status = 'pending'\",\n `type IN (${placeholders})`,\n ];\n const params: unknown[] = [...signalTypes];\n\n if (subscriber) {\n conditions.push(\n `(target_subscriber IS NULL OR target_subscriber = $${paramIndex++})`,\n );\n params.push(subscriber);\n }\n\n if (\n pushTenantPredicate(conditions, params, tenantScope, `$${paramIndex}`)\n ) {\n paramIndex++;\n }\n\n const sql = `SELECT * FROM _smrt_dispatch\n WHERE ${conditions.join(' AND ')}\n ORDER BY created_at ASC LIMIT $${paramIndex}`;\n params.push(limit);\n\n const { rows } = await db.query(sql, ...params);\n\n return rows.map((row: Record<string, unknown>) =>\n Dispatch.fromRow(row as unknown as DispatchData),\n );\n }\n\n /**\n * Atomically claim a pending dispatch for processing (S5 #1398).\n *\n * Performs a conditional `UPDATE ... WHERE id = ? AND status = 'pending'`\n * (plus the tenant predicate for the active scope) and reports whether the\n * row was claimed by THIS call. Only the caller whose update affected a row\n * may run the handler, which closes the claim TOCTOU window where a `process`\n * filter-then-update could let two workers (or a tenant-scoped and a\n * system-scoped processor) both pick up the same compete/global dispatch.\n *\n * On a successful claim the dispatch's status/attempts/updated_at are\n * advanced in the DB; the in-memory `dispatch` instance is updated to match\n * so the caller can proceed to `update()` it to completed/failed.\n *\n * @returns `true` if this call claimed the dispatch, `false` if another\n * worker already moved it out of `pending` (or it is not visible in scope).\n */\n static async claim(\n db: DatabaseInterface,\n dispatch: Dispatch,\n tenantScope?: DispatchTenantScope,\n ): Promise<boolean> {\n dispatch.markProcessing();\n const row = dispatch.toRow();\n\n const conditions: string[] = ['id = $4', \"status = 'pending'\"];\n const params: unknown[] = [\n row.status,\n row.attempts,\n row.updated_at,\n row.id,\n ];\n let paramIndex = 5;\n\n if (\n pushTenantPredicate(conditions, params, tenantScope, `$${paramIndex}`)\n ) {\n paramIndex++;\n }\n\n const { rowCount } = await db.query(\n `UPDATE _smrt_dispatch\n SET status = $1, attempts = $2, updated_at = $3\n WHERE ${conditions.join(' AND ')}`,\n ...params,\n );\n\n return (rowCount ?? 0) > 0;\n }\n\n /**\n * Find failed dispatches eligible for retry.\n *\n * @param tenantScope - Active tenant scope (S5 #1398). Derived server-side by\n * the bus; restricts retryable rows to the active tenant (plus global) so a\n * tenant's `retry()` cannot reset another tenant's failed dispatches. See\n * {@link pushTenantPredicate} for the exact semantics.\n */\n static async findRetryable(\n db: DatabaseInterface,\n options: DispatchRetryOptions = {},\n tenantScope?: DispatchTenantScope,\n ): Promise<Dispatch[]> {\n const conditions: string[] = [\"status = 'failed'\"];\n const params: unknown[] = [];\n let paramIndex = 1;\n\n if (options.maxAttempts) {\n conditions.push(`attempts < $${paramIndex++}`);\n params.push(options.maxAttempts);\n }\n\n if (options.olderThan) {\n conditions.push(`created_at < $${paramIndex++}`);\n params.push(options.olderThan.toISOString());\n }\n\n if (options.signalTypes && options.signalTypes.length > 0) {\n const placeholders = options.signalTypes\n .map(() => `$${paramIndex++}`)\n .join(', ');\n conditions.push(`type IN (${placeholders})`);\n params.push(...options.signalTypes);\n }\n\n if (\n pushTenantPredicate(conditions, params, tenantScope, `$${paramIndex}`)\n ) {\n paramIndex++;\n }\n\n const { rows } = await db.query(\n `SELECT * FROM _smrt_dispatch WHERE ${conditions.join(' AND ')} ORDER BY created_at ASC`,\n ...params,\n );\n\n return rows.map((row: Record<string, unknown>) =>\n Dispatch.fromRow(row as unknown as DispatchData),\n );\n }\n\n /**\n * Count dispatches by status\n */\n static async countByStatus(\n db: DatabaseInterface,\n status: DispatchStatus,\n ): Promise<number> {\n const result = await db.single`\n SELECT COUNT(*) as count FROM _smrt_dispatch WHERE status = ${status}\n `;\n return toSafeInteger(\n (result as { count?: unknown } | null)?.count ?? 0,\n 'Dispatch count',\n );\n }\n\n /**\n * Delete a dispatch\n */\n static async delete(db: DatabaseInterface, id: string): Promise<void> {\n await db.query(`DELETE FROM _smrt_dispatch WHERE id = $1`, id);\n }\n\n /**\n * Cleanup old dispatches.\n *\n * @param tenantScope - Active tenant scope (S5 #1398). Derived server-side by\n * the bus; restricts deletions to the active tenant (plus global) so a\n * tenant's `cleanup()` cannot delete another tenant's dispatches. See\n * {@link pushTenantPredicate} for the exact semantics.\n */\n static async cleanup(\n db: DatabaseInterface,\n options: DispatchCleanupOptions = {},\n tenantScope?: DispatchTenantScope,\n ): Promise<DispatchCleanupResult> {\n const result: DispatchCleanupResult = {\n completedDeleted: 0,\n failedDeleted: 0,\n };\n\n if (options.completedOlderThanDays) {\n const cutoff = new Date();\n cutoff.setDate(cutoff.getDate() - options.completedOlderThanDays);\n\n const conditions: string[] = [\n \"status = 'completed'\",\n 'processed_at < $1',\n ];\n const params: unknown[] = [cutoff.toISOString()];\n pushTenantPredicate(conditions, params, tenantScope, '$2');\n\n result.completedDeleted = await deleteOrCount(\n db,\n conditions,\n params,\n options.dryRun ?? false,\n );\n }\n\n if (options.failedOlderThanDays) {\n const cutoff = new Date();\n cutoff.setDate(cutoff.getDate() - options.failedOlderThanDays);\n\n const conditions: string[] = [\"status = 'failed'\", 'updated_at < $1'];\n const params: unknown[] = [cutoff.toISOString()];\n pushTenantPredicate(conditions, params, tenantScope, '$2');\n\n result.failedDeleted = await deleteOrCount(\n db,\n conditions,\n params,\n options.dryRun ?? false,\n );\n }\n\n return result;\n }\n\n /**\n * Check if dispatch table exists.\n *\n * Delegates to the shared, engine-aware {@link tableExists} rather than\n * probing with `SELECT ... LIMIT 1` inside a try/catch: on PostgreSQL, a\n * caught query error still aborts the surrounding transaction (any\n * statement error does, regardless of whether the application handles it),\n * so a probe-and-catch existence check silently poisons a transaction any\n * caller wraps this in — exactly what `DispatchBus.initialize()`'s\n * lock-guarded bootstrap transaction needs to do (#2861). The shared helper\n * uses `information_schema.tables` on PostgreSQL instead, which never\n * raises for a missing table.\n */\n static async tableExists(\n db: DatabaseInterface,\n typeHint?: string,\n ): Promise<boolean> {\n return tableExists(db, '_smrt_dispatch', typeHint);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAkCA,SAAS,oBACP,YACA,QACA,OACA,aACS;CACT,IAAI,CAAC,OAAO,UACV,OAAO;CAET,IAAI,MAAM,aAAa,MAAM;EAC3B,WAAW,KAAK,gBAAgB,YAAY,uBAAuB;EACnE,OAAO,KAAK,MAAM,QAAQ;EAC1B,OAAO;CACT;CACA,WAAW,KAAK,mBAAmB;CACnC,OAAO;AACT;;;;;;;;;;AAWA,eAAe,cACb,IACA,YACA,QACA,QACiB;CACjB,MAAM,QAAQ,WAAW,KAAK,OAAO;CAKrC,MAAM,QAAQ,eACZ,MALwB,GAAG,MAC3B,sDAAsD,SACtD,GAAG,MACL,EAAA,CAEc,OAAO,EAAE,EAAE,SAAS,GAChC,wBACF;CACA,IAAI,SAAS,GAAG,OAAO;CAEvB,IAAI,CAAC,QACH,MAAM,GAAG,MAAM,oCAAoC,SAAS,GAAG,MAAM;CAGvE,OAAO;AACT;;;;AAKA,IAAa,qBAAb,MAAgC;;;;CAI9B,aAAa,OACX,IACA,UACe;EACf,MAAM,MAAM,SAAS,MAAM;EAC3B,MAAM,GAAG,MACP;;;wFAIA,IAAI,IACJ,IAAI,MACJ,IAAI,QACJ,IAAI,WACJ,IAAI,SACJ,IAAI,QACJ,IAAI,UACJ,IAAI,YACJ,IAAI,cACJ,IAAI,cACJ,IAAI,mBACJ,IAAI,gBACJ,IAAI,WACJ,IAAI,UACJ,IAAI,YACJ,IAAI,UACN;CACF;;;;CAKA,aAAa,OACX,IACA,UACe;EACf,MAAM,MAAM,SAAS,MAAM;EAC3B,MAAM,GAAG,MACP;;;;wBAKA,IAAI,MACJ,IAAI,QACJ,IAAI,WACJ,IAAI,SACJ,IAAI,QACJ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gBACF;CACF;;;;CAKA,aAAa,OAAO,IAAuB,IAA2B;EACpE,MAAM,GAAG,MAAM,4CAA4C,EAAE;CAC/D;;;;;;;;;CAUA,aAAa,QACX,IACA,UAAkC,CAAC,GACnC,aACgC;EAChC,MAAM,SAAgC;GACpC,kBAAkB;GAClB,eAAe;EACjB;EAEA,IAAI,QAAQ,wBAAwB;GAClC,MAAM,yBAAS,IAAI,KAAK;GACxB,OAAO,QAAQ,OAAO,QAAQ,IAAI,QAAQ,sBAAsB;GAEhE,MAAM,aAAuB,CAC3B,wBACA,mBACF;GACA,MAAM,SAAoB,CAAC,OAAO,YAAY,CAAC;GAC/C,oBAAoB,YAAY,QAAQ,aAAa,IAAI;GAEzD,OAAO,mBAAmB,MAAM,cAC9B,IACA,YACA,QACA,QAAQ,UAAU,KACpB;EACF;EAEA,IAAI,QAAQ,qBAAqB;GAC/B,MAAM,yBAAS,IAAI,KAAK;GACxB,OAAO,QAAQ,OAAO,QAAQ,IAAI,QAAQ,mBAAmB;GAE7D,MAAM,aAAuB,CAAC,qBAAqB,iBAAiB;GACpE,MAAM,SAAoB,CAAC,OAAO,YAAY,CAAC;GAC/C,oBAAoB,YAAY,QAAQ,aAAa,IAAI;GAEzD,OAAO,gBAAgB,MAAM,cAC3B,IACA,YACA,QACA,QAAQ,UAAU,KACpB;EACF;EAEA,OAAO;CACT;;;;;;;;;;;;;;CAeA,aAAa,YACX,IACA,UACkB;EAClB,OAAO,YAAY,IAAI,kBAAkB,QAAQ;CACnD;AACF"}
1
+ {"version":3,"file":"Dispatches.js","names":[],"sources":["../../../src/dispatch/collections/Dispatches.ts"],"sourcesContent":["/**\n * DispatchCollection - Storage operations for dispatch messages\n *\n * CRUD operations for the _smrt_dispatch system table.\n */\n\nimport type { DatabaseInterface } from '@happyvertical/sql';\nimport { tableExists } from '../../system/compatibility.js';\nimport { toSafeInteger } from '../../utils/safe-integer.js';\nimport { Dispatch, type DispatchData } from '../models/Dispatch.js';\nimport type { DispatchTenantScope } from '../tenant-resolver.js';\nimport type {\n DispatchCleanupOptions,\n DispatchCleanupResult,\n DispatchListOptions,\n DispatchRetryOptions,\n DispatchStatus,\n} from '../types.js';\n\n/**\n * Build the SQL tenant predicate for the active scope (S5 #1398).\n *\n * Appends a parameterized condition to `conditions` (and the corresponding\n * value to `params`) based on the resolved {@link DispatchTenantScope}:\n *\n * - tenancy off / no scope → no predicate (pre-tenancy behavior)\n * - `enforced` + active tenant T → `(tenant_id = ? OR tenant_id IS NULL)`\n * for processing paths, or `tenant_id = ?` only for the bus's public\n * inspection reads (`includeGlobal: false`, #3301)\n * - `enforced` + no active tenant → `tenant_id IS NULL` (fail-closed global)\n *\n * @param placeholder - Engine-specific positional placeholder (e.g. `$3`) used\n * when a tenant id value is bound. Ignored for the no-value branches.\n * @param includeGlobal - Whether an active tenant also matches global (NULL)\n * rows. Processing paths (claim/findPending/retry/cleanup) keep `true` so a\n * tenant worker can pick up global work (including the wildcard path of\n * `DispatchBus.process()`, which uses `list`). The bus's public inspection\n * reads (`DispatchBus.list()` / `get()`) pass `false`: a global row's\n * payload is not owned by the tenant (e.g. a delivery emitted outside tenant\n * context for another tenant's recipient), so global rows are visible to\n * system scope only.\n * @returns `true` when a value placeholder was consumed (so the caller can\n * advance its param index), `false` otherwise.\n */\nfunction pushTenantPredicate(\n conditions: string[],\n params: unknown[],\n scope: DispatchTenantScope | undefined,\n placeholder: string,\n includeGlobal = true,\n): boolean {\n if (!scope?.enforced) {\n return false;\n }\n if (scope.tenantId !== null) {\n conditions.push(\n includeGlobal\n ? `(tenant_id = ${placeholder} OR tenant_id IS NULL)`\n : `tenant_id = ${placeholder}`,\n );\n params.push(scope.tenantId);\n return true;\n }\n conditions.push('tenant_id IS NULL');\n return false;\n}\n\n/**\n * Count the rows a cleanup predicate selects, then delete them (#2375).\n *\n * Counting first gives a usable figure on every adapter — `rowCount` is not\n * reliably populated across the engines SMRT supports — and is what lets\n * `dryRun` preview the very same predicate the real cleanup runs. The two\n * statements are not transactional, so the figure is approximate under\n * concurrent writers.\n */\nasync function deleteOrCount(\n db: DatabaseInterface,\n conditions: string[],\n params: unknown[],\n dryRun: boolean,\n): Promise<number> {\n const where = conditions.join(' AND ');\n const countResult = await db.query(\n `SELECT COUNT(*) AS total FROM _smrt_dispatch WHERE ${where}`,\n ...params,\n );\n const total = toSafeInteger(\n countResult.rows?.[0]?.total ?? 0,\n 'Dispatch cleanup count',\n );\n if (total <= 0) return 0;\n\n if (!dryRun) {\n await db.query(`DELETE FROM _smrt_dispatch WHERE ${where}`, ...params);\n }\n\n return total;\n}\n\n/**\n * Storage operations for dispatches in _smrt_dispatch table\n */\nexport class DispatchCollection {\n /**\n * Insert a new dispatch\n */\n static async insert(\n db: DatabaseInterface,\n dispatch: Dispatch,\n ): Promise<void> {\n const row = dispatch.toRow();\n await db.query(\n `INSERT INTO _smrt_dispatch\n (id, type, source, source_id, payload, status, attempts, last_error,\n processed_at, processed_by, target_subscriber, correlation_id, tenant_id, metadata, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)`,\n row.id,\n row.type,\n row.source,\n row.source_id,\n row.payload,\n row.status,\n row.attempts,\n row.last_error,\n row.processed_at,\n row.processed_by,\n row.target_subscriber,\n row.correlation_id,\n row.tenant_id,\n row.metadata,\n row.created_at,\n row.updated_at,\n );\n }\n\n /**\n * Update an existing dispatch\n */\n static async update(\n db: DatabaseInterface,\n dispatch: Dispatch,\n ): Promise<void> {\n const row = dispatch.toRow();\n await db.query(\n `UPDATE _smrt_dispatch SET\n type = $1, source = $2, source_id = $3, payload = $4, status = $5,\n attempts = $6, last_error = $7, processed_at = $8, processed_by = $9,\n target_subscriber = $10, correlation_id = $11, metadata = $12, updated_at = $13\n WHERE id = $14`,\n row.type,\n row.source,\n row.source_id,\n row.payload,\n row.status,\n row.attempts,\n row.last_error,\n row.processed_at,\n row.processed_by,\n row.target_subscriber,\n row.correlation_id,\n row.metadata,\n row.updated_at,\n row.id,\n );\n }\n\n /**\n * Get a dispatch by ID (unscoped).\n *\n * Internal/system use only — applies no tenant filter. Public reads go\n * through the DispatchBus, which uses {@link getScoped} so a caller cannot\n * fetch another tenant's dispatch by id (S5 #1398).\n */\n static async get(\n db: DatabaseInterface,\n id: string,\n ): Promise<Dispatch | null> {\n const result = await db.single`\n SELECT * FROM _smrt_dispatch WHERE id = ${id}\n `;\n\n if (!result) return null;\n return Dispatch.fromRow(result as DispatchData);\n }\n\n /**\n * Get a dispatch by ID, enforcing the active tenant scope (S5 #1398).\n *\n * Applies the same tenant predicate as {@link list}, so a cross-tenant or\n * out-of-scope id lookup returns `null` rather than leaking the row. The\n * scope is derived server-side by the DispatchBus.\n *\n * @param includeGlobal - Whether an active tenant also matches global\n * (NULL-tenant) rows. `DispatchBus.get()` passes `false` so global rows are\n * system-only on inspection reads (#3301).\n */\n static async getScoped(\n db: DatabaseInterface,\n id: string,\n tenantScope?: DispatchTenantScope,\n includeGlobal = true,\n ): Promise<Dispatch | null> {\n const conditions: string[] = ['id = $1'];\n const params: unknown[] = [id];\n\n pushTenantPredicate(conditions, params, tenantScope, '$2', includeGlobal);\n\n const { rows } = await db.query(\n `SELECT * FROM _smrt_dispatch WHERE ${conditions.join(' AND ')} LIMIT 1`,\n ...params,\n );\n\n if (rows.length === 0) return null;\n return Dispatch.fromRow(rows[0] as unknown as DispatchData);\n }\n\n /**\n * List dispatches with filtering\n *\n * @param includeGlobal - Whether an active tenant also matches global\n * (NULL-tenant) rows. Defaults to `true`, which the wildcard path of\n * `DispatchBus.process()` relies on to claim global work. The public\n * `DispatchBus.list()` passes `false` so global rows are system-only on\n * inspection reads (#3301).\n */\n static async list(\n db: DatabaseInterface,\n options: DispatchListOptions = {},\n includeGlobal = true,\n ): Promise<Dispatch[]> {\n const conditions: string[] = [];\n const params: unknown[] = [];\n let paramIndex = 1;\n\n if (options.status) {\n conditions.push(`status = $${paramIndex++}`);\n params.push(options.status);\n }\n\n if (options.source) {\n conditions.push(`source = $${paramIndex++}`);\n params.push(options.source);\n }\n\n if (options.type) {\n conditions.push(`type = $${paramIndex++}`);\n params.push(options.type);\n }\n\n if (options.targetSubscriber) {\n conditions.push(`target_subscriber = $${paramIndex++}`);\n params.push(options.targetSubscriber);\n }\n\n if (options.correlationId !== undefined) {\n conditions.push(`correlation_id = $${paramIndex++}`);\n params.push(options.correlationId);\n }\n\n // Tenant isolation (S5 #1398). Scope is derived server-side by the bus;\n // callers cannot widen it. See DispatchListOptions.tenantScope and the\n // `includeGlobal` parameter (#3301).\n if (\n pushTenantPredicate(\n conditions,\n params,\n options.tenantScope,\n `$${paramIndex}`,\n includeGlobal,\n )\n ) {\n paramIndex++;\n }\n\n let sql = 'SELECT * FROM _smrt_dispatch';\n if (conditions.length > 0) {\n sql += ` WHERE ${conditions.join(' AND ')}`;\n }\n\n // Whitelist allowed orderBy values to prevent SQL injection\n const allowedOrderBy = [\n 'created_at DESC',\n 'created_at ASC',\n 'updated_at DESC',\n 'updated_at ASC',\n 'type',\n 'type DESC',\n 'type ASC',\n 'status',\n 'status DESC',\n 'status ASC',\n 'source',\n 'source DESC',\n 'source ASC',\n ];\n const safeOrderBy =\n options.orderBy && allowedOrderBy.includes(options.orderBy)\n ? options.orderBy\n : 'created_at DESC';\n sql += ` ORDER BY ${safeOrderBy}`;\n\n // Use parameterized queries for limit/offset\n if (options.limit !== undefined) {\n sql += ` LIMIT $${paramIndex++}`;\n params.push(options.limit);\n }\n\n if (options.offset !== undefined) {\n sql += ` OFFSET $${paramIndex++}`;\n params.push(options.offset);\n }\n\n const { rows } = await db.query(sql, ...params);\n return rows.map((row: Record<string, unknown>) =>\n Dispatch.fromRow(row as unknown as DispatchData),\n );\n }\n\n /**\n * Find pending dispatches for a set of signal types\n *\n * @param subscriber - When provided, filters to dispatches with no target\n * or targeted specifically at this subscriber (for fan-out delivery)\n * @param tenantScope - Active tenant scope (S5 #1398). Derived server-side by\n * the bus; restricts results so a subscriber in one tenant cannot see\n * another tenant's dispatch. See {@link pushTenantPredicate} for the exact\n * semantics (off → no filter; active tenant → that tenant + global; on but\n * no tenant → global only).\n */\n static async findPending(\n db: DatabaseInterface,\n signalTypes: string[],\n limit: number = 100,\n subscriber?: string,\n tenantScope?: DispatchTenantScope,\n ): Promise<Dispatch[]> {\n if (signalTypes.length === 0) {\n return [];\n }\n\n let paramIndex = 1;\n const placeholders = signalTypes.map(() => `$${paramIndex++}`).join(', ');\n const conditions: string[] = [\n \"status = 'pending'\",\n `type IN (${placeholders})`,\n ];\n const params: unknown[] = [...signalTypes];\n\n if (subscriber) {\n conditions.push(\n `(target_subscriber IS NULL OR target_subscriber = $${paramIndex++})`,\n );\n params.push(subscriber);\n }\n\n if (\n pushTenantPredicate(conditions, params, tenantScope, `$${paramIndex}`)\n ) {\n paramIndex++;\n }\n\n const sql = `SELECT * FROM _smrt_dispatch\n WHERE ${conditions.join(' AND ')}\n ORDER BY created_at ASC LIMIT $${paramIndex}`;\n params.push(limit);\n\n const { rows } = await db.query(sql, ...params);\n\n return rows.map((row: Record<string, unknown>) =>\n Dispatch.fromRow(row as unknown as DispatchData),\n );\n }\n\n /**\n * Atomically claim a pending dispatch for processing (S5 #1398).\n *\n * Performs a conditional `UPDATE ... WHERE id = ? AND status = 'pending'`\n * (plus the tenant predicate for the active scope) and reports whether the\n * row was claimed by THIS call. Only the caller whose update affected a row\n * may run the handler, which closes the claim TOCTOU window where a `process`\n * filter-then-update could let two workers (or a tenant-scoped and a\n * system-scoped processor) both pick up the same compete/global dispatch.\n *\n * On a successful claim the dispatch's status/attempts/updated_at are\n * advanced in the DB; the in-memory `dispatch` instance is updated to match\n * so the caller can proceed to `update()` it to completed/failed.\n *\n * @returns `true` if this call claimed the dispatch, `false` if another\n * worker already moved it out of `pending` (or it is not visible in scope).\n */\n static async claim(\n db: DatabaseInterface,\n dispatch: Dispatch,\n tenantScope?: DispatchTenantScope,\n ): Promise<boolean> {\n dispatch.markProcessing();\n const row = dispatch.toRow();\n\n const conditions: string[] = ['id = $4', \"status = 'pending'\"];\n const params: unknown[] = [\n row.status,\n row.attempts,\n row.updated_at,\n row.id,\n ];\n let paramIndex = 5;\n\n if (\n pushTenantPredicate(conditions, params, tenantScope, `$${paramIndex}`)\n ) {\n paramIndex++;\n }\n\n const { rowCount } = await db.query(\n `UPDATE _smrt_dispatch\n SET status = $1, attempts = $2, updated_at = $3\n WHERE ${conditions.join(' AND ')}`,\n ...params,\n );\n\n return (rowCount ?? 0) > 0;\n }\n\n /**\n * Find failed dispatches eligible for retry.\n *\n * @param tenantScope - Active tenant scope (S5 #1398). Derived server-side by\n * the bus; restricts retryable rows to the active tenant (plus global) so a\n * tenant's `retry()` cannot reset another tenant's failed dispatches. See\n * {@link pushTenantPredicate} for the exact semantics.\n */\n static async findRetryable(\n db: DatabaseInterface,\n options: DispatchRetryOptions = {},\n tenantScope?: DispatchTenantScope,\n ): Promise<Dispatch[]> {\n const conditions: string[] = [\"status = 'failed'\"];\n const params: unknown[] = [];\n let paramIndex = 1;\n\n if (options.maxAttempts) {\n conditions.push(`attempts < $${paramIndex++}`);\n params.push(options.maxAttempts);\n }\n\n if (options.olderThan) {\n conditions.push(`created_at < $${paramIndex++}`);\n params.push(options.olderThan.toISOString());\n }\n\n if (options.signalTypes && options.signalTypes.length > 0) {\n const placeholders = options.signalTypes\n .map(() => `$${paramIndex++}`)\n .join(', ');\n conditions.push(`type IN (${placeholders})`);\n params.push(...options.signalTypes);\n }\n\n if (\n pushTenantPredicate(conditions, params, tenantScope, `$${paramIndex}`)\n ) {\n paramIndex++;\n }\n\n const { rows } = await db.query(\n `SELECT * FROM _smrt_dispatch WHERE ${conditions.join(' AND ')} ORDER BY created_at ASC`,\n ...params,\n );\n\n return rows.map((row: Record<string, unknown>) =>\n Dispatch.fromRow(row as unknown as DispatchData),\n );\n }\n\n /**\n * Count dispatches by status\n */\n static async countByStatus(\n db: DatabaseInterface,\n status: DispatchStatus,\n ): Promise<number> {\n const result = await db.single`\n SELECT COUNT(*) as count FROM _smrt_dispatch WHERE status = ${status}\n `;\n return toSafeInteger(\n (result as { count?: unknown } | null)?.count ?? 0,\n 'Dispatch count',\n );\n }\n\n /**\n * Delete a dispatch\n */\n static async delete(db: DatabaseInterface, id: string): Promise<void> {\n await db.query(`DELETE FROM _smrt_dispatch WHERE id = $1`, id);\n }\n\n /**\n * Cleanup old dispatches.\n *\n * @param tenantScope - Active tenant scope (S5 #1398). Derived server-side by\n * the bus; restricts deletions to the active tenant (plus global) so a\n * tenant's `cleanup()` cannot delete another tenant's dispatches. See\n * {@link pushTenantPredicate} for the exact semantics.\n */\n static async cleanup(\n db: DatabaseInterface,\n options: DispatchCleanupOptions = {},\n tenantScope?: DispatchTenantScope,\n ): Promise<DispatchCleanupResult> {\n const result: DispatchCleanupResult = {\n completedDeleted: 0,\n failedDeleted: 0,\n };\n\n if (options.completedOlderThanDays) {\n const cutoff = new Date();\n cutoff.setDate(cutoff.getDate() - options.completedOlderThanDays);\n\n const conditions: string[] = [\n \"status = 'completed'\",\n 'processed_at < $1',\n ];\n const params: unknown[] = [cutoff.toISOString()];\n pushTenantPredicate(conditions, params, tenantScope, '$2');\n\n result.completedDeleted = await deleteOrCount(\n db,\n conditions,\n params,\n options.dryRun ?? false,\n );\n }\n\n if (options.failedOlderThanDays) {\n const cutoff = new Date();\n cutoff.setDate(cutoff.getDate() - options.failedOlderThanDays);\n\n const conditions: string[] = [\"status = 'failed'\", 'updated_at < $1'];\n const params: unknown[] = [cutoff.toISOString()];\n pushTenantPredicate(conditions, params, tenantScope, '$2');\n\n result.failedDeleted = await deleteOrCount(\n db,\n conditions,\n params,\n options.dryRun ?? false,\n );\n }\n\n return result;\n }\n\n /**\n * Check if dispatch table exists.\n *\n * Delegates to the shared, engine-aware {@link tableExists} rather than\n * probing with `SELECT ... LIMIT 1` inside a try/catch: on PostgreSQL, a\n * caught query error still aborts the surrounding transaction (any\n * statement error does, regardless of whether the application handles it),\n * so a probe-and-catch existence check silently poisons a transaction any\n * caller wraps this in — exactly what `DispatchBus.initialize()`'s\n * lock-guarded bootstrap transaction needs to do (#2861). The shared helper\n * uses `information_schema.tables` on PostgreSQL instead, which never\n * raises for a missing table.\n */\n static async tableExists(\n db: DatabaseInterface,\n typeHint?: string,\n ): Promise<boolean> {\n return tableExists(db, '_smrt_dispatch', typeHint);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CA,SAAS,oBACP,YACA,QACA,OACA,aACA,gBAAgB,MACP;CACT,IAAI,CAAC,OAAO,UACV,OAAO;CAET,IAAI,MAAM,aAAa,MAAM;EAC3B,WAAW,KACT,gBACI,gBAAgB,YAAY,0BAC5B,eAAe,aACrB;EACA,OAAO,KAAK,MAAM,QAAQ;EAC1B,OAAO;CACT;CACA,WAAW,KAAK,mBAAmB;CACnC,OAAO;AACT;;;;;;;;;;AAWA,eAAe,cACb,IACA,YACA,QACA,QACiB;CACjB,MAAM,QAAQ,WAAW,KAAK,OAAO;CAKrC,MAAM,QAAQ,eACZ,MALwB,GAAG,MAC3B,sDAAsD,SACtD,GAAG,MACL,EAAA,CAEc,OAAO,EAAE,EAAE,SAAS,GAChC,wBACF;CACA,IAAI,SAAS,GAAG,OAAO;CAEvB,IAAI,CAAC,QACH,MAAM,GAAG,MAAM,oCAAoC,SAAS,GAAG,MAAM;CAGvE,OAAO;AACT;;;;AAKA,IAAa,qBAAb,MAAgC;;;;CAI9B,aAAa,OACX,IACA,UACe;EACf,MAAM,MAAM,SAAS,MAAM;EAC3B,MAAM,GAAG,MACP;;;wFAIA,IAAI,IACJ,IAAI,MACJ,IAAI,QACJ,IAAI,WACJ,IAAI,SACJ,IAAI,QACJ,IAAI,UACJ,IAAI,YACJ,IAAI,cACJ,IAAI,cACJ,IAAI,mBACJ,IAAI,gBACJ,IAAI,WACJ,IAAI,UACJ,IAAI,YACJ,IAAI,UACN;CACF;;;;CAKA,aAAa,OACX,IACA,UACe;EACf,MAAM,MAAM,SAAS,MAAM;EAC3B,MAAM,GAAG,MAC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GAA8B,CACjD;CACF;;;;CAKA,aAAa,cACX,IACA,QACiB;EAIjB,OAAO,eACJ,MAJkB,GAAG,MAAM;oEACkC,OAAO;QAG7B,SAAS,GACjD,gBACF;CACF;;;;CAKA,aAAa,OAAO,IAAuB,IAA2B;EACpE,MAAM,GAAG,MAAM,4CAA4C,EAAE;CAC/D;;;;;;;;;CAUA,aAAa,QACX,IACA,UAAkC,CAAC,GACnC,aACgC;EAChC,MAAM,SAAgC;GACpC,kBAAkB;GAClB,eAAe;EACjB;EAEA,IAAI,QAAQ,wBAAwB;GAClC,MAAM,yBAAS,IAAI,KAAK;GACxB,OAAO,QAAQ,OAAO,QAAQ,IAAI,QAAQ,sBAAsB;GAEhE,MAAM,aAAuB,CAC3B,wBACA,mBACF;GACA,MAAM,SAAoB,CAAC,OAAO,YAAY,CAAC;GAC/C,oBAAoB,YAAY,QAAQ,aAAa,IAAI;GAEzD,OAAO,mBAAmB,MAAM,cAC9B,IACA,YACA,QACA,QAAQ,UAAU,KACpB;EACF;EAEA,IAAI,QAAQ,qBAAqB;GAC/B,MAAM,yBAAS,IAAI,KAAK;GACxB,OAAO,QAAQ,OAAO,QAAQ,IAAI,QAAQ,mBAAmB;GAE7D,MAAM,aAAuB,CAAC,qBAAqB,iBAAiB;GACpE,MAAM,SAAoB,CAAC,OAAO,YAAY,CAAC;GAC/C,oBAAoB,YAAY,QAAQ,aAAa,IAAI;GAEzD,OAAO,gBAAgB,MAAM,cAC3B,IACA,YACA,QACA,QAAQ,UAAU,KACpB;EACF;EAEA,OAAO;CACT;;;;;;;;;;;;;;CAeA,aAAa,YACX,IACA,UACkB;EAClB,OAAO,YAAY,IAAI,kBAAkB,QAAQ;CACnD;AACF"}
@@ -93,7 +93,11 @@ export interface DispatchListOptions {
93
93
  * active scope. Semantics:
94
94
  *
95
95
  * - omitted / `enforced: false` → no tenant filter (pre-tenancy behavior).
96
- * - `enforced: true`, `tenantId: T` → `(tenant_id = T OR tenant_id IS NULL)`.
96
+ * - `enforced: true`, `tenantId: T` → `(tenant_id = T OR tenant_id IS NULL)`
97
+ * inside the collection (processing paths claim global work), but the
98
+ * public `DispatchBus.list()` narrows this to `tenant_id = T` only: global
99
+ * (NULL-tenant) rows are system-only on inspection reads (#3301) because
100
+ * their payload may belong to another tenant.
97
101
  * - `enforced: true`, `tenantId: null` → `tenant_id IS NULL` only (fail-closed
98
102
  * global scope when tenancy is on but no tenant context is active).
99
103
  *
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/dispatch/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAEhE;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,SAAS,GAAG,YAAY,GAAG,WAAW,GAAG,QAAQ,CAAC;AAE/E;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,wBAAwB;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,+BAA+B;IAC/B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iEAAiE;IACjE,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,0BAA0B;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,kBAAkB;IAClB,EAAE,EAAE,MAAM,CAAC;IACX,+CAA+C;IAC/C,IAAI,EAAE,MAAM,CAAC;IACb,wBAAwB;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,+BAA+B;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,iEAAiE;IACjE,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,kCAAkC;IAClC,SAAS,EAAE,IAAI,CAAC;IAChB,oCAAoC;IACpC,QAAQ,EAAE,MAAM,CAAC;IACjB,w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AAC;KACb,CAAC;IACN,qDAAqD;IACrD,WAAW,CAAC,EAAE;QACZ,IAAI,EAAE,KAAK,GAAG,MAAM,GAAG,QAAQ,CAAC;QAChC,GAAG,EAAE,MAAM,CAAC;KACb,CAAC;CACH"}
@@ -3,7 +3,7 @@ var staticManifest = {
3
3
  "version": "1.0.0",
4
4
  "timestamp": 0,
5
5
  "packageName": "@happyvertical/smrt-core",
6
- "packageVersion": "0.55.11",
6
+ "packageVersion": "0.55.13",
7
7
  "objects": {
8
8
  "@happyvertical/smrt-core:SmrtClass": {
9
9
  "name": "smrtclass",