{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/workflow/workflow.v1.schema.json",
  "title": "RootBlocks Workflow v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "workflowId",
    "name",
    "type",
    "version",
    "standard",
    "startNodeId",
    "nodes",
    "edges",
    "failurePolicy",
    "evidence"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.workflow/v1"
    },
    "workflowId": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "type": {
      "enum": ["application", "software-process", "infrastructure", "cicd", "test", "regression-support"]
    },
    "version": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[A-Za-z0-9.-]+)?(\\+[A-Za-z0-9.-]+)?$"
    },
    "standard": {
      "$ref": "#/$defs/standardRef"
    },
    "inputs": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/port"
      }
    },
    "outputs": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/port"
      }
    },
    "startNodeId": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{1,127}$"
    },
    "nodes": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/node"
      }
    },
    "edges": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/edge"
      }
    },
    "failurePolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["defaultStrategy"],
      "properties": {
        "defaultStrategy": {
          "enum": ["fail-workflow", "continue", "retry", "compensate", "manual-review"]
        },
        "maxRetries": {
          "type": "integer",
          "minimum": 0,
          "maximum": 10
        }
      }
    },
    "sourceMapRef": {
      "type": "string",
      "pattern": "^builderx/source-maps/.+\\.(json|yaml|yml)$"
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "events"],
      "properties": {
        "required": {
          "type": "boolean"
        },
        "events": {
          "type": "array",
          "items": {
            "enum": ["validated", "compiled", "started", "node-completed", "failed", "completed", "regression-linked"]
          },
          "uniqueItems": true
        }
      }
    }
  },
  "$defs": {
    "standardRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "version", "hash"],
      "properties": {
        "bundleId": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "version": {
          "type": "string",
          "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[A-Za-z0-9.-]+)?(\\+[A-Za-z0-9.-]+)?$"
        },
        "hash": {
          "type": "string",
          "pattern": "^[a-f0-9]{64,128}$"
        }
      }
    },
    "port": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "type", "required"],
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[a-zA-Z_][a-zA-Z0-9_.-]{0,80}$"
        },
        "type": {
          "enum": ["string", "number", "integer", "boolean", "object", "array", "secret-ref", "any"]
        },
        "required": {
          "type": "boolean"
        },
        "description": {
          "type": "string",
          "maxLength": 500
        }
      }
    },
    "node": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "name", "kind", "operation", "onFailure"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{1,127}$"
        },
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "kind": {
          "enum": ["start", "end", "action", "decision", "component", "gate", "human-approval", "subflow", "adapter", "emit-evidence"]
        },
        "operation": {
          "enum": [
            "start",
            "end",
            "transform",
            "condition",
            "component-call",
            "http-request",
            "data-query",
            "data-command",
            "emit-event",
            "manual-gate",
            "run-test",
            "run-command",
            "iac-plan",
            "iac-apply",
            "iac-drift",
            "iac-destroy",
            "ci-build",
            "ci-test",
            "ci-deploy",
            "emit-evidence"
          ]
        },
        "componentRef": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}(@[0-9]+\\.[0-9]+\\.[0-9]+)?$"
        },
        "config": {
          "type": "object"
        },
        "onFailure": {
          "$ref": "#/$defs/onFailure"
        }
      }
    },
    "onFailure": {
      "type": "object",
      "additionalProperties": false,
      "required": ["strategy"],
      "properties": {
        "strategy": {
          "enum": ["fail-workflow", "continue", "retry", "compensate", "manual-review"]
        },
        "maxRetries": {
          "type": "integer",
          "minimum": 0,
          "maximum": 10
        }
      }
    },
    "edge": {
      "type": "object",
      "additionalProperties": false,
      "required": ["from", "to"],
      "properties": {
        "from": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{1,127}$"
        },
        "to": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{1,127}$"
        },
        "condition": {
          "type": "string",
          "maxLength": 500
        }
      }
    }
  }
}

