{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aisa.ablera.com/contracts/tool-package.schema.json",
  "title": "Executable tool package \u2014 source and/or binary",
  "x-status": "draft",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "tool_key",
    "version",
    "owner_domain",
    "purpose",
    "applies_when",
    "artifacts",
    "entry",
    "input_schema",
    "output_schema",
    "dependencies",
    "effect",
    "connector_operations",
    "uses_model",
    "repeatability",
    "limits",
    "tests",
    "provenance_refs"
  ],
  "properties": {
    "tool_key": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_.-]+$"
    },
    "version": {
      "type": "integer",
      "minimum": 1
    },
    "owner_domain": {
      "type": "string",
      "minLength": 1
    },
    "purpose": {
      "type": "string",
      "minLength": 1
    },
    "applies_when": {
      "type": "string",
      "minLength": 1
    },
    "artifacts": {
      "type": "object",
      "additionalProperties": false,
      "required": [],
      "properties": {
        "source": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "uri",
            "sha256"
          ],
          "properties": {
            "uri": {
              "type": "string",
              "minLength": 1
            },
            "sha256": {
              "type": "string",
              "pattern": "^[a-f0-9]{64}$"
            },
            "revision": {
              "type": "string",
              "minLength": 1
            },
            "platform": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        "binary": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "uri",
            "sha256"
          ],
          "properties": {
            "uri": {
              "type": "string",
              "minLength": 1
            },
            "sha256": {
              "type": "string",
              "pattern": "^[a-f0-9]{64}$"
            },
            "revision": {
              "type": "string",
              "minLength": 1
            },
            "platform": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      },
      "anyOf": [
        {
          "required": [
            "source"
          ]
        },
        {
          "required": [
            "binary"
          ]
        }
      ]
    },
    "entry": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "artifact",
        "path",
        "runtime_id",
        "runtime_version",
        "kind"
      ],
      "properties": {
        "artifact": {
          "enum": [
            "source",
            "binary"
          ]
        },
        "path": {
          "type": "string",
          "pattern": "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$))[A-Za-z0-9_./-]+$"
        },
        "runtime_id": {
          "type": "string",
          "minLength": 1
        },
        "runtime_version": {
          "type": "string",
          "pattern": "^(?!latest$).+"
        },
        "kind": {
          "enum": [
            "managed_handler",
            "restricted_process"
          ]
        }
      }
    },
    "input_schema": {
      "type": "object"
    },
    "output_schema": {
      "type": "object"
    },
    "dependencies": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "key",
          "version",
          "sha256"
        ],
        "properties": {
          "key": {
            "type": "string",
            "minLength": 1
          },
          "version": {
            "type": "string",
            "minLength": 1
          },
          "sha256": {
            "type": "string",
            "pattern": "^[a-f0-9]{64}$"
          }
        }
      }
    },
    "effect": {
      "enum": [
        "compute",
        "case_artifacts",
        "connector"
      ]
    },
    "connector_operations": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      },
      "uniqueItems": true
    },
    "uses_model": {
      "type": "boolean"
    },
    "repeatability": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "kind",
        "comparison"
      ],
      "properties": {
        "kind": {
          "enum": [
            "deterministic",
            "declared_variation"
          ]
        },
        "comparison": {
          "type": "string",
          "minLength": 1
        },
        "tolerance_ref": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "limits": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "timeout_ms",
        "max_output_bytes"
      ],
      "properties": {
        "timeout_ms": {
          "type": "integer",
          "minimum": 1
        },
        "max_output_bytes": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "tests": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "corpus_ref",
        "result_ref"
      ],
      "properties": {
        "corpus_ref": {
          "type": "string",
          "minLength": 1
        },
        "result_ref": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "provenance_refs": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "string",
        "minLength": 1
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "entry": {
            "properties": {
              "artifact": {
                "const": "source"
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "artifacts": {
            "required": [
              "source"
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "entry": {
            "properties": {
              "artifact": {
                "const": "binary"
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "artifacts": {
            "required": [
              "binary"
            ],
            "properties": {
              "binary": {
                "required": [
                  "platform"
                ]
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "effect": {
            "const": "compute"
          }
        }
      },
      "then": {
        "properties": {
          "connector_operations": {
            "maxItems": 0
          },
          "uses_model": {
            "const": false
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "effect": {
            "const": "connector"
          }
        }
      },
      "then": {
        "properties": {
          "connector_operations": {
            "minItems": 1
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "repeatability": {
            "properties": {
              "kind": {
                "const": "deterministic"
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "uses_model": {
            "const": false
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "repeatability": {
            "properties": {
              "kind": {
                "const": "declared_variation"
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "repeatability": {
            "required": [
              "tolerance_ref"
            ]
          }
        }
      }
    }
  ],
  "$defs": {
    "invocation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "skill_key",
        "skill_version",
        "tool_key",
        "tool_version",
        "input"
      ],
      "properties": {
        "skill_key": {
          "type": "string",
          "minLength": 1
        },
        "skill_version": {
          "type": "integer",
          "minimum": 1
        },
        "tool_key": {
          "type": "string",
          "minLength": 1
        },
        "tool_version": {
          "type": "integer",
          "minimum": 1
        },
        "input": {
          "type": "object"
        }
      }
    }
  }
}
