{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/egohygiene/optiflow/schemas/png-candidate-contract-v1.schema.json",
  "title": "Contract-only static PNG candidate review packet",
  "description": "Shape validation is not byte validation, optimization, source mutation authority, or artifact acceptance.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema",
    "request",
    "provider_result",
    "host_evidence"
  ],
  "properties": {
    "schema": {
      "const": "optiflow.png-candidate-contract.v1"
    },
    "request": {
      "$ref": "#/$defs/request"
    },
    "provider_result": {
      "$ref": "#/$defs/provider_result"
    },
    "host_evidence": {
      "anyOf": [
        {
          "$ref": "#/$defs/host"
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "$defs": {
    "digest": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "algorithm",
        "value"
      ],
      "properties": {
        "algorithm": {
          "const": "blake3-256"
        },
        "value": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        }
      }
    },
    "path": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "encoding",
            "value"
          ],
          "properties": {
            "encoding": {
              "const": "utf8"
            },
            "value": {
              "type": "string",
              "minLength": 1,
              "maxLength": 4096
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "encoding",
            "base64"
          ],
          "properties": {
            "encoding": {
              "const": "unix_bytes"
            },
            "base64": {
              "type": "string",
              "minLength": 4,
              "maxLength": 5464,
              "pattern": "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"
            }
          }
        }
      ]
    },
    "filesystem_identity": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "platform",
        "filesystem_id",
        "file_id",
        "link_count"
      ],
      "properties": {
        "platform": {
          "enum": [
            "linux",
            "macos"
          ]
        },
        "filesystem_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        },
        "file_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        },
        "link_count": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        }
      }
    },
    "content": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "digest",
        "logical_bytes"
      ],
      "properties": {
        "digest": {
          "$ref": "#/$defs/digest"
        },
        "logical_bytes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        }
      }
    },
    "file": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "path",
        "filesystem_identity",
        "observation_fingerprint",
        "content",
        "regular_file",
        "symlink"
      ],
      "properties": {
        "path": {
          "$ref": "#/$defs/path"
        },
        "filesystem_identity": {
          "$ref": "#/$defs/filesystem_identity"
        },
        "observation_fingerprint": {
          "$ref": "#/$defs/digest"
        },
        "content": {
          "$ref": "#/$defs/content"
        },
        "regular_file": {
          "const": true
        },
        "symlink": {
          "const": false
        }
      }
    },
    "tool": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "version",
        "executable",
        "binary_digest",
        "invocation_fingerprint"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        },
        "version": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        },
        "executable": {
          "$ref": "#/$defs/path"
        },
        "binary_digest": {
          "$ref": "#/$defs/digest"
        },
        "invocation_fingerprint": {
          "$ref": "#/$defs/digest"
        }
      }
    },
    "limits": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "input_bytes",
        "candidate_bytes",
        "decoded_bytes",
        "temporary_bytes",
        "memory_bytes",
        "elapsed_ms"
      ],
      "properties": {
        "input_bytes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "candidate_bytes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "decoded_bytes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "temporary_bytes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "memory_bytes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "elapsed_ms": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        }
      }
    },
    "request": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "profile",
        "source_observation_id",
        "source",
        "effective_policy_fingerprint",
        "producer",
        "validator",
        "limits"
      ],
      "properties": {
        "profile": {
          "const": "optiflow.png-idat-preserve.v1"
        },
        "source_observation_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        },
        "source": {
          "$ref": "#/$defs/file"
        },
        "effective_policy_fingerprint": {
          "$ref": "#/$defs/digest"
        },
        "producer": {
          "$ref": "#/$defs/tool"
        },
        "validator": {
          "$ref": "#/$defs/tool"
        },
        "limits": {
          "$ref": "#/$defs/limits"
        }
      }
    },
    "provider_result": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "request_fingerprint",
        "producer",
        "status",
        "candidate"
      ],
      "properties": {
        "request_fingerprint": {
          "$ref": "#/$defs/digest"
        },
        "producer": {
          "$ref": "#/$defs/tool"
        },
        "status": {
          "enum": [
            "succeeded",
            "failed",
            "timed_out",
            "cancelled"
          ]
        },
        "candidate": {
          "anyOf": [
            {
              "$ref": "#/$defs/content"
            },
            {
              "type": "null"
            }
          ]
        }
      }
    },
    "png": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "width",
        "height",
        "bit_depth",
        "color_type",
        "frames",
        "complete_decode",
        "animation_chunks",
        "decoded_bytes",
        "ihdr_digest",
        "decoded_samples_digest",
        "non_idat_chunks_digest",
        "unknown_unsafe_to_copy_chunks"
      ],
      "properties": {
        "width": {
          "type": "integer",
          "minimum": 1,
          "maximum": 2147483647
        },
        "height": {
          "type": "integer",
          "minimum": 1,
          "maximum": 2147483647
        },
        "bit_depth": {
          "enum": [
            1,
            2,
            4,
            8,
            16
          ]
        },
        "color_type": {
          "enum": [
            0,
            2,
            3,
            4,
            6
          ]
        },
        "frames": {
          "type": "integer",
          "minimum": 0,
          "maximum": 4294967295
        },
        "complete_decode": {
          "type": "boolean"
        },
        "animation_chunks": {
          "type": "boolean"
        },
        "decoded_bytes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "ihdr_digest": {
          "$ref": "#/$defs/digest"
        },
        "decoded_samples_digest": {
          "$ref": "#/$defs/digest"
        },
        "non_idat_chunks_digest": {
          "$ref": "#/$defs/digest"
        },
        "unknown_unsafe_to_copy_chunks": {
          "type": "boolean"
        }
      }
    },
    "check": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "kind",
        "status"
      ],
      "properties": {
        "kind": {
          "enum": [
            "source_stable",
            "complete_decode",
            "dimensions",
            "frame_count",
            "decoded_samples",
            "alpha_preserved",
            "color_preserved",
            "metadata_preserved"
          ]
        },
        "status": {
          "enum": [
            "passed",
            "failed",
            "unsupported",
            "not_run"
          ]
        }
      }
    },
    "usage": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "peak_temporary_bytes",
        "peak_memory_bytes",
        "elapsed_ms"
      ],
      "properties": {
        "peak_temporary_bytes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "peak_memory_bytes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "elapsed_ms": {
          "type": "integer",
          "minimum": 0,
          "maximum": 9007199254740991
        }
      }
    },
    "host": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "request_fingerprint",
        "source_before",
        "source_after",
        "candidate",
        "producer_before",
        "producer_after",
        "validator_before",
        "validator_after",
        "source_png",
        "candidate_png",
        "checks",
        "resource_usage"
      ],
      "properties": {
        "request_fingerprint": {
          "$ref": "#/$defs/digest"
        },
        "source_before": {
          "$ref": "#/$defs/file"
        },
        "source_after": {
          "$ref": "#/$defs/file"
        },
        "candidate": {
          "$ref": "#/$defs/file"
        },
        "producer_before": {
          "$ref": "#/$defs/tool"
        },
        "producer_after": {
          "$ref": "#/$defs/tool"
        },
        "validator_before": {
          "$ref": "#/$defs/tool"
        },
        "validator_after": {
          "$ref": "#/$defs/tool"
        },
        "source_png": {
          "$ref": "#/$defs/png"
        },
        "candidate_png": {
          "$ref": "#/$defs/png"
        },
        "checks": {
          "type": "array",
          "maxItems": 16,
          "items": {
            "$ref": "#/$defs/check"
          }
        },
        "resource_usage": {
          "$ref": "#/$defs/usage"
        }
      }
    }
  }
}
