@wildo-ai/saas-minions-lib 1.1.13 → 1.1.15

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.
@@ -1,4 +1,4 @@
1
- import { LoggerService, RabbitMQAdapterBackendService, ScheduledMessageVerifierBackendService, ScheduledExecutionRunner, JobStatusTrackingBackendService, JobPayloadStorageBackendService, WorkerShutdownManagerBackendService, type MinionTickJobToken } from '@wildo-ai/saas-backend-lib';
1
+ import { LoggerService, ScheduledMessageVerifierBackendService, ScheduledExecutionRunner, JobStatusTrackingBackendService, JobPayloadStorageBackendService, WorkerShutdownManagerBackendService, type MinionTickJobToken, type QueueAdapter } from '@wildo-ai/saas-backend-lib';
2
2
  import { type ScheduledAttemptContext, type ScheduledEvidencePolicy, type ScheduledRecoveryPolicy, type ScheduledReconciliationResult } from '@wildo-ai/saas-models';
3
3
  /** Application payload after authentication; transport credentials are never replayed to recovery handlers. */
4
4
  export type MinionTickInvocation = Pick<MinionTickJobToken, 'jobId' | 'applicationId' | 'jobType' | 'minionName' | 'inputDataJson'> & {
@@ -35,6 +35,11 @@ export type MinionTickReconciler = (tick: MinionTickInvocation, attempt: Schedul
35
35
  * responsibility; these are separate guarantees.
36
36
  */
37
37
  export declare class MinionTickConsumerBackendService {
38
+ /**
39
+ * The queue PORT (#2303), not the RabbitMQ class: the token binds whichever adapter the runtime selected
40
+ * (`bindQueueAdapter`), so this consumer runs on any of them. It needs only a declaration, a subscription and the
41
+ * settlement of each delivery.
42
+ */
38
43
  private readonly adapter;
39
44
  private readonly scheduledMessageVerifier;
40
45
  private readonly logger;
@@ -44,7 +49,13 @@ export declare class MinionTickConsumerBackendService {
44
49
  private readonly runner;
45
50
  private reviewScope?;
46
51
  protected readonly logDebug: (message: string, context?: Record<string, unknown>) => void;
47
- constructor(adapter: RabbitMQAdapterBackendService, scheduledMessageVerifier: ScheduledMessageVerifierBackendService, logger: LoggerService, tracker: JobStatusTrackingBackendService, payloadStorage: JobPayloadStorageBackendService, shutdownManager: WorkerShutdownManagerBackendService);
52
+ constructor(
53
+ /**
54
+ * The queue PORT (#2303), not the RabbitMQ class: the token binds whichever adapter the runtime selected
55
+ * (`bindQueueAdapter`), so this consumer runs on any of them. It needs only a declaration, a subscription and the
56
+ * settlement of each delivery.
57
+ */
58
+ adapter: QueueAdapter, scheduledMessageVerifier: ScheduledMessageVerifierBackendService, logger: LoggerService, tracker: JobStatusTrackingBackendService, payloadStorage: JobPayloadStorageBackendService, shutdownManager: WorkerShutdownManagerBackendService);
48
59
  start(input: {
49
60
  /**
50
61
  * The PLATFORM's id for the application this minion serves — the id its tick queue is named
@@ -1 +1 @@
1
- {"version":3,"file":"minion-tick-consumer.backend.service.d.ts","sourceRoot":"","sources":["../../../../src/runtime/queue/minion-tick-consumer.backend.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,aAAa,EACb,6BAA6B,EAE7B,sCAAsC,EAEtC,wBAAwB,EACxB,+BAA+B,EAC/B,+BAA+B,EAC/B,mCAAmC,EAEnC,KAAK,kBAAkB,EACxB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAEc,KAAK,uBAAuB,EAAE,KAAK,uBAAuB,EAAE,KAAK,uBAAuB,EAAE,KAAK,6BAA6B,EAChJ,MAAM,uBAAuB,CAAC;AAE/B,+GAA+G;AAC/G,MAAM,MAAM,oBAAoB,GAAG,IAAI,CAAC,kBAAkB,EAAE,OAAO,GAAG,eAAe,GAAG,SAAS,GAAG,YAAY,GAAG,eAAe,CAAC,GAAG;IACpI,mFAAmF;IACnF,SAAS,EAAE,IAAI,CAAC;IAChB,qFAAqF;IACrF,WAAW,CAAC,EAAE,IAAI,CAAC;CACpB,CAAC;AAEF,qGAAqG;AACrG,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,oBAAoB,EAAE,OAAO,EAAE,uBAAuB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAChH,MAAM,MAAM,oBAAoB,GAAG,CAAC,IAA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AAC,CAAC,CAAC,EAAE,WAAW,CAAC,GACjF,OAAO,CAAC,IAAI,CAAC;IAMT,SAAS,IAAI,OAAO;IAI3B;;;;;;;OAOG;IACI,WAAW,IAAI,OAAO;YAIf,aAAa;CAuD5B"}
1
+ {"version":3,"file":"minion-tick-consumer.backend.service.d.ts","sourceRoot":"","sources":["../../../../src/runtime/queue/minion-tick-consumer.backend.service.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,aAAa,EAEb,sCAAsC,EAEtC,wBAAwB,EACxB,+BAA+B,EAC/B,+BAA+B,EAC/B,mCAAmC,EAEnC,KAAK,kBAAkB,EACvB,KAAK,YAAY,EAIlB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAEc,KAAK,uBAAuB,EAAE,KAAK,uBAAuB,EAAE,KAAK,uBAAuB,EAAE,KAAK,6BAA6B,EAChJ,MAAM,uBAAuB,CAAC;AAE/B,+GAA+G;AAC/G,MAAM,MAAM,oBAAoB,GAAG,IAAI,CAAC,kBAAkB,EAAE,OAAO,GAAG,eAAe,GAAG,SAAS,GAAG,YAAY,GAAG,eAAe,CAAC,GAAG;IACpI,mFAAmF;IACnF,SAAS,EAAE,IAAI,CAAC;IAChB,qFAAqF;IACrF,WAAW,CAAC,EAAE,IAAI,CAAC;CACpB,CAAC;AAEF,qGAAqG;AACrG,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,oBAAoB,EAAE,OAAO,EAAE,uBAAuB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAChH,MAAM,MAAM,oBAAoB,GAAG,CAAC,IAAI,EAAE,oBAAoB,EAAE,OAAO,EAAE,uBAAuB,KAAK,OAAO,CAAC,6BAA6B,CAAC,CAAC;AAE5I;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBACa,gCAAgC;IASzC;;;;OAIG;IAEH,OAAO,CAAC,QAAQ,CAAC,OAAO;IAExB,OAAO,CAAC,QAAQ,CAAC,wBAAwB;IAEzC,OAAO,CAAC,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target, key, paramIndex); }
12
12
  };
13
13
  import { inject, injectable } from 'inversify';
14
- import { describeCaughtError, LoggerService, RabbitMQAdapterBackendService, SAAS_SERVICE_TYPES, ScheduledMessageVerifierBackendService, ScheduledDeliveryVerdict, ScheduledExecutionRunner, JobStatusTrackingBackendService, JobPayloadStorageBackendService, WorkerShutdownManagerBackendService, } from '@wildo-ai/saas-backend-lib';
15
- import { QueueNamingUtils, JobPayloadSchema, JobType, JobInitiator, JobStatus, } from '@wildo-ai/saas-models';
14
+ import { describeCaughtError, LoggerService, SAAS_SERVICE_TYPES, ScheduledMessageVerifierBackendService, ScheduledDeliveryVerdict, ScheduledExecutionRunner, JobStatusTrackingBackendService, JobPayloadStorageBackendService, WorkerShutdownManagerBackendService, assertMinionTickQueue, } from '@wildo-ai/saas-backend-lib';
15
+ import { JobPayloadSchema, JobType, JobInitiator, JobStatus, } from '@wildo-ai/saas-models';
16
16
  /**
17
17
  * Minion-side consumer for the per-minion tick queue
18
18
  * (`wildo.queue.<appId>.minion-tick.<minionName>`, see `QueueNamingUtils.buildMinionTickQueueName`).
@@ -86,38 +86,28 @@ let MinionTickConsumerBackendService = class MinionTickConsumerBackendService {
86
86
  reconcile: reconcile ? (attempt) => reconcile(tick, attempt) : undefined,
87
87
  };
88
88
  };
89
- const queueName = QueueNamingUtils.buildMinionTickQueueName(applicationId, minionName);
90
89
  // M5 — adapter readiness guard. The minion entrypoint calls this
91
90
  // method AFTER `StartApplicationBackendService.initialize`, which
92
- // typically connects the RabbitMQ adapter. Throw a descriptive
93
- // error if the adapter is not connected so a misconfigured
94
- // bootstrap surfaces here rather than at the first cron tick.
91
+ // connects the queue adapter. Throw a descriptive error if it is not
92
+ // connected so a misconfigured bootstrap surfaces here rather than at
93
+ // the first cron tick.
95
94
  if (!this.adapter.isConnected()) {
96
- throw new Error('MinionTickConsumerBackendService.start called before the RabbitMQ adapter connected. ' +
95
+ throw new Error('MinionTickConsumerBackendService.start called before the queue adapter connected. ' +
97
96
  'Ensure the minion bootstrap completes before subscribing to the tick queue.');
98
97
  }
99
- // H2 — the minion runtime OWNS and declares this per-minion tick queue.
100
- // The crontabs-batches-manager publisher is a producer only and passively
101
- // verifies the queue (`checkQueue`) rather than declaring it, so there is no
102
- // publisher-side declaration whose args this must match — the minion is the
103
- // sole topology owner here. `durable: true` + `x-queue-type: 'quorum'` is
104
- // the minion's own chosen shape (durable so ticks survive a broker restart).
98
+ // H2 — the minion runtime OWNS this per-minion tick queue's shape: durable and REPLICATED, so ticks survive a
99
+ // broker restart and the loss of one node. The crontabs-batches-manager publisher only verifies it (`checkQueue`).
100
+ // The backend's scheduled-delivery intake (#2303) declares it too, when a tick delivered over HTTP arrives before
101
+ // this minion has booted, and both go through `assertMinionTickQueue`, so the two declarations cannot differ.
105
102
  //
106
- // Declared on the adapter's dedicated topology channel (not the primary
107
- // publish/consume channel): a `406` arg-mismatch against a pre-existing
108
- // incompatible queue closes only that channel, so it can't tear down the
109
- // primary channel and crash-loop the minion bootstrap. The
110
- // `x-queue-type: 'quorum'` argument is not modelled by `QueueAssertOptions`,
111
- // so we declare directly on the channel rather than via `adapter.assertQueue`.
112
- const channel = await this.adapter.getTopologyChannel();
113
- await channel.assertQueue(queueName, {
114
- durable: true,
115
- arguments: {
116
- 'x-queue-type': 'quorum',
117
- },
118
- });
119
- await this.adapter.consume(queueName, async (msg, channel) => {
120
- await this.handleMessage(msg, channel, applicationId, minionName, queueName, resolver);
103
+ // Through the port (#2303): `replicated` is what RabbitMQ declares as a quorum queue
104
+ // (`x-queue-type: quorum`, the same arguments this consumer declared itself before),
105
+ // on the adapter's dedicated topology channel, so a `406` arg-mismatch against a
106
+ // pre-existing incompatible queue closes only that channel and is reported by the
107
+ // adapter's own topology refusal rather than tearing down the primary channel.
108
+ const queueName = await assertMinionTickQueue(this.adapter, applicationId, minionName);
109
+ await this.adapter.consume(queueName, async (msg, acknowledger) => {
110
+ await this.handleMessage(msg, acknowledger, applicationId, minionName, queueName, resolver);
121
111
  });
122
112
  this.consumingQueueName = queueName;
123
113
  this.isConsuming = true;
@@ -164,7 +154,7 @@ let MinionTickConsumerBackendService = class MinionTickConsumerBackendService {
164
154
  isReceiving() {
165
155
  return this.isConsuming && this.adapter.isConnected();
166
156
  }
167
- async handleMessage(msg, channel, applicationId, expectedMinionName, queueName, resolver) {
157
+ async handleMessage(msg, acknowledger, applicationId, expectedMinionName, queueName, resolver) {
168
158
  this.shutdownManager.jobStarted();
169
159
  let admitted;
170
160
  try {
@@ -205,9 +195,9 @@ let MinionTickConsumerBackendService = class MinionTickConsumerBackendService {
205
195
  // NACK after an ACK throws or run the handler again from this catch boundary.
206
196
  try {
207
197
  if (accepted)
208
- channel.ack(msg);
198
+ acknowledger.ack(msg);
209
199
  else
210
- channel.nack(msg, false, false);
200
+ acknowledger.nack(msg, false, false);
211
201
  }
212
202
  catch (error) {
213
203
  this.logger.error('Minion tick settlement failed', { expectedMinionName, error: describeCaughtError(error) });
@@ -223,14 +213,13 @@ let MinionTickConsumerBackendService = class MinionTickConsumerBackendService {
223
213
  };
224
214
  MinionTickConsumerBackendService = __decorate([
225
215
  injectable(),
226
- __param(0, inject(SAAS_SERVICE_TYPES.RabbitMQAdapterService)),
216
+ __param(0, inject(SAAS_SERVICE_TYPES.QueueAdapterService)),
227
217
  __param(1, inject(SAAS_SERVICE_TYPES.ScheduledMessageVerifierService)),
228
218
  __param(2, inject(SAAS_SERVICE_TYPES.Logger)),
229
219
  __param(3, inject(SAAS_SERVICE_TYPES.JobStatusTrackingService)),
230
220
  __param(4, inject(SAAS_SERVICE_TYPES.JobPayloadStorageService)),
231
221
  __param(5, inject(SAAS_SERVICE_TYPES.WorkerShutdownManagerService)),
232
- __metadata("design:paramtypes", [RabbitMQAdapterBackendService,
233
- ScheduledMessageVerifierBackendService,
222
+ __metadata("design:paramtypes", [Object, ScheduledMessageVerifierBackendService,
234
223
  LoggerService,
235
224
  JobStatusTrackingBackendService,
236
225
  JobPayloadStorageBackendService,
@@ -1 +1 @@
1
- {"version":3,"file":"minion-tick-consumer.backend.service.js","sourceRoot":"","sources":["../../../../src/runtime/queue/minion-tick-consumer.backend.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAE/C,OAAO,EACL,mBAAmB,EACnB,aAAa,EACb,6BAA6B,EAC7B,kBAAkB,EAClB,sCAAsC,EACtC,wBAAwB,EACxB,wBAAwB,EACxB,+BAA+B,EAC/B,+BAA+B,EAC/B,mCAAmC,GAGpC,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,gBAAgB,EAAE,gBAAgB,EAAE,OAAO,EAAE,YAAY,EAAE,SAAS,GAErE,MAAM,uBAAuB,CAAC;AAc/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEI,IAAM,gCAAgC,GAAtC,MAAM,gCAAgC;IAUxB;IAEA;IAEA;IAMA;IAnBX,WAAW,GAAG,KAAK,CAAC;IACpB,kBAAkB,CAAU;IACnB,MAAM,CAA2B;IAC1C,WAAW,CAA+D;IAE/D,QAAQ,CAA+D;IAE1F,YAEmB,OAAsC,EAEtC,wBAAgE,EAEhE,MAAqB,EAEtC,OAAwC,EAExC,cAA+C,EAE9B,eAAoD;QAVpD,YAAO,GAAP,OAAO,CAA+B;QAEtC,6BAAwB,GAAxB,wBAAwB,CAAwC;QAEhE,WAAM,GAAN,MAAM,CAAe;QAMrB,oBAAe,GAAf,eAAe,CAAqC;QAErE,IAAI,CAAC,MAAM,GAAG,IAAI,wBAAwB,CAAC,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE;YAC1E,OAAO,EAAE,GAAG,EAAE,CAAC,eAAe,CAAC,UAAU,EAAE;YAC3C,SAAS,EAAE,GAAG,EAAE,CAAC,eAAe,CAAC,YAAY,EAAE;SAChD,CAAC,CAAC;QACH,iGAAiG;QACjG,eAAe,CAAC,mBAAmB,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,C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C,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IACvD,CAAC;CACF,CAAA;AAvNY,gCAAgC;IAD5C,UAAU,EAAE;IAUR,WAAA,MAAM,CAAC,kBAAkB,CAAC,sBAAsB,CAAC,CAAA;IAEjD,WAAA,MAAM,CAAC,kBAAkB,CAAC,+BAA+B,CAAC,CAAA;IAE1D,WAAA,MAAM,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAA;IAEjC,WAAA,MAAM,CAAC,kBAAkB,CAAC,wBAAwB,CAAC,CAAA;IAEnD,WAAA,MAAM,CAAC,kBAAkB,CAAC,wBAAwB,CAAC,CAAA;IAEnD,WAAA,MAAM,CAAC,kBAAkB,CAAC,4BAA4B,CAAC,CAAA;qCAT9B,6BAA6B;QAEZ,sCAAsC;QAExD,aAAa;QAE7B,+BAA+B;QAExB,+BAA+B;QAEb,mCAAmC;GApB5D,gCAAgC,CAuN5C","sourcesContent":["import { inject, injectable } from 'inversify';\nimport type { Channel, ConsumeMessage } from 'amqplib';\nimport {\n describeCaughtError,\n LoggerService,\n RabbitMQAdapterBackendService,\n SAAS_SERVICE_TYPES,\n ScheduledMessageVerifierBackendService,\n ScheduledDeliveryVerdict,\n ScheduledExecutionRunner,\n JobStatusTrackingBackendService,\n JobPayloadStorageBackendService,\n WorkerShutdownManagerBackendService,\n type ScheduledExecutionResolver,\n type MinionTickJobToken,\n} from '@wildo-ai/saas-backend-lib';\nimport {\n QueueNamingUtils, JobPayloadSchema, JobType, JobInitiator, JobStatus,\n type JobExecution, type ScheduledAttemptContext, type ScheduledEvidencePolicy, type ScheduledRecoveryPolicy, type ScheduledReconciliationResult,\n} from '@wildo-ai/saas-models';\n\n/** Application payload after authentication; transport credentials are never replayed to recovery handlers. */\nexport type MinionTickInvocation = Pick<MinionTickJobToken, 'jobId' | 'applicationId' | 'jobType' | 'minionName' | 'inputDataJson'> & {\n /** Original authenticated publication time, preserved across recovery attempts. */\n createdAt: Date;\n /** Authenticated cron occurrence time; absent for independent manual invocations. */\n scheduledAt?: Date;\n};\n\n/** Stable occurrence plus a fresh attempt identity; retries are owned by the durable coordinator. */\nexport type MinionTickHandler = (tick: MinionTickInvocation, attempt: ScheduledAttemptContext) => Promise<void>;\nexport type MinionTickReconciler = (tick: MinionTickInvocation, attempt: ScheduledAttemptContext) => Promise<ScheduledReconciliationResult>;\n\n/**\n * Minion-side consumer for the per-minion tick queue\n * (`wildo.queue.<appId>.minion-tick.<minionName>`, see `QueueNamingUtils.buildMinionTickQueueName`).\n *\n * Contract:\n * - Subscribes only to the queue dedicated to THIS minion (no shared\n * `scheduled` queue subscription, so the backend-api and minion don't\n * compete on cron tick deliveries).\n * - Verifies every inbound JWT through\n * `ScheduledMessageVerifierBackendService.verifyAndExtractMinionTick(token, expectedMinionName)`\n * — invalid signature, malformed payload, wrong issuer/audience, or\n * wrong target minion all NACK without requeue. An AUTHENTIC tick that\n * arrived after its admission window (usually because the minion was down;\n * see `ScheduledDeliveryLateness`) is recorded as\n * a missed occurrence and acknowledged, never run (#2212); only a late\n * tick that could not be recorded is NACKed.\n * - Dispatches `MINION_TICK` tokens that match this minion's identity to\n * the registered handler. The handler typically routes into the\n * minion's own locally-hosted `flowsActorsRegistry`.\n *\n * Duplicate deliveries and recovery of one occurrence belong to the durable\n * coordinator. Overlap between DISTINCT occurrences remains the handler's\n * responsibility; these are separate guarantees.\n */\n@injectable()\nexport class MinionTickConsumerBackendService {\n private isConsuming = false;\n private consumingQueueName?: string;\n private readonly runner: ScheduledExecutionRunner;\n private reviewScope?: { queueName: string; resolver: ScheduledExecutionResolver };\n\n protected readonly logDebug: (message: string, context?: Record<string, unknown>) => void;\n\n constructor(\n @inject(SAAS_SERVICE_TYPES.RabbitMQAdapterService)\n private readonly adapter: RabbitMQAdapterBackendService,\n @inject(SAAS_SERVICE_TYPES.ScheduledMessageVerifierService)\n private readonly scheduledMessageVerifier: ScheduledMessageVerifierBackendService,\n @inject(SAAS_SERVICE_TYPES.Logger)\n private readonly logger: LoggerService,\n @inject(SAAS_SERVICE_TYPES.JobStatusTrackingService)\n tracker: JobStatusTrackingBackendService,\n @inject(SAAS_SERVICE_TYPES.JobPayloadStorageService)\n payloadStorage: JobPayloadStorageBackendService,\n @inject(SAAS_SERVICE_TYPES.WorkerShutdownManagerService)\n private readonly shutdownManager: WorkerShutdownManagerBackendService,\n ) {\n this.runner = new ScheduledExecutionRunner(tracker, logger, payloadStorage, {\n started: () => shutdownManager.jobStarted(),\n completed: () => shutdownManager.jobCompleted(),\n });\n // Stop broker receipts and recovery admissions before the shared signal coordinator drains work.\n shutdownManager.registerStopHandler(() => this.stop());\n this.logDebug = this.logger.debugByType('MinionTickConsumerBackendService');\n this.logger.setDebugTypeLogging('MinionTickConsumerBackendService', false);\n }\n\n public async start(input: {\n /**\n * The PLATFORM's id for the application this minion serves — the id its tick queue is named\n * from and the id the crontabs-manager signs into every tick. It is addressing, never a row\n * identity: the tick rows and stored payloads are anchored by the tracker and the payload\n * storage to the application RECORD id, which they resolve themselves.\n */\n applicationId: string;\n minionName: string;\n handler?: MinionTickHandler;\n recoveryPolicy?: Partial<ScheduledRecoveryPolicy>;\n evidencePolicy?: ScheduledEvidencePolicy;\n reconcile?: MinionTickReconciler;\n }): Promise<void> {\n if (this.isConsuming) {\n throw new Error('MinionTickConsumerBackendService.start called while already consuming.');\n }\n\n const { applicationId, minionName, handler, recoveryPolicy, evidencePolicy, reconcile } = input;\n // The scanner resolves only this runtime's locally authored handler. Durable input\n // is trusted after first receipt; it is never turned back into a signed JWT.\n const resolver: ScheduledExecutionResolver = async (job) => {\n if (job.applicationId !== applicationId || job.jobType !== JobType.MINION_TICK || job.minionName !== minionName || !handler) return undefined;\n const tick: MinionTickInvocation = {\n jobId: job.jobId, applicationId, jobType: JobType.MINION_TICK, minionName,\n inputDataJson: job.inputDataJson, createdAt: job.createdAt, scheduledAt: job.scheduledAt,\n };\n return {\n policy: recoveryPolicy,\n evidencePolicy,\n execute: async (attempt) => {\n const startedAt = Date.now();\n await handler(tick, attempt);\n return { jobId: job.jobId, status: JobStatus.COMPLETED, startedAt: new Date(startedAt), durationMs: Date.now() - startedAt, completedAt: new Date() };\n },\n reconcile: reconcile ? (attempt) => reconcile(tick, attempt) : undefined,\n };\n };\n const queueName = QueueNamingUtils.buildMinionTickQueueName(applicationId, minionName);\n\n // M5 — adapter readiness guard. The minion entrypoint calls this\n // method AFTER `StartApplicationBackendService.initialize`, which\n // typically connects the RabbitMQ adapter. Throw a descriptive\n // error if the adapter is not connected so a misconfigured\n // bootstrap surfaces here rather than at the first cron tick.\n if (!this.adapter.isConnected()) {\n throw new Error(\n 'MinionTickConsumerBackendService.start called before the RabbitMQ adapter connected. ' +\n 'Ensure the minion bootstrap completes before subscribing to the tick queue.',\n );\n }\n\n // H2 — the minion runtime OWNS and declares this per-minion tick queue.\n // The crontabs-batches-manager publisher is a producer only and passively\n // verifies the queue (`checkQueue`) rather than declaring it, so there is no\n // publisher-side declaration whose args this must match — the minion is the\n // sole topology owner here. `durable: true` + `x-queue-type: 'quorum'` is\n // the minion's own chosen shape (durable so ticks survive a broker restart).\n //\n // Declared on the adapter's dedicated topology channel (not the primary\n // publish/consume channel): a `406` arg-mismatch against a pre-existing\n // incompatible queue closes only that channel, so it can't tear down the\n // primary channel and crash-loop the minion bootstrap. The\n // `x-queue-type: 'quorum'` argument is not modelled by `QueueAssertOptions`,\n // so we declare directly on the channel rather than via `adapter.assertQueue`.\n const channel = await this.adapter.getTopologyChannel();\n await channel.assertQueue(queueName, {\n durable: true,\n arguments: {\n 'x-queue-type': 'quorum',\n },\n });\n\n await this.adapter.consume(queueName, async (msg: ConsumeMessage, channel: Channel) => {\n await this.handleMessage(msg, channel, applicationId, minionName, queueName, resolver);\n });\n\n this.consumingQueueName = queueName;\n this.isConsuming = true;\n this.runner.start(queueName, resolver);\n this.reviewScope = { queueName, resolver };\n this.logger.info('Minion tick consumer started', {\n queueName,\n applicationId,\n minionName,\n });\n }\n\n public async stop(): Promise<void> {\n this.runner.stop();\n this.reviewScope = undefined;\n if (!this.isConsuming || !this.consumingQueueName) {\n return;\n }\n await this.adapter.cancelConsumer(this.consumingQueueName);\n this.isConsuming = false;\n this.consumingQueueName = undefined;\n }\n\n /**\n * Invoke from an application-owned operator operation AFTER authorization.\n * Runtime app/queue ownership is derived here, never supplied by the caller;\n * actor, reason and expected revision are persisted atomically with the decision.\n */\n public async resolveTickReview(\n input: Omit<Parameters<ScheduledExecutionRunner['resolveReview']>[0], 'queueName'>,\n ): Promise<void> {\n const scope = this.reviewScope;\n if (!scope) throw new Error('Minion tick review requires an initialized consumer');\n await this.runner.resolveReview({ ...input, queueName: scope.queueName }, scope.resolver);\n }\n\n public isRunning(): boolean {\n return this.isConsuming;\n }\n\n /**\n * Whether this consumer can still RECEIVE a tick: subscribed, and the broker connection is up.\n *\n * `isRunning()` alone is not enough. The RabbitMQ adapter does not reconnect, so after a broker restart the\n * consumer stays \"running\" over a closed connection and no tick will ever arrive again. This is what the\n * minion liveness heartbeat asks before each beat (#1859), so that state ends in a restart instead of a\n * minion that is alive and deaf.\n */\n public isReceiving(): boolean {\n return this.isConsuming && this.adapter.isConnected();\n }\n\n private async handleMessage(\n msg: ConsumeMessage,\n channel: Channel,\n applicationId: string,\n expectedMinionName: string,\n queueName: string,\n resolver: ScheduledExecutionResolver,\n ): Promise<void> {\n this.shutdownManager.jobStarted();\n let admitted: JobExecution | undefined;\n try {\n let accepted = false;\n try {\n const delivery = await this.scheduledMessageVerifier.verifyAndExtractMinionTick(msg.content.toString(), expectedMinionName);\n const tick = delivery?.value;\n // Signature/target checks do not establish receiving-application ownership.\n // This comparison must precede every durable insert and handler resolution — a LATE tick's\n // included: a genuine token addressed elsewhere is foreign whether or not it is on time.\n if (!delivery || !tick || tick.applicationId !== applicationId || tick.minionName !== expectedMinionName) {\n throw new Error('Scheduled minion token does not match the receiving application and minion');\n }\n const job = JobPayloadSchema.parse({\n jobId: tick.jobId, jobType: JobType.MINION_TICK, applicationId: tick.applicationId,\n minionName: tick.minionName, inputDataJson: tick.inputDataJson,\n initiatedBy: JobInitiator.CRONTAB, createdAt: new Date(tick.iat * 1000), maxRetries: 0,\n scheduledAt: tick.scheduledAt === undefined ? undefined : new Date(tick.scheduledAt),\n });\n if (delivery.verdict === ScheduledDeliveryVerdict.LATE) {\n // Genuine, past its admission deadline: the tick was missed — usually because this minion was\n // not consuming, though the scheduler can share the delay (see `ScheduledDeliveryLateness`).\n // Recorded and acknowledged, reported once per drain by the runner (#2212);\n // dead-lettered only if it could not be recorded.\n accepted = await this.runner.recordLateDelivery(job, queueName, resolver, delivery.lateness);\n } else {\n // Durable receipt, then acknowledge, THEN run — a long tick must not hold later deliveries\n // unreceived past their five-minute token (`ScheduledExecutionRunner.admit`).\n admitted = await this.runner.admit(job, queueName, resolver);\n accepted = true;\n }\n } catch (error) {\n this.logger.error('Minion tick admission failed', { expectedMinionName, error: describeCaughtError(error) });\n }\n\n // Settlement failure is a broker problem, not a new business failure. Never\n // NACK after an ACK throws or run the handler again from this catch boundary.\n try {\n if (accepted) channel.ack(msg);\n else channel.nack(msg, false, false);\n } catch (error) {\n this.logger.error('Minion tick settlement failed', { expectedMinionName, error: describeCaughtError(error) });\n }\n } finally { this.shutdownManager.jobCompleted(); }\n // Started only after the delivery is settled; the runner tracks the run's own activity.\n if (admitted) this.runner.launch(admitted, resolver);\n }\n}\n"]}
1
+ {"version":3,"file":"minion-tick-consumer.backend.service.js","sourceRoot":"","sources":["../../../../src/runtime/queue/minion-tick-consumer.backend.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EACL,mBAAmB,EACnB,aAAa,EACb,kBAAkB,EAClB,sCAAsC,EACtC,wBAAwB,EACxB,wBAAwB,EACxB,+BAA+B,EAC/B,+BAA+B,EAC/B,mCAAmC,EAMnC,qBAAqB,GACtB,MAAM,4BAA4B,CAAC;AACpC,OAAO,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AAS_SERVICE_TYPES,\n ScheduledMessageVerifierBackendService,\n ScheduledDeliveryVerdict,\n ScheduledExecutionRunner,\n JobStatusTrackingBackendService,\n JobPayloadStorageBackendService,\n WorkerShutdownManagerBackendService,\n type ScheduledExecutionResolver,\n type MinionTickJobToken,\n type QueueAdapter,\n type QueueConsumeMessage,\n type QueueMessageAcknowledger,\n assertMinionTickQueue,\n} from '@wildo-ai/saas-backend-lib';\nimport {\n JobPayloadSchema, JobType, JobInitiator, JobStatus,\n type JobExecution, type ScheduledAttemptContext, type ScheduledEvidencePolicy, type ScheduledRecoveryPolicy, type ScheduledReconciliationResult,\n} from '@wildo-ai/saas-models';\n\n/** Application payload after authentication; transport credentials are never replayed to recovery handlers. */\nexport type MinionTickInvocation = Pick<MinionTickJobToken, 'jobId' | 'applicationId' | 'jobType' | 'minionName' | 'inputDataJson'> & {\n /** Original authenticated publication time, preserved across recovery attempts. */\n createdAt: Date;\n /** Authenticated cron occurrence time; absent for independent manual invocations. */\n scheduledAt?: Date;\n};\n\n/** Stable occurrence plus a fresh attempt identity; retries are owned by the durable coordinator. */\nexport type MinionTickHandler = (tick: MinionTickInvocation, attempt: ScheduledAttemptContext) => Promise<void>;\nexport type MinionTickReconciler = (tick: MinionTickInvocation, attempt: ScheduledAttemptContext) => Promise<ScheduledReconciliationResult>;\n\n/**\n * Minion-side consumer for the per-minion tick queue\n * (`wildo.queue.<appId>.minion-tick.<minionName>`, see `QueueNamingUtils.buildMinionTickQueueName`).\n *\n * Contract:\n * - Subscribes only to the queue dedicated to THIS minion (no shared\n * `scheduled` queue subscription, so the backend-api and minion don't\n * compete on cron tick deliveries).\n * - Verifies every inbound JWT through\n * `ScheduledMessageVerifierBackendService.verifyAndExtractMinionTick(token, expectedMinionName)`\n * — invalid signature, malformed payload, wrong issuer/audience, or\n * wrong target minion all NACK without requeue. An AUTHENTIC tick that\n * arrived after its admission window (usually because the minion was down;\n * see `ScheduledDeliveryLateness`) is recorded as\n * a missed occurrence and acknowledged, never run (#2212); only a late\n * tick that could not be recorded is NACKed.\n * - Dispatches `MINION_TICK` tokens that match this minion's identity to\n * the registered handler. The handler typically routes into the\n * minion's own locally-hosted `flowsActorsRegistry`.\n *\n * Duplicate deliveries and recovery of one occurrence belong to the durable\n * coordinator. Overlap between DISTINCT occurrences remains the handler's\n * responsibility; these are separate guarantees.\n */\n@injectable()\nexport class MinionTickConsumerBackendService {\n private isConsuming = false;\n private consumingQueueName?: string;\n private readonly runner: ScheduledExecutionRunner;\n private reviewScope?: { queueName: string; resolver: ScheduledExecutionResolver };\n\n protected readonly logDebug: (message: string, context?: Record<string, unknown>) => void;\n\n constructor(\n /**\n * The queue PORT (#2303), not the RabbitMQ class: the token binds whichever adapter the runtime selected\n * (`bindQueueAdapter`), so this consumer runs on any of them. It needs only a declaration, a subscription and the\n * settlement of each delivery.\n */\n @inject(SAAS_SERVICE_TYPES.QueueAdapterService)\n private readonly adapter: QueueAdapter,\n @inject(SAAS_SERVICE_TYPES.ScheduledMessageVerifierService)\n private readonly scheduledMessageVerifier: ScheduledMessageVerifierBackendService,\n @inject(SAAS_SERVICE_TYPES.Logger)\n private readonly logger: LoggerService,\n @inject(SAAS_SERVICE_TYPES.JobStatusTrackingService)\n tracker: JobStatusTrackingBackendService,\n @inject(SAAS_SERVICE_TYPES.JobPayloadStorageService)\n payloadStorage: JobPayloadStorageBackendService,\n @inject(SAAS_SERVICE_TYPES.WorkerShutdownManagerService)\n private readonly shutdownManager: WorkerShutdownManagerBackendService,\n ) {\n this.runner = new ScheduledExecutionRunner(tracker, logger, payloadStorage, {\n started: () => shutdownManager.jobStarted(),\n completed: () => shutdownManager.jobCompleted(),\n });\n // Stop broker receipts and recovery admissions before the shared signal coordinator drains work.\n shutdownManager.registerStopHandler(() => this.stop());\n this.logDebug = this.logger.debugByType('MinionTickConsumerBackendService');\n this.logger.setDebugTypeLogging('MinionTickConsumerBackendService', false);\n }\n\n public async start(input: {\n /**\n * The PLATFORM's id for the application this minion serves — the id its tick queue is named\n * from and the id the crontabs-manager signs into every tick. It is addressing, never a row\n * identity: the tick rows and stored payloads are anchored by the tracker and the payload\n * storage to the application RECORD id, which they resolve themselves.\n */\n applicationId: string;\n minionName: string;\n handler?: MinionTickHandler;\n recoveryPolicy?: Partial<ScheduledRecoveryPolicy>;\n evidencePolicy?: ScheduledEvidencePolicy;\n reconcile?: MinionTickReconciler;\n }): Promise<void> {\n if (this.isConsuming) {\n throw new Error('MinionTickConsumerBackendService.start called while already consuming.');\n }\n\n const { applicationId, minionName, handler, recoveryPolicy, evidencePolicy, reconcile } = input;\n // The scanner resolves only this runtime's locally authored handler. Durable input\n // is trusted after first receipt; it is never turned back into a signed JWT.\n const resolver: ScheduledExecutionResolver = async (job) => {\n if (job.applicationId !== applicationId || job.jobType !== JobType.MINION_TICK || job.minionName !== minionName || !handler) return undefined;\n const tick: MinionTickInvocation = {\n jobId: job.jobId, applicationId, jobType: JobType.MINION_TICK, minionName,\n inputDataJson: job.inputDataJson, createdAt: job.createdAt, scheduledAt: job.scheduledAt,\n };\n return {\n policy: recoveryPolicy,\n evidencePolicy,\n execute: async (attempt) => {\n const startedAt = Date.now();\n await handler(tick, attempt);\n return { jobId: job.jobId, status: JobStatus.COMPLETED, startedAt: new Date(startedAt), durationMs: Date.now() - startedAt, completedAt: new Date() };\n },\n reconcile: reconcile ? (attempt) => reconcile(tick, attempt) : undefined,\n };\n };\n // M5 — adapter readiness guard. The minion entrypoint calls this\n // method AFTER `StartApplicationBackendService.initialize`, which\n // connects the queue adapter. Throw a descriptive error if it is not\n // connected so a misconfigured bootstrap surfaces here rather than at\n // the first cron tick.\n if (!this.adapter.isConnected()) {\n throw new Error(\n 'MinionTickConsumerBackendService.start called before the queue adapter connected. ' +\n 'Ensure the minion bootstrap completes before subscribing to the tick queue.',\n );\n }\n\n // H2 — the minion runtime OWNS this per-minion tick queue's shape: durable and REPLICATED, so ticks survive a\n // broker restart and the loss of one node. The crontabs-batches-manager publisher only verifies it (`checkQueue`).\n // The backend's scheduled-delivery intake (#2303) declares it too, when a tick delivered over HTTP arrives before\n // this minion has booted, and both go through `assertMinionTickQueue`, so the two declarations cannot differ.\n //\n // Through the port (#2303): `replicated` is what RabbitMQ declares as a quorum queue\n // (`x-queue-type: quorum`, the same arguments this consumer declared itself before),\n // on the adapter's dedicated topology channel, so a `406` arg-mismatch against a\n // pre-existing incompatible queue closes only that channel and is reported by the\n // adapter's own topology refusal rather than tearing down the primary channel.\n const queueName = await assertMinionTickQueue(this.adapter, applicationId, minionName);\n\n await this.adapter.consume(queueName, async (msg: QueueConsumeMessage, acknowledger: QueueMessageAcknowledger) => {\n await this.handleMessage(msg, acknowledger, applicationId, minionName, queueName, resolver);\n });\n\n this.consumingQueueName = queueName;\n this.isConsuming = true;\n this.runner.start(queueName, resolver);\n this.reviewScope = { queueName, resolver };\n this.logger.info('Minion tick consumer started', {\n queueName,\n applicationId,\n minionName,\n });\n }\n\n public async stop(): Promise<void> {\n this.runner.stop();\n this.reviewScope = undefined;\n if (!this.isConsuming || !this.consumingQueueName) {\n return;\n }\n await this.adapter.cancelConsumer(this.consumingQueueName);\n this.isConsuming = false;\n this.consumingQueueName = undefined;\n }\n\n /**\n * Invoke from an application-owned operator operation AFTER authorization.\n * Runtime app/queue ownership is derived here, never supplied by the caller;\n * actor, reason and expected revision are persisted atomically with the decision.\n */\n public async resolveTickReview(\n input: Omit<Parameters<ScheduledExecutionRunner['resolveReview']>[0], 'queueName'>,\n ): Promise<void> {\n const scope = this.reviewScope;\n if (!scope) throw new Error('Minion tick review requires an initialized consumer');\n await this.runner.resolveReview({ ...input, queueName: scope.queueName }, scope.resolver);\n }\n\n public isRunning(): boolean {\n return this.isConsuming;\n }\n\n /**\n * Whether this consumer can still RECEIVE a tick: subscribed, and the broker connection is up.\n *\n * `isRunning()` alone is not enough. The RabbitMQ adapter does not reconnect, so after a broker restart the\n * consumer stays \"running\" over a closed connection and no tick will ever arrive again. This is what the\n * minion liveness heartbeat asks before each beat (#1859), so that state ends in a restart instead of a\n * minion that is alive and deaf.\n */\n public isReceiving(): boolean {\n return this.isConsuming && this.adapter.isConnected();\n }\n\n private async handleMessage(\n msg: QueueConsumeMessage,\n acknowledger: QueueMessageAcknowledger,\n applicationId: string,\n expectedMinionName: string,\n queueName: string,\n resolver: ScheduledExecutionResolver,\n ): Promise<void> {\n this.shutdownManager.jobStarted();\n let admitted: JobExecution | undefined;\n try {\n let accepted = false;\n try {\n const delivery = await this.scheduledMessageVerifier.verifyAndExtractMinionTick(msg.content.toString(), expectedMinionName);\n const tick = delivery?.value;\n // Signature/target checks do not establish receiving-application ownership.\n // This comparison must precede every durable insert and handler resolution — a LATE tick's\n // included: a genuine token addressed elsewhere is foreign whether or not it is on time.\n if (!delivery || !tick || tick.applicationId !== applicationId || tick.minionName !== expectedMinionName) {\n throw new Error('Scheduled minion token does not match the receiving application and minion');\n }\n const job = JobPayloadSchema.parse({\n jobId: tick.jobId, jobType: JobType.MINION_TICK, applicationId: tick.applicationId,\n minionName: tick.minionName, inputDataJson: tick.inputDataJson,\n initiatedBy: JobInitiator.CRONTAB, createdAt: new Date(tick.iat * 1000), maxRetries: 0,\n scheduledAt: tick.scheduledAt === undefined ? undefined : new Date(tick.scheduledAt),\n });\n if (delivery.verdict === ScheduledDeliveryVerdict.LATE) {\n // Genuine, past its admission deadline: the tick was missed — usually because this minion was\n // not consuming, though the scheduler can share the delay (see `ScheduledDeliveryLateness`).\n // Recorded and acknowledged, reported once per drain by the runner (#2212);\n // dead-lettered only if it could not be recorded.\n accepted = await this.runner.recordLateDelivery(job, queueName, resolver, delivery.lateness);\n } else {\n // Durable receipt, then acknowledge, THEN run — a long tick must not hold later deliveries\n // unreceived past their five-minute token (`ScheduledExecutionRunner.admit`).\n admitted = await this.runner.admit(job, queueName, resolver);\n accepted = true;\n }\n } catch (error) {\n this.logger.error('Minion tick admission failed', { expectedMinionName, error: describeCaughtError(error) });\n }\n\n // Settlement failure is a broker problem, not a new business failure. Never\n // NACK after an ACK throws or run the handler again from this catch boundary.\n try {\n if (accepted) acknowledger.ack(msg);\n else acknowledger.nack(msg, false, false);\n } catch (error) {\n this.logger.error('Minion tick settlement failed', { expectedMinionName, error: describeCaughtError(error) });\n }\n } finally { this.shutdownManager.jobCompleted(); }\n // Started only after the delivery is settled; the runner tracks the run's own activity.\n if (admitted) this.runner.launch(admitted, resolver);\n }\n}\n"]}