本页内容
Sommatic 文档
打开控制台

Compensate Saga

为分布式事务(Saga)触发补偿流程。

以 Markdown 查看
在以下应用中打开
运维 实验性 saga.compensate 版本 1.0.0 执行方式: sync

实验性节点。设计用途是触发 saga 回滚。在当前运行时中,它总是以 logger.warn is not a function 从 error 输出;即便如此,它本应返回的 __signal: "COMPENSATE" 也不会被引擎读取,因此不会执行任何撤销步骤。

长事务中发生故障时,该算子会指示引擎“回滚”:按相反顺序,为之前所有成功的活动执行已注册的补偿步骤。

行为:

  • 识别当前活动的 Saga 作用域。
  • 获取已完成步骤的“undo”操作。
  • 按栈的方式(LIFO)执行。

适用场景:

  • 库存预留失败时回滚支付。
  • 航班无法预订时取消酒店预订。

工作原理

  1. 将输入项与解析后的配置合并(配置优先)。
  2. 从配置或输入项中读取 reason(默认 Manual Trigger)和 saga_id。
  3. 无论是否有 saga_id,都会调用 logger.warn() 并抛错;worker 捕获后从 error 输出。下面的步骤目前不会执行。
  4. 没有 saga_id 时:本应以 No Active Saga 从 error 输出。
  5. 有 saga_id 时:本应返回 { __signal: "COMPENSATE", saga_id, reason, port: "compensated", compensated: true },引擎只会把它当作普通输出传递。

在工作流中

节点在工作流文件(draft_nodes[])中的写法,以及一次成功运行的 envelope。

{
  "node_id": "node-compensate-order",
  "label": "Compensate order saga",
  "operator_slug": "saga.compensate",
  "operator_version": "1.0.0",
  "config": {
    "saga_id": "{{ $json.saga_id }}",
    "reason": "Payment declined"
  }
}

配置

该节点没有配置项。

配置中会显示通用执行设置,但该节点目前不执行重试(retryOnFail、maxTries)。所有节点均未实现 continueOnFail。

代码中的行为

除 schema 描述之外,实现代码对这些键的实际处理。

saga_id
不在配置 schema 中(仅出现在输入契约中),但会从节点配置读取;节点上未设置时从输入项读取:saga_id、reason。

输出端口

compensated 默认

在当前运行时中不会到达:logger.warn() 调用会先失败。此处展示代码构建的数据结构。

数据
{
  "__signal": "COMPENSATE",
  "saga_id": "ce78b34b-0bf5-4372-89b0-ae0eb6adddc1",
  "reason": "Payment declined",
  "port": "compensated",
  "compensated": true
}
error

目前每次运行都会如此。输出为整个返回对象 { port, error }。

数据
{
  "port": "error",
  "error": "logger.warn is not a function"
}

在表达式中

后续节点可通过 $("Compensate Saga").item.json.<路径> 读取此节点输出中的内容(请把 Compensate Saga 换成你的节点标签)。Editor 标记工作流编辑器在 Previous Nodes 中提示的路径(输出契约);Worker 标记 worker 实际通过 compensated 返回的路径,依据“端口”一节所示的输出。

表达式EditorWorker
$("Compensate Saga").item.json.compensatedboolean= true补偿流程已启动时为 true。
$("Compensate Saga").item.json.__signalstring编辑器不提示= "COMPENSATE"—
$("Compensate Saga").item.json.saga_idstring编辑器不提示= "ce78b34b-0bf5-4372-89b0-ae0eb6adddc1"—
$("Compensate Saga").item.json.reasonstring编辑器不提示= "Payment declined"—
$("Compensate Saga").item.json.portstring编辑器不提示= "compensated"—

— Worker: worker 会返回它,但编辑器不会提示:请手动输入。对象内部的键可能取决于你的数据或配置(例如每个标签一个键)。

错误

消息 端口 原因与处理
logger.warn is not a function error Worker 调用了运行时 logger 不提供的 logger.warn()。每次运行都会发生,任何配置都无法避免。
No Active Saga error 目前不会到达。它表示节点和输入项中都没有设置 saga_id;从上下文回退查找活动 saga 的逻辑永远找不到。

输入契约

reason string 可选

触发补偿的原因。

saga_id string 可选

指定的 Saga ID(可选,默认取自上下文)。

输出契约

compensated boolean 可选

补偿流程已启动时为 true。

契约(JSON Schema)

与编排器声明完全一致的定义。All 是完整的 *.definition.js;可与上方各节对照,找出文档与 schema 之间的差异。发布前可用它校验节点。

{
  "slug": "saga.compensate",
  "name": "Compensate Saga",
  "category": "ops",
  "version": "1.0.0",
  "description": "Triggers the compensation process for a distributed transaction (Saga).\n\n  When a failure occurs in a long-running transaction, this operator instructs the engine to \"rollback\" by executing the registered compensation steps for all previously successful activities in reverse order.\n\n  **Behavior:**\n  - Identifies the active Saga Scope.\n  - Retrieves the 'undo' operations for completed steps.\n  - Executes them stack-wise (LIFO).\n\n  **Use Cases:**\n  - Rolling back a payment if inventory reservation fails.\n  - Cancelling a hotel booking if the flight is unavailable.",
  "capabilities": [
    "saga",
    "transaction",
    "rollback",
    "ops"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "reason": {
        "type": "string",
        "description": "Reason for compensation trigger."
      },
      "saga_id": {
        "type": "string",
        "description": "Specific Saga ID (optional, defaults to context)."
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "compensated": {
        "type": "boolean",
        "description": "True if compensation workflow initiated."
      }
    }
  },
  "ports": [
    {
      "port_id": "compensated",
      "label": "Compensated",
      "is_default": true,
      "description": "Compensation process started."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Failed to trigger compensation."
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "saga",
    "rollback",
    "transaction"
  ],
  "is_stateful": true,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}

执行

执行方式
sync
有状态
是
重试(retryOnFail)
否
能力
sagatransactionrollbackops
标签
sagarollbacktransaction

说明与限制

  • Worker 调用了 logger.warn(),而运行时 logger 没有该方法(只提供 info、warning、error、log 和 success)。每次运行该调用都会抛错。
  • 要实现可用的补偿还缺少:引擎不读取 __signal,不维护 saga 历史,也没有执行 undo 逻辑的步骤。目前请用 flow.try 加显式补偿节点来实现回滚。
  • 从 error 端口输出不会使运行失败:引擎沿 error 上的连线继续;若没有连线,该分支直接结束。
  • execution 设置(retryOnFail、maxTries、waitBetweenTries、continueOnFail、timeout_ms)可以填写,但本 worker 和引擎都不会应用。

已对照编排器代码核实 · 31e874b · 2026-11-26

安装智能体工具

为你的编程智能体接入 Sommatic AI 官方文档:技能包、文档 MCP 服务器与 Markdown 页面,无需再抓取网页。

安装 Sommatic AI CLI,并为检测到的智能体配置合适的插件。

npx @sommatic/cli agent setup
智能体如何读取本文档