{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://nexus-agent-runs.netlify.app/schemas/run.schema.json",
  "title": "Open Agent Run: run document (oar/0.1, oar/0.2)",
  "type": "object",
  "required": [
    "format", "id", "agent", "model", "input", "status", "messages",
    "pendingApprovals", "bufferedToolResults", "usage", "turns",
    "createdAt", "updatedAt"
  ],
  "properties": {
    "format": { "enum": ["oar/0.1", "oar/0.2"] },
    "tags": { "type": "object", "additionalProperties": { "type": "string" } },
    "id": { "type": "string", "pattern": "^[A-Za-z0-9_-]+$" },
    "agent": { "type": "string", "minLength": 1 },
    "model": { "type": "string", "minLength": 1 },
    "input": { "type": "string" },
    "status": {
      "enum": [
        "running", "waiting_approval", "completed", "failed",
        "halted_budget", "halted_max_turns", "cancelled"
      ]
    },
    "messages": { "type": "array", "items": { "$ref": "#/$defs/message" } },
    "pendingApprovals": { "type": "array", "items": { "$ref": "#/$defs/pendingApproval" } },
    "bufferedToolResults": { "type": "array", "items": { "$ref": "#/$defs/toolResultPart" } },
    "usage": {
      "type": "object",
      "required": ["inputTokens", "outputTokens", "costUsd"],
      "properties": {
        "inputTokens": { "type": "number", "minimum": 0 },
        "outputTokens": { "type": "number", "minimum": 0 },
        "costUsd": { "type": "number", "minimum": 0 }
      }
    },
    "turns": { "type": "integer", "minimum": 0 },
    "result": { "type": "string" },
    "error": { "type": "string" },
    "createdAt": { "type": "string" },
    "updatedAt": { "type": "string" }
  },
  "allOf": [
    {
      "$comment": "The responded decision is an oar/0.2 addition; reject it in a document that declares oar/0.1.",
      "if": { "required": ["format"], "properties": { "format": { "const": "oar/0.1" } } },
      "then": {
        "properties": {
          "pendingApprovals": {
            "type": "array",
            "items": { "type": "object", "properties": { "decision": { "enum": ["approved", "denied"] } } }
          }
        }
      }
    }
  ],
  "$defs": {
    "textPart": {
      "type": "object",
      "required": ["type", "text"],
      "properties": {
        "type": { "const": "text" },
        "text": { "type": "string" }
      }
    },
    "toolCallPart": {
      "type": "object",
      "required": ["type", "id", "name", "arguments"],
      "properties": {
        "type": { "const": "tool_call" },
        "id": { "type": "string" },
        "name": { "type": "string" },
        "arguments": { "type": "object" }
      }
    },
    "toolResultPart": {
      "type": "object",
      "required": ["type", "toolCallId", "toolName", "content"],
      "properties": {
        "type": { "const": "tool_result" },
        "toolCallId": { "type": "string" },
        "toolName": { "type": "string" },
        "content": { "type": "string" },
        "isError": { "type": "boolean" }
      }
    },
    "message": {
      "oneOf": [
        {
          "type": "object",
          "required": ["role", "content"],
          "properties": {
            "role": { "const": "user" },
            "content": {
              "type": "array",
              "items": {
                "oneOf": [
                  { "$ref": "#/$defs/textPart" },
                  { "$ref": "#/$defs/toolResultPart" }
                ]
              }
            }
          }
        },
        {
          "type": "object",
          "required": ["role", "content"],
          "properties": {
            "role": { "const": "assistant" },
            "content": {
              "type": "array",
              "items": {
                "oneOf": [
                  { "$ref": "#/$defs/textPart" },
                  { "$ref": "#/$defs/toolCallPart" }
                ]
              }
            }
          }
        }
      ]
    },
    "pendingApproval": {
      "type": "object",
      "required": ["toolCallId", "toolName", "arguments", "requestedAt"],
      "properties": {
        "toolCallId": { "type": "string" },
        "toolName": { "type": "string" },
        "arguments": { "type": "object" },
        "requestedAt": { "type": "string" },
        "decision": { "enum": ["approved", "denied", "responded"] },
        "decidedBy": { "enum": ["human", "policy"] },
        "reason": { "type": "string" },
        "editedArguments": { "type": "object" },
        "response": { "type": "string" }
      }
    }
  }
}
