{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/Ran-sh/gpt-relay/schema/task-contract.schema.json",
  "title": "GPT Relay Task Contract",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "id",
    "mode",
    "source_branch",
    "source_commit",
    "objective",
    "context",
    "allowed_changes",
    "forbidden_changes",
    "validation",
    "acceptance_criteria",
    "result_contract",
    "completion_commit_contract",
    "delete_active_task_on_completion"
  ],
  "properties": {
    "id": { "type": "string", "minLength": 1 },
    "mode": { "type": "string", "enum": ["IMPLEMENT", "TEST_ONLY", "REVIEW_ONLY"] },
    "source_branch": { "type": "string", "minLength": 1 },
    "source_commit": { "type": "string", "minLength": 1 },
    "objective": { "type": "string", "minLength": 1 },
    "parent_objective": { "type": "string", "minLength": 1 },
    "context": { "type": "string" },
    "required_capabilities": {
      "type": "array",
      "items": { "type": "string", "minLength": 1 },
      "uniqueItems": true
    },
    "allowed_changes": {
      "type": "array",
      "items": { "type": "string", "minLength": 1, "pattern": "^(?!/)(?![A-Za-z]:)(?!.*\\\\)(?!.*(?:^|/)\\.\\.(?:/|$))[^\\u0000]+$" },
      "uniqueItems": true
    },
    "forbidden_changes": {
      "type": "array",
      "items": { "type": "string", "minLength": 1 },
      "minItems": 1,
      "uniqueItems": true
    },
    "validation": {
      "type": "array",
      "items": { "type": "string", "minLength": 1 },
      "minItems": 1,
      "uniqueItems": true
    },
    "acceptance_criteria": {
      "type": "array",
      "items": { "type": "string", "minLength": 1 },
      "minItems": 1,
      "uniqueItems": true
    },
    "result_contract": { "type": "string", "minLength": 1, "pattern": "^docs/agent-results/(?!.*(?:^|/)\\.\\.(?:/|$))(?!.*\\\\)[^*?\\[\\]{}\\u0000]+$" },
    "completion_commit_contract": {
      "type": "array",
      "items": { "type": "string", "minLength": 1, "pattern": "^(?!/)(?![A-Za-z]:)(?!.*\\\\)(?!.*(?:^|/)\\.\\.(?:/|$))[^\\u0000]+$" },
      "uniqueItems": true
    },
    "delete_active_task_on_completion": { "type": "boolean", "const": true },
    "delegated_scope": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "objective",
        "required_capabilities",
        "allowed_changes",
        "forbidden_changes",
        "return"
      ],
      "properties": {
        "objective": { "type": "string", "minLength": 1 },
        "required_capabilities": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 },
          "minItems": 1,
          "uniqueItems": true
        },
        "allowed_changes": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 },
          "uniqueItems": true
        },
        "forbidden_changes": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 },
          "minItems": 1,
          "uniqueItems": true
        },
        "validation": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 },
          "uniqueItems": true
        },
        "return": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 },
          "minItems": 1,
          "uniqueItems": true
        }
      }
    },
    "authorization": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "shell",
        "network",
        "browser_login",
        "credentials",
        "git_commit",
        "git_push",
        "publish",
        "deploy_production",
        "destructive_operations"
      ],
      "properties": {
        "shell": { "type": "boolean" },
        "network": { "type": "boolean" },
        "browser_login": { "type": "boolean" },
        "credentials": { "type": "boolean" },
        "git_commit": { "type": "boolean" },
        "git_push": { "type": "boolean" },
        "publish": { "type": "boolean" },
        "deploy_production": { "type": "boolean" },
        "destructive_operations": { "type": "boolean" }
      }
    },
    "metadata": {
      "type": "object",
      "additionalProperties": { "type": ["string", "number", "boolean", "null"] }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "mode": { "enum": ["TEST_ONLY", "REVIEW_ONLY"] }
        },
        "required": ["mode"]
      },
      "then": {
        "properties": {
          "allowed_changes": {
            "items": { "type": "string", "pattern": "^docs/agent-results/" }
          },
          "completion_commit_contract": {
            "items": {
              "anyOf": [
                { "type": "string", "pattern": "^docs/agent-results/" },
                { "const": "docs/agent-tasks/ACTIVE_TASK.json" },
                { "const": "docs/agent-tasks/ACTIVE_TASK.md" }
              ]
            }
          }
        }
      }
    },
    {
      "if": {
        "anyOf": [
          { "required": ["required_capabilities"] },
          { "required": ["delegated_scope"] },
          { "required": ["authorization"] }
        ]
      },
      "then": {
        "required": ["required_capabilities", "delegated_scope", "authorization"]
      }
    }
  ]
}
