{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/multi-agent-ecosystem/multi-agent-pipeline/pipeline/schemas/phases.schema.json",
  "title": "Phase contract",
  "description": "phases.json: the phase set, the modes that run a subset of it and the phase numbers other code compares against.",
  "type": "object",
  "additionalProperties": false,
  "required": ["contractVersion", "phaseSchema", "phases", "totalMaxTokens", "modes", "thresholds"],
  "properties": {
    "$schema": {
      "type": "string"
    },
    "$id": {
      "type": "string"
    },
    "description": {
      "type": "string"
    },
    "contractVersion": {
      "type": "string",
      "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"
    },
    "phaseSchema": {
      "type": "integer",
      "minimum": 1
    },
    "phaseSchemaNote": {
      "type": "string"
    },
    "legacyPhaseNames": {
      "type": "object",
      "properties": {
        "note": {
          "type": "string"
        }
      },
      "patternProperties": {
        "^[0-9]+$": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    "phases": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id", "name", "doc", "maxTokens"],
        "properties": {
          "id": {
            "type": "integer",
            "minimum": 0
          },
          "name": {
            "type": "string",
            "minLength": 1
          },
          "doc": {
            "type": "string",
            "pattern": "^phase-[0-9]+-[a-z-]+\\.md$"
          },
          "maxTokens": {
            "type": "integer",
            "minimum": 1
          },
          "was": {
            "type": "array",
            "items": {
              "type": "integer",
              "minimum": 0
            }
          },
          "roles": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1
            },
            "description": "Words that name work done in this phase when written beside its number in prose (\"Phase 3 reviewer\"). A word listed under two phases is valid beside either. lint-phase-labels.mjs reads them."
          },
          "steps": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1
            },
            "description": "The phase's sub-steps, in order. phase-tracker.sh `add` registers each as a pending sub-phase numbered <id>.1, <id>.2, ..., so the widget carries them from the first boundary; the phase moves its own with `sub`. A phase whose sub-steps are computed at run time (Dev, which executes the plan's tasks) declares none."
          }
        }
      }
    },
    "totalMaxTokens": {
      "type": "integer",
      "minimum": 1
    },
    "modes": {
      "type": "object",
      "minProperties": 1,
      "additionalProperties": {
        "type": "object",
        "additionalProperties": false,
        "required": ["phases", "autopilot"],
        "properties": {
          "phases": {
            "type": "array",
            "minItems": 1,
            "items": {
              "type": "integer",
              "minimum": 0
            },
            "uniqueItems": true
          },
          "autopilot": {
            "type": "boolean"
          },
          "commits": {
            "type": "boolean",
            "description": "false when the mode delivers no commit in its Phase 4; lib/run-completion.mjs then asks for no commit evidence before status complete. Absent means the mode commits."
          },
          "names": {
            "type": "object",
            "additionalProperties": false,
            "patternProperties": {
              "^[0-9]$": {
                "type": "string",
                "pattern": "^[A-Z][A-Za-z]{1,20}$"
              }
            },
            "description": "The mode's own phase names by id, where they differ from the canonical ones (the analysis pipeline's Intake .. Report). Absent means the canonical names."
          }
        }
      }
    },
    "thresholds": {
      "type": "object",
      "additionalProperties": false,
      "required": ["waitingFromPhase", "mcpAllowedThroughPhase"],
      "properties": {
        "waitingFromPhase": {
          "type": "integer",
          "minimum": 0
        },
        "mcpAllowedThroughPhase": {
          "type": "integer",
          "minimum": 0
        },
        "note": {
          "type": "string"
        }
      }
    }
  }
}
