{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://tradecat.local/contracts/tradecat-auto-decision-trace-report.schema.json",
  "title": "TradeCat Auto Decision Trace Report",
  "description": "Reconstructs paper/watch decision traces from local service-cycle archive and optional local audit journal metadata.",
  "type": "object",
  "required": [
    "schema",
    "schema_version",
    "ok",
    "trace_count",
    "traces",
    "decision_counts",
    "error_code_counts",
    "source_paths",
    "provenance",
    "safety"
  ],
  "properties": {
    "schema": {"const": "tradecat_auto.decision_trace_report.v1"},
    "schema_version": {"const": "1.0.0"},
    "ok": {"type": "boolean"},
    "generated_at": {"type": "string"},
    "trace_count": {"type": "integer", "minimum": 0},
    "opened_count": {"type": "integer", "minimum": 0},
    "rejected_count": {"type": "integer", "minimum": 0},
    "decision_counts": {"type": "object", "additionalProperties": {"type": "integer", "minimum": 0}},
    "error_code_counts": {"type": "object", "additionalProperties": {"type": "integer", "minimum": 0}},
    "source_paths": {
      "type": "object",
      "required": ["archive_path"],
      "properties": {
        "archive_path": {"type": "string"},
        "journal_path": {"type": "string"},
        "archive_sha256": {"type": "string"}
      },
      "additionalProperties": true
    },
    "audit_journal": {"type": "object", "additionalProperties": true},
    "traces": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "schema",
          "schema_version",
          "ok",
          "trace_id",
          "cycle_index",
          "service_action",
          "decision",
          "error_code",
          "error_codes",
          "provenance",
          "safety"
        ],
        "properties": {
          "schema": {"const": "tradecat_auto.decision_trace.v1"},
          "schema_version": {"const": "1.0.0"},
          "ok": {"type": "boolean"},
          "trace_id": {"type": "string", "minLength": 1},
          "cycle_index": {"type": "integer", "minimum": 0},
          "run_id": {"type": "string"},
          "event_id": {"type": "string"},
          "research_cycle_run_id": {"type": "string"},
          "service_action": {"type": "string"},
          "selected_symbol": {"type": "string"},
          "decision": {"enum": ["OPENED", "REJECTED", "WATCH_ONLY", "SKIPPED", "ERROR", "NO_EXECUTION"]},
          "error_code": {"type": ["string", "null"]},
          "error_codes": {"type": "array", "items": {"type": "string"}},
          "risk_decision": {"type": "string"},
          "risk_reasons": {"type": "array", "items": {"type": "string"}},
          "paper_execution_status": {"type": "string"},
          "paper_execution_id": {"type": "string"},
          "paper_fill_count": {"type": "integer", "minimum": 0},
          "provenance": {"type": "object", "additionalProperties": true},
          "safety": {
            "type": "object",
            "required": ["public_readonly_market_data", "public_readonly", "paper_or_watch_only", "real_orders", "signed_requests", "reads_api_keys", "binance_account_state"],
            "properties": {
              "public_readonly_market_data": {"const": true},
              "public_readonly": {"const": true},
              "paper_or_watch_only": {"const": true},
              "real_orders": {"const": false},
              "signed_requests": {"const": false},
              "reads_api_keys": {"const": false},
              "binance_account_state": {"const": false}
            },
            "additionalProperties": true
          }
        },
        "additionalProperties": true
      }
    },
    "provenance": {"type": "object", "additionalProperties": true},
    "safety": {
      "type": "object",
      "required": ["public_readonly_market_data", "public_readonly", "paper_or_watch_only", "real_orders", "signed_requests", "reads_api_keys", "binance_account_state"],
      "properties": {
        "public_readonly_market_data": {"const": true},
        "public_readonly": {"const": true},
        "paper_or_watch_only": {"const": true},
        "real_orders": {"const": false},
        "signed_requests": {"const": false},
        "reads_api_keys": {"const": false},
        "binance_account_state": {"const": false}
      },
      "additionalProperties": true
    },
    "load_errors": {"type": "array", "items": {"type": "object", "additionalProperties": true}}
  },
  "additionalProperties": true
}
