{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://example.com/pipeline/plan-todos.schema.json",
  "title": "Plan Todo List",
  "description": "Structured representation of the Phase 1 plan. Persists into agent-state under `.plan.todos[]` and survives across phases. Phase 2 (Dev) iterates step-by-step; Phase 3 (Review) verifies completed steps against criteria; Phase 5 (Report) renders a per-step rollup. The plan is broken into a live, structured Todo list for step-by-step tracking across phases.",
  "type": "object",
  "additionalProperties": false,
  "required": ["title", "todos"],
  "properties": {
    "title": {
      "type": "string",
      "description": "One-line plan summary, mirrors the task title. Surfaced in Phase 2 progress lines and the Phase 5 report."
    },
    "createdAt": {
      "type": "string",
      "format": "date-time",
      "description": "ISO-8601 timestamp Phase 1 emitted this plan. Used by Phase 5 for elapsed-time math."
    },
    "todos": {
      "type": "array",
      "minItems": 1,
      "description": "Ordered list of steps. Phase 2 picks them in `deps`-respecting order  -  see plan-todos.sh next.",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id", "task", "status"],
        "properties": {
          "id": {
            "type": "string",
            "pattern": "^[tT][0-9]+$",
            "description": "Stable identifier: t1, t2 when written directly, T1, T2 when converted from a planning-output document. Used in dependency edges and progress lines."
          },
          "task": {
            "type": "string",
            "description": "Imperative one-liner  -  what gets done. Avoid 'investigate X' or 'consider Y'  -  Phase 2 needs concrete actions."
          },
          "status": {
            "type": "string",
            "enum": ["pending", "in_progress", "completed", "skipped", "failed"],
            "description": "Lifecycle. pending → in_progress → completed | failed | skipped. The helper script enforces transitions; manual edits to agent-state should still follow this graph."
          },
          "deps": {
            "type": "array",
            "default": [],
            "description": "Array of todo IDs that must reach `completed` (or `skipped`) before this one can move to `in_progress`. Empty for parallelizable steps.",
            "items": { "type": "string", "pattern": "^[tT][0-9]+$" }
          },
          "startedAt": { "type": "string", "format": "date-time" },
          "completedAt": { "type": "string", "format": "date-time" },
          "notes": {
            "type": "string",
            "description": "Free-text outcome  -  commit hash, test count, anything Phase 5 should echo."
          },
          "failureReason": { "type": "string" },
          "skipReason": { "type": "string" },
          "sourceTag": {
            "type": "string",
            "enum": ["Reuse", "Add new", "Modify"],
            "description": "Carried from the analysis Section 14 row this step came from (Locked 16). Phase 2 reads it as an instruction: a Reuse step binds the existing file and must not write a new one. Phase 3 checks the diff against it. Absent when the step did not originate from a Files-to-Add row."
          },
          "type": {
            "type": "string",
            "enum": ["test", "code", "refactor", "docs", "chore"],
            "description": "Carried from the planning-output task type. A docs or chore todo is exempt from the TDD evidence check (lib/tdd-evidence.mjs)."
          },
          "tdd": {
            "type": "object",
            "additionalProperties": false,
            "description": "TDD evidence, written by plan-todos.sh tdd-red / tdd-green / complete through lib/tdd-evidence.mjs. complete reads it according to prefs.global.tddEvidence.mode.",
            "properties": {
              "red": {
                "type": "object",
                "additionalProperties": false,
                "required": ["at", "failed", "total"],
                "description": "The run before the change: at least one failing test, or none when outcome is already-passing.",
                "properties": {
                  "at": { "type": "string", "format": "date-time" },
                  "failed": { "type": "integer", "minimum": 0 },
                  "total": { "type": "integer", "minimum": 1 },
                  "tests": {
                    "type": "array",
                    "items": { "type": "string" },
                    "description": "Failing test ids the todo named; every failing id when it named none; empty when the results carry no names."
                  },
                  "testsUnverifiable": {
                    "type": "boolean",
                    "description": "The todo named tests (--tests) but the results name no failing test, so the names went unchecked. Recorded in warn and off mode; enforce refuses the run instead."
                  },
                  "testFiles": {
                    "type": "array",
                    "maxItems": 200,
                    "description": "The worktree's new and changed test files at RED, each with the blob id its bytes were written to in the repository's object store. tdd-green refuses when one of them changed without --test-edit-reason; tdd-restore writes them back.",
                    "items": {
                      "type": "object",
                      "additionalProperties": false,
                      "required": ["path", "blob"],
                      "properties": {
                        "path": { "type": "string", "minLength": 1 },
                        "blob": { "type": "string", "pattern": "^[0-9a-f]{40,64}$" }
                      }
                    }
                  }
                }
              },
              "green": {
                "type": "object",
                "additionalProperties": false,
                "required": ["at", "failed", "total"],
                "description": "The run after the change, with no failures.",
                "properties": {
                  "at": { "type": "string", "format": "date-time" },
                  "failed": { "const": 0 },
                  "total": { "type": "integer", "minimum": 1 },
                  "testEdits": {
                    "type": "array",
                    "description": "Test files RED kept that GREEN found changed, accepted with the reason given to --test-edit-reason. test-integrity-gate --state reports them for review.",
                    "items": {
                      "type": "object",
                      "additionalProperties": false,
                      "required": ["path", "change", "reason"],
                      "properties": {
                        "path": { "type": "string", "minLength": 1 },
                        "change": { "type": "string", "enum": ["modified", "deleted"] },
                        "reason": { "type": "string", "minLength": 1 }
                      }
                    }
                  }
                }
              },
              "outcome": {
                "type": "string",
                "enum": ["red", "verified", "already-passing", "exempt", "unverified"],
                "description": "red: RED recorded. verified: RED then GREEN. already-passing: the behaviour existed and one passing run stands for both. exempt: no evidence needed. unverified: completed in warn mode without evidence; Phase 3 notes it."
              },
              "reason": {
                "type": "string",
                "description": "Why the outcome is already-passing, exempt or unverified."
              },
              "selfDeclared": {
                "type": "boolean",
                "description": "The exemption came from the todo itself (complete --no-tests), not from its type, the task type or the test policy. Phase 3 notes it."
              }
            }
          },
          "estimatedMinutes": {
            "type": "integer",
            "minimum": 1,
            "description": "Optional Phase 1 estimate for the step. Phase 5 compares against actual duration."
          },
          "files": {
            "type": "array",
            "items": { "type": "string" },
            "description": "The planning-output task's files[], carried by plan-todos.sh set. The drift check compares the files a step changed against it."
          },
          "creates": {
            "type": "array",
            "items": { "type": "string" },
            "description": "The planning-output task's creates[], carried by plan-todos.sh set. The drift check counts these files as planned, like files[]."
          },
          "drift": {
            "type": "object",
            "additionalProperties": false,
            "required": ["outside", "notDone", "at"],
            "description": "Set by plan-todos.sh drift when this step ended off-plan: the files it changed outside files[] and the number of planned items it did not do.",
            "properties": {
              "outside": { "type": "array", "items": { "type": "string" } },
              "notDone": { "type": "integer", "minimum": 0 },
              "at": { "type": "string", "format": "date-time" }
            }
          },
          "needsReplan": {
            "type": "object",
            "additionalProperties": false,
            "required": ["because", "at"],
            "description": "Set by plan-todos.sh drift on every pending step that depends, directly or transitively, on a step that drifted. Until confirmedAt is set, task-graph.mjs reports the step as waiting (reason needs-replan) and plan-todos.sh start refuses it; confirm-replan sets confirmedAt.",
            "properties": {
              "because": {
                "type": "string",
                "pattern": "^[tT][0-9]+$",
                "description": "The step that drifted."
              },
              "at": { "type": "string", "format": "date-time" },
              "reason": {
                "type": "string",
                "description": "What the drift was: files outside the plan, planned items not done, or a stated reason."
              },
              "confirmedAt": {
                "type": "string",
                "format": "date-time",
                "description": "When someone confirmed the step's plan still holds."
              },
              "note": { "type": "string", "description": "What the confirmation decided." }
            }
          }
        }
      }
    }
  }
}
