{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kiwerb.de/forschung/compute-work-order.schema.json",
  "title": "KIWERB OpenLab bounded compute work order v2",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "work_order_id",
    "classification",
    "task_slug",
    "task_url",
    "task_sha256",
    "runner_url",
    "runner_sha256",
    "validator_url",
    "validator_sha256",
    "issued_on",
    "expires_on",
    "input_artifacts",
    "resource_limits",
    "network_access_required",
    "manual_start_required",
    "arbitrary_code_allowed",
    "expected_output",
    "personal_data_allowed",
    "credentials_allowed",
    "possible_discovery_value_sharing",
    "prework_agreement_sha256",
    "automatic_execution",
    "automatic_submission",
    "payment_or_reimbursement_promised",
    "scientific_result_claimed"
  ],
  "properties": {
    "schema_version": {
      "const": "openlab-compute-work-order-v2"
    },
    "work_order_id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9-]{7,119}$"
    },
    "classification": {
      "enum": [
        "operations_canary",
        "scientific_compute"
      ]
    },
    "task_slug": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9-]{2,119}$"
    },
    "task_sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "runner_url": {
      "type": "string",
      "pattern": "^https://kiwerb\\.de/forschung/[a-z0-9._-]+$"
    },
    "runner_sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "validator_url": {
      "type": "string",
      "pattern": "^https://kiwerb\\.de/forschung/[a-z0-9._-]+$"
    },
    "validator_sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "issued_on": {
      "type": "string",
      "format": "date"
    },
    "expires_on": {
      "type": "string",
      "format": "date"
    },
    "input_artifacts": {
      "type": "array",
      "maxItems": 32,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "url",
          "sha256",
          "bytes",
          "rights_status",
          "personal_data"
        ],
        "properties": {
          "url": {
            "type": "string",
            "pattern": "^https://kiwerb\\.de/forschung/[a-z0-9._?=&-]+$"
          },
          "sha256": {
            "type": "string",
            "pattern": "^[0-9a-f]{64}$"
          },
          "bytes": {
            "type": "integer",
            "minimum": 0,
            "maximum": 104857600
          },
          "rights_status": {
            "const": "approved_bounded_use"
          },
          "personal_data": {
            "const": false
          }
        }
      }
    },
    "resource_limits": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "max_runtime_seconds",
        "max_memory_mib",
        "max_output_bytes",
        "cost_limit_eur"
      ],
      "properties": {
        "max_runtime_seconds": {
          "type": "integer",
          "minimum": 1,
          "maximum": 86400
        },
        "max_memory_mib": {
          "type": "integer",
          "minimum": 16,
          "maximum": 262144
        },
        "max_output_bytes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 10485760
        },
        "cost_limit_eur": {
          "type": "number",
          "minimum": 0,
          "maximum": 1000
        }
      }
    },
    "network_access_required": {
      "const": false
    },
    "manual_start_required": {
      "const": true
    },
    "arbitrary_code_allowed": {
      "const": false
    },
    "expected_output": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schema_version",
        "allowed_fields",
        "result_visibility"
      ],
      "properties": {
        "schema_version": {
          "type": "string",
          "pattern": "^[a-z0-9-]{3,120}$"
        },
        "allowed_fields": {
          "type": "array",
          "minItems": 1,
          "maxItems": 64,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^[a-z][a-z0-9_]{0,79}$"
          }
        },
        "result_visibility": {
          "enum": [
            "local_only",
            "private_until_manual_review"
          ]
        }
      }
    },
    "personal_data_allowed": {
      "const": false
    },
    "credentials_allowed": {
      "const": false
    },
    "possible_discovery_value_sharing": {
      "type": "boolean"
    },
    "prework_agreement_sha256": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        {
          "type": "null"
        }
      ]
    },
    "automatic_execution": {
      "const": false
    },
    "automatic_submission": {
      "const": false
    },
    "payment_or_reimbursement_promised": {
      "const": false
    },
    "scientific_result_claimed": {
      "const": false
    },
    "task_url": {
      "type": "string",
      "pattern": "^https://kiwerb\\.de/forschung/[a-z0-9._-]+$"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "possible_discovery_value_sharing": {
            "const": true
          }
        },
        "required": [
          "possible_discovery_value_sharing"
        ]
      },
      "then": {
        "properties": {
          "prework_agreement_sha256": {
            "type": "string",
            "pattern": "^[0-9a-f]{64}$"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "possible_discovery_value_sharing": {
            "const": false
          }
        },
        "required": [
          "possible_discovery_value_sharing"
        ]
      },
      "then": {
        "properties": {
          "prework_agreement_sha256": {
            "type": "null"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "classification": {
            "const": "operations_canary"
          }
        },
        "required": [
          "classification"
        ]
      },
      "then": {
        "properties": {
          "input_artifacts": {
            "maxItems": 0
          },
          "expected_output": {
            "properties": {
              "result_visibility": {
                "const": "local_only"
              }
            }
          }
        }
      }
    }
  ]
}
