{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://law.arxo.io/schema/counterfactual-input.schema.json",
  "title": "Arxo counterfactual input 0.1",
  "description": "Finite, hash-pinned candidate worlds for a counterfactual declaration: alternative inputs the search may explore.",
  "$comment": "SPEC §186",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "schemaVersion": {
      "const": "law.core.counterfactual-input/0.1"
    },
    "kind": {
      "const": "counterfactual-input"
    },
    "id": {
      "$ref": "#/$defs/StableId"
    },
    "programHash": {
      "$ref": "#/$defs/Digest"
    },
    "caseHash": {
      "$ref": "#/$defs/Digest"
    },
    "counterfactual": {
      "$ref": "#/$defs/StableId"
    },
    "costModel": {
      "$ref": "#/$defs/StableId"
    },
    "candidates": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/Candidate"
      }
    },
    "contentHash": {
      "$ref": "#/$defs/Digest"
    },
    "metadata": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "required": [
    "schemaVersion",
    "kind",
    "id",
    "programHash",
    "caseHash",
    "counterfactual",
    "costModel",
    "candidates",
    "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])$"
    },
    "JsonValue": {
      "oneOf": [
        { "type": "null" },
        { "type": "boolean" },
        { "type": "integer" },
        { "type": "string" },
        {
          "type": "array",
          "items": { "$ref": "#/$defs/JsonValue" }
        },
        {
          "type": "object",
          "additionalProperties": { "$ref": "#/$defs/JsonValue" }
        }
      ]
    },
    "SetConst": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": { "const": "setConst" },
        "target": { "$ref": "#/$defs/StableId" },
        "value": { "$ref": "legal-ir.schema.json#/$defs/Term" }
      },
      "required": ["kind", "target", "value"]
    },
    "AddAssertion": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": { "const": "addAssertion" },
        "assertion": { "$ref": "legal-ir.schema.json#/$defs/Assertion" }
      },
      "required": ["kind", "assertion"]
    },
    "RemoveAssertion": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": { "const": "removeAssertion" },
        "assertion": { "$ref": "#/$defs/StableId" }
      },
      "required": ["kind", "assertion"]
    },
    "SetContext": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": { "const": "setContext" },
        "target": {
          "type": "string",
          "pattern": "^context\\.[A-Za-z_][A-Za-z0-9_.-]*$"
        },
        "value": { "$ref": "#/$defs/JsonValue" }
      },
      "required": ["kind", "target", "value"]
    },
    "Operation": {
      "oneOf": [
        { "$ref": "#/$defs/SetConst" },
        { "$ref": "#/$defs/AddAssertion" },
        { "$ref": "#/$defs/RemoveAssertion" },
        { "$ref": "#/$defs/SetContext" }
      ]
    },
    "Candidate": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/StableId" },
        "cost": { "$ref": "#/$defs/DecimalString" },
        "operations": {
          "type": "array",
          "items": { "$ref": "#/$defs/Operation" }
        },
        "metadata": {
          "type": "object",
          "additionalProperties": true
        }
      },
      "required": ["id", "cost", "operations"]
    }
  }
}
