@cadenza.io/service 2.17.42 → 2.17.44

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
@@ -8810,6 +8810,40 @@ var CADENZA_DB_REQUIRED_LOCAL_SYNC_INSERT_TABLES = [
8810
8810
  "intent_to_task_map",
8811
8811
  "directional_task_graph_map"
8812
8812
  ];
8813
+ var AUTHORITY_QUERY_RESULT_KEYS = {
8814
+ task: "tasks",
8815
+ routine: "routines",
8816
+ signal_registry: "signalRegistrys",
8817
+ intent_registry: "intentRegistrys"
8818
+ };
8819
+ function resolveSyncQueryRows(ctx, tableName) {
8820
+ const resultKey = AUTHORITY_QUERY_RESULT_KEYS[tableName];
8821
+ const rows = ctx?.[resultKey];
8822
+ return Array.isArray(rows) ? rows : [];
8823
+ }
8824
+ function resolveSyncQueryTask(isCadenzaDBReady, tableName, queryData = {}, options = {}) {
8825
+ const localQueryTask = CadenzaService.getLocalCadenzaDBQueryTask(tableName);
8826
+ const remoteQueryTask = isCadenzaDBReady ? CadenzaService.createCadenzaDBQueryTask(tableName, queryData, options) : void 0;
8827
+ if (!localQueryTask && !remoteQueryTask) {
8828
+ return void 0;
8829
+ }
8830
+ const targetTask = localQueryTask ?? remoteQueryTask;
8831
+ return CadenzaService.createMetaTask(
8832
+ `Prepare graph sync query for ${tableName}`,
8833
+ (ctx) => ({
8834
+ ...ctx,
8835
+ queryData: {
8836
+ ...ctx.queryData && typeof ctx.queryData === "object" ? ctx.queryData : {},
8837
+ ...queryData
8838
+ }
8839
+ }),
8840
+ `Prepares ${tableName} graph-sync query payloads.`,
8841
+ {
8842
+ register: false,
8843
+ isHidden: true
8844
+ }
8845
+ ).then(targetTask);
8846
+ }
8813
8847
  var GraphSyncController = class _GraphSyncController {
8814
8848
  constructor() {
8815
8849
  this.registeredActors = /* @__PURE__ */ new Set();
@@ -8910,6 +8944,30 @@ var GraphSyncController = class _GraphSyncController {
8910
8944
  },
8911
8945
  { concurrency: 30 }
8912
8946
  );
8947
+ const authoritativeTaskQueryTask = resolveSyncQueryTask(
8948
+ this.isCadenzaDBReady,
8949
+ "task",
8950
+ {},
8951
+ { concurrency: 10 }
8952
+ );
8953
+ const authoritativeRoutineQueryTask = resolveSyncQueryTask(
8954
+ this.isCadenzaDBReady,
8955
+ "routine",
8956
+ {},
8957
+ { concurrency: 10 }
8958
+ );
8959
+ const authoritativeSignalQueryTask = resolveSyncQueryTask(
8960
+ this.isCadenzaDBReady,
8961
+ "signal_registry",
8962
+ {},
8963
+ { concurrency: 10 }
8964
+ );
8965
+ const authoritativeIntentQueryTask = resolveSyncQueryTask(
8966
+ this.isCadenzaDBReady,
8967
+ "intent_registry",
8968
+ {},
8969
+ { concurrency: 10 }
8970
+ );
8913
8971
  this.splitRoutinesTask = CadenzaService.createMetaTask(
8914
8972
  "Split routines for registration",
8915
8973
  (ctx, emit) => {
@@ -9718,6 +9776,286 @@ var GraphSyncController = class _GraphSyncController {
9718
9776
  )
9719
9777
  )
9720
9778
  );
9779
+ const reconcileTaskRegistrationFromAuthorityTask = CadenzaService.createMetaTask(
9780
+ "Reconcile task registration from authority",
9781
+ (ctx, emit) => {
9782
+ const authoritativeTasks = resolveSyncQueryRows(ctx, "task");
9783
+ let changed = false;
9784
+ for (const row of authoritativeTasks) {
9785
+ const taskName = typeof row.name === "string" ? row.name : "";
9786
+ if (!taskName) {
9787
+ continue;
9788
+ }
9789
+ const task = CadenzaService.get(taskName);
9790
+ if (!task || task.registered) {
9791
+ continue;
9792
+ }
9793
+ task.registered = true;
9794
+ changed = true;
9795
+ emit("meta.sync_controller.task_registered", {
9796
+ ...ctx,
9797
+ __taskName: task.name,
9798
+ task,
9799
+ __authoritativeReconciliation: true
9800
+ });
9801
+ }
9802
+ if (authoritativeTasks.length > 0) {
9803
+ CadenzaService.debounce(
9804
+ "meta.sync_controller.task_registration_settled",
9805
+ {
9806
+ __syncing: true,
9807
+ __authoritativeReconciliation: true
9808
+ },
9809
+ 300
9810
+ );
9811
+ }
9812
+ return changed;
9813
+ },
9814
+ "Marks local tasks as registered when authority rows already exist.",
9815
+ {
9816
+ register: false,
9817
+ isHidden: true
9818
+ }
9819
+ );
9820
+ const reconcileRoutineRegistrationFromAuthorityTask = CadenzaService.createMetaTask(
9821
+ "Reconcile routine registration from authority",
9822
+ (ctx) => {
9823
+ const authoritativeRoutines = resolveSyncQueryRows(ctx, "routine");
9824
+ let changed = false;
9825
+ for (const row of authoritativeRoutines) {
9826
+ const routineName = typeof row.name === "string" ? row.name : "";
9827
+ if (!routineName) {
9828
+ continue;
9829
+ }
9830
+ const routine = CadenzaService.getRoutine(routineName);
9831
+ if (!routine || routine.registered) {
9832
+ continue;
9833
+ }
9834
+ routine.registered = true;
9835
+ changed = true;
9836
+ }
9837
+ if (authoritativeRoutines.length > 0) {
9838
+ CadenzaService.debounce(
9839
+ "meta.sync_controller.routine_registration_settled",
9840
+ {
9841
+ __syncing: true,
9842
+ __authoritativeReconciliation: true
9843
+ },
9844
+ 300
9845
+ );
9846
+ }
9847
+ return changed;
9848
+ },
9849
+ "Marks local routines as registered when authority rows already exist.",
9850
+ {
9851
+ register: false,
9852
+ isHidden: true
9853
+ }
9854
+ );
9855
+ const reconcileSignalRegistrationFromAuthorityTask = CadenzaService.createMetaTask(
9856
+ "Reconcile signal registration from authority",
9857
+ (ctx) => {
9858
+ const authoritativeSignals = resolveSyncQueryRows(ctx, "signal_registry");
9859
+ const signalObservers = CadenzaService.signalBroker.signalObservers;
9860
+ let changed = false;
9861
+ for (const row of authoritativeSignals) {
9862
+ const signalName = typeof row.name === "string" ? row.name : "";
9863
+ if (!signalName) {
9864
+ continue;
9865
+ }
9866
+ const observer = signalObservers?.get(signalName);
9867
+ if (!observer || observer.registered) {
9868
+ continue;
9869
+ }
9870
+ observer.registered = true;
9871
+ changed = true;
9872
+ }
9873
+ if (authoritativeSignals.length > 0) {
9874
+ CadenzaService.debounce(
9875
+ "meta.sync_controller.signal_registration_settled",
9876
+ {
9877
+ __syncing: true,
9878
+ __authoritativeReconciliation: true
9879
+ },
9880
+ 300
9881
+ );
9882
+ }
9883
+ return changed;
9884
+ },
9885
+ "Marks local signals as registered when authority rows already exist.",
9886
+ {
9887
+ register: false,
9888
+ isHidden: true
9889
+ }
9890
+ );
9891
+ const reconcileIntentRegistrationFromAuthorityTask = CadenzaService.createMetaTask(
9892
+ "Reconcile intent registration from authority",
9893
+ (ctx) => {
9894
+ const authoritativeIntents = resolveSyncQueryRows(ctx, "intent_registry");
9895
+ let changed = false;
9896
+ for (const row of authoritativeIntents) {
9897
+ const intentName = typeof row.name === "string" ? row.name : "";
9898
+ if (!intentName || !CadenzaService.inquiryBroker.intents.has(intentName)) {
9899
+ continue;
9900
+ }
9901
+ if (this.registeredIntentDefinitions.has(intentName)) {
9902
+ continue;
9903
+ }
9904
+ this.registeredIntentDefinitions.add(intentName);
9905
+ changed = true;
9906
+ }
9907
+ if (authoritativeIntents.length > 0) {
9908
+ CadenzaService.debounce(
9909
+ "meta.sync_controller.intent_registration_settled",
9910
+ {
9911
+ __syncing: true,
9912
+ __authoritativeReconciliation: true
9913
+ },
9914
+ 300
9915
+ );
9916
+ }
9917
+ return changed;
9918
+ },
9919
+ "Marks local intents as registered when authority rows already exist.",
9920
+ {
9921
+ register: false,
9922
+ isHidden: true
9923
+ }
9924
+ );
9925
+ const authoritativeTaskReconciliationGraph = authoritativeTaskQueryTask?.then(reconcileTaskRegistrationFromAuthorityTask) ?? CadenzaService.createMetaTask(
9926
+ "Skip authoritative task reconciliation",
9927
+ () => false,
9928
+ "Skips task reconciliation when no authority query task is available.",
9929
+ {
9930
+ register: false,
9931
+ isHidden: true
9932
+ }
9933
+ );
9934
+ const authoritativeRoutineReconciliationGraph = authoritativeRoutineQueryTask?.then(reconcileRoutineRegistrationFromAuthorityTask) ?? CadenzaService.createMetaTask(
9935
+ "Skip authoritative routine reconciliation",
9936
+ () => false,
9937
+ "Skips routine reconciliation when no authority query task is available.",
9938
+ {
9939
+ register: false,
9940
+ isHidden: true
9941
+ }
9942
+ );
9943
+ const authoritativeSignalReconciliationGraph = authoritativeSignalQueryTask?.then(reconcileSignalRegistrationFromAuthorityTask) ?? CadenzaService.createMetaTask(
9944
+ "Skip authoritative signal reconciliation",
9945
+ () => false,
9946
+ "Skips signal reconciliation when no authority query task is available.",
9947
+ {
9948
+ register: false,
9949
+ isHidden: true
9950
+ }
9951
+ );
9952
+ const authoritativeIntentReconciliationGraph = authoritativeIntentQueryTask?.then(reconcileIntentRegistrationFromAuthorityTask) ?? CadenzaService.createMetaTask(
9953
+ "Skip authoritative intent reconciliation",
9954
+ () => false,
9955
+ "Skips intent reconciliation when no authority query task is available.",
9956
+ {
9957
+ register: false,
9958
+ isHidden: true
9959
+ }
9960
+ );
9961
+ const authoritativeRegistrationTriggers = [
9962
+ "meta.service_registry.initial_sync_complete",
9963
+ "meta.sync_requested",
9964
+ "meta.sync_controller.synced_resource",
9965
+ "meta.sync_controller.authority_registration_reconciliation_requested"
9966
+ ];
9967
+ CadenzaService.createMetaTask(
9968
+ "Prepare authoritative task registration query",
9969
+ (ctx) => {
9970
+ if (!this.isCadenzaDBReady) {
9971
+ return false;
9972
+ }
9973
+ const serviceName2 = resolveSyncServiceName();
9974
+ if (!serviceName2) {
9975
+ return false;
9976
+ }
9977
+ return {
9978
+ ...ctx,
9979
+ __syncServiceName: serviceName2,
9980
+ queryData: {
9981
+ filter: {
9982
+ service_name: serviceName2
9983
+ },
9984
+ fields: ["name", "version", "service_name"]
9985
+ }
9986
+ };
9987
+ },
9988
+ "Builds the authority task query payload for the current service.",
9989
+ {
9990
+ register: false,
9991
+ isHidden: true
9992
+ }
9993
+ ).doOn(...authoritativeRegistrationTriggers).then(authoritativeTaskReconciliationGraph);
9994
+ CadenzaService.createMetaTask(
9995
+ "Prepare authoritative routine registration query",
9996
+ (ctx) => {
9997
+ if (!this.isCadenzaDBReady) {
9998
+ return false;
9999
+ }
10000
+ const serviceName2 = resolveSyncServiceName();
10001
+ if (!serviceName2) {
10002
+ return false;
10003
+ }
10004
+ return {
10005
+ ...ctx,
10006
+ __syncServiceName: serviceName2,
10007
+ queryData: {
10008
+ filter: {
10009
+ service_name: serviceName2
10010
+ },
10011
+ fields: ["name", "version", "service_name"]
10012
+ }
10013
+ };
10014
+ },
10015
+ "Builds the authority routine query payload for the current service.",
10016
+ {
10017
+ register: false,
10018
+ isHidden: true
10019
+ }
10020
+ ).doOn(...authoritativeRegistrationTriggers).then(authoritativeRoutineReconciliationGraph);
10021
+ CadenzaService.createMetaTask(
10022
+ "Prepare authoritative signal registration query",
10023
+ (ctx) => {
10024
+ if (!this.isCadenzaDBReady) {
10025
+ return false;
10026
+ }
10027
+ return {
10028
+ ...ctx,
10029
+ queryData: {
10030
+ fields: ["name"]
10031
+ }
10032
+ };
10033
+ },
10034
+ "Builds the authority signal query payload for local reconciliation.",
10035
+ {
10036
+ register: false,
10037
+ isHidden: true
10038
+ }
10039
+ ).doOn(...authoritativeRegistrationTriggers).then(authoritativeSignalReconciliationGraph);
10040
+ CadenzaService.createMetaTask(
10041
+ "Prepare authoritative intent registration query",
10042
+ (ctx) => {
10043
+ if (!this.isCadenzaDBReady) {
10044
+ return false;
10045
+ }
10046
+ return {
10047
+ ...ctx,
10048
+ queryData: {
10049
+ fields: ["name"]
10050
+ }
10051
+ };
10052
+ },
10053
+ "Builds the authority intent query payload for local reconciliation.",
10054
+ {
10055
+ register: false,
10056
+ isHidden: true
10057
+ }
10058
+ ).doOn(...authoritativeRegistrationTriggers).then(authoritativeIntentReconciliationGraph);
9721
10059
  CadenzaService.signalBroker.getSignalsTask.clone().doOn(
9722
10060
  "meta.sync_controller.sync_tick",
9723
10061
  "meta.service_registry.initial_sync_complete"
@@ -11048,7 +11386,7 @@ var CadenzaService = class {
11048
11386
  this.ensureFrontendSyncLoop();
11049
11387
  } else {
11050
11388
  GraphMetadataController.instance;
11051
- GraphSyncController.instance.isCadenzaDBReady = !!options.cadenzaDB?.connect;
11389
+ GraphSyncController.instance.isCadenzaDBReady = serviceName === "CadenzaDB" || !!options.cadenzaDB?.connect;
11052
11390
  GraphSyncController.instance.init();
11053
11391
  }
11054
11392
  this.log("Service created.");