# Compensate Saga

> Triggers the compensation process for a distributed transaction (Saga).

Source: https://docs.sommatic.ai/en/nodes/saga.compensate/

- Slug: `saga.compensate`
- Family: Operations
- Version: 1.0.0
- Status: Experimental. Always fails today: the worker calls `logger.warn`, which the runtime logger does not provide. Even when fixed, the `COMPENSATE` signal it returns is not read by the execution engine.
- Execution: sync

Triggers the compensation process for a distributed transaction (Saga).

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.

**Behavior:**
- Identifies the active Saga Scope.
- Retrieves the 'undo' operations for completed steps.
- Executes them stack-wise (LIFO).

**Use Cases:**
- Rolling back a payment if inventory reservation fails.
- Cancelling a hotel booking if the flight is unavailable.

## Configuration

This node has no configuration fields.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `compensated` (default): Compensation process started.
- `error`: Failed to trigger compensation.

## Input contract

- `reason` (string, optional): Reason for compensation trigger.
- `saga_id` (string, optional): Specific Saga ID (optional, defaults to context).

## Output contract

- `compensated` (boolean, optional): True if compensation workflow initiated.

## In expressions

What a later node can read from this node's output with `$("Compensate Saga").item.json.<path>` (write your node's label instead of `Compensate Saga`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `compensated`, from the output shown in Ports.

- `$("Compensate Saga").item.json.compensated` (boolean) = `true`: True if compensation workflow initiated.
- `$("Compensate Saga").item.json.__signal` (string) = `"COMPENSATE"` (Not suggested by the editor)
- `$("Compensate Saga").item.json.saga_id` (string) = `"ce78b34b-0bf5-4372-89b0-ae0eb6adddc1"` (Not suggested by the editor)
- `$("Compensate Saga").item.json.reason` (string) = `"Payment declined"` (Not suggested by the editor)
- `$("Compensate Saga").item.json.port` (string) = `"compensated"` (Not suggested by the editor)


## How it works

Experimental. Meant to trigger the rollback of a saga. In the current runtime it always leaves through `error` with `logger.warn is not a function`; even then, the `__signal: "COMPENSATE"` it would return is not read by the engine, so no undo step runs.

1. Merges the input item with the resolved config (config wins).
2. Uses `reason` (default `Manual Trigger`) and `saga_id` from the config or the input item.
3. With or without `saga_id`, calls `logger.warn()`, which throws; the worker catches it and leaves through `error`. The steps below are not reached today.
4. Without `saga_id`: would leave through `error` with `No Active Saga`.
5. With `saga_id`: would return `{ __signal: "COMPENSATE", saga_id, reason, port: "compensated", compensated: true }`, which the engine passes on as plain output.

## In a workflow

### Node

```json
{
  "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"
  }
}
```

### Envelope

```json
{
  "ok": false,
  "port": "error",
  "data": {
    "port": "error",
    "error": "logger.warn is not a function"
  }
}
```

## Output ports (behaviour in the code)

### `compensated`

Not reached in the current runtime: the `logger.warn()` call fails first. Shown with the shape the code builds.

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

### `error`

Every run today. The output is the whole returned object, `{ port, error }`.

```json
{
  "port": "error",
  "error": "logger.warn is not a function"
}
```

## Behaviour in the code

- `saga_id`: Not in the config schema (only in the input contract), but read from the node config or, when unset there, from the input item: `saga_id`, `reason`.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `logger.warn is not a function` | `error` | The worker calls `logger.warn()`, which the runtime logger does not provide. It happens on every run; there is no configuration that avoids it. |
| `No Active Saga` | `error` | Not reached today. It would mean that `saga_id` is set neither on the node nor in the input item; the fallback to an active saga in the context never finds one. |

## Notes and limits

- The worker calls `logger.warn()`, which the runtime logger does not provide (it offers `info`, `warning`, `error`, `log` and `success`). The call throws on every run.
- Missing for a working compensation: the engine does not read `__signal`, keeps no saga history and has no step that runs `undo` logic. Model rollbacks today with `flow.try` and explicit compensation nodes.
- Leaving through `error` does not fail the run: the engine follows the edges wired to `error`, and if there are none the branch simply ends.
- The `execution` settings (`retryOnFail`, `maxTries`, `waitBetweenTries`, `continueOnFail`, `timeout_ms`) are accepted, but neither this worker nor the engine applies them.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "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
    }
  }
}
```

### config_schema

```json
{
  "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

```json
{
  "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

```json
{
  "type": "object",
  "properties": {
    "compensated": {
      "type": "boolean",
      "description": "True if compensation workflow initiated."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "compensated",
    "label": "Compensated",
    "is_default": true,
    "description": "Compensation process started."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Failed to trigger compensation."
  }
]
```
