{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://matrixnetwork.me/schemas/y-theory.action-challenge.v1.schema.json",
  "title": "Y-Theory governed Action / Challenge record",
  "description": "Strict shared contract for public action templates and private action instances. A public template is an application architecture, not evidence, a completed challenge, or canonical approval.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema",
    "id",
    "record_kind",
    "domain_id",
    "title",
    "proposition",
    "action",
    "boundary",
    "source_references",
    "project_mapping",
    "identity",
    "scale",
    "beneficiary",
    "variables",
    "measure",
    "baseline",
    "rivals",
    "success_criterion",
    "failure_criterion",
    "evidence_locators",
    "cost",
    "stop_rule",
    "closure",
    "owner",
    "return_path",
    "authority",
    "execution_state"
  ],
  "properties": {
    "schema": { "const": "y-theory.action-challenge.v1" },
    "id": { "type": "string", "pattern": "^AC-[a-z0-9]+(?:-[a-z0-9]+)*$" },
    "record_kind": { "enum": ["public_template", "private_instance"] },
    "domain_id": { "$ref": "#/$defs/domainId" },
    "title": { "$ref": "#/$defs/nonEmptyString" },
    "proposition": { "$ref": "#/$defs/nonEmptyString" },
    "action": { "$ref": "#/$defs/nonEmptyString" },
    "boundary": { "$ref": "#/$defs/nonEmptyString" },
    "source_references": {
      "type": "array",
      "minItems": 2,
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/sourceReference" }
    },
    "project_mapping": { "$ref": "#/$defs/projectMapping" },
    "identity": { "$ref": "#/$defs/stringDeclaration" },
    "scale": { "$ref": "#/$defs/stringDeclaration" },
    "beneficiary": { "$ref": "#/$defs/stringDeclaration" },
    "variables": { "$ref": "#/$defs/stringListDeclaration" },
    "measure": { "$ref": "#/$defs/stringDeclaration" },
    "baseline": { "$ref": "#/$defs/stringDeclaration" },
    "rivals": { "$ref": "#/$defs/stringListDeclaration" },
    "success_criterion": { "$ref": "#/$defs/stringDeclaration" },
    "failure_criterion": { "$ref": "#/$defs/stringDeclaration" },
    "evidence_locators": { "$ref": "#/$defs/locatorListDeclaration" },
    "cost": { "$ref": "#/$defs/stringDeclaration" },
    "stop_rule": { "$ref": "#/$defs/stringDeclaration" },
    "closure": { "$ref": "#/$defs/stringDeclaration" },
    "owner": { "$ref": "#/$defs/stringDeclaration" },
    "return_path": { "$ref": "#/$defs/stringDeclaration" },
    "authority": { "$ref": "#/$defs/authority" },
    "execution_state": {
      "enum": ["not_instantiated", "draft", "running", "stopped", "closed"]
    }
  },
  "allOf": [
    {
      "if": {
        "properties": { "record_kind": { "const": "public_template" } },
        "required": ["record_kind"]
      },
      "then": {
        "properties": {
          "execution_state": { "const": "not_instantiated" },
          "evidence_locators": { "$ref": "#/$defs/undeclared" },
          "owner": { "$ref": "#/$defs/undeclared" },
          "authority": {
            "allOf": [
              { "$ref": "#/$defs/authority" },
              {
                "properties": {
                  "state": { "const": "public_template_no_evidence_or_canonical_authority" },
                  "canonical_approval": { "const": false },
                  "implies_evidence": { "const": false }
                }
              }
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": { "record_kind": { "const": "private_instance" } },
        "required": ["record_kind"]
      },
      "then": {
        "properties": {
          "execution_state": { "enum": ["draft", "running", "stopped", "closed"] },
          "owner": { "$ref": "#/$defs/declaredString" },
          "authority": {
            "allOf": [
              { "$ref": "#/$defs/authority" },
              {
                "properties": {
                  "state": { "enum": ["private_instance_no_canonical_authority", "reviewed_instance_limited_authority"] },
                  "canonical_approval": { "const": false },
                  "implies_evidence": { "const": false }
                }
              }
            ]
          }
        }
      }
    }
  ],
  "$defs": {
    "nonEmptyString": { "type": "string", "minLength": 1 },
    "locator": { "type": "string", "minLength": 1 },
    "domainId": {
      "enum": [
        "teaching",
        "formal-research",
        "quran-audit",
        "agent-continuity",
        "community-food",
        "community-os",
        "runtime-r-and-d"
      ]
    },
    "sourceReference": {
      "type": "object",
      "additionalProperties": false,
      "required": ["locator", "role", "authority"],
      "properties": {
        "locator": { "$ref": "#/$defs/locator" },
        "role": { "$ref": "#/$defs/nonEmptyString" },
        "authority": {
          "enum": ["application_architecture_input", "canonical_context", "project_registry"]
        }
      }
    },
    "undeclared": {
      "type": "object",
      "additionalProperties": false,
      "required": ["state", "value", "reason", "source_locators"],
      "properties": {
        "state": { "const": "undeclared" },
        "value": { "type": "null" },
        "reason": { "$ref": "#/$defs/nonEmptyString" },
        "source_locators": {
          "type": "array",
          "maxItems": 0
        }
      }
    },
    "declaredString": {
      "type": "object",
      "additionalProperties": false,
      "required": ["state", "value", "reason", "source_locators"],
      "properties": {
        "state": { "const": "declared" },
        "value": { "$ref": "#/$defs/nonEmptyString" },
        "reason": { "type": "null" },
        "source_locators": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/locator" }
        }
      }
    },
    "declaredStringList": {
      "type": "object",
      "additionalProperties": false,
      "required": ["state", "value", "reason", "source_locators"],
      "properties": {
        "state": { "const": "declared" },
        "value": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/nonEmptyString" }
        },
        "reason": { "type": "null" },
        "source_locators": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/locator" }
        }
      }
    },
    "declaredLocatorList": {
      "type": "object",
      "additionalProperties": false,
      "required": ["state", "value", "reason", "source_locators"],
      "properties": {
        "state": { "const": "declared" },
        "value": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/locator" }
        },
        "reason": { "type": "null" },
        "source_locators": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/locator" }
        }
      }
    },
    "stringDeclaration": {
      "oneOf": [
        { "$ref": "#/$defs/undeclared" },
        { "$ref": "#/$defs/declaredString" }
      ]
    },
    "stringListDeclaration": {
      "oneOf": [
        { "$ref": "#/$defs/undeclared" },
        { "$ref": "#/$defs/declaredStringList" }
      ]
    },
    "locatorListDeclaration": {
      "oneOf": [
        { "$ref": "#/$defs/undeclared" },
        { "$ref": "#/$defs/declaredLocatorList" }
      ]
    },
    "projectMapping": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["state", "project_ids", "source_locators", "reason"],
          "properties": {
            "state": { "const": "resolved_exact" },
            "project_ids": {
              "type": "array",
              "minItems": 1,
              "uniqueItems": true,
              "items": { "type": "string", "pattern": "^PRJ-[0-9]{3}$" }
            },
            "source_locators": {
              "type": "array",
              "minItems": 1,
              "uniqueItems": true,
              "items": { "$ref": "#/$defs/locator" }
            },
            "reason": { "$ref": "#/$defs/nonEmptyString" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["state", "project_ids", "source_locators", "reason"],
          "properties": {
            "state": { "const": "review_required" },
            "project_ids": { "type": "array", "maxItems": 0 },
            "source_locators": {
              "type": "array",
              "minItems": 1,
              "uniqueItems": true,
              "items": { "$ref": "#/$defs/locator" }
            },
            "reason": { "$ref": "#/$defs/nonEmptyString" }
          }
        }
      ]
    },
    "authority": {
      "type": "object",
      "additionalProperties": false,
      "required": ["state", "canonical_approval", "implies_evidence", "boundary"],
      "properties": {
        "state": {
          "enum": [
            "public_template_no_evidence_or_canonical_authority",
            "private_instance_no_canonical_authority",
            "reviewed_instance_limited_authority"
          ]
        },
        "canonical_approval": { "type": "boolean" },
        "implies_evidence": { "type": "boolean" },
        "boundary": { "$ref": "#/$defs/nonEmptyString" }
      }
    }
  }
}
