{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.invalid/agentplaykit/runlog.schema.json",
  "title": "AgentPlay run log event (log_version 1); one JSON object per JSONL line",
  "type": "object",
  "required": [
    "type",
    "seq",
    "ts"
  ],
  "properties": {
    "seq": {
      "type": "integer",
      "minimum": 0
    },
    "ts": {
      "type": "string",
      "format": "date-time"
    },
    "type": {
      "type": "string"
    }
  },
  "oneOf": [
    {
      "$ref": "#/$defs/run_start"
    },
    {
      "$ref": "#/$defs/turn_start"
    },
    {
      "$ref": "#/$defs/observation"
    },
    {
      "$ref": "#/$defs/action"
    },
    {
      "$ref": "#/$defs/step_result"
    },
    {
      "$ref": "#/$defs/message"
    },
    {
      "$ref": "#/$defs/message_dropped"
    },
    {
      "$ref": "#/$defs/warning"
    },
    {
      "$ref": "#/$defs/usage"
    },
    {
      "$ref": "#/$defs/provider_error"
    },
    {
      "$ref": "#/$defs/agent_error"
    },
    {
      "$ref": "#/$defs/forfeit"
    },
    {
      "$ref": "#/$defs/limit_hit"
    },
    {
      "$ref": "#/$defs/run_end"
    }
  ],
  "$defs": {
    "agentId": {
      "type": "string",
      "minLength": 1
    },
    "step": {
      "type": "integer",
      "minimum": 0
    },
    "run_start": {
      "properties": {
        "type": {
          "const": "run_start"
        },
        "log_version": {
          "const": 1
        },
        "run_id": {
          "type": "string"
        },
        "seed": {
          "type": "integer"
        },
        "world": {
          "type": "object",
          "required": [
            "name",
            "version"
          ],
          "properties": {
            "name": {
              "type": "string"
            },
            "version": {
              "type": "string"
            },
            "view": {
              "type": "object",
              "description": "Optional drawing hint from describe() (SPEC-012 VWR-012, LOG-022).",
              "required": [
                "type"
              ],
              "properties": {
                "type": {
                  "type": "string"
                }
              }
            }
          }
        },
        "agents": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "required": [
              "id",
              "kind"
            ],
            "properties": {
              "id": {
                "$ref": "#/$defs/agentId"
              },
              "kind": {
                "type": "string"
              },
              "model": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "capabilities": {
                "type": "object"
              },
              "use": {
                "type": "string",
                "description": "the scenario reference as written (LOG-023)"
              },
              "name": {
                "type": "string",
                "description": "display name from the scenario (LOG-023)"
              }
            }
          }
        },
        "limits": {
          "type": "object"
        },
        "scenario_sha256": {
          "type": "string"
        },
        "versions": {
          "type": "object",
          "required": [
            "agentplaykit",
            "python",
            "platform"
          ]
        }
      },
      "required": [
        "log_version",
        "run_id",
        "seed",
        "world",
        "agents",
        "limits",
        "scenario_sha256",
        "versions"
      ]
    },
    "turn_start": {
      "properties": {
        "type": {
          "const": "turn_start"
        },
        "step": {
          "$ref": "#/$defs/step"
        },
        "to_act": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/agentId"
          },
          "minItems": 1
        }
      },
      "required": [
        "step",
        "to_act"
      ]
    },
    "observation": {
      "properties": {
        "type": {
          "const": "observation"
        },
        "agent": {
          "$ref": "#/$defs/agentId"
        },
        "step": {
          "$ref": "#/$defs/step"
        },
        "data": {}
      },
      "required": [
        "agent",
        "step",
        "data"
      ]
    },
    "action": {
      "properties": {
        "type": {
          "const": "action"
        },
        "agent": {
          "$ref": "#/$defs/agentId"
        },
        "step": {
          "$ref": "#/$defs/step"
        },
        "action": {},
        "reasoning": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "required": [
        "agent",
        "step",
        "action"
      ]
    },
    "step_result": {
      "properties": {
        "type": {
          "const": "step_result"
        },
        "agent": {
          "$ref": "#/$defs/agentId"
        },
        "step": {
          "$ref": "#/$defs/step"
        },
        "ok": {
          "type": "boolean"
        },
        "error": {
          "type": [
            "string",
            "null"
          ]
        },
        "done": {
          "type": "boolean"
        },
        "rewards": {
          "type": "object",
          "additionalProperties": {
            "type": "number"
          }
        },
        "info": {
          "type": "object"
        }
      },
      "required": [
        "agent",
        "step",
        "ok",
        "error",
        "done",
        "rewards",
        "info"
      ]
    },
    "message": {
      "properties": {
        "type": {
          "const": "message"
        },
        "from": {
          "$ref": "#/$defs/agentId"
        },
        "to": {
          "anyOf": [
            {
              "const": "*"
            },
            {
              "$ref": "#/$defs/agentId"
            },
            {
              "type": "array",
              "items": {
                "$ref": "#/$defs/agentId"
              },
              "minItems": 1
            }
          ]
        },
        "text": {
          "type": "string"
        },
        "step": {
          "$ref": "#/$defs/step"
        }
      },
      "required": [
        "from",
        "to",
        "text",
        "step"
      ]
    },
    "usage": {
      "properties": {
        "type": {
          "const": "usage"
        },
        "agent": {
          "$ref": "#/$defs/agentId"
        },
        "step": {
          "$ref": "#/$defs/step"
        },
        "input_tokens": {
          "type": "integer",
          "minimum": 0
        },
        "output_tokens": {
          "type": "integer",
          "minimum": 0
        },
        "cost_usd": {
          "type": [
            "number",
            "null"
          ],
          "minimum": 0
        },
        "provider": {
          "type": [
            "string",
            "null"
          ]
        },
        "model": {
          "type": [
            "string",
            "null"
          ]
        },
        "total_cost_usd": {
          "type": "number",
          "minimum": 0
        },
        "mode": {
          "enum": [
            "tool",
            "text"
          ]
        },
        "estimated": {
          "type": "boolean"
        }
      },
      "required": [
        "agent",
        "step",
        "input_tokens",
        "output_tokens",
        "cost_usd",
        "provider",
        "model",
        "total_cost_usd"
      ]
    },
    "provider_error": {
      "properties": {
        "type": {
          "const": "provider_error"
        },
        "agent": {
          "$ref": "#/$defs/agentId"
        },
        "step": {
          "$ref": "#/$defs/step"
        },
        "message": {
          "type": "string"
        },
        "attempt": {
          "type": "integer",
          "minimum": 1
        },
        "retryable": {
          "type": "boolean"
        }
      },
      "required": [
        "agent",
        "step",
        "message",
        "attempt",
        "retryable"
      ]
    },
    "agent_error": {
      "properties": {
        "type": {
          "const": "agent_error"
        },
        "agent": {
          "$ref": "#/$defs/agentId"
        },
        "step": {
          "$ref": "#/$defs/step"
        },
        "message": {
          "type": "string"
        }
      },
      "required": [
        "agent",
        "step",
        "message"
      ]
    },
    "forfeit": {
      "properties": {
        "type": {
          "const": "forfeit"
        },
        "agent": {
          "$ref": "#/$defs/agentId"
        },
        "reason": {
          "type": "string"
        }
      },
      "required": [
        "agent",
        "reason"
      ]
    },
    "limit_hit": {
      "properties": {
        "type": {
          "const": "limit_hit"
        },
        "limit": {
          "enum": [
            "steps",
            "time",
            "cost",
            "tokens"
          ]
        },
        "value": {
          "type": "number"
        }
      },
      "required": [
        "limit",
        "value"
      ]
    },
    "run_end": {
      "properties": {
        "type": {
          "const": "run_end"
        },
        "reason": {
          "enum": [
            "finished",
            "step_limit",
            "time_limit",
            "budget_exceeded",
            "forfeit",
            "world_error",
            "aborted"
          ]
        },
        "outcome": {
          "oneOf": [
            {
              "type": "null"
            },
            {
              "type": "object",
              "required": [
                "winners",
                "scores"
              ],
              "properties": {
                "winners": {
                  "type": "array",
                  "items": {
                    "$ref": "#/$defs/agentId"
                  }
                },
                "scores": {
                  "type": "object",
                  "additionalProperties": {
                    "type": "number"
                  }
                },
                "checks": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "required": [
                      "name",
                      "passed"
                    ],
                    "properties": {
                      "name": {
                        "type": "string"
                      },
                      "passed": {
                        "type": "boolean"
                      },
                      "exit_code": {
                        "type": [
                          "integer",
                          "null"
                        ]
                      },
                      "timed_out": {
                        "type": "boolean"
                      },
                      "output": {
                        "type": "string"
                      }
                    }
                  },
                  "description": "sandbox checks (SPEC-014)"
                },
                "sandbox": {
                  "type": "object",
                  "description": "sandbox image, digest and end of work (SPEC-014)"
                }
              }
            }
          ]
        },
        "steps": {
          "type": "integer",
          "minimum": 0
        },
        "total_cost_usd": {
          "type": "number",
          "minimum": 0
        },
        "duration_s": {
          "type": "number",
          "minimum": 0
        },
        "error": {
          "type": "string",
          "description": "why the run ended world_error or aborted (LOG-024)"
        }
      },
      "required": [
        "reason",
        "outcome",
        "steps",
        "total_cost_usd",
        "duration_s"
      ]
    },
    "message_dropped": {
      "properties": {
        "type": {
          "const": "message_dropped"
        },
        "from": {
          "$ref": "#/$defs/agentId"
        },
        "to": {},
        "text": {
          "type": [
            "string",
            "null"
          ]
        },
        "step": {
          "$ref": "#/$defs/step"
        },
        "reason": {
          "type": "string"
        }
      },
      "required": [
        "from",
        "to",
        "text",
        "step",
        "reason"
      ]
    },
    "warning": {
      "properties": {
        "type": {
          "const": "warning"
        },
        "message": {
          "type": "string"
        },
        "agent": {
          "$ref": "#/$defs/agentId"
        }
      },
      "required": [
        "message"
      ]
    }
  }
}
