{
  "type": "object",
  "required": [
    "schema_version",
    "kind",
    "status",
    "backend",
    "identity",
    "repository",
    "candidates",
    "pull_request",
    "checks",
    "statuses",
    "diagnostics",
    "boundary",
    "observed_at"
  ],
  "properties": {
    "schema_version": {
      "const": 1
    },
    "kind": {
      "enum": [
        "repo",
        "pr",
        "ci"
      ]
    },
    "status": {
      "enum": [
        "ok",
        "error"
      ]
    },
    "backend": {
      "enum": [
        "git",
        "jj",
        "unknown",
        "ambiguous",
        "gitbutler"
      ]
    },
    "identity": {
      "anyOf": [
        {
          "type": "null"
        },
        {
          "anyOf": [
            {
              "$ref": "#/$defs/git_identity"
            },
            {
              "$ref": "#/$defs/jj_identity"
            }
          ]
        }
      ]
    },
    "repository": {
      "anyOf": [
        {
          "type": "null"
        },
        {
          "$ref": "#/$defs/repository"
        }
      ]
    },
    "candidates": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/candidate"
      }
    },
    "pull_request": {
      "anyOf": [
        {
          "type": "null"
        },
        {
          "$ref": "#/$defs/pr"
        }
      ]
    },
    "checks": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/check"
      },
      "maxItems": 1000
    },
    "statuses": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/commit_status"
      },
      "maxItems": 1000
    },
    "diagnostics": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/diagnostic"
      }
    },
    "boundary": {
      "$ref": "#/$defs/boundary"
    },
    "observed_at": {
      "type": "string",
      "format": "date-time"
    }
  },
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://wstein.github.io/sourcesteward/schemas/v1/hosting.schema.json",
  "title": "SourceSteward hosting observation v1",
  "$defs": {
    "git_identity": {
      "title": "SourceSteward inspection result (schema 1)",
      "description": "Success and operational failure results. Usage and writer failures may not emit JSON. Unknown object properties and diagnostic codes are compatible extensions.",
      "type": "object",
      "required": [
        "schema_version",
        "status",
        "backend",
        "input_path",
        "evidence",
        "repository",
        "diagnostics"
      ],
      "properties": {
        "schema_version": {
          "const": 1
        },
        "status": {
          "enum": [
            "ok",
            "unsupported",
            "error"
          ]
        },
        "backend": {
          "enum": [
            "git",
            "jj",
            "gitbutler",
            "ambiguous",
            "unknown"
          ]
        },
        "input_path": {
          "type": "string",
          "description": "Absolute path when available; empty if input cannot be represented or resolved."
        },
        "evidence": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/git_identity/$defs/evidence"
          }
        },
        "repository": {
          "oneOf": [
            {
              "type": "null"
            },
            {
              "$ref": "#/$defs/git_identity/$defs/repository"
            }
          ]
        },
        "diagnostics": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/git_identity/$defs/diagnostic"
          }
        }
      },
      "$defs": {
        "head": {
          "type": "object",
          "required": [
            "state",
            "ref",
            "oid"
          ],
          "properties": {
            "state": {
              "enum": [
                "attached",
                "detached",
                "unborn"
              ]
            },
            "ref": {
              "oneOf": [
                {
                  "type": "null"
                },
                {
                  "type": "string",
                  "pattern": "^refs/heads/.+"
                }
              ]
            },
            "oid": {
              "oneOf": [
                {
                  "type": "null"
                },
                {
                  "type": "string",
                  "pattern": "^([0-9a-f]{40}|[0-9a-f]{64})$"
                }
              ]
            }
          },
          "allOf": [
            {
              "if": {
                "properties": {
                  "state": {
                    "const": "attached"
                  }
                }
              },
              "then": {
                "properties": {
                  "ref": {
                    "type": "string"
                  },
                  "oid": {
                    "type": "string"
                  }
                }
              }
            },
            {
              "if": {
                "properties": {
                  "state": {
                    "const": "detached"
                  }
                }
              },
              "then": {
                "properties": {
                  "ref": {
                    "type": "null"
                  },
                  "oid": {
                    "type": "string"
                  }
                }
              }
            },
            {
              "if": {
                "properties": {
                  "state": {
                    "const": "unborn"
                  }
                }
              },
              "then": {
                "properties": {
                  "ref": {
                    "type": "string"
                  },
                  "oid": {
                    "type": "null"
                  }
                }
              }
            }
          ]
        },
        "repository": {
          "type": "object",
          "required": [
            "git_dir",
            "common_dir",
            "worktree",
            "bare",
            "linked_worktree",
            "object_format",
            "head"
          ],
          "properties": {
            "git_dir": {
              "type": "string",
              "minLength": 1
            },
            "common_dir": {
              "type": "string",
              "minLength": 1
            },
            "worktree": {
              "oneOf": [
                {
                  "type": "string",
                  "minLength": 1
                },
                {
                  "type": "null"
                }
              ]
            },
            "bare": {
              "type": "boolean"
            },
            "linked_worktree": {
              "type": "boolean"
            },
            "object_format": {
              "enum": [
                "sha1",
                "sha256"
              ]
            },
            "head": {
              "$ref": "#/$defs/git_identity/$defs/head"
            }
          },
          "allOf": [
            {
              "if": {
                "properties": {
                  "bare": {
                    "const": true
                  }
                }
              },
              "then": {
                "properties": {
                  "worktree": {
                    "type": "null"
                  },
                  "linked_worktree": {
                    "const": false
                  }
                }
              },
              "else": {
                "properties": {
                  "worktree": {
                    "type": "string",
                    "minLength": 1
                  }
                }
              }
            },
            {
              "if": {
                "properties": {
                  "object_format": {
                    "const": "sha1"
                  }
                }
              },
              "then": {
                "properties": {
                  "head": {
                    "properties": {
                      "oid": {
                        "oneOf": [
                          {
                            "type": "null"
                          },
                          {
                            "type": "string",
                            "pattern": "^[0-9a-f]{40}$",
                            "minLength": 40,
                            "maxLength": 40
                          }
                        ]
                      }
                    }
                  }
                }
              }
            },
            {
              "if": {
                "properties": {
                  "object_format": {
                    "const": "sha256"
                  }
                }
              },
              "then": {
                "properties": {
                  "head": {
                    "properties": {
                      "oid": {
                        "oneOf": [
                          {
                            "type": "null"
                          },
                          {
                            "type": "string",
                            "pattern": "^[0-9a-f]{64}$",
                            "minLength": 64,
                            "maxLength": 64
                          }
                        ]
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        "evidence": {
          "type": "object",
          "required": [
            "kind",
            "location"
          ],
          "properties": {
            "kind": {
              "enum": [
                "jj-metadata",
                "gitbutler-metadata",
                "gitbutler-reference"
              ]
            },
            "location": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        "diagnostic": {
          "type": "object",
          "required": [
            "code",
            "message"
          ],
          "properties": {
            "code": {
              "type": "string",
              "minLength": 1
            },
            "message": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "status": {
                "const": "ok"
              }
            }
          },
          "then": {
            "properties": {
              "backend": {
                "const": "git"
              },
              "repository": {
                "$ref": "#/$defs/git_identity/$defs/repository"
              },
              "diagnostics": {
                "maxItems": 0
              }
            }
          },
          "else": {
            "properties": {
              "repository": {
                "type": "null"
              },
              "diagnostics": {
                "minItems": 1
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "const": "unsupported"
              }
            }
          },
          "then": {
            "properties": {
              "backend": {
                "enum": [
                  "jj",
                  "gitbutler",
                  "ambiguous"
                ]
              }
            }
          }
        }
      ]
    },
    "jj_identity": {
      "title": "SourceSteward native jj inspection result (schema 2)",
      "description": "Native jj stored identity and operational failures. Git/version retain schema 1. Additional object properties and diagnostic codes are compatible extensions. Usage/writer failures may not emit complete JSON.",
      "type": "object",
      "required": [
        "schema_version",
        "status",
        "backend",
        "input_path",
        "evidence",
        "repository",
        "diagnostics"
      ],
      "properties": {
        "schema_version": {
          "const": 2
        },
        "status": {
          "enum": [
            "ok",
            "error"
          ]
        },
        "backend": {
          "const": "jj"
        },
        "input_path": {
          "type": "string",
          "description": "Absolute path when available; empty if input cannot be represented or resolved."
        },
        "evidence": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/jj_identity/$defs/evidence"
          }
        },
        "repository": {
          "type": "null"
        },
        "diagnostics": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/jj_identity/$defs/diagnostic"
          }
        },
        "jj_repository": {
          "$ref": "#/$defs/jj_identity/$defs/jj_repository"
        }
      },
      "$defs": {
        "evidence": {
          "type": "object",
          "required": [
            "kind",
            "location"
          ],
          "properties": {
            "kind": {
              "enum": [
                "jj-metadata",
                "gitbutler-metadata",
                "gitbutler-reference"
              ]
            },
            "location": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        "diagnostic": {
          "type": "object",
          "required": [
            "code",
            "message"
          ],
          "properties": {
            "code": {
              "type": "string",
              "minLength": 1
            },
            "message": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        "jj_repository": {
          "type": "object",
          "required": [
            "workspace_root",
            "metadata_dir",
            "repository_dir",
            "workspace_name",
            "change_id",
            "commit_id",
            "operation_id",
            "observation"
          ],
          "properties": {
            "workspace_root": {
              "type": "string",
              "minLength": 1
            },
            "metadata_dir": {
              "type": "string",
              "minLength": 1
            },
            "repository_dir": {
              "type": "string",
              "minLength": 1
            },
            "workspace_name": {
              "type": "string",
              "minLength": 1
            },
            "change_id": {
              "type": "string",
              "pattern": "^[k-z]{32}$",
              "minLength": 32,
              "maxLength": 32
            },
            "commit_id": {
              "type": "string",
              "pattern": "^[0-9a-f]{40}$",
              "minLength": 40,
              "maxLength": 40
            },
            "operation_id": {
              "type": "string",
              "pattern": "^[0-9a-f]{128}$",
              "minLength": 128,
              "maxLength": 128
            },
            "observation": {
              "type": "object",
              "required": [
                "working_copy",
                "limitation"
              ],
              "properties": {
                "working_copy": {
                  "const": "stored-unsnapshotted"
                },
                "limitation": {
                  "type": "string",
                  "minLength": 1
                }
              }
            }
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "status": {
                "const": "ok"
              }
            }
          },
          "then": {
            "required": [
              "jj_repository"
            ],
            "properties": {
              "diagnostics": {
                "maxItems": 0
              }
            }
          },
          "else": {
            "not": {
              "required": [
                "jj_repository"
              ]
            },
            "properties": {
              "diagnostics": {
                "minItems": 1
              }
            }
          }
        }
      ]
    },
    "repository": {
      "type": "object",
      "required": [
        "host",
        "owner",
        "name",
        "selection"
      ],
      "properties": {
        "host": {
          "type": "string",
          "minLength": 1
        },
        "owner": {
          "type": "string",
          "minLength": 1
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "selection": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "candidate": {
      "type": "object",
      "required": [
        "remote",
        "host",
        "owner",
        "name"
      ],
      "properties": {
        "remote": {
          "type": "string"
        },
        "host": {
          "type": "string"
        },
        "owner": {
          "type": "string"
        },
        "name": {
          "type": "string"
        }
      }
    },
    "pr": {
      "type": "object",
      "required": [
        "number",
        "title",
        "url",
        "state",
        "draft",
        "head_sha",
        "base_sha",
        "head_ref",
        "base_ref",
        "head_repository",
        "base_repository"
      ],
      "properties": {
        "number": {
          "type": "integer",
          "minimum": 1
        },
        "title": {
          "type": "string"
        },
        "url": {
          "type": "string"
        },
        "state": {
          "type": "string"
        },
        "draft": {
          "type": "boolean"
        },
        "head_sha": {
          "type": "string",
          "pattern": "^[0-9a-f]{40}$"
        },
        "base_sha": {
          "type": "string",
          "pattern": "^[0-9a-f]{40}$"
        },
        "head_ref": {
          "type": "string"
        },
        "base_ref": {
          "type": "string"
        },
        "head_repository": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$"
        },
        "base_repository": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$"
        }
      }
    },
    "check": {
      "type": "object",
      "required": [
        "id",
        "name",
        "status",
        "head_sha",
        "url",
        "conclusion"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "minimum": 1
        },
        "name": {
          "type": "string"
        },
        "status": {
          "type": "string"
        },
        "head_sha": {
          "type": "string",
          "pattern": "^[0-9a-f]{40}$"
        },
        "url": {
          "type": "string"
        },
        "conclusion": {
          "anyOf": [
            {
              "type": "null"
            },
            {
              "type": "string"
            }
          ]
        }
      }
    },
    "commit_status": {
      "type": "object",
      "required": [
        "id",
        "context",
        "state",
        "description",
        "url"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "minimum": 1
        },
        "context": {
          "type": "string"
        },
        "state": {
          "type": "string"
        },
        "description": {
          "type": "string"
        },
        "url": {
          "type": "string"
        }
      }
    },
    "diagnostic": {
      "type": "object",
      "required": [
        "code",
        "message"
      ],
      "properties": {
        "code": {
          "type": "string",
          "minLength": 1
        },
        "message": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "boundary": {
      "type": "object",
      "required": [
        "complete",
        "network",
        "max_pages",
        "max_records",
        "max_bytes",
        "timeout_seconds",
        "limitations"
      ],
      "properties": {
        "complete": {
          "type": "boolean"
        },
        "network": {
          "type": "boolean"
        },
        "max_pages": {
          "const": 10
        },
        "max_records": {
          "const": 1000
        },
        "max_bytes": {
          "const": 1048576
        },
        "timeout_seconds": {
          "const": 30
        },
        "limitations": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "status": {
            "const": "ok"
          }
        }
      },
      "then": {
        "properties": {
          "identity": {
            "type": "object",
            "properties": {
              "status": {
                "const": "ok"
              }
            }
          },
          "repository": {
            "type": "object"
          },
          "diagnostics": {
            "maxItems": 0
          },
          "boundary": {
            "properties": {
              "complete": {
                "const": true
              }
            }
          }
        }
      },
      "else": {
        "properties": {
          "diagnostics": {
            "minItems": 1
          },
          "boundary": {
            "properties": {
              "complete": {
                "const": false
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "repo"
          }
        }
      },
      "then": {
        "properties": {
          "pull_request": {
            "type": "null"
          },
          "checks": {
            "maxItems": 0
          },
          "statuses": {
            "maxItems": 0
          },
          "boundary": {
            "properties": {
              "network": {
                "const": false
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "enum": [
              "pr",
              "ci"
            ]
          },
          "status": {
            "const": "ok"
          }
        }
      },
      "then": {
        "properties": {
          "pull_request": {
            "type": "object"
          },
          "boundary": {
            "properties": {
              "network": {
                "const": true
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "backend": {
            "const": "git"
          },
          "identity": {
            "type": "object"
          }
        }
      },
      "then": {
        "properties": {
          "identity": {
            "properties": {
              "backend": {
                "const": "git"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "backend": {
            "const": "jj"
          },
          "identity": {
            "type": "object"
          }
        }
      },
      "then": {
        "properties": {
          "identity": {
            "properties": {
              "backend": {
                "const": "jj"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "backend": {
            "const": "unknown"
          },
          "identity": {
            "type": "object"
          }
        }
      },
      "then": {
        "properties": {
          "identity": {
            "properties": {
              "backend": {
                "const": "unknown"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "backend": {
            "const": "ambiguous"
          },
          "identity": {
            "type": "object"
          }
        }
      },
      "then": {
        "properties": {
          "identity": {
            "properties": {
              "backend": {
                "const": "ambiguous"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "backend": {
            "const": "gitbutler"
          },
          "identity": {
            "type": "object"
          }
        }
      },
      "then": {
        "properties": {
          "identity": {
            "properties": {
              "backend": {
                "const": "gitbutler"
              }
            }
          }
        }
      }
    }
  ]
}
