# Join / Merge

> Consolidates multiple execution branches back into a single path.

Source: https://docs.sommatic.ai/en/nodes/flow.join/

- Slug: `flow.join`
- Family: Flow control
- Version: 1.0.0
- Status: Available
- Execution: sync

Consolidates multiple execution branches back into a single path.

This operator implements the 'Fan-In' synchronization pattern. It is essential when a concurrent split (Fan-Out) needs to be gathered back for aggregated processing. It waits for upstream signals based on the configured mode before proceeding.

**Modes:**
- **Wait All:** Blocks until ALL incoming branches have completed. Ideal for parallel processing where all results are needed.
- **Wait Any:** Proceeds as soon as the FIRST branch completes. ideal for race conditions or redundancy checks.

**Output:**
The result is an array containing the payloads from all joined branches: `[result1, result2, ...]`.

## Configuration

- `mode` (string, optional): Strategy for synchronization. Default: `wait_all`. Allowed values: `wait_all`, `wait_any`.
- `timeout_ms` (integer, optional): Maximum time to wait. 0 means infinite wait. Default: `0`.

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

- `out` (default): Active when join condition is met.
- `timeout`: Active if the join waits longer than the timeout.
- `error`: Active on internal failure

## Input contract

- `payload` (object, optional)

## Output contract

- `payload` (array<object>, optional)

## In expressions

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

- `$("Join / Merge").item.json.payload` (array) **The worker does not return it: reads as undefined.**
- `$("Join / Merge").item.json.merged` (array) = `[…] (2)` (Not suggested by the editor)
- `$("Join / Merge").item.json.count` (number) = `2` (Not suggested by the editor)
- `$("Join / Merge").item.json.mode` (string) = `"wait_all"` (Not suggested by the editor)


## How it works

Collects the results of the items a `flow.split` fanned out and emits them as one array, once the split `done` signal and every item have arrived.

1. The engine buffers every arrival at a join node instead of running it. An arrival through a `done` port marks the gate ready and sets the expected count to `total_items`; any other arrival adds its payload to the list.
2. When `done` has arrived and the list holds at least the expected count, the engine runs the worker once with the list as `inputs` and clears the gate.
3. The worker returns `{ merged, count, mode }` through `out`; `merged` keeps arrival order.
4. With "Execute step" (no engine inputs), `merged` is built from the outputs of the executed nodes in the run graph, or `[payload]` when there are none.

## In a workflow

### Node

```json
{
  "node_id": "node-collect-orders",
  "label": "Collect orders",
  "operator_slug": "flow.join",
  "operator_version": "1.0.0",
  "config": {
    "mode": "wait_all",
    "timeout_ms": 0
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "merged": [
      {
        "order_id": "ord_1",
        "status": "synced"
      },
      {
        "order_id": "ord_2",
        "status": "synced"
      }
    ],
    "count": 2,
    "mode": "wait_all"
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

All items arrived after the split `done` signal.

```json
{
  "merged": [
    {
      "order_id": "ord_1",
      "status": "synced"
    },
    {
      "order_id": "ord_2",
      "status": "synced"
    }
  ],
  "count": 2,
  "mode": "wait_all"
}
```

### `timeout`

Never emitted: `timeout_ms` is ignored.

### `error`

Never emitted by the worker.

## Behaviour in the code

- `mode`: Only echoed in the output: `wait_any` behaves like `wait_all`.
- `timeout_ms`: Ignored: the join never times out.

## Notes and limits

- Without an edge from a `flow.split` `done` port into the join, the join never runs: arrivals from ordinary parallel branches are buffered forever, nothing after the join runs, and the run still completes.
- If an item branch ends elsewhere (an `error` port, a `false` port without an edge) or the split uses `batch_size` > 1, the expected count is never reached and the join does not run.
- An empty array in the split sends only `done`; the join then runs with `merged: []` and `count: 0`.
- Join gates live in memory for the current run only.


## Contracts (JSON Schema)

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

```json
{
  "slug": "flow.join",
  "name": "Join / Merge",
  "category": "flow",
  "version": "1.0.0",
  "description": "Consolidates multiple execution branches back into a single path.\n\n  This operator implements the 'Fan-In' synchronization pattern. It is essential when a concurrent split (Fan-Out) needs to be gathered back for aggregated processing. It waits for upstream signals based on the configured mode before proceeding.\n\n  **Modes:**\n  - **Wait All:** Blocks until ALL incoming branches have completed. Ideal for parallel processing where all results are needed.\n  - **Wait Any:** Proceeds as soon as the FIRST branch completes. ideal for race conditions or redundancy checks.\n\n  **Output:**\n  The result is an array containing the payloads from all joined branches: `[result1, result2, ...]`.",
  "capabilities": [
    "fan-in",
    "aggregation",
    "sync"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "mode": {
        "type": "string",
        "enum": [
          "wait_all",
          "wait_any"
        ],
        "default": "wait_all",
        "title": "Join Mode",
        "description": "Strategy for synchronization."
      },
      "timeout_ms": {
        "type": "integer",
        "title": "Timeout (ms)",
        "description": "Maximum time to wait. 0 means infinite wait.",
        "default": 0
      },
      "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",
    "description": "Receives multiple upstream payloads (handled by engine).",
    "properties": {
      "payload": {
        "type": "object"
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "description": "Array of results from the joined branches.",
    "properties": {
      "payload": {
        "type": "array",
        "items": {
          "type": "object"
        }
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Merged",
      "is_default": true,
      "description": "Active when join condition is met."
    },
    {
      "port_id": "timeout",
      "label": "Timeout",
      "description": "Active if the join waits longer than the timeout."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Active on internal failure"
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "merge",
    "aggregate",
    "synchronize",
    "fan-in"
  ],
  "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": {
    "mode": {
      "type": "string",
      "enum": [
        "wait_all",
        "wait_any"
      ],
      "default": "wait_all",
      "title": "Join Mode",
      "description": "Strategy for synchronization."
    },
    "timeout_ms": {
      "type": "integer",
      "title": "Timeout (ms)",
      "description": "Maximum time to wait. 0 means infinite wait.",
      "default": 0
    },
    "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",
  "description": "Receives multiple upstream payloads (handled by engine).",
  "properties": {
    "payload": {
      "type": "object"
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "description": "Array of results from the joined branches.",
  "properties": {
    "payload": {
      "type": "array",
      "items": {
        "type": "object"
      }
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Merged",
    "is_default": true,
    "description": "Active when join condition is met."
  },
  {
    "port_id": "timeout",
    "label": "Timeout",
    "description": "Active if the join waits longer than the timeout."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Active on internal failure"
  }
]
```
