{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:psyon:processing-event:1.0.0",
  "title": "Psyon trusted-host processing API events",
  "oneOf": [
    {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "action",
        "message"
      ],
      "properties": {
        "action": {
          "const": "plan_turn"
        },
        "message": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "turn_id",
            "participant_id",
            "source_kind",
            "source_ref",
            "content"
          ],
          "properties": {
            "turn_id": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200
            },
            "participant_id": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200
            },
            "source_kind": {
              "enum": [
                "human_message",
                "machine_message",
                "quoted_material",
                "retrieved_document",
                "tool_output",
                "media_transcript",
                "previous_summary"
              ]
            },
            "source_ref": {
              "type": "string",
              "minLength": 1,
              "maxLength": 20000
            },
            "content": {
              "type": "string",
              "minLength": 1,
              "maxLength": 20000
            }
          }
        }
      }
    },
    {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "action",
        "record"
      ],
      "properties": {
        "action": {
          "const": "record_turn"
        },
        "record": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "turn_id",
            "participant_id",
            "source_ref",
            "tool_log",
            "gate",
            "outcome",
            "response_text"
          ],
          "properties": {
            "turn_id": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200
            },
            "participant_id": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200
            },
            "source_ref": {
              "type": "string",
              "minLength": 1,
              "maxLength": 20000
            },
            "tool_log": {
              "type": "array",
              "minItems": 10,
              "maxItems": 10,
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "command_id",
                  "status",
                  "reason",
                  "noticed",
                  "checked",
                  "unknown",
                  "chosen_action",
                  "review"
                ],
                "properties": {
                  "command_id": {
                    "enum": [
                      "K01",
                      "K02",
                      "K03",
                      "K04",
                      "K05",
                      "K06",
                      "K07",
                      "K08",
                      "K09",
                      "K10"
                    ]
                  },
                  "status": {
                    "enum": [
                      "applied",
                      "not_applicable"
                    ]
                  },
                  "reason": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 20000
                  },
                  "noticed": {
                    "type": "array",
                    "maxItems": 100,
                    "items": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 20000
                    }
                  },
                  "checked": {
                    "type": "array",
                    "maxItems": 100,
                    "items": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 20000
                    }
                  },
                  "unknown": {
                    "type": "array",
                    "maxItems": 100,
                    "items": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 20000
                    }
                  },
                  "chosen_action": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 20000
                  },
                  "review": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 20000
                  }
                }
              }
            },
            "gate": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "scope_ok",
                "permissions_ok",
                "source_boundary_ok",
                "privacy_ok",
                "validated_by_host",
                "reason"
              ],
              "properties": {
                "scope_ok": {
                  "type": [
                    "boolean",
                    "null"
                  ]
                },
                "permissions_ok": {
                  "type": [
                    "boolean",
                    "null"
                  ]
                },
                "source_boundary_ok": {
                  "type": [
                    "boolean",
                    "null"
                  ]
                },
                "privacy_ok": {
                  "type": [
                    "boolean",
                    "null"
                  ]
                },
                "validated_by_host": {
                  "const": true
                },
                "reason": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 20000
                }
              }
            },
            "outcome": {
              "enum": [
                "reply",
                "clarify",
                "pause",
                "decline_requested_action",
                "seek_review"
              ]
            },
            "response_text": {
              "type": "string",
              "minLength": 1,
              "maxLength": 20000
            }
          }
        }
      }
    },
    {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "action",
        "correction_id",
        "turn_id",
        "author_id",
        "correction"
      ],
      "properties": {
        "action": {
          "const": "correct_record"
        },
        "correction_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "turn_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "author_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "correction": {
          "type": "string",
          "minLength": 1,
          "maxLength": 20000
        }
      }
    },
    {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "action",
        "review"
      ],
      "properties": {
        "action": {
          "const": "finish_session"
        },
        "review": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "reviewer_id",
            "checks",
            "next_session",
            "carry_forward"
          ],
          "properties": {
            "reviewer_id": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200
            },
            "checks": {
              "type": "array",
              "minItems": 10,
              "maxItems": 10,
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "check_id",
                  "status",
                  "note",
                  "evidence_refs",
                  "blocking",
                  "followup"
                ],
                "properties": {
                  "check_id": {
                    "enum": [
                      "vision",
                      "analysis",
                      "scenarios",
                      "options",
                      "legal_implications",
                      "policy_strategy",
                      "resources",
                      "planning_doing",
                      "policy_performance",
                      "evaluation"
                    ]
                  },
                  "status": {
                    "enum": [
                      "addressed",
                      "unknown",
                      "not_applicable"
                    ]
                  },
                  "note": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 20000
                  },
                  "evidence_refs": {
                    "type": "array",
                    "maxItems": 100,
                    "items": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 20000
                    }
                  },
                  "blocking": {
                    "type": "boolean"
                  },
                  "followup": {
                    "anyOf": [
                      {
                        "type": "null"
                      },
                      {
                        "type": "object",
                        "additionalProperties": false,
                        "required": [
                          "owner",
                          "review_trigger"
                        ],
                        "properties": {
                          "owner": {
                            "type": "string",
                            "minLength": 1,
                            "maxLength": 200
                          },
                          "review_trigger": {
                            "type": "string",
                            "minLength": 1,
                            "maxLength": 20000
                          }
                        }
                      }
                    ]
                  }
                }
              }
            },
            "next_session": {
              "enum": [
                "ready",
                "pause"
              ]
            },
            "carry_forward": {
              "type": "array",
              "maxItems": 100,
              "items": {
                "type": "string",
                "minLength": 1,
                "maxLength": 20000
              }
            }
          }
        }
      }
    },
    {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "action",
        "participant_id"
      ],
      "properties": {
        "action": {
          "const": "withdraw"
        },
        "participant_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        }
      }
    }
  ]
}
