@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.js CHANGED
@@ -8153,6 +8153,12 @@ var import_uuid5 = require("uuid");
8153
8153
 
8154
8154
  // src/graph/controllers/GraphSyncController.ts
8155
8155
  var ACTOR_TASK_METADATA = /* @__PURE__ */ Symbol.for("@cadenza.io/core/actor-task-meta");
8156
+ var LOCAL_SYNC_QUERY_DATA = /* @__PURE__ */ Symbol.for(
8157
+ "@cadenza.io/service/local-sync-query-data"
8158
+ );
8159
+ var LOCAL_SYNC_ORIGINAL_TASK_FUNCTION = /* @__PURE__ */ Symbol.for(
8160
+ "@cadenza.io/service/local-sync-original-task-function"
8161
+ );
8156
8162
  function getActorTaskRuntimeMetadata(taskFunction) {
8157
8163
  if (typeof taskFunction !== "function") {
8158
8164
  return void 0;
@@ -8230,20 +8236,120 @@ function buildIntentRegistryData(intent) {
8230
8236
  };
8231
8237
  }
8232
8238
  function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, options = {}) {
8233
- return CadenzaService.getLocalCadenzaDBInsertTask(tableName) ?? (isCadenzaDBReady ? CadenzaService.createCadenzaDBInsertTask(tableName, queryData, options) : void 0);
8239
+ const localInsertTask = CadenzaService.getLocalCadenzaDBInsertTask(tableName);
8240
+ if (localInsertTask) {
8241
+ const taskWithSyncQueryData = localInsertTask;
8242
+ taskWithSyncQueryData[LOCAL_SYNC_QUERY_DATA] = {
8243
+ ...taskWithSyncQueryData[LOCAL_SYNC_QUERY_DATA] ?? {},
8244
+ ...queryData
8245
+ };
8246
+ if (!taskWithSyncQueryData[LOCAL_SYNC_ORIGINAL_TASK_FUNCTION]) {
8247
+ taskWithSyncQueryData[LOCAL_SYNC_ORIGINAL_TASK_FUNCTION] = taskWithSyncQueryData.taskFunction;
8248
+ taskWithSyncQueryData.taskFunction = (ctx, emit, inquire, progressCallback) => {
8249
+ const existingQueryData = ctx.queryData && typeof ctx.queryData === "object" ? ctx.queryData : {};
8250
+ const syncQueryData = taskWithSyncQueryData[LOCAL_SYNC_QUERY_DATA] ?? {};
8251
+ const nextContext = ctx.__syncing ? {
8252
+ ...ctx,
8253
+ queryData: {
8254
+ ...existingQueryData,
8255
+ ...syncQueryData
8256
+ }
8257
+ } : ctx;
8258
+ return taskWithSyncQueryData[LOCAL_SYNC_ORIGINAL_TASK_FUNCTION](
8259
+ nextContext,
8260
+ emit,
8261
+ inquire,
8262
+ progressCallback
8263
+ );
8264
+ };
8265
+ }
8266
+ return localInsertTask;
8267
+ }
8268
+ return isCadenzaDBReady ? CadenzaService.createCadenzaDBInsertTask(tableName, queryData, options) : void 0;
8234
8269
  }
8270
+ var CADENZA_DB_REQUIRED_LOCAL_SYNC_INSERT_TABLES = [
8271
+ "intent_registry",
8272
+ "routine",
8273
+ "task_to_routine_map",
8274
+ "signal_registry",
8275
+ "task",
8276
+ "actor",
8277
+ "actor_task_map",
8278
+ "signal_to_task_map",
8279
+ "intent_to_task_map",
8280
+ "directional_task_graph_map"
8281
+ ];
8235
8282
  var GraphSyncController = class _GraphSyncController {
8236
8283
  constructor() {
8237
8284
  this.registeredActors = /* @__PURE__ */ new Set();
8238
8285
  this.registeredActorTaskMaps = /* @__PURE__ */ new Set();
8239
8286
  this.registeredIntentDefinitions = /* @__PURE__ */ new Set();
8287
+ this.loggedAuthorityTaskIntentDiagnostics = /* @__PURE__ */ new Set();
8240
8288
  this.isCadenzaDBReady = false;
8289
+ this.initialized = false;
8290
+ this.initRetryScheduled = false;
8291
+ this.loggedCadenzaDBIntentSweep = false;
8292
+ this.lastMissingLocalCadenzaDBInsertTablesKey = "";
8241
8293
  }
8242
8294
  static get instance() {
8243
8295
  if (!this._instance) this._instance = new _GraphSyncController();
8244
8296
  return this._instance;
8245
8297
  }
8298
+ getMissingLocalCadenzaDBInsertTables() {
8299
+ return CADENZA_DB_REQUIRED_LOCAL_SYNC_INSERT_TABLES.filter(
8300
+ (tableName) => !CadenzaService.getLocalCadenzaDBInsertTask(tableName)
8301
+ );
8302
+ }
8303
+ ensureRetryInitTask() {
8304
+ if (this.initRetryTask) {
8305
+ return this.initRetryTask;
8306
+ }
8307
+ this.initRetryTask = CadenzaService.get("Retry graph sync init") ?? CadenzaService.createUniqueMetaTask(
8308
+ "Retry graph sync init",
8309
+ () => {
8310
+ this.initRetryScheduled = false;
8311
+ this.init();
8312
+ return true;
8313
+ },
8314
+ "Retries graph sync controller initialization once local authority tasks exist."
8315
+ ).doOn("meta.sync_controller.init_retry");
8316
+ return this.initRetryTask;
8317
+ }
8246
8318
  init() {
8319
+ if (this.initialized) {
8320
+ return;
8321
+ }
8322
+ const serviceName = resolveSyncServiceName();
8323
+ if (serviceName === "CadenzaDB") {
8324
+ const missingLocalInsertTables = this.getMissingLocalCadenzaDBInsertTables();
8325
+ if (missingLocalInsertTables.length > 0) {
8326
+ this.ensureRetryInitTask();
8327
+ const missingKey = missingLocalInsertTables.join(",");
8328
+ if (missingKey !== this.lastMissingLocalCadenzaDBInsertTablesKey) {
8329
+ this.lastMissingLocalCadenzaDBInsertTablesKey = missingKey;
8330
+ CadenzaService.log(
8331
+ "Waiting for local CadenzaDB sync insert tasks before initializing graph sync controller.",
8332
+ {
8333
+ missingLocalInsertTables
8334
+ },
8335
+ "info"
8336
+ );
8337
+ }
8338
+ if (!this.initRetryScheduled) {
8339
+ this.initRetryScheduled = true;
8340
+ CadenzaService.schedule(
8341
+ "meta.sync_controller.init_retry",
8342
+ {
8343
+ __missingLocalInsertTables: missingLocalInsertTables
8344
+ },
8345
+ 250
8346
+ );
8347
+ }
8348
+ return;
8349
+ }
8350
+ this.lastMissingLocalCadenzaDBInsertTablesKey = "";
8351
+ }
8352
+ this.initialized = true;
8247
8353
  const insertIntentRegistryTask = resolveSyncInsertTask(
8248
8354
  this.isCadenzaDBReady,
8249
8355
  "intent_registry",
@@ -8262,8 +8368,8 @@ var GraphSyncController = class _GraphSyncController {
8262
8368
  async function* (ctx, emit) {
8263
8369
  const { routines } = ctx;
8264
8370
  if (!routines) return;
8265
- const serviceName = resolveSyncServiceName();
8266
- if (!serviceName) {
8371
+ const serviceName2 = resolveSyncServiceName();
8372
+ if (!serviceName2) {
8267
8373
  return;
8268
8374
  }
8269
8375
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
@@ -8276,7 +8382,7 @@ var GraphSyncController = class _GraphSyncController {
8276
8382
  name: routine.name,
8277
8383
  version: routine.version,
8278
8384
  description: routine.description,
8279
- serviceName,
8385
+ serviceName: serviceName2,
8280
8386
  isMeta: routine.isMeta
8281
8387
  },
8282
8388
  __routineName: routine.name
@@ -8319,8 +8425,8 @@ var GraphSyncController = class _GraphSyncController {
8319
8425
  function* (ctx) {
8320
8426
  const { routines } = ctx;
8321
8427
  if (!routines) return;
8322
- const serviceName = resolveSyncServiceName();
8323
- if (!serviceName) {
8428
+ const serviceName2 = resolveSyncServiceName();
8429
+ if (!serviceName2) {
8324
8430
  return;
8325
8431
  }
8326
8432
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
@@ -8342,7 +8448,7 @@ var GraphSyncController = class _GraphSyncController {
8342
8448
  taskVersion: nextTask.version,
8343
8449
  routineName: routine.name,
8344
8450
  routineVersion: routine.version,
8345
- serviceName
8451
+ serviceName: serviceName2
8346
8452
  },
8347
8453
  __routineName: routine.name,
8348
8454
  __taskName: nextTask.name
@@ -8447,8 +8553,8 @@ var GraphSyncController = class _GraphSyncController {
8447
8553
  delayMs: 3e3
8448
8554
  });
8449
8555
  const tasks = ctx.tasks;
8450
- const serviceName = resolveSyncServiceName();
8451
- if (!serviceName) {
8556
+ const serviceName2 = resolveSyncServiceName();
8557
+ if (!serviceName2) {
8452
8558
  return;
8453
8559
  }
8454
8560
  for (const task of tasks) {
@@ -8480,7 +8586,7 @@ var GraphSyncController = class _GraphSyncController {
8480
8586
  retryDelay: task.retryDelay,
8481
8587
  retryDelayMax: task.retryDelayMax,
8482
8588
  retryDelayFactor: task.retryDelayFactor,
8483
- service_name: serviceName,
8589
+ service_name: serviceName2,
8484
8590
  signals: {
8485
8591
  emits: Array.from(task.emitsSignals),
8486
8592
  signalsToEmitAfter: Array.from(task.signalsToEmitAfter),
@@ -8533,15 +8639,15 @@ var GraphSyncController = class _GraphSyncController {
8533
8639
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
8534
8640
  delayMs: 3e3
8535
8641
  });
8536
- const serviceName = resolveSyncServiceName();
8537
- if (!serviceName) {
8642
+ const serviceName2 = resolveSyncServiceName();
8643
+ if (!serviceName2) {
8538
8644
  return;
8539
8645
  }
8540
8646
  const actors = ctx.actors ?? [];
8541
8647
  for (const actor of actors) {
8542
8648
  const data = {
8543
8649
  ...buildActorRegistrationData(actor),
8544
- service_name: serviceName
8650
+ service_name: serviceName2
8545
8651
  };
8546
8652
  if (!data.name) {
8547
8653
  continue;
@@ -8598,11 +8704,11 @@ var GraphSyncController = class _GraphSyncController {
8598
8704
  if (!metadata?.actorName) {
8599
8705
  return;
8600
8706
  }
8601
- const serviceName = resolveSyncServiceName(task);
8602
- if (!serviceName) {
8707
+ const serviceName2 = resolveSyncServiceName(task);
8708
+ if (!serviceName2) {
8603
8709
  return;
8604
8710
  }
8605
- const registrationKey = `${metadata.actorName}|${task.name}|${task.version}|${serviceName}`;
8711
+ const registrationKey = `${metadata.actorName}|${task.name}|${task.version}|${serviceName2}`;
8606
8712
  if (this.registeredActorTaskMaps.has(registrationKey)) {
8607
8713
  return;
8608
8714
  }
@@ -8612,7 +8718,7 @@ var GraphSyncController = class _GraphSyncController {
8612
8718
  actor_version: 1,
8613
8719
  task_name: task.name,
8614
8720
  task_version: task.version,
8615
- service_name: serviceName,
8721
+ service_name: serviceName2,
8616
8722
  mode: metadata.mode,
8617
8723
  description: task.description ?? metadata.actorDescription ?? "",
8618
8724
  is_meta: metadata.actorKind === "meta" || task.isMeta === true
@@ -8668,8 +8774,8 @@ var GraphSyncController = class _GraphSyncController {
8668
8774
  function* (ctx) {
8669
8775
  const task = ctx.task;
8670
8776
  if (task.hidden || !task.register) return;
8671
- const serviceName = resolveSyncServiceName(task);
8672
- if (!serviceName) {
8777
+ const serviceName2 = resolveSyncServiceName(task);
8778
+ if (!serviceName2) {
8673
8779
  return;
8674
8780
  }
8675
8781
  for (const signal of task.observedSignals) {
@@ -8682,7 +8788,7 @@ var GraphSyncController = class _GraphSyncController {
8682
8788
  isGlobal,
8683
8789
  taskName: task.name,
8684
8790
  taskVersion: task.version,
8685
- serviceName
8791
+ serviceName: serviceName2
8686
8792
  },
8687
8793
  __taskName: task.name,
8688
8794
  __signal: signal
@@ -8716,7 +8822,7 @@ var GraphSyncController = class _GraphSyncController {
8716
8822
  delayMs: 3e3
8717
8823
  });
8718
8824
  const intents = Array.isArray(ctx.intents) ? ctx.intents : Array.from(CadenzaService.inquiryBroker.intents.values());
8719
- if (resolveSyncServiceName() === "CadenzaDB") {
8825
+ if (resolveSyncServiceName() === "CadenzaDB" && !this.loggedCadenzaDBIntentSweep) {
8720
8826
  const intentNames = intents.map((intent) => String(intent?.name ?? "").trim()).filter(Boolean);
8721
8827
  const authorityIntentNames = intentNames.filter(
8722
8828
  (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")
@@ -8732,6 +8838,7 @@ var GraphSyncController = class _GraphSyncController {
8732
8838
  },
8733
8839
  "info"
8734
8840
  );
8841
+ this.loggedCadenzaDBIntentSweep = true;
8735
8842
  }
8736
8843
  for (const intent of intents) {
8737
8844
  const intentData = buildIntentRegistryData(intent);
@@ -8785,29 +8892,33 @@ var GraphSyncController = class _GraphSyncController {
8785
8892
  function* (ctx) {
8786
8893
  const task = ctx.task;
8787
8894
  if (task.hidden || !task.register) return;
8788
- const serviceName = resolveSyncServiceName(task);
8789
- if (!serviceName) {
8895
+ const serviceName2 = resolveSyncServiceName(task);
8896
+ if (!serviceName2) {
8790
8897
  return;
8791
8898
  }
8792
8899
  task.__registeredIntents = task.__registeredIntents ?? /* @__PURE__ */ new Set();
8793
8900
  task.__invalidMetaIntentWarnings = task.__invalidMetaIntentWarnings ?? /* @__PURE__ */ new Set();
8794
- if (serviceName === "CadenzaDB" && [
8901
+ if (serviceName2 === "CadenzaDB" && [
8795
8902
  "Query service_instance",
8796
8903
  "Query service_instance_transport",
8797
8904
  "Query intent_to_task_map",
8798
8905
  "Query signal_to_task_map"
8799
8906
  ].includes(task.name)) {
8800
- CadenzaService.log(
8801
- "CadenzaDB authority task intent diagnostics.",
8802
- {
8803
- taskName: task.name,
8804
- taskVersion: task.version,
8805
- isMeta: task.isMeta,
8806
- handlesIntents: Array.from(task.handlesIntents ?? []),
8807
- registeredIntents: Array.from(task.__registeredIntents ?? [])
8808
- },
8809
- "info"
8810
- );
8907
+ const authorityTaskKey = `${task.name}:${task.version}`;
8908
+ if (!this.loggedAuthorityTaskIntentDiagnostics.has(authorityTaskKey)) {
8909
+ this.loggedAuthorityTaskIntentDiagnostics.add(authorityTaskKey);
8910
+ CadenzaService.log(
8911
+ "CadenzaDB authority task intent diagnostics.",
8912
+ {
8913
+ taskName: task.name,
8914
+ taskVersion: task.version,
8915
+ isMeta: task.isMeta,
8916
+ handlesIntents: Array.from(task.handlesIntents ?? []),
8917
+ registeredIntents: Array.from(task.__registeredIntents ?? [])
8918
+ },
8919
+ "info"
8920
+ );
8921
+ }
8811
8922
  }
8812
8923
  for (const intent of task.handlesIntents) {
8813
8924
  if (task.__registeredIntents.has(intent)) continue;
@@ -8835,7 +8946,7 @@ var GraphSyncController = class _GraphSyncController {
8835
8946
  intentName: intent,
8836
8947
  taskName: task.name,
8837
8948
  taskVersion: task.version,
8838
- serviceName
8949
+ serviceName: serviceName2
8839
8950
  },
8840
8951
  __taskName: task.name,
8841
8952
  __intent: intent,
@@ -8844,11 +8955,11 @@ var GraphSyncController = class _GraphSyncController {
8844
8955
  intentName: intent,
8845
8956
  taskName: task.name,
8846
8957
  taskVersion: task.version,
8847
- serviceName
8958
+ serviceName: serviceName2
8848
8959
  }
8849
8960
  };
8850
8961
  }
8851
- }
8962
+ }.bind(this)
8852
8963
  ).then(
8853
8964
  CadenzaService.createMetaTask(
8854
8965
  "Prepare intent definition for intent-to-task map",
@@ -8913,8 +9024,8 @@ var GraphSyncController = class _GraphSyncController {
8913
9024
  if (task.taskMapRegistration.has(t.name) || t.hidden || !t.register) {
8914
9025
  continue;
8915
9026
  }
8916
- const serviceName = resolveSyncServiceName(t);
8917
- if (!serviceName) {
9027
+ const serviceName2 = resolveSyncServiceName(t);
9028
+ if (!serviceName2) {
8918
9029
  continue;
8919
9030
  }
8920
9031
  yield {
@@ -8923,7 +9034,7 @@ var GraphSyncController = class _GraphSyncController {
8923
9034
  taskVersion: t.version,
8924
9035
  predecessorTaskName: task.name,
8925
9036
  predecessorTaskVersion: task.version,
8926
- serviceName,
9037
+ serviceName: serviceName2,
8927
9038
  predecessorServiceName
8928
9039
  },
8929
9040
  __taskName: task.name,
@@ -8972,16 +9083,16 @@ var GraphSyncController = class _GraphSyncController {
8972
9083
  if (task.hidden || !task.register) return;
8973
9084
  if (task.isDeputy && !task.signalName) {
8974
9085
  if (task.registeredDeputyMap) return;
8975
- const serviceName = resolveSyncServiceName(task);
9086
+ const serviceName2 = resolveSyncServiceName(task);
8976
9087
  const predecessorServiceName = resolveSyncServiceName();
8977
- if (!serviceName || !predecessorServiceName) {
9088
+ if (!serviceName2 || !predecessorServiceName) {
8978
9089
  return;
8979
9090
  }
8980
9091
  return {
8981
9092
  data: {
8982
9093
  task_name: task.remoteRoutineName,
8983
9094
  task_version: 1,
8984
- service_name: serviceName,
9095
+ service_name: serviceName2,
8985
9096
  predecessor_task_name: task.name,
8986
9097
  predecessor_task_version: task.version,
8987
9098
  predecessor_service_name: predecessorServiceName