{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Orchestrator State",
  "description": "Output of init_orchestrator_state.py. Tracks threads, emotional beats, patterns, and position across all 60 episodes. Updated by update_orchestrator_state.py after each batch.",
  "type": "object",
  "required": ["meta", "thread_tracker", "emotional_beat_map", "pattern_state", "position", "cross_batch_flags", "batch_summaries"],
  "properties": {
    "meta": {
      "type": "object",
      "required": ["project", "created", "last_updated", "engine_version"],
      "properties": {
        "project": { "type": "string" },
        "created": { "type": "string" },
        "last_updated": { "type": "string" },
        "engine_version": { "type": "string", "const": "V12" }
      }
    },
    "thread_tracker": {
      "type": "object",
      "description": "Thread state keyed by THREAD_ID (uppercase, underscore-separated).",
      "additionalProperties": {
        "type": "object",
        "required": ["name", "status", "target_plant", "target_payoff"],
        "properties": {
          "name": { "type": "string" },
          "status": { "type": "string", "enum": ["pending", "planted", "advancing", "paid_off"] },
          "planted_episode": { "type": ["integer", "null"] },
          "advanced_episodes": { "type": "array", "items": { "type": "integer" } },
          "payoff_episode": { "type": ["integer", "null"] },
          "target_plant": { "type": ["integer", "null"] },
          "target_advances": { "type": "array", "items": { "type": "integer" } },
          "target_payoff": { "type": "integer" },
          "notes": { "type": "string" }
        }
      }
    },
    "emotional_beat_map": {
      "type": "object",
      "description": "Emotional beat tracking keyed by beat name.",
      "additionalProperties": {
        "type": "object",
        "required": ["beat_name", "target_episode", "tolerance", "status"],
        "properties": {
          "beat_name": { "type": "string" },
          "target_episode": { "type": "integer" },
          "tolerance": { "type": "integer" },
          "status": { "type": "string", "enum": ["pending", "hit", "missed"] },
          "actual_episode": { "type": ["integer", "null"] }
        }
      }
    },
    "pattern_state": {
      "type": "object",
      "required": ["hooks", "cliffhangers"],
      "properties": {
        "hooks": {
          "type": "object",
          "properties": {
            "silent_count": { "type": "integer" },
            "dialogue_count": { "type": "integer" },
            "consecutive_silent": { "type": "integer" },
            "consecutive_dialogue": { "type": "integer" },
            "history": { "type": "array", "items": { "type": "string" } }
          }
        },
        "cliffhangers": {
          "type": "object",
          "properties": {
            "mid_action_count": { "type": "integer" },
            "aftermath_count": { "type": "integer" },
            "consecutive_mid_action": { "type": "integer" },
            "consecutive_aftermath": { "type": "integer" },
            "history": { "type": "array", "items": { "type": "string" } }
          }
        }
      }
    },
    "position": {
      "type": "object",
      "required": ["last_completed_batch", "last_completed_episode", "next_batch"],
      "properties": {
        "last_completed_batch": { "type": "integer" },
        "last_completed_episode": { "type": "integer" },
        "next_batch": { "type": "integer" }
      }
    },
    "cross_batch_flags": {
      "type": "object",
      "properties": {
        "voice_concerns": { "type": "array", "items": { "type": "string" } },
        "continuity_breaks": { "type": "array", "items": { "type": "string" } },
        "overdue_threads": { "type": "array", "items": { "type": "string" } },
        "pattern_violations": { "type": "array", "items": { "type": "string" } }
      }
    },
    "batch_summaries": {
      "type": "object",
      "description": "Keyed by batch number (as string). Stores condensed batch summary after each batch.",
      "additionalProperties": { "type": "object" }
    },
    "goal_backward_checkpoints": {
      "type": "object",
      "description": "Goal-backward verification data at structural checkpoints.",
      "additionalProperties": { "type": "object" }
    }
  }
}
