# Read State

> Retrieves a persistent value from the workflow state store.

Source: https://docs.sommatic.ai/en/nodes/ops.state.read/

- Slug: `ops.state.read`
- Family: Operations
- Version: 1.0.0
- Status: Partial. `instance` scope always returns `miss`. Workflow-scoped state is only available in test runs of the draft, not in runs of a published version.
- Execution: sync

Retrieves a persistent value from the workflow state store.

This operator is used to access data saved in previous steps or even from other workflows (using Global/Case scope). It provides a way to maintain context across long-running or distributed executions.

**Behavior:**
- Attempts to find the key in the specified scope.
- If found, returns the value.
- If not found, triggers the 'miss' port to allow for fallback logic.

**Use Cases:**
- Retrieving a cached user profile.
- Validating if a previous step completed via a state flag.

## Configuration

- `scope` (string, required): Visibility level to read from (Instance, Case, or Global system-wide). Default: `instance`. Allowed values: `instance`, `case`, `global`.

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): Value retrieved successfully.
- `miss`: Keys does not exist or has expired.
- `error`: Storage connectivity error.

## Input contract

- `key` (string, required): The unique key of the state variable.

## Output contract

- `value` (any, optional): The stored data.

## In expressions

What a later node can read from this node's output with `$("Read State").item.json.<path>` (write your node's label instead of `Read State`). **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.

- `$("Read State").item.json.value` (any) = `"2026-11-25T23:00:00.000Z"`: The stored data.


## How it works

Reads a value by `key`. The `global` and `case` scopes read the workflow key-value state (`kv_state`); the `instance` scope always returns `miss` in the current runtime.

1. Merges the input item with the resolved config (config wins).
2. Uses `scope` (default `instance`) and leaves through `error` with `Missing 'key'` when `key` is empty.
3. `global`: looks up `<key>` in `$input.$kv_state`. `case`: looks up `case:<key>` in the same store.
4. `instance`: reads the worker context `state`, which the runtime does not provide, so the value is always undefined.
5. An undefined value leaves through `miss` with `{ key }`; anything else through `success` with `{ value }`.

## In a workflow

### Node

```json
{
  "node_id": "node-read-last-sync",
  "label": "Read last sync time",
  "operator_slug": "ops.state.read",
  "operator_version": "1.0.0",
  "config": {
    "scope": "global",
    "key": "last_sync_at"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "success",
  "data": {
    "value": "2026-11-25T23:00:00.000Z"
  }
}
```

## Output ports (behaviour in the code)

### `success`

The key exists in `kv_state` (`global` or `case` scope).

```json
{
  "value": "2026-11-25T23:00:00.000Z"
}
```

### `miss`

The key is not in `kv_state`, the run received no `kv_state`, or the scope is `instance`.

```json
{
  "key": "last_sync_at"
}
```

### `error`

`key` is empty. The output is the whole returned object, `{ port, error }`.

```json
{
  "port": "error",
  "error": "Missing 'key'"
}
```

## Behaviour in the code

- `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: `key`.
- `scope`: `case` is not isolated per case: it only prefixes the key with `case:` in the same per-workflow store as `global`. `global` is per workflow, not system-wide.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Missing 'key'` | `error` | `key` is set neither on the node nor in the input item. |

## Notes and limits

- 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.
- Values are only written by `ops.dedupe`, `ops.checkpoint` and `ops.state.write`, which all fail in the current runtime, so `kv_state` only holds what was stored earlier.
- 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.state.read",
  "name": "Read State",
  "category": "ops",
  "version": "1.0.0",
  "description": "Retrieves a persistent value from the workflow state store.\n\n  This operator is used to access data saved in previous steps or even from other workflows (using Global/Case scope). It provides a way to maintain context across long-running or distributed executions.\n\n  **Behavior:**\n  - Attempts to find the key in the specified scope.\n  - If found, returns the value.\n  - If not found, triggers the 'miss' port to allow for fallback logic.\n\n  **Use Cases:**\n  - Retrieving a cached user profile.\n  - Validating if a previous step completed via a state flag.",
  "capabilities": [
    "state",
    "persistence",
    "read",
    "store"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "scope"
    ],
    "properties": {
      "scope": {
        "type": "string",
        "enum": [
          "instance",
          "case",
          "global"
        ],
        "default": "instance",
        "title": "State Scope",
        "description": "Visibility level to read from (Instance, Case, or Global system-wide)."
      },
      "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": {
      "key": {
        "type": "string",
        "description": "The unique key of the state variable."
      }
    },
    "required": [
      "key"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "description": "The retrieved value.",
    "properties": {
      "value": {
        "type": "any",
        "description": "The stored data."
      }
    }
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Found",
      "is_default": true,
      "description": "Value retrieved successfully."
    },
    {
      "port_id": "miss",
      "label": "Not Found",
      "description": "Keys does not exist or has expired."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Storage connectivity error."
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "state",
    "database",
    "persistence",
    "read"
  ],
  "is_stateful": true,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "required": [
    "scope"
  ],
  "properties": {
    "scope": {
      "type": "string",
      "enum": [
        "instance",
        "case",
        "global"
      ],
      "default": "instance",
      "title": "State Scope",
      "description": "Visibility level to read from (Instance, Case, or Global system-wide)."
    },
    "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": {
    "key": {
      "type": "string",
      "description": "The unique key of the state variable."
    }
  },
  "required": [
    "key"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "description": "The retrieved value.",
  "properties": {
    "value": {
      "type": "any",
      "description": "The stored data."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "success",
    "label": "Found",
    "is_default": true,
    "description": "Value retrieved successfully."
  },
  {
    "port_id": "miss",
    "label": "Not Found",
    "description": "Keys does not exist or has expired."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Storage connectivity error."
  }
]
```
