{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "OrchestratorState",
  "description": "Lightweight state maintained by the orchestrator agent across all batches. Contains summaries only, not full episode text.",
  "type": "object",
  "required": ["meta", "thread_tracker", "emotional_beat_map", "pattern_state", "position", "cross_batch_flags"],
  "properties": {
    "meta": {
      "type": "object",
      "required": ["project", "created", "last_updated", "engine_version"],
      "properties": {
        "project": {
          "type": "string",
          "description": "Project folder name"
        },
        "created": {
          "type": "string",
          "format": "date-time",
          "description": "When orchestrator state was initialized"
        },
        "last_updated": {
          "type": "string",
          "format": "date-time",
          "description": "Last update timestamp"
        },
        "engine_version": {
          "type": "string",
          "description": "Engine version (e.g., V12)"
        }
      }
    },
    "thread_tracker": {
      "type": "object",
      "description": "Tracks narrative threads from treatment.md THREAD INDEX",
      "additionalProperties": {
        "type": "object",
        "required": ["name", "status", "planted_episode", "target_payoff"],
        "properties": {
          "name": {
            "type": "string",
            "description": "Thread name (e.g., BLUE_MEDICINE)"
          },
          "status": {
            "type": "string",
            "enum": ["pending", "planted", "advancing", "paid_off"],
            "description": "Current thread status"
          },
          "planted_episode": {
            "type": ["integer", "null"],
            "description": "Episode where thread was planted"
          },
          "advanced_episodes": {
            "type": "array",
            "items": {"type": "integer"},
            "description": "Episodes where thread was advanced"
          },
          "payoff_episode": {
            "type": ["integer", "null"],
            "description": "Episode where thread paid off"
          },
          "target_payoff": {
            "type": "integer",
            "description": "Target episode for payoff from treatment"
          },
          "notes": {
            "type": "string",
            "description": "Optional notes about thread progress"
          }
        }
      }
    },
    "emotional_beat_map": {
      "type": "object",
      "description": "Scheduled emotional beats from CONSTANTS.md",
      "additionalProperties": {
        "type": "object",
        "required": ["beat_name", "target_episode", "status"],
        "properties": {
          "beat_name": {
            "type": "string",
            "description": "Beat name (e.g., FIRST_CRACK, THRESHOLD)"
          },
          "target_episode": {
            "type": "integer",
            "description": "Target episode for this beat"
          },
          "tolerance": {
            "type": "integer",
            "default": 2,
            "description": "Allowed episode variance (default +-2)"
          },
          "status": {
            "type": "string",
            "enum": ["pending", "hit", "missed"],
            "description": "Whether beat has been hit"
          },
          "actual_episode": {
            "type": ["integer", "null"],
            "description": "Episode where beat actually occurred"
          }
        }
      }
    },
    "pattern_state": {
      "type": "object",
      "description": "Running hook/cliffhanger distribution and consecutive counts",
      "required": ["hooks", "cliffhangers"],
      "properties": {
        "hooks": {
          "type": "object",
          "required": ["silent_count", "dialogue_count", "consecutive_silent", "consecutive_dialogue", "history"],
          "properties": {
            "silent_count": {"type": "integer"},
            "dialogue_count": {"type": "integer"},
            "consecutive_silent": {"type": "integer"},
            "consecutive_dialogue": {"type": "integer"},
            "history": {
              "type": "array",
              "items": {"type": "string", "enum": ["silent", "dialogue"]},
              "description": "Recent hook types for pattern tracking"
            }
          }
        },
        "cliffhangers": {
          "type": "object",
          "required": ["mid_action_count", "aftermath_count", "consecutive_mid_action", "consecutive_aftermath", "history"],
          "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", "enum": ["mid-action", "aftermath"]},
              "description": "Recent cliffhanger types for pattern tracking"
            }
          }
        }
      }
    },
    "position": {
      "type": "object",
      "description": "Current generation position",
      "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",
      "description": "Issues detected across batches requiring attention",
      "properties": {
        "voice_concerns": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "batch": {"type": "integer"},
              "character": {"type": "string"},
              "issue": {"type": "string"},
              "severity": {"type": "string", "enum": ["warning", "critical"]}
            }
          }
        },
        "continuity_breaks": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "batch": {"type": "integer"},
              "episode": {"type": "integer"},
              "description": {"type": "string"}
            }
          }
        },
        "overdue_threads": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "thread_id": {"type": "string"},
              "expected_by": {"type": "integer"},
              "current_episode": {"type": "integer"}
            }
          }
        },
        "pattern_violations": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "batch": {"type": "integer"},
              "type": {"type": "string", "enum": ["hook", "cliffhanger"]},
              "consecutive_count": {"type": "integer"},
              "pattern": {"type": "string"}
            }
          }
        }
      }
    },
    "batch_summaries": {
      "type": "object",
      "description": "Summary data from each completed batch",
      "additionalProperties": {
        "$ref": "#/definitions/batch_summary_ref"
      }
    },
    "goal_backward_checkpoints": {
      "type": "object",
      "description": "Goal-backward verification results at checkpoints (batches 3, 6, 9, 12)",
      "properties": {
        "batch_3": {"$ref": "#/definitions/goal_checkpoint"},
        "batch_6": {"$ref": "#/definitions/goal_checkpoint"},
        "batch_9": {"$ref": "#/definitions/goal_checkpoint"},
        "batch_12": {"$ref": "#/definitions/goal_checkpoint"}
      }
    }
  },
  "definitions": {
    "batch_summary_ref": {
      "type": "object",
      "properties": {
        "batch": {"type": "integer"},
        "episodes": {"type": "string"},
        "threads_planted": {"type": "array", "items": {"type": "string"}},
        "threads_advanced": {"type": "array", "items": {"type": "string"}},
        "threads_paid_off": {"type": "array", "items": {"type": "string"}},
        "emotional_beats_hit": {"type": "array", "items": {"type": "string"}},
        "hook_distribution": {
          "type": "object",
          "properties": {
            "silent": {"type": "integer"},
            "dialogue": {"type": "integer"}
          }
        },
        "cliffhanger_distribution": {
          "type": "object",
          "properties": {
            "mid_action": {"type": "integer"},
            "aftermath": {"type": "integer"}
          }
        }
      }
    },
    "goal_checkpoint": {
      "type": "object",
      "properties": {
        "episode": {"type": "integer"},
        "threads_resolved_expected": {"type": "string"},
        "threads_resolved_actual": {"type": "integer"},
        "arc_progress_expected": {"type": "string"},
        "arc_progress_actual": {"type": "string"},
        "emotional_beats_expected": {"type": "string"},
        "emotional_beats_actual": {"type": "integer"},
        "status": {"type": "string", "enum": ["on_track", "behind", "ahead"]},
        "course_corrections": {"type": "array", "items": {"type": "string"}}
      }
    }
  }
}
