@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.mjs CHANGED
@@ -6524,6 +6524,67 @@ var SCHEMA_TYPES = [
6524
6524
  // src/database/DatabaseController.ts
6525
6525
  import { Pool } from "pg";
6526
6526
  import { camelCase, kebabCase, snakeCase } from "lodash-es";
6527
+ var AUTHORITY_SYNC_DEBUG_PREFIX = "[CADENZA_DB_TASK_DEBUG]";
6528
+ var AUTHORITY_SYNC_DEBUG_TASK_NAMES = /* @__PURE__ */ new Set([
6529
+ "Query service_instance",
6530
+ "Query service_instance_transport",
6531
+ "Query intent_to_task_map",
6532
+ "Query signal_to_task_map",
6533
+ "Forward service instance sync",
6534
+ "Forward service transport sync",
6535
+ "Forward intent to task map sync",
6536
+ "Forward signal to task map sync",
6537
+ "Compile sync data and broadcast",
6538
+ "Prepare for signal sync"
6539
+ ]);
6540
+ var AUTHORITY_SYNC_DEBUG_ROUTINE_NAMES = /* @__PURE__ */ new Set(["Sync services"]);
6541
+ function logAuthoritySyncDebug(event, payload) {
6542
+ console.log(`${AUTHORITY_SYNC_DEBUG_PREFIX} ${event}`, payload);
6543
+ }
6544
+ function resolveAuthoritySyncPayloadName(payload, tableName) {
6545
+ const data = payload.data && typeof payload.data === "object" && !Array.isArray(payload.data) ? payload.data : null;
6546
+ if (!data) {
6547
+ return null;
6548
+ }
6549
+ if (tableName === "task") {
6550
+ const taskName = data.name;
6551
+ return typeof taskName === "string" ? taskName : null;
6552
+ }
6553
+ if (tableName === "task_to_routine_map") {
6554
+ const taskName = data.taskName ?? data.task_name;
6555
+ return typeof taskName === "string" ? taskName : null;
6556
+ }
6557
+ return null;
6558
+ }
6559
+ function shouldDebugAuthoritySyncPayload(tableName, payload) {
6560
+ if (tableName !== "task" && tableName !== "task_to_routine_map") {
6561
+ return false;
6562
+ }
6563
+ const payloadName = resolveAuthoritySyncPayloadName(payload, tableName);
6564
+ if (payloadName && AUTHORITY_SYNC_DEBUG_TASK_NAMES.has(payloadName)) {
6565
+ return true;
6566
+ }
6567
+ if (tableName === "task_to_routine_map") {
6568
+ const data = payload.data && typeof payload.data === "object" && !Array.isArray(payload.data) ? payload.data : null;
6569
+ const routineName = data?.routineName ?? data?.routine_name;
6570
+ return typeof routineName === "string" && AUTHORITY_SYNC_DEBUG_ROUTINE_NAMES.has(routineName);
6571
+ }
6572
+ return false;
6573
+ }
6574
+ function buildAuthoritySyncDebugSummary(payload, context) {
6575
+ const data = payload.data && typeof payload.data === "object" && !Array.isArray(payload.data) ? payload.data : {};
6576
+ return {
6577
+ taskName: data.name ?? data.taskName ?? data.task_name ?? context.__taskName ?? null,
6578
+ routineName: data.routineName ?? data.routine_name ?? context.__routineName ?? null,
6579
+ serviceName: data.serviceName ?? data.service_name ?? context.__syncServiceName ?? null,
6580
+ predecessorTaskName: data.predecessorTaskName ?? data.predecessor_task_name ?? null,
6581
+ queryDataKeys: context.queryData && typeof context.queryData === "object" ? Object.keys(context.queryData) : [],
6582
+ dataKeys: Object.keys(data),
6583
+ onConflict: payload.onConflict && typeof payload.onConflict === "object" ? payload.onConflict : null,
6584
+ rowCount: context.rowCount ?? null,
6585
+ error: context.__error ?? null
6586
+ };
6587
+ }
6527
6588
  function normalizeIntentToken(value) {
6528
6589
  const normalized = kebabCase(String(value ?? "").trim());
6529
6590
  if (!normalized) {
@@ -7876,16 +7937,39 @@ var DatabaseController = class _DatabaseController {
7876
7937
  }
7877
7938
  }
7878
7939
  const operationPayload = typeof context.queryData === "object" && context.queryData ? context.queryData : context;
7879
- this.validateOperationPayload(
7880
- registration,
7881
- op,
7940
+ const shouldDebugAuthoritySync = shouldDebugAuthoritySyncPayload(
7882
7941
  tableName,
7883
- table,
7884
- operationPayload,
7885
- {
7886
- enforceFieldAllowlist: registration.options.securityProfile === "low" || payloadModifiedByTriggers
7887
- }
7942
+ operationPayload
7888
7943
  );
7944
+ if (shouldDebugAuthoritySync) {
7945
+ logAuthoritySyncDebug("input", {
7946
+ tableName,
7947
+ operation: op,
7948
+ summary: buildAuthoritySyncDebugSummary(operationPayload, context)
7949
+ });
7950
+ }
7951
+ try {
7952
+ this.validateOperationPayload(
7953
+ registration,
7954
+ op,
7955
+ tableName,
7956
+ table,
7957
+ operationPayload,
7958
+ {
7959
+ enforceFieldAllowlist: registration.options.securityProfile === "low" || payloadModifiedByTriggers
7960
+ }
7961
+ );
7962
+ } catch (error) {
7963
+ if (shouldDebugAuthoritySync) {
7964
+ logAuthoritySyncDebug("validation_failed", {
7965
+ tableName,
7966
+ operation: op,
7967
+ summary: buildAuthoritySyncDebugSummary(operationPayload, context),
7968
+ error: error instanceof Error ? error.message : String(error)
7969
+ });
7970
+ }
7971
+ throw error;
7972
+ }
7889
7973
  let result;
7890
7974
  if (op === "query") {
7891
7975
  result = await this.queryFunction(registration, tableName, operationPayload);
@@ -7900,6 +7984,14 @@ var DatabaseController = class _DatabaseController {
7900
7984
  ...context,
7901
7985
  ...result
7902
7986
  };
7987
+ if (shouldDebugAuthoritySync) {
7988
+ logAuthoritySyncDebug("result", {
7989
+ tableName,
7990
+ operation: op,
7991
+ summary: buildAuthoritySyncDebugSummary(operationPayload, context),
7992
+ resultKeys: result && typeof result === "object" ? Object.keys(result) : []
7993
+ });
7994
+ }
7903
7995
  if (!context.errored) {
7904
7996
  for (const signal of table.customSignals?.emissions?.[op] ?? []) {
7905
7997
  if (typeof signal === "string") {
@@ -8681,15 +8773,10 @@ function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, opti
8681
8773
  const executionFailedSignal = `meta.sync_controller.insert_execution_failed:${tableName}`;
8682
8774
  const pendingResolverContexts = /* @__PURE__ */ new Map();
8683
8775
  const prepareExecutionTask = CadenzaService.createMetaTask(
8684
- `Prepare graph sync insert execution for ${tableName}`,
8776
+ `Prepare graph sync insert for ${tableName}`,
8685
8777
  (ctx) => {
8686
- const originalContext = {
8687
- ...ctx
8688
- };
8689
- const originalQueryData = buildSyncInsertQueryData(
8690
- ctx,
8691
- queryData
8692
- );
8778
+ const originalContext = { ...ctx };
8779
+ const originalQueryData = buildSyncInsertQueryData(ctx, queryData);
8693
8780
  if (typeof ctx.__resolverRequestId === "string") {
8694
8781
  pendingResolverContexts.set(ctx.__resolverRequestId, {
8695
8782
  originalContext,
@@ -8741,7 +8828,7 @@ function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, opti
8741
8828
  );
8742
8829
  }
8743
8830
  const finalizeExecutionTask = CadenzaService.createMetaTask(
8744
- `Finalize graph sync insert execution for ${tableName}`,
8831
+ `Finalize graph sync insert for ${tableName}`,
8745
8832
  (ctx, emit) => {
8746
8833
  if (!ctx.__resolverRequestId) {
8747
8834
  return false;
@@ -8779,7 +8866,7 @@ function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, opti
8779
8866
  emit(executionResolvedSignal, normalizedContext);
8780
8867
  return normalizedContext;
8781
8868
  },
8782
- `Resolves signal-driven ${tableName} graph-sync insert execution.`,
8869
+ `Finalizes ${tableName} graph-sync insert execution after the authority task finishes.`,
8783
8870
  {
8784
8871
  register: false,
8785
8872
  isHidden: true
@@ -8792,11 +8879,14 @@ function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, opti
8792
8879
  `Log failed graph sync insert execution for ${tableName}`,
8793
8880
  (ctx) => {
8794
8881
  if (tableName === "task") {
8795
- if (shouldDebugTaskSyncPayload(ctx)) {
8882
+ if (shouldDebugTaskSyncPayload(ctx) || shouldDebugSyncTaskName(ctx.__taskName)) {
8796
8883
  logSyncDebug("insert_failed", {
8797
8884
  tableName,
8798
8885
  targetTaskName: targetTask.name,
8799
- payload: buildTaskSyncDebugPayload(ctx)
8886
+ payload: buildTaskSyncDebugPayload({
8887
+ ...ctx,
8888
+ __taskName: ctx.__taskName
8889
+ })
8800
8890
  });
8801
8891
  }
8802
8892
  } else {
@@ -8822,6 +8912,27 @@ function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, opti
8822
8912
  `Resolve graph sync insert for ${tableName}`,
8823
8913
  (ctx, emit) => new Promise((resolve) => {
8824
8914
  const resolverRequestId = uuid6();
8915
+ const resolvedContext = {
8916
+ ...ctx,
8917
+ __resolverRequestId: resolverRequestId
8918
+ };
8919
+ if (debugTable) {
8920
+ if (tableName === "task") {
8921
+ if (shouldDebugTaskSyncPayload(resolvedContext)) {
8922
+ logSyncDebug("insert_resolver_request", {
8923
+ tableName,
8924
+ targetTaskName: targetTask.name,
8925
+ payload: buildTaskSyncDebugPayload(resolvedContext)
8926
+ });
8927
+ }
8928
+ } else {
8929
+ logSyncDebug("insert_resolver_request", {
8930
+ tableName,
8931
+ targetTaskName: targetTask.name,
8932
+ ctx: resolvedContext
8933
+ });
8934
+ }
8935
+ }
8825
8936
  CadenzaService.createEphemeralMetaTask(
8826
8937
  `Resolve graph sync insert execution for ${tableName} (${resolverRequestId})`,
8827
8938
  (resultCtx) => {
@@ -8836,15 +8947,12 @@ function resolveSyncInsertTask(isCadenzaDBReady, tableName, queryData = {}, opti
8836
8947
  resolve(normalizedResult);
8837
8948
  return normalizedResult;
8838
8949
  },
8839
- `Waits for signal-driven ${tableName} graph-sync insert execution.`,
8950
+ `Waits for ${tableName} graph-sync insert execution.`,
8840
8951
  {
8841
8952
  register: false
8842
8953
  }
8843
8954
  ).doOn(executionResolvedSignal, executionFailedSignal);
8844
- emit(executionRequestedSignal, {
8845
- ...ctx,
8846
- __resolverRequestId: resolverRequestId
8847
- });
8955
+ emit(executionRequestedSignal, resolvedContext);
8848
8956
  }),
8849
8957
  `Routes graph sync inserts for ${tableName} through the local authority task when available.`,
8850
8958
  {