{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "contract": {
      "type": "string",
      "const": "AgentSessionCommandResult"
    },
    "commandId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256
    },
    "sessionId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256
    },
    "runId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256
    },
    "idempotencyKey": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256
    },
    "outcome": {
      "type": "string",
      "enum": [
        "accepted",
        "duplicate",
        "rejected",
        "failed"
      ]
    },
    "sessionStatus": {
      "type": "string",
      "enum": [
        "queued",
        "running",
        "waiting_approval",
        "stopping",
        "stopped",
        "completed",
        "failed"
      ]
    },
    "acceptedSequence": {
      "type": "integer",
      "minimum": 0,
      "maximum": 9007199254740991
    },
    "resultEventIds": {
      "default": [],
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 256
      }
    },
    "operation": {
      "type": "object",
      "properties": {
        "lineage": {
          "type": "object",
          "properties": {
            "forkedFromThreadId": {
              "type": "string",
              "minLength": 1,
              "maxLength": 256
            },
            "forkedFromMessageId": {
              "type": "string",
              "minLength": 1,
              "maxLength": 256
            },
            "sourceRunId": {
              "type": "string",
              "minLength": 1,
              "maxLength": 256
            }
          },
          "required": [
            "forkedFromThreadId",
            "forkedFromMessageId",
            "sourceRunId"
          ],
          "additionalProperties": false
        },
        "activeBranch": {
          "type": "object",
          "properties": {
            "branchId": {
              "type": "string",
              "minLength": 1,
              "maxLength": 256
            },
            "threadId": {
              "type": "string",
              "minLength": 1,
              "maxLength": 256
            },
            "sourceMessageId": {
              "type": "string",
              "minLength": 1,
              "maxLength": 256
            },
            "runId": {
              "type": "string",
              "minLength": 1,
              "maxLength": 256
            },
            "lineage": {
              "type": "object",
              "properties": {
                "forkedFromThreadId": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 256
                },
                "forkedFromMessageId": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 256
                },
                "sourceRunId": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 256
                }
              },
              "required": [
                "forkedFromThreadId",
                "forkedFromMessageId",
                "sourceRunId"
              ],
              "additionalProperties": false
            },
            "createdAt": {
              "type": "string",
              "format": "date-time",
              "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d+)?)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
            }
          },
          "required": [
            "branchId",
            "threadId",
            "sourceMessageId",
            "runId",
            "createdAt"
          ],
          "additionalProperties": false
        }
      },
      "required": [
        "lineage",
        "activeBranch"
      ],
      "additionalProperties": false
    },
    "termination": {
      "type": "string",
      "enum": [
        "requested",
        "confirmed"
      ]
    },
    "error": {
      "type": "object",
      "properties": {
        "code": {
          "type": "string",
          "enum": [
            "SESSION_NOT_FOUND",
            "RUN_NOT_FOUND",
            "ACTIVE_RUN_NOT_FOUND",
            "RUN_ALREADY_FINISHED",
            "STOP_TIMEOUT",
            "COMMAND_NOT_SUPPORTED",
            "CAPABILITY_UNAVAILABLE",
            "SEQUENCE_CONFLICT",
            "INVALID_SESSION_STATE",
            "TARGET_EVENT_NOT_FOUND",
            "TARGET_EVENT_NOT_RETRYABLE",
            "APPROVAL_NOT_FOUND",
            "APPROVAL_ALREADY_RESOLVED",
            "SOURCE_THREAD_NOT_FOUND",
            "SOURCE_MESSAGE_NOT_FOUND",
            "SOURCE_RUN_NOT_FOUND",
            "SOURCE_LINEAGE_INVALID",
            "ACTIVE_BRANCH_NOT_FOUND",
            "COMMAND_EXECUTION_FAILED"
          ]
        },
        "message": {
          "type": "string"
        },
        "retryable": {
          "type": "boolean"
        }
      },
      "required": [
        "code",
        "message",
        "retryable"
      ],
      "additionalProperties": false
    },
    "occurredAt": {
      "type": "string",
      "format": "date-time",
      "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d+)?)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
    }
  },
  "required": [
    "contract",
    "commandId",
    "sessionId",
    "idempotencyKey",
    "outcome",
    "sessionStatus",
    "resultEventIds",
    "occurredAt"
  ],
  "additionalProperties": false,
  "$id": "https://schemas.infmonkeys.com/contracts/agent-session-command-result.schema.json",
  "title": "agent-session-command-result"
}
