{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Pi workflow",
  "type": "object",
  "additionalProperties": false,
  "required": ["version", "id", "command", "description", "start", "steps"],
  "properties": {
    "$schema": {
      "type": "string"
    },
    "version": {
      "const": 1
    },
    "id": {
      "$ref": "#/$defs/identifier"
    },
    "command": {
      "allOf": [
        {
          "$ref": "#/$defs/identifier"
        },
        {
          "not": {
            "enum": [
              "arminsayshi",
              "changelog",
              "clone",
              "compact",
              "copy",
              "debug",
              "dementedelves",
              "export",
              "fork",
              "hotkeys",
              "import",
              "login",
              "logout",
              "model",
              "name",
              "new",
              "quit",
              "reload",
              "resume",
              "scoped-models",
              "session",
              "settings",
              "share",
              "tree",
              "trust",
              "workflow-abort",
              "workflow-doctor",
              "workflow-list",
              "workflow-pause",
              "workflow-reload",
              "workflow-restart",
              "workflow-resume",
              "workflow-start",
              "workflow-status"
            ]
          }
        }
      ]
    },
    "description": {
      "type": "string",
      "minLength": 1
    },
    "start": {
      "$ref": "#/$defs/identifier"
    },
    "maxStepVisits": {
      "type": "integer",
      "minimum": 1,
      "maximum": 100,
      "default": 5
    },
    "summaryMaxChars": {
      "type": "integer",
      "minimum": 100,
      "maximum": 50000,
      "default": 4000
    },
    "steps": {
      "type": "object",
      "minProperties": 1,
      "propertyNames": {
        "$ref": "#/$defs/identifier"
      },
      "additionalProperties": {
        "$ref": "#/$defs/step"
      }
    }
  },
  "$defs": {
    "identifier": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9-]{0,63}$"
    },
    "relativePath": {
      "type": "string",
      "minLength": 1,
      "maxLength": 4096,
      "pattern": "^\\S(?:.*\\S)?$",
      "allOf": [
        {
          "not": {
            "pattern": "^/"
          }
        },
        {
          "not": {
            "pattern": "^[A-Za-z]:"
          }
        },
        {
          "not": {
            "pattern": "^\\\\"
          }
        },
        {
          "not": {
            "pattern": "\\u0000"
          }
        }
      ]
    },
    "workspaceRoot": {
      "type": "string",
      "minLength": 1,
      "maxLength": 4096,
      "pattern": "^\\S(?:.*\\S)?$",
      "allOf": [
        {
          "not": {
            "pattern": "\\u0000"
          }
        }
      ]
    },
    "resourceName": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_@./:+-]+$"
    },
    "agentRuntimeName": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9-]*(?:\\.[a-z0-9][a-z0-9-]*)*$"
    },
    "prompt": {
      "type": "object",
      "additionalProperties": false,
      "required": ["file"],
      "properties": {
        "file": {
          "$ref": "#/$defs/relativePath"
        }
      }
    },
    "bashRule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["executable"],
      "allOf": [
        {
          "not": {
            "required": ["argsPrefix", "argsPrefixes"]
          }
        }
      ],
      "properties": {
        "executable": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_./+-]+$"
        },
        "argsPrefix": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^\\S+$"
          },
          "default": []
        },
        "argsPrefixes": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "array",
            "minItems": 1,
            "items": {
              "type": "string",
              "pattern": "^\\S+$"
            }
          }
        }
      }
    },
    "bash": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode"],
      "allOf": [
        {
          "if": {
            "properties": {
              "mode": {
                "const": "allow-list"
              }
            },
            "required": ["mode"]
          },
          "then": {
            "required": ["allow"],
            "properties": {
              "allow": {
                "minItems": 1
              }
            }
          },
          "else": {
            "properties": {
              "allow": {
                "maxItems": 0
              }
            }
          }
        }
      ],
      "properties": {
        "mode": {
          "enum": ["deny", "allow-list", "unrestricted"]
        },
        "allow": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/bashRule"
          }
        }
      }
    },
    "permissions": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "tools": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^[A-Za-z0-9_.:-]+$"
          }
        },
        "mcp": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^[A-Za-z0-9_.:-]+(?:/[A-Za-z0-9_.:-]+)?$"
          }
        },
        "skills": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/resourceName"
          }
        },
        "bash": {
          "$ref": "#/$defs/bash"
        }
      }
    },
    "artifactContract": {
      "type": "object",
      "additionalProperties": false,
      "required": ["maxChars", "requiredHeadings"],
      "properties": {
        "maxChars": {
          "type": "integer",
          "minimum": 1,
          "maximum": 200000
        },
        "requiredHeadings": {
          "type": "array",
          "minItems": 1,
          "maxItems": 32,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["level", "title", "guidance"],
            "properties": {
              "level": { "type": "integer", "enum": [1, 2, 3] },
              "title": {
                "type": "string",
                "minLength": 1,
                "maxLength": 1024
              },
              "guidance": {
                "type": "string",
                "minLength": 1,
                "maxLength": 1024
              }
            }
          }
        }
      }
    },
    "gate": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "timeoutMs": {
          "type": "integer",
          "minimum": 1000,
          "maximum": 30000,
          "default": 30000
        },
        "artifactContract": {
          "$ref": "#/$defs/artifactContract"
        }
      }
    },
    "workspace": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bindOn"],
      "properties": {
        "bindOn": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "const": "ready"
          }
        },
        "allowedRoots": {
          "type": "array",
          "minItems": 1,
          "maxItems": 32,
          "uniqueItems": true,
          "default": ["."],
          "items": {
            "$ref": "#/$defs/workspaceRoot"
          }
        }
      }
    },
    "step": {
      "type": "object",
      "additionalProperties": false,
      "required": ["prompt", "transitions"],
      "properties": {
        "title": {
          "type": "string",
          "minLength": 1
        },
        "prompt": {
          "$ref": "#/$defs/prompt"
        },
        "agent": {
          "$ref": "#/$defs/agentRuntimeName"
        },
        "maxToolCalls": {
          "type": "integer",
          "minimum": 1,
          "maximum": 100000
        },
        "permissions": {
          "$ref": "#/$defs/permissions"
        },
        "transitions": {
          "type": "object",
          "minProperties": 1,
          "propertyNames": {
            "enum": ["ready", "blocked", "handoff", "gaps"]
          },
          "additionalProperties": {
            "type": "string",
            "pattern": "^(?:\\$done|\\$pause|[a-z][a-z0-9-]{0,63})$"
          }
        },
        "gate": {
          "$ref": "#/$defs/gate"
        },
        "workspace": {
          "$ref": "#/$defs/workspace"
        }
      }
    }
  }
}
