{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/OfflineGuideResult",
  "definitions": {
    "OfflineGuideResult": {
      "type": "object",
      "properties": {
        "taskId": {
          "type": "string"
        },
        "task": {
          "anyOf": [
            {
              "$ref": "#/definitions/SetupAgentsTaskRecord"
            },
            {
              "type": "null"
            }
          ]
        },
        "run": {
          "anyOf": [
            {
              "$ref": "#/definitions/WorkflowRunRecord"
            },
            {
              "type": "null"
            }
          ]
        },
        "phase": {
          "anyOf": [
            {
              "$ref": "#/definitions/WorkflowPhase"
            },
            {
              "type": "null"
            }
          ]
        },
        "steps": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "references": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "commands": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": ["taskId", "task", "run", "phase", "steps", "references", "commands"],
      "additionalProperties": false
    },
    "SetupAgentsTaskRecord": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "kind": {
          "type": "string",
          "const": "task"
        },
        "event": {
          "$ref": "#/definitions/SetupAgentsTaskEvent"
        },
        "summary": {
          "type": "string"
        },
        "status": {
          "$ref": "#/definitions/SetupAgentsTaskStatus"
        },
        "owner": {
          "$ref": "#/definitions/SetupAgentsProfileOwner"
        },
        "mode": {
          "$ref": "#/definitions/SetupAgentsWorkspaceMode"
        },
        "roles": {
          "$ref": "#/definitions/SetupAgentsTaskRoles"
        },
        "routing": {
          "$ref": "#/definitions/SetupAgentsProfileRoutingDecision"
        },
        "declaredPhases": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/WorkflowPhase"
          }
        },
        "phaseOverrides": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/PhaseOverride"
          }
        },
        "type": {
          "$ref": "#/definitions/SetupAgentsTaskType"
        },
        "parentId": {
          "type": "string"
        },
        "runId": {
          "type": "string",
          "description": "Structured workflow linkage for phase subtasks."
        },
        "phase": {
          "$ref": "#/definitions/WorkflowPhase"
        },
        "decompositionVotes": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/DecompositionVote"
          }
        },
        "reason": {
          "type": "string"
        },
        "createdAt": {
          "type": "string"
        },
        "updatedAt": {
          "type": "string"
        },
        "evidenceIds": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": ["id", "kind", "event", "summary", "status", "createdAt", "updatedAt", "evidenceIds"],
      "additionalProperties": false
    },
    "SetupAgentsTaskEvent": {
      "type": "string",
      "enum": ["created", "claimed", "done", "cancelled", "blocked", "updated", "deleted", "archived"]
    },
    "SetupAgentsTaskStatus": {
      "type": "string",
      "enum": ["open", "claimed", "done", "cancelled", "blocked", "deleted", "archived"]
    },
    "SetupAgentsProfileOwner": {
      "type": "string",
      "enum": [
        "setup-agents:developer",
        "setup-agents:architect",
        "setup-agents:sa",
        "setup-agents:pm",
        "setup-agents:mulesoft",
        "setup-agents:ux",
        "setup-agents:cgcloud",
        "setup-agents:qa",
        "setup-agents:crma",
        "setup-agents:commerce",
        "setup-agents:data360",
        "setup-agents:sfmc",
        "setup-agents:salescloud",
        "setup-agents:service",
        "setup-agents:cpq",
        "setup-agents:omnistudio",
        "setup-agents:fsl",
        "setup-agents:ai",
        "setup-agents:slack",
        "setup-agents:tableau",
        "setup-agents:experience-cloud",
        "setup-agents:release-manager",
        "setup-agents:fsc",
        "setup-agents:health",
        "setup-agents:manufacturing",
        "setup-agents:communications",
        "setup-agents:automotive",
        "setup-agents:education",
        "setup-agents:netzero",
        "setup-agents:nonprofit",
        "setup-agents:public-sector",
        "setup-agents:media",
        "setup-agents:energy",
        "setup-agents:revenue",
        "setup-agents:maps",
        "setup-agents:loyalty",
        "setup-agents:industries"
      ]
    },
    "SetupAgentsWorkspaceMode": {
      "type": "string",
      "enum": ["project", "advisory"]
    },
    "SetupAgentsTaskRoles": {
      "type": "object",
      "properties": {
        "required": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "optional": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": ["required", "optional"],
      "additionalProperties": false
    },
    "SetupAgentsProfileRoutingDecision": {
      "type": "object",
      "properties": {
        "status": {
          "type": "string",
          "enum": ["selected", "ambiguous", "no-match", "unavailable"]
        },
        "source": {
          "type": "string",
          "enum": ["inference", "explicit-override", "neutral"]
        },
        "ranked": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "profileId": {
                "$ref": "#/definitions/ProfileId"
              },
              "score": {
                "type": "number"
              },
              "matched": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "required": ["profileId", "score", "matched"],
            "additionalProperties": false
          }
        },
        "selectedProfile": {
          "anyOf": [
            {
              "$ref": "#/definitions/ProfileId"
            },
            {
              "type": "null"
            }
          ]
        },
        "suggestedRequiredProfiles": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/ProfileId"
          }
        },
        "suggestedOptionalProfiles": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/ProfileId"
          }
        },
        "acceptedProfiles": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/ProfileId"
          }
        },
        "candidateSpecializations": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": ["ta", "sa", "mulesoft", "informatica", "salesforce-release", "mulesoft-release"]
          }
        },
        "specializationQuestions": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "overrideRationale": {
          "type": "string"
        }
      },
      "required": [
        "status",
        "source",
        "ranked",
        "selectedProfile",
        "suggestedRequiredProfiles",
        "suggestedOptionalProfiles",
        "acceptedProfiles"
      ],
      "additionalProperties": false
    },
    "ProfileId": {
      "type": "string",
      "enum": [
        "developer",
        "architect",
        "sa",
        "pm",
        "mulesoft",
        "ux",
        "cgcloud",
        "qa",
        "crma",
        "commerce",
        "data360",
        "sfmc",
        "salescloud",
        "service",
        "cpq",
        "omnistudio",
        "fsl",
        "ai",
        "slack",
        "tableau",
        "experience-cloud",
        "release-manager",
        "fsc",
        "health",
        "manufacturing",
        "communications",
        "automotive",
        "education",
        "netzero",
        "nonprofit",
        "public-sector",
        "media",
        "energy",
        "revenue",
        "maps",
        "loyalty",
        "industries"
      ]
    },
    "WorkflowPhase": {
      "type": "string",
      "enum": ["pm", "ba", "architect", "ux", "developer", "security-review", "qa", "document-review", "release"]
    },
    "PhaseOverride": {
      "type": "object",
      "properties": {
        "phase": {
          "$ref": "#/definitions/WorkflowPhase"
        },
        "action": {
          "$ref": "#/definitions/PhaseOverrideAction"
        },
        "reason": {
          "type": "string"
        }
      },
      "required": ["phase", "action"],
      "additionalProperties": false
    },
    "PhaseOverrideAction": {
      "type": "string",
      "enum": ["skip", "add", "require-gate", "remove-gate"]
    },
    "SetupAgentsTaskType": {
      "type": "string",
      "enum": ["story", "epic", "subtask"]
    },
    "DecompositionVote": {
      "type": "object",
      "properties": {
        "voter": {
          "type": "string",
          "enum": ["ba", "ta"]
        },
        "verdict": {
          "type": "string",
          "enum": ["proceed", "decompose"]
        },
        "reason": {
          "type": "string"
        },
        "suggestedSplits": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": ["voter", "verdict", "reason"],
      "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."
    },
    "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"]
    }
  }
}
