@cadenza.io/service 2.17.19 → 2.17.21

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.
package/dist/index.mjs CHANGED
@@ -8102,6 +8102,12 @@ import { v4 as uuid5 } from "uuid";
8102
8102
 
8103
8103
  // src/graph/controllers/GraphSyncController.ts
8104
8104
  var ACTOR_TASK_METADATA = /* @__PURE__ */ Symbol.for("@cadenza.io/core/actor-task-meta");
8105
+ var LOCAL_SYNC_QUERY_DATA = /* @__PURE__ */ Symbol.for(
8106
+ "@cadenza.io/service/local-sync-query-data"
8107
+ );
8108
+ var LOCAL_SYNC_ORIGINAL_TASK_FUNCTION = /* @__PURE__ */ Symbol.for(
8109
+ "@cadenza.io/service/local-sync-original-task-function"
8110
+ );
8105
8111
  function getActorTaskRuntimeMetadata(taskFunction) {
8106
8112
  if (typeof taskFunction !== "function") {
8107
8113
  return void 0;
@@ -8179,20 +8185,120 @@ function buildIntentRegistryData(intent) {
8179
8185
  };
8180
8186
  }
8181
8187
  function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, options = {}) {
8182
- return CadenzaService.getLocalCadenzaDBInsertTask(tableName) ?? (isCadenzaDBReady ? CadenzaService.createCadenzaDBInsertTask(tableName, queryData, options) : void 0);
8188
+ const localInsertTask = CadenzaService.getLocalCadenzaDBInsertTask(tableName);
8189
+ if (localInsertTask) {
8190
+ const taskWithSyncQueryData = localInsertTask;
8191
+ taskWithSyncQueryData[LOCAL_SYNC_QUERY_DATA] = {
8192
+ ...taskWithSyncQueryData[LOCAL_SYNC_QUERY_DATA] ?? {},
8193
+ ...queryData
8194
+ };
8195
+ if (!taskWithSyncQueryData[LOCAL_SYNC_ORIGINAL_TASK_FUNCTION]) {
8196
+ taskWithSyncQueryData[LOCAL_SYNC_ORIGINAL_TASK_FUNCTION] = taskWithSyncQueryData.taskFunction;
8197
+ taskWithSyncQueryData.taskFunction = (ctx, emit, inquire, progressCallback) => {
8198
+ const existingQueryData = ctx.queryData && typeof ctx.queryData === "object" ? ctx.queryData : {};
8199
+ const syncQueryData = taskWithSyncQueryData[LOCAL_SYNC_QUERY_DATA] ?? {};
8200
+ const nextContext = ctx.__syncing ? {
8201
+ ...ctx,
8202
+ queryData: {
8203
+ ...existingQueryData,
8204
+ ...syncQueryData
8205
+ }
8206
+ } : ctx;
8207
+ return taskWithSyncQueryData[LOCAL_SYNC_ORIGINAL_TASK_FUNCTION](
8208
+ nextContext,
8209
+ emit,
8210
+ inquire,
8211
+ progressCallback
8212
+ );
8213
+ };
8214
+ }
8215
+ return localInsertTask;
8216
+ }
8217
+ return isCadenzaDBReady ? CadenzaService.createCadenzaDBInsertTask(tableName, queryData, options) : void 0;
8183
8218
  }
8219
+ var CADENZA_DB_REQUIRED_LOCAL_SYNC_INSERT_TABLES = [
8220
+ "intent_registry",
8221
+ "routine",
8222
+ "task_to_routine_map",
8223
+ "signal_registry",
8224
+ "task",
8225
+ "actor",
8226
+ "actor_task_map",
8227
+ "signal_to_task_map",
8228
+ "intent_to_task_map",
8229
+ "directional_task_graph_map"
8230
+ ];
8184
8231
  var GraphSyncController = class _GraphSyncController {
8185
8232
  constructor() {
8186
8233
  this.registeredActors = /* @__PURE__ */ new Set();
8187
8234
  this.registeredActorTaskMaps = /* @__PURE__ */ new Set();
8188
8235
  this.registeredIntentDefinitions = /* @__PURE__ */ new Set();
8236
+ this.loggedAuthorityTaskIntentDiagnostics = /* @__PURE__ */ new Set();
8189
8237
  this.isCadenzaDBReady = false;
8238
+ this.initialized = false;
8239
+ this.initRetryScheduled = false;
8240
+ this.loggedCadenzaDBIntentSweep = false;
8241
+ this.lastMissingLocalCadenzaDBInsertTablesKey = "";
8190
8242
  }
8191
8243
  static get instance() {
8192
8244
  if (!this._instance) this._instance = new _GraphSyncController();
8193
8245
  return this._instance;
8194
8246
  }
8247
+ getMissingLocalCadenzaDBInsertTables() {
8248
+ return CADENZA_DB_REQUIRED_LOCAL_SYNC_INSERT_TABLES.filter(
8249
+ (tableName) => !CadenzaService.getLocalCadenzaDBInsertTask(tableName)
8250
+ );
8251
+ }
8252
+ ensureRetryInitTask() {
8253
+ if (this.initRetryTask) {
8254
+ return this.initRetryTask;
8255
+ }
8256
+ this.initRetryTask = CadenzaService.get("Retry graph sync init") ?? CadenzaService.createUniqueMetaTask(
8257
+ "Retry graph sync init",
8258
+ () => {
8259
+ this.initRetryScheduled = false;
8260
+ this.init();
8261
+ return true;
8262
+ },
8263
+ "Retries graph sync controller initialization once local authority tasks exist."
8264
+ ).doOn("meta.sync_controller.init_retry");
8265
+ return this.initRetryTask;
8266
+ }
8195
8267
  init() {
8268
+ if (this.initialized) {
8269
+ return;
8270
+ }
8271
+ const serviceName = resolveSyncServiceName();
8272
+ if (serviceName === "CadenzaDB") {
8273
+ const missingLocalInsertTables = this.getMissingLocalCadenzaDBInsertTables();
8274
+ if (missingLocalInsertTables.length > 0) {
8275
+ this.ensureRetryInitTask();
8276
+ const missingKey = missingLocalInsertTables.join(",");
8277
+ if (missingKey !== this.lastMissingLocalCadenzaDBInsertTablesKey) {
8278
+ this.lastMissingLocalCadenzaDBInsertTablesKey = missingKey;
8279
+ CadenzaService.log(
8280
+ "Waiting for local CadenzaDB sync insert tasks before initializing graph sync controller.",
8281
+ {
8282
+ missingLocalInsertTables
8283
+ },
8284
+ "info"
8285
+ );
8286
+ }
8287
+ if (!this.initRetryScheduled) {
8288
+ this.initRetryScheduled = true;
8289
+ CadenzaService.schedule(
8290
+ "meta.sync_controller.init_retry",
8291
+ {
8292
+ __missingLocalInsertTables: missingLocalInsertTables
8293
+ },
8294
+ 250
8295
+ );
8296
+ }
8297
+ return;
8298
+ }
8299
+ this.lastMissingLocalCadenzaDBInsertTablesKey = "";
8300
+ }
8301
+ this.initialized = true;
8196
8302
  const insertIntentRegistryTask = resolveSyncInsertTask(
8197
8303
  this.isCadenzaDBReady,
8198
8304
  "intent_registry",
@@ -8211,8 +8317,8 @@ var GraphSyncController = class _GraphSyncController {
8211
8317
  async function* (ctx, emit) {
8212
8318
  const { routines } = ctx;
8213
8319
  if (!routines) return;
8214
- const serviceName = resolveSyncServiceName();
8215
- if (!serviceName) {
8320
+ const serviceName2 = resolveSyncServiceName();
8321
+ if (!serviceName2) {
8216
8322
  return;
8217
8323
  }
8218
8324
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
@@ -8225,7 +8331,7 @@ var GraphSyncController = class _GraphSyncController {
8225
8331
  name: routine.name,
8226
8332
  version: routine.version,
8227
8333
  description: routine.description,
8228
- serviceName,
8334
+ serviceName: serviceName2,
8229
8335
  isMeta: routine.isMeta
8230
8336
  },
8231
8337
  __routineName: routine.name
@@ -8268,8 +8374,8 @@ var GraphSyncController = class _GraphSyncController {
8268
8374
  function* (ctx) {
8269
8375
  const { routines } = ctx;
8270
8376
  if (!routines) return;
8271
- const serviceName = resolveSyncServiceName();
8272
- if (!serviceName) {
8377
+ const serviceName2 = resolveSyncServiceName();
8378
+ if (!serviceName2) {
8273
8379
  return;
8274
8380
  }
8275
8381
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
@@ -8291,7 +8397,7 @@ var GraphSyncController = class _GraphSyncController {
8291
8397
  taskVersion: nextTask.version,
8292
8398
  routineName: routine.name,
8293
8399
  routineVersion: routine.version,
8294
- serviceName
8400
+ serviceName: serviceName2
8295
8401
  },
8296
8402
  __routineName: routine.name,
8297
8403
  __taskName: nextTask.name
@@ -8396,8 +8502,8 @@ var GraphSyncController = class _GraphSyncController {
8396
8502
  delayMs: 3e3
8397
8503
  });
8398
8504
  const tasks = ctx.tasks;
8399
- const serviceName = resolveSyncServiceName();
8400
- if (!serviceName) {
8505
+ const serviceName2 = resolveSyncServiceName();
8506
+ if (!serviceName2) {
8401
8507
  return;
8402
8508
  }
8403
8509
  for (const task of tasks) {
@@ -8429,7 +8535,7 @@ var GraphSyncController = class _GraphSyncController {
8429
8535
  retryDelay: task.retryDelay,
8430
8536
  retryDelayMax: task.retryDelayMax,
8431
8537
  retryDelayFactor: task.retryDelayFactor,
8432
- service_name: serviceName,
8538
+ service_name: serviceName2,
8433
8539
  signals: {
8434
8540
  emits: Array.from(task.emitsSignals),
8435
8541
  signalsToEmitAfter: Array.from(task.signalsToEmitAfter),
@@ -8482,15 +8588,15 @@ var GraphSyncController = class _GraphSyncController {
8482
8588
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
8483
8589
  delayMs: 3e3
8484
8590
  });
8485
- const serviceName = resolveSyncServiceName();
8486
- if (!serviceName) {
8591
+ const serviceName2 = resolveSyncServiceName();
8592
+ if (!serviceName2) {
8487
8593
  return;
8488
8594
  }
8489
8595
  const actors = ctx.actors ?? [];
8490
8596
  for (const actor of actors) {
8491
8597
  const data = {
8492
8598
  ...buildActorRegistrationData(actor),
8493
- service_name: serviceName
8599
+ service_name: serviceName2
8494
8600
  };
8495
8601
  if (!data.name) {
8496
8602
  continue;
@@ -8547,11 +8653,11 @@ var GraphSyncController = class _GraphSyncController {
8547
8653
  if (!metadata?.actorName) {
8548
8654
  return;
8549
8655
  }
8550
- const serviceName = resolveSyncServiceName(task);
8551
- if (!serviceName) {
8656
+ const serviceName2 = resolveSyncServiceName(task);
8657
+ if (!serviceName2) {
8552
8658
  return;
8553
8659
  }
8554
- const registrationKey = `${metadata.actorName}|${task.name}|${task.version}|${serviceName}`;
8660
+ const registrationKey = `${metadata.actorName}|${task.name}|${task.version}|${serviceName2}`;
8555
8661
  if (this.registeredActorTaskMaps.has(registrationKey)) {
8556
8662
  return;
8557
8663
  }
@@ -8561,7 +8667,7 @@ var GraphSyncController = class _GraphSyncController {
8561
8667
  actor_version: 1,
8562
8668
  task_name: task.name,
8563
8669
  task_version: task.version,
8564
- service_name: serviceName,
8670
+ service_name: serviceName2,
8565
8671
  mode: metadata.mode,
8566
8672
  description: task.description ?? metadata.actorDescription ?? "",
8567
8673
  is_meta: metadata.actorKind === "meta" || task.isMeta === true
@@ -8617,8 +8723,8 @@ var GraphSyncController = class _GraphSyncController {
8617
8723
  function* (ctx) {
8618
8724
  const task = ctx.task;
8619
8725
  if (task.hidden || !task.register) return;
8620
- const serviceName = resolveSyncServiceName(task);
8621
- if (!serviceName) {
8726
+ const serviceName2 = resolveSyncServiceName(task);
8727
+ if (!serviceName2) {
8622
8728
  return;
8623
8729
  }
8624
8730
  for (const signal of task.observedSignals) {
@@ -8631,7 +8737,7 @@ var GraphSyncController = class _GraphSyncController {
8631
8737
  isGlobal,
8632
8738
  taskName: task.name,
8633
8739
  taskVersion: task.version,
8634
- serviceName
8740
+ serviceName: serviceName2
8635
8741
  },
8636
8742
  __taskName: task.name,
8637
8743
  __signal: signal
@@ -8665,7 +8771,7 @@ var GraphSyncController = class _GraphSyncController {
8665
8771
  delayMs: 3e3
8666
8772
  });
8667
8773
  const intents = Array.isArray(ctx.intents) ? ctx.intents : Array.from(CadenzaService.inquiryBroker.intents.values());
8668
- if (resolveSyncServiceName() === "CadenzaDB") {
8774
+ if (resolveSyncServiceName() === "CadenzaDB" && !this.loggedCadenzaDBIntentSweep) {
8669
8775
  const intentNames = intents.map((intent) => String(intent?.name ?? "").trim()).filter(Boolean);
8670
8776
  const authorityIntentNames = intentNames.filter(
8671
8777
  (intentName) => intentName === "meta-service-registry-full-sync" || intentName.includes("service_instance") || intentName.includes("service_instance_transport") || intentName.includes("intent_to_task_map") || intentName.includes("signal_to_task_map")
@@ -8681,6 +8787,7 @@ var GraphSyncController = class _GraphSyncController {
8681
8787
  },
8682
8788
  "info"
8683
8789
  );
8790
+ this.loggedCadenzaDBIntentSweep = true;
8684
8791
  }
8685
8792
  for (const intent of intents) {
8686
8793
  const intentData = buildIntentRegistryData(intent);
@@ -8734,29 +8841,33 @@ var GraphSyncController = class _GraphSyncController {
8734
8841
  function* (ctx) {
8735
8842
  const task = ctx.task;
8736
8843
  if (task.hidden || !task.register) return;
8737
- const serviceName = resolveSyncServiceName(task);
8738
- if (!serviceName) {
8844
+ const serviceName2 = resolveSyncServiceName(task);
8845
+ if (!serviceName2) {
8739
8846
  return;
8740
8847
  }
8741
8848
  task.__registeredIntents = task.__registeredIntents ?? /* @__PURE__ */ new Set();
8742
8849
  task.__invalidMetaIntentWarnings = task.__invalidMetaIntentWarnings ?? /* @__PURE__ */ new Set();
8743
- if (serviceName === "CadenzaDB" && [
8850
+ if (serviceName2 === "CadenzaDB" && [
8744
8851
  "Query service_instance",
8745
8852
  "Query service_instance_transport",
8746
8853
  "Query intent_to_task_map",
8747
8854
  "Query signal_to_task_map"
8748
8855
  ].includes(task.name)) {
8749
- CadenzaService.log(
8750
- "CadenzaDB authority task intent diagnostics.",
8751
- {
8752
- taskName: task.name,
8753
- taskVersion: task.version,
8754
- isMeta: task.isMeta,
8755
- handlesIntents: Array.from(task.handlesIntents ?? []),
8756
- registeredIntents: Array.from(task.__registeredIntents ?? [])
8757
- },
8758
- "info"
8759
- );
8856
+ const authorityTaskKey = `${task.name}:${task.version}`;
8857
+ if (!this.loggedAuthorityTaskIntentDiagnostics.has(authorityTaskKey)) {
8858
+ this.loggedAuthorityTaskIntentDiagnostics.add(authorityTaskKey);
8859
+ CadenzaService.log(
8860
+ "CadenzaDB authority task intent diagnostics.",
8861
+ {
8862
+ taskName: task.name,
8863
+ taskVersion: task.version,
8864
+ isMeta: task.isMeta,
8865
+ handlesIntents: Array.from(task.handlesIntents ?? []),
8866
+ registeredIntents: Array.from(task.__registeredIntents ?? [])
8867
+ },
8868
+ "info"
8869
+ );
8870
+ }
8760
8871
  }
8761
8872
  for (const intent of task.handlesIntents) {
8762
8873
  if (task.__registeredIntents.has(intent)) continue;
@@ -8784,7 +8895,7 @@ var GraphSyncController = class _GraphSyncController {
8784
8895
  intentName: intent,
8785
8896
  taskName: task.name,
8786
8897
  taskVersion: task.version,
8787
- serviceName
8898
+ serviceName: serviceName2
8788
8899
  },
8789
8900
  __taskName: task.name,
8790
8901
  __intent: intent,
@@ -8793,11 +8904,11 @@ var GraphSyncController = class _GraphSyncController {
8793
8904
  intentName: intent,
8794
8905
  taskName: task.name,
8795
8906
  taskVersion: task.version,
8796
- serviceName
8907
+ serviceName: serviceName2
8797
8908
  }
8798
8909
  };
8799
8910
  }
8800
- }
8911
+ }.bind(this)
8801
8912
  ).then(
8802
8913
  CadenzaService.createMetaTask(
8803
8914
  "Prepare intent definition for intent-to-task map",
@@ -8862,8 +8973,8 @@ var GraphSyncController = class _GraphSyncController {
8862
8973
  if (task.taskMapRegistration.has(t.name) || t.hidden || !t.register) {
8863
8974
  continue;
8864
8975
  }
8865
- const serviceName = resolveSyncServiceName(t);
8866
- if (!serviceName) {
8976
+ const serviceName2 = resolveSyncServiceName(t);
8977
+ if (!serviceName2) {
8867
8978
  continue;
8868
8979
  }
8869
8980
  yield {
@@ -8872,7 +8983,7 @@ var GraphSyncController = class _GraphSyncController {
8872
8983
  taskVersion: t.version,
8873
8984
  predecessorTaskName: task.name,
8874
8985
  predecessorTaskVersion: task.version,
8875
- serviceName,
8986
+ serviceName: serviceName2,
8876
8987
  predecessorServiceName
8877
8988
  },
8878
8989
  __taskName: task.name,
@@ -8921,16 +9032,16 @@ var GraphSyncController = class _GraphSyncController {
8921
9032
  if (task.hidden || !task.register) return;
8922
9033
  if (task.isDeputy && !task.signalName) {
8923
9034
  if (task.registeredDeputyMap) return;
8924
- const serviceName = resolveSyncServiceName(task);
9035
+ const serviceName2 = resolveSyncServiceName(task);
8925
9036
  const predecessorServiceName = resolveSyncServiceName();
8926
- if (!serviceName || !predecessorServiceName) {
9037
+ if (!serviceName2 || !predecessorServiceName) {
8927
9038
  return;
8928
9039
  }
8929
9040
  return {
8930
9041
  data: {
8931
9042
  task_name: task.remoteRoutineName,
8932
9043
  task_version: 1,
8933
- service_name: serviceName,
9044
+ service_name: serviceName2,
8934
9045
  predecessor_task_name: task.name,
8935
9046
  predecessor_task_version: task.version,
8936
9047
  predecessor_service_name: predecessorServiceName