{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "agent-continuity interchange format",
  "description": "Portable export and recovery envelope for agent-continuity.",
  "type": "object",
  "required": ["format", "session", "messages", "events", "checkpoints"],
  "additionalProperties": false,
  "properties": {
    "format": { "const": "agent-continuity-export" },
    "session": { "$ref": "#/definitions/Session" },
    "branches": {
      "type": "array",
      "items": { "$ref": "#/definitions/Branch" }
    },
    "messages": {
      "type": "array",
      "items": { "$ref": "#/definitions/Message" }
    },
    "events": {
      "type": "array",
      "items": { "$ref": "#/definitions/Event" }
    },
    "checkpoints": {
      "type": "array",
      "items": { "$ref": "#/definitions/Checkpoint" }
    },
    "native_states": {
      "type": "array",
      "items": { "$ref": "#/definitions/NativeState" }
    },
    "artifacts": {
      "type": "array",
      "items": { "$ref": "#/definitions/Artifact" }
    }
  },
  "definitions": {
    "Session": {
      "type": "object",
      "required": ["session_id", "title", "status", "active_branch_id"],
      "additionalProperties": false,
      "properties": {
        "session_id": { "type": "string", "minLength": 1 },
        "title": { "type": "string" },
        "status": { "type": "string", "enum": ["active", "paused", "completed", "archived"] },
        "active_branch_id": {
          "type": "string",
          "minLength": 1,
          "description": "Globally unique identifier of the session's active branch."
        },
        "metadata": { "type": "object" }
      }
    },
    "Message": {
      "type": "object",
      "required": ["message_id", "session_id", "role", "status", "text_content", "sequence"],
      "additionalProperties": false,
      "properties": {
        "message_id": { "type": "string", "minLength": 1 },
        "session_id": { "type": "string", "minLength": 1 },
        "run_id": { "type": ["string", "null"] },
        "branch_id": {
          "type": "string",
          "minLength": 1,
          "description": "Globally unique branch identifier; the human-readable branch name is separate."
        },
        "role": { "type": "string", "enum": ["user", "assistant", "tool"] },
        "status": { "type": "string", "enum": ["pending", "streaming", "complete", "partial", "failed"] },
        "content_json": {},
        "text_content": { "type": "string" },
        "provider_message_id": { "type": ["string", "null"] },
        "idempotency_key": { "type": ["string", "null"] },
        "sequence": { "type": "integer", "minimum": 1 },
        "sha256": { "type": "string", "minLength": 1 },
        "created_at": { "type": "string", "minLength": 1 }
      }
    },
    "Branch": {
      "type": "object",
      "required": ["branch_id", "session_id", "name", "status", "metadata", "created_at", "updated_at"],
      "additionalProperties": false,
      "properties": {
        "branch_id": {
          "type": "string",
          "minLength": 1,
          "description": "Globally unique branch identifier; the human-readable branch name is separate."
        },
        "session_id": { "type": "string", "minLength": 1 },
        "parent_branch_id": { "type": ["string", "null"] },
        "name": { "type": "string", "minLength": 1 },
        "status": { "type": "string", "enum": ["active", "paused", "archived"] },
        "metadata": { "type": "object" },
        "created_at": { "type": "string", "minLength": 1 },
        "updated_at": { "type": "string", "minLength": 1 }
      }
    },
    "Event": {
      "type": "object",
      "required": ["event_id", "session_id", "event_type", "payload"],
      "additionalProperties": false,
      "properties": {
        "event_id": { "type": "string", "minLength": 1 },
        "session_id": { "type": "string", "minLength": 1 },
        "run_id": { "type": ["string", "null"] },
        "branch_id": {
          "type": "string",
          "minLength": 1,
          "description": "Globally unique branch identifier; the human-readable branch name is separate."
        },
        "sequence": { "type": "integer", "minimum": 1 },
        "event_type": { "type": "string", "minLength": 1 },
        "payload": { "type": "object" },
        "idempotency_key": { "type": ["string", "null"] },
        "sha256": { "type": "string", "minLength": 1 },
        "created_at": { "type": "string", "minLength": 1 }
      }
    },
    "Checkpoint": {
      "type": "object",
      "required": ["checkpoint_id", "session_id", "branch_id", "summary"],
      "additionalProperties": false,
      "properties": {
        "checkpoint_id": { "type": "string", "minLength": 1 },
        "session_id": { "type": "string", "minLength": 1 },
        "branch_id": {
          "type": "string",
          "minLength": 1,
          "description": "Globally unique branch identifier; the human-readable branch name is separate."
        },
        "parent_event_id": { "type": ["string", "null"] },
        "summary": { "type": "object" },
        "token_estimate": { "type": "integer", "minimum": 0 },
        "created_by": { "type": ["string", "null"] },
        "created_at": { "type": "string", "minLength": 1 }
      }
    },
    "NativeState": {
      "type": "object",
      "required": ["state_id", "session_id", "provider", "kind", "portable"],
      "additionalProperties": false,
      "properties": {
        "state_id": { "type": "string", "minLength": 1 },
        "session_id": { "type": "string", "minLength": 1 },
        "provider": { "type": "string", "minLength": 1 },
        "kind": { "type": "string", "minLength": 1 },
        "payload": {},
        "portable": { "type": "boolean" },
        "created_at": { "type": "string", "minLength": 1 }
      }
    },
    "Artifact": {
      "type": "object",
      "required": ["artifact_id", "session_id", "branch_id", "kind", "path", "metadata", "created_at"],
      "additionalProperties": false,
      "properties": {
        "artifact_id": { "type": "string", "minLength": 1 },
        "session_id": { "type": "string", "minLength": 1 },
        "run_id": { "type": ["string", "null"] },
        "branch_id": {
          "type": "string",
          "minLength": 1,
          "description": "Globally unique branch identifier; the human-readable branch name is separate."
        },
        "kind": { "type": "string", "minLength": 1 },
        "path": { "type": "string", "minLength": 1 },
        "sha256": { "type": ["string", "null"] },
        "size_bytes": { "type": ["integer", "null"], "minimum": 0 },
        "metadata": { "type": "object" },
        "created_at": { "type": "string", "minLength": 1 }
      }
    }
  }
}
