{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://proofofinsight.org/schema/v0.7.0/step.json",
  "title": "PoI Insight Step",
  "description": "Illustrative JSON Schema for a Proof of Insight (PoI) Insight Step, v0.7.0. The schema is NOT normative; the normative definition is the prose of §2 of the specification. Schema validation is the weakest of five validity gates (see Appendix A of the spec). This schema does not enforce conditional or cross-field requirements (e.g. output_artifact required at R1 or under tolerance replay, weights_hash required at R3, edge uniqueness, redactions consistency); those are checked at higher gates.",
  "type": "object",
  "additionalProperties": false,
  "required": ["version", "type", "predecessors", "payload", "attestor", "signature", "timestamp"],
  "properties": {
    "version":   { "const": "0.7.0" },
    "type":      { "enum": ["observe", "compute", "reason", "attest"] },
    "predecessors": {
      "type": "array",
      "items": {
        "oneOf": [
          {
            "type": "object",
            "additionalProperties": false,
            "required": ["step", "relation"],
            "properties": {
              "step":     { "$ref": "#/$defs/digest" },
              "relation": { "enum": ["derived-from", "conditioned-on", "about"] }
            }
          },
          {
            "type": "object",
            "additionalProperties": false,
            "required": ["step", "relation", "context_role", "declared_relevance_hash"],
            "properties": {
              "step":                    { "$ref": "#/$defs/digest" },
              "relation":                { "const": "conditioned-on" },
              "context_role":            { "type": "string" },
              "declared_relevance_hash": { "$ref": "#/$defs/digest" }
            }
          }
        ]
      }
    },
    "payload":   { "type": "object" },
    "attestor":  { "type": "string", "format": "uri" },
    "signature": { "$ref": "#/$defs/signatureObject" },
    "timestamp": {
      "type": "object",
      "additionalProperties": false,
      "required": ["value", "authority", "token"],
      "properties": {
        "value":     { "type": "string", "format": "date-time" },
        "authority": { "type": "string", "format": "uri" },
        "token":     { "type": "string", "minLength": 1 }
      }
    }
  },
  "$defs": {
    "digest": {
      "type": "object",
      "additionalProperties": false,
      "required": ["alg", "value"],
      "properties": {
        "alg":   { "type": "string", "pattern": "^[a-z0-9][a-z0-9-]*$" },
        "value": { "type": "string", "pattern": "^([0-9a-f]{2})+$" }
      }
    },
    "signatureObject": {
      "type": "object",
      "additionalProperties": false,
      "required": ["alg", "value"],
      "properties": {
        "alg":    { "type": "string", "minLength": 1 },
        "key_id": { "type": "string" },
        "value":  { "type": "string", "pattern": "^[A-Za-z0-9+/]+={0,2}$" }
      }
    },
    "contentRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["uri", "digest"],
      "properties": {
        "uri":    { "type": "string", "format": "uri" },
        "digest": { "$ref": "#/$defs/digest" }
      }
    },
    "inlineOrRef": {
      "anyOf": [
        { "type": "object" },
        { "$ref": "#/$defs/contentRef" }
      ]
    },
    "disclosureLimited": {
      "type": "object",
      "additionalProperties": false,
      "required": ["binding_digest", "disclosed", "disclosed_digest", "policy"],
      "properties": {
        "binding_digest": { "$ref": "#/$defs/digest" },
        "disclosed": {
          "anyOf": [
            { "type": ["object", "array", "string"] },
            { "$ref": "#/$defs/contentRef" }
          ]
        },
        "disclosed_digest": { "$ref": "#/$defs/digest" },
        "policy": {
          "anyOf": [
            { "type": "string" },
            { "$ref": "#/$defs/contentRef" }
          ]
        }
      }
    },
    "artifactCarrier": {
      "anyOf": [
        { "type": ["object", "array", "string"] },
        { "$ref": "#/$defs/contentRef" },
        { "$ref": "#/$defs/disclosureLimited" }
      ]
    },
    "outputEncoding": {
      "anyOf": [
        { "enum": ["jcs+json", "octet-stream"] },
        { "type": "string", "minLength": 1 }
      ]
    }
  },
  "allOf": [
    {
      "if":   { "properties": { "type": { "const": "observe" } } },
      "then": {
        "properties": {
          "predecessors": { "maxItems": 0 },
          "payload": {
            "type": "object",
            "additionalProperties": false,
            "required": ["content_hash", "content_type", "source"],
            "properties": {
              "content_hash": { "$ref": "#/$defs/digest" },
              "content_type": { "type": "string" },
              "source":       { "type": ["string", "object"] },
              "provenance":   { "type": ["string", "object"] }
            }
          }
        }
      }
    },
    {
      "if":   { "properties": { "type": { "const": "compute" } } },
      "then": {
        "properties": {
          "predecessors": {
            "minItems": 1,
            "items": { "properties": { "relation": { "const": "derived-from" } } }
          },
          "payload": {
            "type": "object",
            "additionalProperties": false,
            "required": ["function", "invocation", "invocation_hash", "output_encoding", "output_hash", "environment"],
            "properties": {
              "function":        { "type": "string" },
              "invocation":      { "$ref": "#/$defs/inlineOrRef" },
              "invocation_hash": { "$ref": "#/$defs/digest" },
              "output_encoding": { "$ref": "#/$defs/outputEncoding" },
              "output_hash":     { "$ref": "#/$defs/digest" },
              "output_artifact": { "$ref": "#/$defs/artifactCarrier" },
              "environment": {
                "type": "object",
                "required": ["replay_regime"],
                "properties": {
                  "replay_regime": { "enum": ["bit-identical", "tolerance"] }
                }
              }
            }
          }
        }
      }
    },
    {
      "if":   { "properties": { "type": { "const": "reason" } } },
      "then": {
        "properties": {
          "predecessors": {
            "minItems": 1,
            "items": {
              "properties": {
                "relation": { "enum": ["derived-from", "conditioned-on"] }
              }
            }
          },
          "payload": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "model", "replay_class",
              "invocation", "invocation_hash",
              "input_messages", "input_messages_hash",
              "output_encoding", "output_hash", "sampling"
            ],
            "properties": {
              "model": {
                "type": "object",
                "additionalProperties": false,
                "required": ["identifier"],
                "properties": {
                  "identifier":   { "type": "string" },
                  "weights_hash": { "$ref": "#/$defs/digest" },
                  "version":      { "type": "string" }
                }
              },
              "replay_class":           { "enum": ["R1", "R2", "R3"] },
              "invocation":             { "$ref": "#/$defs/inlineOrRef" },
              "invocation_hash":        { "$ref": "#/$defs/digest" },
              "input_messages":         { "$ref": "#/$defs/artifactCarrier" },
              "input_messages_hash":    { "$ref": "#/$defs/digest" },
              "tool_call_log":          { "$ref": "#/$defs/artifactCarrier" },
              "tool_call_log_hash":     { "$ref": "#/$defs/digest" },
              "visible_rationale":      { "$ref": "#/$defs/artifactCarrier" },
              "visible_rationale_hash": { "$ref": "#/$defs/digest" },
              "finding_type": {
                "anyOf": [
                  { "enum": ["conclusion", "no-finding", "insufficient-evidence", "negative-result"] },
                  { "type": "string", "pattern": "^[a-z][a-z0-9\\-/]*$" }
                ]
              },
              "output_encoding":        { "$ref": "#/$defs/outputEncoding" },
              "output_hash":            { "$ref": "#/$defs/digest" },
              "output_artifact":        { "$ref": "#/$defs/artifactCarrier" },
              "sampling":               { "type": "object" },
              "redactions":             { "type": ["object", "array"] }
            }
          }
        }
      }
    },
    {
      "if":   { "properties": { "type": { "const": "attest" } } },
      "then": {
        "properties": {
          "predecessors": {
            "minItems": 1,
            "items": { "properties": { "relation": { "const": "about" } } }
          },
          "payload": {
            "type": "object",
            "additionalProperties": false,
            "required": ["claim_type", "role", "claim_body", "claim_hash"],
            "properties": {
              "claim_type": {
                "anyOf": [
                  { "type": "string", "format": "uri", "pattern": "^[a-z][a-z0-9+.-]*:" },
                  { "type": "string", "pattern": "^[a-z][a-z0-9-]*/[a-z0-9-]+$" }
                ]
              },
              "role":       { "type": "string" },
              "claim_body": { "type": ["object", "string"] },
              "claim_hash": { "$ref": "#/$defs/digest" }
            }
          }
        }
      }
    }
  ]
}
