{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.dharma-ai.io/task/v1",
  "title": "Dharma Task Envelope",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema",
    "taskId",
    "organizationId",
    "workspaceId",
    "taskType",
    "target",
    "skillBundle",
    "instructions",
    "requiredSkills",
    "authority",
    "execution",
    "acceptance",
    "budget",
    "createdAt",
    "expiresAt",
    "nonce"
  ],
  "properties": {
    "schema": {
      "const": "dharma.task/v1"
    },
    "taskId": {
      "type": "string",
      "format": "uuid"
    },
    "organizationId": {
      "type": "string",
      "minLength": 1
    },
    "workspaceId": {
      "type": "string",
      "minLength": 1
    },
    "taskType": {
      "type": "string",
      "enum": [
        "external_request",
        "a2a_handoff",
        "evaluation_retest",
        "remediation_smoke"
      ]
    },
    "target": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "deviceId",
        "provider"
      ],
      "properties": {
        "deviceId": {
          "type": "string",
          "minLength": 1
        },
        "provider": {
          "type": "string",
          "enum": ["codex", "claude", "agy", "hermes"]
        },
        "endpointId": {
          "type": "string",
          "format": "uuid"
        }
      }
    },
    "requiredCapabilities": {
      "type": "array",
      "maxItems": 1,
      "uniqueItems": true,
      "items": {
        "const": "action_decision_receipts_v1"
      }
    },
    "actionDecision": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "actionDigest", "receipt", "signature", "keyVersion"],
      "properties": {
        "id": { "type": "string", "format": "uuid" },
        "actionDigest": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" },
        "receipt": { "$ref": "https://schemas.dharma-ai.io/action-decision-receipt/v1" },
        "signature": { "type": "string", "minLength": 1, "maxLength": 1000 },
        "keyVersion": { "type": "string", "minLength": 1, "maxLength": 500 }
      }
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["taskId", "endpointId"],
      "properties": {
        "taskId": { "type": "string", "format": "uuid" },
        "endpointId": { "type": "string", "format": "uuid" }
      }
    },
    "skillBundle": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["bundleId", "bundleHash"],
          "properties": {
            "bundleId": {
              "type": "string",
              "format": "uuid"
            },
            "bundleHash": {
              "type": "string",
              "pattern": "^sha256:[a-f0-9]{64}$"
            }
          }
        },
        {
          "type": "null"
        }
      ]
    },
    "instructions": {
      "type": "string",
      "minLength": 1,
      "maxLength": 20000
    },
    "requiredSkills": {
      "type": "array",
      "maxItems": 50,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "skillId",
          "version",
          "commit",
          "contentHash"
        ],
        "properties": {
          "skillId": {
            "type": "string",
            "minLength": 1,
            "maxLength": 200,
            "pattern": "^[A-Za-z0-9][A-Za-z0-9._/@-]{0,199}$"
          },
          "version": {
            "type": "string",
            "minLength": 1,
            "maxLength": 100
          },
          "commit": {
            "type": "string",
            "pattern": "^[A-Fa-f0-9]{40,64}$"
          },
          "contentHash": {
            "type": "string",
            "pattern": "^(sha256|blake3):[A-Fa-f0-9]{32,128}$"
          }
        }
      }
    },
    "stateEnvelope": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "intent",
        "evidence_used",
        "known_state",
        "unknown_or_missing_state",
        "allowed_next_actions",
        "blocked_actions",
        "decision_authority",
        "tool_results"
      ],
      "properties": {
        "intent": { "type": "string", "minLength": 1, "maxLength": 1000 },
        "evidence_used": {
          "type": "array",
          "maxItems": 50,
          "items": { "type": "string", "minLength": 1, "maxLength": 500 }
        },
        "known_state": { "type": "object", "maxProperties": 100 },
        "unknown_or_missing_state": {
          "type": "array",
          "maxItems": 50,
          "items": { "type": "string", "minLength": 1, "maxLength": 1000 }
        },
        "allowed_next_actions": {
          "type": "array",
          "maxItems": 50,
          "items": { "type": "string", "minLength": 1, "maxLength": 500 }
        },
        "blocked_actions": {
          "type": "array",
          "maxItems": 50,
          "items": { "type": "string", "minLength": 1, "maxLength": 500 }
        },
        "decision_authority": { "type": "string", "minLength": 1, "maxLength": 1000 },
        "tool_results": { "type": "array", "maxItems": 50 }
      }
    },
    "evidenceReferences": {
      "type": "array",
      "maxItems": 20,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["trajectoryId", "revision", "capsuleHash"],
        "properties": {
          "trajectoryId": { "type": "string", "format": "uuid" },
          "revision": { "type": "integer", "minimum": 1 },
          "capsuleHash": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" }
        }
      }
    },
    "authority": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "readPaths",
        "writePaths",
        "commands",
        "network",
        "git"
      ],
      "properties": {
        "readPaths": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "writePaths": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "commands": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["commandId"],
            "properties": {
              "commandId": {
                "type": "string",
                "minLength": 1,
                "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$"
              }
            }
          }
        },
        "network": {
          "type": "string",
          "enum": [
            "deny",
            "package_registry_only",
            "allowlisted_domains",
            "inherit_local_provider"
          ]
        },
        "git": {
          "type": "string",
          "enum": [
            "read_only",
            "task_branch",
            "merge_allowed",
            "deploy_allowed"
          ]
        },
        "allowlistedDomains": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "default": []
        }
      }
    },
    "execution": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "isolation",
        "timeoutSeconds",
        "leaseSeconds",
        "maximumConcurrentAgents"
      ],
      "properties": {
        "isolation": {
          "const": "git_worktree"
        },
        "timeoutSeconds": {
          "type": "integer",
          "minimum": 1
        },
        "leaseSeconds": {
          "type": "integer",
          "minimum": 1
        },
        "maximumConcurrentAgents": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "acceptance": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "commands",
        "requiredArtifacts"
      ],
      "properties": {
        "commands": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["commandId"],
            "properties": {
              "commandId": {
                "type": "string",
                "minLength": 1,
                "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$"
              }
            }
          }
        },
        "requiredArtifacts": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "externalEffectReceiptCommandId": {
          "type": "string",
          "minLength": 1,
          "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$"
        }
      }
    },
    "budget": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "mode",
        "maximumDharmaCostCents"
      ],
      "properties": {
        "mode": {
          "type": "string",
          "enum": [
            "byok_local",
            "byok_cloud",
            "dharma_managed"
          ]
        },
        "maximumDharmaCostCents": {
          "type": "integer",
          "minimum": 0
        },
        "maximumProviderCostCents": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        }
      }
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "expiresAt": {
      "type": "string",
      "format": "date-time"
    },
    "nonce": {
      "type": "string",
      "minLength": 1
    },
    "signerKeyVersion": {
      "type": "string",
      "minLength": 1,
      "maxLength": 500
    },
    "signature": {
      "type": [
        "string",
        "null"
      ]
    }
  },
  "allOf": [
    {
      "if": {
        "properties": { "taskType": { "const": "a2a_handoff" } },
        "required": ["taskType"]
      },
      "then": {
        "required": ["source", "stateEnvelope", "evidenceReferences"]
      },
      "else": {
        "not": {
          "anyOf": [
            { "required": ["source"] },
            { "required": ["stateEnvelope"] },
            { "required": ["evidenceReferences"] }
          ]
        }
      }
    },
    {
      "if": {
        "properties": {
          "requiredCapabilities": {
            "type": "array",
            "contains": { "const": "action_decision_receipts_v1" }
          }
        },
        "required": ["requiredCapabilities"]
      },
      "then": {
        "required": ["actionDecision"],
        "properties": {
          "target": { "type": "object", "required": ["endpointId"] }
        }
      }
    },
    {
      "if": { "required": ["actionDecision"] },
      "then": {
        "required": ["requiredCapabilities"],
        "properties": {
          "requiredCapabilities": {
            "type": "array",
            "contains": { "const": "action_decision_receipts_v1" }
          }
        }
      }
    }
  ]
}
