{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Workflow Rules Schema",
  "description": "Schema for opencode-kit workflow rules — defines step sequence, dependencies, and validation",
  "type": "object",
  "required": ["_meta", "workflow", "steps"],
  "properties": {
    "_meta": {
      "type": "object",
      "description": "Inheritance metadata — enables extension and override",
      "properties": {
        "extends": {
          "type": ["string", "null"],
          "description": "Parent workflow rules identifier (null = root)"
        },
        "version": {
          "type": "string",
          "description": "Version of these workflow rules"
        },
        "overrides": {
          "type": "array",
          "items": { "type": "string" },
          "description": "Field paths to override (not merge)"
        },
        "appends": {
          "type": "array",
          "items": { "type": "string" },
          "description": "Field paths to append to (array concat)"
        },
        "excludes": {
          "type": "array",
          "items": { "type": "string" },
          "description": "Inherited items to exclude"
        }
      },
      "required": ["extends"]
    },
    "workflow": {
      "type": "object",
      "description": "Workflow state machine definition",
      "properties": {
        "name": { "type": "string" },
        "description": { "type": "string" },
        "states": {
          "type": "array",
          "items": { "type": "string" },
          "description": "Valid workflow states"
        },
        "transitions": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "from": { "type": "string" },
              "to": { "type": "string" },
              "condition": { "type": "string" }
            },
            "required": ["from", "to", "condition"]
          }
        }
      },
      "required": ["name", "states", "transitions"]
    },
    "steps": {
      "type": "array",
      "description": "Step definitions — ordered sequence of agent actions",
      "items": {
        "type": "object",
        "required": ["id", "name", "agent", "phase", "inputs", "outputs", "dependencies"],
        "properties": {
          "id": {
            "type": "string",
            "description": "Unique step identifier (kebab-case)"
          },
          "name": {
            "type": "string",
            "description": "Human-readable step name"
          },
          "description": {
            "type": "string",
            "description": "What this step does"
          },
          "agent": {
            "type": "string",
            "description": "Agent that executes this step"
          },
          "phase": {
            "type": "string",
            "enum": ["INIT", "PLAN", "EXECUTE", "REVIEW", "COMPLETE"],
            "description": "Workflow phase"
          },
          "inputs": {
            "type": "array",
            "items": { "type": "string" },
            "description": "Required inputs (from previous steps or external)"
          },
          "outputs": {
            "type": "array",
            "items": { "type": "string" },
            "description": "Produced outputs (available to downstream steps)"
          },
          "dependencies": {
            "type": "array",
            "items": { "type": "string" },
            "description": "Step IDs that must complete before this step"
          },
          "validation": {
            "type": "object",
            "properties": {
              "required": {
                "type": "array",
                "items": { "type": "string" },
                "description": "Required validation conditions"
              },
              "checks": {
                "type": "array",
                "items": { "type": "string" },
                "description": "Additional validation checks"
              }
            }
          },
          "on_failure": {
            "type": "string",
            "enum": ["RETRY", "BLOCKED", "SKIP", "WARN"],
            "description": "Action when validation fails"
          },
          "timeout_seconds": {
            "type": "integer",
            "description": "Max execution time in seconds"
          }
        }
      }
    },
    "scoring": {
      "type": "object",
      "description": "Scoring pipeline configuration",
      "properties": {
        "thresholds": {
          "type": "object",
          "properties": {
            "pass": { "type": "number", "minimum": 0, "maximum": 100 },
            "retry": { "type": "number", "minimum": 0, "maximum": 100 },
            "block": { "type": "number", "minimum": 0, "maximum": 100 }
          }
        },
        "max_retries": { "type": "integer", "minimum": 0 },
        "tiers": { "type": "object" }
      }
    },
    "validation": {
      "type": "object",
      "description": "Pre-flight and post-flight validation rules",
      "properties": {
        "pre_flight": { "type": "object" },
        "post_flight": { "type": "object" }
      }
    }
  }
}