@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.
@@ -5663,6 +5663,12 @@ import { v4 as uuid4 } from "uuid";
5663
5663
 
5664
5664
  // src/graph/controllers/GraphSyncController.ts
5665
5665
  var ACTOR_TASK_METADATA = /* @__PURE__ */ Symbol.for("@cadenza.io/core/actor-task-meta");
5666
+ var LOCAL_SYNC_QUERY_DATA = /* @__PURE__ */ Symbol.for(
5667
+ "@cadenza.io/service/local-sync-query-data"
5668
+ );
5669
+ var LOCAL_SYNC_ORIGINAL_TASK_FUNCTION = /* @__PURE__ */ Symbol.for(
5670
+ "@cadenza.io/service/local-sync-original-task-function"
5671
+ );
5666
5672
  function getActorTaskRuntimeMetadata(taskFunction) {
5667
5673
  if (typeof taskFunction !== "function") {
5668
5674
  return void 0;
@@ -5740,20 +5746,120 @@ function buildIntentRegistryData(intent) {
5740
5746
  };
5741
5747
  }
5742
5748
  function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, options = {}) {
5743
- return CadenzaService.getLocalCadenzaDBInsertTask(tableName) ?? (isCadenzaDBReady ? CadenzaService.createCadenzaDBInsertTask(tableName, queryData, options) : void 0);
5749
+ const localInsertTask = CadenzaService.getLocalCadenzaDBInsertTask(tableName);
5750
+ if (localInsertTask) {
5751
+ const taskWithSyncQueryData = localInsertTask;
5752
+ taskWithSyncQueryData[LOCAL_SYNC_QUERY_DATA] = {
5753
+ ...taskWithSyncQueryData[LOCAL_SYNC_QUERY_DATA] ?? {},
5754
+ ...queryData
5755
+ };
5756
+ if (!taskWithSyncQueryData[LOCAL_SYNC_ORIGINAL_TASK_FUNCTION]) {
5757
+ taskWithSyncQueryData[LOCAL_SYNC_ORIGINAL_TASK_FUNCTION] = taskWithSyncQueryData.taskFunction;
5758
+ taskWithSyncQueryData.taskFunction = (ctx, emit, inquire, progressCallback) => {
5759
+ const existingQueryData = ctx.queryData && typeof ctx.queryData === "object" ? ctx.queryData : {};
5760
+ const syncQueryData = taskWithSyncQueryData[LOCAL_SYNC_QUERY_DATA] ?? {};
5761
+ const nextContext = ctx.__syncing ? {
5762
+ ...ctx,
5763
+ queryData: {
5764
+ ...existingQueryData,
5765
+ ...syncQueryData
5766
+ }
5767
+ } : ctx;
5768
+ return taskWithSyncQueryData[LOCAL_SYNC_ORIGINAL_TASK_FUNCTION](
5769
+ nextContext,
5770
+ emit,
5771
+ inquire,
5772
+ progressCallback
5773
+ );
5774
+ };
5775
+ }
5776
+ return localInsertTask;
5777
+ }
5778
+ return isCadenzaDBReady ? CadenzaService.createCadenzaDBInsertTask(tableName, queryData, options) : void 0;
5744
5779
  }
5780
+ var CADENZA_DB_REQUIRED_LOCAL_SYNC_INSERT_TABLES = [
5781
+ "intent_registry",
5782
+ "routine",
5783
+ "task_to_routine_map",
5784
+ "signal_registry",
5785
+ "task",
5786
+ "actor",
5787
+ "actor_task_map",
5788
+ "signal_to_task_map",
5789
+ "intent_to_task_map",
5790
+ "directional_task_graph_map"
5791
+ ];
5745
5792
  var GraphSyncController = class _GraphSyncController {
5746
5793
  constructor() {
5747
5794
  this.registeredActors = /* @__PURE__ */ new Set();
5748
5795
  this.registeredActorTaskMaps = /* @__PURE__ */ new Set();
5749
5796
  this.registeredIntentDefinitions = /* @__PURE__ */ new Set();
5797
+ this.loggedAuthorityTaskIntentDiagnostics = /* @__PURE__ */ new Set();
5750
5798
  this.isCadenzaDBReady = false;
5799
+ this.initialized = false;
5800
+ this.initRetryScheduled = false;
5801
+ this.loggedCadenzaDBIntentSweep = false;
5802
+ this.lastMissingLocalCadenzaDBInsertTablesKey = "";
5751
5803
  }
5752
5804
  static get instance() {
5753
5805
  if (!this._instance) this._instance = new _GraphSyncController();
5754
5806
  return this._instance;
5755
5807
  }
5808
+ getMissingLocalCadenzaDBInsertTables() {
5809
+ return CADENZA_DB_REQUIRED_LOCAL_SYNC_INSERT_TABLES.filter(
5810
+ (tableName) => !CadenzaService.getLocalCadenzaDBInsertTask(tableName)
5811
+ );
5812
+ }
5813
+ ensureRetryInitTask() {
5814
+ if (this.initRetryTask) {
5815
+ return this.initRetryTask;
5816
+ }
5817
+ this.initRetryTask = CadenzaService.get("Retry graph sync init") ?? CadenzaService.createUniqueMetaTask(
5818
+ "Retry graph sync init",
5819
+ () => {
5820
+ this.initRetryScheduled = false;
5821
+ this.init();
5822
+ return true;
5823
+ },
5824
+ "Retries graph sync controller initialization once local authority tasks exist."
5825
+ ).doOn("meta.sync_controller.init_retry");
5826
+ return this.initRetryTask;
5827
+ }
5756
5828
  init() {
5829
+ if (this.initialized) {
5830
+ return;
5831
+ }
5832
+ const serviceName = resolveSyncServiceName();
5833
+ if (serviceName === "CadenzaDB") {
5834
+ const missingLocalInsertTables = this.getMissingLocalCadenzaDBInsertTables();
5835
+ if (missingLocalInsertTables.length > 0) {
5836
+ this.ensureRetryInitTask();
5837
+ const missingKey = missingLocalInsertTables.join(",");
5838
+ if (missingKey !== this.lastMissingLocalCadenzaDBInsertTablesKey) {
5839
+ this.lastMissingLocalCadenzaDBInsertTablesKey = missingKey;
5840
+ CadenzaService.log(
5841
+ "Waiting for local CadenzaDB sync insert tasks before initializing graph sync controller.",
5842
+ {
5843
+ missingLocalInsertTables
5844
+ },
5845
+ "info"
5846
+ );
5847
+ }
5848
+ if (!this.initRetryScheduled) {
5849
+ this.initRetryScheduled = true;
5850
+ CadenzaService.schedule(
5851
+ "meta.sync_controller.init_retry",
5852
+ {
5853
+ __missingLocalInsertTables: missingLocalInsertTables
5854
+ },
5855
+ 250
5856
+ );
5857
+ }
5858
+ return;
5859
+ }
5860
+ this.lastMissingLocalCadenzaDBInsertTablesKey = "";
5861
+ }
5862
+ this.initialized = true;
5757
5863
  const insertIntentRegistryTask = resolveSyncInsertTask(
5758
5864
  this.isCadenzaDBReady,
5759
5865
  "intent_registry",
@@ -5772,8 +5878,8 @@ var GraphSyncController = class _GraphSyncController {
5772
5878
  async function* (ctx, emit) {
5773
5879
  const { routines } = ctx;
5774
5880
  if (!routines) return;
5775
- const serviceName = resolveSyncServiceName();
5776
- if (!serviceName) {
5881
+ const serviceName2 = resolveSyncServiceName();
5882
+ if (!serviceName2) {
5777
5883
  return;
5778
5884
  }
5779
5885
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
@@ -5786,7 +5892,7 @@ var GraphSyncController = class _GraphSyncController {
5786
5892
  name: routine.name,
5787
5893
  version: routine.version,
5788
5894
  description: routine.description,
5789
- serviceName,
5895
+ serviceName: serviceName2,
5790
5896
  isMeta: routine.isMeta
5791
5897
  },
5792
5898
  __routineName: routine.name
@@ -5829,8 +5935,8 @@ var GraphSyncController = class _GraphSyncController {
5829
5935
  function* (ctx) {
5830
5936
  const { routines } = ctx;
5831
5937
  if (!routines) return;
5832
- const serviceName = resolveSyncServiceName();
5833
- if (!serviceName) {
5938
+ const serviceName2 = resolveSyncServiceName();
5939
+ if (!serviceName2) {
5834
5940
  return;
5835
5941
  }
5836
5942
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
@@ -5852,7 +5958,7 @@ var GraphSyncController = class _GraphSyncController {
5852
5958
  taskVersion: nextTask.version,
5853
5959
  routineName: routine.name,
5854
5960
  routineVersion: routine.version,
5855
- serviceName
5961
+ serviceName: serviceName2
5856
5962
  },
5857
5963
  __routineName: routine.name,
5858
5964
  __taskName: nextTask.name
@@ -5957,8 +6063,8 @@ var GraphSyncController = class _GraphSyncController {
5957
6063
  delayMs: 3e3
5958
6064
  });
5959
6065
  const tasks = ctx.tasks;
5960
- const serviceName = resolveSyncServiceName();
5961
- if (!serviceName) {
6066
+ const serviceName2 = resolveSyncServiceName();
6067
+ if (!serviceName2) {
5962
6068
  return;
5963
6069
  }
5964
6070
  for (const task of tasks) {
@@ -5990,7 +6096,7 @@ var GraphSyncController = class _GraphSyncController {
5990
6096
  retryDelay: task.retryDelay,
5991
6097
  retryDelayMax: task.retryDelayMax,
5992
6098
  retryDelayFactor: task.retryDelayFactor,
5993
- service_name: serviceName,
6099
+ service_name: serviceName2,
5994
6100
  signals: {
5995
6101
  emits: Array.from(task.emitsSignals),
5996
6102
  signalsToEmitAfter: Array.from(task.signalsToEmitAfter),
@@ -6043,15 +6149,15 @@ var GraphSyncController = class _GraphSyncController {
6043
6149
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
6044
6150
  delayMs: 3e3
6045
6151
  });
6046
- const serviceName = resolveSyncServiceName();
6047
- if (!serviceName) {
6152
+ const serviceName2 = resolveSyncServiceName();
6153
+ if (!serviceName2) {
6048
6154
  return;
6049
6155
  }
6050
6156
  const actors = ctx.actors ?? [];
6051
6157
  for (const actor of actors) {
6052
6158
  const data = {
6053
6159
  ...buildActorRegistrationData(actor),
6054
- service_name: serviceName
6160
+ service_name: serviceName2
6055
6161
  };
6056
6162
  if (!data.name) {
6057
6163
  continue;
@@ -6108,11 +6214,11 @@ var GraphSyncController = class _GraphSyncController {
6108
6214
  if (!metadata?.actorName) {
6109
6215
  return;
6110
6216
  }
6111
- const serviceName = resolveSyncServiceName(task);
6112
- if (!serviceName) {
6217
+ const serviceName2 = resolveSyncServiceName(task);
6218
+ if (!serviceName2) {
6113
6219
  return;
6114
6220
  }
6115
- const registrationKey = `${metadata.actorName}|${task.name}|${task.version}|${serviceName}`;
6221
+ const registrationKey = `${metadata.actorName}|${task.name}|${task.version}|${serviceName2}`;
6116
6222
  if (this.registeredActorTaskMaps.has(registrationKey)) {
6117
6223
  return;
6118
6224
  }
@@ -6122,7 +6228,7 @@ var GraphSyncController = class _GraphSyncController {
6122
6228
  actor_version: 1,
6123
6229
  task_name: task.name,
6124
6230
  task_version: task.version,
6125
- service_name: serviceName,
6231
+ service_name: serviceName2,
6126
6232
  mode: metadata.mode,
6127
6233
  description: task.description ?? metadata.actorDescription ?? "",
6128
6234
  is_meta: metadata.actorKind === "meta" || task.isMeta === true
@@ -6178,8 +6284,8 @@ var GraphSyncController = class _GraphSyncController {
6178
6284
  function* (ctx) {
6179
6285
  const task = ctx.task;
6180
6286
  if (task.hidden || !task.register) return;
6181
- const serviceName = resolveSyncServiceName(task);
6182
- if (!serviceName) {
6287
+ const serviceName2 = resolveSyncServiceName(task);
6288
+ if (!serviceName2) {
6183
6289
  return;
6184
6290
  }
6185
6291
  for (const signal of task.observedSignals) {
@@ -6192,7 +6298,7 @@ var GraphSyncController = class _GraphSyncController {
6192
6298
  isGlobal,
6193
6299
  taskName: task.name,
6194
6300
  taskVersion: task.version,
6195
- serviceName
6301
+ serviceName: serviceName2
6196
6302
  },
6197
6303
  __taskName: task.name,
6198
6304
  __signal: signal
@@ -6226,7 +6332,7 @@ var GraphSyncController = class _GraphSyncController {
6226
6332
  delayMs: 3e3
6227
6333
  });
6228
6334
  const intents = Array.isArray(ctx.intents) ? ctx.intents : Array.from(CadenzaService.inquiryBroker.intents.values());
6229
- if (resolveSyncServiceName() === "CadenzaDB") {
6335
+ if (resolveSyncServiceName() === "CadenzaDB" && !this.loggedCadenzaDBIntentSweep) {
6230
6336
  const intentNames = intents.map((intent) => String(intent?.name ?? "").trim()).filter(Boolean);
6231
6337
  const authorityIntentNames = intentNames.filter(
6232
6338
  (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")
@@ -6242,6 +6348,7 @@ var GraphSyncController = class _GraphSyncController {
6242
6348
  },
6243
6349
  "info"
6244
6350
  );
6351
+ this.loggedCadenzaDBIntentSweep = true;
6245
6352
  }
6246
6353
  for (const intent of intents) {
6247
6354
  const intentData = buildIntentRegistryData(intent);
@@ -6295,29 +6402,33 @@ var GraphSyncController = class _GraphSyncController {
6295
6402
  function* (ctx) {
6296
6403
  const task = ctx.task;
6297
6404
  if (task.hidden || !task.register) return;
6298
- const serviceName = resolveSyncServiceName(task);
6299
- if (!serviceName) {
6405
+ const serviceName2 = resolveSyncServiceName(task);
6406
+ if (!serviceName2) {
6300
6407
  return;
6301
6408
  }
6302
6409
  task.__registeredIntents = task.__registeredIntents ?? /* @__PURE__ */ new Set();
6303
6410
  task.__invalidMetaIntentWarnings = task.__invalidMetaIntentWarnings ?? /* @__PURE__ */ new Set();
6304
- if (serviceName === "CadenzaDB" && [
6411
+ if (serviceName2 === "CadenzaDB" && [
6305
6412
  "Query service_instance",
6306
6413
  "Query service_instance_transport",
6307
6414
  "Query intent_to_task_map",
6308
6415
  "Query signal_to_task_map"
6309
6416
  ].includes(task.name)) {
6310
- CadenzaService.log(
6311
- "CadenzaDB authority task intent diagnostics.",
6312
- {
6313
- taskName: task.name,
6314
- taskVersion: task.version,
6315
- isMeta: task.isMeta,
6316
- handlesIntents: Array.from(task.handlesIntents ?? []),
6317
- registeredIntents: Array.from(task.__registeredIntents ?? [])
6318
- },
6319
- "info"
6320
- );
6417
+ const authorityTaskKey = `${task.name}:${task.version}`;
6418
+ if (!this.loggedAuthorityTaskIntentDiagnostics.has(authorityTaskKey)) {
6419
+ this.loggedAuthorityTaskIntentDiagnostics.add(authorityTaskKey);
6420
+ CadenzaService.log(
6421
+ "CadenzaDB authority task intent diagnostics.",
6422
+ {
6423
+ taskName: task.name,
6424
+ taskVersion: task.version,
6425
+ isMeta: task.isMeta,
6426
+ handlesIntents: Array.from(task.handlesIntents ?? []),
6427
+ registeredIntents: Array.from(task.__registeredIntents ?? [])
6428
+ },
6429
+ "info"
6430
+ );
6431
+ }
6321
6432
  }
6322
6433
  for (const intent of task.handlesIntents) {
6323
6434
  if (task.__registeredIntents.has(intent)) continue;
@@ -6345,7 +6456,7 @@ var GraphSyncController = class _GraphSyncController {
6345
6456
  intentName: intent,
6346
6457
  taskName: task.name,
6347
6458
  taskVersion: task.version,
6348
- serviceName
6459
+ serviceName: serviceName2
6349
6460
  },
6350
6461
  __taskName: task.name,
6351
6462
  __intent: intent,
@@ -6354,11 +6465,11 @@ var GraphSyncController = class _GraphSyncController {
6354
6465
  intentName: intent,
6355
6466
  taskName: task.name,
6356
6467
  taskVersion: task.version,
6357
- serviceName
6468
+ serviceName: serviceName2
6358
6469
  }
6359
6470
  };
6360
6471
  }
6361
- }
6472
+ }.bind(this)
6362
6473
  ).then(
6363
6474
  CadenzaService.createMetaTask(
6364
6475
  "Prepare intent definition for intent-to-task map",
@@ -6423,8 +6534,8 @@ var GraphSyncController = class _GraphSyncController {
6423
6534
  if (task.taskMapRegistration.has(t.name) || t.hidden || !t.register) {
6424
6535
  continue;
6425
6536
  }
6426
- const serviceName = resolveSyncServiceName(t);
6427
- if (!serviceName) {
6537
+ const serviceName2 = resolveSyncServiceName(t);
6538
+ if (!serviceName2) {
6428
6539
  continue;
6429
6540
  }
6430
6541
  yield {
@@ -6433,7 +6544,7 @@ var GraphSyncController = class _GraphSyncController {
6433
6544
  taskVersion: t.version,
6434
6545
  predecessorTaskName: task.name,
6435
6546
  predecessorTaskVersion: task.version,
6436
- serviceName,
6547
+ serviceName: serviceName2,
6437
6548
  predecessorServiceName
6438
6549
  },
6439
6550
  __taskName: task.name,
@@ -6482,16 +6593,16 @@ var GraphSyncController = class _GraphSyncController {
6482
6593
  if (task.hidden || !task.register) return;
6483
6594
  if (task.isDeputy && !task.signalName) {
6484
6595
  if (task.registeredDeputyMap) return;
6485
- const serviceName = resolveSyncServiceName(task);
6596
+ const serviceName2 = resolveSyncServiceName(task);
6486
6597
  const predecessorServiceName = resolveSyncServiceName();
6487
- if (!serviceName || !predecessorServiceName) {
6598
+ if (!serviceName2 || !predecessorServiceName) {
6488
6599
  return;
6489
6600
  }
6490
6601
  return {
6491
6602
  data: {
6492
6603
  task_name: task.remoteRoutineName,
6493
6604
  task_version: 1,
6494
- service_name: serviceName,
6605
+ service_name: serviceName2,
6495
6606
  predecessor_task_name: task.name,
6496
6607
  predecessor_task_version: task.version,
6497
6608
  predecessor_service_name: predecessorServiceName