{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://raw.githubusercontent.com/realpkuasule/harness-automation/main/docs/api/eval-contract-v1.schema.json",
  "title": "Harness Evaluation Contract v1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "suites"],
  "properties": {
    "$schema": {
      "type": "string"
    },
    "schemaVersion": {
      "enum": ["1.0", "1.1", "1.2"]
    },
    "suites": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/suite"
      }
    }
  },
  "allOf": [
    {
      "if": { "properties": { "schemaVersion": { "const": "1.1" } }, "required": ["schemaVersion"] },
      "then": {
        "properties": {
          "suites": {
            "type": "array",
            "items": {
              "type": "object",
              "allOf": [
                { "type": "object", "required": ["runnerSources", "traceability", "negativeControl"] },
                { "type": "object", "properties": { "baseline": { "type": "object", "required": ["origin"] } } }
              ]
            }
          }
        }
      }
    },
    {
      "if": { "properties": { "schemaVersion": { "const": "1.2" } }, "required": ["schemaVersion"] },
      "then": {
        "properties": {
          "suites": {
            "type": "array",
            "items": {
              "type": "object",
              "allOf": [
                { "type": "object", "required": ["runnerSources", "traceability", "negativeControl"] },
                { "type": "object", "properties": { "baseline": { "type": "object", "required": ["origin"] } } },
                { "type": "object", "properties": { "negativeControl": { "type": "object", "required": ["expectedReport"] } } }
              ]
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "repositoryPath": {
      "type": "string",
      "minLength": 1,
      "pattern": "^(?!/)(?![A-Za-z]:[\\\\/])(?!.*(?:^|[\\\\/])\\.\\.(?:[\\\\/]|$)).+$"
    },
    "suite": {
      "$comment": "Suite and grader identifier uniqueness is enforced by runtime semantic validation.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "kind",
        "owner",
        "description",
        "command",
        "tasks",
        "baseline",
        "target",
        "graders"
      ],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$"
        },
        "kind": {
          "enum": ["capability", "regression"]
        },
        "owner": {
          "type": "string",
          "minLength": 1,
          "pattern": ".*\\S.*"
        },
        "description": {
          "type": "string",
          "minLength": 1,
          "pattern": ".*\\S.*"
        },
        "command": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "tasks": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/repositoryPath"
          }
        },
        "baseline": {
          "$ref": "#/$defs/baseline"
        },
        "target": {
          "$ref": "#/$defs/target"
        },
        "graders": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/grader"
          }
        },
        "runnerSources": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/repositoryPath"
          }
        },
        "traceability": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/traceability" }
        },
        "negativeControl": {
          "$ref": "#/$defs/negativeControl"
        }
      }
    },
    "baseline": {
      "type": "object",
      "additionalProperties": false,
      "required": ["score", "trials", "evidence"],
      "properties": {
        "score": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        },
        "trials": {
          "type": "integer",
          "minimum": 1
        },
        "evidence": {
          "$ref": "#/$defs/repositoryPath"
        },
        "origin": {
          "enum": ["pre-implementation", "adoption"]
        }
      }
    },
    "target": {
      "type": "object",
      "additionalProperties": false,
      "required": ["metric", "threshold", "trials"],
      "properties": {
        "metric": {
          "enum": ["pass-rate", "pass-at-1", "pass-all-trials"]
        },
        "threshold": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        },
        "trials": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "grader": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "role"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$"
        },
        "kind": {
          "enum": ["code", "model", "human"]
        },
        "role": {
          "enum": ["gate", "guidance"]
        },
        "calibrationEvidence": {
          "$ref": "#/$defs/repositoryPath"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "kind": { "const": "model" },
              "role": { "const": "gate" }
            },
            "required": ["kind", "role"]
          },
          "then": {
            "required": ["calibrationEvidence"]
          }
        }
      ]
    },
    "traceability": {
      "type": "object",
      "additionalProperties": false,
      "required": ["requirementId", "ruleIds"],
      "properties": {
        "requirementId": { "type": "string", "minLength": 1, "pattern": ".*\\S.*" },
        "ruleIds": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "type": "string", "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$" }
        }
      }
    },
    "negativeControl": {
      "type": "object",
      "additionalProperties": false,
      "required": ["command", "fixture", "expectedExitCode"],
      "properties": {
        "command": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "minLength": 1 }
        },
        "fixture": { "$ref": "#/$defs/repositoryPath" },
        "expectedExitCode": { "type": "integer", "minimum": 1, "maximum": 255 },
        "expectedReport": {
          "type": "object",
          "additionalProperties": false,
          "required": ["testId", "assertionId", "category"],
          "properties": {
            "testId": { "type": "string", "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$" },
            "assertionId": { "type": "string", "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$" },
            "category": { "type": "string", "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$" }
          }
        }
      }
    }
  }
}
