@cadenza.io/service 2.17.48 → 2.17.49

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
@@ -9016,6 +9016,31 @@ function resolveSyncQueryTask(isCadenzaDBReady, tableName, queryData = {}, optio
9016
9016
  }
9017
9017
  ).then(targetTask);
9018
9018
  }
9019
+ function getRegistrableTasks() {
9020
+ return Array.from(CadenzaService.registry.tasks.values()).filter(
9021
+ (task) => task.register && !task.isHidden
9022
+ );
9023
+ }
9024
+ function getRegistrableRoutines() {
9025
+ return Array.from(CadenzaService.registry.routines.values());
9026
+ }
9027
+ function getRegistrableSignalObservers() {
9028
+ const signalObservers = CadenzaService.signalBroker.signalObservers;
9029
+ return signalObservers ? Array.from(signalObservers.values()) : [];
9030
+ }
9031
+ function getRegistrableIntentNames() {
9032
+ return Array.from(CadenzaService.inquiryBroker.intents.values()).map((intent) => buildIntentRegistryData(intent)).filter(
9033
+ (intentDefinition) => intentDefinition !== null
9034
+ ).map((intentDefinition) => String(intentDefinition.name));
9035
+ }
9036
+ function buildActorRegistrationKey(actor, serviceName) {
9037
+ const data = buildActorRegistrationData(actor);
9038
+ const name = typeof data.name === "string" && data.name.trim().length > 0 ? data.name.trim() : "";
9039
+ if (!name) {
9040
+ return null;
9041
+ }
9042
+ return `${name}|${data.version}|${serviceName}`;
9043
+ }
9019
9044
  var GraphSyncController = class _GraphSyncController {
9020
9045
  constructor() {
9021
9046
  this.registeredActors = /* @__PURE__ */ new Set();
@@ -9140,22 +9165,158 @@ var GraphSyncController = class _GraphSyncController {
9140
9165
  {},
9141
9166
  { concurrency: 10 }
9142
9167
  );
9168
+ const finalizeTaskSync = (emit, ctx) => {
9169
+ const pendingTasks = getRegistrableTasks().filter((task) => !task.registered);
9170
+ if (pendingTasks.length > 0) {
9171
+ this.tasksSynced = false;
9172
+ return false;
9173
+ }
9174
+ const shouldEmit = !this.tasksSynced;
9175
+ this.tasksSynced = true;
9176
+ if (shouldEmit) {
9177
+ emit("meta.sync_controller.synced_tasks", {
9178
+ __syncing: true,
9179
+ ...ctx
9180
+ });
9181
+ }
9182
+ return true;
9183
+ };
9184
+ const finalizeRoutineSync = (emit, ctx) => {
9185
+ const pendingRoutines = getRegistrableRoutines().filter(
9186
+ (routine) => !routine.registered
9187
+ );
9188
+ if (pendingRoutines.length > 0) {
9189
+ this.routinesSynced = false;
9190
+ return false;
9191
+ }
9192
+ const shouldEmit = !this.routinesSynced;
9193
+ this.routinesSynced = true;
9194
+ if (shouldEmit) {
9195
+ emit("meta.sync_controller.synced_routines", {
9196
+ __syncing: true,
9197
+ ...ctx
9198
+ });
9199
+ }
9200
+ return true;
9201
+ };
9202
+ const finalizeSignalSync = (emit, ctx) => {
9203
+ const pendingSignals = getRegistrableSignalObservers().filter(
9204
+ (observer) => observer?.registered !== true
9205
+ );
9206
+ if (pendingSignals.length > 0) {
9207
+ this.signalsSynced = false;
9208
+ return false;
9209
+ }
9210
+ const shouldEmit = !this.signalsSynced;
9211
+ this.signalsSynced = true;
9212
+ if (shouldEmit) {
9213
+ emit("meta.sync_controller.synced_signals", {
9214
+ __syncing: true,
9215
+ ...ctx
9216
+ });
9217
+ }
9218
+ return true;
9219
+ };
9220
+ const finalizeIntentSync = (emit, ctx) => {
9221
+ const pendingIntentNames = getRegistrableIntentNames().filter(
9222
+ (intentName) => !this.registeredIntentDefinitions.has(intentName)
9223
+ );
9224
+ if (pendingIntentNames.length > 0) {
9225
+ this.intentsSynced = false;
9226
+ return false;
9227
+ }
9228
+ const shouldEmit = !this.intentsSynced;
9229
+ this.intentsSynced = true;
9230
+ if (shouldEmit) {
9231
+ emit("meta.sync_controller.synced_intents", {
9232
+ __syncing: true,
9233
+ ...ctx
9234
+ });
9235
+ }
9236
+ return true;
9237
+ };
9238
+ const finalizeActorSync = (emit, ctx) => {
9239
+ const syncServiceName = resolveSyncServiceName();
9240
+ if (!syncServiceName) {
9241
+ this.actorsSynced = false;
9242
+ return false;
9243
+ }
9244
+ const pendingActorKeys = CadenzaService.getAllActors().map((actor) => buildActorRegistrationKey(actor, syncServiceName)).filter((registrationKey) => Boolean(registrationKey)).filter((registrationKey) => !this.registeredActors.has(registrationKey));
9245
+ if (pendingActorKeys.length > 0) {
9246
+ this.actorsSynced = false;
9247
+ return false;
9248
+ }
9249
+ const shouldEmit = !this.actorsSynced;
9250
+ this.actorsSynced = true;
9251
+ if (shouldEmit) {
9252
+ emit("meta.sync_controller.synced_actors", {
9253
+ __syncing: true,
9254
+ ...ctx
9255
+ });
9256
+ }
9257
+ return true;
9258
+ };
9259
+ const gatherTaskRegistrationTask = CadenzaService.createUniqueMetaTask(
9260
+ "Gather task registration",
9261
+ (ctx, emit) => finalizeTaskSync(emit, ctx),
9262
+ "Completes task registration when all registrable tasks are marked registered.",
9263
+ {
9264
+ register: false,
9265
+ isHidden: true
9266
+ }
9267
+ );
9268
+ const gatherRoutineRegistrationTask = CadenzaService.createUniqueMetaTask(
9269
+ "Gather routine registration",
9270
+ (ctx, emit) => finalizeRoutineSync(emit, ctx),
9271
+ "Completes routine registration when all registrable routines are marked registered.",
9272
+ {
9273
+ register: false,
9274
+ isHidden: true
9275
+ }
9276
+ );
9277
+ const gatherSignalRegistrationTask = CadenzaService.createUniqueMetaTask(
9278
+ "Gather signal registration",
9279
+ (ctx, emit) => finalizeSignalSync(emit, ctx),
9280
+ "Completes signal registration when all signal observers are marked registered.",
9281
+ {
9282
+ register: false,
9283
+ isHidden: true
9284
+ }
9285
+ );
9286
+ const gatherIntentRegistrationTask = CadenzaService.createUniqueMetaTask(
9287
+ "Gather intent registration",
9288
+ (ctx, emit) => finalizeIntentSync(emit, ctx),
9289
+ "Completes intent registration when all registrable intents are marked registered.",
9290
+ {
9291
+ register: false,
9292
+ isHidden: true
9293
+ }
9294
+ );
9295
+ const gatherActorRegistrationTask = CadenzaService.createUniqueMetaTask(
9296
+ "Gather actor registration",
9297
+ (ctx, emit) => finalizeActorSync(emit, ctx),
9298
+ "Completes actor registration when all registrable actors are marked registered.",
9299
+ {
9300
+ register: false,
9301
+ isHidden: true
9302
+ }
9303
+ );
9143
9304
  this.splitRoutinesTask = CadenzaService.createMetaTask(
9144
9305
  "Split routines for registration",
9145
- (ctx, emit) => {
9306
+ function* (ctx) {
9146
9307
  const { routines } = ctx;
9147
- if (!routines) return false;
9308
+ if (!routines) return;
9148
9309
  const serviceName2 = resolveSyncServiceName();
9149
9310
  if (!serviceName2) {
9150
- return false;
9311
+ return;
9151
9312
  }
9152
9313
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
9153
9314
  delayMs: 2e3
9154
9315
  });
9155
- let emittedCount = 0;
9156
9316
  for (const routine of routines) {
9157
9317
  if (routine.registered) continue;
9158
- emit("meta.sync_controller.routine_registration_split", {
9318
+ this.routinesSynced = false;
9319
+ yield {
9159
9320
  __syncing: ctx.__syncing,
9160
9321
  data: {
9161
9322
  name: routine.name,
@@ -9165,13 +9326,11 @@ var GraphSyncController = class _GraphSyncController {
9165
9326
  isMeta: routine.isMeta
9166
9327
  },
9167
9328
  __routineName: routine.name
9168
- });
9169
- emittedCount += 1;
9329
+ };
9170
9330
  }
9171
- return emittedCount > 0;
9172
- }
9331
+ }.bind(this)
9173
9332
  );
9174
- CadenzaService.createMetaTask("Process split routine registration", (ctx) => ctx).doOn("meta.sync_controller.routine_registration_split").then(
9333
+ this.splitRoutinesTask.then(
9175
9334
  resolveSyncInsertTask(
9176
9335
  this.isCadenzaDBReady,
9177
9336
  "routine",
@@ -9193,22 +9352,10 @@ var GraphSyncController = class _GraphSyncController {
9193
9352
  delayMs: 3e3
9194
9353
  });
9195
9354
  CadenzaService.getRoutine(ctx.__routineName).registered = true;
9196
- CadenzaService.debounce(
9197
- "meta.sync_controller.routine_registration_settled",
9198
- { __syncing: true },
9199
- 300
9200
- );
9201
9355
  return true;
9202
- })
9356
+ }).then(gatherRoutineRegistrationTask)
9203
9357
  )
9204
9358
  );
9205
- CadenzaService.createUniqueMetaTask(
9206
- "Gather routine registration",
9207
- () => {
9208
- this.routinesSynced = true;
9209
- return true;
9210
- }
9211
- ).doOn("meta.sync_controller.routine_registration_settled").emits("meta.sync_controller.synced_routines");
9212
9359
  this.splitTasksInRoutines = CadenzaService.createMetaTask(
9213
9360
  "Split tasks in routines",
9214
9361
  function* (ctx) {
@@ -9297,18 +9444,19 @@ var GraphSyncController = class _GraphSyncController {
9297
9444
  }
9298
9445
  this.splitSignalsTask = CadenzaService.createMetaTask(
9299
9446
  "Split signals for registration",
9300
- (ctx, emit) => {
9447
+ function* (ctx) {
9301
9448
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
9302
9449
  delayMs: 3e3
9303
9450
  });
9304
9451
  const { signals } = ctx;
9305
- if (!signals) return false;
9452
+ if (!signals) return;
9306
9453
  const filteredSignals = signals.filter(
9307
9454
  (signal) => !signal.data.registered
9308
9455
  ).map((signal) => signal.signal);
9309
9456
  for (const signal of filteredSignals) {
9310
9457
  const { isMeta, isGlobal, domain, action } = decomposeSignalName(signal);
9311
- emit("meta.sync_controller.signal_registration_split", {
9458
+ this.signalsSynced = false;
9459
+ yield {
9312
9460
  __syncing: ctx.__syncing,
9313
9461
  data: {
9314
9462
  name: signal,
@@ -9318,12 +9466,11 @@ var GraphSyncController = class _GraphSyncController {
9318
9466
  isMeta
9319
9467
  },
9320
9468
  __signal: signal
9321
- });
9469
+ };
9322
9470
  }
9323
- return filteredSignals.length > 0;
9324
- }
9471
+ }.bind(this)
9325
9472
  );
9326
- CadenzaService.createMetaTask("Process split signal registration", (ctx) => ctx).doOn("meta.sync_controller.signal_registration_split").then(
9473
+ this.splitSignalsTask.then(
9327
9474
  resolveSyncInsertTask(
9328
9475
  this.isCadenzaDBReady,
9329
9476
  "signal_registry",
@@ -9344,22 +9491,10 @@ var GraphSyncController = class _GraphSyncController {
9344
9491
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
9345
9492
  delayMs: 3e3
9346
9493
  });
9347
- CadenzaService.debounce(
9348
- "meta.sync_controller.signal_registration_settled",
9349
- { __syncing: true },
9350
- 300
9351
- );
9352
9494
  return { signalName: ctx.__signal };
9353
- }).then(CadenzaService.signalBroker.registerSignalTask)
9495
+ }).then(CadenzaService.signalBroker.registerSignalTask).then(gatherSignalRegistrationTask)
9354
9496
  )
9355
9497
  );
9356
- CadenzaService.createUniqueMetaTask(
9357
- "Gather signal registration",
9358
- () => {
9359
- this.signalsSynced = true;
9360
- return true;
9361
- }
9362
- ).doOn("meta.sync_controller.signal_registration_settled").emits("meta.sync_controller.synced_signals");
9363
9498
  this.splitTasksForRegistration = CadenzaService.createMetaTask(
9364
9499
  "Split tasks for registration",
9365
9500
  function* (ctx) {
@@ -9374,6 +9509,7 @@ var GraphSyncController = class _GraphSyncController {
9374
9509
  for (const task of tasks) {
9375
9510
  if (task.registered) continue;
9376
9511
  const { __functionString, __getTagCallback } = task.export();
9512
+ this.tasksSynced = false;
9377
9513
  if (shouldDebugSyncTaskName(task.name)) {
9378
9514
  logSyncDebug("task_registration_split", {
9379
9515
  taskName: task.name,
@@ -9422,7 +9558,7 @@ var GraphSyncController = class _GraphSyncController {
9422
9558
  __taskName: task.name
9423
9559
  };
9424
9560
  }
9425
- }
9561
+ }.bind(this)
9426
9562
  );
9427
9563
  const registerTaskTask = resolveSyncInsertTask(
9428
9564
  this.isCadenzaDBReady,
@@ -9456,13 +9592,8 @@ var GraphSyncController = class _GraphSyncController {
9456
9592
  ...ctx,
9457
9593
  task: CadenzaService.get(ctx.__taskName)
9458
9594
  });
9459
- CadenzaService.debounce(
9460
- "meta.sync_controller.task_registration_settled",
9461
- { __syncing: true },
9462
- 300
9463
- );
9464
9595
  return true;
9465
- })
9596
+ }).then(gatherTaskRegistrationTask)
9466
9597
  );
9467
9598
  if (registerTaskTask) {
9468
9599
  this.splitTasksForRegistration.then(registerTaskTask);
@@ -9495,13 +9626,6 @@ var GraphSyncController = class _GraphSyncController {
9495
9626
  isHidden: true
9496
9627
  }
9497
9628
  ).doOn("meta.task.created").then(this.splitTasksForRegistration);
9498
- CadenzaService.createUniqueMetaTask(
9499
- "Gather task registration",
9500
- () => {
9501
- this.tasksSynced = true;
9502
- return true;
9503
- }
9504
- ).doOn("meta.sync_controller.task_registration_settled").emits("meta.sync_controller.synced_tasks");
9505
9629
  this.splitActorsForRegistration = CadenzaService.createMetaTask(
9506
9630
  "Split actors for registration",
9507
9631
  function* (ctx) {
@@ -9525,6 +9649,7 @@ var GraphSyncController = class _GraphSyncController {
9525
9649
  if (this.registeredActors.has(registrationKey)) {
9526
9650
  continue;
9527
9651
  }
9652
+ this.actorsSynced = false;
9528
9653
  yield {
9529
9654
  data,
9530
9655
  __actorRegistrationKey: registrationKey
@@ -9553,22 +9678,10 @@ var GraphSyncController = class _GraphSyncController {
9553
9678
  delayMs: 3e3
9554
9679
  });
9555
9680
  this.registeredActors.add(ctx.__actorRegistrationKey);
9556
- CadenzaService.debounce(
9557
- "meta.sync_controller.actor_registration_settled",
9558
- { __syncing: true },
9559
- 300
9560
- );
9561
9681
  return true;
9562
- })
9682
+ }).then(gatherActorRegistrationTask)
9563
9683
  )
9564
9684
  );
9565
- CadenzaService.createUniqueMetaTask(
9566
- "Gather actor registration",
9567
- () => {
9568
- this.actorsSynced = true;
9569
- return true;
9570
- }
9571
- ).doOn("meta.sync_controller.actor_registration_settled").emits("meta.sync_controller.synced_actors");
9572
9685
  this.registerActorTaskMapTask = CadenzaService.createMetaTask(
9573
9686
  "Split actor task maps",
9574
9687
  function* (ctx) {
@@ -9715,12 +9828,11 @@ var GraphSyncController = class _GraphSyncController {
9715
9828
  );
9716
9829
  this.splitIntentsTask = CadenzaService.createMetaTask(
9717
9830
  "Split intents for registration",
9718
- function(ctx, emit) {
9831
+ function* (ctx) {
9719
9832
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
9720
9833
  delayMs: 3e3
9721
9834
  });
9722
9835
  const intents = Array.isArray(ctx.intents) ? ctx.intents : Array.from(CadenzaService.inquiryBroker.intents.values());
9723
- let emittedCount = 0;
9724
9836
  for (const intent of intents) {
9725
9837
  const intentData = buildIntentRegistryData(intent);
9726
9838
  if (!intentData) {
@@ -9729,17 +9841,16 @@ var GraphSyncController = class _GraphSyncController {
9729
9841
  if (this.registeredIntentDefinitions.has(intentData.name)) {
9730
9842
  continue;
9731
9843
  }
9732
- emit("meta.sync_controller.intent_registration_split", {
9844
+ this.intentsSynced = false;
9845
+ yield {
9733
9846
  __syncing: ctx.__syncing,
9734
9847
  data: intentData,
9735
9848
  __intentName: intentData.name
9736
- });
9737
- emittedCount += 1;
9849
+ };
9738
9850
  }
9739
- return emittedCount > 0;
9740
9851
  }.bind(this)
9741
9852
  );
9742
- CadenzaService.createMetaTask("Process split intent registration", (ctx) => ctx).doOn("meta.sync_controller.intent_registration_split").then(
9853
+ this.splitIntentsTask.then(
9743
9854
  insertIntentRegistryTask?.then(
9744
9855
  CadenzaService.createMetaTask("Record intent definition registration", (ctx) => {
9745
9856
  if (!didSyncInsertSucceed(ctx)) {
@@ -9749,22 +9860,10 @@ var GraphSyncController = class _GraphSyncController {
9749
9860
  delayMs: 3e3
9750
9861
  });
9751
9862
  this.registeredIntentDefinitions.add(ctx.__intentName);
9752
- CadenzaService.debounce(
9753
- "meta.sync_controller.intent_registration_settled",
9754
- { __syncing: true },
9755
- 300
9756
- );
9757
9863
  return true;
9758
- })
9864
+ }).then(gatherIntentRegistrationTask)
9759
9865
  )
9760
9866
  );
9761
- CadenzaService.createUniqueMetaTask(
9762
- "Gather intent registration",
9763
- () => {
9764
- this.intentsSynced = true;
9765
- return true;
9766
- }
9767
- ).doOn("meta.sync_controller.intent_registration_settled").emits("meta.sync_controller.synced_intents");
9768
9867
  const registerIntentTask = CadenzaService.createMetaTask(
9769
9868
  "Record intent registration",
9770
9869
  (ctx) => {
@@ -10058,15 +10157,11 @@ var GraphSyncController = class _GraphSyncController {
10058
10157
  __authoritativeReconciliation: true
10059
10158
  });
10060
10159
  }
10061
- if (authoritativeTasks.length > 0) {
10062
- CadenzaService.debounce(
10063
- "meta.sync_controller.task_registration_settled",
10064
- {
10065
- __syncing: true,
10066
- __authoritativeReconciliation: true
10067
- },
10068
- 300
10069
- );
10160
+ if (authoritativeTasks.length > 0 || changed) {
10161
+ finalizeTaskSync(emit, {
10162
+ ...ctx,
10163
+ __authoritativeReconciliation: true
10164
+ });
10070
10165
  }
10071
10166
  return changed;
10072
10167
  },
@@ -10078,7 +10173,7 @@ var GraphSyncController = class _GraphSyncController {
10078
10173
  );
10079
10174
  const reconcileRoutineRegistrationFromAuthorityTask = CadenzaService.createMetaTask(
10080
10175
  "Reconcile routine registration from authority",
10081
- (ctx) => {
10176
+ (ctx, emit) => {
10082
10177
  const authoritativeRoutines = resolveSyncQueryRows(ctx, "routine");
10083
10178
  let changed = false;
10084
10179
  for (const row of authoritativeRoutines) {
@@ -10093,15 +10188,11 @@ var GraphSyncController = class _GraphSyncController {
10093
10188
  routine.registered = true;
10094
10189
  changed = true;
10095
10190
  }
10096
- if (authoritativeRoutines.length > 0) {
10097
- CadenzaService.debounce(
10098
- "meta.sync_controller.routine_registration_settled",
10099
- {
10100
- __syncing: true,
10101
- __authoritativeReconciliation: true
10102
- },
10103
- 300
10104
- );
10191
+ if (authoritativeRoutines.length > 0 || changed) {
10192
+ finalizeRoutineSync(emit, {
10193
+ ...ctx,
10194
+ __authoritativeReconciliation: true
10195
+ });
10105
10196
  }
10106
10197
  return changed;
10107
10198
  },
@@ -10113,7 +10204,7 @@ var GraphSyncController = class _GraphSyncController {
10113
10204
  );
10114
10205
  const reconcileSignalRegistrationFromAuthorityTask = CadenzaService.createMetaTask(
10115
10206
  "Reconcile signal registration from authority",
10116
- (ctx) => {
10207
+ (ctx, emit) => {
10117
10208
  const authoritativeSignals = resolveSyncQueryRows(ctx, "signal_registry");
10118
10209
  const signalObservers = CadenzaService.signalBroker.signalObservers;
10119
10210
  let changed = false;
@@ -10129,15 +10220,11 @@ var GraphSyncController = class _GraphSyncController {
10129
10220
  observer.registered = true;
10130
10221
  changed = true;
10131
10222
  }
10132
- if (authoritativeSignals.length > 0) {
10133
- CadenzaService.debounce(
10134
- "meta.sync_controller.signal_registration_settled",
10135
- {
10136
- __syncing: true,
10137
- __authoritativeReconciliation: true
10138
- },
10139
- 300
10140
- );
10223
+ if (authoritativeSignals.length > 0 || changed) {
10224
+ finalizeSignalSync(emit, {
10225
+ ...ctx,
10226
+ __authoritativeReconciliation: true
10227
+ });
10141
10228
  }
10142
10229
  return changed;
10143
10230
  },
@@ -10149,7 +10236,7 @@ var GraphSyncController = class _GraphSyncController {
10149
10236
  );
10150
10237
  const reconcileIntentRegistrationFromAuthorityTask = CadenzaService.createMetaTask(
10151
10238
  "Reconcile intent registration from authority",
10152
- (ctx) => {
10239
+ (ctx, emit) => {
10153
10240
  const authoritativeIntents = resolveSyncQueryRows(ctx, "intent_registry");
10154
10241
  let changed = false;
10155
10242
  for (const row of authoritativeIntents) {
@@ -10163,15 +10250,11 @@ var GraphSyncController = class _GraphSyncController {
10163
10250
  this.registeredIntentDefinitions.add(intentName);
10164
10251
  changed = true;
10165
10252
  }
10166
- if (authoritativeIntents.length > 0) {
10167
- CadenzaService.debounce(
10168
- "meta.sync_controller.intent_registration_settled",
10169
- {
10170
- __syncing: true,
10171
- __authoritativeReconciliation: true
10172
- },
10173
- 300
10174
- );
10253
+ if (authoritativeIntents.length > 0 || changed) {
10254
+ finalizeIntentSync(emit, {
10255
+ ...ctx,
10256
+ __authoritativeReconciliation: true
10257
+ });
10175
10258
  }
10176
10259
  return changed;
10177
10260
  },