{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://deepworkplan.com/schema/plan-state/v5.json",
  "description": "Deep Work Plan live execution state, v5 line. Generation snapshot of the v2 shape for the v5 methodology line (DWP standard 5.0.0): no property was added, removed, or re-typed relative to plan-state/v2.json. Plans referencing v2.json (and v1.json) remain valid and are never rewritten.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema",
    "plan",
    "updated_at",
    "status",
    "completed_count",
    "task_count",
    "format",
    "materialization",
    "tasks"
  ],
  "properties": {
    "schema": {
      "const": "https://deepworkplan.com/schema/plan-state/v5.json"
    },
    "plan": {
      "type": "string",
      "pattern": "^PLAN_[a-z0-9]+(_[a-z0-9]+){1,4}$"
    },
    "updated_at": {
      "type": "string",
      "format": "date-time"
    },
    "updated_by": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "agent": {
          "type": "string",
          "maxLength": 100
        },
        "model": {
          "type": "string",
          "maxLength": 100
        }
      }
    },
    "status": {
      "enum": [
        "pending",
        "in_progress",
        "completed",
        "blocked"
      ]
    },
    "completed_count": {
      "type": "integer",
      "minimum": 0
    },
    "task_count": {
      "type": "integer",
      "minimum": 1
    },
    "format": {
      "enum": [
        "lite",
        "full"
      ]
    },
    "materialization": {
      "enum": [
        "materializing",
        "ready",
        "promoting"
      ]
    },
    "approval": {
      "enum": [
        "pending",
        "approved",
        "pre_approved"
      ]
    },
    "promotion": {
      "type": [
        "object",
        "null"
      ],
      "additionalProperties": false,
      "required": [
        "from",
        "to",
        "phase"
      ],
      "properties": {
        "from": {
          "const": "lite"
        },
        "to": {
          "const": "full"
        },
        "phase": {
          "enum": [
            "intent",
            "tasks_written",
            "switched"
          ]
        }
      }
    },
    "checkpoint": {
      "type": [
        "object",
        "null"
      ],
      "additionalProperties": false,
      "required": [
        "task",
        "at"
      ],
      "properties": {
        "task": {
          "type": "integer",
          "minimum": 1
        },
        "step": {
          "type": "string",
          "maxLength": 200
        },
        "at": {
          "type": "string",
          "format": "date-time"
        },
        "note": {
          "type": "string",
          "maxLength": 500
        }
      }
    },
    "blocked": {
      "type": [
        "object",
        "null"
      ],
      "additionalProperties": false,
      "required": [
        "task",
        "reason",
        "since"
      ],
      "properties": {
        "task": {
          "type": "integer",
          "minimum": 1
        },
        "reason": {
          "type": "string",
          "maxLength": 1000
        },
        "since": {
          "type": "string",
          "format": "date-time"
        },
        "needs": {
          "type": "string",
          "maxLength": 500
        }
      }
    },
    "tasks": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/task"
      }
    }
  },
  "$defs": {
    "task": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "locator",
        "title",
        "status",
        "gates"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "minimum": 1
        },
        "locator": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind",
            "value"
          ],
          "properties": {
            "kind": {
              "enum": [
                "inline",
                "file"
              ]
            },
            "value": {
              "type": "string",
              "pattern": "^(#task-[1-9][0-9]*|[1-9][0-9]*\\.task_[a-z0-9_]+\\.md)$"
            }
          },
          "allOf": [
            {
              "if": {
                "properties": {
                  "kind": {
                    "const": "inline"
                  }
                }
              },
              "then": {
                "properties": {
                  "value": {
                    "pattern": "^#task-[1-9][0-9]*$"
                  }
                }
              },
              "else": {
                "properties": {
                  "value": {
                    "pattern": "^[1-9][0-9]*\\.task_[a-z0-9_]+\\.md$"
                  }
                }
              }
            }
          ]
        },
        "title": {
          "type": "string"
        },
        "status": {
          "enum": [
            "pending",
            "in_progress",
            "completed",
            "blocked",
            "skipped"
          ]
        },
        "gates": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/gate"
          }
        },
        "started_at": {
          "type": "string",
          "format": "date-time"
        },
        "completed_at": {
          "type": "string",
          "format": "date-time"
        },
        "commit": {
          "type": "string",
          "pattern": "^[0-9a-f]{7,40}$"
        },
        "outcome": {
          "$ref": "#/$defs/outcome"
        }
      }
    },
    "gate": {
      "type": "object",
      "required": [
        "command",
        "passes",
        "last_run"
      ],
      "additionalProperties": false,
      "properties": {
        "command": {
          "type": "string",
          "maxLength": 500
        },
        "passes": {
          "type": "boolean"
        },
        "exit_code": {
          "type": "integer"
        },
        "last_run": {
          "type": "string",
          "format": "date-time"
        },
        "evidence": {
          "type": "string",
          "maxLength": 500
        }
      }
    },
    "outcome": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "tried": {
          "type": "array",
          "items": {
            "type": "string",
            "maxLength": 300
          }
        },
        "failed": {
          "type": "array",
          "items": {
            "type": "string",
            "maxLength": 300
          }
        },
        "worked": {
          "type": "string",
          "maxLength": 500
        },
        "notes": {
          "type": "string",
          "maxLength": 1000
        }
      }
    }
  }
}
