{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/SetupAgentsWorkflowRollbackResult",
  "definitions": {
    "SetupAgentsWorkflowRollbackResult": {
      "type": "object",
      "properties": {
        "run": {
          "$ref": "#/definitions/WorkflowRunRecord"
        },
        "targetPhase": {
          "$ref": "#/definitions/WorkflowPhase"
        },
        "reason": {
          "type": "string"
        },
        "file": {
          "type": "string"
        },
        "cwd": {
          "type": "string"
        }
      },
      "required": ["run", "targetPhase", "reason", "file", "cwd"],
      "additionalProperties": false
    },
    "WorkflowRunRecord": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "storyId": {
          "type": "string"
        },
        "gates": {
          "$ref": "#/definitions/WorkflowGateMode"
        },
        "maxIterations": {
          "type": "number"
        },
        "qaIterations": {
          "type": "number"
        },
        "deliveryMode": {
          "type": "string",
          "enum": ["manual", "autonomous-v1"],
          "description": "Versioned opt-in controller mode. Missing means the legacy manual workflow."
        },
        "autonomousSchemaVersion": {
          "type": "number",
          "const": 1
        },
        "policyVersion": {
          "type": "string"
        },
        "sourceDigest": {
          "type": "string"
        },
        "baseline": {
          "$ref": "#/definitions/WorkflowRunBaseline"
        },
        "verificationPlan": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/WorkflowLaneDisposition"
          }
        },
        "verificationHistory": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/WorkflowVerificationResult"
          }
        },
        "acceptanceEvidenceHistory": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/WorkflowAcceptanceEvidence"
          }
        },
        "correctionHistory": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/CorrectionPayload"
          }
        },
        "consecutiveNoProgress": {
          "type": "number"
        },
        "stopReason": {
          "type": "string",
          "enum": [
            "scope-architecture-gate",
            "final-release-gate",
            "required-lane-blocked",
            "attempts-exhausted",
            "no-progress",
            "stale-evidence"
          ]
        },
        "finalReport": {
          "$ref": "#/definitions/WorkflowFinalReport"
        },
        "invalidatedAutonomousHistory": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "invalidatedAt": {
                "type": "string"
              },
              "reason": {
                "type": "string"
              },
              "sourceDigest": {
                "type": "string"
              },
              "finalReport": {
                "$ref": "#/definitions/WorkflowFinalReport"
              },
              "verificationPlan": {
                "type": "array",
                "items": {
                  "$ref": "#/definitions/WorkflowLaneDisposition"
                }
              },
              "verificationHistory": {
                "type": "array",
                "items": {
                  "$ref": "#/definitions/WorkflowVerificationResult"
                }
              },
              "acceptanceEvidenceHistory": {
                "type": "array",
                "items": {
                  "$ref": "#/definitions/WorkflowAcceptanceEvidence"
                }
              },
              "correctionHistory": {
                "type": "array",
                "items": {
                  "$ref": "#/definitions/CorrectionPayload"
                }
              }
            },
            "required": ["invalidatedAt", "reason"],
            "additionalProperties": false
          }
        },
        "phases": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/WorkflowPhaseRecord"
          }
        },
        "status": {
          "type": "string",
          "enum": ["running", "paused", "done", "failed"]
        },
        "createdAt": {
          "type": "string"
        },
        "updatedAt": {
          "type": "string"
        },
        "rollbackReason": {
          "type": "string"
        },
        "pendingDelegatedPhase": {
          "type": "object",
          "properties": {
            "phase": {
              "$ref": "#/definitions/WorkflowPhase"
            },
            "spawnSessionId": {
              "type": "string"
            },
            "dispatchedAt": {
              "type": "string"
            }
          },
          "required": ["phase", "spawnSessionId", "dispatchedAt"],
          "additionalProperties": false,
          "description": "GH-748: set when `workflow run --autonomous` dispatched this phase to the bridge's async pipeline (chat session) instead of awaiting it inline — the same background dispatch `workflow execute` uses (GH-746), but this caller can't just fire-and-forget: it advances the run's phase/gate state synchronously off the phase's real result, so it pauses here and checks this spawn's real completion on the next `--resume` instead. Cleared once that spawn is observed completed (or failed)."
        }
      },
      "required": [
        "id",
        "storyId",
        "gates",
        "maxIterations",
        "qaIterations",
        "phases",
        "status",
        "createdAt",
        "updatedAt"
      ],
      "additionalProperties": false
    },
    "WorkflowGateMode": {
      "type": "string",
      "enum": ["none", "phase", "all"]
    },
    "WorkflowRunBaseline": {
      "type": "object",
      "properties": {
        "capturedAt": {
          "type": "string"
        },
        "sourceDigest": {
          "type": "string"
        },
        "files": {
          "type": "object",
          "additionalProperties": {
            "type": "string"
          }
        }
      },
      "required": ["capturedAt", "sourceDigest", "files"],
      "additionalProperties": false
    },
    "WorkflowLaneDisposition": {
      "type": "object",
      "properties": {
        "lane": {
          "$ref": "#/definitions/WorkflowVerificationLane"
        },
        "state": {
          "type": "string",
          "enum": ["selected", "excluded", "blocked"]
        },
        "ruleId": {
          "type": "string"
        },
        "reasonCode": {
          "type": "string"
        },
        "reason": {
          "type": "string"
        },
        "commandId": {
          "type": "string"
        }
      },
      "required": ["lane", "state", "ruleId", "reasonCode", "reason"],
      "additionalProperties": false
    },
    "WorkflowVerificationLane": {
      "type": "string",
      "enum": ["compile", "lint", "unit", "commands", "compiled-cli", "bridge", "nut", "playwright", "visual"]
    },
    "WorkflowVerificationResult": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string"
        },
        "command": {
          "type": "object",
          "properties": {
            "executable": {
              "type": "string"
            },
            "args": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          },
          "required": ["executable", "args"],
          "additionalProperties": false
        },
        "status": {
          "type": "string",
          "enum": ["passed", "failed", "blocked"]
        },
        "exitCode": {
          "type": "number"
        },
        "evidenceId": {
          "type": "string"
        },
        "output": {
          "type": "string"
        },
        "artifactPaths": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "startedAt": {
          "type": "string"
        },
        "completedAt": {
          "type": "string"
        },
        "assertions": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/WorkflowAssertionEvidence"
          }
        },
        "lane": {
          "$ref": "#/definitions/WorkflowVerificationLane"
        },
        "state": {
          "type": "string",
          "enum": ["selected", "excluded", "blocked"]
        },
        "ruleId": {
          "type": "string"
        },
        "reasonCode": {
          "type": "string"
        },
        "reason": {
          "type": "string"
        },
        "commandId": {
          "type": "string"
        },
        "runId": {
          "type": "string"
        },
        "iteration": {
          "type": "number"
        },
        "sourceDigest": {
          "type": "string"
        }
      },
      "required": [
        "artifactPaths",
        "completedAt",
        "id",
        "iteration",
        "lane",
        "reason",
        "reasonCode",
        "ruleId",
        "runId",
        "sourceDigest",
        "startedAt",
        "state",
        "status"
      ]
    },
    "WorkflowAssertionEvidence": {
      "type": "object",
      "properties": {
        "assertionId": {
          "type": "string"
        },
        "criterionId": {
          "type": "string"
        },
        "status": {
          "type": "string",
          "enum": ["passed", "failed"]
        },
        "message": {
          "type": "string"
        }
      },
      "required": ["assertionId", "criterionId", "status", "message"],
      "additionalProperties": false
    },
    "WorkflowAcceptanceEvidence": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string"
        },
        "criterionId": {
          "type": "string"
        },
        "criterion": {
          "type": "string"
        },
        "evidenceIds": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "assertion": {
          "type": "string"
        },
        "status": {
          "type": "string",
          "enum": ["verified", "failed", "missing"]
        },
        "runId": {
          "type": "string"
        },
        "iteration": {
          "type": "number"
        },
        "sourceDigest": {
          "type": "string"
        }
      },
      "required": [
        "assertion",
        "criterion",
        "criterionId",
        "evidenceIds",
        "id",
        "iteration",
        "runId",
        "sourceDigest",
        "status"
      ]
    },
    "CorrectionPayload": {
      "type": "object",
      "properties": {
        "blocker": {
          "type": "string",
          "description": "One-line description of what failed."
        },
        "severity": {
          "$ref": "#/definitions/CorrectionSeverity",
          "description": "Failure severity; high → auto-retry, low → prefer human escalation."
        },
        "location": {
          "type": "string",
          "description": "Optional `file:line` (or file) pointer to where the failure surfaced."
        },
        "suggestedFix": {
          "type": "string",
          "description": "Optional concrete remediation hint for the next attempt."
        },
        "evidenceIds": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "Evidence record ids backing this correction (test runs, logs, reports)."
        },
        "returnTarget": {
          "$ref": "#/definitions/WorkflowPhase",
          "description": "Phase that receives the correction. Autonomous verification always returns to Developer."
        },
        "identity": {
          "$ref": "#/definitions/EvidenceIdentity",
          "description": "Immutable autonomous evidence identity."
        },
        "fingerprint": {
          "type": "string",
          "description": "Stable fingerprint used for convergence comparisons."
        },
        "status": {
          "type": "string",
          "enum": ["open", "resolved"]
        }
      },
      "required": ["blocker", "severity"],
      "additionalProperties": false,
      "description": "Structured failure detail produced when a phase fails (e.g. QA), recorded on the phase record and injected into the re-entered phase's context so the next attempt sees the specific blocker — not a free-text \"qa-fail\" note (GH-526)."
    },
    "CorrectionSeverity": {
      "type": "string",
      "enum": ["low", "medium", "high"],
      "description": "Severity of a phase failure — drives auto-retry vs human-escalation routing."
    },
    "WorkflowPhase": {
      "type": "string",
      "enum": ["pm", "ba", "architect", "ux", "developer", "security-review", "qa", "document-review", "release"]
    },
    "EvidenceIdentity": {
      "type": "object",
      "properties": {
        "runId": {
          "type": "string"
        },
        "iteration": {
          "type": "number"
        },
        "sourceDigest": {
          "type": "string"
        }
      },
      "required": ["runId", "iteration", "sourceDigest"],
      "additionalProperties": false
    },
    "WorkflowFinalReport": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "schemaVersion": {
          "type": "number",
          "const": 1
        },
        "policyVersion": {
          "type": "string"
        },
        "iterations": {
          "type": "number"
        },
        "findingsDiscovered": {
          "type": "number"
        },
        "findingsResolved": {
          "type": "number"
        },
        "acceptanceCriteriaVerified": {
          "type": "number"
        },
        "acceptanceCriteriaTotal": {
          "type": "number"
        },
        "acceptanceEvidence": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/WorkflowAcceptanceEvidence"
          }
        },
        "lanePlan": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/WorkflowLaneDisposition"
          }
        },
        "commands": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/WorkflowVerificationResult"
          }
        },
        "visualEvidence": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "viewport": {
                "type": "string"
              },
              "evidenceIds": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "required": ["viewport", "evidenceIds"],
            "additionalProperties": false
          }
        },
        "residualRisks": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "recommendation": {
          "type": "string",
          "enum": ["GO", "NO-GO"]
        },
        "stopReason": {
          "type": "string",
          "enum": [
            "scope-architecture-gate",
            "final-release-gate",
            "required-lane-blocked",
            "attempts-exhausted",
            "no-progress",
            "stale-evidence"
          ]
        },
        "runId": {
          "type": "string"
        },
        "iteration": {
          "type": "number"
        },
        "sourceDigest": {
          "type": "string"
        }
      },
      "required": [
        "acceptanceCriteriaTotal",
        "acceptanceCriteriaVerified",
        "acceptanceEvidence",
        "commands",
        "findingsDiscovered",
        "findingsResolved",
        "iteration",
        "iterations",
        "lanePlan",
        "policyVersion",
        "recommendation",
        "residualRisks",
        "runId",
        "schemaVersion",
        "sourceDigest",
        "visualEvidence"
      ]
    },
    "WorkflowPhaseRecord": {
      "type": "object",
      "properties": {
        "phase": {
          "$ref": "#/definitions/WorkflowPhase"
        },
        "status": {
          "$ref": "#/definitions/WorkflowPhaseStatus"
        },
        "taskId": {
          "type": "string"
        },
        "handoffId": {
          "type": "string"
        },
        "reviewIds": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "clarificationIds": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "startedAt": {
          "type": "string"
        },
        "completedAt": {
          "type": "string"
        },
        "notes": {
          "type": "string"
        },
        "correction": {
          "$ref": "#/definitions/CorrectionPayload",
          "description": "Structured failure detail recorded when this phase failed (GH-526)."
        }
      },
      "required": ["phase", "status", "taskId", "startedAt"],
      "additionalProperties": false
    },
    "WorkflowPhaseStatus": {
      "type": "string",
      "enum": ["pending", "running", "done", "blocked", "skipped", "awaiting_clarification"]
    }
  }
}
