# Apply StoneOS Industry Package

> Proposes an industry package for a business unit, previews it and waits for a person with permission to approve.

Source: https://docs.sommatic.ai/en/nodes/stoneos.industry.apply/

- Slug: `stoneos.industry.apply`
- Family: Human in the loop
- Version: 1.0.0
- Status: Partial. Creates the change proposal and ends the initial run; the workflow does not wait for the decision, and the proposal does not carry the run that created it.
- Execution: async

Proposes an industry package for a business unit, previews it and waits for a person with permission to approve. The app applies the change; the worker only proposes, waits and resumes.

## Configuration

- `unit` (string, required): Business unit code (expression allowed)
- `package_id` (string, required)
- `proposal_id` (string, optional)
- `role` (string, optional): Default: `principal`. Allowed values: `principal`, `complement`.
- `size` (string, optional)
- `jurisdiction` (string, optional): Jurisdiction code; defaults to the organization one
- `reason` (string, optional)
- `operation` (string, optional)
- `approver_role` (string, optional): Default: `administrator`.
- `timeout_ms` (integer, optional): Default: `259200000`.

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

- `applied` (default): Approved and applied
- `rejected`: A person rejected the change
- `timeout`: Nobody decided in time
- `partial`: Applied halfway; the flow decides
- `error`: Carries error_code

## Input contract

- `unit` (string, optional)
- `package_id` (string, optional)
- `proposal_id` (string, optional)
- `role` (string, optional)
- `size` (string, optional)
- `operation` (string, optional)
- `approver_role` (string, optional)
- `timeout_ms` (integer, optional)

## Output contract

- `change_id` (string, optional)
- `state` (string, optional)
- `applied_apps` (array<string>, optional)
- `failed_app` (string, optional)
- `approver` (string, optional)
- `error_code` (string, optional)

## In expressions

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

- `$("Apply StoneOS Industry Package").item.json.change_id` (string) = `"chg-xxxxxxxxxxxx"`
- `$("Apply StoneOS Industry Package").item.json.state` (string) = `"applied"`
- `$("Apply StoneOS Industry Package").item.json.applied_apps` (array) = `[…] (2)`
- `$("Apply StoneOS Industry Package").item.json.failed_app` (string) **The worker does not return it: reads as undefined.**
- `$("Apply StoneOS Industry Package").item.json.approver` (string) = `"admin@example.com"`
- `$("Apply StoneOS Industry Package").item.json.error_code` (string) **The worker does not return it: reads as undefined.**


## How it works

Proposes a StoneOS industry package for a business unit, creates its preview and returns a suspend signal; the later decision of a person is mapped to `applied`, `rejected`, `timeout` or `partial` through the orchestrator resume operation.

1. Resolves the config and merges it over the incoming payload (config wins). If the result has a `resume` object, it skips the proposal and maps the decision (last step).
2. Checks `unit` and `package_id`, and that the StoneOS industry service is configured.
3. Calls the service as a service identity acting on behalf of `approver_role` (default `administrator`).
4. Creates a change with `package_id`, `business_unit_code` (from `unit`), `role` (default `principal`), `size`, `jurisdiction` and `reasons` (`reason`, or a default text naming the package), then requests its preview, which must return `installation_id` and `installation_preview_hash`.
5. Returns `{ __signal: "SUSPEND", __metadata: { type: "stoneos_industry_change", id, installation_id, preview_hash, expires_in_ms, resume_on } }`. The envelope has no port, so the engine routes it to `out`, which this node does not declare: the branch ends and the run completes.
6. Resume: the system that holds the decision calls the orchestrator resume operation with `workflow_instance_id`, `node_id`, optional `graph` and `input`, and `resume` `{ change_id, state, port?, approver, applied_apps?, failed_app?, error_code? }`. The worker runs again in resume mode and picks the port from `resume.port` or `resume.state`; with `graph` and `node_id`, a new run starts from the nodes connected to that port, with `data` as payload.

## In a workflow

### Node

```json
{
  "node_id": "node-apply-industry",
  "label": "Apply industry package",
  "operator_slug": "stoneos.industry.apply",
  "operator_version": "1.0.0",
  "config": {
    "unit": "{{ $json.business_unit_code }}",
    "package_id": "<package_id>",
    "role": "principal",
    "reason": "New business unit onboarding",
    "approver_role": "administrator",
    "timeout_ms": 259200000
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "change_id": "chg-xxxxxxxxxxxx",
    "state": "applied",
    "applied_apps": [
      "stoneos-notes",
      "stoneos-todo"
    ],
    "approver": "admin@example.com"
  },
  "port": "applied",
  "meta": {
    "approver": "admin@example.com"
  }
}
```

## Output ports (behaviour in the code)

### `applied`

Resume with state `applied` and an `approver`. The only port with `ok: true`.

```json
{
  "change_id": "chg-xxxxxxxxxxxx",
  "state": "applied",
  "applied_apps": [
    "stoneos-notes",
    "stoneos-todo"
  ],
  "approver": "admin@example.com"
}
```

### `rejected`

Resume with state `rejected` and an `approver`.

```json
{
  "change_id": "chg-xxxxxxxxxxxx",
  "state": "rejected",
  "applied_apps": [],
  "approver": "admin@example.com"
}
```

### `timeout`

Resume with state `timeout`; no `approver` is required.

```json
{
  "change_id": "chg-xxxxxxxxxxxx",
  "state": "timeout",
  "applied_apps": []
}
```

### `partial`

Resume with state `partial` and an `approver`: some apps were applied and `failed_app` names the one that failed.

```json
{
  "change_id": "chg-xxxxxxxxxxxx",
  "state": "partial",
  "applied_apps": [
    "stoneos-notes"
  ],
  "failed_app": "stoneos-todo",
  "approver": "admin@example.com"
}
```

### `error`

A proposal step failed, or the resume is invalid or reports an error. `data` carries only `error_code`; the message is in the envelope `error` field, which does not reach the next node.

```json
{
  "error_code": "missing_unit"
}
```

## Behaviour in the code

- `resume`: Not declared. When the config or the incoming payload carries `resume`, the node maps it to a port instead of proposing a change.
- `proposal_id`: Declared but ignored, like `operation`.
- `timeout_ms`: Default 72 hours. Only returned as `__metadata.expires_in_ms`; the orchestrator does not enforce it. `timeout` comes only from a resume with state `timeout`.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Missing 'unit'` | `error` | No business unit code. `error_code`: `missing_unit`. |
| `Missing 'package_id'` | `error` | No package. `error_code`: `missing_package_id`. |
| `services.stoneosIndustry.uri is not resolved` | `error` | The orchestrator has no StoneOS industry service configured. `error_code`: `service_not_configured`. |
| `The change was not created` | `error` | The service answered without a change id. `error_code`: `change_not_created`. |
| `The change preview was not created` | `error` | The preview has no `installation_id` or `installation_preview_hash`. `error_code`: `preview_failed`. |
| `<HTTP error message>` | `error` | The service rejected a call; its own `error_code` is used when it sends one. `error_code`: `request_failed`. |
| `Unknown resume state '<port>'` | `error` | The resume port is not `applied`, `rejected`, `timeout`, `partial` or `error`. `error_code`: `unknown_resume_state`. |
| `The '<port>' port needs the person who approved` | `error` | `applied`, `rejected` and `partial` require `resume.approver`. `error_code`: `approval_missing`. |
| `<resume error>` | `error` | The resume reports `port` or `state` `error`; `data` keeps the resume fields plus `error_code` (default `apply_failed`). |

## Notes and limits

- The change request sends `workflow_instance_id` and `node_id` empty (the worker reads them from fields the runtime does not set), so the service is not told which run proposed the change; the caller of the resume operation must supply `workflow_instance_id`, `node_id` and `graph`.
- The continuation is a new run: outputs of the nodes before this one are not available (`$("Node")` is empty) and `$input` is the resume `input`.
- `rejected`, `timeout` and `partial` have `ok: false` but still follow their own port. The worker only proposes and waits; applying the package is done by the StoneOS app after approval.


## Contracts (JSON Schema)

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

```json
{
  "slug": "stoneos.industry.apply",
  "name": "Apply StoneOS Industry Package",
  "category": "hitl",
  "version": "1.0.0",
  "description": "Proposes an industry package for a business unit, previews it and waits for a person with permission to approve. The app applies the change; the worker only proposes, waits and resumes.",
  "capabilities": [
    "industry",
    "configuration",
    "human"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "unit",
      "package_id"
    ],
    "properties": {
      "unit": {
        "type": "string",
        "title": "Business unit",
        "description": "Business unit code (expression allowed)"
      },
      "package_id": {
        "type": "string",
        "title": "Package"
      },
      "proposal_id": {
        "type": "string",
        "title": "Proposal"
      },
      "role": {
        "type": "string",
        "enum": [
          "principal",
          "complement"
        ],
        "default": "principal"
      },
      "size": {
        "type": "string",
        "title": "Size"
      },
      "jurisdiction": {
        "type": "string",
        "title": "Jurisdiction",
        "description": "Jurisdiction code; defaults to the organization one"
      },
      "reason": {
        "type": "string",
        "title": "Reason"
      },
      "operation": {
        "type": "string",
        "title": "Operation"
      },
      "approver_role": {
        "type": "string",
        "default": "administrator",
        "title": "Approver role"
      },
      "timeout_ms": {
        "type": "integer",
        "default": 259200000
      },
      "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": {
      "unit": {
        "type": "string"
      },
      "package_id": {
        "type": "string"
      },
      "proposal_id": {
        "type": "string"
      },
      "role": {
        "type": "string"
      },
      "size": {
        "type": "string"
      },
      "operation": {
        "type": "string"
      },
      "approver_role": {
        "type": "string"
      },
      "timeout_ms": {
        "type": "integer"
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "change_id": {
        "type": "string"
      },
      "state": {
        "type": "string"
      },
      "applied_apps": {
        "type": "array",
        "items": {
          "type": "string"
        }
      },
      "failed_app": {
        "type": "string"
      },
      "approver": {
        "type": "string"
      },
      "error_code": {
        "type": "string"
      }
    }
  },
  "ports": [
    {
      "port_id": "applied",
      "label": "Applied",
      "is_default": true,
      "description": "Approved and applied"
    },
    {
      "port_id": "rejected",
      "label": "Rejected",
      "description": "A person rejected the change"
    },
    {
      "port_id": "timeout",
      "label": "Timeout",
      "description": "Nobody decided in time"
    },
    {
      "port_id": "partial",
      "label": "Partial",
      "description": "Applied halfway; the flow decides"
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Carries error_code"
    }
  ],
  "execution_mode": "async",
  "tags": [
    "industry",
    "configuration",
    "human"
  ],
  "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",
  "required": [
    "unit",
    "package_id"
  ],
  "properties": {
    "unit": {
      "type": "string",
      "title": "Business unit",
      "description": "Business unit code (expression allowed)"
    },
    "package_id": {
      "type": "string",
      "title": "Package"
    },
    "proposal_id": {
      "type": "string",
      "title": "Proposal"
    },
    "role": {
      "type": "string",
      "enum": [
        "principal",
        "complement"
      ],
      "default": "principal"
    },
    "size": {
      "type": "string",
      "title": "Size"
    },
    "jurisdiction": {
      "type": "string",
      "title": "Jurisdiction",
      "description": "Jurisdiction code; defaults to the organization one"
    },
    "reason": {
      "type": "string",
      "title": "Reason"
    },
    "operation": {
      "type": "string",
      "title": "Operation"
    },
    "approver_role": {
      "type": "string",
      "default": "administrator",
      "title": "Approver role"
    },
    "timeout_ms": {
      "type": "integer",
      "default": 259200000
    },
    "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": {
    "unit": {
      "type": "string"
    },
    "package_id": {
      "type": "string"
    },
    "proposal_id": {
      "type": "string"
    },
    "role": {
      "type": "string"
    },
    "size": {
      "type": "string"
    },
    "operation": {
      "type": "string"
    },
    "approver_role": {
      "type": "string"
    },
    "timeout_ms": {
      "type": "integer"
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "change_id": {
      "type": "string"
    },
    "state": {
      "type": "string"
    },
    "applied_apps": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "failed_app": {
      "type": "string"
    },
    "approver": {
      "type": "string"
    },
    "error_code": {
      "type": "string"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "applied",
    "label": "Applied",
    "is_default": true,
    "description": "Approved and applied"
  },
  {
    "port_id": "rejected",
    "label": "Rejected",
    "description": "A person rejected the change"
  },
  {
    "port_id": "timeout",
    "label": "Timeout",
    "description": "Nobody decided in time"
  },
  {
    "port_id": "partial",
    "label": "Partial",
    "description": "Applied halfway; the flow decides"
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Carries error_code"
  }
]
```
