{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "bmad-plus/ghostwriter/run-manifest/1",
  "title": "Prepared Ghostwriter editorial run",
  "description": "The controller records an immutable selection. Preparing a manifest executes no editor and establishes no quality or factual verdict.",
  "type": "object",
  "required": [
    "schema_version",
    "run_id",
    "workflow",
    "locale",
    "content_type",
    "input_artifacts",
    "source_artifacts",
    "brief",
    "source_set_sha256",
    "voice_profile",
    "policy",
    "limits",
    "resource_artifacts",
    "execution_requirements",
    "editable_scope",
    "expected_outputs",
    "request"
  ],
  "properties": {
    "schema_version": { "const": 1 },
    "run_id": {
      "type": "string",
      "pattern": "^gw-[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
    },
    "workflow": {
      "enum": ["voice-profile", "draft", "humanize", "improve", "localize", "agent-output-review"]
    },
    "locale": { "type": "string", "minLength": 1 },
    "content_type": { "type": "string", "minLength": 1 },
    "input_artifacts": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/definitions/selected" },
      "maxItems": 1
    },
    "source_artifacts": {
      "type": "array",
      "minItems": 0,
      "items": { "$ref": "#/definitions/artifact" },
      "maxItems": 32
    },
    "brief": { "anyOf": [{ "$ref": "#/definitions/artifact" }, { "type": "null" }] },
    "source_set_sha256": { "$ref": "#/definitions/sha256" },
    "voice_profile": {
      "type": "object",
      "properties": {
        "id": { "type": "string", "pattern": "^[a-z][a-z0-9-]{0,62}$" },
        "version": { "type": "integer", "minimum": 1 },
        "path": { "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }] },
        "sha256": { "$ref": "#/definitions/sha256" }
      },
      "required": ["id", "version", "path", "sha256"],
      "additionalProperties": false
    },
    "policy": { "$ref": "#/definitions/policy" },
    "limits": { "$ref": "#/definitions/limits" },
    "resource_artifacts": { "type": "array", "minItems": 1, "items": { "$ref": "#/definitions/artifact" } },
    "execution_requirements": {
      "type": "object",
      "properties": {
        "mode": { "$ref": "#/definitions/mode" },
        "backend": { "enum": ["codex-exec", null] },
        "reviewer_context": { "const": "separate" }
      },
      "required": ["mode", "backend", "reviewer_context"],
      "additionalProperties": false
    },
    "editable_scope": {
      "type": "object",
      "properties": {
        "input": { "type": "string", "minLength": 1 },
        "fields": { "type": "array", "minItems": 0, "items": { "type": "string", "minLength": 1 } },
        "output_directory": { "type": "string", "minLength": 1 }
      },
      "required": ["input", "fields", "output_directory"],
      "additionalProperties": false
    },
    "expected_outputs": {
      "type": "object",
      "properties": {
        "directory": { "type": "string", "minLength": 1 },
        "format": { "$ref": "#/definitions/format" }
      },
      "required": ["directory", "format"],
      "additionalProperties": false
    },
    "request": {
      "type": "object",
      "required": ["workflow", "input"],
      "properties": {
        "workflow": {
          "enum": ["voice-profile", "draft", "humanize", "improve", "localize", "agent-output-review"]
        },
        "input": { "type": "string", "minLength": 1 },
        "brief": { "type": "string", "minLength": 1 },
        "sources": { "type": "array", "minItems": 0, "items": { "type": "string", "minLength": 1 } },
        "profile": { "type": "string", "minLength": 1 },
        "locale": { "type": "string", "minLength": 1 },
        "source_locale": { "type": "string", "minLength": 1 },
        "mode": { "$ref": "#/definitions/mode" },
        "content_type": { "type": "string", "minLength": 1 },
        "fields": { "type": "array", "minItems": 0, "items": { "type": "string", "minLength": 1 } }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false,
  "definitions": {
    "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
    "artifact": {
      "type": "object",
      "properties": {
        "path": { "type": "string", "minLength": 1 },
        "sha256": { "$ref": "#/definitions/sha256" }
      },
      "required": ["path", "sha256"],
      "additionalProperties": false
    },
    "format": { "enum": ["text", "markdown", "json", "yaml"] },
    "selected": {
      "type": "object",
      "properties": {
        "path": { "type": "string", "minLength": 1 },
        "sha256": { "$ref": "#/definitions/sha256" },
        "format": { "$ref": "#/definitions/format" },
        "units": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "pattern": "^p[1-9][0-9]*$" },
          "uniqueItems": true
        }
      },
      "required": ["path", "sha256", "format", "units"],
      "additionalProperties": false
    },
    "mode": { "enum": ["manual", "host_policy", "orchestrated"] },
    "policy": {
      "type": "object",
      "properties": {
        "config_path": { "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }] },
        "config_sha256": { "anyOf": [{ "$ref": "#/definitions/sha256" }, { "type": "null" }] },
        "facts": { "enum": ["not_run", "provided-sources"] },
        "editable_fields": { "type": "array", "minItems": 0, "items": { "type": "string", "minLength": 1 } },
        "protected_spans": { "type": "array", "minItems": 0, "items": { "type": "string", "minLength": 1 } },
        "protected_terms": { "type": "array", "minItems": 0, "items": { "type": "string", "minLength": 1 } },
        "scope": {
          "type": "object",
          "properties": {
            "include_paths": { "type": "array", "minItems": 0, "items": { "type": "string", "minLength": 1 } },
            "exclude_paths": { "type": "array", "minItems": 0, "items": { "type": "string", "minLength": 1 } }
          },
          "required": ["include_paths", "exclude_paths"],
          "additionalProperties": false
        },
        "default_locale": { "type": "string", "minLength": 1 },
        "source_locale": { "type": "string", "minLength": 1 },
        "fidelity": { "const": "required" },
        "semantic_review": { "const": "required" },
        "reviewer_context": { "const": "separate" },
        "sha256": { "$ref": "#/definitions/sha256" }
      },
      "required": [
        "config_path",
        "config_sha256",
        "facts",
        "editable_fields",
        "protected_spans",
        "protected_terms",
        "scope",
        "default_locale",
        "source_locale",
        "fidelity",
        "semantic_review",
        "reviewer_context",
        "sha256"
      ],
      "additionalProperties": false
    },
    "limits": {
      "type": "object",
      "properties": {
        "max_revision_attempts": { "type": "integer", "minimum": 1, "maximum": 5 },
        "timeout_ms": { "type": "integer", "minimum": 1000, "maximum": 600000 },
        "max_output_bytes": { "type": "integer", "minimum": 1024, "maximum": 524288 }
      },
      "required": ["max_revision_attempts", "timeout_ms", "max_output_bytes"],
      "additionalProperties": false
    }
  }
}
