{
  "$id": "https://comitiva.dev/schema/HubRunFinishInput.json",
  "title": "HubRunFinishInput",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "status": {
      "type": "string",
      "enum": [
        "complete",
        "cancelled",
        "error"
      ]
    },
    "stopReason": {
      "anyOf": [
        {
          "type": "string",
          "enum": [
            "end_turn",
            "max_tokens",
            "stop_sequence",
            "tool_use",
            "refusal",
            "pause_turn",
            "max_iterations",
            "cancelled",
            "other"
          ]
        },
        {
          "type": "null"
        }
      ]
    },
    "content": {
      "type": "array",
      "items": {
        "oneOf": [
          {
            "type": "object",
            "properties": {
              "type": {
                "type": "string",
                "const": "text"
              },
              "text": {
                "type": "string"
              }
            },
            "required": [
              "type",
              "text"
            ]
          },
          {
            "type": "object",
            "properties": {
              "type": {
                "type": "string",
                "const": "image"
              },
              "source": {
                "oneOf": [
                  {
                    "type": "object",
                    "properties": {
                      "kind": {
                        "type": "string",
                        "const": "base64"
                      },
                      "mediaType": {
                        "type": "string"
                      },
                      "data": {
                        "type": "string"
                      }
                    },
                    "required": [
                      "kind",
                      "mediaType",
                      "data"
                    ]
                  },
                  {
                    "type": "object",
                    "properties": {
                      "kind": {
                        "type": "string",
                        "const": "file"
                      },
                      "path": {
                        "type": "string"
                      },
                      "mediaType": {
                        "type": "string"
                      }
                    },
                    "required": [
                      "kind",
                      "path"
                    ]
                  }
                ]
              },
              "name": {
                "type": "string"
              }
            },
            "required": [
              "type",
              "source"
            ]
          },
          {
            "type": "object",
            "properties": {
              "type": {
                "type": "string",
                "const": "document"
              },
              "name": {
                "type": "string"
              },
              "mediaType": {
                "type": "string"
              },
              "source": {
                "oneOf": [
                  {
                    "type": "object",
                    "properties": {
                      "kind": {
                        "type": "string",
                        "const": "base64"
                      },
                      "mediaType": {
                        "type": "string"
                      },
                      "data": {
                        "type": "string"
                      }
                    },
                    "required": [
                      "kind",
                      "mediaType",
                      "data"
                    ]
                  },
                  {
                    "type": "object",
                    "properties": {
                      "kind": {
                        "type": "string",
                        "const": "file"
                      },
                      "path": {
                        "type": "string"
                      },
                      "mediaType": {
                        "type": "string"
                      }
                    },
                    "required": [
                      "kind",
                      "path"
                    ]
                  }
                ]
              }
            },
            "required": [
              "type",
              "name",
              "mediaType",
              "source"
            ]
          },
          {
            "type": "object",
            "properties": {
              "type": {
                "type": "string",
                "const": "tool_use"
              },
              "id": {
                "type": "string"
              },
              "toolServerId": {
                "type": "string"
              },
              "name": {
                "type": "string"
              },
              "input": {},
              "signature": {
                "type": "string"
              }
            },
            "required": [
              "type",
              "id",
              "toolServerId",
              "name",
              "input"
            ]
          },
          {
            "type": "object",
            "properties": {
              "type": {
                "type": "string",
                "const": "tool_result"
              },
              "toolUseId": {
                "type": "string"
              },
              "content": {
                "type": "array",
                "items": {
                  "oneOf": [
                    {
                      "type": "object",
                      "properties": {
                        "type": {
                          "type": "string",
                          "const": "text"
                        },
                        "text": {
                          "type": "string"
                        }
                      },
                      "required": [
                        "type",
                        "text"
                      ]
                    },
                    {
                      "type": "object",
                      "properties": {
                        "type": {
                          "type": "string",
                          "const": "image"
                        },
                        "source": {
                          "oneOf": [
                            {
                              "type": "object",
                              "properties": {
                                "kind": {
                                  "type": "string",
                                  "const": "base64"
                                },
                                "mediaType": {
                                  "type": "string"
                                },
                                "data": {
                                  "type": "string"
                                }
                              },
                              "required": [
                                "kind",
                                "mediaType",
                                "data"
                              ]
                            },
                            {
                              "type": "object",
                              "properties": {
                                "kind": {
                                  "type": "string",
                                  "const": "file"
                                },
                                "path": {
                                  "type": "string"
                                },
                                "mediaType": {
                                  "type": "string"
                                }
                              },
                              "required": [
                                "kind",
                                "path"
                              ]
                            }
                          ]
                        },
                        "name": {
                          "type": "string"
                        }
                      },
                      "required": [
                        "type",
                        "source"
                      ]
                    },
                    {
                      "type": "object",
                      "properties": {
                        "type": {
                          "type": "string",
                          "const": "document"
                        },
                        "name": {
                          "type": "string"
                        },
                        "mediaType": {
                          "type": "string"
                        },
                        "source": {
                          "oneOf": [
                            {
                              "type": "object",
                              "properties": {
                                "kind": {
                                  "type": "string",
                                  "const": "base64"
                                },
                                "mediaType": {
                                  "type": "string"
                                },
                                "data": {
                                  "type": "string"
                                }
                              },
                              "required": [
                                "kind",
                                "mediaType",
                                "data"
                              ]
                            },
                            {
                              "type": "object",
                              "properties": {
                                "kind": {
                                  "type": "string",
                                  "const": "file"
                                },
                                "path": {
                                  "type": "string"
                                },
                                "mediaType": {
                                  "type": "string"
                                }
                              },
                              "required": [
                                "kind",
                                "path"
                              ]
                            }
                          ]
                        }
                      },
                      "required": [
                        "type",
                        "name",
                        "mediaType",
                        "source"
                      ]
                    }
                  ]
                }
              },
              "isError": {
                "type": "boolean"
              },
              "durationMs": {
                "type": "number",
                "minimum": 0
              }
            },
            "required": [
              "type",
              "toolUseId",
              "content",
              "isError"
            ]
          }
        ]
      }
    },
    "error": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "code": {
              "type": "string",
              "enum": [
                "auth_failed",
                "rate_limited",
                "provider_unavailable",
                "provider_error",
                "binary_not_found",
                "not_logged_in",
                "sandbox_unavailable",
                "outside_roots",
                "read_only_root",
                "approval_denied",
                "tool_failed",
                "tool_server_failed",
                "invalid_request",
                "not_implemented",
                "unknown_provider",
                "not_found",
                "unsupported_content",
                "attachment_too_large",
                "unsupported_attachment",
                "attachment_too_many",
                "update_failed",
                "connection_in_use",
                "connection_disabled",
                "model_required",
                "secret_missing",
                "secret_store_unavailable",
                "oauth_not_configured",
                "oauth_failed",
                "oauth_cancelled",
                "google_not_connected",
                "google_reconnect_required",
                "runner_crashed",
                "runner_unavailable",
                "conversation_busy",
                "interrupted",
                "hub_unreachable",
                "hub_auth_required",
                "hub_incompatible",
                "forbidden",
                "run_expired",
                "agent_not_linked",
                "invitation_invalid",
                "invalid_credentials",
                "email_taken",
                "timeout",
                "internal"
              ]
            },
            "message": {
              "type": "string"
            },
            "retryable": {
              "type": "boolean"
            }
          },
          "required": [
            "code",
            "message",
            "retryable"
          ]
        },
        {
          "type": "null"
        }
      ]
    },
    "usage": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "provider": {
              "type": "string",
              "enum": [
                "anthropic",
                "openai-compatible",
                "google",
                "ollama",
                "claude-code",
                "codex",
                "gemini-cli"
              ]
            },
            "model": {
              "type": "string"
            },
            "inputTokens": {
              "anyOf": [
                {
                  "type": "integer",
                  "minimum": 0,
                  "maximum": 9007199254740991
                },
                {
                  "type": "null"
                }
              ]
            },
            "outputTokens": {
              "anyOf": [
                {
                  "type": "integer",
                  "minimum": 0,
                  "maximum": 9007199254740991
                },
                {
                  "type": "null"
                }
              ]
            },
            "cacheReadTokens": {
              "anyOf": [
                {
                  "type": "integer",
                  "minimum": 0,
                  "maximum": 9007199254740991
                },
                {
                  "type": "null"
                }
              ]
            },
            "cacheWriteTokens": {
              "anyOf": [
                {
                  "type": "integer",
                  "minimum": 0,
                  "maximum": 9007199254740991
                },
                {
                  "type": "null"
                }
              ]
            },
            "estimated": {
              "type": "boolean"
            },
            "estimatedCostUsd": {
              "anyOf": [
                {
                  "type": "number",
                  "minimum": 0
                },
                {
                  "type": "null"
                }
              ]
            },
            "costSource": {
              "anyOf": [
                {
                  "type": "string",
                  "enum": [
                    "table",
                    "override",
                    "harness"
                  ]
                },
                {
                  "type": "null"
                }
              ]
            },
            "costEstimated": {
              "type": "boolean"
            },
            "latencyMs": {
              "anyOf": [
                {
                  "type": "integer",
                  "minimum": 0,
                  "maximum": 9007199254740991
                },
                {
                  "type": "null"
                }
              ]
            }
          },
          "required": [
            "provider",
            "model",
            "inputTokens",
            "outputTokens",
            "cacheReadTokens",
            "cacheWriteTokens",
            "estimated",
            "estimatedCostUsd",
            "costSource",
            "costEstimated",
            "latencyMs"
          ]
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "required": [
    "status",
    "content",
    "error",
    "usage"
  ]
}
