{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://tmux-xterm-research.local/harness/envelope.schema.json",
  "title": "Harness Message Envelope",
  "type": "object",
  "required": ["id", "run_id", "from", "to", "type", "ttl_ms", "payload"],
  "properties": {
    "id":          { "type": "string", "minLength": 1 },
    "run_id":      { "type": "string", "minLength": 1 },
    "from":        { "type": "string", "minLength": 1 },
    "to":          { "type": "string", "minLength": 1 },
    "task_id":     { "type": "string" },
    "in_reply_to": { "type": "string" },
    "ttl_ms":      { "type": "integer", "exclusiveMinimum": 0 },
    "priority":    { "type": "integer" },
    "created_at":  { "type": "string" },
    "type": {
      "type": "string",
      "enum": [
        "delegate", "report", "query", "clarify", "answer",
        "ack", "nack", "heartbeat", "steering", "interrupt",
        "action.draft", "action.draft.approve", "action.draft.reject"
      ]
    },
    "payload": {
      "type": "object",
      "properties": {
        "intent":       { "type": "string" },
        "context_refs": { "type": "array", "items": { "type": "string" } },
        "expects":      { "type": "string", "enum": ["none", "ack", "answer", "report"] },
        "deadline":     { "type": "string" },
        "reason":       { "type": "string" },
        "extra":        { "type": "object" }
      },
      "additionalProperties": false
    }
  },
  "allOf": [
    {
      "if":   { "properties": { "type": { "const": "interrupt" } } },
      "then": { "properties": { "payload": { "required": ["expects"], "properties": { "expects": { "const": "ack" } } } } }
    },
    {
      "if":   { "properties": { "type": { "const": "steering" } } },
      "then": {
        "required": ["task_id"],
        "properties": { "payload": { "required": ["expects"], "properties": { "expects": { "const": "ack" } } } }
      }
    }
  ]
}
