{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://dashfin.org/schemas/df-clinical-agent-trace-v0.1.schema.json",
  "title": "DashFin Clinical Agent Trace v0.1",
  "description": "Replayable, hash-chained evidence trace for a synthetic clinical-agent evaluation.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "trace_schema",
    "trace_id",
    "envelope_id",
    "envelope_version",
    "scenario_id",
    "variant",
    "record_class",
    "synthetic_clock",
    "target",
    "events",
    "evaluation",
    "provenance",
    "trace_sha256"
  ],
  "properties": {
    "trace_schema": {
      "const": "DF-CLINICAL-AGENT-TRACE-v0.1"
    },
    "trace_id": {
      "type": "string",
      "pattern": "^DF-SYN-TRACE-[A-Z0-9-]+$"
    },
    "envelope_id": {
      "type": "string",
      "pattern": "^DF-CAE-[A-Z0-9-]+$"
    },
    "envelope_version": {
      "type": "string"
    },
    "scenario_id": {
      "type": "string",
      "pattern": "^DF-SYN-SCN-[0-9]{3}$"
    },
    "variant": {
      "enum": [
        "COMPLIANT_CONTROL",
        "DELIBERATELY_UNSAFE"
      ]
    },
    "record_class": {
      "const": "SYNTHETIC_TEST"
    },
    "synthetic_clock": {
      "const": true
    },
    "target": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "target_id",
        "declared_version"
      ],
      "properties": {
        "target_id": {
          "type": "string"
        },
        "declared_version": {
          "type": "string"
        }
      }
    },
    "events": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/event"
      }
    },
    "evaluation": {
      "$ref": "#/$defs/evaluation"
    },
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "generator",
        "generator_version",
        "deterministic_seed",
        "evaluator",
        "contains_real_person_data",
        "external_side_effects"
      ],
      "properties": {
        "generator": {
          "const": "df-synthetic-agent-workflow"
        },
        "generator_version": {
          "type": "string"
        },
        "deterministic_seed": {
          "type": "integer"
        },
        "evaluator": {
          "const": "deterministic-envelope-checker"
        },
        "contains_real_person_data": {
          "const": false
        },
        "external_side_effects": {
          "const": false
        }
      }
    },
    "trace_sha256": {
      "type": "string",
      "pattern": "^[a-f0-9]{64}$"
    }
  },
  "$defs": {
    "event": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "ordinal",
        "timestamp",
        "actor",
        "event_type",
        "summary",
        "payload",
        "state_before",
        "state_after",
        "previous_event_sha256",
        "event_sha256"
      ],
      "properties": {
        "ordinal": {
          "type": "integer",
          "minimum": 1
        },
        "timestamp": {
          "type": "string",
          "format": "date-time"
        },
        "actor": {
          "enum": [
            "SYSTEM",
            "SYNTHETIC_USER",
            "AGENT",
            "TOOL",
            "EVALUATOR"
          ]
        },
        "event_type": {
          "enum": [
            "SCENARIO_START",
            "MESSAGE",
            "TOOL_CALL",
            "TOOL_RESULT",
            "ACTION_PROPOSED",
            "ACTION_EXECUTED",
            "HUMAN_HANDOFF",
            "CASE_CLOSED",
            "INVARIANT_CHECK"
          ]
        },
        "summary": {
          "type": "string",
          "minLength": 1
        },
        "payload": {
          "type": "object"
        },
        "state_before": {
          "type": "string"
        },
        "state_after": {
          "type": "string"
        },
        "previous_event_sha256": {
          "type": [
            "string",
            "null"
          ],
          "pattern": "^[a-f0-9]{64}$"
        },
        "event_sha256": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        }
      }
    },
    "evaluation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "outcome",
        "checked_invariants",
        "findings"
      ],
      "allOf": [
        {
          "if": {
            "properties": {
              "outcome": {
                "const": "FAIL"
              }
            },
            "required": [
              "outcome"
            ]
          },
          "then": {
            "properties": {
              "findings": {
                "minItems": 1
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "outcome": {
                "const": "PASS"
              }
            },
            "required": [
              "outcome"
            ]
          },
          "then": {
            "properties": {
              "findings": {
                "maxItems": 0
              }
            }
          }
        }
      ],
      "properties": {
        "outcome": {
          "enum": [
            "PASS",
            "FAIL"
          ]
        },
        "checked_invariants": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^DF-INV-[A-Z0-9-]+$"
          }
        },
        "findings": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/finding"
          }
        }
      }
    },
    "finding": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "finding_id",
        "invariant_id",
        "severity",
        "title",
        "observed_behaviour",
        "required_behaviour",
        "evidence_events",
        "remediation_target",
        "retest_status"
      ],
      "properties": {
        "finding_id": {
          "type": "string",
          "pattern": "^DF-SYN-F-[A-Z0-9-]+$"
        },
        "invariant_id": {
          "type": "string",
          "pattern": "^DF-INV-[A-Z0-9-]+$"
        },
        "severity": {
          "enum": [
            "LOW",
            "MEDIUM",
            "HIGH",
            "CRITICAL"
          ]
        },
        "title": {
          "type": "string",
          "minLength": 1
        },
        "observed_behaviour": {
          "type": "string",
          "minLength": 1
        },
        "required_behaviour": {
          "type": "string",
          "minLength": 1
        },
        "evidence_events": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "integer",
            "minimum": 1
          }
        },
        "remediation_target": {
          "type": "string",
          "minLength": 1
        },
        "retest_status": {
          "const": "REQUIRED"
        }
      }
    }
  }
}
