{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://khy-os.dev/schemas/acp/message.json",
  "title": "ACP Message",
  "description": "Agent Communication Protocol message envelope — JSON-RPC 2.0 compatible",

  "type": "object",
  "required": ["jsonrpc", "method"],
  "properties": {
    "jsonrpc": { "const": "2.0" },
    "id": {
      "description": "Request ID for request/response correlation. Absent for notifications.",
      "type": ["string", "number"]
    },
    "method": {
      "type": "string",
      "enum": [
        "agent.spawn",
        "agent.kill",
        "agent.status",
        "task.submit",
        "task.result",
        "task.progress",
        "context.share",
        "tool.invoke",
        "tool.result",
        "message.send",
        "message.broadcast",
        "heartbeat"
      ]
    },
    "params": {
      "type": "object",
      "description": "Method-specific parameters"
    },
    "result": {
      "description": "Success result (response only)"
    },
    "error": {
      "$ref": "#/$defs/Error",
      "description": "Error object (response only)"
    }
  },
  "additionalProperties": false,

  "$defs": {
    "Error": {
      "type": "object",
      "required": ["code", "message"],
      "properties": {
        "code": { "type": "integer" },
        "message": { "type": "string" },
        "data": {}
      }
    },

    "AgentSpawnParams": {
      "type": "object",
      "required": ["role"],
      "properties": {
        "role": { "type": "string" },
        "parentId": { "type": "string" },
        "config": { "type": "object" },
        "processMode": { "type": "boolean", "default": false }
      }
    },
    "AgentKillParams": {
      "type": "object",
      "required": ["agentId"],
      "properties": {
        "agentId": { "type": "string" },
        "reason": { "type": "string" }
      }
    },
    "AgentStatusParams": {
      "type": "object",
      "properties": {
        "agentId": { "type": "string" }
      }
    },

    "TaskSubmitParams": {
      "type": "object",
      "required": ["agentId", "prompt"],
      "properties": {
        "agentId": { "type": "string" },
        "prompt": { "type": "string" },
        "context": { "type": "object" },
        "correlationId": { "type": "string" }
      }
    },
    "TaskResultParams": {
      "type": "object",
      "required": ["agentId", "correlationId"],
      "properties": {
        "agentId": { "type": "string" },
        "correlationId": { "type": "string" },
        "output": {},
        "metrics": { "type": "object" }
      }
    },
    "TaskProgressParams": {
      "type": "object",
      "required": ["agentId", "correlationId"],
      "properties": {
        "agentId": { "type": "string" },
        "correlationId": { "type": "string" },
        "chunk": { "type": "string" },
        "progress": { "type": "number", "minimum": 0, "maximum": 1 }
      }
    },

    "ContextShareParams": {
      "type": "object",
      "required": ["fromAgent", "key", "value"],
      "properties": {
        "fromAgent": { "type": "string" },
        "key": { "type": "string" },
        "value": {},
        "scope": { "type": "string", "enum": ["session", "persistent"], "default": "session" }
      }
    },

    "ToolInvokeParams": {
      "type": "object",
      "required": ["agentId", "tool", "input"],
      "properties": {
        "agentId": { "type": "string" },
        "tool": { "type": "string" },
        "input": { "type": "object" }
      }
    },
    "ToolResultParams": {
      "type": "object",
      "required": ["agentId", "tool"],
      "properties": {
        "agentId": { "type": "string" },
        "tool": { "type": "string" },
        "output": {},
        "error": { "type": "string" }
      }
    },

    "MessageSendParams": {
      "type": "object",
      "required": ["from", "to", "type", "payload"],
      "properties": {
        "from": { "type": "string" },
        "to": { "type": "string" },
        "type": { "type": "string", "enum": ["signal", "query", "alert", "context", "insight"] },
        "payload": { "type": "object" },
        "correlationId": { "type": "string" }
      }
    },
    "MessageBroadcastParams": {
      "type": "object",
      "required": ["from", "type", "payload"],
      "properties": {
        "from": { "type": "string" },
        "type": { "type": "string" },
        "payload": { "type": "object" }
      }
    }
  }
}
