{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/Ran-sh/chatgpt_workflow/schema/result-contract.schema.json",
  "title": "Agent Workflow Result Contract",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "task_id",
    "source_commit",
    "status",
    "changed_files",
    "tests",
    "blockers",
    "result_path"
  ],
  "properties": {
    "schema_version": { "type": "integer", "const": 2 },
    "task_id": { "type": "string", "minLength": 1 },
    "source_commit": { "type": "string", "minLength": 1 },
    "result_commit": { "description": "Optional commit containing work completed before this Result was written. Null is recommended when no such commit applies; the commit containing this file is derived from Git history.", "type": ["string", "null"] },
    "status": {
      "type": "string",
      "enum": ["PASS", "FAIL", "PARTIAL", "SKIP", "BLOCKED", "NOT RUN"]
    },
    "summary": { "type": "string" },
    "timeline": {
      "type": "object",
      "additionalProperties": false,
      "required": ["started_at", "completed_at"],
      "properties": {
        "started_at": {
          "type": "string",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:Z|[+-]\\d{2}:\\d{2})$"
        },
        "completed_at": {
          "type": "string",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:Z|[+-]\\d{2}:\\d{2})$"
        }
      }
    },
    "changed_files": {
      "type": "array",
      "items": { "type": "string", "pattern": "^(?!/)(?![A-Za-z]:)(?!.*\\\\)(?!.*(?:^|/)\\.\\.(?:/|$))[^*?\\[\\]{}\\u0000]+$" },
      "uniqueItems": true
    },
    "tests": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["name", "status"],
        "properties": {
          "name": { "type": "string", "minLength": 1 },
          "status": {
            "type": "string",
            "enum": ["PASS", "FAIL", "PARTIAL", "SKIP", "BLOCKED", "NOT RUN"]
          },
          "evidence": { "type": "string" }
        }
      }
    },
    "blockers": {
      "type": "array",
      "items": { "type": "string" }
    },
    "result_path": { "type": "string", "minLength": 1, "pattern": "^docs/agent-results/(?!.*(?:^|/)\\.\\.(?:/|$))(?!.*\\\\)[^*?\\[\\]{}\\u0000]+$" },
    "result_validation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "validator", "validated_at", "evidence"],
      "properties": {
        "status": { "const": "PASS" },
        "validator": { "type": "string", "minLength": 1 },
        "validated_at": {
          "type": "string",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:Z|[+-]\\d{2}:\\d{2})$"
        },
        "evidence": { "type": "string", "minLength": 1 }
      }
    },
    "notes": { "type": "array", "items": { "type": "string" } }
  },
  "allOf": [
    {
      "if": {
        "required": ["schema_version"],
        "properties": { "schema_version": { "const": 2 } }
      },
      "then": {
        "required": ["timeline", "result_validation"]
      }
    }
  ]
}
