{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://law.arxo.io/schema/counterfactual-result.schema.json",
  "title": "Arxo counterfactual result 0.1",
  "description": "Deterministic result and replay certificate of a finite counterfactual search.",
  "$comment": "SPEC §186",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "schemaVersion": {
      "const": "law.core.counterfactual-result/0.1"
    },
    "kind": {
      "const": "counterfactual-result"
    },
    "id": { "$ref": "#/$defs/StableId" },
    "counterfactual": { "$ref": "#/$defs/StableId" },
    "programHash": { "$ref": "#/$defs/Digest" },
    "caseHash": { "$ref": "#/$defs/Digest" },
    "inputHash": { "$ref": "#/$defs/Digest" },
    "solverProfile": {
      "const": "law.solver.finite-enumeration/0.1"
    },
    "status": {
      "enum": ["ALREADY_SATISFIED", "FOUND", "NO_SOLUTION"]
    },
    "solutions": {
      "type": "array",
      "items": { "$ref": "#/$defs/Solution" }
    },
    "certificate": { "$ref": "#/$defs/Certificate" },
    "contentHash": { "$ref": "#/$defs/Digest" }
  },
  "required": [
    "schemaVersion",
    "kind",
    "id",
    "counterfactual",
    "programHash",
    "caseHash",
    "inputHash",
    "solverProfile",
    "status",
    "solutions",
    "certificate",
    "contentHash"
  ],
  "$defs": {
    "StableId": {
      "type": "string",
      "minLength": 1
    },
    "Digest": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_-]+:[A-Fa-f0-9]+$"
    },
    "DecimalString": {
      "type": "string",
      "pattern": "^(?:0|(?:[1-9][0-9]*)(?:\\.[0-9]*[1-9])?|0\\.[0-9]*[1-9])$"
    },
    "CheckedCandidate": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/StableId" },
        "cost": { "$ref": "#/$defs/DecimalString" },
        "targetStatus": {
          "enum": ["TRUE_ONLY", "FALSE_ONLY", "BOTH", "NEITHER"]
        },
        "constraintsStatus": {
          "enum": ["TRUE_ONLY", "FALSE_ONLY", "BOTH", "NEITHER"]
        },
        "evaluationHash": { "$ref": "#/$defs/Digest" }
      },
      "required": [
        "id",
        "cost",
        "targetStatus",
        "constraintsStatus",
        "evaluationHash"
      ]
    },
    "Solution": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "candidate": { "$ref": "#/$defs/StableId" },
        "cost": { "$ref": "#/$defs/DecimalString" },
        "caseHash": { "$ref": "#/$defs/Digest" },
        "evaluationHash": { "$ref": "#/$defs/Digest" },
        "proofRoots": {
          "type": "array",
          "items": { "$ref": "#/$defs/StableId" },
          "uniqueItems": true
        },
        "operations": {
          "type": "array",
          "items": { "$ref": "counterfactual-input.schema.json#/$defs/Operation" }
        }
      },
      "required": [
        "candidate",
        "cost",
        "caseHash",
        "evaluationHash",
        "proofRoots",
        "operations"
      ]
    },
    "Certificate": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "candidateSetHash": { "$ref": "#/$defs/Digest" },
        "checked": {
          "type": "array",
          "items": { "$ref": "#/$defs/CheckedCandidate" }
        },
        "cutoffCost": {
          "oneOf": [
            { "$ref": "#/$defs/DecimalString" },
            { "type": "null" }
          ]
        }
      },
      "required": ["candidateSetHash", "checked", "cutoffCost"]
    }
  }
}
