{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.anguskit.com/schema/anguscopilot/1.0.0/procedure.json",
  "title": "AngusCopilot Procedure Specification",
  "description": "AngusCopilot Procedure 顶层 Spec（specification: anguscopilot-procedure/1.0.0）。Procedure 是多步业务编排，单步调用请用 Tool。",

  "type": "object",
  "required": ["specification", "info", "spec"],

  "properties": {

    "specification": {
      "$ref": "common.json#/$defs/SpecificationField",
      "pattern": "^anguscopilot-procedure\\/(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-(?:alpha|beta|rc)(?:\\.(?:0|[1-9]\\d*))?)?$"
    },

    "info": { "$ref": "info.json" },

    "spec": {
      "type": "object",
      "required": ["input", "output", "steps"],
      "properties": {

        "input": {
          "description": "Procedure 输入参数 JSON Schema（嵌入 2020-12 子文档）。",
          "$ref": "common.json#/$defs/JsonSchemaRef"
        },
        "output": {
          "description": "Procedure 输出结果 JSON Schema。",
          "$ref": "common.json#/$defs/JsonSchemaRef"
        },

        "safety": {
          "description": "Procedure 整体安全等级，影响 SafetyClassGate 判定。",
          "type": "string",
          "enum": ["safe", "destructive", "sensitive"],
          "default": "safe"
        },

        "timeout": {
          "description": "Procedure 整体执行超时。",
          "$ref": "common.json#/$defs/Duration"
        },

        "dependencies": {
          "description": "Procedure 依赖的其他 Capability（Tool / Procedure）。",
          "type": "array",
          "items": { "$ref": "common.json#/$defs/CapabilityRef" },
          "uniqueItems": true
        },

        "steps": {
          "description": "Procedure 步骤定义。Procedure 必须是多步编排（≥ 2 步）；单步请直接声明 Tool。",
          "type": "array",
          "minItems": 2,
          "items": {
            "type": "object",
            "required": ["id", "type"],
            "properties": {
              "id": { "$ref": "common.json#/$defs/Identifier" },
              "type": {
                "type": "string",
                "enum": ["invoke-capability", "branch", "parallel", "loop", "compensate"]
              },
              "ref": {
                "description": "type=invoke-capability 时引用的 Capability。",
                "$ref": "common.json#/$defs/CapabilityRef"
              },
              "input": {
                "description": "步骤输入映射，可使用 SpEL 表达式引用上一步输出（如 '${steps.step1.output.userId}'）。",
                "type": "object",
                "additionalProperties": true
              },
              "when": {
                "description": "步骤执行条件（SpEL 表达式）。",
                "type": "string"
              },
              "branches": {
                "description": "type=branch 时的分支定义。",
                "type": "array",
                "items": {
                  "type": "object",
                  "required": ["when", "stepIds"],
                  "properties": {
                    "when": { "type": "string" },
                    "stepIds": {
                      "type": "array",
                      "items": { "$ref": "common.json#/$defs/Identifier" },
                      "minItems": 1
                    }
                  },
                  "unevaluatedProperties": false
                }
              },
              "parallel": {
                "description": "type=parallel 时的并行子步骤 ID 列表。",
                "type": "array",
                "items": { "$ref": "common.json#/$defs/Identifier" },
                "minItems": 2,
                "uniqueItems": true
              },
              "loop": {
                "description": "type=loop 时的循环配置。",
                "type": "object",
                "properties": {
                  "iterator": { "type": "string", "description": "SpEL 表达式生成可迭代集合" },
                  "maxIterations": { "type": "integer", "minimum": 1, "default": 100 },
                  "stepIds": {
                    "type": "array",
                    "items": { "$ref": "common.json#/$defs/Identifier" },
                    "minItems": 1
                  }
                },
                "required": ["iterator", "stepIds"],
                "unevaluatedProperties": false
              },
              "compensate": {
                "description": "type=compensate 时的补偿配置（指向需要撤销的目标步骤）。",
                "type": "object",
                "properties": {
                  "targetStepId": { "$ref": "common.json#/$defs/Identifier" },
                  "ref": { "$ref": "common.json#/$defs/CapabilityRef" }
                },
                "required": ["targetStepId"],
                "unevaluatedProperties": false
              },
              "retry": {
                "type": "object",
                "properties": {
                  "maxAttempts": { "type": "integer", "minimum": 1, "default": 1 },
                  "backoff": { "$ref": "common.json#/$defs/Duration", "default": "1s" }
                },
                "unevaluatedProperties": false
              },
              "timeout": { "$ref": "common.json#/$defs/Duration" }
            },
            "patternProperties": { "^x-": true },
            "unevaluatedProperties": false,

            "$comment": "type 与配套字段必填检查。",
            "allOf": [
              {
                "if": { "properties": { "type": { "const": "invoke-capability" } }, "required": ["type"] },
                "then": { "required": ["ref"] }
              },
              {
                "if": { "properties": { "type": { "const": "branch" } }, "required": ["type"] },
                "then": { "required": ["branches"] }
              },
              {
                "if": { "properties": { "type": { "const": "parallel" } }, "required": ["type"] },
                "then": { "required": ["parallel"] }
              },
              {
                "if": { "properties": { "type": { "const": "loop" } }, "required": ["type"] },
                "then": { "required": ["loop"] }
              },
              {
                "if": { "properties": { "type": { "const": "compensate" } }, "required": ["type"] },
                "then": { "required": ["compensate"] }
              }
            ]
          }
        },

        "credentials": { "$ref": "credentials.json" }
      },
      "patternProperties": { "^x-": true },
      "unevaluatedProperties": false
    }
  },
  "patternProperties": { "^x-": true },
  "unevaluatedProperties": false
}
