@cadenza.io/service 2.17.50 → 2.17.52

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
@@ -6575,6 +6575,67 @@ var SCHEMA_TYPES = [
6575
6575
  // src/database/DatabaseController.ts
6576
6576
  var import_pg = require("pg");
6577
6577
  var import_lodash_es = require("lodash-es");
6578
+ var AUTHORITY_SYNC_DEBUG_PREFIX = "[CADENZA_DB_TASK_DEBUG]";
6579
+ var AUTHORITY_SYNC_DEBUG_TASK_NAMES = /* @__PURE__ */ new Set([
6580
+ "Query service_instance",
6581
+ "Query service_instance_transport",
6582
+ "Query intent_to_task_map",
6583
+ "Query signal_to_task_map",
6584
+ "Forward service instance sync",
6585
+ "Forward service transport sync",
6586
+ "Forward intent to task map sync",
6587
+ "Forward signal to task map sync",
6588
+ "Compile sync data and broadcast",
6589
+ "Prepare for signal sync"
6590
+ ]);
6591
+ var AUTHORITY_SYNC_DEBUG_ROUTINE_NAMES = /* @__PURE__ */ new Set(["Sync services"]);
6592
+ function logAuthoritySyncDebug(event, payload) {
6593
+ console.log(`${AUTHORITY_SYNC_DEBUG_PREFIX} ${event}`, payload);
6594
+ }
6595
+ function resolveAuthoritySyncPayloadName(payload, tableName) {
6596
+ const data = payload.data && typeof payload.data === "object" && !Array.isArray(payload.data) ? payload.data : null;
6597
+ if (!data) {
6598
+ return null;
6599
+ }
6600
+ if (tableName === "task") {
6601
+ const taskName = data.name;
6602
+ return typeof taskName === "string" ? taskName : null;
6603
+ }
6604
+ if (tableName === "task_to_routine_map") {
6605
+ const taskName = data.taskName ?? data.task_name;
6606
+ return typeof taskName === "string" ? taskName : null;
6607
+ }
6608
+ return null;
6609
+ }
6610
+ function shouldDebugAuthoritySyncPayload(tableName, payload) {
6611
+ if (tableName !== "task" && tableName !== "task_to_routine_map") {
6612
+ return false;
6613
+ }
6614
+ const payloadName = resolveAuthoritySyncPayloadName(payload, tableName);
6615
+ if (payloadName && AUTHORITY_SYNC_DEBUG_TASK_NAMES.has(payloadName)) {
6616
+ return true;
6617
+ }
6618
+ if (tableName === "task_to_routine_map") {
6619
+ const data = payload.data && typeof payload.data === "object" && !Array.isArray(payload.data) ? payload.data : null;
6620
+ const routineName = data?.routineName ?? data?.routine_name;
6621
+ return typeof routineName === "string" && AUTHORITY_SYNC_DEBUG_ROUTINE_NAMES.has(routineName);
6622
+ }
6623
+ return false;
6624
+ }
6625
+ function buildAuthoritySyncDebugSummary(payload, context) {
6626
+ const data = payload.data && typeof payload.data === "object" && !Array.isArray(payload.data) ? payload.data : {};
6627
+ return {
6628
+ taskName: data.name ?? data.taskName ?? data.task_name ?? context.__taskName ?? null,
6629
+ routineName: data.routineName ?? data.routine_name ?? context.__routineName ?? null,
6630
+ serviceName: data.serviceName ?? data.service_name ?? context.__syncServiceName ?? null,
6631
+ predecessorTaskName: data.predecessorTaskName ?? data.predecessor_task_name ?? null,
6632
+ queryDataKeys: context.queryData && typeof context.queryData === "object" ? Object.keys(context.queryData) : [],
6633
+ dataKeys: Object.keys(data),
6634
+ onConflict: payload.onConflict && typeof payload.onConflict === "object" ? payload.onConflict : null,
6635
+ rowCount: context.rowCount ?? null,
6636
+ error: context.__error ?? null
6637
+ };
6638
+ }
6578
6639
  function normalizeIntentToken(value) {
6579
6640
  const normalized = (0, import_lodash_es.kebabCase)(String(value ?? "").trim());
6580
6641
  if (!normalized) {
@@ -7927,16 +7988,39 @@ var DatabaseController = class _DatabaseController {
7927
7988
  }
7928
7989
  }
7929
7990
  const operationPayload = typeof context.queryData === "object" && context.queryData ? context.queryData : context;
7930
- this.validateOperationPayload(
7931
- registration,
7932
- op,
7991
+ const shouldDebugAuthoritySync = shouldDebugAuthoritySyncPayload(
7933
7992
  tableName,
7934
- table,
7935
- operationPayload,
7936
- {
7937
- enforceFieldAllowlist: registration.options.securityProfile === "low" || payloadModifiedByTriggers
7938
- }
7993
+ operationPayload
7939
7994
  );
7995
+ if (shouldDebugAuthoritySync) {
7996
+ logAuthoritySyncDebug("input", {
7997
+ tableName,
7998
+ operation: op,
7999
+ summary: buildAuthoritySyncDebugSummary(operationPayload, context)
8000
+ });
8001
+ }
8002
+ try {
8003
+ this.validateOperationPayload(
8004
+ registration,
8005
+ op,
8006
+ tableName,
8007
+ table,
8008
+ operationPayload,
8009
+ {
8010
+ enforceFieldAllowlist: registration.options.securityProfile === "low" || payloadModifiedByTriggers
8011
+ }
8012
+ );
8013
+ } catch (error) {
8014
+ if (shouldDebugAuthoritySync) {
8015
+ logAuthoritySyncDebug("validation_failed", {
8016
+ tableName,
8017
+ operation: op,
8018
+ summary: buildAuthoritySyncDebugSummary(operationPayload, context),
8019
+ error: error instanceof Error ? error.message : String(error)
8020
+ });
8021
+ }
8022
+ throw error;
8023
+ }
7940
8024
  let result;
7941
8025
  if (op === "query") {
7942
8026
  result = await this.queryFunction(registration, tableName, operationPayload);
@@ -7951,6 +8035,14 @@ var DatabaseController = class _DatabaseController {
7951
8035
  ...context,
7952
8036
  ...result
7953
8037
  };
8038
+ if (shouldDebugAuthoritySync) {
8039
+ logAuthoritySyncDebug("result", {
8040
+ tableName,
8041
+ operation: op,
8042
+ summary: buildAuthoritySyncDebugSummary(operationPayload, context),
8043
+ resultKeys: result && typeof result === "object" ? Object.keys(result) : []
8044
+ });
8045
+ }
7954
8046
  if (!context.errored) {
7955
8047
  for (const signal of table.customSignals?.emissions?.[op] ?? []) {
7956
8048
  if (typeof signal === "string") {
@@ -8730,15 +8822,10 @@ function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, opti
8730
8822
  const executionFailedSignal = `meta.sync_controller.insert_execution_failed:${tableName}`;
8731
8823
  const pendingResolverContexts = /* @__PURE__ */ new Map();
8732
8824
  const prepareExecutionTask = CadenzaService.createMetaTask(
8733
- `Prepare graph sync insert execution for ${tableName}`,
8825
+ `Prepare graph sync insert for ${tableName}`,
8734
8826
  (ctx) => {
8735
- const originalContext = {
8736
- ...ctx
8737
- };
8738
- const originalQueryData = buildSyncInsertQueryData(
8739
- ctx,
8740
- queryData
8741
- );
8827
+ const originalContext = { ...ctx };
8828
+ const originalQueryData = buildSyncInsertQueryData(ctx, queryData);
8742
8829
  if (typeof ctx.__resolverRequestId === "string") {
8743
8830
  pendingResolverContexts.set(ctx.__resolverRequestId, {
8744
8831
  originalContext,
@@ -8790,7 +8877,7 @@ function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, opti
8790
8877
  );
8791
8878
  }
8792
8879
  const finalizeExecutionTask = CadenzaService.createMetaTask(
8793
- `Finalize graph sync insert execution for ${tableName}`,
8880
+ `Finalize graph sync insert for ${tableName}`,
8794
8881
  (ctx, emit) => {
8795
8882
  if (!ctx.__resolverRequestId) {
8796
8883
  return false;
@@ -8828,7 +8915,7 @@ function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, opti
8828
8915
  emit(executionResolvedSignal, normalizedContext);
8829
8916
  return normalizedContext;
8830
8917
  },
8831
- `Resolves signal-driven ${tableName} graph-sync insert execution.`,
8918
+ `Finalizes ${tableName} graph-sync insert execution after the authority task finishes.`,
8832
8919
  {
8833
8920
  register: false,
8834
8921
  isHidden: true
@@ -8841,11 +8928,14 @@ function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, opti
8841
8928
  `Log failed graph sync insert execution for ${tableName}`,
8842
8929
  (ctx) => {
8843
8930
  if (tableName === "task") {
8844
- if (shouldDebugTaskSyncPayload(ctx)) {
8931
+ if (shouldDebugTaskSyncPayload(ctx) || shouldDebugSyncTaskName(ctx.__taskName)) {
8845
8932
  logSyncDebug("insert_failed", {
8846
8933
  tableName,
8847
8934
  targetTaskName: targetTask.name,
8848
- payload: buildTaskSyncDebugPayload(ctx)
8935
+ payload: buildTaskSyncDebugPayload({
8936
+ ...ctx,
8937
+ __taskName: ctx.__taskName
8938
+ })
8849
8939
  });
8850
8940
  }
8851
8941
  } else {
@@ -8871,6 +8961,27 @@ function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, opti
8871
8961
  `Resolve graph sync insert for ${tableName}`,
8872
8962
  (ctx, emit) => new Promise((resolve) => {
8873
8963
  const resolverRequestId = (0, import_uuid6.v4)();
8964
+ const resolvedContext = {
8965
+ ...ctx,
8966
+ __resolverRequestId: resolverRequestId
8967
+ };
8968
+ if (debugTable) {
8969
+ if (tableName === "task") {
8970
+ if (shouldDebugTaskSyncPayload(resolvedContext)) {
8971
+ logSyncDebug("insert_resolver_request", {
8972
+ tableName,
8973
+ targetTaskName: targetTask.name,
8974
+ payload: buildTaskSyncDebugPayload(resolvedContext)
8975
+ });
8976
+ }
8977
+ } else {
8978
+ logSyncDebug("insert_resolver_request", {
8979
+ tableName,
8980
+ targetTaskName: targetTask.name,
8981
+ ctx: resolvedContext
8982
+ });
8983
+ }
8984
+ }
8874
8985
  CadenzaService.createEphemeralMetaTask(
8875
8986
  `Resolve graph sync insert execution for ${tableName} (${resolverRequestId})`,
8876
8987
  (resultCtx) => {
@@ -8885,15 +8996,12 @@ function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, opti
8885
8996
  resolve(normalizedResult);
8886
8997
  return normalizedResult;
8887
8998
  },
8888
- `Waits for signal-driven ${tableName} graph-sync insert execution.`,
8999
+ `Waits for ${tableName} graph-sync insert execution.`,
8889
9000
  {
8890
9001
  register: false
8891
9002
  }
8892
9003
  ).doOn(executionResolvedSignal, executionFailedSignal);
8893
- emit(executionRequestedSignal, {
8894
- ...ctx,
8895
- __resolverRequestId: resolverRequestId
8896
- });
9004
+ emit(executionRequestedSignal, resolvedContext);
8897
9005
  }),
8898
9006
  `Routes graph sync inserts for ${tableName} through the local authority task when available.`,
8899
9007
  {