@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.
@@ -5714,6 +5714,12 @@ var import_uuid4 = require("uuid");
5714
5714
 
5715
5715
  // src/graph/controllers/GraphSyncController.ts
5716
5716
  var ACTOR_TASK_METADATA = /* @__PURE__ */ Symbol.for("@cadenza.io/core/actor-task-meta");
5717
+ var LOCAL_SYNC_QUERY_DATA = /* @__PURE__ */ Symbol.for(
5718
+ "@cadenza.io/service/local-sync-query-data"
5719
+ );
5720
+ var LOCAL_SYNC_ORIGINAL_TASK_FUNCTION = /* @__PURE__ */ Symbol.for(
5721
+ "@cadenza.io/service/local-sync-original-task-function"
5722
+ );
5717
5723
  function getActorTaskRuntimeMetadata(taskFunction) {
5718
5724
  if (typeof taskFunction !== "function") {
5719
5725
  return void 0;
@@ -5791,20 +5797,120 @@ function buildIntentRegistryData(intent) {
5791
5797
  };
5792
5798
  }
5793
5799
  function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, options = {}) {
5794
- return CadenzaService.getLocalCadenzaDBInsertTask(tableName) ?? (isCadenzaDBReady ? CadenzaService.createCadenzaDBInsertTask(tableName, queryData, options) : void 0);
5800
+ const localInsertTask = CadenzaService.getLocalCadenzaDBInsertTask(tableName);
5801
+ if (localInsertTask) {
5802
+ const taskWithSyncQueryData = localInsertTask;
5803
+ taskWithSyncQueryData[LOCAL_SYNC_QUERY_DATA] = {
5804
+ ...taskWithSyncQueryData[LOCAL_SYNC_QUERY_DATA] ?? {},
5805
+ ...queryData
5806
+ };
5807
+ if (!taskWithSyncQueryData[LOCAL_SYNC_ORIGINAL_TASK_FUNCTION]) {
5808
+ taskWithSyncQueryData[LOCAL_SYNC_ORIGINAL_TASK_FUNCTION] = taskWithSyncQueryData.taskFunction;
5809
+ taskWithSyncQueryData.taskFunction = (ctx, emit, inquire, progressCallback) => {
5810
+ const existingQueryData = ctx.queryData && typeof ctx.queryData === "object" ? ctx.queryData : {};
5811
+ const syncQueryData = taskWithSyncQueryData[LOCAL_SYNC_QUERY_DATA] ?? {};
5812
+ const nextContext = ctx.__syncing ? {
5813
+ ...ctx,
5814
+ queryData: {
5815
+ ...existingQueryData,
5816
+ ...syncQueryData
5817
+ }
5818
+ } : ctx;
5819
+ return taskWithSyncQueryData[LOCAL_SYNC_ORIGINAL_TASK_FUNCTION](
5820
+ nextContext,
5821
+ emit,
5822
+ inquire,
5823
+ progressCallback
5824
+ );
5825
+ };
5826
+ }
5827
+ return localInsertTask;
5828
+ }
5829
+ return isCadenzaDBReady ? CadenzaService.createCadenzaDBInsertTask(tableName, queryData, options) : void 0;
5795
5830
  }
5831
+ var CADENZA_DB_REQUIRED_LOCAL_SYNC_INSERT_TABLES = [
5832
+ "intent_registry",
5833
+ "routine",
5834
+ "task_to_routine_map",
5835
+ "signal_registry",
5836
+ "task",
5837
+ "actor",
5838
+ "actor_task_map",
5839
+ "signal_to_task_map",
5840
+ "intent_to_task_map",
5841
+ "directional_task_graph_map"
5842
+ ];
5796
5843
  var GraphSyncController = class _GraphSyncController {
5797
5844
  constructor() {
5798
5845
  this.registeredActors = /* @__PURE__ */ new Set();
5799
5846
  this.registeredActorTaskMaps = /* @__PURE__ */ new Set();
5800
5847
  this.registeredIntentDefinitions = /* @__PURE__ */ new Set();
5848
+ this.loggedAuthorityTaskIntentDiagnostics = /* @__PURE__ */ new Set();
5801
5849
  this.isCadenzaDBReady = false;
5850
+ this.initialized = false;
5851
+ this.initRetryScheduled = false;
5852
+ this.loggedCadenzaDBIntentSweep = false;
5853
+ this.lastMissingLocalCadenzaDBInsertTablesKey = "";
5802
5854
  }
5803
5855
  static get instance() {
5804
5856
  if (!this._instance) this._instance = new _GraphSyncController();
5805
5857
  return this._instance;
5806
5858
  }
5859
+ getMissingLocalCadenzaDBInsertTables() {
5860
+ return CADENZA_DB_REQUIRED_LOCAL_SYNC_INSERT_TABLES.filter(
5861
+ (tableName) => !CadenzaService.getLocalCadenzaDBInsertTask(tableName)
5862
+ );
5863
+ }
5864
+ ensureRetryInitTask() {
5865
+ if (this.initRetryTask) {
5866
+ return this.initRetryTask;
5867
+ }
5868
+ this.initRetryTask = CadenzaService.get("Retry graph sync init") ?? CadenzaService.createUniqueMetaTask(
5869
+ "Retry graph sync init",
5870
+ () => {
5871
+ this.initRetryScheduled = false;
5872
+ this.init();
5873
+ return true;
5874
+ },
5875
+ "Retries graph sync controller initialization once local authority tasks exist."
5876
+ ).doOn("meta.sync_controller.init_retry");
5877
+ return this.initRetryTask;
5878
+ }
5807
5879
  init() {
5880
+ if (this.initialized) {
5881
+ return;
5882
+ }
5883
+ const serviceName = resolveSyncServiceName();
5884
+ if (serviceName === "CadenzaDB") {
5885
+ const missingLocalInsertTables = this.getMissingLocalCadenzaDBInsertTables();
5886
+ if (missingLocalInsertTables.length > 0) {
5887
+ this.ensureRetryInitTask();
5888
+ const missingKey = missingLocalInsertTables.join(",");
5889
+ if (missingKey !== this.lastMissingLocalCadenzaDBInsertTablesKey) {
5890
+ this.lastMissingLocalCadenzaDBInsertTablesKey = missingKey;
5891
+ CadenzaService.log(
5892
+ "Waiting for local CadenzaDB sync insert tasks before initializing graph sync controller.",
5893
+ {
5894
+ missingLocalInsertTables
5895
+ },
5896
+ "info"
5897
+ );
5898
+ }
5899
+ if (!this.initRetryScheduled) {
5900
+ this.initRetryScheduled = true;
5901
+ CadenzaService.schedule(
5902
+ "meta.sync_controller.init_retry",
5903
+ {
5904
+ __missingLocalInsertTables: missingLocalInsertTables
5905
+ },
5906
+ 250
5907
+ );
5908
+ }
5909
+ return;
5910
+ }
5911
+ this.lastMissingLocalCadenzaDBInsertTablesKey = "";
5912
+ }
5913
+ this.initialized = true;
5808
5914
  const insertIntentRegistryTask = resolveSyncInsertTask(
5809
5915
  this.isCadenzaDBReady,
5810
5916
  "intent_registry",
@@ -5823,8 +5929,8 @@ var GraphSyncController = class _GraphSyncController {
5823
5929
  async function* (ctx, emit) {
5824
5930
  const { routines } = ctx;
5825
5931
  if (!routines) return;
5826
- const serviceName = resolveSyncServiceName();
5827
- if (!serviceName) {
5932
+ const serviceName2 = resolveSyncServiceName();
5933
+ if (!serviceName2) {
5828
5934
  return;
5829
5935
  }
5830
5936
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
@@ -5837,7 +5943,7 @@ var GraphSyncController = class _GraphSyncController {
5837
5943
  name: routine.name,
5838
5944
  version: routine.version,
5839
5945
  description: routine.description,
5840
- serviceName,
5946
+ serviceName: serviceName2,
5841
5947
  isMeta: routine.isMeta
5842
5948
  },
5843
5949
  __routineName: routine.name
@@ -5880,8 +5986,8 @@ var GraphSyncController = class _GraphSyncController {
5880
5986
  function* (ctx) {
5881
5987
  const { routines } = ctx;
5882
5988
  if (!routines) return;
5883
- const serviceName = resolveSyncServiceName();
5884
- if (!serviceName) {
5989
+ const serviceName2 = resolveSyncServiceName();
5990
+ if (!serviceName2) {
5885
5991
  return;
5886
5992
  }
5887
5993
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
@@ -5903,7 +6009,7 @@ var GraphSyncController = class _GraphSyncController {
5903
6009
  taskVersion: nextTask.version,
5904
6010
  routineName: routine.name,
5905
6011
  routineVersion: routine.version,
5906
- serviceName
6012
+ serviceName: serviceName2
5907
6013
  },
5908
6014
  __routineName: routine.name,
5909
6015
  __taskName: nextTask.name
@@ -6008,8 +6114,8 @@ var GraphSyncController = class _GraphSyncController {
6008
6114
  delayMs: 3e3
6009
6115
  });
6010
6116
  const tasks = ctx.tasks;
6011
- const serviceName = resolveSyncServiceName();
6012
- if (!serviceName) {
6117
+ const serviceName2 = resolveSyncServiceName();
6118
+ if (!serviceName2) {
6013
6119
  return;
6014
6120
  }
6015
6121
  for (const task of tasks) {
@@ -6041,7 +6147,7 @@ var GraphSyncController = class _GraphSyncController {
6041
6147
  retryDelay: task.retryDelay,
6042
6148
  retryDelayMax: task.retryDelayMax,
6043
6149
  retryDelayFactor: task.retryDelayFactor,
6044
- service_name: serviceName,
6150
+ service_name: serviceName2,
6045
6151
  signals: {
6046
6152
  emits: Array.from(task.emitsSignals),
6047
6153
  signalsToEmitAfter: Array.from(task.signalsToEmitAfter),
@@ -6094,15 +6200,15 @@ var GraphSyncController = class _GraphSyncController {
6094
6200
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
6095
6201
  delayMs: 3e3
6096
6202
  });
6097
- const serviceName = resolveSyncServiceName();
6098
- if (!serviceName) {
6203
+ const serviceName2 = resolveSyncServiceName();
6204
+ if (!serviceName2) {
6099
6205
  return;
6100
6206
  }
6101
6207
  const actors = ctx.actors ?? [];
6102
6208
  for (const actor of actors) {
6103
6209
  const data = {
6104
6210
  ...buildActorRegistrationData(actor),
6105
- service_name: serviceName
6211
+ service_name: serviceName2
6106
6212
  };
6107
6213
  if (!data.name) {
6108
6214
  continue;
@@ -6159,11 +6265,11 @@ var GraphSyncController = class _GraphSyncController {
6159
6265
  if (!metadata?.actorName) {
6160
6266
  return;
6161
6267
  }
6162
- const serviceName = resolveSyncServiceName(task);
6163
- if (!serviceName) {
6268
+ const serviceName2 = resolveSyncServiceName(task);
6269
+ if (!serviceName2) {
6164
6270
  return;
6165
6271
  }
6166
- const registrationKey = `${metadata.actorName}|${task.name}|${task.version}|${serviceName}`;
6272
+ const registrationKey = `${metadata.actorName}|${task.name}|${task.version}|${serviceName2}`;
6167
6273
  if (this.registeredActorTaskMaps.has(registrationKey)) {
6168
6274
  return;
6169
6275
  }
@@ -6173,7 +6279,7 @@ var GraphSyncController = class _GraphSyncController {
6173
6279
  actor_version: 1,
6174
6280
  task_name: task.name,
6175
6281
  task_version: task.version,
6176
- service_name: serviceName,
6282
+ service_name: serviceName2,
6177
6283
  mode: metadata.mode,
6178
6284
  description: task.description ?? metadata.actorDescription ?? "",
6179
6285
  is_meta: metadata.actorKind === "meta" || task.isMeta === true
@@ -6229,8 +6335,8 @@ var GraphSyncController = class _GraphSyncController {
6229
6335
  function* (ctx) {
6230
6336
  const task = ctx.task;
6231
6337
  if (task.hidden || !task.register) return;
6232
- const serviceName = resolveSyncServiceName(task);
6233
- if (!serviceName) {
6338
+ const serviceName2 = resolveSyncServiceName(task);
6339
+ if (!serviceName2) {
6234
6340
  return;
6235
6341
  }
6236
6342
  for (const signal of task.observedSignals) {
@@ -6243,7 +6349,7 @@ var GraphSyncController = class _GraphSyncController {
6243
6349
  isGlobal,
6244
6350
  taskName: task.name,
6245
6351
  taskVersion: task.version,
6246
- serviceName
6352
+ serviceName: serviceName2
6247
6353
  },
6248
6354
  __taskName: task.name,
6249
6355
  __signal: signal
@@ -6277,7 +6383,7 @@ var GraphSyncController = class _GraphSyncController {
6277
6383
  delayMs: 3e3
6278
6384
  });
6279
6385
  const intents = Array.isArray(ctx.intents) ? ctx.intents : Array.from(CadenzaService.inquiryBroker.intents.values());
6280
- if (resolveSyncServiceName() === "CadenzaDB") {
6386
+ if (resolveSyncServiceName() === "CadenzaDB" && !this.loggedCadenzaDBIntentSweep) {
6281
6387
  const intentNames = intents.map((intent) => String(intent?.name ?? "").trim()).filter(Boolean);
6282
6388
  const authorityIntentNames = intentNames.filter(
6283
6389
  (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")
@@ -6293,6 +6399,7 @@ var GraphSyncController = class _GraphSyncController {
6293
6399
  },
6294
6400
  "info"
6295
6401
  );
6402
+ this.loggedCadenzaDBIntentSweep = true;
6296
6403
  }
6297
6404
  for (const intent of intents) {
6298
6405
  const intentData = buildIntentRegistryData(intent);
@@ -6346,29 +6453,33 @@ var GraphSyncController = class _GraphSyncController {
6346
6453
  function* (ctx) {
6347
6454
  const task = ctx.task;
6348
6455
  if (task.hidden || !task.register) return;
6349
- const serviceName = resolveSyncServiceName(task);
6350
- if (!serviceName) {
6456
+ const serviceName2 = resolveSyncServiceName(task);
6457
+ if (!serviceName2) {
6351
6458
  return;
6352
6459
  }
6353
6460
  task.__registeredIntents = task.__registeredIntents ?? /* @__PURE__ */ new Set();
6354
6461
  task.__invalidMetaIntentWarnings = task.__invalidMetaIntentWarnings ?? /* @__PURE__ */ new Set();
6355
- if (serviceName === "CadenzaDB" && [
6462
+ if (serviceName2 === "CadenzaDB" && [
6356
6463
  "Query service_instance",
6357
6464
  "Query service_instance_transport",
6358
6465
  "Query intent_to_task_map",
6359
6466
  "Query signal_to_task_map"
6360
6467
  ].includes(task.name)) {
6361
- CadenzaService.log(
6362
- "CadenzaDB authority task intent diagnostics.",
6363
- {
6364
- taskName: task.name,
6365
- taskVersion: task.version,
6366
- isMeta: task.isMeta,
6367
- handlesIntents: Array.from(task.handlesIntents ?? []),
6368
- registeredIntents: Array.from(task.__registeredIntents ?? [])
6369
- },
6370
- "info"
6371
- );
6468
+ const authorityTaskKey = `${task.name}:${task.version}`;
6469
+ if (!this.loggedAuthorityTaskIntentDiagnostics.has(authorityTaskKey)) {
6470
+ this.loggedAuthorityTaskIntentDiagnostics.add(authorityTaskKey);
6471
+ CadenzaService.log(
6472
+ "CadenzaDB authority task intent diagnostics.",
6473
+ {
6474
+ taskName: task.name,
6475
+ taskVersion: task.version,
6476
+ isMeta: task.isMeta,
6477
+ handlesIntents: Array.from(task.handlesIntents ?? []),
6478
+ registeredIntents: Array.from(task.__registeredIntents ?? [])
6479
+ },
6480
+ "info"
6481
+ );
6482
+ }
6372
6483
  }
6373
6484
  for (const intent of task.handlesIntents) {
6374
6485
  if (task.__registeredIntents.has(intent)) continue;
@@ -6396,7 +6507,7 @@ var GraphSyncController = class _GraphSyncController {
6396
6507
  intentName: intent,
6397
6508
  taskName: task.name,
6398
6509
  taskVersion: task.version,
6399
- serviceName
6510
+ serviceName: serviceName2
6400
6511
  },
6401
6512
  __taskName: task.name,
6402
6513
  __intent: intent,
@@ -6405,11 +6516,11 @@ var GraphSyncController = class _GraphSyncController {
6405
6516
  intentName: intent,
6406
6517
  taskName: task.name,
6407
6518
  taskVersion: task.version,
6408
- serviceName
6519
+ serviceName: serviceName2
6409
6520
  }
6410
6521
  };
6411
6522
  }
6412
- }
6523
+ }.bind(this)
6413
6524
  ).then(
6414
6525
  CadenzaService.createMetaTask(
6415
6526
  "Prepare intent definition for intent-to-task map",
@@ -6474,8 +6585,8 @@ var GraphSyncController = class _GraphSyncController {
6474
6585
  if (task.taskMapRegistration.has(t.name) || t.hidden || !t.register) {
6475
6586
  continue;
6476
6587
  }
6477
- const serviceName = resolveSyncServiceName(t);
6478
- if (!serviceName) {
6588
+ const serviceName2 = resolveSyncServiceName(t);
6589
+ if (!serviceName2) {
6479
6590
  continue;
6480
6591
  }
6481
6592
  yield {
@@ -6484,7 +6595,7 @@ var GraphSyncController = class _GraphSyncController {
6484
6595
  taskVersion: t.version,
6485
6596
  predecessorTaskName: task.name,
6486
6597
  predecessorTaskVersion: task.version,
6487
- serviceName,
6598
+ serviceName: serviceName2,
6488
6599
  predecessorServiceName
6489
6600
  },
6490
6601
  __taskName: task.name,
@@ -6533,16 +6644,16 @@ var GraphSyncController = class _GraphSyncController {
6533
6644
  if (task.hidden || !task.register) return;
6534
6645
  if (task.isDeputy && !task.signalName) {
6535
6646
  if (task.registeredDeputyMap) return;
6536
- const serviceName = resolveSyncServiceName(task);
6647
+ const serviceName2 = resolveSyncServiceName(task);
6537
6648
  const predecessorServiceName = resolveSyncServiceName();
6538
- if (!serviceName || !predecessorServiceName) {
6649
+ if (!serviceName2 || !predecessorServiceName) {
6539
6650
  return;
6540
6651
  }
6541
6652
  return {
6542
6653
  data: {
6543
6654
  task_name: task.remoteRoutineName,
6544
6655
  task_version: 1,
6545
- service_name: serviceName,
6656
+ service_name: serviceName2,
6546
6657
  predecessor_task_name: task.name,
6547
6658
  predecessor_task_version: task.version,
6548
6659
  predecessor_service_name: predecessorServiceName