{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://oms.local/schemas/result-gate-payload.schema.json",
  "title": "OMS protected result gate payload",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "gate_id",
    "subject",
    "created_at",
    "updated_at",
    "deterministic_evidence",
    "grade_request",
    "grade_evidence",
    "reflection_request",
    "reflection_evidence",
    "outcome",
    "trust_evidence",
    "blocked_reason"
  ],
  "properties": {
    "gate_id": {
      "type": "string",
      "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
    },
    "subject": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "goal_id",
        "goal_version",
        "dispatch_id",
        "dispatch_version",
        "ticket_id",
        "verifying_ticket_version",
        "attempt",
        "max_attempts",
        "lease_token",
        "worker_record_id",
        "worker_id",
        "session_id",
        "executor_role",
        "executor_engine",
        "worktree_id",
        "worktree_root",
        "worktree_branch",
        "worktree_base_commit",
        "expected_commit",
        "policy_version",
        "policy_digest",
        "plan_digest",
        "verification_plan_ref",
        "executor_evidence"
      ],
      "properties": {
        "goal_id": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "goal_version": { "type": "integer", "minimum": 0 },
        "dispatch_id": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "dispatch_version": { "type": "integer", "minimum": 0 },
        "ticket_id": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "verifying_ticket_version": { "type": "integer", "minimum": 0 },
        "attempt": { "type": "integer", "minimum": 1 },
        "max_attempts": { "type": "integer", "minimum": 1 },
        "lease_token": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "worker_record_id": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "worker_id": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "session_id": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "executor_role": {
          "enum": ["oms.ticket-executor", "oms.goal-long-executor"]
        },
        "executor_engine": { "enum": ["claude", "codex"] },
        "worktree_id": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "worktree_root": { "type": "string", "pattern": "^/", "maxLength": 4096 },
        "worktree_branch": { "type": "string", "minLength": 1, "maxLength": 255 },
        "worktree_base_commit": {
          "type": "string",
          "pattern": "^[a-f0-9]{40}(?:[a-f0-9]{24})?$"
        },
        "expected_commit": {
          "type": "string",
          "pattern": "^[a-f0-9]{40}(?:[a-f0-9]{24})?$"
        },
        "policy_version": { "type": "integer", "minimum": 1 },
        "policy_digest": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "plan_digest": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "verification_plan_ref": {
          "type": "object",
          "additionalProperties": false,
          "required": ["evidence_id", "sha256", "file_name"],
          "properties": {
            "evidence_id": {
              "type": "string",
              "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
            },
            "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
            "file_name": { "type": "string", "minLength": 6, "maxLength": 134 }
          }
        },
        "executor_evidence": {
          "type": "object",
          "additionalProperties": false,
          "required": ["evidence_id", "sha256", "file_name"],
          "properties": {
            "evidence_id": {
              "type": "string",
              "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
            },
            "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
            "file_name": { "type": "string", "minLength": 6, "maxLength": 134 }
          }
        }
      }
    },
    "created_at": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}Z$"
    },
    "updated_at": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}Z$"
    },
    "deterministic_evidence": {
      "type": ["object", "null"],
      "additionalProperties": false,
      "required": ["evidence_id", "sha256", "file_name"],
      "properties": {
        "evidence_id": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "file_name": { "type": "string", "minLength": 6, "maxLength": 134 }
      }
    },
    "grade_request": {
      "type": ["object", "null"],
      "additionalProperties": false,
      "required": ["request_id", "role_id", "engine", "evidence_schema", "input", "started_at", "session_id"],
      "properties": {
        "request_id": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "role_id": { "enum": ["oms.grader.codex", "oms.grader.claude"] },
        "engine": { "enum": ["codex", "claude"] },
        "evidence_schema": { "const": "grader-output.v1" },
        "input": { "type": "object" },
        "started_at": {
          "type": ["string", "null"],
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}Z$"
        },
        "session_id": { "type": ["string", "null"], "maxLength": 128 }
      }
    },
    "grade_evidence": {
      "type": ["object", "null"],
      "additionalProperties": false,
      "required": ["evidence_id", "sha256", "file_name"],
      "properties": {
        "evidence_id": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "file_name": { "type": "string", "minLength": 6, "maxLength": 134 }
      }
    },
    "reflection_request": {
      "type": ["object", "null"],
      "additionalProperties": false,
      "required": ["request_id", "role_id", "engine", "evidence_schema", "input", "started_at", "session_id"],
      "properties": {
        "request_id": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "role_id": { "const": "oms.bounded-dreamer" },
        "engine": { "const": "claude" },
        "evidence_schema": { "const": "reflection.v1" },
        "input": { "type": "object" },
        "started_at": {
          "type": ["string", "null"],
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}Z$"
        },
        "session_id": { "type": ["string", "null"], "maxLength": 128 }
      }
    },
    "reflection_evidence": {
      "type": ["object", "null"],
      "additionalProperties": false,
      "required": ["evidence_id", "sha256", "file_name"],
      "properties": {
        "evidence_id": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "file_name": { "type": "string", "minLength": 6, "maxLength": 134 }
      }
    },
    "outcome": {
      "type": ["string", "null"],
      "enum": ["completed", "retryable", "blocked", null]
    },
    "trust_evidence": {
      "type": ["object", "null"],
      "additionalProperties": false,
      "required": ["evidence_id", "sha256", "file_name"],
      "properties": {
        "evidence_id": {
          "type": "string",
          "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$"
        },
        "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "file_name": { "type": "string", "minLength": 6, "maxLength": 134 }
      }
    },
    "blocked_reason": { "type": ["string", "null"], "minLength": 1, "maxLength": 2048 }
  }
}
