{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://dashfin.org/schemas/df-clinical-action-envelope-v0.1.schema.json",
  "title": "DashFin Clinical Action Envelope v0.1",
  "description": "Experimental, synthetic-only contract for declaring the permitted and required behaviour of a tool-using clinical AI workflow.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "protocol_id",
    "envelope_id",
    "version",
    "status",
    "workflow",
    "target",
    "data_policy",
    "tools",
    "safety_invariants",
    "failure_injections",
    "evidence_contract",
    "boundaries"
  ],
  "properties": {
    "protocol_id": {
      "const": "DF-CAE-v0.1"
    },
    "envelope_id": {
      "type": "string",
      "pattern": "^DF-CAE-[A-Z0-9-]+$"
    },
    "version": {
      "type": "string",
      "pattern": "^0\\.[0-9]+\\.[0-9]+$"
    },
    "status": {
      "enum": [
        "EXPERIMENTAL",
        "RATIFIED",
        "RETIRED"
      ]
    },
    "workflow": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "workflow_id",
        "name",
        "description",
        "intended_user",
        "jurisdiction",
        "record_class"
      ],
      "properties": {
        "workflow_id": {
          "type": "string",
          "pattern": "^DF-SYN-WF-[A-Z0-9-]+$"
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "description": {
          "type": "string",
          "minLength": 1
        },
        "intended_user": {
          "type": "string",
          "minLength": 1
        },
        "jurisdiction": {
          "const": "UK"
        },
        "record_class": {
          "const": "SYNTHETIC_TEST"
        }
      }
    },
    "target": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "target_id",
        "declared_version",
        "autonomy_tier",
        "human_acceptance_authority"
      ],
      "properties": {
        "target_id": {
          "type": "string",
          "minLength": 1
        },
        "declared_version": {
          "type": "string",
          "minLength": 1
        },
        "autonomy_tier": {
          "enum": [
            "ADVISORY_ONLY",
            "PROPOSES_ACTIONS",
            "EXECUTES_REVERSIBLE_ACTIONS",
            "EXECUTES_CONSEQUENTIAL_ACTIONS"
          ]
        },
        "human_acceptance_authority": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "data_policy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "synthetic_only",
        "identifiable_patient_data_permitted",
        "cross_session_access_permitted",
        "external_side_effects_permitted"
      ],
      "properties": {
        "synthetic_only": {
          "const": true
        },
        "identifiable_patient_data_permitted": {
          "const": false
        },
        "cross_session_access_permitted": {
          "const": false
        },
        "external_side_effects_permitted": {
          "const": false
        }
      }
    },
    "tools": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/tool"
      }
    },
    "safety_invariants": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/invariant"
      }
    },
    "failure_injections": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/failureInjection"
      }
    },
    "evidence_contract": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "capture_exact_messages",
        "capture_tool_arguments",
        "capture_tool_results",
        "capture_state_transitions",
        "sha256_chain_required",
        "original_findings_immutable_on_retest"
      ],
      "properties": {
        "capture_exact_messages": {
          "const": true
        },
        "capture_tool_arguments": {
          "const": true
        },
        "capture_tool_results": {
          "const": true
        },
        "capture_state_transitions": {
          "const": true
        },
        "sha256_chain_required": {
          "const": true
        },
        "original_findings_immutable_on_retest": {
          "const": true
        }
      }
    },
    "boundaries": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "simulation_only",
        "not_clinical_care",
        "not_regulatory_certification",
        "not_dcb_signoff",
        "client_retains_deployment_authority"
      ],
      "properties": {
        "simulation_only": {
          "const": true
        },
        "not_clinical_care": {
          "const": true
        },
        "not_regulatory_certification": {
          "const": true
        },
        "not_dcb_signoff": {
          "const": true
        },
        "client_retains_deployment_authority": {
          "const": true
        }
      }
    }
  },
  "$defs": {
    "tool": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "tool_id",
        "purpose",
        "access",
        "effect",
        "approval",
        "resource_scope",
        "maximum_calls_per_trace"
      ],
      "properties": {
        "tool_id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]+$"
        },
        "purpose": {
          "type": "string",
          "minLength": 1
        },
        "access": {
          "enum": [
            "READ",
            "WRITE"
          ]
        },
        "effect": {
          "enum": [
            "NONE",
            "REVERSIBLE",
            "CONSEQUENTIAL"
          ]
        },
        "approval": {
          "enum": [
            "NOT_REQUIRED",
            "HUMAN_CONFIRMATION"
          ]
        },
        "resource_scope": {
          "type": "string",
          "minLength": 1
        },
        "maximum_calls_per_trace": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "invariant": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "invariant_id",
        "category",
        "rule",
        "severity_on_breach",
        "required_behaviour"
      ],
      "properties": {
        "invariant_id": {
          "type": "string",
          "pattern": "^DF-INV-[A-Z0-9-]+$"
        },
        "category": {
          "enum": [
            "ESCALATION",
            "IDENTITY_AND_SCOPE",
            "INSTRUCTION_INTEGRITY",
            "TOOL_FAILURE",
            "IDEMPOTENCY",
            "HUMAN_AUTHORITY"
          ]
        },
        "rule": {
          "type": "string",
          "minLength": 1
        },
        "severity_on_breach": {
          "enum": [
            "LOW",
            "MEDIUM",
            "HIGH",
            "CRITICAL"
          ]
        },
        "required_behaviour": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "failureInjection": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "injection_id",
        "category",
        "description"
      ],
      "properties": {
        "injection_id": {
          "type": "string",
          "pattern": "^DF-INJ-[A-Z0-9-]+$"
        },
        "category": {
          "enum": [
            "CLINICAL_DETERIORATION",
            "WRONG_RECORD",
            "INDIRECT_PROMPT_INJECTION",
            "DEPENDENCY_OUTAGE",
            "AMBIGUOUS_WRITE_RESULT",
            "MISSING_HUMAN_APPROVAL"
          ]
        },
        "description": {
          "type": "string",
          "minLength": 1
        }
      }
    }
  }
}
