{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://driftframe.studio/schemas/a3-stacked-cube-state-v0.1.json",
  "title": "A3 Stacked Cube Canonical State Model",
  "description": "Validation contract for the v0.1 authority, evidence, recovery, and presentation state that drives the A3 visual system.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "artifact_identity",
    "source_revision",
    "semantic_contract",
    "storyboard_frames",
    "authority_states",
    "gates",
    "capability_cubes",
    "human_workflows",
    "typed_connectors",
    "view_state",
    "semantic_locality",
    "recovery_calibration"
  ],
  "properties": {
    "schema_version": { "const": "0.1.0" },
    "artifact_identity": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "display_name", "subtitle", "version", "family", "placement", "primary_cognitive_move", "visual_preset", "source_band"],
      "properties": {
        "id": { "const": "A3-CUBE-MANIFEST" },
        "display_name": { "$ref": "#/$defs/non_empty_string" },
        "subtitle": { "$ref": "#/$defs/non_empty_string" },
        "version": { "const": "0.1.0" },
        "family": { "$ref": "#/$defs/non_empty_string" },
        "placement": { "$ref": "#/$defs/non_empty_string" },
        "primary_cognitive_move": { "$ref": "#/$defs/non_empty_string" },
        "visual_preset": { "$ref": "#/$defs/non_empty_string" },
        "source_band": { "$ref": "#/$defs/non_empty_string_set" }
      }
    },
    "source_revision": {
      "type": "object",
      "additionalProperties": false,
      "required": ["a3_page_id", "a3_page_version", "a3_page_url", "repository", "repository_commit", "repository_path", "verified_on"],
      "properties": {
        "source_key": {
          "const": "SRC-A3",
          "description": "Operator-blind source identity for locator-safe projections. Optional in v0.1 so earlier instances retain validation compatibility."
        },
        "a3_page_id": {
          "type": "string",
          "description": "Legacy v0.1 wire field. Locator-safe projections carry the operator-blind source key; earlier instances retain their opaque page-identifier shape.",
          "anyOf": [
            {
              "pattern": "^SRC-[A-Z0-9-]+$",
              "description": "Stable source key used by locator-safe projections."
            },
            {
              "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
              "description": "Legacy hyphenated opaque page-identifier shape retained for v0.1 validation compatibility.",
              "deprecated": true
            },
            {
              "pattern": "^[0-9a-f]{32}$",
              "description": "Legacy compact opaque page-identifier shape retained for v0.1 validation compatibility.",
              "deprecated": true
            }
          ]
        },
        "a3_page_version": { "$ref": "#/$defs/non_empty_string" },
        "a3_page_url": {
          "type": "string",
          "format": "uri",
          "description": "Legacy v0.1 field name carrying a public source URI or stable source-key URN."
        },
        "repository": { "const": "urn:source-key:prototype-exemplars" },
        "repository_commit": { "type": "string", "pattern": "^[0-9a-f]{40}$" },
        "repository_path": { "$ref": "#/$defs/non_empty_string" },
        "verified_on": { "type": "string", "format": "date" }
      }
    },
    "semantic_contract": {
      "type": "object",
      "additionalProperties": false,
      "required": ["cube_means", "position_means", "migration_means", "coupling_rule", "human_work_rule", "unknown_rule", "historical_anchor", "color_rule"],
      "properties": {
        "cube_means": { "$ref": "#/$defs/non_empty_string" },
        "position_means": { "$ref": "#/$defs/non_empty_string" },
        "migration_means": { "$ref": "#/$defs/non_empty_string" },
        "coupling_rule": { "$ref": "#/$defs/non_empty_string" },
        "human_work_rule": { "$ref": "#/$defs/non_empty_string" },
        "unknown_rule": { "$ref": "#/$defs/non_empty_string" },
        "historical_anchor": { "$ref": "#/$defs/non_empty_string" },
        "color_rule": { "$ref": "#/$defs/non_empty_string" }
      }
    },
    "storyboard_frames": {
      "type": "array",
      "prefixItems": [
        { "allOf": [{ "$ref": "#/$defs/frame" }, { "properties": { "id": { "const": "F0" }, "sequence": { "const": 0 } } }] },
        { "allOf": [{ "$ref": "#/$defs/frame" }, { "properties": { "id": { "const": "F1" }, "sequence": { "const": 1 } } }] },
        { "allOf": [{ "$ref": "#/$defs/frame" }, { "properties": { "id": { "const": "F2" }, "sequence": { "const": 2 } } }] },
        { "allOf": [{ "$ref": "#/$defs/frame" }, { "properties": { "id": { "const": "F3" }, "sequence": { "const": 3 } } }] }
      ],
      "items": false,
      "minItems": 4
    },
    "authority_states": {
      "type": "array",
      "prefixItems": [
        { "allOf": [{ "$ref": "#/$defs/authority_state" }, { "properties": { "id": { "const": "legacy" } } }] },
        { "allOf": [{ "$ref": "#/$defs/authority_state" }, { "properties": { "id": { "const": "seam" } } }] },
        { "allOf": [{ "$ref": "#/$defs/authority_state" }, { "properties": { "id": { "const": "shadow" } } }] },
        { "allOf": [{ "$ref": "#/$defs/authority_state" }, { "properties": { "id": { "const": "new" } } }] },
        { "allOf": [{ "$ref": "#/$defs/authority_state" }, { "properties": { "id": { "const": "retained_legacy" } } }] },
        { "allOf": [{ "$ref": "#/$defs/authority_state" }, { "properties": { "id": { "const": "human" } } }] },
        { "allOf": [{ "$ref": "#/$defs/authority_state" }, { "properties": { "id": { "const": "unknown" } } }] }
      ],
      "items": false,
      "minItems": 7
    },
    "gates": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/gate" }
    },
    "capability_cubes": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/capability" }
    },
    "human_workflows": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/human_workflow" }
    },
    "typed_connectors": {
      "type": "array",
      "items": { "$ref": "#/$defs/connector" }
    },
    "view_state": {
      "type": "object",
      "additionalProperties": false,
      "required": ["active_view_id", "views"],
      "properties": {
        "active_view_id": { "$ref": "#/$defs/stable_id" },
        "views": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/view" }
        }
      }
    },
    "semantic_locality": {
      "type": "object",
      "additionalProperties": false,
      "required": ["preferred_phrasing", "replacement_state_guidance"],
      "properties": {
        "preferred_phrasing": { "$ref": "#/$defs/non_empty_string_set" },
        "replacement_state_guidance": { "$ref": "#/$defs/non_empty_string_set" }
      }
    },
    "recovery_calibration": {
      "type": "object",
      "additionalProperties": false,
      "required": ["strain_cues", "stabilizing_read", "restored_state", "recovery_moves", "named_failure_geometries"],
      "properties": {
        "strain_cues": { "$ref": "#/$defs/non_empty_string_set" },
        "stabilizing_read": { "$ref": "#/$defs/non_empty_string" },
        "restored_state": { "$ref": "#/$defs/non_empty_string" },
        "recovery_moves": { "$ref": "#/$defs/non_empty_string_set" },
        "named_failure_geometries": { "$ref": "#/$defs/non_empty_string_set" }
      }
    }
  },
  "$defs": {
    "non_empty_string": { "type": "string", "minLength": 1 },
    "stable_id": { "type": "string", "pattern": "^[A-Za-z][A-Za-z0-9_-]*$" },
    "non_empty_string_set": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/non_empty_string" }
    },
    "frame_id": { "enum": ["F0", "F1", "F2", "F3"] },
    "authority_id": { "enum": ["legacy", "seam", "shadow", "new", "retained_legacy", "human", "unknown"] },
    "frame": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "sequence", "label", "short_label", "annotation", "cognitive_move"],
      "properties": {
        "id": { "$ref": "#/$defs/frame_id" },
        "sequence": { "type": "integer", "minimum": 0, "maximum": 3 },
        "label": { "$ref": "#/$defs/non_empty_string" },
        "short_label": { "$ref": "#/$defs/non_empty_string" },
        "annotation": { "$ref": "#/$defs/non_empty_string" },
        "cognitive_move": { "$ref": "#/$defs/non_empty_string" }
      }
    },
    "authority_state": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "label", "visual"],
      "properties": {
        "id": { "$ref": "#/$defs/authority_id" },
        "label": { "$ref": "#/$defs/non_empty_string" },
        "visual": { "$ref": "#/$defs/non_empty_string" }
      }
    },
    "gate": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "label", "status", "criteria", "protected_behavior_ids", "review_trigger"],
      "properties": {
        "id": { "type": "string", "pattern": "^GATE-[A-Z0-9-]+$" },
        "label": { "$ref": "#/$defs/non_empty_string" },
        "status": { "$ref": "#/$defs/non_empty_string" },
        "criteria": { "$ref": "#/$defs/non_empty_string_set" },
        "protected_behavior_ids": { "$ref": "#/$defs/non_empty_string_set" },
        "review_trigger": { "$ref": "#/$defs/non_empty_string" }
      }
    },
    "frame_states": {
      "type": "object",
      "additionalProperties": false,
      "required": ["F0", "F1", "F2", "F3"],
      "properties": {
        "F0": { "$ref": "#/$defs/authority_id" },
        "F1": { "$ref": "#/$defs/authority_id" },
        "F2": { "$ref": "#/$defs/authority_id" },
        "F3": { "$ref": "#/$defs/authority_id" }
      }
    },
    "capability": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "label", "short_label", "kind", "family", "canonical_order", "system_of_record", "coupling", "evidence_status", "migration_state", "gate_id", "rollback_route", "regulatory_risk", "source_refs", "frame_states"],
      "properties": {
        "id": { "type": "string", "pattern": "^(CAP|SVC)-[A-Z0-9-]+$" },
        "label": { "$ref": "#/$defs/non_empty_string" },
        "short_label": { "$ref": "#/$defs/non_empty_string" },
        "kind": { "enum": ["software_capability", "service_dependency", "unknown"] },
        "family": { "$ref": "#/$defs/non_empty_string" },
        "canonical_order": { "type": "integer", "minimum": 1 },
        "system_of_record": { "$ref": "#/$defs/non_empty_string" },
        "coupling": { "$ref": "#/$defs/non_empty_string" },
        "evidence_status": { "$ref": "#/$defs/non_empty_string" },
        "migration_state": { "$ref": "#/$defs/non_empty_string" },
        "gate_id": { "anyOf": [{ "type": "string", "pattern": "^GATE-[A-Z0-9-]+$" }, { "type": "null" }] },
        "rollback_route": { "$ref": "#/$defs/non_empty_string" },
        "regulatory_risk": { "$ref": "#/$defs/non_empty_string" },
        "source_refs": { "$ref": "#/$defs/non_empty_string_set" },
        "frame_states": { "$ref": "#/$defs/frame_states" }
      }
    },
    "human_workflow": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "label", "canonical_order", "evidence_status"],
      "properties": {
        "id": { "type": "string", "pattern": "^HUM-[A-Z0-9-]+$" },
        "label": { "$ref": "#/$defs/non_empty_string" },
        "canonical_order": { "type": "integer", "minimum": 1 },
        "evidence_status": { "$ref": "#/$defs/non_empty_string" }
      }
    },
    "connector": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "from", "to", "type", "label", "status"],
      "properties": {
        "id": { "type": "string", "pattern": "^CON-[A-Z0-9-]+$" },
        "from": { "$ref": "#/$defs/non_empty_string" },
        "to": { "$ref": "#/$defs/non_empty_string" },
        "type": { "enum": ["depends_on", "mirrors", "routes", "rolls_back"] },
        "label": { "$ref": "#/$defs/non_empty_string" },
        "status": { "$ref": "#/$defs/non_empty_string" }
      }
    },
    "view": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "name", "active_frame_id", "cube_order", "hidden_cube_ids", "selected_cube_id", "text_mode"],
      "properties": {
        "id": { "$ref": "#/$defs/stable_id" },
        "name": { "$ref": "#/$defs/non_empty_string" },
        "active_frame_id": { "$ref": "#/$defs/frame_id" },
        "cube_order": { "type": "array", "uniqueItems": true, "items": { "type": "string", "pattern": "^(CAP|SVC)-[A-Z0-9-]+$" } },
        "hidden_cube_ids": { "type": "array", "uniqueItems": true, "items": { "type": "string", "pattern": "^(CAP|SVC)-[A-Z0-9-]+$" } },
        "selected_cube_id": { "type": "string", "pattern": "^(CAP|SVC)-[A-Z0-9-]+$" },
        "text_mode": { "$ref": "#/$defs/non_empty_string" }
      }
    }
  }
}
