@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.mjs CHANGED
@@ -8967,6 +8967,31 @@ function resolveSyncQueryTask(isCadenzaDBReady, tableName, queryData = {}, optio
8967
8967
  }
8968
8968
  ).then(targetTask);
8969
8969
  }
8970
+ function getRegistrableTasks() {
8971
+ return Array.from(CadenzaService.registry.tasks.values()).filter(
8972
+ (task) => task.register && !task.isHidden
8973
+ );
8974
+ }
8975
+ function getRegistrableRoutines() {
8976
+ return Array.from(CadenzaService.registry.routines.values());
8977
+ }
8978
+ function getRegistrableSignalObservers() {
8979
+ const signalObservers = CadenzaService.signalBroker.signalObservers;
8980
+ return signalObservers ? Array.from(signalObservers.values()) : [];
8981
+ }
8982
+ function getRegistrableIntentNames() {
8983
+ return Array.from(CadenzaService.inquiryBroker.intents.values()).map((intent) => buildIntentRegistryData(intent)).filter(
8984
+ (intentDefinition) => intentDefinition !== null
8985
+ ).map((intentDefinition) => String(intentDefinition.name));
8986
+ }
8987
+ function buildActorRegistrationKey(actor, serviceName) {
8988
+ const data = buildActorRegistrationData(actor);
8989
+ const name = typeof data.name === "string" && data.name.trim().length > 0 ? data.name.trim() : "";
8990
+ if (!name) {
8991
+ return null;
8992
+ }
8993
+ return `${name}|${data.version}|${serviceName}`;
8994
+ }
8970
8995
  var GraphSyncController = class _GraphSyncController {
8971
8996
  constructor() {
8972
8997
  this.registeredActors = /* @__PURE__ */ new Set();
@@ -9091,22 +9116,158 @@ var GraphSyncController = class _GraphSyncController {
9091
9116
  {},
9092
9117
  { concurrency: 10 }
9093
9118
  );
9119
+ const finalizeTaskSync = (emit, ctx) => {
9120
+ const pendingTasks = getRegistrableTasks().filter((task) => !task.registered);
9121
+ if (pendingTasks.length > 0) {
9122
+ this.tasksSynced = false;
9123
+ return false;
9124
+ }
9125
+ const shouldEmit = !this.tasksSynced;
9126
+ this.tasksSynced = true;
9127
+ if (shouldEmit) {
9128
+ emit("meta.sync_controller.synced_tasks", {
9129
+ __syncing: true,
9130
+ ...ctx
9131
+ });
9132
+ }
9133
+ return true;
9134
+ };
9135
+ const finalizeRoutineSync = (emit, ctx) => {
9136
+ const pendingRoutines = getRegistrableRoutines().filter(
9137
+ (routine) => !routine.registered
9138
+ );
9139
+ if (pendingRoutines.length > 0) {
9140
+ this.routinesSynced = false;
9141
+ return false;
9142
+ }
9143
+ const shouldEmit = !this.routinesSynced;
9144
+ this.routinesSynced = true;
9145
+ if (shouldEmit) {
9146
+ emit("meta.sync_controller.synced_routines", {
9147
+ __syncing: true,
9148
+ ...ctx
9149
+ });
9150
+ }
9151
+ return true;
9152
+ };
9153
+ const finalizeSignalSync = (emit, ctx) => {
9154
+ const pendingSignals = getRegistrableSignalObservers().filter(
9155
+ (observer) => observer?.registered !== true
9156
+ );
9157
+ if (pendingSignals.length > 0) {
9158
+ this.signalsSynced = false;
9159
+ return false;
9160
+ }
9161
+ const shouldEmit = !this.signalsSynced;
9162
+ this.signalsSynced = true;
9163
+ if (shouldEmit) {
9164
+ emit("meta.sync_controller.synced_signals", {
9165
+ __syncing: true,
9166
+ ...ctx
9167
+ });
9168
+ }
9169
+ return true;
9170
+ };
9171
+ const finalizeIntentSync = (emit, ctx) => {
9172
+ const pendingIntentNames = getRegistrableIntentNames().filter(
9173
+ (intentName) => !this.registeredIntentDefinitions.has(intentName)
9174
+ );
9175
+ if (pendingIntentNames.length > 0) {
9176
+ this.intentsSynced = false;
9177
+ return false;
9178
+ }
9179
+ const shouldEmit = !this.intentsSynced;
9180
+ this.intentsSynced = true;
9181
+ if (shouldEmit) {
9182
+ emit("meta.sync_controller.synced_intents", {
9183
+ __syncing: true,
9184
+ ...ctx
9185
+ });
9186
+ }
9187
+ return true;
9188
+ };
9189
+ const finalizeActorSync = (emit, ctx) => {
9190
+ const syncServiceName = resolveSyncServiceName();
9191
+ if (!syncServiceName) {
9192
+ this.actorsSynced = false;
9193
+ return false;
9194
+ }
9195
+ const pendingActorKeys = CadenzaService.getAllActors().map((actor) => buildActorRegistrationKey(actor, syncServiceName)).filter((registrationKey) => Boolean(registrationKey)).filter((registrationKey) => !this.registeredActors.has(registrationKey));
9196
+ if (pendingActorKeys.length > 0) {
9197
+ this.actorsSynced = false;
9198
+ return false;
9199
+ }
9200
+ const shouldEmit = !this.actorsSynced;
9201
+ this.actorsSynced = true;
9202
+ if (shouldEmit) {
9203
+ emit("meta.sync_controller.synced_actors", {
9204
+ __syncing: true,
9205
+ ...ctx
9206
+ });
9207
+ }
9208
+ return true;
9209
+ };
9210
+ const gatherTaskRegistrationTask = CadenzaService.createUniqueMetaTask(
9211
+ "Gather task registration",
9212
+ (ctx, emit) => finalizeTaskSync(emit, ctx),
9213
+ "Completes task registration when all registrable tasks are marked registered.",
9214
+ {
9215
+ register: false,
9216
+ isHidden: true
9217
+ }
9218
+ );
9219
+ const gatherRoutineRegistrationTask = CadenzaService.createUniqueMetaTask(
9220
+ "Gather routine registration",
9221
+ (ctx, emit) => finalizeRoutineSync(emit, ctx),
9222
+ "Completes routine registration when all registrable routines are marked registered.",
9223
+ {
9224
+ register: false,
9225
+ isHidden: true
9226
+ }
9227
+ );
9228
+ const gatherSignalRegistrationTask = CadenzaService.createUniqueMetaTask(
9229
+ "Gather signal registration",
9230
+ (ctx, emit) => finalizeSignalSync(emit, ctx),
9231
+ "Completes signal registration when all signal observers are marked registered.",
9232
+ {
9233
+ register: false,
9234
+ isHidden: true
9235
+ }
9236
+ );
9237
+ const gatherIntentRegistrationTask = CadenzaService.createUniqueMetaTask(
9238
+ "Gather intent registration",
9239
+ (ctx, emit) => finalizeIntentSync(emit, ctx),
9240
+ "Completes intent registration when all registrable intents are marked registered.",
9241
+ {
9242
+ register: false,
9243
+ isHidden: true
9244
+ }
9245
+ );
9246
+ const gatherActorRegistrationTask = CadenzaService.createUniqueMetaTask(
9247
+ "Gather actor registration",
9248
+ (ctx, emit) => finalizeActorSync(emit, ctx),
9249
+ "Completes actor registration when all registrable actors are marked registered.",
9250
+ {
9251
+ register: false,
9252
+ isHidden: true
9253
+ }
9254
+ );
9094
9255
  this.splitRoutinesTask = CadenzaService.createMetaTask(
9095
9256
  "Split routines for registration",
9096
- (ctx, emit) => {
9257
+ function* (ctx) {
9097
9258
  const { routines } = ctx;
9098
- if (!routines) return false;
9259
+ if (!routines) return;
9099
9260
  const serviceName2 = resolveSyncServiceName();
9100
9261
  if (!serviceName2) {
9101
- return false;
9262
+ return;
9102
9263
  }
9103
9264
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
9104
9265
  delayMs: 2e3
9105
9266
  });
9106
- let emittedCount = 0;
9107
9267
  for (const routine of routines) {
9108
9268
  if (routine.registered) continue;
9109
- emit("meta.sync_controller.routine_registration_split", {
9269
+ this.routinesSynced = false;
9270
+ yield {
9110
9271
  __syncing: ctx.__syncing,
9111
9272
  data: {
9112
9273
  name: routine.name,
@@ -9116,13 +9277,11 @@ var GraphSyncController = class _GraphSyncController {
9116
9277
  isMeta: routine.isMeta
9117
9278
  },
9118
9279
  __routineName: routine.name
9119
- });
9120
- emittedCount += 1;
9280
+ };
9121
9281
  }
9122
- return emittedCount > 0;
9123
- }
9282
+ }.bind(this)
9124
9283
  );
9125
- CadenzaService.createMetaTask("Process split routine registration", (ctx) => ctx).doOn("meta.sync_controller.routine_registration_split").then(
9284
+ this.splitRoutinesTask.then(
9126
9285
  resolveSyncInsertTask(
9127
9286
  this.isCadenzaDBReady,
9128
9287
  "routine",
@@ -9144,22 +9303,10 @@ var GraphSyncController = class _GraphSyncController {
9144
9303
  delayMs: 3e3
9145
9304
  });
9146
9305
  CadenzaService.getRoutine(ctx.__routineName).registered = true;
9147
- CadenzaService.debounce(
9148
- "meta.sync_controller.routine_registration_settled",
9149
- { __syncing: true },
9150
- 300
9151
- );
9152
9306
  return true;
9153
- })
9307
+ }).then(gatherRoutineRegistrationTask)
9154
9308
  )
9155
9309
  );
9156
- CadenzaService.createUniqueMetaTask(
9157
- "Gather routine registration",
9158
- () => {
9159
- this.routinesSynced = true;
9160
- return true;
9161
- }
9162
- ).doOn("meta.sync_controller.routine_registration_settled").emits("meta.sync_controller.synced_routines");
9163
9310
  this.splitTasksInRoutines = CadenzaService.createMetaTask(
9164
9311
  "Split tasks in routines",
9165
9312
  function* (ctx) {
@@ -9248,18 +9395,19 @@ var GraphSyncController = class _GraphSyncController {
9248
9395
  }
9249
9396
  this.splitSignalsTask = CadenzaService.createMetaTask(
9250
9397
  "Split signals for registration",
9251
- (ctx, emit) => {
9398
+ function* (ctx) {
9252
9399
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
9253
9400
  delayMs: 3e3
9254
9401
  });
9255
9402
  const { signals } = ctx;
9256
- if (!signals) return false;
9403
+ if (!signals) return;
9257
9404
  const filteredSignals = signals.filter(
9258
9405
  (signal) => !signal.data.registered
9259
9406
  ).map((signal) => signal.signal);
9260
9407
  for (const signal of filteredSignals) {
9261
9408
  const { isMeta, isGlobal, domain, action } = decomposeSignalName(signal);
9262
- emit("meta.sync_controller.signal_registration_split", {
9409
+ this.signalsSynced = false;
9410
+ yield {
9263
9411
  __syncing: ctx.__syncing,
9264
9412
  data: {
9265
9413
  name: signal,
@@ -9269,12 +9417,11 @@ var GraphSyncController = class _GraphSyncController {
9269
9417
  isMeta
9270
9418
  },
9271
9419
  __signal: signal
9272
- });
9420
+ };
9273
9421
  }
9274
- return filteredSignals.length > 0;
9275
- }
9422
+ }.bind(this)
9276
9423
  );
9277
- CadenzaService.createMetaTask("Process split signal registration", (ctx) => ctx).doOn("meta.sync_controller.signal_registration_split").then(
9424
+ this.splitSignalsTask.then(
9278
9425
  resolveSyncInsertTask(
9279
9426
  this.isCadenzaDBReady,
9280
9427
  "signal_registry",
@@ -9295,22 +9442,10 @@ var GraphSyncController = class _GraphSyncController {
9295
9442
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
9296
9443
  delayMs: 3e3
9297
9444
  });
9298
- CadenzaService.debounce(
9299
- "meta.sync_controller.signal_registration_settled",
9300
- { __syncing: true },
9301
- 300
9302
- );
9303
9445
  return { signalName: ctx.__signal };
9304
- }).then(CadenzaService.signalBroker.registerSignalTask)
9446
+ }).then(CadenzaService.signalBroker.registerSignalTask).then(gatherSignalRegistrationTask)
9305
9447
  )
9306
9448
  );
9307
- CadenzaService.createUniqueMetaTask(
9308
- "Gather signal registration",
9309
- () => {
9310
- this.signalsSynced = true;
9311
- return true;
9312
- }
9313
- ).doOn("meta.sync_controller.signal_registration_settled").emits("meta.sync_controller.synced_signals");
9314
9449
  this.splitTasksForRegistration = CadenzaService.createMetaTask(
9315
9450
  "Split tasks for registration",
9316
9451
  function* (ctx) {
@@ -9325,6 +9460,7 @@ var GraphSyncController = class _GraphSyncController {
9325
9460
  for (const task of tasks) {
9326
9461
  if (task.registered) continue;
9327
9462
  const { __functionString, __getTagCallback } = task.export();
9463
+ this.tasksSynced = false;
9328
9464
  if (shouldDebugSyncTaskName(task.name)) {
9329
9465
  logSyncDebug("task_registration_split", {
9330
9466
  taskName: task.name,
@@ -9373,7 +9509,7 @@ var GraphSyncController = class _GraphSyncController {
9373
9509
  __taskName: task.name
9374
9510
  };
9375
9511
  }
9376
- }
9512
+ }.bind(this)
9377
9513
  );
9378
9514
  const registerTaskTask = resolveSyncInsertTask(
9379
9515
  this.isCadenzaDBReady,
@@ -9407,13 +9543,8 @@ var GraphSyncController = class _GraphSyncController {
9407
9543
  ...ctx,
9408
9544
  task: CadenzaService.get(ctx.__taskName)
9409
9545
  });
9410
- CadenzaService.debounce(
9411
- "meta.sync_controller.task_registration_settled",
9412
- { __syncing: true },
9413
- 300
9414
- );
9415
9546
  return true;
9416
- })
9547
+ }).then(gatherTaskRegistrationTask)
9417
9548
  );
9418
9549
  if (registerTaskTask) {
9419
9550
  this.splitTasksForRegistration.then(registerTaskTask);
@@ -9446,13 +9577,6 @@ var GraphSyncController = class _GraphSyncController {
9446
9577
  isHidden: true
9447
9578
  }
9448
9579
  ).doOn("meta.task.created").then(this.splitTasksForRegistration);
9449
- CadenzaService.createUniqueMetaTask(
9450
- "Gather task registration",
9451
- () => {
9452
- this.tasksSynced = true;
9453
- return true;
9454
- }
9455
- ).doOn("meta.sync_controller.task_registration_settled").emits("meta.sync_controller.synced_tasks");
9456
9580
  this.splitActorsForRegistration = CadenzaService.createMetaTask(
9457
9581
  "Split actors for registration",
9458
9582
  function* (ctx) {
@@ -9476,6 +9600,7 @@ var GraphSyncController = class _GraphSyncController {
9476
9600
  if (this.registeredActors.has(registrationKey)) {
9477
9601
  continue;
9478
9602
  }
9603
+ this.actorsSynced = false;
9479
9604
  yield {
9480
9605
  data,
9481
9606
  __actorRegistrationKey: registrationKey
@@ -9504,22 +9629,10 @@ var GraphSyncController = class _GraphSyncController {
9504
9629
  delayMs: 3e3
9505
9630
  });
9506
9631
  this.registeredActors.add(ctx.__actorRegistrationKey);
9507
- CadenzaService.debounce(
9508
- "meta.sync_controller.actor_registration_settled",
9509
- { __syncing: true },
9510
- 300
9511
- );
9512
9632
  return true;
9513
- })
9633
+ }).then(gatherActorRegistrationTask)
9514
9634
  )
9515
9635
  );
9516
- CadenzaService.createUniqueMetaTask(
9517
- "Gather actor registration",
9518
- () => {
9519
- this.actorsSynced = true;
9520
- return true;
9521
- }
9522
- ).doOn("meta.sync_controller.actor_registration_settled").emits("meta.sync_controller.synced_actors");
9523
9636
  this.registerActorTaskMapTask = CadenzaService.createMetaTask(
9524
9637
  "Split actor task maps",
9525
9638
  function* (ctx) {
@@ -9666,12 +9779,11 @@ var GraphSyncController = class _GraphSyncController {
9666
9779
  );
9667
9780
  this.splitIntentsTask = CadenzaService.createMetaTask(
9668
9781
  "Split intents for registration",
9669
- function(ctx, emit) {
9782
+ function* (ctx) {
9670
9783
  CadenzaService.debounce("meta.sync_controller.synced_resource", {
9671
9784
  delayMs: 3e3
9672
9785
  });
9673
9786
  const intents = Array.isArray(ctx.intents) ? ctx.intents : Array.from(CadenzaService.inquiryBroker.intents.values());
9674
- let emittedCount = 0;
9675
9787
  for (const intent of intents) {
9676
9788
  const intentData = buildIntentRegistryData(intent);
9677
9789
  if (!intentData) {
@@ -9680,17 +9792,16 @@ var GraphSyncController = class _GraphSyncController {
9680
9792
  if (this.registeredIntentDefinitions.has(intentData.name)) {
9681
9793
  continue;
9682
9794
  }
9683
- emit("meta.sync_controller.intent_registration_split", {
9795
+ this.intentsSynced = false;
9796
+ yield {
9684
9797
  __syncing: ctx.__syncing,
9685
9798
  data: intentData,
9686
9799
  __intentName: intentData.name
9687
- });
9688
- emittedCount += 1;
9800
+ };
9689
9801
  }
9690
- return emittedCount > 0;
9691
9802
  }.bind(this)
9692
9803
  );
9693
- CadenzaService.createMetaTask("Process split intent registration", (ctx) => ctx).doOn("meta.sync_controller.intent_registration_split").then(
9804
+ this.splitIntentsTask.then(
9694
9805
  insertIntentRegistryTask?.then(
9695
9806
  CadenzaService.createMetaTask("Record intent definition registration", (ctx) => {
9696
9807
  if (!didSyncInsertSucceed(ctx)) {
@@ -9700,22 +9811,10 @@ var GraphSyncController = class _GraphSyncController {
9700
9811
  delayMs: 3e3
9701
9812
  });
9702
9813
  this.registeredIntentDefinitions.add(ctx.__intentName);
9703
- CadenzaService.debounce(
9704
- "meta.sync_controller.intent_registration_settled",
9705
- { __syncing: true },
9706
- 300
9707
- );
9708
9814
  return true;
9709
- })
9815
+ }).then(gatherIntentRegistrationTask)
9710
9816
  )
9711
9817
  );
9712
- CadenzaService.createUniqueMetaTask(
9713
- "Gather intent registration",
9714
- () => {
9715
- this.intentsSynced = true;
9716
- return true;
9717
- }
9718
- ).doOn("meta.sync_controller.intent_registration_settled").emits("meta.sync_controller.synced_intents");
9719
9818
  const registerIntentTask = CadenzaService.createMetaTask(
9720
9819
  "Record intent registration",
9721
9820
  (ctx) => {
@@ -10009,15 +10108,11 @@ var GraphSyncController = class _GraphSyncController {
10009
10108
  __authoritativeReconciliation: true
10010
10109
  });
10011
10110
  }
10012
- if (authoritativeTasks.length > 0) {
10013
- CadenzaService.debounce(
10014
- "meta.sync_controller.task_registration_settled",
10015
- {
10016
- __syncing: true,
10017
- __authoritativeReconciliation: true
10018
- },
10019
- 300
10020
- );
10111
+ if (authoritativeTasks.length > 0 || changed) {
10112
+ finalizeTaskSync(emit, {
10113
+ ...ctx,
10114
+ __authoritativeReconciliation: true
10115
+ });
10021
10116
  }
10022
10117
  return changed;
10023
10118
  },
@@ -10029,7 +10124,7 @@ var GraphSyncController = class _GraphSyncController {
10029
10124
  );
10030
10125
  const reconcileRoutineRegistrationFromAuthorityTask = CadenzaService.createMetaTask(
10031
10126
  "Reconcile routine registration from authority",
10032
- (ctx) => {
10127
+ (ctx, emit) => {
10033
10128
  const authoritativeRoutines = resolveSyncQueryRows(ctx, "routine");
10034
10129
  let changed = false;
10035
10130
  for (const row of authoritativeRoutines) {
@@ -10044,15 +10139,11 @@ var GraphSyncController = class _GraphSyncController {
10044
10139
  routine.registered = true;
10045
10140
  changed = true;
10046
10141
  }
10047
- if (authoritativeRoutines.length > 0) {
10048
- CadenzaService.debounce(
10049
- "meta.sync_controller.routine_registration_settled",
10050
- {
10051
- __syncing: true,
10052
- __authoritativeReconciliation: true
10053
- },
10054
- 300
10055
- );
10142
+ if (authoritativeRoutines.length > 0 || changed) {
10143
+ finalizeRoutineSync(emit, {
10144
+ ...ctx,
10145
+ __authoritativeReconciliation: true
10146
+ });
10056
10147
  }
10057
10148
  return changed;
10058
10149
  },
@@ -10064,7 +10155,7 @@ var GraphSyncController = class _GraphSyncController {
10064
10155
  );
10065
10156
  const reconcileSignalRegistrationFromAuthorityTask = CadenzaService.createMetaTask(
10066
10157
  "Reconcile signal registration from authority",
10067
- (ctx) => {
10158
+ (ctx, emit) => {
10068
10159
  const authoritativeSignals = resolveSyncQueryRows(ctx, "signal_registry");
10069
10160
  const signalObservers = CadenzaService.signalBroker.signalObservers;
10070
10161
  let changed = false;
@@ -10080,15 +10171,11 @@ var GraphSyncController = class _GraphSyncController {
10080
10171
  observer.registered = true;
10081
10172
  changed = true;
10082
10173
  }
10083
- if (authoritativeSignals.length > 0) {
10084
- CadenzaService.debounce(
10085
- "meta.sync_controller.signal_registration_settled",
10086
- {
10087
- __syncing: true,
10088
- __authoritativeReconciliation: true
10089
- },
10090
- 300
10091
- );
10174
+ if (authoritativeSignals.length > 0 || changed) {
10175
+ finalizeSignalSync(emit, {
10176
+ ...ctx,
10177
+ __authoritativeReconciliation: true
10178
+ });
10092
10179
  }
10093
10180
  return changed;
10094
10181
  },
@@ -10100,7 +10187,7 @@ var GraphSyncController = class _GraphSyncController {
10100
10187
  );
10101
10188
  const reconcileIntentRegistrationFromAuthorityTask = CadenzaService.createMetaTask(
10102
10189
  "Reconcile intent registration from authority",
10103
- (ctx) => {
10190
+ (ctx, emit) => {
10104
10191
  const authoritativeIntents = resolveSyncQueryRows(ctx, "intent_registry");
10105
10192
  let changed = false;
10106
10193
  for (const row of authoritativeIntents) {
@@ -10114,15 +10201,11 @@ var GraphSyncController = class _GraphSyncController {
10114
10201
  this.registeredIntentDefinitions.add(intentName);
10115
10202
  changed = true;
10116
10203
  }
10117
- if (authoritativeIntents.length > 0) {
10118
- CadenzaService.debounce(
10119
- "meta.sync_controller.intent_registration_settled",
10120
- {
10121
- __syncing: true,
10122
- __authoritativeReconciliation: true
10123
- },
10124
- 300
10125
- );
10204
+ if (authoritativeIntents.length > 0 || changed) {
10205
+ finalizeIntentSync(emit, {
10206
+ ...ctx,
10207
+ __authoritativeReconciliation: true
10208
+ });
10126
10209
  }
10127
10210
  return changed;
10128
10211
  },