# Checkpoint

> Marks a guaranteed durability point in the workflow execution.

Source: https://docs.sommatic.ai/en/nodes/ops.checkpoint/

- Slug: `ops.checkpoint`
- Family: Operations
- Version: 1.0.0
- Status: Experimental. Always fails today: the worker calls `logger.debug`, which the runtime logger does not provide, so every run leaves through `error` with "logger.debug is not a function". Do not use it until it is fixed.
- Execution: sync

Marks a guaranteed durability point in the workflow execution.

A Checkpoint acts as a "Save Game" spot. If the workflow crashes or is restarted, it can resume from the last successful checkpoint rather than restarting from the beginning. It also supports Idempotency, identifying duplicate requests to prevent double-processing.

**Behavior:**
- **First Run:** Persists the checkpoint and triggers 'success'.
- **Replay:** Detects the checkpoint exists and triggers 'skip' (or returns the previous output).
- **Hashing:** Can optionally hash the input payload to ensure it's the *exact same* request.

**Use Cases:**
- Between charging a credit card and shipping a product.
- Ensuring an email is sent exactly once.

## Configuration

- `payload_hash_mode` (boolean, optional): If true, the input payload must match exactly to be considered a duplicate replay. Default: `false`.

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

- `success` (default): Checkpoint created. Continue execution.
- `skip`: Checkpoint already exists. Skipping downstream logic.
- `error`: Failed to persist.

## Input contract

- `idempotency_key` (string, required): Unique identifier for this specific operation execution.

## In expressions

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

This node declares no output fields.


## How it works

Meant to skip replays of an operation by remembering an idempotency key in the workflow key-value state. In the current runtime it always leaves through `error` with `logger.debug is not a function`, before reading or writing any state.

1. Merges the input item with the resolved config (config wins).
2. Leaves through `error` with `Missing 'idempotency_key'` when the key is empty.
3. Builds the state key `chk:<idempotency_key>`; with `payload_hash_mode` it appends `:<md5>`.
4. Calls `logger.debug()`, which throws; the worker catches it and leaves through `error`. The steps below are not reached today.
5. Looks the key up in `$input.$kv_state`. Found: leaves through `skip` with the stored payload.
6. Not found: leaves through `success` with `{}` and asks the backend to save `{ timestamp, payload }` under the key through `meta.kv_state`.

## In a workflow

### Node

```json
{
  "node_id": "node-checkpoint-order",
  "label": "Checkpoint order",
  "operator_slug": "ops.checkpoint",
  "operator_version": "1.0.0",
  "config": {
    "idempotency_key": "{{ $json.order_id }}",
    "payload_hash_mode": false
  }
}
```

### Envelope

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

## Output ports (behaviour in the code)

### `success`

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

```json
{}
```

### `skip`

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

```json
{
  "order_id": "ord_123",
  "total": 129.9
}
```

### `error`

Every run today. The output is the whole returned object, `{ port, error }`, because the worker sets no `data`.

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

## Behaviour in the code

- `idempotency_key`: Not in the config schema (only in the input contract), but read from the node config or, when unset there, from the input item: `idempotency_key`.
- `payload_hash_mode`: The hash is computed over the worker context `payload`, which the runtime does not set, so it is always the MD5 of `{}`: the suffix never distinguishes one payload from another.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `logger.debug is not a function` | `error` | The worker calls `logger.debug()`, which the runtime logger does not provide. It happens on every run; there is no configuration that avoids it. |
| `Missing 'idempotency_key'` | `error` | `idempotency_key` is set neither on the node nor in the input item. |

## Notes and limits

- The worker calls `logger.debug()`, which the runtime logger does not provide (it offers `info`, `warning`, `error`, `log` and `success`). The call throws on every run.
- Key-value state lives in the workflow definition metadata (`kv_state`), shared by every run of that workflow. The backend injects it at run start as `$input.$kv_state` only when the run uses the draft graph; runs of a published version receive none. Writes (`meta.kv_state` of each step) are merged into the metadata after the run ends, so later nodes of the same run do not see them. There is no expiry.
- The worker returns its fields at the top level instead of under `data`, so the engine uses the whole returned object, `port` included, as the node output.
- 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": "ops.checkpoint",
  "name": "Checkpoint",
  "category": "ops",
  "version": "1.0.0",
  "description": "Marks a guaranteed durability point in the workflow execution.\n\n  A Checkpoint acts as a \"Save Game\" spot. If the workflow crashes or is restarted, it can resume from the last successful checkpoint rather than restarting from the beginning. It also supports Idempotency, identifying duplicate requests to prevent double-processing.\n\n  **Behavior:**\n  - **First Run:** Persists the checkpoint and triggers 'success'.\n  - **Replay:** Detects the checkpoint exists and triggers 'skip' (or returns the previous output).\n  - **Hashing:** Can optionally hash the input payload to ensure it's the *exact same* request.\n\n  **Use Cases:**\n  - Between charging a credit card and shipping a product.\n  - Ensuring an email is sent exactly once.",
  "capabilities": [
    "reliability",
    "recovery",
    "idempotency",
    "checkpoint"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "payload_hash_mode": {
        "type": "boolean",
        "default": false,
        "title": "Strict Payload Hashing",
        "description": "If true, the input payload must match exactly to be considered a duplicate replay."
      },
      "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": {
      "idempotency_key": {
        "type": "string",
        "description": "Unique identifier for this specific operation execution."
      }
    },
    "required": [
      "idempotency_key"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "description": "Empty on success, or previous result on skip.",
    "properties": {}
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Success",
      "is_default": true,
      "description": "Checkpoint created. Continue execution."
    },
    {
      "port_id": "skip",
      "label": "Skip (Duplicate)",
      "description": "Checkpoint already exists. Skipping downstream logic."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Failed to persist."
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "reliability",
    "recovery",
    "idempotency"
  ],
  "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": {
    "payload_hash_mode": {
      "type": "boolean",
      "default": false,
      "title": "Strict Payload Hashing",
      "description": "If true, the input payload must match exactly to be considered a duplicate replay."
    },
    "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": {
    "idempotency_key": {
      "type": "string",
      "description": "Unique identifier for this specific operation execution."
    }
  },
  "required": [
    "idempotency_key"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "description": "Empty on success, or previous result on skip.",
  "properties": {}
}
```

### ports

```json
[
  {
    "port_id": "success",
    "label": "Success",
    "is_default": true,
    "description": "Checkpoint created. Continue execution."
  },
  {
    "port_id": "skip",
    "label": "Skip (Duplicate)",
    "description": "Checkpoint already exists. Skipping downstream logic."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Failed to persist."
  }
]
```
