{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://heartcortex.ai/schemas/1.0/trial.schema.json",
  "title": "trial",
  "description": "A Proposal being tried, watched and kept or undone (Core Standard, Section 7.5). Created when the Proposal is accepted. Its final state is the Outcome.",
  "type": "object",
  "required": [
    "id",
    "target",
    "state",
    "createdAt"
  ],
  "properties": {
    "id": {
      "$ref": "common.schema.json#/$defs/uuid",
      "x-heart-privacy": "internal"
    },
    "proposalId": {
      "$ref": "common.schema.json#/$defs/uuid",
      "x-heart-privacy": "internal",
      "description": "The Proposal it came from. Absent when a Playbook ran the fix (ruleId)."
    },
    "ruleId": {
      "description": "Set when a Playbook ran this fix as a reflex.",
      "type": "string",
      "x-heart-privacy": "internal"
    },
    "target": {
      "type": "string",
      "x-heart-privacy": "internal"
    },
    "state": {
      "$ref": "#/$defs/state",
      "x-heart-privacy": "internal"
    },
    "approvedBy": {
      "description": "Who approved the first Action, and so the whole plan and its undo (Section 7.5).",
      "type": "object",
      "required": [
        "kind"
      ],
      "additionalProperties": false,
      "properties": {
        "kind": {
          "enum": [
            "human",
            "allowlist",
            "rule"
          ]
        },
        "actionId": {
          "description": "The first Action. Later steps and the undo point to it.",
          "$ref": "common.schema.json#/$defs/uuid"
        },
        "rule": {
          "description": "The Playbook, when kind is rule.",
          "type": "string"
        },
        "person": {
          "description": "The person, when kind is human.",
          "type": "string",
          "x-heart-privacy": "private"
        }
      },
      "x-heart-privacy": "internal"
    },
    "previous": {
      "description": "The values the Core read before the first step: what the undo restores. Covered by the first Action's digest.",
      "type": "object",
      "x-heart-privacy": "internal"
    },
    "steps": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "at",
          "arguments",
          "guard"
        ],
        "properties": {
          "at": {
            "$ref": "common.schema.json#/$defs/timestamp"
          },
          "arguments": {
            "type": "object"
          },
          "guard": {
            "enum": [
              "passed",
              "failed"
            ]
          },
          "checks": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "check",
                "held"
              ],
              "properties": {
                "check": {
                  "$ref": "common.schema.json#/$defs/check"
                },
                "observed": {
                  "type": "number"
                },
                "held": {
                  "type": "boolean"
                }
              }
            }
          }
        }
      },
      "x-heart-privacy": "internal"
    },
    "before": {
      "type": "array",
      "items": {
        "$ref": "common.schema.json#/$defs/feature"
      },
      "x-heart-privacy": "internal",
      "description": "The same features as the Consultation, for the same window length."
    },
    "after": {
      "type": "array",
      "items": {
        "$ref": "common.schema.json#/$defs/feature"
      },
      "x-heart-privacy": "internal",
      "description": "The same features as before, for the same window length, ending when the Trial ended."
    },
    "reason": {
      "description": "Why it ended, in plain language.",
      "type": "string",
      "x-heart-privacy": "internal"
    },
    "startedAt": {
      "$ref": "common.schema.json#/$defs/timestamp",
      "x-heart-privacy": "internal",
      "description": "When the first step was applied."
    },
    "endedAt": {
      "$ref": "common.schema.json#/$defs/timestamp",
      "x-heart-privacy": "internal"
    },
    "ruleProposed": {
      "description": "The learned Playbook proposed after a kept Trial.",
      "type": "string",
      "x-heart-privacy": "internal"
    },
    "ext": {
      "$ref": "common.schema.json#/$defs/ext",
      "x-heart-privacy": "private"
    },
    "plan": {
      "description": "Every step's arguments, in order. Covered by the first Action's digest.",
      "type": "array",
      "items": {
        "type": "object"
      },
      "x-heart-privacy": "internal"
    },
    "createdAt": {
      "$ref": "common.schema.json#/$defs/timestamp",
      "x-heart-privacy": "internal"
    }
  },
  "$defs": {
    "state": {
      "description": "awaiting-approval: accepted, the first Action waits for a person. running: steps are applied and watched. undoing: the stored values are being restored. kept: every expect held and the undo watch passed. undone: the stored values were restored after undoIf, a failed expect, a failed step or a restart. refused: nothing was applied (the first Action was denied, expired or failed, or a guard refused it). stopped: stop was pressed and the undo, if anything was applied, is done. undo-failed: the undo could not run, so the system is left changed; a critical alert was raised.",
      "enum": [
        "awaiting-approval",
        "running",
        "undoing",
        "kept",
        "undone",
        "refused",
        "stopped",
        "undo-failed"
      ]
    },
    "finalState": {
      "enum": [
        "kept",
        "undone",
        "refused",
        "stopped",
        "undo-failed"
      ]
    }
  },
  "oneOf": [
    {
      "required": [
        "proposalId"
      ],
      "not": {
        "required": [
          "ruleId"
        ]
      }
    },
    {
      "required": [
        "ruleId"
      ],
      "not": {
        "required": [
          "proposalId"
        ]
      }
    }
  ]
}
